index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="quill-editor">
  3. <el-upload class="avatar-uploaderEdi" :action="url" :show-file-list="false" :on-success="uploadSuccess" :before-upload="beforeUpload">
  4. </el-upload>
  5. <quill-editor v-model="editorContent" class="ql-container sicepot" @change="onEditorChange($event)" :content="editorContent" :options="editorOption" ref="QuillEditor">
  6. </quill-editor>
  7. </div>
  8. </template>
  9. <script>
  10. import Cookies from "js-cookie";
  11. import 'quill/dist/quill.core.css'
  12. import 'quill/dist/quill.snow.css'
  13. import 'quill/dist/quill.bubble.css'
  14. import {
  15. quillEditor,
  16. Quill
  17. } from 'vue-quill-editor'
  18. const undoIcon = Quill.import('ui/icons')['undo']
  19. const redoIcon = Quill.import('ui/icons')['redo']
  20. const toolbarOptions = [
  21. [{
  22. 'undo': undoIcon
  23. }, {
  24. 'redo': redoIcon
  25. }], // 撤销,重做
  26. ['bold', 'italic', 'underline', 'strike'], // 加粗,斜体,下划线,删除线
  27. ['blockquote', 'code-block'], //引用,代码块
  28. [{
  29. 'header': 1
  30. }, {
  31. 'header': 2
  32. }], // 几级标题
  33. [{
  34. 'list': 'ordered'
  35. }, {
  36. 'list': 'bullet'
  37. }], // 有序列表,无序列表
  38. [{
  39. 'script': 'sub'
  40. }, {
  41. 'script': 'super'
  42. }], // 下角标,上角标
  43. [{
  44. 'indent': '-1'
  45. }, {
  46. 'indent': '+1'
  47. }], // 缩进
  48. [{
  49. 'direction': 'rtl'
  50. }], // 文字输入方向
  51. [{
  52. 'size': ['small', false, 'large', 'huge']
  53. }], // 字体大小
  54. [{
  55. 'header': [1, 2, 3, 4, 5, 6, false]
  56. }], // 标题
  57. [{
  58. 'color': []
  59. }, {
  60. 'background': []
  61. }], // 颜色选择
  62. [{
  63. 'font': ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial']
  64. }], // 字体
  65. [{
  66. 'align': []
  67. }], // 居中
  68. ['clean'], // 清除样式,
  69. ['link', 'image'], // 上传图片、上传视频
  70. ]
  71. export default {
  72. data() {
  73. return {
  74. editorContent: this.content || '',
  75. editorOption: {
  76. placeholder: '请在这里输入',
  77. theme: 'snow', //主题 snow/bubble
  78. modules: {
  79. history: {
  80. delay: 1000,
  81. maxStack: 50,
  82. userOnly: false
  83. },
  84. toolbar: {
  85. container: toolbarOptions,
  86. handlers: {
  87. undo: function () {
  88. this.quill.history.undo()
  89. },
  90. redo: function () {
  91. this.quill.history.redo()
  92. },
  93. image: function (value) {
  94. if (value) {
  95. // 调用element的图片上传组件
  96. document.querySelector('.avatar-uploaderEdi input').click()
  97. } else {
  98. this.quill.format('image', false)
  99. }
  100. }
  101. }
  102. }
  103. }
  104. },
  105. url: `${
  106. window.SITE_CONFIG["apiURL"]
  107. }/sys/oss/upload?token=${Cookies.get("token")}`,
  108. }
  109. },
  110. props: ['content'],
  111. components: {
  112. quillEditor
  113. },
  114. watch: {
  115. content(newValue) {
  116. // 检查新值是否与当前编辑器内容不同,避免不必要的更新和光标跳动
  117. if (newValue !== this.editorContent) {
  118. this.editorContent = newValue;
  119. }
  120. }
  121. },
  122. methods: {
  123. // init() {
  124. // this.editorContent = this.content || '';
  125. // },
  126. beforeUpload(file) {},
  127. // 值发生变化
  128. onEditorChange(editor) {
  129. this.$emit('EditorChange', this.editorContent);
  130. },
  131. uploadSuccess(res) {
  132. // 获取富文本组件实例
  133. let quill = this.$refs.QuillEditor.quill
  134. // 如果上传成功
  135. if (res) {
  136. // 获取光标所在位置
  137. let length = quill.getSelection().index;
  138. // 插入图片,res为服务器返回的图片链接地址
  139. quill.insertEmbed(length, 'image', res.data)
  140. // 调整光标到最后
  141. quill.setSelection(length + 1)
  142. } else {
  143. // 提示信息,需引入Message
  144. this.$message.error('图片插入失败!')
  145. }
  146. },
  147. }
  148. }
  149. </script>
  150. <style>
  151. .sicepot {
  152. height: auto !important;
  153. }
  154. .sicepot .ql-container.ql-snow {
  155. height: 600px !important;
  156. }
  157. </style>