2424from kivymd .uix .snackbar import BaseSnackbar
2525
2626from notes_app import __version__
27+ from notes_app .diff import merge_strings
2728from notes_app .observer .notes_observer import Observer
2829
2930from notes_app .color import (
@@ -192,7 +193,9 @@ def __init__(self, **kw):
192193 )
193194 self .current_section_identifier = self .file .default_section_identifier
194195 self .filter_data_split_by_section ()
195- self .set_drawer_items (section_identifiers = self .file .section_identifiers )
196+ self .set_drawer_items (
197+ section_identifiers = self .file .section_identifiers_sorted_by_name
198+ )
196199
197200 @property
198201 def is_unsaved_change (self ):
@@ -388,7 +391,9 @@ def execute_open_file(self, file_path):
388391 controller = self .controller ,
389392 defaults = self .defaults ,
390393 )
391- self .set_drawer_items (section_identifiers = self .file .section_identifiers )
394+ self .set_drawer_items (
395+ section_identifiers = self .file .section_identifiers_sorted_by_name
396+ )
392397 self .filter_data_split_by_section (
393398 section_identifier = self .file .default_section_identifier
394399 )
@@ -510,7 +515,8 @@ def execute_add_section(self, *args):
510515 not section_name
511516 or len (section_name ) < SECTION_FILE_NAME_MINIMAL_CHAR_COUNT
512517 or section_name .isspace ()
513- or section_name in [si .section_name for si in self .file .section_identifiers ]
518+ or section_name
519+ in [si .section_name for si in self .file .section_identifiers_sorted_by_name ]
514520 ):
515521 self .dialog .content_cls .add_section_result_message = "Invalid name"
516522 return
@@ -529,7 +535,9 @@ def execute_add_section(self, *args):
529535
530536 self .filter_data_split_by_section (section_identifier = section_identifier )
531537
532- self .set_drawer_items (section_identifiers = self .file .section_identifiers )
538+ self .set_drawer_items (
539+ section_identifiers = self .file .section_identifiers_sorted_by_name
540+ )
533541
534542 self .cancel_dialog ()
535543
@@ -541,7 +549,7 @@ def execute_edit_section(self, *args):
541549 or len (new_section_name ) < SECTION_FILE_NAME_MINIMAL_CHAR_COUNT
542550 or new_section_name .isspace ()
543551 or new_section_name
544- in [si .section_name for si in self .file .section_identifiers ]
552+ in [si .section_name for si in self .file .section_identifiers_sorted_by_name ]
545553 or old_section_name == new_section_name
546554 ):
547555 self .dialog .content_cls .edit_section_result_message = "Invalid name"
@@ -562,7 +570,11 @@ def execute_edit_section(self, *args):
562570
563571 self .filter_data_split_by_section (section_identifier = new_section_identifier )
564572
565- self .set_drawer_items (section_identifiers = self .file .section_identifiers )
573+ self .set_drawer_items (
574+ section_identifiers = self .file .section_identifiers_sorted_by_name
575+ )
576+
577+ self .current_section_identifier = new_section_identifier
566578
567579 self .cancel_dialog ()
568580
@@ -574,22 +586,46 @@ def cancel_dialog(self, *args):
574586 self .dialog = MDDialog ()
575587
576588 def save_current_section_to_file (self ):
589+ merged_current_section_text_data = None
590+
591+ if self .model .external_update :
592+ self .file .reload ()
593+ try :
594+ current_section_text_before = self .file .get_section_content (
595+ section_file_separator = self .text_section_view .section_file_separator
596+ )
597+ # KeyError raised if the current section was removed or renamed by a external update
598+ except KeyError :
599+ # merge_strings prioritizes current_section_text_after over current_section_text_before
600+ # so empty string placeholder is set to current_section_text_before
601+ current_section_text_before = ""
602+ # self.file.reload() will remove the current section identifier from self.file.section_identifiers
603+ # in case it was deleted or renamed so the current section identifier is added back
604+ self .file .add_section_identifier (
605+ section_file_separator = self .text_section_view .section_file_separator
606+ )
607+
608+ current_section_text_after = self .text_section_view .text
609+
610+ merged_current_section_text_data = merge_strings (
611+ before = current_section_text_before , after = current_section_text_after
612+ )
613+
614+ self .text_section_view .text = merged_current_section_text_data
615+ self .set_drawer_items (
616+ section_identifiers = self .file .section_identifiers_sorted_by_name
617+ )
618+
577619 self .file .set_section_content (
578620 section_file_separator = self .text_section_view .section_file_separator ,
579- section_content = self .text_section_view .text ,
621+ section_content = merged_current_section_text_data
622+ or self .text_section_view .text ,
580623 )
581624
582625 text_data = self .file .transform_data_by_sections_to_raw_data_content ()
583626
584627 self .controller .save_file_data (data = text_data )
585628
586- if self .model .external_update :
587- self .file .reload ()
588- self .text_section_view .text = self .file .get_section_content (
589- section_file_separator = self .text_section_view .section_file_separator
590- )
591- self .set_drawer_items (section_identifiers = self .file .section_identifiers )
592-
593629 def press_menu_item_save_file (self , * args ):
594630 self .save_current_section_to_file ()
595631
@@ -656,7 +692,7 @@ def press_edit_section(self, section_item):
656692 self .dialog .open ()
657693
658694 def press_delete_section (self , section_item ):
659- if len (self .file .section_identifiers ) == 1 :
695+ if len (self .file .section_identifiers_sorted_by_name ) == 1 :
660696 self .show_error_bar (error_message = "Cannot delete last section" )
661697 return
662698
0 commit comments