Skip to content

Commit 2f34276

Browse files
Listener Documentation & Section Naming (#126)
* Updated listener docs * Fixed section naming consistency * Changed `onEditorCreate` to `onEditorReady`
1 parent c0d8a42 commit 2f34276

File tree

9 files changed

+100
-104
lines changed

9 files changed

+100
-104
lines changed

packages/core/src/BlockNoteEditor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type BlockNoteEditorOptions = {
3838
slashCommands: BaseSlashMenuItem[];
3939
parentElement: HTMLElement;
4040
editorDOMAttributes: Record<string, string>;
41-
onEditorCreate: (editor: BlockNoteEditor) => void;
41+
onEditorReady: (editor: BlockNoteEditor) => void;
4242
onEditorContentChange: (editor: BlockNoteEditor) => void;
4343
onTextCursorPositionChange: (editor: BlockNoteEditor) => void;
4444

@@ -75,7 +75,7 @@ export class BlockNoteEditor {
7575
...blockNoteTipTapOptions,
7676
...options._tiptapOptions,
7777
onCreate: () => {
78-
options.onEditorCreate?.(this);
78+
options.onEditorReady?.(this);
7979
},
8080
onUpdate: () => {
8181
options.onEditorContentChange?.(this);

packages/website/docs/.vitepress/config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ const SIDEBAR_DEFAULT = [
2222
text: "Editor setup",
2323
collapsed: false,
2424
items: [
25-
{ text: "Customizing the editor", link: "/docs/editor" },
26-
{ text: "Slash menu", link: "/docs/slash-menu" },
27-
{ text: "Formatting toolbar", link: "/docs/formatting-toolbar" },
28-
{ text: "Side menu", link: "/docs/side-menu" },
25+
{ text: "Customizing the Editor", link: "/docs/editor" },
26+
{ text: "Slash Menu", link: "/docs/slash-menu" },
27+
{ text: "Formatting Toolbar", link: "/docs/formatting-toolbar" },
28+
{ text: "Side Menu", link: "/docs/side-menu" },
2929
],
3030
},
3131
{
3232
text: "Working with Blocks",
3333
collapsed: false,
3434
items: [
3535
{ text: "Introduction to Blocks", link: "/docs/blocks" },
36-
{ text: "Block types", link: "/docs/block-types" },
36+
{ text: "Block Types & Properties", link: "/docs/block-types" },
3737
{ text: "Manipulating Blocks", link: "/docs/manipulating-blocks" },
3838
{ text: "Inline Content", link: "/docs/inline-content" },
39-
{ text: "Cursor and selections", link: "/docs/cursor-selections" },
40-
{ text: "Markdown and HTML", link: "/docs/converting-blocks" },
39+
{ text: "Cursor & Selections", link: "/docs/cursor-selections" },
40+
{ text: "Markdown & HTML", link: "/docs/converting-blocks" },
4141
],
4242
},
4343

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
# Converting Blocks to and from other formats
1+
# Markdown & HTML
22

3-
It's possible to export Blocks to other formats, or import Blocks from other formats.
3+
It's possible to export or import Blocks to and from Markdown and HTML.
44

55
::: warning
6-
The functions to import / export to and from HTML / Markdown are considered "lossy"; some information might be dropped when you export Blocks to those formats.
6+
The functions to import/export to and from Markdown/HTML are considered "lossy"; some information might be dropped when you export Blocks to those formats.
77

88
To serialize Blocks to a non-lossy format (for example, to store the contents of the editor in your backend), simply export the built-in Block format using `JSON.stringify(editor.allBlocks)`.
99
:::
1010

11-
## HTML
11+
## Markdown
1212

13-
We expose functions to convert Blocks to and from HTML. Converting Blocks to HTML will lose some information such as the nesting of nodes in order to export a simple HTML structure.
13+
BlockNote can import / export Blocks to and from Markdown. Note that this is also considered "lossy", as not all structures can be entirely represented in Markdown.
1414

15-
### Converting Blocks to HTML
15+
### Converting Blocks to Markdown
1616

17-
`Block` objects can be serialized to an HTML string using the following function:
17+
`Block` objects can be serialized to a Markdown string using the following function:
1818

1919
```typescript
2020
// Definition
2121
class BlockNoteEditor {
2222
...
23-
public blocksToHTML(blocks: Block[]): string;
23+
public blocksToMarkdown(blocks: Block[]): string;
2424
...
2525
}
2626

2727
// Usage
28-
const HTMLFromBlocks = editor.blocksToHTML(blocks);
28+
const markdownFromBlocks = editor.blocksToMarkdown(blocks);
2929
```
3030

31-
`returns:` The blocks, serialized as an HTML string.
31+
`returns:` The blocks, serialized as a Markdown string.
3232

33-
To better conform to HTML standards, children of blocks which aren't list items are un-nested in the output HTML.
33+
The output is simplified as Markdown does not support all features of BlockNote - children of blocks which aren't list items are un-nested and certain styles are removed.
3434

3535
**Demo**
3636

@@ -43,55 +43,54 @@ import { BlockNoteView, useBlockNote } from "@blocknote/react";
4343
import "@blocknote/core/style.css";
4444

4545
export default function App() {
46-
// Stores the editor's contents as HTML.
47-
const [html, setHTML] = useState<string | null>(null);
46+
// Stores the editor's contents as Markdown.
47+
const [markdown, setMarkdown] = useState<string | null>(null);
4848

4949
// Creates a new editor instance.
5050
const editor = useBlockNote({
5151
// Listens for when the editor's contents change.
5252
onEditorContentChange: (editor: BlockNoteEditor) => {
53-
// Converts the editor's contents from Block objects to HTML and saves
54-
// them.
55-
const saveBlocksAsHTML = async () => {
56-
const html = await editor.blocksToHTML(editor.topLevelBlocks);
57-
setHTML(html);
53+
// Converts the editor's contents from Block objects to Markdown and
54+
// saves them.
55+
const saveBlocksAsMarkdown = async () => {
56+
const markdown = await editor.blocksToMarkdown(editor.topLevelBlocks);
57+
setMarkdown(markdown);
5858
};
59-
saveBlocksAsHTML();
59+
saveBlocksAsMarkdown();
6060
}
6161
});
62-
63-
// Renders a BlockNote editor, and its contents as HTML below.
62+
63+
// Renders a BlockNote editor, and its contents as Markdown below.
6464
return (
6565
<div>
6666
<BlockNoteView editor={editor} />
67-
<pre style={{ whiteSpace: "pre-wrap" }}>{html}</pre>
67+
<pre style={{ whiteSpace: "pre-wrap" }}>{markdown}</pre>
6868
</div>
6969
);
7070
}
7171
```
7272

73-
7473
:::
7574

76-
### Converting HTML to Blocks
75+
### Converting Markdown to Blocks
7776

78-
`Block` objects can be parsed from an HTML string using the following function:
77+
`Block` objects can be parsed from a Markdown string using the following function:
7978

8079
```typescript
8180
// Definition
8281
class BlockNoteEditor {
8382
...
84-
public HTMLToBlocks(html: string): Blocks[];
83+
public markdownToBlocks(markdown: string): Blocks[];
8584
...
8685
}
8786

8887
// Usage
89-
const blocksFromHTML = editor.HTMLToBlocks(html);
88+
const blocksFromMarkdown = editor.markdownToBlocks(markdown);
9089
```
9190

92-
`returns:` The blocks parsed from the HTML string.
91+
`returns:` The blocks parsed from the Markdown string.
9392

94-
Tries to create `Block` objects out of any HTML block-level elements, and `InlineNode` objects from any HTML inline elements, though not all HTML tags are recognized. If BlockNote doesn't recognize an element's tag, it will parse it as a paragraph or plain text.
93+
Tries to create `Block` and `InlineNode` objects based on Markdown syntax, though not all symbols are recognized. If BlockNote doesn't recognize a symbol, it will parse it as text.
9594

9695
**Demo**
9796

@@ -112,29 +111,29 @@ export default function App() {
112111
},
113112
})
114113

115-
// Stores the current HTML content.
116-
const [html, setHTML] = useState<string>("");
114+
// Stores the current Markdown content.
115+
const [markdown, setMarkdown] = useState<string>("");
117116

118117
useEffect(() => {
119118
if (editor) {
120-
// Whenever the current HTML content changes, converts it to an array of
121-
// Block objects and replaces the editor's content with them.
119+
// Whenever the current Markdown content changes, converts it to an array
120+
// of Block objects and replaces the editor's content with them.
122121
const getBlocks = async () => {
123-
const blocks = await editor.HTMLToBlocks(html);
122+
const blocks = await editor.markdownToBlocks(markdown);
124123
editor.replaceBlocks(editor.topLevelBlocks, blocks);
125124
};
126125
getBlocks();
127126
}
128-
}, [editor, html]);
127+
}, [editor, markdown]);
129128

130-
// Renders a text area for you to write/paste HTML in and a BlockNote editor
131-
// below, which displays the current HTML as blocks.
129+
// Renders a text area for you to write/paste Markdown in and a BlockNote
130+
// editor below, which displays the current Markdown as blocks.
132131
return (
133132
<div>
134133
<textarea
135134
style={{ width: "100%", height: "100px" }}
136-
value={html}
137-
onChange={(event) => setHTML(event.target.value)}
135+
value={markdown}
136+
onChange={(event) => setMarkdown(event.target.value)}
138137
/>
139138
<BlockNoteView editor={editor} />
140139
</div>
@@ -144,29 +143,29 @@ export default function App() {
144143

145144
:::
146145

147-
## Markdown
146+
## HTML
148147

149-
BlockNote can import / export Blocks to and from Markdown. Note that this is also considered "lossy", as not all structures can be entirely represented in Markdown.
148+
We expose functions to convert Blocks to and from HTML. Converting Blocks to HTML will lose some information such as the nesting of nodes in order to export a simple HTML structure.
150149

151-
### Converting Blocks to Markdown
150+
### Converting Blocks to HTML
152151

153-
`Block` objects can be serialized to a Markdown string using the following function:
152+
`Block` objects can be serialized to an HTML string using the following function:
154153

155154
```typescript
156155
// Definition
157156
class BlockNoteEditor {
158157
...
159-
public blocksToMarkdown(blocks: Block[]): string;
158+
public blocksToHTML(blocks: Block[]): string;
160159
...
161160
}
162161

163162
// Usage
164-
const markdownFromBlocks = editor.blocksToMarkdown(blocks);
163+
const HTMLFromBlocks = editor.blocksToHTML(blocks);
165164
```
166165

167-
`returns:` The blocks, serialized as a Markdown string.
166+
`returns:` The blocks, serialized as an HTML string.
168167

169-
The output is simplified as Markdown does not support all features of BlockNote - children of blocks which aren't list items are un-nested and certain styles are removed.
168+
To better conform to HTML standards, children of blocks which aren't list items are un-nested in the output HTML.
170169

171170
**Demo**
172171

@@ -179,54 +178,54 @@ import { BlockNoteView, useBlockNote } from "@blocknote/react";
179178
import "@blocknote/core/style.css";
180179

181180
export default function App() {
182-
// Stores the editor's contents as Markdown.
183-
const [markdown, setMarkdown] = useState<string | null>(null);
181+
// Stores the editor's contents as HTML.
182+
const [html, setHTML] = useState<string | null>(null);
184183

185184
// Creates a new editor instance.
186185
const editor = useBlockNote({
187186
// Listens for when the editor's contents change.
188187
onEditorContentChange: (editor: BlockNoteEditor) => {
189-
// Converts the editor's contents from Block objects to Markdown and
190-
// saves them.
191-
const saveBlocksAsMarkdown = async () => {
192-
const markdown = await editor.blocksToMarkdown(editor.topLevelBlocks);
193-
setMarkdown(markdown);
188+
// Converts the editor's contents from Block objects to HTML and saves
189+
// them.
190+
const saveBlocksAsHTML = async () => {
191+
const html = await editor.blocksToHTML(editor.topLevelBlocks);
192+
setHTML(html);
194193
};
195-
saveBlocksAsMarkdown();
194+
saveBlocksAsHTML();
196195
}
197196
});
198-
199-
// Renders a BlockNote editor, and its contents as Markdown below.
197+
198+
// Renders a BlockNote editor, and its contents as HTML below.
200199
return (
201200
<div>
202201
<BlockNoteView editor={editor} />
203-
<pre style={{ whiteSpace: "pre-wrap" }}>{markdown}</pre>
202+
<pre style={{ whiteSpace: "pre-wrap" }}>{html}</pre>
204203
</div>
205204
);
206205
}
207206
```
208207

209208
:::
210209

211-
### Converting Markdown to Blocks
210+
### Converting HTML to Blocks
212211

213-
`Block` objects can be parsed from a Markdown string using the following function:
212+
`Block` objects can be parsed from an HTML string using the following function:
214213

215214
```typescript
216215
// Definition
217216
class BlockNoteEditor {
218217
...
219-
public markdownToBlocks(markdown: string): Blocks[];
218+
public HTMLToBlocks(html: string): Blocks[];
220219
...
221220
}
222221

223222
// Usage
224-
const blocksFromMarkdown = editor.markdownToBlocks(markdown);
223+
const blocksFromHTML = editor.HTMLToBlocks(html);
225224
```
226225

227-
`returns:` The blocks parsed from the Markdown string.
226+
`returns:` The blocks parsed from the HTML string.
228227

229-
Tries to create `Block` and `InlineNode` objects based on Markdown syntax, though not all symbols are recognized. If BlockNote doesn't recognize a symbol, it will parse it as text.
228+
Tries to create `Block` objects out of any HTML block-level elements, and `InlineNode` objects from any HTML inline elements, though not all HTML tags are recognized. If BlockNote doesn't recognize an element's tag, it will parse it as a paragraph or plain text.
230229

231230
**Demo**
232231

@@ -247,34 +246,34 @@ export default function App() {
247246
},
248247
})
249248

250-
// Stores the current Markdown content.
251-
const [markdown, setMarkdown] = useState<string>("");
249+
// Stores the current HTML content.
250+
const [html, setHTML] = useState<string>("");
252251

253252
useEffect(() => {
254253
if (editor) {
255-
// Whenever the current Markdown content changes, converts it to an array
256-
// of Block objects and replaces the editor's content with them.
254+
// Whenever the current HTML content changes, converts it to an array of
255+
// Block objects and replaces the editor's content with them.
257256
const getBlocks = async () => {
258-
const blocks = await editor.markdownToBlocks(markdown);
257+
const blocks = await editor.HTMLToBlocks(html);
259258
editor.replaceBlocks(editor.topLevelBlocks, blocks);
260259
};
261260
getBlocks();
262261
}
263-
}, [editor, markdown]);
262+
}, [editor, html]);
264263

265-
// Renders a text area for you to write/paste Markdown in and a BlockNote
266-
// editor below, which displays the current Markdown as blocks.
264+
// Renders a text area for you to write/paste HTML in and a BlockNote editor
265+
// below, which displays the current HTML as blocks.
267266
return (
268267
<div>
269268
<textarea
270269
style={{ width: "100%", height: "100px" }}
271-
value={markdown}
272-
onChange={(event) => setMarkdown(event.target.value)}
270+
value={html}
271+
onChange={(event) => setHTML(event.target.value)}
273272
/>
274273
<BlockNoteView editor={editor} />
275274
</div>
276275
);
277276
}
278277
```
279278

280-
:::
279+
:::

packages/website/docs/docs/cursor-selections.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
# Cursor and Selections
1+
# Cursor & Selections
22

3-
This page will explain how to work with the Cursor and Selections
4-
5-
- explain differences
6-
- how to get / set them
7-
- examples (how to get)
3+
If you want to know which block(s) the user is currently editing, you can do so using cursor positions and selections.
84

95
## Cursor Positions
106

11-
BlockNote allows you to keep track of the text cursor position in the editor, to get information about the block it's in and its surrounding blocks.
7+
BlockNote keeps track of the text cursor position in the editor and exposes functions that let you retrieve it or change it.
128

139
## Text Cursor
1410

15-
The text cursor is the blinking vertical line you see when typing in the editor. In code, its position is represented using the following object:
11+
The text cursor is the blinking vertical line you see when typing in the editor. BlockNote uses `TextCursorPosition` objects to give you information about the block it's in as well as those around it:
1612

1713
```typescript
1814
type TextCursorPosition = {

0 commit comments

Comments
 (0)