Skip to content

Commit 1c20b2f

Browse files
authored
Merge pull request #95 from missing-elements/fix/build-files-by-kind
fix(build): a single file of a kind is taken whatever it is called, and a file that is not used is said (#94)
2 parents 0af17d4 + 7d39b3d commit 1c20b2f

5 files changed

Lines changed: 76 additions & 43 deletions

File tree

‎src/assets/site.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,9 @@ pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.9375re
10371037
white-space: nowrap;
10381038
}
10391039
.build .file-list code { white-space: normal; }
1040+
/* A chosen file that no path took, and why. */
1041+
.build .file-note { margin: 0.5rem 0 0; color: var(--muted); font-size: 0.9375rem; overflow-wrap: anywhere; }
1042+
.build .file-note[hidden] { display: none; }
10401043
/* Beside the name's first line: a ring to fill, then a check. */
10411044
.build .file-list li::before {
10421045
content: "";

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import '@missing-elements/h5p-offline-player'
22
import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser'
33
import { pack as installPack } from '@missing-elements/h5p-libraries/install'
44
import { runtime } from '@missing-elements/h5p-runtime'
5-
import { compactSource, forTheChat, fragmentFromSource, linksSource, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, typesNamed, type Problem, type TypeFile } from './build-source'
5+
import { baseName, compactSource, forTheChat, fragmentFromSource, kindOf, linksSource, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, typesNamed, type Problem, type TypeFile } from './build-source'
66
import type { SourceEditor } from './editor'
77
import { formatSource, jsonProblem, place, type Target } from './json-scan'
88

@@ -47,6 +47,7 @@ const openSource = $<HTMLButtonElement>('open-source')
4747
const filesStep = $('files-step')
4848
const fileList = $<HTMLUListElement>('file-list')
4949
const filesInput = $<HTMLInputElement>('files')
50+
const filesNote = $('files-note')
5051
const buildButton = $<HTMLButtonElement>('build')
5152
const buildNumber = $('build-number')
5253
const playNumber = $('play-number')
@@ -252,16 +253,25 @@ function number(withFiles: boolean): void {
252253
function updateFiles(): string[] {
253254
const parsed = parseSource(sourceField.value)
254255
const paths = 'source' in parsed ? namedFiles(parsed.source) : []
255-
const { files } = matchFiles(paths, [...chosen.values()])
256+
const { files, unused } = matchFiles(paths, [...chosen.values()])
256257
fileList.replaceChildren(...paths.map((path) => {
257258
const item = document.createElement('li')
258259
const name = document.createElement('code')
259260
name.textContent = path
260261
const file = files[path]
261262
item.dataset.chosen = String(Boolean(file))
262-
item.append(name, file ? ` chosen, ${sized(file.size)}` : ' not chosen yet')
263+
// A file taken under another name than the source's is named, so that it is seen which one it is.
264+
item.append(name, !file ? ' not chosen yet' : file.name === baseName(path) ? ` chosen, ${sized(file.size)}` : ` chosen: ${file.name}, ${sized(file.size)}`)
263265
return item
264266
}))
267+
// A chosen file that no path took is said, with why: picking it would otherwise seem to do nothing.
268+
const said = (files: File[], why: string): string => files.length ? `${new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(files.map((file) => file.name))} ${files.length === 1 ? 'is' : 'are'} not used: ${why}` : ''
269+
const fits = (file: File): boolean => paths.some((path) => kindOf(path) === kindOf(file.name))
270+
filesNote.textContent = [
271+
said(unused.filter(fits), 'with more than one file of that kind, each has to be called as the source calls it.'),
272+
said(unused.filter((file) => !fits(file)), 'the source names no file of that kind.')
273+
].filter(Boolean).join(' ')
274+
filesNote.hidden = !unused.length
265275
number(paths.length > 0)
266276
return paths
267277
}
@@ -451,7 +461,17 @@ openSource.addEventListener('click', () => sourceFile.click())
451461
sourceLabel.addEventListener('click', () => editor?.focus())
452462

453463
filesInput.addEventListener('change', () => {
454-
for (const file of filesInput.files ?? []) chosen.set(file.name, file)
464+
const picked = [...(filesInput.files ?? [])]
465+
// The last file picked for the one path of its kind is its file, whatever each is called: those
466+
// picked for it before go, as a second choice replaces the first where the names are the source's.
467+
const paths = updateFiles()
468+
const named = new Set(paths.map(baseName))
469+
for (const file of picked) {
470+
const same = paths.filter((path) => kindOf(path) === kindOf(file.name))
471+
const only = same.length === 1 && !named.has(file.name) ? baseName(same[0]!) : null
472+
for (const name of [...chosen.keys()]) if (kindOf(name) === kindOf(file.name) && (!named.has(name) || name === only)) chosen.delete(name)
473+
}
474+
for (const file of picked) chosen.set(file.name, file)
455475
filesInput.value = ''
456476
updateFiles()
457477
})

‎src/h5p/build/build-source.ts‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,46 @@ export function namedFiles(source: unknown): string[] {
200200
/** A path's last part, which is what a file chosen from a computer is called. */
201201
export const baseName = (path: string): string => path.slice(path.replace(/\\/g, '/').lastIndexOf('/') + 1)
202202

203+
/** A file's kind, by the ending of its name. */
204+
export const kindOf = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase()
205+
203206
/**
204-
* The files to build with: each path the source names, matched to a chosen file by its name,
205-
* keyed by the path as the source writes it.
207+
* Each path the source names, matched to one of the things given for it under a name: a file
208+
* chosen, or an address a chat holds. By name first. A chat writes a file's name its own way, and
209+
* the person's file is called what their camera or the chat called it: so where one path of a kind
210+
* is left without its match, and one given thing of that kind without a path, that is its match.
211+
* More of a kind on either side are left, since nothing says which is which. `unused` are the
212+
* given things no path took.
206213
*/
207-
export function matchFiles(paths: string[], chosen: File[]): { files: Record<string, File>, missing: string[] } {
208-
const byName = new Map(chosen.map((file) => [file.name, file]))
209-
const files: Record<string, File> = {}
210-
const missing: string[] = []
214+
export function matchNamed<T>(paths: string[], given: (readonly [name: string, thing: T])[]): { matched: Record<string, T>, missing: string[], unused: T[] } {
215+
const byName = new Map(given)
216+
const matched: Record<string, T> = {}
217+
const unnamed: string[] = []
218+
const taken = new Set<string>()
211219
for (const path of paths) {
212-
const file = byName.get(baseName(path))
213-
if (file) files[path] = file
214-
else missing.push(path)
220+
const name = baseName(path)
221+
if (byName.has(name)) {
222+
matched[path] = byName.get(name)!
223+
taken.add(name)
224+
} else unnamed.push(path)
225+
}
226+
const left = [...byName.keys()].filter((name) => !taken.has(name))
227+
const missing: string[] = []
228+
for (const path of unnamed) {
229+
const fits = left.filter((name) => kindOf(name) === kindOf(path))
230+
const [name] = fits
231+
if (name !== undefined && fits.length === 1 && unnamed.filter((other) => kindOf(other) === kindOf(path)).length === 1) {
232+
matched[path] = byName.get(name)!
233+
taken.add(name)
234+
} else missing.push(path)
215235
}
216-
return { files, missing }
236+
return { matched, missing, unused: [...byName].filter(([name]) => !taken.has(name)).map(([, thing]) => thing) }
237+
}
238+
239+
/** The files to build with, keyed by the path as the source writes it: `matchNamed`, of the chosen files. */
240+
export function matchFiles(paths: string[], chosen: File[]): { files: Record<string, File>, missing: string[], unused: File[] } {
241+
const { matched, missing, unused } = matchNamed(paths, chosen.map((file) => [file.name, file] as const))
242+
return { files: matched, missing, unused }
217243
}
218244

219245
/**

‎src/h5p/build/index.njk‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
205205

206206
<section class="step" id="files-step" aria-labelledby="files-heading" hidden>
207207
<h3 id="files-heading"><span class="step-marker"><span class="step-number">2</span></span> Files</h3>
208-
<p>The source names these files. Choose them from your computer; each is matched by its name.</p>
208+
<p>The source names these files. Choose them from your computer. Each is matched by its name, and a single file of a kind fits whatever it is called.</p>
209209
{#- The lists are drawn without bullets, and Safari then reads one as a list only if it is told. -#}
210210
<ul class="file-list" id="file-list" role="list"></ul>
211+
<p class="file-note" id="files-note" role="status" hidden></p>
211212
<p>
212213
<label for="files" class="inline-label">Choose files</label>
213214
<input type="file" id="files" multiple>

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

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import '@missing-elements/h5p-offline-player'
22
import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser'
33
import { pack as installPack } from '@missing-elements/h5p-libraries/install'
44
import { runtime } from '@missing-elements/h5p-runtime'
5-
import { baseName, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'
5+
import { baseName, kindOf, matchFiles, matchNamed, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'
66

77
/**
88
* The play page, `/h5p/play/`: the activity in a link's `#source=` built in the browser, as the
@@ -145,45 +145,28 @@ edit.href = fix.href = `/h5p/build/${location.hash}`
145145
if (framed) new ResizeObserver(() => toParent({ type: 'height', height: Math.ceil(document.documentElement.getBoundingClientRect().height) })).observe(document.body)
146146

147147
/** A file's kind, by the ending of its name. */
148-
const kind = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase()
148+
const kind = kindOf
149149
/** What a person calls a file of that kind. */
150150
const called = (name: string): string => /^(mp4|webm|ogv|mov)$/.test(kind(name)) ? 'video' : /^(mp3|m4a|wav|ogg|oga)$/.test(kind(name)) ? 'recording' : /^(png|jpe?g|gif|bmp)$/.test(kind(name)) ? 'picture' : 'file'
151151

152152
/**
153-
* The given files for the paths the source names, matched by name as the build page matches them.
154-
* Where it names one file and one of the same kind was given, that is the file whatever it is
155-
* called: a chat may write the name its own way, and the source cannot be changed here.
153+
* The given files for the paths the source names, matched as the build page matches them: by name,
154+
* and a single file of a kind whatever it is called, since a chat may write the name its own way
155+
* and the source cannot be changed here.
156156
*/
157-
function filesFor(paths: string[]): { files: Record<string, File>, missing: string[] } {
158-
const given = [...chosen.values()]
159-
const matched = matchFiles(paths, given)
160-
const [path] = paths
161-
const [file] = given
162-
if (matched.missing.length && paths.length === 1 && given.length === 1 && path !== undefined && file !== undefined && kind(path) === kind(file.name)) {
163-
return { files: { [path]: file }, missing: [] }
164-
}
165-
return matched
166-
}
157+
const filesFor = (paths: string[]): { files: Record<string, File>, missing: string[] } => matchFiles(paths, [...chosen.values()])
167158

168159
/** Whether a file of that name can be played from an address: a video or a recording. */
169160
const playable = (name: string): boolean => called(name) === 'video' || called(name) === 'recording'
170161

171162
/**
172-
* The addresses for the paths still missing, matched as files are: by name, or where one path and
173-
* one address are left, by kind. A picture is never played from an address: its size is read.
163+
* The addresses for the paths still missing, matched as files are, by the same rule: by name, then
164+
* a single one of a kind whatever it is called. A picture is never played from an address: its
165+
* size is read.
174166
*/
175167
function linksFor(paths: string[]): { linked: Record<string, string>, missing: string[] } {
176-
const given = [...links]
177-
const linked: Record<string, string> = {}
178-
const missing: string[] = []
179-
for (const path of paths) {
180-
const [only] = given
181-
const address = !playable(path) ? undefined
182-
: links.get(baseName(path)) ?? (paths.length === 1 && given.length === 1 && only && kind(only[0]) === kind(path) ? only[1] : undefined)
183-
if (address) linked[path] = address
184-
else missing.push(path)
185-
}
186-
return { linked, missing }
168+
const { matched } = matchNamed(paths.filter(playable), [...links])
169+
return { linked: matched, missing: paths.filter((path) => !(path in matched)) }
187170
}
188171

189172
/** Something said to a screen reader once, beside the state's own words. */

0 commit comments

Comments
 (0)