mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 17:26:36 -04:00
refactor focus/blur code; implement cloze via wrap()
This commit is contained in:
parent
cf6fccf454
commit
c69d68e141
1 changed files with 38 additions and 45 deletions
|
@ -76,11 +76,12 @@ function clearChangeTimer() {
|
||||||
|
|
||||||
function onFocus(elem) {
|
function onFocus(elem) {
|
||||||
currentField = elem;
|
currentField = elem;
|
||||||
setTimeout(foo, 1);
|
setTimeout(unfocusHack, 1);
|
||||||
py.run("onfocus");
|
py.run("focus:" + currentField.id.substring(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
function foo() {
|
// tabbing into a new field highlights everything, which we don't want
|
||||||
|
function unfocusHack() {
|
||||||
var s = document.getSelection();
|
var s = document.getSelection();
|
||||||
if (s.rangeCount) {
|
if (s.rangeCount) {
|
||||||
var r = s.getRangeAt(0);
|
var r = s.getRangeAt(0);
|
||||||
|
@ -92,16 +93,15 @@ function foo() {
|
||||||
|
|
||||||
function onBlur() {
|
function onBlur() {
|
||||||
if (currentField) {
|
if (currentField) {
|
||||||
saveField("focus");
|
saveField("blur");
|
||||||
}
|
}
|
||||||
clearChangeTimer();
|
clearChangeTimer();
|
||||||
currentField = null;
|
currentField = null;
|
||||||
py.run("onblur");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function saveField(type) {
|
function saveField(type) {
|
||||||
// type is either 'focus' or 'key'
|
// type is either 'blur' or 'key'
|
||||||
py.run(type + ":" + currentField.id.substring(1) + ":" + currentField.innerHTML);
|
py.run(type + ":" + currentField.innerHTML);
|
||||||
};
|
};
|
||||||
|
|
||||||
function wrap(front, back) {
|
function wrap(front, back) {
|
||||||
|
@ -292,19 +292,23 @@ class Editor(object):
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
def bridge(self, str):
|
def bridge(self, str):
|
||||||
if str.startswith("onblur"):
|
# focus lost or key/button pressed?
|
||||||
self.disableButtons()
|
if str.startswith("blur") or str.startswith("key"):
|
||||||
elif str.startswith("onfocus"):
|
print "save fact"
|
||||||
self.enableButtons()
|
(type, txt) = str.split(":", 1)
|
||||||
elif str.startswith("focus") or str.startswith("key"):
|
self.fact._fields[self.currentField] = txt
|
||||||
print str
|
if type == "blur":
|
||||||
(type, num, txt) = str.split(":", 2)
|
self.disableButtons()
|
||||||
self.fact._fields[int(num)] = txt
|
|
||||||
if type == "focus":
|
|
||||||
runHook("editor.focusLost", self.fact)
|
runHook("editor.focusLost", self.fact)
|
||||||
else:
|
else:
|
||||||
runHook("editor.keyPressed", self.fact)
|
runHook("editor.keyPressed", self.fact)
|
||||||
self.fact.flush()
|
self.fact.flush()
|
||||||
|
# focused into field?
|
||||||
|
elif str.startswith("focus"):
|
||||||
|
(type, num) = str.split(":", 1)
|
||||||
|
self.enableButtons()
|
||||||
|
self.currentField = int(num)
|
||||||
|
# state buttons changed?
|
||||||
elif str.startswith("state"):
|
elif str.startswith("state"):
|
||||||
(cmd, txt) = str.split(":", 1)
|
(cmd, txt) = str.split(":", 1)
|
||||||
r = simplejson.loads(txt)
|
r = simplejson.loads(txt)
|
||||||
|
@ -313,33 +317,6 @@ class Editor(object):
|
||||||
self._buttons['text_under'].setChecked(r['under'])
|
self._buttons['text_under'].setChecked(r['under'])
|
||||||
self._buttons['text_super'].setChecked(r['super'])
|
self._buttons['text_super'].setChecked(r['super'])
|
||||||
self._buttons['text_sub'].setChecked(r['sub'])
|
self._buttons['text_sub'].setChecked(r['sub'])
|
||||||
elif str.startswith("cloze"):
|
|
||||||
(cmd, num, txt) = str.split(":", 2)
|
|
||||||
if not txt:
|
|
||||||
showInfo(_("Please select some text first."),
|
|
||||||
help="ClozeDeletion")
|
|
||||||
return
|
|
||||||
# check that the model is set up for cloze deletion
|
|
||||||
ok = False
|
|
||||||
for t in self.fact.model().templates:
|
|
||||||
if "cloze" in t['qfmt'] or "cloze" in t['afmt']:
|
|
||||||
ok = True
|
|
||||||
break
|
|
||||||
if not ok:
|
|
||||||
showInfo(_("Please add a cloze deletion model."),
|
|
||||||
help="ClozeDeletion")
|
|
||||||
return
|
|
||||||
num = int(num)
|
|
||||||
f = self.fact._fields[num]
|
|
||||||
# find the highest existing cloze
|
|
||||||
m = re.findall("\{\{c(\d+)::", f)
|
|
||||||
if m:
|
|
||||||
next = sorted([int(x) for x in m])[-1] + 1
|
|
||||||
else:
|
|
||||||
next = 1
|
|
||||||
self.fact._fields[num] = f.replace(
|
|
||||||
txt, "{{c%d::%s}}" % (next, txt))
|
|
||||||
self.loadFact()
|
|
||||||
else:
|
else:
|
||||||
print str
|
print str
|
||||||
|
|
||||||
|
@ -530,8 +507,24 @@ class Editor(object):
|
||||||
self.web.eval("setFormat('removeFormat');")
|
self.web.eval("setFormat('removeFormat');")
|
||||||
|
|
||||||
def onCloze(self):
|
def onCloze(self):
|
||||||
self.removeFormat()
|
# check that the model is set up for cloze deletion
|
||||||
self.web.eval("cloze();")
|
ok = False
|
||||||
|
for t in self.fact.model().templates:
|
||||||
|
if "cloze" in t['qfmt'] or "cloze" in t['afmt']:
|
||||||
|
ok = True
|
||||||
|
break
|
||||||
|
if not ok:
|
||||||
|
showInfo(_("Please add a cloze deletion model."),
|
||||||
|
help="ClozeDeletion")
|
||||||
|
return
|
||||||
|
f = self.fact._fields[self.currentField]
|
||||||
|
# find the highest existing cloze
|
||||||
|
m = re.findall("\{\{c(\d+)::", f)
|
||||||
|
if m:
|
||||||
|
next = sorted([int(x) for x in m])[-1] + 1
|
||||||
|
else:
|
||||||
|
next = 1
|
||||||
|
self.web.eval("wrap('{{c%d::', '}}');" % next)
|
||||||
|
|
||||||
# Foreground colour
|
# Foreground colour
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
Loading…
Reference in a new issue