Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows for the unregistering of custom key commands. #534

Merged
merged 1 commit into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions tests/acceptance/editor-key-commands-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});