View Source AnkiConnect.Actions.Model (anki_connect v0.1.1)

Model actions.

All functions are delegated inside AnkiConnect module, so you should import them from there.

Link to this section Summary

Functions

Creates a new model to be used in Anki.

Find and replace string in existing model by model name.

Creates a new field within a given model.

Gets the complete list of field descriptions (the text seen in the gui editor when a field is empty) for the provided model name.

Gets the complete list of fonts along with their font sizes.

Gets the complete list of field names for the provided model name.

Deletes a field within a given model.

Rename the field name of a given model.

Reposition the field within the field list of a given model.

Sets the description (the text seen in the gui editor when a field is empty) for a field within a given model.

Sets the font for a field within a given model.

Sets the font size for a field within a given model.

Returns an object indicating the fields on the question and answer side of each card template for the given model name.

Gets the complete list of model names for the current user.

Gets the complete list of model names and their corresponding IDs for the current user.

Gets the CSS styling for the provided model by name.

Adds a template to an existing model by name.

Removes a template from an existing model.

Renames a template in an existing model.

Repositions a template in an existing model.

Returns an object indicating the template content for each card connected to the provided model by name.

Modify the CSS styling of an existing model by name.

Modify the templates of an existing model by name.

Link to this section Functions

@spec create_model(%{
  model_name: String.t(),
  in_order_fields: [String.t()],
  card_templates: [map()],
  css: String.t() | nil,
  is_cloze: boolean() | nil
}) :: {:ok, map()} | {:error, any()}

Creates a new model to be used in Anki.

User must provide the model_name, in_order_fields and card_templates to be used in the model.

There are optional fields ccs and is_cloze. If not specified, css will use the default Anki css and is_cloze will be equal to false. If is_cloze is true then model will be created as Cloze.

Optionally the Name field can be provided for each entry of card_templates. By default the card names will be Card 1, Card 2, and so on.

sample-param

Sample param:

%{
  model_name: "newModelName",
  in_order_fields: ["Field1", "Field2", "Field3"],
  css: "Optional CSS with default to builtin css",
  is_cloze: false,
  card_templates: [
    {
      Name: "My Card 1",
      Front: "Front html {{Field1}}",
      Back: "Back html  {{Field2}}"
    }
  ]
}

sample-result

Sample result:

%{
  "sortf" => 0,
  "did" => 1,
  "latexPre" => "\documentclass[12pt]{article}
\special{papersize=3in,5in}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}
",
  "latexPost" => "\end{document}",
  "mod" => 1551462107,
  "usn" => -1,
  "vers" => [],
  "type" => 0,
  "css" => ".card {
 font-family: arial;
 font-size: 20px;
 text-align: center;
 color: black;
 background-color: white;
}
",
  "name" => "TestApiModel",
  "flds" => [
    %{
      "name" => "Field1",
      "ord" => 0,
      "sticky" => false,
      "rtl" => false,
      "font" => "Arial",
      "size" => 20,
      "media" => []
    },
    %{
      "name" => "Field2",
      "ord" => 1,
      "sticky" => false,
      "rtl" => false,
      "font" => "Arial",
      "size" => 20,
      "media" => []
    }
  ],
  "tmpls" => [
    %{
      "name" => "My Card 1",
      "ord" => 0,
      "qfmt" => "",
      "afmt" => "This is the back of the card {{Field2}}",
      "did" => null,
      "bqfmt" => "",
      "bafmt" => ""
    }
  ],
  "tags" => [],
  "id" => 1551462107104,
  "req" => [
    [
      0,
      "none",
      []
    ]
  ]
}
Link to this function

find_and_replace_in_models(param)

View Source
@spec find_and_replace_in_models(%{
  model: %{
    model_name: String.t(),
    find_text: String.t(),
    replace_text: String.t(),
    Front: boolean() | nil,
    Back: boolean() | nil,
    css: boolean() | nil
  }
}) :: {:ok, integer()} | {:error, any()}

Find and replace string in existing model by model name.

Customise to replace in front, back or css by setting to true/false.

