From 65cc6c0b12e0ca6b396eecdc22a3701c923a3494 Mon Sep 17 00:00:00 2001 From: gugutu <59798823+gugutu@users.noreply.github.com> Date: Sun, 25 Feb 2024 05:11:17 +0800 Subject: [PATCH] Modify the implementation method of "avoiding secondary refresh" The previous implementation may encounter issues in specific scenarios and could potentially become invalid after Anki updates. Now, the approach has been modified to check whether the note content has been modified, which is a simpler and more understandable method. --- src/__init__.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 7a1136d..d8878e6 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -103,8 +103,8 @@ class EditorPreview(object): self.editors.add(editor) # The initial loading of notes will also trigger an editing event # which will cause a second refresh - # It is disabled here and enabled after the first editing - editor.need_reload_on_edit = False + # Caching the content of notes here will be used to determine if the content has changed + editor.cached_fields = list(editor.note.fields) self.refresh(editor) def editor_init_button_hook(self, buttons, editor): @@ -134,13 +134,11 @@ class EditorPreview(object): return f"_showAnswer({json.dumps(a)},'{bodyclass}');" - def onedit_hook(self, origin): + def onedit_hook(self, note): for editor in self.editors: - if editor.note == origin: - if editor.need_reload_on_edit: - self.refresh(editor) - else: - editor.need_reload_on_edit = True + if editor.note == note and editor.cached_fields != note.fields: + editor.cached_fields = list(note.fields) + self.refresh(editor) def refresh(self, editor): editor.editor_preview.eval(self._obtainCardText(editor.note))