1
1
import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
2
- import type { MouseEvent , MutableRefObject } from "react" ;
3
2
import { createPortal } from "react-dom" ;
4
3
5
4
import { css } from "@emotion/react" ;
@@ -18,6 +17,7 @@ import "../editor/setup";
18
17
import { memoize , normalizeFileName , notifyError } from "../utils" ;
19
18
20
19
import type { UseMainState , ReactNode } from "../consts" ;
20
+ import type { MouseEvent , MutableRefObject } from "react" ;
21
21
22
22
const TabBar = styled . div `
23
23
display: flex;
@@ -234,10 +234,10 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
234
234
sample ||= "untitled" ;
235
235
const indices = schemaNames
236
236
. map ( name => {
237
- if ( name === sample + " .js" ) return 0 ;
238
- if ( ! name . startsWith ( sample + "-" ) || ! name . endsWith ( ".js" ) ) return - 1 ;
237
+ if ( name === ` ${ sample } .js` ) return 0 ;
238
+ if ( ! name . startsWith ( ` ${ sample } -` ) || ! name . endsWith ( ".js" ) ) return - 1 ;
239
239
const start = sample . length + 1 ;
240
- for ( let i = start ; i < name . length - 3 ; i ++ ) if ( name [ i ] < + ( i === start ) + "" || name [ i ] > "9" ) return - 1 ;
240
+ for ( let i = start ; i < name . length - 3 ; i ++ ) if ( name [ i ] < ` ${ + ( i === start ) } ` || name [ i ] > "9" ) return - 1 ;
241
241
return + name . slice ( start , - 3 ) ;
242
242
} )
243
243
. sort ( ( a , b ) => a - b ) ;
@@ -268,10 +268,9 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
268
268
for ( const file of files ) {
269
269
// POSIX allows all characters other than `\0` and `/` in file names,
270
270
// this is necessary to ensure that the file name is valid on all platforms.
271
- const name =
272
- getDefaultFileNameWithSchemaNames ( currSchemaNames ) (
273
- normalizeFileName ( file . name ) . replace ( invalidCharsRegex , "_" ) ,
274
- ) + ".js" ;
271
+ const name = `${ getDefaultFileNameWithSchemaNames ( currSchemaNames ) (
272
+ normalizeFileName ( file . name ) . replace ( invalidCharsRegex , "_" ) ,
273
+ ) } .js`;
275
274
currSchemaNames . push ( name ) ;
276
275
newState = actions . addSchema ( { name, input : contents [ i ++ ] } ) ( newState ) ;
277
276
}
@@ -408,7 +407,7 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
408
407
const index = schemas . findIndex ( schema => schema . name === name ) ;
409
408
const children = [ ] . slice . call ( tabBarRef . current . children , 0 , - 1 ) as HTMLElement [ ] ;
410
409
const widths = children . map ( element => element . getBoundingClientRect ( ) . width ) ;
411
- const currentWidth = widths [ index ] + "px" ;
410
+ const currentWidth = ` ${ widths [ index ] } px` ;
412
411
const threshold : number [ ] = [ ] ;
413
412
threshold [ index ] = 0 ;
414
413
@@ -424,22 +423,22 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
424
423
let clientX = startX ;
425
424
426
425
function move ( event : { clientX : number } | TouchEvent ) {
427
- clientX = "clientX" in event ? event . clientX : ( event . touches ?. [ 0 ] ? .clientX ?? clientX ) ;
426
+ clientX = "clientX" in event ? event . clientX : ( event . touches [ 0 ] . clientX ?? clientX ) ;
428
427
let value = clientX - startX ;
429
- children [ index ] . style . left = value + "px" ;
428
+ children [ index ] . style . left = ` ${ value } px` ;
430
429
if ( value < 0 ) {
431
430
value = - value ;
432
431
for ( let i = 0 ; i < index ; i ++ ) children [ i ] . style . left = value >= threshold [ i ] ? currentWidth : "" ;
433
432
for ( let i = length - 1 ; i > index ; i -- ) children [ i ] . style . left = "" ;
434
433
} else {
435
434
for ( let i = 0 ; i < index ; i ++ ) children [ i ] . style . left = "" ;
436
435
for ( let i = length - 1 ; i > index ; i -- )
437
- children [ i ] . style . left = value >= threshold [ i ] ? "-" + currentWidth : "" ;
436
+ children [ i ] . style . left = value >= threshold [ i ] ? `- ${ currentWidth } ` : "" ;
438
437
}
439
438
}
440
439
441
440
function end ( event : { clientX : number } | TouchEvent ) {
442
- clientX = "clientX" in event ? event . clientX : ( event . touches ?. [ 0 ] ? .clientX ?? clientX ) ;
441
+ clientX = "clientX" in event ? event . clientX : ( event . touches [ 0 ] . clientX ?? clientX ) ;
443
442
let value = clientX - startX ;
444
443
children . forEach ( element => ( element . style . left = "" ) ) ;
445
444
let i : number ;
@@ -475,7 +474,7 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
475
474
const hasSchemaName = ( name : string ) => schemas . find ( schema => schema . name === name ) ;
476
475
if ( ! name ) return "檔案名稱為空" ;
477
476
if ( invalidCharsRegex . test ( name ) ) return "檔案名稱含有特殊字元" ;
478
- if ( hasSchemaName ( name + " .js" ) ) return "檔案名稱與現有檔案重複" ;
477
+ if ( hasSchemaName ( ` ${ name } .js` ) ) return "檔案名稱與現有檔案重複" ;
479
478
return "" ;
480
479
}
481
480
@@ -497,15 +496,15 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
497
496
confirmButtonText : "確定" ,
498
497
cancelButtonText : "取消" ,
499
498
} ) ;
500
- const confirmButton = Swal . getConfirmButton ( ) as HTMLButtonElement ;
499
+ const confirmButton = Swal . getConfirmButton ( ) ! ;
501
500
confirmButton . disabled = true ;
502
501
confirmButton . style . pointerEvents = "none" ;
503
- const input = Swal . getInput ( ) as HTMLInputElement ;
502
+ const input = Swal . getInput ( ) ! ;
504
503
input . addEventListener ( "input" , ( ) => {
505
504
const newName = normalizeFileName ( input . value ) ;
506
505
const validation = validateFileName ( newName ) ;
507
506
if ( validation ) {
508
- if ( newName + " .js" !== name ) {
507
+ if ( ` ${ newName } .js` !== name ) {
509
508
const { selectionStart, selectionEnd, selectionDirection } = input ;
510
509
Swal . showValidationMessage ( validation ) ;
511
510
input . setSelectionRange ( selectionStart , selectionEnd , selectionDirection || undefined ) ;
@@ -521,7 +520,7 @@ export default function SchemaEditor({ state, setState, commonOptions, evaluateH
521
520
const { isConfirmed, value } = await promise ;
522
521
if ( isConfirmed ) {
523
522
const newName = normalizeFileName ( value ) ;
524
- if ( ! validateFileName ( newName ) ) setState ( actions . renameSchema ( name , newName + " .js" ) ) ;
523
+ if ( ! validateFileName ( newName ) ) setState ( actions . renameSchema ( name , ` ${ newName } .js` ) ) ;
525
524
}
526
525
}
527
526
}
0 commit comments