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 e96a833
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 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,30 @@ 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 single 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.startContainer === newDomRange.endContainer &&
newDomRange.startContainer.textContent === '\uFEFF' &&
startOffset === 1 &&
startOffset === newDomRange.endOffset
) {
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 e96a833

Please sign in to comment.