2
0

🐛 (textBubble) Fix text bubble not updating when focusing on another one

Closes #1400
This commit is contained in:
Baptiste Arnaud
2024-04-02 11:45:49 +02:00
parent 2663ca2e18
commit c9b7f6a7b6
2 changed files with 22 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useRef } from 'react'
import { Plate } from '@udecode/plate-core'
import { platePlugins } from '@/lib/plate'
import { TElement } from '@udecode/plate-common'
@ -7,18 +7,28 @@ import { TextEditorEditorContent } from './TextEditorEditorContent'
type TextBubbleEditorContentProps = {
id: string
initialValue: TElement[]
onClose: (newContent: TElement[]) => void
onChange: (newContent: TElement[]) => void
onClose: () => void
}
export const TextBubbleEditor = ({
id,
initialValue,
onChange,
onClose,
}: TextBubbleEditorContentProps) => {
const [textEditorValue, setTextEditorValue] =
useState<TElement[]>(initialValue)
const textEditorValueRef = useRef<TElement[]>(initialValue)
const closeEditor = () => onClose(textEditorValue)
const setTextEditorValue = (newValue: TElement[]) => {
textEditorValueRef.current = newValue
}
useEffect(
() => () => {
onChange(textEditorValueRef.current)
},
[onChange]
)
return (
<Plate
@ -31,7 +41,7 @@ export const TextBubbleEditor = ({
}
onChange={setTextEditorValue}
>
<TextEditorEditorContent closeEditor={closeEditor} />
<TextEditorEditorContent closeEditor={onClose} />
</Plate>
)
}

View File

@ -144,10 +144,13 @@ export const BlockNode = ({
})
}
const handleCloseEditor = (content: TElement[]) => {
const handleCloseEditor = () => {
setOpenedBlockId(undefined)
}
const handleTextEditorChange = (content: TElement[]) => {
const updatedBlock = { ...block, content: { richText: content } }
updateBlock(indices, updatedBlock)
setOpenedBlockId(undefined)
}
const handleClick = (e: React.MouseEvent) => {
@ -223,6 +226,7 @@ export const BlockNode = ({
<TextBubbleEditor
id={block.id}
initialValue={block.content?.richText ?? []}
onChange={handleTextEditorChange}
onClose={handleCloseEditor}
/>
) : (