Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet/preview: fix failing tests

Adorian Doran013f590c3ffa
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
index 8d85c8f33f..52271bdab9 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -2028,6 +2028,36 @@ describe("renderSpreadsheetToHtml", () => {
expect(tall(1048575, 1000)).toContain(`rowspan="2"`);
});
+ it("gives up half an edge to a bordered neighbour when sizing a cell's box", () => {
+ // Under border-collapse the wider of two facing borders wins the shared edge, so a cell with
+ // none of its own still loses half of its neighbour's. A cell that fills its row exactly,
+ // which a turned one does, is a border's half too tall without this.
+ const html = renderSpreadsheetToHtml(JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ styles: {},
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ mergeData: [],
+ rowData: { "0": { h: 40 }, "1": { h: 24 } },
+ columnData: {},
+ cellData: {
+ "0": { "0": { v: "turned", t: 1, s: { tr: { a: 90 } } } },
+ "1": { "0": { v: "bordered", t: 1, s: { bd: { t: { s: BorderStyle.MEDIUM } } } } }
+ }
+ }
+ }
+ }
+ }));
+
+ // 40 less the 2px of padding, less half of the neighbour's 2px border.
+ expect(html).toContain("height:37px");
+ });
+
it("counts a border as structure the grid has to reach, but not a fill", () => {
const beyond = (style: unknown) => renderSpreadsheetToHtml(JSON.stringify({
version: 1,
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 57e72e96a0..096728d696 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -368,7 +368,8 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
const bound = spillBound(cell, cellStyle, col, mergeInfo, neighbours);
const padding = cellPadding(cellStyle);
const boxHeight = spannedHeight(heights, row, mergeInfo?.endRow ?? row)
- - padding.t - padding.b - collapsedBorderHeight(cellStyle);
+ - padding.t - padding.b
+ - collapsedBorderHeight(cellStyle, row, mergeInfo?.endRow ?? row, col, rowData, cellData, styles);
const declarations = [buildCssText(cellStyle, cell)];
if (hasContent(cell)) {
declarations.push(`padding:${px(padding.t)}px ${px(padding.r)}px ${px(padding.b)}px ${px(padding.l)}px`);
@@ -734,14 +735,35 @@ function spannedHeight(heights: number[], from: number, to: number): number {
}
/**
- * The height a cell's own borders take from its row. Under `border-collapse` a border is shared
- * with the cell across it, so each side keeps half, and the sheet's row heights count the gridline
- * as drawn inside the row rather than added to it.
+ * The height a cell's borders take from its row. Under `border-collapse` an edge is shared with the
+ * cell across it and the wider of the two borders wins, so a cell with none of its own still gives
+ * up half an edge to a bordered neighbour. Reading only the cell's own borders leaves a cell that
+ * fills its row exactly, which a turned one does, a border's half taller than the row it sits in.
*/
-function collapsedBorderHeight(style: IStyleData | null): number {
- const half = (border: IBorderStyleData | null | undefined) =>
- (!border || border.s === BorderStyle.NONE ? 0 : Number.parseFloat(borderStyleToWidth(border.s)) / 2);
- return half(style?.bd?.t) + half(style?.bd?.b);
+function collapsedBorderHeight(
+ style: IStyleData | null,
+ row: number,
+ endRow: number,
+ col: number,
+ rowData: Record<number, IRowData>,
+ cellData: CellMatrix,
+ styles: Record<string, IStyleData | null>
+): number {
+ const width = (border: IBorderStyleData | null | undefined) =>
+ (!border || border.s === BorderStyle.NONE ? 0 : Number.parseFloat(borderStyleToWidth(border.s)));
+
+ const above = resolveCellStyle(cellData[renderedRow(row, -1, rowData)]?.[col]?.s, styles);
+ const below = resolveCellStyle(cellData[renderedRow(endRow, 1, rowData)]?.[col]?.s, styles);
+
+ return (Math.max(width(style?.bd?.t), width(above?.bd?.b))
+ + Math.max(width(style?.bd?.b), width(below?.bd?.t))) / 2;
+}
+
+/** The row an edge is shared with: the nearest one that is drawn, since a hidden row draws nothing. */
+function renderedRow(from: number, step: -1 | 1, rowData: Record<number, IRowData>): number {
+ let row = from + step;
+ while (row >= 0 && rowData[row]?.hd) row += step;
+ return row;
}
/** The padding a cell is laid out with, which the box has to leave room for inside the row. */