diff --git a/README.md b/README.md index ddfb7dc..bcd5411 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ The site of the [missing-elements](https://github.com/missing-elements) web comp | `/` | The project list, the tools (the build page first, with its how-to), and a footer of links | [src/index.njk](src/index.njk); the lists are in [src/_data/site.js](src/_data/site.js) | | `/pdfjs-viewer/` | The docs of ``, with a live preview | [src/pdfjs-viewer/](src/pdfjs-viewer/) | | `/h5p/build/` | The build page, for people: what it makes, a sample that plays, and the build of a chat's source into a `.h5p`, in the visitor's browser | [src/h5p/build/](src/h5p/build/) | +| `/h5p/play/` | The play page: the activity in a link's `#source=`, built in the visitor's browser as the build page builds it, and played with nothing beside it but its download and a link to the build page, for a chat app to show in its reply (h5p-toolkit#210); no page links to it | [src/h5p/play/](src/h5p/play/) | | `/h5p/create-with-ai/` | The how-to, for people: installing the skill, asking a chat, the prompts to start from, the types, and what to do when something goes wrong | [src/h5p/create-with-ai/](src/h5p/create-with-ai/) | | `/h5p/skill/` | The skill as zips to install: `h5p-author.zip`, installed once in a chat's place for skills, or attached to the message in each new chat where it has none, and `h5p-author-chatgpt.zip`, the same skill laid out as a plugin; and a page that lists them and the bundles' addresses, each in full, which no page links to | [src/h5p/skill/](src/h5p/skill/); the zips written by [vite.config.ts](vite.config.ts) | | `/h5p/bundles/` | The skill's build tool and a library bundle per content type, for a chat that runs Node but cannot reach npm; and `h5p-author-build.tar.gz`, the tool and every library in one archive, for a person to attach to a chat whose code can download nothing | written by [vite.config.ts](vite.config.ts) | @@ -36,7 +37,7 @@ pnpm preview # serves _site/ as GitHub Pages does, http://localhost:4173 ## How it is built and deployed - **Eleventy** writes the pages from `src/` into `_site/`. They ship no framework. Their scripts are the theme toggle and the code-block buttons ([src/assets/site.js](src/assets/site.js)), the pdfjs-viewer live preview ([src/pdfjs-viewer/docs.js](src/pdfjs-viewer/docs.js)) and the build page. -- **Vite** then writes `/h5p/` into the same `_site/`: the build page's script, bundled with the H5P player, its runtime and the library pack; the build page's sample; the type files; and the bundles. That is build output: git holds none of it, and the bundles alone are over 100 MB. +- **Vite** then writes `/h5p/` into the same `_site/`: the build page's and the play page's scripts, bundled with the H5P player, its runtime and the library pack; the build page's sample; the type files; and the bundles. That is build output: git holds none of it, and the bundles alone are over 100 MB. - A push to `main` deploys through [pages.yml](.github/workflows/pages.yml). A pull request is built by [build.yml](.github/workflows/build.yml). ## What the site pins diff --git a/src/assets/site.css b/src/assets/site.css index a879a1d..2412139 100644 --- a/src/assets/site.css +++ b/src/assets/site.css @@ -1086,3 +1086,12 @@ pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.9375re .feature-icon::before { forced-color-adjust: none; background: CanvasText; } .step-marker::before, .sample-note[data-loading]::before { border-color: GrayText; border-top-color: Highlight; } } + +/* The play page, /h5p/play/: the activity alone, for a chat app's frame. */ +.play { padding: 0.5rem; } +.play .player-box { margin: 0; } +.play-status { margin: 0 0 0.5rem; color: var(--muted); font-size: 0.9rem; } +.play-status[data-state="error"] { color: inherit; font-weight: 600; } +.play-status[data-state="ready"] { display: none; } +.play-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.5rem 0 0; } +.play-actions [hidden] { display: none; } diff --git a/src/h5p/play/index.njk b/src/h5p/play/index.njk new file mode 100644 index 0000000..bc9149d --- /dev/null +++ b/src/h5p/play/index.njk @@ -0,0 +1,23 @@ +--- +layout: base.njk +title: "Play an H5P activity" +description: "An H5P activity an AI chat wrote, built and played in your browser, with its .h5p file to download. Nothing is sent to a server." +# Not a page to land on from a search: it is opened with an activity's source in its link. +eleventyExcludeFromCollections: true +# As the build page's policy: packages it built play in the player's frame, under the policy the +# player's Service Worker gives each response there. +csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data: blob:; font-src 'self'; connect-src * data: blob:; worker-src 'self' blob:; frame-src 'self' blob:; object-src 'none'; base-uri 'self'" +--- +{#- The player alone, for an activity's source in the link (#source=, as the build page takes it), + so that a chat app can show the activity in its reply: play-page.ts builds it and plays it. -#} +
+

Building the activity in your browser…

+
+ +
+

+ + Open in the build page +

+
+ diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts new file mode 100644 index 0000000..1752650 --- /dev/null +++ b/src/h5p/play/play-page.ts @@ -0,0 +1,74 @@ +import '@missing-elements/h5p-offline-player' +import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser' +import { pack as installPack } from '@missing-elements/h5p-libraries/install' +import { runtime } from '@missing-elements/h5p-runtime' +import { namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source' + +/** + * The play page, `/h5p/play/`: the activity in a link's `#source=` built in the browser, as the + * build page builds it, and played, with nothing else on the page but its file to download and a + * way to the build page. A chat app frames it to show the activity in a reply (h5p-toolkit#210). + * Framed, it tells the frame's parent its height, and gives it the built file, which a parent that + * may not let the frame download can save its own way. Nothing leaves the browser otherwise. + */ + +const $ = (id: string): T => document.getElementById(id) as T +const status = $('status') +const download = $('download') +const edit = $('edit') +const player = $('player') as HTMLElementTagNameMap['h5p-player'] +player.runtime = runtime + +const framed = window.parent !== window +const toParent = (message: Record): void => { if (framed) window.parent.postMessage({ source: 'h5p-play', ...message }, '*') } + +function say(text: string, state: 'building' | 'ready' | 'error'): void { + status.textContent = text + status.dataset.state = state + toParent({ type: 'status', state, text }) +} + +// The build page, with the same source, where its problems can be fixed and its files chosen. +edit.href = `/h5p/build/${location.hash}` + +// The page's height for the parent, which sizes its frame to it. +if (framed) new ResizeObserver(() => toParent({ type: 'height', height: Math.ceil(document.documentElement.getBoundingClientRect().height) })).observe(document.body) + +async function play(): Promise { + let text: string | null + try { + text = await sourceFromFragment(location.hash) + } catch { + return say('The link is damaged: its activity cannot be read. Ask the chat for it again.', 'error') + } + if (text === null) return say('The link carries no activity. This page plays the one an AI chat put in its link.', 'error') + const parsed = parseSource(text) + if ('problem' in parsed) return say('The activity in the link cannot be read. Open it in the build page to see why.', 'error') + if (namedFiles(parsed.source).length) return say('The activity names files of your own: open it in the build page to choose them.', 'error') + try { + const result = await buildPackage(parsed.source, { libraries: await openPack(installPack), files: {} }) + const name = packageName(String(result.manifest.title ?? '')) + download.href = URL.createObjectURL(result.blob) + download.download = name + download.hidden = false + const file = new File([result.blob], name, { type: 'application/zip' }) + toParent({ type: 'built', file, title: result.manifest.title, size: sized(result.blob.size) }) + say(`“${String(result.manifest.title ?? name)}”, ${sized(result.blob.size)}. Starting it…`, 'building') + player.file = file + } catch (error) { + if (error instanceof SourceError) { + const problems = (error as SourceError & { problems: Problem[] }).problems + return say(`The activity has ${problems.length} problem${problems.length === 1 ? '' : 's'}: open it in the build page to see and fix ${problems.length === 1 ? 'it' : 'them'}.`, 'error') + } + say(`It could not be built: ${error instanceof Error ? error.message : error}`, 'error') + } +} + +player.addEventListener('ready', () => say(status.textContent!.replace(/ Starting it…$/, ''), 'ready')) +player.addEventListener('error', (event) => { + if (player.state === 'ready') return + say(`It was built, but does not play: ${(event as unknown as CustomEvent<{ message: string }>).detail.message}`, 'error') +}) + +void play() +window.addEventListener('hashchange', () => location.reload()) diff --git a/vite.config.ts b/vite.config.ts index 4ffa550..e7b4796 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -375,8 +375,8 @@ export default defineConfig({ // ours), beside the runtime's and the pack's: MIT and BSD-3-Clause ask it to travel with them. license: { fileName: `${ASSETS}/build-page-LICENSES.txt` }, rolldownOptions: { - input: { 'build-page': 'src/h5p/build/build-page.ts' }, - output: { entryFileNames: 'h5p/build/[name].js' } + input: { 'build-page': 'src/h5p/build/build-page.ts', 'play-page': 'src/h5p/play/play-page.ts' }, + output: { entryFileNames: (chunk) => chunk.name === 'play-page' ? 'h5p/play/[name].js' : 'h5p/build/[name].js' } } } })