edits.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <keep-alive>
  3. <div>
  4. <div class="editor"></div>
  5. </div>
  6. </keep-alive>
  7. </template>
  8. <script>
  9. import Quill from 'quill'
  10. import 'quill/dist/quill.snow.css'
  11. const titleConfig = {
  12. 'ql-bold': '加粗',
  13. 'ql-color': '颜色',
  14. 'ql-font': '字体',
  15. 'ql-code': '插入代码',
  16. 'ql-italic': '斜体',
  17. 'ql-link': '添加链接',
  18. 'ql-background': '颜色',
  19. 'ql-size': '字体大小',
  20. 'ql-strike': '删除线',
  21. 'ql-script': '上标/下标',
  22. 'ql-underline': '下划线',
  23. 'ql-blockquote': '引用',
  24. 'ql-header': '标题',
  25. 'ql-indent': '缩进',
  26. 'ql-list': '列表',
  27. 'ql-align': '文本对齐',
  28. 'ql-direction': '文本方向',
  29. 'ql-code-block': '代码块',
  30. 'ql-formula': '公式',
  31. 'ql-image': '图片',
  32. 'ql-video': '视频',
  33. 'ql-clean': '清除字体样式',
  34. 'ql-upload': '文件',
  35. 'ql-table': '插入表格',
  36. 'ql-table-insert-row': '插入行',
  37. 'ql-table-insert-column': '插入列',
  38. 'ql-table-delete-row': '删除行',
  39. 'ql-table-delete-column': '删除列'
  40. }
  41. export default {
  42. name: 'Editor',
  43. props: {
  44. value: Object
  45. },
  46. data () {
  47. return {
  48. quill: null,
  49. options: {
  50. theme: 'snow',
  51. modules: {
  52. toolbar: {
  53. container: [
  54. ['bold', 'italic', 'underline', 'strike'],
  55. [{ header: 1 }, { header: 2 }],
  56. [{ list: 'ordered' }, { list: 'bullet' }],
  57. [{ indent: '-1' }, { indent: '+1' }],
  58. [{ color: [] }, { background: [] }],
  59. [{ font: [] }],
  60. [{ align: [] }],
  61. ['clean'],
  62. [
  63. { table: 'TD' },
  64. { 'table-insert-row': 'TIR' },
  65. { 'table-insert-column': 'TIC' },
  66. { 'table-delete-row': 'TDR' },
  67. { 'table-delete-column': 'TDC' }
  68. ]
  69. ],
  70. handlers: {
  71. table: function (val) {
  72. this.quill.getModule('table').insertTable(2, 3)
  73. },
  74. 'table-insert-row': function () {
  75. this.quill.getModule('table').insertRowBelow()
  76. },
  77. 'table-insert-column': function () {
  78. this.quill.getModule('table').insertColumnRight()
  79. },
  80. 'table-delete-row': function () {
  81. this.quill.getModule('table').deleteRow()
  82. },
  83. 'table-delete-column': function () {
  84. this.quill.getModule('table').deleteColumn()
  85. }
  86. }
  87. },
  88. table: true
  89. },
  90. placeholder: ''
  91. }
  92. }
  93. },
  94. methods: {
  95. addQuillTitle () {
  96. const oToolBar = document.querySelector('.ql-toolbar')
  97. const aButton = oToolBar.querySelectorAll('button')
  98. const aSelect = oToolBar.querySelectorAll('select')
  99. aButton.forEach(function (item) {
  100. if (item.className === 'ql-script') {
  101. item.value === 'sub' ? (item.title = '下标') : (item.title = '上标')
  102. } else if (item.className === 'ql-indent') {
  103. item.value === '+1' ? (item.title = '向右缩进') : (item.title = '向左缩进')
  104. } else {
  105. item.title = titleConfig[item.classList[0]]
  106. }
  107. })
  108. aSelect.forEach(function (item) {
  109. item.parentNode.title = titleConfig[item.classList[0]]
  110. })
  111. },
  112. getContentData () {
  113. return this.quill.getContents()
  114. }
  115. },
  116. mounted () {
  117. const dom = this.$el.querySelector('.editor')
  118. this.quill = new Quill(dom, this.options)
  119. // this.quill.setContents(this.value)
  120. this.quill.on('text-change', () => {
  121. // console.log(this.quill.getContents())
  122. // this.$emit('contentData', this.quill.getContents())
  123. // console.log(this.quill.root.innerHTML)
  124. this.$emit('contentData', this.quill.root.innerHTML)
  125. })
  126. this.$el.querySelector(
  127. '.ql-table-insert-row'
  128. ).innerHTML = `<svg t="1591862376726" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6306" width="18" height="200"><path d="M500.8 604.779L267.307 371.392l-45.227 45.27 278.741 278.613L779.307 416.66l-45.248-45.248z" p-id="6307"></path></svg>`
  129. this.$el.querySelector(
  130. '.ql-table-insert-column'
  131. ).innerHTML = `<svg t="1591862238963" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6509" width="18" height="200"><path d="M593.450667 512.128L360.064 278.613333l45.290667-45.226666 278.613333 278.762666L405.333333 790.613333l-45.226666-45.269333z" p-id="6510"></path></svg>`
  132. this.$el.querySelector(
  133. '.ql-table-delete-row'
  134. ).innerHTML = `<svg t="1591862253524" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6632" width="18" height="200"><path d="M500.8 461.909333L267.306667 695.296l-45.226667-45.269333 278.741333-278.613334L779.306667 650.026667l-45.248 45.226666z" p-id="6633"></path></svg>`
  135. this.$el.querySelector(
  136. '.ql-table-delete-column'
  137. ).innerHTML = `<svg t="1591862261059" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6755" width="18" height="200"><path d="M641.28 278.613333l-45.226667-45.226666-278.634666 278.762666 278.613333 278.485334 45.248-45.269334-233.365333-233.237333z" p-id="6756"></path></svg>`
  138. this.addQuillTitle()
  139. },
  140. activated () {
  141. this.quill.setContents({})
  142. }
  143. }
  144. </script>