@@ -2,7 +2,7 @@ import '@missing-elements/h5p-offline-player'
22import { buildPackage , openPack , SourceError } from '@missing-elements/h5p-author/browser'
33import { pack as installPack } from '@missing-elements/h5p-libraries/install'
44import { 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'
66import type { SourceEditor } from './editor'
77import { formatSource , jsonProblem , place , type Target } from './json-scan'
88
@@ -47,6 +47,7 @@ const openSource = $<HTMLButtonElement>('open-source')
4747const filesStep = $ ( 'files-step' )
4848const fileList = $ < HTMLUListElement > ( 'file-list' )
4949const filesInput = $ < HTMLInputElement > ( 'files' )
50+ const filesNote = $ ( 'files-note' )
5051const buildButton = $ < HTMLButtonElement > ( 'build' )
5152const buildNumber = $ ( 'build-number' )
5253const playNumber = $ ( 'play-number' )
@@ -252,16 +253,25 @@ function number(withFiles: boolean): void {
252253function 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())
451461sourceLabel . addEventListener ( 'click' , ( ) => editor ?. focus ( ) )
452462
453463filesInput . 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} )
0 commit comments