editoritem.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div id="editor"></div>
  3. </template>
  4. <script>
  5. import E from 'wangeditor';
  6. import MyMenu from '@lib/editorButtom.js';
  7. import $axios from '@lib/axios.js';
  8. export default ({
  9. components: {},
  10. props: {
  11. value: {
  12. type: String,
  13. default: ''
  14. },
  15. isClear: {
  16. type: Boolean,
  17. default: false
  18. }
  19. },
  20. watch: {
  21. isClear (val) {
  22. // 触发清除文本域内容
  23. if (val) {
  24. this.editor.txt.clear();
  25. this.info_ = null;
  26. }
  27. },
  28. value: function (value) {
  29. if (value !== this.editor.txt.html()) {
  30. this.editor.txt.html(this.value);
  31. }
  32. }
  33. // value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  34. },
  35. data() {
  36. return {
  37. editor: null,
  38. html: '',
  39. toolbarConfig: {
  40. insertKeys: {
  41. index: 1, // 插入的位置,基于当前的 toolbarKeys
  42. keys: ['menu1']
  43. }
  44. },
  45. editorConfig: { placeholder: '请输入内容...' },
  46. mode: 'default' // or 'simple'
  47. };
  48. },
  49. methods: {
  50. init() {
  51. this.editor = new E('#editor');
  52. E.registerMenu('menu1', MyMenu);
  53. this.editor.config.height = 500;
  54. this.editor.config.uploadImgShowBase64 = false; // base 64 存储图片
  55. this.editor.config.uploadImgMaxSize = 50 * 1024 * 1024; // 将图片大小限制为 2M
  56. this.editor.config.uploadImgMaxLength = 1; // 限制一次最多上传 3 张图片
  57. this.editor.config.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间
  58. this.editor.config.showLinkImg = false;
  59. // 自定义文件上传
  60. this.editor.config.customUploadImg = async function (resultFiles, insertImgFn) {
  61. // resultFiles 是 input 中选中的文件列表
  62. var data = new FormData();
  63. const filesUpload = '/api/files/filestore/upload';
  64. data.append('file', resultFiles[0]);
  65. const headers = {
  66. 'Content-Type': 'multipart/form-data'
  67. };
  68. const res = await $axios.post(filesUpload, data, null, headers);
  69. const url = res.data.filePath;
  70. // 上传图片,返回结果,将图片插入到编辑器中
  71. insertImgFn(url);
  72. };
  73. // 自定义上传视频
  74. // this.editor.config.onlineVideoCheck = (src) => {
  75. // const videoMod = `<iframe src="${src}"> </iframe>`;
  76. // // this.editor.txt.append(videoMod);
  77. // this.editor.cmd.do('insertHTML', videoMod);
  78. // };
  79. this.editor.config.onchange = (html) => {
  80. this.info_ = html; // 绑定当前逐渐地值
  81. this.$emit('change', this.info_); // 将内容同步到父组件中
  82. };
  83. // this.editor.config.onchangeTimeout = 50;
  84. this.editor.create();
  85. }
  86. },
  87. mounted() {
  88. this.init();
  89. this.editor.txt.html(this.value);
  90. }
  91. });
  92. </script>
  93. <style lang="scss" scoped>
  94. button {
  95. background: none;
  96. border: none;
  97. }
  98. </style>