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(core): let a screenshot keep its bytes outside the database

Luis Ortiz39b09edd3763
diff --git a/packages/core/src/capture/sink.ts b/packages/core/src/capture/sink.ts
index 160e2f7..a7f5528 100644
--- a/packages/core/src/capture/sink.ts
+++ b/packages/core/src/capture/sink.ts
@@ -2,7 +2,8 @@ import type { DOMContext } from '@/core/capture/dom/context';
import type { ElementMeta } from '@/core/guides/types';
export interface CaptureImage {
- png: Uint8Array;
+ screenshotId: string;
+ src: string;
width: number;
height: number;
}
diff --git a/packages/core/src/guides/__tests__/snapshots.test.ts b/packages/core/src/guides/__tests__/snapshots.test.ts
index 88dddb2..ad5d669 100644
--- a/packages/core/src/guides/__tests__/snapshots.test.ts
+++ b/packages/core/src/guides/__tests__/snapshots.test.ts
@@ -375,7 +375,7 @@ describe('revertToSnapshot', () => {
alt: 'original caption',
annotations: [{ id: 'a1', type: 'redact', x: 1, y: 2, w: 3, h: 4, style: 'blur' }],
});
- expect(await restored?.blob.text()).toBe('edited');
+ expect(await restored?.blob?.text()).toBe('edited');
expect((await db.guides.get('g1'))?.updatedAt).toBeGreaterThan(1000);
});
@@ -488,7 +488,7 @@ describe('append-only screenshots', () => {
await revertToSnapshot(snapshot!.id);
expect((await db.steps.get('s1'))?.screenshotId).toBe('sc1');
- expect(await (await db.screenshots.get('sc1'))?.blob.text()).toBe('img');
+ expect(await (await db.screenshots.get('sc1'))?.blob?.text()).toBe('img');
});
it('restores a deleted image', async () => {
diff --git a/packages/core/src/guides/db.ts b/packages/core/src/guides/db.ts
index 7247ad9..2bd65b4 100644
--- a/packages/core/src/guides/db.ts
+++ b/packages/core/src/guides/db.ts
@@ -1,10 +1,10 @@
import Dexie, { type EntityTable } from 'dexie';
-import type { Guide, Screenshot, Snapshot, Step } from './types';
+import type { Guide, Snapshot, Step, StoredScreenshot } from './types';
export class MimikDB extends Dexie {
guides!: EntityTable<Guide, 'id'>;
steps!: EntityTable<Step, 'id'>;
- screenshots!: EntityTable<Screenshot, 'id'>;
+ screenshots!: EntityTable<StoredScreenshot, 'id'>;
snapshots!: EntityTable<Snapshot, 'id'>;
constructor(name = 'mimik') {
diff --git a/packages/core/src/guides/service.ts b/packages/core/src/guides/service.ts
index 4ccb2a9..799024d 100644
--- a/packages/core/src/guides/service.ts
+++ b/packages/core/src/guides/service.ts
@@ -4,7 +4,16 @@ import type { ScreenshotEdits } from '@/core/screenshot/types';
import { db } from './db';
import { getMostCommonDomain } from './domain';
import { hashPayload } from './snapshot-hash';
-import type { BlockType, CalloutVariant, DescriptionSource, Guide, Screenshot, Snapshot, Step } from './types';
+import type {
+ BlockType,
+ CalloutVariant,
+ DescriptionSource,
+ Guide,
+ Screenshot,
+ Snapshot,
+ Step,
+ StoredScreenshot,
+} from './types';
export type GuideChangeEvent = { type: 'starred'; id: string; starred: boolean } | { type: 'mutated' };
@@ -20,6 +29,20 @@ function notifyGuidesChanged(event: GuideChangeEvent) {
guidesChannel.postMessage(event);
}
+async function hydrate(row: StoredScreenshot | undefined): Promise<Screenshot | undefined> {
+ if (!row) return undefined;
+ if (row.blob) return row as Screenshot;
+ if (!row.src) return undefined;
+ const response = await fetch(row.src);
+ if (!response.ok) return undefined;
+ return { ...row, blob: await response.blob() };
+}
+
+async function hydrateAll(rows: StoredScreenshot[]): Promise<Screenshot[]> {
+ const out = await Promise.all(rows.map((row) => hydrate(row)));
+ return out.filter((row): row is Screenshot => row !== undefined);
+}
+
export async function createGuide(guideId: string, staging = false): Promise<Guide> {
const guide: Guide = {
id: guideId,
@@ -42,7 +65,7 @@ export async function getGuide(
if (!guide) return null;
const steps = await db.steps.where('guideId').equals(id).sortBy('index');
const screenshotIds = steps.map((s) => s.screenshotId).filter(Boolean) as string[];
- const screenshotRows = await db.screenshots.where('id').anyOf(screenshotIds).toArray();
+ const screenshotRows = await hydrateAll(await db.screenshots.where('id').anyOf(screenshotIds).toArray());
const screenshots = new Map(screenshotRows.map((s) => [s.stepId, s]));
return { guide, steps, screenshots };
}
@@ -283,7 +306,11 @@ export async function getGuideDomain(guideId: string): Promise<string> {
return getMostCommonDomain(steps);
}
-export async function saveScreenshot(screenshot: Screenshot): Promise<void> {
+export async function allScreenshotIds(): Promise<string[]> {
+ return (await db.screenshots.toCollection().primaryKeys()) as string[];
+}
+
+export async function saveScreenshot(screenshot: StoredScreenshot): Promise<void> {
await db.screenshots.add(screenshot);
}
@@ -318,7 +345,7 @@ export async function deleteScreenshot(stepId: string): Promise<void> {
}
export async function getScreenshotsForSteps(stepIds: string[]): Promise<Map<string, Screenshot>> {
- const rows = await db.screenshots.where('id').anyOf(stepIds).toArray();
+ const rows = await hydrateAll(await db.screenshots.where('id').anyOf(stepIds).toArray());
return new Map(rows.map((s) => [s.stepId, s]));
}
@@ -326,7 +353,7 @@ export async function getFirstScreenshot(guideId: string): Promise<Screenshot |
const steps = await db.steps.where('guideId').equals(guideId).sortBy('index');
for (const step of steps) {
if (step.screenshotId) {
- const screenshot = await db.screenshots.get(step.screenshotId);
+ const screenshot = await hydrate(await db.screenshots.get(step.screenshotId));
if (screenshot) return screenshot;
}
}
diff --git a/packages/core/src/guides/types.ts b/packages/core/src/guides/types.ts
index 498d756..de50cb1 100644
--- a/packages/core/src/guides/types.ts
+++ b/packages/core/src/guides/types.ts
@@ -55,6 +55,7 @@ export interface Screenshot {
id: string;
stepId: string;
blob: Blob;
+ src?: string;
mimeType: string;
width: number;
height: number;
@@ -64,6 +65,8 @@ export interface Screenshot {
edits?: ScreenshotEdits;
}
+export type StoredScreenshot = Omit<Screenshot, 'blob'> & { blob?: Blob };
+
export interface Settings {
aiApiKey: string;
aiApiKeys: AIApiKeys;