store type ans value instead of field, don't show when field empty

This commit is contained in:
Damien Elmes 2012-02-13 13:07:29 +09:00
parent 7be3935c02
commit fa7a88b534

View file

@ -135,7 +135,7 @@ function _typeAnsPress() {
self.web.eval("_updateQA(%s);" % simplejson.dumps(q)) self.web.eval("_updateQA(%s);" % simplejson.dumps(q))
self._showAnswerButton() self._showAnswerButton()
# if we have a type answer field, focus main web # if we have a type answer field, focus main web
if self.typeField: if self.typeCorrect:
self.mw.web.setFocus() self.mw.web.setFocus()
# user hook # user hook
runHook('showQuestion') runHook('showQuestion')
@ -239,20 +239,24 @@ img { max-width: 95%; max-height: 95%; }
return self.typeAnsAnswerFilter(buf) return self.typeAnsAnswerFilter(buf)
def typeAnsQuestionFilter(self, buf): def typeAnsQuestionFilter(self, buf):
self.typeField = None self.typeCorrect = None
m = re.search(self.typeAnsPat, buf) m = re.search(self.typeAnsPat, buf)
if not m: if not m:
return buf return buf
fld = m.group(1) fld = m.group(1)
for f in self.card.model()['flds']: for f in self.card.model()['flds']:
if f['name'] == fld: if f['name'] == fld:
self.typeField = f self.typeCorrect = self.card.note()[f['name']]
self.typeFont = f['font'] self.typeFont = f['font']
self.typeSize = f['size'] self.typeSize = f['size']
break break
if not self.typeField: if not self.typeCorrect:
if self.typeCorrect is None:
return re.sub( return re.sub(
self.typeAnsPat, _("Type answer: unknown field %s") % fld, buf) self.typeAnsPat, _("Type answer: unknown field %s") % fld, buf)
else:
# empty field, remove type answer pattern
return re.sub(self.typeAnsPat, "", buf)
return re.sub(self.typeAnsPat, """ return re.sub(self.typeAnsPat, """
<center> <center>
<input type=text id=typeans onkeypress="_typeAnsPress();" <input type=text id=typeans onkeypress="_typeAnsPress();"
@ -261,13 +265,12 @@ img { max-width: 95%; max-height: 95%; }
""" % (self.typeFont, self.typeSize), buf) """ % (self.typeFont, self.typeSize), buf)
def typeAnsAnswerFilter(self, buf): def typeAnsAnswerFilter(self, buf):
if not self.typeField: if not self.typeCorrect:
return buf return re.sub(self.typeAnsPat, "", buf)
# tell webview to call us back with the input content # tell webview to call us back with the input content
self.web.eval("_getTypedText();") self.web.eval("_getTypedText();")
# get the correct value from the field # munge correct value
cor = self.mw.col.media.strip( cor = self.mw.col.media.strip(stripHTML(self.typeCorrect))
stripHTML(self.card.note()[self.typeField['name']]))
# compare with typed answer # compare with typed answer
res = self.correct(cor, self.typedAnswer) res = self.correct(cor, self.typedAnswer)
# and update the type answer area # and update the type answer area