@@ -324,6 +324,7 @@ def init_general_sections_state_and_server_vars(self):
324324
325325 # loop through input file sections
326326 self .state .general_sections = {}
327+ self .state .add_section = ""
327328 for section_name , section_data in self ._server_vars [
328329 "fourc_yaml_content"
329330 ].sections .items ():
@@ -942,6 +943,99 @@ def change_export_fourc_yaml_path(self, export_fourc_yaml_path, **kwargs):
942943 #################################################
943944 # SELECTION CHANGES #################################
944945 ################################################
946+ @controller .set ("click_delete_section_button" )
947+ def click_delete_section_button (self , ** kwargs ):
948+ """Deletes the currently selected section; if it was the last
949+ subsection, delete the main too."""
950+ if self .state .selected_section_name in self .state .json_schema .get (
951+ "required" , []
952+ ):
953+ return
954+
955+ cur_main = self .state .selected_main_section_name
956+ cur_section = self .state .selected_section_name
957+ if not cur_main or not cur_section :
958+ return
959+
960+ general_sections = dict (self .state .general_sections or {})
961+ sections_names = {
962+ k : dict (v ) for k , v in (self .state .section_names or {}).items ()
963+ }
964+
965+ # delete the subsection's data
966+ del general_sections [cur_main ][cur_section ]
967+ self .state .general_sections = general_sections
968+
969+ # rebuild subsections list (new list ref -> reactive)
970+ subs_before = sections_names [cur_main ]["subsections" ]
971+ new_subs = [s for s in subs_before if s != cur_section ]
972+ sections_names [cur_main ] = {** sections_names [cur_main ], "subsections" : new_subs }
973+
974+ if cur_section == cur_main :
975+ # last one -> delete the main group immediately
976+ sections_names .pop (cur_main , None )
977+ general_sections .pop (cur_main , None )
978+ self .state .section_names = sections_names
979+ self .state .general_sections = general_sections
980+
981+ # choose a new valid selection
982+ new_main = next (iter (sections_names .keys ()), "" )
983+ self .state .selected_main_section_name = new_main
984+ self .state .selected_section_name = (
985+ sections_names [new_main ]["subsections" ][0 ] if new_main else ""
986+ )
987+ return
988+
989+ self .state .section_names = sections_names
990+ self .state .selected_main_section_name = cur_main
991+ self .state .selected_section_name = new_subs [0 ]
992+
993+ @change ("add_section" )
994+ def change_add_section (self , ** kwargs ):
995+ """Reaction to section selection."""
996+ add_section = self .state .add_section
997+ main_section_name = add_section .split ("/" )[0 ]
998+
999+ if add_section not in self .state .json_schema .get ("properties" , {}):
1000+ return
1001+
1002+ general_sections = dict (self .state .general_sections or {})
1003+ section_names = {
1004+ k : dict (v ) for k , v in (self .state .section_names or {}).items ()
1005+ }
1006+
1007+ # Ensure main buckets exist
1008+ if main_section_name not in section_names :
1009+ section_names [main_section_name ] = {
1010+ "subsections" : [main_section_name ],
1011+ "content_mode" : self .state .all_content_modes ["general_section" ],
1012+ }
1013+ if main_section_name not in general_sections :
1014+ general_sections [main_section_name ] = {main_section_name : {}}
1015+
1016+ # Store data under main -> sub
1017+ if add_section not in general_sections [main_section_name ]:
1018+ general_sections [main_section_name ][add_section ] = {}
1019+
1020+ # Replace subsections list with a NEW list object
1021+ subs = section_names [main_section_name ]["subsections" ]
1022+ if add_section not in subs :
1023+ subs = subs + [add_section ] # new list ref
1024+ section_names [main_section_name ] = {
1025+ ** section_names [main_section_name ], # keep content_mode
1026+ "subsections" : subs , # new list ref
1027+ }
1028+
1029+ # Commit (new references -> reactive)
1030+ self .state .general_sections = general_sections
1031+ self .state .section_names = section_names
1032+
1033+ # Set a valid selection so VSelect updates
1034+ self .state .selected_main_section_name = main_section_name
1035+ self .state .selected_section_name = add_section
1036+
1037+ self .state .add_section = ""
1038+
9451039 @change ("selected_main_section_name" )
9461040 def change_selected_main_section_name (self , selected_main_section_name , ** kwargs ):
9471041 """Reaction to change of state.selected_main_section_name."""
0 commit comments