Skip to content

Commit d3f90b4

Browse files
committed
fix(play): a package bigger than the framing page saves is still built and kept behind Download, and a framing page that opens links is handed them (#100)
1 parent 9c3d169 commit d3f90b4

3 files changed

Lines changed: 40 additions & 48 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The site of the [missing-elements](https://github.com/missing-elements) web comp
99
| `/` | 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) |
1010
| `/pdfjs-viewer/` | The docs of `<pdfjs-viewer-element>`, with a live preview | [src/pdfjs-viewer/](src/pdfjs-viewer/) |
1111
| `/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/) |
12-
| `/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/) |
12+
| `/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 (one bigger than it saves stays the page's link, with how to save it from such a frame said), and a link to the build page, which a framing page that opens links itself is handed, for a chat app to show in its reply (h5p-toolkit#210); no page links to it | [src/h5p/play/](src/h5p/play/) |
1313
| `/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/) |
1414
| `/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) |
1515
| `/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) |

‎src/h5p/play/index.njk‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
6363
<div class="play-progress" id="keep-progress" role="progressbar" aria-label="Getting the file for the download" aria-valuemin="0" aria-valuemax="100" data-indeterminate><span id="keep-bar"></span></div>
6464
</div>
6565
<button type="button" class="button" id="keep-instead">Choose it from your computer instead</button>
66-
{#- Where the page that frames this one saves files and cannot save this one: the build page, in a tab of its own, can. -#}
67-
<a class="button" id="keep-open" target="_blank" rel="noopener" hidden>Open in the build page</a>
6866
</div>
6967
</main>
7068
<script type="module" src="/h5p/play/play-page.js"></script>

‎src/h5p/play/play-page.ts‎

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import { baseName, kindOf, matchFiles, matchNamed, namedFiles, packageName, pars
2727
* link with nothing said (ChatGPT, 2026-11-23), while the card itself could ask the chat to save a
2828
* file. So a parent that saves files says so (#98), with the most it takes, and Download then hands
2929
* it the package, numbered, and says under the stage how the parent says that one went. A package
30-
* bigger than that is not handed over, and a video that would make one is not fetched for it: the
31-
* build page, in a tab of its own, is offered, where the person chooses the file and downloads as
32-
* anywhere.
30+
* bigger than that, or one the parent did not save, is still built here and stays behind Download:
31+
* the browser's own menu on a link was found to work in such a frame (right-click, open in a new
32+
* tab), so that is said under the stage, and nobody is sent out of the chat to choose the video
33+
* again (#100). A link of this page opens nothing there on a press, so a parent that opens links
34+
* says so too, and is handed the address.
3335
*
3436
* What is seen is one stage, in one of five states (`data-state` on `main`): `building`, a loader
3537
* over the place the player takes; `fetching`, the loader with how much of a file the parent has
@@ -65,7 +67,6 @@ const keepLabel = $('keep-label')
6567
const keepProgress = $('keep-progress')
6668
const keepBar = $('keep-bar')
6769
const keepInstead = $<HTMLButtonElement>('keep-instead')
68-
const keepOpen = $<HTMLAnchorElement>('keep-open')
6970
const announcer = document.querySelector<HTMLElement>('[data-status="page"]')
7071
const edit = $<HTMLAnchorElement>('edit')
7172
const player = $('player') as HTMLElementTagNameMap['h5p-player']
@@ -107,9 +108,8 @@ let handed = false
107108
/** 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. */
108109
let handedId = 0
109110
let handedTimer: ReturnType<typeof setTimeout> | undefined
110-
/** The address each file of the preview plays from, by its path in the source, and the sizes the parent gave, by address. */
111-
let shownFrom: Record<string, string> = {}
112-
const sizeAt = new Map<string, number>()
111+
/** Whether the parent opens links itself, where a press on one of this page's opens nothing. */
112+
let hostOpens = false
113113
/** Shows the question for the person's files a moment late, where the parent may bring them first. */
114114
let askTimer: ReturnType<typeof setTimeout> | undefined
115115

@@ -160,7 +160,7 @@ function about(name: unknown, type: unknown, size?: number | string): void {
160160
}
161161

162162
// The build page, with the same source, where its problems can be fixed and its files chosen.
163-
edit.href = fix.href = keepOpen.href = `/h5p/build/${location.hash}`
163+
edit.href = fix.href = `/h5p/build/${location.hash}`
164164

165165
// The page's height for the parent, which sizes its frame to it.
166166
if (framed) new ResizeObserver(() => toParent({ type: 'height', height: Math.ceil(document.documentElement.getBoundingClientRect().height) })).observe(document.body)
@@ -281,7 +281,6 @@ const previewedAs = (): string => previewed.length === 1 ? called(previewed[0]!)
281281
function preparing(text: string, choice: boolean): void {
282282
keepBox.hidden = false
283283
keepInstead.hidden = !choice
284-
keepOpen.hidden = true
285284
keepLabel.textContent = text
286285
keepProgress.hidden = false
287286
keepProgress.dataset.indeterminate = ''
@@ -290,11 +289,10 @@ function preparing(text: string, choice: boolean): void {
290289
downloadLabel.textContent = 'Preparing…'
291290
}
292291

293-
/** 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. */
294-
function saying(text: string, { busy = false, open = false } = {}): void {
292+
/** The row under the stage for a download the parent makes: what it is doing, or how it went. */
293+
function saying(text: string, { busy = false } = {}): void {
295294
keepBox.hidden = false
296295
keepInstead.hidden = true
297-
keepOpen.hidden = !open
298296
keepLabel.textContent = text
299297
keepProgress.hidden = !busy
300298
if (busy) {
@@ -307,17 +305,18 @@ function saying(text: string, { busy = false, open = false } = {}): void {
307305
}
308306

309307
/**
310-
* A package the parent cannot save: the build page can, with the person's files chosen there.
311-
* Before it is built, its size is at least its files': said as "over".
308+
* The way to the package where the parent does not save it and its frame may let this page save
309+
* nothing: Download is a link to it, and the browser's own menu opens a link from any frame.
312310
*/
313-
function tooBig(size: number, atLeast = false): void {
314-
const files = wanted.length ? `choose your ${wanted.length === 1 ? called(wanted[0]!) : 'files'} there, ` : ''
315-
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 })
311+
const BY_HAND = 'To save it, right-click Download, or press and hold it, and open the link in a new tab.'
312+
313+
/** A package bigger than the parent saves: the browser is left to save it, and the way is said where it may not. */
314+
function tooBig(size: number): void {
315+
saying(`This .h5p is ${sized(size)}, more than the chat saves with one press. ${BY_HAND}`)
316316
}
317317

318318
/** The package handed to a parent that saves files itself, which says how that went; no word in a minute is taken as no. */
319319
function handOver(file: File): void {
320-
if (hostSaves !== null && file.size > hostSaves) return tooBig(file.size)
321320
handed = true
322321
saying('The chat is saving the .h5p…', { busy: true })
323322
toParent({ type: 'save', id: ++handedId, file })
@@ -329,22 +328,11 @@ function handOver(file: File): void {
329328
function saveDone(ok: boolean): void {
330329
clearTimeout(handedTimer)
331330
handed = false
332-
if (!ok) return saying('The chat did not save it. Open it in the build page, and download it there.', { open: true })
331+
if (!ok) return saying(`The chat did not save it. ${BY_HAND}`)
333332
saying('Saved. Look in your downloads.')
334333
handedTimer = setTimeout(() => { keepBox.hidden = true }, 8000)
335334
}
336335

337-
/** What the files of a preview weigh, where the parent said for each: the package is that and a little. */
338-
function previewSize(): number | null {
339-
let total = 0
340-
for (const path of previewed) {
341-
const size = sizeAt.get(shownFrom[path] ?? '')
342-
if (size === undefined) return null
343-
total += size
344-
}
345-
return total
346-
}
347-
348336
/**
349337
* Download of a preview: the parent is asked for the bytes of what it plays from an address, and
350338
* how much has come is shown under the stage while the activity plays on. The person can choose
@@ -410,8 +398,8 @@ async function keep(): Promise<void> {
410398
about(result.manifest.title, result.builtWith, result.blob.size)
411399
toParent({ type: 'built', file: kept, title: result.manifest.title, size: sized(result.blob.size) })
412400
if (!asked) return
413-
// The download the person asked for: made by a parent that saves files, or by this page.
414-
if (hostSaves !== null) return handOver(kept)
401+
// The download the person asked for: made by a parent that saves files, where it takes this one, or by this page.
402+
if (hostSaves !== null && kept.size <= hostSaves) return handOver(kept)
415403
announce(`The .h5p is ready, ${sized(result.blob.size)}: downloading.`)
416404
download.click()
417405
} catch (error) {
@@ -445,7 +433,6 @@ async function play(): Promise<void> {
445433
if (packageUrl) URL.revokeObjectURL(packageUrl)
446434
const file = new File([result.blob], name, { type: 'application/zip' })
447435
previewed = result.linked
448-
shownFrom = linked
449436
download.hidden = false
450437
if (previewed.length) {
451438
// A preview is not a file to hand out: its address expires. Download fetches the bytes first.
@@ -506,19 +493,27 @@ choose.addEventListener('click', () => filesInput.click())
506493
instead.addEventListener('click', () => filesInput.click())
507494
keepInstead.addEventListener('click', () => filesInput.click())
508495
// Download of a preview fetches the files first; of a package with its files in it, it is a link,
509-
// or, under a parent that saves files itself, the package handed to it.
496+
// or, under a parent that saves files itself, the package handed to it. One bigger than that
497+
// parent saves stays the link, with the way to save it said where the frame saves nothing.
510498
download.addEventListener('click', (event) => {
511499
if (previewed.length) {
512500
event.preventDefault()
513-
// A video whose package the parent could not save is not fetched for nothing.
514-
const size = hostSaves !== null && !keeping ? previewSize() : null
515-
if (size !== null && size > hostSaves!) return tooBig(size, true)
516501
return fetchToKeep()
517502
}
518503
if (hostSaves === null || !kept) return
504+
if (kept.size > hostSaves) return tooBig(kept.size)
519505
event.preventDefault()
520506
if (!handed) handOver(kept)
521507
})
508+
// A link of this page, under a parent that opens links itself: the parent is handed its address.
509+
// A press with a key held, or the browser's own menu, is left to the browser.
510+
document.addEventListener('click', (event) => {
511+
if (!hostOpens || event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
512+
const link = event.target instanceof Element ? event.target.closest<HTMLAnchorElement>('a[target="_blank"]') : null
513+
if (!link?.href) return
514+
event.preventDefault()
515+
toParent({ type: 'open', href: link.href })
516+
})
522517
filesInput.addEventListener('change', () => {
523518
const files = [...(filesInput.files ?? [])]
524519
filesInput.value = ''
@@ -547,14 +542,11 @@ const amount = (value: unknown): number | null => typeof value === 'number' && N
547542
* video or a recording, and the activity built with them where a file of its own is still missing.
548543
* While a preview plays they are only kept: a new address for the same file is not a new activity.
549544
*/
550-
function takeLinks(given: Record<string, unknown>, sizes: Record<string, unknown>): void {
545+
function takeLinks(given: Record<string, unknown>): void {
551546
let added = false
552547
for (const [name, address] of Object.entries(given)) {
553548
if (typeof address !== 'string' || !/^https?:\/\//i.test(address) || !URL.canParse(address) || !playable(name)) continue
554-
// What the file there weighs, where the parent knows: a download is not begun that cannot be saved.
555-
const size = amount(sizes[name])
556-
if (size !== null) sizeAt.set(address, size)
557-
// The same address again, as with its size once that is known, is nothing new to build with.
549+
// The same address again is nothing new to build with.
558550
if (links.get(baseName(name)) === address) continue
559551
links.set(baseName(name), address)
560552
added = true
@@ -564,14 +556,16 @@ function takeLinks(given: Record<string, unknown>, sizes: Record<string, unknown
564556

565557
// From the frame's parent, which may hold what the person attached in the chat: the files, how
566558
// much of one has come so far, and the chat's theme where it is not the system's. And from one
567-
// that saves files itself: that it does, with the most it takes, and how a save went.
559+
// that saves files itself: that it does, with the most it takes, and how a save went; and from
560+
// one that opens links itself, that it does.
568561
window.addEventListener('message', (event) => {
569562
if (!framed || event.source !== window.parent) return
570-
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
563+
const message = event.data as { source?: unknown, type?: unknown, files?: unknown, links?: unknown, name?: unknown, received?: unknown, total?: unknown, theme?: unknown, max?: unknown, id?: unknown, ok?: unknown } | null
571564
if (message?.source !== 'h5p-play-host') return
572565
if (message.type === 'files' && Array.isArray(message.files)) take(message.files.filter((file): file is File => file instanceof File))
573-
else if (message.type === 'links' && message.links && typeof message.links === 'object') takeLinks(message.links as Record<string, unknown>, message.sizes && typeof message.sizes === 'object' ? message.sizes as Record<string, unknown> : {})
566+
else if (message.type === 'links' && message.links && typeof message.links === 'object') takeLinks(message.links as Record<string, unknown>)
574567
else if (message.type === 'saves') hostSaves = amount(message.max) ?? Infinity
568+
else if (message.type === 'opens') hostOpens = true
575569
else if (message.type === 'saved' && handedId && message.id === handedId && typeof message.ok === 'boolean' && (handed || message.ok)) saveDone(message.ok)
576570
else if (message.type === 'progress' && amount(message.received) !== null) fetching(typeof message.name === 'string' ? message.name : '', amount(message.received)!, amount(message.total) ?? 0)
577571
else if (message.type === 'theme' && (message.theme === 'light' || message.theme === 'dark')) document.querySelector<HTMLMetaElement>('meta[name="color-scheme"]')!.content = message.theme

0 commit comments

Comments
 (0)