Skip to content

Commit bd8b3be

Browse files
committed
Implemented table drag & drop logic
1 parent 47fd63e commit bd8b3be

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts

+37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BlockNoteEditor,
55
BlockSchema,
66
getDraggableBlockFromCoords,
7+
PartialBlock,
78
} from "../..";
89
import { EventEmitter } from "../../shared/EventEmitter";
910
import { Block } from "../Blocks/api/blockTypes";
@@ -102,7 +103,9 @@ export class TableHandlesView<BSchema extends BlockSchema>
102103
};
103104

104105
pmView.dom.addEventListener("mousemove", this.mouseMoveHandler);
106+
105107
document.addEventListener("dragover", this.dragOverHandler);
108+
document.addEventListener("drop", this.dropHandler);
106109

107110
document.addEventListener("scroll", this.scrollHandler);
108111
}
@@ -278,6 +281,38 @@ export class TableHandlesView<BSchema extends BlockSchema>
278281
}
279282
};
280283

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+
281316
scrollHandler = () => {
282317
if (this.state?.show) {
283318
const tableElement = document.querySelector(
@@ -297,7 +332,9 @@ export class TableHandlesView<BSchema extends BlockSchema>
297332

298333
destroy() {
299334
this.pmView.dom.removeEventListener("mousedown", this.mouseMoveHandler);
335+
300336
document.removeEventListener("dragover", this.dragOverHandler);
337+
document.removeEventListener("drop", this.dropHandler);
301338

302339
document.removeEventListener("scroll", this.scrollHandler);
303340
}

0 commit comments

Comments
 (0)