Skip to content

Commit

Permalink
Merge pull request #317 from Tofandel/master
Browse files Browse the repository at this point in the history
fix: delta comparison
  • Loading branch information
luthfimasruri authored Feb 4, 2023
2 parents 545cfa4 + f96121b commit dc589e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
7 changes: 1 addition & 6 deletions demo/src/examples/BasicExample.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ export default defineComponent({
},
setup: () => {
const content = ref<Delta>(
new Delta([
{ insert: 'Gandalf', attributes: { bold: true } },
{ insert: ' the ' },
{ insert: 'Grey', attributes: { color: '#ccc' } },
])
new Delta()
)
return { content }
},
})
</script>

4 changes: 3 additions & 1 deletion demo/src/examples/ContentType.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export default defineComponent({
]);
contentHTML.value = '<h3>This is a different HTML header</h3>';
contentText.value = 'This is some more plain text';
setTimeout(() =>
contentDelta.value.insert('\n I am also deeply reactive and a ref update works'), 200)
}
return { contentDelta, contentHTML, contentText, update }
},
})
</script>

22 changes: 13 additions & 9 deletions packages/vue-quill/src/components/QuillEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ export const QuillEditor = defineComponent({
)
}

const maybeClone = (delta: Delta | string) => {
return typeof delta === 'object' ? delta.slice() : delta
}

const deltaHasValuesOtherThanRetain = (delta: Delta): boolean => {
return Object.values(delta).some((v) => !v.retain)
return Object.values(delta.ops).some(
(v) => !v.retain || Object.keys(v).length !== 1
)
}

// eslint-disable-next-line vue/no-setup-props-destructure
let internalModel = props.content // Doesn't need reactivity
// Doesn't need reactivity, but does need to be cloned to avoid deep mutations always registering as equal
let internalModel: typeof props.content
const internalModelEquals = (against: Delta | String | undefined) => {
if (typeof internalModel === typeof against) {
if (against === internalModel) {
Expand All @@ -211,10 +217,7 @@ export const QuillEditor = defineComponent({
oldContents: Delta,
source: Sources
) => {
// Quill should never be null at this point because we receive an event
// so content should not be undefined but let's make ts and eslint happy
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
internalModel = getContents()!
internalModel = maybeClone(getContents() as string | Delta)
// Update v-model:content when text changes
if (!internalModelEquals(props.content)) {
ctx.emit('update:content', internalModel)
Expand Down Expand Up @@ -302,6 +305,7 @@ export const QuillEditor = defineComponent({
} else {
quill?.setContents(content as Delta, source)
}
internalModel = maybeClone(content)
}

const getText = (index?: number, length?: number): string => {
Expand Down Expand Up @@ -338,14 +342,14 @@ export const QuillEditor = defineComponent({
(newContent) => {
if (!quill || !newContent || internalModelEquals(newContent)) return

internalModel = newContent
// Restore the selection and cursor position after updating the content
const selection = quill.getSelection()
if (selection) {
nextTick(() => quill?.setSelection(selection))
}
setContents(newContent)
}
},
{ deep: true }
)

watch(
Expand Down

0 comments on commit dc589e1

Please sign in to comment.