diff --git a/crowd_anki/anki/hook_vendor.py b/crowd_anki/anki/hook_vendor.py index 6572c85..99c5b28 100644 --- a/crowd_anki/anki/hook_vendor.py +++ b/crowd_anki/anki/hook_vendor.py @@ -1,3 +1,5 @@ +from aqt import gui_hooks + from dataclasses import dataclass from typing import Any @@ -5,6 +7,7 @@ from ..anki.adapters.hook_manager import AnkiHookManager from ..export.anki_exporter_wrapper import exporters_hook from ..history.archiver_vendor import ArchiverVendor +from ..utils.deckconf import disambiguate_crowdanki_uuid @dataclass @@ -16,6 +19,7 @@ class HookVendor: def setup_hooks(self): self.setup_exporter_hook() self.setup_snapshot_hooks() + self.setup_add_config_hook() def setup_exporter_hook(self): self.hook_manager.hook("exportersList", exporters_hook) @@ -24,3 +28,6 @@ def setup_snapshot_hooks(self): snapshot_handler = ArchiverVendor(self.window, self.config).snapshot_on_sync self.hook_manager.hook('profileLoaded', snapshot_handler) self.hook_manager.hook('unloadProfile', snapshot_handler) + + def setup_add_config_hook(self): + gui_hooks.deck_conf_did_add_config.append(disambiguate_crowdanki_uuid) diff --git a/crowd_anki/utils/deckconf.py b/crowd_anki/utils/deckconf.py new file mode 100644 index 0000000..eed727d --- /dev/null +++ b/crowd_anki/utils/deckconf.py @@ -0,0 +1,14 @@ +# Note that this only works for Anki ≤ 2.1.45, or when using the old +# deck options popup with Anki 2.1.46+. Anki 2.1.46+ has a new +# JavaScript-based options popup (with no hooks), by default. + +from .constants import UUID_FIELD_NAME + +def disambiguate_crowdanki_uuid(deck_conf, deck, + config, new_name, + new_conf_id): + new_deck_conf = deck_conf.mw.col.decks.get_config(new_conf_id) + if (new_deck_conf and (UUID_FIELD_NAME in new_deck_conf)): + # Delete rather than generating anew, (with uuid1()) to avoid code duplication. + del new_deck_conf[UUID_FIELD_NAME] + deck_conf.mw.col.decks.update_config(new_deck_conf)