From df282c10cfd2eb666dc822450b34c4bee9dd0a31 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 18 Nov 2017 02:02:02 +0100 Subject: [PATCH] Allow defining a command with just a function --- src/commands/index.js | 23 ++++++++++++++++++++--- src/editor/index.js | 26 -------------------------- src/storage_manager/index.js | 16 +--------------- 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/src/commands/index.js b/src/commands/index.js index b0922da9a..d0b6429b4 100644 --- a/src/commands/index.js +++ b/src/commands/index.js @@ -38,6 +38,7 @@ * }, * ... */ +import { isFunction } from 'underscore'; module.exports = () => { var c = {}, @@ -48,6 +49,10 @@ module.exports = () => { // Need it here as it would be used below var add = function(id, obj){ + if (isFunction(obj)) { + obj = { run: obj }; + } + delete obj.initialize; commands[id] = AbsCommands.extend(obj); return this; @@ -193,6 +198,14 @@ module.exports = () => { }, }; + // Core commands + defaultCommands['core:undo'] = e => e.UndoManager.undo(1); + defaultCommands['core:redo'] = e => e.UndoManager.redo(1); + defaultCommands['core:canvas-clear'] = e => { + e.DomComponents.clear(); + e.CssComposer.clear(); + }; + if(c.em) c.model = c.em.get('Canvas'); @@ -204,16 +217,20 @@ module.exports = () => { /** * Add new command to the collection * @param {string} id Command's ID - * @param {Object} command Object representing you command. Methods `run` and `stop` are required + * @param {Object|Function} command Object representing your command, + * By passing just a function it's intended as a stateless command + * (just like passing an object with only `run` method). * @return {this} * @example * commands.add('myCommand', { - * run: function(editor, sender){ + * run(editor, sender) { * alert('Hello world!'); * }, - * stop: function(editor, sender){ + * stop(editor, sender) { * }, * }); + * // As a function + * commands.add('myCommand2', editor => { ... }); * */ add, diff --git a/src/editor/index.js b/src/editor/index.js index ae7c90166..3c313396e 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -1,29 +1,4 @@ /** - * - * * [getConfig](#getconfig) - * * [getHtml](#gethtml) - * * [getCss](#getcss) - * * [getJs](#getjs) - * * [getComponents](#getcomponents) - * * [setComponents](#setcomponents) - * * [addComponents](#addcomponents) - * * [getStyle](#getstyle) - * * [setStyle](#setstyle) - * * [getSelected](#getselected) - * * [getSelectedToStyle](#getselectedtostyle) - * * [setDevice](#setdevice) - * * [getDevice](#getdevice) - * * [runCommand](#runcommand) - * * [stopCommand](#stopcommand) - * * [store](#store) - * * [load](#load) - * * [getContainer](#getcontainer) - * * [refresh](#refresh) - * * [on](#on) - * * [off](#off) - * * [trigger](#trigger) - * * [render](#render) - * * Editor class contains the top level API which you'll probably use to custom the editor or extend it with plugins. * You get the Editor instance on init method * @@ -75,7 +50,6 @@ * * `canvasScroll` - Triggered when the canvas is scrolle * * `load` - When the editor is loaded * - * @module Editor * @param {Object} config Configurations * @param {string} config.container='' Selector for the editor container, eg. '#myEditor' * @param {string|Array} [config.components=''] HTML string or object of components diff --git a/src/storage_manager/index.js b/src/storage_manager/index.js index cfc80185b..2c675942c 100644 --- a/src/storage_manager/index.js +++ b/src/storage_manager/index.js @@ -1,24 +1,9 @@ /** - * - [isAutosave](#isautosave) - * - [setAutosave](#setautosave) - * - [getStepsBeforeSave](#getstepsbeforesave) - * - [setStepsBeforeSave](#setstepsbeforesave) - * - [getStorages](#getstorages) - * - [getCurrent](#getcurrent) - * - [setCurrent](#setcurrent) - * - [add](#add) - * - [get](#get) - * - [store](#store) - * - [load](#load) - * - * * Before using methods you should get first the module from the editor instance, in this way: * * ```js * var storageManager = editor.StorageManager; * ``` - * - * @module StorageManager */ module.exports = () => { var c = {}, @@ -47,6 +32,7 @@ module.exports = () => { * @param {number} [config.stepsBeforeSave=1] If autosave enabled, indicates how many steps/changes are necessary * before autosave is triggered * @param {string} [config.type='local'] Default storage type. Available: 'local' | 'remote' | ''(do not store) + * @private * @example * ... * {