Tinymce.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div style="border: 1px solid #ccc">
  3. <Toolbar
  4. style="border-bottom: 1px solid #ccc"
  5. :editor="editor"
  6. :defaultConfig="toolbarConfig"
  7. :mode="mode"
  8. />
  9. <Editor
  10. style="height: 300px; overflow-y: hidden"
  11. v-model="html"
  12. :defaultConfig="editorConfig"
  13. :mode="mode"
  14. @onCreated="onCreated"
  15. @change="handleEditorChange"
  16. />
  17. <!-- <input
  18. type="file"
  19. ref="fileInput"
  20. style="display: none"
  21. @change="handleFileUpload"
  22. /> -->
  23. </div>
  24. </template>
  25. <script>
  26. import Vue from "vue";
  27. import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
  28. import { Boot } from "@wangeditor/editor";
  29. import { uploadFile } from "@/api/performance";
  30. // const customMenuKey = "customUpload";
  31. // let isCustomMenuRegistered = false;
  32. // function registerCustomUploadIcon(componentInstance) {
  33. // if (isCustomMenuRegistered) return;
  34. // const customUploadMenu = {
  35. // key: customMenuKey,
  36. // factory() {
  37. // return {
  38. // tag: "button",
  39. // getValue(editor) {
  40. // return editor.getHtml();
  41. // },
  42. // isActive(editor) {
  43. // return false;
  44. // },
  45. // exec: () => {
  46. // componentInstance.uploadFile();
  47. // },
  48. // isDisabled(editor) {
  49. // return false;
  50. // },
  51. // title: "上传文件",
  52. // iconSvg: `<svg t="1718589544286" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1591" width="200" height="200">
  53. // <path d="M1024 693.248q0 25.6-8.704 48.128t-24.576 40.448-36.864 30.208-45.568 16.384l1.024 1.024-17.408 0-4.096 0-4.096 0-675.84 0q-5.12 1.024-16.384 1.024-39.936 0-74.752-15.36t-60.928-41.472-40.96-60.928-14.848-74.752 14.848-74.752 40.96-60.928 60.928-41.472 74.752-15.36l1.024 0q-1.024-8.192-1.024-15.36l0-16.384q0-72.704 27.648-137.216t75.776-112.128 112.128-75.264 136.704-27.648 137.216 27.648 112.64 75.264 75.776 112.128 27.648 137.216q0 37.888-8.192 74.24t-22.528 69.12q5.12-1.024 10.752-1.536t10.752-0.512q27.648 0 52.736 10.752t43.52 29.696 29.184 44.032 10.752 53.76zM665.6 571.392q20.48 0 26.624-4.608t-8.192-22.016q-14.336-18.432-31.744-48.128t-36.352-60.416-38.4-57.344-37.888-38.912q-18.432-13.312-27.136-14.336t-25.088 12.288q-18.432 15.36-35.84 38.912t-35.328 50.176-35.84 52.224-36.352 45.056q-18.432 18.432-13.312 32.768t25.6 14.336l16.384 0q9.216 0 19.968 0.512t20.992 0.512l17.408 0q14.336 1.024 18.432 9.728t4.096 24.064q0 17.408-0.512 30.72t-0.512 25.6-0.512 25.6-0.512 30.72q0 7.168 1.536 15.36t5.632 15.36 12.288 11.776 21.504 4.608l23.552 0q9.216 0 27.648 1.024 24.576 0 28.16-12.288t3.584-38.912q0-23.552 0.512-42.496t0.512-51.712q0-23.552 4.608-36.352t19.968-12.8q11.264 0 32.256-0.512t32.256-0.512z" p-id="1592"></path>
  54. // </svg>`,
  55. // text: "上传文件",
  56. // };
  57. // },
  58. // };
  59. // Boot.registerMenu({
  60. // key: customMenuKey,
  61. // ...customUploadMenu,
  62. // });
  63. // isCustomMenuRegistered = true;
  64. // }
  65. export default Vue.extend({
  66. components: { Editor, Toolbar },
  67. props: {
  68. value: {
  69. type: String,
  70. default: "",
  71. },
  72. },
  73. data() {
  74. return {
  75. editor: null,
  76. html: "<p>hello</p>",
  77. toolbarConfig: {
  78. excludeKeys: ["group-video"], // 移除上传视频功能
  79. },
  80. editorConfig: {
  81. placeholder: "请输入内容...",
  82. MENU_CONF: {
  83. // 上传图片
  84. uploadImage: {
  85. async customUpload(file, insertFn) {
  86. if (!/^(image\/png|image\/jpeg|image\/jpg)$/i.test(file.type)) {
  87. alert("只能上传格式为png、jpg、jpeg的图片文件!");
  88. return;
  89. }
  90. const form = new FormData();
  91. form.append("file", file);
  92. uploadFile(form)
  93. .then((res) => {
  94. let data = res.data;
  95. insertFn(`${data}`);
  96. Vue.prototype.$message({
  97. type: "success",
  98. message: `${file.name}上传成功`,
  99. });
  100. })
  101. .catch((err) => console.log(err, 888));
  102. },
  103. },
  104. },
  105. },
  106. mode: "default",
  107. };
  108. },
  109. created() {
  110. this.html = this.value;
  111. // registerCustomUploadIcon(this);
  112. },
  113. methods: {
  114. onCreated(editor) {
  115. this.editor = Object.seal(editor);
  116. // this.toolbarConfig.insertKeys = {
  117. // index: 22,
  118. // keys: [customMenuKey],
  119. // };
  120. },
  121. // handleFileUpload(event) {
  122. // const file = event.target.files[0];
  123. // if (!file) return;
  124. // if (
  125. // !/^(text\/html|application\/pdf|application\/msword|application\/vnd\.ms-excel|application\/vnd\.ms-office|application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet|application\/vnd\.openxmlformats-officedocument\.wordprocessingml\.document|application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet|text\/csv)$/i.test(
  126. // file.type
  127. // )
  128. // ) {
  129. // alert("只能上传格式为html、pdf、word、excel、csv、docx、xlsx的文件!");
  130. // return;
  131. // }
  132. // const form = new FormData();
  133. // form.append("file", file);
  134. // uploadFile(form)
  135. // .then((res) => {
  136. // let data = res.data;
  137. // console.log(data, file.type, 7777, this.editor);
  138. // if (this.editor) {
  139. // this.editor.dangerouslyInsertHtml(
  140. // `<a href="${data}" target="_blank">点击查看 ${file.name}</a>`
  141. // );
  142. // this.$message({
  143. // type: "success",
  144. // message: `${file.name}上传成功`,
  145. // });
  146. // } else {
  147. // console.error("Editor instance is not available.");
  148. // }
  149. // })
  150. // .catch((err) => console.log(err, 888));
  151. // },
  152. // uploadFile() {
  153. // this.$refs.fileInput.click();
  154. // },
  155. handleEditorChange(newVal) {
  156. this.$emit("input", newVal);
  157. },
  158. },
  159. watch: {
  160. value(newVal) {
  161. this.html = newVal;
  162. },
  163. html(newVal) {
  164. this.$emit("input", newVal);
  165. },
  166. },
  167. });
  168. </script>
  169. <style scoped>
  170. /* Add your custom styles here */
  171. </style>