added modelFieldFonts, modelFieldSetFont, modelFieldSetFontSize

This commit is contained in:
Austin Siew 2022-12-24 04:10:56 -07:00
parent ad4a4a4661
commit 59f75a2946

View File

@ -189,6 +189,22 @@ class AnkiConnect:
return media return media
def getModel(self, modelName):
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
return model
def getField(self, modelName, fieldName):
model = self.getModel(modelName)
fieldMap = self.collection().models.fieldMap(model)
if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
return fieldMap[fieldName][1]
def startEditing(self): def startEditing(self):
self.window().requireReset() self.window().requireReset()
@ -1082,6 +1098,25 @@ class AnkiConnect:
return ['' for field in model['flds']] return ['' for field in model['flds']]
@util.api()
def modelFieldFonts(self, modelName):
model = self.getModel(modelName)
fonts = []
for field in model['flds']:
#fonts.append([field['font'], field['size']])
fieldFont = {
field['name']: {
'font': field['font'],
'size': field['size'],
}
}
fonts.append(fieldFont)
return fonts
@util.api() @util.api()
def modelFieldsOnTemplates(self, modelName): def modelFieldsOnTemplates(self, modelName):
@ -1201,15 +1236,19 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldRename(self, modelName, oldFieldName, newFieldName): def modelFieldRename(self, modelName, oldFieldName, newFieldName):
mm = self.collection().models #mm = self.collection().models
model = mm.byName(modelName) #model = mm.byName(modelName)
if model is None: #if model is None:
raise Exception('model was not found: {}'.format(modelName)) # raise Exception('model was not found: {}'.format(modelName))
fieldMap = mm.fieldMap(model) #fieldMap = mm.fieldMap(model)
if oldFieldName not in fieldMap: #if oldFieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, oldFieldName)) # raise Exception('field was not found in {}: {}'.format(modelName, oldFieldName))
field = fieldMap[oldFieldName][1] #field = fieldMap[oldFieldName][1]
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, oldFieldName)
mm.renameField(model, field, newFieldName) mm.renameField(model, field, newFieldName)
@ -1218,15 +1257,19 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldReposition(self, modelName, fieldName, index): def modelFieldReposition(self, modelName, fieldName, index):
mm = self.collection().models #mm = self.collection().models
model = mm.byName(modelName) #model = mm.byName(modelName)
if model is None: #if model is None:
raise Exception('model was not found: {}'.format(modelName)) # raise Exception('model was not found: {}'.format(modelName))
fieldMap = mm.fieldMap(model) #fieldMap = mm.fieldMap(model)
if fieldName not in fieldMap: #if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, fieldName)) # raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
field = fieldMap[fieldName][1] #field = fieldMap[fieldName][1]
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
mm.repositionField(model, field, index) mm.repositionField(model, field, index)
@ -1236,9 +1279,11 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldAdd(self, modelName, fieldName, index=None): def modelFieldAdd(self, modelName, fieldName, index=None):
mm = self.collection().models mm = self.collection().models
model = mm.byName(modelName) model = self.getModel(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName)) #model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
# only adds the field if it doesn't already exist # only adds the field if it doesn't already exist
fieldMap = mm.fieldMap(model) fieldMap = mm.fieldMap(model)
@ -1257,21 +1302,53 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldRemove(self, modelName, fieldName): def modelFieldRemove(self, modelName, fieldName):
mm = self.collection().models #mm = self.collection().models
model = mm.byName(modelName) #model = mm.byName(modelName)
if model is None: #if model is None:
raise Exception('model was not found: {}'.format(modelName)) # raise Exception('model was not found: {}'.format(modelName))
fieldMap = mm.fieldMap(model) #fieldMap = mm.fieldMap(model)
if fieldName not in fieldMap: #if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, fieldName)) # raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
field = fieldMap[fieldName][1] #field = fieldMap[fieldName][1]
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
mm.removeField(model, field) mm.removeField(model, field)
self.save_model(mm, model) self.save_model(mm, model)
@util.api()
def modelFieldSetFont(self, modelName, fieldName, font):
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
if not isinstance(font, str):
raise Exception("font should be a string: {}".format(font))
field["font"] = font
self.save_model(mm, model)
@util.api()
def modelFieldSetFontSize(self, modelName, fieldName, fontSize):
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
if not isinstance(fontSize, int):
raise Exception("fontSize should be an integer: {}".format(fontSize))
field["size"] = fontSize
self.save_model(mm, model)
@util.api() @util.api()
def deckNameFromId(self, deckId): def deckNameFromId(self, deckId):
deck = self.collection().decks.get(deckId) deck = self.collection().decks.get(deckId)