π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
feat(guides): merge one guide's steps into another at an index
Luis Ortiz3af2d5ac204e
diff --git a/src/core/guides/__tests__/merge-guide-into.test.ts b/src/core/guides/__tests__/merge-guide-into.test.tsnew file mode 100644index 0000000..1f4eb1a--- /dev/null+++ b/src/core/guides/__tests__/merge-guide-into.test.ts@@ -0,0 +1,133 @@+import 'fake-indexeddb/auto';+import { afterEach, describe, expect, it, vi } from 'vitest';++vi.hoisted(() => {+ globalThis.BroadcastChannel = class BroadcastChannel {+ name: string;+ constructor(name: string) {+ this.name = name;+ }+ postMessage() {}+ addEventListener() {}+ removeEventListener() {}+ close() {}+ onmessage = null;+ onmessageerror = null;+ dispatchEvent() {+ return true;+ }+ } as unknown as typeof BroadcastChannel;+});++import { db } from '../db';+import { mergeGuideInto } from '../service';+import type { Guide, Step } from '../types';++function makeStep(id: string, index: number, guideId = 'g1'): Step {+ return {+ id,+ guideId,+ index,+ description: id,+ action: 'click',+ url: 'https://example.com',+ timestamp: 1,+ };+}++async function seedGuide(guideId: string, ids: string[]) {+ const guide: Guide = {+ id: guideId,+ title: guideId,+ createdAt: 1,+ updatedAt: 1,+ stepIds: ids,+ starred: false,+ deletedAt: null,+ };+ await db.guides.put(guide);+ await db.steps.bulkPut(ids.map((id, index) => makeStep(id, index, guideId)));+}++async function orderedIds(guideId = 'g1') {+ const steps = await db.steps.where('guideId').equals(guideId).sortBy('index');+ return steps.map((step) => step.id);+}++afterEach(async () => {+ await db.steps.clear();+ await db.guides.clear();+});++describe('mergeGuideInto', () => {+ it('inserts the recorded steps at the anchor in capture order', async () => {+ await seedGuide('g1', ['a', 'b', 'c']);+ await seedGuide('tmp', ['x', 'y', 'z']);++ const moved = await mergeGuideInto('tmp', 'g1', 1);++ expect(moved).toBe(3);+ expect(await orderedIds('g1')).toEqual(['a', 'x', 'y', 'z', 'b', 'c']);+ });++ it('reassigns moved steps to the target guide and renumbers contiguously', async () => {+ await seedGuide('g1', ['a', 'b']);+ await seedGuide('tmp', ['x']);++ await mergeGuideInto('tmp', 'g1', 1);++ const steps = await db.steps.where('guideId').equals('g1').sortBy('index');+ expect(steps.map((step) => step.index)).toEqual([0, 1, 2]);+ expect(steps.every((step) => step.guideId === 'g1')).toBe(true);+ });++ it('keeps target.stepIds in the merged order', async () => {+ await seedGuide('g1', ['a', 'b']);+ await seedGuide('tmp', ['x']);++ await mergeGuideInto('tmp', 'g1', 2);++ const guide = await db.guides.get('g1');+ expect(guide?.stepIds).toEqual(['a', 'b', 'x']);+ });++ it('deletes the staging guide once merged', async () => {+ await seedGuide('g1', ['a']);+ await seedGuide('tmp', ['x']);++ await mergeGuideInto('tmp', 'g1', 0);++ expect(await db.guides.get('tmp')).toBeUndefined();+ expect(await orderedIds('tmp')).toEqual([]);+ });++ it('carries each moved step screenshot across', async () => {+ await seedGuide('g1', ['a']);+ await seedGuide('tmp', ['x']);+ await db.steps.update('x', { screenshotId: 'shot-1' });++ await mergeGuideInto('tmp', 'g1', 1);++ expect((await db.steps.get('x'))?.screenshotId).toBe('shot-1');+ });++ it('drops an empty staging guide and leaves the target untouched', async () => {+ await seedGuide('g1', ['a', 'b']);+ await seedGuide('tmp', []);++ const moved = await mergeGuideInto('tmp', 'g1', 1);++ expect(moved).toBe(0);+ expect(await orderedIds('g1')).toEqual(['a', 'b']);+ expect(await db.guides.get('tmp')).toBeUndefined();+ });++ it('appends when the anchor is past the end of the target', async () => {+ await seedGuide('g1', ['a']);+ await seedGuide('tmp', ['x', 'y']);++ await mergeGuideInto('tmp', 'g1', 99);++ expect(await orderedIds('g1')).toEqual(['a', 'x', 'y']);+ });+});diff --git a/src/core/guides/service.ts b/src/core/guides/service.tsindex 5d4e070..4379a39 100644--- a/src/core/guides/service.ts+++ b/src/core/guides/service.ts@@ -18,7 +18,7 @@ function notifyGuidesChanged(event: GuideChangeEvent) {guidesChannel.postMessage(event);}-export async function createGuide(guideId: string): Promise<Guide> {+export async function createGuide(guideId: string, staging = false): Promise<Guide> {const guide: Guide = {id: guideId,title: i18n.t('fullview.untitledGuide'),@@ -27,6 +27,7 @@ export async function createGuide(guideId: string): Promise<Guide> {stepIds: [],starred: false,deletedAt: null,+ ...(staging ? { staging: true } : {}),};await db.guides.add(guide);return guide;@@ -48,7 +49,7 @@ export async function getGuides(): Promise<Guide[]> {return db.guides.orderBy('updatedAt').reverse()- .filter((g) => g.deletedAt == null)+ .filter((g) => g.deletedAt == null && !g.staging).toArray();}@@ -56,7 +57,7 @@ export async function getStarredGuides(): Promise<Guide[]> {return db.guides.orderBy('updatedAt').reverse()- .filter((g) => g.starred === true && g.deletedAt == null)+ .filter((g) => g.starred === true && g.deletedAt == null && !g.staging).toArray();}@@ -139,6 +140,29 @@ export async function createStep(step: Step): Promise<void> {await db.steps.add(step);}+export async function mergeGuideInto(sourceGuideId: string, targetGuideId: string, atIndex: number): Promise<number> {+ const moved = await db.transaction('rw', db.steps, db.guides, async () => {+ const incoming = await db.steps.where('guideId').equals(sourceGuideId).sortBy('index');+ const target = await db.steps.where('guideId').equals(targetGuideId).sortBy('index');+ if (incoming.length > 0) {+ target.splice(+ Math.max(0, Math.min(atIndex, target.length)),+ 0,+ ...incoming.map((step) => ({ ...step, guideId: targetGuideId })),+ );+ await db.steps.bulkPut(target.map((step, index) => ({ ...step, index })));+ await db.guides.update(targetGuideId, {+ stepIds: target.map((step) => step.id),+ updatedAt: Date.now(),+ });+ }+ await db.guides.delete(sourceGuideId);+ return incoming.length;+ });+ if (moved > 0) notifyGuidesChanged({ type: 'mutated' });+ return moved;+}+export async function insertBlock(guideId: string,atIndex: number,diff --git a/src/core/guides/types.ts b/src/core/guides/types.tsindex 9b18a45..c0f5a3e 100644--- a/src/core/guides/types.ts+++ b/src/core/guides/types.ts@@ -9,6 +9,7 @@ export interface Guide {stepIds: string[];starred: boolean;deletedAt: number | null;+ staging?: boolean;}export type BlockType = 'heading' | 'callout';