From c4c472ea324385df83a2bf752828717cfe6c9960 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Mon, 18 Dec 2017 23:30:44 +0100 Subject: [PATCH] Create `core:copy` and `core:paste`, commands and keymaps --- src/commands/index.js | 22 +++++++++++++++++++++- src/commands/view/SelectComponent.js | 8 +++----- src/editor/index.js | 1 - src/grapesjs/index.js | 1 - src/keymaps/index.js | 8 ++++++++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/commands/index.js b/src/commands/index.js index acc613466..9467924bd 100644 --- a/src/commands/index.js +++ b/src/commands/index.js @@ -41,6 +41,7 @@ import { isFunction } from 'underscore'; module.exports = () => { + let em; var c = {}, commands = {}, defaultCommands = {}, @@ -78,7 +79,7 @@ module.exports = () => { if (!(name in c)) c[name] = defaults[name]; } - + em = c.em; var ppfx = c.pStylePrefix; if(ppfx) c.stylePrefix = ppfx + c.stylePrefix; @@ -206,6 +207,25 @@ module.exports = () => { e.DomComponents.clear(); e.CssComposer.clear(); }; + defaultCommands['core:copy'] = ed => { + const em = ed.getModel(); + const model = ed.getSelected(); + + if (model && model.get('copyable') && !ed.Canvas.isInputFocused()) { + em.set('clipboard', model); + } + }; + defaultCommands['core:paste'] = ed => { + const em = ed.getModel(); + const clp = em.get('clipboard'); + const model = ed.getSelected(); + const coll = model && model.collection; + + if (coll && clp && !ed.Canvas.isInputFocused()) { + const at = coll.indexOf(model) + 1; + coll.add(clp.clone(), { at }); + } + }; if(c.em) c.model = c.em.get('Canvas'); diff --git a/src/commands/view/SelectComponent.js b/src/commands/view/SelectComponent.js index 18f4f28fd..d262b661f 100644 --- a/src/commands/view/SelectComponent.js +++ b/src/commands/view/SelectComponent.js @@ -11,8 +11,7 @@ const $ = Backbone.$; module.exports = { init(o) { - bindAll(this, 'onHover', 'onOut', 'onClick', 'onKeyPress', - 'copyComp', 'pasteComp', 'onFrameScroll'); + bindAll(this, 'onHover', 'onOut', 'onClick', 'onKeyPress', 'onFrameScroll'); }, @@ -20,7 +19,6 @@ module.exports = { this.frameOff = this.canvasOff = this.adjScroll = null; var config = this.config.em.get('Config'); this.startSelectComponent(); - this.toggleClipboard(config.copyPaste); var em = this.config.em; showOffsets = 1; @@ -35,6 +33,7 @@ module.exports = { * @private */ toggleClipboard(active) { + /* var en = active || 0; if(en){ key('⌘+c, ctrl+c', this.copyComp); @@ -42,7 +41,7 @@ module.exports = { }else{ key.unbind('⌘+c, ctrl+c'); key.unbind('⌘+v, ctrl+v'); - } + }*/ }, /** @@ -580,7 +579,6 @@ module.exports = { this.stopSelectComponent(); !opts.preserveSelected && em.setSelected(null); this.clean(); - this.toggleClipboard(); this.hideBadge(); this.hideFixedElementOffset(); this.canvas.getToolbarEl().style.display = 'none'; diff --git a/src/editor/index.js b/src/editor/index.js index 70ffbb6a1..fad4c33e7 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -60,7 +60,6 @@ * @param {string|Array} [config.components=''] HTML string or object of components * @param {string|Array} [config.style=''] CSS string or object of rules * @param {Boolean} [config.fromElement=false] If true, will fetch HTML and CSS from selected container - * @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 {Boolean} [config.autorender=true] If true renders editor on init * @param {Boolean} [config.noticeOnUnload=true] Enable/Disable alert message before unload the page diff --git a/src/grapesjs/index.js b/src/grapesjs/index.js index 4afabc684..81a82be78 100644 --- a/src/grapesjs/index.js +++ b/src/grapesjs/index.js @@ -26,7 +26,6 @@ module.exports = (() => { * @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.fromElement=false] If true, will fetch HTML and CSS from selected container - * @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 diff --git a/src/keymaps/index.js b/src/keymaps/index.js index 8d4277ab3..f29ce6866 100644 --- a/src/keymaps/index.js +++ b/src/keymaps/index.js @@ -24,6 +24,14 @@ module.exports = () => { 'core:redo': { keys: '⌘+shift+z, ctrl+shift+z', handler: 'core:redo', + }, + 'core:copy': { + keys: '⌘+c, ctrl+c', + handler: 'core:copy', + }, + 'core:paste': { + keys: '⌘+v, ctrl+v', + handler: 'core:paste', } } };