tinymce新增多图片上传功能

news2024/10/5 12:53:23

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

在这里插入图片描述

/**
 * 文件路径:/src/tinymce/plugins/images/index.js
 */
import request from "@/utils/request"; 
tinymce.PluginManager.add('images', function (editor) {
  let pluginName = '多图上传'

  let bodyId = editor.getParam('body_id', '', 'hash')
  var imgList = []
  //配置弹出层
  let openDialog = function () {
    return editor.windowManager.open({
      title: pluginName,
      size: 'medium',
      body: {
        type: 'panel',
        items: [
          {
            name: 'multifile',
            type: 'htmlpanel',
            html: ' <div style="min-height:200px"><div style="background: #3d97d4;color: #fff;border-radius: 3px;padding: 8px 16px;width: 120px;text-align: center;cursor: pointer;" class="addfile " id="addfile">+ 添加文件</div> <ul id="file_list"></ul></div>',
          }
        ]
      },
      buttons: [{
        type: 'cancel',
        text: 'Close'
      }, {
        type: 'custom',
        text: 'Save',
        name: 'save',
        primary: true
      }],
      onAction: function (api, details) {
         
          console.log(imgList,"里面获取到什么==========")
        switch (details.name) {
          case 'save':
           	var html = '';
						var imgs = imgList;
            var len = imgs.length;
            if (len>0) {
              	for(let i=0;i<len;i++){
							if( imgs[i] ){
								html += '<img src="'+imgs[i]+'" />	';
							}
						}
            editor.insertContent(html)
            api.close()
            }
					
            break
          default:
            break
        }
      },
      onChange(api, evt) {
        console.log('change')
        console.log(api)
        console.log(evt)
      }
    })
  }

  //添加事件监听
  let addEventListener = function (editor) {
    imgList = []
        //添加文件
    document.querySelector('.addfile').addEventListener('click',()=>{
      var input = document.createElement('input');
      const filetype=".png,.gif,.jpg,.jpeg"
        input.setAttribute('type', 'file');
      input.setAttribute('multiple', 'multiple');
      input.setAttribute('accept', filetype);
        input.click();
        input.onchange = function(event) {
            var files = event.target.files;
            addList(files);
        }
    });
// let inputElement = document.getElementById('inputId');
// // 添加事件监听
// if (inputElement) {
//   inputElement.addEventListener('change', function(event) {
//     // 文件选择时执行的代码
//     console.log('文件已选择:', event.target.files);
//     const files = event.target.files
//     addList(files)

//   });
// } else {
//   console.log('未找到图片上传的input元素!');
// }
  }
  let addList = function (files) {
       console.log(files, "获取到什么222------------");
      var files_sum = files.length;
    var vDom = document.createDocumentFragment();
   
      for (let i = 0; i < files_sum; i++) {
        let file = files[i];

                  let params = new FormData();
            params.append("attachmentFile", file);
            let config = {
              headers: {
                "Content-Type": "multipart/form-data",
              },
            };
            request({
              url: "/attachment/upload",
              method: "post",
              headers: config.headers,
              data: params,
              onUploadProgress: (progressEvent) => {},
            })
              .then((resp) => {
                if (resp.code == 0) {
                  imgList.push(resp.data.attachment.url); //上传成功,在成功函数里填入图片路径
                } else {
                  failure("上传失败");
                }
              })
              .catch((error) => {
                failure("上传出错,服务器开小差了呢");
              });
        let blobUrl = window.URL.createObjectURL(file);
          let li = document.createElement("li");
          li.setAttribute("class", "up-no");
          li.setAttribute("data-time", file.lastModified);
          li.innerHTML =
            '<div class="picbox"><img style="width:150px;height:150px;margin:20px" src="' +
            blobUrl +
          '"></div>'
          // < div class="namebox" > <span>' +
          //   file.name +
          //   '</span></div><div class="tools"><a class="remove"></a></div>';
          vDom.appendChild(li);
        }
        document.querySelector("#file_list").appendChild(vDom);
        //reSort();
      
    }
  //添加图标
  editor.ui.registry.getAll().icons.images || editor.ui.registry.addIcon('images', '<svg viewBox="0 0 1280 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M1126.2,779.8V87.6c0-24-22.6-86.9-83.5-86.9H83.5C14.7,0.7,0,63.7,0,87.7v692c0,36.2,29.2,89.7,83.5,89.7l959.3-1.3c51.7,0,83.5-42.5,83.5-88.3zm-1044,4V86.3h961.6V783.7H82.2v0.1z" fill="#53565A"/><path d="M603,461.6L521.1,366.3,313,629.8,227.2,546.8,102.4,716.8H972.8v-170L768.2,235.2,603.1,461.6zM284.6,358.4a105.4,105.4,0,0,0,73.5-30c19.5-19.1,30.3-45,30.2-71.8,0-56.8-45.9-103-102.4-103-56.6,0-102.4,46.1-102.4,103C183.4,313.5,228,358.4,284.6,358.4z" fill="#9598A0"/><path d="M1197.7,153.6l-0.3,669.3s13.5,113.9-67.4,113.9H153.6c0,24.1,23.9,87.2,83.5,87.2h959.3c58.3,0,83.6-49.5,83.6-89.9V240.8c-0.1-41.8-44.9-87.2-82.3-87.2z" fill="#53565A"/></svg>')

  //注册toolbar按钮
  editor.ui.registry.addButton('images', {
    icon: 'images',
    tooltip: pluginName,
    onAction: function () {
      openDialog()
      addEventListener()
    }
  })
  //注册菜单选项
  editor.ui.registry.addMenuItem('images', {
    icon: 'images',
    text: pluginName,
    onAction: function () {
      openDialog()
      addEventListener()
    }
  })
  return {
    getMetadata: function () {
      return {
        name: pluginName,
        url: "https://dotatong.cn",
      }
    }
  }
})

