fix(richtext-lexical): enable select inputs with ctrl+a or cmd+a (#12453)
Fixes #6871 To review this PR, use `pnpm dev lexical` and the auto-created document in the `lexical fields` collection. Select any input within the blocks and press `cmd+a`. The selection should contain the entire input. I made sure that `cmd+a` still works fine inside the editor but outside of inputs.
This commit is contained in:
@@ -19,6 +19,7 @@ import { DraggableBlockPlugin } from './plugins/handles/DraggableBlockPlugin/ind
|
||||
import { InsertParagraphAtEndPlugin } from './plugins/InsertParagraphAtEnd/index.js'
|
||||
import { MarkdownShortcutPlugin } from './plugins/MarkdownShortcut/index.js'
|
||||
import { NormalizeSelectionPlugin } from './plugins/NormalizeSelection/index.js'
|
||||
import { SelectAllPlugin } from './plugins/SelectAllPlugin/index.js'
|
||||
import { SlashMenuPlugin } from './plugins/SlashMenu/index.js'
|
||||
import { TextPlugin } from './plugins/TextPlugin/index.js'
|
||||
import { LexicalContentEditable } from './ui/ContentEditable.js'
|
||||
@@ -111,6 +112,7 @@ export const LexicalEditor: React.FC<
|
||||
<InsertParagraphAtEndPlugin />
|
||||
<DecoratorPlugin />
|
||||
<TextPlugin features={editorConfig.features} />
|
||||
<SelectAllPlugin />
|
||||
<OnChangePlugin
|
||||
// Selection changes can be ignored here, reducing the
|
||||
// frequency that the FieldComponent and Payload receive updates.
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
|
||||
import { $getSelection, COMMAND_PRIORITY_LOW, SELECT_ALL_COMMAND } from 'lexical'
|
||||
import { useEffect } from 'react'
|
||||
|
||||
/**
|
||||
* Allows to select inputs with `ctrl+a` or `cmd+a`.
|
||||
* Required because Lexical preventDefault the event.
|
||||
* see: https://github.com/payloadcms/payload/issues/6871
|
||||
*/
|
||||
export function SelectAllPlugin() {
|
||||
const [editor] = useLexicalComposerContext()
|
||||
|
||||
useEffect(() => {
|
||||
return editor.registerCommand(
|
||||
SELECT_ALL_COMMAND,
|
||||
() => {
|
||||
const selection = $getSelection()
|
||||
if (selection) {
|
||||
return false
|
||||
}
|
||||
const activeElement = document.activeElement
|
||||
if (activeElement instanceof HTMLInputElement) {
|
||||
activeElement.select()
|
||||
}
|
||||
return true
|
||||
},
|
||||
COMMAND_PRIORITY_LOW,
|
||||
)
|
||||
}, [editor])
|
||||
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user