Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/js/editor/key-commands.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Key from '../utils/key';
import { MODIFIERS, SPECIAL_KEYS } from '../utils/key';
import { MODIFIERS, SPECIAL_KEYS, DIRECTION } from '../utils/key';
import { filter, reduce } from '../utils/array-utils';
import assert from '../utils/assert';
import Range from '../utils/cursor/range';
Expand Down Expand Up @@ -27,6 +27,24 @@ function gotoEndOfLine(editor) {
});
}

function selectLineToLeft(editor) {
let {range} = editor;
let {tail: {section}} = range;
let selectionRange = new Range(section.headPosition(), range.tail);

selectionRange.direction = DIRECTION.BACKWARD;
editor.selectRange(selectionRange);
}

function selectLineToRight(editor) {
let {range} = editor;
let {tail: {section}} = range;
let selectionRange = new Range(range.head, section.tailPosition());

selectionRange.direction = DIRECTION.FORWARD;
editor.selectRange(selectionRange);
}

export const DEFAULT_KEY_COMMANDS = [{
str: 'META+B',
run(editor) {
Expand Down Expand Up @@ -76,6 +94,13 @@ export const DEFAULT_KEY_COMMANDS = [{
}
}
}, {
str: 'META+SHIFT+LEFT',
run(editor) {
if (Browser.isMac) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker for this PR, but we've really got to figure out a more generic solution for environment-dependent hotkeys. Phew.

selectLineToLeft(editor);
}
}
},{
str: 'META+A',
run(editor) {
if (Browser.isMac) {
Expand All @@ -96,6 +121,13 @@ export const DEFAULT_KEY_COMMANDS = [{
gotoEndOfLine(editor);
}
}
}, {
str: 'META+SHIFT+RIGHT',
run(editor) {
if (Browser.isMac) {
selectLineToRight(editor);
}
}
}, {
str: 'META+K',
run(editor) {
Expand Down
51 changes: 51 additions & 0 deletions tests/acceptance/editor-key-commands-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,57 @@ test(`cmd-right goes to the end of a line (MacOS only)`, (assert) => {
}
});

test(`cmd-shif-left select text to the beginning of a line (MacOS only)`, (assert) => {
let initialText = 'something';
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
markupSection('p', [marker(initialText)])
]));

assert.ok(editor.hasCursor(), 'has cursor');

let textElement = editor.post.sections.head.markers.head.renderNode.element;

Helpers.dom.moveCursorTo(editor, textElement, 4);
let originalCursorPosition = Helpers.dom.getCursorPosition();

Helpers.dom.triggerKeyCommand(editor, 'LEFT', [MODIFIERS.META, MODIFIERS.SHIFT]);

let changedCursorPosition = Helpers.dom.getCursorPosition();

if (Browser.isMac) {
assert.positionIsEqual(editor.range.head, editor.post.headPosition());
assert.equal(editor.range.tail.offset, originalCursorPosition.offset);

} else {
assert.equal(changedCursorPosition.offset, originalCursorPosition.offset, 'cursor not moved to the end of the line (non-MacOS)');
}
});

test(`cmd-shif-right select text to the end of a line (MacOS only)`, (assert) => {
let initialText = 'something';
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
markupSection('p', [marker(initialText)])
]));

assert.ok(editor.hasCursor(), 'has cursor');

let textElement = editor.post.sections.head.markers.head.renderNode.element;

Helpers.dom.moveCursorTo(editor, textElement, 4);
let originalCursorPosition = Helpers.dom.getCursorPosition();

Helpers.dom.triggerKeyCommand(editor, 'RIGHT', [MODIFIERS.META, MODIFIERS.SHIFT]);

let changedCursorPosition = Helpers.dom.getCursorPosition();

if (Browser.isMac) {
assert.positionIsEqual(editor.range.head.offset, originalCursorPosition.offset);
assert.positionIsEqual(editor.range.tail, editor.post.tailPosition());

} else {
assert.equal(changedCursorPosition.offset, initialText.length, 'cursor not moved to the end of the line (non-MacOS)');
}
});
test(`ctrl-k clears to the end of a line`, (assert) => {
let initialText = 'something';
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
Expand Down