Skip to content

Commit 8ddba9c

Browse files
committed
perf(site): lazy loads export URI construction
- removes construction of these URI's from the regular "render" function (which should make it a bit more snappy) - lazy loads the conversion code (as it's only necessary when the user opens the context meny)
1 parent ee3f90f commit 8ddba9c

File tree

8 files changed

+56
-43
lines changed

8 files changed

+56
-43
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ docs/interpreter/smcat-online-interpreter.js: $(ONLINE_INTERPRETER_SOURCES)
7070
--minify \
7171
--sourcemap \
7272
--legal-comments=external \
73+
--outdir=docs/interpreter/ \
7374
--drop:debugger \
74-
--drop:console \
75-
--outdir=docs/interpreter/
75+
--drop:console
7676

7777
docs/grammar.html: src/parse/smcat/smcat-parser.peggy
7878
$(GRAMMKIT) --output-format html --output $@ $<

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<meta property="og:image:type" content="image/png" />
6363
<link rel="canonical" href="https://state-machine-cat.js.org">
6464
<script nonce="known-inline-script">let LOG = false;</script>
65-
<script src="interpreter/smcat-online-interpreter.js" type="module" defer integrity="sha512-XjrKKONx+efGqsQQdnrpnNMJSMHa3CfZdJxfWMxcymB9axyHYGWHf+cjkACB7o3MtjS8smZyFDRey1PGccShFw=="></script>
65+
<script src="interpreter/smcat-online-interpreter.js" type="module" defer integrity="sha512-9uVqUABZX39lSTVoklrl+ihqooSXO2OevhAUMyTpm7+50rwk+r3TKo1ROi7APG78pECbZvoLbfRwyBNkP1vcwg=="></script>
6666
<script defer src="./vendor/material.min.js" integrity="sha512-9p3qCAKlef9K+szKNCvO5wULH5EF1+RVMEYUVl9mTeWBZX4cO4w4yUi5k2g/40R7VU/IZI99b8qZjJEZKyUTAQ==" async></script>
6767
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6868
<meta name="theme-color" content="purple">

docs/interpreter/smcat-online-interpreter.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/interpreter/smcat-online-interpreter.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/interpreter/svg-to-uri-5DWZR2SQ.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/interpreter/svg-to-uri-5DWZR2SQ.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const MAX_SIGNED_SHORT = 32767;
22

3-
export const toRasterURI = (pSVG, pCallback) => {
3+
export function toRasterURI(pSVG, pCallback) {
44
const lImg = document.createElement("img");
55

66
lImg.src =
@@ -30,3 +30,12 @@ export const toRasterURI = (pSVG, pCallback) => {
3030
}
3131
});
3232
};
33+
34+
export function toVectorURI(pSVGSource) {
35+
return (
36+
"data:image/svg+xml;charset=utf-8," +
37+
encodeURIComponent(
38+
'<!DOCTYPE svg [<!ENTITY nbsp "&#160;">]>'.concat(pSVGSource),
39+
)
40+
);
41+
}

docs/smcat-online-interpreter.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { version, render as renderSmCat } from "../src/index.mjs";
2-
import { toRasterURI } from "./sitesrc/to-raster-uri";
2+
// import { toRasterURI } from "./sitesrc/to-raster-uri";
33
import { themeAttributeMap } from "./sitesrc/theme-attribute-map";
44

5+
let gSVGToURI = null;
56
const LOCALSTORAGE_KEY = `state-machine-cat-${version.split(".")[0]}`;
67
const DEFAULT_INPUTSCRIPT = `initial,
78
"media player off",
@@ -58,15 +59,6 @@ function getState(pKey, pDefault) {
5859
return lRetval;
5960
}
6061

61-
function toVectorURI(pSVGSource) {
62-
return (
63-
"data:image/svg+xml;charset=utf-8," +
64-
encodeURIComponent(
65-
'<!DOCTYPE svg [<!ENTITY nbsp "&#160;">]>'.concat(pSVGSource),
66-
)
67-
);
68-
}
69-
7062
function updateViewModel(pTarget) {
7163
return async (pEvent) => {
7264
gModel[pTarget || pEvent.target.id] =
@@ -101,19 +93,6 @@ async function showModel(pModel) {
10193
} else {
10294
document.getElementById("render").style = "";
10395
}
104-
105-
if (outputIsSaveable()) {
106-
const lSVGs = window.output.getElementsByTagName("svg");
107-
const lUniqueIshPostfix = new Date().toISOString();
108-
document.getElementById("save-svg").href = toVectorURI(lSVGs[0].outerHTML);
109-
document.getElementById("save-svg").download =
110-
`state-machine-${lUniqueIshPostfix}.svg`;
111-
toRasterURI(lSVGs[0].outerHTML, (pRasterURI) => {
112-
document.getElementById("save-png").href = pRasterURI;
113-
document.getElementById("save-png").download =
114-
`state-machine-${lUniqueIshPostfix}.png`;
115-
});
116-
}
11796
}
11897

11998
function getAttrFromQueryParams(pQueryParams) {
@@ -225,7 +204,23 @@ function setTextAreaToWindowHeight() {
225204
);
226205
}
227206

228-
function showContextMenu(pX, pY) {
207+
async function showContextMenu(pX, pY) {
208+
const lSVGs = window.output.getElementsByTagName("svg");
209+
const lUniqueIshPostfix = new Date().toISOString();
210+
211+
gSVGToURI ??= await import("./sitesrc/svg-to-uri.js");
212+
213+
document.getElementById("save-svg").href = gSVGToURI.toVectorURI(
214+
lSVGs[0].outerHTML,
215+
);
216+
document.getElementById("save-svg").download =
217+
`state-machine-${lUniqueIshPostfix}.svg`;
218+
gSVGToURI.toRasterURI(lSVGs[0].outerHTML, (pRasterURI) => {
219+
document.getElementById("save-png").href = pRasterURI;
220+
document.getElementById("save-png").download =
221+
`state-machine-${lUniqueIshPostfix}.png`;
222+
});
223+
229224
window.contextmenu.style = `display: block; position: absolute; z-index: 2; left: ${pX}px; top: ${
230225
pY - 70
231226
}px`;
@@ -300,11 +295,11 @@ window.autoRender.addEventListener("click", await updateViewModel(), false);
300295
window.desugar.addEventListener("click", await updateViewModel(), false);
301296
window.render.addEventListener("click", async () => await render(), false);
302297
window.addEventListener("resize", setTextAreaToWindowHeight);
303-
window.output.addEventListener("contextmenu", (pEvent) => {
298+
window.output.addEventListener("contextmenu", async (pEvent) => {
304299
if (outputIsSaveable()) {
305300
pEvent.preventDefault();
306301

307-
showContextMenu(pEvent.clientX, pEvent.clientY);
302+
await showContextMenu(pEvent.clientX, pEvent.clientY);
308303
}
309304
});
310305

0 commit comments

Comments
 (0)