editor.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="my-editor">
  3. <div
  4. class="quill-editor"
  5. :content="content"
  6. @change="handleChange"
  7. v-quill:myQuillEditor="editorOption"
  8. ></div>
  9. <input type="file" id="imgInput" @change="handleContentFileChange" style="display: none;" />
  10. </div>
  11. </template>
  12. <script>
  13. import Vue from "vue";
  14. import "quill/dist/quill.core.css";
  15. import "quill/dist/quill.snow.css";
  16. // import 'quill/dist/quill.bubble.css'
  17. if (process.browser) {
  18. const VueQuillEditor = require("vue-quill-editor/dist/ssr");
  19. Vue.use(VueQuillEditor);
  20. }
  21. import hljs from "hljs";
  22. export default {
  23. props: ["content"],
  24. components: {
  25. // quillEditor
  26. // Editor
  27. },
  28. data() {
  29. return {
  30. editorOption: {
  31. theme: "snow",
  32. placeholder: "请输入正文...",
  33. modules: {
  34. toolbar: [
  35. ["bold", "italic", "underline", "strike"],
  36. ["blockquote", "code-block"],
  37. [{ header: 1 }, { header: 2 }],
  38. [{ list: "ordered" }, { list: "bullet" }],
  39. [{ script: "sub" }, { script: "super" }],
  40. [{ indent: "-1" }, { indent: "+1" }],
  41. [{ direction: "rtl" }],
  42. [{ size: ["small", false, "large", "huge"] }],
  43. [{ header: [1, 2, 3, 4, 5, 6, false] }],
  44. [{ font: [] }],
  45. [{ color: [] }, { background: [] }],
  46. [{ align: [] }],
  47. ["clean"],
  48. ["link", "image"]
  49. ],
  50. syntax: {
  51. highlight: text => hljs.highlightAuto(text).value
  52. }
  53. }
  54. }
  55. };
  56. },
  57. computed: {},
  58. mounted() {
  59. // 为图片ICON绑定事件 getModule 为编辑器的内部属性
  60. this.myQuillEditor
  61. .getModule("toolbar")
  62. .addHandler("image", this.imgHandler);
  63. // this.myQuillEditor
  64. // .getModule("toolbar")
  65. // .addHandler("video", this.videoHandler); // 为视频ICON绑定事件
  66. },
  67. methods: {
  68. handleChange(e) {
  69. this.$emit("change", e.html);
  70. },
  71. // 点击图片ICON触发事件
  72. imgHandler(state) {
  73. this.addRange = this.myQuillEditor.getSelection();
  74. if (state) {
  75. let fileInput = document.getElementById("imgInput");
  76. fileInput.click(); // 加一个触发事件
  77. }
  78. this.uploadType = "image";
  79. },
  80. // 点击视频ICON触发事件
  81. videoHandler(state) {
  82. this.addRange = this.myQuillEditor.getSelection();
  83. if (state) {
  84. let fileInput = document.getElementById("imgInput");
  85. fileInput.click(); // 加一个触发事件
  86. }
  87. // this.uploadType = "video";
  88. },
  89. handleContentFileChange(e) {
  90. const file = e.target.files[0];
  91. if (file.size / 1024 > 500) {
  92. this.$message.error("图片大小不得超过500k,请重新选择");
  93. return false;
  94. }
  95. const formData = new FormData();
  96. formData.append("file", file);
  97. formData.append("original_filename", file.name);
  98. this.$axios
  99. .$post(`/upload_image`, formData, {
  100. headers: { "Content-Type": "multipart/form-data" }
  101. })
  102. .then(res => {
  103. this.$emit(
  104. "change",
  105. this.content + `<img src="${res.filename}" alt="${file.name}"/>`
  106. );
  107. });
  108. }
  109. }
  110. };
  111. </script>
  112. <style lang="scss">
  113. .my-editor {
  114. position: relative;
  115. width: 100%;
  116. height: 100%;
  117. min-height: 244px;
  118. background: #fff;
  119. .ql-toolbar {
  120. border-width: 0 !important;
  121. }
  122. .ql-editor,
  123. .quill-editor {
  124. min-height: 244px;
  125. border: 0 !important;
  126. font-size: 14px;
  127. line-height: 25px;
  128. }
  129. .ql-snow.ql-toolbar::after {
  130. display: inline-block;
  131. }
  132. }
  133. </style>