move css generation into model

The model now has a css column, and when it's flushed, it generates the css
for the fields and templates. This means we don't have to generate the CSS on
deck load anymore.

The hex cache has also been removed. Javascript couldn't handle big ints, but
since ints are small numbers now, we no longer need a cache to efficiently
convert an id to hex.
This commit is contained in:
Damien Elmes 2011-03-11 03:27:44 +09:00
parent f5b326c753
commit 81a093a8f4
3 changed files with 43 additions and 54 deletions

View file

@ -155,8 +155,6 @@ qconf=?, conf=?, data=?""",
def reset(self):
self.sched.reset()
# recache css
self.rebuildCSS()
def getCard(self, id):
return anki.cards.Card(self, id)
@ -676,54 +674,11 @@ select id from cards where fid in (select id from facts where mid = ?)""",
"where facts.mid = :id",
id=model.id)
def rebuildCSS(self):
print "fix rebuildCSS()"
return
# css for all fields
def _genCSS(prefix, row):
(id, fam, siz, col, align, rtl, pre) = row
t = ""
if fam: t += 'font-family:"%s";' % toPlatformFont(fam)
if siz: t += 'font-size:%dpx;' % siz
if col: t += 'color:%s;' % col
if rtl == "rtl":
t += "direction:rtl;unicode-bidi:embed;"
if pre:
t += "white-space:pre-wrap;"
if align != -1:
if align == 0: align = "center"
elif align == 1: align = "left"
else: align = "right"
t += 'text-align:%s;' % align
if t:
t = "%s%s {%s}\n" % (prefix, hexifyID(id), t)
return t
css = "".join([_genCSS(".fm", row) for row in self.db.all("""
select id, quizFontFamily, quizFontSize, quizFontColour, -1,
features, editFontFamily from fields""")])
cardRows = self.db.all("""
select id, null, null, null, questionAlign, 0, 0 from templates""")
css += "".join([_genCSS("#cmq", row) for row in cardRows])
css += "".join([_genCSS("#cma", row) for row in cardRows])
css += "".join([".cmb%s {background:%s;}\n" %
(hexifyID(row[0]), row[1]) for row in self.db.all("""
select id, lastFontColour from templates""")])
self.css = css
self.data['cssCache'] = css
self.addHexCache()
return css
def addHexCache(self):
ids = self.db.list("""
select id from fields union
select id from templates union
select id from models""")
cache = {}
for id in ids:
cache[id] = hexifyID(id)
self.data['hexCache'] = cache
# Model changing
##########################################################################
def changeModel(self, fids, newModel, fieldMap, cardMap):
raise Exception()
self.modSchema()
sfids = ids2str(fids)
self.startProgress()

View file

@ -3,7 +3,8 @@
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
import simplejson
from anki.utils import intTime
from anki.utils import intTime, hexifyID
from anki.fonts import toPlatformFont
from anki.lang import _
# Models
@ -24,6 +25,7 @@ class Model(object):
self.name = u""
self.mod = intTime()
self.conf = defaultConf.copy()
self.css = ""
self.fields = []
self.templates = []
@ -40,10 +42,11 @@ select mod, name, flds, conf from models where id = ?""", self.id)
def flush(self):
self.mod = intTime()
ret = self.deck.db.execute("""
insert or replace into models values (?, ?, ?, ?, ?)""",
insert or replace into models values (?, ?, ?, ?, ?, ?)""",
self.id, self.mod, self.name,
simplejson.dumps(self.fields),
simplejson.dumps(self.conf))
simplejson.dumps(self.conf),
self.genCSS())
self.id = ret.lastrowid
[t._flush() for t in self.templates]
@ -107,6 +110,35 @@ insert or replace into models values (?, ?, ?, ?, ?)""",
t._flush()
return new
# CSS generation
##################################################
def genCSS(self):
# fields
css = "".join([self._fieldCSS(
".fm%s.%s" % (hexifyID(self.id), hexifyID(c)),
(f['font'], f['qsize'], f['qcol'], f['rtl'], f['pre']))
for c, f in enumerate(self.fields)])
# templates
for t in self.templates:
css += "#cm%s {text-align:%s;background:%s}\n" % (
hexifyID(t.id),
("center", "left", "right")[t.conf['align']],
t.conf['bg'])
return css
def _fieldCSS(self, prefix, row):
(fam, siz, col, rtl, pre) = row
t = 'font-family:"%s";' % toPlatformFont(fam)
t += 'font-size:%dpx;' % siz
t += 'color:%s;' % col
if rtl:
t += "direction:rtl;unicode-bidi:embed;"
if pre:
t += "white-space:pre-wrap;"
t = "%s {%s}\n" % (prefix, t)
return t
# Field object
##########################################################################

View file

@ -104,7 +104,8 @@ create table if not exists models (
mod integer not null,
name text not null,
flds text not null,
conf text not null
conf text not null,
css text not null
);
create table if not exists templates (
@ -317,7 +318,7 @@ originalPath from media2""")
_moveTable(db, "models")
db.execute("""
insert into models select id, cast(modified as int),
name, "{}", "{}" from models2""")
name, "{}", "{}", "" from models2""")
db.execute("drop table models2")
# reviewHistory -> revlog
@ -403,9 +404,10 @@ quizFontFamily, quizFontSize, quizFontColour, editFontSize from fieldModels"""):
conf['qsize'],
conf['qcol'],
conf['esize']) = row[3:]
# setup bools
# ensure data is good
conf['rtl'] = not not conf['rtl']
conf['pre'] = True
conf['qcol'] = conf['qcol'] or "#fff"
# add to model list with ordinal for sorting
mods[row[1]].append((row[2], conf))
# now we've gathered all the info, save it into the models