From 51974436fff22bcd2c37471351165179bb5daa23 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Thu, 30 May 2024 10:44:22 -0700
Subject: WIP: dynamically add references to scenario svg
---
src/import_external_resources.js | 25 ++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)
(limited to 'src')
diff --git a/src/import_external_resources.js b/src/import_external_resources.js
index aba29d2..cb1d7fb 100644
--- a/src/import_external_resources.js
+++ b/src/import_external_resources.js
@@ -1,15 +1,18 @@
import mapsheets from './assets/images/mapsheets.svg';
-import counters from './assets/images/counters.svg';
-const files = {
- 'mapsheets.svg': mapsheets,
- 'counters.svg': counters
-}
+const doc = new DOMParser().parseFromString(mapsheets, 'image/svg+xml');
+const refs = [];
-document.querySelectorAll('use[data-href*=".svg"').forEach(el => {
- const [filename] = el.dataset.href.match(/.+\.svg/g)
- , href = el.dataset.href.replace(filename, files[filename].split('/').pop())
- ;
-
- el.setAttributeNS(null, 'href', href);
+document.querySelectorAll('use[href*=".svg"').forEach(el => {
+ const fragId = el.getAttributeNS(null, 'href').split('.svg').pop();
+ const frag = doc.querySelector(fragId);
+ frag.querySelectorAll('use').forEach(el => refs.push(el.getAttributeNS(null, 'href')));
+ if (el.style.transform) frag.style.transform = el.style.transform;
+ el.replaceWith(frag);
});
+
+const refsQuery = [...new Set([...refs])].join(', ');
+const refNodes = doc.querySelectorAll(refsQuery);
+const defs = document.querySelector('defs');
+
+refNodes.forEach(n => defs.appendChild(n));
--
cgit v1.2.3