From 57d6e992626c3e40559b12f804fc17bb0f83a189 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Thu, 27 Apr 2023 05:34:33 +0000 Subject: [PATCH] Add a hook that allows to edit a note before it is added. (#2481) * add note_will_be_added hook * attempt to fix Tests.buildkite/linux/entrypoint * attempt to fix Tests.buildkite/linux/entrypoint * apply patch --- pylib/anki/collection.py | 1 + pylib/tools/genhooks.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index ae14152aa..4b924fe17 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -562,6 +562,7 @@ class Collection(DeprecatedNamesMixin): return Note(self, notetype) def add_note(self, note: Note, deck_id: DeckId) -> OpChanges: + hooks.note_will_be_added(self, note, deck_id) out = self._backend.add_note(note=note._to_backend_note(), deck_id=deck_id) note.id = NoteId(out.note_id) return out.changes diff --git a/pylib/tools/genhooks.py b/pylib/tools/genhooks.py index da0a87cee..388a182f1 100644 --- a/pylib/tools/genhooks.py +++ b/pylib/tools/genhooks.py @@ -31,6 +31,20 @@ hooks = [ args=["col: anki.collection.Collection", "ids: Sequence[anki.notes.NoteId]"], legacy_hook="remNotes", ), + Hook( + name="note_will_be_added", + args=[ + "col: anki.collection.Collection", + "note: anki.notes.Note", + "deck_id: anki.decks.DeckId", + ], + doc="""Allows modifying a note before it's added to the collection. + + This hook may be called both when users use the Add screen, and when + add-ons like AnkiConnect add notes. It is not called when importing. If + you wish to alter the Add screen, use gui_hooks.add_cards_will_add_note + instead.""", + ), Hook( name="media_files_did_export", args=["count: int"],