index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!--
  2. * @FilePath: index.vue
  3. * @Author: 旭颖
  4. * @Date: 2022-12-16 12:14:16
  5. * @LastEditors: Please set LastEditors
  6. * @LastEditTime: 2023-08-01 11:14:09
  7. -->
  8. <template>
  9. <div style="border: 1px solid #ccc">
  10. <Toolbar
  11. :default-config="toolbarConfig"
  12. :editor="editorRef"
  13. :mode="mode"
  14. style="border-bottom: 1px solid #ccc"
  15. />
  16. <Editor
  17. v-model="valueHtml"
  18. :default-config="editorConfig"
  19. :mode="mode"
  20. style="min-height: 700px"
  21. @onChange="changeEdit"
  22. @onCreated="handleCreated"
  23. />
  24. </div>
  25. </template>
  26. <script>
  27. import '@wangeditor/editor/dist/css/style.css' // 引入 css
  28. import { useUserStore } from '@/store/modules/user'
  29. import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
  30. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  31. export default {
  32. components: { Editor, Toolbar },
  33. props: {
  34. content: {
  35. type: String,
  36. required: true,
  37. },
  38. },
  39. setup(props, { emit }) {
  40. // 编辑器实例,必须用 shallowRef
  41. const editorRef = shallowRef()
  42. const valueHtml = ref(props.content)
  43. watch(props, (newProps) => {
  44. valueHtml.value = newProps.content
  45. })
  46. // 模拟 ajax 异步获取内容
  47. onMounted(() => {
  48. // setTimeout(() => {
  49. // this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
  50. // }, 1500)
  51. // 内容 HTML
  52. })
  53. const toolbarConfig = {}
  54. // const editorConfig = { placeholder: '请输入内容...' }
  55. const editorConfig = {
  56. // JS 语法
  57. MENU_CONF: {},
  58. placeholder: '请输入内容...',
  59. // 其他属性...
  60. }
  61. // 组件销毁时,也及时销毁编辑器
  62. onBeforeUnmount(() => {
  63. const editor = editorRef.value
  64. if (editor == null) return
  65. editor.destroy()
  66. })
  67. const userStore = useUserStore()
  68. //自定义上传图片
  69. editorConfig.MENU_CONF['uploadImage'] = {
  70. server: `${process.env.VUE_APP_BASE_URL}` + '/base/resource/upload',
  71. // / form-data fieldName ,默认值 'wangeditor-uploaded-image'
  72. fieldName: 'file',
  73. // 单个文件的最大体积限制,默认为 2M
  74. maxFileSize: 10 * 1024 * 1024, // 1M
  75. // 最多可上传几个文件,默认为 100
  76. maxNumberOfFiles: 10,
  77. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  78. allowedFileTypes: ['image/*'],
  79. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  80. meta: {
  81. file_type: 'img',
  82. dir: 'default',
  83. },
  84. // 将 meta 拼接到 url 参数中,默认 false
  85. metaWithUrl: false,
  86. // 自定义增加 http header
  87. headers: {
  88. Authorization: userStore.token,
  89. },
  90. // 跨域是否传递 cookie ,默认为 false
  91. withCredentials: true,
  92. // 超时时间,默认为 10 秒
  93. timeout: 5 * 1000, // 5 秒
  94. customInsert(res, insertFn) {
  95. // JS 语法
  96. // res 即服务端的返回结果
  97. console.log('上传图片成功', res)
  98. // 从 res 中找到 url alt href ,然后插入图片
  99. insertFn(res.data.url)
  100. },
  101. }
  102. const handleCreated = (editor) => {
  103. editorRef.value = editor // 记录 editor 实例,重要!
  104. }
  105. const changeEdit = () => {
  106. emit('change-edit', valueHtml.value)
  107. }
  108. //富文本编辑器失焦事件
  109. const handleBlur = (editor) => {
  110. console.log(valueHtml, 'blur', editor)
  111. }
  112. const handleChange = (editor) => {
  113. console.log('change:', editor.getHtml())
  114. }
  115. return {
  116. editorRef,
  117. valueHtml,
  118. mode: 'default', // 或 'simple'
  119. toolbarConfig,
  120. editorConfig,
  121. changeEdit,
  122. handleBlur,
  123. handleChange,
  124. handleCreated,
  125. }
  126. },
  127. }
  128. </script>