Skip to content

Commit

Permalink
Ensure the activeElement is set after rendering cursor
Browse files Browse the repository at this point in the history
Fixes an issue on Firefox where it blurs the editor element after
`editor.insertCard`
  • Loading branch information
bantic committed Jul 15, 2016
1 parent 82a141c commit 2ce581e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/js/utils/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ const Cursor = class Cursor {
const { node:headNode, offset:headOffset } = this._findNodeForPosition(head),
{ node:tailNode, offset:tailOffset } = this._findNodeForPosition(tail);
this._moveToNode(headNode, headOffset, tailNode, tailOffset, direction);
if (document.activeElement !== this.editor.element) {
this.editor.element.focus();
}
}

get selection() {
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/editor/editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,35 @@ test('#insertCard returns card object', (assert) => {
return post();
}, {cards: [card]});

Helpers.dom.selectRange(editorElement, 0, editorElement, 0);

assert.ok(editor.hasCursor(), 'precond - editor has cursor');
assert.ok(editor.post.isBlank, 'precond - post is blank');

const insertedCard = editor.insertCard('the-card');

assert.ok(!!insertedCard, 'insertedCard is present');
assert.equal(editor.post.sections.tail, insertedCard, 'returned card is the inserted card');
});

test('#insertCard focuses the cursor at the end of the card', (assert) => {
let card = {
name: 'the-card',
type: 'dom',
render() {
}
};

editor = Helpers.mobiledoc.renderInto(editorElement, ({post}) => {
return post();
}, {cards: [card]});

Helpers.dom.selectRange(editorElement, 0, editorElement, 0);

let insertedCard = editor.insertCard('the-card');

let range = editor.range;
assert.positionIsEqual(range.head, insertedCard.tailPosition(), 'range head on card tail');
assert.positionIsEqual(range.tail, insertedCard.tailPosition(), 'range tail on card tail');
assert.ok(document.activeElement === editorElement, 'editor element retains focus');
});

0 comments on commit 2ce581e

Please sign in to comment.