Skip to content

Commit

Permalink
Add the possibility to use plugins assigned in window
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Aug 23, 2018
1 parent b2bef71 commit 7a7f71e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import $ from 'cash-dom';
import Editor from './editor';
import { isElement } from 'underscore';
import { isElement, isFunction } from 'underscore';
import polyfills from 'utils/polyfills';
import PluginManager from './plugin_manager';

Expand Down Expand Up @@ -54,12 +54,19 @@ module.exports = (() => {

// Load plugins
config.plugins.forEach(pluginId => {
const plugin = plugins.get(pluginId);
let plugin = plugins.get(pluginId);
const plgOptions = config.pluginsOpts[pluginId] || {};

// Try to search in global context
if (!plugin) {
const wplg = window[pluginId];
plugin = wplg && wplg.defaults ? wplg.defaults : wplg;
}

if (plugin) {
plugin(editor, config.pluginsOpts[pluginId] || {});
} else if (typeof pluginId === 'function') {
pluginId(editor, config.pluginsOpts[pluginId] || {});
plugin(editor, plgOptions);
} else if (isFunction(pluginId)) {
pluginId(editor, plgOptions);
} else {
console.warn(`Plugin ${pluginId} not found`);
}
Expand Down
12 changes: 12 additions & 0 deletions test/specs/grapesjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ describe('GrapesJS', () => {
expect(editor.customValue).toEqual('TEST');
});

test('Use plugins defined on window, with custom options', () => {
window.globalPlugin = (edt, opts) => {
var opts = opts || {};
edt.customValue = opts.cVal || '';
};
config.plugins = ['globalPlugin'];
config.pluginsOpts = {};
config.pluginsOpts['globalPlugin'] = { cVal: 'TEST' };
var editor = obj.init(config);
expect(editor.customValue).toEqual('TEST');
});

test('Execute custom command', () => {
var editor = obj.init(config);
editor.testVal = '';
Expand Down

0 comments on commit 7a7f71e

Please sign in to comment.