Skip to content

Commit

Permalink
fix: delta comparison and add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Nov 29, 2022
1 parent f55d907 commit f4eba73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
33 changes: 29 additions & 4 deletions demo/src/examples/ContentType.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<template>
<h1>Content Type</h1>
<QuillEditor v-model:content="contentDelta" contentType="delta" />
<QuillEditor
v-model:content="contentDelta"
content-type="delta"
/>
<pre>{{ contentDelta }}</pre>
<QuillEditor v-model:content="contentHTML" contentType="html" />
<QuillEditor
v-model:content="contentHTML"
content-type="html"
/>
<pre>{{ contentHTML }}</pre>
<QuillEditor v-model:content="contentText" contentType="text" />
<QuillEditor
v-model:content="contentText"
content-type="text"
/>
<pre>{{ contentText }}</pre>
<button
type="button"
@click="update"
>
Trigger external content update
</button>
</template>

<script lang="ts">
Expand All @@ -27,7 +42,17 @@ export default defineComponent({
const contentHTML = ref('<h1>This is html header</h1>')
const contentText = ref('This is just plain text')
return { contentDelta, contentHTML, contentText }
const update = () => {
contentDelta.value = new Delta([
{ insert: 'Gandalf', attributes: { bold: true } },
{ insert: ' the ' },
{ insert: 'White', attributes: { color: '#fff', background: '#000' } },
]);
contentHTML.value = '<h3>This is a different HTML header</h3>';
contentText.value = 'This is some more plain text';
}
return { contentDelta, contentHTML, contentText, update }
},
})
</script>
Expand Down
18 changes: 14 additions & 4 deletions packages/vue-quill/src/components/QuillEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
PropType,
watch,
ref,
shallowRef,
h,
} from 'vue'
import { toolbarOptions, ToolbarOptions } from './options'
Expand Down Expand Up @@ -186,18 +185,29 @@ export const QuillEditor = defineComponent({
)
}

const internalModel = shallowRef(props.content)
const deltaHasValuesOtherThanRetain = (delta: Delta): boolean => {
return Object.values(delta).some((v) => !v.retain)
}

const internalModel = ref(props.content)
const internalModelEquals = (against: Delta | String | undefined) => {
if (typeof internalModel.value === typeof against) {
if (against === internalModel.value) {
return true
}
if (against instanceof Delta && internalModel.value instanceof Delta) {
return internalModel.value.diff(against).length() > 0
// Ref/Proxy does not support instanceof, so do a loose check
if (
typeof against === 'object' &&
typeof internalModel.value === 'object'
) {
return !deltaHasValuesOtherThanRetain(
internalModel.value.diff(against as Delta)
)
}
}
return false
}

const handleTextChange: TextChangeHandler = (
delta: Delta,
oldContents: Delta,
Expand Down

0 comments on commit f4eba73

Please sign in to comment.