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 πŸ“–

feat(ui): add a dominant screenshot ratio helper

Luis Ortiz64c70b9063dc
diff --git a/src/ui/shared/ImagePlaceholder.tsx b/src/ui/shared/ImagePlaceholder.tsx
index 921c3e1..698de46 100644
--- a/src/ui/shared/ImagePlaceholder.tsx
+++ b/src/ui/shared/ImagePlaceholder.tsx
@@ -29,3 +29,25 @@ export function siblingRatio(screenshots: Map<string, Screenshot>): number | und
}
return undefined;
}
+
+const RATIO_BUCKET_PRECISION = 100;
+
+export function dominantRatio(screenshots: Map<string, Screenshot>): number | undefined {
+ const buckets = new Map<number, { ratio: number; count: number }>();
+
+ for (const screenshot of screenshots.values()) {
+ const viewport = resolveViewport(screenshot);
+ if (!viewport.width || !viewport.height) continue;
+ const ratio = viewport.width / viewport.height;
+ const key = Math.round(ratio * RATIO_BUCKET_PRECISION) / RATIO_BUCKET_PRECISION;
+ const bucket = buckets.get(key);
+ if (bucket) bucket.count += 1;
+ else buckets.set(key, { ratio, count: 1 });
+ }
+
+ let winner: { ratio: number; count: number } | undefined;
+ for (const bucket of buckets.values()) {
+ if (!winner || bucket.count > winner.count) winner = bucket;
+ }
+ return winner?.ratio;
+}
diff --git a/src/ui/shared/__tests__/image-placeholder.test.ts b/src/ui/shared/__tests__/image-placeholder.test.ts
new file mode 100644
index 0000000..8e0e4ee
--- /dev/null
+++ b/src/ui/shared/__tests__/image-placeholder.test.ts
@@ -0,0 +1,97 @@
+import { describe, expect, it } from 'vitest';
+import type { Screenshot } from '@/core/guides/types';
+import { dominantRatio } from '@/ui/shared/ImagePlaceholder';
+
+function makeScreenshot(id: string, overrides: Partial<Screenshot> = {}): Screenshot {
+ return {
+ id,
+ stepId: `step-${id}`,
+ blob: new Blob(['x']),
+ mimeType: 'image/png',
+ width: 1600,
+ height: 1000,
+ ...overrides,
+ };
+}
+
+function toMap(screenshots: Screenshot[]): Map<string, Screenshot> {
+ return new Map(screenshots.map((s) => [s.stepId, s]));
+}
+
+describe('dominantRatio', () => {
+ it('returns undefined for an empty map', () => {
+ expect(dominantRatio(new Map())).toBeUndefined();
+ });
+
+ it('returns the shared ratio when every screenshot matches', () => {
+ const map = toMap([makeScreenshot('a'), makeScreenshot('b'), makeScreenshot('c')]);
+ expect(dominantRatio(map)).toBeCloseTo(1.6, 5);
+ });
+
+ it('ignores an outlier that loses to the majority', () => {
+ const map = toMap([
+ makeScreenshot('portrait', { width: 800, height: 1400 }),
+ makeScreenshot('b'),
+ makeScreenshot('c'),
+ ]);
+ expect(dominantRatio(map)).toBeCloseTo(1.6, 5);
+ });
+
+ it('breaks a tie in favour of the earliest bucket', () => {
+ const map = toMap([
+ makeScreenshot('a'),
+ makeScreenshot('portrait-1', { width: 800, height: 1400 }),
+ makeScreenshot('b'),
+ makeScreenshot('portrait-2', { width: 800, height: 1400 }),
+ ]);
+ expect(dominantRatio(map)).toBeCloseTo(1.6, 5);
+ });
+
+ it('breaks a tie in favour of the earliest bucket when the outlier comes first', () => {
+ const map = toMap([
+ makeScreenshot('portrait-1', { width: 800, height: 1400 }),
+ makeScreenshot('a'),
+ makeScreenshot('portrait-2', { width: 800, height: 1400 }),
+ makeScreenshot('b'),
+ ]);
+ expect(dominantRatio(map)).toBeCloseTo(800 / 1400, 5);
+ });
+
+ it('returns an observed ratio rather than the rounded bucket key', () => {
+ const map = toMap([makeScreenshot('a', { width: 1366, height: 768 })]);
+ expect(dominantRatio(map)).toBe(1366 / 768);
+ });
+
+ it('buckets near-identical ratios together', () => {
+ const map = toMap([
+ makeScreenshot('a', { width: 1600, height: 1000 }),
+ makeScreenshot('b', { width: 1601, height: 1000 }),
+ makeScreenshot('portrait', { width: 800, height: 1400 }),
+ ]);
+ expect(dominantRatio(map)).toBeCloseTo(1.6, 5);
+ });
+
+ it('skips screenshots with zero or missing dimensions', () => {
+ const map = toMap([
+ makeScreenshot('zero-width', { width: 0, height: 1000 }),
+ makeScreenshot('zero-height', { width: 1600, height: 0 }),
+ makeScreenshot('missing', { width: undefined as unknown as number, height: undefined as unknown as number }),
+ makeScreenshot('portrait', { width: 800, height: 1400 }),
+ ]);
+ expect(dominantRatio(map)).toBeCloseTo(800 / 1400, 5);
+ });
+
+ it('returns undefined when no screenshot has usable dimensions', () => {
+ const map = toMap([makeScreenshot('a', { width: 0, height: 0 }), makeScreenshot('b', { width: 1600, height: 0 })]);
+ expect(dominantRatio(map)).toBeUndefined();
+ });
+
+ it('counts a cropped screenshot by its viewport rather than its original size', () => {
+ const map = toMap([
+ makeScreenshot('cropped-1', { edits: { viewport: { x: 0, y: 0, width: 500, height: 500 } } }),
+ makeScreenshot('cropped-2', { edits: { viewport: { x: 10, y: 10, width: 300, height: 300 } } }),
+ makeScreenshot('uncropped'),
+ ]);
+ expect(dominantRatio(map)).toBe(1);
+ });
+});