diff --git a/src/plugins/ui_feature/format.ts b/src/plugins/ui_feature/format.ts index 791ae22881..4decb1392a 100644 --- a/src/plugins/ui_feature/format.ts +++ b/src/plugins/ui_feature/format.ts @@ -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, @@ -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 = {}; // Find the each cell with a number value and get the format for (const zone of zones) { for (const position of positions(zone)) { @@ -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, + }); + } } /**