Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: caption text area korean ime issue #3799

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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