mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-15 11:44:05 +02:00
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.
This commit is contained in:
committed by
Ian Storm Taylor
parent
f56f28a8fa
commit
805c329e5d
@@ -530,25 +530,33 @@ function AfterPlugin(options = {}) {
|
||||
// an inline is selected, we need to handle these hotkeys manually because
|
||||
// browsers won't know what to do.
|
||||
if (Hotkeys.isMoveBackward(event)) {
|
||||
const { previousText, startText } = value
|
||||
const isPreviousInVoid =
|
||||
previousText && document.hasVoidParent(previousText.key, editor)
|
||||
event.preventDefault()
|
||||
|
||||
if (hasVoidParent || isPreviousInVoid || startText.text == '') {
|
||||
event.preventDefault()
|
||||
return change.moveBackward()
|
||||
if (!selection.isCollapsed) {
|
||||
return change.moveToStart()
|
||||
}
|
||||
|
||||
return change.moveBackward()
|
||||
}
|
||||
|
||||
if (Hotkeys.isMoveForward(event)) {
|
||||
const { nextText, startText } = value
|
||||
const isNextInVoid =
|
||||
nextText && document.hasVoidParent(nextText.key, editor)
|
||||
event.preventDefault()
|
||||
|
||||
if (hasVoidParent || isNextInVoid || startText.text == '') {
|
||||
event.preventDefault()
|
||||
return change.moveForward()
|
||||
if (!selection.isCollapsed) {
|
||||
return change.moveToEnd()
|
||||
}
|
||||
|
||||
return change.moveForward()
|
||||
}
|
||||
|
||||
if (Hotkeys.isMoveWordBackward(event)) {
|
||||
event.preventDefault()
|
||||
return change.moveWordBackward()
|
||||
}
|
||||
|
||||
if (Hotkeys.isMoveWordForward(event)) {
|
||||
event.preventDefault()
|
||||
return change.moveWordForward()
|
||||
}
|
||||
|
||||
if (Hotkeys.isExtendBackward(event)) {
|
||||
|
Reference in New Issue
Block a user