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

Master ref clear cell command laa #4490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions src/clipboard_handlers/cell_clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,10 @@ export class CellClipboardHandler extends AbstractCellClipboardHandler<
* Clear the clipped zones: remove the cells and clear the formatting
*/
private clearClippedZones(content: ClipboardContent) {
for (const row of content.cells) {
for (const cell of row) {
if (cell.cell) {
this.dispatch("CLEAR_CELL", cell.position);
}
}
}
this.dispatch("CLEAR_CELLS", {
sheetId: content.sheetId,
target: content.zones,
});
this.dispatch("CLEAR_FORMATTING", {
sheetId: content.sheetId,
target: content.zones,
Expand Down Expand Up @@ -271,7 +268,12 @@ export class CellClipboardHandler extends AbstractCellClipboardHandler<
format: origin.cell?.format,
});
} else if (targetCell) {
this.dispatch("CLEAR_CELL", target);
this.dispatch("UPDATE_CELL", {
...target,
content: "",
style: null,
format: "",
});
}
}

Expand Down
32 changes: 31 additions & 1 deletion src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
format: "",
});
break;

case "CLEAR_CELLS":
this.clearCells(cmd.sheetId, cmd.target);
break;
case "DELETE_CONTENT":
this.clearZones(cmd.sheetId, cmd.target);
break;
Expand Down Expand Up @@ -204,6 +206,26 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
}
}

/**
* Clear the styles, the format and the content of zones
*/
private clearCells(sheetId: UID, zones: Zone[]) {
for (const zone of recomputeZones(zones)) {
for (let col = zone.left; col <= zone.right; col++) {
for (let row = zone.top; row <= zone.bottom; row++) {
this.dispatch("UPDATE_CELL", {
sheetId: sheetId,
col,
row,
content: "",
style: null,
format: "",
});
}
}
}
}

/**
* Copy the style of the reference column/row to the new columns/rows.
*/
Expand Down Expand Up @@ -672,6 +694,14 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
return isInside(col, row, sheetZone) ? CommandResult.Success : CommandResult.TargetOutOfSheet;
}

// private checkZoneOutOfSheet(cmd: ZoneDependentCommand): CommandResult {
// const { sheetId, zone } = cmd;
// const sheet = this.getters.tryGetSheet(sheetId);
// if (!sheet) return CommandResult.InvalidSheetId;
// const sheetZone = this.getters.getSheetZone(sheetId);
// return isZoneInside(zone, sheetZone) ? CommandResult.Success : CommandResult.TargetOutOfSheet;
// }

private checkUselessClearCell(cmd: ClearCellCommand): CommandResult {
const cell = this.getters.getCell(cmd);
if (!cell) return CommandResult.NoChanges;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/core/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isDefined,
isEqual,
overlap,
positionToZone,
positions,
splitReference,
toXC,
Expand Down Expand Up @@ -445,7 +446,7 @@ export class MergePlugin extends CorePlugin<MergeState> implements MergeState {
const merge = this.getMerge(position);
if (!merge || merge.id !== id) {
this.history.update("mergeCellMap", sheetId, col, row, undefined);
this.dispatch("CLEAR_CELL", position);
this.dispatch("CLEAR_CELLS", { sheetId, target: [positionToZone(position)] });
}
}
}
Expand Down
44 changes: 24 additions & 20 deletions src/plugins/core/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,19 +857,24 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
}

