@@ -16,11 +16,12 @@ import {
16
16
PropType ,
17
17
watch ,
18
18
ref ,
19
+ shallowRef ,
19
20
h ,
20
21
} from 'vue'
21
22
import { toolbarOptions , ToolbarOptions } from './options'
22
23
23
- export type Module = { name : string ; module : any ; options ?: object }
24
+ export type Module = { name : string ; module : unknown ; options ?: object }
24
25
25
26
export const QuillEditor = defineComponent ( {
26
27
name : 'QuillEditor' ,
@@ -161,7 +162,7 @@ export const QuillEditor = defineComponent({
161
162
}
162
163
if ( props . modules ) {
163
164
const modules = ( ( ) => {
164
- const modulesOption : { [ key : string ] : any } = { }
165
+ const modulesOption : { [ key : string ] : unknown } = { }
165
166
if ( Array . isArray ( props . modules ) ) {
166
167
for ( const module of props . modules ) {
167
168
modulesOption [ module . name ] = module . options ?? { }
@@ -185,13 +186,32 @@ export const QuillEditor = defineComponent({
185
186
)
186
187
}
187
188
189
+ const internalModel = shallowRef ( props . content )
190
+ const internalModelEquals = ( against : Delta | String | undefined ) => {
191
+ if ( typeof internalModel . value === typeof against ) {
192
+ if ( against === internalModel . value ) {
193
+ return true
194
+ }
195
+ if ( against instanceof Delta && internalModel . value instanceof Delta ) {
196
+ return internalModel . value . diff ( against ) . length ( ) > 0
197
+ }
198
+ }
199
+ return false
200
+ }
188
201
const handleTextChange : TextChangeHandler = (
189
202
delta : Delta ,
190
203
oldContents : Delta ,
191
204
source : Sources
192
205
) => {
193
- // Update v-model:content when text changes
194
- ctx . emit ( 'update:content' , getContents ( ) )
206
+ const content = getContents ( )
207
+ if ( content ) {
208
+ // Quill should never be null at this point, so content should not be undefined but let's make ts happy
209
+ internalModel . value = content
210
+ // Update v-model:content when text changes
211
+ if ( ! internalModelEquals ( props . content ) ) {
212
+ ctx . emit ( 'update:content' , internalModel . value )
213
+ }
214
+ }
195
215
ctx . emit ( 'textChange' , { delta, oldContents, source } )
196
216
}
197
217
@@ -202,7 +222,7 @@ export const QuillEditor = defineComponent({
202
222
source : Sources
203
223
) => {
204
224
// Set isEditorFocus if quill.hasFocus()
205
- isEditorFocus . value = quill ?. hasFocus ( ) ? true : false
225
+ isEditorFocus . value = ! ! quill ?. hasFocus ( )
206
226
ctx . emit ( 'selectionChange' , { range, oldRange, source } )
207
227
}
208
228
@@ -306,13 +326,20 @@ export const QuillEditor = defineComponent({
306
326
} )
307
327
}
308
328
309
- // watch(
310
- // () => props.content,
311
- // (newContent, oldContents) => {
312
- // if (!quill || !newContent || newContent === oldContents) return
313
- // setContents(newContent)
314
- // }
315
- // )
329
+ watch (
330
+ ( ) => props . content ,
331
+ ( newContent ) => {
332
+ if ( ! quill || ! newContent || internalModelEquals ( newContent ) ) return
333
+
334
+ internalModel . value = newContent
335
+ // Restore the selection and cursor position after updating the content
336
+ const selection = quill . getSelection ( )
337
+ if ( selection ) {
338
+ nextTick ( ( ) => quill ?. setSelection ( selection ) )
339
+ }
340
+ setContents ( newContent )
341
+ }
342
+ )
316
343
317
344
watch (
318
345
( ) => props . enable ,
0 commit comments