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.
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"
]
}]
}
}
}

View File

@ -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']

View File

@ -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')