index.vue 964 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template>
  2. <div :style="customStyle">
  3. <QuillEditor
  4. v-model:content="editorValue"
  5. :toolbar="toolbar"
  6. :enable="!disabled"
  7. :read-only="disabled"
  8. content-type="html"
  9. ref="quillEditor"
  10. />
  11. </div>
  12. </template>
  13. <script setup>
  14. import { nextTick, ref, watch } from 'vue'
  15. const props = defineProps({
  16. modelValue: {
  17. type: String,
  18. default: ''
  19. },
  20. toolbar: {type: Array},
  21. disabled: {type: Boolean},
  22. customStyle: {type: Object}
  23. })
  24. const editorValue = ref(props.modelValue)
  25. let newValue = props.modelValue
  26. const emit = defineEmits(['update:modelValue'])
  27. const quillEditor = ref(null)
  28. watch(() => props.modelValue, (val) => {
  29. if (newValue === val) return
  30. quillEditor.value.setHTML(val)
  31. nextTick(() => {
  32. let q = quillEditor.value.getQuill()
  33. q.setSelection(val.length, 0, 'api')
  34. q.focus()
  35. })
  36. })
  37. watch(editorValue, (val) => {
  38. emit('update:modelValue', val)
  39. newValue = val
  40. })
  41. </script>