tinymce富文本支持word内容同时粘贴文字图片上传 vue2

效果图

先放文件


文件自取tinymce: tinymce富文本简单配置及word内容粘贴图片上传
封装tinymce

文件自取:tinymce: tinymce富文本简单配置及word内容粘贴图片上传
页面引用组件

javascript 复制代码
          <TinymceSimplify ref='TinymceSimplify'   v-model="knowledgeBlockItem.content" :disabled="disable" :height="530" class="filter-item" />


  import TinymceSimplify from '@/components/TinymceSimplify'

    components: {

      TinymceSimplify,
    },



  searchData(res) {
         this.knowledgeBlockItem.content=res.data
 this.$refs.TinymceSimplify.changeVaulue(res.data)
        },

简单解释一下封装文件的内容

总体思路就是,找到粘贴的内容将img的src转换成Blob URI,这样页面可以正常展示图片了
src/components/TinymceSimplify/index

复制代码
data() {
    return {
      //初始化配置
      myValue: this.value,
      init: {
       
        init_instance_callback: editor => {
          editor.on("paste", evt => {
            // 监听粘贴事件
            this.onPaste(evt);
          });
        },
 
    };
  },

提取图片信息,转换成Blob URI,再在监听事件中获取页面元素,将img src转换

javascript 复制代码
  editor.on("NodeChange Change KeyUp SetContent", () => {
            this.hasChange = true;
            this.$nextTick(() => {
              console.log("输入?zhelieditor.getContent()");
              console.log("this.pasteChange", this.pasteChange);

              if (this.pasteChange) {
                // HTML字符串
                let htmlString = editor.getContent();
                // Blob URL数组
                let urls = this.blobUrlArr;

                // 使用DOMParser解析HTML字符串
                let parser = new DOMParser();
                let doc = parser.parseFromString(htmlString, "text/html");

                // 获取所有的<img>标签
                let imgs = doc.querySelectorAll("img");

                // 遍历imgs和urls数组,设置每个img的src属性
                imgs.forEach((img, index) => {
                  if (index < urls.length) {
                    // 确保不会越界
                    img.src = urls[index];
                  }
                });

                // 现在doc包含了修改后的DOM,但如果你需要将其转换回字符串,可以这样做:
                let modifiedHtmlString = doc.body.innerHTML;

                console.log("htmlContent8999", modifiedHtmlString);
                this.pasteChange = false;

                this.$nextTick(() => {
                  editor.setContent(modifiedHtmlString);
                });
              }

              this.$emit("input", editor.getContent());
            });
          });
          editor.on("paste", (evt) => {
            // 监听粘贴事件
            this.onPaste(evt, editor);
          });

        },
相关推荐
路溪非溪12 小时前
python标准库使用总结
开发语言·python
SomeOtherTime14 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
zh_xuan14 小时前
kotlin常见面试题
开发语言·kotlin·面试题
草莓熊Lotso16 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
huaqianzkh17 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
沐晓时光1 天前
C语言入门,内存函数知识讲解
c语言·开发语言
David猪大卫1 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
用户3721574261351 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
fengci.1 天前
CTF2MISC中等部分
开发语言·前端·学习
xys_6781 天前
JavaEE通关手册:初识Java
java·开发语言