index.vue 8.2 KB

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