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);
          });

        },
相关推荐
Eiceblue27 分钟前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
m0_555762901 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
浪裡遊2 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
lzb_kkk3 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼3 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
简佐义的博客3 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
程序员爱钓鱼4 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt
Frank学习路上4 小时前
【IOS】XCode创建firstapp并运行(成为IOS开发者)
开发语言·学习·ios·cocoa·xcode
2301_805054564 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python