index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="uploadUrl"
  5. :on-error="handleUploadError"
  6. name="file"
  7. :show-file-list="false"
  8. :headers="headers"
  9. style="display: none"
  10. ref="upload"
  11. :http-request="imageChange"
  12. >
  13. </el-upload>
  14. <div class="editor" ref="editor" :style="styles"></div>
  15. </div>
  16. </template>
  17. <script>
  18. import Quill from "quill";
  19. import "quill/dist/quill.core.css";
  20. import "quill/dist/quill.snow.css";
  21. import "quill/dist/quill.bubble.css";
  22. import { getToken } from "@/utils/auth";
  23. import ImageResize from "quill-image-resize-module";
  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. maxHeight: {
  44. type: Number,
  45. default: null,
  46. },
  47. /* 只读 */
  48. readOnly: {
  49. type: Boolean,
  50. default: false,
  51. },
  52. // /* 上传地址 */
  53. uploadStatus: {
  54. type: Number,
  55. default: null,
  56. },
  57. },
  58. data() {
  59. return {
  60. datas: {},
  61. uploadUrl: "",
  62. headers: {
  63. AuthorizationToken: "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"], // 链接、图片
  84. ],
  85. imageResize: {
  86. displayStyles: {
  87. backgroundColor: "black",
  88. border: "none",
  89. color: "white",
  90. },
  91. modules: ["Resize", "DisplaySize", "Toolbar"],
  92. },
  93. },
  94. placeholder: "请输入内容",
  95. readOnly: this.readOnly,
  96. },
  97. };
  98. },
  99. computed: {
  100. styles() {
  101. let style = {};
  102. if (this.minHeight) {
  103. style.minHeight = `${this.minHeight}px`;
  104. }
  105. if (this.maxHeight) {
  106. style.maxHeight = `${this.maxHeight}px`;
  107. }
  108. if (this.height) {
  109. style.height = `${this.height}px`;
  110. }
  111. return style;
  112. },
  113. },
  114. watch: {
  115. value: {
  116. handler(val) {
  117. if (val !== this.currentValue) {
  118. this.currentValue = val === null ? "" : val;
  119. if (this.Quill) {
  120. this.Quill.pasteHTML(this.currentValue);
  121. }
  122. }
  123. },
  124. immediate: true,
  125. },
  126. },
  127. mounted() {
  128. Quill.register("modules/imageResize", ImageResize);
  129. new Promise((resolve, reject) => {
  130. var datats = {
  131. imageStatus: this.uploadStatus,
  132. };
  133. this.$api.getPolicy(datats).then((res) => {
  134. this.datas = res.data.resultContent;
  135. this.uploadUrl = res.data.resultContent.host;
  136. resolve();
  137. });
  138. }).then(() => {
  139. this.init();
  140. });
  141. },
  142. beforeDestroy() {
  143. this.Quill = null;
  144. },
  145. methods: {
  146. init() {
  147. const editor = this.$refs.editor;
  148. this.Quill = new Quill(editor, this.options);
  149. console.log(this.Quill)
  150. // 如果设置了上传地址则自定义图片上传事件
  151. if (this.uploadUrl) {
  152. let toolbar = this.Quill.getModule("toolbar");
  153. toolbar.addHandler("image", (value) => {
  154. this.uploadType = "image";
  155. if (value) {
  156. // document.querySelector(".avatar-uploader input").click();
  157. this.$refs.upload.$children[0].$refs.input.click();
  158. } else {
  159. this.quill.format("image", false);
  160. }
  161. });
  162. toolbar.addHandler("video", (value) => {
  163. this.uploadType = "video";
  164. if (value) {
  165. this.$refs.upload.$children[0].$refs.input.click();
  166. } else {
  167. this.quill.format("video", false);
  168. }
  169. });
  170. }
  171. this.Quill.enable(false);
  172. this.Quill.pasteHTML(this.currentValue);
  173. // this.$nextTick(function() {
  174. // this.Quill.blur();
  175. // this.Quill.enable(true);
  176. // });
  177. this.Quill.on("text-change", (delta, oldDelta, source) => {
  178. const html = this.$refs.editor.children[0].innerHTML;
  179. const text = this.Quill.getText();
  180. const quill = this.Quill;
  181. this.currentValue = html;
  182. this.$emit("input", html);
  183. this.$emit("on-change", { html, text, quill });
  184. this.$emit("on-text-change", delta, oldDelta, source);
  185. });
  186. this.Quill.on("selection-change", (range, oldRange, source) => {
  187. this.$emit("on-selection-change", range, oldRange, source);
  188. });
  189. this.Quill.on("editor-change", (eventName, ...args) => {
  190. this.$emit("on-editor-change", eventName, ...args);
  191. });
  192. editor.onclick=() => {
  193. this.Quill.enable(true)
  194. this.Quill.focus();
  195. }
  196. },
  197. imageChange(param, type) {
  198. this.$upload
  199. .upload(param.file, this.uploadStatus)
  200. .then((res) => {
  201. let quill = this.Quill;
  202. // 获取光标所在位置
  203. let length = quill.getSelection().index;
  204. // 插入图片 res.url为服务器返回的图片地址
  205. quill.insertEmbed(
  206. length,
  207. "image",
  208. this.$methodsTools.splitImgHost(res)
  209. );
  210. // 调整光标到最后
  211. quill.setSelection(length + 1);
  212. })
  213. .catch((err) => {
  214. console.log(err);
  215. });
  216. },
  217. handleUploadError() {
  218. this.$message.error("图片插入失败");
  219. },
  220. },
  221. };
  222. </script>
  223. <style>
  224. .editor,
  225. .ql-toolbar {
  226. white-space: pre-wrap !important;
  227. line-height: normal !important;
  228. }
  229. .ql-toolbar.ql-snow {
  230. border-top-left-radius: 8px;
  231. border-top-right-radius: 8px;
  232. }
  233. .ql-container {
  234. border-bottom-left-radius: 8px;
  235. border-bottom-right-radius: 8px;
  236. overflow: auto;
  237. }
  238. .quill-img {
  239. display: none;
  240. }
  241. .ql-snow .ql-tooltip[data-mode="link"]::before {
  242. content: "请输入链接地址:";
  243. }
  244. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  245. border-right: 0px;
  246. content: "保存";
  247. padding-right: 0px;
  248. }
  249. .ql-snow .ql-tooltip[data-mode="video"]::before {
  250. content: "请输入视频地址:";
  251. }
  252. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  253. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  254. content: "14px";
  255. }
  256. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  257. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  258. content: "10px";
  259. }
  260. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  261. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  262. content: "18px";
  263. }
  264. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  265. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  266. content: "32px";
  267. }
  268. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  269. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  270. content: "文本";
  271. }
  272. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  273. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  274. content: "标题1";
  275. }
  276. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  277. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  278. content: "标题2";
  279. }
  280. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  281. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  282. content: "标题3";
  283. }
  284. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  285. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  286. content: "标题4";
  287. }
  288. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  289. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  290. content: "标题5";
  291. }
  292. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  293. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  294. content: "标题6";
  295. }
  296. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  297. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  298. content: "标准字体";
  299. }
  300. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  301. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  302. content: "衬线字体";
  303. }
  304. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  305. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  306. content: "等宽字体";
  307. }
  308. .ql-toolbar {
  309. background-color: #eee !important;
  310. }
  311. </style>