From 930e47d99bf94f776725aa58f56bd555fb9f5a87 Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Fri, 29 Sep 2023 10:01:24 +0300 Subject: [PATCH] fix: autofocus nested editors correctly --- src/plugins/core/NestedLexicalEditor.tsx | 11 +++-------- src/plugins/core/index.ts | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/plugins/core/NestedLexicalEditor.tsx b/src/plugins/core/NestedLexicalEditor.tsx index 6e32e419..089f170f 100644 --- a/src/plugins/core/NestedLexicalEditor.tsx +++ b/src/plugins/core/NestedLexicalEditor.tsx @@ -136,11 +136,6 @@ export interface NestedEditorProps { * Whether or not the editor edits blocks (multiple paragraphs) */ block?: boolean - - /** - * An emitter (usually constructed in the lexical node) that will activate the editor when the node is focused. - */ - focusEmitter?: VoidEmitter } /** @@ -161,7 +156,7 @@ export interface NestedEditorProps { */ export const NestedLexicalEditor = function (props: NestedEditorProps) { const { getContent, getUpdatedMdastNode, contentEditableProps, block = false } = props - const { mdastNode, lexicalNode } = useNestedEditorContext() + const { mdastNode, lexicalNode, focusEmitter } = useNestedEditorContext() const updateMdastNode = useMdastNodeUpdater() const removeNode = useLexicalNodeRemove() const content = getContent(mdastNode) @@ -188,10 +183,10 @@ export const NestedLexicalEditor = function (props: Nes }) React.useEffect(() => { - props.focusEmitter?.subscribe(() => { + focusEmitter?.subscribe(() => { editor.focus() }) - }, [editor, props.focusEmitter]) + }, [editor, focusEmitter]) React.useEffect(() => { editor.update(() => { diff --git a/src/plugins/core/index.ts b/src/plugins/core/index.ts index 43d11e49..7c8c8359 100644 --- a/src/plugins/core/index.ts +++ b/src/plugins/core/index.ts @@ -415,7 +415,7 @@ export const coreSystem = system((r) => { } if (Object.hasOwn(node, 'select') && typeof node.select === 'function') { // eslint-disable-next-line @typescript-eslint/no-unsafe-call - setTimeout(() => node.select(), 80) + setTimeout(() => node.select()) } }) }