Skip to content

Commit c0ea16b

Browse files
committed
Merge branch 'plugin-system' of github.com:TypeCellOS/BlockNote into plugin-system
2 parents 14a9ee4 + 208e967 commit c0ea16b

98 files changed

Lines changed: 352 additions & 1030 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/03-ui-components/02-formatting-toolbar-buttons/src/BlueButton.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import "@blocknote/mantine/style.css";
22
import {
33
useBlockNoteEditor,
44
useComponentsContext,
5-
useEditorContentOrSelectionChange,
5+
useEditorState,
66
useSelectedBlocks,
77
} from "@blocknote/react";
8-
import { useState } from "react";
98

109
// Custom Formatting Toolbar Button to toggle blue text & background color.
1110
export function BlueButton() {
@@ -14,18 +13,12 @@ export function BlueButton() {
1413
const Components = useComponentsContext()!;
1514

1615
// Tracks whether the text & background are both blue.
17-
const [isSelected, setIsSelected] = useState<boolean>(
18-
editor.getActiveStyles().textColor === "blue" &&
19-
editor.getActiveStyles().backgroundColor === "blue",
20-
);
21-
22-
// Updates state on content or selection change.
23-
useEditorContentOrSelectionChange(() => {
24-
setIsSelected(
16+
const isSelected = useEditorState({
17+
editor,
18+
selector: ({ editor }) =>
2519
editor.getActiveStyles().textColor === "blue" &&
26-
editor.getActiveStyles().backgroundColor === "blue",
27-
);
28-
}, editor);
20+
editor.getActiveStyles().backgroundColor === "blue",
21+
});
2922

3023
// Doesn't render unless a at least one block with inline content is
3124
// selected. You can use a similar pattern of returning `null` to

examples/03-ui-components/04-side-menu-buttons/src/RemoveBlockButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {} from "@blocknote/core";
2-
import { SideMenu } from "@blocknote/core/extensions";
2+
import { SideMenuExtension } from "@blocknote/core/extensions";
33
import {
44
useBlockNoteEditor,
55
useComponentsContext,
@@ -13,7 +13,7 @@ export function RemoveBlockButton() {
1313

1414
const Components = useComponentsContext()!;
1515

16-
const sideMenu = useExtension(SideMenu);
16+
const sideMenu = useExtension(SideMenuExtension);
1717

1818
return (
1919
<Components.SideMenu.Button

examples/03-ui-components/05-side-menu-drag-handle-items/src/ResetBlockTypeItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {} from "@blocknote/core";
2-
import { SideMenu } from "@blocknote/core/extensions";
2+
import { SideMenuExtension } from "@blocknote/core/extensions";
33
import {
44
useBlockNoteEditor,
55
useComponentsContext,
@@ -12,7 +12,7 @@ export function ResetBlockTypeItem(props: { children: ReactNode }) {
1212

1313
const Components = useComponentsContext()!;
1414

15-
const sideMenu = useExtension(SideMenu);
15+
const sideMenu = useExtension(SideMenuExtension);
1616

1717
return (
1818
<Components.Generic.Menu.Item

examples/03-ui-components/13-custom-ui/src/MUIFormattingToolbar.tsx

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Block } from "@blocknote/core";
22
import {
33
blockTypeSelectItems,
44
useBlockNoteEditor,
5-
useEditorContentOrSelectionChange,
5+
useEditorState,
66
} from "@blocknote/react";
77
import {
88
Done,
@@ -108,17 +108,10 @@ function MUIToolbarSelect<Item extends { name: string; icon?: FC }>(props: {
108108
function MUIBlockTypeSelect() {
109109
const editor = useBlockNoteEditor<TextBlockSchema>();
110110

111-
// The block currently containing the text cursor.
112-
const [block, setBlock] = useState<Block>(
113-
editor.getTextCursorPosition().block,
114-
);
115-
116-
// Updates the block currently containing the text cursor whenever the editor
117-
// content or selection changes.
118-
useEditorContentOrSelectionChange(
119-
() => setBlock(editor.getTextCursorPosition().block),
111+
const block = useEditorState({
120112
editor,
121-
);
113+
selector: ({ editor }) => editor.getTextCursorPosition().block,
114+
});
122115

123116
// Gets the default items for the select.
124117
const defaultBlockTypeSelectItems = useMemo(
@@ -154,8 +147,6 @@ function MUIBlockTypeSelect() {
154147
props: newSelectedItem.props,
155148
});
156149
editor.focus();
157-
158-
setBlock(editor.getTextCursorPosition().block);
159150
},
160151
[block, defaultBlockTypeSelectItems, editor],
161152
);
@@ -220,16 +211,10 @@ function MUIBasicTextStyleButton(props: {
220211
const editor = useBlockNoteEditor<TextBlockSchema>();
221212

222213
// Whether the text style is currently active.
223-
const [textStyleActive, setTextStyleActive] = useState(
224-
!!editor.getActiveStyles()[props.textStyle],
225-
);
226-
227-
// Updates whether the text style is active when the editor content or
228-
// selection changes.
229-
useEditorContentOrSelectionChange(
230-
() => setTextStyleActive(props.textStyle in editor.getActiveStyles()),
214+
const textStyleActive = useEditorState({
231215
editor,
232-
);
216+
selector: ({ editor }) => props.textStyle in editor.getActiveStyles(),
217+
});
233218

234219
// Tooltip for the button.
235220
const tooltip = useMemo(
@@ -273,24 +258,16 @@ function MUITextAlignButton(props: {
273258
const editor = useBlockNoteEditor<TextBlockSchema>();
274259

275260
// The text alignment of the block currently containing the text cursor.
276-
const [activeTextAlignment, setActiveTextAlignment] = useState(() => {
277-
const props = editor.getTextCursorPosition().block.props;
278-
if ("textAlignment" in props) {
279-
return props.textAlignment;
280-
}
281-
return undefined;
282-
});
283-
284-
// Updates the text alignment when the editor content or selection changes.
285-
useEditorContentOrSelectionChange(() => {
286-
setActiveTextAlignment(() => {
261+
const activeTextAlignment = useEditorState({
262+
editor,
263+
selector: ({ editor }) => {
287264
const props = editor.getTextCursorPosition().block.props;
288265
if ("textAlignment" in props) {
289266
return props.textAlignment;
290267
}
291268
return undefined;
292-
});
293-
}, editor);
269+
},
270+
});
294271

295272
// Tooltip for the button.
296273
const tooltip = useMemo(
@@ -346,21 +323,15 @@ function MUIColorStyleButton() {
346323
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
347324

348325
// The active text and background colors.
349-
const [activeTextColor, setActiveTextColor] = useState(
350-
() => editor.getActiveStyles().textColor || "default",
351-
);
352-
const [activeBackgroundColor, setActiveBackgroundColor] = useState(
353-
() => editor.getActiveStyles().backgroundColor || "default",
354-
);
355-
356-
// Updates the active text and background colors when the editor content or
357-
// selection changes.
358-
useEditorContentOrSelectionChange(() => {
359-
const activeStyles = editor.getActiveStyles();
360-
361-
setActiveTextColor(activeStyles.textColor || "default");
362-
setActiveBackgroundColor(activeStyles.backgroundColor || "default");
363-
}, editor);
326+
const activeTextColor = useEditorState({
327+
editor,
328+
selector: ({ editor }) => editor.getActiveStyles().textColor || "default",
329+
});
330+
const activeBackgroundColor = useEditorState({
331+
editor,
332+
selector: ({ editor }) =>
333+
editor.getActiveStyles().backgroundColor || "default",
334+
});
364335

365336
// Handles opening and closing the color menu.
366337
const onClick = useCallback(

examples/03-ui-components/13-custom-ui/src/MUISideMenu.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {} from "@blocknote/core";
2-
import { SideMenu } from "@blocknote/core/extensions";
2+
import { SideMenuExtension } from "@blocknote/core/extensions";
33
import {
44
SideMenuProps,
55
useBlockNoteEditor,
@@ -25,7 +25,7 @@ function MUIRemoveBlockItem(
2525
props: SideMenuProps & { closeDragHandleMenu: () => void },
2626
) {
2727
const editor = useBlockNoteEditor();
28-
const sideMenu = useExtension(SideMenu, { editor });
28+
const sideMenu = useExtension(SideMenuExtension, { editor });
2929
// Deletes the block next to the side menu.
3030
const onClick = useCallback(() => {
3131
sideMenu.unfreezeMenu();
@@ -82,7 +82,7 @@ function MUIDragHandleButton(props: SideMenuProps) {
8282
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
8383

8484
const editor = useBlockNoteEditor();
85-
const sideMenu = useExtension(SideMenu, { editor });
85+
const sideMenu = useExtension(SideMenuExtension, { editor });
8686
// Handles opening and closing the drag handle menu.
8787
const onClick = useCallback(
8888
(event: MouseEvent<HTMLButtonElement>) => {
@@ -130,7 +130,7 @@ function MUISideMenu(props: SideMenuProps & { children: ReactNode }) {
130130
// Since the side menu is positioned by the top-left corner of a block, we
131131
// manually set its height based on the hovered block so that it's vertically
132132
// centered.
133-
const sideMenuHeight = useExtensionState(SideMenu, {
133+
const sideMenuHeight = useExtensionState(SideMenuExtension, {
134134
selector: (state) => {
135135
// TODO this feels like a hack
136136
if (state && state.block.type === "heading") {

examples/07-collaboration/05-comments/src/App.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import {
4+
CommentsExtension,
45
DefaultThreadStoreAuth,
56
YjsThreadStore,
67
} from "@blocknote/core/comments";
@@ -74,10 +75,7 @@ function Document() {
7475
// setup the editor with comments and collaboration
7576
const editor = useCreateBlockNote(
7677
{
77-
resolveUsers,
78-
comments: {
79-
threadStore,
80-
},
78+
extensions: [CommentsExtension({ threadStore, resolveUsers })],
8179
collaboration: {
8280
provider,
8381
fragment: doc.getXmlFragment("blocknote"),

examples/07-collaboration/06-comments-with-sidebar/src/App.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {
44
DefaultThreadStoreAuth,
55
YjsThreadStore,
6+
CommentsExtension,
67
} from "@blocknote/core/comments";
78
import { BlockNoteView } from "@blocknote/mantine";
89
import "@blocknote/mantine/style.css";
@@ -77,15 +78,12 @@ export default function App() {
7778
// setup the editor with comments and collaboration
7879
const editor = useCreateBlockNote(
7980
{
80-
resolveUsers,
81-
comments: {
82-
threadStore,
83-
},
8481
collaboration: {
8582
provider,
8683
fragment: doc.getXmlFragment("blocknote"),
8784
user: { color: getRandomColor(), name: activeUser.username },
8885
},
86+
extensions: [CommentsExtension({ threadStore, resolveUsers })],
8987
},
9088
[activeUser, threadStore],
9189
);

examples/07-collaboration/08-forking/src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "@blocknote/core/fonts/inter.css";
22
import {} from "@blocknote/core";
3-
import { ForkYDoc } from "@blocknote/core/extensions";
3+
import { ForkYDocExtension } from "@blocknote/core/extensions";
44
import {
55
useCreateBlockNote,
66
useExtension,
@@ -34,8 +34,8 @@ export default function App() {
3434
},
3535
},
3636
});
37-
const forkYDocPlugin = useExtension(ForkYDoc, { editor });
38-
const isForked = useExtensionState(ForkYDoc, {
37+
const forkYDocPlugin = useExtension(ForkYDocExtension, { editor });
38+
const isForked = useExtensionState(ForkYDocExtension, {
3939
editor,
4040
selector: (state) => state.isForked,
4141
});

examples/09-ai/01-minimal/.bnexample.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"dependencies": {
77
"@blocknote/xl-ai": "latest",
88
"@mantine/core": "^8.3.4",
9-
"ai": "^5.0.45",
10-
"zustand": "^5.0.3"
9+
"ai": "^5.0.45"
1110
}
1211
}

examples/09-ai/02-playground/.bnexample.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"dependencies": {
77
"@blocknote/xl-ai": "latest",
88
"@mantine/core": "^8.3.4",
9-
"ai": "^5.0.45",
10-
"zustand": "^5.0.3"
9+
"ai": "^5.0.45"
1110
}
1211
}

0 commit comments

Comments
 (0)