Skip to content

Commit 96fb2f3

Browse files
authored
Merge pull request #134 from missing-elements/feat/several-attachments
feat(play): say which of several files from the chat is coming
2 parents 9eb02ed + d3fae69 commit 96fb2f3

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,21 +374,23 @@ 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
389+
progress.setAttribute('aria-label', count > 1 ? `Getting file ${index} of ${count}` : 'Getting the file')
388390
progress.setAttribute('aria-valuenow', String(done))
389391
progressBar.style.setProperty('--done', `${done}%`)
390392
instead.hidden = false
391-
if (main.dataset.state !== 'fetching') show('fetching', `Getting your ${what} from the chat.`)
393+
if (main.dataset.state !== 'fetching') show('fetching', count > 1 ? `Getting your ${count} files from the chat.` : `Getting your ${what} from the chat.`)
392394
}
393395

394396
/** Files given, by the person (`mine`) or by the parent: the activity is built once none is missing. */
@@ -539,9 +541,10 @@ function fetchToKeep(): void {
539541
* Files fetched by this page from their addresses, by their paths in the source, counted as they
540542
* come, ten times a second at most. The browser keeps each as a Blob, which it may hold on disk.
541543
*/
542-
async function fetched(from: Record<string, string>, signal: AbortSignal, count: (name: string, received: number, total: number) => void): Promise<File[]> {
544+
async function fetched(from: Record<string, string>, signal: AbortSignal, count: (name: string, received: number, total: number, index: number, of: number) => void): Promise<File[]> {
543545
const files: File[] = []
544-
for (const [path, address] of Object.entries(from)) {
546+
const entries = Object.entries(from)
547+
for (const [index, [path, address]] of entries.entries()) {
545548
const response = await fetch(address, { signal })
546549
if (!response.ok || !response.body) throw new Error(`the chat answered ${response.status}`)
547550
const total = Number(response.headers.get('content-length')) || 0
@@ -550,7 +553,7 @@ async function fetched(from: Record<string, string>, signal: AbortSignal, count:
550553
const counted = response.body.pipeThrough(new TransformStream<Uint8Array, Uint8Array>({
551554
transform(chunk, controller) {
552555
received += chunk.byteLength
553-
if (performance.now() - shown > 100) { shown = performance.now(); count(baseName(path), received, total) }
556+
if (performance.now() - shown > 100) { shown = performance.now(); count(baseName(path), received, total, index + 1, entries.length) }
554557
controller.enqueue(chunk)
555558
},
556559
}))
@@ -665,10 +668,11 @@ function stay(): void {
665668
}
666669

667670
/** How much of a file for the download has come. */
668-
function keepCount(name: string, received: number, total: number): void {
671+
function keepCount(name: string, received: number, total: number, index = 1, count = 1): void {
669672
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)}` : ''}`
673+
const what = count > 1 ? 'files' : previewed.length === 1 ? called(name || previewed[0]!) : 'file'
674+
keepLabel.textContent = `Getting your ${what} for the download…${count > 1 ? ` ${index} of ${count},` : ''} ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
675+
keepProgress.setAttribute('aria-label', count > 1 ? `Getting file ${index} of ${count} for the download` : 'Getting the file for the download')
672676
if (total > 0) {
673677
const done = Math.min(100, Math.round(received / total * 100))
674678
delete keepProgress.dataset.indeterminate
@@ -951,15 +955,15 @@ function takeLinks(given: Record<string, unknown>, sizes: Record<string, unknown
951955
// one that opens links itself, that it does. And that a file it was asked for did not come.
952956
window.addEventListener('message', (event) => {
953957
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
958+
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
955959
if (message?.source !== 'h5p-play-host') return
956960
if (message.type === 'files' && Array.isArray(message.files)) take(message.files.filter((file): file is File => file instanceof File))
957961
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> : {})
958962
else if (message.type === 'failed') failed()
959963
else if (message.type === 'saves') hostSaves = amount(message.max) ?? Infinity
960964
else if (message.type === 'opens') hostOpens = true
961965
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)
966+
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)
963967
else if (message.type === 'theme' && (message.theme === 'light' || message.theme === 'dark')) document.querySelector<HTMLMetaElement>('meta[name="color-scheme"]')!.content = message.theme
964968
})
965969

0 commit comments

Comments
 (0)