Skip to content

Commit

Permalink
feat: Copy a range of cells and paste it into another range, extendin…
Browse files Browse the repository at this point in the history
…g the target range if it is too small - Unable to paste all. silevis#280
  • Loading branch information
qiufeihong2018 committed Dec 19, 2023
1 parent c6e5212 commit bd62435
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
32 changes: 16 additions & 16 deletions src/lib/Functions/expandGridIfNeeded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ export const expandGridIfNeeded = (
const currentRowCount = cellMatrix.rows.length;
const currentColumnCount = cellMatrix.columns.length;

// Add new rows if needed
if (endRow >= currentRowCount) {
const rowsToAdd = endRow - currentRowCount + 1;
const prefix = cellMatrix.rows[currentRowCount - 1];
for (let i = 0; i < rowsToAdd; i++) {
cellMatrix.rows.push({
...prefix,
idx: currentRowCount + i,
rowId: `row-${currentRowCount + i}`,
top: prefix.bottom,
bottom: prefix.bottom + prefix.height || CellMatrix.DEFAULT_ROW_HEIGHT,
});
}
updated = true;
}

// Add new columns if needed
if (endColumn >= currentColumnCount) {
const columnsToAdd = endColumn - currentColumnCount + 1;
Expand All @@ -59,6 +43,22 @@ export const expandGridIfNeeded = (
updated = true;
}

// Add new rows if needed
if (endRow >= currentRowCount) {
const rowsToAdd = endRow - currentRowCount + 1;
const prefix = cellMatrix.rows[currentRowCount - 1];
for (let i = 0; i < rowsToAdd; i++) {
cellMatrix.rows.push({
...prefix,
idx: currentRowCount + i,
rowId: `row-${currentRowCount + i}`,
top: prefix.bottom,
bottom: prefix.bottom + prefix.height || CellMatrix.DEFAULT_ROW_HEIGHT,
});
}
updated = true;
}

// If needed, add a new status column that returns only when the grid is actually updated
return updated ? { ...state, cellMatrix } : state;
};
11 changes: 7 additions & 4 deletions src/test/TestGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export const TestGrid: React.FC<TestGridProps> = (props) => {
// eslint-disable-next-line
rows[0].cells.find((cell) => cell.type === "text" && cell.text);


const handleChanges = (changes: CellChange<TestGridCells>[]) => {
setRows((prevRows) => {
const currentRows = [...prevRows];
Expand All @@ -329,13 +330,16 @@ export const TestGrid: React.FC<TestGridProps> = (props) => {
);
// Extension column
if (changeColumnIdx === -1) {
setColumns([
const newColumn = [
...columns,
{
columnId: change.columnId,
} as Column,
]);
changeColumnIdx = columns.length - 1;
] as Column[];
setColumns(newColumn);
changeColumnIdx = newColumn.findIndex(
(el) => el.columnId === change.columnId
);
}
let changeRowIdx = currentRows.findIndex(
(el) => el.rowId === change.rowId
Expand Down Expand Up @@ -373,7 +377,6 @@ export const TestGrid: React.FC<TestGridProps> = (props) => {
});
};


const reorderArray = <T extends unknown>(
arr: T[],
idxs: number[],
Expand Down
4 changes: 2 additions & 2 deletions src/test/testEnvConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const config: TestConfig = {
minCellWidth: 40,
fillHandleWidth: 18,

columns: 10,
rows: 10,
columns: 5,
rows: 5,

lineWidth: 1,

Expand Down

0 comments on commit bd62435

Please sign in to comment.