EditorWithBinding.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div style="border: 1px solid #ccc; width: 100%; margin: 10px auto">
  3. <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" mode="default"/>
  4. <Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml" :defaultConfig="editorConfig" mode="default"
  5. @onCreated="handleCreated" @customPaste="customPaste" @input="handleChange"/>
  6. </div>
  7. <!-- <el-button style="margin: 0 auto" @click="getEditorHTML">获取富文本HTML内容</el-button>-->
  8. </template>
  9. <script setup>
  10. // 富文本编辑器文档链接: https://www.wangeditor.com/v5/getting-started.html
  11. // 引入富文本编辑器CSS
  12. import '@wangeditor/editor/dist/css/style.css';
  13. import {onBeforeUnmount, ref, shallowRef, watch,defineEmits} from 'vue';
  14. // 导入富文本编辑器的组件
  15. import {Editor, Toolbar} from '@wangeditor/editor-for-vue';
  16. import resource from "../api/resource.js";
  17. // 编辑器实例,必须用 shallowRef
  18. const editorRef = shallowRef();
  19. // 内容 HTML
  20. // const valueHtml = ref('');
  21. const toolbarConfig = {};
  22. const editorConfig = ref({placeholder: '请输入内容...', MENU_CONF: {}});
  23. // 新增 props
  24. const props = defineProps({
  25. modelValue: {
  26. type: String,
  27. default: '',
  28. },
  29. });
  30. // 新增 data
  31. const valueHtml = ref(props.modelValue);
  32. const emit=defineEmits(['input'])
  33. // 新增事件处理方法
  34. watch(
  35. () => props.modelValue,
  36. (newVal) => {
  37. valueHtml.value = newVal; // 确保内部数据与外部modelValue同步
  38. },
  39. {immediate: true} // 立即执行,确保初始化时也能同步
  40. );
  41. const handleChange = (content) => {
  42. console.log('content', content);
  43. emit('input', editorRef.value.getHtml());
  44. };
  45. // 自定义图片上传
  46. editorConfig.value.MENU_CONF['uploadImage'] = {
  47. async customUpload(file, insertFn) {
  48. let dataForm = new FormData();
  49. dataForm.append('types', "serve")
  50. dataForm.append('file', file)
  51. const fileRes = await resource.uploadResource(dataForm)
  52. // 这里的file为上传的图片对象,insertFn传入
  53. insertFn(fileRes.data.Data, 'img');
  54. },
  55. };
  56. // 自定义视频上传
  57. editorConfig.value.MENU_CONF['uploadVideo'] = {
  58. async customUpload(file, insertFn) {
  59. // console.log('上传视频', file);
  60. },
  61. };
  62. // 富文本编辑器生成后触发
  63. const handleCreated = editor => {
  64. editorRef.value = editor; // 记录 editor 实例,重要!
  65. // console.log(editorConfig.value.MENU_CONF, 'editorConfig.value');
  66. };
  67. // 监听富文本编辑器粘贴行为
  68. const customPaste = (editor, event, callback) => {
  69. // 获取粘贴的纯文本
  70. const text = event.clipboardData.getData('text/plain');
  71. if (text) {
  72. editor.insertText(text);
  73. event.preventDefault();
  74. callback(false);
  75. }
  76. };
  77. // 获取富文本html内容
  78. const getEditorHTML = () => {
  79. console.log(editorRef.value.getHtml());
  80. };
  81. // 组件销毁时,也及时销毁编辑器
  82. onBeforeUnmount(() => {
  83. const editor = editorRef.value;
  84. if (editor == null) return;
  85. editor.destroy();
  86. });
  87. </script>