Home / Articles / Blog

8 steps to create your own writing editor like Notion, Editor JS
EditorjsWritingnpmFrontendWriting Editor

8 steps to create your own writing editor like Notion, Editor JS

Getting started with EditorJS

FrontendShrey Vijayvargiya

Section 1

Under the Hood

The first story of 2022, the idea begins when we were developing the story editor for our website iHateReading. Our idea is to provide the interface to read and share the tech stories within our local website. So we end up using this package called editorjs.

Please note that Editor js is not the only editor or WYSIWYG, draft js and slate js accord the same services. I found editorjs a bit easier to handle and minimalistic. In today’s story, we will get started with basic functionality to advance on with editorjs in React project.

Section 2

Getting Started

Editor js is a blog styled editor and an npm module, which provides a constructor to create the editor instance and our job is to connect this instance to the required element.

Let's begin with installing the required packages. Editor js provides tones of other plugins or modules to embed the image, videos, embeds, links and so on. For every tool or plugin to work with you need to install it manually and add the configuration in the editorjs constructor.

text
yarn add @editorjs/editorjs

Section 3

Creating constructor

Once the module is installed, we are ready to create the editorjs instance using the constructor. The editorjs constructor required certain imperative parameters otherwise it will not render the tool or editor to work around.

Treat these parameters as props and required props if not provided then the editor component will throw an error or will not render.

If you are implementing editor js in the next js project or with a server-side rendering application then you need to pin a hack to import the editor instance dynamically instead of manually.

text
const editor = EditorJS({   holder: 'editorjs'});

Once the constructor is created we need an instance to find the id similar to the value of the holder.

text
<div id="editorjs" />

Section 4

Rendering Editor

Once the constructor is created we need an id to attach the instance. The id passed to the div element will render the editor component.

text
const EditorComponent = () => {
 const editor = EditorJS({
   holder: 'editorjs'
  });
 
  return (
    <div>
      <div id="editorjs" />
    </div>
  );
};
export default EditorComponent;

The div element will simply render the Editor component.

Section 5

Adding Plugins

Editor js by default provides the support for paragraphs only. To add images, videos, links, embeds they divide the editor into sections called plugins.

Each feature or support is called a plugin, so to add an image in the editor you need an image plugin and provide the configuration for image plugins within the editor constructor.

Define the plugins configuration in the tools key.

text
import EditorJS from '@editorjs/editorjs';
import Header from '@editorjs/header';
const EditorComponent = () => {
 
 const editor = EditorJS({
   holder: 'editorjs',
   tools: {
     heading: {
       class: Header,
       shortcut: 'CMD+SHIFT+H',
      }
   }
  });
 
  return (
    <div>
      <div id="editorjs" />
    </div>
  );
};
export default EditorComponent;

Editor js grab the configuration of the tool adds the corresponding plugins in the editor component.

Similarly, you can add all the required plugins such as links, images, videos, embeds and code by installing each one of them manually and adding their respective plugins.

Section 6

Plugins

Every third party support provided is called a plugin. Here is the list of total plugins provided by editorjs.

  1. Header
  2. Link embeds
  3. Raw HTML blocks
  4. Simple Image (without backend requirement)
  5. Image
  6. Checklist
  7. List
  8. Embeds
  9. Quote

By clicking each of the plugins you will reach its respective Github page.

Section 7

API’s

Editor js provides an API to work around, collect data, modify the UI of the editor, add styles, cleaning and sanitize data. The most important and used API is saved. Basically save API return the data an array of data consisting of an object of each block, its type and its content.

text
editor.save().then(data => { console.log(data, 'data')});

The editor instance has a save method to return a promise and once resolved you will get your entire blocks data which you can directly store in the database.

This single save method saved a lot of time and the schema followed by each block object is simple and powerful.

text
const blocks = [
  {
   type: "paragraph",
   data: { text: 'Title'},
   id: "qpl8tGreMi"
  } 
]

Each block has a type key consisting of the information of the type of elements such as p tag or h tag, data key consist of text value or URL in case of links and lastly, id is used to establish distinguish of each block among the array of blocks.

Section 8

Other Methods

Editorjs instance provides other methods to support the customisation of the editor. For example, the onChange method is invoked whenever the user makes changes in the editor. The data object consists of predefined data for an editor to be rendered. You can learn more about such methods in the documentation part.

https://editorjs.io/getting-started

Conclusion

You can download the ready-to-use repository for editorjs with nextjs from the below link of our custom editor.

That’s it for today, until next time.

Shrey

External Links (10)

Explore topics

More in

Weekly letter

Subscribe to iHateReading

Our once-a-week newsletter on programming, jobs, AI, and building products online.

Our once a week newsletter on Programming, Jobs, AI, and Business