editor.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="my-editor">
  3. <div
  4. class="quill-editor"
  5. :content="content"
  6. :placeholder="placeholder || ''"
  7. @change="handleChange"
  8. v-quill:[quillName]="editorOption"
  9. ></div>
  10. <input type="file" accept="image/png, image/jpeg" :id="'imgInput'+quillName" @change="handleContentFileChange($event)"
  11. style="display: none;" />
  12. <el-dialog title="提示" :visible.sync="linkDialog" :before-close="handleLinkClose">
  13. <el-form :model="link">
  14. <el-form-item label="标题" :label-width="formLabelWidth">
  15. <el-input v-model="link.title" autocomplete="off"></el-input>
  16. </el-form-item>
  17. <el-form-item label="链接" :label-width="formLabelWidth">
  18. <el-input v-model="link.url" autocomplete="off"></el-input>
  19. </el-form-item>
  20. </el-form>
  21. <span slot="footer" class="dialog-footer">
  22. <el-button @click="handleLinkClose">取 消</el-button>
  23. <el-button type="primary" @click="handleLinkOk">确 定</el-button>
  24. </span>
  25. </el-dialog>
  26. </div>
  27. </template>
  28. <script>
  29. import Vue from "vue";
  30. import "quill/dist/quill.core.css";
  31. import "quill/dist/quill.snow.css";
  32. // import 'quill/dist/quill.bubble.css'
  33. let handerLink = null
  34. if (process.browser) {
  35. const VueQuillEditor = require("vue-quill-editor/dist/ssr");
  36. Vue.use(VueQuillEditor);
  37. handerLink = VueQuillEditor.Quill.import("formats/link")
  38. }
  39. import hljs from "hljs";
  40. export default {
  41. props: ["content", "hideImage", "placeholder", "haveVideo"],
  42. components: {
  43. },
  44. data() {
  45. const extra = ["link"];
  46. if (!this.hideImage) {
  47. extra.push("image");
  48. }
  49. // if (this.haveVideo) {
  50. // extra.push("video");
  51. // }
  52. let placeholder = this.placeholder
  53. return {
  54. editorOption: {
  55. name: Math.random(),
  56. theme: "snow",
  57. placeholder: placeholder || "请输入正文...",
  58. modules: {
  59. toolbar: [
  60. ["bold", "italic", "underline", "strike"],
  61. ["blockquote"],
  62. [{ list: "ordered" }, { list: "bullet" }],
  63. [{ indent: "-1" }, { indent: "+1" }],
  64. [{ size: ["small", false, "large", "huge"] }],
  65. [{ header: [1, 2, 3, 4, 5, 6, false] }],
  66. [{ font: [] }],
  67. [{ color: [] }, { background: [] }],
  68. [{ align: [] }],
  69. extra
  70. ],
  71. syntax: {
  72. highlight: text => hljs.highlightAuto(text).value
  73. }
  74. }
  75. },
  76. formLabelWidth: "60px",
  77. linkDialog: false,
  78. link: {
  79. title: "",
  80. url: ""
  81. },
  82. quillName: 'myQuillEditor' + Math.floor(Math.random() * 1000000000)
  83. };
  84. },
  85. computed: {},
  86. mounted() {
  87. console.log('this.quillName', this.quillName)
  88. // 为图片ICON绑定事件 getModule 为编辑器的内部属性
  89. this[this.quillName]
  90. .getModule("toolbar").addHandler("image", this.imgHandler);
  91. this.changeParseEvent()
  92. this.addLinkEvent()
  93. },
  94. methods: {
  95. handleChange(e) {
  96. if (typeof e.html === "undefined") {
  97. return;
  98. }
  99. this.$emit("change", e.html);
  100. },
  101. // 点击图片ICON触发事件
  102. imgHandler(state) {
  103. this.addRange = this[this.quillName].getSelection();
  104. if (state) {
  105. let fileInput = document.getElementById("imgInput"+this.quillName);
  106. fileInput.click(); // 加一个触发事件
  107. }
  108. this.uploadType = "image";
  109. },
  110. //change parse
  111. changeParseEvent() {
  112. // 自定义粘贴图片功能
  113. this[this.quillName].root.addEventListener('paste', evt => {
  114. console.log("evt", evt)
  115. if (evt.clipboardData && evt.clipboardData.files && evt.clipboardData.files.length) {
  116. evt.preventDefault();
  117. [].forEach.call(evt.clipboardData.files, file => {
  118. if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) {
  119. return;
  120. }
  121. this.handleContentFileChange({target: {files:[file]}})
  122. });
  123. }
  124. }, false);
  125. },
  126. addLinkEvent() {
  127. let dom = document.getElementsByClassName("ql-link")
  128. console.log("dom", dom)
  129. if (dom.length === 0) {
  130. setTimeout(()=>{
  131. this.addLinkEvent()
  132. }, 2000)
  133. } else {
  134. dom[0].addEventListener("click", this.linkHandler.bind(this), false)
  135. }
  136. },
  137. // 点击link触发事件
  138. linkHandler() {
  139. const selectedRange = this[this.quillName].getSelection();
  140. if (selectedRange && !selectedRange.length ) {
  141. this.$message.warning("请先选择需要插入超链接的内容")
  142. }
  143. },
  144. handleLinkClose() {
  145. this.linkDialog = false;
  146. this.link = {
  147. title: "",
  148. url: ""
  149. };
  150. },
  151. handleLinkOk() {
  152. const link = this.link;
  153. this[this.quillName].deleteText(link.index, link.length);
  154. this[this.quillName].insertEmbed(link.index, "link", {
  155. href: link.url,
  156. innerText: link.title
  157. });
  158. // this[this.quillName].insertText(
  159. // link.index,
  160. // `<a href="${this.link.url}">${this.link.title}</a>`
  161. // );
  162. this.handleLinkClose();
  163. },
  164. // 点击视频ICON触发事件
  165. videoHandler(state) {
  166. this.addRange = this[this.quillName].getSelection();
  167. if (state) {
  168. let fileInput = document.getElementById("imgInput"+this.quillName);
  169. fileInput.click(); // 加一个触发事件
  170. }
  171. // this.uploadType = "video";
  172. },
  173. handleContentFileChange(e) {
  174. const file = e.target.files[0];
  175. if (file.size / 1024/1024 > 2) {
  176. this.$message.error("图片大小不得超过2M,请重新选择");
  177. return false;
  178. }
  179. const formData = new FormData();
  180. formData.append("file", file);
  181. formData.append("original_filename", file.name);
  182. this.$axios
  183. .$post(`/upload_image`, formData, {
  184. headers: { "Content-Type": "multipart/form-data" }
  185. })
  186. .then(res => {
  187. const index = this[this.quillName].selection.savedRange.index;
  188. this[this.quillName].insertEmbed(index, "image", res.filename);
  189. });
  190. }
  191. }
  192. };
  193. </script>
  194. <style lang="scss">
  195. .my-editor {
  196. position: relative;
  197. width: 100%;
  198. height: 100%;
  199. min-height: 244px;
  200. background: #fff;
  201. .ql-toolbar {
  202. border-width: 0 !important;
  203. }
  204. .ql-editor,
  205. .quill-editor {
  206. min-height: 244px;
  207. border: 0 !important;
  208. font-size: 14px;
  209. line-height: 25px;
  210. }
  211. .ql-snow.ql-toolbar::after {
  212. display: inline-block;
  213. }
  214. }
  215. </style>