Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 94929dd

Browse files
committed
Merge branch 'improve-dnd'
2 parents 98df444 + d4ead67 commit 94929dd

File tree

5 files changed

+55
-73
lines changed

5 files changed

+55
-73
lines changed

src/cdm/HeaderModel.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Header, Table } from "@tanstack/react-table";
1+
import { ColumnOrderState, Header, Table } from "@tanstack/react-table";
22
import { DatabaseHeaderProps, RowDataType, TableColumn } from "cdm/FolderModel";
33

44
export type HeaderMenuProps = {
@@ -15,9 +15,6 @@ export type HeaderMenuProps = {
1515
export type TableHeaderProps = {
1616
table: Table<RowDataType>;
1717
header: Header<RowDataType, TableColumn>;
18-
findColumn: (id: string) => {
19-
findedColumn: TableColumn;
20-
index: number;
21-
};
18+
reorderColumn: (draggedColumnId: string, targetColumnId: string, columnOrder: string[]) => ColumnOrderState;
2219
headerIndex: number;
2320
};

src/components/DefaultCell.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export default function DefaultCell(
6161
/** Checkbox option */
6262
case InputType.CHECKBOX:
6363
return <CheckboxCell defaultCell={defaultCell} />;
64+
65+
/** New column option */
6466
case InputType.NEW_COLUMN:
6567
// Do nothing
6668
break;

src/components/Table.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,18 @@ export function Table(tableData: TableDataType) {
103103
columnsInfo.getValueOfAllColumnsAsociatedWith("id")
104104
);
105105

106-
const findColumn = React.useCallback(
107-
(id: string) => {
108-
const findedColumn = columns.find((c) => c.id === id);
109-
return {
110-
findedColumn,
111-
index: columns.indexOf(findedColumn),
112-
};
113-
},
114-
[columns]
115-
);
106+
const reorderColumn = (
107+
draggedColumnId: string,
108+
targetColumnId: string,
109+
columnOrder: string[]
110+
): ColumnOrderState => {
111+
columnOrder.splice(
112+
columnOrder.indexOf(targetColumnId),
113+
0,
114+
columnOrder.splice(columnOrder.indexOf(draggedColumnId), 1)[0] as string
115+
);
116+
return [...columnOrder];
117+
};
116118
// Niveling number of columns
117119
if (columnOrder.length !== columns.length) {
118120
setColumnOrder(columnsInfo.getValueOfAllColumnsAsociatedWith("id"));
@@ -352,7 +354,7 @@ export function Table(tableData: TableDataType) {
352354
key={`${header.id}-${headerIndex}`}
353355
table={table}
354356
header={header}
355-
findColumn={findColumn}
357+
reorderColumn={reorderColumn}
356358
headerIndex={headerIndex}
357359
/>
358360
)

src/components/TableHeader.tsx

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from "react";
2-
import { flexRender } from "@tanstack/react-table";
2+
import { Column, flexRender } from "@tanstack/react-table";
33
import { c } from "helpers/StylesHelper";
44
import { TableHeaderProps } from "cdm/HeaderModel";
55
import { useDrag, useDrop } from "react-dnd";
6-
import { TableColumn } from "cdm/FolderModel";
6+
import { RowDataType } from "cdm/FolderModel";
77
import { DnDConfiguration } from "helpers/Constants";
88

99
interface DragItem {
@@ -13,76 +13,57 @@ interface DragItem {
1313
}
1414

1515
export default function TableHeader(headerProps: TableHeaderProps) {
16-
const { table, header, findColumn, headerIndex } = headerProps;
17-
const { id } = header.column.columnDef as TableColumn;
18-
const { view, tableState } = table.options.meta;
16+
const { table, header, reorderColumn, headerIndex } = headerProps;
17+
const { view } = table.options.meta;
1918
const { columnOrder } = table.options.state;
20-
const columns = tableState.columns((state) => state.columns);
2119

22-
const originalIndex = React.useMemo(() => {
23-
return columnOrder.findIndex((colId) => colId === id);
24-
}, [columnOrder]);
25-
26-
//DnD
27-
const DnDref = React.useRef<HTMLDivElement>(null);
28-
29-
function moveColumn(source: number, destinationKey: string) {
30-
const { index: destIndex } = findColumn(destinationKey);
31-
const newColumnOrder = [...columnOrder];
32-
newColumnOrder.splice(destIndex, 1);
33-
newColumnOrder.splice(source, 0, columns[destIndex].id);
34-
table.setColumnOrder(newColumnOrder);
20+
function moveColumn(
21+
draggedColumnId: string,
22+
targetColumnId: string,
23+
columnOrder: string[]
24+
) {
25+
const newColumnOrder = reorderColumn(
26+
draggedColumnId,
27+
targetColumnId,
28+
columnOrder
29+
);
30+
// Save on disk
3531
view.diskConfig.reorderColumns(newColumnOrder);
32+
// Save on memory
33+
return newColumnOrder;
3634
}
3735

38-
const [{ isDragging, handlerId }, drag] = useDrag(
39-
() => ({
40-
type: DnDConfiguration.DRAG_TYPE,
41-
item: { id, originalIndex },
42-
collect: (monitor) => ({
43-
handlerId: monitor.getHandlerId(),
44-
isDragging: monitor.isDragging(),
45-
}),
46-
end: (item, monitor) => {
47-
const { id: droppedId, originalIndex } = item;
48-
const didDrop = monitor.didDrop();
49-
if (!didDrop) {
50-
moveColumn(originalIndex, droppedId);
51-
}
52-
},
53-
}),
54-
[id, originalIndex, findColumn]
55-
);
36+
const [, dropRef] = useDrop({
37+
accept: DnDConfiguration.DRAG_TYPE,
38+
drop: (draggedColumn: Column<RowDataType>) => {
39+
const newColumnOrder = moveColumn(
40+
draggedColumn.id,
41+
header.column.id,
42+
columnOrder
43+
);
44+
table.setColumnOrder(newColumnOrder);
45+
},
46+
});
5647

57-
const [, drop] = useDrop(
58-
() => ({
59-
accept: DnDConfiguration.DRAG_TYPE,
60-
hover({ id: draggedId }: DragItem) {
61-
if (draggedId !== id) {
62-
const { index: overIndex } = findColumn(id);
63-
moveColumn(overIndex, draggedId);
64-
}
65-
},
48+
const [{ isDragging }, dragRef] = useDrag({
49+
collect: (monitor) => ({
50+
isDragging: monitor.isDragging(),
6651
}),
67-
[findColumn]
68-
);
52+
item: () => header.column,
53+
type: DnDConfiguration.DRAG_TYPE,
54+
});
6955

70-
const opacity = isDragging ? 0 : 1;
71-
drag(drop(DnDref));
7256
return (
7357
<div
7458
key={`${header.id}-${headerIndex}`}
7559
className={`${c("th noselect")} header`}
7660
style={{
7761
width: header.getSize(),
78-
opacity,
62+
opacity: isDragging ? 0.5 : 1,
7963
}}
64+
ref={dropRef}
8065
>
81-
<div
82-
ref={DnDref}
83-
data-handler-id={handlerId}
84-
key={`${header.id}-${headerIndex}-dnd`}
85-
>
66+
<div ref={dragRef} key={`${header.id}-${headerIndex}-dnd`}>
8667
{header.isPlaceholder
8768
? null
8869
: flexRender(header.column.columnDef.header, header.getContext())}

src/mock/mockDataviewUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export const generateStateManager = (): StateManager => {
77
app.workspace.getMostRecentLeaf(),
88
null
99
);
10-
const stateManager = new StateManager(app, initialView, null, null, null);
10+
const stateManager = new StateManager(initialView, null, null, null);
1111
return stateManager;
1212
};

0 commit comments

Comments
 (0)