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/preview: fix some issues
Adorian Doran3b4be58bc5cf
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 749801b90f..8d3d9ac25e 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -669,7 +669,7 @@ describe("renderSpreadsheetToHtml", () => {expect(room(row({ 0: { v: "a long label", t: 1 }, 2: { v: "next", t: 1 } }), 0)).toEqual([0, 88]);});- it("gives the text the width of the empty cells it may run across", () => {+ it("gives the text the width of the empty cells it can run across", () => {const long = { v: "a long label", t: 1 };const widths = { 1: { w: 30 }, 2: { w: 50 } };@@ -1398,7 +1398,7 @@ describe("renderSpreadsheetToHtml", () => {// Column 1 between A and C has no cell -> empty <td>.expect(html).toContain("<td></td>");expect(unboxed(html)).toContain("<td>C</td>");- // A may run across the empty column, so its text carries the room it has.+ // A can run across the empty column, so its text carries the room it has.expect(html).toContain(`<td style="padding:0px 2px 2px 2px"><span style="display:block;overflow:hidden;max-height:22px;width:calc(100% + 88px);margin-right:-88px;line-height:normal">A</span></td>`);});@@ -1968,6 +1968,36 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain(`<col span="2" style="width:88px">`);});+ it("counts a border as structure the grid has to reach, but not a fill", () => {+ const beyond = (style: unknown) => renderSpreadsheetToHtml(JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: {},+ sheets: {+ s1: {+ id: "s1",+ name: "Sheet1",+ hidden: 0,+ mergeData: [],+ cellData: { "0": { "0": { v: "a", t: 1 } }, "1": { "3": { s: style } } },+ rowData: {},+ columnData: {}+ }+ }+ }+ }));++ // An empty bordered cell is a drawn box someone means to keep, so the grid reaches it.+ const bordered = beyond({ bd: { t: { s: BorderStyle.THIN }, b: { s: BorderStyle.THIN } } });+ expect((bordered.match(/<td/g) ?? []).length).toBe(8);+ expect(bordered).toContain("border-top");++ // A fill comes from colouring whole rows, so it does not carry the grid out to meet it.+ expect((beyond({ bg: { rgb: "#FFE699" } }).match(/<td/g) ?? []).length).toBe(1);+ expect((beyond({ bd: { t: { s: BorderStyle.NONE } } }).match(/<td/g) ?? []).length).toBe(1);+ });+it("falls back to every cell for a sheet that is nothing but formatting", () => {const html = 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.tsindex c1388f4ae8..7a044e5910 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -301,7 +301,7 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |// cover any floating image that reaches past the data, so the grid encloses it as the editor does.// A merge widens it as before, except one applied to entire rows or columns, which would// pull the sheet back out to the far edge of the grid.- const bounds = computeBounds(cellData, boundingMerges(mergeData, sheet), holdsSomething)+ const bounds = computeBounds(cellData, boundingMerges(mergeData, sheet), (cell) => holdsSomething(cell, styles))?? computeBounds(cellData, mergeData);const { maxRow, maxCol } = extendBoundsForImages(sheet, bounds?.maxRow ?? -1, bounds?.maxCol ?? -1, images);if (maxRow < 0 || maxCol < 0) {@@ -571,7 +571,7 @@ interface RowNeighbours {defaultWidth: number;}-/** How far a cell's text may run past its own edges, in px on each side. */+/** How far a cell's text can run past its own edges, in px on each side. */interface SpillBound {before: number;after: number;@@ -772,11 +772,17 @@ function boundingMerges(mergeData: IRange[], sheet: IWorksheetData): IRange[] {}/**- * Whether a cell holds something of its own, which is what the rendered grid is bounded by. A- * formula counts even when its result is blank, since the cell is part of the sheet's data.+ * Whether a cell holds something the grid has to reach, which is what it is bounded by. A formula+ * counts even when its result is blank, since the cell is part of the sheet's data, and so does a+ * border: an empty bordered cell is a drawn box, a form or a table outline someone means to keep.+ * A fill does not, because it comes from colouring whole rows, which would carry the grid out to+ * the far edge for a band nobody reads.*/-function holdsSomething(cell: ICellData): boolean {- return Boolean(cell.f) || hasContent(cell);+function holdsSomething(cell: ICellData, styles: Record<string, IStyleData | null>): boolean {+ if (cell.f || hasContent(cell)) return true;++ const borders = resolveCellStyle(cell.s, styles)?.bd;+ return Boolean(borders && (borders.t?.s || borders.r?.s || borders.b?.s || borders.l?.s));}/** Whether a cell shows anything. A cell carrying only a style is empty, as it is in the editor. */