Skip to content

Build your personal knowledge base with Trilium Notes

feat(spreadsheet): rotate/flip floating images in share

Elian Doran81f3ff015c19
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 f4877f8bf6..ed1a94801f 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -1472,6 +1472,43 @@ describe("renderSpreadsheetToHtml", () => {
expect(html).toContain("top:208.8px");
});
+ it("rotates a floating image by its transform angle", () => {
+ const html = renderSpreadsheetToHtml(
+ workbookWithFloatingDrawings([
+ { drawingId: "img1", imageSourceType: "URL", source: "api/attachments/cgN4jEBCA1Kn/image/image.png", transform: { left: 0, top: 0, width: 50, height: 50, angle: 45 } }
+ ])
+ );
+ expect(html).toContain("transform:rotate(45deg)");
+ });
+
+ it("flips a floating image horizontally and vertically", () => {
+ const html = renderSpreadsheetToHtml(
+ workbookWithFloatingDrawings([
+ { drawingId: "img1", source: "api/attachments/cgN4jEBCA1Kn/image/image.png", transform: { left: 0, top: 0, width: 50, height: 50, flipX: true, flipY: true } }
+ ])
+ );
+ expect(html).toContain("scaleX(-1)");
+ expect(html).toContain("scaleY(-1)");
+ });
+
+ it("combines rotation and flip (flip first, then rotate)", () => {
+ const html = renderSpreadsheetToHtml(
+ workbookWithFloatingDrawings([
+ { drawingId: "img1", source: "api/attachments/cgN4jEBCA1Kn/image/image.png", transform: { left: 0, top: 0, width: 50, height: 50, angle: 90, flipX: true } }
+ ])
+ );
+ expect(html).toContain("transform:rotate(90deg) scaleX(-1)");
+ });
+
+ it("does not emit a transform for an unrotated, unflipped image", () => {
+ const html = renderSpreadsheetToHtml(
+ workbookWithFloatingDrawings([
+ urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 0, top: 0, width: 50, height: 50 })
+ ])
+ );
+ expect(html).not.toContain("transform:");
+ });
+
it("renders a base64 floating image", () => {
const html = renderSpreadsheetToHtml(
workbookWithFloatingDrawings([
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 6ebfb61997..9534f38907 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -74,6 +74,8 @@ interface PlacedImage {
top: number;
width: number;
height: number;
+ /** CSS `transform` value for rotation/flip, or "" when the image is upright and unflipped. */
+ transform: string;
}
/**
@@ -96,12 +98,28 @@ function placeFloatingImages(sheet: IWorksheetData, drawings: ISheetDrawing[]):
left: toFinite(drawing.transform.left) - headerWidth,
top: toFinite(drawing.transform.top) - headerHeight,
width: toFinite(drawing.transform.width),
- height: toFinite(drawing.transform.height)
+ height: toFinite(drawing.transform.height),
+ transform: cssTransform(drawing.transform)
});
}
return placed;
}
+/**
+ * Builds the CSS `transform` for a drawing's rotation/flip, around the default centre origin (which
+ * matches Univer). Flips are applied before the rotation (so they read in the image's own axes), and
+ * an upright, unflipped image yields "" so no transform is emitted.
+ */
+function cssTransform(transform: NonNullable<ISheetDrawing["transform"]>): string {
+ const parts: string[] = [];
+ if (isFiniteNumber(transform.angle) && transform.angle % 360 !== 0) {
+ parts.push(`rotate(${px(transform.angle)}deg)`);
+ }
+ if (transform.flipX) parts.push("scaleX(-1)");
+ if (transform.flipY) parts.push("scaleY(-1)");
+ return parts.join(" ");
+}
+
/**
* Wraps a rendered sheet table in a positioned container carrying the sheet's floating images, each
* placed absolutely at its content-space coordinates (the table is rendered from A1 with no header
@@ -116,8 +134,9 @@ function wrapWithFloatingImages(tableHtml: string, images: PlacedImage[]): strin
const tags: string[] = [];
for (const image of images) {
maxBottom = Math.max(maxBottom, image.top + image.height);
+ const transform = image.transform ? `;transform:${image.transform}` : "";
tags.push(
- `<img class="spreadsheet-floating-image" style="position:absolute;left:${px(image.left)}px;top:${px(image.top)}px;width:${px(image.width)}px;height:${px(image.height)}px" src="${escapeHtml(image.src)}" alt="">`
+ `<img class="spreadsheet-floating-image" style="position:absolute;left:${px(image.left)}px;top:${px(image.top)}px;width:${px(image.width)}px;height:${px(image.height)}px${transform}" src="${escapeHtml(image.src)}" alt="">`
);
}
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.ts
index d9abf15648..b50e919e98 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts
@@ -102,6 +102,10 @@ export interface IDrawingTransform {
top?: number;
width?: number;
height?: number;
+ /** Clockwise rotation in degrees, applied around the box centre. */
+ angle?: number;
+ flipX?: boolean;
+ flipY?: boolean;
}
export interface IStyleData {