test('returning false from key command causes next match to run', (assert) => { const mobiledoc = Helpers.mobiledoc.build( ({post, markupSection, marker}) => post([ markupSection('p', [marker('something')]) ])); let firstCommandRan, secondCommandRan; editor = new Editor({mobiledoc}); editor.registerKeyCommand({ str: 'ctrl+x', run() { firstCommandRan = true; } }); editor.registerKeyCommand({ str: 'ctrl+x', run() { secondCommandRan = true; return false; } }); editor.render(editorElement); Helpers.dom.triggerKeyCommand(editor, 'X', MODIFIERS.CTRL); assert.ok(!!secondCommandRan, 'last registered method is called'); assert.ok(!!firstCommandRan, 'first registered method is called'); });
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(editor, 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'); });
test('new key commands can be registered without modifiers', (assert) => { const mobiledoc = Helpers.mobiledoc.build( ({post, markupSection, marker}) => post([ markupSection('p', [marker('something')]) ])); let passedEditor; editor = new Editor({mobiledoc}); editor.registerKeyCommand({ str: 'X', run(editor) { passedEditor = editor; } }); editor.render(editorElement); Helpers.dom.triggerKeyCommand(editor, 'Y', MODIFIERS.CTRL); assert.ok(!passedEditor, 'incorrect key combo does not trigger key command'); Helpers.dom.triggerKeyCommand(editor, 'X', MODIFIERS.CTRL); assert.ok(!passedEditor, 'key with modifier combo does not trigger key command'); Helpers.dom.triggerKeyCommand(editor, 'X'); assert.ok(!!passedEditor && passedEditor === editor, 'run method is called'); });
test('duplicate key commands can be registered with the last registered winning', (assert) => { const mobiledoc = Helpers.mobiledoc.build( ({post, markupSection, marker}) => post([ markupSection('p', [marker('something')]) ])); let firstCommandRan, secondCommandRan; editor = new Editor({mobiledoc}); editor.registerKeyCommand({ str: 'ctrl+x', run() { firstCommandRan = true; } }); editor.registerKeyCommand({ str: 'ctrl+x', run() { secondCommandRan = true; } }); editor.render(editorElement); Helpers.dom.triggerKeyCommand(editor, 'X', MODIFIERS.CTRL); assert.ok(!firstCommandRan, 'first registered method not called'); assert.ok(!!secondCommandRan, 'last registered method is called'); });