Skip to content

Commit

Permalink
Merge pull request #3799 from siosio34/main
Browse files Browse the repository at this point in the history
fix: caption text area korean ime issue
  • Loading branch information
zbeyens authored Nov 22, 2024
2 parents 64f5940 + 3861e69 commit 846d5ec
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-peaches-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-caption": patch
---

fix: caption text area korean ime issue
87 changes: 59 additions & 28 deletions packages/caption/src/react/components/CaptionTextarea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useState } from 'react';

import type { TextareaAutosizeProps } from 'react-textarea-autosize';

Expand Down Expand Up @@ -49,25 +49,59 @@ export const useCaptionTextareaState = () => {
const element = useElement<TCaptionElement>();
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<TCaptionElement>(
editor,
{ caption: [{ text: newValue }] },
{ at: path },
)
},
[editor, element],
)

function setCaptionValue(newValue: TextareaAutosizeProps['value']) {
const path = findNodePath(editor, element);
const handleChange = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newValue = e.target.value
setCaptionValue(newValue)

if (!isComposing) {
updateEditorCaptionValue(newValue)
}
},
[isComposing, updateEditorCaptionValue],
)

const handleCompositionStart = useCallback(() => {
setIsComposing(true)
}, [])

const handleCompositionEnd = useCallback(
(e: React.CompositionEvent<HTMLTextAreaElement>) => {
setIsComposing(false)
const newValue = e.currentTarget.value
setCaptionValue(newValue)
updateEditorCaptionValue(newValue)
},
[updateEditorCaptionValue],
)

if (!path) return;

setNodes<TCaptionElement>(
editor,
{ caption: [{ text: newValue }] },
{ at: path }
);
}

const readOnly = useReadOnly();

Expand All @@ -79,7 +113,9 @@ export const useCaptionTextareaState = () => {
captionValue,
element,
readOnly,
setCaptionValue,
handleChange,
handleCompositionStart,
handleCompositionEnd,
textareaRef,
};
};
Expand All @@ -88,20 +124,13 @@ export const useCaptionTextarea = ({
captionValue,
element,
readOnly,
setCaptionValue,
handleChange,
handleCompositionStart,
handleCompositionEnd,
textareaRef,
}: ReturnType<typeof useCaptionTextareaState>) => {
const editor = useEditorRef();

const onChange: TextareaAutosizeProps['onChange'] = React.useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newValue = e.target.value;

setCaptionValue(newValue);
},
[setCaptionValue]
);

const onKeyDown: TextareaAutosizeProps['onKeyDown'] = (e) => {
// select image
if (isHotkey('up', e)) {
Expand Down Expand Up @@ -142,8 +171,10 @@ export const useCaptionTextarea = ({
readOnly,
value: captionValue,
onBlur,
onChange,
onKeyDown,
onChange: handleChange,
onCompositionStart: handleCompositionStart,
onCompositionEnd: handleCompositionEnd,
},
ref: textareaRef,
};
Expand Down

0 comments on commit 846d5ec

Please sign in to comment.