From db602ebe9a0c2c6f56230f90ee436b6533d3f846 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 4 May 2019 23:24:13 +0200 Subject: [PATCH 1/3] Add return values --- plugin/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugin/__init__.py b/plugin/__init__.py index c8a18ff..cfc3bf6 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -977,6 +977,8 @@ class AnkiConnect: aqt.dialogs.open(windowName, self.window()) addCards.setAndFocusNote(editor.note) + return ankiNote.id + elif note is not None: currentWindow = aqt.dialogs._dialogs['AddCards'][1] @@ -1011,10 +1013,14 @@ class AnkiConnect: else: openNewWindow() + return aqt.dialogs._dialogs['AddCards'][1].editor.note.id + else: addCards = aqt.dialogs.open('AddCards', self.window()) addCards.activateWindow() + return addCards.editor.note.id + @util.api() def guiReviewActive(self): return self.reviewer().card is not None and self.window().state == 'review' From 1824cd8c1c2798059160859313e4aaa19c51a32a Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 4 May 2019 23:24:13 +0200 Subject: [PATCH 2/3] Fix return value of guiAddCards * For the case where the current AddCards dialogue has to be closed first --- plugin/__init__.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/plugin/__init__.py b/plugin/__init__.py index cfc3bf6..a34788c 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -980,40 +980,43 @@ class AnkiConnect: return ankiNote.id elif note is not None: - currentWindow = aqt.dialogs._dialogs['AddCards'][1] + collection = self.collection() + ankiNote = anki.notes.Note(collection, model) + + # fill out card beforehand, so we can be sure of the note id + if 'fields' in note: + for name, value in note['fields'].items(): + if name in ankiNote: + ankiNote[name] = value + + if 'tags' in note: + ankiNote.tags = note['tags'] def openNewWindow(): + nonlocal ankiNote + addCards = aqt.dialogs.open('AddCards', self.window()) if savedMid: deck['mid'] = savedMid - editor = addCards.editor - ankiNote = editor.note - - # we have to fill out the card in the callback - # otherwise we can't assure, the new window is open - if 'fields' in note: - for name, value in note['fields'].items(): - if name in ankiNote: - ankiNote[name] = value - editor.loadNote() - - if 'tags' in note: - ankiNote.tags = note['tags'] - editor.updateTags() + addCards.editor.note = ankiNote + addCards.editor.loadNote() + addCards.editor.updateTags() addCards.activateWindow() aqt.dialogs.open('AddCards', self.window()) - addCards.setAndFocusNote(editor.note) + addCards.setAndFocusNote(addCards.editor.note) + + currentWindow = aqt.dialogs._dialogs['AddCards'][1] if currentWindow is not None: currentWindow.closeWithCallback(openNewWindow) else: openNewWindow() - return aqt.dialogs._dialogs['AddCards'][1].editor.note.id + return ankiNote.id else: addCards = aqt.dialogs.open('AddCards', self.window()) From ed06e5c276d7dfa5563a1373aa8456988a3e023c Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 4 May 2019 23:24:13 +0200 Subject: [PATCH 3/3] Add documentation for return value of guiAddCards --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0fb5bd8..7a22248 100644 --- a/README.md +++ b/README.md @@ -1594,6 +1594,8 @@ guarantee that your application continues to function properly in the future. The `closeAfterAdding` member inside `options` group can be set to true to create a dialog that closes upon adding the note. Invoking the action mutliple times with this option will create _multiple windows_. + The result is the ID of the note which would be added, if the user chose to confirm the *Add Cards* dialogue. + *Sample request*: ```json { @@ -1621,7 +1623,7 @@ guarantee that your application continues to function properly in the future. *Sample result*: ```json { - "result": null, + "result": 1496198395707, "error": null } ```