name="file"
:headers="header"
:show-file-list="false"
:on-success="uploadSuccess"
:on-error="uploadError"
:before-upload="beforeUpload"
>
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@change="onEditorChange($event)"
>
const toolbarOptions = [
["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"] // 链接、图片、视频
];
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css"; // 富文本核心样式
import "quill/dist/quill.snow.css"; // 富文本雪地主题样式
import "quill/dist/quill.bubble.css"; // 富文本气泡主题样式
export default {
props: {
/* 编辑器的内容 */
value: {
type: String
},
action: {
type: String
},
/* 图片大小限制 */
maxSize: {
type: Number,
default: 4000 // kb
}
},
components: {
quillEditor
},
data() {
return {
content: this.value, // 当前编辑器内容
quillUpdateImg: false, // 是否显示加载动画
editorOption: {
placeholder: "", // 占位符
theme: "snow", // 主题
modules: {
toolbar: {
container: toolbarOptions, // 工具栏配置
handlers: {
image: function(value) {
if (value) {
// 触发图片上传
document.querySelector(".avatar-uploader input").click();
} else {
this.quill.format("image", false);
}
}
// link: function(value) {
// if (value) {
// var href = prompt('请输入url');
// this.quill.format("link", href);
// } else {
// this.quill.format("link", false);
// }
// },
}
}
}
},
header: {
'Token': this.$storage.get("Token") // 请求头中的 Token
}
};
},
computed: {
// 动态计算上传接口 URL
getActionUrl: function() {
return `/${this.$base.name}/` + this.action;
}
},
methods: {
onEditorBlur() {
// 失去焦点事件
},
onEditorFocus() {
// 获得焦点事件
},
onEditorChange() {
console.log(this.value);
// 内容改变事件
this.$emit("input", this.value);
},
// 富文本图片上传前
beforeUpload(file) {
// 显示加载动画
this.quillUpdateImg = true;
const isLtMaxSize = file.size / 1024 < this.maxSize; // 检查文件大小是否超过限制
if (!isLtMaxSize) {
this.$message.error(`上传图片大小不能超过 ${this.maxSize} KB!`);
}
return isLtMaxSize;
},
uploadSuccess(res, file) {
// 上传成功回调
let quill = this.$refs.myQuillEditor.quill; // 获取富文本实例
if (res.code === 0) {
let length = quill.getSelection().index; // 获取光标位置
quill.insertEmbed(length, "image", this.$base.url + "upload/" + res.file); // 插入图片
quill.setSelection(length + 1); // 移动光标
} else {
this.$message.error("图片插入失败");
}
this.quillUpdateImg = false; // 隐藏加载动画
},
uploadError() {
// 上传失败回调
this.quillUpdateImg = false; // 隐藏加载动画
this.$message.error("图片插入失败");
}
}
};
/* 样式部分 */