Add support for Anki 2.0
This commit is contained in:
parent
daecf30408
commit
8e6f688e23
@ -31,7 +31,7 @@ import sys
|
||||
from operator import itemgetter
|
||||
from time import time
|
||||
from unicodedata import normalize
|
||||
from random import choices
|
||||
from random import choice
|
||||
from string import ascii_letters
|
||||
|
||||
|
||||
@ -48,6 +48,7 @@ TICK_INTERVAL = 25
|
||||
URL_TIMEOUT = 10
|
||||
URL_UPGRADE = 'https://raw.githubusercontent.com/FooSoft/anki-connect/master/AnkiConnect.py'
|
||||
|
||||
ANKI21 = anki.version.startswith('2.1')
|
||||
|
||||
#
|
||||
# Helpers
|
||||
@ -1056,9 +1057,10 @@ class AnkiConnect:
|
||||
|
||||
if closeAfterAdding:
|
||||
|
||||
randomString = ''.join(choices(ascii_letters, k=10))
|
||||
randomString = ''.join(choice(ascii_letters) for _ in range(10))
|
||||
windowName = 'AddCardsAndClose' + randomString
|
||||
|
||||
if ANKI21:
|
||||
class AddCardsAndClose(aqt.addcards.AddCards):
|
||||
|
||||
def __init__(self, mw):
|
||||
@ -1076,6 +1078,24 @@ class AnkiConnect:
|
||||
super()._reject()
|
||||
aqt.dialogs.markClosed = savedMarkClosed
|
||||
|
||||
else:
|
||||
class AddCardsAndClose(aqt.addcards.AddCards):
|
||||
|
||||
def __init__(self, mw):
|
||||
super(AddCardsAndClose, self).__init__(mw)
|
||||
self.addButton.setText("Add and Close")
|
||||
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
|
||||
|
||||
def addCards(self):
|
||||
super(AddCardsAndClose, self).addCards()
|
||||
self.reject()
|
||||
|
||||
def reject(self):
|
||||
savedClose = aqt.dialogs.close
|
||||
aqt.dialogs.close = lambda _: savedClose(windowName)
|
||||
super(AddCardsAndClose, self).reject()
|
||||
aqt.dialogs.close = savedClose
|
||||
|
||||
aqt.dialogs._dialogs[windowName] = [AddCardsAndClose, None]
|
||||
addCards = aqt.dialogs.open(windowName, self.window())
|
||||
|
||||
@ -1095,6 +1115,7 @@ class AnkiConnect:
|
||||
# if Anki does not Focus, the window will not notice that the
|
||||
# fields are actually filled
|
||||
aqt.dialogs.open(windowName, self.window())
|
||||
if ANKI21:
|
||||
addCards.setAndFocusNote(editor.note)
|
||||
|
||||
elif note is not None:
|
||||
@ -1120,9 +1141,8 @@ class AnkiConnect:
|
||||
|
||||
addCards.activateWindow()
|
||||
|
||||
# if Anki does not Focus, the window will not notice that the
|
||||
# fields are actually filled
|
||||
aqt.dialogs.open(windowName, self.window())
|
||||
aqt.dialogs.open('AddCards', self.window())
|
||||
if ANKI21:
|
||||
addCards.setAndFocusNote(editor.note)
|
||||
|
||||
if currentWindow is not None:
|
||||
|
1303
__init__.py
Normal file
1303
__init__.py
Normal file
File diff suppressed because it is too large
Load Diff
@ -12,6 +12,15 @@ class TestGui(unittest.TestCase):
|
||||
# guiAddCards
|
||||
util.invoke('guiAddCards')
|
||||
|
||||
# guiAddCards with preset
|
||||
util.invoke('createDeck', deck='test')
|
||||
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)
|
||||
|
||||
# guiCurrentCard
|
||||
# util.invoke('guiCurrentCard')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user