首页 > 其他分享 >wangeditor 富文本 使用及 上传本地图片的方法

wangeditor 富文本 使用及 上传本地图片的方法

时间:2023-12-02 15:23:24浏览次数:26  
标签:const wangeditor content 编辑器 editor props 组件 文本 上传

文章标题:Vue 组件实现富文本编辑器

文章摘要:本文介绍了如何使用 Vue 和 Wangeditor 插件实现富文本编辑器组件,并详细解释了组件中的各个部分和功能。

Vue 组件实现富文本编辑器 在 Web 开发中,富文本编辑器是一个非常常见的功能,它能够让用户以所见即所得的方式编辑和排版文本内容。本文将介绍如何使用 Vue 和 Wangeditor 插件实现一个富文本编辑器组件。

  1. 安装依赖 首先,我们需要安装@wangeditor/editor-for-vue 插件,该插件提供了与 Vue 集成的富文本编辑器组件。可以通过以下命令来安装:

npm install @wangeditor/editor-for-vue

  1. 创建富文本编辑器组件 在 Vue 项目中,创建一个名为 RichTextEditor 的组件,用于展示富文本编辑器界面和处理相关逻辑。

组件模板代码如下:

<template>
  <div v-if="newVisible">
    <div class="container"></div>
    <div style="border: 1px solid #ccc">
      <!-- 工具栏 -->

      <Toolbar
        :editor="editorRef"
        style="border-bottom: 1px solid #ccc"
        :defaultConfig="toolbarConfig"
      />
      <!-- 编辑器 -->
      <Editor
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        style="height: 500px; overflow-y: hidden"
        @onCreated="handleCreated"
      />
    </div>
    <div class="btn-container">
      <CBotton type="primary" @click="save">保存</CBotton>
      <CBotton type="dashed" @click="cancel">取消</CBotton>
    </div>
  </div>
</template>

在上述代码中,我们通过 v-if 指令控制编辑器组件的显示和隐藏。组件包含一个工具栏和一个编辑器,并在底部添加了保存和取消按钮。

  1. 定义组件的数据和方法在标签中,我们需要定义组件的数据和方法。首先,引入所需的依赖:
