Skip to content

Commit 759cc61

Browse files
authored
Merge pull request #87 from missing-elements/feat/play-page
feat(play): a play page, the activity alone for a chat app's card
2 parents ed6134b + 4fd4ace commit 759cc61

5 files changed

Lines changed: 110 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The site of the [missing-elements](https://github.com/missing-elements) web comp
99
| `/` | 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) |
1010
| `/pdfjs-viewer/` | The docs of `<pdfjs-viewer-element>`, with a live preview | [src/pdfjs-viewer/](src/pdfjs-viewer/) |
1111
| `/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/) |
12+
| `/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/) |
1213
| `/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/) |
1314
| `/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) |
1415
| `/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
3637
## How it is built and deployed
3738

3839
- **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.
39-
- **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.
40+
- **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.
4041
- A push to `main` deploys through [pages.yml](.github/workflows/pages.yml). A pull request is built by [build.yml](.github/workflows/build.yml).
4142

4243
## What the site pins

‎src/assets/site.css‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,12 @@ pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.9375re
10861086
.feature-icon::before { forced-color-adjust: none; background: CanvasText; }
10871087
.step-marker::before, .sample-note[data-loading]::before { border-color: GrayText; border-top-color: Highlight; }
10881088
}
1089+
1090+
/* The play page, /h5p/play/: the activity alone, for a chat app's frame. */
1091+
.play { padding: 0.5rem; }
1092+
.play .player-box { margin: 0; }
1093+
.play-status { margin: 0 0 0.5rem; color: var(--muted); font-size: 0.9rem; }
1094+
.play-status[data-state="error"] { color: inherit; font-weight: 600; }
1095+
.play-status[data-state="ready"] { display: none; }
1096+
.play-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.5rem 0 0; }
1097+
.play-actions [hidden] { display: none; }

‎src/h5p/play/index.njk‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
layout: base.njk
3+
title: "Play an H5P activity"
4+
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."
5+
# Not a page to land on from a search: it is opened with an activity's source in its link.
6+
eleventyExcludeFromCollections: true
7+
# As the build page's policy: packages it built play in the player's frame, under the policy the
8+
# player's Service Worker gives each response there.
9+
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'"
10+
---
11+
{#- The player alone, for an activity's source in the link (#source=, as the build page takes it),
12+
so that a chat app can show the activity in its reply: play-page.ts builds it and plays it. -#}
13+
<main class="play" id="main">
14+
<p class="play-status" id="status" role="status" data-state="building">Building the activity in your browser…</p>
15+
<div class="player-box">
16+
<h5p-player id="player"></h5p-player>
17+
</div>
18+
<p class="play-actions">
19+
<a class="button button-solid" id="download" download hidden>Download the .h5p</a>
20+
<a class="button" id="edit" target="_blank" rel="noopener">Open in the build page</a>
21+
</p>
22+
</main>
23+
<script type="module" src="/h5p/play/play-page.js"></script>

‎src/h5p/play/play-page.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import '@missing-elements/h5p-offline-player'
2+
import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser'
3+
import { pack as installPack } from '@missing-elements/h5p-libraries/install'
4+
import { runtime } from '@missing-elements/h5p-runtime'
5+
import { namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'
6+
7+
/**
8+
* The play page, `/h5p/play/`: the activity in a link's `#source=` built in the browser, as the
9+
* build page builds it, and played, with nothing else on the page but its file to download and a
10+
* way to the build page. A chat app frames it to show the activity in a reply (h5p-toolkit#210).
11+
* Framed, it tells the frame's parent its height, and gives it the built file, which a parent that
12+
* may not let the frame download can save its own way. Nothing leaves the browser otherwise.
13+
*/
14+
15+
const $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T
16+
const status = $('status')
17+
const download = $<HTMLAnchorElement>('download')
18+
const edit = $<HTMLAnchorElement>('edit')
19+
const player = $('player') as HTMLElementTagNameMap['h5p-player']
20+
player.runtime = runtime
21+
22+
const framed = window.parent !== window
23+
const toParent = (message: Record<string, unknown>): void => { if (framed) window.parent.postMessage({ source: 'h5p-play', ...message }, '*') }
24+
25+
function say(text: string, state: 'building' | 'ready' | 'error'): void {
26+
status.textContent = text
27+
status.dataset.state = state
28+
toParent({ type: 'status', state, text })
29+
}
30+
31+
// The build page, with the same source, where its problems can be fixed and its files chosen.
32+
edit.href = `/h5p/build/${location.hash}`
33+
34+
// The page's height for the parent, which sizes its frame to it.
35+
if (framed) new ResizeObserver(() => toParent({ type: 'height', height: Math.ceil(document.documentElement.getBoundingClientRect().height) })).observe(document.body)
36+
37+
async function play(): Promise<void> {
38+
let text: string | null
39+
try {
40+
text = await sourceFromFragment(location.hash)
41+
} catch {
42+
return say('The link is damaged: its activity cannot be read. Ask the chat for it again.', 'error')
43+
}
44+
if (text === null) return say('The link carries no activity. This page plays the one an AI chat put in its link.', 'error')
45+
const parsed = parseSource(text)
46+
if ('problem' in parsed) return say('The activity in the link cannot be read. Open it in the build page to see why.', 'error')
47+
if (namedFiles(parsed.source).length) return say('The activity names files of your own: open it in the build page to choose them.', 'error')
48+
try {
49+
const result = await buildPackage(parsed.source, { libraries: await openPack(installPack), files: {} })
50+
const name = packageName(String(result.manifest.title ?? ''))
51+
download.href = URL.createObjectURL(result.blob)
52+
download.download = name
53+
download.hidden = false
54+
const file = new File([result.blob], name, { type: 'application/zip' })
55+
toParent({ type: 'built', file, title: result.manifest.title, size: sized(result.blob.size) })
56+
say(`“${String(result.manifest.title ?? name)}”, ${sized(result.blob.size)}. Starting it…`, 'building')
57+
player.file = file
58+
} catch (error) {
59+
if (error instanceof SourceError) {
60+
const problems = (error as SourceError & { problems: Problem[] }).problems
61+
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')
62+
}
63+
say(`It could not be built: ${error instanceof Error ? error.message : error}`, 'error')
64+
}
65+
}
66+
67+
player.addEventListener('ready', () => say(status.textContent!.replace(/ Starting it…$/, ''), 'ready'))
68+
player.addEventListener('error', (event) => {
69+
if (player.state === 'ready') return
70+
say(`It was built, but does not play: ${(event as unknown as CustomEvent<{ message: string }>).detail.message}`, 'error')
71+
})
72+
73+
void play()
74+
window.addEventListener('hashchange', () => location.reload())

‎vite.config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ export default defineConfig({
375375
// ours), beside the runtime's and the pack's: MIT and BSD-3-Clause ask it to travel with them.
376376
license: { fileName: `${ASSETS}/build-page-LICENSES.txt` },
377377
rolldownOptions: {
378-
input: { 'build-page': 'src/h5p/build/build-page.ts' },
379-
output: { entryFileNames: 'h5p/build/[name].js' }
378+
input: { 'build-page': 'src/h5p/build/build-page.ts', 'play-page': 'src/h5p/play/play-page.ts' },
379+
output: { entryFileNames: (chunk) => chunk.name === 'play-page' ? 'h5p/play/[name].js' : 'h5p/build/[name].js' }
380380
}
381381
}
382382
})

0 commit comments

Comments
 (0)