Skip to content

Build your personal knowledge base with Trilium Notes

fix(spreadsheet): keep a filled cell readable on a dark theme

Elian Doran7f9fadd053af
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
index 45566f0bf6..e5a779057d 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -1427,6 +1427,36 @@ describe("renderSpreadsheetToHtml", () => {
expect(html).toContain("color:red");
});
+ it("gives a filled cell the color Excel shows an automatic font in", () => {
+ // The fill comes from the workbook, the surrounding text color from the theme, so a pale
+ // fill would be unreadable wherever that theme is dark.
+ const html = renderSpreadsheetToHtml(singleCellWorkbook({ v: "x", t: 1, s: { bg: { rgb: "#FFE699" } } }));
+
+ expect(html).toContain("background-color:#FFE699");
+ expect(html).toContain("color:#000");
+ });
+
+ it("leaves a filled cell's own text color alone, and an unfilled cell with none", () => {
+ const own = renderSpreadsheetToHtml(
+ singleCellWorkbook({ v: "x", t: 1, s: { bg: { rgb: "#203864" }, cl: { rgb: "#FFFFFF" } } })
+ );
+ expect(own).toContain("color:#FFFFFF");
+ expect(own).not.toContain("color:#000");
+
+ // With no fill there is nothing absolute to read against, so the cell goes on inheriting.
+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ v: "x", t: 1, s: { bl: 1 } })))
+ .not.toContain("color:");
+ });
+
+ it("lets a number format's color win over a fill's automatic one", () => {
+ const html = renderSpreadsheetToHtml(singleCellWorkbook({
+ v: -8800.2, t: 2, s: { bg: { rgb: "#FFE699" }, n: { pattern: "#,##0.00;[Red]#,##0.00" } }
+ }));
+
+ expect(html).toContain("color:red");
+ expect(html).not.toContain("color:#000");
+ });
+
it("does not apply the pattern color to a positive value", () => {
const html = renderSpreadsheetToHtml(
singleCellWorkbook({ v: 12.5, t: 2, s: { n: { pattern: "#,##0.00;[Red]#,##0.00" } } })
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 320c98c60f..6849eb7ab2 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -627,6 +627,12 @@ function buildMergeMap(mergeData: IRange[], minRow: number, maxRow: number, minC
// #region Style resolution
+/**
+ * What Excel renders an automatic font in. `parse_from_xlsx` drops that default so a cell can
+ * take the editor's themed text color; static HTML has no such default to fall back on.
+ */
+const AUTOMATIC_TEXT_COLOR = "#000";
+
function buildCssText(style: IStyleData | null, cell?: ICellData): string {
const parts: string[] = [];
@@ -655,9 +661,12 @@ function buildCssText(style: IStyleData | null, cell?: ICellData): string {
if (style.bg?.rgb) parts.push(`background-color:${sanitizeCssColor(style.bg.rgb)}`);
// A color produced by the number-format pattern (e.g. `[Red]` for negatives) takes
- // precedence over the cell's own text color, matching Univer's rendering.
+ // precedence over the cell's own text color, matching Univer's rendering. A filled cell that
+ // sets neither falls back to the color Excel shows an automatic font in, rather than to the
+ // surrounding text color: the fill comes from the workbook while that color comes from the
+ // theme around it, so a pale fill is unreadable wherever the theme is dark.
const patternColor = resolvePatternColor(style, cell);
- const textColor = patternColor ?? style.cl?.rgb;
+ const textColor = patternColor ?? style.cl?.rgb ?? (style.bg?.rgb ? AUTOMATIC_TEXT_COLOR : undefined);
if (textColor) parts.push(`color:${sanitizeCssColor(textColor)}`);
if (style.vt != null) {