项目需求:
vue项目中实现多图片批量上传功能
问题:
tinymce富文本编辑器的多图片批量上传插件 支持版本:5.0.4+
项目中现有的富文本编辑器版本:4.9.4
为实现这一功能选择更换tinymce版本 于是开启踩坑之旅。。
实现:
1.替换项目中tinymce文件 本项目放在\static\plugins文件夹
将tinymce.4.9.4更换成从官网上 http://download.tiny.cloud/tinymce/community/tinymce_5.7.0.zip
下载下来解压过的文件
并将语言包下载解压放到tinymce文件中 langs文件夹的zh_CN.js文件
2.更换路径
index.html文件内部的引入路径
<script src="./static/plugins/tinymce.4.9.4/tinymce.min.js"></script>
<script src="./static/plugins/tinymce.4.9.4/zh_CN.js"></script>
更换成自己替换后的文件:
<script src="./static/plugins/tinymce/tinymce.min.js"></script>
<script src="./static/plugins/tinymce/langs/zh_CN.js"></script>
static\config中init.js文件内部
'https://cdn.bootcss.com/tinymce/4.9.4/tinymce.min.js',
window.SITE_CONFIG.cdnUrl + '/static/plugins/tinymce.4.9.4/zh_CN.js',
同样作替换(用外部链接之后页面出现加载太慢问题,这里作下调整):
window.SITE_CONFIG.cdnUrl + '/static/plugins/tinymce/tinymce.min.js',
window.SITE_CONFIG.cdnUrl + '/static/plugins/tinymce/langs/zh_CN.js',
根据自己修改后的路径进行正确替换即可 如果此时页面上没有富文本编辑器可能不是你路径问题
3.富文本编辑器不出现原因
排查之后是theme: 'modern', 这个主题问题
改成TinyMCE随附的默认主题silver theme: 'silver', 之后 成功显示在页面 可参考文档做主题替换
4.工具栏内容不出现
通过查看文档http://tinymce.ax-z.cn/general/basic-setup.php 在toolbar逐步添加自己需要的工具
通过参照文档在plugins添加需要的插件

此时可以参考文档加入多图片批量上传插件

