From 645e0012d931f8e98dca002037aec3b3d2f39e0a Mon Sep 17 00:00:00 2001 From: Sunny Hirai Date: Tue, 29 Nov 2016 23:22:09 -0800 Subject: [PATCH] Fix for OPTION-SHIFT-UP and OPTION-SHIFT-DOWN --- src/plugins/core.js | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/plugins/core.js b/src/plugins/core.js index 50cd08abc..aa19febe8 100644 --- a/src/plugins/core.js +++ b/src/plugins/core.js @@ -353,6 +353,8 @@ function Plugin(options = {}) { case 'delete': return onKeyDownDelete(e, data, state) case 'left': return onKeyDownLeft(e, data, state) case 'right': return onKeyDownRight(e, data, state) + case 'up': return onKeyDownUp(e, data, state) + case 'down': return onKeyDownDown(e, data, state) case 'd': return onKeyDownD(e, data, state) case 'h': return onKeyDownH(e, data, state) case 'k': return onKeyDownK(e, data, state) @@ -510,6 +512,68 @@ function Plugin(options = {}) { } } + /** + * On `up` key down, for Macs, fix select to start of block. + * + * OPTION-SHIFT-UP is supposed to expand to the start of the current block + * then the start of the previous block and so on. + * + * On Chrome and possibly other browsers, OPTION-SHIFT-UP expands the + * selection to the start of the current block then extends the selection to + * somewhere in the previous block but not to the start of the next + * block. This problem does not appear for OPTION-UP. + * + * @param {Event} e + * @param {Object} data + * @param {State} state + * @return {State} + */ + + function onKeyDownUp(e, data, state) { + if (!IS_MAC || data.isCtrl || !data.isAlt || !data.isShift) return; + e.preventDefault(); + const {selection, document, focusBlock} = state + const isStart = selection.hasFocusAtStartOf(focusBlock) + const selectBlock = isStart ? document.getPreviousBlock(focusBlock.get('key')) : focusBlock; + if (!selectBlock) return; + var selectText = selectBlock.getTextAtOffset(0); + return state + .transform() + .extendToStartOf(selectText) + .apply(); + } + + /** + * On `down` key down, for Macs, fix select to end of block. + * + * OPTION-SHIFT-DOWN is supposed to expand to the end of the current block + * then the end of the next block and so on. + * + * On Chrome and possibly other browsers, OPTION-SHIFT-DOWN expands the + * selection to the end of the current block then extends the selection to + * somewhere in the next block but not to the end of the next + * block. This problem does not appear for OPTION-DOWN. + * + * @param {Event} e + * @param {Object} data + * @param {State} state + * @return {State} + */ + + function onKeyDownDown(e, data, state) { + if (!IS_MAC || data.isCtrl || !data.isAlt || !data.isShift) return; + e.preventDefault(); + const {selection, document, focusBlock} = state + const isEnd = selection.hasFocusAtEndOf(focusBlock) + const selectBlock = isEnd ? document.getNextBlock(focusBlock.get('key')) : focusBlock; + if (!selectBlock) return; + var selectText = selectBlock.getTextAtOffset(selectBlock.length); + return state + .transform() + .extendToEndOf(selectText) + .apply(); + } + /** * On `d` key down, for Macs, delete one character forward. *