Skip to content

Commit

Permalink
[IMP] selection: alleviate size of revision on 'move header'
Browse files Browse the repository at this point in the history
Aleviate the size  of the message dispatched on the server
by grouping the dispatch per size and only if it's different
from the current size.

closes #5310

Task: 4378896
Signed-off-by: Pierre Rousseau (pro) <[email protected]>
  • Loading branch information
fdamhaut committed Dec 18, 2024
1 parent ae44c66 commit ef27a15
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/plugins/ui_stateful/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,26 @@ export class GridSelectionPlugin extends UIPlugin {

const toRemove = isBasedBefore ? cmd.elements.map((el) => el + thickness) : cmd.elements;
let currentIndex = cmd.base;

const resizingGroups: Record<number, number[]> = {};

for (const element of toRemove) {
const size = this.getters.getHeaderSize(cmd.sheetId, cmd.dimension, element);
const currentSize = this.getters.getHeaderSize(cmd.sheetId, cmd.dimension, currentIndex);
if (size != currentSize) {
resizingGroups[size] ??= [];
resizingGroups[size].push(currentIndex);
currentIndex += 1;
}
}

for (const size in resizingGroups) {
this.dispatch("RESIZE_COLUMNS_ROWS", {
dimension: cmd.dimension,
sheetId: cmd.sheetId,
size,
elements: [currentIndex],
size: parseInt(size, 10),
elements: resizingGroups[size],
});
currentIndex += 1;
}

this.dispatch("REMOVE_COLUMNS_ROWS", {
Expand Down
70 changes: 70 additions & 0 deletions tests/sheet/selection_plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CoreCommand, CorePlugin } from "../../src";
import { DEFAULT_CELL_HEIGHT, DEFAULT_CELL_WIDTH } from "../../src/constants";
import {
numberToLetters,
Expand All @@ -8,6 +9,7 @@ import {
zoneToXc,
} from "../../src/helpers";
import { Model } from "../../src/model";
import { corePluginRegistry } from "../../src/plugins";
import { CommandResult, Direction } from "../../src/types";
import {
activateSheet,
Expand Down Expand Up @@ -42,6 +44,7 @@ import {
getCellContent,
getSelectionAnchorCellXc,
} from "../test_helpers/getters_helpers";
import { addTestPlugin } from "../test_helpers/helpers";

let model: Model;
const hiddenContent = "hidden content to be skipped";
Expand Down Expand Up @@ -925,6 +928,39 @@ describe("move elements(s)", () => {
expect(model.getters.getColSize(sheetId, 2)).toEqual(10);
});

test("Move resized columns preserves their sizes", () => {
let cmds: CoreCommand[] = [];
class CommandSpy extends CorePlugin {
static getters = [];
handle(command: CoreCommand) {
if (command.type === "RESIZE_COLUMNS_ROWS") {
cmds.push(command);
}
}
}
addTestPlugin(corePluginRegistry, CommandSpy);

const model = new Model();
resizeColumns(model, ["A", "B"], 10);
resizeColumns(model, ["C", "D"], 20);

moveColumns(model, "A", ["C", "D"]);

const sheetId = model.getters.getActiveSheetId();
expect(model.getters.getColSize(sheetId, 0)).toEqual(20);
expect(model.getters.getColSize(sheetId, 1)).toEqual(20);
expect(model.getters.getColSize(sheetId, 2)).toEqual(10);
expect(model.getters.getColSize(sheetId, 3)).toEqual(10);

expect(cmds[2]).toStrictEqual({
type: "RESIZE_COLUMNS_ROWS",
dimension: "COL",
sheetId,
elements: [0, 1],
size: 20,
});
});

test("Move a resized row preserves its size", () => {
const model = new Model();
resizeRows(model, [0], 10);
Expand All @@ -936,6 +972,40 @@ describe("move elements(s)", () => {
expect(model.getters.getRowSize(sheetId, 2)).toEqual(10);
});

test("Move resized rows preserves their sizes", () => {
let cmds: CoreCommand[] = [];
class CommandSpy extends CorePlugin {
static getters = [];
handle(command: CoreCommand) {
if (command.type === "RESIZE_COLUMNS_ROWS") {
cmds.push(command);
}
}
}
addTestPlugin(corePluginRegistry, CommandSpy);

const model = new Model();

resizeRows(model, [1, 2], 10);
resizeRows(model, [3, 4], 20);

moveRows(model, 1, [3, 4]);

const sheetId = model.getters.getActiveSheetId();
expect(model.getters.getRowSize(sheetId, 1)).toEqual(20);
expect(model.getters.getRowSize(sheetId, 2)).toEqual(20);
expect(model.getters.getRowSize(sheetId, 3)).toEqual(10);
expect(model.getters.getRowSize(sheetId, 4)).toEqual(10);

expect(cmds[2]).toStrictEqual({
type: "RESIZE_COLUMNS_ROWS",
dimension: "ROW",
sheetId,
elements: [1, 2],
size: 20,
});
});

test("Can move a column to the end of the sheet", () => {
const model = new Model();
setCellContent(model, "A1", "5");
Expand Down

0 comments on commit ef27a15

Please sign in to comment.