Logo

dev-resources.site

for different kinds of informations.

How To Add Editor.Js To Your Next.Js Code

Published at
9/16/2024
Categories
nextjs
approuter
react
javascript
Author
nifty-little-me
Categories
4 categories in total
nextjs
open
approuter
open
react
open
javascript
open
Author
15 person written this
nifty-little-me
open
How To Add Editor.Js To Your Next.Js Code

Note: This article will be using the App Router

In case you have an application that requires the user or yourself to type something and save it in a database, you will need to use something like Editor.js to accomplish this.

Of course, there are other options.

Like, you can choose to use the Quill WYSIWYG rich text editor that I talk about here. Anyways, today we will solely discuss using Editor.js inside our next.js projects. Let’s dive into it.

Unsplash Image by Kevin Ku

Run this command in your terminal:

npm i @editorjs/editorjs react-editor-js @editorjs/embed @editorjs/table @editorjs/paragraph @editorjs/list @editorjs/warning @editorjs/code @editorjs/link @editorjs/image @editorjs/raw @editorjs/header @editorjs/quote @editorjs/marker @editorjs/checklist @editorjs/delimiter @editorjs/inline-code @editorjs/simple-image
Enter fullscreen mode Exit fullscreen mode

Note: There’s something I want to go over before we get started. When installing @editorjs/header, @editorjs/paragraph, and so on, when importing and using typescript, you must change the file to a .js file. If you don’t, it will result in “Could not find a declaration file for module…”

So, with that being said, let’s continue.

Integrating Editor.js into Next.js Project

Create an editor.js file inside your components folder, or whatever folder you want to use. Add this code to it:

import React, { useEffect, useRef, useState } from "react";
import EditorJS from "@editorjs/editorjs";
import CheckList from "@editorjs/checklist";
import Code from "@editorjs/code";
import Delimiter from "@editorjs/delimiter";
import Embed from "@editorjs/embed";
import Image from "@editorjs/image";
import InlineCode from "@editorjs/inline-code";
import List from "@editorjs/list";
import Quote from "@editorjs/quote";
import Table from "@editorjs/table";
import SimpleImage from "@editorjs/simple-image";
import Paragraph from "@editorjs/paragraph";
import Header from "@editorjs/header";
import Raw from "@editorjs/raw";

const EDITOR_TOOLS = {
  code: Code,
  header: {
    class: Header,
    shortcut: "CMD+H",
    inlineToolbar: true,
    config: {
      placeholder: "Enter a Header",
      levels: [2, 3, 4],
      defaultLevel: 2,
    },
  },
  paragraph: {
    class: Paragraph,
    inlineToolbar: true,
  },
  checklist: CheckList,
  inlineCode: InlineCode,
  table: Table,
  list: List,
  quote: Quote,
  delimiter: Delimiter,
  raw: Raw,
};
function Editor({ data, onChange, holder }) {
  const ref = useRef();
  useEffect(() => {
    if (!ref.current) {
      const editor = new EditorJS({
        holder: holder,
        placeholder: "Start writing here..",
        tools: EDITOR_TOOLS,
        data,

      });
      ref.current = editor;
    }

    return () => {
      if (ref.current && ref.current.destroy) {
        ref.current.destroy();
      }
    };
  }, []);

  return (
    <>
      <div
        id={holder}
        style={{
          width: "100%",
          minHeight: 500,
          borderRadius: " 7px",
          background: "fff",
        }}
      />
    </>
  );
}

export default Editor;
Enter fullscreen mode Exit fullscreen mode

Now, inside your page.tsx file you want to have the editor in, add this code:

'use client';
import React, { useState } from 'react';
import { useRouter } from 'next/navigation';
import dynamic from 'next/dynamic';

const ReactQuill = dynamic(() => import("react-quill"), { ssr: false });

const DocumentPage = () => {
  const Editor = dynamic(
    () => import('../../components/editor'),
    { ssr: false }
  );
  const [value, setValue] = useState('');;

  return (
    <main className="p-4">
        <Editor data={value} onChange={(e: any) => setValue(e)} holder="Start Typing Here..." />
    </main>
  );
};

export default DocumentPage;
Enter fullscreen mode Exit fullscreen mode

So, that’s how you add editor.js to your next.js code. If you liked this short article, follow me on Medium and subscribe to my newsletter.

Happy Coding Folks!

approuter Article's
23 articles in total
Favicon
Show a loading screen when changing pages in Next.js App router
Favicon
Learning Next.js 13 App Router: A Comprehensive Guide 🚀
Favicon
Guide to build a modern Web App using Next.js 14 (App Router), Fully authentication (NextAuth), Theming and i18n
Favicon
A practical Guide - Migrating to Next.js App Router
Favicon
Adding Chat Functionality To Your Next.Js Project With Firebase
Favicon
Spicing Up Your Next.Js Projects With 3D: What Are Your Options?
Favicon
No More Pages
Favicon
How To Create A Basic Infinity Canvas For Your Next.Js Project
Favicon
How To Implement Text-To-Speech Functionality For BlockNote In Next.Js
Favicon
How To Add Drag-And-Drop Functionality With Editable Draggable Items In Next.js
Favicon
Adding Drag And Drop Functionality In Your Next.Js Project Without A Library
Favicon
Creating NPM Packages in Next.Js for Next.Js
Favicon
Using Firebase To Store Folders and BlockNote Documents In Next.Js
Favicon
An Alternative To Editor.js: BlockNote For React
Favicon
How To Add Editor.Js To Your Next.Js Code
Favicon
How To Use The Quill Rich Text Editor in Your Next.Js App Router Project
Favicon
Simple NextJS GraphQL client
Favicon
Implementing Internationalization (i18n) in Next.js 14 using App Router
Favicon
Web Streams API in Action: Delivering 6000+ Log Lines Concurrently Across 20 Tabs
Favicon
How to use React-Toastify with Next.js App router
Favicon
NextAuth - Implementando "Custom Login Pages" com "Credentials Sign in" utilizando App Router
Favicon
The origin of App Router - A Next.Js Rewind
Favicon
How to add multiple routers in a node application without using app.use() for each router ?

Featured ones: