Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet/print & PDF export: add support for cropped images

Adorian Doran1765ce6ea371
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 e2df8d5c09..cb62be1e5a 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -1908,6 +1908,30 @@ describe("renderSpreadsheetToHtml", () => {
// #region Floating images (SHEET_DRAWING_PLUGIN resource)
+ it("shows a cropped drawing through its box, with the whole image held inside", () => {
+ const cropped = {
+ ...urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 10, top: 20, width: 100, height: 50 }),
+ srcRect: { left: 4, top: 8, right: 6, bottom: 12 }
+ };
+ const html = renderSpreadsheetToHtml(workbookWithFloatingDrawings([cropped]));
+
+ // The box stays the drawing's own size and clips; the image inside grows by the insets and
+ // is pulled back by them, so the visible window is the part the editor shows.
+ expect(html).toContain(`<span class="spreadsheet-floating-image" style="position:absolute;left:10px;top:20px;`
+ + `width:100px;height:50px;display:block;overflow:hidden">`);
+ expect(html).toContain(`<img style="position:absolute;left:-4px;top:-8px;width:110px;height:70px"`);
+ });
+
+ it("leaves a drawing whose crop takes nothing as a bare image", () => {
+ const html = renderSpreadsheetToHtml(workbookWithFloatingDrawings([{
+ ...urlDrawing("img1", "api/attachments/cgN4jEBCA1Kn/image/image.png", { left: 0, top: 0, width: 100, height: 50 }),
+ srcRect: { left: 0, top: 0, right: 0, bottom: 0 }
+ }]));
+
+ expect(html).toContain('<img class="spreadsheet-floating-image"');
+ expect(html).not.toContain(`<span class="spreadsheet-floating-image"`);
+ });
+
it("renders a floating image absolutely positioned in a per-sheet wrapper", () => {
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 58701f549b..051fe9ad84 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -32,6 +32,7 @@ import {
isFiniteNumber,
type IRange,
type IRowData,
+ type ISourceRect,
type ISheetDrawing,
type IStyleData,
type ITextRotation,
@@ -87,6 +88,8 @@ interface PlacedImage {
height: number;
/** CSS `transform` value for rotation/flip, or "" when the image is upright and unflipped. */
transform: string;
+ /** How far the image runs past each edge of the box, when only part of it is shown. */
+ crop: Required<ISourceRect> | null;
}
/**
@@ -110,12 +113,22 @@ function placeFloatingImages(sheet: IWorksheetData, drawings: ISheetDrawing[]):
top: toFinite(drawing.transform.top) - headerHeight,
width: toFinite(drawing.transform.width),
height: toFinite(drawing.transform.height),
- transform: cssTransform(drawing.transform)
+ transform: cssTransform(drawing.transform),
+ crop: cropInsets(drawing.srcRect)
});
}
return placed;
}
+/** A drawing's crop insets, or `null` when the whole image is shown. */
+function cropInsets(srcRect: ISourceRect | null | undefined): Required<ISourceRect> | null {
+ if (!srcRect) return null;
+
+ const side = (value: number | undefined) => (isFiniteNumber(value) ? value : 0);
+ const insets = { left: side(srcRect.left), top: side(srcRect.top), right: side(srcRect.right), bottom: side(srcRect.bottom) };
+ return insets.left || insets.top || insets.right || insets.bottom ? insets : null;
+}
+
/**
* 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
@@ -146,8 +159,23 @@ function wrapWithFloatingImages(tableHtml: string, images: PlacedImage[]): strin
for (const image of images) {
maxBottom = Math.max(maxBottom, image.top + image.height);
const transform = image.transform ? `;transform:${image.transform}` : "";
+ const box = `position:absolute;left:${px(image.left)}px;top:${px(image.top)}px`
+ + `;width:${px(image.width)}px;height:${px(image.height)}px${transform}`;
+ const src = escapeHtml(image.src);
+
+ if (!image.crop) {
+ tags.push(`<img class="spreadsheet-floating-image" style="${box}" src="${src}" alt="">`);
+ continue;
+ }
+
+ // A cropped drawing keeps the box as its window and holds the whole image inside it,
+ // enlarged by the insets and pulled back by them, which is how the editor draws it.
+ const { left, top, right, bottom } = image.crop;
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${transform}" src="${escapeHtml(image.src)}" alt="">`
+ `<span class="spreadsheet-floating-image" style="${box};display:block;overflow:hidden">`
+ + `<img style="position:absolute;left:${px(-left)}px;top:${px(-top)}px`
+ + `;width:${px(image.width + left + right)}px;height:${px(image.height + top + bottom)}px" src="${src}" alt="">`
+ + `</span>`
);
}
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.ts
index e0db535c42..7e09301c5b 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts
@@ -107,6 +107,19 @@ export interface ISheetDrawing {
transform?: IDrawingTransform | null;
/** Cell-anchored extent (top-left/bottom-right), used by the XLSX emitter's two-cell anchor. */
sheetTransform?: ISheetDrawingAnchor | null;
+ /** How much of the image is cropped away, in px of the drawing's own box on each side. */
+ srcRect?: ISourceRect | null;
+}
+
+/**
+ * A drawing's crop. Univer draws the whole image enlarged by these insets and offset by them, with
+ * the drawing's box as the window onto it, so each value is how far past that edge the image runs.
+ */
+export interface ISourceRect {
+ left?: number;
+ top?: number;
+ right?: number;
+ bottom?: number;
}
export interface ISheetDrawingAnchor {