From 6fec307168bc8dfede41863bc922c91b08821f04 Mon Sep 17 00:00:00 2001 From: Kieran Black Date: Sat, 11 Nov 2023 21:51:20 -0500 Subject: [PATCH] update editor focus lost hook to report when note was changed Previously when a note was modified, the plugin would manually reload the note in the editor. This commit modifies things to take advantage of the built in hook infrastructure to auto-reload the note. As a bonus, this fixes a bug caused assuming only one editor would be open at a time and closes #27. --- chinese/edit.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/chinese/edit.py b/chinese/edit.py index 1577612..5077277 100644 --- a/chinese/edit.py +++ b/chinese/edit.py @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License along with # Chinese Support 3. If not, see . +import aqt.editor +import anki.notes from anki.hooks import addHook from aqt import mw @@ -30,8 +32,7 @@ def __init__(self): addHook('loadNote', self.updateButton) addHook('editFocusLost', self.onFocusLost) - def setupButton(self, buttons, editor): - self.editor = editor + def setupButton(self, buttons: list[str], editor: aqt.editor.Editor): self.buttonOn = False editor._links['chineseSupport'] = self.onToggle @@ -64,20 +65,18 @@ def updateButton(self, editor): editor.web.eval('toggleEditorButton(chineseSupport);') self.buttonOn = not self.buttonOn - def onFocusLost(self, _, note, index): + def onFocusLost(self, changed: bool, note: anki.notes.Note, index: int): if not self.buttonOn: - return False + return changed - allFields = mw.col.models.field_names(note.note_type()) + if not (note_type := note.note_type()): + return changed + allFields = mw.col.models.field_names(note_type) field = allFields[index] + if not update_fields(note, field, allFields): + return changed - if update_fields(note, field, allFields): - if index == len(allFields) - 1: - self.editor.loadNote(focusTo=index) - else: - self.editor.loadNote(focusTo=index+1) - - return False + return True def append_tone_styling(editor):