Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

qfe: Fit image size

Luiz Henriqueacd138d6df55
diff --git a/src/core/export/__tests__/docx-export.test.ts b/src/core/export/__tests__/docx-export.test.ts
index 1c94cf7..c43002c 100644
--- a/src/core/export/__tests__/docx-export.test.ts
+++ b/src/core/export/__tests__/docx-export.test.ts
@@ -1,7 +1,7 @@
// @vitest-environment jsdom
import { strFromU8, unzipSync } from 'fflate';
import { describe, expect, it } from 'vitest';
-import { exportGuideAsDOCX } from '@/core/export/docx-export';
+import { exportGuideAsDOCX, fitDocxImageSize } from '@/core/export/docx-export';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
function makeGuide(overrides: Partial<Guide> = {}): Guide {
@@ -96,3 +96,27 @@ describe('exportGuideAsDOCX', () => {
expect(Object.keys(files).some((name) => name.startsWith('word/media/'))).toBe(false);
});
});
+
+describe('fitDocxImageSize', () => {
+ it('preserves aspect ratio and never upscales', () => {
+ expect(fitDocxImageSize(400, 300)).toEqual({ width: 400, height: 300 });
+ });
+
+ it('clamps wide screenshots to max width', () => {
+ const { width, height } = fitDocxImageSize(1040, 600);
+ expect(width).toBe(520);
+ expect(height).toBe(300);
+ });
+
+ it('clamps tall screenshots so they fit one page', () => {
+ const { width, height } = fitDocxImageSize(400, 2000);
+ expect(height).toBe(640);
+ expect(width).toBe(128);
+ });
+
+ it('accounts for left indent when clamping width', () => {
+ const { width, height } = fitDocxImageSize(800, 600, 900);
+ expect(width).toBe(475);
+ expect(height).toBe(356);
+ });
+});
diff --git a/src/core/export/docx-export.ts b/src/core/export/docx-export.ts
index cb94be5..85a8dce 100644
--- a/src/core/export/docx-export.ts
+++ b/src/core/export/docx-export.ts
@@ -19,9 +19,24 @@ import { logger } from '@/lib/logger';
type SupportedDocxImageType = 'bmp' | 'gif' | 'jpg' | 'png';
const DOCX_MAX_IMAGE_WIDTH = 520;
+const DOCX_MAX_IMAGE_HEIGHT = 640; // px @ 96dpi, fits one page after margins
const DOCX_STEP_INDENT = 900;
const DOCX_FONT_FAMILY = 'Helvetica';
+/** Scale screenshot to fit page bounds without upscaling or distorting. */
+export function fitDocxImageSize(
+ screenshotWidth: number,
+ screenshotHeight: number,
+ leftIndent = 0,
+): { width: number; height: number } {
+ const maxWidth = DOCX_MAX_IMAGE_WIDTH - Math.round(leftIndent / 20);
+ const scale = Math.min(maxWidth / screenshotWidth, DOCX_MAX_IMAGE_HEIGHT / screenshotHeight, 1);
+ return {
+ width: Math.max(1, Math.round(screenshotWidth * scale)),
+ height: Math.max(1, Math.round(screenshotHeight * scale)),
+ };
+}
+
function buildGradientDividerParagraph(): Paragraph {
const gradientSegments = ['4F46E5', '635BED', '8178F4', 'A4A1F9', 'C7D2FE', '9BD2FE', '60C8FB', '38BDF8'];
@@ -145,8 +160,7 @@ async function buildImageParagraph(
try {
const arrayBuffer = await blobToArrayBuffer(screenshot.blob);
- const width = Math.min(screenshot.width, DOCX_MAX_IMAGE_WIDTH - Math.round(leftIndent / 20));
- const height = Math.max(1, Math.round((screenshot.height / screenshot.width) * width));
+ const { width, height } = fitDocxImageSize(screenshot.width, screenshot.height, leftIndent);
return new Paragraph({
alignment: AlignmentType.LEFT,