Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix void node clipboard event handling in Firefox #4775

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 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 @@ -217,17 +218,29 @@ export const Editable = (props: EditableProps) => {
const newDomRange = selection && ReactEditor.toDOMRange(editor, selection)

if (newDomRange) {
let startOffset = newDomRange.startOffset
// COMPAT: Firefox will not trig clipboard events when selecting void nodes.
// Make sure that the range has 0-1 (not 1-1) offset so something is actually selected.
// This obviously works fine in other browsers. Related to the zero-value (\uFEFF)?
if (
IS_FIREFOX &&
newDomRange[
Range.isBackward(selection!) ? 'endContainer' : 'startContainer'
].textContent === '\uFEFF'
) {
startOffset = 0
}
if (Range.isBackward(selection!)) {
domSelection.setBaseAndExtent(
newDomRange.endContainer,
newDomRange.endOffset,
newDomRange.startContainer,
newDomRange.startOffset
startOffset
)
} else {
domSelection.setBaseAndExtent(
newDomRange.startContainer,
newDomRange.startOffset,
startOffset,
newDomRange.endContainer,
newDomRange.endOffset
)
Expand Down Expand Up @@ -1085,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