diff --git a/README.md b/README.md index ac0f351..72aea22 100644 --- a/README.md +++ b/README.md @@ -1865,6 +1865,38 @@ corresponding to when the API was available for use. } ``` +* **modelFieldFonts** + + Gets the complete list of fonts along with their font sizes. + + *Sample request*: + ```json + { + "action": "modelFieldFonts", + "version": 6, + "params": { + "modelName": "Basic" + } + } + ``` + + *Sample result*: + ```json + { + "result": { + "Front": { + "font": "Arial", + "size": 20 + }, + "Back": { + "font": "Arial", + "size": 20 + } + }, + "error": null + } + ``` + * **modelFieldsOnTemplates** Returns an object indicating the fields on the question and answer side of each card template for the given model @@ -2214,18 +2246,19 @@ corresponding to when the API was available for use. } ``` -* **modelFieldRemove** +* **modelFieldSetFont** - Deletes a field within a given model. + Sets the font for a field within a given model. *Sample Request*: ```json { - "action": "modelFieldRemove", + "action": "modelFieldSetFont", "version": 6, "params": { "modelName": "Basic", - "fieldName": "Front" + "fieldName": "Front", + "font": "Courier" } } ``` @@ -2238,6 +2271,58 @@ corresponding to when the API was available for use. } ``` +* **modelFieldSetFontSize** + + Sets the font size for a field within a given model. + + *Sample Request*: + ```json + { + "action": "modelFieldSetFont", + "version": 6, + "params": { + "modelName": "Basic", + "fieldName": "Front", + "fontSize": 10 + } + } + ``` + + *Sample result*: + ```json + { + "result": null, + "error": null + } + ``` + +* **modelFieldSetDescription** + + Sets the description (the text seen in the gui editor when a field is empty) for a field within a given model. + + Older versions of Anki (2.1.49 and below) do not have field descriptions. In that case, this will return with `false`. + + *Sample Request*: + ```json + { + "action": "modelFieldSetDescription", + "version": 6, + "params": { + "modelName": "Basic", + "fieldName": "Front", + "description": "example field description" + } + } + ``` + + *Sample result*: + ```json + { + "result": true, + "error": null + } + ``` + #### Note Actions * **addNote** diff --git a/plugin/__init__.py b/plugin/__init__.py index 02c7e92..38851c5 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1102,18 +1102,13 @@ class AnkiConnect: def modelFieldFonts(self, modelName): model = self.getModel(modelName) - fonts = [] + fonts = {} for field in model['flds']: - #fonts.append([field['font'], field['size']]) - - fieldFont = { - field['name']: { - 'font': field['font'], - 'size': field['size'], - } + fonts[field['name']] = { + 'font': field['font'], + 'size': field['size'], } - fonts.append(fieldFont) return fonts @@ -1236,16 +1231,6 @@ 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)) - - #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) @@ -1257,16 +1242,6 @@ 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)) - - #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) @@ -1281,10 +1256,6 @@ class AnkiConnect: mm = self.collection().models 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) if fieldName not in fieldMap: @@ -1302,16 +1273,6 @@ 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)) - - #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) @@ -1335,20 +1296,6 @@ class AnkiConnect: self.save_model(mm, model) - @util.api() - def modelFieldSetDescription(self, modelName, fieldName, description): - mm = self.collection().models - model = self.getModel(modelName) - field = self.getField(modelName, fieldName) - - if not isinstance(description, str): - raise Exception('description should be a string: {}'.format(description)) - - field['description'] = description - - self.save_model(mm, model) - - @util.api() def modelFieldSetFontSize(self, modelName, fieldName, fontSize): mm = self.collection().models @@ -1363,6 +1310,22 @@ class AnkiConnect: self.save_model(mm, model) + @util.api() + def modelFieldSetDescription(self, modelName, fieldName, description): + mm = self.collection().models + model = self.getModel(modelName) + field = self.getField(modelName, fieldName) + + if not isinstance(description, str): + raise Exception('description should be a string: {}'.format(description)) + + if 'description' in field: # older versions do not have the 'description' key + field['description'] = description + self.save_model(mm, model) + return True + return False + + @util.api() def deckNameFromId(self, deckId): deck = self.collection().decks.get(deckId) diff --git a/tests/test_models.py b/tests/test_models.py index bed3b82..801d900 100755 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -21,6 +21,20 @@ def test_modelFieldDescriptions(setup): assert result == ["", ""] +def test_modelFieldFonts(setup): + result = ac.modelFieldFonts(modelName="test_model") + assert result == { + "field1": { + "font": "Arial", + "size": 20, + }, + "field2": { + "font": "Arial", + "size": 20, + }, + } + + def test_modelFieldsOnTemplates(setup): result = ac.modelFieldsOnTemplates(modelName="test_model") assert result == { @@ -172,3 +186,55 @@ class TestModelFieldNames: result = ac.modelFieldNames(modelName="test_model") assert result == ["field2"] + + def test_modelFieldSetFont(self, setup): + ac.modelFieldSetFont( + modelName="test_model", + fieldName="field1", + font="Courier", + ) + + result = ac.modelFieldFonts(modelName="test_model") + assert result == { + "field1": { + "font": "Courier", + "size": 20, + }, + "field2": { + "font": "Arial", + "size": 20, + }, + } + + def test_modelFieldSetFontSize(self, setup): + ac.modelFieldSetFontSize( + modelName="test_model", + fieldName="field2", + fontSize=16, + ) + + result = ac.modelFieldFonts(modelName="test_model") + assert result == { + "field1": { + "font": "Arial", + "size": 20, + }, + "field2": { + "font": "Arial", + "size": 16, + }, + } + + def test_modelFieldSetDescription(self, setup): + set_desc = ac.modelFieldSetDescription( + modelName="test_model", + fieldName="field1", + description="test description", + ) + + result = ac.modelFieldDescriptions(modelName="test_model") + + if set_desc: + assert result == ["test description", ""] + else: + assert result == ["", ""]