Skip to content

Commit

Permalink
saveNow() now requires a callback
Browse files Browse the repository at this point in the history
the current code was freezing when clicking on 'cards' in the
browser - it looks like like the javascript callback was never
being called despite calling processEvents(). so we need to
refactor the code to call saveNow() with a callback that does the
subsequent processing.

a lot of the browser code was implicitly calling saveNow() via
beginReset(), so we've had to change all that code to save
immediately before it begins any processing. found a probable bug in
the process - it doesn't look like onRowChange() was saving before
overwriting the note, so theoretically edits could be lost if the
user switched to another card very quickly after typing something.

onSearch() has been split into a GUI-activated onSearchActivated()
that takes care of saving, and a lower level search() that refreshes
the current search. it keeps track of the last search via an instance
variable so that it refreshes properly if a user accidentally adds
some characters to their search without activating the search, then
does something like reverse the sort order.
  • Loading branch information
Damien Elmes committed Jul 14, 2016
1 parent 37bac39 commit 8e71554
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 91 deletions.
5 changes: 5 additions & 0 deletions README.addons
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Anki's webviews are now using WebEngine. Of note:
modified to use this.
- Javascript is evaluated asynchronously, so if you need the result of a JS
expression you can use ankiwebview's evalWithCallback().
- As a result of this asynchronous behaviour, editor.saveNow() now requires a
callback. If your add-on performs actions in the browser, you likely need to
call editor.saveNow() first and then run the rest of your code in the callback.
Calls to .onSearch() will need to be changed to .search()/.onSearchActivated()
as well. See the browser's .deleteNotes() for an example.
- You can now debug the webviews using an external Chrome instance, by setting
the env var QTWEBENGINE_REMOTE_DEBUGGING to 8080 prior to starting Anki,
then surfing to localhost:8080 in Chrome. If you run into issues, try
Expand Down
6 changes: 4 additions & 2 deletions aqt/addcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def onHistory(self):
def editHistory(self, nid):
browser = aqt.dialogs.open("Browser", self.mw)
browser.form.searchEdit.lineEdit().setText("nid:%d" % nid)
browser.onSearch()
browser.onSearchActivated()

def addNote(self, note):
note.model()['did'] = self.deckChooser.selectedId()
Expand All @@ -178,7 +178,9 @@ def addNote(self, note):
return note

def addCards(self):
self.editor.saveNow()
self.editor.saveNow(self._addCards)

def _addCards(self):
self.editor.saveAddModeVars()
note = self.editor.note
note = self.addNote(note)
Expand Down
Loading

0 comments on commit 8e71554

Please sign in to comment.