mirror of
https://github.com/ankitects/anki.git
synced 2025-11-14 16:47:12 -05:00
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:
parent
f5b326c753
commit
81a093a8f4
3 changed files with 43 additions and 54 deletions
51
anki/deck.py
51
anki/deck.py
|
|
@ -155,8 +155,6 @@ qconf=?, conf=?, data=?""",
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self.sched.reset()
|
self.sched.reset()
|
||||||
# recache css
|
|
||||||
self.rebuildCSS()
|
|
||||||
|
|
||||||
def getCard(self, id):
|
def getCard(self, id):
|
||||||
return anki.cards.Card(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",
|
"where facts.mid = :id",
|
||||||
id=model.id)
|
id=model.id)
|
||||||
|
|
||||||
def rebuildCSS(self):
|
# Model changing
|
||||||
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
|
|
||||||
|
|
||||||
def changeModel(self, fids, newModel, fieldMap, cardMap):
|
def changeModel(self, fids, newModel, fieldMap, cardMap):
|
||||||
|
raise Exception()
|
||||||
self.modSchema()
|
self.modSchema()
|
||||||
sfids = ids2str(fids)
|
sfids = ids2str(fids)
|
||||||
self.startProgress()
|
self.startProgress()
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
||||||
|
|
||||||
import simplejson
|
import simplejson
|
||||||
from anki.utils import intTime
|
from anki.utils import intTime, hexifyID
|
||||||
|
from anki.fonts import toPlatformFont
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
|
|
||||||
# Models
|
# Models
|
||||||
|
|
@ -24,6 +25,7 @@ class Model(object):
|
||||||
self.name = u""
|
self.name = u""
|
||||||
self.mod = intTime()
|
self.mod = intTime()
|
||||||
self.conf = defaultConf.copy()
|
self.conf = defaultConf.copy()
|
||||||
|
self.css = ""
|
||||||
self.fields = []
|
self.fields = []
|
||||||
self.templates = []
|
self.templates = []
|
||||||
|
|
||||||
|
|
@ -40,10 +42,11 @@ select mod, name, flds, conf from models where id = ?""", self.id)
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.mod = intTime()
|
self.mod = intTime()
|
||||||
ret = self.deck.db.execute("""
|
ret = self.deck.db.execute("""
|
||||||
insert or replace into models values (?, ?, ?, ?, ?)""",
|
insert or replace into models values (?, ?, ?, ?, ?, ?)""",
|
||||||
self.id, self.mod, self.name,
|
self.id, self.mod, self.name,
|
||||||
simplejson.dumps(self.fields),
|
simplejson.dumps(self.fields),
|
||||||
simplejson.dumps(self.conf))
|
simplejson.dumps(self.conf),
|
||||||
|
self.genCSS())
|
||||||
self.id = ret.lastrowid
|
self.id = ret.lastrowid
|
||||||
[t._flush() for t in self.templates]
|
[t._flush() for t in self.templates]
|
||||||
|
|
||||||
|
|
@ -107,6 +110,35 @@ insert or replace into models values (?, ?, ?, ?, ?)""",
|
||||||
t._flush()
|
t._flush()
|
||||||
return new
|
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
|
# Field object
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,8 @@ create table if not exists models (
|
||||||
mod integer not null,
|
mod integer not null,
|
||||||
name text not null,
|
name text not null,
|
||||||
flds 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 (
|
create table if not exists templates (
|
||||||
|
|
@ -317,7 +318,7 @@ originalPath from media2""")
|
||||||
_moveTable(db, "models")
|
_moveTable(db, "models")
|
||||||
db.execute("""
|
db.execute("""
|
||||||
insert into models select id, cast(modified as int),
|
insert into models select id, cast(modified as int),
|
||||||
name, "{}", "{}" from models2""")
|
name, "{}", "{}", "" from models2""")
|
||||||
db.execute("drop table models2")
|
db.execute("drop table models2")
|
||||||
|
|
||||||
# reviewHistory -> revlog
|
# reviewHistory -> revlog
|
||||||
|
|
@ -403,9 +404,10 @@ quizFontFamily, quizFontSize, quizFontColour, editFontSize from fieldModels"""):
|
||||||
conf['qsize'],
|
conf['qsize'],
|
||||||
conf['qcol'],
|
conf['qcol'],
|
||||||
conf['esize']) = row[3:]
|
conf['esize']) = row[3:]
|
||||||
# setup bools
|
# ensure data is good
|
||||||
conf['rtl'] = not not conf['rtl']
|
conf['rtl'] = not not conf['rtl']
|
||||||
conf['pre'] = True
|
conf['pre'] = True
|
||||||
|
conf['qcol'] = conf['qcol'] or "#fff"
|
||||||
# add to model list with ordinal for sorting
|
# add to model list with ordinal for sorting
|
||||||
mods[row[1]].append((row[2], conf))
|
mods[row[1]].append((row[2], conf))
|
||||||
# now we've gathered all the info, save it into the models
|
# now we've gathered all the info, save it into the models
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue