From bc55e975454bb03bde8973cea0abcc95bdb91153 Mon Sep 17 00:00:00 2001 From: Jon Bake Date: Sat, 18 Jun 2022 12:02:05 -0500 Subject: [PATCH] Fixes #681 - Add font awesome CSS to exported SVG --- src/lib/components/actions.svelte | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/components/actions.svelte b/src/lib/components/actions.svelte index 9f5f72bd..ed3d8cfd 100644 --- a/src/lib/components/actions.svelte +++ b/src/lib/components/actions.svelte @@ -14,7 +14,7 @@ svg?.setAttribute('height', `${height}px`); svg?.setAttribute('width', `${width}px`); // Workaround https://stackoverflow.com/questions/28690643/firefox-error-rendering-an-svg-image-to-html5-canvas-with-drawimage if (!svg) { - svg = document.querySelector('#container svg'); + svg = getSvgEl(); } const svgString = svg.outerHTML .replaceAll('
', '
') @@ -24,7 +24,7 @@ const exportImage = (event: Event, exporter: Exporter) => { const canvas: HTMLCanvasElement = document.createElement('canvas'); - const svg: HTMLElement = document.querySelector('#container svg'); + const svg: HTMLElement = getSvgEl(); const box: DOMRect = svg.getBoundingClientRect(); canvas.width = box.width; canvas.height = box.height; @@ -50,6 +50,22 @@ event.preventDefault(); }; + const getSvgEl = () => { + const svgEl: HTMLElement = document + .querySelector('#container svg') + .cloneNode(true) as HTMLElement; + const fontAwesomeCdnUrl = Array.from(document.head.getElementsByTagName('link')) + .map((l) => l.href) + .find((h) => h && h.includes('font-awesome')); + if (fontAwesomeCdnUrl == null) { + return svgEl; + } + const styleEl = document.createElement('style'); + styleEl.innerText = `@import url("${fontAwesomeCdnUrl}");'`; + svgEl.prepend(styleEl); + return svgEl; + }; + const simulateDownload = (download: string, href: string): void => { const a = document.createElement('a'); a.download = download;