index.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="uploadUrl"
  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 { getToken } 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. uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  62. headers: {
  63. Authorization: "Bearer " + getToken()
  64. },
  65. Quill: null,
  66. currentValue: "",
  67. options: {
  68. theme: "snow",
  69. bounds: document.body,
  70. debug: "warn",
  71. modules: {
  72. // 工具栏配置
  73. toolbar: [
  74. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  75. ["blockquote", "code-block"], // 引用 代码块
  76. [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  77. [{ indent: "-1" }, { indent: "+1" }], // 缩进
  78. [{ size: ["small", false, "large", "huge"] }], // 字体大小
  79. [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  80. [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  81. [{ align: [] }], // 对齐方式
  82. ["clean"], // 清除文本格式
  83. ["link", "image", "video"] // 链接、图片、视频
  84. ],
  85. },
  86. placeholder: "请输入内容",
  87. readOnly: this.readOnly,
  88. },
  89. };
  90. },
  91. computed: {
  92. styles() {
  93. let style = {};
  94. if (this.minHeight) {
  95. style.minHeight = `${this.minHeight}px`;
  96. }
  97. if (this.height) {
  98. style.height = `${this.height}px`;
  99. }
  100. return style;
  101. },
  102. },
  103. watch: {
  104. value: {
  105. handler(val) {
  106. if (val !== this.currentValue) {
  107. this.currentValue = val === null ? "" : val;
  108. if (this.Quill) {
  109. this.Quill.pasteHTML(this.currentValue);
  110. }
  111. }
  112. },
  113. immediate: true,
  114. },
  115. },
  116. mounted() {
  117. this.init();
  118. },
  119. beforeDestroy() {
  120. this.Quill = null;
  121. },
  122. methods: {
  123. init() {
  124. const editor = this.$refs.editor;
  125. this.Quill = new Quill(editor, this.options);
  126. // 如果设置了上传地址则自定义图片上传事件
  127. if (this.type == 'url') {
  128. let toolbar = this.Quill.getModule("toolbar");
  129. toolbar.addHandler("image", (value) => {
  130. this.uploadType = "image";
  131. if (value) {
  132. this.$refs.upload.$children[0].$refs.input.click();
  133. } else {
  134. this.quill.format("image", false);
  135. }
  136. });
  137. }
  138. this.Quill.pasteHTML(this.currentValue);
  139. this.Quill.on("text-change", (delta, oldDelta, source) => {
  140. const html = this.$refs.editor.children[0].innerHTML;
  141. const text = this.Quill.getText();
  142. const quill = this.Quill;
  143. this.currentValue = html;
  144. this.$emit("input", html);
  145. this.$emit("on-change", { html, text, quill });
  146. });
  147. this.Quill.on("text-change", (delta, oldDelta, source) => {
  148. this.$emit("on-text-change", delta, oldDelta, source);
  149. });
  150. this.Quill.on("selection-change", (range, oldRange, source) => {
  151. this.$emit("on-selection-change", range, oldRange, source);
  152. });
  153. this.Quill.on("editor-change", (eventName, ...args) => {
  154. this.$emit("on-editor-change", eventName, ...args);
  155. });
  156. },
  157. // 上传前校检格式和大小
  158. handleBeforeUpload(file) {
  159. // 校检文件大小
  160. if (this.fileSize) {
  161. const isLt = file.size / 1024 / 1024 < this.fileSize;
  162. if (!isLt) {
  163. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  164. return false;
  165. }
  166. }
  167. return true;
  168. },
  169. handleUploadSuccess(res, file) {
  170. // 获取富文本组件实例
  171. let quill = this.Quill;
  172. // 如果上传成功
  173. if (res.code == 200) {
  174. // 获取光标所在位置
  175. let length = quill.getSelection().index;
  176. // 插入图片 res.url为服务器返回的图片地址
  177. quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName);
  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>