From 46a540e22f50101e88b56af1f701176e788ca217 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 28 May 2018 13:40:35 +1000 Subject: [PATCH] sync field contents before applying cloze deletion based on a patch by dlon: https://github.com/dae/anki/pull/236 --- aqt/editor.py | 7 +++++-- web/editor.js | 11 +++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index 522c3c1ed..51f8759b1 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -332,14 +332,14 @@ class Editor: return [(f['font'], f['size'], f['rtl']) for f in self.note.model()['flds']] - def saveNow(self, callback): + def saveNow(self, callback, keepFocus=False): "Save unsaved edits then call callback()." if not self.note: # calling code may not expect the callback to fire immediately self.mw.progress.timer(10, callback, False) return self.saveTags() - self.web.evalWithCallback("saveNow()", lambda res: callback()) + self.web.evalWithCallback("saveNow(%d)" % keepFocus, lambda res: callback()) def checkValid(self): cols = [] @@ -472,6 +472,9 @@ class Editor: self.web.eval("setFormat('removeFormat');") def onCloze(self): + self.saveNow(self._onCloze, keepFocus=True) + + def _onCloze(self): # check that the model is set up for cloze deletion if not re.search('{{(.*:)*cloze:',self.note.model()['tmpls'][0]['qfmt']): if self.addMode: diff --git a/web/editor.js b/web/editor.js index 492a79311..c2e4a7046 100644 --- a/web/editor.js +++ b/web/editor.js @@ -14,9 +14,16 @@ function setFGButton(col) { $("#forecolor")[0].style.backgroundColor = col; } -function saveNow() { +function saveNow(keepFocus) { + if (!currentField) { + return; + } + clearChangeTimer(); - if (currentField) { + + if (keepFocus) { + saveField("key"); + } else { currentField.blur(); } }