JS API の使用

Discourse の JavaScript API を使用すると、テーマやプラグインでユーザーエクスペリエンスを大幅にカスタマイズできます。これを使用する最も簡単な方法は、管理パネルから新しいテーマを作成し、「コードの編集」をクリックしてから、JS タブに移動することです。

ファイルベースのテーマの場合、API は api-initializers ディレクトリにファイルを作成することで使用できます。テーマの場合は {theme}/javascripts/api-initializers/init-theme.gjs、プラグインの場合は {plugin}/assets/javascripts/discourse/api-initializers/init-plugin.js となります。内容は次のとおりです。

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  // ここにコードを記述
});

利用可能なすべての API は、Discourse コアの plugin-api.gjs ソースコード に、簡単な説明と例とともにリストされています。

JS API の使用例を含む完全なチュートリアルについては、以下を確認してください。


このドキュメントはバージョン管理されています。変更の提案はgithubで行ってください。

「いいね!」 39

What’s the best way to “import” as a Site Customisation? - is it just to use require?

Whilst this works:

<script type="text/discourse-plugin" version="0.1">
var HamburgerMenuComponent = require('discourse/components/hamburger-menu').default;
</script>

This does not:

<script type="text/discourse-plugin" version="0.1">
import {default as HamburgerMenuComponent2 } from 'discourse/components/hamburger-menu';

</script>

Where I get this error:

<script type="text/discourse-js-error">unknown: 'import' and 'export' may only appear at the top level (3:0)
  1 | Discourse._registerPluginCode('0.1', api => {
  2 |   
> 3 | import {default as HamburgerMenuComponent2 } from 'discourse/components/hamburger-menu';
    | ^
  4 | 
  5 | 
  6 | }); at <eval>:8695:14</script>
「いいね!」 2

Instead of importing it, you can look it up with

api.container.lookupFactory('component:hamburger-menu')
「いいね!」 5

Is there a way to get access to the h helper from virtual-dom in a site customization? I’m trying to add a simple dropdown widget to use in our header, and I can’t get that darned h, even though I can get createWidget

Does:

h = require('virtual-dom').h;

not work?

「いいね!」 2

Yes it does! Works perfectly, thanks a ton!

I wouldn’t recommend doing it that way as it would likely break future compatibility. You can use this for now but I’ll try to introduce a workaround shortly that will be safer long term.

「いいね!」 5

Makes sense, it’s kinda circumventing the whole Plugin API thing and relying on implementation details of the ES6 compilation output, both things that are Dangerous™

Anyways, I’ll definitely keep an eye out for a better solution

I’ll try to get to it soon and I’ll reply in this topic, so watch it and you’ll see :slight_smile:

「いいね!」 4

Actually thinking about it, the decorateWidget helper gets called with an object that has the h method. How are you inserting your widget if not via a decorator?

If you could post code that would be helpful.

{{mount-widget}} in a template for a plugin outlet.

「いいね!」 1

Ah that’s clever – I didn’t think people would try that. Okay, let me try something out.

Okay, I’ve added h to the pluginApi object as long as you request plugin api v0.3:

<script type="text/discourse-plugin" version="0.3">
  console.log(api.h('b', ['hello', 'world']));
</script>

That should work for you!

「いいね!」 9

[quote=“Discourse, post:1, topic:41281”]
`

「いいね!」 1

こちらと他の多くのバージョン管理されたドキュメントを更新しました。お知らせありがとうございます @NateDhaliwal

「いいね!」 2

このリンクは機能しなくなりました。次のメッセージが表示されます。

discourseのmainブランチには、app/assets/javascripts/discourse/app/lib/plugin-api.gjsのパスが含まれていません。

「いいね!」 2

これは、javascript ファイルが app/assets/javascripts/ ではなく frontend/ ディレクトリに移動されたためです。

PR を開きました。

「いいね!」 4

このPRを再浮上させます。レビューしていただけますか?ありがとうございます!

「いいね!」 1