From ecf5912a0d3243e455fa77b770355c8bc4ec0eab Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Thu, 27 Jul 2017 11:43:34 -0400 Subject: [PATCH] fix(tab character insertion): Key#isTab is false if a modifier key is pressed (#566) Fixes #565 --- src/js/utils/key.js | 8 +++++++- tests/acceptance/editor-input-handlers-test.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/js/utils/key.js b/src/js/utils/key.js index 056beaa42..365cca40d 100644 --- a/src/js/utils/key.js +++ b/src/js/utils/key.js @@ -141,8 +141,14 @@ const Key = class Key { return this.keyCode === Keycodes.SPACE; } + // In Firefox, pressing ctrl-TAB will switch to another open browser tab, but it will + // also fire a keydown event for the tab+modifier (ctrl). This causes Mobiledoc + // to erroneously insert a tab character before FF switches to the new browser tab. + // Chrome doesn't fire this event so the issue doesn't arise there. + // Fix this by returning false when the TAB key event includes a modifier. + // See: https://github.com/bustle/mobiledoc-kit/issues/565 isTab() { - return this.keyCode === Keycodes.TAB; + return !this.hasAnyModifier() && this.keyCode === Keycodes.TAB; } isEnter() { diff --git a/tests/acceptance/editor-input-handlers-test.js b/tests/acceptance/editor-input-handlers-test.js index 5ca907e42..597e1153b 100644 --- a/tests/acceptance/editor-input-handlers-test.js +++ b/tests/acceptance/editor-input-handlers-test.js @@ -2,6 +2,7 @@ import Helpers from '../test-helpers'; import Range from 'mobiledoc-kit/utils/cursor/range'; import { NO_BREAK_SPACE } from 'mobiledoc-kit/renderers/editor-dom'; import { TAB } from 'mobiledoc-kit/utils/characters'; +import { MODIFIERS } from 'mobiledoc-kit/utils/key'; const { module, test } = Helpers; const { editor: { buildFromText } } = Helpers; @@ -300,6 +301,15 @@ test('input handler can be triggered by TAB', (assert) => { assert.ok(didMatch); }); +// See https://github.com/bustle/mobiledoc-kit/issues/565 +test('typing ctrl-TAB does not insert TAB text', (assert) => { + editor = Helpers.editor.buildFromText('abc|', {element: editorElement}); + + Helpers.dom.triggerKeyCommand(editor, TAB, [MODIFIERS.CTRL]); + + assert.equal(editorElement.textContent, 'abc', 'no TAB is inserted'); +}); + test('can unregister all handlers', (assert) => { editor = Helpers.editor.buildFromText(''); // there are 3 default helpers