Skip to content

Commit

Permalink
Update pasting html - fire text change and selection change, fix demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alekswebnet committed Apr 29, 2024
1 parent 41e1231 commit 57b983b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion demo/src/components/BasicEditor.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { QuillyEditor } from 'vue-quilly'
import { QuillyEditor } from '../../../src/'
import Quill, { Delta, Range } from 'quill/core'
import 'quill/dist/quill.core.css'
Expand Down
2 changes: 1 addition & 1 deletion demo/src/components/CompleteEditor.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { QuillyEditor } from 'vue-quilly'
import { QuillyEditor } from '../../../src/'
import { Delta, Range } from 'quill/core'
import Quill from 'quill'
import 'quill/dist/quill.snow.css'
Expand Down
20 changes: 12 additions & 8 deletions src/components/QuillyEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ const container = ref<HTMLElement>()
const model = ref<string>()
// Convert modelValue HTML to Delta and replace editor content
const pasteHTML = (quill: Quill | null) => {
const pasteHTML = (quill: Quill) => {
model.value = props.modelValue
const content = quill!.clipboard.convert({ html: props.modelValue })
return content
const oldContent = quill.getContents()
const oldRange = quill.getSelection()!
const delta = quill.clipboard.convert({ html: props.modelValue })
const range = { index: model.value!.length, length: 0 }
quill.setContents(delta)
quill.setSelection(range)
emit('text-change', { delta, oldContent, source: 'api' })
emit('selection-change', { range, oldRange, source: 'api' })
return delta
}
// Editor initialization, returns Quill instance
Expand All @@ -40,10 +47,7 @@ const initialize = (QuillClass: typeof Quill) => {
// Set editor initial model
if (props.modelValue) {
const oldContent = quill.getContents()
const delta = pasteHTML(quill)
console.log(delta, oldContent)
emit('text-change', { delta, oldContent, source: 'api' })
pasteHTML(quill)
}
// Handle editor selection change, emit blur and focus
Expand Down Expand Up @@ -79,7 +83,7 @@ watch(
() => props.modelValue,
(newValue) => {
if (newValue && newValue !== model.value) {
pasteHTML(quillInstance)
pasteHTML(quillInstance!)
model.value = quillInstance!.root.innerHTML
} else if (!newValue) {
quillInstance!.setContents([])
Expand Down

0 comments on commit 57b983b

Please sign in to comment.