Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/content/docs/features/collaboration/comments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ To enable comments in your editor, you need to:
- provide a `resolveUsers` so BlockNote can retrieve and display user information (names and avatars).
- provide a `ThreadStore` so BlockNote can store and retrieve comment threads.
- enable real-time collaboration (see [Real-time collaboration](/docs/features/collaboration))
- 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.

```tsx
const editor = useCreateBlockNote({
Expand All @@ -21,6 +22,7 @@ const editor = useCreateBlockNote({
},
comments: {
threadStore: yourThreadStore, // see below
schema: BlockNoteSchema.create(...) // optional, can be left undefined
},
// ...
collaboration: {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export type BlockNoteEditorOptions<
* @remarks `CommentsOptions`
*/
comments?: {
schema?: BlockNoteSchema<any, any, any>;
threadStore: ThreadStore;
};

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/editor/BlockNoteExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import type {
BlockNoteEditorOptions,
SupportedExtension,
} from "./BlockNoteEditor.js";
import { BlockNoteSchema } from "./BlockNoteSchema.js";
import { ForkYDocPlugin } from "../extensions/Collaboration/ForkYDocPlugin.js";

type ExtensionOptions<
Expand Down Expand Up @@ -92,6 +93,7 @@ type ExtensionOptions<
>;
tabBehavior?: "prefer-navigate-ui" | "prefer-indent";
comments?: {
schema?: BlockNoteSchema<any, any, any>;
threadStore: ThreadStore;
};
pasteHandler: BlockNoteEditorOptions<any, any, any>["pasteHandler"];
Expand Down Expand Up @@ -156,6 +158,7 @@ export const getBlockNoteExtensions = <
opts.editor,
opts.comments.threadStore,
CommentMark.name,
opts.comments.schema,
);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/extensions/Comments/CommentsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from "../../comments/index.js";
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
import { BlockNoteSchema } from "../../editor/BlockNoteSchema.js";
import { UserStore } from "./userstore/UserStore.js";

const PLUGIN_KEY = new PluginKey(`blocknote-comments`);
Expand Down Expand Up @@ -134,6 +135,7 @@ export class CommentsPlugin extends BlockNoteExtension {
private readonly editor: BlockNoteEditor<any, any, any>,
public readonly threadStore: ThreadStore,
private readonly markType: string,
public readonly commentEditorSchema?: BlockNoteSchema<any, any, any>,
) {
super();

Expand Down
12 changes: 8 additions & 4 deletions packages/react/src/components/Comments/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useDictionary } from "../../i18n/dictionary.js";
import { CommentEditor } from "./CommentEditor.js";
import { EmojiPicker } from "./EmojiPicker.js";
import { ReactionBadge } from "./ReactionBadge.js";
import { schema } from "./schema.js";
import { defaultCommentEditorSchema } from "./defaultCommentEditorSchema.js";
import { useUser } from "./useUsers.js";

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

const editor = useBlockNoteEditor();

if (!editor.comments) {
throw new Error("Comments plugin not found");
}

const dict = useDictionary();

const commentEditor = useCreateBlockNote(
Expand All @@ -55,7 +61,7 @@ export const Comment = ({
emptyDocument: dict.placeholders.edit_comment,
},
},
schema,
schema: editor.comments.commentEditorSchema || defaultCommentEditorSchema,
},
[comment.body],
);
Expand All @@ -64,8 +70,6 @@ export const Comment = ({

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

const editor = useBlockNoteEditor();

if (!editor.comments) {
throw new Error("Comments plugin not found");
}
Expand Down
7 changes: 1 addition & 6 deletions packages/react/src/components/Comments/CommentEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BlockNoteEditor } from "@blocknote/core";
import { FC, useCallback, useEffect, useState } from "react";
import { useComponentsContext } from "../../editor/ComponentsContext.js";
import { useEditorChange } from "../../hooks/useEditorChange.js";
import { schema } from "./schema.js";

/**
* The CommentEditor component displays an editor for creating or editing a comment.
Expand All @@ -21,11 +20,7 @@ export const CommentEditor = (props: {
isFocused: boolean;
isEmpty: boolean;
}>;
editor: BlockNoteEditor<
typeof schema.blockSchema,
typeof schema.inlineContentSchema,
typeof schema.styleSchema
>;
editor: BlockNoteEditor<any, any, any>;
}) => {
const [isFocused, setIsFocused] = useState(false);
const [isEmpty, setIsEmpty] = useState(props.editor.isEmpty);
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/Comments/FloatingComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js";
import { useCreateBlockNote } from "../../hooks/useCreateBlockNote.js";
import { useDictionary } from "../../i18n/dictionary.js";
import { CommentEditor } from "./CommentEditor.js";
import { schema } from "./schema.js";
import { defaultCommentEditorSchema } from "./defaultCommentEditorSchema.js";

/**
* The FloatingComposer component displays a comment editor "floating" card.
Expand All @@ -32,7 +32,7 @@ export function FloatingComposer() {
emptyDocument: dict.placeholders.new_comment,
},
},
schema,
schema: editor.comments.commentEditorSchema || defaultCommentEditorSchema,
});

return (
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/Comments/Thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useCreateBlockNote } from "../../hooks/useCreateBlockNote.js";
import { useDictionary } from "../../i18n/dictionary.js";
import { CommentEditor } from "./CommentEditor.js";
import { Comments } from "./Comments.js";
import { schema } from "./schema.js";
import { defaultCommentEditorSchema } from "./defaultCommentEditorSchema.js";

export type ThreadProps = {
/**
Expand Down Expand Up @@ -81,7 +81,7 @@ export const Thread = ({
emptyDocument: dict.placeholders.comment_reply,
},
},
schema,
schema: editor.comments.commentEditorSchema || defaultCommentEditorSchema,
});

const onNewCommentSave = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const paragraph = createBlockSpecFromStronglyTypedTiptapNode(
const { textColor, backgroundColor, ...styleSpecs } = defaultStyleSpecs;

// the schema to use for comments
export const schema = BlockNoteSchema.create({
export const defaultCommentEditorSchema = BlockNoteSchema.create({
blockSpecs: {
paragraph,
},
Expand Down
Loading