Skip to content

Commit 2eee7dc

Browse files
committed
Improve editor
1 parent 9e9dae6 commit 2eee7dc

File tree

1 file changed

+24
-3
lines changed
  • apps/components_guide_web/lib/components_guide_web/templates/react_typescript

1 file changed

+24
-3
lines changed

apps/components_guide_web/lib/components_guide_web/templates/react_typescript/editor.html.eex

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const theme = window.matchMedia &&
2727

2828
const value = `
2929
const a = 1 + 1;
30+
31+
export function Example() {
32+
return a + 4;
33+
}
3034
`.trim();
3135

3236
const types = fetch("https://workers.cloudflare.com/index.d.ts", { cache: 'force-cache' })
@@ -54,19 +58,34 @@ require(["vs/editor/editor.main"], function () {
5458
theme,
5559
minimap: false
5660
});
57-
console.log(input);
5861
const output = monaco.editor.create(document.getElementById('output'), {
5962
language: 'javascript',
6063
value: '//',
6164
theme,
6265
readOnly: true,
6366
minimap: false
6467
});
68+
const statusEl = document.getElementById('status');
69+
const resultEl = document.getElementById('result');
6570
const onEdit = () => {
6671
const body = input.getValue();
72+
73+
const encoder = new TextEncoder();
74+
const data = encoder.encode(body);
75+
crypto.subtle.digest('SHA-256', data).then(hash => {
76+
const hex = Array.from(new Uint8Array(hash), chunk => chunk.toString(16).padStart(2, '0')).join('');
77+
statusEl.textContent = `SHA256: ${hex}`;
78+
});
79+
6780
esbuildPromise
68-
.then(esbuild => esbuild.transform(body, { loader: 'jsx' }))
69-
.then(content => output.getModel().setValue(content.code))
81+
.then(esbuild => esbuild.transform(body, { loader: 'jsx', format: 'iife', globalName: 'exports', }))
82+
.then(content => {
83+
output.getModel().setValue(content.code);
84+
85+
const executor = new Function(`${content.code}; return exports.Example();`);
86+
console.log('executor', executor, executor());
87+
resultEl.textContent = JSON.stringify(executor());
88+
})
7089
.catch((err) => output.getModel().setValue(err.message.replace(/^/gm, '// $&')));
7190
/*fetch('/upload', { method: 'POST', body })
7291
.then(async (response) => {
@@ -81,6 +100,8 @@ require(["vs/editor/editor.main"], function () {
81100
model.setValue(input.getValue() + "\n");
82101
});
83102
</script>
103+
<output id=status class="block text-xs opacity-50"></output>
104+
<output id=result class="block text-xs opacity-50"></output>
84105
<div class="flex-container" id="container" style="display: flex; min-height: 100vh;">
85106
<div id="input" style="flex: 1;"></div>
86107
<div id="output" style="flex: 1;"></div>

0 commit comments

Comments
 (0)