es-grid-template 1.4.5 → 1.4.7

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.
@@ -1,5 +1,5 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
- import React, { Fragment, useEffect, useMemo, useRef, useState } from 'react';
2
+ import React, { Fragment, useMemo, useRef, useState } from 'react';
3
3
  import customParseFormat from 'dayjs/plugin/customParseFormat';
4
4
  import classNames from "classnames";
5
5
  import { Button, Dropdown, Form, Modal, Typography } from "antd";
@@ -950,7 +950,7 @@ const GridEdit = props => {
950
950
  const isEditing = React.useCallback(record => {
951
951
  return record[rowKey] === editingKey;
952
952
  }, [editingKey, rowKey]);
953
- useEffect(() => {
953
+ React.useEffect(() => {
954
954
  const handleClickOutside = event => {
955
955
  const element = event.target;
956
956
 
@@ -990,9 +990,11 @@ const GridEdit = props => {
990
990
  setRangeCells(new Set());
991
991
  }
992
992
  };
993
- document.addEventListener("click", handleClickOutside);
993
+ document.addEventListener('mousedown', handleClickOutside);
994
+ document.addEventListener('touchstart', handleClickOutside);
994
995
  return () => {
995
- document.removeEventListener("click", handleClickOutside);
996
+ document.removeEventListener('mousedown', handleClickOutside);
997
+ document.removeEventListener('touchstart', handleClickOutside);
996
998
  };
997
999
  }, [id]);
998
1000
  const triggerDragPaste = pastesArray => {
@@ -1114,210 +1116,482 @@ const GridEdit = props => {
1114
1116
  }
1115
1117
  pasteCells.current = new Set();
1116
1118
  };
1117
- const handlePointDoubleClick = e => {
1118
- // e.preventDefault()
1119
- e.stopPropagation();
1120
- const colStart = getFirstSelectCell(selectedCells.current).col;
1121
- const colEnd = getLastSelectCell(selectedCells.current).col;
1122
- const startPasteRow = getLastSelectCell(selectedCells.current).row;
1123
- const newPasteCells = new Set();
1124
- for (let r = Math.min(startPasteRow, dataSource.length - 1) + 1; r <= Math.max(startPasteRow, dataSource.length - 1); r++) {
1125
- for (let c = Math.min(colStart, colStart); c <= Math.max(colStart, colEnd); c++) {
1126
- newPasteCells.add(`${r}-${c}`);
1127
- }
1128
- }
1129
- hideDraggingPoint(selectedCells.current, id);
1130
- triggerDragPaste(newPasteCells);
1131
- };
1132
- const handleMouseDown = (record, row, col, e) => {
1133
- if (e.button === 2) {
1134
- e.stopPropagation();
1135
- return;
1136
- }
1137
- if (editingKey && editingKey === record[rowKey]) {
1138
- return;
1139
- }
1140
- if (record[rowKey] !== editingKey && editingKey !== '') {
1141
- setEditingKey('');
1142
- }
1119
+ const handlePasted = (record, indexCol, rowNumber, pasteData) => {
1120
+ const rows = pasteData.slice(0, onCellPaste?.maxRowsPaste ?? 200);
1121
+ if (!record?.parentId) {
1122
+ // Cập nhật data mới
1123
+ const newData = [...dataSource];
1143
1124
 
1144
- // isDragMouse.current = true
1145
- isMouseDown.current = true;
1146
- if (e.ctrlKey) {
1147
- isSelecting.current = true;
1148
- startCell.current = {
1149
- row,
1150
- col
1151
- };
1125
+ // const indexRows = newData.findIndex((it) => it[rowKey as any] === record[rowKey])
1152
1126
 
1153
- // const cell: any = new Set([`${row}-${col}`])
1127
+ // Lấy vị trí bắt đầu
1128
+ // const { row: startRow, col: startCol } = selectedCell;
1129
+ const startRow = newData.findIndex(it => it[rowKey] === record[rowKey]);
1130
+ const startCol = indexCol;
1154
1131
 
1155
- // setCurrentCtrlCells(cell)
1156
- } else {
1157
- isSelecting.current = true;
1158
- startCell.current = {
1159
- row,
1160
- col
1161
- };
1162
- const target = e.target;
1163
- if (target.closest('.dragging-point')) {
1164
- e.stopPropagation();
1165
- e.preventDefault();
1166
- return; // Không xử lý gì cả
1167
- } else {
1168
- // setStartSelectedCell({row, col})
1132
+ // const flattData = flattenArray(newData);
1169
1133
 
1170
- startSelectedCells.current = {
1171
- row,
1172
- col
1173
- };
1134
+ const pastedRows = [];
1135
+ const pastedColumns = new Set();
1136
+ rows.forEach((rowValues, rowIndex) => {
1137
+ const targetRow = startRow + rowIndex;
1174
1138
 
1175
- // setSelectedCells(new Set([`${row}-${col}`]));
1139
+ // Nếu vượt quá số dòng hiện có, thêm dòng mới
1140
+ if (targetRow >= newData.length) {
1141
+ // @ts-ignore
1142
+ // newData.push({ id: newGuid()});
1143
+ newData.push({
1144
+ id: undefined,
1145
+ rowId: newGuid()
1146
+ });
1147
+ }
1148
+ rowValues.forEach((cellValue, colIndex) => {
1149
+ const targetCol = startCol + colIndex;
1150
+ if (targetCol >= visibleCols.length) {
1151
+ // Không vượt quá số cột
1152
+ return;
1153
+ }
1154
+ if (visibleCols[targetCol].editEnable) {
1155
+ // @ts-ignore
1156
+ const columnKey = visibleCols[targetCol].field;
1176
1157
 
1177
- const cells = new Set([`${row}-${col}`]);
1178
- if (selectedCells.current && selectedCells.current.size > 0) {
1179
- if (!isEqualSet(cells, rangeCells)) {
1180
- // onRemoveBgSelectedCell(rangeCells, id)
1181
- // onRemoveBorderSelectedCell(rangeCells, id)
1182
- hideDraggingPoint(rangeCells, id);
1183
- onRemoveBgCellIndex(selectedCells.current, id);
1158
+ // @ts-ignore
1159
+ newData[targetRow] = {
1160
+ ...newData[targetRow],
1161
+ [columnKey]: cellValue.trim()
1162
+ };
1163
+ pastedColumns.add(columnKey);
1184
1164
  }
1185
- }
1186
- if (rowsSelected.current && rowsSelected.current.size > 0) {
1187
- removeClassCellIndexSelected(rowsSelected.current, id);
1188
- }
1189
- if (!isEqualSet(cells, selectedCells.current)) {
1190
- onRemoveBgSelectedCell(selectedCells.current, id);
1191
- hideDraggingPoint(selectedCells.current, id);
1192
- selectedCells.current = cells;
1193
- // setRangeCells(cells)
1165
+ });
1194
1166
 
1195
- onAddBgCellIndex(cells, id);
1196
- onAddBorderSelectedCell(cells, id);
1197
- onAddBgSelectedCell(selectedCells.current, id, false);
1198
- showDraggingPoint(selectedCells.current, id);
1199
- }
1167
+ // Lưu dòng được paste
1168
+ pastedRows.push(newData[targetRow]);
1169
+ });
1170
+ const pastedColumnsArray = Array.from(pastedColumns) ?? [];
1171
+ triggerPaste?.(pastedRows, pastedColumnsArray, newData);
1172
+ } else {
1173
+ // Cập nhật data mới
1174
+ const newData = [...dataSource];
1175
+ const parent = findItemByKey(newData, rowKey, record.parentId);
1200
1176
 
1201
- // setRowsSelected(new Set())
1202
- rowsSelected.current = new Set();
1203
- }
1204
- }
1205
- };
1206
- const handleMouseUp = e => {
1207
- isSelecting.current = false;
1208
- startCell.current = null;
1209
- isSelectingRow.current = false;
1210
- rowStart.current = null;
1211
- isDragMouse.current = false;
1212
- isMouseDown.current = false;
1213
- setIsPasteDragging(false);
1214
- if (e.ctrlKey) {
1177
+ // Cập nhật childData mới
1178
+ const childData = parent?.children ? [...parent.children] : [];
1215
1179
 
1216
- // setCtrlCells([...ctrlCells, currentCtrlCells])
1217
- }
1180
+ // Lấy vị trí bắt đầu
1181
+ // const { row: startRow, col: startCol } = selectedCell;
1182
+ const startRow = childData.findIndex(it => it[rowKey] === record[rowKey]);
1183
+ const startCol = indexCol;
1184
+ const pastedRows = [];
1185
+ const pastedColumns = new Set();
1186
+ rows.forEach((rowValues, rowIndex) => {
1187
+ const targetRow = startRow + rowIndex;
1218
1188
 
1219
- // nếu ctrlCell length > 0 thì set selectCells
1189
+ // Nếu vượt quá số dòng hiện có, thêm dòng mới
1190
+ if (targetRow >= childData.length) {
1191
+ childData.push({
1192
+ id: undefined,
1193
+ rowId: newGuid(),
1194
+ parentId: parent[rowKey ?? 'id']
1195
+ });
1196
+ }
1197
+ rowValues.forEach((cellValue, colIndex) => {
1198
+ const targetCol = startCol + colIndex;
1199
+ if (targetCol >= visibleCols.length) {
1200
+ // Không vượt quá số cột
1201
+ return;
1202
+ }
1203
+ if (visibleCols[targetCol].editEnable) {
1204
+ // @ts-ignore
1205
+ const columnKey = visibleCols[targetCol].field;
1220
1206
 
1221
- if (pasteCells && pasteCells.current.size > 0) {
1222
- triggerDragPaste(pasteCells.current);
1223
- } else {
1224
- setRangeCells(selectedCells.current);
1225
- const target = e.target;
1226
- if (target.closest('.dragging-point')) {
1227
- e.stopPropagation();
1228
- e.preventDefault();
1229
- return; // Không xử lý gì cả
1230
- }
1231
- if (selectedCells.current && selectedCells.current.size > 1) {
1232
- hideDraggingPoint(selectedCells.current, id);
1233
- onAddBorderSelectedCell(selectedCells.current, id);
1207
+ // @ts-ignore
1208
+ childData[targetRow] = {
1209
+ ...childData[targetRow],
1210
+ [columnKey]: cellValue.trim()
1211
+ };
1212
+ pastedColumns.add(columnKey);
1213
+ }
1214
+ });
1234
1215
 
1235
- // showDraggingPoint(selectedCells.current, id)
1236
- }
1237
- showDraggingPoint(selectedCells.current, id);
1238
- // onAddBorderSelectedCell(selectedCells.current, id)
1216
+ // Lưu dòng được paste
1217
+ pastedRows.push(childData[targetRow]);
1218
+ });
1219
+ const pastedColumnsArray = Array.from(pastedColumns) ?? [];
1220
+ const newRowData = {
1221
+ ...parent,
1222
+ children: childData
1223
+ };
1224
+ const newDataSource = updateArrayByKey(newData, newRowData, rowKey);
1225
+ triggerPaste?.(pastedRows, pastedColumnsArray, newDataSource);
1239
1226
  }
1240
1227
  };
1241
- const handleMouseEnter = (row, col, e) => {
1242
- if (!isSelecting.current || !startCell.current) {
1243
- return;
1244
- }
1245
- const {
1246
- row: startRow,
1247
- col: startCol
1248
- } = startCell.current;
1249
- if (e.ctrlKey) {
1250
- // const newCtrlCells = new Set();
1251
- // for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
1252
- // for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
1253
- // newCtrlCells.add(`${r}-${c}`)
1254
- // }
1255
- // }
1228
+ const handlePaste = (record, indexCol, rowNumber, e) => {
1229
+ // const clipboard: any = (e.clipboardData || (window && window?.Clipboard)).getData("text")
1230
+ const pasteData = e.clipboardData.getData("text/plain");
1256
1231
 
1257
- // setCurrentCtrlCells(newCtrlCells)
1232
+ // Chuyển đổi dữ liệu từ clipboard thành mảng
1233
+ const rowsPasted = pasteData.split("\n").map(row =>
1234
+ // const rows = pasteData.split("\n").map((row: any) =>
1235
+ row.replace(/\r/g, "").split("\t"));
1236
+ if (rowsPasted.length > (onCellPaste?.maxRowsPaste ?? 200)) {
1237
+ // bật popup thông báo
1258
1238
 
1259
- return;
1239
+ Modal.confirm({
1240
+ content: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Paragraph, {
1241
+ style: {
1242
+ marginBottom: '.25rem',
1243
+ fontSize: 14
1244
+ }
1245
+ }, "D\u1EEF li\u1EC7u sao ch\xE9p v\u01B0\u1EE3t qu\xE1 s\u1ED1 d\xF2ng cho ph\xE9p (500 d\xF2ng).Ph\u1EA7n m\u1EC1n s\u1EBD ch\u1EC9 l\u1EA5y 500 d\xF2ng \u0111\u1EA7u ti\xEAn."), /*#__PURE__*/React.createElement(Title, {
1246
+ level: 5,
1247
+ style: {
1248
+ marginTop: '.75rem'
1249
+ }
1250
+ }, "B\u1EA1n c\xF3 mu\u1ED1n ti\u1EBFp t\u1EE5c sao ch\xE9p kh\xF4ng?")),
1251
+ centered: true,
1252
+ className: 'be-popup-container',
1253
+ onOk: () => {
1254
+ handlePasted(record, indexCol, rowNumber, rowsPasted);
1255
+ }
1256
+ // footer: (_, { OkBtn, CancelBtn }) => (
1257
+ // <>
1258
+ // <OkBtn />
1259
+ // <CancelBtn />
1260
+ // </>
1261
+ // ),
1262
+ });
1263
+ } else {
1264
+ handlePasted(record, indexCol, rowNumber, rowsPasted);
1260
1265
  }
1261
- if (!isPasteDragging) {
1262
- // chọn vùng copy
1263
-
1264
- // setSelectIsDragging(true)
1265
1266
 
1266
- setIsPasteDragging(false);
1267
- // isPasteDragging.current = false
1268
-
1269
- const newSelectedCells = new Set();
1270
- for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
1271
- for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
1272
- newSelectedCells.add(`${r}-${c}`);
1273
- }
1267
+ // const rows = rowsPasted.slice(0, (onCellPaste?.maxRowsPaste ?? 200));
1268
+ //
1269
+ //
1270
+ // if (!record?.parentId ) {
1271
+ //
1272
+ // // Cập nhật data mới
1273
+ // const newData = [...dataSource];
1274
+ //
1275
+ // // @ts-ignore
1276
+ // const indexRows = newData.findIndex((it) => it[rowKey] === record[rowKey])
1277
+ //
1278
+ // // Lấy vị trí bắt đầu
1279
+ // // const { row: startRow, col: startCol } = selectedCell;
1280
+ // const startRow = indexRows
1281
+ // const startCol = indexCol
1282
+ //
1283
+ //
1284
+ //
1285
+ // // const flattData = flattenArray(newData);
1286
+ //
1287
+ // const pastedRows: RecordType[] = [];
1288
+ // const pastedColumns = new Set()
1289
+ //
1290
+ //
1291
+ // rows.forEach((rowValues: any, rowIndex: any) => {
1292
+ // const targetRow = startRow + rowIndex;
1293
+ //
1294
+ // // Nếu vượt quá số dòng hiện có, thêm dòng mới
1295
+ // if (targetRow >= newData.length) {
1296
+ // // @ts-ignore
1297
+ // // newData.push({ id: newGuid()});
1298
+ // newData.push({ id: undefined, rowId: newGuid()});
1299
+ // }
1300
+ //
1301
+ // rowValues.forEach((cellValue: any, colIndex: any) => {
1302
+ // const targetCol = startCol + colIndex;
1303
+ // if (targetCol >= columns.length) { // Không vượt quá số cột
1304
+ // return
1305
+ // }
1306
+ //
1307
+ // if (columns[targetCol].editEnable) {
1308
+ // // @ts-ignore
1309
+ // const columnKey = columns[targetCol].field;
1310
+ //
1311
+ // // @ts-ignore
1312
+ // newData[targetRow] = { ...newData[targetRow], [columnKey]: cellValue.trim() };
1313
+ // pastedColumns.add(columnKey);
1314
+ // }
1315
+ //
1316
+ // });
1317
+ //
1318
+ // // Lưu dòng được paste
1319
+ // pastedRows.push(newData[targetRow]);
1320
+ //
1321
+ // });
1322
+ //
1323
+ // const pastedColumnsArray = Array.from(pastedColumns) ?? [];
1324
+ //
1325
+ // triggerPaste?.(pastedRows, pastedColumnsArray as string[], newData)
1326
+ //
1327
+ //
1328
+ // } else {
1329
+ //
1330
+ // // Cập nhật data mới
1331
+ // const newData = [...dataSource];
1332
+ //
1333
+ // const parent = findItemByKey(newData, rowKey as any, record.parentId)
1334
+ //
1335
+ // // Cập nhật childData mới
1336
+ // const childData: any[] = parent?.children ? [...parent.children] : []
1337
+ //
1338
+ //
1339
+ // // Lấy vị trí bắt đầu
1340
+ // // const { row: startRow, col: startCol } = selectedCell;
1341
+ // const startRow = childData.findIndex((it) => it[rowKey] === record[rowKey])
1342
+ // const startCol = indexCol
1343
+ //
1344
+ // const pastedRows: RecordType[] = []
1345
+ // const pastedColumns = new Set()
1346
+ //
1347
+ //
1348
+ // rows.forEach((rowValues: any, rowIndex: any) => {
1349
+ // const targetRow = startRow + rowIndex
1350
+ //
1351
+ // // Nếu vượt quá số dòng hiện có, thêm dòng mới
1352
+ // if (targetRow >= childData.length) {
1353
+ //
1354
+ // childData.push({ id: undefined, rowId: newGuid(), parentId: parent[rowKey ?? 'id']})
1355
+ // }
1356
+ //
1357
+ // rowValues.forEach((cellValue: any, colIndex: any) => {
1358
+ // const targetCol = startCol + colIndex
1359
+ // if (targetCol >= columns.length) { // Không vượt quá số cột
1360
+ // return
1361
+ // }
1362
+ //
1363
+ // if (columns[targetCol].editEnable) {
1364
+ //
1365
+ // // @ts-ignore
1366
+ // const columnKey = columns[targetCol].field
1367
+ //
1368
+ // // @ts-ignore
1369
+ // childData[targetRow] = { ...childData[targetRow], [columnKey]: cellValue.trim() }
1370
+ // pastedColumns.add(columnKey)
1371
+ // }
1372
+ //
1373
+ // })
1374
+ //
1375
+ // // Lưu dòng được paste
1376
+ // pastedRows.push(childData[targetRow])
1377
+ //
1378
+ // })
1379
+ //
1380
+ // const pastedColumnsArray = Array.from(pastedColumns) ?? []
1381
+ //
1382
+ // const newRowData = {...parent, children: childData}
1383
+ //
1384
+ // const newDataSource = updateArrayByKey(newData, newRowData, rowKey as string)
1385
+ //
1386
+ // triggerPaste?.(pastedRows, pastedColumnsArray as string[], newDataSource )
1387
+ // }
1388
+ };
1389
+ const handlePointDoubleClick = e => {
1390
+ // e.preventDefault()
1391
+ e.stopPropagation();
1392
+ const colStart = getFirstSelectCell(selectedCells.current).col;
1393
+ const colEnd = getLastSelectCell(selectedCells.current).col;
1394
+ const startPasteRow = getLastSelectCell(selectedCells.current).row;
1395
+ const newPasteCells = new Set();
1396
+ for (let r = Math.min(startPasteRow, dataSource.length - 1) + 1; r <= Math.max(startPasteRow, dataSource.length - 1); r++) {
1397
+ for (let c = Math.min(colStart, colStart); c <= Math.max(colStart, colEnd); c++) {
1398
+ newPasteCells.add(`${r}-${c}`);
1274
1399
  }
1400
+ }
1401
+ hideDraggingPoint(selectedCells.current, id);
1402
+ triggerDragPaste(newPasteCells);
1403
+ };
1404
+ const handleMouseDown = (record, row, col, e) => {
1405
+ if (e.button === 2) {
1406
+ e.stopPropagation();
1407
+ return;
1408
+ }
1409
+ if (editingKey && editingKey === record[rowKey]) {
1410
+ return;
1411
+ }
1412
+ if (record[rowKey] !== editingKey && editingKey !== '') {
1413
+ setTimeout(() => {
1414
+ setEditingKey('');
1415
+ });
1416
+ }
1275
1417
 
1276
- // setSelectedCells(newSelectedCells)
1418
+ // isDragMouse.current = true
1419
+ isMouseDown.current = true;
1420
+ if (e.ctrlKey) {
1421
+ isSelecting.current = true;
1422
+ startCell.current = {
1423
+ row,
1424
+ col
1425
+ };
1277
1426
 
1278
- if (selectedCells.current && selectedCells.current.size > 0) {
1279
- onRemoveBgSelectedCell(selectedCells.current, id);
1280
- }
1281
- selectedCells.current = newSelectedCells;
1282
- onAddBgSelectedCell(newSelectedCells, id);
1283
- } else {
1284
- // chọn vùng paste
1427
+ // const cell: any = new Set([`${row}-${col}`])
1285
1428
 
1286
- // setSelectIsDragging(false)
1429
+ // setCurrentCtrlCells(cell)
1430
+ } else {
1431
+ isSelecting.current = true;
1432
+ startCell.current = {
1433
+ row,
1434
+ col
1435
+ };
1436
+ const target = e.target;
1437
+ if (target.closest('.dragging-point')) {
1438
+ e.stopPropagation();
1439
+ e.preventDefault();
1440
+ return; // Không xử lý gì cả
1441
+ } else {
1442
+ // setStartSelectedCell({row, col})
1287
1443
 
1288
- // setIsPasteDragging(true) ////////
1444
+ startSelectedCells.current = {
1445
+ row,
1446
+ col
1447
+ };
1289
1448
 
1290
- const newSelectedCells = new Set();
1291
- for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
1292
- for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
1293
- newSelectedCells.add(`${r}-${c}`);
1294
- }
1295
- }
1296
- const colStart = getFirstSelectCell(selectedCells.current).col;
1297
- const colEnd = getLastSelectCell(selectedCells.current).col;
1298
- const rowSelectedEnd = getLastSelectCell(selectedCells.current).row;
1299
- if (row >= rowSelectedEnd) {
1300
- // kéo xuống dưới
1449
+ // setSelectedCells(new Set([`${row}-${col}`]));
1301
1450
 
1302
- const newPasteCells = new Set();
1303
- for (let r = Math.min(startRow, row) + 1; r <= Math.max(startRow, row); r++) {
1304
- for (let c = Math.min(colStart, col); c <= Math.max(colStart, colEnd); c++) {
1305
- newPasteCells.add(`${r}-${c}`);
1451
+ const cells = new Set([`${row}-${col}`]);
1452
+ if (selectedCells.current && selectedCells.current.size > 0) {
1453
+ if (!isEqualSet(cells, rangeCells)) {
1454
+ // onRemoveBgSelectedCell(rangeCells, id)
1455
+ // onRemoveBorderSelectedCell(rangeCells, id)
1456
+ hideDraggingPoint(rangeCells, id);
1457
+ onRemoveBgCellIndex(selectedCells.current, id);
1306
1458
  }
1307
1459
  }
1460
+ if (rowsSelected.current && rowsSelected.current.size > 0) {
1461
+ removeClassCellIndexSelected(rowsSelected.current, id);
1462
+ }
1463
+ if (!isEqualSet(cells, selectedCells.current)) {
1464
+ onRemoveBgSelectedCell(selectedCells.current, id);
1465
+ hideDraggingPoint(selectedCells.current, id);
1466
+ selectedCells.current = cells;
1467
+ // setRangeCells(cells)
1308
1468
 
1309
- // setPasteCells(newPasteCells)
1310
-
1311
- if (pasteCells.current && pasteCells.current.size > 0) {
1312
- removeClassBorderPasteCell(pasteCells.current, 'down', id);
1469
+ onAddBgCellIndex(cells, id);
1470
+ onAddBorderSelectedCell(cells, id);
1471
+ onAddBgSelectedCell(selectedCells.current, id, false);
1472
+ showDraggingPoint(selectedCells.current, id);
1313
1473
  }
1314
- pasteCells.current = newPasteCells;
1315
- addClassBorderPasteCell(newPasteCells, 'down', id);
1474
+
1475
+ // setRowsSelected(new Set())
1476
+ rowsSelected.current = new Set();
1316
1477
  }
1317
- if (row < rowSelectedEnd) {
1318
- // kéo lên trên
1478
+ }
1479
+ };
1480
+ const handleMouseUp = e => {
1481
+ isSelecting.current = false;
1482
+ startCell.current = null;
1483
+ isSelectingRow.current = false;
1484
+ rowStart.current = null;
1485
+ isDragMouse.current = false;
1486
+ isMouseDown.current = false;
1487
+ setIsPasteDragging(false);
1488
+ if (e.ctrlKey) {
1319
1489
 
1320
- const rowSelectedStart = getFirstSelectCell(selectedCells.current).row;
1490
+ // setCtrlCells([...ctrlCells, currentCtrlCells])
1491
+ }
1492
+
1493
+ // nếu ctrlCell length > 0 thì set selectCells
1494
+
1495
+ if (pasteCells && pasteCells.current.size > 0) {
1496
+ triggerDragPaste(pasteCells.current);
1497
+ } else {
1498
+ setRangeCells(selectedCells.current);
1499
+ const target = e.target;
1500
+ if (target.closest('.dragging-point')) {
1501
+ e.stopPropagation();
1502
+ e.preventDefault();
1503
+ return; // Không xử lý gì cả
1504
+ }
1505
+ if (selectedCells.current && selectedCells.current.size > 1) {
1506
+ hideDraggingPoint(selectedCells.current, id);
1507
+ onAddBorderSelectedCell(selectedCells.current, id);
1508
+
1509
+ // showDraggingPoint(selectedCells.current, id)
1510
+ }
1511
+ showDraggingPoint(selectedCells.current, id);
1512
+ // onAddBorderSelectedCell(selectedCells.current, id)
1513
+ }
1514
+ };
1515
+ const handleMouseEnter = (row, col, e) => {
1516
+ if (!isSelecting.current || !startCell.current) {
1517
+ return;
1518
+ }
1519
+ const {
1520
+ row: startRow,
1521
+ col: startCol
1522
+ } = startCell.current;
1523
+ if (e.ctrlKey) {
1524
+ // const newCtrlCells = new Set();
1525
+ // for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
1526
+ // for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
1527
+ // newCtrlCells.add(`${r}-${c}`)
1528
+ // }
1529
+ // }
1530
+
1531
+ // setCurrentCtrlCells(newCtrlCells)
1532
+
1533
+ return;
1534
+ }
1535
+ if (!isPasteDragging) {
1536
+ // chọn vùng copy
1537
+
1538
+ // setSelectIsDragging(true)
1539
+
1540
+ setIsPasteDragging(false);
1541
+ // isPasteDragging.current = false
1542
+
1543
+ const newSelectedCells = new Set();
1544
+ for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
1545
+ for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
1546
+ newSelectedCells.add(`${r}-${c}`);
1547
+ }
1548
+ }
1549
+
1550
+ // setSelectedCells(newSelectedCells)
1551
+
1552
+ if (selectedCells.current && selectedCells.current.size > 0) {
1553
+ onRemoveBgSelectedCell(selectedCells.current, id);
1554
+ }
1555
+ selectedCells.current = newSelectedCells;
1556
+ onAddBgSelectedCell(newSelectedCells, id);
1557
+ } else {
1558
+ // chọn vùng paste
1559
+
1560
+ // setSelectIsDragging(false)
1561
+
1562
+ // setIsPasteDragging(true) ////////
1563
+
1564
+ const newSelectedCells = new Set();
1565
+ for (let r = Math.min(startRow, row); r <= Math.max(startRow, row); r++) {
1566
+ for (let c = Math.min(startCol, col); c <= Math.max(startCol, col); c++) {
1567
+ newSelectedCells.add(`${r}-${c}`);
1568
+ }
1569
+ }
1570
+ const colStart = getFirstSelectCell(selectedCells.current).col;
1571
+ const colEnd = getLastSelectCell(selectedCells.current).col;
1572
+ const rowSelectedEnd = getLastSelectCell(selectedCells.current).row;
1573
+ if (row >= rowSelectedEnd) {
1574
+ // kéo xuống dưới
1575
+
1576
+ const newPasteCells = new Set();
1577
+ for (let r = Math.min(startRow, row) + 1; r <= Math.max(startRow, row); r++) {
1578
+ for (let c = Math.min(colStart, col); c <= Math.max(colStart, colEnd); c++) {
1579
+ newPasteCells.add(`${r}-${c}`);
1580
+ }
1581
+ }
1582
+
1583
+ // setPasteCells(newPasteCells)
1584
+
1585
+ if (pasteCells.current && pasteCells.current.size > 0) {
1586
+ removeClassBorderPasteCell(pasteCells.current, 'down', id);
1587
+ }
1588
+ pasteCells.current = newPasteCells;
1589
+ addClassBorderPasteCell(newPasteCells, 'down', id);
1590
+ }
1591
+ if (row < rowSelectedEnd) {
1592
+ // kéo lên trên
1593
+
1594
+ const rowSelectedStart = getFirstSelectCell(selectedCells.current).row;
1321
1595
  if (row < rowSelectedStart) {
1322
1596
  const newPasteCells = new Set();
1323
1597
  for (let r = Math.min(rowSelectedStart, row); r <= Math.max(rowSelectedStart, row) - 1; r++) {
@@ -1511,276 +1785,6 @@ const GridEdit = props => {
1511
1785
  e.clipboardData.setData("text/plain", copyText);
1512
1786
  Message(t ? t('CopySuccessful') : 'Copy Successful');
1513
1787
  };
1514
- const handlePasted = (record, indexCol, rowNumber, pasteData) => {
1515
- const rows = pasteData.slice(0, onCellPaste?.maxRowsPaste ?? 200);
1516
- if (!record?.parentId) {
1517
- // Cập nhật data mới
1518
- const newData = [...dataSource];
1519
-
1520
- // const indexRows = newData.findIndex((it) => it[rowKey as any] === record[rowKey])
1521
-
1522
- // Lấy vị trí bắt đầu
1523
- // const { row: startRow, col: startCol } = selectedCell;
1524
- const startRow = newData.findIndex(it => it[rowKey] === record[rowKey]);
1525
- const startCol = indexCol;
1526
-
1527
- // const flattData = flattenArray(newData);
1528
-
1529
- const pastedRows = [];
1530
- const pastedColumns = new Set();
1531
- rows.forEach((rowValues, rowIndex) => {
1532
- const targetRow = startRow + rowIndex;
1533
-
1534
- // Nếu vượt quá số dòng hiện có, thêm dòng mới
1535
- if (targetRow >= newData.length) {
1536
- // @ts-ignore
1537
- // newData.push({ id: newGuid()});
1538
- newData.push({
1539
- id: undefined,
1540
- rowId: newGuid()
1541
- });
1542
- }
1543
- rowValues.forEach((cellValue, colIndex) => {
1544
- const targetCol = startCol + colIndex;
1545
- if (targetCol >= visibleCols.length) {
1546
- // Không vượt quá số cột
1547
- return;
1548
- }
1549
- if (visibleCols[targetCol].editEnable) {
1550
- // @ts-ignore
1551
- const columnKey = visibleCols[targetCol].field;
1552
-
1553
- // @ts-ignore
1554
- newData[targetRow] = {
1555
- ...newData[targetRow],
1556
- [columnKey]: cellValue.trim()
1557
- };
1558
- pastedColumns.add(columnKey);
1559
- }
1560
- });
1561
-
1562
- // Lưu dòng được paste
1563
- pastedRows.push(newData[targetRow]);
1564
- });
1565
- const pastedColumnsArray = Array.from(pastedColumns) ?? [];
1566
- triggerPaste?.(pastedRows, pastedColumnsArray, newData);
1567
- } else {
1568
- // Cập nhật data mới
1569
- const newData = [...dataSource];
1570
- const parent = findItemByKey(newData, rowKey, record.parentId);
1571
-
1572
- // Cập nhật childData mới
1573
- const childData = parent?.children ? [...parent.children] : [];
1574
-
1575
- // Lấy vị trí bắt đầu
1576
- // const { row: startRow, col: startCol } = selectedCell;
1577
- const startRow = childData.findIndex(it => it[rowKey] === record[rowKey]);
1578
- const startCol = indexCol;
1579
- const pastedRows = [];
1580
- const pastedColumns = new Set();
1581
- rows.forEach((rowValues, rowIndex) => {
1582
- const targetRow = startRow + rowIndex;
1583
-
1584
- // Nếu vượt quá số dòng hiện có, thêm dòng mới
1585
- if (targetRow >= childData.length) {
1586
- childData.push({
1587
- id: undefined,
1588
- rowId: newGuid(),
1589
- parentId: parent[rowKey ?? 'id']
1590
- });
1591
- }
1592
- rowValues.forEach((cellValue, colIndex) => {
1593
- const targetCol = startCol + colIndex;
1594
- if (targetCol >= visibleCols.length) {
1595
- // Không vượt quá số cột
1596
- return;
1597
- }
1598
- if (visibleCols[targetCol].editEnable) {
1599
- // @ts-ignore
1600
- const columnKey = visibleCols[targetCol].field;
1601
-
1602
- // @ts-ignore
1603
- childData[targetRow] = {
1604
- ...childData[targetRow],
1605
- [columnKey]: cellValue.trim()
1606
- };
1607
- pastedColumns.add(columnKey);
1608
- }
1609
- });
1610
-
1611
- // Lưu dòng được paste
1612
- pastedRows.push(childData[targetRow]);
1613
- });
1614
- const pastedColumnsArray = Array.from(pastedColumns) ?? [];
1615
- const newRowData = {
1616
- ...parent,
1617
- children: childData
1618
- };
1619
- const newDataSource = updateArrayByKey(newData, newRowData, rowKey);
1620
- triggerPaste?.(pastedRows, pastedColumnsArray, newDataSource);
1621
- }
1622
- };
1623
- const handlePaste = (record, indexCol, rowNumber, e) => {
1624
- // const clipboard: any = (e.clipboardData || (window && window?.Clipboard)).getData("text")
1625
- const pasteData = e.clipboardData.getData("text/plain");
1626
-
1627
- // Chuyển đổi dữ liệu từ clipboard thành mảng
1628
- const rowsPasted = pasteData.split("\n").map(row =>
1629
- // const rows = pasteData.split("\n").map((row: any) =>
1630
- row.replace(/\r/g, "").split("\t"));
1631
- if (rowsPasted.length > (onCellPaste?.maxRowsPaste ?? 200)) {
1632
- // bật popup thông báo
1633
-
1634
- Modal.confirm({
1635
- content: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Paragraph, {
1636
- style: {
1637
- marginBottom: '.25rem',
1638
- fontSize: 14
1639
- }
1640
- }, "D\u1EEF li\u1EC7u sao ch\xE9p v\u01B0\u1EE3t qu\xE1 s\u1ED1 d\xF2ng cho ph\xE9p (500 d\xF2ng).Ph\u1EA7n m\u1EC1n s\u1EBD ch\u1EC9 l\u1EA5y 500 d\xF2ng \u0111\u1EA7u ti\xEAn."), /*#__PURE__*/React.createElement(Title, {
1641
- level: 5,
1642
- style: {
1643
- marginTop: '.75rem'
1644
- }
1645
- }, "B\u1EA1n c\xF3 mu\u1ED1n ti\u1EBFp t\u1EE5c sao ch\xE9p kh\xF4ng?")),
1646
- centered: true,
1647
- className: 'be-popup-container',
1648
- onOk: () => {
1649
- handlePasted(record, indexCol, rowNumber, rowsPasted);
1650
- }
1651
- // footer: (_, { OkBtn, CancelBtn }) => (
1652
- // <>
1653
- // <OkBtn />
1654
- // <CancelBtn />
1655
- // </>
1656
- // ),
1657
- });
1658
- } else {
1659
- handlePasted(record, indexCol, rowNumber, rowsPasted);
1660
- }
1661
-
1662
- // const rows = rowsPasted.slice(0, (onCellPaste?.maxRowsPaste ?? 200));
1663
- //
1664
- //
1665
- // if (!record?.parentId ) {
1666
- //
1667
- // // Cập nhật data mới
1668
- // const newData = [...dataSource];
1669
- //
1670
- // // @ts-ignore
1671
- // const indexRows = newData.findIndex((it) => it[rowKey] === record[rowKey])
1672
- //
1673
- // // Lấy vị trí bắt đầu
1674
- // // const { row: startRow, col: startCol } = selectedCell;
1675
- // const startRow = indexRows
1676
- // const startCol = indexCol
1677
- //
1678
- //
1679
- //
1680
- // // const flattData = flattenArray(newData);
1681
- //
1682
- // const pastedRows: RecordType[] = [];
1683
- // const pastedColumns = new Set()
1684
- //
1685
- //
1686
- // rows.forEach((rowValues: any, rowIndex: any) => {
1687
- // const targetRow = startRow + rowIndex;
1688
- //
1689
- // // Nếu vượt quá số dòng hiện có, thêm dòng mới
1690
- // if (targetRow >= newData.length) {
1691
- // // @ts-ignore
1692
- // // newData.push({ id: newGuid()});
1693
- // newData.push({ id: undefined, rowId: newGuid()});
1694
- // }
1695
- //
1696
- // rowValues.forEach((cellValue: any, colIndex: any) => {
1697
- // const targetCol = startCol + colIndex;
1698
- // if (targetCol >= columns.length) { // Không vượt quá số cột
1699
- // return
1700
- // }
1701
- //
1702
- // if (columns[targetCol].editEnable) {
1703
- // // @ts-ignore
1704
- // const columnKey = columns[targetCol].field;
1705
- //
1706
- // // @ts-ignore
1707
- // newData[targetRow] = { ...newData[targetRow], [columnKey]: cellValue.trim() };
1708
- // pastedColumns.add(columnKey);
1709
- // }
1710
- //
1711
- // });
1712
- //
1713
- // // Lưu dòng được paste
1714
- // pastedRows.push(newData[targetRow]);
1715
- //
1716
- // });
1717
- //
1718
- // const pastedColumnsArray = Array.from(pastedColumns) ?? [];
1719
- //
1720
- // triggerPaste?.(pastedRows, pastedColumnsArray as string[], newData)
1721
- //
1722
- //
1723
- // } else {
1724
- //
1725
- // // Cập nhật data mới
1726
- // const newData = [...dataSource];
1727
- //
1728
- // const parent = findItemByKey(newData, rowKey as any, record.parentId)
1729
- //
1730
- // // Cập nhật childData mới
1731
- // const childData: any[] = parent?.children ? [...parent.children] : []
1732
- //
1733
- //
1734
- // // Lấy vị trí bắt đầu
1735
- // // const { row: startRow, col: startCol } = selectedCell;
1736
- // const startRow = childData.findIndex((it) => it[rowKey] === record[rowKey])
1737
- // const startCol = indexCol
1738
- //
1739
- // const pastedRows: RecordType[] = []
1740
- // const pastedColumns = new Set()
1741
- //
1742
- //
1743
- // rows.forEach((rowValues: any, rowIndex: any) => {
1744
- // const targetRow = startRow + rowIndex
1745
- //
1746
- // // Nếu vượt quá số dòng hiện có, thêm dòng mới
1747
- // if (targetRow >= childData.length) {
1748
- //
1749
- // childData.push({ id: undefined, rowId: newGuid(), parentId: parent[rowKey ?? 'id']})
1750
- // }
1751
- //
1752
- // rowValues.forEach((cellValue: any, colIndex: any) => {
1753
- // const targetCol = startCol + colIndex
1754
- // if (targetCol >= columns.length) { // Không vượt quá số cột
1755
- // return
1756
- // }
1757
- //
1758
- // if (columns[targetCol].editEnable) {
1759
- //
1760
- // // @ts-ignore
1761
- // const columnKey = columns[targetCol].field
1762
- //
1763
- // // @ts-ignore
1764
- // childData[targetRow] = { ...childData[targetRow], [columnKey]: cellValue.trim() }
1765
- // pastedColumns.add(columnKey)
1766
- // }
1767
- //
1768
- // })
1769
- //
1770
- // // Lưu dòng được paste
1771
- // pastedRows.push(childData[targetRow])
1772
- //
1773
- // })
1774
- //
1775
- // const pastedColumnsArray = Array.from(pastedColumns) ?? []
1776
- //
1777
- // const newRowData = {...parent, children: childData}
1778
- //
1779
- // const newDataSource = updateArrayByKey(newData, newRowData, rowKey as string)
1780
- //
1781
- // triggerPaste?.(pastedRows, pastedColumnsArray as string[], newDataSource )
1782
- // }
1783
- };
1784
1788
  const onSubmit = formData => {
1785
1789
  try {
1786
1790
  // const record = (await form.validateFields()) as RecordType;