Skip to content

Commit 0e5ff24

Browse files
authored
fix(richtext-lexical): preserve selection in Firefox when using LexicalMenu (#10867)
Fixes #10724 The selection is never touched in an `editor.read`, but BEFORE starting an `editor.update` it is synced with `window.selection`. Firefox for some reason loses the editor selection, so on the next update the selection is null. For reference, there was a brief discussion on the Lexical Discord server: https://discord.com/channels/953974421008293909/1333916489870348309
1 parent 3094c92 commit 0e5ff24

File tree

1 file changed

+13
-1
lines changed
  • packages/richtext-lexical/src/lexical/plugins/SlashMenu/LexicalTypeaheadMenuPlugin

1 file changed

+13
-1
lines changed

packages/richtext-lexical/src/lexical/plugins/SlashMenu/LexicalTypeaheadMenuPlugin/LexicalMenu.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use client'
2-
import type { LexicalCommand, LexicalEditor, TextNode } from 'lexical'
2+
import type { BaseSelection, LexicalCommand, LexicalEditor, TextNode } from 'lexical'
33
import type { JSX, ReactPortal, RefObject } from 'react'
44

55
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext.js'
66
import { mergeRegister } from '@lexical/utils'
77
import {
88
$getSelection,
99
$isRangeSelection,
10+
$setSelection,
1011
COMMAND_PRIORITY_LOW,
1112
createCommand,
1213
KEY_ARROW_DOWN_COMMAND,
@@ -266,6 +267,17 @@ export function LexicalMenu({
266267
})
267268

268269
setTimeout(() => {
270+
// Needed in Firefox. See https://github.com/payloadcms/payload/issues/10724
271+
let selection: BaseSelection | undefined
272+
editor.read(() => {
273+
selection = $getSelection()?.clone()
274+
})
275+
editor.update(() => {
276+
if (selection) {
277+
$setSelection(selection)
278+
}
279+
})
280+
269281
selectedItem.onSelect({
270282
editor,
271283
queryString: resolution.match ? resolution.match.matchingString : '',

0 commit comments

Comments
 (0)