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

Firefox: fix wrong text highlighting with double-click (revived PR) #5275

Merged
merged 4 commits into from
Jan 30, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/thirty-starfishes-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': major
---

Firefox: fix wrong text highlighting with double-click
55 changes: 47 additions & 8 deletions packages/slate-react/src/plugin/react-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ export const ReactEditor = {
editor: ReactEditor,
domPoint: DOMPoint,
options: {
exactMatch: T
exactMatch: boolean
suppressThrow: T
}
): T extends true ? Point | null : Point {
Expand Down Expand Up @@ -703,7 +703,7 @@ export const ReactEditor = {
editor: ReactEditor,
domRange: DOMRange | DOMStaticRange | DOMSelection,
options: {
exactMatch: T
exactMatch: boolean
suppressThrow: T
}
): T extends true ? Range | null : Range {
Expand Down Expand Up @@ -754,16 +754,15 @@ export const ReactEditor = {
)
}

const anchor = ReactEditor.toSlatePoint(
editor,
[anchorNode, anchorOffset],
{ exactMatch, suppressThrow }
)
let anchor = ReactEditor.toSlatePoint(editor, [anchorNode, anchorOffset], {
exactMatch,
suppressThrow,
})
if (!anchor) {
return null as T extends true ? Range | null : Range
}

const focus = isCollapsed
let focus = isCollapsed
? anchor
: ReactEditor.toSlatePoint(editor, [focusNode, focusOffset], {
exactMatch,
Expand All @@ -773,6 +772,46 @@ export const ReactEditor = {
return null as T extends true ? Range | null : Range
}

/**
* suppose we have this document:
*
* { type: 'paragraph',
* children: [
* { text: 'foo ' },
* { text: 'bar' },
* { text: ' baz' }
* ]
* }
*
* a double click on "bar" on chrome will create this range:
*
* anchor -> [0,1] offset 0
* focus -> [0,1] offset 3
*
* while on firefox will create this range:
*
* anchor -> [0,0] offset 4
* focus -> [0,2] offset 0
*
* let's try to fix it...
*/

if (IS_FIREFOX && !isCollapsed && anchorNode !== focusNode) {
const isEnd = Editor.isEnd(editor, anchor!, anchor.path)
const isStart = Editor.isStart(editor, focus!, focus.path)

if (isEnd) {
const after = Editor.after(editor, anchor as Point)
// Editor.after() might return undefined
anchor = (after || anchor!) as T extends true ? Point | null : Point
}

if (isStart) {
const before = Editor.before(editor, focus as Point)
focus = (before || focus!) as T extends true ? Point | null : Point
}
}

let range: Range = { anchor: anchor as Point, focus: focus as Point }
// if the selection is a hanging range that ends in a void
// and the DOM focus is an Element
Expand Down