Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete empty cells #494

Merged
merged 9 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/grid/actions/DeleteCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@ export const DeleteCells = async (args: DeleteCellsArgs) => {

if (create_transaction ?? true) sheetController.start_transaction();

// cancel transaction if there are no cells to delete
let deletingCells = false;

// delete cells row by row
for (var current_row = y0; current_row <= y1; current_row++) {
const cells_to_delete = sheetController.sheet.grid.getNakedCells(x0, current_row, x1, current_row);

// delete cells
await updateCellAndDCells({
starting_cells: cells_to_delete,
sheetController,
pyodide,
delete_starting_cells: true,
app,
create_transaction: false,
});
if (cells_to_delete.length !== 0) {
deletingCells = true;
await updateCellAndDCells({
starting_cells: cells_to_delete,
sheetController,
pyodide,
delete_starting_cells: true,
app,
create_transaction: false,
});
}
}

if (create_transaction ?? true) sheetController.end_transaction();
if (create_transaction ?? true) {
if (!deletingCells) {
sheetController.cancel_transaction();
} else {
sheetController.end_transaction();
}
}
};
8 changes: 8 additions & 0 deletions src/grid/controller/sheetController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ export class SheetController {
return reverse_transaction;
}

public cancel_transaction(): void {
if (!this.transaction_in_progress || !this.transaction_in_progress_reverse) {
throw new Error('No transaction in progress.');
}
this.transaction_in_progress = undefined;
this.transaction_in_progress_reverse = undefined;
}

public predefined_transaction(statements: Statement[]): Transaction {
// Starts a transaction, executes all statements, and ends the transaction.
// Returns the transaction.
Expand Down