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 committed Nov 12, 2023
1 parent 86d8bea commit 1d60535
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 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,17 @@ 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())
field = allFields[index]
we_changed = False
if note_type := note.note_type():
allFields = mw.col.models.field_names(note_type)
field = allFields[index]
we_changed |= update_fields(note, field, allFields)

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 changed or we_changed


def append_tone_styling(editor):
Expand Down

0 comments on commit 1d60535

Please sign in to comment.