Explode onBridgeCmd

This way, an add-on can catch a blur command, do its change and then
call self.blur
This commit is contained in:
Arthur Milchior 2020-03-24 17:48:49 +01:00
parent beee1f10e0
commit 9f4c4ea355

View file

@ -367,48 +367,59 @@ class Editor:
# shutdown # shutdown
return return
# focus lost or key/button pressed? # focus lost or key/button pressed?
if cmd.startswith("blur") or cmd.startswith("key"): type, remaining = cmd.split(":", 1)
(type, ord, nid, txt) = cmd.split(":", 3) if type == "blur":
ord = int(ord) self.onBlur(*remaining.split(":", 2))
try: elif type == "key":
nid = int(nid) self.onKey(*remaining.split(":", 2))
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()
# focused into field? # focused into field?
elif cmd.startswith("focus"): elif type == "focus":
(type, num) = cmd.split(":", 1) self.onFocus(remaining)
self.currentField = int(num)
gui_hooks.editor_did_focus_field(self.note, self.currentField)
elif cmd in self._links: elif cmd in self._links:
self._links[cmd](self) self._links[cmd](self)
else: else:
print("uncaught cmd", cmd) 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): def mungeHTML(self, txt):
if txt in ("<br>", "<div><br></div>"): if txt in ("<br>", "<div><br></div>"):
return "" return ""