@fileverse-dev/fortune-react 1.1.83 → 1.1.84

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.
@@ -10,6 +10,7 @@ var _lodash = _interopRequireDefault(require("lodash"));
10
10
  var _react = _interopRequireWildcard(require("react"));
11
11
  var _context = _interopRequireDefault(require("../../context"));
12
12
  var _SVGIcon = _interopRequireDefault(require("../SVGIcon"));
13
+ var _columnHelpers = require("./drag_and_drop/column-helpers");
13
14
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  var ColumnHeader = function ColumnHeader() {
@@ -43,6 +44,14 @@ var ColumnHeader = function ColumnHeader() {
43
44
  }
44
45
  return context.scrollLeft;
45
46
  }, [context.visibledatacolumn, sheet === null || sheet === void 0 ? void 0 : sheet.frozen, context.scrollLeft]);
47
+ var selectedLocationRef = (0, _react.useRef)(selectedLocation);
48
+ (0, _react.useEffect)(function () {
49
+ selectedLocationRef.current = selectedLocation;
50
+ }, [selectedLocation]);
51
+ var _f = (0, _columnHelpers.useColumnDragAndDrop)(containerRef, selectedLocationRef),
52
+ initiateDrag = _f.initiateDrag,
53
+ getColIndexClicked = _f.getColIndexClicked,
54
+ isColDoubleClicked = _f.isColDoubleClicked;
46
55
  var onMouseMove = (0, _react.useCallback)(function (e) {
47
56
  var _a;
48
57
  if (context.luckysheet_cols_change_size) {
@@ -73,11 +82,28 @@ var ColumnHeader = function ColumnHeader() {
73
82
  }]);
74
83
  }, [context, hoverLocation.col_index, refs.globalCache.freezen]);
