在vue项目中使用tinymce并实现自定义的图片上传
tinymce和vue的介绍不说了,这些自行百度 先上我的环境 npm 6.9.0 node v12.3.0 tinymce 5.7.0
文件结构 
- langs:语言包(名字langs是固定的),链接来自http://tinymce.ax-z.cn/static/tiny/langs/zh_CN.js
- plugins:插件位置(名字plugins是固定的),这个是用来保存你自己制作的插件的,每个目录对应一个插件名称,里面的
config.js 该插件的配置 index.js 入口文件 plugin.js 插件的具体实现内容,文件名也是固定的 - index.vue 略
- plugin.js 富文本的插件
- toolbar.js 富文本的工具栏
直接上项目代码,一些遇到的问题和坑我都标上注释了
项目代码 imgUpload-config.js
const width=700;
const resize=false;
const height=500;
const imgChange = function (file) {
return new Promise((resolve, reject) => {
console.log("获取到的文件", file);
let defaultUrl = "https://www.tiny.cloud/docs/images/logos/for-base/logo-spaces-docs-for-base.svg";
let url = "";
resolve(url || defaultUrl);
});
};
export default {
imgChange,width,height,resize
}
imgUpload-config.js
require("./plugin.js")
impUpload-plugins.js
import config from "./config";
tinymce.PluginManager.add('imgUpload', function (editor, url) {
function openFileUpload(e) {
let input = document.createElement("input");
input.type = "file";
input.accept = "image/jpeg,image/png";
input.click();
input.addEventListener("change", function (e) {
let files = e.target.files;
let file = {};
if (files.length > 1) {
file = files[0];
console.log("富文本编辑器中上传的图片超出一个,这里只取第一个");
} else {
file = files[0];
}
config.imgChange(file).then(url => {
editor.insertContent(`<img src="${url}">`)
input.remove();
});
})
}
editor.ui.registry.addButton('imgUpload', {
icon: 'image',
tooltip: '图片上传',
onAction: function (...e) {
openFileUpload(e);
}
});
editor.ui.registry.addMenuItem('imgUpload', {
icon: 'image',
tooltip: '图片上传',
onAction: function (...e) {
openFileUpload(e);
}
});
return {
getMetadata: function () {
return {
name: "imgUpload",
url: "#",
};
}
};
});
index.vue代码
<template>
<div>
<div id="textarea"></div>
</div>
</template>
<script>
const id = "textarea";
import config from "./plugins/imgUpload/config";
import toolbar from "./toolbar";
import plugins from "./plugins";
import './plugins/imgUpload';
export default {
name: "index",
data() {
return {
init: {
selector: "#"+id,
plugins,
toolbar,
language: 'zh_CN',
width: config.width,
height: config.height,
resize: config.resize,
statusbar: true,
branding: true,
toolbar_mode: "sliding",
placeholder: '请输入内容',
},
}
},
mounted() {
const _this=this;
this.initTinymce();
setTimeout(function () {
_this.handleSetContent("hello world")
},5000)
setTimeout(function () {
let content=_this.handleGetContent();
console.log("content",content)
},10000)
},
methods: {
initTinymce() {
tinymce.init(this.init);
},
handleSetContent(content) {
tinymce.get(id).setContent(content);
},
handleGetContent() {
let content = tinymce.get(id).getContent();
console.log("获取到的富文本内容是", content);
return content;
}
}
}
</script>
<style scoped>
</style>
plugins.js
export default [
'fullscreen',
"hr",
"emoticons",
"insertdatetime",
"preview",
"print",
"searchreplace",
"wordcount",
"imgUpload",
"table",
];
toolbar.js
export default
"imgUpload "
+ "hr "
+ "emoticons "
+ "insertdatetime "
+ "searchreplace "
+ "wordcount "
+ "fullscreen "
+ "preview "
+ "print "
效果 
更多内容参考文档 http://tinymce.ax-z.cn/
|