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
feat(share/spreadsheet): handle gridlines
Elian Doran07aa89c42e36
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 1e245cf6dd..b435d336c9 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -640,16 +640,17 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain("<td>x</td>");});- it("renders borders on all four sides with widths and styles", () => {+ it("renders borders on all four sides with the correct Univer widths and styles", () => {+ // Univer BorderStyleTypes: THIN=1, DOTTED=3, DOUBLE=7, MEDIUM=8, THICK=13.const html = renderSpreadsheetToHtml(singleCellWorkbook({v: "bordered",s: {bd: {t: { s: 1, cl: { rgb: "#111111" } }, // THIN -> 1px solid- r: { s: 6, cl: { rgb: "#222222" } }, // MEDIUM -> 2px solid- b: { s: 9, cl: { rgb: "#333333" } }, // THICK -> 3px solid- l: { s: 3, cl: { rgb: "#444444" } } // DASHED -> 1px dashed+ r: { s: 8, cl: { rgb: "#222222" } }, // MEDIUM -> 2px solid+ b: { s: 13, cl: { rgb: "#333333" } }, // THICK -> 3px solid+ l: { s: 4, cl: { rgb: "#444444" } } // DASHED -> 1px dashed}}})@@ -660,18 +661,52 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain("border-left:1px dashed #444444");});- it("renders dotted border style and defaults missing border color to #000", () => {+ it("renders dotted (3), double (7) and medium-dashed (9) border styles", () => {const html = renderSpreadsheetToHtml(singleCellWorkbook({- v: "dotted",+ v: "styles",s: {bd: {- t: { s: 4 } // DOTTED, no color -> default #000+ t: { s: 3, cl: { rgb: "#111111" } }, // DOTTED -> 1px dotted+ r: { s: 7, cl: { rgb: "#222222" } }, // DOUBLE -> 3px double+ b: { s: 9, cl: { rgb: "#333333" } } // MEDIUM_DASHED -> 2px dashed}}}));- expect(html).toContain("border-top:1px dotted #000");+ expect(html).toContain("border-top:1px dotted #111111");+ expect(html).toContain("border-right:3px double #222222");+ expect(html).toContain("border-bottom:2px dashed #333333");+ });++ it("defaults a missing border style to 1px solid and missing color to #000", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({+ v: "default",+ s: {+ bd: {+ t: {} // no style, no color -> 1px solid #000+ }+ }+ })+ );+ expect(html).toContain("border-top:1px solid #000");+ });++ it("skips a border side explicitly set to NONE (0)", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({+ v: "none",+ s: {+ bd: {+ t: { s: 0, cl: { rgb: "#111111" } },+ b: { s: 1, cl: { rgb: "#222222" } }+ }+ }+ })+ );+ expect(html).not.toContain("border-top");+ expect(html).toContain("border-bottom:1px solid #222222");});it("skips border sides that are null or undefined", () => {@@ -1056,6 +1091,88 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain("<td>1234.5</td>");});+ it("marks the table with show-gridlines when the sheet has gridlines enabled", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x" }, { showGridlines: 1 })+ );+ expect(html).toContain('<table class="spreadsheet-table show-gridlines">');+ });++ it("marks a filled cell with has-fill so gridlines can be suppressed under the fill", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x", s: { bg: { rgb: "#f9f9f9" } } }, { showGridlines: 1 })+ );+ expect(html).toContain('class="has-fill"');+ });++ it("marks a cell filled via a referenced style", () => {+ const input = JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: { band: { bg: { rgb: "#f1f1f1" } } },+ sheets: {+ s1: {+ id: "s1",+ name: "Sheet1",+ hidden: 0,+ rowCount: 10,+ columnCount: 5,+ mergeData: [],+ cellData: { "0": { "0": { s: "band", v: "x" } } },+ rowData: {},+ columnData: {}+ }+ }+ }+ });+ const html = renderSpreadsheetToHtml(input);+ expect(html).toContain('class="has-fill"');+ });++ it("does not add has-fill to a cell without a background", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x", s: { cl: { rgb: "#414657" } } }, { showGridlines: 1 })+ );+ expect(html).not.toContain("has-fill");+ });++ it("shows gridlines by default when showGridlines is absent (editor default)", () => {+ // singleCellWorkbook does not set showGridlines.+ const html = renderSpreadsheetToHtml(singleCellWorkbook({ v: "x" }));+ expect(html).toContain("spreadsheet-table show-gridlines");+ });++ it("omits show-gridlines when the sheet hides gridlines", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x" }, { showGridlines: 0 })+ );+ expect(html).toContain('<table class="spreadsheet-table">');+ expect(html).not.toContain("show-gridlines");+ });++ it("emits a custom gridline color as a CSS variable", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x" }, { showGridlines: 1, gridlinesColor: "#abcdef" })+ );+ expect(html).toContain("--spreadsheet-gridline-color:#abcdef");+ });++ it("does not emit a gridline color variable when gridlines are hidden", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x" }, { showGridlines: 0, gridlinesColor: "#abcdef" })+ );+ expect(html).not.toContain("--spreadsheet-gridline-color");+ });++ it("sanitizes a malicious gridline color", () => {+ const html = renderSpreadsheetToHtml(+ singleCellWorkbook({ v: "x" }, { showGridlines: 1, gridlinesColor: "#000;background:url(//evil.com)" })+ );+ expect(html).not.toContain("evil.com");+ expect(html).toContain("--spreadsheet-gridline-color:transparent");+ });+it("extends bounds to cover a merge range that exceeds the cell data", () => {const input = 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 483b8c6ac7..57d8340835 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -38,6 +38,7 @@ interface IWorksheetData {rowData?: Record<number, IRowData>;columnData?: Record<number, IColumnData>;showGridlines?: number;+ gridlinesColor?: string | null;}type CellMatrix = Record<number, Record<number, ICellData>>;@@ -117,13 +118,22 @@ const enum VerticalAlign {BOTTOM = 3}-// Border style enum+// Border style enum — mirrors Univer's `BorderStyleTypes` (@univerjs/core).const enum BorderStyle {+ NONE = 0,THIN = 1,- MEDIUM = 6,- THICK = 9,- DASHED = 3,- DOTTED = 4+ HAIR = 2,+ DOTTED = 3,+ DASHED = 4,+ DASH_DOT = 5,+ DASH_DOT_DOT = 6,+ DOUBLE = 7,+ MEDIUM = 8,+ MEDIUM_DASHED = 9,+ MEDIUM_DASH_DOT = 10,+ MEDIUM_DASH_DOT_DOT = 11,+ SLANT_DASH_DOT = 12,+ THICK = 13}// #endregion@@ -180,7 +190,7 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |const mergeMap = buildMergeMap(mergeData, minRow, maxRow, minCol, maxCol);const lines: string[] = [];- lines.push('<table class="spreadsheet-table">');+ lines.push(buildTableTag(sheet));// Colgroup for column widths.const defaultWidth = sheet.defaultColumnWidth ?? 88;@@ -214,6 +224,9 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |const value = formatCellValue(cell, cellStyle);const attrs: string[] = [];+ // Cells with a background fill carry `has-fill` so the stylesheet can suppress+ // gridlines under the fill, matching the editor (a fill covers the grid).+ if (cellStyle?.bg?.rgb) attrs.push(`class="has-fill"`);if (cssText) attrs.push(`style="${cssText}"`);if (mergeInfo) {if (mergeInfo.rowSpan > 1) attrs.push(`rowspan="${mergeInfo.rowSpan}"`);@@ -230,6 +243,27 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |return lines.join("\n");}+/**+ * Builds the opening `<table>` tag, reflecting the sheet's gridline state. Univer stores+ * gridline visibility per sheet (`showGridlines`, 0 = hidden) and an optional custom+ * `gridlinesColor`. When gridlines are on, the table gets a `show-gridlines` class so the+ * stylesheet can draw a light border on every cell; explicit per-cell borders from the+ * data are emitted inline and override those on the sides they define.+ */+function buildTableTag(sheet: IWorksheetData): string {+ // Default to shown (matching the editor) unless explicitly disabled.+ const showGridlines = sheet.showGridlines !== 0;+ if (!showGridlines) {+ return '<table class="spreadsheet-table">';+ }++ let style = "";+ if (sheet.gridlinesColor) {+ style = ` style="--spreadsheet-gridline-color:${sanitizeCssColor(sheet.gridlinesColor)}"`;+ }+ return `<table class="spreadsheet-table show-gridlines"${style}>`;+}+// #region Bounds computationinterface Bounds {@@ -388,7 +422,7 @@ function verticalAlignToCss(align: number): string | null {}function appendBorderCss(parts: string[], property: string, border: IBorderStyleData | null | undefined): void {- if (!border) return;+ if (!border || border.s === BorderStyle.NONE) return;const width = borderStyleToWidth(border.s);const color = sanitizeCssColor(border.cl?.rgb ?? "#000");const style = borderStyleToCss(border.s);@@ -397,17 +431,36 @@ function appendBorderCss(parts: string[], property: string, border: IBorderStylefunction borderStyleToWidth(style: number | undefined): string {switch (style) {- case BorderStyle.MEDIUM: return "2px";- case BorderStyle.THICK: return "3px";- default: return "1px";+ case BorderStyle.MEDIUM:+ case BorderStyle.MEDIUM_DASHED:+ case BorderStyle.MEDIUM_DASH_DOT:+ case BorderStyle.MEDIUM_DASH_DOT_DOT:+ case BorderStyle.SLANT_DASH_DOT:+ return "2px";+ case BorderStyle.THICK:+ case BorderStyle.DOUBLE:+ return "3px";+ default:+ return "1px";}}function borderStyleToCss(style: number | undefined): string {switch (style) {- case BorderStyle.DASHED: return "dashed";- case BorderStyle.DOTTED: return "dotted";- default: return "solid";+ case BorderStyle.DOTTED:+ return "dotted";+ case BorderStyle.DASHED:+ case BorderStyle.DASH_DOT:+ case BorderStyle.DASH_DOT_DOT:+ case BorderStyle.MEDIUM_DASHED:+ case BorderStyle.MEDIUM_DASH_DOT:+ case BorderStyle.MEDIUM_DASH_DOT_DOT:+ case BorderStyle.SLANT_DASH_DOT:+ return "dashed";+ case BorderStyle.DOUBLE:+ return "double";+ default:+ return "solid";}}diff --git a/packages/share-theme/src/styles/content.css b/packages/share-theme/src/styles/content.cssindex 05d57490dc..da43b883a5 100644--- a/packages/share-theme/src/styles/content.css+++ b/packages/share-theme/src/styles/content.css@@ -112,3 +112,20 @@ body.type-webView {.spreadsheet-table td {padding: 2px 4px;}++/* When the source sheet has gridlines enabled, draw a light line on every cell edge.+ Gridlines are rendered as an inset box-shadow rather than a border so they do NOT take+ part in the border-collapse conflict resolution: an inset shadow paints below the border+ layer, so any real cell border from the workbook data (emitted inline, in any style or+ width) renders on top and is never clobbered — matching how the editor layers cell+ borders over the grid. The color comes from the sheet when set, otherwise falls back to+ the theme's table border color.++ Filled cells (`has-fill`) are excluded: in the editor a background fill covers the grid,+ so only unfilled cells show gridlines. An unfilled neighbour still draws the shared edge,+ so the visible grid stops cleanly at a fill boundary. */+.spreadsheet-table.show-gridlines td:not(.has-fill) {+ box-shadow:+ inset -1px -1px 0 0 var(--spreadsheet-gridline-color, var(--background-highlight)),+ inset 1px 1px 0 0 var(--spreadsheet-gridline-color, var(--background-highlight));+}