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

marks on double click selection using firefox fix #5580

Merged
merged 6 commits into from
Dec 13, 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/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 }
Loading