index.vue 4.2 KB

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