代码地址:https://github.com/hanghang2/tinymce-pastewordimage,撸码不宜给颗小星星呐!
tinymce 4和5版本有powerpaste复制word图片,但是不支持wps软件,看了几款富文本都没有支持wps和office粘贴保留图片的,于是乎决定自己做一款。
原生的paste事件中,可以获取到用户的粘贴信息,走入误区的我看到了text/html的信息有图片信息,地址是file:///xxx 的地址,浏览器安全策略限制,js无法访问客户端的这些文件,于是乎陷入了困境。
后续查到粘贴的text/rtf内容中有16进制的图片信息,于是便有了解决办法:把数据中的16进制图片信息转换为base64,然后替换页面的图片路径即可实现word的复制粘贴,于是乎做了一款tinymce 6版本的word粘贴插件,使用方法和源码地址:https://github.com/hanghang2/tinymce-pastewordimage
tinymce6 pastewordimage插件
一款从word粘贴到富文本框图片正常显示插件,wps和office都可以正常复制粘贴图片
使用步骤:
第一步:把当前plugins目录下的pastewordimage文件夹拷贝到你的项目tinymce/plugins目录下去;
例如react项目,路径为 项目根目录/public/tinymce/plugins/
第二步:tinymce初始化init配置plugins参属下增加pastewordimage;
例如:
// 如下增加pastewordimage参数
<Editor
init={{
branding: false, // 去掉右下角的水印
menubar: true, // 菜单栏
height: 500,
plugins: [
'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen',
'insertdatetime', 'media', 'table', 'code', 'help', 'wordcount', 'codesample',
'pastewordimage', // word复制图片插件
],
}}
initialValue="<p>This is the initial content of the editor.</p>"
onInit={(evt, editor) => editorRef.current = editor}
rootClassName={style.editor}
/>
第三步:版当前项目的tinymce.min.js文件拷贝到项目的tinymce目录中进行覆盖;
解释:
// 源码中 wps粘贴单个图片时候粘贴的内容中会有text/html类型,导致getDataTransferItems函数返回结果不正确;
// 修改如下:items[contentType]赋值增加判断;
const getDataTransferItems = dataTransfer => {
const items = {};
if (dataTransfer && dataTransfer.types) {
for (let i = 0; i < dataTransfer.types.length; i++) {
const contentType = dataTransfer.types[i];
try {
// items[contentType] = dataTransfer.getData(contentType);
items[contentType] = dataTransfer.types.indexOf('Files') === -1 ? dataTransfer.getData(contentType) : ''; // wps粘贴单个图片问题处理
} catch (ex) {
items[contentType] = '';
}
}
}
return items;
};