sample-param

Sample param:

%{
  model: {
    model_name: "Custom",
    find_text: "find",
    replace_text: "replace",
    Front: true,
    Back: true,
    css: true
  }
}

sample-result

Sample result:

{:ok, 1}
@spec model_field_add(%{
  model_name: String.t(),
  field_name: String.t(),
  index: integer() | nil
}) ::
  {:ok, nil} | {:error, any()}

Creates a new field within a given model.

Optionally, the index value can be provided, which works exactly the same as the index in model_field_reposition. By default, the field is added to the end of the field list.

Link to this function

model_field_descriptions(map)

View Source
@spec model_field_descriptions(%{model_name: String.t()}) ::
  {:ok, [String.t()]} | {:error, any()}

Gets the complete list of field descriptions (the text seen in the gui editor when a field is empty) for the provided model name.

sample-param

Sample param:

%{
  model_name: "Basic"
}

sample-result

Sample result:

{:ok, ["", ""]}
@spec model_field_fonts(%{model_name: String.t()}) :: {:ok, map()} | {:error, any()}

Gets the complete list of fonts along with their font sizes.

sample-param

Sample param:

%{
  model_name: "Basic"
}

sample-result

Sample result:

%{
  "Front" => {
    "font" => "Arial",
    "size" => 20
  },
  "Back" => {
    "font" => "Arial",
    "size" => 20
  }
}
@spec model_field_names(%{model_name: String.t()}) ::
  {:ok, [String.t()]} | {:error, any()}

Gets the complete list of field names for the provided model name.

sample-param

Sample param:

%{
  model_name: "Basic"
}

sample-result

Sample result:

{:ok, ["Front", "Back"]}
@spec model_field_remove(%{model_name: String.t(), field_name: String.t()}) ::
  {:ok, nil} | {:error, any()}

Deletes a field within a given model.

@spec model_field_rename(%{
  model_name: String.t(),
  old_field_name: String.t(),
  new_field_name: String.t()
}) :: {:ok, nil} | {:error, any()}

Rename the field name of a given model.

Link to this function

model_field_reposition(map)

View Source
@spec model_field_reposition(%{
  model_name: String.t(),
  field_name: String.t(),
  index: integer()
}) ::
  {:ok, nil} | {:error, any()}

Reposition the field within the field list of a given model.

The value of index starts at 0. For example, an index of 0 puts the field in the first position, and an index of 2 puts the field in the third position.

Link to this function

model_field_set_description(map)

View Source
@spec model_field_set_description(%{
  model_name: String.t(),
  field_name: String.t(),
  description: String.t()
}) :: {:ok, nil} | {:error, any()}

Sets the description (the text seen in the gui editor when a field is empty) for a field within a given model.

Link to this function

model_field_set_font(map)

View Source
@spec model_field_set_font(%{
  model_name: String.t(),
  field_name: String.t(),
  font: String.t()
}) ::
  {:ok, nil} | {:error, any()}

Sets the font for a field within a given model.

Link to this function

model_field_set_font_size(map)

View Source
@spec model_field_set_font_size(%{
  model_name: String.t(),
  field_name: String.t(),
  font_size: integer()
}) ::
  {:ok, nil} | {:error, any()}

Sets the font size for a field within a given model.

Link to this function

model_fields_on_templates(map)

View Source
@spec model_fields_on_templates(%{model_name: String.t()}) ::
  {:ok, map()} | {:error, any()}

Returns an object indicating the fields on the question and answer side of each card template for the given model name.

The question side is given first in each array.

sample-param

Sample param:

%{
  model_name: "Basic (and reversed card)"
}

sample-result

Sample result:

%{
  "Card 1" => [["Front"], ["Back"]],
  "Card 2" => [["Back"], ["Front"]]
}
@spec model_names() :: {:ok, [String.t()]} | {:error, any()}

Gets the complete list of model names for the current user.

sample-result

Sample result:

{:ok, ["Basic", "Basic (and reversed card)", "Cloze"]}
@spec model_names_and_ids() :: {:ok, map()} | {:error, any()}

