Skip to content

Commit

Permalink
fix: autofocus nested editors correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Sep 29, 2023
1 parent 0f51f6f commit 930e47d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
11 changes: 3 additions & 8 deletions src/plugins/core/NestedLexicalEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ export interface NestedEditorProps<T extends Mdast.Content> {
* 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
}

/**
Expand All @@ -161,7 +156,7 @@ export interface NestedEditorProps<T extends Mdast.Content> {
*/
export const NestedLexicalEditor = function <T extends Mdast.Content>(props: NestedEditorProps<T>) {
const { getContent, getUpdatedMdastNode, contentEditableProps, block = false } = props
const { mdastNode, lexicalNode } = useNestedEditorContext<T>()
const { mdastNode, lexicalNode, focusEmitter } = useNestedEditorContext<T>()
const updateMdastNode = useMdastNodeUpdater<T>()
const removeNode = useLexicalNodeRemove()
const content = getContent(mdastNode)
Expand All @@ -188,10 +183,10 @@ export const NestedLexicalEditor = function <T extends Mdast.Content>(props: Nes
})

React.useEffect(() => {
props.focusEmitter?.subscribe(() => {
focusEmitter?.subscribe(() => {
editor.focus()
})
}, [editor, props.focusEmitter])
}, [editor, focusEmitter])

React.useEffect(() => {
editor.update(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
Expand Down

0 comments on commit 930e47d

Please sign in to comment.