1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-25 09:43:21 +01:00
Eric Edem 805c329e5d fix: use text utils to move forward and backward by word (#2169)
* fix: don't check for adjacent void with modified move

This was causing a problem where when the current text was followed by a void node using a modifier key to move forward would force the selection to creep forward a character at a time.

With this change, now the modifier will move as expected, but will jump over void nodes. This is not ideal, but seems like a behavior that will be slightly better than the current one.

* fix: modified key movement.

Use TextUtils.getWordOffsetForward and TextUtils.getWordOffsetBackward to move around by words.

The idea now is that if you move forward or backward, it is completely controlled by slate instead of trying to rely on a combination of browser behavior and slate trying to stop the browser from doing something wrong. This makes things quite a bit more intuitive in the implementation, and gives us a bit more control.

* tests: a whole bunch of selection movement tests.

* tests: more movement tests.
2018-10-22 11:21:20 -07:00

91 lines
2.2 KiB
JavaScript

import { isKeyHotkey } from 'is-hotkey'
import { IS_IOS, IS_MAC } from 'slate-dev-environment'
/**
* Hotkey mappings for each platform.
*
* @type {Object}
*/
const HOTKEYS = {
bold: 'mod+b',
compose: ['down', 'left', 'right', 'up', 'backspace', 'enter'],
moveBackward: 'left',
moveForward: 'right',
moveWordBackward: 'ctrl+left',
moveWordForward: 'ctrl+right',
deleteBackward: 'shift?+backspace',
deleteForward: 'shift?+delete',
extendBackward: 'shift+left',
extendForward: 'shift+right',
italic: 'mod+i',
splitBlock: 'shift?+enter',
undo: 'mod+z',
}
const APPLE_HOTKEYS = {
moveLineBackward: 'opt+up',
moveLineForward: 'opt+down',
moveWordBackward: 'opt+left',
moveWordForward: 'opt+right',
deleteBackward: ['ctrl+backspace', 'ctrl+h'],
deleteForward: ['ctrl+delete', 'ctrl+d'],
deleteLineBackward: 'cmd+shift?+backspace',
deleteLineForward: ['cmd+shift?+delete', 'ctrl+k'],
deleteWordBackward: 'opt+shift?+backspace',
deleteWordForward: 'opt+shift?+delete',
extendLineBackward: 'opt+shift+up',
extendLineForward: 'opt+shift+down',
redo: 'cmd+shift+z',
transposeCharacter: 'ctrl+t',
}
const WINDOWS_HOTKEYS = {
deleteWordBackward: 'ctrl+shift?+backspace',
deleteWordForward: 'ctrl+shift?+delete',
redo: 'ctrl+y',
}
/**
* Hotkeys.
*
* @type {Object}
*/
const Hotkeys = {}
const IS_APPLE = IS_IOS || IS_MAC
const IS_WINDOWS = !IS_APPLE
const KEYS = []
.concat(Object.keys(HOTKEYS))
.concat(Object.keys(APPLE_HOTKEYS))
.concat(Object.keys(WINDOWS_HOTKEYS))
KEYS.forEach(key => {
const method = `is${key[0].toUpperCase()}${key.slice(1)}`
if (Hotkeys[method]) return
const generic = HOTKEYS[key]
const apple = APPLE_HOTKEYS[key]
const windows = WINDOWS_HOTKEYS[key]
const isGeneric = generic && isKeyHotkey(generic)
const isApple = apple && isKeyHotkey(apple)
const isWindows = windows && isKeyHotkey(windows)
Hotkeys[method] = event => {
if (isGeneric && isGeneric(event)) return true
if (IS_APPLE && isApple && isApple(event)) return true
if (IS_WINDOWS && isWindows && isWindows(event)) return true
return false
}
})
/**
* Export.
*
* @type {Object}
*/
export default Hotkeys