Skip to content

Commit

Permalink
fix(richtext-lexical): inline Block drawers opened when document moun…
Browse files Browse the repository at this point in the history
…ts (#10480)

Fixes #10439
  • Loading branch information
GermanJablo authored Jan 9, 2025
1 parent 686e48d commit bdb96dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,20 @@ export const InlineBlockComponent: React.FC<Props> = (props) => {
const [editor] = useLexicalComposerContext()
const { i18n, t } = useTranslation<object, string>()
const {
createdInlineBlock,
fieldProps: {
featureClientSchemaMap,
initialLexicalFormState,
permissions,
readOnly,
schemaPath,
},
setCreatedInlineBlock,
uuid: uuidFromContext,
} = useEditorConfigContext()
const { getFormState } = useServerFunctions()
const editDepth = useEditDepth()
const hasMounted = useRef(false)
const firstTimeDrawer = useRef(false)

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

const clientBlock = blocksField.blocks[0]

// Open drawer on mount
// Open drawer on "mount"
useEffect(() => {
// > 2 because they always have "id" and "blockName" fields
if (!hasMounted.current && clientBlock.fields.length > 2) {
toggleDrawer()
hasMounted.current = true
if (!firstTimeDrawer.current && createdInlineBlock?.getKey() === nodeKey) {
// > 2 because they always have "id" and "blockName" fields
if (clientBlock.fields.length > 2) {
toggleDrawer()
}
setCreatedInlineBlock?.(undefined)
firstTimeDrawer.current = true
}
}, [clientBlock, toggleDrawer])
}, [clientBlock.fields.length, createdInlineBlock, nodeKey, setCreatedInlineBlock, toggleDrawer])

const removeInlineBlock = useCallback(() => {
editor.update(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const BlocksPlugin: PluginComponent = () => {

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

const { uuid } = useEditorConfigContext()
const { setCreatedInlineBlock, uuid } = useEditorConfigContext()
const editDepth = useEditDepth()

const drawerSlug = formatDrawerSlug({
Expand Down Expand Up @@ -98,6 +98,7 @@ export const BlocksPlugin: PluginComponent = () => {
}

const inlineBlockNode = $createInlineBlockNode(fields as BlockFields)
setCreatedInlineBlock?.(inlineBlockNode)
$insertNodes([inlineBlockNode])
if ($isRootOrShadowRoot(inlineBlockNode.getParentOrThrow())) {
$wrapNodeInElement(inlineBlockNode, $createParagraphNode).selectEnd()
Expand All @@ -108,7 +109,7 @@ export const BlocksPlugin: PluginComponent = () => {
COMMAND_PRIORITY_EDITOR,
),
)
}, [editor, targetNodeKey, toggleDrawer])
}, [editor, setCreatedInlineBlock, targetNodeKey, toggleDrawer])

return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext
import * as React from 'react'
import { createContext, useContext, useMemo, useRef, useState } from 'react'

import type { InlineBlockNode } from '../../../features/blocks/client/nodes/InlineBlocksNode.js'
import type { LexicalRichTextFieldProps } from '../../../types.js'
import type { SanitizedClientEditorConfig } from '../types.js'

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

editorContainerRef: React.RefObject<HTMLDivElement>
fieldProps: MarkRequired<LexicalRichTextFieldProps, 'path' | 'schemaPath'>
focusedEditor: EditorConfigContextType | null
// Editor focus handling
focusEditor: (editorContext: EditorConfigContextType) => void
parentEditor: EditorConfigContextType
registerChild: (uuid: string, editorContext: EditorConfigContextType) => void
setCreatedInlineBlock?: React.Dispatch<React.SetStateAction<InlineBlockNode | undefined>>
unregisterChild?: (uuid: string) => void
uuid: string
}
Expand Down Expand Up @@ -61,6 +64,7 @@ export const EditorConfigProvider = ({
const childrenEditors = useRef<Map<string, EditorConfigContextType>>(new Map())
const [focusedEditor, setFocusedEditor] = useState<EditorConfigContextType | null>(null)
const focusHistory = useRef<Set<string>>(new Set())
const [createdInlineBlock, setCreatedInlineBlock] = useState<InlineBlockNode>()

const editorContext = useMemo(
() =>
Expand All @@ -70,6 +74,7 @@ export const EditorConfigProvider = ({
focusHistory.current.clear() // Reset focus history when focus is lost
},
childrenEditors,
createdInlineBlock,
editor,
editorConfig,
editorContainerRef,
Expand Down Expand Up @@ -105,6 +110,7 @@ export const EditorConfigProvider = ({
childrenEditors.current = newMap
}
},
setCreatedInlineBlock,
unregisterChild: (childUUID) => {
if (childrenEditors.current.has(childUUID)) {
const newMap = new Map(childrenEditors.current)
Expand All @@ -116,6 +122,8 @@ export const EditorConfigProvider = ({
uuid,
}) as EditorConfigContextType,
[
createdInlineBlock,
setCreatedInlineBlock,
editor,
childrenEditors,
editorConfig,
Expand Down

0 comments on commit bdb96dd

Please sign in to comment.