当前位置:   article > 正文

Vue3中使用富文本编辑器_vue3 富文本编辑器

vue3 富文本编辑器

vue3中我们可以使用@wangeditor/editor、@wangeditor/editor-for-vue来实现其功能

npm地址:https://www.npmjs.com/package/@wangeditor/editor-for-vue/v/5.1.12?activeTab=readme

官网:Editor 

1. 安装
pnpm add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save
 
pnpm add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save
 2. 组件封装
@/comps/Editor/index.vue 

首先为了能让vue认识@wangeditor/editor-for-vue库、我们可以在.d.ts中声明一下即可

// 声明外部 npm 插件模块
declare module '@wangeditor/editor-for-vue';


 

  1. <template>
  2.   <div class="Wft-Editor flex flex-col">
  3.     <Toolbar
  4.       class="default-border"
  5.       :editor="editorRef"
  6.       :mode="mode"
  7.     />
  8.     <Editor
  9.       class="flex-1 overflow-y-auto"
  10.       v-model="valueHtml"
  11.       :defaultConfig="editorConfig"
  12.       :mode="mode"
  13.       @onCreated="handleCreated"
  14.       @onChange="onChange"
  15.     />
  16.   </div>
  17. </template>
  18. <script setup lang="ts">
  19. import '@wangeditor/editor/dist/css/style.css'
  20. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  21. import type { IDomEditor } from '@wangeditor/editor'
  22. import { onBeforeUnmount, shallowRef, computed, watch } from 'vue'
  23.  
  24. type TProps = {
  25.   mode?: string,
  26.   valueHtml?: string,
  27.   placeholder?: string,
  28.   disable?: boolean
  29. }
  30. const props = withDefaults(defineProps<TProps>(), {
  31.   mode: 'default', // or 'simple'
  32.   valueHtml: '',
  33.   placeholder: '请输入内容...',
  34.   disable: false
  35. })
  36. type TEmits = {
  37.   (e: 'update:valueHtml', params: string): void
  38.   (e: 'update:valueText', params: string): void
  39.   (e: 'onChange', params: IDomEditor): void
  40. }
  41. const emits = defineEmits<TEmits>()
  42.  
  43. const editorRef = shallowRef()
  44.  
  45. const valueHtml = computed({
  46.   get() {
  47.     return props.valueHtml
  48.   },
  49.   set(valueHtml) {
  50.     emits('update:valueHtml', valueHtml)
  51.   }
  52. })
  53.  
  54. watch(() => props.disable, bool => {
  55.   if(!editorRef.value) return
  56.   bool ? (editorRef.value as IDomEditor).disable() : (editorRef.value as IDomEditor).enable()
  57. }, { deep: true })
  58.  
  59. const editorConfig = computed(() => {
  60.   return { placeholder: props.placeholder }
  61. })
  62.  
  63. // 记录 editor 实例,重要!
  64. const handleCreated = (editor: IDomEditor) => {
  65.   editorRef.value = editor
  66. }
  67.  
  68. // editor 改变
  69. const onChange = (editor: IDomEditor) => {
  70.   emits('onChange', editor)
  71. }
  72.  
  73. // 销毁编辑器
  74. onBeforeUnmount(() => {
  75.   if(!editorRef.value) return
  76.   editorRef.value.destroy()
  77. })
  78.  
  79. function getHtml() {
  80.   return (editorRef.value as IDomEditor).getHtml()
  81. }
  82. function getText() {
  83.   return (editorRef.value as IDomEditor).getText()
  84. }
  85. defineExpose({ getHtml, getText })
  86. </script>
  87. <style scoped>
  88. .Wft-Editor {
  89.   width: 100%;
  90.   height: 100%;
  91. }
  92. .flex {
  93.   display: flex;
  94. }
  95.  
  96. .flex-col {
  97.   flex-direction: column;
  98. }
  99.  
  100. .default-border {
  101.   border-bottom: 1px solid #ccc;
  102. }
  103.  
  104. .flex-1 {
  105.   flex: 1;
  106. }
  107.  
  108. .overflow-y-auto {
  109.   overflow-x: hidden;
  110.   overflow-y: auto;
  111. }
  112. </style>
  1. 3. 使用 
  2. <template>
  3.   <div class="wel">
  4.     <div class="btn">
  5.       <button @click="submit">提交</button>
  6.       <button @click="getHtml">获取HTML</button>
  7.       <button @click="getText">获取TEXT</button>
  8.       <button @click="editorDisable = !editorDisable">{{ editorDisable ? '启用' : '禁用' }}</button>
  9.     </div>
  10.     <div class="editor-container">
  11.       <Editor
  12.         ref="EditorRef"
  13.         :disable="editorDisable"
  14.         v-model:value-html="editorValue"
  15.         @on-change="editorChange"
  16.       />
  17.     </div>
  18.   </div>
  19. </template>
  20. <script setup lang="ts">
  21. import Editor from '@/comps/Editor/index.vue'
  22. import { onMounted, ref, shallowRef } from 'vue'
  23. import type { IDomEditor } from '@wangeditor/editor'
  24.  
  25. let editorValue = ref('') // 提交的数据
  26. let editorDisable = ref(false)
  27. let EditorRef = shallowRef<InstanceType<typeof Editor>>()
  28.  
  29. onMounted(() => {
  30.   setTimeout(() => {
  31.     editorValue.value = '<h1>回显测试</h1>'
  32.   }, 3000)
  33. })
  34.  
  35. const submit = () => {
  36.   console.log(editorValue.value)
  37. }
  38. const getHtml = () => {
  39.   console.log(EditorRef.value?.getHtml())
  40. }
  41. const getText = () => {
  42.   console.log(EditorRef.value?.getText())
  43. }
  44. const editorChange = (editor: IDomEditor) => {
  45.   console.log(editor.getHtml())
  46.   console.log(editor.getText())
  47. }
  48. </script>
  49. <style scoped>
  50. .wel {
  51.   width: 100%;
  52.   height: 100%;
  53. }
  54. .btn {
  55.   width: 100%;
  56.   height: 40px;
  57.   display: flex;
  58.   align-items: center;
  59. }
  60. .btn button {
  61.   margin-left: 15px;
  62. }
  63. .editor-container {
  64.   width: 100%;
  65.   height: calc(100% - 40px);
  66. }
  67. </style>


4. 效果 
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/634972
推荐阅读
相关标签
  

闽ICP备14008679号