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

Several methods to execute sql files under mysql command line

Table of contents The first method: When the MySQ...

Start a local Kubernetes environment using kind and Docker

introduce Have you ever spent a whole day trying ...

Implementing file content deduplication and intersection and difference in Linux

1. Data Deduplication In daily work, there may be...

Why is the scroll bar on the web page set on the right?

Why are the scroll bars of the browsers and word ...

Vue achieves seamless carousel effect (marquee)

This article example shares the specific code of ...

Use and understanding of MySQL triggers

Table of contents 1. What is a trigger? 2. Create...

Detailed explanation of count(), group by, order by in MySQL

I recently encountered a problem when doing IM, a...

B2C website user experience detail design reference

Recently, when using Apple.com/Ebay.com/Amazon.co...

React Native scaffolding basic usage detailed explanation

Build the project Execute the command line in the...