Skip to content

Build your personal knowledge base with Trilium Notes

feat(spreadsheet): align share images with fixed layout and cell wrapping

Elian Doran750222126eb0
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 f52809e42b..95a3921a01 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -625,6 +625,47 @@ describe("renderSpreadsheetToHtml", () => {
expect(html).toContain("<td>x</td>");
});
+ it("wraps a cell whose style enables the wrap strategy", () => {
+ // Univer WrapStrategy.WRAP === 3. Cells default to nowrap (overflow) via the stylesheet;
+ // a wrapping cell must opt back into normal wrapping inline.
+ const html = renderSpreadsheetToHtml(
+ singleCellWorkbook({ v: "This is a cell with line-wrapping", t: 1, s: { tb: 3 } })
+ );
+ expect(html).toContain("white-space:normal");
+ expect(html).toContain("overflow-wrap:break-word");
+ });
+
+ it("wraps a cell via a referenced style that enables wrapping", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ styles: { wrapStyle: { tb: 3 } },
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ rowCount: 10,
+ columnCount: 5,
+ mergeData: [],
+ cellData: { "0": { "0": { v: "wrapped", s: "wrapStyle" } } },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+ const html = renderSpreadsheetToHtml(input);
+ expect(html).toContain("white-space:normal");
+ });
+
+ it("does not emit wrap styling for a non-wrapping (overflow) cell", () => {
+ // WrapStrategy.OVERFLOW === 1 -> the cell keeps the default nowrap/overflow behaviour.
+ const html = renderSpreadsheetToHtml(singleCellWorkbook({ v: "plain", s: { tb: 1 } }));
+ expect(html).not.toContain("white-space:normal");
+ });
+
it("renders all vertical alignment values", () => {
const top = renderSpreadsheetToHtml(singleCellWorkbook({ v: "t", s: { vt: 1 } }));
const middle = renderSpreadsheetToHtml(singleCellWorkbook({ v: "m", s: { vt: 2 } }));
@@ -1095,7 +1136,34 @@ describe("renderSpreadsheetToHtml", () => {
const html = renderSpreadsheetToHtml(
singleCellWorkbook({ v: "x" }, { showGridlines: 1 })
);
- expect(html).toContain('<table class="spreadsheet-table show-gridlines">');
+ expect(html).toContain('<table class="spreadsheet-table show-gridlines" style="width:88px">');
+ });
+
+ it("emits an explicit fixed table width summing the visible column widths", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ styles: {},
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ rowCount: 10,
+ columnCount: 5,
+ defaultColumnWidth: 88,
+ mergeData: [],
+ cellData: { "0": { "0": { v: "a" }, "1": { v: "b" } } },
+ rowData: {},
+ columnData: { "0": { w: 120 } }
+ }
+ }
+ }
+ });
+ const html = renderSpreadsheetToHtml(input);
+ // Column 0 is 120, column 1 falls back to the default 88 -> 208.
+ expect(html).toContain('style="width:208px"');
});
it("marks a filled cell with has-fill so gridlines can be suppressed under the fill", () => {
@@ -1147,7 +1215,7 @@ describe("renderSpreadsheetToHtml", () => {
const html = renderSpreadsheetToHtml(
singleCellWorkbook({ v: "x" }, { showGridlines: 0 })
);
- expect(html).toContain('<table class="spreadsheet-table">');
+ expect(html).toContain('<table class="spreadsheet-table" style="width:88px">');
expect(html).not.toContain("show-gridlines");
});
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 050edc73d8..ae7790090c 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -27,7 +27,8 @@ import {
type IWorksheetData,
parseWorkbookData,
resolveCellStyle,
- VerticalAlign
+ VerticalAlign,
+ WrapStrategy
} from "./workbook_model.js";
/**
@@ -190,17 +191,22 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
// Build a set of cells that are hidden by merges (non-origin cells).
const mergeMap = buildMergeMap(mergeData, minRow, maxRow, minCol, maxCol);
- const lines: string[] = [];
- lines.push(buildTableTag(sheet));
-
- // Colgroup for column widths.
+ // Visible column widths, reused for the colgroup and the table's fixed total width.
const defaultWidth = sheet.defaultColumnWidth ?? 88;
- lines.push("<colgroup>");
+ const colWidths: number[] = [];
for (let col = minCol; col <= maxCol; col++) {
const colMeta = columnData[col];
if (colMeta?.hd) continue;
- const width = isFiniteNumber(colMeta?.w) ? colMeta.w : defaultWidth;
- lines.push(`<col style="width:${width}px">`);
+ colWidths.push(isFiniteNumber(colMeta?.w) ? colMeta.w : defaultWidth);
+ }
+ const totalWidth = colWidths.reduce((sum, w) => sum + w, 0);
+
+ const lines: string[] = [];
+ lines.push(buildTableTag(sheet, totalWidth));
+
+ lines.push("<colgroup>");
+ for (const width of colWidths) {
+ lines.push(`<col style="width:${px(width)}px">`);
}
lines.push("</colgroup>");
@@ -251,18 +257,21 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
* 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 {
+function buildTableTag(sheet: IWorksheetData, totalWidth: number): string {
// Default to shown (matching the editor) unless explicitly disabled.
const showGridlines = sheet.showGridlines !== 0;
- if (!showGridlines) {
- return '<table class="spreadsheet-table">';
- }
+ const className = showGridlines ? "spreadsheet-table show-gridlines" : "spreadsheet-table";
- let style = "";
- if (sheet.gridlinesColor) {
- style = ` style="--spreadsheet-gridline-color:${sanitizeCssColor(sheet.gridlinesColor)}"`;
+ const styles: string[] = [];
+ if (showGridlines && sheet.gridlinesColor) {
+ styles.push(`--spreadsheet-gridline-color:${sanitizeCssColor(sheet.gridlinesColor)}`);
}
- return `<table class="spreadsheet-table show-gridlines"${style}>`;
+ // An explicit width is required for `table-layout: fixed` (the stylesheet) to honour the
+ // column widths, so cell text overflows into empty neighbours like a spreadsheet instead of
+ // wrapping and growing rows — which would shift the absolutely-positioned floating images.
+ styles.push(`width:${px(totalWidth)}px`);
+
+ return `<table class="${className}" style="${styles.join(";")}">`;
}
// #region Merge handling
@@ -343,6 +352,13 @@ function buildCssText(style: IStyleData | null, cell?: ICellData): string {
if (valign) parts.push(`vertical-align:${valign}`);
}
+ // Cells default to nowrap (overflow into empty neighbours, like the editor); a cell with the
+ // WRAP strategy opts back into normal wrapping so its text breaks within the column width.
+ if (style.tb === WrapStrategy.WRAP) {
+ parts.push("white-space:normal");
+ parts.push("overflow-wrap:break-word");
+ }
+
if (style.bd) {
appendBorderCss(parts, "border-top", style.bd.t);
appendBorderCss(parts, "border-right", style.bd.r);
diff --git a/packages/share-theme/src/styles/content.css b/packages/share-theme/src/styles/content.css
index afce14555a..ade53a651b 100644
--- a/packages/share-theme/src/styles/content.css
+++ b/packages/share-theme/src/styles/content.css
@@ -108,10 +108,19 @@ body.type-webView {
border-collapse: collapse;
width: auto;
margin-top: 1em;
+ /* Honour the per-column widths emitted in the colgroup (and the explicit table width) so the
+ grid keeps the editor's geometry. Without this, a cell wider than its column would stretch
+ the column and break the alignment of the absolutely-positioned floating images. */
+ table-layout: fixed;
}
.spreadsheet-table td {
padding: 2px 4px;
+ /* Match the spreadsheet: long text overflows into empty neighbouring cells rather than
+ wrapping, which keeps every row at its declared height. Wrapping would grow rows and push
+ the floating images (positioned in absolute px) out of alignment with the grid. */
+ white-space: nowrap;
+ overflow: visible;
}
/* When the source sheet has gridlines enabled, draw a light 1px line on every cell edge.