From 28f96bf8840e74f0db361deb08638c3534ba144d Mon Sep 17 00:00:00 2001 From: Ryan McCarvill Date: Mon, 20 Feb 2017 15:42:27 +1300 Subject: [PATCH] Allows users to add an optional `name` attribute to a keyCommand object as passed to the `registerKeyCommand` method. If the `name` attribute is added a new `unregisterKeyCommands` method will remove these keycommands from the keycommand array. Multiple keycommands can have the same name, if so unregisterKeyCommands will remove all instances with that name. Usage: ``` editor.registerKeyCommand({ name: 'cut', str: 'ctrl+x', run(editor) { ... } }); editor.unregisterKeyCommands('cut'); ``` --- src/js/editor/editor.js | 14 ++++++++ tests/acceptance/editor-key-commands-test.js | 37 ++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/js/editor/editor.js b/src/js/editor/editor.js index 4f43277f1..ce4339fc9 100644 --- a/src/js/editor/editor.js +++ b/src/js/editor/editor.js @@ -284,6 +284,20 @@ class Editor { this.keyCommands.unshift(keyCommand); } + /** + * @param {String} name If the keyCommand event has a name attribute it can be removed. + * @public + */ + unregisterKeyCommands(name) { + for(let i = this.keyCommands.length-1; i > -1; i--) { + let keyCommand = this.keyCommands[i]; + + if(keyCommand.name === name) { + this.keyCommands.splice(i,1); + } + } + } + /** * Convenience for {@link PostEditor#deleteAtPosition}. Deletes and puts the * cursor in the new position. diff --git a/tests/acceptance/editor-key-commands-test.js b/tests/acceptance/editor-key-commands-test.js index 8ebd807de..ea14b1b88 100644 --- a/tests/acceptance/editor-key-commands-test.js +++ b/tests/acceptance/editor-key-commands-test.js @@ -443,3 +443,40 @@ test('returning false from key command still runs built-in functionality', (asse assert.equal($('#editor p').length, 2, 'has added a new paragraph'); }); + +test('new key commands can be registered and then unregistered', (assert) => { + editor = Helpers.mobiledoc.renderIntoAndFocusTail(editorElement, ({post, markupSection, marker}) => post([ + markupSection('p', [marker('something')]) + ])); + + assert.ok(editor.hasCursor(), 'has cursor'); + let passedEditorCount = 0; + let passedEditor; + editor.registerKeyCommand({ + name: 'cut', + str: 'ctrl+x', + run(editor) { passedEditor = editor; passedEditorCount++; } + }); + + editor.registerKeyCommand({ + name: 'cut', + str: 'ctrl+d', + run(editor) { passedEditor = editor; passedEditorCount++; } + }); + + + + + Helpers.dom.triggerKeyCommand(editor, 'x', MODIFIERS.CTRL); + Helpers.dom.triggerKeyCommand(editor, 'd', MODIFIERS.CTRL); + + assert.ok(!!passedEditor && passedEditor === editor, 'run method is called'); + assert.ok(passedEditorCount === 2, 'the passedEditor has been called twice'); + + editor.unregisterKeyCommands('cut'); + + Helpers.dom.triggerKeyCommand(editor, 'x', MODIFIERS.CTRL); + Helpers.dom.triggerKeyCommand(editor, 'd', MODIFIERS.CTRL); + + assert.ok(passedEditorCount === 2, 'the passedEditor has still only been called twice'); +});