Skip to content

Commit

Permalink
fix: handle up/down key navigation over void nodes with custom code
Browse files Browse the repository at this point in the history
Handle up down key navigation over void nodes with this specific behaviour.
In this way we have a consistent way of handling these events, and it will not be up to the default browser behaviour to move the cursor.

Firefox seems to handle this a bit differently than the other browsers.
After doing e96a833 you had to press up/down twice to move the focus up/down between void blocks.
  • Loading branch information
skogsmaskin committed Jan 10, 2022
1 parent 00557b6 commit a7a2a30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Text,
Transforms,
Path,
BaseElement,
} from 'slate'
import getDirection from 'direction'
import debounce from 'lodash/debounce'
Expand Down Expand Up @@ -1097,6 +1098,35 @@ export const Editable = (props: EditableProps) => {
return
}

// Make sure we move consistently over void nodes when extending the selection with arrow up/down.
if (
selection &&
(Hotkeys.isExtendUp(nativeEvent) ||
Hotkeys.isExtendDown(nativeEvent)) &&
editor.isVoid(element as BaseElement)
) {
event.preventDefault()
const reverse = Hotkeys.isExtendUp(nativeEvent)
Transforms.move(editor, {
unit: 'line',
edge: 'focus',
reverse,
})
return
}

// Make sure we move consistently over void nodes on arrow up/down.
if (
(Hotkeys.isMoveUp(nativeEvent) ||
Hotkeys.isMoveDown(nativeEvent)) &&
editor.isVoid(element as BaseElement)
) {
event.preventDefault()
const reverse = Hotkeys.isMoveDown(nativeEvent)
Transforms.move(editor, { unit: 'line', reverse })
return
}

if (Hotkeys.isExtendLineBackward(nativeEvent)) {
event.preventDefault()
Transforms.move(editor, {
Expand Down
8 changes: 8 additions & 0 deletions packages/slate-react/src/utils/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ const HOTKEYS = {
compose: ['down', 'left', 'right', 'up', 'backspace', 'enter'],
moveBackward: 'left',
moveForward: 'right',
moveUp: 'up',
moveDown: 'down',
moveWordBackward: 'ctrl+left',
moveWordForward: 'ctrl+right',
deleteBackward: 'shift?+backspace',
deleteForward: 'shift?+delete',
extendBackward: 'shift+left',
extendForward: 'shift+right',
extendUp: 'shift+up',
extendDown: 'shift+down',
italic: 'mod+i',
splitBlock: 'shift?+enter',
undo: 'mod+z',
Expand Down Expand Up @@ -81,9 +85,13 @@ export default {
isDeleteWordForward: create('deleteWordForward'),
isExtendBackward: create('extendBackward'),
isExtendForward: create('extendForward'),
isExtendDown: create('extendDown'),
isExtendUp: create('extendUp'),
isExtendLineBackward: create('extendLineBackward'),
isExtendLineForward: create('extendLineForward'),
isItalic: create('italic'),
isMoveUp: create('moveDown'),
isMoveDown: create('moveUp'),
isMoveLineBackward: create('moveLineBackward'),
isMoveLineForward: create('moveLineForward'),
isMoveWordBackward: create('moveWordBackward'),
Expand Down

0 comments on commit a7a2a30

Please sign in to comment.