Comments (7)
@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 isimport { 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.
Remove react strictmode to avoid double instances
from editor.js.
@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.
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.
@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.
@hsnfirdaus hi, can we talk in private. it would be helpfull if you can share your emil or something
from editor.js.
@hsnfirdaus thanks bro, working perfectly,
from editor.js.
Related Issues (20)
- Question: Toggling Toolbar
- Inline toolbar usage and render data
- Can the block and tune components communicate with each other
- Tools not working ? new way to import in React ?
- TypeError: Cannot read properties of undefined (reading 'toString') HOT 3
- Block Saver with sanitizer
- Sanitize on Block Tools overwrite the base sanitize config
- Editor state getting reverted using header and console logs "Block «header» skipped because saved data is invalid". HOT 4
- Latest editorjs bold button click triggers form submit HOT 2
- Custom default paragraph is adding 2 paragraphs when you press the 'Add' button if it's the last block
- Type error, type files are missing while compiling HOT 4
- When Editor is on readonly' mode editor throw warning "EventDispatcher .off(): there is no subscribers for event"
- An error is raised when BlockTool does not define toolbox static properties 【 2.30.0 and 2.30.1】 HOT 1
- Disable converter in inline toolbar for some specific tools HOT 1
- Action on paste: prevent from creating new blocks HOT 1
- When opening the toolbar, the save method is called. Why? HOT 3
- Does it support adding a horizontal toolbar menu at the top of the editing area? thank you!
- "Convert to" in i18n.messages.ui.popover missing
- Allow async render in Block Tool Plugin
- d.ts import file loss or error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from editor.js.