@@ -171,7 +171,7 @@ const toolbarOptions = [
171
171
] ;
172
172
173
173
const childrenMap = {
174
- value :stringExposingStateControl ( "value" ) ,
174
+ value :stringExposingStateControl ( "value" ) ,
175
175
delta :stringExposingStateControl ( "delta" ) ,
176
176
hideToolbar :BoolControl ,
177
177
readOnly :BoolControl ,
@@ -195,7 +195,7 @@ interface IProps {
195
195
hideToolbar :boolean ;
196
196
readOnly :boolean ;
197
197
autoHeight :boolean ;
198
- onChange :( html :string , deltaJSON :string , text :string ) => void ;
198
+ onChange :( html :string , deltaJSON :string , text :string ) => void ;
199
199
$style :RichTextEditorStyleType ;
200
200
contentScrollBar :boolean ;
201
201
tabIndex ?:number ;
@@ -209,6 +209,13 @@ function RichTextEditor(props: IProps) {
209
209
const wrapperRef = useRef < HTMLDivElement > ( null ) ;
210
210
const editorRef = useRef < ReactQuill > ( null ) ;
211
211
212
+ // know exactly when the editor mounts
213
+ const [ editorReady , setEditorReady ] = useState ( false ) ;
214
+ const setEditorRef = ( node :ReactQuill | null ) => {
215
+ ( editorRef as any ) . current = node as any ;
216
+ setEditorReady ( ! ! node ) ;
217
+ } ;
218
+
212
219
const getQuill = ( ) => ( editorRef . current as any ) ?. getEditor ?.( ) ;
213
220
214
221
const tryParseDelta = ( v :unknown ) => {
@@ -251,7 +258,7 @@ function RichTextEditor(props: IProps) {
251
258
( editor . scroll . domNode as HTMLElement ) . tabIndex = props . tabIndex ;
252
259
}
253
260
}
254
- } , [ props . tabIndex , key ] ) ;
261
+ } , [ props . tabIndex , key ] ) ;
255
262
256
263
const contains = ( parent :HTMLElement , descendant :HTMLElement ) => {
257
264
try {
@@ -267,11 +274,7 @@ function RichTextEditor(props: IProps) {
267
274
268
275
useEffect ( ( ) => {
269
276
const q = getQuill ( ) ;
270
-
271
277
if ( ! q ) {
272
- const v = props . value ?? "" ;
273
- const looksHtml = / < \/ ? [ a - z ] [ \s \S ] * > / i. test ( v ) ;
274
- setContent ( looksHtml ?v :`<p class="">${ v } </p>` ) ;
275
278
return ;
276
279
}
277
280
@@ -282,12 +285,11 @@ function RichTextEditor(props: IProps) {
282
285
setContent ( html ) ;
283
286
return ;
284
287
}
285
-
286
288
const v = props . value ?? "" ;
287
289
const looksHtml = / < \/ ? [ a - z ] [ \s \S ] * > / i. test ( v ) ;
288
290
const html = looksHtml ?v :`<p class="">${ v } </p>` ;
289
291
setContent ( html ) ;
290
- } , [ props . value ] ) ;
292
+ } , [ props . value , editorReady ] ) ;
291
293
292
294
293
295
const handleClickWrapper = ( e :React . MouseEvent < HTMLDivElement > ) => {
@@ -316,7 +318,7 @@ function RichTextEditor(props: IProps) {
316
318
< Suspense fallback = { < Skeleton /> } >
317
319
< ReactQuillEditor
318
320
key = { key }
319
- ref = { editorRef }
321
+ ref = { setEditorRef }
320
322
bounds = { `#${ id } ` }
321
323
modules = { {
322
324
toolbar :JSON . parse ( props . toolbar ) ,
@@ -339,17 +341,26 @@ function RichTextEditor(props: IProps) {
339
341
}
340
342
341
343
const RichTextEditorCompBase = new UICompBuilder ( childrenMap , ( props ) => {
344
+ const propsRef = useRef ( props ) ;
345
+ propsRef . current = props ;
346
+
342
347
const debouncedOnChangeRef = useRef (
343
348
debounce ( ( html :string , deltaJSON :string , text :string ) => {
344
- props . value . onChange ( html ) ;
345
- props . delta . onChange ( deltaJSON ) ;
346
- props . onEvent ( "change" ) ;
347
- } , 1000 )
349
+ propsRef . current . value . onChange ( html ) ;
350
+ propsRef . current . delta . onChange ( deltaJSON ) ;
351
+ propsRef . current . onEvent ( "change" ) ;
352
+ } , 500 )
348
353
) ;
349
354
350
- const handleChange = ( html :string , deltaJSON :string , text :string ) => {
351
- debouncedOnChangeRef . current ?.( html , deltaJSON , text ) ;
355
+ useEffect ( ( ) => {
356
+ return ( ) => {
357
+ debouncedOnChangeRef . current ?. cancel ( ) ;
352
358
} ;
359
+ } , [ ] ) ;
360
+
361
+ const handleChange = ( html :string , deltaJSON :string , text :string ) => {
362
+ debouncedOnChangeRef . current ?.( html , deltaJSON , text ) ;
363
+ } ;
353
364
354
365
return (
355
366
< RichTextEditor