diff --git a/README.md b/README.md index 065a6df..fbc9c44 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The agent writes `.stackmap//diagram.json`, validates and repairs it, and - **Find:** search titles, ids and types; the **lens** dims the node types you don't need. - **Guided views:** tabs the agent defines, like "Checkout path", that focus one part of the diagram. **Present** steps through them full screen. - **Flow:** **Play** sends pulses along whatever you're looking at: the whole diagram, a view, a trace or a route, hop by hop. -- **Share:** the URL keeps the view, selection, route and playback. Export PNG, JPEG, WebP, SVG, or a short video of the flow. +- **Share:** the URL keeps the view, selection, route and playback. Export PNG, JPEG, WebP, SVG, a short video of the flow, or the diagram again as one offline HTML file. - **Keyboard:** `/` search, `R` route, `F` present, `P` play, `M` minimap, `Esc` clear; Tab and the arrow keys reach everything else. - **Themes:** dark and light, and animations respect reduced motion. diff --git a/packages/cli/e2e/offline.spec.ts b/packages/cli/e2e/offline.spec.ts index 177005d..2dc30f5 100644 --- a/packages/cli/e2e/offline.spec.ts +++ b/packages/cli/e2e/offline.spec.ts @@ -1,5 +1,5 @@ import { execFileSync } from 'node:child_process'; -import { mkdtempSync, writeFileSync } from 'node:fs'; +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; @@ -28,6 +28,28 @@ test('a delivered file renders from file:// with no network', async ({ page }) = expect(requests).toEqual([]); }); +test('Export saves the delivered page as HTML that opens offline, and the mark links to the website', async ({ page }) => { + const dir = mkdtempSync(join(tmpdir(), 'stackmap-e2e-')); + const input = join(dir, 'platform.json'); + writeFileSync(input, JSON.stringify(groupedPlatform)); + execFileSync('node', [fileURLToPath(new URL('../dist/cli.js', import.meta.url)), 'deliver', input, '-o', join(dir, 'delivered.html')]); + await page.goto(pathToFileURL(join(dir, 'delivered.html')).href); + await expect(page.locator('.sm-card')).toHaveCount(groupedPlatform.nodes.length); + await expect(page.getByRole('link', { name: /stackmap website/ })).toHaveAttribute('href', 'https://stackmap.omsimos.com/'); + + await page.getByRole('button', { name: 'Export' }).click(); + const [dl] = await Promise.all([page.waitForEvent('download'), page.getByRole('menuitem', { name: /^HTML/ }).click()]); + expect(dl.suggestedFilename()).toBe('platform.html'); + const saved = join(dir, 'saved.html'); + await dl.saveAs(saved); + // The page as delivered, not as rendered: the viewer's own markup is left out and rebuilt on open. + expect(readFileSync(saved, 'utf8')).toMatch(/
<\/div>/); + + await page.goto(pathToFileURL(saved).href); + await expect(page.locator('.sm-card')).toHaveCount(groupedPlatform.nodes.length); + await expect(page).toHaveTitle('Platform · stackmap'); +}); + test('the bare template says no diagram is embedded', async ({ page }) => { await page.goto(new URL('../dist/viewer.html', import.meta.url).href); await expect(page.getByRole('status')).toHaveText(/No diagram embedded/); diff --git a/packages/cli/e2e/serve.spec.ts b/packages/cli/e2e/serve.spec.ts index 8612f60..8cdad62 100644 --- a/packages/cli/e2e/serve.spec.ts +++ b/packages/cli/e2e/serve.spec.ts @@ -1,5 +1,5 @@ import { spawn, type ChildProcess } from 'node:child_process'; -import { mkdtempSync, writeFileSync } from 'node:fs'; +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -51,6 +51,18 @@ test('edits reload the page; a broken edit keeps the last good version and says await expect(toast).toHaveCount(0); }); +test('an HTML export of a served page is the offline file, without the live connection', async ({ page }) => { + await start(commerceApi); + await page.goto(url); + await expect(page.locator('.sm-card')).toHaveCount(commerceApi.nodes.length); + await page.getByRole('button', { name: 'Export' }).click(); + const [dl] = await Promise.all([page.waitForEvent('download'), page.getByRole('menuitem', { name: /^HTML/ }).click()]); + const html = readFileSync((await dl.path())!, 'utf8'); + expect(html).toContain('' + + ''; + const page = await load(); + expect(page).toMatch(/^\n{"draft":{}}'); + expect(page).not.toContain('stackmap-live'); + // The live page itself is left alone. + expect(document.getElementById('stackmap-live')).not.toBeNull(); + }); + + it('is undefined when the page embeds no diagram', async () => { + document.body.innerHTML = '
'; + expect(await load()).toBeUndefined(); + }); +}); diff --git a/packages/viewer/src/standalone.ts b/packages/viewer/src/standalone.ts new file mode 100644 index 0000000..554a232 --- /dev/null +++ b/packages/viewer/src/standalone.ts @@ -0,0 +1,15 @@ +import { DATA_ELEMENT_ID } from './data'; +import { LIVE_ELEMENT_ID } from './live'; + +/** + * The page as delivered, read when this module loads, before the viewer renders into it, so Export can save it + * again as an offline file. main.tsx imports it first for that reason. Undefined where there is no embedded + * diagram to save (the website's embedded viewer, the dev gallery). + */ +export const standalonePage: string | undefined = (() => { + if (typeof document === 'undefined' || !document.getElementById(DATA_ELEMENT_ID)?.textContent?.trim()) return undefined; + const root = document.documentElement.cloneNode(true) as HTMLElement; + // A page from `stackmap serve` would otherwise keep reconnecting to a server that isn't there. + root.querySelector(`#${LIVE_ELEMENT_ID}`)?.remove(); + return `\n${root.outerHTML}`; +})(); diff --git a/site/app/docs/viewer/page.tsx b/site/app/docs/viewer/page.tsx index 7f443a2..99955b8 100644 --- a/site/app/docs/viewer/page.tsx +++ b/site/app/docs/viewer/page.tsx @@ -62,6 +62,10 @@ export default function ViewerPage() { JPEG and WebP, where the browser can encode them , SVG, + <> + HTML: the diagram as one offline file, the same as deliver writes. Opened from serve, it leaves out the live + connection. The viewers on this site don’t have it. + , 'A short video of the flow, in the format your browser records', ]} />