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
Merge branch 'main' into feature/xlsx_performance_improvement
Elian Doran2b6ebc4a1fbd
diff --cc apps/client/src/services/spreadsheet_preview_styles.spec.tsindex 365ab0e9de,0000000000..0ac381d54dmode 100644,000000..100644--- a/apps/client/src/services/spreadsheet_preview_styles.spec.ts+++ b/apps/client/src/services/spreadsheet_preview_styles.spec.ts@@@ -1,114 -1,0 +1,116 @@@+import { readFileSync } from "node:fs";+import { join } from "node:path";++import { renderSpreadsheetToHtml } from "@triliumnext/commons/src/lib/spreadsheet/render_to_html.js";+import { describe, expect, it } from "vitest";++/**+ * The renderer emits a workbook's cell styling as `.spreadsheet-table .sst-*` rules, while+ * content_renderer.css supplies the defaults every cell starts from. Those two have to stay on+ * speaking terms: if a default ever outranks a generated rule, cells silently lose their+ * borders, wrapping or clipping. This renders a workbook against the real stylesheet and reads+ * the computed styles back, which is the only way that contract shows up as a test failure.+ */+function spreadsheetRulesFrom(stylesheet: string): string {+ const css = readFileSync(join(__dirname, stylesheet), "utf-8");+ return (css.match(/[^{}]*spreadsheet-table[^{]*\{[^}]*\}/g) ?? []).join("\n");+}++const WORKBOOK = JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: {},+ sheets: {+ s1: {+ id: "s1", name: "S", hidden: 0, rowCount: 10, columnCount: 5, showGridlines: 1,+ mergeData: [],+ cellData: {+ "0": {+ // Wraps and carries its own border, both of which the defaults also set.+ "0": { v: "a long wrapped label", t: 1, s: { tb: 3, bd: { t: { s: 3, cl: { rgb: "#FF0000" } } } } },+ "1": { v: "next", t: 1 }+ }+ },+ rowData: {}, columnData: {}+ }+ }+ }+});++describe("spreadsheet preview styling", () => {+ it("lets a cell's own styling outrank the preview defaults, which still reach a plain cell", () => {+ document.head.innerHTML = `<style>${spreadsheetRulesFrom("content_renderer.css")}</style>`;+ document.body.innerHTML = `<div class="ck-content office-preview-body">${renderSpreadsheetToHtml(WORKBOOK)}</div>`;++ const [styled, plain] = document.querySelectorAll("td");+ const styledCss = getComputedStyle(styled);++ expect(styledCss.whiteSpace).toBe("normal");+ expect(styledCss.borderTopStyle).toBe("dotted");+ expect(styledCss.borderTopColor).toBe("#FF0000");+- // A cell that styles nothing still picks up the gridline and padding defaults.++ // A cell that styles nothing still picks up the gridline default.+ const plainCss = getComputedStyle(plain);+ expect(plainCss.borderTopStyle).toBe("solid");- expect(plainCss.paddingLeft).toBe("4px");++ // Its padding comes from the renderer, which measures every cell holding content against++ // the box the editor lays it out in, rather than from the stylesheet's own.++ expect(plainCss.paddingLeft).toBe("2px");+ });++ it("draws gridlines from the host's color, which a sheet's own gridline color overrides", () => {+ // The stylesheet resolves the host color to var(--main-border-color); happy-dom cannot+ // resolve a custom property whose value is itself a var(), so the token is substituted+ // here. The rule under test — the renderer's own — is used exactly as it ships.+ document.head.innerHTML = `<style>${spreadsheetRulesFrom("content_renderer.css")}+ .office-preview-body .spreadsheet-table { --spreadsheet-gridline-color: #aabbcc; }</style>`;++ const gridlineColor = (gridlinesColor?: string) => {+ const workbook = JSON.parse(WORKBOOK);+ if (gridlinesColor) workbook.workbook.sheets.s1.gridlinesColor = gridlinesColor;+ document.body.innerHTML =+ `<div class="ck-content office-preview-body">${renderSpreadsheetToHtml(JSON.stringify(workbook))}</div>`;+ // The second cell styles nothing of its own, so what it shows is the gridline.+ return getComputedStyle(document.querySelectorAll("td")[1]).borderTopColor;+ };++ expect(gridlineColor()).toBe("#aabbcc");+ expect(gridlineColor("#00FF00")).toBe("#00FF00");+ });++ it("applies the folded td default to bare cells, and lets a classed cell override it", () => {+ // Three bold cells and one italic: bold folds into the default, italic keeps a class and+ // is completed with font-weight:inherit so it does not come out bold as well.+ const workbook = JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"], styles: {},+ sheets: {+ s1: {+ id: "s1", name: "S", hidden: 0, rowCount: 10, columnCount: 5, showGridlines: 1,+ mergeData: [],+ cellData: {+ "0": { "0": { v: "a", t: 1, s: { bl: 1 } } },+ "1": { "0": { v: "b", t: 1, s: { bl: 1 } } },+ "2": { "0": { v: "c", t: 1, s: { bl: 1 } } },+ "3": { "0": { v: "d", t: 1, s: { it: 1 } } }+ },+ rowData: {}, columnData: {}+ }+ }+ }+ });++ document.head.innerHTML = `<style>${spreadsheetRulesFrom("content_renderer.css")}</style>`;+ document.body.innerHTML =+ `<div class="ck-content office-preview-body">${renderSpreadsheetToHtml(workbook)}</div>`;++ const [folded, , , classed] = document.querySelectorAll("td");+ expect(folded.getAttribute("class")).toBeNull();+ expect(getComputedStyle(folded).fontWeight).toBe("bold");++ // The cell that kept a class is not dragged bold by the default it sits under.+ expect(getComputedStyle(classed).fontStyle).toBe("italic");+ expect(getComputedStyle(classed).fontWeight).not.toBe("bold");+ });+});diff --cc packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex bcf3374cc1,4e482fef8a..4f218b0813--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@@ -1,75 -1,20 +1,87 @@@import { describe, expect, it } from "vitest";-import { renderSpreadsheetToHtml } from "./render_to_html.js";+import { renderSpreadsheetToHtml as renderRaw } from "./render_to_html.js";+ import { BorderStyle, HorizontalAlign, VerticalAlign, WrapStrategy } from "./workbook_model.js";+/**+ * Renders, then expands the emitted `<style>` back into `style` attributes, so a test asserts on+ * the declarations a cell resolves to rather than on generated class names. What the renderer+ * actually emits is covered by the "style deduplication" block, which uses `renderRaw`.+ */+function renderSpreadsheetToHtml(jsonContent: string): string {+ return withInlineStyles(renderRaw(jsonContent));+}++function withInlineStyles(html: string): string {+ const stylesheet = /^<style>([\s\S]*?)<\/style>\n/.exec(html);+ if (!stylesheet) return html;++ const declarations = new Map<string, string>();+ for (const rule of stylesheet[1].matchAll(/\.spreadsheet-table \.([\w-]+)\{([^}]*)\}/g)) {+ declarations.set(rule[1], withoutThemedColors(rule[2]));+ }+ // The renderer folds the commonest cell style into a `td` default, leaving those cells without+ // a class; every other rule sets what the default does, so a classed cell resolves to its rule+ // alone. Putting the default back on the bare cells is what the browser works out.+ const fallback = withoutThemedColors(/\.spreadsheet-table td\{([^}]*)\}/.exec(stylesheet[1])?.[1] ?? "");++ const body = html.slice(stylesheet[0].length).replace(/ class="([^"]*)"/g, (_whole, names: string) => {+ const kept: string[] = [];+ const css: string[] = [];+ for (const name of names.split(" ")) {+ const declaration = declarations.get(name);+ if (declaration) css.push(declaration);+ else kept.push(name);+ }+ return (kept.length ? ` class="${kept.join(" ")}"` : "") + (css.length ? ` style="${css.join(";")}"` : "");+ });++ return fallback ? body.replace(/<td(?=[ >])(?![^>]*style=")/g, `<td style="${fallback}"`) : body;+}++/**+ * Drops the `light-dark()` declaration each color is followed by, leaving the plain one in front+ * of it — which is what a light theme renders, and what every assertion here is written against.+ * The pairing itself is covered by the "dark mode" block, reading what is actually emitted.+ */+function withoutThemedColors(declarations: string): string {+ return declarations.split(";").filter((part) => !part.includes("light-dark(")).join(";");+}- import { BorderStyle, HorizontalAlign, WrapStrategy } from "./workbook_model.js";+++ /**+ * The markup with the box model stripped: each cell's sizing box and the padding it is measured+ * against, leaving the content and the cell's own theming.+ */+ function unboxed(html: string): string {+ return html+ .replace(/<td([^>]*)><span style="display:block;overflow:hidden[^"]*">([\s\S]*?)<\/span><\/td>/g, "<td$1>$2</td>")+ .replace(/padding:[^;"]*;?/g, "")+ .replace(/;"/g, '"')+ .replace(/ style=""/g, "");+ }describe("renderSpreadsheetToHtml", () => {+ it("accepts an already-parsed workbook as well as its JSON", () => {+ // The XLSX preview holds the workbook `parseXlsxToWorkbook` returns, so it renders it+ // directly instead of serializing it for the renderer to parse back.+ const data = {+ version: 1,+ workbook: {+ sheetOrder: ["sheet1"],+ styles: { boldStyle: { bl: 1 } },+ sheets: {+ sheet1: {+ id: "sheet1",+ name: "Sheet1",+ cellData: { 0: { 0: { v: "Hello", t: 1, s: "boldStyle" } } }+ }+ }+ }+ };++ expect(renderRaw(data)).toBe(renderRaw(JSON.stringify(data)));+ expect(renderRaw(data)).toContain("Hello");+ });+it("renders a basic spreadsheet with values and styles", () => {const input = JSON.stringify({version: 1,@@@ -2199,177 -2459,3 +2558,181 @@@expect(html).toContain('colspan="4"');});});++describe("style deduplication", () => {+ const gridWorkbook = (cellData: Record<string, unknown>) => JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: {},+ sheets: {+ s1: {+ id: "s1", name: "Sheet1", hidden: 0, rowCount: 1000, columnCount: 20,+ mergeData: [], cellData, rowData: {}, columnData: {}+ }+ }+ }+ });++ // One cell per row, so none has an occupied neighbour and none of them clips.+ const rows = (...cells: unknown[]) => Object.fromEntries(cells.map((cell, row) => [String(row), { "0": cell }]));+ const bold = { v: "x", t: 1, s: { bl: 1 } };+ const italic = { v: "y", t: 1, s: { it: 1 } };++ it("writes a style shared by many cells once, and puts the stylesheet in front", () => {+ const html = renderRaw(gridWorkbook(rows(bold, bold, bold, italic)));++ expect(html.startsWith("<style>")).toBe(true);+ // One rule for the three bold cells, not three copies of the declaration. Being the+ // commonest style they carry, it is written as the folded `td` default.+ expect(html.match(/font-weight:bold/g)).toHaveLength(1);- expect(html).toContain(".spreadsheet-table td{font-weight:bold}");++ expect(html).toMatch(/\.spreadsheet-table td\{[^}]*font-weight:bold/);++ // The minority style keeps a class, used by the one cell that has it.+ const name = /\.spreadsheet-table \.(sst-[\w-]+)\{font-style:italic/.exec(html)?.[1];+ expect(name).toBeTruthy();+ expect(html.match(new RegExp(`class="${name}"`, "g"))).toHaveLength(1);+ });++ it("scopes every rule under the table so a class cannot reach the rest of the page", () => {+ const stylesheet = /^<style>([\s\S]*?)<\/style>/.exec(renderRaw(gridWorkbook(rows(bold, italic))))?.[1] ?? "";++ expect(stylesheet).toBeTruthy();+ for (const rule of stylesheet.split("}").map((r) => r.trim()).filter(Boolean)) {+ // Cell rules, the folded `td` default, and the gridline rule whose context sits in+ // :where(); none of them can match anything outside a rendered sheet.+ expect(rule).toMatch(/^(\.spreadsheet-table (\.sst-|td\{)|:where\(\.spreadsheet-table)/);+ }+ });++ it("emits the gridline rule only for a workbook that asks for gridlines", () => {+ const cells = { "0": { "0": { v: "x", t: 1 } } };+ const withGridlines = renderRaw(gridWorkbook(cells));+ expect(withGridlines).toContain(":where(.spreadsheet-table.show-gridlines) td:not(.has-fill)");++ const off = JSON.parse(gridWorkbook(cells));+ off.workbook.sheets.s1.showGridlines = 0;+ expect(renderRaw(JSON.stringify(off))).not.toContain("show-gridlines");+ });++ it("names a class after its declarations, so two documents share rather than collide", () => {+ // Bold dominates in both, so italic is the one left carrying a class.+ const nameIn = (html: string) => /\.spreadsheet-table \.(sst-[\w-]+)\{font-style:italic/.exec(html)?.[1];+ const one = renderRaw(gridWorkbook(rows(bold, bold, italic)));+ const other = renderRaw(gridWorkbook(rows(bold, bold, italic, bold)));++ expect(nameIn(one)).toBeTruthy();+ expect(nameIn(one)).toBe(nameIn(other));+ // A different declaration gets a different name.+ const underlined = renderRaw(gridWorkbook(rows(bold, bold, { v: "y", t: 1, s: { ul: { s: 1 } } })));+ expect(nameIn(one)).not.toBe(/\.spreadsheet-table \.(sst-[\w-]+)\{text-decoration/.exec(underlined)?.[1]);+ });++ it("keeps has-fill alongside the generated class, and emits no stylesheet for an empty sheet", () => {+ const filled = { v: "x", t: 1, s: { bg: { rgb: "#FFE699" } } };+ expect(renderRaw(gridWorkbook(rows(bold, bold, filled)))).toMatch(/<td class="has-fill sst-[\w-]+"/);++ expect(renderRaw(JSON.stringify({ version: 1, workbook: { sheetOrder: [], styles: {}, sheets: {} } })))+ .not.toContain("<style>");+ });++ describe("folding the commonest style into a td default", () => {+ it("drops the class from the cells that used it, keeping it on the rest", () => {+ const html = renderRaw(gridWorkbook(rows(bold, bold, bold, italic)));+- expect(html).toContain(".spreadsheet-table td{font-weight:bold}");++ expect(html).toMatch(/\.spreadsheet-table td\{[^}]*font-weight:bold/);+ // Three bare cells for the folded style, one still classed for the other.+ expect(html.match(/<td>/g)).toHaveLength(3);+ expect(html.match(/<td class="sst-[\w-]+">/g)).toHaveLength(1);+ });++ it("completes a rule that leaves an inherited property the default sets unset", () => {+ // The default sets font-weight; the italic cell says nothing about it, and would+ // otherwise come out bold. font-weight is inherited, so `inherit` restores exactly+ // what no declaration at all would have computed to.+ const html = renderRaw(gridWorkbook(rows(bold, bold, italic)));+- expect(html).toContain(".spreadsheet-table td{font-weight:bold}");- expect(html).toMatch(/\.spreadsheet-table \.sst-[\w-]+\{font-style:italic;font-weight:inherit\}/);++ expect(html).toMatch(/\.spreadsheet-table td\{[^}]*font-weight:bold/);++ expect(html).toMatch(/\.spreadsheet-table \.sst-[\w-]+\{[^}]*font-style:italic[^}]*font-weight:inherit\}/);+ });++ it("declines when a rule would inherit a property that cannot be reset", () => {+ // A background fill dominates, and no value means "as if never declared" for it, so+ // the bold cell could not be kept from picking the fill up. Nothing is folded.+ const filled = { v: "x", t: 1, s: { bg: { rgb: "#FFE699" } } };+ const html = renderRaw(gridWorkbook(rows(filled, filled, filled, bold)));++ expect(html).not.toContain(".spreadsheet-table td{");+ expect(html).toMatch(/<td class="has-fill sst-[\w-]+"/);+ });++ it("declines when a cell styles nothing, having no rule to complete", () => {- // A left-aligned text cell emits no declarations at all, so it carries no class and- // would take the default whole.- const html = renderRaw(gridWorkbook(rows(bold, bold, { v: "plain", t: 1 })));++ // A cell holding nothing takes neither a style nor the padding a cell with content++ // gets, so it carries no class and would take the default whole. Here one sits in the++ // gap between two bold cells, inside the bounds their content sets.++ const html = renderRaw(gridWorkbook({++ "0": { "0": bold, "2": bold },++ "1": { "0": bold }++ }));++ expect(html).not.toContain(".spreadsheet-table td{");- expect(html).toContain("<td>plain</td>");++ expect(html).toContain("<td></td>");+ });+ });+});++describe("dark mode", () => {+ const cellWorkbook = (cell: Record<string, unknown>) => JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"], styles: {},+ sheets: {+ s1: {+ id: "s1", name: "S", hidden: 0, rowCount: 5, columnCount: 5, showGridlines: 0,+ mergeData: [], cellData: { "0": { "0": cell } }, rowData: {}, columnData: {}+ }+ }+ }+ });++ it("pairs a fill and its text with what a dark theme shows instead", () => {+ const html = renderRaw(cellWorkbook({ v: "x", t: 1, s: { bg: { rgb: "#FFE699" } } }));++ // Pale yellow darkens to brown rather than to the blue a plain 255-c would give, and the+ // automatic font colour it sits on flips the other way.+ expect(html).toContain("background-color:light-dark(#FFE699,#543b00)");+ expect(html).toContain("color:light-dark(#000,#ffffff)");+ });++ it("pairs a border colour too, as Univer inverts every colour it paints", () => {+ const html = renderRaw(cellWorkbook({+ v: "x", t: 1, s: { bd: { t: { s: 1, cl: { rgb: "#203864" } }, b: { s: 1 } } }+ }));++ expect(html).toContain("border-top:1px solid light-dark(#203864,#a2bae6)");+ // A border with no colour of its own defaults to black, which inverts to white.+ expect(html).toContain("border-bottom:1px solid light-dark(#000,#ffffff)");+ });++ it("inverts a named colour a number format asks for", () => {+ const html = renderRaw(cellWorkbook({+ v: -8800.2, t: 2, s: { n: { pattern: "#,##0.00;[Red]#,##0.00" } }+ }));++ expect(html).toContain("color:light-dark(red,#ff5555)");+ });++ it("leaves a colour it cannot read alone rather than dropping it", () => {+ // sanitizeCssColor passes rgb() through, and nothing in a workbook produces one, so it has+ // no dark half; emitting it unpaired is better than emitting nothing.+ const html = renderRaw(cellWorkbook({ v: "x", t: 1, s: { cl: { rgb: "rgb(1,2,3)" } } }));++ expect(html).toContain("color:rgb(1,2,3)");+ expect(html).not.toContain("light-dark(rgb");+ });++ it("inverts a grey to its exact complement", () => {+ const html = renderRaw(cellWorkbook({ v: "x", t: 1, s: { bg: { rgb: "#DBDBDB" } } }));+ expect(html).toContain("background-color:light-dark(#DBDBDB,#242424)");+ });+});diff --cc packages/commons/src/lib/spreadsheet/render_to_html.tsindex a0c1a63757,e0352a96bb..62ba60d6d2--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@@ -434,12 -291,18 +485,18 @@@ function trackIndexAtPx(targetPx: numbe// #endregion-function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData | null>, images: PlacedImage[]): string {+function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData | null>, images: PlacedImage[], classes: StyleClasses): string {const { cellData, mergeData = [], columnData = {}, rowData = {} } = sheet;- // Determine the actual bounds (only cells with data), then extend them to cover any floating- // images that reach past the data so the grid encloses them like the editor does.- const bounds = computeBounds(cellData, mergeData);+ // A sheet often carries formatting far past its data: a fill applied to whole rows leaves tens+ // of thousands of empty cells, each of which would become a `td` the browser has to lay out for+ // a band it draws as one rectangle. The grid is bounded by the cells that hold something, and+ // falls back to every cell for a sheet that is nothing but formatting. Then it is extended to+ // 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), (cell) => holdsSomething(cell, styles))+ ?? computeBounds(cellData, mergeData);const { maxRow, maxCol } = extendBoundsForImages(sheet, bounds?.maxRow ?? -1, bounds?.maxCol ?? -1, images);if (maxRow < 0 || maxCol < 0) {return "<p>Empty sheet.</p>";@@@ -467,10 -330,20 +524,20 @@@const lines: string[] = [];lines.push(buildTableTag(sheet, totalWidth));- const cols = colWidths.map((width) => `<col class="${classes.classFor(`width:${px(width)}px`)}">`);- lines.push("<colgroup>");++ const cols: string[] = [];+ for (let index = 0; index < colWidths.length;) {+ let span = 1;+ while (index + span < colWidths.length && colWidths[index + span] === colWidths[index]) span++;- lines.push(`<col${span > 1 ? ` span="${span}"` : ""} style="width:${px(colWidths[index])}px">`);++ cols.push(`<col${span > 1 ? ` span="${span}"` : ""} class="${classes.classFor(`width:${px(colWidths[index])}px`)}">`);+ index += span;+ }- lines.push("</colgroup>");+ lines.push(`<colgroup>${cols.join("")}</colgroup>`);const defaultHeight = sheet.defaultRowHeight ?? 24;+ const heights: number[] = [];+ for (let row = minRow; row <= maxRow; row++) {+ heights[row] = rowData[row]?.hd ? 0 : rowHeight(rowData[row], defaultHeight);+ }for (let row = minRow; row <= maxRow; row++) {const rowMeta = rowData[row];@@@ -480,10 -353,9 +547,10 @@@// Univer leaves a cell at the bottom of its row unless it sets `vt`, while HTML centres it.// The row carries that default because a `td` inherits `vertical-align`, so a cell that// sets its own still wins.- lines.push(`<tr style="height:${height}px;vertical-align:bottom">`);+ const rowClass = classes.classFor(`height:${height}px;vertical-align:bottom`);- const neighbours: RowNeighbours = { row, minCol, maxCol, cellData, columnData, mergeMap };+ const neighbours: RowNeighbours = { row, minCol, maxCol, cellData, columnData, mergeMap, defaultWidth };+ const cells: string[] = [];for (let col = minCol; col <= maxCol; col++) {if (columnData[col]?.hd) continue;