From 9d56cb5552b0fa0d7bc7e7e7b9f45e1d632fc5a3 Mon Sep 17 00:00:00 2001 From: lessless Date: Wed, 18 May 2016 10:05:03 +0700 Subject: [PATCH] Add line selection shortcuts --- src/js/editor/key-commands.js | 34 ++++++++++++- tests/acceptance/editor-key-commands-test.js | 51 ++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/js/editor/key-commands.js b/src/js/editor/key-commands.js index ddc6dff06..eb7174e99 100644 --- a/src/js/editor/key-commands.js +++ b/src/js/editor/key-commands.js @@ -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'; @@ -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) { @@ -76,6 +94,13 @@ export const DEFAULT_KEY_COMMANDS = [{ } } }, { + str: 'META+SHIFT+LEFT', + run(editor) { + if (Browser.isMac) { + selectLineToLeft(editor); + } + } +},{ str: 'META+A', run(editor) { if (Browser.isMac) { @@ -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) { diff --git a/tests/acceptance/editor-key-commands-test.js b/tests/acceptance/editor-key-commands-test.js index e89528a23..a5de48e50 100644 --- a/tests/acceptance/editor-key-commands-test.js +++ b/tests/acceptance/editor-key-commands-test.js @@ -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([