diff --git a/src/commands/view/ComponentDelete.js b/src/commands/view/ComponentDelete.js index 2fc73c0331..6ca695f3ef 100644 --- a/src/commands/view/ComponentDelete.js +++ b/src/commands/view/ComponentDelete.js @@ -2,7 +2,6 @@ import { isArray } from 'underscore'; module.exports = { run(ed, sender, opts = {}) { - if (ed.getModel().isEditing() || ed.Canvas.isInputFocused()) return; let components = opts.component || ed.getSelectedAll(); components = isArray(components) ? [...components] : [components]; diff --git a/src/commands/view/ComponentEnter.js b/src/commands/view/ComponentEnter.js index 3f1a9bc3b1..a06efae09e 100644 --- a/src/commands/view/ComponentEnter.js +++ b/src/commands/view/ComponentEnter.js @@ -1,11 +1,6 @@ module.exports = { run(ed) { - if ( - !ed.Canvas.hasFocus() || - ed.getModel().isEditing() || - ed.Canvas.isInputFocused() - ) - return; + if (!ed.Canvas.hasFocus()) return; const toSelect = []; ed.getSelectedAll().forEach(component => { diff --git a/src/commands/view/ComponentExit.js b/src/commands/view/ComponentExit.js index 0dff37e039..d13328704e 100644 --- a/src/commands/view/ComponentExit.js +++ b/src/commands/view/ComponentExit.js @@ -1,11 +1,6 @@ module.exports = { run(ed) { - if ( - !ed.Canvas.hasFocus() || - ed.getModel().isEditing() || - ed.Canvas.isInputFocused() - ) - return; + if (!ed.Canvas.hasFocus()) return; const toSelect = []; ed.getSelectedAll().forEach(component => { diff --git a/src/commands/view/ComponentNext.js b/src/commands/view/ComponentNext.js index c0af4744d8..f47ea4d5b0 100644 --- a/src/commands/view/ComponentNext.js +++ b/src/commands/view/ComponentNext.js @@ -1,11 +1,6 @@ module.exports = { run(ed) { - if ( - !ed.Canvas.hasFocus() || - ed.getModel().isEditing() || - ed.Canvas.isInputFocused() - ) - return; + if (!ed.Canvas.hasFocus()) return; const toSelect = []; ed.getSelectedAll().forEach(component => { diff --git a/src/commands/view/ComponentPrev.js b/src/commands/view/ComponentPrev.js index 0fe40ebcbb..47c57150d2 100644 --- a/src/commands/view/ComponentPrev.js +++ b/src/commands/view/ComponentPrev.js @@ -1,11 +1,6 @@ module.exports = { run(ed) { - if ( - !ed.Canvas.hasFocus() || - ed.getModel().isEditing() || - ed.Canvas.isInputFocused() - ) - return; + if (!ed.Canvas.hasFocus()) return; const toSelect = []; ed.getSelectedAll().forEach(component => { diff --git a/src/commands/view/CopyComponent.js b/src/commands/view/CopyComponent.js index c26cd1ecb3..dc396401a6 100644 --- a/src/commands/view/CopyComponent.js +++ b/src/commands/view/CopyComponent.js @@ -3,7 +3,7 @@ module.exports = { const em = ed.getModel(); const models = [...ed.getSelectedAll()]; - if (models.length && !em.isEditing() && !ed.Canvas.isInputFocused()) { + if (models.length) { em.set('clipboard', models); } } diff --git a/src/commands/view/PasteComponent.js b/src/commands/view/PasteComponent.js index 98ebeedbde..c715b87207 100644 --- a/src/commands/view/PasteComponent.js +++ b/src/commands/view/PasteComponent.js @@ -6,7 +6,7 @@ module.exports = { const clp = em.get('clipboard'); const selected = ed.getSelected(); - if (clp && selected && !em.isEditing() && !ed.Canvas.isInputFocused()) { + if (clp && selected) { ed.getSelectedAll().forEach(comp => { if (!comp) return; const coll = comp.collection; diff --git a/src/keymaps/index.js b/src/keymaps/index.js index aad9844415..51d63ce199 100644 --- a/src/keymaps/index.js +++ b/src/keymaps/index.js @@ -110,7 +110,7 @@ module.exports = () => { for (let id in defKeys) { const value = defKeys[id]; - this.add(id, value.keys, value.handler); + this.add(id, value.keys, value.handler, { force: false }); } }, @@ -137,8 +137,9 @@ module.exports = () => { */ add(id, keys, handler, opts = {}) { const em = this.em; - const editor = em.getEditor(); + const force = opts.force || false; const cmd = em.get('Commands'); + const editor = em.getEditor(); const canvas = em.get('Canvas'); const keymap = { id, keys, handler }; const pk = keymaps[id]; @@ -149,12 +150,15 @@ module.exports = () => { const opt = { event: e, h }; handler = isString(handler) ? cmd.get(handler) : handler; opts.prevent && canvas.getCanvasView().preventDefault(e); - typeof handler == 'object' - ? handler.run(editor, 0, opt) - : handler(editor, 0, opt); - const args = [id, h.shortcut, e]; - em.trigger('keymap:emit', ...args); - em.trigger(`keymap:emit:${id}`, ...args); + const ableTorun = !em.isEditing() && !editor.Canvas.isInputFocused(); + if (ableTorun || force) { + typeof handler == 'object' + ? handler.run(editor, 0, opt) + : handler(editor, 0, opt); + const args = [id, h.shortcut, e]; + em.trigger('keymap:emit', ...args); + em.trigger(`keymap:emit:${id}`, ...args); + } }); em.trigger('keymap:add', keymap); return keymap; diff --git a/test/specs/keymaps/index.js b/test/specs/keymaps/index.js index 85f7d954c8..858aad1342 100644 --- a/test/specs/keymaps/index.js +++ b/test/specs/keymaps/index.js @@ -1,14 +1,16 @@ -const Editor = require('editor/model/Editor'); +import Editor from 'editor/index'; const Keymaps = require('keymaps'); describe('Keymaps', () => { describe('Main', () => { let em; let obj; + let editor; beforeEach(() => { - em = new Editor(); - obj = new Keymaps().init({ em }); + editor = Editor().init(); + em = editor.getModel(); + obj = editor.Keymaps; }); test('Object exists', () => { @@ -53,5 +55,62 @@ describe('Keymaps', () => { const removed = obj.remove('tes'); expect(called).toEqual(1); }); + + describe('Given the edit is not on edit mode', () => { + beforeEach(() => { + em.setEditing(0); + }); + + it('Should run the handler', () => { + const handler = { + run: jest.fn() + }; + obj.add('test', 'ctrl+a', handler); + const keyboardEvent = new KeyboardEvent('keydown', { + keyCode: 65, + which: 65, + ctrlKey: true + }); + document.dispatchEvent(keyboardEvent); + + expect(handler.run).toBeCalled(); + }); + }); + + describe('Given the edit is on edit mode', () => { + beforeEach(() => { + em.setEditing(1); + }); + + it('Should not run the handler', () => { + const handler = { + run: jest.fn() + }; + obj.add('test', 'ctrl+a', handler); + const keyboardEvent = new KeyboardEvent('keydown', { + keyCode: 65, + which: 65, + ctrlKey: true + }); + document.dispatchEvent(keyboardEvent); + + expect(handler.run).toBeCalledTimes(0); + }); + + it('Should run the handler if checked as force', () => { + const handler = { + run: jest.fn() + }; + obj.add('test', 'ctrl+a', handler, { force: true }); + const keyboardEvent = new KeyboardEvent('keydown', { + keyCode: 65, + which: 65, + ctrlKey: true + }); + document.dispatchEvent(keyboardEvent); + + expect(handler.run).toBeCalled(); + }); + }); }); });