Skip to content

Commit

Permalink
feat: impl deleting blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
iyxan23 committed Oct 18, 2024
1 parent fcc4e63 commit ab2108a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/sheet/sheet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,4 +646,61 @@ describe("sheet tests", () => {

expect(sheet.getWholeRow({ row: 3 })).toEqual([10, 12, 3, null]);
});

it("deletes a block and fill from col", () => {
const sheet = new Sheet<number>([
[1, 2, 3, 8],
[4, 5, null, null],
[7, null, 9, 10],
[10, 11, 12, 3],
[null, 14, 15, 2],
]);

sheet.deleteBlock({
start: { row: 1, col: 1 },
end: { row: 2, col: 2 },
fillFrom: "col",
});

expect(sheet.getWholeRow({ row: 1 })).toEqual([4, null, null, null]);
expect(sheet.getWholeRow({ row: 2 })).toEqual([7, 10, null, null]);
});

it("deletes a block and fill from row", () => {
const sheet = new Sheet<number>([
[1, 2, 3, 8],
[4, 5, null, null],
[7, null, 9, 10],
[10, 11, 12, 3],
[null, 14, 15, 2],
]);

sheet.deleteBlock({
start: { row: 1, col: 1 },
end: { row: 2, col: 2 },
fillFrom: "row",
});

expect(sheet.getWholeRow({ row: 1 })).toEqual([4, 11, 12, null]);
expect(sheet.getWholeRow({ row: 2 })).toEqual([7, 14, 15, 10]);
});

it("deletes a block without filling", () => {
const sheet = new Sheet<number>([
[1, 2, 3, 8],
[4, 5, null, null],
[7, null, 9, 10],
[10, 11, 12, 3],
[null, 14, 15, 2],
]);

sheet.deleteBlock({
start: { row: 1, col: 1 },
end: { row: 2, col: 2 },
fillFrom: null,
});

expect(sheet.getWholeRow({ row: 1 })).toEqual([4, null, null, null]);
expect(sheet.getWholeRow({ row: 2 })).toEqual([7, null, null, 10]);
});
});
35 changes: 35 additions & 0 deletions src/sheet/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,41 @@ export class Sheet<T> {
this.moveBlock({ colStart: col + 1, rowStart: 0 }, { col: col });
}

deleteBlock({
start,
end,
fillFrom,
}: {
start: { col: number; row: number };
end: { col: number; row: number };
fillFrom: "col" | "row" | null;
}) {
const width = end.col - start.col + 1;
const height = end.row - start.row + 1;

for (let row = start.row; row <= end.row; row++) {
for (let col = start.col; col <= end.col; col++) {
this.setCell(col, row, null);
}
}

if (fillFrom === "col") {
this.moveBlock(
{ colStart: start.col + width, rowStart: start.row, rowEnd: end.row },
{ col: start.col, row: start.row },
);
} else if (fillFrom === "row") {
this.moveBlock(
{
rowStart: start.row + height,
colStart: start.col,
colEnd: end.col,
},
{ row: start.row, col: start.col },
);
}
}

// todo: faster method by moving a big block at the start, then actually
// clone them one-by-one without insertMapAfterCol
cloneMapCol({
Expand Down

0 comments on commit ab2108a

Please sign in to comment.