ztxkui 4.2.23-552 → 4.2.23-553
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.
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// 监听是否摁下ctrl键
|
|
2
|
+
import { useRef, useEffect, useCallback } from 'react';
|
|
3
|
+
export default function useCtrl() {
|
|
4
|
+
var ctrlKeyPressedRef = useRef(false);
|
|
5
|
+
var getCtrlKeyPressed = useCallback(function (e) {
|
|
6
|
+
return e.ctrlKey || e.metaKey;
|
|
7
|
+
}, []);
|
|
8
|
+
// 监听 Ctrl 键状态
|
|
9
|
+
useEffect(function () {
|
|
10
|
+
var handleKeyDown = function (e) {
|
|
11
|
+
if (getCtrlKeyPressed(e)) {
|
|
12
|
+
ctrlKeyPressedRef.current = true;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var handleKeyUp = function (e) {
|
|
16
|
+
if (!getCtrlKeyPressed(e)) {
|
|
17
|
+
ctrlKeyPressedRef.current = false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
21
|
+
document.addEventListener('keyup', handleKeyUp);
|
|
22
|
+
return function () {
|
|
23
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
24
|
+
document.removeEventListener('keyup', handleKeyUp);
|
|
25
|
+
};
|
|
26
|
+
}, []);
|
|
27
|
+
return {
|
|
28
|
+
ctrlKeyPressedRef: ctrlKeyPressedRef,
|
|
29
|
+
getCtrlKeyPressed: getCtrlKeyPressed,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -2,8 +2,10 @@ import i18next from 'ztxkutils/dist/i18next';
|
|
|
2
2
|
import { useRef } from 'react';
|
|
3
3
|
import { useDrag, useDrop } from 'react-dnd';
|
|
4
4
|
import { tableDragRowType } from '../constants';
|
|
5
|
+
import useCtrl from './useCtrl';
|
|
5
6
|
function useDropRef(index, moverow) {
|
|
6
7
|
var ref = useRef(null);
|
|
8
|
+
var ctrlKeyPressedRef = useCtrl().ctrlKeyPressedRef;
|
|
7
9
|
/**
|
|
8
10
|
* @description 定义拖拽项
|
|
9
11
|
*/
|
|
@@ -13,7 +15,14 @@ function useDropRef(index, moverow) {
|
|
|
13
15
|
collect: function (monitor) { return ({
|
|
14
16
|
isDragging: monitor.isDragging(),
|
|
15
17
|
}); },
|
|
16
|
-
canDrag: function (
|
|
18
|
+
canDrag: function () {
|
|
19
|
+
var _a;
|
|
20
|
+
// 如果摁下了ctrl键 不可以拖拽行
|
|
21
|
+
if (ctrlKeyPressedRef.current) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return !((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.tagName.toLowerCase().match(/input|textarea/));
|
|
25
|
+
},
|
|
17
26
|
}); }, [index]), drag = _a[1];
|
|
18
27
|
/**
|
|
19
28
|
* @description 定义可放置项
|
|
@@ -3,10 +3,12 @@ import { useRef, useEffect, useCallback } from 'react';
|
|
|
3
3
|
import { domFind, domParentsUntil } from '../utils/dom';
|
|
4
4
|
import { plus } from 'number-precision';
|
|
5
5
|
import { copyValueHandle } from '../../utils/index';
|
|
6
|
+
import useCtrl from './useCtrl';
|
|
6
7
|
export default function useSelectSubtotal(tableRef, props) {
|
|
7
8
|
var onMoveRow = props.onMoveRow, onEditableSave = props.onEditableSave;
|
|
8
9
|
var tipRef = useRef();
|
|
9
10
|
var rowResultRef = useRef();
|
|
11
|
+
var getCtrlKeyPressed = useCtrl().getCtrlKeyPressed;
|
|
10
12
|
// 找到每个单元格实际对应的坐标
|
|
11
13
|
var cellPosition = useCallback(function (container) {
|
|
12
14
|
console.time(i18next.t('开始计算耗时'));
|
|
@@ -88,9 +90,9 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
88
90
|
// // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
89
91
|
// }, [dataSource, cellPosition]);
|
|
90
92
|
useEffect(function () {
|
|
91
|
-
if (onMoveRow || onEditableSave) {
|
|
92
|
-
|
|
93
|
-
}
|
|
93
|
+
// if (onMoveRow || onEditableSave) {
|
|
94
|
+
// return;
|
|
95
|
+
// }
|
|
94
96
|
var tableContent = domFind(tableRef.current, '.ant-table-content');
|
|
95
97
|
if (!tableContent) {
|
|
96
98
|
tableContent = domFind(tableRef.current, '.ant-table-container');
|
|
@@ -108,13 +110,18 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
108
110
|
}
|
|
109
111
|
var mousedownHandler = function (e) {
|
|
110
112
|
var _a;
|
|
113
|
+
// 当编辑模式时,需要摁下ctrl键才可以复制。
|
|
114
|
+
if ((onMoveRow || onEditableSave) && !getCtrlKeyPressed(e)) {
|
|
115
|
+
clearSelections(); // 清除之前的选择
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
111
118
|
// 这里要判断如果最上层是TD也行
|
|
112
119
|
var target = ((_a = e.target) === null || _a === void 0 ? void 0 : _a.tagName) === 'TD' ? e.target : domParentsUntil(e.target, 'td');
|
|
113
120
|
if ((target === null || target === void 0 ? void 0 : target.tagName) === 'TD') {
|
|
114
121
|
isDragging = true;
|
|
115
122
|
startCell = target;
|
|
116
123
|
// 是否按下ctrl键 可以选取多段内容
|
|
117
|
-
if (!e
|
|
124
|
+
if (!getCtrlKeyPressed(e)) {
|
|
118
125
|
clearSelections();
|
|
119
126
|
}
|
|
120
127
|
}
|
|
@@ -153,7 +160,7 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
153
160
|
}
|
|
154
161
|
}
|
|
155
162
|
// 是否按下ctrl键
|
|
156
|
-
if (!e
|
|
163
|
+
if (!getCtrlKeyPressed(e)) {
|
|
157
164
|
container.classList.remove('table-select-none');
|
|
158
165
|
}
|
|
159
166
|
};
|
|
@@ -164,7 +171,7 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
164
171
|
lastEndCell = e.target;
|
|
165
172
|
if (startCell !== lastEndCell) {
|
|
166
173
|
container.classList.add('table-select-none');
|
|
167
|
-
var isCtrl = e
|
|
174
|
+
var isCtrl = getCtrlKeyPressed(e);
|
|
168
175
|
selectCells(startCell, e.target, isCtrl); // 仅在必要时更新选择
|
|
169
176
|
}
|
|
170
177
|
}
|
|
@@ -172,7 +179,7 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
172
179
|
};
|
|
173
180
|
container.addEventListener('mousemove', mousemoveHandler);
|
|
174
181
|
var selectCells = function (start, end, isCtrl) {
|
|
175
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
182
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
176
183
|
if (!isCtrl) {
|
|
177
184
|
clearSelections(); // 清除之前的选择
|
|
178
185
|
}
|
|
@@ -206,25 +213,31 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
206
213
|
var lenRow = Math.max(startRow, endRow);
|
|
207
214
|
var initCell = Math.min(startCol, endCol);
|
|
208
215
|
var lenCell = Math.max(startCol, endCol);
|
|
216
|
+
// 标记当前选择区域的单元格为选中状态
|
|
209
217
|
for (var i = initRow; i <= lenRow; i++) {
|
|
210
|
-
var positionArr = [];
|
|
211
218
|
for (var j = initCell; j <= lenCell; j++) {
|
|
212
|
-
// const $td = container.rows[i]?.cells[j];
|
|
213
219
|
var $td = (_f = (_e = rowResult[i - 1]) === null || _e === void 0 ? void 0 : _e[j]) === null || _f === void 0 ? void 0 : _f.td;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
220
|
+
(_g = $td === null || $td === void 0 ? void 0 : $td.classList) === null || _g === void 0 ? void 0 : _g.add('move-selected');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
// 重新构建 textArr 和 textPositionArr(基于所有已选中的单元格)
|
|
224
|
+
textArr = [];
|
|
225
|
+
textPositionArr = [];
|
|
226
|
+
// 遍历所有单元格,按行组织已选中的单元格
|
|
227
|
+
var rowsCount = rowResult.length;
|
|
228
|
+
var colsCount = ((_h = rowResult[0]) === null || _h === void 0 ? void 0 : _h.length) || 0;
|
|
229
|
+
for (var i = 0; i < rowsCount; i++) {
|
|
230
|
+
var rowPositionArr = [];
|
|
231
|
+
for (var j = 0; j < colsCount; j++) {
|
|
232
|
+
var $td = (_k = (_j = rowResult[i]) === null || _j === void 0 ? void 0 : _j[j]) === null || _k === void 0 ? void 0 : _k.td;
|
|
233
|
+
if ($td && $td.classList.contains('move-selected')) {
|
|
234
|
+
var innerText = getCellValue($td);
|
|
221
235
|
textArr.push(innerText);
|
|
222
|
-
|
|
236
|
+
rowPositionArr.push(innerText);
|
|
223
237
|
}
|
|
224
|
-
(_g = $td === null || $td === void 0 ? void 0 : $td.classList) === null || _g === void 0 ? void 0 : _g.add('move-selected');
|
|
225
238
|
}
|
|
226
|
-
if (
|
|
227
|
-
textPositionArr.push(
|
|
239
|
+
if (rowPositionArr.length > 0) {
|
|
240
|
+
textPositionArr.push(rowPositionArr);
|
|
228
241
|
}
|
|
229
242
|
}
|
|
230
243
|
};
|
|
@@ -244,6 +257,7 @@ export default function useSelectSubtotal(tableRef, props) {
|
|
|
244
257
|
var isCtrlPressed = event.ctrlKey || event.metaKey;
|
|
245
258
|
// 如果同时按下了Ctrl/Cmd键和C键,则执行你的复制操作
|
|
246
259
|
if (isCtrlPressed && isCKey && textPositionArr.length > 0) {
|
|
260
|
+
console.log('复制文本textPositionArr', textPositionArr);
|
|
247
261
|
// 执行你的复制操作,例如将文本添加到剪贴板
|
|
248
262
|
var textToCopy = textPositionArr
|
|
249
263
|
.map(function (row) { return row.join('\t'); })
|
|
@@ -304,3 +318,52 @@ function sumArrayValues(arr) {
|
|
|
304
318
|
}
|
|
305
319
|
return sum;
|
|
306
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* 判断传入的dom元素中的值
|
|
323
|
+
* 一种情况就是直接通过innerText获取
|
|
324
|
+
* 另外一种就是通过输入框获取
|
|
325
|
+
* @param td 表格单元格元素
|
|
326
|
+
* @returns 单元格中的文本值
|
|
327
|
+
*/
|
|
328
|
+
function getCellValue(td) {
|
|
329
|
+
var _a, _b, _c, _d;
|
|
330
|
+
if (!td) {
|
|
331
|
+
return '';
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
// 查找 antd Select 显示的值
|
|
335
|
+
var antdSelect = td.querySelector('.ant-select-selection-item');
|
|
336
|
+
if (antdSelect) {
|
|
337
|
+
return (_a = antdSelect.textContent) !== null && _a !== void 0 ? _a : '';
|
|
338
|
+
}
|
|
339
|
+
// 优先查找输入框元素的值
|
|
340
|
+
var inputElement = td.querySelector('input, textarea');
|
|
341
|
+
if (inputElement) {
|
|
342
|
+
return (_b = inputElement.value) !== null && _b !== void 0 ? _b : '';
|
|
343
|
+
}
|
|
344
|
+
// 查找带 contenteditable 属性的元素
|
|
345
|
+
var editableElement = td.querySelector('[contenteditable="true"]');
|
|
346
|
+
if (editableElement && editableElement.innerText !== undefined) {
|
|
347
|
+
return editableElement.innerText;
|
|
348
|
+
}
|
|
349
|
+
// 查找 antd 相关组件的输入值
|
|
350
|
+
var antdInput = td.querySelector('.ant-input, .ant-input-number-input');
|
|
351
|
+
if (antdInput) {
|
|
352
|
+
if ('value' in antdInput) {
|
|
353
|
+
return (_c = antdInput.value) !== null && _c !== void 0 ? _c : '';
|
|
354
|
+
}
|
|
355
|
+
return (_d = antdInput.textContent) !== null && _d !== void 0 ? _d : '';
|
|
356
|
+
}
|
|
357
|
+
// 默认通过 innerText 获取
|
|
358
|
+
var innerText = td.innerText;
|
|
359
|
+
if (innerText !== undefined && innerText !== null) {
|
|
360
|
+
// 去除 "复制" 等后缀文字
|
|
361
|
+
return innerText.replace(/\s+复制/g, '').trim();
|
|
362
|
+
}
|
|
363
|
+
return '';
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
console.error(i18next.t('获取单元格值失败'), error);
|
|
367
|
+
return '';
|
|
368
|
+
}
|
|
369
|
+
}
|