4
4
BlockNoteEditor ,
5
5
BlockSchema ,
6
6
getDraggableBlockFromCoords ,
7
+ PartialBlock ,
7
8
} from "../.." ;
8
9
import { EventEmitter } from "../../shared/EventEmitter" ;
9
10
import { Block } from "../Blocks/api/blockTypes" ;
@@ -102,7 +103,9 @@ export class TableHandlesView<BSchema extends BlockSchema>
102
103
} ;
103
104
104
105
pmView . dom . addEventListener ( "mousemove" , this . mouseMoveHandler ) ;
106
+
105
107
document . addEventListener ( "dragover" , this . dragOverHandler ) ;
108
+ document . addEventListener ( "drop" , this . dropHandler ) ;
106
109
107
110
document . addEventListener ( "scroll" , this . scrollHandler ) ;
108
111
}
@@ -278,6 +281,38 @@ export class TableHandlesView<BSchema extends BlockSchema>
278
281
}
279
282
} ;
280
283
284
+ dropHandler = ( event : DragEvent ) => {
285
+ if ( this . state === undefined || this . state . draggingState === undefined ) {
286
+ return ;
287
+ }
288
+
289
+ event . preventDefault ( ) ;
290
+
291
+ const rows = this . state . block . content . rows ;
292
+
293
+ if ( this . state . draggingState . draggedCellOrientation === "row" ) {
294
+ const rowToMove = rows [ this . state . draggingState . originalIndex ] ;
295
+ rows . splice ( this . state . draggingState . originalIndex , 1 ) ;
296
+ rows . splice ( this . state . rowIndex , 0 , rowToMove ) ;
297
+ } else {
298
+ const cellsToMove = rows . map (
299
+ ( row ) => row . cells [ this . state ! . draggingState ! . originalIndex ]
300
+ ) ;
301
+ rows . forEach ( ( row , rowIndex ) => {
302
+ row . cells . splice ( this . state ! . draggingState ! . originalIndex , 1 ) ;
303
+ row . cells . splice ( this . state ! . colIndex , 0 , cellsToMove [ rowIndex ] ) ;
304
+ } ) ;
305
+ }
306
+
307
+ this . editor . updateBlock ( this . state . block , {
308
+ type : "table" ,
309
+ content : {
310
+ type : "tableContent" ,
311
+ rows : rows ,
312
+ } ,
313
+ } as PartialBlock < BSchema > ) ;
314
+ } ;
315
+
281
316
scrollHandler = ( ) => {
282
317
if ( this . state ?. show ) {
283
318
const tableElement = document . querySelector (
@@ -297,7 +332,9 @@ export class TableHandlesView<BSchema extends BlockSchema>
297
332
298
333
destroy ( ) {
299
334
this . pmView . dom . removeEventListener ( "mousedown" , this . mouseMoveHandler ) ;
335
+
300
336
document . removeEventListener ( "dragover" , this . dragOverHandler ) ;
337
+ document . removeEventListener ( "drop" , this . dropHandler ) ;
301
338
302
339
document . removeEventListener ( "scroll" , this . scrollHandler ) ;
303
340
}
0 commit comments