From 53227493797d5943f30089f177132f4ab00bc3e0 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Mon, 23 Nov 2026 21:51:43 +0200 Subject: [PATCH 1/2] fix(build): a single file of a kind is taken whatever it is called, and a file that is not used is said (#94) The build page took a picked file only under the exact name the source writes, and said nothing otherwise: a video called what a camera or a chat called it stayed "not chosen yet". The play page already took a single file of the right kind. Both now match by one rule, in build-source: by name, then one path of a kind left without its file with one chosen file of that kind left without a path. The row names the file taken under another name, a file picked again for that path replaces the one before, and a picked file no path takes is said with why. --- src/assets/site.css | 3 +++ src/h5p/build/build-page.ts | 28 ++++++++++++++++++++++++---- src/h5p/build/build-source.ts | 28 +++++++++++++++++++++++----- src/h5p/build/index.njk | 3 ++- src/h5p/play/play-page.ts | 21 ++++++--------------- 5 files changed, 58 insertions(+), 25 deletions(-) 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..9f236f5 100644 --- a/src/h5p/build/build-source.ts +++ b/src/h5p/build/build-source.ts @@ -200,20 +200,38 @@ 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. + * keyed by the path as the source writes it. 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 file, and one chosen file of that kind without a path, that is its file. More of + * a kind on either side are left, since nothing says which is which. `unused` are the chosen files + * no path took. */ -export function matchFiles(paths: string[], chosen: File[]): { files: Record, missing: string[] } { +export function matchFiles(paths: string[], chosen: File[]): { files: Record, missing: string[], unused: File[] } { const byName = new Map(chosen.map((file) => [file.name, file])) const files: Record = {} - const missing: string[] = [] + const unnamed: string[] = [] for (const path of paths) { const file = byName.get(baseName(path)) if (file) files[path] = file - else missing.push(path) + else unnamed.push(path) + } + const taken = new Set(Object.values(files)) + const left = chosen.filter((file) => !taken.has(file)) + const missing: string[] = [] + for (const path of unnamed) { + const fits = left.filter((file) => kindOf(file.name) === kindOf(path)) + const [file] = fits + if (file && fits.length === 1 && unnamed.filter((other) => kindOf(other) === kindOf(path)).length === 1) { + files[path] = file + taken.add(file) + } else missing.push(path) } - return { files, missing } + return { files, missing, unused: chosen.filter((file) => !taken.has(file)) } } /** 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