From 955686a7656dd9d71f6c0f9006388be8fe2adc5c Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Mon, 23 Nov 2026 23:21:10 +0200 Subject: [PATCH 1/3] feat(play): a framing page that saves files is handed the package by Download (#98) Inside a chat's card the page's own Download saved nothing: the chat lets the frame save no file, and drops the press unsaid. The card can ask the chat to save one. So a framing page says it saves files, with the most it takes ({ type: 'saves', max }), and Download hands it the package ({ type: 'save', file }) where the page would have saved it; the framing page says how it went ({ type: 'saved', ok }), which the row under the stage says: saving, saved, or not saved, with the build page to open. No word in a minute is taken as not saved. A package above max is not handed over, and a video that would make one is not fetched for it where the framing page gave its size (sizes, beside links): the row says the size and offers the build page, where the person chooses the file and downloads as anywhere. With no 'saves', everything is as it was. --- README.md | 2 +- src/h5p/play/index.njk | 5 +- src/h5p/play/play-page.ts | 121 +++++++++++++++++++++++++++++++++----- 3 files changed, 112 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 045d668..13d48fc 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: 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 or given by the page that frames it (a video or a recording as its address, played from there at once and fetched whole only for the download), 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 or given by the page that frames it (a video or a recording as its address, played from there at once and fetched whole only for the download), and played with nothing beside it but its download, which a framing page that saves files itself is handed, 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) | diff --git a/src/h5p/play/index.njk b/src/h5p/play/index.njk index de3850c..9b97d60 100644 --- a/src/h5p/play/index.njk +++ b/src/h5p/play/index.njk @@ -55,13 +55,16 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da

- {#- Download of an activity whose video plays from the chat: its bytes come first, while it plays on. -#} + {#- What a download is doing, where it is not one press: an activity whose video plays from the chat has its bytes + brought first, while it plays on, and a page that frames this one may save the file itself. -#} diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index e0b961f..bf02582 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -23,6 +23,13 @@ import { baseName, kindOf, matchFiles, matchNamed, namedFiles, packageName, pars * Download asks the parent for the bytes, shows them coming under the stage while the activity plays * on, and builds the package to keep with the file in it. * + * A frame may be let save nothing: a chat's card was found to drop the download of this page's own + * link with nothing said (ChatGPT, 2026-11-23), while the card itself could ask the chat to save a + * file. So a parent that saves files says so (#98), with the most it takes, and Download then hands + * it the package and says under the stage how that went. A package bigger than that is not handed + * over, and a video that would make one is not fetched for it: the build page, in a tab of its + * own, is offered, where the person chooses the file and downloads as anywhere. + * * 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; @@ -57,6 +64,7 @@ const keepLabel = $('keep-label') const keepProgress = $('keep-progress') const keepBar = $('keep-bar') const keepInstead = $('keep-instead') +const keepOpen = $('keep-open') const announcer = document.querySelector('[data-status="page"]') const edit = $('edit') const player = $('player') as HTMLElementTagNameMap['h5p-player'] @@ -89,6 +97,18 @@ let bringing = false let packing = false /** Whether the package that plays has its files in it. Files that come after that change nothing. */ let whole = false +/** That package, as a file, for a parent that saves it. */ +let kept: File | null = null +/** The most bytes the parent saves of a file it is handed, where it saves files itself; null where this page does. */ +let hostSaves: number | null = null +/** Whether a file was handed to the parent to save, and it has not said how that went. */ +let handed = false +/** Whether one ever was: a yes that comes after the wait for it ended is still said. */ +let everHanded = false +let handedTimer: ReturnType | undefined +/** The address each file of the preview plays from, by its path in the source, and the sizes the parent gave, by address. */ +let shownFrom: Record = {} +const sizeAt = new Map() /** Shows the question for the person's files a moment late, where the parent may bring them first. */ let askTimer: ReturnType | undefined @@ -139,7 +159,7 @@ function about(name: unknown, type: unknown, size?: number | string): void { } // The build page, with the same source, where its problems can be fixed and its files chosen. -edit.href = fix.href = `/h5p/build/${location.hash}` +edit.href = fix.href = keepOpen.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) @@ -260,6 +280,7 @@ const previewedAs = (): string => previewed.length === 1 ? called(previewed[0]!) function preparing(text: string, choice: boolean): void { keepBox.hidden = false keepInstead.hidden = !choice + keepOpen.hidden = true keepLabel.textContent = text keepProgress.hidden = false keepProgress.dataset.indeterminate = '' @@ -268,6 +289,61 @@ function preparing(text: string, choice: boolean): void { downloadLabel.textContent = 'Preparing…' } +/** The row under the stage for a download the parent makes: what it is doing, or how it went, with the build page where that is the way on. */ +function saying(text: string, { busy = false, open = false } = {}): void { + keepBox.hidden = false + keepInstead.hidden = true + keepOpen.hidden = !open + keepLabel.textContent = text + keepProgress.hidden = !busy + if (busy) { + keepProgress.dataset.indeterminate = '' + keepProgress.removeAttribute('aria-valuenow') + } + download[busy ? 'setAttribute' : 'removeAttribute']('aria-disabled', 'true') + downloadLabel.textContent = busy ? 'Saving…' : 'Download .h5p' + announce(text) +} + +/** + * A package the parent cannot save: the build page can, with the person's files chosen there. + * Before it is built, its size is at least its files': said as "over". + */ +function tooBig(size: number, atLeast = false): void { + const files = wanted.length ? `choose your ${wanted.length === 1 ? called(wanted[0]!) : 'files'} there, ` : '' + saying(`${atLeast ? `With your ${previewedAs()} in it, this .h5p is over` : 'This .h5p is'} ${sized(size)}, more than the chat can save from here. Open it in the build page, ${files}and download it.`, { open: true }) +} + +/** The package handed to a parent that saves files itself, which says how that went; no word in a minute is taken as no. */ +function handOver(file: File): void { + if (hostSaves !== null && file.size > hostSaves) return tooBig(file.size) + handed = everHanded = true + saying('The chat is saving the .h5p…', { busy: true }) + toParent({ type: 'save', file }) + clearTimeout(handedTimer) + handedTimer = setTimeout(() => saveDone(false), 60_000) +} + +/** How the parent's save went. A yes that comes after the minute is still said. */ +function saveDone(ok: boolean): void { + clearTimeout(handedTimer) + handed = false + if (!ok) return saying('The chat did not save it. Open it in the build page, and download it there.', { open: true }) + saying('Saved. Look in your downloads.') + handedTimer = setTimeout(() => { keepBox.hidden = true }, 8000) +} + +/** What the files of a preview weigh, where the parent said for each: the package is that and a little. */ +function previewSize(): number | null { + let total = 0 + for (const path of previewed) { + const size = sizeAt.get(shownFrom[path] ?? '') + if (size === undefined) return null + total += size + } + return total +} + /** * Download of a preview: the parent is asked for the bytes of what it plays from an address, and * how much has come is shown under the stage while the activity plays on. The person can choose @@ -319,6 +395,7 @@ async function keep(): Promise { const name = packageName(String(result.manifest.title ?? '')) if (packageUrl) URL.revokeObjectURL(packageUrl) packageUrl = URL.createObjectURL(result.blob) + kept = new File([result.blob], name, { type: 'application/zip' }) // Download may have been pressed while this was built. const asked = keeping previewed = [] @@ -330,11 +407,11 @@ async function keep(): Promise { download.removeAttribute('aria-disabled') downloadLabel.textContent = 'Download .h5p' about(result.manifest.title, result.builtWith, result.blob.size) - toParent({ type: 'built', file: new File([result.blob], name, { type: 'application/zip' }), title: result.manifest.title, size: sized(result.blob.size) }) + toParent({ type: 'built', file: kept, title: result.manifest.title, size: sized(result.blob.size) }) if (!asked) return + // The download the person asked for: made by a parent that saves files, or by this page. + if (hostSaves !== null) return handOver(kept) announce(`The .h5p is ready, ${sized(result.blob.size)}: downloading.`) - // The download the person asked for. A frame its parent lets download nothing has given the - // parent the file above, to save its own way. download.click() } catch (error) { // Unasked, nothing is said: the preview plays on, and Download builds from the same files. @@ -367,6 +444,7 @@ async function play(): Promise { if (packageUrl) URL.revokeObjectURL(packageUrl) const file = new File([result.blob], name, { type: 'application/zip' }) previewed = result.linked + shownFrom = linked download.hidden = false if (previewed.length) { // A preview is not a file to hand out: its address expires. Download fetches the bytes first. @@ -377,6 +455,7 @@ async function play(): Promise { about(result.manifest.title, result.builtWith, `${previewedAs() === 'files' ? 'its files play' : `its ${previewedAs()} plays`} from the chat`) } else { whole = true + kept = file packageUrl = URL.createObjectURL(result.blob) download.href = packageUrl download.download = name @@ -425,11 +504,19 @@ player.addEventListener('error', (event) => { choose.addEventListener('click', () => filesInput.click()) instead.addEventListener('click', () => filesInput.click()) keepInstead.addEventListener('click', () => filesInput.click()) -// Download of a preview fetches the files first; of a package with its files in it, it is a link. +// Download of a preview fetches the files first; of a package with its files in it, it is a link, +// or, under a parent that saves files itself, the package handed to it. download.addEventListener('click', (event) => { - if (!previewed.length) return + if (previewed.length) { + event.preventDefault() + // A video whose package the parent could not save is not fetched for nothing. + const size = hostSaves !== null && !keeping ? previewSize() : null + if (size !== null && size > hostSaves!) return tooBig(size, true) + return fetchToKeep() + } + if (hostSaves === null || !kept) return event.preventDefault() - fetchToKeep() + if (!handed) handOver(kept) }) filesInput.addEventListener('change', () => { const files = [...(filesInput.files ?? [])] @@ -451,32 +538,38 @@ main.addEventListener('drop', (event) => { take([...event.dataTransfer.files]) }) +/** A count of bytes as a message gives it: a number that is one, and not below nothing. */ +const amount = (value: unknown): number | null => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null + /** * Addresses the parent gives for files it holds, by name: kept where they are http(s) and name a * video or a recording, and the activity built with them where a file of its own is still missing. * While a preview plays they are only kept: a new address for the same file is not a new activity. */ -function takeLinks(given: Record): void { +function takeLinks(given: Record, sizes: Record): void { let added = false for (const [name, address] of Object.entries(given)) { if (typeof address !== 'string' || !/^https?:\/\//i.test(address) || !URL.canParse(address) || !playable(name)) continue links.set(baseName(name), address) + // What the file there weighs, where the parent knows: a download is not begun that cannot be saved. + const size = amount(sizes[name]) + if (size !== null) sizeAt.set(address, size) added = true } if (added && !previewed.length && filesFor(wanted).missing.length) void play() } -/** A count of bytes as a message gives it: a number that is one, and not below nothing. */ -const amount = (value: unknown): number | null => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null - // From the frame's parent, which may hold what the person attached in the chat: the files, how -// much of one has come so far, and the chat's theme where it is not the system's. +// much of one has come so far, and the chat's theme where it is not the system's. And from one +// that saves files itself: that it does, with the most it takes, and how a save went. window.addEventListener('message', (event) => { if (!framed || event.source !== window.parent) return - const message = event.data as { source?: unknown, type?: unknown, files?: unknown, links?: unknown, name?: unknown, received?: unknown, total?: unknown, theme?: unknown } | null + const message = event.data as { source?: unknown, type?: unknown, files?: unknown, links?: unknown, sizes?: unknown, name?: unknown, received?: unknown, total?: unknown, theme?: unknown, max?: unknown, ok?: unknown } | null if (message?.source !== 'h5p-play-host') return if (message.type === 'files' && Array.isArray(message.files)) take(message.files.filter((file): file is File => file instanceof File)) - else if (message.type === 'links' && message.links && typeof message.links === 'object') takeLinks(message.links as Record) + else if (message.type === 'links' && message.links && typeof message.links === 'object') takeLinks(message.links as Record, message.sizes && typeof message.sizes === 'object' ? message.sizes as Record : {}) + else if (message.type === 'saves') hostSaves = amount(message.max) ?? Infinity + else if (message.type === 'saved' && (handed || (everHanded && message.ok === true))) saveDone(message.ok !== false) else if (message.type === 'progress' && amount(message.received) !== null) fetching(typeof message.name === 'string' ? message.name : '', amount(message.received)!, amount(message.total) ?? 0) else if (message.type === 'theme' && (message.theme === 'light' || message.theme === 'dark')) document.querySelector('meta[name="color-scheme"]')!.content = message.theme }) From 6ad6cbb66c0d11165a546493706397163389fd3c Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Mon, 23 Nov 2026 23:21:58 +0200 Subject: [PATCH 2/3] fix(play): an address given again, as with its size, does not start the build again (#98) --- src/h5p/play/play-page.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index bf02582..d4471bf 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -550,10 +550,12 @@ function takeLinks(given: Record, sizes: Record Date: Mon, 23 Nov 2026 23:31:14 +0200 Subject: [PATCH 3/3] fix(play): a save is numbered, and only a yes or a no for the last one handed is taken (#98) --- src/h5p/play/play-page.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index d4471bf..4abdc79 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -26,9 +26,10 @@ import { baseName, kindOf, matchFiles, matchNamed, namedFiles, packageName, pars * A frame may be let save nothing: a chat's card was found to drop the download of this page's own * link with nothing said (ChatGPT, 2026-11-23), while the card itself could ask the chat to save a * file. So a parent that saves files says so (#98), with the most it takes, and Download then hands - * it the package and says under the stage how that went. A package bigger than that is not handed - * over, and a video that would make one is not fetched for it: the build page, in a tab of its - * own, is offered, where the person chooses the file and downloads as anywhere. + * it the package, numbered, and says under the stage how the parent says that one went. A package + * bigger than that is not handed over, and a video that would make one is not fetched for it: the + * build page, in a tab of its own, is offered, where the person chooses the file and downloads as + * anywhere. * * 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 @@ -103,8 +104,8 @@ let kept: File | null = null let hostSaves: number | null = null /** Whether a file was handed to the parent to save, and it has not said how that went. */ let handed = false -/** Whether one ever was: a yes that comes after the wait for it ended is still said. */ -let everHanded = false +/** The number of the last one handed, which the parent's word on it carries: word on an earlier one says nothing of this one. */ +let handedId = 0 let handedTimer: ReturnType | undefined /** The address each file of the preview plays from, by its path in the source, and the sizes the parent gave, by address. */ let shownFrom: Record = {} @@ -317,14 +318,14 @@ function tooBig(size: number, atLeast = false): void { /** The package handed to a parent that saves files itself, which says how that went; no word in a minute is taken as no. */ function handOver(file: File): void { if (hostSaves !== null && file.size > hostSaves) return tooBig(file.size) - handed = everHanded = true + handed = true saying('The chat is saving the .h5p…', { busy: true }) - toParent({ type: 'save', file }) + toParent({ type: 'save', id: ++handedId, file }) clearTimeout(handedTimer) handedTimer = setTimeout(() => saveDone(false), 60_000) } -/** How the parent's save went. A yes that comes after the minute is still said. */ +/** How the parent's save of the last package handed went. A yes that comes after the minute is still said. */ function saveDone(ok: boolean): void { clearTimeout(handedTimer) handed = false @@ -566,12 +567,12 @@ function takeLinks(given: Record, sizes: Record { if (!framed || event.source !== window.parent) return - const message = event.data as { source?: unknown, type?: unknown, files?: unknown, links?: unknown, sizes?: unknown, name?: unknown, received?: unknown, total?: unknown, theme?: unknown, max?: unknown, ok?: unknown } | null + const message = event.data as { source?: unknown, type?: unknown, files?: unknown, links?: unknown, sizes?: unknown, name?: unknown, received?: unknown, total?: unknown, theme?: unknown, max?: unknown, id?: unknown, ok?: unknown } | null if (message?.source !== 'h5p-play-host') return if (message.type === 'files' && Array.isArray(message.files)) take(message.files.filter((file): file is File => file instanceof File)) else if (message.type === 'links' && message.links && typeof message.links === 'object') takeLinks(message.links as Record, message.sizes && typeof message.sizes === 'object' ? message.sizes as Record : {}) else if (message.type === 'saves') hostSaves = amount(message.max) ?? Infinity - else if (message.type === 'saved' && (handed || (everHanded && message.ok === true))) saveDone(message.ok !== false) + else if (message.type === 'saved' && handedId && message.id === handedId && typeof message.ok === 'boolean' && (handed || message.ok)) saveDone(message.ok) else if (message.type === 'progress' && amount(message.received) !== null) fetching(typeof message.name === 'string' ? message.name : '', amount(message.received)!, amount(message.total) ?? 0) else if (message.type === 'theme' && (message.theme === 'light' || message.theme === 'dark')) document.querySelector('meta[name="color-scheme"]')!.content = message.theme })