富文本编辑器(图片拖拽缩放)

需求:

根据业务要求,需要能够上传图片,且上传的图片能在移动端中占满屏幕宽度,故需要能等比缩放上传的图片,还需要能拖拽缩放改变图片大小。尝试多个第三方富文本编辑器,很难找到一个完美符合自己要求的编辑器。经过多次尝试,最终选择了wangEditor富文本编辑器

最初使用的是vue2Editor富文本编辑器vue2Editor本身是不支持图片拖拽的,但是提供了可配置图片拖拽的方法,需要借助Qill.js来实现图片拖拽。虽然满足了业务需求,但是在移动端展示的效果不是很理想。

此次编辑器主要是上传的富文本需要在移动端进行展示,为了达到理想效果,需要能修改图片百分比,当设置img标签width属性100%时,就可以满足需求。最近发新版本(第四版 v4)的wangEditor可以满足需求,最终选择了它用于实际开发中。

效果图:

代码案例及相关配置如下:

  • 安装插件
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 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富文本编辑器(图片拖拽缩放)的更多相关文章

  1. vue+富文本编辑器UEditor

    vue+富文本编辑器UEditor 昨天的需求是把textarea换成富文本编辑器的形式, 网上找了几种富文本编辑器ueditor.tinymce等, 觉得ueditor实现双向绑定还挺有意思, 分享 ...

  2. vue富文本编辑器vue-quill-editor使用总结(包含图片上传,拖拽,放大和缩小)

    vue-quill-editor是vue很好的富文本编辑器,富文本的功能基本上都支持,样式是黑白色,简洁大方. 第一步下载 vue-quill-editor: npm i vue-quill-edit ...

  3. vue 富文本编辑器 项目实战用法

    1.挑个富文本编辑器 首先针对自己项目的类型,确定自己要用啥编辑器. 1.1 wangeditor 如果一般类似博客这种项目不需要花里胡哨的,功能也不要求贼多的,推荐一下wangeditor(点击跳转 ...

  4. react-quill 富文本编辑器 ---- 图片处理

    import React,{Component} from 'react'; import ReactQuill,{ Quill } from 'react-quill'; import 'react ...

  5. 图片拖拽缩放功能:兼容Chrome、Firefox、IE8+

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. vue富文本编辑器vue-quill-editor

    1.下载Vue-Quill-Editor npm install vue-quill-editor --save 2.下载quill(Vue-Quill-Editor需要依赖) npm install ...

  7. vue富文本编辑器

    基于webpack和vue 一.npm 安装 vue-quill-editor 二.在main.js中引入 import VueQuillEditor from 'vue-quill-editor'/ ...

  8. Kindeditor JS 富文本编辑器图片上传指定路径

    js //================== KindEditor.ready(function (K) { var hotelid = $("#hotelid").val(); ...

  9. vue富文本编辑器TinyMec才是最好用的

    最近在做一个后台管理系统,系统中需要一个编辑器,没多想,百度查之,找了好些.如下: UEditor CKEditor 4 Vue-html5-editor wangeditor quill .... ...

随机推荐

  1. 关于java基础_数组的学习

    数组的学习 1.数组的概念?作用是什么? 系统中存储多个值, 2.数组的定义? 数据类型[] 数组名; 3.定义好数组以后需要对其进行初始化 数组初始化有两种: 第一种动态初始化,指定数组的长度,长度 ...

  2. Linux常用命令详解(3)

    pidofpstopipuptimewgetcurltrddtargrepfind 命令详解 1.pidof 获取正在运行程序的PID 实例1: [root@ken ~]# pidof sshd 24 ...

  3. selenium的文档API

    你用WebDriver要做的第一件事就是指定一个链接,一般我们使用get方法: from selenium import webdriver from selenium.webdriver.commo ...

  4. 优酷kux转mp4

    利用YouKu客户端下载的视频格式为kux,只能通过YouKu客户端播放,很不方便.在网上看到有人通过ffmpeg解码器进行转换,写成了批处理,如下: @echo off setlocal enabl ...

  5. 基于redis的分布式锁的实现与框架解决方案

    利用切面实现redis分布式锁:https://www.cnblogs.com/xiaoxiongcanguan/p/10718202.html 细节分析redis实现分布式锁的前因后果:https: ...

  6. ISCC2018 writeup(web)

    比较数字大小 F12 修改maxlength为4 web01 strcmp()函数遇到数组会返回NULL 而PHP是弱类型语言  在==比较的时候,如果有数值的话会先将字符串转换为数值在进行比较,而N ...

  7. 转载:python argparse用法总结

    https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog.py是 ...

  8. 基础篇:详解JAVA对象实例化过程

    目录 1 对象的实例化过程 2 类的加载过程 3 触发类加载的条件 4 对象的实例化过程 5 类加载器和双亲委派规则,如何打破双亲委派规则 欢迎指正文中错误 关注公众号,一起交流 参考文章 1 对象的 ...

  9. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  10. USB虚拟串口 使用基于stm32的RT-Thread

    参考我的RT Thread论坛文章 https://www.rt-thread.org/qa/thread-422644-1-1.html