Skip to content

Commit

Permalink
[FIX] format: dispatch only once
Browse files Browse the repository at this point in the history
Steps to reproduce:
- select a huge zone with numbers
- click on the "Increase decimal places" tool in the top bar

=> there's one dipatched command for each position in the zone, which
can huge.

We want:
- one dispatched command per format
- positions grouped to a single zone if there are contiguous (a single big
  zone instead of many zones, each being actually a single position)

Issue introduced by 1b66725

closes #4775

Task: 4091502
X-original-commit: f9611f7
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
LucasLefevre committed Aug 6, 2024
1 parent c5ae261 commit a32470f
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/plugins/ui_feature/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {
changeDecimalPlaces,
createDefaultFormat,
isDateTimeFormat,
positions,
positionToZone,
positions,
} from "../../helpers";
import { futureRecomputeZones } from "../../helpers/recompute_zones";
import {
CellPosition,
CellValueType,
Command,
Format,
Position,
SetDecimalStep,
UID,
Zone,
Expand Down Expand Up @@ -41,6 +43,7 @@ export class FormatPlugin extends UIPlugin {
* evaluated and updated with the number type.
*/
private setDecimal(sheetId: UID, zones: Zone[], step: SetDecimalStep) {
const positionsByFormat: Record<Format, Position[]> = {};
// Find the each cell with a number value and get the format
for (const zone of zones) {
for (const position of positions(zone)) {
Expand All @@ -50,15 +53,22 @@ export class FormatPlugin extends UIPlugin {
// of the format
const locale = this.getters.getLocale();
const newFormat = changeDecimalPlaces(numberFormat, step, locale);
// Apply the new format on the whole zone
this.dispatch("SET_FORMATTING", {
sheetId,
target: [positionToZone(position)],
format: newFormat,
});
positionsByFormat[newFormat] = positionsByFormat[newFormat] || [];
positionsByFormat[newFormat].push(position);
}
}
}
// consolidate all positions with the same format in bigger zones
for (const newFormat in positionsByFormat) {
const zones = futureRecomputeZones(
positionsByFormat[newFormat].map((position) => positionToZone(position))
);
this.dispatch("SET_FORMATTING", {
sheetId,
format: newFormat,
target: zones,
});
}
}

/**
Expand Down

0 comments on commit a32470f

Please sign in to comment.