Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/h5p/play/play-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,23 @@ function showAsk(listed: string, one: boolean): void {
* has come, drawn as a bar where its size is known. The person can still choose the file from
* their computer, which needs no wait. Told a screen reader once, not with every number.
*/
function fetching(name: string, received: number, total: number): void {
if (keeping) return keepCount(name, received, total)
function fetching(name: string, received: number, total: number, index = 1, count = 1): void {
if (keeping) return keepCount(name, received, total, index, count)
// Asked for, the question shown or still held back a moment: the file on its way is shown in its place.
if (main.dataset.state !== 'asking' && main.dataset.state !== 'fetching' && askTimer === undefined) return
clearTimeout(askTimer)
askTimer = undefined
const what = wanted.length === 1 ? called(name || wanted[0]!) : 'file'
label.textContent = `Getting your ${what}… ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
// Several come one after another (h5p-toolkit#301): which one this is, and its own bytes.
const what = count > 1 ? 'files' : wanted.length === 1 ? called(name || wanted[0]!) : 'file'
label.textContent = `Getting your ${what}…${count > 1 ? ` ${index} of ${count},` : ''} ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
note.textContent = 'From the chat to your browser, nowhere else.'
const done = total > 0 ? Math.min(100, Math.round(received / total * 100)) : 0
progress.hidden = total <= 0
progress.setAttribute('aria-label', count > 1 ? `Getting file ${index} of ${count}` : 'Getting the file')
progress.setAttribute('aria-valuenow', String(done))
progressBar.style.setProperty('--done', `${done}%`)
instead.hidden = false
if (main.dataset.state !== 'fetching') show('fetching', `Getting your ${what} from the chat.`)
if (main.dataset.state !== 'fetching') show('fetching', count > 1 ? `Getting your ${count} files from the chat.` : `Getting your ${what} from the chat.`)
}

/** Files given, by the person (`mine`) or by the parent: the activity is built once none is missing. */
Expand Down Expand Up @@ -539,9 +541,10 @@ function fetchToKeep(): void {
* 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<string, string>, signal: AbortSignal, count: (name: string, received: number, total: number) => void): Promise<File[]> {
async function fetched(from: Record<string, string>, signal: AbortSignal, count: (name: string, received: number, total: number, index: number, of: number) => void): Promise<File[]> {
const files: File[] = []
for (const [path, address] of Object.entries(from)) {
const entries = Object.entries(from)
for (const [index, [path, address]] of entries.entries()) {
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
Expand All @@ -550,7 +553,7 @@ async function fetched(from: Record<string, string>, signal: AbortSignal, count:
const counted = response.body.pipeThrough(new TransformStream<Uint8Array, Uint8Array>({
transform(chunk, controller) {
received += chunk.byteLength
if (performance.now() - shown > 100) { shown = performance.now(); count(baseName(path), received, total) }
if (performance.now() - shown > 100) { shown = performance.now(); count(baseName(path), received, total, index + 1, entries.length) }
controller.enqueue(chunk)
},
}))
Expand Down Expand Up @@ -665,10 +668,11 @@ function stay(): void {
}

/** How much of a file for the download has come. */
function keepCount(name: string, received: number, total: number): void {
function keepCount(name: string, received: number, total: number, index = 1, count = 1): void {
if (!bringing) return
const what = previewed.length === 1 ? called(name || previewed[0]!) : 'file'
keepLabel.textContent = `Getting your ${what} for the download… ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
const what = count > 1 ? 'files' : previewed.length === 1 ? called(name || previewed[0]!) : 'file'
keepLabel.textContent = `Getting your ${what} for the download…${count > 1 ? ` ${index} of ${count},` : ''} ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
keepProgress.setAttribute('aria-label', count > 1 ? `Getting file ${index} of ${count} for the download` : 'Getting the file for the download')
if (total > 0) {
const done = Math.min(100, Math.round(received / total * 100))
delete keepProgress.dataset.indeterminate
Expand Down Expand Up @@ -951,15 +955,15 @@ function takeLinks(given: Record<string, unknown>, sizes: Record<string, unknown
// one that opens links itself, that it does. And that a file it was asked for did not come.
window.addEventListener('message', (event) => {
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, id?: 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, index?: unknown, count?: 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<string, unknown>, message.sizes && typeof message.sizes === 'object' ? message.sizes as Record<string, unknown> : {})
else if (message.type === 'failed') failed()
else if (message.type === 'saves') hostSaves = amount(message.max) ?? Infinity
else if (message.type === 'opens') hostOpens = true
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 === 'progress' && amount(message.received) !== null) fetching(typeof message.name === 'string' ? message.name : '', amount(message.received)!, amount(message.total) ?? 0, amount(message.index) || 1, amount(message.count) || 1)
else if (message.type === 'theme' && (message.theme === 'light' || message.theme === 'dark')) document.querySelector<HTMLMetaElement>('meta[name="color-scheme"]')!.content = message.theme
})

Expand Down