Skip to content

Commit cd2b7c6

Browse files
feat: Custom schemas for comment editors (#1976)
1 parent a92aa1c commit cd2b7c6

File tree

9 files changed

+22
-15
lines changed

9 files changed

+22
-15
lines changed

docs/content/docs/features/collaboration/comments.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ To enable comments in your editor, you need to:
1313
- provide a `resolveUsers` so BlockNote can retrieve and display user information (names and avatars).
1414
- provide a `ThreadStore` so BlockNote can store and retrieve comment threads.
1515
- enable real-time collaboration (see [Real-time collaboration](/docs/features/collaboration))
16+
- optionally provide a schema for comments and comment editors to use. If left undefined, they will use the [default comment editor schema](https://github.com/TypeCellOS/BlockNote/blob/main/packages/react/src/components/Comments/defaultCommentEditorSchema.ts). See [here](/docs/features/custom-schemas) to find out more about custom schemas.
1617

1718
```tsx
1819
const editor = useCreateBlockNote({
@@ -21,6 +22,7 @@ const editor = useCreateBlockNote({
2122
},
2223
comments: {
2324
threadStore: yourThreadStore, // see below
25+
schema: BlockNoteSchema.create(...) // optional, can be left undefined
2426
},
2527
// ...
2628
collaboration: {

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export type BlockNoteEditorOptions<
201201
* @remarks `CommentsOptions`
202202
*/
203203
comments?: {
204+
schema?: BlockNoteSchema<any, any, any>;
204205
threadStore: ThreadStore;
205206
};
206207

packages/core/src/editor/BlockNoteExtensions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import type {
5757
BlockNoteEditorOptions,
5858
SupportedExtension,
5959
} from "./BlockNoteEditor.js";
60+
import { BlockNoteSchema } from "./BlockNoteSchema.js";
6061
import { ForkYDocPlugin } from "../extensions/Collaboration/ForkYDocPlugin.js";
6162

6263
type ExtensionOptions<
@@ -92,6 +93,7 @@ type ExtensionOptions<
9293
>;
9394
tabBehavior?: "prefer-navigate-ui" | "prefer-indent";
9495
comments?: {
96+
schema?: BlockNoteSchema<any, any, any>;
9597
threadStore: ThreadStore;
9698
};
9799
pasteHandler: BlockNoteEditorOptions<any, any, any>["pasteHandler"];
@@ -156,6 +158,7 @@ export const getBlockNoteExtensions = <
156158
opts.editor,
157159
opts.comments.threadStore,
158160
CommentMark.name,
161+
opts.comments.schema,
159162
);
160163
}
161164

packages/core/src/extensions/Comments/CommentsPlugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
} from "../../comments/index.js";
1111
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
1212
import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
13+
import { BlockNoteSchema } from "../../editor/BlockNoteSchema.js";
1314
import { UserStore } from "./userstore/UserStore.js";
1415

1516
const PLUGIN_KEY = new PluginKey(`blocknote-comments`);
@@ -134,6 +135,7 @@ export class CommentsPlugin extends BlockNoteExtension {
134135
private readonly editor: BlockNoteEditor<any, any, any>,
135136
public readonly threadStore: ThreadStore,
136137
private readonly markType: string,
138+
public readonly commentEditorSchema?: BlockNoteSchema<any, any, any>,
137139
) {
138140
super();
139141

packages/react/src/components/Comments/Comment.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { useDictionary } from "../../i18n/dictionary.js";
1919
import { CommentEditor } from "./CommentEditor.js";
2020
import { EmojiPicker } from "./EmojiPicker.js";
2121
import { ReactionBadge } from "./ReactionBadge.js";
22-
import { schema } from "./schema.js";
22+
import { defaultCommentEditorSchema } from "./defaultCommentEditorSchema.js";
2323
import { useUser } from "./useUsers.js";
2424

2525
export type CommentProps = {
@@ -43,6 +43,12 @@ export const Comment = ({
4343
// TODO: if REST API becomes popular, all interactions (click handlers) should implement a loading state and error state
4444
// (or optimistic local updates)
4545

46+
const editor = useBlockNoteEditor();
47+
48+
if (!editor.comments) {
49+
throw new Error("Comments plugin not found");
50+
}
51+
4652
const dict = useDictionary();
4753

4854
const commentEditor = useCreateBlockNote(
@@ -55,7 +61,7 @@ export const Comment = ({
5561
emptyDocument: dict.placeholders.edit_comment,
5662
},
5763
},
58-
schema,
64+
schema: editor.comments.commentEditorSchema || defaultCommentEditorSchema,
5965
},
6066
[comment.body],
6167
);
@@ -64,8 +70,6 @@ export const Comment = ({
6470

6571
const [isEditing, setEditing] = useState(false);
6672

67-
const editor = useBlockNoteEditor();
68-
6973
if (!editor.comments) {
7074
throw new Error("Comments plugin not found");
7175
}

packages/react/src/components/Comments/CommentEditor.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { BlockNoteEditor } from "@blocknote/core";
22
import { FC, useCallback, useEffect, useState } from "react";
33
import { useComponentsContext } from "../../editor/ComponentsContext.js";
44
import { useEditorChange } from "../../hooks/useEditorChange.js";
5-
import { schema } from "./schema.js";
65

76
/**
87
* The CommentEditor component displays an editor for creating or editing a comment.
@@ -21,11 +20,7 @@ export const CommentEditor = (props: {
2120
isFocused: boolean;
2221
isEmpty: boolean;
2322
}>;
24-
editor: BlockNoteEditor<
25-
typeof schema.blockSchema,
26-
typeof schema.inlineContentSchema,
27-
typeof schema.styleSchema
28-
>;
23+
editor: BlockNoteEditor<any, any, any>;
2924
}) => {
3025
const [isFocused, setIsFocused] = useState(false);
3126
const [isEmpty, setIsEmpty] = useState(props.editor.isEmpty);

packages/react/src/components/Comments/FloatingComposer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js";
55
import { useCreateBlockNote } from "../../hooks/useCreateBlockNote.js";
66
import { useDictionary } from "../../i18n/dictionary.js";
77
import { CommentEditor } from "./CommentEditor.js";
8-
import { schema } from "./schema.js";
8+
import { defaultCommentEditorSchema } from "./defaultCommentEditorSchema.js";
99

1010
/**
1111
* The FloatingComposer component displays a comment editor "floating" card.
@@ -32,7 +32,7 @@ export function FloatingComposer() {
3232
emptyDocument: dict.placeholders.new_comment,
3333
},
3434
},
35-
schema,
35+
schema: editor.comments.commentEditorSchema || defaultCommentEditorSchema,
3636
});
3737

3838
return (

packages/react/src/components/Comments/Thread.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useCreateBlockNote } from "../../hooks/useCreateBlockNote.js";
88
import { useDictionary } from "../../i18n/dictionary.js";
99
import { CommentEditor } from "./CommentEditor.js";
1010
import { Comments } from "./Comments.js";
11-
import { schema } from "./schema.js";
11+
import { defaultCommentEditorSchema } from "./defaultCommentEditorSchema.js";
1212

1313
export type ThreadProps = {
1414
/**
@@ -81,7 +81,7 @@ export const Thread = ({
8181
emptyDocument: dict.placeholders.comment_reply,
8282
},
8383
},
84-
schema,
84+
schema: editor.comments.commentEditorSchema || defaultCommentEditorSchema,
8585
});
8686

8787
const onNewCommentSave = useCallback(async () => {

packages/react/src/components/Comments/schema.ts renamed to packages/react/src/components/Comments/defaultCommentEditorSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const paragraph = createBlockSpecFromStronglyTypedTiptapNode(
2020
const { textColor, backgroundColor, ...styleSpecs } = defaultStyleSpecs;
2121

2222
// the schema to use for comments
23-
export const schema = BlockNoteSchema.create({
23+
export const defaultCommentEditorSchema = BlockNoteSchema.create({
2424
blockSpecs: {
2525
paragraph,
2626
},

0 commit comments

Comments
 (0)