1
- import React from 'react' ;
1
+ import React , { useCallback , useState } from 'react' ;
2
2
3
3
import type { TextareaAutosizeProps } from 'react-textarea-autosize' ;
4
4
@@ -49,25 +49,59 @@ export const useCaptionTextareaState = () => {
49
49
const element = useElement < TCaptionElement > ( ) ;
50
50
const editor = useEditorRef ( ) ;
51
51
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
+ }
55
68
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
+ )
59
77
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
+ )
62
103
63
- if ( ! path ) return ;
64
104
65
- setNodes < TCaptionElement > (
66
- editor ,
67
- { caption : [ { text : newValue } ] } ,
68
- { at : path }
69
- ) ;
70
- }
71
105
72
106
const readOnly = useReadOnly ( ) ;
73
107
@@ -79,7 +113,9 @@ export const useCaptionTextareaState = () => {
79
113
captionValue,
80
114
element,
81
115
readOnly,
82
- setCaptionValue,
116
+ handleChange,
117
+ handleCompositionStart,
118
+ handleCompositionEnd,
83
119
textareaRef,
84
120
} ;
85
121
} ;
@@ -88,20 +124,13 @@ export const useCaptionTextarea = ({
88
124
captionValue,
89
125
element,
90
126
readOnly,
91
- setCaptionValue,
127
+ handleChange,
128
+ handleCompositionStart,
129
+ handleCompositionEnd,
92
130
textareaRef,
93
131
} : ReturnType < typeof useCaptionTextareaState > ) => {
94
132
const editor = useEditorRef ( ) ;
95
133
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
-
105
134
const onKeyDown : TextareaAutosizeProps [ 'onKeyDown' ] = ( e ) => {
106
135
// select image
107
136
if ( isHotkey ( 'up' , e ) ) {
@@ -142,8 +171,10 @@ export const useCaptionTextarea = ({
142
171
readOnly,
143
172
value : captionValue ,
144
173
onBlur,
145
- onChange,
146
174
onKeyDown,
175
+ onChange : handleChange ,
176
+ onCompositionStart : handleCompositionStart ,
177
+ onCompositionEnd : handleCompositionEnd ,
147
178
} ,
148
179
ref : textareaRef ,
149
180
} ;
0 commit comments