forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
56 lines (49 loc) · 1.64 KB
/
App.tsx
File metadata and controls
56 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Block, BlockNoteEditor, PartialBlock } from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import { useEffect, useMemo, useState } from "react";
async function saveToStorage(jsonBlocks: Block[]) {
// Save contents to local storage. You might want to debounce this or replace
// with a call to your API / database.
localStorage.setItem("editorContent", JSON.stringify(jsonBlocks));
}
async function loadFromStorage() {
// Gets the previously stored editor contents.
const storageString = localStorage.getItem("editorContent");
return storageString
? (JSON.parse(storageString) as PartialBlock[])
: undefined;
}
export default function App() {
const [initialContent, setInitialContent] = useState<
PartialBlock[] | undefined | "loading"
>("loading");
// Loads the previously stored editor contents.
useEffect(() => {
loadFromStorage().then((content) => {
setInitialContent(content);
});
}, []);
// Creates a new editor instance.
// We use useMemo + createBlockNoteEditor instead of useCreateBlockNote so we
// can delay the creation of the editor until the initial content is loaded.
const editor = useMemo(() => {
if (initialContent === "loading") {
return undefined;
}
return BlockNoteEditor.create({ initialContent });
}, [initialContent]);
if (editor === undefined) {
return "Loading content...";
}
// Renders the editor instance.
return (
<BlockNoteView
editor={editor}
onChange={() => {
saveToStorage(editor.document);
}}
/>
);
}