Integrate Medium editor in angular 8

Integrate Medium editor in angular 8


Integrate Medium editor in angular 8

Let's Step by step Integrate Medium Editor in angular 8

Here is the Demo of what we will make

An image can express in better way

In this article, we will try to create a small editor like a medium

Step 1 :

so first thing first create a project in angular

and you can use bootstrap to make it pretty good just add CDN links under the

ng g new mediumeditor

This command will generate an angular starter application once it has completed
installing the dependencies and all

Step 2:

Install an npm package Mentioned below

npm install medium-editor --save

Include the css and js in angular.json file

"styles": [
            "scripts": ["node_modules/medium-editor/dist/js/medium-editor.min.js"]

Step 3:

create a component as per your choice name ill create one with name create

ng g c create

Step 4 :

go to newly created component.html and make a div and give it a template
reference of name lets say the editable

<div class="container mt-3 ">
    <div class="row justify-content-center">
 class=' col-8' 
                    style="min-height: 85vh">   


Just wrapped it under some bootstrap classes to give basic stylings but the main
thing is div with a template reference editable

Step 4 :

Go to your component class

make a variable editor and a view child property name editable as bellow

  }) editable:ElementRef;

Step 5 :

now we will be using one lifecycle hook of angular that is ngAfterViewInit

if you want to know more about it you can read here

ngAfterViewInit(): void {
    this.editor = new MediumEditor(this.editable.nativeElement)

and this may give you an error like media editor is not defined in that case we
may need to declare it at the top like

declare var MediumEditor:any;

And with these changes, you have almost created a small-medium editor for your
self give it a shot and you should see a cursor on your screen

and you can write over their just select the text you have written and you see
the magic

Step 6:

now let's say you need more options in your editor toolbar for that we have to
pass a configuration object in the MediumEditor Constructor so, I have created a
default one

ngAfterViewInit(): void {
    this.editor = new MediumEditor(this.editable.nativeElement,{

and with changes, you will have lots of option available

Step 7:

Now that you have an editor how to get data from it like if someone writes a
post then we should get HTML of that post write

So what we will do is we will divide our Screen into two halves in one half we
will have the editor and in the other half we will show the preview of the post

we will modify our create.component.html a bit

<div class="container mt-3 ">
    <div class="row justify-content-center">
            <div #editable class=' col-6' style="min-height: 85vh"></div>
            <div *ngIf="editor"  class="shadow col-6" 


note in the second div we are assigning the value of inner HTML as

the editor is basically the medium editor if you remember in ngafterviewinit we
are storing the result of new MediumEditor(..) in the editor variable and we are
using the same above

So that it play with it and create something amazing

So the final code will be


<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <base href="/">
  <link rel="stylesheet" href="
" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">


<div class="container mt-3 ">
    <div class="row justify-content-center">
            <div #editable class=' col-6' style="min-height: 85vh"></div>
            <div *ngIf="editor"  class="shadow col-6" [innerHTML]="editor.elements[0].innerHTML"></div>



import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '
declare var MediumEditor:any;
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.scss']
export class CreateComponent implements OnInit,AfterViewInit {
  }) editable:ElementRef;
  ngAfterViewInit(): void {
    this.editor = new MediumEditor(this.editable.nativeElement,{
      paste: {
        /* This example includes the default options for paste,
           if nothing is passed this is what it used */
        forcePlainText: false,
        cleanPastedHTML: true,
        cleanReplacements: [],
        cleanAttrs: ['class', 'style', 'dir','name'],
        cleanTags: ['meta'],
        unwrapTags: []
      toolbar: {
          /* These are the default options for the toolbar,
             if nothing is passed this is what is used */
          allowMultiParagraphSelection: true,
          buttons: BUTTONS,
          diffLeft: 0,
          diffTop: -10,
          firstButtonClass: 'medium-editor-button-first',
          lastButtonClass: 'medium-editor-button-last',
          relativeContainer: null,
          standardizeSelectionStart: false,
          static: false,
          /* options which only apply when static is true */
          align: 'center',
          sticky: false,
          updateOnEmptySelection: false

constructor() { }

ngOnInit() {


const BUTTONS = [



Thanks a lot

Visit Smartcodehub For more angular tutorials