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

vue front-end HbuliderEslint real-time verification automatic repair settings

Table of contents ESLint plugin installation in H...

Implementing shopping cart function based on vuex

This article example shares the specific code of ...

Avoiding Problems Caused by Closures in JavaScript

About let to avoid problems caused by closure Use...

Parsing MySQL binlog

Table of contents 1. Introduction to binlog 2. Bi...

Specific use of Linux dirname command

01. Command Overview dirname - strip non-director...

MYSQL Left Join optimization (10 seconds to 20 milliseconds)

Table of contents 【Function Background】 [Raw SQL]...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

Native JS realizes compound motion of various motions

This article shares with you a compound motion im...

Some summary of html to pdf conversion cases (multiple pictures recommended)

Due to work requirements, I recently spent some t...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

Detailed explanation of the mechanism and implementation of accept lock in Nginx

Preface nginx uses a multi-process model. When a ...

Some slightly more complex usage example codes in mysql

Preface I believe that the syntax of MySQL is not...

How to simply configure multiple servers in nginx

1: I won’t go into the details of how to install ...

Analysis of the principle of Mybatis mapper dynamic proxy

Preface Before we start explaining the principle ...