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
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):
self.window().requireReset()
@ -1082,6 +1098,25 @@ class AnkiConnect:
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()
def modelFieldsOnTemplates(self, modelName):
@ -1201,15 +1236,19 @@ class AnkiConnect:
@util.api()
def modelFieldRename(self, modelName, oldFieldName, newFieldName):
mm = self.collection().models
model = mm.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
#mm = self.collection().models
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
fieldMap = mm.fieldMap(model)
if oldFieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, oldFieldName))
field = fieldMap[oldFieldName][1]
#fieldMap = mm.fieldMap(model)
#if oldFieldName not in fieldMap:
# raise Exception('field was not found in {}: {}'.format(modelName, oldFieldName))
#field = fieldMap[oldFieldName][1]
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, oldFieldName)
mm.renameField(model, field, newFieldName)
@ -1218,15 +1257,19 @@ class AnkiConnect:
@util.api()
def modelFieldReposition(self, modelName, fieldName, index):
mm = self.collection().models
model = mm.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
#mm = self.collection().models
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
fieldMap = mm.fieldMap(model)
if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
field = fieldMap[fieldName][1]
#fieldMap = mm.fieldMap(model)
#if fieldName not in fieldMap:
# raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
#field = fieldMap[fieldName][1]
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
mm.repositionField(model, field, index)
@ -1236,9 +1279,11 @@ class AnkiConnect:
@util.api()
def modelFieldAdd(self, modelName, fieldName, index=None):
mm = self.collection().models
model = mm.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
model = self.getModel(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
fieldMap = mm.fieldMap(model)
@ -1257,21 +1302,53 @@ class AnkiConnect:
@util.api()
def modelFieldRemove(self, modelName, fieldName):
mm = self.collection().models
model = mm.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
#mm = self.collection().models
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
fieldMap = mm.fieldMap(model)
if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
field = fieldMap[fieldName][1]
#fieldMap = mm.fieldMap(model)
#if fieldName not in fieldMap:
# raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
#field = fieldMap[fieldName][1]
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
mm.removeField(model, field)
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()
def deckNameFromId(self, deckId):
deck = self.collection().decks.get(deckId)