Skip to content

Commit

Permalink
refactor with new api
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Feb 4, 2025
1 parent c86d93b commit 104491c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
Binary file modified src/d2.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ async function init() {
Modal.init();

const versionDOM = document.getElementById("hero-text-version");
versionDOM.innerHTML = `d2 version: ${d2Version()}`;
const version = JSON.parse(d2.version());
versionDOM.innerHTML = `d2 version: ${version.data}`;

// TODO defer load hero images all the way here
}
Expand Down
46 changes: 25 additions & 21 deletions src/js/modules/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ async function initMonaco() {
let initialScript = "x -> y";
const paramScript = QueryParams.get("script");
if (paramScript) {
const decodedResult = JSON.parse(d2Decode(paramScript));
if (decodedResult.result !== "") {
initialScript = decodedResult.result;
const decodedResult = JSON.parse(d2.decode(paramScript));
if (decodedResult.data?.result !== "") {
initialScript = decodedResult.data.result;
} else {
QueryParams.del("script");
}
Expand Down Expand Up @@ -188,8 +188,8 @@ async function compile() {
script += "\n";
}

const encodeResult = JSON.parse(d2Encode(script));
if (encodeResult.result == "") {
const encodeResult = JSON.parse(d2.encode(script));
if (encodeResult.data?.result == "") {
Alert.show(
`D2 encountered an encoding error. Please help improve D2 by opening an issue on&nbsp;<a href="https://github.com/terrastruct/d2/issues/new?body=${encodeURIComponent(
script
Expand All @@ -198,30 +198,34 @@ async function compile() {
);
return;
}
const encoded = encodeResult.result;
const encoded = encodeResult.data.result;
const urlEncoded = encodeURIComponent(window.location.href);

// set even if compilation or layout later fails. User may want to share debug session
QueryParams.set("script", encoded);

const compiled = d2Compile(script);
const fs = { fs: { index: script } };

const compiled = d2.compile(JSON.stringify(fs));
if (compiled) {
let parsed = JSON.parse(compiled);
if (parsed.result != "") {
script = parsed.result;
if (parsed.data) {
script = parsed.data.fs["index"];
setScript(script);
} else if (parsed.userError != "") {
parsed = JSON.parse(parsed.userError);
displayCompileErrors(parsed.errs);
unlockCompileBtn();
return;
} else if (parsed.d2Error != "") {
unlockCompileBtn();
Alert.show(
`D2 encountered a compile error. Please help improve D2 by opening an issue on&nbsp;<a href="https://github.com/terrastruct/d2/issues/new?body=${urlEncoded}">Github</a>.`,
6000
);
return;
} else if (parsed.error) {
if (parsed.error.code === 400) {
parsed = JSON.parse(parsed.error.message);
displayCompileErrors(parsed);
unlockCompileBtn();
return;
} else {
unlockCompileBtn();
Alert.show(
`D2 encountered a compile error: "${parsed.error.message}". Please help improve D2 by opening an issue on&nbsp;<a href="https://github.com/terrastruct/d2/issues/new?body=${urlEncoded}">Github</a>.`,
6000
);
return;
}
}
}
clearCompileErrors();
Expand Down

0 comments on commit 104491c

Please sign in to comment.