Skip to content

Commit 03126c5

Browse files
committed
Updated custom mentions example
1 parent d5cb006 commit 03126c5

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

examples/editor/examples/basic/App.tsx

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
1+
import {
2+
BlockNoteEditor,
3+
DefaultBlockSchema,
4+
defaultInlineContentSchema,
5+
defaultInlineContentSpecs,
6+
DefaultStyleSchema,
7+
InlineContentSchema,
8+
InlineContentSpecs,
9+
uploadToTmpFilesDotOrg_DEV_ONLY,
10+
} from "@blocknote/core";
211
import {
312
BlockNoteView,
13+
createReactInlineContentSpec,
414
DefaultSlashMenu,
515
FormattingToolbarPositioner,
6-
getDefaultReactSlashMenuItems,
716
HyperlinkToolbarPositioner,
817
ImageToolbarPositioner,
918
SideMenuPositioner,
@@ -16,8 +25,68 @@ import "@blocknote/react/style.css";
1625

1726
type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };
1827

28+
const MentionInlineContent = createReactInlineContentSpec(
29+
{
30+
type: "mention",
31+
propSchema: {
32+
user: {
33+
default: "Unknown",
34+
},
35+
},
36+
content: "none",
37+
},
38+
{
39+
render: (props) => (
40+
<span style={{ backgroundColor: "#8400ff33" }}>
41+
@{props.inlineContent.props.user}
42+
</span>
43+
),
44+
}
45+
);
46+
47+
const customInlineContentSpecs = {
48+
...defaultInlineContentSpecs,
49+
mention: MentionInlineContent,
50+
} satisfies InlineContentSpecs;
51+
const customInlineContentSchema = {
52+
...defaultInlineContentSchema,
53+
mention: MentionInlineContent.config,
54+
} satisfies InlineContentSchema;
55+
56+
async function getMentionMenuItems(query: string) {
57+
const users = ["Steve", "Bob", "Joe", "Mike"];
58+
const items = users.map((user) => ({
59+
name: user,
60+
execute: (
61+
editor: BlockNoteEditor<
62+
DefaultBlockSchema,
63+
typeof customInlineContentSchema,
64+
DefaultStyleSchema
65+
>
66+
) => {
67+
editor._tiptapEditor.commands.insertContent({
68+
type: "mention",
69+
attrs: {
70+
user: user,
71+
},
72+
});
73+
},
74+
aliases: [] as string[],
75+
}));
76+
77+
return items.filter(
78+
({ name, aliases }) =>
79+
name.toLowerCase().startsWith(query.toLowerCase()) ||
80+
(aliases &&
81+
aliases.filter((alias) =>
82+
alias.toLowerCase().startsWith(query.toLowerCase())
83+
).length !== 0)
84+
);
85+
}
86+
1987
export function App() {
2088
const editor = useBlockNote({
89+
inlineContentSpecs: customInlineContentSpecs,
2190
domAttributes: {
2291
editor: {
2392
class: "editor",
@@ -29,7 +98,7 @@ export function App() {
2998
{
3099
name: "mentions",
31100
triggerCharacter: "@",
32-
getItems: getDefaultReactSlashMenuItems,
101+
getItems: getMentionMenuItems,
33102
},
34103
],
35104
});

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ export type BlockNoteEditorOptions<
9191
extraSuggestionMenus: {
9292
name: string;
9393
triggerCharacter: string;
94-
getItems: <Item extends SuggestionItem<any, any, any>>(
95-
query: string
96-
) => Promise<Item[]>;
94+
getItems: (query: string) => Promise<SuggestionItem<any, any, any>[]>;
9795
}[];
9896

9997
/**

0 commit comments

Comments
 (0)