|
| 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()) |
0 commit comments