index.vue 8.3 KB

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