index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.clipboard.dangerouslyPasteHTML(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. if (value) {
  131. this.$refs.upload.$children[0].$refs.input.click();
  132. } else {
  133. this.quill.format("image", false);
  134. }
  135. });
  136. }
  137. this.Quill.clipboard.dangerouslyPasteHTML(this.currentValue);
  138. this.Quill.on("text-change", (delta, oldDelta, source) => {
  139. const html = this.$refs.editor.children[0].innerHTML;
  140. const text = this.Quill.getText();
  141. const quill = this.Quill;
  142. this.currentValue = html;
  143. this.$emit("input", html);
  144. this.$emit("on-change", { html, text, quill });
  145. });
  146. this.Quill.on("text-change", (delta, oldDelta, source) => {
  147. this.$emit("on-text-change", delta, oldDelta, source);
  148. });
  149. this.Quill.on("selection-change", (range, oldRange, source) => {
  150. this.$emit("on-selection-change", range, oldRange, source);
  151. });
  152. this.Quill.on("editor-change", (eventName, ...args) => {
  153. this.$emit("on-editor-change", eventName, ...args);
  154. });
  155. },
  156. // 上传前校检格式和大小
  157. handleBeforeUpload(file) {
  158. const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"];
  159. const isJPG = type.includes(file.type);
  160. // 检验文件格式
  161. if (!isJPG) {
  162. this.$message.error(`图片格式错误!`);
  163. return false;
  164. }
  165. // 校检文件大小
  166. if (this.fileSize) {
  167. const isLt = file.size / 1024 / 1024 < this.fileSize;
  168. if (!isLt) {
  169. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  170. return false;
  171. }
  172. }
  173. return true;
  174. },
  175. handleUploadSuccess(res, file) {
  176. // 如果上传成功
  177. if (res.code == 200) {
  178. // 获取富文本组件实例
  179. let quill = this.Quill;
  180. // 获取光标所在位置
  181. let length = quill.getSelection().index;
  182. // 插入图片 res.url为服务器返回的图片地址
  183. quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName);
  184. // 调整光标到最后
  185. quill.setSelection(length + 1);
  186. } else {
  187. this.$message.error("图片插入失败");
  188. }
  189. },
  190. handleUploadError() {
  191. this.$message.error("图片插入失败");
  192. },
  193. },
  194. };
  195. </script>
  196. <style>
  197. .editor, .ql-toolbar {
  198. white-space: pre-wrap !important;
  199. line-height: normal !important;
  200. }
  201. .quill-img {
  202. display: none;
  203. }
  204. .ql-snow .ql-tooltip[data-mode="link"]::before {
  205. content: "请输入链接地址:";
  206. }
  207. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  208. border-right: 0px;
  209. content: "保存";
  210. padding-right: 0px;
  211. }
  212. .ql-snow .ql-tooltip[data-mode="video"]::before {
  213. content: "请输入视频地址:";
  214. }
  215. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  216. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  217. content: "14px";
  218. }
  219. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  220. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  221. content: "10px";
  222. }
  223. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  224. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  225. content: "18px";
  226. }
  227. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  228. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  229. content: "32px";
  230. }
  231. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  232. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  233. content: "文本";
  234. }
  235. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  236. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  237. content: "标题1";
  238. }
  239. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  240. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  241. content: "标题2";
  242. }
  243. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  244. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  245. content: "标题3";
  246. }
  247. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  248. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  249. content: "标题4";
  250. }
  251. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  252. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  253. content: "标题5";
  254. }
  255. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  256. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  257. content: "标题6";
  258. }
  259. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  260. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  261. content: "标准字体";
  262. }
  263. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  264. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  265. content: "衬线字体";
  266. }
  267. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  268. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  269. content: "等宽字体";
  270. }
  271. </style>