From 1458493481be3fb0cfbfc1f54c6daf3279018e74 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Tue, 24 Nov 2026 12:55:31 +0200 Subject: [PATCH 1/2] feat(play): the tab that saves a package draws no player, and closes itself when it is saved (#104) --- README.md | 2 +- src/assets/site.css | 4 +- src/h5p/play/index.njk | 10 +++ src/h5p/play/play-page.ts | 134 ++++++++++++++++++++++++++++++++------ 4 files changed, 128 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 243fd57..5cd16b0 100644 --- a/README.md +++ b/README.md @@ -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 ``, 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, for a chat app to show in its reply (h5p-toolkit#210); no page links to it. It builds the activity in a link's `#source=` in the visitor's browser, as the build page does, and plays it with nothing beside it but its download and a link to the build page. A file of the visitor's own that the source names is chosen there or given by the page that frames it. A video or a recording can be given as its address: it plays from there at once and is fetched whole only for the download. A framing page that saves files itself is handed the download. A package bigger than that page saves stays behind the Download link, and the page says how to save it from such a frame. A framing page that opens links itself is handed the link to the build page. A link to the page may also carry the files' addresses and a wish to save (`&links=`, `&save=1`): the page then fetches them itself and tries to save the package with no press, saying to press Download where the browser saved nothing. That is how a framing page that opens links gets a package saved that it cannot save itself. | [src/h5p/play/](src/h5p/play/) | +| `/h5p/play/` | The play page, for a chat app to show in its reply (h5p-toolkit#210); no page links to it. It builds the activity in a link's `#source=` in the visitor's browser, as the build page does, and plays it with nothing beside it but its download and a link to the build page. A file of the visitor's own that the source names is chosen there or given by the page that frames it. A video or a recording can be given as its address: it plays from there at once and is fetched whole only for the download. A framing page that saves files itself is handed the download. A package bigger than that page saves stays behind the Download link, and the page says how to save it from such a frame. A framing page that opens links itself is handed the link to the build page. A link to the page may also carry the files' addresses and a wish to save (`&links=`, `&save=1`): in a tab of its own the page then draws no player: it fetches the files itself, builds the package, tries to save it with no press and closes the tab, or says to press Download where the browser saved nothing. That is how a framing page that opens links gets a package saved that it cannot save itself. | [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) | diff --git a/src/assets/site.css b/src/assets/site.css index 18c9f5d..82aa8c3 100644 --- a/src/assets/site.css +++ b/src/assets/site.css @@ -1111,7 +1111,7 @@ html[data-standalone] .play { padding: 1rem; } .play[data-drop] .play-stage { border-color: var(--accent); } .play[data-drop] .play-panel { background: var(--tint); } -.play-loader, .play-ask, .play-error { display: grid; justify-items: center; gap: 0.75rem; width: 100%; min-width: 0; max-width: 26rem; } +.play-loader, .play-ask, .play-error, .play-saved { display: grid; justify-items: center; gap: 0.75rem; width: 100%; min-width: 0; max-width: 26rem; } .play-panel .button { white-space: normal; } .play-label, .play-text, .play-note, .play-heading { margin: 0; } .play-label { font-size: 0.9375rem; font-weight: 600; line-height: 1.4; } @@ -1123,6 +1123,8 @@ html[data-standalone] .play { padding: 1rem; } .play-progress span { display: block; width: var(--done, 0%); height: 100%; border-radius: inherit; background: var(--accent); } .play-icon { box-sizing: border-box; width: 3rem; height: 3rem; padding: 0.75rem; border-radius: 50%; background: var(--tint); color: var(--accent); fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; } .play-error .play-icon { background: color-mix(in srgb, var(--t-tag) 14%, transparent); color: var(--t-tag); } +.play-saved-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem; margin: 0; } +.play-saved-actions:not(:has(> :not([hidden]))) { display: none; } /* Under the stage: what the activity is, and what to do with it. Until its name is known a bar of its size stands there. */ diff --git a/src/h5p/play/index.njk b/src/h5p/play/index.njk index 1f4f462..f23cd0e 100644 --- a/src/h5p/play/index.njk +++ b/src/h5p/play/index.njk @@ -43,6 +43,16 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da

+ {#- In a tab a link asked to save the package (&save=1): it is saved, and the tab closes itself. -#} +
diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index f20dbb1..61e5532 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -42,6 +42,12 @@ import { baseName, compactSource, fragmentFromSource, fragmentWithLinks, kindOf, * links, Download of a video said to weigh more than the parent saves hands it such a link, at * once, while the press still lets a tab open. * + * Such a tab is there to save a file, not to play one (#104): it draws no player, only the file + * coming, the package being built, and that it is saved, and then it closes itself, which a tab + * with nothing behind it may do. It waits a few seconds first, and longer while something of the + * browser's own is over the page, as a question where to save is. "Keep this tab open" stops it, + * and the activity can then be played there. + * * What is seen is one stage, in one of five states (`data-state` on `main`): `building`, a loader * over the place the player takes; `fetching`, the loader with how much of a file the parent has * brought so far; `asking`, for the person's files; `error`, with the one thing to do about it; @@ -63,6 +69,10 @@ const askText = $('ask-text') const choose = $('choose') const filesInput = $('files') const errorBox = $('error') +const savedBox = $('saved') +const savedText = $('saved-text') +const savedStay = $('saved-stay') +const savedPlay = $('saved-play') const errorTitle = $('error-title') const errorText = $('error-text') const fix = $('fix') @@ -81,7 +91,7 @@ const edit = $('edit') const player = $('player') as HTMLElementTagNameMap['h5p-player'] player.runtime = runtime -type State = 'building' | 'fetching' | 'asking' | 'ready' | 'error' +type State = 'building' | 'fetching' | 'asking' | 'ready' | 'error' | 'saved' type Opened = Awaited> /** The pack, opened at the first build and kept for the next, which a file chosen later starts. */ let opened: Promise | null = null @@ -134,6 +144,10 @@ let unasked = false let ownFragment = '' /** Stops the page's own fetch of a file, where the person chooses it themselves meanwhile. */ let fetchStop: AbortController | null = null +/** Whether this is a tab of its own that a link asked to save the package: it draws no player. */ +let savingOnly = carried.save && window.parent === window +/** Counts down to the tab closing itself, once the package is saved. */ +let closing: ReturnType | undefined /** Shows the question for the person's files a moment late, where the parent may bring them first. */ let askTimer: ReturnType | undefined @@ -147,6 +161,7 @@ function show(state: State, text: string): void { loader.hidden = state !== 'building' && state !== 'fetching' filesBox.hidden = state !== 'asking' errorBox.hidden = state !== 'error' + savedBox.hidden = state !== 'saved' // The way to the build page is the problem's own button where it has one. edit.hidden = state === 'error' && !fix.hidden if (status.textContent !== text) status.textContent = text @@ -289,6 +304,8 @@ function take(files: File[]): void { // meanwhile, must not start the activity again under them, nor take the place of the file // the package to keep is being built from. if (whole || packing) return + // In a tab that only saves, a file chosen while the same one is fetched is the one built from. + if (savingOnly) fetchStop?.abort() // 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) @@ -395,29 +412,34 @@ function fetchToKeep(): void { } /** - * The files of a preview fetched by this page, where no parent holds them: from the addresses it - * plays from, counted as they come. The browser keeps each as a Blob, which it may hold on disk. + * Files fetched by this page from their addresses, by their paths in the source, counted as they + * come, ten times a second at most. The browser keeps each as a Blob, which it may hold on disk. */ +async function fetched(from: Record, signal: AbortSignal, count: (name: string, received: number, total: number) => void): Promise { + const files: File[] = [] + for (const [path, address] of Object.entries(from)) { + const response = await fetch(address, { signal }) + if (!response.ok || !response.body) throw new Error(`the chat answered ${response.status}`) + const total = Number(response.headers.get('content-length')) || 0 + let received = 0 + let shown = 0 + const counted = response.body.pipeThrough(new TransformStream({ + transform(chunk, controller) { + received += chunk.byteLength + if (performance.now() - shown > 100) { shown = performance.now(); count(baseName(path), received, total) } + controller.enqueue(chunk) + }, + })) + files.push(new File([await new Response(counted).blob()], baseName(path), { type: response.headers.get('content-type') ?? '' })) + } + return files +} + +/** The files of a preview fetched by this page, where no parent holds them: from the addresses it plays from. */ async function fetchFiles(): Promise { const stop = fetchStop = new AbortController() try { - const files: File[] = [] - for (const path of previewed) { - const response = await fetch(shownFrom[path] ?? '', { signal: stop.signal }) - if (!response.ok || !response.body) throw new Error(`the chat answered ${response.status}`) - const total = Number(response.headers.get('content-length')) || 0 - let received = 0 - let shown = 0 - const counted = response.body.pipeThrough(new TransformStream({ - transform(chunk, controller) { - received += chunk.byteLength - // Shown ten times a second at most. - if (performance.now() - shown > 100) { shown = performance.now(); keepCount(baseName(path), received, total) } - controller.enqueue(chunk) - }, - })) - files.push(new File([await new Response(counted).blob()], baseName(path), { type: response.headers.get('content-type') ?? '' })) - } + const files = await fetched(Object.fromEntries(previewed.map((path) => [path, shownFrom[path] ?? ''])), stop.signal, keepCount) if (stop.signal.aborted) return take(files) } catch (error) { @@ -434,6 +456,64 @@ async function fetchFiles(): Promise { } } +/** + * In a tab that only saves: the files fetched from the addresses the link carried, shown coming in + * the stage, and then built from. One that cannot be fetched is asked for, as where none was given. + */ +async function fetchThenBuild(from: Record): Promise { + const stop = fetchStop = new AbortController() + const paths = Object.keys(from) + const what = paths.length === 1 ? called(paths[0]!) : 'files' + label.textContent = `Getting your ${what}…` + note.textContent = 'From the chat to your browser, nowhere else.' + progress.hidden = true + instead.hidden = false + show('fetching', `Getting your ${what} from the chat.`) + try { + const files = await fetched(from, stop.signal, fetching) + if (stop.signal.aborted) return + take(files) + } catch { + if (stop.signal.aborted) return + links.clear() + ask(paths) + askText.textContent = `Your ${what} could not be fetched from the chat: its address may have run out. Choose ${paths.length === 1 ? 'it' : 'them'} from your computer; ${paths.length === 1 ? 'it stays' : 'they stay'} in your browser.` + } +} + +/** + * The package saved in a tab that only saves, with no press, and the tab closed a few seconds + * later. The count stops while the page is seen and something else has the keyboard: a question + * of the browser's own, where to save or whether to, is then likely over it. + */ +function savedHere(file: File): void { + saveUnasked = false + download.click() + const named = `${file.name}, ${sized(file.size)},` + let left = 5 + const said = (): void => { savedText.textContent = `${named} is in your downloads. This tab closes in ${left} s.` } + savedStay.hidden = false + savedPlay.hidden = true + said() + show('saved', `Your .h5p is saved: ${named} is in your downloads. This tab closes itself in a few seconds.`) + clearInterval(closing) + closing = setInterval(() => { + if (!document.hidden && !document.hasFocus()) return + if (--left > 0) return said() + // A tab the browser lets no page close stays, and says what it would have. + stay() + window.close() + }, 1000) +} + +/** The tab kept open: the way on where the browser saved nothing, and the activity to play. */ +function stay(): void { + clearInterval(closing) + savedText.textContent = `${kept?.name ?? 'The .h5p'}, ${sized(kept?.size ?? 0)}, is in your downloads. If nothing was saved, press Download.` + savedStay.hidden = true + savedPlay.hidden = false +} + /** How much of a file for the download has come. */ function keepCount(name: string, received: number, total: number): void { if (!bringing) return @@ -540,6 +620,8 @@ async function play(): Promise { about(result.manifest.title, result.builtWith, result.blob.size) toParent({ type: 'built', file, title: result.manifest.title, size: sized(result.blob.size) }) } + // A tab that only saves has what it came for: no player is drawn. + if (savingOnly && whole) return savedHere(file) busy('Starting the activity…') playerBox.hidden = false player.file = file @@ -572,6 +654,9 @@ async function start(): Promise { wanted = namedFiles(source) const written = source as { title?: unknown, type?: unknown } about(written.title, written.type) + // A tab that only saves fetches what the link gave addresses for first, and builds once. + const { linked } = savingOnly ? linksFor(wanted) : { linked: {} } + if (Object.keys(linked).length) return fetchThenBuild(linked) await play() } @@ -589,6 +674,15 @@ player.addEventListener('error', (event) => { fail('It was built, but does not play', (event as unknown as CustomEvent<{ message: string }>).detail.message, true) }) +savedStay.addEventListener('click', stay) +// The package that was saved, played in the tab that was kept open. +savedPlay.addEventListener('click', () => { + if (!kept) return + savingOnly = false + busy('Starting the activity…') + playerBox.hidden = false + player.file = kept +}) // The file field is hidden, and the buttons open it. choose.addEventListener('click', () => filesInput.click()) instead.addEventListener('click', () => filesInput.click()) From a9a100f8c44ffe43acf1f581472fd7cba5a63bfc Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Tue, 24 Nov 2026 13:10:03 +0200 Subject: [PATCH 2/2] fix(play): the saving tab counts to its closing only while it is looked at, and builds from files alone once an address is used or passed over (#104) --- README.md | 2 +- src/h5p/play/play-page.ts | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5cd16b0..f21b644 100644 --- a/README.md +++ b/README.md @@ -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 ``, 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, for a chat app to show in its reply (h5p-toolkit#210); no page links to it. It builds the activity in a link's `#source=` in the visitor's browser, as the build page does, and plays it with nothing beside it but its download and a link to the build page. A file of the visitor's own that the source names is chosen there or given by the page that frames it. A video or a recording can be given as its address: it plays from there at once and is fetched whole only for the download. A framing page that saves files itself is handed the download. A package bigger than that page saves stays behind the Download link, and the page says how to save it from such a frame. A framing page that opens links itself is handed the link to the build page. A link to the page may also carry the files' addresses and a wish to save (`&links=`, `&save=1`): in a tab of its own the page then draws no player: it fetches the files itself, builds the package, tries to save it with no press and closes the tab, or says to press Download where the browser saved nothing. That is how a framing page that opens links gets a package saved that it cannot save itself. | [src/h5p/play/](src/h5p/play/) | +| `/h5p/play/` | The play page, for a chat app to show in its reply (h5p-toolkit#210); no page links to it. It builds the activity in a link's `#source=` in the visitor's browser, as the build page does, and plays it with nothing beside it but its download and a link to the build page. A file of the visitor's own that the source names is chosen there or given by the page that frames it. A video or a recording can be given as its address: it plays from there at once and is fetched whole only for the download. A framing page that saves files itself is handed the download. A package bigger than that page saves stays behind the Download link, and the page says how to save it from such a frame. A framing page that opens links itself is handed the link to the build page. A link to the page may also carry the files' addresses and a wish to save (`&links=`, `&save=1`): in a tab of its own the page then draws no player: it fetches the files itself, builds the package and tries to save it with no press. Saved, it counts five seconds while it is looked at and then tries to close the tab; a tab the browser keeps open says to press Download where nothing was saved. That is how a framing page that opens links gets a package saved that it cannot save itself. | [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) | diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index 61e5532..390494c 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -44,14 +44,18 @@ import { baseName, compactSource, fragmentFromSource, fragmentWithLinks, kindOf, * * Such a tab is there to save a file, not to play one (#104): it draws no player, only the file * coming, the package being built, and that it is saved, and then it closes itself, which a tab - * with nothing behind it may do. It waits a few seconds first, and longer while something of the - * browser's own is over the page, as a question where to save is. "Keep this tab open" stops it, - * and the activity can then be played there. + * with nothing behind it may do. It waits a few seconds first, counted only while the page is seen + * and has the keyboard: a question of the browser's own, where to save or whether to, may be over + * it otherwise, or waiting in a tab nobody has looked at yet. "Keep this tab open" stops it, and + * the activity can then be played there. Such a tab builds from files only, never from an + * address: what it fetched, or what the person chose in its place. * - * What is seen is one stage, in one of five states (`data-state` on `main`): `building`, a loader - * over the place the player takes; `fetching`, the loader with how much of a file the parent has - * brought so far; `asking`, for the person's files; `error`, with the one thing to do about it; - * `ready`, the player. The parent is told each as a `status`, in words a screen reader is told too. + * What is seen is one stage, in one of six states (`data-state` on `main`): `building`, a loader + * over the place the player takes; `fetching`, the loader with how much of a file has come so far, + * from the parent or from its address; `asking`, for the person's files; `error`, with the one + * thing to do about it; `ready`, the player; `saved`, in a tab that only saves, the package saved + * and the tab about to close. The parent is told each as a `status`, in words a screen reader is + * told too. */ const $ = (id: string): T => document.getElementById(id) as T @@ -304,8 +308,12 @@ function take(files: File[]): void { // meanwhile, must not start the activity again under them, nor take the place of the file // the package to keep is being built from. if (whole || packing) return - // In a tab that only saves, a file chosen while the same one is fetched is the one built from. - if (savingOnly) fetchStop?.abort() + // In a tab that only saves, a file chosen while files are fetched ends the fetch, and with it + // the addresses: the package is built from files alone, and one still missing is asked for. + if (savingOnly) { + fetchStop?.abort() + links.clear() + } // 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) @@ -472,6 +480,8 @@ async function fetchThenBuild(from: Record): Promise { try { const files = await fetched(from, stop.signal, fetching) if (stop.signal.aborted) return + // The addresses are used up: a file still missing is asked for, not played from another's. + links.clear() take(files) } catch { if (stop.signal.aborted) return @@ -483,8 +493,9 @@ async function fetchThenBuild(from: Record): Promise { /** * The package saved in a tab that only saves, with no press, and the tab closed a few seconds - * later. The count stops while the page is seen and something else has the keyboard: a question - * of the browser's own, where to save or whether to, is then likely over it. + * later. The seconds are counted only while the page is seen and has the keyboard: otherwise a + * question of the browser's own, where to save or whether to, may be open over it, or waiting + * in a tab that was opened behind another. */ function savedHere(file: File): void { saveUnasked = false @@ -498,7 +509,7 @@ function savedHere(file: File): void { show('saved', `Your .h5p is saved: ${named} is in your downloads. This tab closes itself in a few seconds.`) clearInterval(closing) closing = setInterval(() => { - if (!document.hidden && !document.hasFocus()) return + if (document.hidden || !document.hasFocus()) return if (--left > 0) return said() // A tab the browser lets no page close stays, and says what it would have. stay()