Skip to content

Commit

Permalink
Registered key commands can override built-in functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rlivsey committed Oct 14, 2015
1 parent f6cfe26 commit cbd6ec0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,10 @@ class Editor {
this._insertEmptyMarkupSectionAtCursor();
}

if (this.handleKeyCommand(event)) {
return;
}

const key = Key.fromEvent(event);

if (key.isDelete()) {
Expand All @@ -600,7 +604,6 @@ class Editor {
}

this.handleExpansion(event);
this.handleKeyCommand(event);
}

/**
Expand All @@ -614,6 +617,7 @@ class Editor {
*
* @method handleKeyCommand
* @param {Event} event The keyboard event triggered by the user
* @return {Boolean} true when a command was successfully run
* @private
*/
handleKeyCommand(event) {
Expand All @@ -622,9 +626,10 @@ class Editor {
let keyCommand = keyCommands[i];
if (keyCommand.run(this) !== false) {
event.preventDefault();
return;
return true;
}
}
return false;
}

handlePaste(event) {
Expand Down
53 changes: 53 additions & 0 deletions tests/acceptance/editor-key-commands-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,56 @@ test('returning false from key command causes next match to run', (assert) => {
assert.ok(!!firstCommandRan, 'first registered method is called');
});

test('key commands can override built-in functionality', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => post([
markupSection('p', [marker('something')])
]));

editor = new Editor({mobiledoc});

let passedEditor;
editor.registerKeyCommand({
str: 'enter',
run(editor) { passedEditor = editor; }
});

editor.render(editorElement);
assert.equal($('#editor p').length, 1, 'has 1 paragraph to start');

Helpers.dom.moveCursorTo(editorElement.childNodes[0].childNodes[0], 5);
Helpers.dom.triggerEnter(editor);

assert.ok(!!passedEditor && passedEditor === editor, 'run method is called');

assert.equal($('#editor p').length, 1, 'still has just one paragraph');
});

test('returning false from key command still runs built-in functionality', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => post([
markupSection('p', [marker('something')])
]));

editor = new Editor({mobiledoc});

let passedEditor;
editor.registerKeyCommand({
str: 'enter',
run(editor) {
passedEditor = editor;
return false;
}
});

editor.render(editorElement);
assert.equal($('#editor p').length, 1, 'has 1 paragraph to start');

Helpers.dom.moveCursorTo(editorElement.childNodes[0].childNodes[0], 5);
Helpers.dom.triggerEnter(editor);

assert.ok(!!passedEditor && passedEditor === editor, 'run method is called');

assert.equal($('#editor p').length, 2, 'has added a new paragraph');
});

0 comments on commit cbd6ec0

Please sign in to comment.