From db9f377f2c49c010a53af18d3598e6bdd998221e Mon Sep 17 00:00:00 2001 From: Danny McGee Date: Wed, 11 Oct 2023 09:59:49 -0400 Subject: [PATCH] Memoize static SVG rendering --- studio-frontend/src/app/icon.element.ts | 34 +++++++++++++++---------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/studio-frontend/src/app/icon.element.ts b/studio-frontend/src/app/icon.element.ts index 949948c..5ed1409 100644 --- a/studio-frontend/src/app/icon.element.ts +++ b/studio-frontend/src/app/icon.element.ts @@ -1,5 +1,5 @@ import { match } from "@storyteller/utility"; -import { ElementPart, LitElement, html, nothing, unsafeCSS } from "lit"; +import { ElementPart, LitElement, TemplateResult, html, nothing, unsafeCSS } from "lit"; import { customElement, property } from "lit/decorators.js"; import { Directive, directive } from "lit/directive.js"; import { unsafeSVG } from "lit/directives/unsafe-svg.js"; @@ -24,24 +24,30 @@ export class IconElement extends LitElement { `; } -// TODO: Consider moving this -function renderSvg(fullSvg: string) { - console.log("parsing:", fullSvg); +const RENDER_RESULTS = new Map(); - const [, attrsString, body] = fullSvg.match(/(.+?)<\/svg>/)!; +function renderSvg(iconId: string) { + if (RENDER_RESULTS.has(iconId)) + return RENDER_RESULTS.get(iconId)!; + + const svgSrc = match (iconId, { + "circle-info": () => fasCircleInfo, + "cube": () => fasCube, + "globe": () => fasGlobe, + "up-down-left-right": () => fasUpDownLeftRight, + "rotate": () => fasRotate, + "scale": () => fasScale, + _: () => null, + }); + + if (!svgSrc) return nothing; + + const [, attrsString, body] = svgSrc.match(/(.+?)<\/svg>/)!; const attrs = Object.fromEntries( Array.from(attrsString.matchAll(/([-:_a-zA-Z0-9]+)(?:=['"]?([^'"]*)['"]?)/g)) .map(([, attr, value]) => [attr, value])