简单封装 wangEditor富文本编辑器,保留大部分粘贴样式

效果:
在这里插入图片描述
在这里插入图片描述

这个编辑器有个好处就是形成的富文本样式都是行内样式,不用担心样式丢失(除了表格样式),网页粘贴过来的内容能保存大部分样式,粘贴word文档除了图片不行,其他大部分样式基本保留

<template>
  <div>
    <div id="editor"></div>
    <hr />
    效果预览(v-html)
    <div class="html" v-html="text1"></div>
    <hr />
    富文本的 HTML内容 <br />
    <textarea id="text1" v-model="text1" style="height: 200px"></textarea>
    <hr />
  </div>
</template>

<script>
import E from "wangeditor";

export default {
  data() {
    return {
      text1: "",
      editor: undefined,
    };
  },
  computed: {},
  mounted() {
    this.editor = new E("#editor");
    // 设置编辑区域高度为 300px
    this.editor.config.height = 500;
    // 编辑器 z-index 默认为 10000,可以自行调整。
    this.editor.config.zIndex = 20;
    // 可以修改 placeholder 的提示文字。
    this.editor.config.placeholder = "请输入正文";
    // 取消自动 focus
    this.editor.config.focus = false;

    // *****************************************************
    this.editor.config.menus = [
      "head",
      "bold",
      "fontSize",
      "fontName",
      "italic",
      "underline",
      "strikeThrough",
      "indent",
      "lineHeight",
      "foreColor",
      "backColor",
      "link",
      "list",
      "todo",
      "justify",
      "quote",
      "emoticon",
      "image",
      "video",
      "table",
      "code", // 插入代码
      "splitLine",
      "undo",
      "redo",
    ];
    // *****************************************************
    // 配置颜色(文字颜色、背景色)
    this.editor.config.colors = [
      "#000000",
      "#eeece0",
      "#1c487f",
      "#4d80bf",
      "#ffffff",
      "#FF0000",
      "#00FF00",
      " #0000FF",
      "#FF00FF",
      "#FFFF00",
      "#70DB93",
      "#5C3317",
    ];
    // *****************************************************
    // 配置字体
    this.editor.config.fontNames = [
      // 字符串形式
      "仿宋",
      "黑体",
      "楷体",
      "标楷体",
      "华文仿宋",
      "华文楷体",
      "宋体",
      "微软雅黑",
      "Arial",
      "Tahoma",
      "Verdana",
      "Times New Roman",
      "Courier New",
    ];
    // *****************************************************
    // 设置字号,只能改变  name字段
    this.editor.config.fontSizes = {
      "x-small": { name: "14px", value: "1" },
      small: { name: "16px", value: "2" },
      normal: { name: "18px", value: "3" },
      large: { name: "20px", value: "4" },
      "x-large": { name: "22px", value: "5" },
      "xx-large": { name: "24px", value: "6" },
      "xxx-large": { name: "26px", value: "7" },
    };
    // 配置行高
    this.editor.config.lineHeights = [
      "1",
      "1.25",
      "1.5",
      "1.75",
      "2",
      "2.5",
      "3",
    ];
    // *****************************************************
    // 插入网络图片的回调
    this.editor.config.linkImgCallback = function (src, alt, href) {};
    // *********************粘贴********************************
    // 关闭粘贴样式的过滤
    this.editor.config.pasteFilterStyle = false;
    // 忽略粘贴内容中的图片
    this.editor.config.pasteIgnoreImg = false;
    let sss = false;
    // 显示在textarea中
    this.editor.config.onchange = (html) => {
      // 可以在这进行父子组件的传值;
      this.text1 = html;
    };
    // *************************上传图片****************************
    this.editor.config.showLinkImg = true; //上传网络图片
    this.editor.config.uploadImgShowBase64 = true; //base64 保存图片
    // 限制图片大小和类型
    this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024; // 2M
    // // 如果不希望限制类型,可将其设为空数组:
    this.editor.config.uploadImgAccept = [];
    this.editor.config.uploadImgMaxLength = 3; // 一次最多上传 5 个图片
    // ***********************创建富文本***************
    this.editor.create();
  },
};
</script>

<style scoped>
#editor {
  width: 870px;
}
#text1 {
  width: 900px;
}
.html {
  width: 670px;
  margin-left: 200px;
}
.wrap {
  width: 870px;
  min-height: 300px;
  line-height: 300px;
  background-color: rgb(75, 73, 73);
  color: #fff;
  text-align: center;
}
/* 修改富文本字号 */
font[size="1"] {
  font-size: 14px;
}
font[size="2"] {
  font-size: 16px;
}
font[size="3"] {
  font-size: 18px;
}
font[size="4"] {
  font-size: 20px;
}
font[size="5"] {
  font-size: 22px;
}
font[size="6"] {
  font-size: 24px;
}
font[size="7"] {
  font-size: 26px;
}
/* 表格样式 */
/* table 样式  必须加 否则表格没有样式 */
.html >>> table {
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
  margin: 10px 0;
  line-height: 1.5;
}
.html >>> table td,
.html >>> table th {
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  padding: 3px 5px;
  min-height: 30px;
  height: 30px;
}
.html >>> table th {
  border-bottom: 2px solid #ccc;
  text-align: center;
  background-color: #f1f1f1;
}
</style>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wangEditor是一款基于JavaScript和CSS开发的Web文本编辑器,它具有轻量级、简洁、易用的特点。相比于普通的文本编辑器文本编辑器可以输入超越文本的数据内容,包括上传图片、输入表情、字体大小字号调整、颜色设置、对齐方式等功能操作。\[1\] 在使用wangEditor文本编辑器时,首先需要引入相关代码。在editor.vue文件中,可以使用以下代码引入文本编辑器: ```html <template> <div> <div ref="editor" style="text-align:left"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'MyEditor', data() { return { editorContent: '', editor: null } }, props: { value: { type: String, required: true } }, model: { prop: 'value' }, methods: { getContent: function () { alert(this.editorContent) }, _initEditor(that) { var editor = new E(this.$refs.editor) editor.config.zIndex = 100 editor.create() that.editor = editor } }, mounted() { this._initEditor(this) setTimeout(() => { this.editor.txt.html(this.value) }, 1000) } } </script> <style scoped> </style> ``` 以上代码是一个使用wangEditor的基本示例,通过在组件中引入wangEditor并进行相关配置,可以实现文本编辑功能。\[2\] 如果在项目中多个地方都需要使用文本编辑器,可以将文本编辑器封装成一个组件,以减少重复代码。具体的使用方法可以参考相关文档和示例。\[3\] #### 引用[.reference_title] - *1* [文本编辑器wangEditor的使用(即学即用)](https://blog.csdn.net/kid00712138/article/details/122495640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [wangEditor文本编辑器(第一章:基础使用)](https://blog.csdn.net/DW14687/article/details/118440176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值