Skip to content

Commit bdb96dd

Browse files
authored
fix(richtext-lexical): inline Block drawers opened when document mounts (#10480)
Fixes #10439
1 parent 686e48d commit bdb96dd

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

packages/richtext-lexical/src/features/blocks/client/componentInline/index.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@ export const InlineBlockComponent: React.FC<Props> = (props) => {
7070
const [editor] = useLexicalComposerContext()
7171
const { i18n, t } = useTranslation<object, string>()
7272
const {
73+
createdInlineBlock,
7374
fieldProps: {
7475
featureClientSchemaMap,
7576
initialLexicalFormState,
7677
permissions,
7778
readOnly,
7879
schemaPath,
7980
},
81+
setCreatedInlineBlock,
8082
uuid: uuidFromContext,
8183
} = useEditorConfigContext()
8284
const { getFormState } = useServerFunctions()
8385
const editDepth = useEditDepth()
84-
const hasMounted = useRef(false)
86+
const firstTimeDrawer = useRef(false)
8587

8688
const [initialState, setInitialState] = React.useState<false | FormState | undefined>(
8789
initialLexicalFormState?.[formData.id]?.formState,
@@ -117,14 +119,17 @@ export const InlineBlockComponent: React.FC<Props> = (props) => {
117119

118120
const clientBlock = blocksField.blocks[0]
119121

120-
// Open drawer on mount
122+
// Open drawer on "mount"
121123
useEffect(() => {
122-
// > 2 because they always have "id" and "blockName" fields
123-
if (!hasMounted.current && clientBlock.fields.length > 2) {
124-
toggleDrawer()
125-
hasMounted.current = true
124+
if (!firstTimeDrawer.current && createdInlineBlock?.getKey() === nodeKey) {
125+
// > 2 because they always have "id" and "blockName" fields
126+
if (clientBlock.fields.length > 2) {
127+
toggleDrawer()
128+
}
129+
setCreatedInlineBlock?.(undefined)
130+
firstTimeDrawer.current = true
126131
}
127-
}, [clientBlock, toggleDrawer])
132+
}, [clientBlock.fields.length, createdInlineBlock, nodeKey, setCreatedInlineBlock, toggleDrawer])
128133

129134
const removeInlineBlock = useCallback(() => {
130135
editor.update(() => {

packages/richtext-lexical/src/features/blocks/client/plugin/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const BlocksPlugin: PluginComponent = () => {
3232

3333
const [targetNodeKey, setTargetNodeKey] = useState<null | string>(null)
3434

35-
const { uuid } = useEditorConfigContext()
35+
const { setCreatedInlineBlock, uuid } = useEditorConfigContext()
3636
const editDepth = useEditDepth()
3737

3838
const drawerSlug = formatDrawerSlug({
@@ -98,6 +98,7 @@ export const BlocksPlugin: PluginComponent = () => {
9898
}
9999

100100
const inlineBlockNode = $createInlineBlockNode(fields as BlockFields)
101+
setCreatedInlineBlock?.(inlineBlockNode)
101102
$insertNodes([inlineBlockNode])
102103
if ($isRootOrShadowRoot(inlineBlockNode.getParentOrThrow())) {
103104
$wrapNodeInElement(inlineBlockNode, $createParagraphNode).selectEnd()
@@ -108,7 +109,7 @@ export const BlocksPlugin: PluginComponent = () => {
108109
COMMAND_PRIORITY_EDITOR,
109110
),
110111
)
111-
}, [editor, targetNodeKey, toggleDrawer])
112+
}, [editor, setCreatedInlineBlock, targetNodeKey, toggleDrawer])
112113

113114
return null
114115
}

packages/richtext-lexical/src/lexical/config/client/EditorConfigProvider.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext
77
import * as React from 'react'
88
import { createContext, useContext, useMemo, useRef, useState } from 'react'
99

10+
import type { InlineBlockNode } from '../../../features/blocks/client/nodes/InlineBlocksNode.js'
1011
import type { LexicalRichTextFieldProps } from '../../../types.js'
1112
import type { SanitizedClientEditorConfig } from '../types.js'
1213

@@ -19,16 +20,18 @@ export interface EditorConfigContextType {
1920
// Editor focus handling
2021
blurEditor: (editorContext: EditorConfigContextType) => void
2122
childrenEditors: React.RefObject<Map<string, EditorConfigContextType>>
23+
createdInlineBlock?: InlineBlockNode
2224
editor: LexicalEditor
2325
editorConfig: SanitizedClientEditorConfig
24-
editorContainerRef: React.RefObject<HTMLDivElement>
2526

27+
editorContainerRef: React.RefObject<HTMLDivElement>
2628
fieldProps: MarkRequired<LexicalRichTextFieldProps, 'path' | 'schemaPath'>
2729
focusedEditor: EditorConfigContextType | null
2830
// Editor focus handling
2931
focusEditor: (editorContext: EditorConfigContextType) => void
3032
parentEditor: EditorConfigContextType
3133
registerChild: (uuid: string, editorContext: EditorConfigContextType) => void
34+
setCreatedInlineBlock?: React.Dispatch<React.SetStateAction<InlineBlockNode | undefined>>
3235
unregisterChild?: (uuid: string) => void
3336
uuid: string
3437
}
@@ -61,6 +64,7 @@ export const EditorConfigProvider = ({
6164
const childrenEditors = useRef<Map<string, EditorConfigContextType>>(new Map())
6265
const [focusedEditor, setFocusedEditor] = useState<EditorConfigContextType | null>(null)
6366
const focusHistory = useRef<Set<string>>(new Set())
67+
const [createdInlineBlock, setCreatedInlineBlock] = useState<InlineBlockNode>()
6468

6569
const editorContext = useMemo(
6670
() =>
@@ -70,6 +74,7 @@ export const EditorConfigProvider = ({
7074
focusHistory.current.clear() // Reset focus history when focus is lost
7175
},
7276
childrenEditors,
77+
createdInlineBlock,
7378
editor,
7479
editorConfig,
7580
editorContainerRef,
@@ -105,6 +110,7 @@ export const EditorConfigProvider = ({
105110
childrenEditors.current = newMap
106111
}
107112
},
113+
setCreatedInlineBlock,
108114
unregisterChild: (childUUID) => {
109115
if (childrenEditors.current.has(childUUID)) {
110116
const newMap = new Map(childrenEditors.current)
@@ -116,6 +122,8 @@ export const EditorConfigProvider = ({
116122
uuid,
117123
}) as EditorConfigContextType,
118124
[
125+
createdInlineBlock,
126+
setCreatedInlineBlock,
119127
editor,
120128
childrenEditors,
121129
editorConfig,

0 commit comments

Comments
 (0)