Gets the complete list of model names and their corresponding IDs for the current user.

sample-result

Sample result:

%{
  "Basic" => 1483883011648,
  "Basic (and reversed card)" => 1483883011644,
  "Basic (optional reversed card)" => 1483883011631,
  "Cloze" => 1483883011630
}
@spec model_styling(%{model_name: String.t()}) :: {:ok, map()} | {:error, any()}

Gets the CSS styling for the provided model by name.

Sample param:

%{
  model_name: "Basic (and reversed card)"
}

sample-result

Sample result:

%{
   "css" => ".card {
 font-family: arial;
 font-size: 20px;
 text-align: center;
 color: black;
 background-color: white;
}
"
}
@spec model_template_add(%{
  model_name: String.t(),
  template: %{name: String.t(), Front: String.t(), Back: String.t()}
}) :: {:ok, nil} | {:error, any()}

Adds a template to an existing model by name.

If you want to update an existing template, use updateModelTemplates.

sample-param

Sample param:

%{
  model_name: "Basic",
  template: {
    Name: "Card 3",
    Front: "Front html {{Field1}}",
    Back: "Back html {{Field2}}"
  }
}

sample-result

Sample result:

{:ok, nil}
Link to this function

model_template_remove(map)

View Source
@spec model_template_remove(%{model_name: String.t(), template_name: String.t()}) ::
  {:ok, nil} | {:error, any()}

Removes a template from an existing model.

Link to this function

model_template_rename(map)

View Source
@spec model_template_rename(%{
  model_name: String.t(),
  old_template_name: String.t(),
  new_template_name: String.t()
}) :: {:ok, nil} | {:error, any()}

Renames a template in an existing model.

sample-param

Sample param:

%{
  model_name: "Basic",
  old_template_name: "Card 1",
  new_template_name: "Card 1 renamed"
}

sample-result

Sample result:

{:ok, nil}
Link to this function

model_template_reposition(map)

View Source
@spec model_template_reposition(%{
  model_name: String.t(),
  template_name: String.t(),
  index: integer()
}) ::
  {:ok, nil} | {:error, any()}

Repositions a template in an existing model.

The value of index starts at 0. For example, an index of 0 puts the template in the first position, and an index of 2 puts the template in the third position.

sample-param

Sample param:

%{
  model_name: "Basic",
  template_name: "Card 1",
  index: 1
}

sample-result

Sample result:

{:ok, nil}
@spec model_templates(%{model_name: String.t()}) :: {:ok, map()} | {:error, any()}

Returns an object indicating the template content for each card connected to the provided model by name.

Sample param:

%{
  model_name: "Basic (and reversed card)"
}

Sample result:

%{
  "Card 1" => {
    "Front" => "{{Front}}",
    "Back" => "{{FrontSide}}

<hr id=answer>

{{Back}}"
  },
  "Card 2" => {
    "Front" => "{{Back}}",
    "Back" => "{{FrontSide}}

<hr id=answer>

{{Front}}"
  }
},
Link to this function

update_model_styling(map)

View Source
@spec update_model_styling(%{model: %{name: String.t(), css: String.t()}}) ::
  {:ok, nil} | {:error, any()}

Modify the CSS styling of an existing model by name.

sample-param

Sample param:

%{
  model: {
    name: "Custom",
    css: "p { color: blue; }"
  }
}

sample-result

Sample result:

{:ok, nil}
Link to this function

update_model_templates(map)

View Source
@spec update_model_templates(%{model: %{name: String.t(), templates: map()}}) ::
  {:ok, nil} | {:error, any()}

Modify the templates of an existing model by name.

Only specifies cards and specified sides will be modified. If an existing card or side is not included in the request, it will be left unchanged.

sample-param

Sample param:

%{
  model: {
    name: "Custom",
    templates: {
      "Card 1" => {
        Front: "{{Question}}?",
        Back: "{{Answer}}!"
      }
    }
  }
}

sample-result

Sample result:

{:ok, nil}