@@ -4,8 +4,6 @@ import { Editor as TiptapEditor } from "@tiptap/core";
4
4
5
5
import { Node } from "@tiptap/pm/model" ;
6
6
7
- import { EditorView } from "@tiptap/pm/view" ;
8
-
9
7
import { EditorState , Transaction } from "@tiptap/pm/state" ;
10
8
import { blockToNode } from "../api/nodeConversions/blockToNode.js" ;
11
9
import { PartialBlock } from "../blocks/defaultBlocks.js" ;
@@ -22,46 +20,20 @@ export type BlockNoteTipTapEditorOptions = Partial<
22
20
* the creation of the view from the constructor.
23
21
*/
24
22
export class BlockNoteTipTapEditor extends TiptapEditor {
25
- private _state : EditorState ;
26
-
27
23
public static create = (
28
24
options : BlockNoteTipTapEditorOptions ,
29
25
styleSchema : StyleSchema
30
26
) => {
31
- // because we separate the constructor from the creation of the view,
32
- // we need to patch setTimeout to prevent this code from having any effect:
33
- // https://github.com/ueberdosis/tiptap/blob/45bac803283446795ad1b03f43d3746fa54a68ff/packages/core/src/Editor.ts#L117
34
- const oldSetTimeout = globalThis ?. window ?. setTimeout ;
35
- if ( typeof globalThis ?. window ?. setTimeout !== "undefined" ) {
36
- globalThis . window . setTimeout = ( ( ) => {
37
- return 0 ;
38
- } ) as any ;
39
- }
40
- try {
41
- return new BlockNoteTipTapEditor ( options , styleSchema ) ;
42
- } finally {
43
- if ( oldSetTimeout ) {
44
- globalThis . window . setTimeout = oldSetTimeout ;
45
- }
46
- }
27
+ return new BlockNoteTipTapEditor ( options , styleSchema ) ;
47
28
} ;
48
29
49
30
protected constructor (
50
31
options : BlockNoteTipTapEditorOptions ,
51
32
styleSchema : StyleSchema
52
33
) {
53
- // possible fix for next.js server side rendering
54
- // const d = globalThis.document;
55
- // const w = globalThis.window;
56
- // if (!globalThis.document) {
57
- // globalThis.document = {
58
- // createElement: () => {},
59
- // };
60
- // }
61
-
62
34
// options.injectCSS = false
63
35
64
- super ( { ...options , content : undefined } ) ;
36
+ super ( { ...options , content : undefined , element : null } ) ;
65
37
// try {
66
38
// globalThis.window = w;
67
39
// } catch(e) {}
@@ -120,100 +92,16 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
120
92
) ;
121
93
}
122
94
123
- // Create state immediately, so that it's available independently from the View,
124
- // the way Prosemirror "intends it to be". This also makes sure that we can access
125
- // the state before the view is created / mounted.
126
- this . _state = EditorState . create ( {
127
- doc,
128
- schema : this . schema ,
129
- // selection: selection || undefined,
130
- } ) ;
131
- }
132
-
133
- get state ( ) {
134
- if ( this . view ) {
135
- this . _state = this . view . state ;
136
- }
137
- return this . _state ;
95
+ // Leverage the fact that we know the view is not created yet, and quickly set the initial state
96
+ this . view . updateState (
97
+ EditorState . create ( {
98
+ doc,
99
+ schema : this . schema ,
100
+ } )
101
+ ) ;
138
102
}
139
103
140
104
dispatch ( tr : Transaction ) {
141
- if ( this . view ) {
142
- this . view . dispatch ( tr ) ;
143
- } else {
144
- // before view has been initialized
145
- this . _state = this . state . apply ( tr ) ;
146
- }
147
- }
148
-
149
- /**
150
- * Replace the default `createView` method with a custom one - which we call on mount
151
- */
152
- private createViewAlternative ( contentComponent ?: any ) {
153
- ( this as any ) . contentComponent = contentComponent ;
154
-
155
- const markViews : any = { } ;
156
- this . extensionManager . extensions . forEach ( ( extension ) => {
157
- if ( extension . type === "mark" && extension . config . addMarkView ) {
158
- // Note: migrate to using `addMarkView` from tiptap as soon as this lands
159
- // (currently tiptap doesn't support markviews)
160
- markViews [ extension . name ] = extension . config . addMarkView ;
161
- }
162
- } ) ;
163
-
164
- this . view = new EditorView (
165
- { mount : this . options . element as any } , // use mount option so that we reuse the existing element instead of creating a new one
166
- {
167
- ...this . options . editorProps ,
168
- // @ts -ignore
169
- dispatchTransaction : this . dispatchTransaction . bind ( this ) ,
170
- state : this . state ,
171
- markViews,
172
- }
173
- ) ;
174
-
175
- // `editor.view` is not yet available at this time.
176
- // Therefore we will add all plugins and node views directly afterwards.
177
- const newState = this . state . reconfigure ( {
178
- plugins : this . extensionManager . plugins ,
179
- } ) ;
180
-
181
- this . view . updateState ( newState ) ;
182
-
183
- this . createNodeViews ( ) ;
184
-
185
- // emit the created event, call here manually because we blocked the default call in the constructor
186
- // (https://github.com/ueberdosis/tiptap/blob/45bac803283446795ad1b03f43d3746fa54a68ff/packages/core/src/Editor.ts#L117)
187
- this . commands . focus (
188
- this . options . autofocus ||
189
- this . options . element . getAttribute ( "data-bn-autofocus" ) === "true" ,
190
- { scrollIntoView : false }
191
- ) ;
192
- this . emit ( "create" , { editor : this } ) ;
193
- this . isInitialized = true ;
105
+ this . view . dispatch ( tr ) ;
194
106
}
195
-
196
- /**
197
- * Mounts / unmounts the editor to a dom element
198
- *
199
- * @param element DOM element to mount to, ur null / undefined to destroy
200
- */
201
- public mount = ( element ?: HTMLElement | null , contentComponent ?: any ) => {
202
- if ( ! element ) {
203
- this . destroy ( ) ;
204
- } else {
205
- this . options . element = element ;
206
- this . createViewAlternative ( contentComponent ) ;
207
- }
208
- } ;
209
107
}
210
-
211
- ( BlockNoteTipTapEditor . prototype as any ) . createView = function ( ) {
212
- // no-op
213
- // Disable default call to `createView` in the Editor constructor.
214
- // We should call `createView` manually only when a DOM element is available
215
-
216
- // additional fix because onPaste and onDrop depend on installing plugins in constructor which we don't support
217
- // (note: can probably be removed after tiptap upgrade fixed in 2.8.0)
218
- this . options . onPaste = this . options . onDrop = undefined ;
219
- } ;
0 commit comments