Skip to content

Commit be732ae

Browse files
committed
feat(play): say which of several files from the chat is coming (missing-elements/h5p-toolkit#301)
1 parent 9eb02ed commit be732ae

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,15 @@ function showAsk(listed: string, one: boolean): void {
374374
* has come, drawn as a bar where its size is known. The person can still choose the file from
375375
* their computer, which needs no wait. Told a screen reader once, not with every number.
376376
*/
377-
function fetching(name: string, received: number, total: number): void {
378-
if (keeping) return keepCount(name, received, total)
377+
function fetching(name: string, received: number, total: number, index = 1, count = 1): void {
378+
if (keeping) return keepCount(name, received, total, index, count)
379379
// Asked for, the question shown or still held back a moment: the file on its way is shown in its place.
380380
if (main.dataset.state !== 'asking' && main.dataset.state !== 'fetching' && askTimer === undefined) return
381381
clearTimeout(askTimer)
382382
askTimer = undefined
383-
const what = wanted.length === 1 ? called(name || wanted[0]!) : 'file'
384-
label.textContent = `Getting your ${what}… ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
383+
// Several come one after another (h5p-toolkit#301): which one this is, and its own bytes.
384+
const what = count > 1 ? 'files' : wanted.length === 1 ? called(name || wanted[0]!) : 'file'
385+
label.textContent = `Getting your ${what}…${count > 1 ? ` ${index} of ${count},` : ''} ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
385386
note.textContent = 'From the chat to your browser, nowhere else.'
386387
const done = total > 0 ? Math.min(100, Math.round(received / total * 100)) : 0
387388
progress.hidden = total <= 0
@@ -665,10 +666,10 @@ function stay(): void {
665666
}
666667

667668
/** How much of a file for the download has come. */
668-
function keepCount(name: string, received: number, total: number): void {
669+
function keepCount(name: string, received: number, total: number, index = 1, count = 1): void {
669670
if (!bringing) return
670-
const what = previewed.length === 1 ? called(name || previewed[0]!) : 'file'
671-
keepLabel.textContent = `Getting your ${what} for the download… ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
671+
const what = count > 1 ? 'files' : previewed.length === 1 ? called(name || previewed[0]!) : 'file'
672+
keepLabel.textContent = `Getting your ${what} for the download…${count > 1 ? ` ${index} of ${count},` : ''} ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
672673
if (total > 0) {
673674
const done = Math.min(100, Math.round(received / total * 100))
674675
delete keepProgress.dataset.indeterminate
@@ -951,15 +952,15 @@ function takeLinks(given: Record<string, unknown>, sizes: Record<string, unknown
951952
// one that opens links itself, that it does. And that a file it was asked for did not come.
952953
window.addEventListener('message', (event) => {
953954
if (!framed || event.source !== window.parent) return
954-
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
955+
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
955956
if (message?.source !== 'h5p-play-host') return
956957
if (message.type === 'files' && Array.isArray(message.files)) take(message.files.filter((file): file is File => file instanceof File))
957958
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> : {})
958959
else if (message.type === 'failed') failed()
959960
else if (message.type === 'saves') hostSaves = amount(message.max) ?? Infinity
960961
else if (message.type === 'opens') hostOpens = true
961962
else if (message.type === 'saved' && handedId && message.id === handedId && typeof message.ok === 'boolean' && (handed || message.ok)) saveDone(message.ok)
962-
else if (message.type === 'progress' && amount(message.received) !== null) fetching(typeof message.name === 'string' ? message.name : '', amount(message.received)!, amount(message.total) ?? 0)
963+
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)
963964
else if (message.type === 'theme' && (message.theme === 'light' || message.theme === 'dark')) document.querySelector<HTMLMetaElement>('meta[name="color-scheme"]')!.content = message.theme
964965
})
965966

0 commit comments

Comments
 (0)