index.vue 8.6 KB

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