1
1
import { Fragment , NodeType , Node as PMNode , Slice } from "prosemirror-model" ;
2
- import { EditorState } from "prosemirror-state" ;
2
+ import { Transaction } from "prosemirror-state" ;
3
3
4
4
import { ReplaceStep } from "prosemirror-transform" ;
5
5
import { Block , PartialBlock } from "../../../../blocks/defaultBlocks.js" ;
@@ -34,33 +34,33 @@ export const updateBlockCommand =
34
34
block : PartialBlock < BSchema , I , S >
35
35
) =>
36
36
( {
37
- state ,
37
+ tr ,
38
38
dispatch,
39
39
} : {
40
- state : EditorState ;
41
- dispatch : ( ( args ?: any ) => any ) | undefined ;
40
+ tr : Transaction ;
41
+ dispatch : ( ( ) => void ) | undefined ;
42
42
} ) => {
43
43
const blockInfo = getBlockInfoFromResolvedPos (
44
- state . doc . resolve ( posBeforeBlock )
44
+ tr . doc . resolve ( posBeforeBlock )
45
45
) ;
46
46
47
47
if ( dispatch ) {
48
48
// Adds blockGroup node with child blocks if necessary.
49
49
50
- const oldNodeType = state . schema . nodes [ blockInfo . blockNoteType ] ;
50
+ const oldNodeType = editor . pmSchema . nodes [ blockInfo . blockNoteType ] ;
51
51
const newNodeType =
52
- state . schema . nodes [ block . type || blockInfo . blockNoteType ] ;
52
+ editor . pmSchema . nodes [ block . type || blockInfo . blockNoteType ] ;
53
53
const newBnBlockNodeType = newNodeType . isInGroup ( "bnBlock" )
54
54
? newNodeType
55
- : state . schema . nodes [ "blockContainer" ] ;
55
+ : editor . pmSchema . nodes [ "blockContainer" ] ;
56
56
57
57
if ( blockInfo . isBlockContainer && newNodeType . isInGroup ( "blockContent" ) ) {
58
- updateChildren ( block , state , editor , blockInfo ) ;
58
+ updateChildren ( block , tr , editor , blockInfo ) ;
59
59
// The code below determines the new content of the block.
60
60
// or "keep" to keep as-is
61
61
updateBlockContentNode (
62
62
block ,
63
- state ,
63
+ tr ,
64
64
editor ,
65
65
oldNodeType ,
66
66
newNodeType ,
@@ -70,7 +70,7 @@ export const updateBlockCommand =
70
70
! blockInfo . isBlockContainer &&
71
71
newNodeType . isInGroup ( "bnBlock" )
72
72
) {
73
- updateChildren ( block , state , editor , blockInfo ) ;
73
+ updateChildren ( block , tr , editor , blockInfo ) ;
74
74
// old node was a bnBlock type (like column or columnList) and new block as well
75
75
// No op, we just update the bnBlock below (at end of function) and have already updated the children
76
76
} else {
@@ -88,15 +88,15 @@ export const updateBlockCommand =
88
88
editor . schema . styleSchema ,
89
89
editor . blockCache
90
90
) ;
91
- state . tr . replaceWith (
91
+ tr . replaceWith (
92
92
blockInfo . bnBlock . beforePos ,
93
93
blockInfo . bnBlock . afterPos ,
94
94
blockToNode (
95
95
{
96
96
children : existingBlock . children , // if no children are passed in, use existing children
97
97
...block ,
98
98
} ,
99
- state . schema ,
99
+ editor . pmSchema ,
100
100
editor . schema . styleSchema
101
101
)
102
102
) ;
@@ -106,7 +106,7 @@ export const updateBlockCommand =
106
106
107
107
// Adds all provided props as attributes to the parent blockContainer node too, and also preserves existing
108
108
// attributes.
109
- state . tr . setNodeMarkup ( blockInfo . bnBlock . beforePos , newBnBlockNodeType , {
109
+ tr . setNodeMarkup ( blockInfo . bnBlock . beforePos , newBnBlockNodeType , {
110
110
...blockInfo . bnBlock . node . attrs ,
111
111
...block . props ,
112
112
} ) ;
@@ -121,7 +121,7 @@ function updateBlockContentNode<
121
121
S extends StyleSchema
122
122
> (
123
123
block : PartialBlock < BSchema , I , S > ,
124
- state : EditorState ,
124
+ tr : Transaction ,
125
125
editor : BlockNoteEditor < BSchema , I , S > ,
126
126
oldNodeType : NodeType ,
127
127
newNodeType : NodeType ,
@@ -140,7 +140,7 @@ function updateBlockContentNode<
140
140
// Adds a single text node with no marks to the content.
141
141
content = inlineContentToNodes (
142
142
[ block . content ] ,
143
- state . schema ,
143
+ editor . pmSchema ,
144
144
editor . schema . styleSchema ,
145
145
newNodeType . name
146
146
) ;
@@ -149,14 +149,14 @@ function updateBlockContentNode<
149
149
// for each InlineContent object.
150
150
content = inlineContentToNodes (
151
151
block . content ,
152
- state . schema ,
152
+ editor . pmSchema ,
153
153
editor . schema . styleSchema ,
154
154
newNodeType . name
155
155
) ;
156
156
} else if ( block . content . type === "tableContent" ) {
157
157
content = tableContentToNodes (
158
158
block . content ,
159
- state . schema ,
159
+ editor . pmSchema ,
160
160
editor . schema . styleSchema
161
161
) ;
162
162
} else {
@@ -186,9 +186,9 @@ function updateBlockContentNode<
186
186
// content is being replaced or not.
187
187
if ( content === "keep" ) {
188
188
// use setNodeMarkup to only update the type and attributes
189
- state . tr . setNodeMarkup (
189
+ tr . setNodeMarkup (
190
190
blockInfo . blockContent . beforePos ,
191
- block . type === undefined ? undefined : state . schema . nodes [ block . type ] ,
191
+ block . type === undefined ? undefined : editor . pmSchema . nodes [ block . type ] ,
192
192
{
193
193
...blockInfo . blockContent . node . attrs ,
194
194
...block . props ,
@@ -198,7 +198,7 @@ function updateBlockContentNode<
198
198
// use replaceWith to replace the content and the block itself
199
199
// also reset the selection since replacing the block content
200
200
// sets it to the next block.
201
- state . tr . replaceWith (
201
+ tr . replaceWith (
202
202
blockInfo . blockContent . beforePos ,
203
203
blockInfo . blockContent . afterPos ,
204
204
newNodeType . createChecked (
@@ -218,21 +218,21 @@ function updateChildren<
218
218
S extends StyleSchema
219
219
> (
220
220
block : PartialBlock < BSchema , I , S > ,
221
- state : EditorState ,
221
+ tr : Transaction ,
222
222
editor : BlockNoteEditor < BSchema , I , S > ,
223
223
blockInfo : BlockInfo
224
224
) {
225
225
if ( block . children !== undefined && block . children . length > 0 ) {
226
226
const childNodes = block . children . map ( ( child ) => {
227
- return blockToNode ( child , state . schema , editor . schema . styleSchema ) ;
227
+ return blockToNode ( child , editor . pmSchema , editor . schema . styleSchema ) ;
228
228
} ) ;
229
229
230
230
// Checks if a blockGroup node already exists.
231
231
if ( blockInfo . childContainer ) {
232
232
// Replaces all child nodes in the existing blockGroup with the ones created earlier.
233
233
234
234
// use a replacestep to avoid the fitting algorithm
235
- state . tr . step (
235
+ tr . step (
236
236
new ReplaceStep (
237
237
blockInfo . childContainer . beforePos + 1 ,
238
238
blockInfo . childContainer . afterPos - 1 ,
@@ -244,9 +244,9 @@ function updateChildren<
244
244
throw new Error ( "impossible" ) ;
245
245
}
246
246
// Inserts a new blockGroup containing the child nodes created earlier.
247
- state . tr . insert (
247
+ tr . insert (
248
248
blockInfo . blockContent . afterPos ,
249
- state . schema . nodes [ "blockGroup" ] . createChecked ( { } , childNodes )
249
+ editor . pmSchema . nodes [ "blockGroup" ] . createChecked ( { } , childNodes )
250
250
) ;
251
251
}
252
252
}
@@ -261,34 +261,38 @@ export function updateBlock<
261
261
blockToUpdate : BlockIdentifier ,
262
262
update : PartialBlock < BSchema , I , S >
263
263
) : Block < BSchema , I , S > {
264
- const ttEditor = editor . _tiptapEditor ;
265
-
266
264
const id =
267
265
typeof blockToUpdate === "string" ? blockToUpdate : blockToUpdate . id ;
266
+ return editor . transact ( ( ) => {
267
+ const tr = editor . transaction ;
268
+ const posInfo = getNodeById ( id , tr . doc ) ;
269
+ if ( ! posInfo ) {
270
+ throw new Error ( `Block with ID ${ id } not found` ) ;
271
+ }
268
272
269
- const posInfo = getNodeById ( id , ttEditor . state . doc ) ;
270
- if ( ! posInfo ) {
271
- throw new Error ( `Block with ID ${ id } not found` ) ;
272
- }
273
-
274
- ttEditor . commands . command ( ( { state, dispatch } ) => {
275
273
updateBlockCommand (
276
274
editor ,
277
275
posInfo . posBeforeNode ,
278
276
update
279
- ) ( { state, dispatch } ) ;
280
- return true ;
281
- } ) ;
277
+ ) ( {
278
+ tr,
279
+ dispatch : ( ) => {
280
+ // no-op
281
+ } ,
282
+ } ) ;
283
+ // Actually dispatch that transaction
284
+ editor . dispatch ( tr ) ;
282
285
283
- const blockContainerNode = ttEditor . state . doc
284
- . resolve ( posInfo . posBeforeNode + 1 ) // TODO: clean?
285
- . node ( ) ;
286
+ const blockContainerNode = tr . doc
287
+ . resolve ( posInfo . posBeforeNode + 1 ) // TODO: clean?
288
+ . node ( ) ;
286
289
287
- return nodeToBlock (
288
- blockContainerNode ,
289
- editor . schema . blockSchema ,
290
- editor . schema . inlineContentSchema ,
291
- editor . schema . styleSchema ,
292
- editor . blockCache
293
- ) ;
290
+ return nodeToBlock (
291
+ blockContainerNode ,
292
+ editor . schema . blockSchema ,
293
+ editor . schema . inlineContentSchema ,
294
+ editor . schema . styleSchema ,
295
+ editor . blockCache
296
+ ) ;
297
+ } ) ;
294
298
}
0 commit comments