Asset Manager [Official support]

Is there a reason why you want to include that styling with Asset Manager?

The CSS in the “Styling” section of the card editor will apply to all card templates anyway.


No. Here’s why:

Asset Manager supports HTML snippets (which can be any text string) and JS snippets, which always get wrapped with a <script> tag.

If for some reason you still really want to use Asset Manager for your CSS, put it inside of an HTML snippet and wrap it with <style>.

Is there a reason why you want to include that styling with Asset Manager?
Asset Manager supports HTML snippets (which can be any text string) and JS snippets

@kleinerpirat

Only because I assumed it was possible, given that the JS asset editor offers “CSS” as a type. See below.

Otherwise, I’m happy to continue to manage it via the Styling section of the template editor. But the documentation is silent on why “CSS” is given as a choice.

1 Like

Good point! Forgot about that :sweat_smile:

I have a fix for Asset Manager that allows bare CSS to be inserted in a <style> block when the CSS option is selected. After I test it for a bit, I’ll send a pull request.

1 Like

@OjisanSeiuchi
That would be great!
The reason why I that option exists in the first place, that at one point I wanted to add support for SCSS, however I then realized that that wouldn’t work, because SCSS cannot be compiled from a Web environment, only Node.

Hi,
I’ve figured out how to use the addon but, as I have explained in my other post:

I have a note type with around 40 cards, each of which have a slightly different template. For example, the first card might reference fields 01, 02 & 03, and the second card reference fields 02, 03 & 04, and so on.

I want to add fields that end in current card’s number minus 1 or plus 1 or 2, etc. They are in this format Field01
Is there a way to do it automatically?

Hi, I’m having trouble with getting a script to function when it’s inserted into the template via asset manager.

The Script
function doc_keyUp(e) {
    if (e.key === 'h') {
        flipVisibility('hintzu');
    }
}
document.addEventListener('keyup', doc_keyUp, false);

I can use it no problem when I include it inside normal <script></script> anywhere in the template. But when it’s inserted via asset manager, it does not function.

The function that is called on keypress:
 function flipVisibility(elementId) {
            if (document.getElementById(elementId).style.display=='none') {
                document.getElementById('hintzu') .style.display='';
            }
            else{
                document.getElementById(elementId).style.display='none';
            }
        }

And this function is also called in the onclick of a html button in the card:

<button class="hintzubutton" onclick=" flipVisibility('hintzu')" type="button">

I have tried:

  1. Inserting them both inside one script in asset manager - the button works but the keypress doesn’t
  2. Inserting them using separate scripts in asset manager - same as above.
  3. Inserting just the keypress script directly into the template using <script> and the function via asset manager - both work.

Anki Version: 2.1.41, OS: Windows, addon fully updated.

After inserting it via Asset Manager, you can still check out the template and see what Asset Manager inserted exactly. Maybe that will clear up things.

Well yes I did check the templates, and it had inserted the correct scripts.
Please disregard the previous post, because it turns out the problem is probably not with asset manager but with the script or anki itself.
I tried inserting it directly into the templates and it seems to work only sometimes. And upon further trials, it sometimes works when inserted via asset manager too.
It works every alternate time that the particular side of the card is rendered. Tested by repeatedly pressing the ‘Back Template’ Radio button in the ‘Card Types Window’, which renders that side of the card anew. Can’t figure the reason for this odd behaviour.


Also, I couldn't find it in the addon's page, but can we use asset manager to manage the styling section too?

@Bakr_10 Sorry, this was not meant as an answer to you, happened on accident.

Provided this gets merged, the insertion of CSS wrapped in <style> tags will probably be possible with the next update.

This will however still insert the content into the HTML templates (Front | Back).

Perhaps a separate tab for the stylesheet, where the content gets inserted into the Styling section of the template, would be a good addition?

1 Like

Could you send me some sample cards of your note type?
I think it could be solved with some creative renaming of the fields.


@kleinerpirat good to know, thanks

Sorry for cutting in.

Maybe the cause is that the following line registers an event listener cumulatively on the document object each time a card is displayed.

Anki Desktop only updates part of the document each time a card is displayed. Therefore, once an event listener is registered on the document, it will remain there unless you execute removeEventListener().

screenshot

There are several ways to deal with this, but in your case, I think it would be easy to check if the function exists in the global scope, and only if it does not, define the function in the global scope and execute addEventListener() as follows:

code
if (typeof doc_keyUp !== "function") {
    var doc_keyUp = function (e) {
        if (e.key === "h") {
            flipVisibility("hintzu");
        }
    };
    document.addEventListener("keyup", doc_keyUp, false);
}
3 Likes

Hi hengiesel,
Since the new Anki version is available since a couple of weeks, I hope Asset Manager will be compatible soon with the new version. I heard it doesnt work yet and i am just a little anxious that i will lose all my assets which took me a long time to create when i update anki. Anyway thanks a lot this addon makes using anki a lot easier for me.

I’ve already updated it last week for 2.1.47. So as long as your Anki is up-to-date, you should be fine :slight_smile:

1 Like

Great, thanks for the quick reply

1 Like

Asset Manager seems to be more unstable on 2.1.47 than it has been on 2.1.44. Edit: Unstable is the wrong word. It’s just less forgiving now, probably because Anki itself got a bit more strict.

I’ve encountered this error with two completely different note types on “Write to Templates”:

  File "/home/user/.local/share/Anki2/addons21/656021484/gui_config/config.py", line 58, in writeBackCurrentSetting
    self.write_back_callback(*self.export_data())
  File "/home/user/.local/share/Anki2/addons21/656021484/src/models.py", line 22, in write_back
    setup_model(model_id, html_data, script_data)
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/__init__.py", line 9, in setup_model
    setup_full(model_id, html, scripts)
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/setup_html.py", line 208, in setup_full
    insert(unminifieds, template_fmts, lambda: mw.col.models.update_dict(model))
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/minify.py", line 22, in insert_unminified
    process_minifieds(unminifieds, template_fmts, callback)
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/minifier.py", line 38, in process_minifieds
    callback()
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/setup_html.py", line 208, in <lambda>
    insert(unminifieds, template_fmts, lambda: mw.col.models.update_dict(model))
  File "anki/models.py", line 223, in update_dict
  File "anki/_backend/generated.py", line 444, in update_notetype_legacy
  File "anki/_backend/__init__.py", line 131, in _run_command
anki.errors.TemplateError: Card template ⁨1⁩ in notetype '⁨SomeNoteType⁩' has a problem.<br>See the preview for more information.

It happened instantly on my main note type, on the other one it happened after adding new card templates. I feel like it might be caused by the now more aggressive template warning system on Anki’s side.

Could you make out what the exact error message is? Maybe I could add a workaround for it.

Sadly I couldn’t, because all of this happens in the background. There’s no way to “see the preview” when using Asset Manager.

I’ll try to find a way to reproduce this issue reliably…

This may be me being dumb, but I can’t work out how to open the main window for this add on…
I’ve tried:

  • Main Anki Tools menu (where add-ons normally are)
  • All the menus within edit card

The manager is accessible via an extra button in the “Manage Notetypes” screen, but in the add-on config, you can set it to also show a quick access button (Assets) in the editor.

1 Like