Utilisation de l'API JS

L’API JavaScript de Discourse permet aux thèmes et aux plugins d’effectuer des personnalisations étendues de l’expérience utilisateur. La manière la plus simple de l’utiliser est de créer un nouveau thème depuis le panneau d’administration, de cliquer sur « Modifier le code », puis d’aller à l’onglet JS.

Pour les thèmes basés sur des fichiers, l’API peut être utilisée en créant un fichier dans le répertoire api-initializers. Pour les thèmes, c’est {theme}/javascripts/api-initializers/init-theme.gjs, et pour les plugins, c’est {plugin}/assets/javascripts/discourse/api-initializers/init-plugin.js. Le contenu doit être :

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

export default apiInitializer((api) => {
  // Votre code ici
});

Toutes les API disponibles sont répertoriées dans le code source de plugin-api.gjs dans le cœur de Discourse, ainsi qu’une brève description et des exemples.

Pour un tutoriel complet, y compris des exemples d’utilisation de l’API JS, consultez :


Ce document est contrôlé par version - suggérez des modifications sur github.

39 « J'aime »

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 « J'aime »

Instead of importing it, you can look it up with

api.container.lookupFactory('component:hamburger-menu')
5 « J'aime »

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 « J'aime »

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 « J'aime »

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 « J'aime »

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 « J'aime »

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 « J'aime »

Ceci est maintenant obsolète à partir de :

1 « J'aime »

Mis à jour ici, et dans un tas d’autres documents avec gestion de version. Merci de l’info @NateDhaliwal

2 « J'aime »

Ce lien ne fonctionne plus, j’obtiens ce message :

La branche main de discourse ne contient pas le chemin app/assets/javascripts/discourse/app/lib/plugin-api.gjs.

2 « J'aime »

Ah, c’est parce que les fichiers javascript ont été déplacés dans le répertoire frontend/ au lieu de app/assets/javascripts/.

J’ai ouvert une PR :

4 « J'aime »

Remontée de cette PR. Pourrait-elle être examinée ? Merci !

1 « J'aime »