Skip to content

Commit

Permalink
Collapse expanded selection before move word (#4211)
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed May 3, 2021
1 parent 737aaa9 commit 1c32b97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/move-word-collapse.md
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Collapse expanded selection before handling `moveWordBackward` (`alt + left`) and `moveWordForward` (`alt + right`) hotkeys.
10 changes: 10 additions & 0 deletions packages/slate-react/src/components/editable.tsx
Expand Up @@ -915,12 +915,22 @@ export const Editable = (props: EditableProps) => {

if (Hotkeys.isMoveWordBackward(nativeEvent)) {
event.preventDefault()

if (selection && Range.isExpanded(selection)) {
Transforms.collapse(editor, { edge: 'focus' })
}

Transforms.move(editor, { unit: 'word', reverse: !isRTL })
return
}

if (Hotkeys.isMoveWordForward(nativeEvent)) {
event.preventDefault()

if (selection && Range.isExpanded(selection)) {
Transforms.collapse(editor, { edge: 'focus' })
}

Transforms.move(editor, { unit: 'word', reverse: isRTL })
return
}
Expand Down

0 comments on commit 1c32b97

Please sign in to comment.