@@ -3,20 +3,26 @@ import { Editor, EditorOptions } from "@tiptap/core";
3
3
import { getBlockNoteExtensions } from "./BlockNoteExtensions" ;
4
4
import styles from "./editor.module.css" ;
5
5
import { BubbleMenuFactory } from "./extensions/BubbleMenu/BubbleMenuFactoryTypes" ;
6
- import { HyperlinkHoverMenuFactory } from "./extensions/Hyperlinks/HyperlinkMenuFactoryTypes" ;
7
- import { SuggestionsMenuFactory } from "./shared/plugins/suggestion/SuggestionsMenuFactoryTypes" ;
8
- import rootStyles from "./root.module.css" ;
6
+ import {
7
+ AddBlockButtonFactory ,
8
+ DragHandleFactory ,
9
+ DragHandleMenuFactory ,
10
+ } from "./extensions/DraggableBlocks/DragMenuFactoryTypes" ;
11
+ import { HyperlinkMenuFactory } from "./extensions/Hyperlinks/HyperlinkMenuFactoryTypes" ;
9
12
import { SuggestionItem } from "./shared/plugins/suggestion/SuggestionItem" ;
13
+ import { SuggestionsMenuFactory } from "./shared/plugins/suggestion/SuggestionsMenuFactoryTypes" ;
10
14
11
- type BlockNoteEditorOptions = EditorOptions & {
15
+ export type BlockNoteEditorOptions = EditorOptions & {
12
16
enableBlockNoteExtensions : boolean ;
13
17
disableHistoryExtension : boolean ;
14
- } ;
15
-
16
- export type MenuFactories = {
17
- bubbleMenuFactory : BubbleMenuFactory ;
18
- hyperlinkMenuFactory : HyperlinkHoverMenuFactory ;
19
- suggestionsMenuFactory : SuggestionsMenuFactory < SuggestionItem > ;
18
+ uiFactories : {
19
+ bubbleMenuFactory : BubbleMenuFactory ;
20
+ hyperlinkMenuFactory : HyperlinkMenuFactory ;
21
+ suggestionsMenuFactory : SuggestionsMenuFactory < SuggestionItem > ;
22
+ addBlockButtonFactory : AddBlockButtonFactory ;
23
+ dragHandleFactory : DragHandleFactory ;
24
+ dragHandleMenuFactory : DragHandleMenuFactory ;
25
+ } ;
20
26
} ;
21
27
22
28
const blockNoteExtensions = getBlockNoteExtensions ( ) ;
@@ -27,55 +33,78 @@ const blockNoteOptions = {
27
33
enableCoreExtensions : false ,
28
34
} ;
29
35
30
- export const mountBlockNoteEditor = (
31
- menuFactories : MenuFactories ,
32
- options : Partial < BlockNoteEditorOptions > = { }
33
- ) => {
34
- let extensions = options . disableHistoryExtension
35
- ? blockNoteExtensions . filter ( ( e ) => e . name !== "history" )
36
- : blockNoteExtensions ;
36
+ export class BlockNoteEditor {
37
+ public readonly tiptapEditor : Editor & { contentComponent : any } ;
37
38
38
- // TODO: review
39
- extensions = extensions . map ( ( extension ) => {
40
- if ( extension . name === "BubbleMenuExtension" ) {
41
- return extension . configure ( {
42
- bubbleMenuFactory : menuFactories . bubbleMenuFactory ,
43
- } ) ;
44
- }
39
+ constructor ( options : Partial < BlockNoteEditorOptions > = { } ) {
40
+ let extensions = options . disableHistoryExtension
41
+ ? blockNoteExtensions . filter ( ( e ) => e . name !== "history" )
42
+ : blockNoteExtensions ;
45
43
46
- if ( extension . name === "link" ) {
47
- return extension . configure ( {
48
- hyperlinkMenuFactory : menuFactories . hyperlinkMenuFactory ,
49
- } ) ;
50
- }
44
+ // TODO: review
45
+ extensions = extensions . map ( ( extension ) => {
46
+ if (
47
+ extension . name === "BubbleMenuExtension" &&
48
+ options . uiFactories ?. bubbleMenuFactory
49
+ ) {
50
+ return extension . configure ( {
51
+ bubbleMenuFactory : options . uiFactories . bubbleMenuFactory ,
52
+ } ) ;
53
+ }
51
54
52
- if ( extension . name === "slash-command" ) {
53
- return extension . configure ( {
54
- suggestionsMenuFactory : menuFactories . suggestionsMenuFactory ,
55
- } ) ;
56
- }
55
+ if (
56
+ extension . name === "link" &&
57
+ options . uiFactories ?. hyperlinkMenuFactory
58
+ ) {
59
+ return extension . configure ( {
60
+ hyperlinkMenuFactory : options . uiFactories . hyperlinkMenuFactory ,
61
+ } ) ;
62
+ }
57
63
58
- return extension ;
59
- } ) ;
64
+ if (
65
+ extension . name === "slash-command" &&
66
+ options . uiFactories ?. suggestionsMenuFactory
67
+ ) {
68
+ return extension . configure ( {
69
+ suggestionsMenuFactory : options . uiFactories . suggestionsMenuFactory ,
70
+ } ) ;
71
+ }
60
72
61
- const tiptapOptions = {
62
- ...blockNoteOptions ,
63
- ...options ,
64
- extensions :
65
- options . enableBlockNoteExtensions === false
66
- ? options . extensions
67
- : [ ...( options . extensions || [ ] ) , ...extensions ] ,
68
- editorProps : {
69
- attributes : {
70
- ...( options . editorProps ?. attributes || { } ) ,
71
- class : [
72
- styles . bnEditor ,
73
- rootStyles . bnRoot ,
74
- ( options . editorProps ?. attributes as any ) ?. class || "" ,
75
- ] . join ( " " ) ,
73
+ if (
74
+ extension . name === "DraggableBlocksExtension" &&
75
+ options . uiFactories
76
+ ) {
77
+ return extension . configure ( {
78
+ addBlockButtonFactory : options . uiFactories . addBlockButtonFactory ,
79
+ dragHandleFactory : options . uiFactories . dragHandleFactory ,
80
+ dragHandleMenuFactory : options . uiFactories . dragHandleMenuFactory ,
81
+ } ) ;
82
+ }
83
+
84
+ return extension ;
85
+ } ) ;
86
+
87
+ const tiptapOptions = {
88
+ ...blockNoteOptions ,
89
+ ...options ,
90
+ extensions :
91
+ options . enableBlockNoteExtensions === false
92
+ ? options . extensions
93
+ : [ ...( options . extensions || [ ] ) , ...extensions ] ,
94
+ editorProps : {
95
+ attributes : {
96
+ ...( options . editorProps ?. attributes || { } ) ,
97
+ class : [
98
+ styles . bnEditor ,
99
+ styles . bnRoot ,
100
+ ( options . editorProps ?. attributes as any ) ?. class || "" ,
101
+ ] . join ( " " ) ,
102
+ } ,
76
103
} ,
77
- } ,
78
- } ;
104
+ } ;
79
105
80
- return new Editor ( tiptapOptions ) ;
81
- } ;
106
+ this . tiptapEditor = new Editor ( tiptapOptions ) as Editor & {
107
+ contentComponent : any ;
108
+ } ;
109
+ }
110
+ }
0 commit comments