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
refactor(spreadsheet): let the renderer own the gridline rule
Elian Doran7535964e45b8
diff --git a/apps/client/src/print.css b/apps/client/src/print.cssindex d6d49162ac..512f0e494a 100644--- a/apps/client/src/print.css+++ b/apps/client/src/print.css@@ -282,6 +282,11 @@ span[style] {/* #region Spreadsheet */.spreadsheet-table {border-collapse: collapse;+ /* Gridlines are drawn by the renderer's own rule; on paper they are a hairline. At 0.5pt one+ is narrower than every real cell border (>= 1px from the workbook data), so a cell's own+ border always wins the border-collapse conflict and is never clobbered. */+ --spreadsheet-gridline-width: 0.5pt;+ --spreadsheet-gridline-color: #ccc;/* Honour the per-column widths in the colgroup, as the share renderer does, so the printedgrid keeps the editor's geometry and the floating images stay over the cells they belong to. */table-layout: fixed;@@ -300,17 +305,6 @@ span[style] {overflow: visible;}-/* Gridlines: only when the sheet enables them, and only on unfilled cells (a background- fill covers the grid), mirroring the share renderer. Kept as a hairline border rather- than a box-shadow because print engines routinely drop shadows. At 0.5pt it is narrower- than every real cell border (>= 1px from the workbook data), so under border-collapse- the custom border always wins the conflict and is never clobbered. */-/* The context is in :where() so this stays a default: the renderer emits a cell's own styling- as `.spreadsheet-table .sst-*` rules, which have to win over it. */-:where(.spreadsheet-table.show-gridlines) td:not(.has-fill) {- border: 0.5pt solid var(--spreadsheet-gridline-color, #ccc);-}-/* On paper a link cannot be followed, so it keeps the cell's own text color and the underlineis the only marker, matching how .ck-content links print. */.spreadsheet-table a {diff --git a/apps/client/src/services/content_renderer.css b/apps/client/src/services/content_renderer.cssindex 44a28d9fc9..065c896d25 100644--- a/apps/client/src/services/content_renderer.css+++ b/apps/client/src/services/content_renderer.css@@ -76,16 +76,18 @@}}-/* Companion styles for the native spreadsheet renderer used by XLSX previews. The- renderer emits classed markup (spreadsheet-table / show-gridlines / has-fill) and- relies on each host to provide these rules — share-theme and print.css carry their- own copies; these mirror the share-theme semantics (see its comments for the why- of every rule, notably gridlines-as-borders and the fixed layout). */+/* Companion styles for the native spreadsheet renderer used by XLSX previews. The renderer emits+ classed markup (spreadsheet-table / show-gridlines / has-fill) plus a stylesheet carrying the+ cell styling and the gridline rule; what each host supplies is the layout around it and the+ values gridlines vary by. share-theme and print.css carry their own copies of these. */.office-preview-body .spreadsheet-table {border-collapse: collapse;width: auto;margin-top: 1em;table-layout: fixed;+ /* The gridline rule itself is emitted by the renderer; a sheet with its own gridline color+ overrides this on the table element. */+ --spreadsheet-gridline-color: var(--main-border-color);}/* The context is in :where() so this stays a default: the renderer emits a cell's own styling@@ -96,12 +98,6 @@overflow: visible;}-/* The context is in :where() so this stays a default: the renderer emits a cell's own styling- as `.spreadsheet-table .sst-*` rules, which have to win over it. */-:where(.office-preview-body .spreadsheet-table.show-gridlines) td:not(.has-fill) {- border: 1px solid var(--spreadsheet-gridline-color, var(--main-border-color));-}-.office-preview-body .spreadsheet-sheet {font-size: 11pt;line-height: 1.2;diff --git a/apps/client/src/services/spreadsheet_preview_styles.spec.ts b/apps/client/src/services/spreadsheet_preview_styles.spec.tsindex 17705aa96b..90a49d15ca 100644--- a/apps/client/src/services/spreadsheet_preview_styles.spec.ts+++ b/apps/client/src/services/spreadsheet_preview_styles.spec.ts@@ -55,4 +55,24 @@ describe("spreadsheet preview styling", () => {expect(plainCss.borderTopStyle).toBe("solid");expect(plainCss.paddingLeft).toBe("4px");});++ 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");+ });});diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 0316010618..419e33211e 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -2245,10 +2245,22 @@ describe("style deduplication", () => {expect(stylesheet).toBeTruthy();for (const rule of stylesheet.split("}").filter(Boolean)) {- expect(rule).toMatch(/^\.spreadsheet-table \.sst-/);+ // Cell rules, and the gridline rule whose context sits in :where(); neither can+ // match anything outside a rendered sheet.+ expect(rule).toMatch(/^(\.spreadsheet-table \.sst-|: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", () => {const one = renderRaw(gridWorkbook({ "0": { "0": { v: "a", t: 1, s: { bl: 1 } } } }));const other = renderRaw(gridWorkbook({ "0": { "0": { v: "b", t: 1, s: { bl: 1 } } } }));diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.tsindex 79e57171ed..e37ab9d3d6 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -74,10 +74,34 @@ export function renderSpreadsheetToHtml(jsonContent: string): string {// The stylesheet is only complete once every sheet has been rendered, so it is built last// and put in front, where a consumer that has to lift it out finds it without a parse.- const stylesheet = classes.toStylesheet();+ const stylesheet = buildStylesheet(visibleSheets, classes);return stylesheet ? `${stylesheet}\n${parts.join("\n")}` : parts.join("\n");}+/**+ * Builds the `<style>` a render is preceded by: the gridline rule when any sheet asks for+ * gridlines, then one rule per distinct cell style. Returns "" when there is nothing to write.+ */+function buildStylesheet(sheets: IWorksheetData[], classes: StyleClasses): string {+ const gridlines = sheets.some((sheet) => sheet.showGridlines !== 0) ? GRIDLINE_RULE : "";+ const rules = gridlines + classes.toRules();++ return rules ? `<style>${rules}</style>` : "";+}++/**+ * Gridlines for the sheets that enable them. Drawn as a real border rather than a box-shadow so+ * they sit in the same border-collapse model as the cells' own borders and line up with them, and+ * skipped on filled cells because a fill covers the grid, as it does in the editor. The selector's+ * context is in `:where()` so a cell's own border, emitted as a `.sst-*` rule, outranks it.+ *+ * Hosts set `--spreadsheet-gridline-color`, and `--spreadsheet-gridline-width` where a hairline is+ * wanted (print). A sheet carrying its own gridline color sets the first on the table element,+ * which shadows the host's for that sheet.+ */+const GRIDLINE_RULE = ":where(.spreadsheet-table.show-gridlines) td:not(.has-fill)"+ + "{border:var(--spreadsheet-gridline-width,1px) solid var(--spreadsheet-gridline-color,#ccc)}";+/*** Collects the declarations a render emits and hands back a class for each distinct one, so a* style shared by thousands of cells is written once in a `<style>` instead of on every cell.@@ -105,19 +129,19 @@ class StyleClasses {return name;}- /** Builds the `<style>` element for everything collected, or "" when nothing was. */- toStylesheet(): string {- if (this.names.size === 0) return "";-+ /**+ * Builds a rule per distinct style collected, or "" when none was.+ *+ * The declarations cannot contain `<` or `/`: colors are validated against a pattern and every+ * other author-supplied value goes through `sanitizeCssValue`, which strips both. That is what+ * keeps a cell's styling from closing its rule, or the element these rules are written into.+ */+ toRules(): string {const rules: string[] = [];for (const [declarations, name] of this.names) {rules.push(`.spreadsheet-table .${name}{${declarations}}`);}-- // The declarations cannot contain `<` or `/`: colors are validated against a pattern and- // every other author-supplied value goes through `sanitizeCssValue`, which strips both.- // That is what keeps a cell's styling from closing this element or its rule.- return `<style>${rules.join("")}</style>`;+ return rules.join("");}}diff --git a/packages/share-theme/src/styles/content.css b/packages/share-theme/src/styles/content.cssindex 558bd899c8..b5d5d6a617 100644--- a/packages/share-theme/src/styles/content.css+++ b/packages/share-theme/src/styles/content.css@@ -112,6 +112,13 @@ body.type-webView {merge into single lines instead of doubling across the default border-spacing gaps. */.spreadsheet-table {border-collapse: collapse;+ /* The gridline color, for the sheets that enable gridlines; the rule drawing them is emitted+ by the renderer, and a sheet carrying its own color overrides this on the table element.+ Borders collapse, so adjacent cells' gridlines merge into single 1px lines (no doubling).+ A real data border at the same 1px width ties the gridline under collapse and wins by+ source order, so heavier (>=2px) and filled-cell borders always show; an unfilled cell's+ own 1px border may cede its top/left edge to a neighbour's gridline — fine for a faint grid. */+ --spreadsheet-gridline-color: var(--background-highlight);width: auto;margin-top: 1em;/* Honour the per-column widths emitted in the colgroup (and the explicit table width) so the@@ -129,25 +136,6 @@ body.type-webView {overflow: visible;}-/* When the source sheet has gridlines enabled, draw a light 1px line on every cell edge.- Implemented as a real border (not a box-shadow) so the gridline sits in the same- border-collapse model as the cells' own borders and aligns pixel-exactly with them — an- inset shadow is offset 1px inside the edge and visibly misaligns with collapsed borders.- Borders collapse, so adjacent cells' gridlines merge into single 1px lines (no doubling).-- Filled cells (`has-fill`) are skipped: in the editor a background fill covers the grid, so- only unfilled cells show gridlines; a filled neighbour hides the line on that shared edge.- The color comes from the sheet when set, else the theme's table border color.-- A real data border at the same 1px width ties the gridline under collapse and wins by- source order, so heavier (>=2px) and filled-cell borders always show; an unfilled cell's- own 1px border may cede its top/left edge to a neighbour's gridline — fine for a faint grid. */-/* The context is in :where() so this stays a default: the renderer emits a cell's own styling- as `.spreadsheet-table .sst-*` rules, which have to win over it. */-:where(.spreadsheet-table.show-gridlines) td:not(.has-fill) {- border: 1px solid var(--spreadsheet-gridline-color, var(--background-highlight));-}-/* A sheet that has floating images is wrapped in a positioned container; the images are emitted asabsolutely-positioned <img> with inline left/top/width/height in the grid's content coordinates(the Univer transform, with the row/column header sizes removed). */