Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/spreadsheet/print & PDF export: fix an issue
Adorian Dorandbb4c192735e
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 377a5d73d0..53d3d598cb 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -553,8 +553,8 @@ describe("renderSpreadsheetToHtml", () => {});describe("overflow into neighbouring cells", () => {- /** One row of `cells`, keyed by column. */- function row(cells: Record<number, unknown>): string {+ /** A sheet from a `cellData` matrix, with optional sheet-level overrides. */+ function grid(cellData: Record<number, unknown>, extra: Record<string, unknown> = {}): string {return JSON.stringify({version: 1,workbook: {@@ -566,15 +566,21 @@ describe("renderSpreadsheetToHtml", () => {name: "Sheet1",hidden: 0,mergeData: [],- cellData: { "0": cells },+ cellData,rowData: {},- columnData: {}+ columnData: {},+ ...extra}}}});}+ /** One row of `cells`, keyed by column. */+ function row(cells: Record<number, unknown>, extra: Record<string, unknown> = {}): string {+ return grid({ "0": cells }, extra);+ }+/** Whether the workbook's cell at `column` clips its text. The render always starts at A1. */function clips(json: string, column: number): boolean {const cells = renderSpreadsheetToHtml(json).match(/<td[^>]*>/g) ?? [];@@ -610,6 +616,29 @@ describe("renderSpreadsheetToHtml", () => {expect(clips(row({ 0: { s: { bl: 1 } }, 1: { v: "next", t: 1 } }), 0)).toBe(false);});+ it("judges adjacency on the rendered row rather than the cell matrix", () => {+ const long = { v: "a long label", t: 1 };++ // A hidden column reaches nobody, so the cell steps over it in both directions.+ expect(clips(row({ 0: long, 1: { v: "next", t: 1 } }, { columnData: { 1: { hd: 1 } } }), 0)).toBe(false);+ expect(clips(row({ 0: long, 1: {}, 2: { v: "next", t: 1 } }, { columnData: { 1: { hd: 1 } } }), 0)).toBe(true);++ // A merged cell starts looking past the last column its own range covers.+ const merged = [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 1 }];+ expect(clips(row({ 0: long, 1: {}, 2: { v: "next", t: 1 } }, { mergeData: merged }), 0)).toBe(true);+ expect(clips(row({ 0: long, 1: {} }, { mergeData: merged }), 0)).toBe(false);+ });++ it("reads a column a merge spans into as holding that range's content", () => {+ // The second row renders only the right-aligned cell; the range fills the column beside it.+ const html = renderSpreadsheetToHtml(grid(+ { "0": { 0: { v: "tall", t: 1 } }, "1": { 1: { v: "x", t: 1, s: { ht: HorizontalAlign.RIGHT } } } },+ { mergeData: [{ startRow: 0, endRow: 1, startColumn: 0, endColumn: 0 }] }+ ));++ expect(html.split("</tr>")[1]).toContain(`<td style="text-align:right;overflow:hidden">x</td>`);+ });+it("counts a cell whose text lives only in its rich-text document as occupied", () => {const html = row({0: { v: "a long label", t: 1 },diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.tsindex 336513f6b2..44f3cc2f3e 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -15,6 +15,7 @@ import { format as formatNumfmt, formatColor as formatNumfmtColor } from "numfmtimport {BorderStyle,type CellDocumentSegment,+ type CellMatrix,CellValueType,computeBounds,getCellDocumentSegments,@@ -22,10 +23,10 @@ import {getFloatingDrawings,getVisibleSheets,HorizontalAlign,- type CellMatrix,type IBorderData,type IBorderStyleData,type ICellData,+ type IColumnData,isFiniteNumber,type IRange,type ISheetDrawing,@@ -290,16 +291,18 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |// sets its own still wins.lines.push(`<tr style="height:${height}px;vertical-align:bottom">`);+ const neighbours: RowNeighbours = { row, minCol, maxCol, cellData, columnData, mergeMap };+for (let col = minCol; col <= maxCol; col++) {if (columnData[col]?.hd) continue;const mergeInfo = mergeMap.get(cellKey(row, col));- if (mergeInfo === "hidden") continue;+ if (mergeInfo?.kind === "member") continue;const cell = cellData[row]?.[col];const cellStyle = mergeAwareStyle(resolveCellStyle(cell?.s, styles), mergeInfo, row, col, cellData, styles);const declarations = [buildCssText(cellStyle, cell)];- if (clipsOverflow(cell, cellStyle, row, col, cellData)) declarations.push("overflow:hidden");+ if (clipsOverflow(cell, cellStyle, col, mergeInfo, neighbours)) declarations.push("overflow:hidden");const cssText = declarations.filter(Boolean).join(";");const value = rotate(formatCellValue(cell, cellStyle), cellStyle?.tr)+ (cell ? renderCellImages(cell) : "");@@ -350,7 +353,9 @@ function buildTableTag(sheet: IWorksheetData, totalWidth: number): string {// #region Merge handling+/** The cell a merged range is rendered from, carrying the span it covers. */interface MergeOrigin {+ kind: "origin";rowSpan: number;colSpan: number;/** Last row and column of the range, clamped to the rendered bounds. */@@ -358,6 +363,13 @@ interface MergeOrigin {endColumn: number;}+/** A cell the range covers. It is not rendered; the anchor's content fills its place. */+interface MergeMember {+ kind: "member";+ anchorRow: number;+ anchorColumn: number;+}+/*** Replaces a merged range's border with the one composed from the cells on its edges. Excel keeps* a range's outline on the member cell each edge belongs to: the anchor holds the top and the@@ -383,7 +395,7 @@ function mergeAwareStyle(return { ...style, bd };}-type MergeInfo = MergeOrigin | "hidden";+type MergeInfo = MergeOrigin | MergeMember;function cellKey(row: number, col: number): string {return `${row},${col}`;@@ -399,6 +411,7 @@ function buildMergeMap(mergeData: IRange[], minRow: number, maxRow: number, minCconst endCol = Math.min(range.endColumn, maxCol);map.set(cellKey(range.startRow, range.startColumn), {+ kind: "origin",rowSpan: endRow - startRow + 1,colSpan: endCol - startCol + 1,endRow,@@ -408,7 +421,7 @@ function buildMergeMap(mergeData: IRange[], minRow: number, maxRow: number, minCfor (let r = startRow; r <= endRow; r++) {for (let c = startCol; c <= endCol; c++) {if (r === range.startRow && c === range.startColumn) continue;- map.set(cellKey(r, c), "hidden");+ map.set(cellKey(r, c), { kind: "member", anchorRow: range.startRow, anchorColumn: range.startColumn });}}}@@ -475,6 +488,16 @@ function buildCssText(style: IStyleData | null, cell?: ICellData): string {return parts.join(";");}+/** One row's rendered layout, which is what decides whose value a cell's text would spill over. */+interface RowNeighbours {+ row: number;+ minCol: number;+ maxCol: number;+ cellData: CellMatrix;+ columnData: Record<number, IColumnData>;+ mergeMap: Map<string, MergeInfo>;+}+/*** Whether a cell has to clip its text at its own edge. A spreadsheet spills text into a* neighbouring cell only while that neighbour is empty, and never spills a number or a boolean@@ -485,20 +508,40 @@ function buildCssText(style: IStyleData | null, cell?: ICellData): string {function clipsOverflow(cell: ICellData | undefined,style: IStyleData | null,- row: number,col: number,- cellData: CellMatrix+ merge: MergeOrigin | undefined,+ neighbours: RowNeighbours): boolean {if (!hasContent(cell) || style?.tb === WrapStrategy.WRAP) return false;if (cell?.t === CellValueType.NUMBER || cell?.t === CellValueType.BOOLEAN) return true;- const before = hasContent(cellData[row]?.[col - 1]);- const after = hasContent(cellData[row]?.[col + 1]);+ // Rightwards, a merged cell starts looking past the last column its own range covers.+ const before = () => neighbourHasContent(col, -1, neighbours);+ const after = () => neighbourHasContent(merge?.endColumn ?? col, 1, neighbours);switch (style?.ht) {- case HorizontalAlign.RIGHT: return before;- case HorizontalAlign.CENTER: return before || after;- default: return after;+ case HorizontalAlign.RIGHT: return before();+ case HorizontalAlign.CENTER: return before() || after();+ default: return after();+ }+}++/**+ * Whether the cell rendered beside `from` shows anything. Adjacency follows the rendered row, not+ * the cell matrix: hidden columns are stepped over because nothing of them reaches the page, and a+ * column a merge covers reports that range's anchor, whose text is what fills the space there.+ */+function neighbourHasContent(from: number, step: -1 | 1, neighbours: RowNeighbours): boolean {+ const { row, minCol, maxCol, cellData, columnData, mergeMap } = neighbours;++ for (let col = from + step; col >= minCol && col <= maxCol; col += step) {+ if (columnData[col]?.hd) continue;++ const info = mergeMap.get(cellKey(row, col));+ return hasContent(info?.kind === "member"+ ? cellData[info.anchorRow]?.[info.anchorColumn]+ : cellData[row]?.[col]);}+ return false;}/** Whether a cell shows anything. A cell carrying only a style is empty, as it is in the editor. */