Skip to content

Commit

Permalink
safari support
Browse files Browse the repository at this point in the history
  • Loading branch information
CaveNightingale committed May 4, 2024
1 parent f115bba commit 1db7ac9
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/lib/component/content/Graph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
}
get _height() {
return Number(this.getAttribute("height"));
return +this.getAttribute("height");
}
get _width() {
return Math.min(Number(this.getAttribute("width")), this.clientWidth);
return Math.min(+this.getAttribute("width"), this.clientWidth);
}
recompile() {
Expand All @@ -82,7 +82,9 @@
this.prevWidth = this._width;
this.prevHeight = this._height;
this.prevDirected = this._directed;
let graph = this._directed ? new Graph() : new UndirectedGraph();
let graph = this._directed
? new Graph()
: new UndirectedGraph();
let [vertices, edges] = evaluate(
this.prevCode,
this.prevWidth,
Expand Down Expand Up @@ -120,7 +122,9 @@
displayEdgesWeight: false,
displayEdgesLabel: true,
drawEdgesAsArcs: true,
}) + GRAPH_COMMON + KATEX_COMMON;
}) +
GRAPH_COMMON +
KATEX_COMMON;
// Post-process
// Double line
let doubleCircle = this.root.querySelectorAll(".double circle");
Expand All @@ -133,6 +137,12 @@
c.parentNode.insertBefore(newCircle, c);
}
let text = this.root.querySelectorAll("text");
let textGroup = document.createElementNS(
"http://www.w3.org/2000/svg",
"g",
);
textGroup.setAttribute("class", "text");
this.root.querySelector("svg").appendChild(textGroup);
for (let t of [...text]) {
const size = (content) => {
let div = document.createElement("div");
Expand All @@ -148,10 +158,23 @@
"http://www.w3.org/2000/svg",
"foreignObject",
);
let content = katex.renderToString(t.textContent.replaceAll("'", "\\").replaceAll("$", ""));
let content = katex.renderToString(
t.textContent.replaceAll("'", "\\").replaceAll("$", ""),
);
let { width, height } = size(content);
dom.setAttribute("x", t.getAttribute("x") - width / 2);
dom.setAttribute("y", t.getAttribute("y") - height / 2);
let x = +t.getAttribute("x") - width / 2;
let y = +t.getAttribute("y") - height / 2;
let p = t.parentNode;
while (p.tagName !== "svg") {
let transform = /translate\(([^,]+),([^,]+)\)/.exec(p.getAttribute("transform"));
if (transform) {
x += +transform[1];
y += +transform[2];
}
p = p.parentNode;
}
dom.setAttribute("x", x);
dom.setAttribute("y", y);
dom.setAttribute("width", width);
dom.setAttribute("height", height);
dom.setAttribute(
Expand All @@ -162,8 +185,11 @@
"dominant-baseline",
t.getAttribute("dominant-baseline"),
);
dom.innerHTML = `${content}`;
t.parentNode.replaceChild(dom, t);
let span = document.createElement("span");
span.innerHTML = content;
dom.appendChild(span);
t.parentNode.removeChild(t);
textGroup.appendChild(dom);
}
this.pending = false;
}
Expand Down Expand Up @@ -191,4 +217,4 @@
<graph-inner {directed} {height} {width}>
<slot></slot>
</graph-inner>
</div>
</div>

0 comments on commit 1db7ac9

Please sign in to comment.