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
fix(spreadsheet): render share grid from the sheet origin
Elian Doran77010ab7fa1d
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 95a3921a01..f44289c06a 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -1390,17 +1390,17 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain("height:80px");});- it("offsets a floating image by the trimmed table origin", () => {- // Data only at row 2, col 1 -> the table is trimmed; origin is the px distance- // from A1 to that corner: 2 default rows (48px) down, 1 default col (88px) right.+ it("positions a floating image at its absolute sheet coordinates regardless of where data starts", () => {+ // Data only at row 2, col 1. Because the grid is rendered from the sheet origin (A1),+ // the floating image keeps the absolute transform coordinates Univer stored (no offset).const html = renderSpreadsheetToHtml(workbookWithFloatingDrawings([urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 200, top: 100, width: 50, height: 40 })],{ cellData: { "2": { "1": { v: "x" } } } }));- expect(html).toContain("left:112px"); // 200 - 88- expect(html).toContain("top:52px"); // 100 - 48+ expect(html).toContain("left:200px");+ expect(html).toContain("top:100px");});it("preserves floating image z-order", () => {@@ -1462,6 +1462,47 @@ describe("renderSpreadsheetToHtml", () => {// #endregion+ // Wraps a single value placed at an arbitrary (row, col) into a complete workbook payload.+ function cellAtWorkbook(row: number, col: number): string {+ return JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: {},+ sheets: {+ s1: {+ id: "s1",+ name: "Sheet1",+ hidden: 0,+ rowCount: 1000,+ columnCount: 20,+ defaultColumnWidth: 88,+ defaultRowHeight: 24,+ mergeData: [],+ cellData: { [row]: { [col]: { v: "x" } } },+ rowData: {},+ columnData: {}+ }+ }+ }+ });+ }++ it("renders leading empty rows so the grid starts at the sheet origin", () => {+ // Data only at row 2 -> rows 0 and 1 must still be emitted (empty) so the grid keeps the+ // editor's geometry and absolutely-positioned floating images line up.+ const html = renderSpreadsheetToHtml(cellAtWorkbook(2, 0));+ const rowCount = (html.match(/<tr/g) ?? []).length;+ expect(rowCount).toBe(3);+ });++ it("renders leading empty columns so the grid starts at the sheet origin", () => {+ // Data only at column 2 -> columns 0 and 1 must still be emitted.+ const html = renderSpreadsheetToHtml(cellAtWorkbook(0, 2));+ const colCount = (html.match(/<col /g) ?? []).length;+ expect(colCount).toBe(3);+ });+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 ae7790090c..d20cc21a02 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -67,29 +67,25 @@ export function renderSpreadsheetToHtml(jsonContent: string): string {// #region Images/**- * Wraps a rendered sheet table in a positioned container carrying the sheet's floating images,- * each placed absolutely. Univer stores a floating image's `transform.left`/`top` in px from the- * sheet origin (A1), but the table is trimmed to populated cells, so positions are shifted by the- * px distance from A1 to the trimmed top-left corner. The container's `min-height` is stretched to- * contain images that float below the table so they don't overlap following content. Returns the- * table unchanged when the sheet has no renderable floating images.+ * Wraps a rendered sheet table in a positioned container carrying the sheet's floating images, each+ * placed absolutely. Univer stores a floating image's `transform.left`/`top` in px from the sheet+ * origin (A1); the table is also rendered from A1 (leading empty rows/columns included), so those+ * coordinates apply directly. The container's `min-height` is stretched to contain images that+ * float below the table so they don't overlap following content. Returns the table unchanged when+ * the sheet has no renderable floating images.*/function wrapWithFloatingImages(workbook: IWorkbookData, sheet: IWorksheetData, tableHtml: string): string {const drawings = getFloatingDrawings(workbook, sheet.id);if (drawings.length === 0) return tableHtml;- const bounds = computeBounds(sheet.cellData, sheet.mergeData ?? []);- const originX = bounds ? sumColumnWidths(sheet, 0, bounds.minCol) : 0;- const originY = bounds ? sumRowHeights(sheet, 0, bounds.minRow) : 0;-const images: string[] = [];let maxBottom = 0;for (const drawing of drawings) {const src = sanitizeImageSource(drawing.source);if (!src || !drawing.transform) continue;- const left = toFinite(drawing.transform.left) - originX;- const top = toFinite(drawing.transform.top) - originY;+ const left = toFinite(drawing.transform.left);+ const top = toFinite(drawing.transform.top);const width = toFinite(drawing.transform.width);const height = toFinite(drawing.transform.height);maxBottom = Math.max(maxBottom, top + height);@@ -127,30 +123,6 @@ function renderCellImages(cell: ICellData): string {return images.join("");}-function sumRowHeights(sheet: IWorksheetData, fromRow: number, toRowExclusive: number): number {- const rowData = sheet.rowData ?? {};- const defaultHeight = sheet.defaultRowHeight ?? 24;- let sum = 0;- for (let row = fromRow; row < toRowExclusive; row++) {- const meta = rowData[row];- if (meta?.hd) continue;- sum += isFiniteNumber(meta?.h) ? meta.h : defaultHeight;- }- return sum;-}--function sumColumnWidths(sheet: IWorksheetData, fromCol: number, toColExclusive: number): number {- const columnData = sheet.columnData ?? {};- const defaultWidth = sheet.defaultColumnWidth ?? 88;- let sum = 0;- for (let col = fromCol; col < toColExclusive; col++) {- const meta = columnData[col];- if (meta?.hd) continue;- sum += isFiniteNumber(meta?.w) ? meta.w : defaultWidth;- }- return sum;-}-/*** Validates an image source for inclusion in shared/exported HTML. Accepts only the relative* attachment-image URL Trilium emits (`api/attachments/<id>/image/...`, served by both the app@@ -186,7 +158,12 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |return "<p>Empty sheet.</p>";}- const { minRow, maxRow, minCol, maxCol } = bounds;+ // Render from the sheet origin (A1), not the first populated cell: Univer positions floating+ // images in absolute px from A1, and emitting the leading empty rows/columns keeps the grid in+ // step with the editor so those images line up. Only trailing empty rows/columns are trimmed.+ const { maxRow, maxCol } = bounds;+ const minRow = 0;+ const minCol = 0;// Build a set of cells that are hidden by merges (non-origin cells).const mergeMap = buildMergeMap(mergeData, minRow, maxRow, minCol, maxCol);