wait for keystrokes and don't reload entire page

This commit is contained in:
Damien Elmes 2017-08-08 16:02:55 +10:00
parent 2c54139d3c
commit 4d064e1cba
2 changed files with 47 additions and 16 deletions

View File

@ -13,6 +13,7 @@ from aqt.utils import saveGeom, restoreGeom, mungeQA,\
showWarning, openHelp, downArrow showWarning, openHelp, downArrow
from anki.utils import isMac, isWin, joinFields from anki.utils import isMac, isWin, joinFields
from aqt.webview import AnkiWebView from aqt.webview import AnkiWebView
import json
class CardLayout(QDialog): class CardLayout(QDialog):
@ -107,13 +108,28 @@ class CardLayout(QDialog):
# gtk+ requires margins in inner layout # gtk+ requires margins in inner layout
pform.frontPrevBox.setContentsMargins(0, 11, 0, 0) pform.frontPrevBox.setContentsMargins(0, 11, 0, 0)
pform.backPrevBox.setContentsMargins(0, 11, 0, 0) pform.backPrevBox.setContentsMargins(0, 11, 0, 0)
# for cloze notes, show that it's one of n cards
self.setupWebviews()
l.addWidget(right, 5)
w.setLayout(l)
def setupWebviews(self):
pform = self.pform
pform.frontWeb = AnkiWebView() pform.frontWeb = AnkiWebView()
pform.frontPrevBox.addWidget(pform.frontWeb) pform.frontPrevBox.addWidget(pform.frontWeb)
pform.backWeb = AnkiWebView() pform.backWeb = AnkiWebView()
pform.backPrevBox.addWidget(pform.backWeb) pform.backPrevBox.addWidget(pform.backWeb)
l.addWidget(right, 5) base = self.mw.baseHTML()
w.setLayout(l) jsinc = ["jquery.js","browsersel.js",
"mathjax/conf.js", "mathjax/MathJax.js",
"reviewer.js"]
pform.frontWeb.stdHtml(self.mw.reviewer._revHtml+
pform.frontWeb.bundledCSS("reviewer.css"),
head=base, js=jsinc)
pform.backWeb.stdHtml(self.mw.reviewer._revHtml+
pform.backWeb.bundledCSS("reviewer.css"),
head=base, js=jsinc)
def updateMainArea(self): def updateMainArea(self):
self.tabs.clear() self.tabs.clear()
@ -219,21 +235,30 @@ Please create a new card type first."""))
# Preview # Preview
########################################################################## ##########################################################################
_previewTimer = None
def renderPreview(self): def renderPreview(self):
# schedule a preview when timing stops
self.cancelPreviewTimer()
self._previewTimer = self.mw.progress.timer(500, self._renderPreview, False)
def cancelPreviewTimer(self):
if self._previewTimer:
self._previewTimer.stop()
self._previewTimer = None
def _renderPreview(self):
self.cancelPreviewTimer()
c = self.card c = self.card
ti = self.maybeTextInput ti = self.maybeTextInput
base = self.mw.baseHTML() bodyclass="card card%d" % (c.ord+1)
jsinc = ["jquery.js","browsersel.js", q = ti(mungeQA(self.mw.col, c.q(reload=True)))
"mathjax/conf.js", "mathjax/MathJax.js", a = ti(mungeQA(self.mw.col, c.a()), type='a')
"mathjax/queue-typeset.js"]
self.pform.frontWeb.stdHtml( self.pform.frontWeb.eval("_showQuestion(%s,'%s');" % (json.dumps(q), bodyclass))
ti(mungeQA(self.mw.col, c.q(reload=True)))+ self.pform.backWeb.eval("_showAnswer(%s, '%s');" % (json.dumps(a), bodyclass))
self.pform.frontWeb.bundledCSS("reviewer.css"),
bodyClass="card card%d" % (c.ord+1), head=base, js=jsinc),
self.pform.backWeb.stdHtml(
ti(mungeQA(self.mw.col, c.a()), type='a')+
self.pform.backWeb.bundledCSS("reviewer.css"),
bodyClass="card card%d" % (c.ord+1), head=base, js=jsinc),
clearAudioQueue() clearAudioQueue()
if c.id not in self.playedAudio: if c.id not in self.playedAudio:
playFromText(c.q()) playFromText(c.q())
@ -441,6 +466,7 @@ Enter deck to place new %s cards in, or leave blank:""") %
self.reject() self.reject()
def reject(self): def reject(self):
self.cancelPreviewTimer()
clearAudioQueue() clearAudioQueue()
if self.addMode: if self.addMode:
# remove the filler fields we added # remove the filler fields we added

View File

@ -41,8 +41,13 @@ function _showQuestion(q, bodyclass) {
}); });
} }
function _showAnswer(a) { function _showAnswer(a, bodyclass) {
_updateQA(a, function(obj) { _updateQA(a, function(obj) {
if (bodyclass) {
// when previewing
document.body.className = bodyclass;
}
// scroll to answer? // scroll to answer?
var e = $("#answer"); var e = $("#answer");
if (e[0]) { if (e[0]) {