Skip to content

Commit

Permalink
Merge pull request #365 from moonbitlang/zhiyuan/update-moonpad-monaco
Browse files Browse the repository at this point in the history
update moonpad-monaco, support safari
  • Loading branch information
bzy-debug authored Dec 17, 2024
2 parents 5a5050e + 713606a commit f28fee2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 95 deletions.
2 changes: 1 addition & 1 deletion moonbit-tour/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"vite": "^6.0.1"
},
"dependencies": {
"@moonbit/moonpad-monaco": "^0.1.202412060",
"@moonbit/moonpad-monaco": "^0.1.202412160",
"monaco-editor-core": "^0.52.0"
}
}
41 changes: 9 additions & 32 deletions moonbit-tour/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,23 @@ const codePre = document.querySelector<HTMLPreElement>(".shiki")!;

const model = monaco.editor.createModel(codePre.textContent ?? "", "moonbit");

function lineTransformStream() {
let buffer = "";
return new TransformStream<string, string>({
transform(chunk, controller) {
buffer += chunk;
const lines = buffer.split("\n");
buffer = lines.pop() ?? buffer;
for (const line of lines) {
controller.enqueue(line);
}
},
flush(controller) {
if (buffer.length > 0) {
controller.enqueue(buffer);
}
controller.terminate();
},
});
}

const output = document.querySelector<HTMLPreElement>("#output")!;

async function run() {
const content = model.getValue();
const result = await moon.compile({ libContents: [content] });
switch (result.kind) {
case "success": {
const wasm = result.wasm;
const stream = await moon.run(wasm);
const js = result.js;
const stream = await moon.run(js);
let buffer = "";
await stream
.pipeThrough(new TextDecoderStream("utf-16"))
.pipeThrough(lineTransformStream())
.pipeTo(
new WritableStream({
write(chunk) {
buffer += `${chunk}\n`;
},
}),
);
await stream.pipeTo(
new WritableStream({
write(chunk) {
buffer += `${chunk}\n`;
},
}),
);
output.textContent = buffer;
return;
}
Expand Down
Loading

0 comments on commit f28fee2

Please sign in to comment.