vue集成一個(gè)支持圖片縮放拖拽的富文本編輯器
需求:
根據(jù)業(yè)務(wù)要求,需要能夠上傳圖片,且上傳的圖片能在移動(dòng)端中占滿屏幕寬度,故需要能等比縮放上傳的圖片,還需要能拖拽、縮放、改變圖片大小。嘗試多個(gè)第三方富文本編輯器,很難找到一個(gè)完美符合自己要求的編輯器。經(jīng)過(guò)多次嘗試,最終選擇了wangEditor富文本編輯器。 最初使用的是vue2Editor富文本編輯器,vue2Editor本身是不支持圖片拖拽的,但是提供了可配置圖片拖拽的方法,需要借助Quill.js來(lái)實(shí)現(xiàn)圖片拖拽。雖然滿足了業(yè)務(wù)需求,但是在移動(dòng)端展示的效果不是很理想。 此次編輯器主要是上傳的富文本需要在移動(dòng)端進(jìn)行展示,為了達(dá)到理想效果,需要能修改圖片百分比,當(dāng)設(shè)置img標(biāo)簽的width屬性為100% 時(shí),就可以滿足需求。最近發(fā)新版本(第四版 v4)的wangEditor可以滿足需求,最終選擇了它用于實(shí)際開(kāi)發(fā)中。
效果圖:

代碼案例及相關(guān)配置如下:
安裝插件
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: {
// 默認(rèn)值
defaultText: { type: String, default: "" },
// 富文本更新的值
richText: { type: String, default: "" }
},
data() {
return {
// 編輯器實(shí)例
editor: null,
// 富文本菜單選項(xiàng)配置
menuItem: [
"head",
"bold",
"fontSize",
"fontName",
"italic",
"underline",
"indent",
"lineHeight",
"foreColor",
"backColor",
"link",
"list",
"justify",
"image",
"video"
]
};
},
watch: {
// 監(jiān)聽(tīng)默認(rèn)值
defaultText(nv, ov) {
if (nv != "") {
this.editor.txt.html(nv);
this.$emit("update:rich-text", nv);
}
}
},
mounted() {
this.initEditor();
},
methods: {
// 初始化編輯器
initEditor() {
// 獲取編輯器dom節(jié)點(diǎn)
const editor = new WE("#w_view");
// 配置編輯器
editor.config.showLinkImg = false; /* 隱藏插入網(wǎng)絡(luò)圖片的功能 */
editor.config.onchangeTimeout = 400; /* 配置觸發(fā) onchange 的時(shí)間頻率,默認(rèn)為 200ms */
editor.config.uploadImgMaxLength = 1; /* 限制一次最多能傳幾張圖片 */
// editor.config.showFullScreen = false; /* 配置全屏功能按鈕是否展示 */
editor.config.menus = [...this.menuItem]; /* 自定義系統(tǒng)菜單 */
// editor.config.uploadImgMaxSize = 5 * 1024 * 1024 /* 限制圖片大小 */;
editor.config.customAlert = err => {
Message.error(err);
};
// 監(jiān)控變化,同步更新數(shù)據(jù)
editor.config.onchange = newHtml => {
// 異步更新組件富文本值的變化
this.$emit("update:rich-text", newHtml);
};
// 自定義上傳圖片
editor.config.customUploadImg = (resultFiles, insertImgFn) => {
/**
* resultFiles:圖片上傳文件流
* insertImgFn:插入圖片到富文本
* 返回結(jié)果為生成的圖片URL地址
* */
// 此方法為自己封賺改寫(xiě)的阿里云圖片OSS直傳插件。
this.$oss(resultFiles[0], resultFiles[0]["name"]).then(url => {
!!url && insertImgFn(url); /* oss圖片上傳,將圖片插入到編輯器中 */
});
};
// 創(chuàng)建編輯器
editor.create();
this.editor = editor;
}
},
beforeDestroy() {
// 銷(xiāo)毀編輯器
this.editor.destroy();
this.editor = null;
}
};
</script>
注: 具體參數(shù)配置請(qǐng)參考編輯器文檔使用說(shuō)明。
組件中使用抽離的編輯器:
<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 {
// 默認(rèn)值
defaultText: "",
// 富文本更新的值
richText: ""
};
},
created() {
// 等待組件加載完畢賦值
this.$nextTick(() => {
this.defaultText = `<p style="text-align: center; "><img src="https://tr-mba.oss-cn-beijing.aliyuncs.com/picture/202010/20_222430_8011.png" width="30%" style="text-align: center; max-width: 100%;"></p>`;
});
}
};
</script>
以上就是vue集成一個(gè)支持圖片縮放拖拽的富文本編輯器的詳細(xì)內(nèi)容,更多關(guān)于vue 富文本編輯器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決ant-design-vue中menu菜單無(wú)法默認(rèn)展開(kāi)的問(wèn)題
這篇文章主要介紹了解決ant-design-vue中menu菜單無(wú)法默認(rèn)展開(kāi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Vue必學(xué)知識(shí)點(diǎn)之forEach()的使用
在前端開(kāi)發(fā)中,經(jīng)常會(huì)遇到一些通過(guò)遍歷循環(huán)來(lái)獲取想要的內(nèi)容的情形,這時(shí)候就需要用到forEach()了,下面這篇文章主要給大家介紹了關(guān)于Vue必學(xué)知識(shí)點(diǎn)之forEach()使用的相關(guān)資料,需要的朋友可以參考下2021-05-05
Vue.js 實(shí)現(xiàn)微信公眾號(hào)菜單編輯器功能(一)
最近vue.js 非?;馃?,小編也趁機(jī)學(xué)習(xí)了下vuejs的一些基礎(chǔ)知識(shí),于是嘗試做一個(gè)像微信平臺(tái)里的菜單編輯器功能,下面腳本之家小編把vue.js 微信公眾號(hào)菜單編輯器功能的實(shí)現(xiàn)代碼分享給大家,需要的朋友參考下2018-05-05
利用HBuilder打包前端開(kāi)發(fā)webapp為apk的方法
下面小編就為大家?guī)?lái)一篇利用HBuilder打包前端開(kāi)發(fā)webapp為apk的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
vue3 reactive函數(shù)用法實(shí)戰(zhàn)教程
reactive是Vue3中提供實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的方法,reactive的用法與ref的用法相似,也是將數(shù)據(jù)變成響應(yīng)式數(shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)UI也會(huì)自動(dòng)更新,這篇文章主要介紹了vue3 reactive函數(shù)用法,需要的朋友可以參考下2022-11-11

