omardelarosa / obsidian-mtg

A plugin for managing Magic: The Gathering decks and card lists as Obsidian notes
MIT License
30 stars 5 forks source link

Display mana costs (with symbols) before card name #34

Open Ascariel1984 opened 6 months ago

Ascariel1984 commented 6 months ago

First of all, great plugin, I love it!

It would be great for an overview, if the mana value is displayed to the left side of the card name! If can be done with the actual mana symbols, that would be awesome!

Thank you!

Ascariel1984 commented 6 months ago

image I made it this far, but unfortunately this is where my JS skills end. I could not figure out how to use the scryfall api to get the actual symbols as svg

Ascariel1984 commented 6 months ago

So, I managed to do it finally, sorry for the noise. If you are interested, below is the code and here is the result:

image

          const cardManaEl = createSpan(lineEl, {
            cls: "mana-cost"
          });

and

            const svg_list = [];
            let parts = cardInfo.mana_cost.replace(/\//g, "").split("{").slice(1);
            for (let x of parts) {
              svg_list.push("https://svgs.scryfall.io/card-symbols/" + x.slice(0,-1) + ".svg");
            }
            for (let x of svg_list) {
              fetch(x)
                .then(response => response.text())
                .then(svgData => {
                  var svgElement = document.createElement('div');
                  svgElement.innerHTML = svgData;
                  var svg = svgElement.querySelector('svg');
                  svg.setAttribute('width', 18);
                  svg.setAttribute('height', 18);
                  cardManaEl.appendChild(svg);
                });
            }
          } else {
            cardNameEl.textContent = `${cardInfo && cardInfo.name || line.cardName || UNKNOWN_CARD}`;
          }