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.
This commit is contained in:
Arthur Milchior 2020-03-24 21:16:53 +01:00
parent 9f4c4ea355
commit c9496ef095

View file

@ -367,20 +367,16 @@ class Editor:
# shutdown # shutdown
return return
# focus lost or key/button pressed? # focus lost or key/button pressed?
type, remaining = cmd.split(":", 1) splitted = cmd.split(":", 1)
if type == "blur": cmd = splitted[0]
self.onBlur(*remaining.split(":", 2)) args = splitted[1:]
elif type == "key": if cmd in self._links:
self.onKey(*remaining.split(":", 2)) self._links[cmd](self, *args) # type: ignore
# focused into field?
elif type == "focus":
self.onFocus(remaining)
elif cmd in self._links:
self._links[cmd](self)
else: else:
print("uncaught cmd", cmd) print("uncaught cmd", cmd)
def onBlurOrKey(self, ord, nid, txt): def onBlurOrKey(self, args):
ord, nid, txt = args.split(":", 2)
ord = int(ord) ord = int(ord)
try: try:
nid = int(nid) nid = int(nid)
@ -399,9 +395,10 @@ class Editor:
if not self.addMode: if not self.addMode:
self.note.flush() self.note.flush()
self.mw.requireReset() self.mw.requireReset()
return ord
def onBlur(self, ord, nid, txt): def onBlur(self, args):
self.onBlurOrKey(ord, nid, txt) ord = self.onBlurOrKey(args)
self.currentField = None self.currentField = None
# run any filters # run any filters
if gui_hooks.editor_did_unfocus_field(False, self.note, int(ord)): if gui_hooks.editor_did_unfocus_field(False, self.note, int(ord)):
@ -411,14 +408,14 @@ class Editor:
else: else:
self.checkValid() self.checkValid()
def onKey(self, ord, nid, txt): def onKey(self, args):
self.onBlurOrKey(ord, nid, txt) self.onBlurOrKey(args)
gui_hooks.editor_did_fire_typing_timer(self.note) gui_hooks.editor_did_fire_typing_timer(self.note)
self.checkValid() self.checkValid()
def onFocus(self, num): def onFocus(self, num):
self.currentField = int(num) self.currentField = int(num)
gui_hooks.editor_did_focus_field(self.note, self.currentField) gui_hooks.editor_did_focus_field(self.note, self.currentField)
def mungeHTML(self, txt): def mungeHTML(self, txt):
if txt in ("<br>", "<div><br></div>"): if txt in ("<br>", "<div><br></div>"):
@ -981,6 +978,9 @@ to a cloze type first, via Edit>Change Note Type."""
dupes=showDupes, dupes=showDupes,
paste=onPaste, paste=onPaste,
cutOrCopy=onCutOrCopy, cutOrCopy=onCutOrCopy,
blur=onBlur,
focus=onFocus,
key=onKey,
) )