Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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 `<pdfjs-viewer-element>`, 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/play/` | The play page: the activity in a link's `#source=`, built in the visitor's browser as the build page builds it, with any file of their own it names chosen there, 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) |
Expand Down
2 changes: 2 additions & 0 deletions src/assets/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,9 @@ pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.9375re
.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="asking"] { color: inherit; }
.play-status[data-state="error"] { color: inherit; font-weight: 600; }
.play-files { margin: 0 0 0.5rem; }
.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; }
8 changes: 7 additions & 1 deletion src/h5p/play/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
so that a chat app can show the activity in its reply: play-page.ts builds it and plays it. -#}
<main class="play" id="main">
<p class="play-status" id="status" role="status" data-state="building">Building the activity in your browser…</p>
<div class="player-box">
{#- For a source that names files of the person's own, a video beside it: chosen here, where
they stay in the browser. The field is hidden, and the button opens it. -#}
<p class="play-files" id="files-box" hidden>
<button type="button" class="button button-solid" id="choose">Choose the file</button>
<input type="file" id="files" multiple hidden>
</p>
<div class="player-box" id="player-box" hidden>
<h5p-player id="player"></h5p-player>
</div>
<p class="play-actions">
Expand Down
137 changes: 122 additions & 15 deletions src/h5p/play/play-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,52 @@ 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'
import { baseName, matchFiles, 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.
*
* A source that names files of the person's own, a video beside it, is asked for them here: the
* person chooses them from their computer, or the frame's parent gives them, and the activity is
* built once they are all here. A chat app was found to hand a card no file the person attached
* (ChatGPT, 2026-11-20: the tool got a piece of text in the file's place), so the page asks itself.
* A file chosen here goes nowhere: the build reads it in the browser.
*/

const $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T
const status = $('status')
const filesBox = $('files-box')
const choose = $<HTMLButtonElement>('choose')
const filesInput = $<HTMLInputElement>('files')
const playerBox = $('player-box')
const download = $<HTMLAnchorElement>('download')
const edit = $<HTMLAnchorElement>('edit')
const player = $('player') as HTMLElementTagNameMap['h5p-player']
player.runtime = runtime

type Opened = Awaited<ReturnType<typeof openPack>>
/** The pack, opened at the first build and kept for the next, which a file chosen later starts. */
let opened: Promise<Opened> | null = null
/** The source the link carries, once read, and the files it names. */
let source: unknown
let wanted: string[] = []
/** The files given so far, by name. */
const chosen = new Map<string, File>()
/** The address of the package last built, given back when the next one replaces it. */
let packageUrl: string | null = null
/** Counts builds, so that one overtaken by a newer one shows nothing. */
let builds = 0
/** The missing names the parent was last told, so that it is told again only when they change. */
let told = ''

const framed = window.parent !== window
const toParent = (message: Record<string, unknown>): void => { if (framed) window.parent.postMessage({ source: 'h5p-play', ...message }, '*') }

function say(text: string, state: 'building' | 'ready' | 'error'): void {
function say(text: string, state: 'building' | 'asking' | 'ready' | 'error'): void {
status.textContent = text
status.dataset.state = state
toParent({ type: 'status', state, text })
Expand All @@ -34,41 +59,123 @@ 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<void> {
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')
/** A file's kind, by the ending of its name. */
const kind = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase()

/**
* The given files for the paths the source names, matched by name as the build page matches them.
* Where it names one file and one of the same kind was given, that is the file whatever it is
* called: a chat may write the name its own way, and the source cannot be changed here.
*/
function filesFor(paths: string[]): { files: Record<string, File>, missing: string[] } {
const given = [...chosen.values()]
const matched = matchFiles(paths, given)
const [path] = paths
const [file] = given
if (matched.missing.length && paths.length === 1 && given.length === 1 && path !== undefined && file !== undefined && kind(path) === kind(file.name)) {
return { files: { [path]: file }, missing: [] }
}
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')
return matched
}

/**
* Asks for the files still missing, by their names, and tells the parent, which may have them. The
* parent is told once for a set of names: one that answers each time with the same files, some of
* them or none that match, would otherwise be asked again with every answer, without end.
*/
function ask(missing: string[]): void {
const names = missing.map(baseName)
const listed = new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(names)
const one = missing.length === 1
choose.textContent = one ? 'Choose the file' : 'Choose the files'
filesInput.accept = [...new Set(missing.map((path) => `.${kind(path)}`))].join(',')
filesBox.hidden = false
say(chosen.size
? `Still needed: ${listed}.`
: `This activity uses ${one ? 'a file' : 'files'} of yours: ${listed}. Choose ${one ? 'it' : 'them'} from your computer; ${one ? 'it stays' : 'they stay'} in your browser.`, 'asking')
const missingNow = names.join('\n')
if (missingNow === told) return
told = missingNow
toParent({ type: 'needs-files', names })
Comment thread
alekswebnet marked this conversation as resolved.
Resolved
}

/** Files given, by the person or by the parent: the activity is built once none is missing. */
function take(files: File[]): void {
if (!files.length || !wanted.length) return
// Where one file is named, a second choice replaces the first.
if (wanted.length === 1) chosen.clear()
for (const file of files) chosen.set(file.name, file)
void play()
}

async function play(): Promise<void> {
const build = ++builds
const { files, missing } = filesFor(wanted)
if (missing.length) return ask(missing)
filesBox.hidden = true
say('Building the activity in your browser…', 'building')
try {
const result = await buildPackage(parsed.source, { libraries: await openPack(installPack), files: {} })
opened ??= openPack(installPack)
const result = await buildPackage(source, { libraries: await opened, files })
if (build !== builds) return
const name = packageName(String(result.manifest.title ?? ''))
download.href = URL.createObjectURL(result.blob)
if (packageUrl) URL.revokeObjectURL(packageUrl)
packageUrl = URL.createObjectURL(result.blob)
download.href = packageUrl
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')
playerBox.hidden = false
player.file = file
} catch (error) {
if (build !== builds) return
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')
}
// A pack that could not open is opened again at the next build.
opened = null
say(`It could not be built: ${error instanceof Error ? error.message : error}`, 'error')
}
}

async function start(): Promise<void> {
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')
source = parsed.source
wanted = namedFiles(source)
await play()
}

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()
// The file field is hidden, and the button opens it.
choose.addEventListener('click', () => filesInput.click())
filesInput.addEventListener('change', () => {
const files = [...(filesInput.files ?? [])]
filesInput.value = ''
take(files)
})
// Files from the frame's parent, which may hold what the person attached in the chat.
window.addEventListener('message', (event) => {
if (!framed || event.source !== window.parent) return
const message = event.data as { source?: unknown, type?: unknown, files?: unknown } | null
if (message?.source !== 'h5p-play-host' || message.type !== 'files' || !Array.isArray(message.files)) return
take(message.files.filter((file): file is File => file instanceof File))
})

void start()
window.addEventListener('hashchange', () => location.reload())