@fileverse-dev/fortune-react 1.1.83 → 1.1.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/components/SheetList/SheetHiddenButton.js +1 -1
- package/es/components/SheetList/index.css +3 -4
- package/es/components/SheetOverlay/ColumnHeader.js +31 -5
- package/es/components/SheetOverlay/RowHeader.js +40 -16
- package/es/components/SheetOverlay/drag_and_drop/column-helpers.d.ts +6 -0
- package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +235 -0
- package/es/components/SheetOverlay/drag_and_drop/row-helpers.d.ts +6 -0
- package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +226 -0
- package/es/components/SheetTab/index.css +13 -0
- package/es/components/SheetTab/index.js +2 -2
- package/es/components/Workbook/index.d.ts +4 -4
- package/lib/components/SheetList/SheetHiddenButton.js +1 -1
- package/lib/components/SheetList/index.css +3 -4
- package/lib/components/SheetOverlay/ColumnHeader.js +31 -5
- package/lib/components/SheetOverlay/RowHeader.js +40 -16
- package/lib/components/SheetOverlay/drag_and_drop/column-helpers.d.ts +6 -0
- package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +242 -0
- package/lib/components/SheetOverlay/drag_and_drop/row-helpers.d.ts +6 -0
- package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +233 -0
- package/lib/components/SheetTab/index.css +13 -0
- package/lib/components/SheetTab/index.js +2 -2
- package/lib/components/Workbook/index.d.ts +4 -4
- package/package.json +2 -2
|
@@ -22,7 +22,7 @@ var SheetHiddenButton = function SheetHiddenButton(_a) {
|
|
|
22
22
|
showSheet();
|
|
23
23
|
},
|
|
24
24
|
tabIndex: 0,
|
|
25
|
-
className: "fortune-sheet-hidden-button"
|
|
25
|
+
className: "fortune-sheet-hidden-button mb-2"
|
|
26
26
|
}, (sheet === null || sheet === void 0 ? void 0 : sheet.hide) === 1 ? (/*#__PURE__*/React.createElement(SVGIcon, {
|
|
27
27
|
name: "hidden",
|
|
28
28
|
width: 16,
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
.fortune-sheet-list-item-name {
|
|
21
|
-
margin-right: 15px;
|
|
21
|
+
/* margin-right: 15px; */
|
|
22
22
|
position: relative;
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -34,10 +34,9 @@
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
.fortune-sheet-hidden-button {
|
|
37
|
-
margin-right: 15px;
|
|
38
37
|
display: inline-flex;
|
|
39
|
-
position: absolute;
|
|
40
|
-
right: 0;
|
|
38
|
+
/* position: absolute;
|
|
39
|
+
right: 0; */
|
|
41
40
|
justify-content: flex-end;
|
|
42
41
|
}
|
|
43
42
|
|
|
@@ -3,6 +3,7 @@ import _ from "lodash";
|
|
|
3
3
|
import React, { useContext, useState, useRef, useCallback, useEffect, useMemo } from "react";
|
|
4
4
|
import WorkbookContext from "../../context";
|
|
5
5
|
import SVGIcon from "../SVGIcon";
|
|
6
|
+
import { useColumnDragAndDrop } from "./drag_and_drop/column-helpers";
|
|
6
7
|
var ColumnHeader = function ColumnHeader() {
|
|
7
8
|
var _a;
|
|
8
9
|
var _b = useContext(WorkbookContext),
|
|
@@ -34,6 +35,14 @@ var ColumnHeader = function ColumnHeader() {
|
|
|
34
35
|
}
|
|
35
36
|
return context.scrollLeft;
|
|
36
37
|
}, [context.visibledatacolumn, sheet === null || sheet === void 0 ? void 0 : sheet.frozen, context.scrollLeft]);
|
|
38
|
+
var selectedLocationRef = useRef(selectedLocation);
|
|
39
|
+
useEffect(function () {
|
|
40
|
+
selectedLocationRef.current = selectedLocation;
|
|
41
|
+
}, [selectedLocation]);
|
|
42
|
+
var _f = useColumnDragAndDrop(containerRef, selectedLocationRef),
|
|
43
|
+
initiateDrag = _f.initiateDrag,
|
|
44
|
+
getColIndexClicked = _f.getColIndexClicked,
|
|
45
|
+
isColDoubleClicked = _f.isColDoubleClicked;
|
|
37
46
|
var onMouseMove = useCallback(function (e) {
|
|
38
47
|
var _a;
|
|
39
48
|
if (context.luckysheet_cols_change_size) {
|
|
@@ -64,11 +73,28 @@ var ColumnHeader = function ColumnHeader() {
|
|
|
64
73
|
}]);
|
|
65
74
|
}, [context, hoverLocation.col_index, refs.globalCache.freezen]);
|
|
66
75
|
var onMouseDown = useCallback(function (e) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
if (e.button !== 0) return;
|
|
77
|
+
var targetEl = e.target;
|
|
78
|
+
if (targetEl.closest(".fortune-cols-change-size") || targetEl.closest(".fortune-cols-freeze-handle") || targetEl.closest(".header-arrow")) return;
|
|
79
|
+
var headerEl = containerRef.current;
|
|
80
|
+
if (!headerEl) return;
|
|
81
|
+
var clickedColIndex = getColIndexClicked(e.pageX, headerEl);
|
|
82
|
+
if (clickedColIndex < 0) return;
|
|
83
|
+
if (isColDoubleClicked(clickedColIndex)) {
|
|
84
|
+
setContext(function (draft) {
|
|
85
|
+
draft.luckysheet_scroll_status = true;
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
var nativeEvent_1 = e.nativeEvent;
|
|
89
|
+
setContext(function (draft) {
|
|
90
|
+
handleColumnHeaderMouseDown(draft, refs.globalCache, nativeEvent_1, containerRef.current, refs.cellInput.current, refs.fxInput.current);
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
e.stopPropagation();
|
|
96
|
+
initiateDrag(clickedColIndex, e.pageX);
|
|
97
|
+
}, [refs.globalCache, refs.cellInput, refs.fxInput, setContext, getColIndexClicked, isColDoubleClicked, initiateDrag]);
|
|
72
98
|
var onMouseLeave = useCallback(function () {
|
|
73
99
|
if (context.luckysheet_cols_change_size) {
|
|
74
100
|
return;
|
|
@@ -2,6 +2,7 @@ import { rowLocation, rowLocationByIndex, selectTitlesMap, selectTitlesRange, ha
|
|
|
2
2
|
import _ from "lodash";
|
|
3
3
|
import React, { useContext, useState, useRef, useCallback, useEffect, useMemo } from "react";
|
|
4
4
|
import WorkbookContext from "../../context";
|
|
5
|
+
import { useRowDragAndDrop } from "./drag_and_drop/row-helpers";
|
|
5
6
|
var RowHeader = function RowHeader() {
|
|
6
7
|
var _a;
|
|
7
8
|
var _b = useContext(WorkbookContext),
|
|
@@ -33,21 +34,25 @@ var RowHeader = function RowHeader() {
|
|
|
33
34
|
}
|
|
34
35
|
return context.scrollTop;
|
|
35
36
|
}, [context.visibledatarow, sheet === null || sheet === void 0 ? void 0 : sheet.frozen, context.scrollTop]);
|
|
37
|
+
var selectedLocationRef = useRef(selectedLocation);
|
|
38
|
+
useEffect(function () {
|
|
39
|
+
selectedLocationRef.current = selectedLocation;
|
|
40
|
+
}, [selectedLocation]);
|
|
36
41
|
var onMouseMove = useCallback(function (e) {
|
|
37
42
|
var _a;
|
|
38
|
-
if (context.luckysheet_rows_change_size)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var mouseY = e.pageY -
|
|
42
|
-
var _y = mouseY +
|
|
43
|
+
if (context.luckysheet_rows_change_size) return;
|
|
44
|
+
var container = containerRef.current;
|
|
45
|
+
if (!container) return;
|
|
46
|
+
var mouseY = e.pageY - container.getBoundingClientRect().top - window.scrollY;
|
|
47
|
+
var _y = mouseY + container.scrollTop;
|
|
43
48
|
var freeze = (_a = refs.globalCache.freezen) === null || _a === void 0 ? void 0 : _a[context.currentSheetId];
|
|
44
49
|
var _b = fixPositionOnFrozenCells(freeze, 0, _y, 0, mouseY),
|
|
45
50
|
y = _b.y,
|
|
46
51
|
inHorizontalFreeze = _b.inHorizontalFreeze;
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
row =
|
|
50
|
-
row_index =
|
|
52
|
+
var _c = rowLocation(y, context.visibledatarow),
|
|
53
|
+
row_pre = _c[0],
|
|
54
|
+
row = _c[1],
|
|
55
|
+
row_index = _c[2];
|
|
51
56
|
if (row_pre !== hoverLocation.row_pre || row !== hoverLocation.row) {
|
|
52
57
|
setHoverLocation({
|
|
53
58
|
row_pre: row_pre,
|
|
@@ -57,16 +62,35 @@ var RowHeader = function RowHeader() {
|
|
|
57
62
|
setHoverInFreeze(inHorizontalFreeze);
|
|
58
63
|
}
|
|
59
64
|
}, [context.luckysheet_rows_change_size, context.visibledatarow, hoverLocation.row, hoverLocation.row_pre, refs.globalCache.freezen, context.currentSheetId]);
|
|
65
|
+
var _f = useRowDragAndDrop(containerRef, selectedLocationRef),
|
|
66
|
+
initiateDrag = _f.initiateDrag,
|
|
67
|
+
getRowIndexClicked = _f.getRowIndexClicked,
|
|
68
|
+
isRowDoubleClicked = _f.isRowDoubleClicked;
|
|
60
69
|
var onMouseDown = useCallback(function (e) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
70
|
+
if (e.button !== 0) return;
|
|
71
|
+
var targetEl = e.target;
|
|
72
|
+
if (targetEl.closest(".fortune-rows-change-size") || targetEl.closest(".fortune-rows-freeze-handle")) return;
|
|
73
|
+
var headerEl = containerRef.current;
|
|
74
|
+
if (!headerEl) return;
|
|
75
|
+
var clickedRowIndex = getRowIndexClicked(e.pageY, headerEl);
|
|
76
|
+
if (clickedRowIndex < 0) return;
|
|
77
|
+
if (isRowDoubleClicked(clickedRowIndex)) {
|
|
78
|
+
setContext(function (draft) {
|
|
79
|
+
draft.luckysheet_scroll_status = true;
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
var nativeEvent_1 = e.nativeEvent;
|
|
83
|
+
setContext(function (draft) {
|
|
84
|
+
handleRowHeaderMouseDown(draft, refs.globalCache, nativeEvent_1, containerRef.current, refs.cellInput.current, refs.fxInput.current);
|
|
85
|
+
});
|
|
68
86
|
return;
|
|
69
87
|
}
|
|
88
|
+
e.preventDefault();
|
|
89
|
+
e.stopPropagation();
|
|
90
|
+
initiateDrag(clickedRowIndex, e.pageY);
|
|
91
|
+
}, [refs.globalCache, context.visibledatarow, context.currentSheetId, setContext]);
|
|
92
|
+
var onMouseLeave = useCallback(function () {
|
|
93
|
+
if (context.luckysheet_rows_change_size) return;
|
|
70
94
|
setHoverLocation({
|
|
71
95
|
row: -1,
|
|
72
96
|
row_pre: -1,
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RefObject } from "react";
|
|
2
|
+
export declare const useColumnDragAndDrop: (containerRef: RefObject<HTMLDivElement | null>, selectedLocationRef: RefObject<any[]>) => {
|
|
3
|
+
initiateDrag: (clickedColIndex: number, startX: number) => void;
|
|
4
|
+
getColIndexClicked: (pageX: number, headerEl: HTMLDivElement) => number;
|
|
5
|
+
isColDoubleClicked: (clickedColIndex: number) => boolean;
|
|
6
|
+
};
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { useContext, useRef } from "react";
|
|
2
|
+
import { fixPositionOnFrozenCells, getSheetIndex, colLocation, colLocationByIndex, updateContextWithSheetData } from "@fileverse-dev/fortune-core";
|
|
3
|
+
import WorkbookContext from "../../../context";
|
|
4
|
+
export var useColumnDragAndDrop = function useColumnDragAndDrop(containerRef, selectedLocationRef) {
|
|
5
|
+
var DOUBLE_CLICK_MS = 300;
|
|
6
|
+
var START_DRAG_THRESHOLD_PX = 6;
|
|
7
|
+
var clickRef = useRef({
|
|
8
|
+
lastTime: 0,
|
|
9
|
+
lastCol: -1
|
|
10
|
+
});
|
|
11
|
+
var _a = useContext(WorkbookContext),
|
|
12
|
+
context = _a.context,
|
|
13
|
+
setContext = _a.setContext,
|
|
14
|
+
refs = _a.refs;
|
|
15
|
+
var dragRef = useRef({
|
|
16
|
+
mouseDown: false,
|
|
17
|
+
startX: 0,
|
|
18
|
+
source: -1,
|
|
19
|
+
active: false,
|
|
20
|
+
lineEl: null,
|
|
21
|
+
ghostEl: null,
|
|
22
|
+
onDocMove: null,
|
|
23
|
+
onDocUp: null,
|
|
24
|
+
prevUserSelect: "",
|
|
25
|
+
prevWebkitUserSelect: "",
|
|
26
|
+
lastNativeEvent: null,
|
|
27
|
+
ghostWidthPx: 60
|
|
28
|
+
});
|
|
29
|
+
var removeDragLine = function removeDragLine() {
|
|
30
|
+
var _a = dragRef.current,
|
|
31
|
+
lineEl = _a.lineEl,
|
|
32
|
+
ghostEl = _a.ghostEl;
|
|
33
|
+
try {
|
|
34
|
+
if (lineEl === null || lineEl === void 0 ? void 0 : lineEl.parentElement) lineEl.parentElement.removeChild(lineEl);
|
|
35
|
+
} catch (_b) {}
|
|
36
|
+
try {
|
|
37
|
+
if (ghostEl === null || ghostEl === void 0 ? void 0 : ghostEl.parentElement) ghostEl.parentElement.removeChild(ghostEl);
|
|
38
|
+
} catch (_c) {}
|
|
39
|
+
dragRef.current.lineEl = null;
|
|
40
|
+
dragRef.current.ghostEl = null;
|
|
41
|
+
dragRef.current.active = false;
|
|
42
|
+
};
|
|
43
|
+
var getColIndexClicked = function getColIndexClicked(pageX, headerEl) {
|
|
44
|
+
var _a;
|
|
45
|
+
var rect = headerEl.getBoundingClientRect();
|
|
46
|
+
var mouseXInHeader = pageX - rect.left - window.scrollX;
|
|
47
|
+
var localXInHeader = mouseXInHeader + headerEl.scrollLeft;
|
|
48
|
+
var freeze = (_a = refs.globalCache.freezen) === null || _a === void 0 ? void 0 : _a[context.currentSheetId];
|
|
49
|
+
var adjustedX = fixPositionOnFrozenCells(freeze, localXInHeader, 0, mouseXInHeader, 0).x;
|
|
50
|
+
var _b = colLocation(adjustedX, context.visibledatacolumn),
|
|
51
|
+
colIndex = _b[2];
|
|
52
|
+
return colIndex;
|
|
53
|
+
};
|
|
54
|
+
var isColDoubleClicked = function isColDoubleClicked(clickedColIndex) {
|
|
55
|
+
var now = performance.now();
|
|
56
|
+
var isDoubleClicked = now - clickRef.current.lastTime < DOUBLE_CLICK_MS && clickRef.current.lastCol === clickedColIndex;
|
|
57
|
+
clickRef.current.lastTime = now;
|
|
58
|
+
clickRef.current.lastCol = clickedColIndex;
|
|
59
|
+
return isDoubleClicked;
|
|
60
|
+
};
|
|
61
|
+
var computeInsertionFromPageX = function computeInsertionFromPageX(pageX) {
|
|
62
|
+
var _a, _b, _c, _d, _e, _f;
|
|
63
|
+
var workbookEl = containerRef.current;
|
|
64
|
+
if (!workbookEl) return {
|
|
65
|
+
insertionIndex: -1,
|
|
66
|
+
lineLeftPx: 0,
|
|
67
|
+
mouseXInWorkbook: 0
|
|
68
|
+
};
|
|
69
|
+
var wbRect = workbookEl.getBoundingClientRect();
|
|
70
|
+
var mouseXInWorkbook = pageX - wbRect.left - window.scrollX;
|
|
71
|
+
var localXInWorkbook = mouseXInWorkbook + ((_b = (_a = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current) === null || _a === void 0 ? void 0 : _a.scrollLeft) !== null && _b !== void 0 ? _b : context.scrollLeft);
|
|
72
|
+
var freeze = (_c = refs.globalCache.freezen) === null || _c === void 0 ? void 0 : _c[context.currentSheetId];
|
|
73
|
+
var xWorkbook = fixPositionOnFrozenCells(freeze, localXInWorkbook, 0, mouseXInWorkbook, 0).x;
|
|
74
|
+
var _g = colLocation(xWorkbook, context.visibledatacolumn),
|
|
75
|
+
colLeftPx = _g[0],
|
|
76
|
+
colRightPx = _g[1],
|
|
77
|
+
hoveredColIndex = _g[2];
|
|
78
|
+
var colMidPx = (colLeftPx + colRightPx) / 2;
|
|
79
|
+
var insertionIndex = hoveredColIndex + (xWorkbook > colMidPx ? 1 : 0);
|
|
80
|
+
var sheetIdx = getSheetIndex(context, context.currentSheetId);
|
|
81
|
+
var sheetLocal = sheetIdx == null ? null : context.luckysheetfile[sheetIdx];
|
|
82
|
+
var maxCols = (_f = (_e = (_d = sheetLocal === null || sheetLocal === void 0 ? void 0 : sheetLocal.data) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : context.visibledatacolumn.length;
|
|
83
|
+
insertionIndex = Math.max(0, Math.min(maxCols, insertionIndex));
|
|
84
|
+
var lineLeftPx = xWorkbook > colMidPx ? colRightPx : colLeftPx;
|
|
85
|
+
return {
|
|
86
|
+
insertionIndex: insertionIndex,
|
|
87
|
+
lineLeftPx: lineLeftPx,
|
|
88
|
+
mouseXInWorkbook: mouseXInWorkbook
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
var createInsertionLine = function createInsertionLine(host) {
|
|
92
|
+
var el = document.createElement("div");
|
|
93
|
+
el.style.position = "absolute";
|
|
94
|
+
el.style.top = "0px";
|
|
95
|
+
el.style.bottom = "0px";
|
|
96
|
+
el.style.width = "2px";
|
|
97
|
+
el.style.background = "#EFC703";
|
|
98
|
+
el.style.zIndex = "9999";
|
|
99
|
+
el.style.pointerEvents = "none";
|
|
100
|
+
host.appendChild(el);
|
|
101
|
+
return el;
|
|
102
|
+
};
|
|
103
|
+
var createGhost = function createGhost(host) {
|
|
104
|
+
var el = document.createElement("div");
|
|
105
|
+
el.style.position = "fixed";
|
|
106
|
+
el.style.top = "135px";
|
|
107
|
+
el.style.height = "".concat(window.innerHeight, "px");
|
|
108
|
+
el.style.boxSizing = "border-box";
|
|
109
|
+
el.style.padding = "6px 8px";
|
|
110
|
+
el.style.background = "rgba(239,199,3,0.10)";
|
|
111
|
+
el.style.border = "1px solid #EFC703";
|
|
112
|
+
el.style.borderRadius = "6px";
|
|
113
|
+
el.style.zIndex = "10000";
|
|
114
|
+
el.style.pointerEvents = "none";
|
|
115
|
+
el.style.display = "flex";
|
|
116
|
+
el.style.alignItems = "start";
|
|
117
|
+
el.style.justifyContent = "center";
|
|
118
|
+
el.style.fontSize = "12px";
|
|
119
|
+
el.style.fontWeight = "500";
|
|
120
|
+
var ghostWidthPx = 60;
|
|
121
|
+
var ghostLabel = "Col ".concat(String.fromCharCode(65 + dragRef.current.source));
|
|
122
|
+
var selectedBlock = selectedLocationRef.current.find(function (s) {
|
|
123
|
+
return s.c1 <= dragRef.current.source && dragRef.current.source <= s.c2;
|
|
124
|
+
});
|
|
125
|
+
if (selectedBlock) {
|
|
126
|
+
ghostWidthPx = Math.max(60, selectedBlock.col - selectedBlock.col_pre - 1);
|
|
127
|
+
var count = selectedBlock.c2 - selectedBlock.c1 + 1;
|
|
128
|
+
ghostLabel = count > 1 ? "".concat(count, " cols") : "Col ".concat(String.fromCharCode(65 + selectedBlock.c1));
|
|
129
|
+
} else {
|
|
130
|
+
var _a = colLocationByIndex(dragRef.current.source, context.visibledatacolumn),
|
|
131
|
+
pre = _a[0],
|
|
132
|
+
end = _a[1];
|
|
133
|
+
var sourceColWidth = end - pre - 1;
|
|
134
|
+
ghostWidthPx = Math.max(60, sourceColWidth);
|
|
135
|
+
}
|
|
136
|
+
el.style.width = "".concat(ghostWidthPx, "px");
|
|
137
|
+
el.textContent = ghostLabel;
|
|
138
|
+
host.appendChild(el);
|
|
139
|
+
return {
|
|
140
|
+
el: el,
|
|
141
|
+
ghostWidthPx: ghostWidthPx
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
var isDragActivated = function isDragActivated(host, pixelDeltaX) {
|
|
145
|
+
if (dragRef.current.active) return true;
|
|
146
|
+
if (pixelDeltaX < START_DRAG_THRESHOLD_PX) return false;
|
|
147
|
+
dragRef.current.active = true;
|
|
148
|
+
dragRef.current.prevUserSelect = document.body.style.userSelect;
|
|
149
|
+
dragRef.current.prevWebkitUserSelect = document.body.style.webkitUserSelect;
|
|
150
|
+
document.body.style.userSelect = "none";
|
|
151
|
+
document.body.style.webkitUserSelect = "none";
|
|
152
|
+
dragRef.current.lineEl = createInsertionLine(host);
|
|
153
|
+
var _a = createGhost(host),
|
|
154
|
+
el = _a.el,
|
|
155
|
+
ghostWidthPx = _a.ghostWidthPx;
|
|
156
|
+
dragRef.current.ghostEl = el;
|
|
157
|
+
dragRef.current.ghostWidthPx = ghostWidthPx;
|
|
158
|
+
return true;
|
|
159
|
+
};
|
|
160
|
+
var handleColumnDrag = function handleColumnDrag(ev) {
|
|
161
|
+
if (!dragRef.current.mouseDown) return;
|
|
162
|
+
dragRef.current.lastNativeEvent = ev;
|
|
163
|
+
var host = containerRef.current;
|
|
164
|
+
if (!host) return;
|
|
165
|
+
var dragOffset = Math.abs(ev.pageX - dragRef.current.startX);
|
|
166
|
+
if (!isDragActivated(host, dragOffset)) return;
|
|
167
|
+
var lineLeftPx = computeInsertionFromPageX(ev.pageX).lineLeftPx;
|
|
168
|
+
var ghostPosOffset = dragRef.current.startX > ev.pageX ? 0 : 120;
|
|
169
|
+
if (dragRef.current.lineEl) {
|
|
170
|
+
dragRef.current.lineEl.style.left = "".concat(lineLeftPx, "px");
|
|
171
|
+
}
|
|
172
|
+
if (dragRef.current.ghostEl) {
|
|
173
|
+
dragRef.current.ghostEl.style.left = "".concat(ev.pageX - ghostPosOffset, "px");
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var handleColumnDragEnd = function handleColumnDragEnd(ev) {
|
|
177
|
+
if (!dragRef.current.mouseDown) return;
|
|
178
|
+
dragRef.current.mouseDown = false;
|
|
179
|
+
try {
|
|
180
|
+
document.body.style.userSelect = dragRef.current.prevUserSelect || "";
|
|
181
|
+
document.body.style.webkitUserSelect = dragRef.current.prevWebkitUserSelect || "";
|
|
182
|
+
} catch (_a) {}
|
|
183
|
+
if (dragRef.current.active) {
|
|
184
|
+
var finalInsertionIndex_1 = computeInsertionFromPageX(ev.pageX).insertionIndex;
|
|
185
|
+
var sourceIndex_1 = dragRef.current.source;
|
|
186
|
+
var sheetIdx_1 = getSheetIndex(context, context.currentSheetId);
|
|
187
|
+
if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
|
|
188
|
+
setContext(function (draft) {
|
|
189
|
+
var _a, _b;
|
|
190
|
+
var _sheet = draft.luckysheetfile[sheetIdx_1];
|
|
191
|
+
if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
|
|
192
|
+
var rows = _sheet.data;
|
|
193
|
+
if (rows.length === 0) return;
|
|
194
|
+
var numCols = (_b = (_a = rows[0]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
|
|
195
|
+
if (sourceIndex_1 < 0 || sourceIndex_1 >= numCols) return;
|
|
196
|
+
var targetIndex = finalInsertionIndex_1;
|
|
197
|
+
if (targetIndex > sourceIndex_1) targetIndex -= 1;
|
|
198
|
+
for (var i = 0; i < rows.length; i += 1) {
|
|
199
|
+
var row = rows[i];
|
|
200
|
+
if (!row || sourceIndex_1 >= row.length) continue;
|
|
201
|
+
var cellData = row.splice(sourceIndex_1, 1)[0];
|
|
202
|
+
if (targetIndex < 0) targetIndex = 0;
|
|
203
|
+
if (targetIndex > row.length) targetIndex = row.length;
|
|
204
|
+
row.splice(targetIndex, 0, cellData);
|
|
205
|
+
}
|
|
206
|
+
_sheet.data = rows;
|
|
207
|
+
updateContextWithSheetData(draft, _sheet.data);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
removeDragLine();
|
|
212
|
+
dragRef.current.active = false;
|
|
213
|
+
dragRef.current.source = -1;
|
|
214
|
+
if (dragRef.current.onDocMove) document.removeEventListener("mousemove", dragRef.current.onDocMove);
|
|
215
|
+
if (dragRef.current.onDocUp) document.removeEventListener("mouseup", dragRef.current.onDocUp);
|
|
216
|
+
dragRef.current.onDocMove = null;
|
|
217
|
+
dragRef.current.onDocUp = null;
|
|
218
|
+
};
|
|
219
|
+
var initiateDrag = function initiateDrag(clickedColIndex, startX) {
|
|
220
|
+
dragRef.current.mouseDown = true;
|
|
221
|
+
dragRef.current.startX = startX;
|
|
222
|
+
dragRef.current.source = clickedColIndex;
|
|
223
|
+
dragRef.current.active = false;
|
|
224
|
+
console.log("initiateDrag", startX);
|
|
225
|
+
dragRef.current.onDocMove = handleColumnDrag;
|
|
226
|
+
dragRef.current.onDocUp = handleColumnDragEnd;
|
|
227
|
+
document.addEventListener("mousemove", handleColumnDrag);
|
|
228
|
+
document.addEventListener("mouseup", handleColumnDragEnd);
|
|
229
|
+
};
|
|
230
|
+
return {
|
|
231
|
+
initiateDrag: initiateDrag,
|
|
232
|
+
getColIndexClicked: getColIndexClicked,
|
|
233
|
+
isColDoubleClicked: isColDoubleClicked
|
|
234
|
+
};
|
|
235
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RefObject } from "react";
|
|
2
|
+
export declare const useRowDragAndDrop: (containerRef: RefObject<HTMLDivElement | null>, selectedLocationRef: RefObject<any[]>) => {
|
|
3
|
+
initiateDrag: (clickedRowIndex: number, startY: number) => void;
|
|
4
|
+
getRowIndexClicked: (pageY: number, headerEl: HTMLDivElement) => number;
|
|
5
|
+
isRowDoubleClicked: (clickedRowIndex: number) => boolean;
|
|
6
|
+
};
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { useContext, useRef } from "react";
|
|
2
|
+
import { fixPositionOnFrozenCells, getSheetIndex, rowLocation, rowLocationByIndex, updateContextWithSheetData } from "@fileverse-dev/fortune-core";
|
|
3
|
+
import WorkbookContext from "../../../context";
|
|
4
|
+
export var useRowDragAndDrop = function useRowDragAndDrop(containerRef, selectedLocationRef) {
|
|
5
|
+
var DOUBLE_MS = 300;
|
|
6
|
+
var START_DRAG_THRESHOLD_PX = 6;
|
|
7
|
+
var clickRef = useRef({
|
|
8
|
+
lastTime: 0,
|
|
9
|
+
lastRow: -1
|
|
10
|
+
});
|
|
11
|
+
var _a = useContext(WorkbookContext),
|
|
12
|
+
context = _a.context,
|
|
13
|
+
setContext = _a.setContext,
|
|
14
|
+
refs = _a.refs;
|
|
15
|
+
var dragRef = useRef({
|
|
16
|
+
mouseDown: false,
|
|
17
|
+
startY: 0,
|
|
18
|
+
source: -1,
|
|
19
|
+
active: false,
|
|
20
|
+
lineEl: null,
|
|
21
|
+
ghostEl: null,
|
|
22
|
+
onDocMove: null,
|
|
23
|
+
onDocUp: null,
|
|
24
|
+
prevUserSelect: "",
|
|
25
|
+
prevWebkitUserSelect: "",
|
|
26
|
+
lastNativeEvent: null,
|
|
27
|
+
ghostHeightPx: 24
|
|
28
|
+
});
|
|
29
|
+
var removeDragLine = function removeDragLine() {
|
|
30
|
+
var _a = dragRef.current,
|
|
31
|
+
lineEl = _a.lineEl,
|
|
32
|
+
ghostEl = _a.ghostEl;
|
|
33
|
+
try {
|
|
34
|
+
if (lineEl === null || lineEl === void 0 ? void 0 : lineEl.parentElement) lineEl.parentElement.removeChild(lineEl);
|
|
35
|
+
} catch (_b) {}
|
|
36
|
+
try {
|
|
37
|
+
if (ghostEl === null || ghostEl === void 0 ? void 0 : ghostEl.parentElement) ghostEl.parentElement.removeChild(ghostEl);
|
|
38
|
+
} catch (_c) {}
|
|
39
|
+
dragRef.current.lineEl = null;
|
|
40
|
+
dragRef.current.ghostEl = null;
|
|
41
|
+
dragRef.current.active = false;
|
|
42
|
+
};
|
|
43
|
+
var getRowIndexClicked = function getRowIndexClicked(pageY, headerEl) {
|
|
44
|
+
var _a;
|
|
45
|
+
var rect = headerEl.getBoundingClientRect();
|
|
46
|
+
var mouseYInHeader = pageY - rect.top - window.scrollY;
|
|
47
|
+
var localYInHeader = mouseYInHeader + headerEl.scrollTop;
|
|
48
|
+
var freeze = (_a = refs.globalCache.freezen) === null || _a === void 0 ? void 0 : _a[context.currentSheetId];
|
|
49
|
+
var adjustedY = fixPositionOnFrozenCells(freeze, 0, localYInHeader, 0, mouseYInHeader).y;
|
|
50
|
+
var _b = rowLocation(adjustedY, context.visibledatarow),
|
|
51
|
+
rowIndex = _b[2];
|
|
52
|
+
return rowIndex;
|
|
53
|
+
};
|
|
54
|
+
var isRowDoubleClicked = function isRowDoubleClicked(clickedRowIndex) {
|
|
55
|
+
var now = performance.now();
|
|
56
|
+
var isDoubleClicked = now - clickRef.current.lastTime < DOUBLE_MS && clickRef.current.lastRow === clickedRowIndex;
|
|
57
|
+
clickRef.current.lastTime = now;
|
|
58
|
+
clickRef.current.lastRow = clickedRowIndex;
|
|
59
|
+
return isDoubleClicked;
|
|
60
|
+
};
|
|
61
|
+
var computeInsertionFromPageY = function computeInsertionFromPageY(pageY) {
|
|
62
|
+
var _a, _b, _c, _d, _e;
|
|
63
|
+
var workbookEl = containerRef.current;
|
|
64
|
+
if (!workbookEl) return {
|
|
65
|
+
insertionIndex: -1,
|
|
66
|
+
lineTopPx: 0,
|
|
67
|
+
mouseYInWorkbook: 0
|
|
68
|
+
};
|
|
69
|
+
var wbRect = workbookEl.getBoundingClientRect();
|
|
70
|
+
var mouseYInWorkbook = pageY - wbRect.top - window.scrollY;
|
|
71
|
+
var localYInWorkbook = mouseYInWorkbook + ((_b = (_a = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current) === null || _a === void 0 ? void 0 : _a.scrollTop) !== null && _b !== void 0 ? _b : context.scrollTop);
|
|
72
|
+
var freeze = (_c = refs.globalCache.freezen) === null || _c === void 0 ? void 0 : _c[context.currentSheetId];
|
|
73
|
+
var yWorkbook = fixPositionOnFrozenCells(freeze, 0, localYInWorkbook, 0, mouseYInWorkbook).y;
|
|
74
|
+
var _f = rowLocation(yWorkbook, context.visibledatarow),
|
|
75
|
+
rowTopPx = _f[0],
|
|
76
|
+
rowBottomPx = _f[1],
|
|
77
|
+
hoveredRowIndex = _f[2];
|
|
78
|
+
var rowMidPx = (rowTopPx + rowBottomPx) / 2;
|
|
79
|
+
var insertionIndex = hoveredRowIndex + (yWorkbook > rowMidPx ? 1 : 0);
|
|
80
|
+
var sheetIdx = getSheetIndex(context, context.currentSheetId);
|
|
81
|
+
var sheetLocal = sheetIdx == null ? null : context.luckysheetfile[sheetIdx];
|
|
82
|
+
var maxRows = (_e = (_d = sheetLocal === null || sheetLocal === void 0 ? void 0 : sheetLocal.data) === null || _d === void 0 ? void 0 : _d.length) !== null && _e !== void 0 ? _e : context.visibledatarow.length;
|
|
83
|
+
insertionIndex = Math.max(0, Math.min(maxRows, insertionIndex));
|
|
84
|
+
var lineTopPx = yWorkbook > rowMidPx ? rowBottomPx : rowTopPx;
|
|
85
|
+
return {
|
|
86
|
+
insertionIndex: insertionIndex,
|
|
87
|
+
lineTopPx: lineTopPx,
|
|
88
|
+
mouseYInWorkbook: mouseYInWorkbook
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
var createInsertionLine = function createInsertionLine(host) {
|
|
92
|
+
var el = document.createElement("div");
|
|
93
|
+
el.style.position = "absolute";
|
|
94
|
+
el.style.left = "0px";
|
|
95
|
+
el.style.right = "0px";
|
|
96
|
+
el.style.height = "2px";
|
|
97
|
+
el.style.background = "#EFC703";
|
|
98
|
+
el.style.zIndex = "9999";
|
|
99
|
+
el.style.pointerEvents = "none";
|
|
100
|
+
host.appendChild(el);
|
|
101
|
+
return el;
|
|
102
|
+
};
|
|
103
|
+
var createGhost = function createGhost(host) {
|
|
104
|
+
var el = document.createElement("div");
|
|
105
|
+
el.style.position = "fixed";
|
|
106
|
+
el.style.left = "55px";
|
|
107
|
+
el.style.width = "".concat(window.innerWidth, "px");
|
|
108
|
+
el.style.boxSizing = "border-box";
|
|
109
|
+
el.style.padding = "6px 8px";
|
|
110
|
+
el.style.background = "rgba(239,199,3,0.10)";
|
|
111
|
+
el.style.border = "1px solid #EFC703";
|
|
112
|
+
el.style.borderRadius = "6px";
|
|
113
|
+
el.style.zIndex = "10000";
|
|
114
|
+
el.style.pointerEvents = "none";
|
|
115
|
+
el.style.display = "flex";
|
|
116
|
+
el.style.alignItems = "center";
|
|
117
|
+
el.style.fontSize = "12px";
|
|
118
|
+
el.style.fontWeight = "500";
|
|
119
|
+
var ghostHeightPx = 24;
|
|
120
|
+
var ghostLabel = "".concat(dragRef.current.source + 1, " row");
|
|
121
|
+
var selectedBlock = selectedLocationRef.current.find(function (s) {
|
|
122
|
+
return s.r1 <= dragRef.current.source && dragRef.current.source <= s.r2;
|
|
123
|
+
});
|
|
124
|
+
if (selectedBlock) {
|
|
125
|
+
ghostHeightPx = Math.max(24, selectedBlock.row - selectedBlock.row_pre - 1);
|
|
126
|
+
var count = selectedBlock.r2 - selectedBlock.r1 + 1;
|
|
127
|
+
ghostLabel = count > 1 ? "".concat(count, " rows") : "".concat(selectedBlock.r1 + 1, " row");
|
|
128
|
+
} else {
|
|
129
|
+
var _a = rowLocationByIndex(dragRef.current.source, context.visibledatarow),
|
|
130
|
+
pre = _a[0],
|
|
131
|
+
end = _a[1];
|
|
132
|
+
var sourceRowHeight = end - pre - 1;
|
|
133
|
+
ghostHeightPx = Math.max(24, sourceRowHeight);
|
|
134
|
+
}
|
|
135
|
+
el.style.height = "".concat(ghostHeightPx, "px");
|
|
136
|
+
el.textContent = ghostLabel;
|
|
137
|
+
host.appendChild(el);
|
|
138
|
+
return {
|
|
139
|
+
el: el,
|
|
140
|
+
ghostHeightPx: ghostHeightPx
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
var isDragActivated = function isDragActivated(host, pixelDeltaY) {
|
|
144
|
+
if (dragRef.current.active) return true;
|
|
145
|
+
if (pixelDeltaY < START_DRAG_THRESHOLD_PX) return false;
|
|
146
|
+
dragRef.current.active = true;
|
|
147
|
+
dragRef.current.prevUserSelect = document.body.style.userSelect;
|
|
148
|
+
dragRef.current.prevWebkitUserSelect = document.body.style.webkitUserSelect;
|
|
149
|
+
document.body.style.userSelect = "none";
|
|
150
|
+
document.body.style.webkitUserSelect = "none";
|
|
151
|
+
dragRef.current.lineEl = createInsertionLine(host);
|
|
152
|
+
var _a = createGhost(host),
|
|
153
|
+
el = _a.el,
|
|
154
|
+
ghostHeightPx = _a.ghostHeightPx;
|
|
155
|
+
dragRef.current.ghostEl = el;
|
|
156
|
+
dragRef.current.ghostHeightPx = ghostHeightPx;
|
|
157
|
+
return true;
|
|
158
|
+
};
|
|
159
|
+
var handleRowDrag = function handleRowDrag(ev) {
|
|
160
|
+
if (!dragRef.current.mouseDown) return;
|
|
161
|
+
dragRef.current.lastNativeEvent = ev;
|
|
162
|
+
var host = containerRef.current;
|
|
163
|
+
if (!host) return;
|
|
164
|
+
var dragOffset = Math.abs(ev.pageY - dragRef.current.startY);
|
|
165
|
+
if (!isDragActivated(host, dragOffset)) return;
|
|
166
|
+
var lineTopPx = computeInsertionFromPageY(ev.pageY).lineTopPx;
|
|
167
|
+
var ghostPosOffset = dragRef.current.startY > ev.pageY ? 0 : 25;
|
|
168
|
+
if (dragRef.current.lineEl) {
|
|
169
|
+
dragRef.current.lineEl.style.top = "".concat(lineTopPx, "px");
|
|
170
|
+
}
|
|
171
|
+
if (dragRef.current.ghostEl) {
|
|
172
|
+
dragRef.current.ghostEl.style.top = "".concat(ev.pageY - ghostPosOffset, "px");
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
var handleRowDragEnd = function handleRowDragEnd(ev) {
|
|
176
|
+
if (!dragRef.current.mouseDown) return;
|
|
177
|
+
dragRef.current.mouseDown = false;
|
|
178
|
+
try {
|
|
179
|
+
document.body.style.userSelect = dragRef.current.prevUserSelect || "";
|
|
180
|
+
document.body.style.webkitUserSelect = dragRef.current.prevWebkitUserSelect || "";
|
|
181
|
+
} catch (_a) {}
|
|
182
|
+
if (dragRef.current.active) {
|
|
183
|
+
var finalInsertionIndex_1 = computeInsertionFromPageY(ev.pageY).insertionIndex;
|
|
184
|
+
var sourceIndex_1 = dragRef.current.source;
|
|
185
|
+
var sheetIdx_1 = getSheetIndex(context, context.currentSheetId);
|
|
186
|
+
if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
|
|
187
|
+
setContext(function (draft) {
|
|
188
|
+
var _sheet = draft.luckysheetfile[sheetIdx_1];
|
|
189
|
+
if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
|
|
190
|
+
var rows = _sheet.data;
|
|
191
|
+
if (sourceIndex_1 < 0 || sourceIndex_1 >= rows.length) return;
|
|
192
|
+
var targetIndex = finalInsertionIndex_1;
|
|
193
|
+
if (targetIndex > sourceIndex_1) targetIndex -= 1;
|
|
194
|
+
var rowData = rows.splice(sourceIndex_1, 1)[0];
|
|
195
|
+
if (targetIndex < 0) targetIndex = 0;
|
|
196
|
+
if (targetIndex > rows.length) targetIndex = rows.length;
|
|
197
|
+
rows.splice(targetIndex, 0, rowData);
|
|
198
|
+
_sheet.data = rows;
|
|
199
|
+
updateContextWithSheetData(draft, _sheet.data);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
removeDragLine();
|
|
204
|
+
dragRef.current.active = false;
|
|
205
|
+
dragRef.current.source = -1;
|
|
206
|
+
if (dragRef.current.onDocMove) document.removeEventListener("mousemove", dragRef.current.onDocMove);
|
|
207
|
+
if (dragRef.current.onDocUp) document.removeEventListener("mouseup", dragRef.current.onDocUp);
|
|
208
|
+
dragRef.current.onDocMove = null;
|
|
209
|
+
dragRef.current.onDocUp = null;
|
|
210
|
+
};
|
|
211
|
+
var initiateDrag = function initiateDrag(clickedRowIndex, startY) {
|
|
212
|
+
dragRef.current.mouseDown = true;
|
|
213
|
+
dragRef.current.startY = startY;
|
|
214
|
+
dragRef.current.source = clickedRowIndex;
|
|
215
|
+
dragRef.current.active = false;
|
|
216
|
+
dragRef.current.onDocMove = handleRowDrag;
|
|
217
|
+
dragRef.current.onDocUp = handleRowDragEnd;
|
|
218
|
+
document.addEventListener("mousemove", handleRowDrag);
|
|
219
|
+
document.addEventListener("mouseup", handleRowDragEnd);
|
|
220
|
+
};
|
|
221
|
+
return {
|
|
222
|
+
initiateDrag: initiateDrag,
|
|
223
|
+
getRowIndexClicked: getRowIndexClicked,
|
|
224
|
+
isRowDoubleClicked: isRowDoubleClicked
|
|
225
|
+
};
|
|
226
|
+
};
|
|
@@ -215,6 +215,19 @@
|
|
|
215
215
|
background-color: hsl(var(--color-button-secondary-hover)) !important;
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
+
.fortune-sheettab-button-arrow {
|
|
219
|
+
display: flex;
|
|
220
|
+
align-items: center;
|
|
221
|
+
justify-content: center;
|
|
222
|
+
height: 30px;
|
|
223
|
+
width: 30px;
|
|
224
|
+
background-color: hsl(var(--color-bg-secondary)) !important;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.fortune-sheettab-button-arrow:hover {
|
|
228
|
+
background-color: hsl(var(--color-button-secondary-hover)) !important;
|
|
229
|
+
}
|
|
230
|
+
|
|
218
231
|
.luckysheet-noselected-text {
|
|
219
232
|
-moz-user-select: -moz-test;
|
|
220
233
|
-khtml-user-select: none;
|