Skip to content

Commit 63e468d

Browse files
committed
Registering the plugin
1 parent f0296c3 commit 63e468d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

DevDocs/DeveloperNotes/GettingStarted/NewEditorPlugin.md

+65
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,70 @@ into the document.
4040

4141
## Step 2: Define a new TinyMCE plugin
4242

43+
### Step 2.1: Registering a plugin
44+
Lets define a script in `src/main/webapp/ui/tinyMCE/plugins/newPlugin/index.js`,
45+
inside of which we will call `tinymce.PluginManager.add("newPlugin", NewPlugin);`,
46+
where `NewPlugin` is a class. This class doesn't need to have any particular
47+
methods, it just needs to have a constructor that takes as its argument a
48+
reference to the editor. The class will be instantiated and the constructor
49+
called when the user goes to edit the rich text field and a new instance of
50+
TinyMCE is created, so whilst `tinymce.PluginManager.add("newPlugin", NewPlugin);`
51+
is to be called on page-load the class will be instantiated for each text field
52+
as it transitions from the viewing to editing modes.
53+
54+
Inside this constructor, we want to set up handles for the insert menu, toolbar
55+
icon, and the slash menu. To do this we call the following code
56+
```
57+
editor.ui.registry.addMenuItem('optNewPlugin', {
58+
text: 'New Plugin',
59+
icon: 'newPlugin',
60+
onAction: () => {
61+
// do something
62+
}
63+
});
64+
editor.ui.registry.addButton('newPlugin', {
65+
tooltip: 'New Plugin',
66+
icon: 'newPlugin',
67+
onAction: () => {
68+
// do something
69+
}
70+
});
71+
if (!window.insertActions) window.insertActions = new Map();
72+
window.insertActions.set('optNewPlugin', {
73+
text: 'New Plugin',
74+
icon: 'newPlugin',
75+
onAction: () => {
76+
// do something
77+
}
78+
});
79+
```
80+
In each case, the `onAction` callback is where the actual logic for the plugin
81+
should go. In most cases, this will involve displaying a dialog, making some API
82+
calls, and having the user make a selection before generating HTML to display,
83+
but before we get to that there are a few more initialisation steps to take care
84+
of.
85+
1. In `src/main/webapp/ui/webpack.config.js` we need to add a new entry point for our new script.
86+
We load the plugin through a separate entry point so that it is only downloaded
87+
when the plugin is enabled.
88+
```
89+
tinymceNewPlugin: "./src/tinyMCE/newPlugin/index.js",
90+
```
91+
2. In `src/main/webapp/scripts/pages/workspace/editor/tinymce5_configuration.js`,
92+
we need to add the following code:
93+
* Check whether the plugin is enabled with
94+
```
95+
const newPluginEnabled = integrations.NEW_PLUGIN.enabled && integrations.NEW_PLUGIN.available;
96+
```
97+
* Register the new plugin's source file and to tell TinyMCE to show the
98+
toolbar button and menu item when they are registered.
99+
```
100+
if (newPluginEnabled) {
101+
localTinymcesetup.external_plugins["newPlugin"] = "/ui/dist/tinymceNewPlugin.js";
102+
addToToolbarIfNotPresent(localTinymcesetup, " | newPlugin");
103+
addToMenuIfNotPresent(localTinymcesetup, " | optNewPlugin");
104+
}
105+
```
106+
3. A simple SVG needs to be added to `src/main/webapp/scripts/tinymce/tinymce516/icons/custom_icons/icons.js`
107+
43108
## Step 3: Testing
44109
- How will we know if the plugin stops working e.g. due to API changes/services being down?

0 commit comments

Comments
 (0)