使用vue怎么实现一个支持图片缩放拖拽的富文本编辑器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
npm i wangeditor --save
// or
yarn add wangeditor
编辑器配置
<template>
<div class="w_editor">
<!-- 富文本编辑器 -->
<div id="w_view"></div>
</div>
</template>
<script>
// 引入富文本
import WE from "wangeditor";
// 引入elementUI Message模块(用于提示信息)
import { Message } from "element-ui";
export default {
name: "WEditor",
props: {
// 默认值
defaultText: { type: String, default: "" },
// 富文本更新的值
richText: { type: String, default: "" }
},
data() {
return {
// 编辑器实例
editor: null,
// 富文本菜单选项配置
menuItem: [
"head",
"bold",
"fontSize",
"fontName",
"italic",
"underline",
"indent",
"lineHeight",
"foreColor",
"backColor",
"link",
"list",
"justify",
"image",
"video"
]
};
},
watch: {
// 监听默认值
defaultText(nv, ov) {
if (nv != "") {
this.editor.txt.html(nv);
this.$emit("update:rich-text", nv);
}
}
},
mounted() {
this.initEditor();
},
methods: {
// 初始化编辑器
initEditor() {
// 获取编辑器dom节点
const editor = new WE("#w_view");
// 配置编辑器
editor.config.showLinkImg = false; /* 隐藏插入网络图片的功能 */
editor.config.onchangeTimeout = 400; /* 配置触发 onchange 的时间频率,默认为 200ms */
editor.config.uploadImgMaxLength = 1; /* 限制一次最多能传几张图片 */
// editor.config.showFullScreen = false; /* 配置全屏功能按钮是否展示 */
editor.config.menus = [...this.menuItem]; /* 自定义系统菜单 */
// editor.config.uploadImgMaxSize = 5 * 1024 * 1024 /* 限制图片大小 */;
editor.config.customAlert = err => {
Message.error(err);
};
// 监控变化,同步更新数据
editor.config.onchange = newHtml => {
// 异步更新组件富文本值的变化
this.$emit("update:rich-text", newHtml);
};
// 自定义上传图片
editor.config.customUploadImg = (resultFiles, insertImgFn) => {
/**
* resultFiles:图片上传文件流
* insertImgFn:插入图片到富文本
* 返回结果为生成的图片URL地址
* */
// 此方法为自己封赚改写的阿里云图片OSS直传插件。
this.$oss(resultFiles[0], resultFiles[0]["name"]).then(url => {
!!url && insertImgFn(url); /* oss图片上传,将图片插入到编辑器中 */
});
};
// 创建编辑器
editor.create();
this.editor = editor;
}
},
beforeDestroy() {
// 销毁编辑器
this.editor.destroy();
this.editor = null;
}
};
</script>
注: 具体参数配置请参考编辑器文档使用说明。
<template>
<div class="editor">
<el-card shadow="never">
<div slot="header" class="clearfix">
<span>富文本编辑器</span>
</div>
<div class="card_center">
<WEditor :defaultText="defaultText" :richText.sync="richText" />
</div>
</el-card>
</div>
</template>
<script>
// 引入封装好的编辑器
import WEditor from "@/components/WEditor";
export default {
name: "Editor",
components: { WEditor },
data() {
return {
// 默认值
defaultText: "",
// 富文本更新的值
richText: ""
};
},
created() {
// 等待组件加载完毕赋值
this.$nextTick(() => {
this.defaultText = `<p ><img src="https://tr-mba.oss-cn-beijing.aliyuncs.com/picture/202010/20_222430_8011.png" width="30%" ></p>`;
});
}
};
</script>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。