@@ -341,21 +341,28 @@ async function saveConfig(configData) {
341341 try {
342342 const res = await fetch ( `https://${ GetParentResourceName ( ) } /saveConfig` , {
343343 method : "POST" ,
344- headers : { "Content-Type" : "application/json" } ,
345- body : JSON . stringify ( { file : currentFile , data : configData } ) ,
344+ headers : { "Content-Type" : "application/json" } ,
345+ body : JSON . stringify ( { file : currentFile , data : configData } ) ,
346346 } ) ;
347- const result = await res . json ( ) ;
348- if ( result === "ok" ) {
347+
348+ const raw = await res . text ( ) ;
349+ let result ;
350+ try { result = JSON . parse ( raw ) ; } catch { result = raw ; }
351+
352+ if ( result === "ok" || ( result && result . status === "ok" ) ) {
349353 allData [ currentFile ] = configData ;
350354 console . log ( "Saved successfully!" ) ;
351355 closeModal ( ) ;
352356 renderData ( ) ;
353- } else throw new Error ( result ) ;
357+ } else {
358+ throw new Error ( typeof result === "string" ? result : JSON . stringify ( result ) ) ;
359+ }
354360 } catch ( err ) {
355361 console . error ( "Save failed." , err ) ;
356362 }
357363}
358364
365+
359366// Modal close handlers
360367function setupModalClose ( ) {
361368 document . getElementById ( "close-modal-btn" ) . onclick = closeModal ;
@@ -365,6 +372,12 @@ function setupModalClose() {
365372 } ;
366373}
367374
375+ function closeEditorUI ( ) {
376+ const editor = document . getElementById ( "editor-container" ) ;
377+ editor . classList . add ( "hidden" ) ;
378+ fetch ( `https://${ GetParentResourceName ( ) } /closeEditor` , { method : "POST" } ) ;
379+ }
380+
368381function closeModal ( ) {
369382 document . getElementById ( "edit-modal" ) . classList . add ( "hidden" ) ;
370383}
@@ -382,27 +395,63 @@ function setupSearch() {
382395// Keyboard shortcuts
383396function setupKeyboard ( ) {
384397 window . addEventListener ( "keydown" , ( e ) => {
385- if ( e . key === "Escape" ) closeModal ( ) ;
398+ if ( e . key === "Escape" ) {
399+ const modal = document . getElementById ( "edit-modal" ) ;
400+ if ( modal && ! modal . classList . contains ( "hidden" ) ) {
401+ closeModal ( ) ;
402+ } else {
403+ closeEditorUI ( ) ;
404+ }
405+ }
386406 } ) ;
387407}
388408
409+
389410// Initialize UI
390411window . addEventListener ( "DOMContentLoaded" , ( ) => {
391412 document . querySelectorAll ( ".file-list li" ) . forEach ( ( li ) => {
392413 const file = li . dataset . file ;
393414 li . addEventListener ( "click" , ( ) => selectFile ( file , li ) ) ;
394415 } ) ;
416+
417+ const closeBtn = document . getElementById ( "close-btn" ) ;
418+ if ( closeBtn ) {
419+ closeBtn . addEventListener ( "click" , closeEditorUI ) ;
420+ }
421+
422+ const saveBtn = document . getElementById ( "save-btn" ) ;
423+ if ( saveBtn ) {
424+ saveBtn . addEventListener ( "click" , ( ) => {
425+ if ( ! currentFile ) return ;
426+ const data = allData [ currentFile ] ;
427+ if ( ! data ) return ;
428+ saveConfig ( data ) ;
429+ } ) ;
430+ }
431+
395432 setupSearch ( ) ;
396433 setupKeyboard ( ) ;
397434} ) ;
398435
436+
399437// Listen for NUI messages and auto-select first file
400438window . addEventListener ( "message" , ( event ) => {
401439 if ( event . data . action === "populateData" ) {
402440 allData = event . data . data ;
441+ document . getElementById ( "editor-container" ) . classList . remove ( "hidden" ) ;
403442 if ( ! currentFile ) {
404443 const firstLi = document . querySelector ( ".file-list li" ) ;
405444 if ( firstLi ) firstLi . click ( ) ;
406445 }
407446 }
447+ if ( event . data . action === "closeEditor" ) {
448+ closeEditorUI ( ) ;
449+ }
450+ } ) ;
451+
452+ document . getElementById ( "close-btn" ) . addEventListener ( "click" , ( ) => {
453+ document . getElementById ( "editor-container" ) . classList . add ( "hidden" ) ;
454+ fetch ( `https://${ GetParentResourceName ( ) } /closeEditor` , {
455+ method : "POST"
456+ } ) ;
408457} ) ;
0 commit comments