tinymce文档代码:
tinymce.init({
selector: '#tinydemo',
plugins: "code image axupimgs",
toolbar: "code axupimgs",
images_upload_base_path: '/demo',
images_upload_handler: function (blobInfo, succFun, failFun) {
var xhr, formData;
var file = blobInfo.blob();//转化为易于理解的file对象
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', '/demo/upimg2.php');
xhr.onload = function() {
var json;
if (xhr.status != 200) {
failFun('HTTP Error: ' + xhr.status);
return;
}
json = JSON.parse(xhr.responseText);
if (!json || typeof json.location != 'string') {
failFun('Invalid JSON: ' + xhr.responseText);
return;
}
succFun(json.location);
};
formData = new FormData();
formData.append('file', file, file.name );
xhr.send(formData);
}
});
5.点击菜单和部分工具栏没反应
搞半天发现是层级问题 因为是在弹框上使用该富文本编辑器 弹框层级过高 修改部分样式即可解决这一问题
最后附上富文本编辑器代码 这个项目是封装成了一个组件
<template>
<div class="tinymce" >
<textarea :id="tinymceId"></textarea>
</div>
</template>
<script>
import { getUUID } from '@/utils'
export default {
data () {
const tinymceId = 'tinymce' + getUUID()
return {
flag: true,
tinymceId: tinymceId,
myEditor: null
}
},
props: {
value: {
default: ''
},
tinymceHeight: {
default: 500,
type: Number
}
},
watch: {
value (val) {
if (this.flag) {
this.setContent(val)
}
this.flag = true
}
},
mounted () {
this.init()
},
beforeDestroy () {
tinymce.remove(`#${this.tinymceId}`)
},
methods: {
init () {
const self = this
this.Editor = tinymce.init({
// 默认配置
...this.getDefalutConfig(),
// 图片上传
images_upload_handler: (blobInfo, success, failure) => {
if (blobInfo.blob().size > self.maxSize) {
failure('文件体积过大')
}
const formData = new FormData()
formData.append('editorFile', blobInfo.blob())
this.$http({
url: this.$http.adornUrl(`/admin/file/upload/tinymceEditor`),
headers: {
'Content-Type': 'multipart/form-data',
'token': this.$cookie.get('token')
},
method: 'post',
data: formData
}).then(({ data }) => {
success(data)
}).catch(() => {
failure('服务器出了点小差')
})
},
// prop内传入的的config
...this.config,
// 挂载的DOM对象
selector: `#${this.tinymceId}`,
setup: (editor) => {
self.myEditor = editor
editor.on(
'init', () => {
self.loading = false
if (self.value) {
editor.setContent(self.value)
} else {
editor.setContent('')
}
}
)
// 抛出 'input' 事件钩子,同步value数据
editor.on(
'input change undo redo execCommand', () => {
self.flag = false
self.$emit('input', editor.getContent())
}
)
}
})
},
getDefalutConfig () {
return {
// GLOBAL
height: this.tinymceHeight,
theme: 'silver',
language: 'zh_CN',
menubar: 'file edit insert view format table tools help',
branding: false,
// toolbar: `insertfile`, // 需要的工具栏
toolbar:`undo redo | styleselect | formatselect | fontselect | fontsizeselect | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | image axupimgs link | insert paste code | numlist bullist | table fullscreen | forecolor backcolor hr | preview removeformat`,
plugins:`image link paste code lists advlist importcss table fullscreen media hr preview axupimgs`,
// plugins: `
// textcolor
// colorpicker
// `,
// CONFIG
forced_root_block: 'p',//将任何非块元素或文本节点都包装在块元素中
// force_p_newlines: true,
importcss_append: true,//将导入的样式附加到Format菜单的末尾,并将替换默认格式
fullscreen_native: true,
toolbar_mode:'sliding',//工具栏模式
// CONFIG: ContentStyle 这块很重要, 在最后呈现的页面也要写入这个基本样式保证前后一致, `table`和`img`的问题基本就靠这个来填坑了
content_style: `
* { padding:0; margin:0; }
html, body { height:100%; }
img { max-width:100%; display:block;height:auto; }
a { text-decoration: none; }
iframe { width: 100%; }
p { line-height:1.6; margin: 0px; }
table { word-wrap:break-word; word-break:break-all; max-width:100%; border:none; border-color:#999; }
.mce-object-iframe { width:100%; box-sizing:border-box; margin:0; padding:0; }
ul,ol { list-style-position:inside; }
`,
// insert_button_items: 'image link | inserttable', 已删除该配置
setup: function (editor) {
editor.ui.registry.addMenuButton('insert', {
icon: 'plus',
tooltip: 'Insert',
fetch: function (callback) {
callback('image link | inserttable');
}
});
},
// CONFIG: Paste
paste_retain_style_properties: 'all', //贴内容时要保留的样式
paste_word_valid_elements: '*[*]', // word需要它
paste_data_images: true, // 粘贴的同时能把内容里的图片自动上传,非常强力的功能
paste_convert_word_fake_lists: true, // 插入word文档需要该属性
paste_webkit_styles: 'all', //指定粘贴到WebKit中时要保留的样式
paste_merge_formats: true, //合并相同的文本格式元素
nonbreaking_force_tab: false, //按下键盘tab键时强制TinyMCE插入三个实体
// paste_auto_cleanup_on_paste: false,
// CONFIG: Font
fontsize_formats: '10px 11px 12px 14px 16px 18px 20px 24px',//文字大小列表
// CONFIG: StyleSelect 自定义段落样式格式
style_formats: [
{
title: '首行缩进',
block: 'p',
styles: { 'text-indent': '2em' }
},
{
title: '行高',
items: [
{ title: '1', styles: { 'line-height': '1' }, inline: 'span' },
{ title: '1.5', styles: { 'line-height': '1.5' }, inline: 'span' },
{ title: '2', styles: { 'line-height': '2' }, inline: 'span' },
{ title: '2.5', styles: { 'line-height': '2.5' }, inline: 'span' },
{ title: '3', styles: { 'line-height': '3' }, inline: 'span' }
]
}
],
// FontSelect 字体列表
font_formats: `
微软雅黑=微软雅黑;
宋体=宋体;
黑体=黑体;
仿宋=仿宋;
楷体=楷体;
隶书=隶书;
幼圆=幼圆;
Andale Mono=andale mono,times;
Arial=arial, helvetica,
sans-serif;
Arial Black=arial black, avant garde;
Book Antiqua=book antiqua,palatino;
Comic Sans MS=comic sans ms,sans-serif;
Courier New=courier new,courier;
Georgia=georgia,palatino;
Helvetica=helvetica;
Impact=impact,chicago;
Symbol=symbol;
Tahoma=tahoma,arial,helvetica,sans-serif;
Terminal=terminal,monaco;
Times New Roman=times new roman,times;
Trebuchet MS=trebuchet ms,geneva;
Verdana=verdana,geneva;
Webdings=webdings;
Wingdings=wingdings,zapf dingbats`,
// Tab
tabfocus_elements: ':prev,:next',//按下Tab键时,此设置可用于更改编辑器的焦点行为
object_resizing: true,//可以打开/关闭图像,表格或媒体对象的大小调整手柄
// Image
imagetools_toolbar: 'rotateleft rotateright | flipv fliph | editimage imageoptions'//控制将在上下文工具栏上显示的按钮的确切选择
}
},
setContent (content) {
if (content != "") {
this.myEditor.setContent(content)
}else{
this.myEditor.setContent('')
}
},
getContent () {
return this.myEditor.getContent()
}
}
}
</script>
<style >
.tinymce{
width: 850px!important
}
.tox-tinymce-aux {
z-index: 2006!important;
}
.tox .tox-dialog-wrap__backdrop {
display: none;
}
.tox .tox-dialog {
box-shadow: none;
}
.tox-dialog tox-dialog--width-lg {
z-index: 2006!important;
}
</style>
以上是我踩过的坑 欢迎来踩
项目需要在Vue环境中实现tinymce的多图片批量上传功能,但现有版本为4.9.4。升级过程中遇到问题,包括替换tinymce文件、调整引入路径、主题不兼容、工具栏缺失及弹框层级冲突等。通过替换文件、选择silver主题、添加所需插件和调整弹框样式,最终成功实现功能。

4160

被折叠的 条评论
为什么被折叠?



