Vue integrates a rich text editor that supports image zooming and dragging

Vue integrates a rich text editor that supports image zooming and dragging

need:

According to business requirements, it is necessary to be able to upload pictures, and the uploaded pictures must occupy the full screen width on the mobile terminal, so it is necessary to be able to proportionally scale the uploaded pictures, and also to be able to drag, scale, and change the size of the pictures. After trying multiple third-party rich text editors, it is difficult to find an editor that perfectly meets your requirements. After many attempts, I finally chose the wangEditor rich text editor. Initially, the vue2Editor rich text editor was used. vue2Editor itself does not support image dragging, but it provides a configurable image dragging method, which requires the use of Quill.js to implement image dragging. Although it meets business needs, the display effect on mobile devices is not ideal. The main purpose of this editor is to upload rich text that needs to be displayed on the mobile terminal. In order to achieve the ideal effect, it is necessary to be able to modify the image percentage. When the width attribute of the img tag is set to 100%, the requirement can be met. The recently released new version (fourth edition v4) of wangEditor can meet the needs, and it was finally chosen for actual development.

Effect picture:

The code examples and related configurations are as follows:

Install the plugin

npm i wangeditor --save
// or
yarn add wangeditor

Editor Configuration

<template>
	<div class="w_editor">
		<!-- Rich text editor -->
		<div id="w_view"></div>
	</div>
</template>

<script>
// Import rich text import WE from "wangeditor";
//Introduce the elementUI Message module (for prompt information)
import { Message } from "element-ui";

export default {
	name: "WEditor",
	props: {
		// default value defaultText: { type: String, default: "" },
		// Rich text updated value richText: { type: String, default: "" }
	},
	data() {
		return {
			// Editor instance editor: null,
			// Rich text menu option configuration menuItem: [
				"head",
				"bold",
				"fontSize",
				"fontName",
				"italic",
				"underline",
				"indent",
				"lineHeight",
				"foreColor",
				"backColor",
				"link",
				"list",
				"justify",
				"image",
				"video"
			]
		};
	},
	watch:
		// Listen for default values ​​defaultText(nv, ov) {
			if (nv != "") {
				this.editor.txt.html(nv);
				this.$emit("update:rich-text", nv);
			}
		}
	},
	mounted() {
		this.initEditor();
	},
	methods: {
		// Initialize the editor initEditor() {
			// Get the editor dom node const editor = new WE("#w_view");
			//Configure the editor editor.config.showLinkImg = false; /* Hide the function of inserting network pictures*/
			editor.config.onchangeTimeout = 400; /* Configure the time frequency of triggering onchange, the default is 200ms */
			editor.config.uploadImgMaxLength = 1; /* Limit the maximum number of images that can be uploaded at one time*/
			// editor.config.showFullScreen = false; /* Configure whether to display the full screen function button*/
			editor.config.menus = [...this.menuItem]; /* Custom system menu */
			// editor.config.uploadImgMaxSize = 5 * 1024 * 1024 /* Limit the image size */;
			editor.config.customAlert = err => {
				Message.error(err);
			};
			// Monitor changes and update data synchronously editor.config.onchange = newHtml => {
				// Asynchronously update the changes of component rich text value this.$emit("update:rich-text", newHtml);
			};
			// Custom upload image editor.config.customUploadImg = (resultFiles, insertImgFn) => {
				/**
				 * resultFiles: image upload file stream * insertImgFn: insert image into rich text * The returned result is the generated image URL address * */
				// This method is rewritten by Fengzhuan for the Alibaba Cloud Image OSS Direct Transfer Plugin.
				this.$oss(resultFiles[0], resultFiles[0]["name"]).then(url => {
					!!url && insertImgFn(url); /* OSS image upload, insert the image into the editor*/
				});
			};
			// Create an editor editor.create();
			this.editor = editor;
		}
	},
	beforeDestroy() {
		// Destroy the editor this.editor.destroy();
		this.editor = null;
	}
};
</script>

Note: For specific parameter configuration, please refer to the editor documentation instructions.

Use the extracted editor in the component:

<template>
	<div class="editor">
		<el-card shadow="never">
			<div slot="header" class="clearfix">
				<span>Rich Text Editor</span>
			</div>
			<div class="card_center">
				<WEditor :defaultText="defaultText" :richText.sync="richText" />
			</div>
		</el-card>
	</div>
</template>

<script>
// Import the packaged editor import WEditor from "@/components/WEditor";

export default {
	name: "Editor",
	components: { WEditor },
	data() {
		return {
			// Default value defaultText: "",
			// Rich text updated value richText: ""
		};
	},
	created() {
		// Wait for the component to load and assign value 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>

The above is the details of Vue integrating a rich text editor that supports image zooming and dragging. For more information about Vue rich text editor, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue implements image preview effect example (zoom in, zoom out, drag)
  • Vue implements drag and drop or click to upload pictures
  • Vue implements image drag and drop function
  • Vue implements the problem of image scaling
  • Use vue components to realize the drag and zoom function of pictures

<<:  Introduction to Linux system swap space

>>:  Record a slow query event caused by a misjudgment of the online MySQL optimizer

Recommend

Vue realizes picture switching effect

This article example shares the specific code of ...

Detailed example of using typescript to encapsulate axios in Vue3

This axios package is used in the vue3 demo. For ...

How to show or hide common icons on the desktop in Windows Server 2012

Windows Server 2012 and Windows Server 2008 diffe...

js to implement the snake game with comments

This article example shares the specific code of ...

SQL Aggregation, Grouping, and Sorting

Table of contents 1. Aggregate Query 1. COUNT fun...

HTML uses the title attribute to display text when the mouse hovers

Copy code The code is as follows: <a href=# ti...

How to remotely connect to MySQL database with Navicat Premium

The party that creates a new connection is equiva...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...

WeChat applet realizes the nine-square grid effect

This article shares the specific code for the WeC...

Method of building redis cluster based on docker

Download the redis image docker pull yyyyttttwwww...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...