Repository navigation
feat(play): a play page, the activity alone for a chat app's card #87
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. -#} | ||
| <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"> | ||
| <h5p-player id="player"></h5p-player> | ||
| </div> | ||
| <p class="play-actions"> | ||
| <a class="button button-solid" id="download" download hidden>Download the .h5p</a> | ||
| <a class="button" id="edit" target="_blank" rel="noopener">Open in the build page</a> | ||
| </p> | ||
| </main> | ||
| <script type="module" src="/h5p/play/play-page.js"></script> |
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,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 $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T | ||
| const status = $('status') | ||
| const download = $<HTMLAnchorElement>('download') | ||
| const edit = $<HTMLAnchorElement>('edit') | ||
| const player = $('player') as HTMLElementTagNameMap['h5p-player'] | ||
| player.runtime = runtime | ||
|
|
||
| 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 { | ||
| 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<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') | ||
| 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()) |
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
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.