allow guiAddCards to add media (#266)

* add media to editing note

* update readme
This commit is contained in:
Jason Won 2021-06-20 23:35:09 -04:00 committed by GitHub
parent 9c6a62d20b
commit 591ac06aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 13 deletions

View File

@ -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. 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. 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. 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_. 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": [ "tags": [
"countries" "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"
] ]
}]
} }
} }
} }

View File

@ -618,14 +618,7 @@ class AnkiConnect:
def addNote(self, note): def addNote(self, note):
ankiNote = self.createNote(note) ankiNote = self.createNote(note)
audioObjectOrList = note.get('audio') self.addMediaFromNote(ankiNote, note)
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)
collection = self.collection() collection = self.collection()
self.startEditing() self.startEditing()
@ -638,6 +631,18 @@ class AnkiConnect:
return ankiNote.id 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): def addMedia(self, ankiNote, mediaObjectOrList, mediaType):
if mediaObjectOrList is None: if mediaObjectOrList is None:
return return
@ -1372,6 +1377,8 @@ class AnkiConnect:
for name, value in note['fields'].items(): for name, value in note['fields'].items():
if name in ankiNote: if name in ankiNote:
ankiNote[name] = value ankiNote[name] = value
self.addMediaFromNote(ankiNote, note)
editor.loadNote() editor.loadNote()
if 'tags' in note: if 'tags' in note:
@ -1395,6 +1402,8 @@ class AnkiConnect:
if name in ankiNote: if name in ankiNote:
ankiNote[name] = value ankiNote[name] = value
self.addMediaFromNote(ankiNote, note)
if 'tags' in note: if 'tags' in note:
ankiNote.tags = note['tags'] ankiNote.tags = note['tags']

View File

@ -14,12 +14,32 @@ class TestGui(unittest.TestCase):
# guiAddCards with preset # guiAddCards with preset
util.invoke('createDeck', deck='test') 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) util.invoke('guiAddCards', note=note)
# guiAddCards with preset and closeAfterAdding # guiAddCards with preset and closeAfterAdding
noteWithOption = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'options': { 'closeAfterAdding': True }, 'tags': ['tag1']} util.invoke('guiAddCards', note={
util.invoke('guiAddCards', note=noteWithOption) **note,
'options': { 'closeAfterAdding': True },
})
util.invoke('guiAddCards', note={
**note,
'picture': [{
'url': 'https://via.placeholder.com/150.png',
'filename': 'placeholder.png',
'fields': ['Front'],
}]
})
# guiCurrentCard # guiCurrentCard
# util.invoke('guiCurrentCard') # util.invoke('guiCurrentCard')