@@ -47,11 +47,24 @@ export const BatchRename = () => {
4747 const [ validationErrorNew , setValidationErrorNew ] = createSignal < string > ( "" )
4848 const t = useT ( )
4949
50+ const validateRegex = ( pattern : string ) => {
51+ try {
52+ // eslint-disable-next-line no-new
53+ new RegExp ( pattern )
54+ return { valid : true as const }
55+ } catch ( e ) {
56+ return { valid : false as const , error : "invalid_regex" }
57+ }
58+ }
59+
5060 const handleInputSrc = ( newValue : string ) => {
5161 setSrcName ( newValue )
5262 if ( type ( ) === "2" || type ( ) === "3" ) {
5363 const validation = validateFilename ( newValue )
5464 setValidationErrorSrc ( validation . valid ? "" : validation . error || "" )
65+ } else if ( type ( ) === "1" ) {
66+ const validation = validateRegex ( newValue )
67+ setValidationErrorSrc ( validation . valid ? "" : validation . error || "" )
5568 } else {
5669 setValidationErrorSrc ( "" )
5770 }
@@ -92,6 +105,13 @@ export const BatchRename = () => {
92105 notify . warning ( t ( "global.empty_input" ) )
93106 return
94107 }
108+ if ( type ( ) === "1" ) {
109+ const validationSrc = validateRegex ( srcName ( ) )
110+ if ( ! validationSrc . valid ) {
111+ notify . warning ( t ( `global.${ validationSrc . error } ` ) )
112+ return
113+ }
114+ }
95115 if ( type ( ) === "2" || type ( ) === "3" ) {
96116 const validationSrc = validateFilename ( srcName ( ) )
97117 if ( ! validationSrc . valid ) {
@@ -104,10 +124,10 @@ export const BatchRename = () => {
104124 return
105125 }
106126 }
107- const replaceRegexp = new RegExp ( srcName ( ) , "g" )
108-
109127 let matchNames : RenameObj [ ]
110128 if ( type ( ) === "1" ) {
129+ const replaceRegexp = new RegExp ( srcName ( ) , "g" )
130+
111131 matchNames = selectedObjs ( )
112132 . filter ( ( obj ) => obj . name . match ( srcName ( ) ) )
113133 . map ( ( obj ) => {
@@ -239,6 +259,9 @@ export const BatchRename = () => {
239259 } else if ( event === "2" ) {
240260 setNewNameType ( "number" )
241261 }
262+ // Clear validation errors when switching type
263+ setValidationErrorSrc ( "" )
264+ setValidationErrorNew ( "" )
242265 } }
243266 >
244267 < HStack spacing = "$4" >
@@ -292,6 +315,11 @@ export const BatchRename = () => {
292315 }
293316 } }
294317 />
318+ < Show when = { validationErrorNew ( ) } >
319+ < Text color = "$danger9" fontSize = "$sm" >
320+ { t ( `global.${ validationErrorNew ( ) } ` ) }
321+ </ Text >
322+ </ Show >
295323 < Show when = { type ( ) === "2" } >
296324 < Input
297325 id = "modal-input3"
@@ -312,11 +340,6 @@ export const BatchRename = () => {
312340 } }
313341 />
314342 </ Show >
315- < Show when = { validationErrorNew ( ) } >
316- < Text color = "$danger9" fontSize = "$sm" >
317- { t ( `global.${ validationErrorNew ( ) } ` ) }
318- </ Text >
319- </ Show >
320343 </ VStack >
321344 </ ModalBody >
322345 < ModalFooter display = "flex" gap = "$2" >
0 commit comments