From 9f4c4ea3551b2df5d68f041e4b7cd22ba11245c5 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Tue, 24 Mar 2020 17:48:49 +0100 Subject: [PATCH 1/2] Explode onBridgeCmd This way, an add-on can catch a blur command, do its change and then call self.blur --- qt/aqt/editor.py | 83 +++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index b269df969..bcc7f5494 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -367,48 +367,59 @@ class Editor: # shutdown return # focus lost or key/button pressed? - if cmd.startswith("blur") or cmd.startswith("key"): - (type, ord, nid, txt) = cmd.split(":", 3) - ord = int(ord) - try: - nid = int(nid) - except ValueError: - nid = 0 - if nid != self.note.id: - print("ignored late blur") - return - txt = unicodedata.normalize("NFC", txt) - txt = self.mungeHTML(txt) - # misbehaving apps may include a null byte in the text - txt = txt.replace("\x00", "") - # reverse the url quoting we added to get images to display - txt = self.mw.col.media.escapeImages(txt, unescape=True) - self.note.fields[ord] = txt - if not self.addMode: - self.note.flush() - self.mw.requireReset() - if type == "blur": - self.currentField = None - # run any filters - if gui_hooks.editor_did_unfocus_field(False, self.note, ord): - # something updated the note; update it after a subsequent focus - # event has had time to fire - self.mw.progress.timer(100, self.loadNoteKeepingFocus, False) - else: - self.checkValid() - else: - gui_hooks.editor_did_fire_typing_timer(self.note) - self.checkValid() + type, remaining = cmd.split(":", 1) + if type == "blur": + self.onBlur(*remaining.split(":", 2)) + elif type == "key": + self.onKey(*remaining.split(":", 2)) # focused into field? - elif cmd.startswith("focus"): - (type, num) = cmd.split(":", 1) - self.currentField = int(num) - gui_hooks.editor_did_focus_field(self.note, self.currentField) + elif type == "focus": + self.onFocus(remaining) elif cmd in self._links: self._links[cmd](self) else: print("uncaught cmd", cmd) + def onBlurOrKey(self, ord, nid, txt): + ord = int(ord) + try: + nid = int(nid) + except ValueError: + nid = 0 + if nid != self.note.id: + print("ignored late blur") + return + txt = unicodedata.normalize("NFC", txt) + txt = self.mungeHTML(txt) + # misbehaving apps may include a null byte in the text + txt = txt.replace("\x00", "") + # reverse the url quoting we added to get images to display + txt = self.mw.col.media.escapeImages(txt, unescape=True) + self.note.fields[ord] = txt + if not self.addMode: + self.note.flush() + self.mw.requireReset() + + def onBlur(self, ord, nid, txt): + self.onBlurOrKey(ord, nid, txt) + self.currentField = None + # run any filters + if gui_hooks.editor_did_unfocus_field(False, self.note, int(ord)): + # something updated the note; update it after a subsequent focus + # event has had time to fire + self.mw.progress.timer(100, self.loadNoteKeepingFocus, False) + else: + self.checkValid() + + def onKey(self, ord, nid, txt): + self.onBlurOrKey(ord, nid, txt) + gui_hooks.editor_did_fire_typing_timer(self.note) + self.checkValid() + + def onFocus(self, num): + self.currentField = int(num) + gui_hooks.editor_did_focus_field(self.note, self.currentField) + def mungeHTML(self, txt): if txt in ("
", "

"): return "" From c9496ef0956930bd83f9a165f00af0fc11b85d87 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Tue, 24 Mar 2020 21:16:53 +0100 Subject: [PATCH 2/2] Ensure that focus, key and blur are treated as other commands The reason to do that is that I can then call blur/key from other method in add-on. More precisely, I expect to create a method which captures the blur command, ask anki to execute the standard version of the command, and then execute more code once the note contains the new field value. I should note that the code executed during blur/key/focus itself didn't change. It's only it's location which changed. --- qt/aqt/editor.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index bcc7f5494..00980aebe 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -367,20 +367,16 @@ class Editor: # shutdown return # focus lost or key/button pressed? - type, remaining = cmd.split(":", 1) - if type == "blur": - self.onBlur(*remaining.split(":", 2)) - elif type == "key": - self.onKey(*remaining.split(":", 2)) - # focused into field? - elif type == "focus": - self.onFocus(remaining) - elif cmd in self._links: - self._links[cmd](self) + splitted = cmd.split(":", 1) + cmd = splitted[0] + args = splitted[1:] + if cmd in self._links: + self._links[cmd](self, *args) # type: ignore else: print("uncaught cmd", cmd) - def onBlurOrKey(self, ord, nid, txt): + def onBlurOrKey(self, args): + ord, nid, txt = args.split(":", 2) ord = int(ord) try: nid = int(nid) @@ -399,9 +395,10 @@ class Editor: if not self.addMode: self.note.flush() self.mw.requireReset() + return ord - def onBlur(self, ord, nid, txt): - self.onBlurOrKey(ord, nid, txt) + def onBlur(self, args): + ord = self.onBlurOrKey(args) self.currentField = None # run any filters if gui_hooks.editor_did_unfocus_field(False, self.note, int(ord)): @@ -411,14 +408,14 @@ class Editor: else: self.checkValid() - def onKey(self, ord, nid, txt): - self.onBlurOrKey(ord, nid, txt) + def onKey(self, args): + self.onBlurOrKey(args) gui_hooks.editor_did_fire_typing_timer(self.note) self.checkValid() def onFocus(self, num): - self.currentField = int(num) - gui_hooks.editor_did_focus_field(self.note, self.currentField) + self.currentField = int(num) + gui_hooks.editor_did_focus_field(self.note, self.currentField) def mungeHTML(self, txt): if txt in ("
", "

"): @@ -981,6 +978,9 @@ to a cloze type first, via Edit>Change Note Type.""" dupes=showDupes, paste=onPaste, cutOrCopy=onCutOrCopy, + blur=onBlur, + focus=onFocus, + key=onKey, )