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: improve test coverage
Adorian Dorana0c4bae99d65
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 52271bdab9..4e482fef8a 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -1303,6 +1303,14 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain('<tr style="height:50px;vertical-align:bottom">');});+ it("lays a cell out with the padding it states", () => {+ const html = renderSpreadsheetToHtml(singleCellWorkbook({ v: "x", t: 1, s: { pd: { t: 3, r: 6, b: 5, l: 6 } } }));++ expect(html).toContain(`<td style="padding:3px 6px 5px 6px">`);+ // The box is measured against that padding rather than the default.+ expect(html).toContain("max-height:16px");+ });+it("takes a self-sizing row's height from the height Univer measured for it", () => {const html = renderSpreadsheetToHtml(JSON.stringify({version: 1,@@ -1929,6 +1937,16 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain(`<img style="position:absolute;left:-4px;top:-8px;width:110px;height:70px"`);});+ it("treats a crop side the drawing leaves out as no inset", () => {+ const html = renderSpreadsheetToHtml(workbookWithFloatingDrawings([{+ ...urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 0, top: 0, width: 100, height: 50 }),+ srcRect: { left: 4 }+ }]));++ // Only the left is cut, so the image grows by that alone and is pulled back by it.+ expect(html).toContain(`<img style="position:absolute;left:-4px;top:0px;width:104px;height:50px"`);+ });+it("leaves a drawing whose crop takes nothing as a bare image", () => {const html = renderSpreadsheetToHtml(workbookWithFloatingDrawings([{...urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 0, top: 0, width: 100, height: 50 }),@@ -2324,6 +2342,20 @@ describe("renderSpreadsheetToHtml", () => {expect((html.match(/<tr/g) ?? []).length).toBe(1);});+ it("counts hidden and explicitly sized columns when extending the grid sideways", () => {+ // The column axis has to read the same way as the row one: a hidden column takes none of+ // the image's reach, and a column with a width of its own is counted at it.+ const html = renderSpreadsheetToHtml(+ workbookWithFloatingDrawings(+ [urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 0, top: 0, width: 200, height: 10 })],+ { columnData: { "0": { hd: 1 }, "1": { w: 100 }, "2": { w: 100 } } }+ )+ );+ // Column 0 contributes nothing and columns 1-2 contribute 100px each, so the image ends on+ // column 2 and the grid grows to three, of which the hidden one is not emitted.+ expect(html).toContain(`<col span="2" style="width:100px">`);+ });+it("counts hidden and explicitly sized tracks correctly when extending the grid", () => {// Hidden rows occupy no space, so they must not consume any of the image's reach, while// rows carrying an explicit height are counted at that height rather than the default.diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.tsindex 096728d696..e0352a96bb 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -602,23 +602,20 @@ function spillBound(}// Text runs the way its alignment points; rightwards a merged cell starts past its own range.- const align = style?.ht;- const before = align === HorizontalAlign.RIGHT || align === HorizontalAlign.CENTER- ? spillRoom(col, -1, neighbours)- : null;- const after = align === HorizontalAlign.RIGHT- ? null- : spillRoom(merge?.endColumn ?? col, 1, neighbours);+ const leftwards = () => spillRoom(col, -1, neighbours).width;+ const rightwards = () => spillRoom(merge?.endColumn ?? col, 1, neighbours).width;// Centred text has to stay centred on its own cell, so the room it is given is the same on// both sides: growing one side further would carry the middle of the text away from the// middle of the cell, which is where the editor keeps it.- if (align === HorizontalAlign.CENTER) {- const room = Math.min(before?.width ?? 0, after?.width ?? 0);+ if (style?.ht === HorizontalAlign.CENTER) {+ const room = Math.min(leftwards(), rightwards());return { before: room, after: room };}- return { before: before?.width ?? 0, after: after?.width ?? 0 };+ return style?.ht === HorizontalAlign.RIGHT+ ? { before: leftwards(), after: 0 }+ : { before: 0, after: rightwards() };}/**@@ -688,10 +685,8 @@ function isTurned(rotation: ITextRotation | null | undefined): boolean {* the cell's own alignments already place.*/function rotatesByTransform(rotation: ITextRotation | null | undefined): boolean {- if (!rotation || rotation.v) return false;-- const angle = isFiniteNumber(rotation.a) ? rotation.a : 0;- return angle !== 0 && angle !== 90 && angle !== -90;+ if (!rotation || rotation.v || !isFiniteNumber(rotation.a)) return false;+ return rotation.a !== 0 && rotation.a !== 90 && rotation.a !== -90;}/**@@ -730,7 +725,7 @@ function turnedJustify(style: IStyleData | null): string {/** The height a cell covers, summing the rows a `rowspan` reaches across. */function spannedHeight(heights: number[], from: number, to: number): number {let total = 0;- for (let row = from; row <= to; row++) total += heights[row] ?? 0;+ for (let row = from; row <= to; row++) total += heights[row];return total;}