1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-01 04:50:27 +02:00

Merge pull request #465 from thesunny/fix-opt-shift-up-and-down

Fix for OPTION-SHIFT-UP and OPTION-SHIFT-DOWN
This commit is contained in:
Ian Storm Taylor
2016-12-01 11:39:02 -08:00
committed by GitHub

View File

@@ -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) 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
const selectText = selectBlock.getTextAtOffset(0)
return state
.transform()
[data.isShift ? 'extendToStartOf' : 'collapseToStartOf'](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) 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
const selectText = selectBlock.getTextAtOffset(selectBlock.length)
return state
.transform()
[data.isShift ? 'extendToEndOf' : 'collapseToEndOf'](selectText)
.apply()
}
/**
* On `d` key down, for Macs, delete one character forward.
*