From dddb3b54b9697b9a7d04ff1a0c89307c6a140c26 Mon Sep 17 00:00:00 2001 From: RisingOrange Date: Tue, 11 Mar 2025 13:12:58 +0100 Subject: [PATCH 1/4] Update test --- tests/expected.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/expected.json b/tests/expected.json index 17029c2..6e66fae 100644 --- a/tests/expected.json +++ b/tests/expected.json @@ -672,7 +672,7 @@ "autoreveal_personal_notes": false, "autoscroll_to_button": true, "back_signal_tag": "XXXYYYZZZ", - "back_tts": true, + "back_tts": false, "back_tts_speed": 1.4, "background_color": "#f8f9fd", "bold_text_color": "inherit", From 18a64b4dc53656b8e61cd2a00907541f246a8415 Mon Sep 17 00:00:00 2001 From: RisingOrange Date: Tue, 11 Mar 2025 13:13:21 +0100 Subject: [PATCH 2/4] Don't remove extra local fields --- .../gui/extra_notetype_versions.py | 2 +- src/anking_notetypes/utils.py | 68 +++++++++++++------ 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/anking_notetypes/gui/extra_notetype_versions.py b/src/anking_notetypes/gui/extra_notetype_versions.py index 320bc28..5865e7b 100644 --- a/src/anking_notetypes/gui/extra_notetype_versions.py +++ b/src/anking_notetypes/gui/extra_notetype_versions.py @@ -72,7 +72,7 @@ def convert_extra_notetypes( new_model["id"] = model_copy["id"] new_model["name"] = model_copy["name"] # to prevent duplicates new_model["usn"] = -1 # triggers full sync - new_model = adjust_field_ords(model_copy, new_model) + new_model["flds"] = adjust_field_ords(model_copy["flds"], new_model["flds"]) mw.col.models.update_dict(new_model) # change the notes of type to type diff --git a/src/anking_notetypes/utils.py b/src/anking_notetypes/utils.py index c795ca2..bbbee10 100644 --- a/src/anking_notetypes/utils.py +++ b/src/anking_notetypes/utils.py @@ -1,6 +1,7 @@ import re import time from copy import deepcopy +from typing import Dict, List from aqt import mw @@ -33,7 +34,7 @@ def update_notetype_to_newest_version( if ankihub_field: new_model["flds"].append(ankihub_field) - new_model = adjust_field_ords(model, new_model) + new_model["flds"] = adjust_field_ords(model["flds"], new_model["flds"]) new_model = _retain_ankihub_modifications(model, new_model) @@ -109,28 +110,51 @@ def _updated_note_type_content( def adjust_field_ords( - cur_model: "NotetypeDict", new_model: "NotetypeDict" -) -> "NotetypeDict": - # this makes sure that when fields get added or are moved - # field contents end up in the field with the same name as before - # note that the resulting model will have exactly the same set of fields as the new_model - for fld in new_model["flds"]: - if ( - cur_ord := next( - ( - _fld["ord"] - for _fld in cur_model["flds"] - if _fld["name"] == fld["name"] - ), - None, - ) - ) is not None: - fld["ord"] = cur_ord + cur_model_fields: List[Dict], new_model_fields: List[Dict] +) -> List[Dict]: + """ + Prepares note type fields for updates by merging fields from the current and new models. + + This function handles several operations when updating note types: + 1. Maintains field content mapping by assigning appropriate 'ord' values to matching fields + 2. Assigns high 'ord' values to new fields so they start empty + 3. Appends fields that only exist locally to the new model + 4. Ensures the ankihub_id field remains at the end if it exists + + Returns: + Updated note type fields + """ + new_model_fields = deepcopy(new_model_fields) + + cur_model_field_map = { + field["name"].lower(): field["ord"] for field in cur_model_fields + } + + # Set appropriate ord values for each new field + for new_model_field in new_model_fields: + field_name_lower = new_model_field["name"].lower() + if field_name_lower in cur_model_field_map: + # If field exists in current model, preserve its ord value + new_model_field["ord"] = cur_model_field_map[field_name_lower] else: - # it's okay to assign this to multiple fields because the - # backend assigns new ords equal to the fields index - fld["ord"] = len(new_model["flds"]) - 1 - return new_model + # For new fields, set ord to a value outside the range of current fields + new_model_field["ord"] = len(cur_model_fields) + 1 + + # Append fields that only exist locally to the new model, while keeping the ankihub_id field at the end + new_model_field_names = {field["name"].lower() for field in new_model_fields} + only_local_fields = [ + field + for field in cur_model_fields + if field["name"].lower() not in new_model_field_names + ] + if new_model_fields[-1]["name"] == "ankihub_id": + final_fields = ( + new_model_fields[:-1] + only_local_fields + [new_model_fields[-1]] + ) + else: + final_fields = new_model_fields + only_local_fields + + return final_fields def create_backup() -> None: From 97f101a0704da06775af969c937f4d915f029abd Mon Sep 17 00:00:00 2001 From: RisingOrange Date: Tue, 11 Mar 2025 13:30:53 +0100 Subject: [PATCH 3/4] Don't assume ankihub_id field is the last --- src/anking_notetypes/utils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/anking_notetypes/utils.py b/src/anking_notetypes/utils.py index bbbee10..64dd4b3 100644 --- a/src/anking_notetypes/utils.py +++ b/src/anking_notetypes/utils.py @@ -147,9 +147,16 @@ def adjust_field_ords( for field in cur_model_fields if field["name"].lower() not in new_model_field_names ] - if new_model_fields[-1]["name"] == "ankihub_id": + + ankihub_id_field = next( + (field for field in new_model_fields if field["name"] == "ankihub_id"), None + ) + if ankihub_id_field: + new_model_fields_without_ankihub = [ + field for field in new_model_fields if field["name"] != "ankihub_id" + ] final_fields = ( - new_model_fields[:-1] + only_local_fields + [new_model_fields[-1]] + new_model_fields_without_ankihub + only_local_fields + [ankihub_id_field] ) else: final_fields = new_model_fields + only_local_fields From 25771884498b5916c57720ee81fa2707eea5a5ec Mon Sep 17 00:00:00 2001 From: RisingOrange Date: Tue, 11 Mar 2025 14:19:23 +0100 Subject: [PATCH 4/4] ref: Rename adjust_fields --- src/anking_notetypes/gui/extra_notetype_versions.py | 4 ++-- src/anking_notetypes/utils.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/anking_notetypes/gui/extra_notetype_versions.py b/src/anking_notetypes/gui/extra_notetype_versions.py index 5865e7b..6ef6176 100644 --- a/src/anking_notetypes/gui/extra_notetype_versions.py +++ b/src/anking_notetypes/gui/extra_notetype_versions.py @@ -8,7 +8,7 @@ from ..constants import NOTETYPE_COPY_RE from ..notetype_setting_definitions import anking_notetype_names -from ..utils import adjust_field_ords, create_backup +from ..utils import adjust_fields, create_backup def handle_extra_notetype_versions() -> None: @@ -72,7 +72,7 @@ def convert_extra_notetypes( new_model["id"] = model_copy["id"] new_model["name"] = model_copy["name"] # to prevent duplicates new_model["usn"] = -1 # triggers full sync - new_model["flds"] = adjust_field_ords(model_copy["flds"], new_model["flds"]) + new_model["flds"] = adjust_fields(model_copy["flds"], new_model["flds"]) mw.col.models.update_dict(new_model) # change the notes of type to type diff --git a/src/anking_notetypes/utils.py b/src/anking_notetypes/utils.py index 64dd4b3..8e7f658 100644 --- a/src/anking_notetypes/utils.py +++ b/src/anking_notetypes/utils.py @@ -34,7 +34,7 @@ def update_notetype_to_newest_version( if ankihub_field: new_model["flds"].append(ankihub_field) - new_model["flds"] = adjust_field_ords(model["flds"], new_model["flds"]) + new_model["flds"] = adjust_fields(model["flds"], new_model["flds"]) new_model = _retain_ankihub_modifications(model, new_model) @@ -109,7 +109,7 @@ def _updated_note_type_content( ) -def adjust_field_ords( +def adjust_fields( cur_model_fields: List[Dict], new_model_fields: List[Dict] ) -> List[Dict]: """