Vue-Quill-Editor插件
- 插件安装
下载Vue-Quill-Editor
下载quill(Vue-Quill-Editor需要依赖)
npm install vue-quill-editor --save
npm install quill --save
main.js引入样式文件
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
- 同级目录下新建quill-config.js文件
/*富文本编辑图片上传配置*/
const uploadConfig = {
action: '', // 必填参数 图片上传地址
methods: 'POST', // 必填参数 图片上传方式
token: '', // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
name: 'img', // 必填参数 文件的参数名
size: 500, // 可选参数 图片大小,单位为Kb, 1M = 1024Kb
accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon' // 可选 可上传的图片格式
};
// toolbar工具栏的工具选项(默认展示全部)
const toolOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{'header': 1}, {'header': 2}],
[{'list': 'ordered'}, {'list': 'bullet'}],
[{'script': 'sub'}, {'script': 'super'}],
[{'indent': '-1'}, {'indent': '+1'}],
[{'direction': 'rtl'}],
[{'size': ['small', false, 'large', 'huge']}],
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'color': []}, {'background': []}],
[{'font': []}],
[{'align': []}],
['clean'],
['link', 'image', 'video']
];
const handlers = {
image: function image() {
var self = this;
var fileInput = this.container.querySelector('input.ql-image[type=file]');
if (fileInput === null) {
fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
// 设置图片参数名
if (uploadConfig.name) {
fileInput.setAttribute('name', uploadConfig.name);
}
// 可设置上传图片的格式
fileInput.setAttribute('accept', uploadConfig.accept);
fileInput.classList.add('ql-image');
// 监听选择文件
fileInput.addEventListener('change', function () {
// 创建formData
var formData = new FormData();
formData.append(uploadConfig.name, fileInput.files[0]);
formData.append('object','product');
// 如果需要token且存在token
if (uploadConfig.token) {
formData.append('token', uploadConfig.token)
}
// 图片上传
var xhr = new XMLHttpRequest();
xhr.open(uploadConfig.methods, uploadConfig.action, true);
// 上传数据成功,会触发
xhr.onload = function (e) {
if (xhr.status === 200) {
var res = JSON.parse(xhr.responseText);
let length = self.quill.getSelection(true).index;
//这里很重要,你图片上传成功后,img的src需要在这里添加,res.path就是你服务器返回的图片链接。
self.quill.insertEmbed(length, 'image', res.path);
self.quill.setSelection(length + 1)
}
fileInput.value = ''
};
// 开始上传数据
xhr.upload.onloadstart = function (e) {
fileInput.value = ''
};
// 当发生网络异常的时候会触发,如果上传数据的过程还未结束
xhr.upload.onerror = function (e) {
};
// 上传数据完成(成功或者失败)时会触发
xhr.upload.onloadend = function (e) {
// console.log('上传结束')
};
xhr.send(formData)
});
this.container.appendChild(fileInput);
}
fileInput.click();
}
};
export default {
placeholder: '',
theme: 'snow', // 主题
modules: {
toolbar: {
container: toolOptions, // 工具栏选项
handlers: handlers // 事件重写
}
}
};
实际项目中调用组件
<template>
<div id="Test">
<quill-editor ref="myTextEditor"
v-model="content" :options="quillOption">
</quill-editor>
</div>
</template>
<script>
import { quillEditor } from 'vue-quill-editor'
import quillConfig from './quill-config.js'
export default {
components: {
quillEditor
},
data () {
return {
content: '<h2>hello quill-editor</h2>',
quillOption: quillConfig,
}
}
}
</script>
<style>
</style>
tinymce整合
- 插件安装
安装tinymce
npm install tinymce -S
安装tinymce-vue
npm install @tinymce/tinymce-vue -S
package.json
"@tinymce/tinymce-vue": "^3.2.2",
"tinymce": "^5.4.1",
- tinymce高版本报错
1、 报错 icons.js 报错Unexpected token '<'
import 'tinymce/icons/default/icons' //解决了icons.js 报错Unexpected token '<'
2、@tinymce/tinymce-vue版本过高报错(4.0.0以上版本是vue3.0的)
解决方法将@tinymce/tinymce-vue版本改成3.2.8
warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'defineComponent' was not found in 'vue' warning in
./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'h' was not found in 'vue' warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'onActivated' was not found in 'vue'
warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'onBeforeUnmount' was not found in 'vue' warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'onDeactivated' was not found in 'vue' warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'onMounted' was not found in 'vue'
warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'ref' was not found in 'vue' warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'toRefs' was not found in 'vue' warning in ./node_modules/@tinymce/tinymce-vue/lib/es2015/main/ts/components/Editor.js "export 'watch' was not found in 'vue'
- 下载中文语言包
安装之后,在 node_modules 中找到 tinymce/skins 目录,然后将 skins 目录拷贝到 static 目录下
// 如果是使用 vue-cli 3.x 构建的 typescript 项目,就放到 public 目录下,文中所有 static 目录相关都这样处理
tinymce提供了很多的语言包,这里我们下载中文语言包
地址:https://www.tiny.cloud/get-tiny/language-packages/
下载完后放到静态文件static目录下
1、在static目录下新建tinymce目录
2、在node_modules里面找到tinymce目录,将此目录下skins目录复制到static/tinymce里面
3、将上面下载的语言包解压到该目录,如下图
封装成组件
<template>
<div class="tinymce-editor">
<editor v-model="myValue"
:init="init"
:disabled="disabled"
@onClick="onClick">
</editor>
</div>
</template>
<script>
// import {uploadFile} from '@/api/upload'
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import axios from 'axios'
// import 'tinymce/themes/modern/theme'
import 'tinymce/themes/silver/theme'
import 'tinymce/icons/default/icons'
import 'tinymce/plugins/image'// 插入上传图片插件
import 'tinymce/plugins/media'// 插入视频插件
import 'tinymce/plugins/table'// 插入表格插件
import 'tinymce/plugins/link' //超链接插件
import 'tinymce/plugins/code' //代码块插件
import 'tinymce/plugins/lists'// 列表插件
import 'tinymce/plugins/contextmenu' //右键菜单插件
import 'tinymce/plugins/wordcount' // 字数统计插件
import 'tinymce/plugins/colorpicker' //选择颜色插件
import 'tinymce/plugins/textcolor' //文本颜色插件
import 'tinymce/plugins/fullscreen' //全屏
import 'tinymce/plugins/help' // 帮助
import 'tinymce/plugins/charmap'
import 'tinymce/plugins/paste'
import 'tinymce/plugins/print' //打印
import 'tinymce/plugins/preview' // 预览
import 'tinymce/plugins/hr' // 水平线
import 'tinymce/plugins/anchor'
import 'tinymce/plugins/pagebreak'
import 'tinymce/plugins/spellchecker'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/visualblocks'
import 'tinymce/plugins/visualchars'
import 'tinymce/plugins/insertdatetime'
import 'tinymce/plugins/nonbreaking'
import 'tinymce/plugins/autosave'
import 'tinymce/plugins/fullpage'
export default {
components: {
Editor
},
props: {
//传入一个value,使组件支持v-model绑定
value: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
},
plugins: {
type: [String, Array],
default: 'lists image media table textcolor wordcount contextmenu'
},
toolbar: {
type: [String, Array],
default: 'undo redo | formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat table'
}
},
data() {
return {
//配置文件服务器的静态访问路径前缀
static_web_preurl: 'http://localhost/files/hxzy_img/',
//初始化配置
init: {
language_url: '/static/tinymce/langs/zh_CN.js',
language: 'zh_CN',
skin_url: '/static/tinymce/skins/ui/oxide',
height: 300,
// plugins: this.plugins,
// toolbar: this.toolbar,
plugins: 'link lists image code table colorpicker textcolor wordcount contextmenu',
toolbar:
'bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat | table',
branding: false,
menubar: false,
//此处为图片上传处理函数,这个直接用了base64的图片形式上传图片,
//如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
images_upload_handler: (blobInfo, success, failure) => {
this.handleImgUpload(blobInfo, success, failure)
},
},
myValue: this.value
}
},
mounted() {
tinymce.init({})
},
methods: {
//添加相关的事件,可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
//需要什么事件可以自己增加
onClick(e) {
this.$emit('onClick', e, tinymce)
},
//可以添加一些自己的自定义事件,如清空内容
clear() {
this.myValue = ''
},
handleImgUpload (blobInfo, success, failure) {
let formdata = new FormData()
formdata.set('file', blobInfo.blob())
// uploadFile(formdata).then(response => {
// this.listLoading = false;
// this.list = response.data.records;
// this.total = response.data.total;
// this.totalPage = response.data.pages;
// this.pageSize = response.data.size;
// });
axios.post('/fileapi/FILE/upload/hxzy_img', formdata).then(res => {
console.log(res)
//返回信息必须要保证在浏览器上直接打开图片才能正确显示
var returnUrl=this.static_web_preurl+res.data.filename
success(returnUrl)
}).catch(res => {
failure('error')
})
},
},
watch: {
value(newValue) {
this.myValue = newValue
},
myValue(newValue) {
this.$emit('input', newValue)
}
}
}
</script>
<style scoped>
</style>
组件调用
<template>
<div>
<!-- {{ msg }} -->
<tinymce-editor v-model="msg" :value="msg"
ref="editor"></tinymce-editor>
<button @click="clear">清空内容</button>
<button @click="disabled = true">禁用</button>
</div>
</template>
<script>
import TinymceEditor from '@/components/Tinymce/tinymce-editor'
export default {
components: {
TinymceEditor
},
data() {
return {
msg: 'Welcome to Use Tinymce Editor',
disabled: false
}
},
methods: {
//鼠标单击的事件
onClick(e, editor) {
console.log('Element clicked')
console.log(e)
console.log(editor)
},
//清空内容
clear() {
this.$refs.editor.clear()
}
},
}
</script>
<style scoped>
</style>
配置文件上传
......
vue3配置tinymce