private moveCellOnColumnsDeletion(sheet: Sheet, deletedColumn: number) {
this.dispatch("CLEAR_CELLS", {
sheetId: sheet.id,
target: [
{
left: deletedColumn,
top: 0,
right: deletedColumn,
bottom: sheet.rows.length - 1,
},
],
});

for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
for (let i in row.cells) {
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
if (colIndex === deletedColumn) {
this.dispatch("CLEAR_CELL", {
sheetId: sheet.id,
col: colIndex,
row: rowIndex,
});
}
if (colIndex > deletedColumn) {
this.dispatch("UPDATE_CELL_POSITION", {
sheetId: sheet.id,
Expand Down Expand Up @@ -931,22 +936,21 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
deleteFromRow: HeaderIndex,
deleteToRow: HeaderIndex
) {
this.dispatch("CLEAR_CELLS", {
sheetId: sheet.id,
target: [
{
left: 0,
top: deleteFromRow,
right: this.getters.getNumberRows(sheet.id),
bottom: deleteToRow,
},
],
});

const numberRows = deleteToRow - deleteFromRow + 1;
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
if (rowIndex >= deleteFromRow && rowIndex <= deleteToRow) {
for (let i in row.cells) {
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
this.dispatch("CLEAR_CELL", {
sheetId: sheet.id,
col: colIndex,
row: rowIndex,
});
}
}
}
if (rowIndex > deleteToRow) {
for (let i in row.cells) {
const colIndex = Number(i);
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/ui_feature/data_cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ export class DataCleanupPlugin extends UIPlugin {
return;
}

for (const { col, row } of positions(zone)) {
this.dispatch("CLEAR_CELL", { col, row, sheetId });
}
this.dispatch("CLEAR_CELLS", { target: [zone], sheetId });

const zonePasted: Zone = {
left: zone.left,
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/ui_stateful/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ClipboardHandler } from "../../clipboard_handlers/abstract_clipboard_ha
import { cellStyleToCss, cssPropertiesToCss } from "../../components/helpers";
import { SELECTION_BORDER_COLOR } from "../../constants";
import { getClipboardDataPositions } from "../../helpers/clipboard/clipboard_helpers";
import { isZoneValid, positions, union } from "../../helpers/index";
import { isZoneValid, union } from "../../helpers/index";
import {
ClipboardContent,
ClipboardData,
Expand Down Expand Up @@ -198,9 +198,10 @@ export class ClipboardPlugin extends UIPlugin {
case "DELETE_CELL": {
const { cut, paste } = this.getDeleteCellsTargets(cmd.zone, cmd.shiftDimension);
if (!isZoneValid(cut[0])) {
for (const { col, row } of positions(cmd.zone)) {
this.dispatch("CLEAR_CELL", { col, row, sheetId: this.getters.getActiveSheetId() });
}
this.dispatch("CLEAR_CELLS", {
target: [cmd.zone],
sheetId: this.getters.getActiveSheetId(),
});
break;
}
const copiedData = this.copy(cut);
Expand Down
1 change: 1 addition & 0 deletions src/registries/repeat_commands_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const repeatCommandTransformRegistry = new Registry<RepeatTransform>();

repeatCommandTransformRegistry.add("UPDATE_CELL", genericRepeat);
repeatCommandTransformRegistry.add("CLEAR_CELL", genericRepeat);
repeatCommandTransformRegistry.add("CLEAR_CELLS", genericRepeat);
repeatCommandTransformRegistry.add("DELETE_CONTENT", genericRepeat);

repeatCommandTransformRegistry.add("ADD_MERGE", genericRepeat);
Expand Down
6 changes: 6 additions & 0 deletions src/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export const coreTypes = new Set<CoreCommandTypes>([
"UPDATE_CELL",
"UPDATE_CELL_POSITION",
"CLEAR_CELL",
"CLEAR_CELLS",
"DELETE_CONTENT",

/** GRID SHAPE */
Expand Down Expand Up @@ -802,6 +803,10 @@ export interface ClearCellCommand extends PositionDependentCommand {
type: "CLEAR_CELL";
}

export interface ClearCellsCommand extends TargetDependentCommand {
type: "CLEAR_CELLS";
}

export interface UndoCommand {
type: "UNDO";
commands: readonly CoreCommand[];
Expand Down Expand Up @@ -954,6 +959,7 @@ export type CoreCommand =
| UpdateCellCommand
| UpdateCellPositionCommand
| ClearCellCommand
| ClearCellsCommand
| DeleteContentCommand

/** GRID SHAPE */
Expand Down
42 changes: 41 additions & 1 deletion tests/cells/cell_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
addColumns,
addRows,
clearCell,
clearCells,
copy,
createSheet,
deleteColumns,
Expand Down Expand Up @@ -160,20 +161,46 @@ describe("getCellText", () => {
expect(getCell(model, "A1")).toBeUndefined();
});

test("clear style", () => {
test("clear some content", () => {
const model = new Model();
setCellContent(model, "A1", "hello");
setCellContent(model, "A2", "there");
clearCells(model, ["A1:A2"]);
expect(getCell(model, "A1")).toBeUndefined();
expect(getCell(model, "A2")).toBeUndefined();
});

test("clear some style", () => {
const model = new Model();
setStyle(model, "A1", { bold: true });
clearCell(model, "A1");
expect(getCell(model, "A1")).toBeUndefined();
});

test("clear some style", () => {
const model = new Model();
setStyle(model, "A1", { bold: true });
setStyle(model, "A2", { italic: true });
clearCells(model, ["A1:A2"]);
expect(getCell(model, "A1")).toBeUndefined();
expect(getCell(model, "A2")).toBeUndefined();
});

test("clear format", () => {
const model = new Model();
setCellFormat(model, "A1", "#,##0.0");
clearCell(model, "A1");
expect(getCell(model, "A1")).toBeUndefined();
});

test("clear some format", () => {
const model = new Model();
setCellFormat(model, "A1", "#,##0.0");
setCellFormat(model, "A2", "0%");
clearCells(model, ["A1:A2"]);
expect(getCell(model, "A1")).toBeUndefined();
});

test("clear content, style and format", () => {
const model = new Model();
setCellContent(model, "A1", "hello");
Expand All @@ -183,6 +210,19 @@ describe("getCellText", () => {
expect(getCell(model, "A1")).toBeUndefined();
});

test("clear some content, style and format", () => {
const model = new Model();
setCellContent(model, "A1", "hello");
setCellContent(model, "A2", "there");
setStyle(model, "A1", { bold: true });
setStyle(model, "A2", { italic: true });
setCellFormat(model, "A1", "#,##0.0");
setCellFormat(model, "A2", "0%");
clearCells(model, ["A1", "A2"]);
expect(getCell(model, "A1")).toBeUndefined();
expect(getCell(model, "A2")).toBeUndefined();
});

test("clear cell outside of sheet", () => {
const model = new Model();
const result = clearCell(model, "AAA999");
Expand Down
7 changes: 7 additions & 0 deletions tests/collaborative/inverses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AddColumnsRowsCommand,
AddMergeCommand,
ClearCellCommand,
ClearCellsCommand,
ClearFormattingCommand,
CoreCommand,
CreateSheetCommand,
Expand Down Expand Up @@ -249,6 +250,11 @@ describe("Inverses commands", () => {
col: 1,
row: 1,
};
const clearCells: ClearCellsCommand = {
type: "CLEAR_CELLS",
sheetId: "1",
target: [toZone("A1")],
};
const deleteContent: DeleteContentCommand = {
type: "DELETE_CONTENT",
sheetId: "1",
Expand Down Expand Up @@ -301,6 +307,7 @@ describe("Inverses commands", () => {
updateCell,
updateCellPosition,
clearCell,
clearCells,
deleteContent,
resizeColumns,
resizeRows,
Expand Down
3 changes: 3 additions & 0 deletions tests/repeat_commands_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe("Repeat commands basics", () => {
const repeatableCommands = [
"UPDATE_CELL",
"CLEAR_CELL",
"CLEAR_CELLS",
"DELETE_CONTENT",
"ADD_MERGE",
"REMOVE_MERGE",
Expand Down Expand Up @@ -135,6 +136,7 @@ describe("Repeat command transform generics", () => {
test.each([
TEST_COMMANDS.UPDATE_CELL,
TEST_COMMANDS.CLEAR_CELL,
TEST_COMMANDS.CLEAR_CELLS,
TEST_COMMANDS.DELETE_CONTENT,
TEST_COMMANDS.ADD_MERGE,
TEST_COMMANDS.REMOVE_MERGE,
Expand Down Expand Up @@ -166,6 +168,7 @@ describe("Repeat command transform generics", () => {
TEST_COMMANDS.REMOVE_TABLE,
TEST_COMMANDS.SET_FORMATTING,
TEST_COMMANDS.CLEAR_FORMATTING,
TEST_COMMANDS.CLEAR_CELLS,
])(
"Target dependant commands have target equal to current current selection",
(command: CoreCommand) => {
Expand Down
11 changes: 11 additions & 0 deletions tests/test_helpers/commands_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ export function clearCell(
return model.dispatch("CLEAR_CELL", { col, row, sheetId });
}

/**
* Clear cells in zones
*/
export function clearCells(
model: Model,
xcs: string[],
sheetId: UID = model.getters.getActiveSheetId()
) {
return model.dispatch("CLEAR_CELLS", { target: xcs.map(toZone), sheetId });
}

/**
* Set the content of a cell
*/
Expand Down
Loading