Repository navigation
feat: self-contained HTML page of a space's map views #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** Writes dist/page.html, the page template with its JS and CSS inlined. Run after the library build. */ | ||
|
|
||
| import { readFileSync, writeFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { PAGE_SLOTS } from '../src/node/page'; | ||
|
|
||
| const root = join(import.meta.dir, '..'); | ||
|
|
||
| const result = await Bun.build({ | ||
| entrypoints: [join(root, 'src/page/main.tsx')], | ||
| target: 'browser', | ||
| format: 'esm', | ||
| minify: true, | ||
| define: { 'process.env.NODE_ENV': '"production"' }, | ||
| }); | ||
| if (!result.success) throw new AggregateError(result.logs, 'Bundling the page host failed'); | ||
| if (result.outputs.length !== 1) throw new Error(`Expected one bundle, got ${result.outputs.length}.`); | ||
| // A script element ends at the first `</script`, wherever it appears | ||
| const js = (await result.outputs[0]!.text()).replaceAll('</script', '<\\/script'); | ||
|
|
||
| const css = [join(root, 'dist/styles.css'), join(root, 'src/viewer/viewer.css')] | ||
| .map((f) => readFileSync(f, 'utf8')) | ||
| .join('\n') | ||
| .replaceAll('</style', '<\\/style'); | ||
|
|
||
| const page = `<!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <title>${PAGE_SLOTS.title}</title> | ||
| <style> | ||
| ${css} | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="application/json" id="sctx-snapshots">${PAGE_SLOTS.snapshots}</script> | ||
| <script type="module">${js}</script> | ||
| </body> | ||
| </html> | ||
| `; | ||
| writeFileSync(join(root, 'dist/page.html'), page); | ||
| console.log(`dist/page.html: ${(page.length / 1024).toFixed(0)} KB`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { SpaceSnapshot } from '../core/snapshot'; | ||
|
|
||
| export const PAGE_SLOTS = { title: '<!--sctx-title-->', snapshots: '<!--sctx-snapshots-->' } as const; | ||
|
|
||
| /** `template` (`dist/page.html`) with the title and snapshots written in. */ | ||
| export function fillPage(template: string, snapshots: SpaceSnapshot[], title: string): string { | ||
| for (const slot of Object.values(PAGE_SLOTS)) { | ||
| if (template.split(slot).length !== 2) throw new Error(`The page template must hold ${slot} exactly once.`); | ||
| } | ||
| // Escaping `<` stops a string in the data ending the script element | ||
| const json = JSON.stringify(snapshots).replaceAll('<', '\\u003c'); | ||
| return template.replace(PAGE_SLOTS.title, () => escapeHtml(title)).replace(PAGE_SLOTS.snapshots, () => json); | ||
| } | ||
|
|
||
| function escapeHtml(text: string): string { | ||
| return text.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { createRoot } from 'react-dom/client'; | ||
| import type { SpaceSnapshot } from '../core/snapshot'; | ||
| import { Viewer } from '../viewer/Viewer'; | ||
|
|
||
| /** The host for a self-contained page: shows the snapshots the page carries. */ | ||
|
|
||
| function readSnapshots(): SpaceSnapshot[] { | ||
| const text = document.getElementById('sctx-snapshots')?.textContent?.trim(); | ||
| if (!text) throw new Error('This page carries no space snapshot.'); | ||
| const snapshots = JSON.parse(text) as SpaceSnapshot[]; | ||
| if (!Array.isArray(snapshots) || snapshots.length === 0) throw new Error('This page carries no space snapshot.'); | ||
| return snapshots; | ||
| } | ||
|
|
||
| const root = createRoot(document.getElementById('root')!); | ||
| try { | ||
| root.render(<Viewer spaces={readSnapshots()} />); | ||
| } catch (err) { | ||
| root.render(<p className="viewer-message">{err instanceof Error ? err.message : String(err)}</p>); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import type { SpaceSnapshot } from '../src/core/snapshot'; | ||
| import { fillPage, PAGE_SLOTS } from '../src/node/page'; | ||
|
|
||
| const template = `<title>${PAGE_SLOTS.title}</title><script type="application/json" id="s">${PAGE_SLOTS.snapshots}</script>`; | ||
|
|
||
| const carried = (html: string) => JSON.parse(/id="s">(.*)<\/script>/s.exec(html)![1]!); | ||
|
|
||
| describe('fillPage', () => { | ||
| test('writes the title and snapshots, escaped so data cannot close the script or title', () => { | ||
| const snapshot = { | ||
| space: 'a</script><!--', | ||
| schemaId: undefined, | ||
| graph: {}, | ||
| validation: { issues: [] }, | ||
| definitions: [], | ||
| }; | ||
| const html = fillPage(template, [snapshot as unknown as SpaceSnapshot], '<b>Plan</b> & $&'); | ||
| expect(html).toContain('<title><b>Plan</b> & $&</title>'); | ||
| expect(html.match(/<\/script>/g)).toHaveLength(1); | ||
| expect(carried(html)).toEqual([{ ...snapshot, schemaId: undefined }]); | ||
| }); | ||
|
|
||
| test('rejects a template without each slot exactly once', () => { | ||
| expect(() => fillPage('<title></title>', [], 'x')).toThrow(PAGE_SLOTS.title); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.