diff --git a/README.md b/README.md index 5a75017..2467feb 100644 --- a/README.md +++ b/README.md @@ -976,6 +976,9 @@ corresponding to when the API was available for use. Invokes the *Add Cards* dialog, presets the note using the given deck and model, with the provided field values and tags. Invoking it multiple times closes the old window and _reopen the window_ with the new provided values. + Audio, video, and picture files can be embedded into the fields via the `audio`, `video`, and `picture` keys, respectively. + Refer to the documentation of `addNote` and `storeMediaFile` for an explanation of these fields. + 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_. @@ -999,7 +1002,14 @@ corresponding to when the API was available for use. }, "tags": [ "countries" - ] + ], + "picture": [{ + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/EU-Romania.svg/285px-EU-Romania.svg.png", + "filename": "romania.png", + "fields": [ + "Extra" + ] + }] } } } diff --git a/plugin/__init__.py b/plugin/__init__.py index 2f7e5db..9546dcc 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -618,14 +618,7 @@ class AnkiConnect: def addNote(self, note): ankiNote = self.createNote(note) - audioObjectOrList = note.get('audio') - self.addMedia(ankiNote, audioObjectOrList, util.MediaType.Audio) - - videoObjectOrList = note.get('video') - self.addMedia(ankiNote, videoObjectOrList, util.MediaType.Video) - - pictureObjectOrList = note.get('picture') - self.addMedia(ankiNote, pictureObjectOrList, util.MediaType.Picture) + self.addMediaFromNote(ankiNote, note) collection = self.collection() self.startEditing() @@ -638,6 +631,18 @@ class AnkiConnect: return ankiNote.id + def addMediaFromNote(self, ankiNote, note): + audioObjectOrList = note.get('audio') + self.addMedia(ankiNote, audioObjectOrList, util.MediaType.Audio) + + videoObjectOrList = note.get('video') + self.addMedia(ankiNote, videoObjectOrList, util.MediaType.Video) + + pictureObjectOrList = note.get('picture') + self.addMedia(ankiNote, pictureObjectOrList, util.MediaType.Picture) + + + def addMedia(self, ankiNote, mediaObjectOrList, mediaType): if mediaObjectOrList is None: return @@ -1372,7 +1377,9 @@ class AnkiConnect: for name, value in note['fields'].items(): if name in ankiNote: ankiNote[name] = value - editor.loadNote() + + self.addMediaFromNote(ankiNote, note) + editor.loadNote() if 'tags' in note: ankiNote.tags = note['tags'] @@ -1395,6 +1402,8 @@ class AnkiConnect: if name in ankiNote: ankiNote[name] = value + self.addMediaFromNote(ankiNote, note) + if 'tags' in note: ankiNote.tags = note['tags'] diff --git a/tests/test_graphical.py b/tests/test_graphical.py index 0512f0c..e95a9a8 100755 --- a/tests/test_graphical.py +++ b/tests/test_graphical.py @@ -14,12 +14,32 @@ class TestGui(unittest.TestCase): # guiAddCards with preset util.invoke('createDeck', deck='test') - note = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ['tag1']} + + note = { + 'deckName': 'test', + 'modelName': 'Basic', + 'fields': { + 'Front': 'front1', + 'Back': 'back1' + }, + 'tags': ['tag1'], + } util.invoke('guiAddCards', note=note) # guiAddCards with preset and closeAfterAdding - noteWithOption = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'options': { 'closeAfterAdding': True }, 'tags': ['tag1']} - util.invoke('guiAddCards', note=noteWithOption) + util.invoke('guiAddCards', note={ + **note, + 'options': { 'closeAfterAdding': True }, + }) + + util.invoke('guiAddCards', note={ + **note, + 'picture': [{ + 'url': 'https://via.placeholder.com/150.png', + 'filename': 'placeholder.png', + 'fields': ['Front'], + }] + }) # guiCurrentCard # util.invoke('guiCurrentCard')