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" ;
211import {
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
1726type 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+
1987export 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 } ) ;
0 commit comments