From f9611f7345e1c6ce7add89c7176b3c10e2ea6cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Fri, 2 Aug 2024 13:39:58 +0200 Subject: [PATCH] [FIX] format: dispatch only once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 1b667256cc386e4b8e8ff2e216e66bd9b9a03d62 closes odoo/o-spreadsheet#4770 Task: 4091502 Signed-off-by: RĂ©mi Rahir (rar) --- src/plugins/ui_feature/format.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) 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, + }); + } } /**