diff --git a/src/assets/site.css b/src/assets/site.css index dc23dff..18c9f5d 100644 --- a/src/assets/site.css +++ b/src/assets/site.css @@ -1037,6 +1037,9 @@ pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.9375re white-space: nowrap; } .build .file-list code { white-space: normal; } +/* A chosen file that no path took, and why. */ +.build .file-note { margin: 0.5rem 0 0; color: var(--muted); font-size: 0.9375rem; overflow-wrap: anywhere; } +.build .file-note[hidden] { display: none; } /* Beside the name's first line: a ring to fill, then a check. */ .build .file-list li::before { content: ""; diff --git a/src/h5p/build/build-page.ts b/src/h5p/build/build-page.ts index c78b048..d7cf2d5 100644 --- a/src/h5p/build/build-page.ts +++ b/src/h5p/build/build-page.ts @@ -2,7 +2,7 @@ import '@missing-elements/h5p-offline-player' import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser' import { pack as installPack } from '@missing-elements/h5p-libraries/install' import { runtime } from '@missing-elements/h5p-runtime' -import { compactSource, forTheChat, fragmentFromSource, linksSource, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, typesNamed, type Problem, type TypeFile } from './build-source' +import { baseName, compactSource, forTheChat, fragmentFromSource, kindOf, linksSource, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, typesNamed, type Problem, type TypeFile } from './build-source' import type { SourceEditor } from './editor' import { formatSource, jsonProblem, place, type Target } from './json-scan' @@ -47,6 +47,7 @@ const openSource = $('open-source') const filesStep = $('files-step') const fileList = $('file-list') const filesInput = $('files') +const filesNote = $('files-note') const buildButton = $('build') const buildNumber = $('build-number') const playNumber = $('play-number') @@ -252,16 +253,25 @@ function number(withFiles: boolean): void { function updateFiles(): string[] { const parsed = parseSource(sourceField.value) const paths = 'source' in parsed ? namedFiles(parsed.source) : [] - const { files } = matchFiles(paths, [...chosen.values()]) + const { files, unused } = matchFiles(paths, [...chosen.values()]) fileList.replaceChildren(...paths.map((path) => { const item = document.createElement('li') const name = document.createElement('code') name.textContent = path const file = files[path] item.dataset.chosen = String(Boolean(file)) - item.append(name, file ? ` chosen, ${sized(file.size)}` : ' not chosen yet') + // A file taken under another name than the source's is named, so that it is seen which one it is. + item.append(name, !file ? ' not chosen yet' : file.name === baseName(path) ? ` chosen, ${sized(file.size)}` : ` chosen: ${file.name}, ${sized(file.size)}`) return item })) + // A chosen file that no path took is said, with why: picking it would otherwise seem to do nothing. + 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}` : '' + const fits = (file: File): boolean => paths.some((path) => kindOf(path) === kindOf(file.name)) + filesNote.textContent = [ + said(unused.filter(fits), 'with more than one file of that kind, each has to be called as the source calls it.'), + said(unused.filter((file) => !fits(file)), 'the source names no file of that kind.') + ].filter(Boolean).join(' ') + filesNote.hidden = !unused.length number(paths.length > 0) return paths } @@ -451,7 +461,17 @@ openSource.addEventListener('click', () => sourceFile.click()) sourceLabel.addEventListener('click', () => editor?.focus()) filesInput.addEventListener('change', () => { - for (const file of filesInput.files ?? []) chosen.set(file.name, file) + const picked = [...(filesInput.files ?? [])] + // The last file picked for the one path of its kind is its file, whatever each is called: those + // picked for it before go, as a second choice replaces the first where the names are the source's. + const paths = updateFiles() + const named = new Set(paths.map(baseName)) + for (const file of picked) { + const same = paths.filter((path) => kindOf(path) === kindOf(file.name)) + const only = same.length === 1 && !named.has(file.name) ? baseName(same[0]!) : null + for (const name of [...chosen.keys()]) if (kindOf(name) === kindOf(file.name) && (!named.has(name) || name === only)) chosen.delete(name) + } + for (const file of picked) chosen.set(file.name, file) filesInput.value = '' updateFiles() }) diff --git a/src/h5p/build/build-source.ts b/src/h5p/build/build-source.ts index ed6ef1b..8c21e70 100644 --- a/src/h5p/build/build-source.ts +++ b/src/h5p/build/build-source.ts @@ -200,20 +200,46 @@ export function namedFiles(source: unknown): string[] { /** A path's last part, which is what a file chosen from a computer is called. */ export const baseName = (path: string): string => path.slice(path.replace(/\\/g, '/').lastIndexOf('/') + 1) +/** A file's kind, by the ending of its name. */ +export const kindOf = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase() + /** - * The files to build with: each path the source names, matched to a chosen file by its name, - * keyed by the path as the source writes it. + * Each path the source names, matched to one of the things given for it under a name: a file + * chosen, or an address a chat holds. By name first. A chat writes a file's name its own way, and + * the person's file is called what their camera or the chat called it: so where one path of a kind + * is left without its match, and one given thing of that kind without a path, that is its match. + * More of a kind on either side are left, since nothing says which is which. `unused` are the + * given things no path took. */ -export function matchFiles(paths: string[], chosen: File[]): { files: Record, missing: string[] } { - const byName = new Map(chosen.map((file) => [file.name, file])) - const files: Record = {} - const missing: string[] = [] +export function matchNamed(paths: string[], given: (readonly [name: string, thing: T])[]): { matched: Record, missing: string[], unused: T[] } { + const byName = new Map(given) + const matched: Record = {} + const unnamed: string[] = [] + const taken = new Set() for (const path of paths) { - const file = byName.get(baseName(path)) - if (file) files[path] = file - else missing.push(path) + const name = baseName(path) + if (byName.has(name)) { + matched[path] = byName.get(name)! + taken.add(name) + } else unnamed.push(path) + } + const left = [...byName.keys()].filter((name) => !taken.has(name)) + const missing: string[] = [] + for (const path of unnamed) { + const fits = left.filter((name) => kindOf(name) === kindOf(path)) + const [name] = fits + if (name !== undefined && fits.length === 1 && unnamed.filter((other) => kindOf(other) === kindOf(path)).length === 1) { + matched[path] = byName.get(name)! + taken.add(name) + } else missing.push(path) } - return { files, missing } + return { matched, missing, unused: [...byName].filter(([name]) => !taken.has(name)).map(([, thing]) => thing) } +} + +/** The files to build with, keyed by the path as the source writes it: `matchNamed`, of the chosen files. */ +export function matchFiles(paths: string[], chosen: File[]): { files: Record, missing: string[], unused: File[] } { + const { matched, missing, unused } = matchNamed(paths, chosen.map((file) => [file.name, file] as const)) + return { files: matched, missing, unused } } /** diff --git a/src/h5p/build/index.njk b/src/h5p/build/index.njk index 9a9177d..5e85b5d 100644 --- a/src/h5p/build/index.njk +++ b/src/h5p/build/index.njk @@ -205,9 +205,10 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da