From f63f57c6493ea3e4b27271dbef04976c66408936 Mon Sep 17 00:00:00 2001 From: heka Date: Fri, 22 Nov 2024 19:33:50 +0900 Subject: [PATCH 1/2] fix: caption text area korean ime issue --- .../src/react/components/CaptionTextarea.tsx | 87 +++++++++++++------ 1 file changed, 59 insertions(+), 28 deletions(-) diff --git a/packages/caption/src/react/components/CaptionTextarea.tsx b/packages/caption/src/react/components/CaptionTextarea.tsx index 53087061aa..6ff040a84a 100644 --- a/packages/caption/src/react/components/CaptionTextarea.tsx +++ b/packages/caption/src/react/components/CaptionTextarea.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useCallback, useState } from 'react'; import type { TextareaAutosizeProps } from 'react-textarea-autosize'; @@ -49,25 +49,59 @@ export const useCaptionTextareaState = () => { const element = useElement(); const editor = useEditorRef(); - const { - caption: nodeCaption = [{ children: [{ text: '' }] }] as [TElement], - } = element; + const [isComposing, setIsComposing] = useState(false) + + const [captionValue, setCaptionValue] = useState< + TextareaAutosizeProps['value'] + >(() => { + const nodeCaption = + element.caption ?? ([{ children: [{ text: '' }] }] as [TElement]) + return getNodeString(nodeCaption[0]) + }) + + const updateEditorCaptionValue = useCallback( + (newValue: string) => { + const path = findNodePath(editor, element) + if (!path) { + return + } - const captionValue: TextareaAutosizeProps['value'] = getNodeString( - nodeCaption[0] - ); + setNodes( + editor, + { caption: [{ text: newValue }] }, + { at: path }, + ) + }, + [editor, element], + ) - function setCaptionValue(newValue: TextareaAutosizeProps['value']) { - const path = findNodePath(editor, element); + const handleChange = useCallback( + (e: React.ChangeEvent) => { + const newValue = e.target.value + setCaptionValue(newValue) + + if (!isComposing) { + updateEditorCaptionValue(newValue) + } + }, + [isComposing, updateEditorCaptionValue], + ) + + const handleCompositionStart = useCallback(() => { + setIsComposing(true) + }, []) + + const handleCompositionEnd = useCallback( + (e: React.CompositionEvent) => { + setIsComposing(false) + const newValue = e.currentTarget.value + setCaptionValue(newValue) + updateEditorCaptionValue(newValue) + }, + [updateEditorCaptionValue], + ) - if (!path) return; - setNodes( - editor, - { caption: [{ text: newValue }] }, - { at: path } - ); - } const readOnly = useReadOnly(); @@ -79,7 +113,9 @@ export const useCaptionTextareaState = () => { captionValue, element, readOnly, - setCaptionValue, + handleChange, + handleCompositionStart, + handleCompositionEnd, textareaRef, }; }; @@ -88,20 +124,13 @@ export const useCaptionTextarea = ({ captionValue, element, readOnly, - setCaptionValue, + handleChange, + handleCompositionStart, + handleCompositionEnd, textareaRef, }: ReturnType) => { const editor = useEditorRef(); - const onChange: TextareaAutosizeProps['onChange'] = React.useCallback( - (e: React.ChangeEvent) => { - const newValue = e.target.value; - - setCaptionValue(newValue); - }, - [setCaptionValue] - ); - const onKeyDown: TextareaAutosizeProps['onKeyDown'] = (e) => { // select image if (isHotkey('up', e)) { @@ -142,8 +171,10 @@ export const useCaptionTextarea = ({ readOnly, value: captionValue, onBlur, - onChange, onKeyDown, + onChange: handleChange, + onCompositionStart: handleCompositionStart, + onCompositionEnd: handleCompositionEnd, }, ref: textareaRef, }; From 3861e69d142a988c3bde4c1d0d1a42f565a7bbbd Mon Sep 17 00:00:00 2001 From: Ziad Beyens Date: Fri, 22 Nov 2024 13:03:04 +0100 Subject: [PATCH 2/2] Create beige-peaches-divide.md --- .changeset/beige-peaches-divide.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/beige-peaches-divide.md diff --git a/.changeset/beige-peaches-divide.md b/.changeset/beige-peaches-divide.md new file mode 100644 index 0000000000..2e760b3fb5 --- /dev/null +++ b/.changeset/beige-peaches-divide.md @@ -0,0 +1,5 @@ +--- +"@udecode/plate-caption": patch +--- + +fix: caption text area korean ime issue