• vue2中使用quill编辑器+表格功能


    注意:现在quill2才能支持表格功能,quill2官方未发布正式版本,现在是开发版本

    步骤一:安装quill

    $npm install quill@2.0.0-dev.3 -dev–save

    步骤二:创建edtior.vue组件

    1. <template>
    2. <div>
    3. <div class="editor" style="height: 1000px">div>
    4. div>
    5. template>
    6. <script>
    7. import Quill from 'quill'
    8. import 'quill/dist/quill.snow.css'
    9. export default {
    10. name: 'Editor',
    11. props: {
    12. content: {
    13. type: String,
    14. default: ''
    15. }
    16. },
    17. data () {
    18. return {
    19. quill: null,
    20. options: {
    21. theme: 'snow',
    22. modules: {
    23. toolbar: {
    24. container: [
    25. ['bold', 'italic', 'underline', 'strike'],
    26. [{ header: 1 }, { header: 2 }],
    27. [{ color: [] }, { background: [] }],
    28. [{ list: 'ordered' }, { list: 'bullet' }],
    29. [{ align: [] }],
    30. [{ indent: '-1' }, { indent: '+1' }],
    31. ['link','image','video'],
    32. [
    33. { table: 'TD' },
    34. { 'table-insert-row': 'TIR' },
    35. { 'table-insert-column': 'TIC' },
    36. { 'table-delete-row': 'TDR' },
    37. { 'table-delete-column': 'TDC' }
    38. ],
    39. ],
    40. handlers: {
    41. table: function (val) {
    42. this.quill.getModule('table').insertTable(2, 3)
    43. },
    44. 'table-insert-row': function () {
    45. this.quill.getModule('table').insertRowBelow()
    46. },
    47. 'table-insert-column': function () {
    48. this.quill.getModule('table').insertColumnRight()
    49. },
    50. 'table-delete-row': function () {
    51. this.quill.getModule('table').deleteRow()
    52. },
    53. 'table-delete-column': function () {
    54. this.quill.getModule('table').deleteColumn()
    55. }
    56. }
    57. },
    58. table: true
    59. },
    60. placeholder: ''
    61. }
    62. }
    63. },
    64. mounted () {
    65. console.log(this.content)
    66. const dom = this.$el.querySelector('.editor')
    67. this.quill = new Quill(dom, this.options)
    68. // this.quill.setContents(this.content)
    69. // debugger
    70. this.quill.root.innerHTML = this.content
    71. // debugger
    72. this.quill.on('text-change', () => {
    73. // console.log(this.quill.getContents())
    74. // this.$emit('contentData', this.quill.getContents())
    75. // console.log(this.quill.root.innerHTML)
    76. this.$emit('contentData', this.quill.root.innerHTML)
    77. })
    78. this.$el.querySelector(
    79. '.ql-table-insert-row'
    80. ).innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="24" height="24" viewBox="0 0 24 24"><defs><clipPath id="master_svg0_152_6483"><rect x="0" y="0" width="24" height="24" rx="0"/>clipPath>defs><g clip-path="url(#master_svg0_152_6483)"><g><path d="M10,3C10.552,3,11,3.448,11,4L11,20C11,20.552,10.552,21,10,21L4,21C3.448,21,3,20.552,3,20L3,4C3,3.448,3.448,3,4,3L10,3ZM9,5L5,5L5,19L9,19L9,5ZM18,7C20.761,7,23,9.239,23,12C23,14.761,20.761,17,18,17C15.239,17,13,14.761,13,12C13,9.239,15.239,7,18,7ZM19,9L17,9L17,10.998999999999999L15,11L15,13L17,12.999L17,15L19,15L19,12.999L21,13L21,11L19,10.998999999999999L19,9Z" fill="#444444" fill-opacity="1"/>g>g>svg>`
    81. this.$el.querySelector(
    82. '.ql-table-insert-column'
    83. ).innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="24" height="24" viewBox="0 0 24 24"><defs><clipPath id="master_svg0_152_6487"><rect x="0" y="0" width="24" height="24" rx="0"/>clipPath>defs><g clip-path="url(#master_svg0_152_6487)"><g><path d="M12,13C14.761,13,17,15.239,17,18C17,20.761,14.761,23,12,23C9.239,23,7,20.761,7,18C7,15.239,9.239,13,12,13ZM13,15L11,15L11,16.999000000000002L9,17L9,19L11,18.999000000000002L11,21L13,21L13,18.999000000000002L15,19L15,17L13,16.999000000000002L13,15ZM20,3C20.552,3,21,3.448,21,4L21,10C21,10.552,20.552,11,20,11L4,11C3.448,11,3,10.552,3,10L3,4C3,3.448,3.448,3,4,3L20,3ZM5,5L5,9L19,9L19,5L5,5Z" fill="#444444" fill-opacity="1"/>g>g>svg>`
    84. this.$el.querySelector(
    85. '.ql-table-delete-row'
    86. ).innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="24" height="24" viewBox="0 0 24 24"><defs><clipPath id="master_svg0_152_6479"><rect x="0" y="0" width="24" height="24" rx="0"/>clipPath>defs><g clip-path="url(#master_svg0_152_6479)"><g><path d="M20,3C20.552,3,21,3.448,21,4L21,20C21,20.552,20.552,21,20,21L14,21C13.448,21,13,20.552,13,20L13,4C13,3.448,13.448,3,14,3L20,3ZM19,5L15,5L15,19L19,19L19,5ZM6,7C8.761,7,11,9.239,11,12C11,14.761,8.761,17,6,17C3.239,17,1,14.761,1,12C1,9.239,3.239,7,6,7ZM7,9L5,9L5,10.998999999999999L3,11L3,13L5,12.999L5,15L7,15L7,12.999L9,13L9,11L7,10.998999999999999L7,9Z" fill="#444444" fill-opacity="1"/>g>g>svg>`
    87. this.$el.querySelector(
    88. '.ql-table-delete-column'
    89. ).innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" width="24" height="24" viewBox="0 0 24 24"><defs><clipPath id="master_svg0_152_6491"><rect x="0" y="0" width="24" height="24" rx="0"/>clipPath>defs><g clip-path="url(#master_svg0_152_6491)"><g><path d="M20,13C20.552,13,21,13.448,21,14L21,20C21,20.552,20.552,21,20,21L4,21C3.448,21,3,20.552,3,20L3,14C3,13.448,3.448,13,4,13L20,13ZM19,15L5,15L5,19L19,19L19,15ZM12,1C14.761,1,17,3.239,17,6C17,8.761,14.761,11,12,11C9.239,11,7,8.761,7,6C7,3.239,9.239,1,12,1ZM13,3L11,3L11,4.9990000000000006L9,5L9,7L11,6.999L11,9L13,9L13,6.999L15,7L15,5L13,4.9990000000000006L13,3Z" fill="#444444" fill-opacity="1"/>g>g>svg>`
    90. this.addQuillTitle()
    91. },
    92. activated () {
    93. this.quill.setContents({})
    94. },
    95. methods: {
    96. addQuillTitle () {
    97. const oToolBar = document.querySelector('.ql-toolbar')
    98. const aButton = oToolBar.querySelectorAll('button')
    99. const aSelect = oToolBar.querySelectorAll('select')
    100. aButton.forEach(function (item) {
    101. if (item.className === 'ql-script') {
    102. item.value === 'sub' ? (item.title = '下标') : (item.title = '上标')
    103. } else if (item.className === 'ql-indent') {
    104. item.value === '+1' ? (item.title = '向右缩进') : (item.title = '向左缩进')
    105. } else {
    106. item.title = titleConfig[item.classList[0]]
    107. }
    108. })
    109. aSelect.forEach(function (item) {
    110. item.parentNode.title = titleConfig[item.classList[0]]
    111. })
    112. },
    113. getContentData () {
    114. return this.quill.getContents()
    115. }
    116. }
    117. }
    118. script>
    119. <style>
    120. style>

    步骤三:引用该组件的父组件

    1. <template>
    2. <quillEditor v-if="content" :model-value="content" @input="editorChange"/>
    3. template>
    4. <script>
    5. import quillEditor from '@/components/quillEditor/editor.vue'
    6. export default {
    7. data() {
    8. return {
    9. content: ''
    10. }
    11. },
    12. methods: {
    13. contentChange(data) {
    14. this.content = data
    15. },
    16. }
    17. }
    18. script>

  • 相关阅读:
    ORB-SLAM2 ---- computeOrbDescriptor函数
    Ansys Zemax | 大功率激光系统的 STOP 分析1:如何使用 OpticStudio 优化光学设置
    语音合成技术汇总1:Glow-TTS:通过单调对齐实现文本到语音的生成流
    动画讲解TCP的3次握手,4次挥手,让你一次看明白
    Flutter 应用加速之本地缓存管理
    Nginx 文件解析漏洞复现
    较难算法美丽塔时间复杂度O(n)
    前端实现docx、pdf格式文件在线预览
    [笔记] 错排问题 #错排
    【数据结构】——常见排序
  • 原文地址:https://blog.csdn.net/qq_41852789/article/details/132857317