@@ -7,20 +7,41 @@ const { dialog } = remote;
77import restartSVG from "../../assets/icons/restart.svg?raw" ;
88import { unsafeSVG } from "lit/directives/unsafe-svg.js" ;
99
10- function getObjectTypeReferenceString ( type , multiple , { nested, native } = { } ) {
11- if ( Array . isArray ( type ) )
12- return `${ multiple ? "" : "a " } ${ type
13- . map ( ( type ) => getObjectTypeReferenceString ( type , multiple , { native, nested : true } ) )
14- . join ( " / " ) } `;
15-
16- const isDir = type === "directory" ;
17- return multiple && ( ! isDir || ( isDir && ! native ) || dialog )
18- ? type === "directory"
19- ? "directories"
20- : "files"
21- : nested
22- ? type
23- : `a ${ type } ` ;
10+ /**
11+ * Generates a human-readable reference string for filesystem object types.
12+ *
13+ * @param {string|Array<string> } type - The filesystem object type(s). Can be "file-path",
14+ * "directory-path", or an array ["file-path", "directory-path"].
15+ * @param {boolean } multiple - Whether multiple objects are expected (affects pluralization).
16+ *
17+ * @returns {string } A formatted reference string like "a file", "directories",
18+ * "a file / directory", etc.
19+ */
20+ function getObjectTypeReferenceString ( type , multiple ) {
21+ if ( Array . isArray ( type ) ) {
22+ const article = multiple ? "" : "a " ;
23+ return `${ article } file / directory` ;
24+ }
25+
26+ if ( multiple ) {
27+ return type === "directory-path" ? "directories" : "files" ;
28+ }
29+
30+ const cleanTypeName = type . replace ( "-path" , "" ) ;
31+ return `a ${ cleanTypeName } ` ;
32+ }
33+
34+ /**
35+ * Generates a simple string representation of a filesystem object type.
36+ *
37+ * @param {string|Array<string> } type - The filesystem object type(s).
38+ * @returns {string } A simple string representation of the type(s): "file", "directory", or "file / directory".
39+ */
40+ function getSimpleObjectTypeString ( type ) {
41+ if ( Array . isArray ( type ) ) {
42+ return "file / directory" ;
43+ }
44+ return type . replace ( "-path" , "" ) ;
2445}
2546
2647const componentCSS = css `
@@ -113,7 +134,7 @@ export class FilesystemSelector extends LitElement {
113134
114135 this . accept = props . accept ;
115136 this . multiple = props . multiple ;
116- this . type = props . type ?? "file" ;
137+ this . type = props . type ?? "file-path " ;
117138 this . value = props . value ?? "" ;
118139 this . dialogOptions = props . dialogOptions ?? { } ;
119140 this . onChange = props . onChange ?? ( ( ) => { } ) ;
@@ -146,7 +167,7 @@ export class FilesystemSelector extends LitElement {
146167 }
147168
148169 options . properties = [
149- type === "file" ? "openFile" : "openDirectory" ,
170+ type === "file-path " ? "openFile" : "openDirectory" ,
150171 "noResolveAliases" ,
151172 ...( options . properties ?? [ ] ) ,
152173 ] ;
@@ -165,8 +186,11 @@ export class FilesystemSelector extends LitElement {
165186 #check = ( value ) => {
166187 // Check type
167188 const isLikelyFile = fs ? fs . statSync ( value ) . isFile ( ) : value . split ( "." ) . length ;
168- if ( ( this . type === "directory" && isLikelyFile ) || ( this . type === "file" && ! isLikelyFile ) )
169- this . #onThrow( "Incorrect filesystem object" , `Please provide a <b>${ this . type } </b> instead.` ) ;
189+ if ( ( this . type === "directory-path" && isLikelyFile ) || ( this . type === "file-path" && ! isLikelyFile ) )
190+ this . #onThrow(
191+ "Incorrect filesystem object" ,
192+ `Please provide a <b>${ this . type . replace ( "-path" , "" ) } </b> instead.`
193+ ) ;
170194 } ;
171195
172196 #handleFiles = async ( pathOrPaths , type ) => {
@@ -182,7 +206,7 @@ export class FilesystemSelector extends LitElement {
182206 if ( Array . isArray ( resolvedValue ) && ! this . multiple ) {
183207 if ( resolvedValue . length > 1 )
184208 this . #onThrow(
185- `Too many ${ resolvedType === "directory" ? "directories" : "files" } detected` ,
209+ `Too many ${ resolvedType === "directory-path " ? "directories" : "files" } detected` ,
186210 `This selector will only accept one.`
187211 ) ;
188212 resolvedValue = resolvedValue [ 0 ] ;
@@ -205,7 +229,7 @@ export class FilesystemSelector extends LitElement {
205229 this . #handleFiles( results . filePath ?? results . filePaths , type ) ;
206230 } else {
207231 let handles = await (
208- type === "directory"
232+ type === "directory-path "
209233 ? window . showDirectoryPicker ( )
210234 : window . showOpenFilePicker ( { multiple : this . multiple } )
211235 ) . catch ( ( ) => [ ] ) ; // Call using the same options
@@ -230,8 +254,6 @@ export class FilesystemSelector extends LitElement {
230254 : this . value [ 0 ]
231255 : this . value ;
232256
233- const objectTypeReference = getObjectTypeReferenceString ( this . type , this . multiple ) ;
234-
235257 const instanceThis = this ;
236258
237259 return html `
@@ -261,20 +283,18 @@ export class FilesystemSelector extends LitElement {
261283 ${ dialog
262284 ? ""
263285 : html `< br /> < small
264- > Cannot get full ${ isMultipleTypes ? this . type . join ( " / " ) : this . type }
286+ > Cannot get full ${ getSimpleObjectTypeString ( this . type ) }
265287 path${ this . multiple ? "s" : "" } on web distribution</ small
266288 > ` }
267289 `
268290 : html `< span
269- > Drop ${ objectTypeReference }
291+ > Drop ${ getObjectTypeReferenceString ( this . type , this . multiple ) }
270292 here${ isMultipleTypes
271293 ? ""
272- : `, or click to choose ${ getObjectTypeReferenceString ( this . type , this . multiple , {
273- native : true ,
274- } ) } `} </ span
294+ : `, or click to choose ${ getObjectTypeReferenceString ( this . type , this . multiple ) } ` } </ span
275295 > ${ this . multiple &&
276- ( this . type === "directory" ||
277- ( isMultipleTypes && this . type . includes ( "directory" ) && ! dialog ) )
296+ ( this . type === "directory-path " ||
297+ ( isMultipleTypes && this . type . includes ( "directory-path " ) && ! dialog ) )
278298 ? html `< br /> < small
279299 > Multiple directory support only available using drag-and-drop.</ small
280300 > `
@@ -301,8 +321,7 @@ export class FilesystemSelector extends LitElement {
301321 ${ this . type . map (
302322 ( type ) =>
303323 html `< nwb-button primary @click =${ ( ) => this . selectFormat ( type ) }
304- > Select
305- ${ getObjectTypeReferenceString ( type , this . multiple , { native : true } ) } </ nwb-button
324+ > Select ${ getObjectTypeReferenceString ( type , this . multiple ) } </ nwb-button
306325 > `
307326 ) }
308327 </ div > `
0 commit comments