TEditor编辑器配置


```bash
<template>
  <div class="tinymce-box">
    <Editor
      v-model="contentValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick"
    />
  </div>
</template>

<script>
// import api from '../api/api.js'

//引入tinymce编辑器
import Editor from "@tinymce/tinymce-vue";

//引入node_modules里的tinymce相关文件文件
import tinymce from "tinymce/tinymce"; //tinymce默认hidden,不引入则不显示编辑器
import "tinymce/themes/silver"; //编辑器主题,不引入则报错
import "tinymce/icons/default"; //引入编辑器图标icon,不引入则不显示对应图标

// 引入编辑器插件(基本免费插件都在这儿了)
import "tinymce/plugins/advlist"; //高级列表
import "tinymce/plugins/anchor"; //锚点
import "tinymce/plugins/autolink"; //自动链接
import "tinymce/plugins/autoresize"; //编辑器高度自适应,注:plugins里引入此插件时,Init里设置的height将失效
import "tinymce/plugins/autosave"; //自动存稿
import "tinymce/plugins/charmap"; //特殊字符
import "tinymce/plugins/code"; //编辑源码
import "tinymce/plugins/codesample"; //代码示例
import "tinymce/plugins/directionality"; //文字方向
import "tinymce/plugins/emoticons"; //表情
import "tinymce/plugins/fullpage"; //文档属性
import "tinymce/plugins/fullscreen"; //全屏
import "tinymce/plugins/help"; //帮助
import "tinymce/plugins/hr"; //水平分割线
import "tinymce/plugins/image"; //插入编辑图片
import "tinymce/plugins/importcss"; //引入css
import "tinymce/plugins/insertdatetime"; //插入日期时间
import "tinymce/plugins/link"; //超链接
import "tinymce/plugins/lists"; //列表插件
import "tinymce/plugins/media"; //插入编辑媒体
import "tinymce/plugins/nonbreaking"; //插入不间断空格
import "tinymce/plugins/pagebreak"; //插入分页符
import "tinymce/plugins/paste"; //粘贴插件
import "tinymce/plugins/preview"; //预览
import "tinymce/plugins/print"; //打印
import "tinymce/plugins/quickbars"; //快速工具栏
import "tinymce/plugins/save"; //保存
import "tinymce/plugins/searchreplace"; //查找替换
// import 'tinymce/plugins/spellchecker'  //拼写检查,暂未加入汉化,不建议使用
import "tinymce/plugins/tabfocus"; //切入切出,按tab键切出编辑器,切入页面其他输入框中
import "tinymce/plugins/table"; //表格
import "tinymce/plugins/template"; //内容模板
import "tinymce/plugins/textcolor"; //文字颜色
import "tinymce/plugins/textpattern"; //快速排版
import "tinymce/plugins/toc"; //目录生成器
import "tinymce/plugins/visualblocks"; //显示元素范围
import "tinymce/plugins/visualchars"; //显示不可见字符
import "tinymce/plugins/wordcount";
import "/public/tinymce/plugins/images"; //多图上传
import request from "@/utils/request"; //字数统计

export default {
  name: "TEditor",
  components: {
    Editor,
  },
  props: {
    value: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    plugins: {
      type: [String, Array],
      default:
        "print preview searchreplace autolink directionality visualblocks visualchars fullscreen image images link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists textpattern autosave ",
    },
    toolbar: {
      type: [String, Array],
      default:
        " bold italic underline image images strikethrough  alignment |  forecolor backcolor  outdent indent  | \
                    formatselect  fontsizeselect | bullist numlist  blockquote  link table  code",
      // default: 'bold italic underline   image  alignment forecolor | outdent indent | \
      //     backcolor   styleselect formatselect fontselect fontsizeselect | bullist numlist |strikethrough blockquote subscript superscript removeformat link| \
      //     table  media charmap hr pagebreak insertdatetime  preview | code selectall searchreplace visualblocks | indent2em lineheight formatpainter axupimgs'
    },
    height: {
      type: Number,
      default: 300,
    },
    isCustomStyle: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      init: {
        force_br_newlines: false,
        force_p_newlines: false, //不使用p标签换行
        forced_root_block: "",
        statusbar: false,
        language_url: "/tinymce/langs/zh_CN.js", //引入语言包文件
        language: "zh_CN", //语言类型

        skin_url: "/tinymce/skins/ui/oxide", //皮肤:浅色
        // skin_url: '/tinymce/skins/ui/oxide-dark',//皮肤:暗色

        plugins: this.plugins, //插件配置
        toolbar: this.toolbar, //工具栏配置,设为false则隐藏
        toolbar_groups: {
          alignment: {
            icon: "align-left",
            tooltip: "对齐",
            items: "alignleft aligncenter alignright alignjustify lineheight",
          },
        },
        menubar: false, //菜单栏配置,设为false则隐藏,不配置则默认显示全部菜单,也可自定义配置--查看 http://tinymce.ax-z.cn/configure/editor-appearance.php --搜索“自定义菜单”

        fontsize_formats:
          "12px 14px 16px 18px 20px 22px 24px 28px 32px 36px 48px 56px 72px", //字体大小
        font_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;", //字体样式
        lineheight_formats: "0.5 0.8 1 1.2 1.5 1.75 2 2.5 3 4 5", //行高配置,也可配置成"12px 14px 16px 20px"这种形式

        height: this.height, //注:引入autoresize插件时,此属性失效
        placeholder: "在这里输入文字",
        branding: false, //tiny技术支持信息是否显示
        resize: false, //编辑器宽高是否可变,false-否,true-高可变,'both'-宽高均可,注意引号
        // statusbar: false,  //最下方的元素路径和字数统计那一栏是否显示
        elementpath: false, //元素路径是否显示

        content_style: "img {max-width:100%;}", //直接自定义可编辑区域的css样式
        // content_css: '/tinycontent.css',  //以css文件方式自定义可编辑区域的css样式,css文件需自己创建并引入

        // images_upload_url: '/apib/api-upload/uploadimg',  //后端处理程序的url,建议直接自定义上传函数image_upload_handler,这个就可以不用了
        // images_upload_base_path: '/demo',  //相对基本路径--关于图片上传建议查看--http://tinymce.ax-z.cn/general/upload-images.php
        paste_data_images: true, //图片是否可粘贴
        images_upload_handler: (blobInfo, success, failure) => {
          if (blobInfo.blob().size / 1024 / 1024 > 2) {
            failure("上传失败,图片大小请控制在 2M 以内");
          } else {
            let params = new FormData();
            params.append("attachmentFile", blobInfo.blob());
            let config = {
              headers: {
                "Content-Type": "multipart/form-data",
              },
            };
            request({
              url: "/attachment/upload",
              method: "post",
              headers: config.headers,
              data: params,
              onUploadProgress: (progressEvent) => {},
            })
              .then((resp) => {
                if (resp.code == 0) {
                  success(resp.data.attachment.url); //上传成功,在成功函数里填入图片路径
                } else {
                  failure("上传失败");
                }
              })
              .catch((error) => {
                failure("上传出错,服务器开小差了呢");
              });
 
          }
        },
        setup: function (editor) {
          editor.on("init", function (e) {
            let isCustomStyle = localStorage.getItem("IsCustomStyle");
            if (isCustomStyle == "true") {
              this.getBody().style.fontSize = "12px";
              this.getBody().style.color = "#6b778c";
              this.getBody().style.lineheight = "12px";
            }
          });
        },
      },
      contentValue: this.value,
    };
  },
  watch: {
    value(newValue) {
      this.contentValue = newValue;
    },
    contentValue(newValue) {
      this.$emit("input", newValue);
    },
  },
  created() {},
  mounted() {
    localStorage.setItem("IsCustomStyle", this.isCustomStyle);
    tinymce.init({});
  },
  methods: {
    // 添加相关的事件,可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    onClick(e) {
      this.$emit("onClick", e, tinymce);
    },
    //清空内容
    clear() {
      this.contentValue = "";
    },
  },
};
</script>

<style lang="less">
.tox-tinymce {
  border: 1px solid#ebecf0 !important;
  border-radius: 4px !important;
}
.tox:not([dir="rtl"]) .tox-toolbar__group:not(:last-of-type) {
  border-right: 1px solid#ebecf0 !important;
}
.tox .tox-toolbar,
.tox .tox-toolbar__overflow,
.tox .tox-toolbar__primary {
  border-bottom: 1px solid#ebecf0 !important ;
  background: #f2f6fc !important;
}
.tox .tox-tbtn svg {
  fill: #6b778c !important;
}

.tinymce-box .tox .tox-tbtn--bespoke .tox-tbtn__select-label {
  width: 60px;
}
.tox-tinymce-aux {
  z-index: 5000 !important;
}
#file_list {
  margin-left: -56px;
  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
}
.up-no {
  list-style: none;
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1139615.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

windows使用cygwin编译Xyce

windows使用cygwin编译Xyce 整体流程如Xyce构建教程大致类似&#xff0c;我这里把我编译的步骤写下来&#xff0c;方便大家食用 环境准备&#xff1a; win10及以上操作系统 cygwin安装包&#xff1a;Cygwin trillinos源码&#xff1a;Trillinos Xyce源码&#xff1a;Xyce …

TimeGPT:时间序列预测的第一个基础模型

时间序列预测领域在最近的几年有着快速的发展&#xff0c;比如N-BEATS、N-HiTS、PatchTST和TimesNet。 大型语言模型(llm)最近在ChatGPT等应用程序中变得非常流行&#xff0c;因为它们可以适应各种各样的任务&#xff0c;而无需进一步的训练。 这就引出了一个问题:时间序列的…

21款奔驰GLE450升级23P驾驶辅助 缓解开车疲劳

驾驶辅助和自动驾驶的区别就是需要人为去接管&#xff0c;虽然车辆会根据道路自己行驶&#xff0c;弯道上也能居中自动修正行驶&#xff0c;长时间不接管方向盘&#xff0c;系统会提示人为接管&#xff0c;这就是奔驰的23P驾驶辅助系统&#xff0c; 很多车友升级23P驾驶辅助系…

跨境商城开发秘籍揭密:如何选择最适合你的技术方案?

在全球化的商业环境下&#xff0c;跨境电商正蓬勃发展&#xff0c;成为许多企业拓展国际市场的首选方式。而跨境商城开发作为实现这一目标的关键&#xff0c;选择合适的技术方案显得尤为重要。本文将揭示跨境商城开发的秘籍&#xff0c;为您提供权威的指导&#xff0c;助力您选…

Mysql数据库 4.SQL语言 DQL数据查询语言 查询

DQL数据查询语言 从数据表中提取满足特定条件的记录 1.单表查询 2.多表查询 查询基础语法 select 关键字后指定要查询到的记录的哪些列 语法&#xff1a;select 列名&#xff08;字段名&#xff09;/某几列/全部列 from 表名 [具体条件]&#xff1b; select colnumName…

如何助力企业出海?未来发展趋势是什么?尽在「云通信」Tech专场

2023杭州云栖大会 倒计时4天&#xff01; 阿里云云通信 2大并行Session 6场话题演讲 今日「云通信」Tech 议程内容抢先知晓 01 「云通信」Tech • 国内企业出海&#xff0c;如何更高地提升市场营销的ROI&#xff0c;提升客户的转化率&#xff1f; • 面对海外存量客户&a…

数据库简介和sqlite3安装

数据库就是存储数据的仓库&#xff0c;其本质是一个文件系统&#xff0c;数据按照特定的格式将数据存储起来&#xff0c;用户可以对数据库中的数据进行增加&#xff0c;修改&#xff0c;删除及查询操作。 严格意义上来说,"数据库"不能被称之为"数据库",而…

GoLong的学习之路(十四)语法之标准库 time(时间包)的使用

文章目录 time包跨时区时间戳时间间隔时间操作addSubEqualBeforeAfter 定时器时间格式化解析字符串格式的时间 time包 时间和日期是我们编程中经常会用到的&#xff0c;本文主要介绍了 Go 语言内置的 time 包的基本用法。 time 包提供了一些关于时间显示和测量用的函数。time…

Mac 安装nvm

安装方案&#xff1a; 1. 从github下载nvm仓库到 ~/目录 地址&#xff1a;https://github.com/nvm-sh/nvm.git git clone https://github.com/nvm-sh/nvm.git 2. 进入nvm目录中执行install.sh等待执行完成&#xff0c;执行的操作方法就是直接将文件拖入到终端然后回车。 3.…

微信小程序 slot 不显示

问题:创建组件&#xff0c;使用带名字的slot&#xff0c;页面调用组件使用slot不显示 源码&#xff1a; 组件xml <view class"p-item br24" style"{{style}}"><slot name"right" wx:if"{{!custBottom}}"></slot>&l…

IP应用场景API的反欺诈潜力:保护在线市场不受欺诈行为侵害

前言 在数字化时代&#xff0c;网络上的商业活动迅速增长&#xff0c;但与之同时&#xff0c;欺诈行为也在不断演化。欺诈者不断寻找新方法来窃取个人信息、进行金融欺诈以及实施其他不法行为。为了应对这一威胁&#xff0c;企业和组织需要强大的工具&#xff0c;以识别和防止…

【干货】JVS低代码表单基础组件的配置与应用

表单的基础组件主要用于收集用户输入的数据&#xff0c;并对这些数据进行验证和处理。通过表单组件&#xff0c;用户可以输入各种类型的数据&#xff0c;如文本、数字、日期、选择项等。这些数据可以通过表单的提交按钮提交到服务器进行处理&#xff0c;从而使网站或应用程序能…

元宇宙热潮依旧,《神由都城》引人注目

随着5G、云计算、人工智能、虚拟现实等技术的成熟,元宇宙的发展已然驶入快车道,这对企业来说是一个发展壮大的好机会。常孝股份抓住创新发展的机遇,打造属于自己的元宇宙世界——《神由都城》。 《神由都城》是以常孝股份旗下的文旅景区“中华孝道园”为依托,打造的元宇宙世界…

leetcode刷题日记之做菜顺序

原题链接&#xff1a; 做菜顺序 一个厨师收集了他 n 道菜的满意程度 satisfaction &#xff0c;这个厨师做出每道菜的时间都是 1 单位时间。 一道菜的 「 like-time 系数 」定义为烹饪这道菜结束的时间&#xff08;包含之前每道菜所花费的时间&#xff09;乘以这道菜的满意程…

【点云学习PCL 】一

点云学习 说明&#xff1a;仅做学习使用&#xff0c;侵删 参考网址1 一、点云基础 0 概述 PCL&#xff08;Point Cloud Library&#xff09;是用于 2D/3D 图像和点云处理的大型开源跨平台的 C 编程库&#xff0c;PCL 框架实现了大量点云相关的通用算法和高效的数据结构&…

Bytebase 2.10.0 - 支持更灵活的变更发布人:指定任意角色或自定义审批流的最后节点

&#x1f680; 新功能 发布策略支持制定更灵活的变更发布人&#xff1a;可以指定任意角色集合&#xff0c;也可以指定自定义审批流的最后一个审批人。 支持在项目中创建分支保护规则。支持给数据库设置标签。支持给字段设置标签。支持给表设置分类分级。 &#x1f384; 改进 …

C# Winform编程(10)Chart图表控件

Chart控件 Chart控件Chart属性详述Chart属性设置图表样式属性数据样式属性图例样式图标区样式SeriesChartType类型 Chart控件鼠标滚轮事件特殊处理Series绑定数据演示代码鼠标滚轮缩放图表示例参考引用 Chart控件 Chart控件是微软自带的一种图形可视化组件&#xff0c;使用简单…

node开发微信群聊机器人第⑤章

▍PART 序 看本文时&#xff0c;请确保前4章都已经看过&#xff0c;不然本章你看着看着思维容易跳脱&#xff01;再一个机器人教程只在公众号&#xff1a;“程序员野区”首发。csdn会跟着发一份&#xff0c;未经博主同意&#xff0c;请勿转载&#xff01;欢迎分享到自己的微信…

前端入门(一)JavaScript语法、数据类型、运算、函数

文章目录 概念JavaScript编写的位置基本语法数据类型变量声明var、let、constundefined与null的区别字符串类型数组Map和Set函数定义与参数传递 变量的作用域let、const方法的定义与调用常用内部对象json对象原型操作BOM对象操作DOM对象表单操作&#xff08;验证&#xff09;MD…

年轻人开发谁用默认背景?我直接美图安排上

文章目录 一、你还在用传统的开发界面吗二、年轻人的界面 1.动漫型2.偶像型3.提神型 三、更换背景的操作 第一步第二步第三步 一、你还在用传统的开发界面吗 不比不知道&#xff0c;一比吓一跳&#xff0c;都2023年了&#xff0c;你还在用Pycharm的默认背景写代码吗&#xf…