75
84
  var onMouseDown = (0, _react.useCallback)(function (e) {
76
- var nativeEvent = e.nativeEvent;
77
- setContext(function (draftCtx) {
78
- (0, _fortuneCore.handleColumnHeaderMouseDown)(draftCtx, refs.globalCache, nativeEvent, containerRef.current, refs.cellInput.current, refs.fxInput.current);
79
- });
80
- }, [refs.globalCache, refs.cellInput, refs.fxInput, setContext]);
85
+ if (e.button !== 0) return;
86
+ var targetEl = e.target;
87
+ if (targetEl.closest(".fortune-cols-change-size") || targetEl.closest(".fortune-cols-freeze-handle") || targetEl.closest(".header-arrow")) return;
88
+ var headerEl = containerRef.current;
89
+ if (!headerEl) return;
90
+ var clickedColIndex = getColIndexClicked(e.pageX, headerEl);
91
+ if (clickedColIndex < 0) return;
92
+ if (isColDoubleClicked(clickedColIndex)) {
93
+ setContext(function (draft) {
94
+ draft.luckysheet_scroll_status = true;
95
+ });
96
+ } else {
97
+ var nativeEvent_1 = e.nativeEvent;
98
+ setContext(function (draft) {
99
+ (0, _fortuneCore.handleColumnHeaderMouseDown)(draft, refs.globalCache, nativeEvent_1, containerRef.current, refs.cellInput.current, refs.fxInput.current);
100
+ });
101
+ return;
102
+ }
103
+ e.preventDefault();
104
+ e.stopPropagation();
105
+ initiateDrag(clickedColIndex, e.pageX);
106
+ }, [refs.globalCache, refs.cellInput, refs.fxInput, setContext, getColIndexClicked, isColDoubleClicked, initiateDrag]);
81
107
  var onMouseLeave = (0, _react.useCallback)(function () {
82
108
  if (context.luckysheet_cols_change_size) {
83
109
  return;
@@ -9,6 +9,7 @@ var _fortuneCore = require("@fileverse-dev/fortune-core");
9
9
  var _lodash = _interopRequireDefault(require("lodash"));
10
10
  var _react = _interopRequireWildcard(require("react"));
11
11
  var _context = _interopRequireDefault(require("../../context"));
12
+ var _rowHelpers = require("./drag_and_drop/row-helpers");
12
13
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
13
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
15
  var RowHeader = function RowHeader() {
@@ -42,21 +43,25 @@ var RowHeader = function RowHeader() {
42
43
  }
43
44
  return context.scrollTop;
44
45
  }, [context.visibledatarow, sheet === null || sheet === void 0 ? void 0 : sheet.frozen, context.scrollTop]);
46
+ var selectedLocationRef = (0, _react.useRef)(selectedLocation);
47
+ (0, _react.useEffect)(function () {
48
+ selectedLocationRef.current = selectedLocation;
49
+ }, [selectedLocation]);
45
50
  var onMouseMove = (0, _react.useCallback)(function (e) {
46
51
  var _a;
47
- if (context.luckysheet_rows_change_size) {
48
- return;
49
- }
50
- var mouseY = e.pageY - containerRef.current.getBoundingClientRect().top - window.scrollY;
51
- var _y = mouseY + containerRef.current.scrollTop;
52
+ if (context.luckysheet_rows_change_size) return;
53
+ var container = containerRef.current;
54
+ if (!container) return;
55
+ var mouseY = e.pageY - container.getBoundingClientRect().top - window.scrollY;
56
+ var _y = mouseY + container.scrollTop;
52
57
  var freeze = (_a = refs.globalCache.freezen) === null || _a === void 0 ? void 0 : _a[context.currentSheetId];
53
58
  var _b = (0, _fortuneCore.fixPositionOnFrozenCells)(freeze, 0, _y, 0, mouseY),
54
59
  y = _b.y,
55
60
  inHorizontalFreeze = _b.inHorizontalFreeze;
56
- var row_location = (0, _fortuneCore.rowLocation)(y, context.visibledatarow);
57
- var row_pre = row_location[0],
58
- row = row_location[1],
59
- row_index = row_location[2];
61
+ var _c = (0, _fortuneCore.rowLocation)(y, context.visibledatarow),
62
+ row_pre = _c[0],
63
+ row = _c[1],
64
+ row_index = _c[2];
60
65
  if (row_pre !== hoverLocation.row_pre || row !== hoverLocation.row) {
61
66
  setHoverLocation({
62
67
  row_pre: row_pre,
@@ -66,16 +71,35 @@ var RowHeader = function RowHeader() {
66
71
  setHoverInFreeze(inHorizontalFreeze);
67
72
  }
68
73
  }, [context.luckysheet_rows_change_size, context.visibledatarow, hoverLocation.row, hoverLocation.row_pre, refs.globalCache.freezen, context.currentSheetId]);
74
+ var _f = (0, _rowHelpers.useRowDragAndDrop)(containerRef, selectedLocationRef),
75
+ initiateDrag = _f.initiateDrag,
76
+ getRowIndexClicked = _f.getRowIndexClicked,
77
+ isRowDoubleClicked = _f.isRowDoubleClicked;
69
78
  var onMouseDown = (0, _react.useCallback)(function (e) {
70
- var nativeEvent = e.nativeEvent;
71
- setContext(function (draftCtx) {
72
- (0, _fortuneCore.handleRowHeaderMouseDown)(draftCtx, refs.globalCache, nativeEvent, containerRef.current, refs.cellInput.current, refs.fxInput.current);
73
- });
74
- }, [refs.globalCache, refs.cellInput, refs.fxInput, setContext]);
75
- var onMouseLeave = (0, _react.useCallback)(function () {
76
- if (context.luckysheet_rows_change_size) {
79
+ if (e.button !== 0) return;
80
+ var targetEl = e.target;
81
+ if (targetEl.closest(".fortune-rows-change-size") || targetEl.closest(".fortune-rows-freeze-handle")) return;
82
+ var headerEl = containerRef.current;
83
+ if (!headerEl) return;
84
+ var clickedRowIndex = getRowIndexClicked(e.pageY, headerEl);
85
+ if (clickedRowIndex < 0) return;
86
+ if (isRowDoubleClicked(clickedRowIndex)) {
87
+ setContext(function (draft) {
88
+ draft.luckysheet_scroll_status = true;
89
+ });
90
+ } else {
91
+ var nativeEvent_1 = e.nativeEvent;
92
+ setContext(function (draft) {
93
+ (0, _fortuneCore.handleRowHeaderMouseDown)(draft, refs.globalCache, nativeEvent_1, containerRef.current, refs.cellInput.current, refs.fxInput.current);
94
+ });
77
95
  return;
78
96
  }
97
+ e.preventDefault();
98
+ e.stopPropagation();
99
+ initiateDrag(clickedRowIndex, e.pageY);
100
+ }, [refs.globalCache, context.visibledatarow, context.currentSheetId, setContext]);
101
+ var onMouseLeave = (0, _react.useCallback)(function () {
102
+ if (context.luckysheet_rows_change_size) return;
79
103
  setHoverLocation({
80
104
  row: -1,
81
105
  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,242 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useColumnDragAndDrop = void 0;
7
+ var _react = require("react");
8
+ var _fortuneCore = require("@fileverse-dev/fortune-core");
9
+ var _context = _interopRequireDefault(require("../../../context"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ var useColumnDragAndDrop = exports.useColumnDragAndDrop = function useColumnDragAndDrop(containerRef, selectedLocationRef) {
12
+ var DOUBLE_CLICK_MS = 300;
13
+ var START_DRAG_THRESHOLD_PX = 6;
14
+ var clickRef = (0, _react.useRef)({
15
+ lastTime: 0,
16
+ lastCol: -1
17
+ });
18
+ var _a = (0, _react.useContext)(_context.default),
19
+ context = _a.context,
20
+ setContext = _a.setContext,
21
+ refs = _a.refs;
22
+ var dragRef = (0, _react.useRef)({
23
+ mouseDown: false,
24
+ startX: 0,
25
+ source: -1,
26
+ active: false,
27
+ lineEl: null,
28
+ ghostEl: null,
29
+ onDocMove: null,
30
+ onDocUp: null,
31
+ prevUserSelect: "",
32
+ prevWebkitUserSelect: "",
33
+ lastNativeEvent: null,
34
+ ghostWidthPx: 60
35
+ });
36
+ var removeDragLine = function removeDragLine() {
37
+ var _a = dragRef.current,
38
+ lineEl = _a.lineEl,
39
+ ghostEl = _a.ghostEl;
40
+ try {
41
+ if (lineEl === null || lineEl === void 0 ? void 0 : lineEl.parentElement) lineEl.parentElement.removeChild(lineEl);
42
+ } catch (_b) {}
43
+ try {
44
+ if (ghostEl === null || ghostEl === void 0 ? void 0 : ghostEl.parentElement) ghostEl.parentElement.removeChild(ghostEl);
45
+ } catch (_c) {}
46
+ dragRef.current.lineEl = null;
47
+ dragRef.current.ghostEl = null;
48
+ dragRef.current.active = false;
49
+ };
50
+ var getColIndexClicked = function getColIndexClicked(pageX, headerEl) {
51
+ var _a;
52
+ var rect = headerEl.getBoundingClientRect();
53
+ var mouseXInHeader = pageX - rect.left - window.scrollX;
54
+ var localXInHeader = mouseXInHeader + headerEl.scrollLeft;
55
+ var freeze = (_a = refs.globalCache.freezen) === null || _a === void 0 ? void 0 : _a[context.currentSheetId];
56
+ var adjustedX = (0, _fortuneCore.fixPositionOnFrozenCells)(freeze, localXInHeader, 0, mouseXInHeader, 0).x;
57
+ var _b = (0, _fortuneCore.colLocation)(adjustedX, context.visibledatacolumn),
58
+ colIndex = _b[2];
59
+ return colIndex;
60
+ };
61
+ var isColDoubleClicked = function isColDoubleClicked(clickedColIndex) {
62
+ var now = performance.now();
63
+ var isDoubleClicked = now - clickRef.current.lastTime < DOUBLE_CLICK_MS && clickRef.current.lastCol === clickedColIndex;
64
+ clickRef.current.lastTime = now;
65
+ clickRef.current.lastCol = clickedColIndex;
66
+ return isDoubleClicked;
67
+ };
68
+ var computeInsertionFromPageX = function computeInsertionFromPageX(pageX) {
69
+ var _a, _b, _c, _d, _e, _f;
70
+ var workbookEl = containerRef.current;
71
+ if (!workbookEl) return {
72
+ insertionIndex: -1,
73
+ lineLeftPx: 0,
74
+ mouseXInWorkbook: 0
75
+ };
76
+ var wbRect = workbookEl.getBoundingClientRect();
77
+ var mouseXInWorkbook = pageX - wbRect.left - window.scrollX;
78
+ 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);
79
+ var freeze = (_c = refs.globalCache.freezen) === null || _c === void 0 ? void 0 : _c[context.currentSheetId];
80
+ var xWorkbook = (0, _fortuneCore.fixPositionOnFrozenCells)(freeze, localXInWorkbook, 0, mouseXInWorkbook, 0).x;
81
+ var _g = (0, _fortuneCore.colLocation)(xWorkbook, context.visibledatacolumn),
82
+ colLeftPx = _g[0],
83
+ colRightPx = _g[1],
84
+ hoveredColIndex = _g[2];
85
+ var colMidPx = (colLeftPx + colRightPx) / 2;
86
+ var insertionIndex = hoveredColIndex + (xWorkbook > colMidPx ? 1 : 0);
87
+ var sheetIdx = (0, _fortuneCore.getSheetIndex)(context, context.currentSheetId);
88
+ var sheetLocal = sheetIdx == null ? null : context.luckysheetfile[sheetIdx];
89
+ 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;
90
+ insertionIndex = Math.max(0, Math.min(maxCols, insertionIndex));
91
+ var lineLeftPx = xWorkbook > colMidPx ? colRightPx : colLeftPx;
92
+ return {
93
+ insertionIndex: insertionIndex,
94
+ lineLeftPx: lineLeftPx,
95
+ mouseXInWorkbook: mouseXInWorkbook
96
+ };
97
+ };
98
+ var createInsertionLine = function createInsertionLine(host) {
99
+ var el = document.createElement("div");
100
+ el.style.position = "absolute";
101
+ el.style.top = "0px";
102
+ el.style.bottom = "0px";
103
+ el.style.width = "2px";
104
+ el.style.background = "#EFC703";
105
+ el.style.zIndex = "9999";
106
+ el.style.pointerEvents = "none";
107
+ host.appendChild(el);
108
+ return el;
109
+ };
110
+ var createGhost = function createGhost(host) {
111
+ var el = document.createElement("div");
112
+ el.style.position = "fixed";
113
+ el.style.top = "124px";
114
+ el.style.height = "".concat(window.innerHeight, "px");
115
+ el.style.boxSizing = "border-box";
116
+ el.style.padding = "6px 8px";
117
+ el.style.background = "rgba(239,199,3,0.10)";
118
+ el.style.border = "1px solid #EFC703";
119
+ el.style.borderRadius = "6px";
120
+ el.style.zIndex = "10000";
121
+ el.style.pointerEvents = "none";
122
+ el.style.display = "flex";
123
+ el.style.alignItems = "start";
124
+ el.style.justifyContent = "center";
125
+ el.style.fontSize = "12px";
126
+ el.style.fontWeight = "500";
127
+ var ghostWidthPx = 60;
128
+ var ghostLabel = "Col ".concat(String.fromCharCode(65 + dragRef.current.source));
129
+ var selectedBlock = selectedLocationRef.current.find(function (s) {
130
+ return s.c1 <= dragRef.current.source && dragRef.current.source <= s.c2;
131
+ });
132
+ if (selectedBlock) {
133
+ ghostWidthPx = Math.max(60, selectedBlock.col - selectedBlock.col_pre - 1);
134
+ var count = selectedBlock.c2 - selectedBlock.c1 + 1;
135
+ ghostLabel = count > 1 ? "".concat(count, " cols") : "Col ".concat(String.fromCharCode(65 + selectedBlock.c1));
136
+ } else {
137
+ var _a = (0, _fortuneCore.colLocationByIndex)(dragRef.current.source, context.visibledatacolumn),
138
+ pre = _a[0],
139
+ end = _a[1];
140
+ var sourceColWidth = end - pre - 1;
141
+ ghostWidthPx = Math.max(60, sourceColWidth);
142
+ }
143
+ el.style.width = "".concat(ghostWidthPx, "px");
144
+ el.textContent = ghostLabel;
145
+ host.appendChild(el);
146
+ return {
147
+ el: el,
148
+ ghostWidthPx: ghostWidthPx
149
+ };
150
+ };
151
+ var isDragActivated = function isDragActivated(host, pixelDeltaX) {
152
+ if (dragRef.current.active) return true;
153
+ if (pixelDeltaX < START_DRAG_THRESHOLD_PX) return false;
154
+ dragRef.current.active = true;
155
+ dragRef.current.prevUserSelect = document.body.style.userSelect;
156
+ dragRef.current.prevWebkitUserSelect = document.body.style.webkitUserSelect;
157
+ document.body.style.userSelect = "none";
158
+ document.body.style.webkitUserSelect = "none";
159
+ dragRef.current.lineEl = createInsertionLine(host);
160
+ var _a = createGhost(host),
161
+ el = _a.el,
162
+ ghostWidthPx = _a.ghostWidthPx;
163
+ dragRef.current.ghostEl = el;
164
+ dragRef.current.ghostWidthPx = ghostWidthPx;
165
+ return true;
166
+ };
167
+ var handleColumnDrag = function handleColumnDrag(ev) {
168
+ if (!dragRef.current.mouseDown) return;
169
+ dragRef.current.lastNativeEvent = ev;
170
+ var host = containerRef.current;
171
+ if (!host) return;
172
+ var dragOffset = Math.abs(ev.pageX - dragRef.current.startX);
173
+ if (!isDragActivated(host, dragOffset)) return;
174
+ var lineLeftPx = computeInsertionFromPageX(ev.pageX).lineLeftPx;
175
+ var ghostPosOffset = dragRef.current.startX > ev.pageX ? 0 : 120;
176
+ if (dragRef.current.lineEl) {
177
+ dragRef.current.lineEl.style.left = "".concat(lineLeftPx, "px");
178
+ }
179
+ if (dragRef.current.ghostEl) {
180
+ dragRef.current.ghostEl.style.left = "".concat(ev.pageX - ghostPosOffset, "px");
181
+ }
182
+ };
183
+ var handleColumnDragEnd = function handleColumnDragEnd(ev) {
184
+ if (!dragRef.current.mouseDown) return;
185
+ dragRef.current.mouseDown = false;
186
+ try {
187
+ document.body.style.userSelect = dragRef.current.prevUserSelect || "";
188
+ document.body.style.webkitUserSelect = dragRef.current.prevWebkitUserSelect || "";
189
+ } catch (_a) {}
190
+ if (dragRef.current.active) {
191
+ var finalInsertionIndex_1 = computeInsertionFromPageX(ev.pageX).insertionIndex;
192
+ var sourceIndex_1 = dragRef.current.source;
193
+ var sheetIdx_1 = (0, _fortuneCore.getSheetIndex)(context, context.currentSheetId);
194
+ if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
195
+ setContext(function (draft) {
196
+ var _a, _b;
197
+ var _sheet = draft.luckysheetfile[sheetIdx_1];
198
+ if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
199
+ var rows = _sheet.data;
200
+ if (rows.length === 0) return;
201
+ var numCols = (_b = (_a = rows[0]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
202
+ if (sourceIndex_1 < 0 || sourceIndex_1 >= numCols) return;
203
+ var targetIndex = finalInsertionIndex_1;
204
+ if (targetIndex > sourceIndex_1) targetIndex -= 1;
205
+ for (var i = 0; i < rows.length; i += 1) {
206
+ var row = rows[i];
207
+ if (!row || sourceIndex_1 >= row.length) continue;
208
+ var cellData = row.splice(sourceIndex_1, 1)[0];
209
+ if (targetIndex < 0) targetIndex = 0;
210
+ if (targetIndex > row.length) targetIndex = row.length;
211
+ row.splice(targetIndex, 0, cellData);
212
+ }
213
+ _sheet.data = rows;
214
+ (0, _fortuneCore.updateContextWithSheetData)(draft, _sheet.data);
215
+ });
216
+ }
217
+ }
218
+ removeDragLine();
219
+ dragRef.current.active = false;
220
+ dragRef.current.source = -1;
221
+ if (dragRef.current.onDocMove) document.removeEventListener("mousemove", dragRef.current.onDocMove);
222
+ if (dragRef.current.onDocUp) document.removeEventListener("mouseup", dragRef.current.onDocUp);
223
+ dragRef.current.onDocMove = null;
224
+ dragRef.current.onDocUp = null;
225
+ };
226
+ var initiateDrag = function initiateDrag(clickedColIndex, startX) {
227
+ dragRef.current.mouseDown = true;
228
+ dragRef.current.startX = startX;
229
+ dragRef.current.source = clickedColIndex;
230
+ dragRef.current.active = false;
231
+ console.log("initiateDrag", startX);
232
+ dragRef.current.onDocMove = handleColumnDrag;
233
+ dragRef.current.onDocUp = handleColumnDragEnd;
234
+ document.addEventListener("mousemove", handleColumnDrag);
235
+ document.addEventListener("mouseup", handleColumnDragEnd);
236
+ };
237
+ return {
238
+ initiateDrag: initiateDrag,
239
+ getColIndexClicked: getColIndexClicked,
240
+ isColDoubleClicked: isColDoubleClicked
241
+ };
242
+ };
@@ -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,233 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useRowDragAndDrop = void 0;
7
+ var _react = require("react");
8
+ var _fortuneCore = require("@fileverse-dev/fortune-core");
9
+ var _context = _interopRequireDefault(require("../../../context"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ var useRowDragAndDrop = exports.useRowDragAndDrop = function useRowDragAndDrop(containerRef, selectedLocationRef) {
12
+ var DOUBLE_MS = 300;
13
+ var START_DRAG_THRESHOLD_PX = 6;
14
+ var clickRef = (0, _react.useRef)({
15
+ lastTime: 0,
16
+ lastRow: -1
17
+ });
18
+ var _a = (0, _react.useContext)(_context.default),
19
+ context = _a.context,
20
+ setContext = _a.setContext,
21
+ refs = _a.refs;
22
+ var dragRef = (0, _react.useRef)({
23
+ mouseDown: false,
24
+ startY: 0,
25
+ source: -1,
26
+ active: false,
27
+ lineEl: null,
28
+ ghostEl: null,
29
+ onDocMove: null,
30
+ onDocUp: null,
31
+ prevUserSelect: "",
32
+ prevWebkitUserSelect: "",
33
+ lastNativeEvent: null,
34
+ ghostHeightPx: 24
35
+ });
36
+ var removeDragLine = function removeDragLine() {
37
+ var _a = dragRef.current,
38
+ lineEl = _a.lineEl,
39
+ ghostEl = _a.ghostEl;
40
+ try {
41
+ if (lineEl === null || lineEl === void 0 ? void 0 : lineEl.parentElement) lineEl.parentElement.removeChild(lineEl);
42
+ } catch (_b) {}
43
+ try {
44
+ if (ghostEl === null || ghostEl === void 0 ? void 0 : ghostEl.parentElement) ghostEl.parentElement.removeChild(ghostEl);
45
+ } catch (_c) {}
46
+ dragRef.current.lineEl = null;
47
+ dragRef.current.ghostEl = null;
48
+ dragRef.current.active = false;
49
+ };
50
+ var getRowIndexClicked = function getRowIndexClicked(pageY, headerEl) {
51
+ var _a;
52
+ var rect = headerEl.getBoundingClientRect();
53
+ var mouseYInHeader = pageY - rect.top - window.scrollY;
54
+ var localYInHeader = mouseYInHeader + headerEl.scrollTop;
55
+ var freeze = (_a = refs.globalCache.freezen) === null || _a === void 0 ? void 0 : _a[context.currentSheetId];
56
+ var adjustedY = (0, _fortuneCore.fixPositionOnFrozenCells)(freeze, 0, localYInHeader, 0, mouseYInHeader).y;
57
+ var _b = (0, _fortuneCore.rowLocation)(adjustedY, context.visibledatarow),
58
+ rowIndex = _b[2];
59
+ return rowIndex;
60
+ };
61
+ var isRowDoubleClicked = function isRowDoubleClicked(clickedRowIndex) {
62
+ var now = performance.now();
63
+ var isDoubleClicked = now - clickRef.current.lastTime < DOUBLE_MS && clickRef.current.lastRow === clickedRowIndex;
64
+ clickRef.current.lastTime = now;
65
+ clickRef.current.lastRow = clickedRowIndex;
66
+ return isDoubleClicked;
67
+ };
68
+ var computeInsertionFromPageY = function computeInsertionFromPageY(pageY) {
69
+ var _a, _b, _c, _d, _e;
70
+ var workbookEl = containerRef.current;
71
+ if (!workbookEl) return {
72
+ insertionIndex: -1,
73
+ lineTopPx: 0,
74
+ mouseYInWorkbook: 0
75
+ };
76
+ var wbRect = workbookEl.getBoundingClientRect();
77
+ var mouseYInWorkbook = pageY - wbRect.top - window.scrollY;
78
+ 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);
79
+ var freeze = (_c = refs.globalCache.freezen) === null || _c === void 0 ? void 0 : _c[context.currentSheetId];
80
+ var yWorkbook = (0, _fortuneCore.fixPositionOnFrozenCells)(freeze, 0, localYInWorkbook, 0, mouseYInWorkbook).y;
81
+ var _f = (0, _fortuneCore.rowLocation)(yWorkbook, context.visibledatarow),
82
+ rowTopPx = _f[0],
83
+ rowBottomPx = _f[1],
84
+ hoveredRowIndex = _f[2];
85
+ var rowMidPx = (rowTopPx + rowBottomPx) / 2;
86
+ var insertionIndex = hoveredRowIndex + (yWorkbook > rowMidPx ? 1 : 0);
87
+ var sheetIdx = (0, _fortuneCore.getSheetIndex)(context, context.currentSheetId);
88
+ var sheetLocal = sheetIdx == null ? null : context.luckysheetfile[sheetIdx];
89
+ 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;
90
+ insertionIndex = Math.max(0, Math.min(maxRows, insertionIndex));
91
+ var lineTopPx = yWorkbook > rowMidPx ? rowBottomPx : rowTopPx;
92
+ return {
93
+ insertionIndex: insertionIndex,
94
+ lineTopPx: lineTopPx,
95
+ mouseYInWorkbook: mouseYInWorkbook
96
+ };
97
+ };
98
+ var createInsertionLine = function createInsertionLine(host) {
99
+ var el = document.createElement("div");
100
+ el.style.position = "absolute";
101
+ el.style.left = "0px";
102
+ el.style.right = "0px";
103
+ el.style.height = "2px";
104
+ el.style.background = "#EFC703";
105
+ el.style.zIndex = "9999";
106
+ el.style.pointerEvents = "none";
107
+ host.appendChild(el);
108
+ return el;
109
+ };
110
+ var createGhost = function createGhost(host) {
111
+ var el = document.createElement("div");
112
+ el.style.position = "fixed";
113
+ el.style.left = "45px";
114
+ el.style.width = "".concat(window.innerWidth, "px");
115
+ el.style.boxSizing = "border-box";
116
+ el.style.padding = "6px 8px";
117
+ el.style.background = "rgba(239,199,3,0.10)";
118
+ el.style.border = "1px solid #EFC703";
119
+ el.style.borderRadius = "6px";
120
+ el.style.zIndex = "10000";
121
+ el.style.pointerEvents = "none";
122
+ el.style.display = "flex";
123
+ el.style.alignItems = "center";
124
+ el.style.fontSize = "12px";
125
+ el.style.fontWeight = "500";
126
+ var ghostHeightPx = 24;
127
+ var ghostLabel = "".concat(dragRef.current.source + 1, " row");
128
+ var selectedBlock = selectedLocationRef.current.find(function (s) {
129
+ return s.r1 <= dragRef.current.source && dragRef.current.source <= s.r2;
130
+ });
131
+ if (selectedBlock) {
132
+ ghostHeightPx = Math.max(24, selectedBlock.row - selectedBlock.row_pre - 1);
133
+ var count = selectedBlock.r2 - selectedBlock.r1 + 1;
134
+ ghostLabel = count > 1 ? "".concat(count, " rows") : "".concat(selectedBlock.r1 + 1, " row");
135
+ } else {
136
+ var _a = (0, _fortuneCore.rowLocationByIndex)(dragRef.current.source, context.visibledatarow),
137
+ pre = _a[0],
138
+ end = _a[1];
139
+ var sourceRowHeight = end - pre - 1;
140
+ ghostHeightPx = Math.max(24, sourceRowHeight);
141
+ }
142
+ el.style.height = "".concat(ghostHeightPx, "px");
143
+ el.textContent = ghostLabel;
144
+ host.appendChild(el);
145
+ return {
146
+ el: el,
147
+ ghostHeightPx: ghostHeightPx
148
+ };
149
+ };
150
+ var isDragActivated = function isDragActivated(host, pixelDeltaY) {
151
+ if (dragRef.current.active) return true;
152
+ if (pixelDeltaY < START_DRAG_THRESHOLD_PX) return false;
153
+ dragRef.current.active = true;
154
+ dragRef.current.prevUserSelect = document.body.style.userSelect;
155
+ dragRef.current.prevWebkitUserSelect = document.body.style.webkitUserSelect;
156
+ document.body.style.userSelect = "none";
157
+ document.body.style.webkitUserSelect = "none";
158
+ dragRef.current.lineEl = createInsertionLine(host);
159
+ var _a = createGhost(host),
160
+ el = _a.el,
161
+ ghostHeightPx = _a.ghostHeightPx;
162
+ dragRef.current.ghostEl = el;
163
+ dragRef.current.ghostHeightPx = ghostHeightPx;
164
+ return true;
165
+ };
166
+ var handleRowDrag = function handleRowDrag(ev) {
167
+ if (!dragRef.current.mouseDown) return;
168
+ dragRef.current.lastNativeEvent = ev;
169
+ var host = containerRef.current;
170
+ if (!host) return;
171
+ var dragOffset = Math.abs(ev.pageY - dragRef.current.startY);
172
+ if (!isDragActivated(host, dragOffset)) return;
173
+ var lineTopPx = computeInsertionFromPageY(ev.pageY).lineTopPx;
174
+ var ghostPosOffset = dragRef.current.startY > ev.pageY ? 0 : 25;
175
+ if (dragRef.current.lineEl) {
176
+ dragRef.current.lineEl.style.top = "".concat(lineTopPx, "px");
177
+ }
178
+ if (dragRef.current.ghostEl) {
179
+ dragRef.current.ghostEl.style.top = "".concat(ev.pageY - ghostPosOffset, "px");
180
+ }
181
+ };
182
+ var handleRowDragEnd = function handleRowDragEnd(ev) {
183
+ if (!dragRef.current.mouseDown) return;
184
+ dragRef.current.mouseDown = false;
185
+ try {
186
+ document.body.style.userSelect = dragRef.current.prevUserSelect || "";
187
+ document.body.style.webkitUserSelect = dragRef.current.prevWebkitUserSelect || "";
188
+ } catch (_a) {}
189
+ if (dragRef.current.active) {
190
+ var finalInsertionIndex_1 = computeInsertionFromPageY(ev.pageY).insertionIndex;
191
+ var sourceIndex_1 = dragRef.current.source;
192
+ var sheetIdx_1 = (0, _fortuneCore.getSheetIndex)(context, context.currentSheetId);
193
+ if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
194
+ setContext(function (draft) {
195
+ var _sheet = draft.luckysheetfile[sheetIdx_1];
196
+ if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
197
+ var rows = _sheet.data;
198
+ if (sourceIndex_1 < 0 || sourceIndex_1 >= rows.length) return;
199
+ var targetIndex = finalInsertionIndex_1;
200
+ if (targetIndex > sourceIndex_1) targetIndex -= 1;
201
+ var rowData = rows.splice(sourceIndex_1, 1)[0];
202
+ if (targetIndex < 0) targetIndex = 0;
203
+ if (targetIndex > rows.length) targetIndex = rows.length;
204
+ rows.splice(targetIndex, 0, rowData);
205
+ _sheet.data = rows;
206
+ (0, _fortuneCore.updateContextWithSheetData)(draft, _sheet.data);
207
+ });
208
+ }
209
+ }
210
+ removeDragLine();
211
+ dragRef.current.active = false;
212
+ dragRef.current.source = -1;
213
+ if (dragRef.current.onDocMove) document.removeEventListener("mousemove", dragRef.current.onDocMove);
214
+ if (dragRef.current.onDocUp) document.removeEventListener("mouseup", dragRef.current.onDocUp);
215
+ dragRef.current.onDocMove = null;
216
+ dragRef.current.onDocUp = null;
217
+ };
218
+ var initiateDrag = function initiateDrag(clickedRowIndex, startY) {
219
+ dragRef.current.mouseDown = true;
220
+ dragRef.current.startY = startY;
221
+ dragRef.current.source = clickedRowIndex;
222
+ dragRef.current.active = false;
223
+ dragRef.current.onDocMove = handleRowDrag;
224
+ dragRef.current.onDocUp = handleRowDragEnd;
225
+ document.addEventListener("mousemove", handleRowDrag);
226
+ document.addEventListener("mouseup", handleRowDragEnd);
227
+ };
228
+ return {
229
+ initiateDrag: initiateDrag,
230
+ getRowIndexClicked: getRowIndexClicked,
231
+ isRowDoubleClicked: isRowDoubleClicked
232
+ };
233
+ };