From d8329676caf5538e6bc039ae6e7544256fb4ff9b Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 27 Nov 2025 18:37:46 +0100 Subject: [PATCH] update: add a function to manage row height on font increase --- .../src/components/Workbook/Workbook.tsx | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/webapp/IronCalc/src/components/Workbook/Workbook.tsx b/webapp/IronCalc/src/components/Workbook/Workbook.tsx index aa8e0b9..7829649 100644 --- a/webapp/IronCalc/src/components/Workbook/Workbook.tsx +++ b/webapp/IronCalc/src/components/Workbook/Workbook.tsx @@ -135,7 +135,67 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => { }; const onIncreaseFontSize = (delta: number) => { + /** Automatically adjusts row heights when font size changes. */ + const DEFAULT_ROW_HEIGHT = 28; + + const { + sheet, + range: [rowStart, columnStart, rowEnd, columnEnd], + } = model.getSelectedView(); + + /** Normalize the range (selection can be from bottom-right to top-left) */ + const actualRowStart = Math.min(rowStart, rowEnd); + const actualRowEnd = Math.max(rowStart, rowEnd); + const actualColumnStart = Math.min(columnStart, columnEnd); + const actualColumnEnd = Math.max(columnStart, columnEnd); + + /** Capture current state BEFORE updating font size */ + const rowData: Array<{ + row: number; + oldFontSize: number; + oldRowHeight: number; + }> = []; + + for (let row = actualRowStart; row <= actualRowEnd; row++) { + let maxFontSize = 0; + for (let col = actualColumnStart; col <= actualColumnEnd; col++) { + const style = model.getCellStyle(sheet, row, col); + maxFontSize = Math.max(maxFontSize, style.font.sz); + } + const oldRowHeight = model.getRowHeight(sheet, row); + rowData.push({ + row, + oldFontSize: maxFontSize, + oldRowHeight, + }); + } + + /** Update the font size in the model */ updateRangeStyle("font.size_delta", `${delta}`); + + /** Adjust row heights based on the new font sizes */ + for (const { row, oldFontSize, oldRowHeight } of rowData) { + const newFontSize = oldFontSize + delta; + + if (oldRowHeight < DEFAULT_ROW_HEIGHT) { + continue; + } + + const requiredHeight = 8 + newFontSize; + + let newRowHeight: number; + if (requiredHeight > DEFAULT_ROW_HEIGHT) { + newRowHeight = requiredHeight; + } else { + newRowHeight = DEFAULT_ROW_HEIGHT; + } + + if (Math.abs(newRowHeight - oldRowHeight) > 0.1) { + model.setRowsHeight(sheet, row, row, newRowHeight); + } + } + + setRedrawId((id) => id + 1); }; const onCopyStyles = () => {