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: add support for rotated text
Adorian Doranf5900e80afd4
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 85e00cefbc..8d95f3bd11 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -508,6 +508,50 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain("42.0%");});+ it("rotates a cell's text", () => {+ const rotated = (tr: unknown) => renderSpreadsheetToHtml(singleCellWorkbook({ v: "Days left", t: 1, s: { tr } }));++ // A quarter turn either way becomes a vertical writing mode, so the row can grow to fit it.+ expect(rotated({ a: 90 })).toContain(+ `<span style="display:inline-block;writing-mode:vertical-rl;transform:rotate(180deg)">Days left</span>`);+ expect(rotated({ a: -90 })).toContain(+ `<span style="display:inline-block;writing-mode:vertical-rl">Days left</span>`);++ // Stacked text reads downwards with upright characters.+ expect(rotated({ v: 1 })).toContain(+ `<span style="display:inline-block;writing-mode:vertical-rl;text-orientation:upright">Days left</span>`);++ // Any other angle turns the glyphs; CSS rotates clockwise, so the sign flips.+ expect(rotated({ a: 45 })).toContain(+ `<span style="display:inline-block;transform:rotate(-45deg)">Days left</span>`);+ expect(rotated({ a: -30.5 })).toContain(`transform:rotate(30.5deg)`);+ });++ it("leaves a cell unwrapped when it has no rotation to apply", () => {+ const plain = (s: unknown) => renderSpreadsheetToHtml(singleCellWorkbook({ v: "Days left", t: 1, s }));++ expect(plain({})).toContain("<td>Days left</td>");+ expect(plain({ tr: { a: 0, v: 0 } })).toContain("<td>Days left</td>");+ expect(plain({ tr: { v: 0 } })).toContain("<td>Days left</td>");++ // An empty cell gets no wrapper either, rotation or not.+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ s: { tr: { a: 90 } } }))).toContain("<td></td>");+ });++ it("keeps a cell's image upright when its text is rotated", () => {+ const html = renderSpreadsheetToHtml(singleCellWorkbook({+ v: "Days left",+ t: 1,+ s: { tr: { a: 90 } },+ p: {+ drawings: { d1: { drawingId: "d1", source: "api/attachments/abc123/image/i.png" } },+ drawingsOrder: ["d1"]+ }+ }));++ expect(html).toContain(`Days left</span><img class="spreadsheet-cell-image"`);+ });+// Helper to wrap a single styled cell into a complete workbook payload.function singleCellWorkbook(cell: unknown, sheetExtra: Record<string, unknown> = {}): string {return JSON.stringify({diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.tsindex c68be50bb9..c51d82c663 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -27,6 +27,7 @@ import {type IRange,type ISheetDrawing,type IStyleData,+ type ITextRotation,type IWorksheetData,parseWorkbookData,resolveCellStyle,@@ -295,7 +296,8 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |const cell = cellData[row]?.[col];const cellStyle = resolveCellStyle(cell?.s, styles);const cssText = buildCssText(cellStyle, cell);- const value = formatCellValue(cell, cellStyle) + (cell ? renderCellImages(cell) : "");+ const value = rotate(formatCellValue(cell, cellStyle), cellStyle?.tr)+ + (cell ? renderCellImages(cell) : "");const attrs: string[] = [];// Cells with a background fill carry `has-fill` so the stylesheet can suppress@@ -563,6 +565,36 @@ function formatCellValue(cell: ICellData | undefined, style: IStyleData | null):return escapeHtml(String(cell.v));}+/**+ * Wraps a cell's text in the element that carries its rotation. The wrapper keeps the rotation off+ * the `td` itself, which would take the background and borders with it, and images anchored in the+ * cell stay upright as they do in the editor.+ */+function rotate(html: string, rotation: ITextRotation | null | undefined): string {+ if (!html) return html;++ const css = rotationCss(rotation);+ return css ? `<span style="${css}">${html}</span>` : html;+}++/**+ * The CSS for Univer's text rotation. A quarter turn and stacked text become a vertical writing+ * mode, which gives the text a real vertical box the row grows to fit; any other angle falls back+ * to a transform, which turns the glyphs without reserving room for them. Excel measures the angle+ * counter-clockwise and CSS turns clockwise, so the sign flips.+ */+function rotationCss(rotation: ITextRotation | null | undefined): string | null {+ if (!rotation) return null;+ // Stacked text: upright characters reading downwards.+ if (rotation.v) return "display:inline-block;writing-mode:vertical-rl;text-orientation:upright";++ const angle = isFiniteNumber(rotation.a) ? rotation.a : 0;+ if (angle === 0) return null;+ if (angle === 90) return "display:inline-block;writing-mode:vertical-rl;transform:rotate(180deg)";+ if (angle === -90) return "display:inline-block;writing-mode:vertical-rl";+ return `display:inline-block;transform:rotate(${px(-angle)}deg)`;+}+/*** Renders a cell from its rich-text document, or returns `null` when the plain value is the* better source. The document wins when the cell has no plain value, which is how Univer stores