Skip to content

Commit

Permalink
update editor focus lost hook to report when note was changed
Browse files Browse the repository at this point in the history
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 Gustaf-C#27.
  • Loading branch information
kieranlblack authored and Gustaf-C committed Nov 15, 2023
1 parent 985945a commit b6ff69c
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions chinese/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# You should have received a copy of the GNU General Public License along with
# Chinese Support 3. If not, see <https://www.gnu.org/licenses/>.

import aqt.editor
import anki.notes
from anki.hooks import addHook
from aqt import mw

Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b6ff69c

Please sign in to comment.