diff --git a/src/config/require-config.js b/src/config/require-config.js index b55d859de..cd8687ad0 100644 --- a/src/config/require-config.js +++ b/src/config/require-config.js @@ -37,6 +37,7 @@ require.config({ { name: 'StyleManager', location: 'style_manager', }, { name: 'ClassManager', location: 'class_manager', }, { name: 'StorageManager', location: 'storage_manager', }, + { name: 'PluginManager', location: 'plugin_manager', }, { name: 'Navigator', location: 'navigator', }, { name: 'DomComponents', location: 'dom_components', }, { name: 'RichTextEditor', location: 'rich_text_editor', }, diff --git a/src/grapesjs/config/config.js b/src/grapesjs/config/config.js index d5b60b957..12a8fda85 100644 --- a/src/grapesjs/config/config.js +++ b/src/grapesjs/config/config.js @@ -9,7 +9,7 @@ define(function () { // CSS string or object of rules style: '', - // If true, will fetch HTML and CSS from selected element + // If true, will fetch HTML and CSS from selected container fromElement: false, // Enable/Disable the possibility to copy(ctrl + c) & paste(ctrl + v) components @@ -23,6 +23,7 @@ define(function () { //Indicates which storage to use. Available: local | remote | none storageType: 'local', // (!) + // More correct storage:{ id: '', type: '', diff --git a/src/grapesjs/main.js b/src/grapesjs/main.js index e69de29bb..f57fce5bd 100644 --- a/src/grapesjs/main.js +++ b/src/grapesjs/main.js @@ -0,0 +1,52 @@ +define(function(require) { + + return function(config) { + + var c = config || {}, + defaults = require('./config/config'); + + // Set default options + for (var name in defaults) { + if (!(name in c)) + c[name] = defaults[name]; + } + + var plugins = []; + /* + .plugins will be PluginManager + grapesjs.plugins.add('sheeet', function(editor){ + editor.commands.add('openbar', function(){}); + }); + */ + var editors = []; + + return { + + plugins: plugins, + + /** + * Initializes an editor based on passed options + * @param {Object} config Configuration object + * @param {string} config.container Selector which indicates where render the editor + * @param {Object|string} config.components='' HTML string or Component model in JSON format + * @param {Object|string} config.style='' CSS string or CSS model in JSON format + * @param {Boolean} [config.copyPaste=true] Enable/Disable the possibility to copy(ctrl+c) & paste(ctrl+v) components + * @param {Boolean} [config.undoManager=true] Enable/Disable undo manager + * @param {Array} [config.plugins=[]] Array of plugins to execute on start + * @return {grapesjs.Editor} GrapesJS Editor instance + */ + init: function(config) { + // + var editor = new Editor(config); + //- new EditorView({model: editor}).render(); + //- inject and start plugins (plugins) + // foreach config.plugins + // pluginManager.get('plugin')(editor); + // + return editor; + }, + + }; + }; + +}); \ No newline at end of file diff --git a/src/plugin_manager/config/config.js b/src/plugin_manager/config/config.js new file mode 100644 index 000000000..10057f450 --- /dev/null +++ b/src/plugin_manager/config/config.js @@ -0,0 +1,7 @@ +define(function () { + return { + + plugins: [] + + }; +}); \ No newline at end of file diff --git a/src/plugin_manager/main.js b/src/plugin_manager/main.js new file mode 100644 index 000000000..cc32983ab --- /dev/null +++ b/src/plugin_manager/main.js @@ -0,0 +1,59 @@ +define(function(require) { + + return function(config) { + + var c = config || {}, + defaults = require('./config/config'); + + // Set default options + for (var name in defaults) { + if (!(name in c)) + c[name] = defaults[name]; + } + + var plugins = {}; + + return { + + /** + * Add new plugin. Plugins could not be overwritten + * @param {string} id Plugin ID + * @param {Function} plugin Function which contains all plugin logic + * @example + * PluginManager.add('some-plugin', function(editor){ + * editor.Commands.add('new-command', { + * run: function(editor, senderBtn){ + * console.log('Executed new-command'); + * } + * }) + * }); + */ + add: function(id, plugin){ + plugins[id] = plugin; + return this; + }, + + /** + * Returns plugin by ID + * @param {string} id Plugin ID + * @return {Function|undefined} Plugin + * @example + * var plugin = PluginManager.get('some-plugin'); + * plugin(editor); + */ + get: function(id){ + return plugins[id]; + }, + + /** + * Returns object with all plugins + * @return {Object} + */ + getAll: function(){ + return plugins; + } + + }; + }; + +}); \ No newline at end of file diff --git a/test/runner/main.js b/test/runner/main.js index 7282656c7..76d5a2464 100644 --- a/test/runner/main.js +++ b/test/runner/main.js @@ -19,6 +19,7 @@ require(['../src/config/require-config.js', 'config/config.js'], function() { 'specs/commands/main.js', 'specs/style_manager/main.js', 'specs/storage_manager/main.js', + 'specs/plugin_manager/main.js', 'specs/parser/main.js', 'specs/utils/main.js' ], function(chai) diff --git a/test/specs/plugin_manager/main.js b/test/specs/plugin_manager/main.js new file mode 100644 index 000000000..900930d82 --- /dev/null +++ b/test/specs/plugin_manager/main.js @@ -0,0 +1,39 @@ +var modulePath = './../../../test/specs/plugin_manager'; + +define(['PluginManager'], function(PluginManager) { + + describe('PluginManager', function() { + + describe('Main', function() { + + var obj; + var val; + var testPlugin = function(e){ + val = e; + }; + + beforeEach(function () { + obj = new PluginManager(); + }); + + afterEach(function () { + delete obj; + }); + + it('Object exists', function() { + obj.should.be.exist; + }); + + it('No plugins inside', function() { + obj.getAll().should.be.empty; + }); + + it('Add new plugin', function() { + obj.add('test', testPlugin); + obj.get('test').should.not.be.empty; + }); + + }); + + }); +}); \ No newline at end of file