富文本编辑组件封装,tinymce、tinymce-vue

依赖:package.json

yarn add tinymce @tinymce/tinymce-vue

{
  "dependencies": {
    "@tinymce/tinymce-vue": "5.0.0",
    "tinymce": "6.3.1",
    "vue": "3.2.45",
  },
}

本地依赖:

在public下创建 static/tinymce 文件夹,包含

1:langs(中文语言包,官方下载地址

2:skins(皮肤,从 node_modules/tinymce/skins 中复制)

 

组件封装:com-editor/index.vue

<template>
  <div>
    <editor v-model="content" :init="init" :disabled="disabled"></editor>
  </div>
</template>

<script>
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/models/dom";
import "tinymce/themes/silver/theme";
import "tinymce/icons/default/icons";
import "tinymce/plugins/link"; //链接
import "tinymce/plugins/code"; //源代码
import "tinymce/plugins/codesample"; //代码示例
import "tinymce/plugins/wordcount"; //字数统计
import "tinymce/plugins/table"; //表格
import "tinymce/plugins/lists"; //列表(无序、有序)
import "tinymce/plugins/advlist"; //增强列表
import "tinymce/plugins/emoticons"; //字符表情
import "tinymce/plugins/emoticons/js/emojiimages";
import "tinymce/plugins/emoticons/js/emojis";
import "tinymce/plugins/preview"; //预览
import "tinymce/plugins/fullscreen"; //全屏
import "tinymce/plugins/image"; //图片
import "tinymce/plugins/searchreplace"; //查找替换
export default {
  components: {
    Editor,
  },
  props: {
    modelValue: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    height: {
      type: Number,
      default: 500,
    },
  },
  data() {
    return {
      //初始化配置
      init: {
        branding: false, //不显示右下角富文本支持方
        menubar: false, //不显示菜单栏
        statusbar: false, //不显示底部状态栏
        language_url: "/static/tinymce/langs/zh-Hans.js", //汉化
        language: "zh-Hans",
        skin_url: "/static/tinymce/skins/ui/tinymce-5", //主题
        content_css: "/static/tinymce/skins/content/default/content.css", //样式
        font_size_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px", //字号
        font_family_formats:
          "微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;", //字体
        height: this.height, //高度
        plugins: "",
        toolbar: "",
        // tinymce插入图片支持转base64操作
        images_upload_handler: (blobInfo, progress) => {
          return new Promise((resolve, reject) => {
            resolve(
              "data:" + blobInfo.blob().type + ";base64," + blobInfo.base64()
            );
          });
        },
      },
      content: this.modelValue,
      plugins: {
        lists: "列表",
        advlist: "列表增强",
        link: "链接",
        table: "表格",
        code: "源代码",
        codesample: "代码示例",
        wordcount: "字数统计",
        image: "图片",
        emoticons: "表情",
        preview: "预览",
        fullscreen: "全屏",
        searchreplace: "查找替换",
      },
      toolbar: {
        undo: "回退",
        redo: "前进",
        "removeformat |": "清除格式",
        // styles: "样式",
        blocks: "段落",
        fontsize: "字号",
        // fontfamily: "字体",
        bold: "加粗",
        italic: "斜体",
        strikethrough: "删除",
        "underline |": "下划线",
        forecolor: "字体颜色",
        "backcolor |": "背景颜色",
        alignleft: "左对齐",
        aligncenter: "居中对齐",
        alignright: "右对齐",
        alignjustify: "两端对齐",
        bullist: "无序列表",
        numlist: "有序列表",
        outdent: "减少缩进",
        indent: "增加缩进",
        "lineheight |": "行高",
        link: "链接",
        blockquote: "引用",
        hr: "分割线",
        emoticons: "表情",
        image: "图片",
        table: "表格",
        "codesample |": "代码块",
        wordcount: "字数统计",
        code: "源代码",
        preview: "预览",
        fullscreen: "全屏",
        searchreplace: "查找替换",
      },
      menubar: {
        // file:'文件',
        edit: "编辑",
        view: "查看",
        insert: "插入",
        format: "格式",
        tools: "工具",
        table: "表格",
      },
    };
  },
  created() {
    // this.init.menubar = Object.keys(this.menubar).join(" "); //菜单栏
    this.init.plugins = Object.keys(this.plugins).join(" "); //插件依赖
    this.init.toolbar = Object.keys(this.toolbar).join(" "); //插件栏
    tinymce.init({});
  },
  methods: {},
  watch: {
    modelValue(newValue) {
      this.content = newValue;
    },
    content(newValue) {
      this.$emit("update:modelValue", newValue);
    },
  },
};
</script>
<style lang="scss">
//不展示右侧升级链接
.tox-promotion {
  display: none;
}
</style>

组件使用:com-editor/demo.vue

<template>
  <div>
    com-editor Demo
    <div>{{ content }}</div>
    <com-editor v-model="content"></com-editor>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      content: "<p>dddddd</p>",
    };
  },
  methods: {},
};
</script>

<style scoped></style>

使用效果:

参考链接:

vue项目中使用 tinymce 富文本(本地依赖版)

tinymce官方文档

tinymce个人中文文档

Migrating from TinyMCE 5

inymce使用images_upload_handler将图片处理成base64或者url

tinymce6 上传图片报错Cannot read properties of undefined (reading ‘then‘)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在tinymce/tinymce-vue富文本编辑器中实现图片上传功能,你可以按照以下步骤操作: 1. 首先,确保你已经安装了tinymcetinymce-vue的依赖包。你可以去[TinyMCE官方网站](https://www.tiny.cloud/get-tiny/self-hosted/)下载最新的TinyMCE压缩包,并解压到你的项目目录中。然后,安装tinymce-vue依赖包,可以使用npm或yarn进行安装。 2. 接下来,将TinyMCE的skins文件夹复制到你的项目的public文件夹下。这个文件夹包含了富文本编辑器的样式文件。 3. 然后,创建一个Vue组件封装el-upload控件,并将其整合到tinymce-vue中。你可以将这个组件放在你的项目的src/components文件夹下。具体的组件代码可以参考上述提供的链接。 通过以上步骤,你就可以在tinymce/tinymce-vue富文本编辑器中实现图片上传功能了。请注意,这只是一种实现方式,具体的实现方式可能因项目需求的不同而有所差异。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vue3 中 tinymce+tinymce-vue 富文本编辑使用](https://blog.csdn.net/oooosadas/article/details/131176384)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vue-tinymce 富文本编辑器自定义图片上传](https://download.csdn.net/download/hadues/13183093)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值