editor.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="quill-editor">
  3. <slot name="toolbar"></slot>
  4. <div ref="editor"></div>
  5. </div>
  6. </template>
  7. <script>
  8. // require sources
  9. import _Quill from 'quill'
  10. import defaultOptions from './options'
  11. const Quill = window.Quill || _Quill
  12. // pollfill
  13. if (typeof Object.assign != 'function') {
  14. Object.defineProperty(Object, 'assign', {
  15. value(target, varArgs) {
  16. if (target == null) {
  17. throw new TypeError('Cannot convert undefined or null to object')
  18. }
  19. const to = Object(target)
  20. for (let index = 1; index < arguments.length; index++) {
  21. const nextSource = arguments[index]
  22. if (nextSource != null) {
  23. for (const nextKey in nextSource) {
  24. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  25. to[nextKey] = nextSource[nextKey]
  26. }
  27. }
  28. }
  29. }
  30. return to
  31. },
  32. writable: true,
  33. configurable: true
  34. })
  35. }
  36. // export
  37. export default {
  38. name: 'quill-editor',
  39. data() {
  40. return {
  41. _options: {},
  42. _content: '',
  43. defaultOptions
  44. }
  45. },
  46. props: {
  47. content: String,
  48. value: String,
  49. disabled: {
  50. type: Boolean,
  51. default: false
  52. },
  53. options: {
  54. type: Object,
  55. required: false,
  56. default: () => ({})
  57. },
  58. globalOptions: {
  59. type: Object,
  60. required: false,
  61. default: () => ({})
  62. }
  63. },
  64. mounted() {
  65. this.initialize()
  66. },
  67. beforeDestroy() {
  68. this.quill = null
  69. delete this.quill
  70. },
  71. methods: {
  72. // Init Quill instance
  73. initialize() {
  74. if (this.$el) {
  75. // Options
  76. this._options = Object.assign({}, this.defaultOptions, this.globalOptions, this.options)
  77. // Instance
  78. this.quill = new Quill(this.$refs.editor, this._options)
  79. this.quill.enable(false)
  80. // Set editor content
  81. if (this.value || this.content) {
  82. this.quill.pasteHTML(this.value || this.content)
  83. }
  84. // Disabled editor
  85. if (!this.disabled) {
  86. this.quill.enable(true)
  87. }
  88. // Mark model as touched if editor lost focus
  89. this.quill.on('selection-change', range => {
  90. if (!range) {
  91. this.$emit('blur', this.quill)
  92. } else {
  93. this.$emit('focus', this.quill)
  94. }
  95. })
  96. // Update model if text changes
  97. this.quill.on('text-change', (delta, oldDelta, source) => {
  98. let html = this.$refs.editor.children[0].innerHTML
  99. const quill = this.quill
  100. const text = this.quill.getText()
  101. if (html === '<p><br></p>') html = ''
  102. this._content = html
  103. this.$emit('input', this._content)
  104. this.$emit('change', { html, text, quill })
  105. })
  106. // Emit ready event
  107. this.$emit('ready', this.quill)
  108. }
  109. }
  110. },
  111. watch: {
  112. // Watch content change
  113. content(newVal, oldVal) {
  114. if (this.quill) {
  115. if (newVal && newVal !== this._content) {
  116. this._content = newVal
  117. this.quill.pasteHTML(newVal)
  118. } else if(!newVal) {
  119. this.quill.setText('')
  120. }
  121. }
  122. },
  123. // Watch content change
  124. value(newVal, oldVal) {
  125. if (this.quill) {
  126. if (newVal && newVal !== this._content) {
  127. this._content = newVal
  128. this.quill.pasteHTML(newVal)
  129. } else if(!newVal) {
  130. this.quill.setText('')
  131. }
  132. }
  133. },
  134. // Watch disabled change
  135. disabled(newVal, oldVal) {
  136. if (this.quill) {
  137. this.quill.enable(!newVal)
  138. }
  139. }
  140. }
  141. }
  142. </script>