🐛 (textBubble) Fix text bubble not updating when focusing on another one
Closes #1400
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from 'react'
|
import React, { useEffect, useRef } from 'react'
|
||||||
import { Plate } from '@udecode/plate-core'
|
import { Plate } from '@udecode/plate-core'
|
||||||
import { platePlugins } from '@/lib/plate'
|
import { platePlugins } from '@/lib/plate'
|
||||||
import { TElement } from '@udecode/plate-common'
|
import { TElement } from '@udecode/plate-common'
|
||||||
@@ -7,18 +7,28 @@ import { TextEditorEditorContent } from './TextEditorEditorContent'
|
|||||||
type TextBubbleEditorContentProps = {
|
type TextBubbleEditorContentProps = {
|
||||||
id: string
|
id: string
|
||||||
initialValue: TElement[]
|
initialValue: TElement[]
|
||||||
onClose: (newContent: TElement[]) => void
|
onChange: (newContent: TElement[]) => void
|
||||||
|
onClose: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TextBubbleEditor = ({
|
export const TextBubbleEditor = ({
|
||||||
id,
|
id,
|
||||||
initialValue,
|
initialValue,
|
||||||
|
onChange,
|
||||||
onClose,
|
onClose,
|
||||||
}: TextBubbleEditorContentProps) => {
|
}: TextBubbleEditorContentProps) => {
|
||||||
const [textEditorValue, setTextEditorValue] =
|
const textEditorValueRef = useRef<TElement[]>(initialValue)
|
||||||
useState<TElement[]>(initialValue)
|
|
||||||
|
|
||||||
const closeEditor = () => onClose(textEditorValue)
|
const setTextEditorValue = (newValue: TElement[]) => {
|
||||||
|
textEditorValueRef.current = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
onChange(textEditorValueRef.current)
|
||||||
|
},
|
||||||
|
[onChange]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Plate
|
<Plate
|
||||||
@@ -31,7 +41,7 @@ export const TextBubbleEditor = ({
|
|||||||
}
|
}
|
||||||
onChange={setTextEditorValue}
|
onChange={setTextEditorValue}
|
||||||
>
|
>
|
||||||
<TextEditorEditorContent closeEditor={closeEditor} />
|
<TextEditorEditorContent closeEditor={onClose} />
|
||||||
</Plate>
|
</Plate>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 } }
|
const updatedBlock = { ...block, content: { richText: content } }
|
||||||
updateBlock(indices, updatedBlock)
|
updateBlock(indices, updatedBlock)
|
||||||
setOpenedBlockId(undefined)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleClick = (e: React.MouseEvent) => {
|
const handleClick = (e: React.MouseEvent) => {
|
||||||
@@ -223,6 +226,7 @@ export const BlockNode = ({
|
|||||||
<TextBubbleEditor
|
<TextBubbleEditor
|
||||||
id={block.id}
|
id={block.id}
|
||||||
initialValue={block.content?.richText ?? []}
|
initialValue={block.content?.richText ?? []}
|
||||||
|
onChange={handleTextEditorChange}
|
||||||
onClose={handleCloseEditor}
|
onClose={handleCloseEditor}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
Reference in New Issue
Block a user