48 lines
990 B
TypeScript
Raw Normal View History

2019-08-30 07:04:28 +00:00
import Diff from "scripts/Diff"
export default class SVGIcon extends HTMLElement {
static cache = new Map<string, Promise<string>>()
static get observedAttributes() {
return ["name"]
}
attributeChangedCallback(attrName) {
if(attrName === "name") {
this.render()
}
}
async render() {
2019-08-31 04:45:42 +00:00
let cache = SVGIcon.cache.get(this.name)
2019-08-30 07:04:28 +00:00
if(cache) {
let text = await cache
Diff.mutations.queue(() => this.innerHTML = text)
return
}
2019-08-31 04:45:42 +00:00
SVGIcon.cache.set(this.name, new Promise(async (resolve, reject) => {
2019-08-30 07:04:28 +00:00
let url = `//media.notify.moe/images/icons/${this.name}.svg`
let response = await fetch(url)
if(!response.ok) {
console.warn(`Failed loading SVG icon: ${url}`)
reject(response.statusText)
return
}
let text = await response.text()
Diff.mutations.queue(() => this.innerHTML = text)
resolve(text)
2019-08-31 04:45:42 +00:00
}))
2019-08-30 07:04:28 +00:00
}
get name() {
return this.getAttribute("name") || ""
}
set name(value: string) {
this.setAttribute("name", value)
}
}