Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/assets/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -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: "";
Expand Down
28 changes: 24 additions & 4 deletions src/h5p/build/build-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -47,6 +47,7 @@ const openSource = $<HTMLButtonElement>('open-source')
const filesStep = $('files-step')
const fileList = $<HTMLUListElement>('file-list')
const filesInput = $<HTMLInputElement>('files')
const filesNote = $('files-note')
const buildButton = $<HTMLButtonElement>('build')
const buildNumber = $('build-number')
const playNumber = $('play-number')
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
})
Expand Down
46 changes: 36 additions & 10 deletions src/h5p/build/build-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, File>, missing: string[] } {
const byName = new Map(chosen.map((file) => [file.name, file]))
const files: Record<string, File> = {}
const missing: string[] = []
export function matchNamed<T>(paths: string[], given: (readonly [name: string, thing: T])[]): { matched: Record<string, T>, missing: string[], unused: T[] } {
const byName = new Map(given)
const matched: Record<string, T> = {}
const unnamed: string[] = []
const taken = new Set<string>()
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<string, File>, missing: string[], unused: File[] } {
const { matched, missing, unused } = matchNamed(paths, chosen.map((file) => [file.name, file] as const))
return { files: matched, missing, unused }
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/h5p/build/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da

<section class="step" id="files-step" aria-labelledby="files-heading" hidden>
<h3 id="files-heading"><span class="step-marker"><span class="step-number">2</span></span> Files</h3>
<p>The source names these files. Choose them from your computer; each is matched by its name.</p>
<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>
{#- The lists are drawn without bullets, and Safari then reads one as a list only if it is told. -#}
<ul class="file-list" id="file-list" role="list"></ul>
<p class="file-note" id="files-note" role="status" hidden></p>
<p>
<label for="files" class="inline-label">Choose files</label>
<input type="file" id="files" multiple>
Expand Down
39 changes: 11 additions & 28 deletions src/h5p/play/play-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { baseName, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'
import { baseName, kindOf, matchFiles, matchNamed, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'

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

/** A file's kind, by the ending of its name. */
const kind = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase()
const kind = kindOf
/** What a person calls a file of that kind. */
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'

/**
* The given files for the paths the source names, matched by name as the build page matches them.
* Where it names one file and one of the same kind was given, that is the file whatever it is
* called: a chat may write the name its own way, and the source cannot be changed here.
* The given files for the paths the source names, matched as the build page matches them: by name,
* and a single file of a kind whatever it is called, since a chat may write the name its own way
* and the source cannot be changed here.
*/
function filesFor(paths: string[]): { files: Record<string, File>, missing: string[] } {
const given = [...chosen.values()]
const matched = matchFiles(paths, given)
const [path] = paths
const [file] = given
if (matched.missing.length && paths.length === 1 && given.length === 1 && path !== undefined && file !== undefined && kind(path) === kind(file.name)) {
return { files: { [path]: file }, missing: [] }
}
return matched
}
const filesFor = (paths: string[]): { files: Record<string, File>, missing: string[] } => matchFiles(paths, [...chosen.values()])
Comment thread
alekswebnet marked this conversation as resolved.
Resolved

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

/**
* The addresses for the paths still missing, matched as files are: by name, or where one path and
* one address are left, by kind. A picture is never played from an address: its size is read.
* The addresses for the paths still missing, matched as files are, by the same rule: by name, then
* a single one of a kind whatever it is called. A picture is never played from an address: its
* size is read.
*/
function linksFor(paths: string[]): { linked: Record<string, string>, missing: string[] } {
const given = [...links]
const linked: Record<string, string> = {}
const missing: string[] = []
for (const path of paths) {
const [only] = given
const address = !playable(path) ? undefined
: links.get(baseName(path)) ?? (paths.length === 1 && given.length === 1 && only && kind(only[0]) === kind(path) ? only[1] : undefined)
if (address) linked[path] = address
else missing.push(path)
}
return { linked, missing }
const { matched } = matchNamed(paths.filter(playable), [...links])
return { linked: matched, missing: paths.filter((path) => !(path in matched)) }
}

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