Skip to content

Commit 2ce581e

Browse files
committed
Ensure the activeElement is set after rendering cursor
Fixes an issue on Firefox where it blurs the editor element after `editor.insertCard`
1 parent 82a141c commit 2ce581e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/js/utils/cursor.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ const Cursor = class Cursor {
116116
const { node:headNode, offset:headOffset } = this._findNodeForPosition(head),
117117
{ node:tailNode, offset:tailOffset } = this._findNodeForPosition(tail);
118118
this._moveToNode(headNode, headOffset, tailNode, tailOffset, direction);
119+
if (document.activeElement !== this.editor.element) {
120+
this.editor.element.focus();
121+
}
119122
}
120123

121124
get selection() {

tests/unit/editor/editor-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,35 @@ test('#insertCard returns card object', (assert) => {
667667
return post();
668668
}, {cards: [card]});
669669

670+
Helpers.dom.selectRange(editorElement, 0, editorElement, 0);
671+
672+
assert.ok(editor.hasCursor(), 'precond - editor has cursor');
673+
assert.ok(editor.post.isBlank, 'precond - post is blank');
674+
670675
const insertedCard = editor.insertCard('the-card');
671676

677+
assert.ok(!!insertedCard, 'insertedCard is present');
672678
assert.equal(editor.post.sections.tail, insertedCard, 'returned card is the inserted card');
673679
});
680+
681+
test('#insertCard focuses the cursor at the end of the card', (assert) => {
682+
let card = {
683+
name: 'the-card',
684+
type: 'dom',
685+
render() {
686+
}
687+
};
688+
689+
editor = Helpers.mobiledoc.renderInto(editorElement, ({post}) => {
690+
return post();
691+
}, {cards: [card]});
692+
693+
Helpers.dom.selectRange(editorElement, 0, editorElement, 0);
694+
695+
let insertedCard = editor.insertCard('the-card');
696+
697+
let range = editor.range;
698+
assert.positionIsEqual(range.head, insertedCard.tailPosition(), 'range head on card tail');
699+
assert.positionIsEqual(range.tail, insertedCard.tailPosition(), 'range tail on card tail');
700+
assert.ok(document.activeElement === editorElement, 'editor element retains focus');
701+
});

0 commit comments

Comments
 (0)