index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
author | Catalin Mititiuc <webdevcat@proton.me> | 2024-05-30 10:44:22 -0700 |
---|---|---|
committer | Catalin Mititiuc <webdevcat@proton.me> | 2024-05-30 10:44:22 -0700 |
commit | 51974436fff22bcd2c37471351165179bb5daa23 (patch) | |
tree | c7582e4604da66cc0e936084d7afd6b3c3e7a671 /src/import_external_resources.js | |
parent | 1bfc83dfc6a543f0721cfc6efc04f4910ecaad05 (diff) |
WIP: dynamically add references to scenario svg
Diffstat (limited to 'src/import_external_resources.js')
-rw-r--r-- | src/import_external_resources.js | 25 |
1 files changed, 14 insertions, 11 deletions
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)); |