How to encapsulate WangEditor rich text component in Angular

How to encapsulate WangEditor rich text component in Angular

The rich text component is a very commonly used component in web programs, especially when developing a website backend such as a blog or forum.

Thanks to the power of Angular, encapsulating WangEditor components is very simple

1. Install wangeditor using yarn or npm

yarn add wangeditor

2. Create an Angular component

ng gc q-wang-editor

3. Encapsulate component logic

3.1 Template

<div #wang></div>

3.2 ts logic

import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';

import E from "wangeditor"
import hljs from 'highlight.js'
import "node_modules/highlight.js/styles/xcode.css"

@Component({
  selector: 'q-wang-editor',
  templateUrl: './q-wang-editor.component.html',
  styleUrls: [
    './q-wang-editor.component.less',
    '../../../../../node_modules/highlight.js/styles/xcode.css'],
  encapsulation: ViewEncapsulation.None,
})
export class QWangEditorComponent implements OnInit, ControlValueAccessor,OnDestroy {

  @ViewChild("wang")
  editor!: ElementRef;

  @Input() value: string = '';

  @Input() height = 300;

  @Output() valueChange = new EventEmitter();

  onChange: ((value: string) => {}) | undefined;

  html = ''

  wangEditor: E | undefined;

  constructor() { }
  ngOnDestroy(): void {
    this.wangEditor?.destroy();
  }
  writeValue(obj: any): void {
    this.html = obj;
    this.wangEditor?.txt.html(this.html)
  }
  registerOnChange(fn: any): void {
  }
  registerOnTouched(fn: any): void {
  }

  ngOnInit(): void {
    setTimeout(() => {
      this.wangEditor = new E(this.editor.nativeElement)
      this.wangEditor.config.zIndex = 500;
      this.wangEditor.config.height = this.height
      this.wangEditor.highlight = hljs;
      this.wangEditor.config.onchange = (html: any) => {
        this.valueChange.emit(html)
        if (this.onChange) {
          this.onChange(html);
        }
      }
      this.wangEditor.config.onchangeTimeout = 500;
      this.wangEditor.create();
      this.wangEditor.txt.html(this.html)
    }, 200);
  }

}

The general idea:

  • Use ViewChild to reference the DOM element of HTML
  • After the success of OnInit, initialize the WangEditor editor, put the ElementRef in the template into the container of WangEditor, and let WangEditor control the DOM operation of the interface.
  • Implement ControlValueAccessor to enable this component to support Angular form validation.
  • Implement ngOnDestroy. When the component is destroyed, call WangEditor's destroy

4. Use components

<q-wang-editor [height]="550"></q-wang-editor>

5. Effect preview

6. Last

The Angular component encapsulation of WangEditor is basically completed. If you need more functions, such as picture uploading, you can add functions according to your needs.

This is the end of this article about Angular encapsulation of WangEditor rich text component. For more relevant Angular WangEditor rich text component content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of two methods to implement two-way binding of custom components in Angular
  • Angular5 child component monitors the change of the value passed in by the parent component
  • How to use Angular cli to generate custom files and components
  • Example of regularly refreshing and clearing timers in angular2 components
  • Angular6 writes a simple Select component example

<<:  Detailed tutorial on installing Python 3.8.1 on Linux

>>:  Eight common SQL usage examples in MySQL

Recommend

Build nginx virtual host based on domain name, port and IP

There are three types of virtual hosts supported ...

How to solve the DOS window garbled problem in MySQL

The garbled code problem is as follows: The reaso...

Detailed explanation of pid and socket in MySQL

Table of contents 1. Introduction to pid-file 2.S...

Even a novice can understand the difference between typeof and instanceof in js

Table of contents 1. typeof 2. instanceof 3. Diff...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

Summary of MySQL log related knowledge

Table of contents SQL execution order bin log Wha...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

How to build a MySQL high-availability and high-performance cluster

Table of contents What is MySQL NDB Cluster Preli...

Detailed explanation of the use cases of Vue listeners

The first one is to use jQuery's ajax to send...

How to visualize sketched charts in Vue.js using RoughViz

introduce A chart is a graphical representation o...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...