<script setup>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { uploadFile } from "@/api/base";
import { submitUploadInfoApi, getHelpContent } from "@/api/sale/help-set.ts";
import { message } from "ant-design-vue";
import {
  onBeforeUnmount,
  shallowRef,
  defineProps,
  ref,
  computed,
  defineEmits,
  reactive,
  watch,
} from "vue";
</script>
接着,定义组件需要的数据和方法: ```javascript
<script setup>
// 编辑器实例,必须用 shallowRef,重要!
const editorRef = shallowRef();
const photoGalleryDialogVisible = ref();
const spinning = ref(false);

// 内容 HTML
const valueHtml = ref("");

const props = defineProps({
  visible: { type: Boolean, default: false },
  info: { type: Object, default: null },
  content: { type: Object, default: null },
});
const emit = defineEmits(["confirm", "update:visible", "select"]);

const newVisible = computed({
  get() {
    // 传过来的一行
    return props.visible;
  },
});

watch(
  () => props.visible,
  (val) => {
    console.log("val: ", props.info);
    if (val) {
      if (props?.content?.content) {
        valueHtml.value = props.content.content;
      }
    } else {
    }
  }
);

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
};

const save = async () => {
  // 保存逻辑
  const p = {
    categoryId: props.info.id,
    contentType: 1,
    content: valueHtml.value,
  };
  const res = await submitUploadInfoApi(p);
  if (res.code == 0) {
    cancel();
    emit("select", props.info.id);
    return message.success(res.msg);
  }
};

const cancel = () => {
  // 取消逻辑
  console.log("取消编辑");
  emit("update:visible", false);
  valueHtml.value = "";
};

// 组件销毁时,及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});
</script>

在上述代码中,我们使用了 Vue 3 中的 Composition API 来定义组件的数据和方法。其中,editorRef 用于保存编辑器实例,valueHtml 用于存储编辑器中的 HTML 内容。

save 方法用于保存编辑的内容,首先将内容发送到后端进行保存,并在保存成功后触发 select 事件。cancel 方法用于取消编辑操作,清空内容并隐藏编辑器。

最后,我们使用 onBeforeUnmount 钩子函数,在组件销毁前及时销毁编辑器实例。

  1. 配置工具栏和编辑器 在上述代码中,我们还需要配置编辑器的工具栏和编辑器本身的相关属性。代码如下:
<script setup>
// 工具栏配置
const toolbarConfig = {
  toolbarKeys: [
    // 一些常用的菜单 key
    "bold", // 加粗
    "italic", // 斜体
    "through", // 删除线
    "underline", // 下划线
    "bulletedList", // 无序列表
    "numberedList", // 有序列表
    "color", // 文字颜色
    "fontSize", // 字体大小
    "lineHeight", // 行高
    "uploadImage", // 上传图片
    "delIndent", // 缩进
    "indent", // 增进
    "deleteImage", //删除图片
    "divider", // 分割线
    "justifyCenter", // 居中对齐
    "justifyJustify", // 两端对齐
    "justifyLeft", // 左对齐
    "justifyRight", // 右对齐
    "undo", // 撤销
    "redo", // 重做
    "clearStyle", // 清除格式
  ],
};

// 编辑器配置
const editorConfig = {
  placeholder: "",
  MENU_CONF: {
    uploadImage: {
      server: uploadFile.url,
      headers: { Authorization: uploadFile.Authorization },
      "tenant-id": "1",
      fieldName: "file",
      customInsert(res, insertFn) {
        if (res.code == 0) {
          insertFn(res.data, null, res.data);
        }
      },
    },
  },
};
</script>

在上述代码中,我们通过 toolbarConfig 配置对象定义了工具栏的显示菜单,包括加粗、斜体、删除线、下划线、无序列表、有序列表、文字颜色、字体大小、行高、上传图片等功能。

editorConfig 用于配置编辑器的相关属性,包括占位符文本(placeholder)以及上传图片的配置。

  1. 添加样式 最后,在组件的标签中,我们可以添加一些样式来美化富文本编辑器的界面。例如:
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
  .btn-container {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    margin-top: 60px;
  }
  .container {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  h1 {
    text-align: center;
    font-size: 28px;
  }
</style>

以上代码中,我们引入了 Wangeditor 默认的样式文件,并自定义了一些样式来调整按钮容器和编辑器容器的布局。

  1. 使用富文本编辑器组件 完成以上步骤后,我们就可以在其他 Vue 组件中使用 RichTextEditor 组件了。例如:
<Editor
  v-show="onsitesVisible"
  :info="onsiteActive"
  :content="content"
  v-model:visible="onsitesVisible"
  @confirm="getLists"
  @select="selects"
/>

在上述代码中,我们将 RichTextEditor 组件作为子组件引入,并通过:visible、:info 和:content 属性来传递组件需要的参数。同时,可以监听 select 事件来处理富文本编辑器中的选中操作。

结语 在本文中,我们介绍了如何使用 Vue 和 Wangeditor 插件实现一个富文本编辑器组件,并详细解释了组件中的各个部分和功能。通过这个组件,用户可以方便地进行富文本内容的编辑和排版。希望本文能对你理解和使用富文本编辑器有所帮助!

完整代码

<template>
  <div v-if="newVisible">
    <div class="container"></div>
    <div style="border: 1px solid #ccc">
      <!-- 工具栏 -->

      <Toolbar
        :editor="editorRef"
        style="border-bottom: 1px solid #ccc"
        :defaultConfig="toolbarConfig"
      />
      <!-- 编辑器 -->
      <Editor
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        style="height: 500px; overflow-y: hidden"
        @onCreated="handleCreated"
      />
    </div>
    <div class="btn-container">
      <CBotton type="primary" @click="save">保存</CBotton>
      <CBotton type="dashed" @click="cancel">取消</CBotton>
    </div>
  </div>
</template>
<script setup>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { uploadFile } from "@/api/base";
import { submitUploadInfoApi, getHelpContent } from "@/api/sale/help-set.ts";
import { message } from "ant-design-vue";
import {
  onBeforeUnmount,
  shallowRef,
  defineProps,
  ref,
  computed,
  defineEmits,
  reactive,
  watch,
} from "vue";

// 编辑器实例,必须用 shallowRef,重要!
const editorRef = shallowRef();
const photoGalleryDialogVisible = ref();
const spinning = ref(false);

// 内容 HTML
const valueHtml = ref("");

const props = defineProps({
  visible: { type: Boolean, default: false },
  info: { type: Object, default: null },
  content: { type: Object, default: null },
});
const emit = defineEmits(["confirm", "update:visible", "select"]);

const newVisible = computed({
  get() {
    // 传过来的一行
    return props.visible;
  },
});
watch(
  () => props.visible,
  (val) => {
    console.log("val: ", props.info);
    if (val) {
      if (props?.content?.content) {
        valueHtml.value = props.content.content;
      }
    } else {
    }
  }
);
const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
};
const save = async () => {
  // 保存逻辑
  const p = {
    categoryId: props.info.id,
    contentType: 1,
    content: valueHtml.value,
  };
  const res = await submitUploadInfoApi(p);
  if (res.code == 0) {
    cancel();
    emit("select", props.info.id);
    return message.success(res.msg);
  }
};

const cancel = () => {
  // 取消逻辑
  console.log("取消编辑");
  emit("update:visible", false);
  valueHtml.value = "";
};

// 组件销毁时,及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});
// 工具栏配置
const toolbarConfig = {
  toolbarKeys: [
    // 一些常用的菜单 key
    "bold", // 加粗
    "italic", // 斜体
    "through", // 删除线
    "underline", // 下划线
    "bulletedList", // 无序列表
    "numberedList", // 有序列表
    "color", // 文字颜色
    "fontSize", // 字体大小
    "lineHeight", // 行高
    "uploadImage", // 上传图片
    "delIndent", // 缩进
    "indent", // 增进
    "deleteImage", //删除图片
    "divider", // 分割线
    "justifyCenter", // 居中对齐
    "justifyJustify", // 两端对齐
    "justifyLeft", // 左对齐
    "justifyRight", // 右对齐
    "undo", // 撤销
    "redo", // 重做
    "clearStyle", // 清除格式
  ],
};
// 编辑器配置

const editorConfig = {
  placeholder: "",
  MENU_CONF: {
    uploadImage: {
      server: uploadFile.url,
      headers: { Authorization: uploadFile.Authorization },
      "tenant-id": "1",
      fieldName: "file", // 这里有个坑,如果返回的响应结果是没有上传文件,跟这里关系很大
      customInsert(res, insertFn) {
        if (res.code == 0) {
          insertFn(res.data, null, res.data);
        }
      },
    },
  },
};
</script>

<!-- 别忘了引入样式 -->
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.btn-container {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  margin-top: 60px;
}
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

h1 {
  text-align: center;
  font-size: 28px;
}
</style>

标签:const,wangeditor,content,编辑器,editor,props,组件,文本,上传
From: https://www.cnblogs.com/wp-leonard/p/17871641.html

相关文章

  • vue3使用富文本编辑器wangEditor 5,增加自定义下拉框,并动态改变下拉框内容
    官方资料wangEditor官网效果展示准备工作这里按照wangEditor官网提供的Vue3Demo操作就行,下面的内容可以直接跳过安装yarnadd@wangeditor/editor#或者npminstall@wangeditor/editor--saveyarnadd@wangeditor/editor-for-vue@next#或者npminstall@w......
  • 微软Windows硬件最新驱动下载地址,无讨论,版本新,都是硬件厂家上传
    下面地址可以下载Windows硬件最新驱动,这里下载的地址是纯粹的驱动,没有其他累赘。这里的驱动是硬件厂家上传的驱动,我查找的是realtek8852AE的驱动,其他任何地方找到的驱动都没有这里的新。realtek8852AE这款螃蟹无线网卡,联想,红米等很多厂家的笔记本都在用,但器旧版驱动容易出......
  • python HTTP Server 文件上传与下载
    pythonHTTPServer文件上传与下载实现在局域网(同一WIFI下)文件上传与下载该模块通过实现标准GET在BaseHTTPServer上构建和HEAD请求。(将所有代码粘贴到同一个py文件中,即可使用)所需包基于python3版本实现,python2版本无涉猎importosimportsysimportargparseimport......
  • 1、自定义上传组件实现动态指定action
    1、增加ynamicAction:String2、修改constuploadImgUrl=ref(props.dynamicAction||import.meta.env.VITE_APP_BASE_API+"/common/upload");//上传的图片服务器地址<el-uploadmultiple:action="uploadImgUrl"3、父组件<el-form-itemlab......
  • C/C++ 实现FTP文件上传下载
    FTP(文件传输协议)是一种用于在网络上传输文件的标准协议。它属于因特网标准化的协议族之一,为文件的上传、下载和文件管理提供了一种标准化的方法,在Windows系统中操作FTP上传下载可以使用WinINet库,WinINet(WindowsInternet)库是Windows操作系统中的一个网络API库,用于访问Interne......
  • CSS进阶1--字体样式-文本样式
    link.css--连接外部样式表1.文字样式:①font-family:字体样式②font-style:文本的字体样式 属性:normal-正常 italic-斜体字样式显示 oblique-文字向一边倾斜(与italic类似,但不太支持)③font-variant--以小型大写字体或正......
  • Python 将文本转换成语音
    #coding:utf-8#pipinstallpyttsx3importpyttsx3aspyttsxtext='Icanbecauseithinkican.逆境清醒Lifeisnotallroses.人生并不是康庄大道。'engine=pyttsx.init()engine.say(text)#engine.setProperty('rate',100)#设置语速#engine.setPr......
  • 关于layui使用弹出层模版后对应上传组件不工作的问题记录
    这里页面的聊天室是使用的弹出层:util.on('lay-on',{'open-chat-room':function(){layer.open({title:'项目聊天室',type:1,offset:&#......
  • SFTP文件本地及远程创建,上传,下载,删除,获取脚本
    关键参数:sftp_route:远程路径local_route:本地路径(windows需要r解析)header:txt表头字段(列表方式)data:txt内容信息(列表嵌套列表方式)filename_success:是否创建success文件默认为falsefilename:文件名称(无需带后缀默认为txt文件) 注意:上传文件需要带文件名称下载只需要到文件路......
  • 富文本样式处理
    读取富文本接口,对富文本的样式进行处理此处为uniapp的方式(微信小程序同理)res.data.article_content=res.data.article_content.replace(/(style="(.*?)")|(width="(.*?)")|(height="(.*?)")/ig,'')//清除富文本所有标签的styleres.data.article_content=res.da......