[FEATURE REQUEST] Promise returning plugins
Question
Hi @artf, I have a case where a custom plugin needs to check user permissions before doing any kind of initialization. The method that does the check returns a Promise, so I have to wait for it to resolve before doing anything. Unfortunately, this creates a raise condition with editor rendering and UI gets into some weird state. In order to solve this issue I have to set 'editor.autorender' to false and then render editor manually once my plugin is initialized. This is fine for now, but I'm not quite sure how I would handle this case if I had multiple custom plugins depending on some async logic.
So, it would be really nice if GrapesJS could check if any of the registered plugins return Promises and only render the editor when all of them are resolved.
Thank you for you awesome project.
Answers (3)
I had a similar requirement. If it helps others, my solution in the end was to create a plugin which then loads my other plugins;
The editor config would just reference the one plugin and the plugin options for that plugin would contain the plugins and options for the others to load.
The plugin code;
export default grapesjs.plugins.add("async-plugins", async (editor, opts) => {
/**
* opts = {
* plugins: [ ... ]
* pluginsOpts: { ... },
* render: true | false, // If not set this will default to true.
* onFinished: async (editor) => {
* // When the async plugin has finished.
* },
* };
*
* If using the 'render' here, make sure to set 'autorender' to false in the editor config.
*/
// Load the specified plugins.
for (let i = 0, len = opts.plugins.length; i < len; i++) {
const pluginId = opts.plugins[i];
if (!opts.pluginsOpts) {
opts.pluginsOpts = {};
}
// Attempt to get the plugin if it has been registered.
let plugin = grapesjs.plugins.get(pluginId);
if (!plugin) {
const wplg = window[pluginId];
plugin = wplg && wplg.default ? wplg.default : wplg;
}
// Attempt to execute the plugin using 'await'.
if (plugin) {
await plugin(editor, opts.pluginsOpts[pluginId] || {});
}
else if (pluginId instanceof Function) {
await pluginId(editor, {});
}
else {
console.warn(`Failed to load plugin ${pluginId}.`);
}
}
// Render the editor if this plugin has been configured to do so.
if (opts.render === undefined || opts.render === true) {
editor.render();
}
// If there is an 'onFinished' callback, call it.
if (opts.onFinished) {
await opts.onFinished(editor);
}
});
The editor init code;
grapesjs.init({
...
autorender: false,
plugins: [ 'async-plugins' ],
pluginsOpts: {
'async-plugins': {
plugins: [ ... ],
pluginsOpts: { ... },
render: true,
onFinished: async (editor) => {
console.log("FINISHED");
},
},
},
...
});
Also, this may require GrapesJS' init method to return a Promise instead of the editor, but this can be limited only to cases when there are any plugins that return Promises.
I have to set 'editor.autorender' to false and then render editor manually once my plugin is initialized. This is fine for now, but I'm not quite sure how I would handle this case if I had multiple custom plugins depending on some async logic
Just use a variable as a flag which counts your custom execution logic and once it's finished you're ready to render it. IMHO, the solution with Promises it's absolutely overwhelmed for such a thing
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3265
RenderField in traitView does not handle null or undefined
Hi @artf! Working on the editor on one of my custom traits i had an issue where for some reason that is not important, the createInput was...
Issue #1469
Check canvas event
Hi @artf, Is there any common method where we can know something updated on canvas. We want to create a custom undo manager and also want t...
Issue #355
Custom plugins does not overrides default commands
@artf , Until versions 0.9.x , GrapesJS loads all it's internal's modules before all the plugins are initialized. Since from version 0.10.2...
Issue #1697
Implement TinyMCE and disable RTE
I'm trying to apply tinyMCE as a replacement for RTE, but I'm doing an impossible task, I'm following the same steps of the plugin CKEDITOR...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.