Skip to content

Commit

Permalink
Set cursor to collapsed after hitting enter
Browse files Browse the repository at this point in the history
Range.fromSelection was being used to create a cursor position after
hitting enter. The implementation of fromSelection is such that it
creates a range and doesn't simply set a collapsed cursor.
  • Loading branch information
mixonic committed Dec 14, 2015
1 parent 15dfb9e commit 68cfa16
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ class Editor {
let nextPosition = postEditor.deleteRange(range);
cursorSection = nextPosition.section;
if (cursorSection && cursorSection.isBlank) {
postEditor.setRange(Range.fromSection(cursorSection));
postEditor.setRange(new Range(cursorSection.headPosition()));
return;
}
}
cursorSection = postEditor.splitSection(range.head)[1];
postEditor.setRange(Range.fromSection(cursorSection));
postEditor.setRange(new Range(cursorSection.headPosition()));
});
}

Expand Down
1 change: 1 addition & 0 deletions src/js/utils/characters.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const TAB = '\t';
export const ENTER = '\n';
1 change: 1 addition & 0 deletions src/js/utils/keycodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
'NUMPAD_0': 186,
'NUMPAD_9': 111,
';': 186,
'.': 190,
'`': 192,
'[': 219,
'"': 222,
Expand Down
40 changes: 39 additions & 1 deletion tests/acceptance/basic-editor-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Editor } from 'mobiledoc-kit';
import Helpers from '../test-helpers';
import { TAB } from 'mobiledoc-kit/utils/characters';
import Range from 'mobiledoc-kit/utils/cursor/range';
import Position from 'mobiledoc-kit/utils/cursor/position';
import {
TAB,
ENTER
} from 'mobiledoc-kit/utils/characters';

const { test, module } = Helpers;

Expand Down Expand Up @@ -198,3 +203,36 @@ test('select-all and type text works ok', (assert) => {
done();
}, 0);
});

test('typing enter splits lines, sets cursor', (assert) => {
let done = assert.async();
let mobiledoc = Helpers.mobiledoc.build(({post, markupSection, marker}) => {
return post([
markupSection('p', [ marker('hihey') ])
]);
});
editor = new Editor({mobiledoc});
editor.render(editorElement);

assert.hasElement('#editor p');

Helpers.dom.moveCursorTo($('#editor p')[0].firstChild, 2);
Helpers.dom.insertText(editor, ENTER);
window.setTimeout(() => {
let editedMobiledoc = editor.serialize();
assert.deepEqual(editedMobiledoc.sections, [
[],
[
[1, 'p', [
[[], 0, `hi`]
]],
[1, 'p', [
[[], 0, `hey`]
]]
]
], 'correctly encoded');
let expectedRange = new Range(new Position(editor.post.sections.tail, 0));
assert.ok(expectedRange.isEqual(editor.range), 'range is at start of new section');
done();
}, 0);
});
5 changes: 4 additions & 1 deletion tests/helpers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ function keyCodeForChar(letter) {
let keyCode;
switch (letter) {
case '.':
keyCode = 190;
keyCode = KEY_CODES['.'];
break;
case '\n':
keyCode = KEY_CODES.ENTER;
break;
default:
keyCode = letter.charCodeAt(0);
Expand Down

0 comments on commit 68cfa16

Please sign in to comment.