Skip to content

Commit f63f57c

Browse files
committed
fix: caption text area korean ime issue
1 parent 64f5940 commit f63f57c

File tree

1 file changed

+59
-28
lines changed

1 file changed

+59
-28
lines changed

packages/caption/src/react/components/CaptionTextarea.tsx

+59-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useCallback, useState } from 'react';
22

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

@@ -49,25 +49,59 @@ export const useCaptionTextareaState = () => {
4949
const element = useElement<TCaptionElement>();
5050
const editor = useEditorRef();
5151

52-
const {
53-
caption: nodeCaption = [{ children: [{ text: '' }] }] as [TElement],
54-
} = element;
52+
const [isComposing, setIsComposing] = useState(false)
53+
54+
const [captionValue, setCaptionValue] = useState<
55+
TextareaAutosizeProps['value']
56+
>(() => {
57+
const nodeCaption =
58+
element.caption ?? ([{ children: [{ text: '' }] }] as [TElement])
59+
return getNodeString(nodeCaption[0])
60+
})
61+
62+
const updateEditorCaptionValue = useCallback(
63+
(newValue: string) => {
64+
const path = findNodePath(editor, element)
65+
if (!path) {
66+
return
67+
}
5568

56-
const captionValue: TextareaAutosizeProps['value'] = getNodeString(
57-
nodeCaption[0]
58-
);
69+
setNodes<TCaptionElement>(
70+
editor,
71+
{ caption: [{ text: newValue }] },
72+
{ at: path },
73+
)
74+
},
75+
[editor, element],
76+
)
5977

60-
function setCaptionValue(newValue: TextareaAutosizeProps['value']) {
61-
const path = findNodePath(editor, element);
78+
const handleChange = useCallback(
79+
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
80+
const newValue = e.target.value
81+
setCaptionValue(newValue)
82+
83+
if (!isComposing) {
84+
updateEditorCaptionValue(newValue)
85+
}
86+
},
87+
[isComposing, updateEditorCaptionValue],
88+
)
89+
90+
const handleCompositionStart = useCallback(() => {
91+
setIsComposing(true)
92+
}, [])
93+
94+
const handleCompositionEnd = useCallback(
95+
(e: React.CompositionEvent<HTMLTextAreaElement>) => {
96+
setIsComposing(false)
97+
const newValue = e.currentTarget.value
98+
setCaptionValue(newValue)
99+
updateEditorCaptionValue(newValue)
100+
},
101+
[updateEditorCaptionValue],
102+
)
62103

63-
if (!path) return;
64104

65-
setNodes<TCaptionElement>(
66-
editor,
67-
{ caption: [{ text: newValue }] },
68-
{ at: path }
69-
);
70-
}
71105

72106
const readOnly = useReadOnly();
73107

@@ -79,7 +113,9 @@ export const useCaptionTextareaState = () => {
79113
captionValue,
80114
element,
81115
readOnly,
82-
setCaptionValue,
116+
handleChange,
117+
handleCompositionStart,
118+
handleCompositionEnd,
83119
textareaRef,
84120
};
85121
};
@@ -88,20 +124,13 @@ export const useCaptionTextarea = ({
88124
captionValue,
89125
element,
90126
readOnly,
91-
setCaptionValue,
127+
handleChange,
128+
handleCompositionStart,
129+
handleCompositionEnd,
92130
textareaRef,
93131
}: ReturnType<typeof useCaptionTextareaState>) => {
94132
const editor = useEditorRef();
95133

96-
const onChange: TextareaAutosizeProps['onChange'] = React.useCallback(
97-
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
98-
const newValue = e.target.value;
99-
100-
setCaptionValue(newValue);
101-
},
102-
[setCaptionValue]
103-
);
104-
105134
const onKeyDown: TextareaAutosizeProps['onKeyDown'] = (e) => {
106135
// select image
107136
if (isHotkey('up', e)) {
@@ -142,8 +171,10 @@ export const useCaptionTextarea = ({
142171
readOnly,
143172
value: captionValue,
144173
onBlur,
145-
onChange,
146174
onKeyDown,
175+
onChange: handleChange,
176+
onCompositionStart: handleCompositionStart,
177+
onCompositionEnd: handleCompositionEnd,
147178
},
148179
ref: textareaRef,
149180
};

0 commit comments

Comments
 (0)