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

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...

Display special symbols in HTML (with special character correspondence table)

Problem Reproduction When using HTML for editing,...

Solve the problem of blank gap at the bottom of Img picture

When working on a recent project, I found that th...

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

How to import js configuration file on Vue server

Table of contents background accomplish Supplemen...

XHTML no longer uses some obsolete elements in HTML

When we do CSS web page layout, we all know that i...

Provides helpful suggestions for improving website design

<br />Scientifically Design Your Website: 23...

Detailed explanation of how to find the location of the nginx configuration file

How can you find the location of the configuratio...

Use shell script to install python3.8 environment in CentOS7 (recommended)

One-click execution To install Python 3.8 in a vi...

Common solutions for Mysql read-write separation expiration

The pitfalls of MySQL read-write separation The m...

Usage and demonstration of ref in Vue

ref definition: used to register reference inform...

Causes and solutions for MySQL master-slave synchronization delay

For historical reasons, MySQL replication is base...