Skip to content

Commit 6925ee1

Browse files
Editor option for initial contents (#154)
* Added `initialContent` editor option and basic docs * Changed `initialContent` type to `PartialBlock` * Changed `initialContent` type to `PartialBlock` in docs * Added editor content saving/restoring demo
1 parent fdbb1ed commit 6925ee1

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packages/core/src/BlockNoteEditor.ts

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type BlockNoteEditorOptions = {
4141
onEditorReady: (editor: BlockNoteEditor) => void;
4242
onEditorContentChange: (editor: BlockNoteEditor) => void;
4343
onTextCursorPositionChange: (editor: BlockNoteEditor) => void;
44+
initialContent: PartialBlock[];
4445

4546
// tiptap options, undocumented
4647
_tiptapOptions: any;
@@ -72,10 +73,19 @@ export class BlockNoteEditor {
7273
: blockNoteExtensions;
7374

7475
const tiptapOptions: EditorOptions = {
76+
// TODO: This approach to setting initial content is "cleaner" but requires the PM editor schema, which is only
77+
// created after initializing the TipTap editor. Not sure it's feasible.
78+
// content:
79+
// options.initialContent &&
80+
// options.initialContent.map((block) =>
81+
// blockToNode(block, this._tiptapEditor.schema).toJSON()
82+
// ),
7583
...blockNoteTipTapOptions,
7684
...options._tiptapOptions,
7785
onCreate: () => {
7886
options.onEditorReady?.(this);
87+
options.initialContent &&
88+
this.replaceBlocks(this.topLevelBlocks, options.initialContent);
7989
},
8090
onUpdate: () => {
8191
options.onEditorContentChange?.(this);

packages/website/docs/docs/editor.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Customizing the Editor
22

3+
While you can get started with BlockNote in minutes, it's likely that you'll want to customize its features and functionality to better suit your app.
4+
5+
## Editor Options
6+
37
There are a number of options that you can pass to `useBlockNote()`, which you can use to customize the editor. You can find the full list of these below:
48

59
```typescript
610
export type BlockNoteEditorOptions = {
11+
initialContent: PartialBlock[];
712
editorDOMAttributes: Record<string, string>;
813
onEditorReady: (editor: BlockNoteEditor) => void;
914
onEditorContentChange: (editor: BlockNoteEditor) => void;
@@ -13,6 +18,8 @@ export type BlockNoteEditorOptions = {
1318
};
1419
```
1520

21+
`initialContent:` The content that should be in the editor when it's created, represented as an array of [partial block objects](/docs/manipulating-blocks#partial-blocks).
22+
1623
`editorDOMAttributes:` An object containing attributes that should be added to the editor's HTML element.
1724

1825
`onEditorReady:` A callback function that runs when the editor is ready to be used.
@@ -23,4 +30,41 @@ export type BlockNoteEditorOptions = {
2330

2431
`slashMenuItems:` The commands that are listed in the editor's [Slash Menu](/docs/slash-menu). If this option isn't defined, a default list of commands is loaded.
2532

26-
`uiFactories:` Factories used to create a custom UI for BlockNote, which you can find out more about in [Creating Your Own UI Elements](/docs/vanilla-js#creating-your-own-ui-elements).
33+
`uiFactories:` Factories used to create a custom UI for BlockNote, which you can find out more about in [Creating Your Own UI Elements](/docs/vanilla-js#creating-your-own-ui-elements).
34+
35+
## Demo: Saving & Restoring Editor Contents
36+
37+
By default, BlockNote doesn't preserve the editor contents when your app is reopened or refreshed. However, using the editor options, you can change this by using the editor options.
38+
39+
In the example below, we use the `onEditorContentChange` option to save the editor contents in local storage whenever they change, then pass them to `initialContent` whenever the page is reloaded.
40+
41+
::: sandbox {template=react-ts}
42+
43+
```typescript /App.tsx
44+
import { BlockNoteEditor } from "@blocknote/core";
45+
import { BlockNoteView, useBlockNote } from "@blocknote/react";
46+
import "@blocknote/core/style.css";
47+
48+
// Gets the previously stored editor contents.
49+
const initialContent: string | null = localStorage.getItem("editorContent");
50+
51+
export default function App() {
52+
// Creates a new editor instance.
53+
const editor: BlockNoteEditor | null = useBlockNote({
54+
// If the editor contents were previously saved, restores them.
55+
initialContent: initialContent ? JSON.parse(initialContent) : undefined,
56+
// Serializes and saves the editor contents to local storage.
57+
onEditorContentChange: (editor) => {
58+
localStorage.setItem(
59+
"editorContent",
60+
JSON.stringify(editor.topLevelBlocks)
61+
);
62+
}
63+
});
64+
65+
// Renders the editor instance.
66+
return <BlockNoteView editor={editor} />;
67+
}
68+
```
69+
70+
:::

0 commit comments

Comments
 (0)