Skip to content

Commit f52b8c7

Browse files
Fixed types and comments (#136)
1 parent 21fe25b commit f52b8c7

File tree

5 files changed

+45
-38
lines changed

5 files changed

+45
-38
lines changed

packages/website/docs/docs/blocks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Now that we know how blocks are represented in code, let's take a look at the li
6363

6464
```typescript /App.tsx
6565
import { useState } from "react";
66-
import { BlockNoteEditor } from "@blocknote/core";
66+
import { BlockNoteEditor, Block } from "@blocknote/core";
6767
import { BlockNoteView, useBlockNote } from "@blocknote/react";
6868
import "@blocknote/core/style.css";
6969

@@ -79,8 +79,8 @@ export default function App() {
7979
setBlocks(editor.topLevelBlocks)
8080
})
8181

82-
// Renders a BlockNote editor, and its contents as an array of Block objects
83-
// below.
82+
// Renders the editor instance and its contents, as an array of Block
83+
// objects, below.
8484
return (
8585
<div>
8686
<BlockNoteView editor={editor}/>

packages/website/docs/docs/converting-blocks.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,24 @@ import "@blocknote/core/style.css";
4444

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

4949
// Creates a new editor instance.
50-
const editor = useBlockNote({
50+
const editor: BlockNoteEditor | null = useBlockNote({
5151
// Listens for when the editor's contents change.
5252
onEditorContentChange: (editor: BlockNoteEditor) => {
5353
// Converts the editor's contents from Block objects to Markdown and
5454
// saves them.
5555
const saveBlocksAsMarkdown = async () => {
56-
const markdown = await editor.blocksToMarkdown(editor.topLevelBlocks);
56+
const markdown: string =
57+
await editor.blocksToMarkdown(editor.topLevelBlocks);
5758
setMarkdown(markdown);
5859
};
5960
saveBlocksAsMarkdown();
6061
}
6162
});
6263

63-
// Renders a BlockNote editor, and its contents as Markdown below.
64+
// Renders the editor instance, and its contents as Markdown below.
6465
return (
6566
<div>
6667
<BlockNoteView editor={editor} />
@@ -98,11 +99,14 @@ Tries to create `Block` and `InlineNode` objects based on Markdown syntax, thoug
9899

99100
```typescript /App.tsx
100101
import { useEffect, useState } from "react";
101-
import { BlockNoteEditor } from "@blocknote/core";
102+
import { BlockNoteEditor, Block } from "@blocknote/core";
102103
import { BlockNoteView, useBlockNote } from "@blocknote/react";
103104
import "@blocknote/core/style.css";
104105

105106
export default function App() {
107+
// Stores the current Markdown content.
108+
const [markdown, setMarkdown] = useState<string>("");
109+
106110
// Creates a new editor instance.
107111
const editor: BlockNoteEditor | null = useBlockNote({
108112
// Makes the editor non-editable.
@@ -111,23 +115,20 @@ export default function App() {
111115
},
112116
})
113117

114-
// Stores the current Markdown content.
115-
const [markdown, setMarkdown] = useState<string>("");
116-
117118
useEffect(() => {
118119
if (editor) {
119120
// Whenever the current Markdown content changes, converts it to an array
120121
// of Block objects and replaces the editor's content with them.
121122
const getBlocks = async () => {
122-
const blocks = await editor.markdownToBlocks(markdown);
123+
const blocks: Block[] = await editor.markdownToBlocks(markdown);
123124
editor.replaceBlocks(editor.topLevelBlocks, blocks);
124125
};
125126
getBlocks();
126127
}
127128
}, [editor, markdown]);
128129

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.
130+
// Renders a text area for you to write/paste Markdown in, and the editor
131+
// instance below, which displays the current Markdown as blocks.
131132
return (
132133
<div>
133134
<textarea
@@ -179,23 +180,23 @@ import "@blocknote/core/style.css";
179180

180181
export default function App() {
181182
// Stores the editor's contents as HTML.
182-
const [html, setHTML] = useState<string | null>(null);
183+
const [html, setHTML] = useState<string>("");
183184

184185
// Creates a new editor instance.
185-
const editor = useBlockNote({
186+
const editor: BlockNoteEditor | null = useBlockNote({
186187
// Listens for when the editor's contents change.
187188
onEditorContentChange: (editor: BlockNoteEditor) => {
188189
// Converts the editor's contents from Block objects to HTML and saves
189190
// them.
190191
const saveBlocksAsHTML = async () => {
191-
const html = await editor.blocksToHTML(editor.topLevelBlocks);
192+
const html: string = await editor.blocksToHTML(editor.topLevelBlocks);
192193
setHTML(html);
193194
};
194195
saveBlocksAsHTML();
195196
}
196197
});
197198

198-
// Renders a BlockNote editor, and its contents as HTML below.
199+
// Renders the editor instance, and its contents as HTML below.
199200
return (
200201
<div>
201202
<BlockNoteView editor={editor} />
@@ -233,11 +234,14 @@ Tries to create `Block` objects out of any HTML block-level elements, and `Inlin
233234

234235
```typescript /App.tsx
235236
import { useEffect, useState } from "react";
236-
import { BlockNoteEditor } from "@blocknote/core";
237+
import { BlockNoteEditor, Block } from "@blocknote/core";
237238
import { BlockNoteView, useBlockNote } from "@blocknote/react";
238239
import "@blocknote/core/style.css";
239240

240241
export default function App() {
242+
// Stores the current HTML content.
243+
const [html, setHTML] = useState<string>("");
244+
241245
// Creates a new editor instance.
242246
const editor: BlockNoteEditor | null = useBlockNote({
243247
// Makes the editor non-editable.
@@ -246,22 +250,19 @@ export default function App() {
246250
},
247251
})
248252

249-
// Stores the current HTML content.
250-
const [html, setHTML] = useState<string>("");
251-
252253
useEffect(() => {
253254
if (editor) {
254255
// Whenever the current HTML content changes, converts it to an array of
255256
// Block objects and replaces the editor's content with them.
256257
const getBlocks = async () => {
257-
const blocks = await editor.HTMLToBlocks(html);
258+
const blocks: Block[] = await editor.HTMLToBlocks(html);
258259
editor.replaceBlocks(editor.topLevelBlocks, blocks);
259260
};
260261
getBlocks();
261262
}
262263
}, [editor, html]);
263264

264-
// Renders a text area for you to write/paste HTML in and a BlockNote editor
265+
// Renders a text area for you to write/paste HTML in, and the editor instance
265266
// below, which displays the current HTML as blocks.
266267
return (
267268
<div>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ Throws an error if the target block could not be found.
7272
::: sandbox {template=react-ts}
7373

7474
```typescript /App.tsx
75-
import { BlockNoteEditor } from "@blocknote/core";
75+
import { BlockNoteEditor, Block } from "@blocknote/core";
7676
import { BlockNoteView, useBlockNote } from "@blocknote/react";
7777
import "@blocknote/core/style.css";
7878

7979
export default function App() {
8080
// Creates a new editor instance.
8181
const editor: BlockNoteEditor | null = useBlockNote({
8282
// Listens for when the text cursor position changes.
83-
onTextCursorPositionChange: (editor) => {
83+
onTextCursorPositionChange: (editor: BlockNoteEditor) => {
8484
// Gets the block currently hovered by the text cursor.
85-
const hoveredBlock = editor.getTextCursorPosition().block;
85+
const hoveredBlock: Block = editor.getTextCursorPosition().block;
8686

8787
// Traverses all blocks.
88-
editor.forEachBlock((block) => {
88+
editor.forEachBlock((block: Block) => {
8989
if (
9090
block.id === hoveredBlock.id &&
9191
block.props.backgroundColor !== "blue"
@@ -99,7 +99,7 @@ export default function App() {
9999
block.id !== hoveredBlock.id &&
100100
block.props.backgroundColor === "blue"
101101
) {
102-
// If the block not is currently hovered by the text cursor, resets
102+
// If the block is not currently hovered by the text cursor, resets
103103
// its background if it's blue.
104104
editor.updateBlock(block, {
105105
props: {backgroundColor: "default"},
@@ -109,7 +109,7 @@ export default function App() {
109109
}
110110
})
111111

112-
// Renders a BlockNote editor.
112+
// Renders the editor instance.
113113
return <BlockNoteView editor={editor}/>;
114114
}
115115
```

packages/website/docs/docs/quickstart.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default function App() {
4848
const editor: BlockNoteEditor | null = useBlockNote({});
4949

5050
// Renders the editor instance using a React component.
51-
5251
return <BlockNoteView editor={editor} />;
5352
}
5453
```

packages/website/docs/docs/slash-menu.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,43 @@ import "@blocknote/core/style.css";
8282
import { HiOutlineGlobeAlt } from "react-icons/all";
8383

8484
export default function App() {
85+
// Command to insert "Hello World" in bold in a new block below.
8586
const insertHelloWorld = (editor: BlockNoteEditor) => {
87+
// Block that the text cursor is currently in.
8688
const currentBlock: Block = editor.getTextCursorPosition().block;
89+
90+
// New block we want to insert.
8791
const helloWorldBlock: PartialBlock = {
8892
type: "paragraph",
8993
content: [{type: "text", text: "Hello World", styles: {bold: true}}],
9094
};
9195

96+
// Inserting the new block after the current one.
9297
editor.insertBlocks([helloWorldBlock], currentBlock, "after");
9398
};
9499

100+
// Slash Menu item which executes the command.
95101
const insertHelloWorldItem: ReactSlashMenuItem =
96102
new ReactSlashMenuItem(
97103
"Insert Hello World",
98104
insertHelloWorld,
99105
["helloworld", "hw"],
100106
"Other",
101-
<HiOutlineGlobeAlt size = {18}
102-
/>,
103-
"Used to insert a block with 'Hello World' below."
104-
)
107+
<HiOutlineGlobeAlt size = {18}/>,
108+
"Used to insert a block with 'Hello World' below."
109+
)
105110

106-
const editor = useBlockNote({
111+
// Creates a new editor instance.
112+
const editor: BlockNoteEditor = useBlockNote({
113+
// Adds all default Slash Menu items as well as our custom one.
107114
slashCommands: [
108115
...defaultReactSlashMenuItems,
109116
insertHelloWorldItem
110117
]
111118
});
112119

113-
return <BlockNoteView editor = {editor}
114-
/>;
120+
// Renders the editor instance.
121+
return <BlockNoteView editor = {editor}/>;
115122
}
116123
```
117124

0 commit comments

Comments
 (0)