Skip to content

Commit

Permalink
fix: Fix broken clipboard event handling with single void nodes in Fi…
Browse files Browse the repository at this point in the history
…refox

COMPAT: Firefox will not trig clipboard events when selecting single void nodes.
Make sure that the range has 0-1 (not 1-1) offset so something is selected.
This obviously works fine in other browsers. Related to the zero-value (\uFEFF)?
  • Loading branch information
skogsmaskin committed Jan 7, 2022
1 parent 4b2d4de commit 7af21c8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,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

0 comments on commit 7af21c8

Please sign in to comment.