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', ]} /> diff --git a/site/components/viewer/useTry.ts b/site/components/viewer/useTry.ts index 461ee32..5040f6d 100644 --- a/site/components/viewer/useTry.ts +++ b/site/components/viewer/useTry.ts @@ -2,6 +2,7 @@ import { useState } from 'react'; import { useExplore } from '@stackmap/viewer/src/explore/ExploreContext'; +import { isRouting } from '@stackmap/viewer/src/explore/state'; export type Try = 'select' | 'trace' | 'route' | 'find' | 'views' | 'play'; @@ -21,11 +22,12 @@ export function useTry(show: Showcase) { const { state, dispatch } = useExplore(); // The mode that was on when Play was last pressed; undefined once another button is. const [playedOver, setPlayedOver] = useState(); - const active: Try | null = state.route || state.routing ? 'route' : state.query !== null ? 'find' : state.selected && state.trace ? 'trace' : state.selected ? 'select' : state.view ? 'views' : null; + const tracing = state.mode.kind === 'trace'; + const active: Try | null = isRouting(state.mode) ? 'route' : state.query !== null ? 'find' : state.selected && tracing ? 'trace' : state.selected ? 'select' : state.view ? 'views' : null; const on = (id: Try) => (id === 'play' ? state.playing : active === id); const reset = () => { dispatch({ type: 'clear' }); - if (state.trace) dispatch({ type: 'toggleTrace' }); + if (tracing) dispatch({ type: 'toggleTrace' }); if (state.view) dispatch({ type: 'view', id: null }); }; const go = (id: Try) => { diff --git a/site/e2e/smoke.spec.ts b/site/e2e/smoke.spec.ts index 518e162..0ab3681 100644 --- a/site/e2e/smoke.spec.ts +++ b/site/e2e/smoke.spec.ts @@ -328,11 +328,14 @@ test('the docs search loads its index again after a failed fetch', async ({ page await page.route('**/search.json', (r) => (fail ? r.abort() : r.continue())); await page.goto('/docs'); const box = page.getByRole('combobox', { name: 'Search the docs' }); + const failed = page.waitForEvent('requestfailed', (r) => r.url().endsWith('/search.json')); await expect(async () => { await page.keyboard.press('ControlOrMeta+k'); await expect(box).toBeFocused({ timeout: 500 }); }).toPass(); await box.fill('serve'); + // An open while the failing fetch is still pending shares it, so the retry is only due once it has failed. + await failed; await expect(page.getByRole('option')).toHaveCount(0); await page.keyboard.press('Escape'); await expect(box).toHaveCount(0);