Skip to content

Commit

Permalink
marks on double click selection using firefox fix (#5580)
Browse files Browse the repository at this point in the history
* fixed: marks on double click selection using firefox

* adding changeset

* minor -> patch

* removing useless platform check

* yarn fix

* Add test

---------

Co-authored-by: Joe Anderson <[email protected]>
  • Loading branch information
tdaron and 12joan authored Dec 13, 2023
1 parent bf5a4ab commit a374895
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-flies-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

Fix firefox double-click marks issue
24 changes: 22 additions & 2 deletions packages/slate/src/editor/marks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ import { Range } from '../interfaces/range'
import { Path } from '../interfaces/path'
import { Text } from '../interfaces/text'
import { Element } from '../interfaces/element'
import { Point } from '../interfaces'

export const marks: EditorInterface['marks'] = (editor, options = {}) => {
const { marks, selection } = editor

if (!selection) {
return null
}
let { anchor, focus } = selection

if (marks) {
return marks
}

if (Range.isExpanded(selection)) {
const [match] = Editor.nodes(editor, { match: Text.isText })
/**
* COMPAT: Make sure hanging ranges (caused by double clicking in Firefox)
* do not adversely affect the returned marks.
*/
const isEnd = Editor.isEnd(editor, anchor, anchor.path)
if (isEnd) {
const after = Editor.after(editor, anchor as Point)
if (after) {
anchor = after
}
}

const [match] = Editor.nodes(editor, {
match: Text.isText,
at: {
anchor,
focus,
},
})

if (match) {
const [node] = match as NodeEntry<Text>
Expand All @@ -28,8 +48,8 @@ export const marks: EditorInterface['marks'] = (editor, options = {}) => {
}
}

const { anchor } = selection
const { path } = anchor

let [node] = Editor.leaf(editor, path)

if (anchor.offset === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../../..'

/**
* This test verifies that when double clicking a marked word in Firefox,
* Editor.marks for the resulting selection includes the marked word. Double
* clicking a marked word in Firefox results in a selection that starts at the
* end of the previous text node and ends at the end of the marked text node.
*/

export const input = (
<editor>
<block>
plain <anchor />
<text bold>
bold
<focus />
</text>
<text> plain</text>
</block>
<block>block two</block>
</editor>
)
export const test = editor => {
return Editor.marks(editor)
}
export const output = { bold: true }

0 comments on commit a374895

Please sign in to comment.