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

效果图

先放文件

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

封装tinymce

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

页面引用组件

          <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转换
 

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

        },

相关推荐

  1. vue + TinyMCE实现文本编辑器

    2024-07-19 20:16:05       25 阅读
  2. vue2文档例子,pdf转换word例子

    2024-07-19 20:16:05       17 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 20:16:05       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 20:16:05       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 20:16:05       62 阅读
  4. Python语言-面向对象

    2024-07-19 20:16:05       72 阅读

热门阅读

  1. 【玩转python】入门篇day10-python运算符详解

    2024-07-19 20:16:05       18 阅读
  2. ios CCSystem.m

    2024-07-19 20:16:05       18 阅读
  3. MySql的运用

    2024-07-19 20:16:05       20 阅读
  4. 使用 tcpdump 进行网络流量捕获与分析

    2024-07-19 20:16:05       20 阅读
  5. 挂马病毒是什么

    2024-07-19 20:16:05       19 阅读
  6. 深入理解并使用 MySQL 的 SUBSTRING_INDEX 函数

    2024-07-19 20:16:05       20 阅读
  7. 【无标题】

    2024-07-19 20:16:05       21 阅读
  8. z-fighting多视锥方案缺失depth information的思考

    2024-07-19 20:16:05       15 阅读
  9. 代码随想录打卡第二十九天

    2024-07-19 20:16:05       19 阅读
  10. 产品经理的样板

    2024-07-19 20:16:05       14 阅读
  11. 关于二进制和八进制

    2024-07-19 20:16:05       18 阅读
  12. Linux 和 Unix 系统中非常流行文本处理工具awk

    2024-07-19 20:16:05       16 阅读
  13. 专升本-1.0.4(英语)-升本208天-学习成果展示

    2024-07-19 20:16:05       18 阅读
  14. 1818:ATP

    2024-07-19 20:16:05       21 阅读
  15. 使用容器化技术部署淘客返利系统的实践与挑战

    2024-07-19 20:16:05       20 阅读
  16. 【WiFi】DFS Vs ZW-DFS

    2024-07-19 20:16:05       17 阅读