rich-edit.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <div class="border border-solid border-gray-100 h-full">
  3. <Toolbar
  4. :default-config="toolbarConfig"
  5. :editor="editorRef"
  6. mode="default"
  7. />
  8. <Editor
  9. v-model="valueHtml"
  10. :default-config="editorConfig"
  11. class="overflow-y-hidden mt-0.5"
  12. mode="default"
  13. style="height: 18rem"
  14. @onChange="change"
  15. @onCreated="handleCreated"
  16. />
  17. </div>
  18. </template>
  19. <script setup>
  20. import { defineProps, defineEmits } from 'vue'
  21. import '@wangeditor/editor/dist/css/style.css' // 引入 css
  22. const basePath = import.meta.env.VITE_BASE_API
  23. import { onBeforeUnmount, ref, shallowRef, watch } from 'vue'
  24. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  25. import { useUserStore } from '@/pinia/modules/user'
  26. import { ElMessage } from 'element-plus'
  27. import { getUrl } from '@/utils/image'
  28. const userStore = useUserStore()
  29. const emits = defineEmits(['change', 'update:modelValue'])
  30. const change = editor => {
  31. emits('change', editor)
  32. emits('update:modelValue', valueHtml.value)
  33. }
  34. const props = defineProps({
  35. modelValue: {
  36. type: String,
  37. default: ''
  38. }
  39. })
  40. const editorRef = shallowRef()
  41. const valueHtml = ref('')
  42. const toolbarConfig = {}
  43. const editorConfig = {
  44. placeholder: '请输入内容...',
  45. MENU_CONF: {}
  46. }
  47. editorConfig.MENU_CONF['uploadImage'] = {
  48. fieldName: 'file',
  49. server: basePath + '/fileUploadAndDownload/upload?noSave=1',
  50. customInsert(res, insertFn) {
  51. if (res.code === 100000) {
  52. const urlPath = getUrl(res.data.file.url)
  53. insertFn(urlPath, res.data.file.name)
  54. return
  55. }
  56. ElMessage.error(res.msg)
  57. }
  58. }
  59. // 组件销毁时,也及时销毁编辑器
  60. onBeforeUnmount(() => {
  61. const editor = editorRef.value
  62. if (editor == null) return
  63. editor.destroy()
  64. })
  65. const handleCreated = editor => {
  66. editorRef.value = editor
  67. valueHtml.value = props.modelValue
  68. }
  69. watch(
  70. () => props.modelValue,
  71. () => {
  72. valueHtml.value = props.modelValue
  73. }
  74. )
  75. </script>
  76. <style lang="scss" scoped></style>