From 17c1d57003fb8039d39092fa44a033ee39e39221 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 5 Jul 2026 17:17:54 +0000 Subject: [PATCH] perf: derive in-cell image bound from column width instead of probing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous measurement removed the image from layout (display:none) and read back the cell size on every ResizeObserver callback, forcing a synchronous reflow each time — expensive with many in-cell images. Read the column's explicit width from the table layout instead: it is the authoritative bound, costs no reflow, and is not subject to the image-forces-the-cell-wide circularity (so shrinking a column now clamps correctly). When a column has no explicit width, fall back to half the document width. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01Bpy5eV6C2wwf2MNut68BEh --- shared/editor/components/Image.tsx | 77 ++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/shared/editor/components/Image.tsx b/shared/editor/components/Image.tsx index 75a6bb4862..7bf3a8e364 100644 --- a/shared/editor/components/Image.tsx +++ b/shared/editor/components/Image.tsx @@ -70,29 +70,42 @@ const Image = (props: Props) => { ref, }); - // When rendered inside a table cell, measure the cell's usable content width - // so oversized images scale down to fit the column instead of overflowing it. - // The image is momentarily removed from layout during measurement so the cell - // reports its natural column width rather than the width the image forces. + // When rendered inside a table cell, constrain the image to the column so + // oversized images scale down to fit instead of overflowing it. The column's + // explicit width (from the table layout) is the authoritative bound and is + // cheap to read; when a column has no explicit width we fall back to half the + // document width. A ResizeObserver keeps the bound in sync as columns or the + // window are resized. React.useLayoutEffect(() => { const element = ref.current; const cell = element?.closest("td, th"); - if (!element || !(cell instanceof HTMLElement)) { + if (!element || !(cell instanceof HTMLTableCellElement)) { setCellMaxWidth(Number.POSITIVE_INFINITY); return; } const measure = () => { - const previousDisplay = element.style.display; - element.style.display = "none"; - const style = getComputedStyle(cell); - const available = - cell.clientWidth - - parseFloat(style.paddingLeft) - - parseFloat(style.paddingRight); - element.style.display = previousDisplay; + const columnsWidth = getCellColumnsWidth(cell); + if (columnsWidth !== null) { + const style = getComputedStyle(cell); + const inset = + parseFloat(style.paddingLeft) + + parseFloat(style.paddingRight) + + parseFloat(style.borderLeftWidth) + + parseFloat(style.borderRightWidth); + const inner = columnsWidth - inset; + setCellMaxWidth(inner > 0 ? inner : columnsWidth); + return; + } - setCellMaxWidth(available > 0 ? available : Number.POSITIVE_INFINITY); + const documentWidth = parseFloat( + getComputedStyle(element).getPropertyValue("--document-width") + ); + setCellMaxWidth( + Number.isFinite(documentWidth) + ? documentWidth / 2 + : Number.POSITIVE_INFINITY + ); }; measure(); @@ -340,6 +353,42 @@ function getPlaceholder(width: number, height: number) { return ``; } +/** + * Returns the combined explicit width, in pixels, of the table columns that the + * given cell spans. + * + * @param cell the table cell to measure. + * @returns the total column width in pixels, or `null` when any spanned column + * has no explicit width (an auto-sized column). + */ +function getCellColumnsWidth(cell: HTMLTableCellElement): number | null { + const colgroup = cell.closest("table")?.querySelector("colgroup"); + const row = cell.parentElement; + if (!colgroup || !row) { + return null; + } + + let columnIndex = 0; + for (const sibling of Array.from(row.children)) { + if (sibling === cell) { + break; + } + columnIndex += sibling instanceof HTMLTableCellElement ? sibling.colSpan : 1; + } + + let total = 0; + for (let index = columnIndex; index < columnIndex + cell.colSpan; index++) { + const col = colgroup.children[index]; + const width = + col instanceof HTMLElement ? parseFloat(col.style.width) : Number.NaN; + if (!Number.isFinite(width)) { + return null; + } + total += width; + } + return total; +} + export const Error = styled(Flex)` max-width: 100%; color: ${s("textTertiary")};