Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet/preview: fix cell value alignment in certain conditions

Adorian Doran89f64daf8e6c
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 03b84c49eb..749801b90f 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -690,10 +690,13 @@ describe("renderSpreadsheetToHtml", () => {
const centered = { t: 1, s: { ht: HorizontalAlign.CENTER } };
expect(room(row({ 0: { v: "before", t: 1 }, 1: { v: "x", ...centered } }), 1)).toEqual([0, 0]);
- expect(room(row({ 1: { v: "x", ...centered } }), 1)).toEqual([88, 0]);
+ // Nothing to its right, so the room on its left is given up to keep it centred.
+ expect(room(row({ 1: { v: "x", ...centered } }), 1)).toEqual([0, 0]);
- // Stopped on the right, with the empty column on its left still open to it.
- expect(room(row({ 1: { v: "x", ...centered }, 2: { v: "after", t: 1 } }), 1)).toEqual([88, 0]);
+ // Centred text is given the same room on both sides, so the middle of the text stays on
+ // the middle of the cell: stopped on the right, it gives up the room on its left too.
+ expect(room(row({ 1: { v: "x", ...centered }, 2: { v: "after", t: 1 } }), 1)).toEqual([0, 0]);
+ expect(room(row({ 1: { v: "x", ...centered }, 3: { v: "after", t: 1 } }), 1)).toEqual([88, 88]);
});
it("keeps turned text in its own cell rather than running it into a neighbour", () => {
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index f000bb6c40..c1388f4ae8 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -609,6 +609,14 @@ function spillBound(
? null
: spillRoom(merge?.endColumn ?? col, 1, neighbours);
+ // Centred text has to stay centred on its own cell, so the room it is given is the same on
+ // both sides: growing one side further would carry the middle of the text away from the
+ // middle of the cell, which is where the editor keeps it.
+ if (align === HorizontalAlign.CENTER) {
+ const room = Math.min(before?.width ?? 0, after?.width ?? 0);
+ return { before: room, after: room };
+ }
+
return { before: before?.width ?? 0, after: after?.width ?? 0 };
}