Giter Site home page Giter Site logo

Comments (7)

hsnfirdaus avatar hsnfirdaus commented on July 19, 2024 1

@hsnfirdaus hello, bro can you share the full code with me, that would be verry helpfull,

const useEditor = ({ onDestroy, ...config }: EditorConfig & { onDestroy?: () => void }) => {
	const [isEditorReady, setIsEditorReady] = useState(false);
	const editorInstance = useRef<EditorJS>(null);

and please clearify this for me, where this { onDestroy, ...config is comming from and one more thing is import { DEFAULT_EDITORJS_TOOLS } from "@/lib/editorjs/editorjs"; and what are these

I'm using typescript, this is simplified example of my previous reply useEditor.ts. config is configuration of editorjs (holder, tools, etc.):

import { useEffect, useRef, useState } from "react";
import EditorJS from "@editorjs/editorjs";
import { EditorConfig } from "@editorjs/editorjs/types/configs";

const useEditor = (config: EditorConfig) => {
	const [isEditorReady, setIsEditorReady] = useState(false);
	const editorInstance = useRef<EditorJS>(null);

	useEffect(() => {
		if (!editorInstance.current) {
			editorInstance.current = new EditorJS({
				...config,
				onReady: () => {
					setIsEditorReady(true);
					config.onReady?.();
				},
			});
		}
		return () => {
			if (editorInstance.current && editorInstance.current.destroy) {
				editorInstance.current.destroy();
				editorInstance.current = null;
			}
		};
	}, []);

	return {
		isEditorReady,
		editor: editorInstance.current,
	};
};

export default useEditor;

Usage (App.tsx):

import { EditorConfig } from "@editorjs/editorjs/types/configs";
import Paragraph from "@editorjs/paragraph";
import Header from "@editorjs/header";
import useEditor from "./useEditor";

const config: EditorConfig = {
  holder: 'editorjs',
  tools: {
    paragraph: Paragraph,
    header: Header
  }
} 

function App() {
  const { editor, isEditorReady } = useEditor(config);

  return (
      <div>
        <div id="editorjs"></div>
      </div>
  )
}

export default App

editor and isEditorReady variable is useful when you want to dynamicly change the content of editor (eg. After fetching content from server)

from editor.js.

Vishvsalvi avatar Vishvsalvi commented on July 19, 2024

Remove react strictmode to avoid double instances

from editor.js.

imran1khan avatar imran1khan commented on July 19, 2024

@Vishvsalvi , bro i am sorry to say but it's still not working, every single time when it re-rander it creates new instance of Editorjs

from editor.js.

hsnfirdaus avatar hsnfirdaus commented on July 19, 2024

I'm using hooks, it's working:

import { useEffect, useRef, useState } from "react";
import EditorJS from "@editorjs/editorjs";
import { EditorConfig } from "@editorjs/editorjs/types/configs";
import { DEFAULT_EDITORJS_TOOLS } from "@/lib/editorjs/editorjs";

const useEditor = ({ onDestroy, ...config }: EditorConfig & { onDestroy?: () => void }) => {
	const [isEditorReady, setIsEditorReady] = useState(false);
	const editorInstance = useRef<EditorJS>(null);

	useEffect(() => {
		if (!editorInstance.current) {
			let dom: HTMLElement | undefined = undefined;
			if (config.holder instanceof HTMLElement) {
				dom = config.holder;
			} else if (config.holder) {
				const el = document.getElementById(config.holder);
				if (el) {
					dom = el;
				}
			}
			if (dom !== undefined) {
				const editorJsDOM = document.createElement("div");
				dom.appendChild(editorJsDOM);
				editorInstance.current = new EditorJS({
					tools: DEFAULT_EDITORJS_TOOLS,
					...config,
					holder: editorJsDOM,
					onReady: () => {
						setIsEditorReady(true);
						config.onReady?.();
					},
				});
			}
		}
		return () => {
			if (editorInstance.current && editorInstance.current.destroy) {
				editorInstance.current.destroy();
				editorInstance.current = null;
				onDestroy?.();
			}
		};
	}, []);

	return {
		isEditorReady,
		editor: editorInstance.current,
	};
};

export default useEditor;

from editor.js.

imran1khan avatar imran1khan commented on July 19, 2024

@hsnfirdaus hello, bro can you share the full code with me, that would be verry helpfull,

const useEditor = ({ onDestroy, ...config }: EditorConfig & { onDestroy?: () => void }) => {
	const [isEditorReady, setIsEditorReady] = useState(false);
	const editorInstance = useRef<EditorJS>(null);

and please clearify this for me, where this { onDestroy, ...config is comming from and one more thing is
import { DEFAULT_EDITORJS_TOOLS } from "@/lib/editorjs/editorjs"; and what are these

from editor.js.

imran1khan avatar imran1khan commented on July 19, 2024

@hsnfirdaus hi, can we talk in private. it would be helpfull if you can share your emil or something

from editor.js.

imran1khan avatar imran1khan commented on July 19, 2024

@hsnfirdaus thanks bro, working perfectly,

from editor.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.