diff --git a/src/helpers/text_helper.ts b/src/helpers/text_helper.ts index cc2cb780f0..7acbd10304 100644 --- a/src/helpers/text_helper.ts +++ b/src/helpers/text_helper.ts @@ -8,7 +8,10 @@ import { NEWLINE, PADDING_AUTORESIZE_VERTICAL, } from "../constants"; -import { Cell, Pixel, PixelPosition, Style } from "../types"; +import { Cell, DEFAULT_LOCALE, Locale, Pixel, PixelPosition, Style } from "../types"; +import { CellErrorType } from "../types/errors"; +import { parseLiteral } from "./cells"; +import { formatValue } from "./format"; import { isMarkdownLink, parseMarkdownLink } from "./misc"; export function computeTextLinesHeight(textLineHeight: number, numberOfLines: number = 1) { @@ -21,12 +24,22 @@ export function computeTextLinesHeight(textLineHeight: number, numberOfLines: nu export function getDefaultCellHeight( ctx: CanvasRenderingContext2D, cell: Cell | undefined, + locale: Locale, colSize: number ) { if (!cell || (!cell.isFormula && !cell.content)) { return DEFAULT_CELL_HEIGHT; } - const content = cell.isFormula ? "" : cell.content; + let content = ""; + + try { + if (!cell.isFormula) { + const localeFormat = { format: cell.format, locale: DEFAULT_LOCALE }; + content = formatValue(parseLiteral(cell.content, locale), localeFormat); + } + } catch { + content = CellErrorType.GenericError; + } return getCellContentHeight(ctx, content, cell.style, colSize); } diff --git a/src/plugins/ui_core_views/header_sizes_ui.ts b/src/plugins/ui_core_views/header_sizes_ui.ts index 6b0cb9d02e..6379862c28 100644 --- a/src/plugins/ui_core_views/header_sizes_ui.ts +++ b/src/plugins/ui_core_views/header_sizes_ui.ts @@ -173,7 +173,7 @@ export class HeaderSizeUIPlugin extends UIPlugin implements Hea const cell = this.getters.getCell(position); const colSize = this.getters.getColSize(position.sheetId, position.col); - return getDefaultCellHeight(this.ctx, cell, colSize); + return getDefaultCellHeight(this.ctx, cell, this.getters.getLocale(), colSize); } private isInMultiRowMerge(position: CellPosition): boolean { diff --git a/tests/collaborative/collaborative.test.ts b/tests/collaborative/collaborative.test.ts index adb79e2d5e..a6c1cc6f28 100644 --- a/tests/collaborative/collaborative.test.ts +++ b/tests/collaborative/collaborative.test.ts @@ -3,7 +3,13 @@ import { DEFAULT_REVISION_ID, MESSAGE_VERSION } from "../../src/constants"; import { functionRegistry } from "../../src/functions"; import { getDefaultCellHeight, range, toCartesian, toZone, zoneToXc } from "../../src/helpers"; import { featurePluginRegistry } from "../../src/plugins"; -import { Command, CommandResult, CoreCommand, DataValidationCriterion } from "../../src/types"; +import { + Command, + CommandResult, + CoreCommand, + DEFAULT_LOCALE, + DataValidationCriterion, +} from "../../src/types"; import { CollaborationMessage } from "../../src/types/collaborative/transport_service"; import { MockTransportService } from "../__mocks__/transport_service"; import { @@ -1083,7 +1089,7 @@ describe("Multi users synchronisation", () => { const ctx = document.createElement("canvas").getContext("2d")!; expect([alice, bob, charlie]).toHaveSynchronizedValue( (user) => user.getters.getRowSize("sheet2", 0), - getDefaultCellHeight(ctx, getCell(alice, "A1"), colSize) + getDefaultCellHeight(ctx, getCell(alice, "A1"), DEFAULT_LOCALE, colSize) ); }); diff --git a/tests/headers/resizing_plugin.test.ts b/tests/headers/resizing_plugin.test.ts index fc6b671f07..05748cfdab 100644 --- a/tests/headers/resizing_plugin.test.ts +++ b/tests/headers/resizing_plugin.test.ts @@ -6,7 +6,7 @@ import { } from "../../src/constants"; import { getDefaultCellHeight as getDefaultCellHeightHelper, toXC } from "../../src/helpers"; import { Model } from "../../src/model"; -import { Cell, CommandResult, Sheet, Wrapping } from "../../src/types"; +import { Cell, CommandResult, DEFAULT_LOCALE, Sheet, Wrapping } from "../../src/types"; import { activateSheet, addColumns, @@ -22,15 +22,17 @@ import { resizeColumns, resizeRows, setCellContent, + setFormat, setStyle, unMerge, undo, } from "../test_helpers/commands_helpers"; import { getCell } from "../test_helpers/getters_helpers"; +import { target } from "../test_helpers/helpers"; const ctx = document.createElement("canvas").getContext("2d")!; function getDefaultCellHeight(cell: Cell | undefined, colSize = DEFAULT_CELL_WIDTH) { - return Math.round(getDefaultCellHeightHelper(ctx, cell, colSize)); + return Math.round(getDefaultCellHeightHelper(ctx, cell, DEFAULT_LOCALE, colSize)); } describe("Model resizer", () => { @@ -441,6 +443,20 @@ describe("Model resizer", () => { expect(model.getters.getRowSize(sheet.id, 0)).toBe(wrappedCellHeight); }); + test("wrapped formatted text updates the row size", () => { + setStyle(model, "C1", { fontSize: 10, wrapping: "wrap" }); + resizeColumns(model, ["C"], 100); + setCellContent(model, "C1", "200"); + + expect(model.getters.getRowSize(sheet.id, 0)).toBe(DEFAULT_CELL_HEIGHT); + + setFormat(model, "0[$long escaped string in the format that will be wrapped]", target("C1")); + const cell = getCell(model, "C1"); + const expectedHeight = getDefaultCellHeight(cell, 100); + expect(expectedHeight).toBeGreaterThan(DEFAULT_CELL_HEIGHT); + expect(model.getters.getRowSize(sheet.id, 0)).toBe(expectedHeight); + }); + test.each(["overflow", "clip"])( `wrapping text with style %s does not update the row size`, (wrap: Wrapping) => {