记录下:vue使用wangEditor封装了个富文本编辑器编辑时光标老乱飘
解决方案:
光标乱飘可以在watch监听文本内容实时更新时,在输入文本的时候让监听无效。
data() {
return {
editor: null,
editorContent: null,
isChange: false,// 加个判断值
};
},
watch: {
// isChange 监听实时更新,判断输入文本时让监听无效
value(val) {
if (!this.isChange) {
this.editorContent = val;
this.editor.txt.html(val);
}
this.isChange = false;
},
initEditor() {
this.editor = new WEditor(this.$refs.editor);
this.editor.config.menus = [
'head',
'bold',
'fontSize',
'fontName',
'italic',
'strikeThrough',
'justify',
'quote',
'undo',
'redo',
];
this.editor.config.customUploadImg = (resultFiles, insertImgFn) => {
const formData = new FormData();
resultFiles.forEach((item) => {
formData.append('file', item);
});
uploadImage(formData).then((data) => {
console.log(data);
insertImgFn(data);
});
};
this.editor.config.onchange = (html) => {
// 这儿得赋值让离开编辑后状态又生效
this.isChange = true;
this.editorContent = html;
this.$emit('change', this.editorContent);
};
this.editor.config.zIndex = 2;
this.editor.create();
},