@@ -26,6 +26,7 @@ import { ValidatedHtmlInputElement } from "../elements/input-validation";
2626import { ElementWithUtils } from "../utils/dom" ;
2727import { configMetadata } from "../config/metadata" ;
2828import { getConfigChanges as getConfigChangesFromConfig } from "../config/utils" ;
29+ import { normalizeName } from "../utils/strings" ;
2930
3031const state = {
3132 presetType : "full" as PresetType ,
@@ -46,7 +47,12 @@ export function show(action: string, id?: string, name?: string): void {
4647 presetNameEl ??= new ValidatedHtmlInputElement (
4748 modalEl . qsr ( "input[type=text]" ) ,
4849 {
49- schema : PresetNameSchema ,
50+ isValid : async ( name ) => {
51+ const parsed = PresetNameSchema . safeParse ( normalizeName ( name ) ) ;
52+ if ( parsed . success ) return true ;
53+ return parsed . error . errors . map ( ( err ) => err . message ) . join ( ", " ) ;
54+ } ,
55+ debounceDelay : 0 ,
5056 } ,
5157 ) ;
5258 if ( action === "add" ) {
@@ -66,7 +72,7 @@ export function show(action: string, id?: string, name?: string): void {
6672 modalEl . setAttribute ( "data-preset-id" , id ) ;
6773 modalEl . qsr ( ".popupTitle" ) . setHtml ( "Edit preset" ) ;
6874 modalEl . qsr ( ".submit" ) . setHtml ( `save` ) ;
69- presetNameEl ?. setValue ( name . replaceAll ( " " , "_" ) ) ;
75+ presetNameEl ?. setValue ( name ) ;
7076 presetNameEl ?. getParent ( ) ?. show ( ) ;
7177
7278 modalEl . qsa ( "input" ) . show ( ) ;
@@ -221,7 +227,7 @@ function hide(): void {
221227async function apply ( ) : Promise < void > {
222228 const modalEl = modal . getModal ( ) ;
223229 const action = modalEl . getAttribute ( "data-action" ) ;
224- const presetName = modalEl
230+ const propPresetName = modalEl
225231 . qsr < HTMLInputElement > ( ".group input[title='presets']" )
226232 . getValue ( ) as string ;
227233 const presetId = modalEl . getAttribute ( "data-preset-id" ) as string ;
@@ -248,22 +254,24 @@ async function apply(): Promise<void> {
248254 }
249255
250256 const addOrEditAction = action === "add" || action === "edit" ;
251- if ( addOrEditAction ) {
252- //validate the preset name only in add or edit mode
253257
254- const noPresetName : boolean =
255- presetName . replace ( / ^ _ + | _ + $ / g, "" ) . length === 0 ; //all whitespace names are rejected
256- if ( noPresetName ) {
257- showNoticeNotification ( "Preset name cannot be empty" ) ;
258- return ;
259- }
258+ if ( addOrEditAction && propPresetName . trim ( ) . length === 0 ) {
259+ showNoticeNotification ( "Preset name cannot be empty" ) ;
260+ return ;
261+ }
260262
261- if ( presetNameEl ?. getValidationResult ( ) . status === "failed" ) {
262- showNoticeNotification ( "Preset name is not valid" ) ;
263- return ;
264- }
263+ const cleanedPresetName = normalizeName ( propPresetName ) ;
264+ const parsedPresetName = addOrEditAction
265+ ? PresetNameSchema . safeParse ( cleanedPresetName )
266+ : null ;
267+
268+ if ( parsedPresetName && ! parsedPresetName . success ) {
269+ showNoticeNotification ( "Preset name is not valid" ) ;
270+ return ;
265271 }
266272
273+ const presetName = parsedPresetName ?. data ?? "" ;
274+
267275 hide ( ) ;
268276
269277 showLoaderBar ( ) ;
@@ -291,7 +299,7 @@ async function apply(): Promise<void> {
291299 ...( state . presetType === "partial" && {
292300 settingGroups : activeSettingGroups ,
293301 } ) ,
294- display : presetName . replaceAll ( "_" , " " ) ,
302+ display : presetName . replace ( / _ / g , " " ) ,
295303 _id : response . body . data . presetId ,
296304 } as SnapshotPreset ) ;
297305 }
@@ -323,7 +331,7 @@ async function apply(): Promise<void> {
323331 showSuccessNotification ( "Preset updated" ) ;
324332
325333 preset . name = presetName ;
326- preset . display = presetName . replaceAll ( "_" , " " ) ;
334+ preset . display = presetName . replace ( / _ / g , " " ) ;
327335 if ( updateConfig ) {
328336 preset . config = configChanges ;
329337 if ( state . presetType === "partial" ) {
0 commit comments