add check to only run script on root svg

This commit is contained in:
Bernard Xie 2023-01-03 14:44:36 -08:00
parent 1dc45b64a6
commit 070b50a6e2
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C

View file

@ -1,5 +1,10 @@
window.addEventListener("DOMContentLoaded", () => {
const svgEl = document.querySelector("svg");
const svgID = "d2-svg";
svgEl.setAttribute("id", svgID);
if (document.documentElement.getAttribute("id") !== svgID) {
return;
}
let width = parseInt(svgEl.getAttribute("width"), 10);
let height = parseInt(svgEl.getAttribute("height"), 10);
let ratio;
@ -10,9 +15,7 @@ window.addEventListener("DOMContentLoaded", () => {
} else if (height > window.innerHeight) {
ratio = window.innerHeight / height;
}
// Scale svg fit to zoom
if (ratio) {
// body padding is 8px
svgEl.setAttribute("width", width * ratio - 16);
svgEl.setAttribute("height", height * ratio - 16);
}