index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="uploadUrl"
  5. :on-success="handleUploadSuccess"
  6. :on-error="handleUploadError"
  7. name="file"
  8. :show-file-list="false"
  9. :headers="headers"
  10. style="display: none"
  11. ref="upload"
  12. v-if="this.uploadUrl"
  13. >
  14. </el-upload>
  15. <div class="editor" ref="editor" :style="styles"></div>
  16. </div>
  17. </template>
  18. <script>
  19. import Quill from "quill";
  20. import "quill/dist/quill.core.css";
  21. import "quill/dist/quill.snow.css";
  22. import "quill/dist/quill.bubble.css";
  23. import { getToken } from "@/utils/auth";
  24. export default {
  25. name: "Editor",
  26. props: {
  27. /* 编辑器的内容 */
  28. value: {
  29. type: String,
  30. default: "",
  31. },
  32. /* 高度 */
  33. height: {
  34. type: Number,
  35. default: null,
  36. },
  37. /* 最小高度 */
  38. minHeight: {
  39. type: Number,
  40. default: null,
  41. },
  42. /* 只读 */
  43. readOnly: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. /* 上传地址 */
  48. uploadUrl: {
  49. type: String,
  50. default: "",
  51. }
  52. },
  53. data() {
  54. return {
  55. headers: {
  56. Authorization: "Bearer " + getToken()
  57. },
  58. Quill: null,
  59. currentValue: "",
  60. options: {
  61. theme: "snow",
  62. bounds: document.body,
  63. debug: "warn",
  64. modules: {
  65. // 工具栏配置
  66. toolbar: [
  67. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  68. ["blockquote", "code-block"], // 引用 代码块
  69. [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  70. [{ indent: "-1" }, { indent: "+1" }], // 缩进
  71. [{ size: ["small", false, "large", "huge"] }], // 字体大小
  72. [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  73. [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  74. [{ align: [] }], // 对齐方式
  75. ["clean"], // 清除文本格式
  76. ["link", "image", "video"] // 链接、图片、视频
  77. ],
  78. },
  79. placeholder: "请输入内容",
  80. readOnly: this.readOnly,
  81. },
  82. };
  83. },
  84. computed: {
  85. styles() {
  86. let style = {};
  87. if (this.minHeight) {
  88. style.minHeight = `${this.minHeight}px`;
  89. }
  90. if (this.height) {
  91. style.height = `${this.height}px`;
  92. }
  93. return style;
  94. },
  95. },
  96. watch: {
  97. value: {
  98. handler(val) {
  99. if (val !== this.currentValue) {
  100. this.currentValue = val === null ? "" : val;
  101. if (this.Quill) {
  102. this.Quill.pasteHTML(this.currentValue);
  103. }
  104. }
  105. },
  106. immediate: true,
  107. },
  108. },
  109. mounted() {
  110. this.init();
  111. },
  112. beforeDestroy() {
  113. this.Quill = null;
  114. },
  115. methods: {
  116. init() {
  117. const editor = this.$refs.editor;
  118. this.Quill = new Quill(editor, this.options);
  119. // 如果设置了上传地址则自定义图片上传事件
  120. if (this.uploadUrl) {
  121. let toolbar = this.Quill.getModule("toolbar");
  122. toolbar.addHandler("image", (value) => {
  123. this.uploadType = "image";
  124. if (value) {
  125. this.$refs.upload.$children[0].$refs.input.click();
  126. } else {
  127. this.quill.format("image", false);
  128. }
  129. });
  130. toolbar.addHandler("video", (value) => {
  131. this.uploadType = "video";
  132. if (value) {
  133. this.$refs.upload.$children[0].$refs.input.click();
  134. } else {
  135. this.quill.format("video", false);
  136. }
  137. });
  138. }
  139. this.Quill.pasteHTML(this.currentValue);
  140. this.Quill.on("text-change", (delta, oldDelta, source) => {
  141. const html = this.$refs.editor.children[0].innerHTML;
  142. const text = this.Quill.getText();
  143. const quill = this.Quill;
  144. this.currentValue = html;
  145. this.$emit("input", html);
  146. this.$emit("on-change", { html, text, quill });
  147. });
  148. this.Quill.on("text-change", (delta, oldDelta, source) => {
  149. this.$emit("on-text-change", delta, oldDelta, source);
  150. });
  151. this.Quill.on("selection-change", (range, oldRange, source) => {
  152. this.$emit("on-selection-change", range, oldRange, source);
  153. });
  154. this.Quill.on("editor-change", (eventName, ...args) => {
  155. this.$emit("on-editor-change", eventName, ...args);
  156. });
  157. },
  158. handleUploadSuccess(res, file) {
  159. console.info(file);
  160. // 获取富文本组件实例
  161. let quill = this.Quill;
  162. // 如果上传成功
  163. if (res.code == 200) {
  164. // 获取光标所在位置
  165. let length = quill.getSelection().index;
  166. // 插入图片 res.url为服务器返回的图片地址
  167. quill.insertEmbed(length, "image", res.url);
  168. // 调整光标到最后
  169. quill.setSelection(length + 1);
  170. } else {
  171. this.$message.error("图片插入失败");
  172. }
  173. },
  174. handleUploadError() {
  175. this.$message.error("图片插入失败");
  176. },
  177. },
  178. };
  179. </script>
  180. <style>
  181. .editor, .ql-toolbar {
  182. white-space: pre-wrap !important;
  183. line-height: normal !important;
  184. }
  185. .quill-img {
  186. display: none;
  187. }
  188. .ql-snow .ql-tooltip[data-mode="link"]::before {
  189. content: "请输入链接地址:";
  190. }
  191. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  192. border-right: 0px;
  193. content: "保存";
  194. padding-right: 0px;
  195. }
  196. .ql-snow .ql-tooltip[data-mode="video"]::before {
  197. content: "请输入视频地址:";
  198. }
  199. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  200. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  201. content: "14px";
  202. }
  203. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  204. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  205. content: "10px";
  206. }
  207. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  208. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  209. content: "18px";
  210. }
  211. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  212. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  213. content: "32px";
  214. }
  215. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  216. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  217. content: "文本";
  218. }
  219. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  220. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  221. content: "标题1";
  222. }
  223. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  224. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  225. content: "标题2";
  226. }
  227. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  228. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  229. content: "标题3";
  230. }
  231. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  232. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  233. content: "标题4";
  234. }
  235. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  236. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  237. content: "标题5";
  238. }
  239. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  240. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  241. content: "标题6";
  242. }
  243. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  244. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  245. content: "标准字体";
  246. }
  247. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  248. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  249. content: "衬线字体";
  250. }
  251. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  252. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  253. content: "等宽字体";
  254. }
  255. </style>