Browse Source

Add the possibility to use plugins assigned in `window`

pull/1382/head
Artur Arseniev 8 years ago
parent
commit
7a7f71ee1a
  1. 17
      src/index.js
  2. 12
      test/specs/grapesjs/index.js

17
src/index.js

@ -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';
@ -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`);
}

12
test/specs/grapesjs/index.js

@ -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 = '';

Loading…
Cancel
Save