当前位置:   article > 正文

element ui富文本编辑器的使用(quill-editor)_elementui富文本编辑器

elementui富文本编辑器

引用组件

  1. <el-form-item label="内容">
  2. <editor v-model="obj.activity_content" :min-height="192"/>
  3. </el-form-item>

组件封装

  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. <!--editorIndex-->
  20. <script>
  21. import Quill from "quill";
  22. import "quill/dist/quill.core.css";
  23. import "quill/dist/quill.snow.css";
  24. import "quill/dist/quill.bubble.css";
  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. title: {default: ""}
  59. , readonly: {type: Boolean}
  60. , tip: {default: ""}//文件说明,空字符串不显示
  61. , maxFileSize: {default: 1024 * 1024 * 10}//文件大小限制
  62. , uploadFile: {
  63. default: function () {
  64. return {id: "", url: ""}
  65. }
  66. }//默认现在的文件列表{name,url}
  67. , uploadFileName: {default: "uploadFile"}//父组件的对应的数据名称
  68. , fileType: {default: ""}//上传文件类型,或后缀名逗号分隔
  69. },
  70. data() {
  71. return {
  72. // uploadUrl: this.axiosUrl(this.global.sysUrl) + "/sysFile/uploadFile.do", // 上传的图片服务器地址
  73. headers: {
  74. // Authorization: "Bearer " + getToken()
  75. },
  76. Quill: null,
  77. currentValue: "",
  78. options: {
  79. theme: "snow",
  80. bounds: document.body,
  81. debug: "warn",
  82. modules: {
  83. // 工具栏配置
  84. toolbar: [
  85. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  86. ["blockquote", "code-block"], // 引用 代码块
  87. [{list: "ordered"}, {list: "bullet"}], // 有序、无序列表
  88. [{indent: "-1"}, {indent: "+1"}], // 缩进
  89. [{size: ["small", false, "large", "huge"]}], // 字体大小
  90. [{header: [1, 2, 3, 4, 5, 6, false]}], // 标题
  91. [{color: []}, {background: []}], // 字体颜色、字体背景颜色
  92. [{align: []}], // 对齐方式
  93. ["clean"], // 清除文本格式
  94. ["link", "image", "video"] // 链接、图片、视频
  95. ],
  96. },
  97. placeholder: "请输入内容",
  98. readOnly: this.readOnly,
  99. },
  100. };
  101. },
  102. computed: {
  103. styles() {
  104. let style = {};
  105. if (this.minHeight) {
  106. style.minHeight = `${this.minHeight}px`;
  107. }
  108. if (this.height) {
  109. style.height = `${this.height}px`;
  110. }
  111. return style;
  112. },
  113. uploadUrl() {
  114. return this.axiosUrl(this.global.sysUrl) + "/sysFile/uploadFile.html";
  115. }
  116. },
  117. watch: {
  118. value: {
  119. handler(val) {
  120. if (val !== this.currentValue) {
  121. this.currentValue = val === null ? "" : val;
  122. if (this.Quill) {
  123. this.Quill.pasteHTML(this.currentValue);
  124. }
  125. }
  126. },
  127. immediate: true,
  128. },
  129. },
  130. mounted() {
  131. this.init();
  132. },
  133. beforeDestroy() {
  134. this.Quill = null;
  135. },
  136. methods: {
  137. init() {
  138. const editor = this.$refs.editor;
  139. this.Quill = new Quill(editor, this.options);
  140. // 如果设置了上传地址则自定义图片上传事件
  141. if (this.type == 'url') {
  142. let toolbar = this.Quill.getModule("toolbar");
  143. toolbar.addHandler("image", (value) => {
  144. this.uploadType = "image";
  145. if (value) {
  146. this.$refs.upload.$children[0].$refs.input.click();
  147. } else {
  148. this.quill.format("image", false);
  149. }
  150. });
  151. }
  152. this.Quill.pasteHTML(this.currentValue);
  153. this.Quill.on("text-change", (delta, oldDelta, source) => {
  154. const html = this.$refs.editor.children[0].innerHTML;
  155. const text = this.Quill.getText();
  156. const quill = this.Quill;
  157. this.currentValue = html;
  158. this.$emit("input", html);
  159. this.$emit("on-change", {html, text, quill});
  160. });
  161. this.Quill.on("text-change", (delta, oldDelta, source) => {
  162. this.$emit("on-text-change", delta, oldDelta, source);
  163. });
  164. this.Quill.on("selection-change", (range, oldRange, source) => {
  165. this.$emit("on-selection-change", range, oldRange, source);
  166. });
  167. this.Quill.on("editor-change", (eventName, ...args) => {
  168. this.$emit("on-editor-change", eventName, ...args);
  169. });
  170. },
  171. // 上传前校检格式和大小
  172. handleBeforeUpload(file) {
  173. if(file.size==0){
  174. return false;
  175. }
  176. var imageType = "jpg,jpeg,png,bmp,gif";
  177. var suffixList = "";
  178. if (this.fileType == undefined || this.fileType == "") {
  179. suffixList = imageType;
  180. } else {
  181. suffixList = this.fileType;
  182. }
  183. suffixList = suffixList.toLocaleLowerCase();
  184. var suffix = file.name.substr(file.name.lastIndexOf(".") + 1).toLocaleLowerCase();
  185. var suffixListTemp = "," + suffixList + ",";
  186. if (suffixListTemp.indexOf("," + suffix + ",") == -1) {
  187. this.$message.error('上传的文件类型为:' + suffixList);
  188. return false;
  189. }
  190. if (file.size > this.maxFileSize) {
  191. this.$message.error('上传的文件大小不能超过:' + this.maxFileSize / 1024 + " KB");
  192. return false;
  193. }
  194. console.log("上传的文件类型为")
  195. return true;
  196. },
  197. handleUploadSuccess(res, file, fileList) {
  198. // 获取富文本组件实例
  199. let quill = this.Quill;
  200. console.log("上传结果",res)
  201. // 如果上传成功
  202. if (res._result.code == 200) {
  203. // 获取光标所在位置
  204. let length = quill.getSelection().index;
  205. // 插入图片 res.url为服务器返回的图片地址
  206. quill.insertEmbed(length, "image", res.url);
  207. // 调整光标到最后
  208. quill.setSelection(length + 1);
  209. } else {
  210. this.$message.error("图片插入失败");
  211. }
  212. },
  213. handleUploadError() {
  214. this.$message.error("图片插入失败...");
  215. },
  216. },
  217. };
  218. </script>
  219. <style>
  220. .editor, .ql-toolbar {
  221. white-space: pre-wrap !important;
  222. line-height: normal !important;
  223. }
  224. .quill-img {
  225. display: none;
  226. }
  227. .ql-snow .ql-tooltip[data-mode="link"]::before {
  228. content: "请输入链接地址:";
  229. }
  230. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  231. border-right: 0px;
  232. content: "保存";
  233. padding-right: 0px;
  234. }
  235. .ql-snow .ql-tooltip[data-mode="video"]::before {
  236. content: "请输入视频地址:";
  237. }
  238. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  239. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  240. content: "14px";
  241. }
  242. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  243. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  244. content: "10px";
  245. }
  246. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  247. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  248. content: "18px";
  249. }
  250. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  251. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  252. content: "32px";
  253. }
  254. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  255. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  256. content: "文本";
  257. }
  258. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  259. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  260. content: "标题1";
  261. }
  262. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  263. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  264. content: "标题2";
  265. }
  266. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  267. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  268. content: "标题3";
  269. }
  270. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  271. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  272. content: "标题4";
  273. }
  274. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  275. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  276. content: "标题5";
  277. }
  278. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  279. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  280. content: "标题6";
  281. }
  282. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  283. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  284. content: "标准字体";
  285. }
  286. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  287. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  288. content: "衬线字体";
  289. }
  290. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  291. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  292. content: "等宽字体";
  293. }
  294. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/544365
推荐阅读
相关标签
  

闽ICP备14008679号