1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 20:40:19 +02:00

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

* fix node edges selection on firefox

* add changeset

* Fix type signatures

---------

Co-authored-by: Jawell <skytor08@gmail.com>
This commit is contained in:
Joe Anderson
2023-01-30 05:11:19 +00:00
committed by GitHub
parent a1b558ac72
commit 5bc69d8d65
2 changed files with 52 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': major
---
Firefox: fix wrong text highlighting with double-click

View File

@@ -534,7 +534,7 @@ export const ReactEditor = {
editor: ReactEditor, editor: ReactEditor,
domPoint: DOMPoint, domPoint: DOMPoint,
options: { options: {
exactMatch: T exactMatch: boolean
suppressThrow: T suppressThrow: T
} }
): T extends true ? Point | null : Point { ): T extends true ? Point | null : Point {
@@ -703,7 +703,7 @@ export const ReactEditor = {
editor: ReactEditor, editor: ReactEditor,
domRange: DOMRange | DOMStaticRange | DOMSelection, domRange: DOMRange | DOMStaticRange | DOMSelection,
options: { options: {
exactMatch: T exactMatch: boolean
suppressThrow: T suppressThrow: T
} }
): T extends true ? Range | null : Range { ): T extends true ? Range | null : Range {
@@ -754,16 +754,15 @@ export const ReactEditor = {
) )
} }
const anchor = ReactEditor.toSlatePoint( let anchor = ReactEditor.toSlatePoint(editor, [anchorNode, anchorOffset], {
editor, exactMatch,
[anchorNode, anchorOffset], suppressThrow,
{ exactMatch, suppressThrow } })
)
if (!anchor) { if (!anchor) {
return null as T extends true ? Range | null : Range return null as T extends true ? Range | null : Range
} }
const focus = isCollapsed let focus = isCollapsed
? anchor ? anchor
: ReactEditor.toSlatePoint(editor, [focusNode, focusOffset], { : ReactEditor.toSlatePoint(editor, [focusNode, focusOffset], {
exactMatch, exactMatch,
@@ -773,6 +772,46 @@ export const ReactEditor = {
return null as T extends true ? Range | null : Range 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 } let range: Range = { anchor: anchor as Point, focus: focus as Point }
// if the selection is a hanging range that ends in a void // if the selection is a hanging range that ends in a void
// and the DOM focus is an Element // and the DOM focus is an Element