-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
506 lines (422 loc) · 16.9 KB
/
index.html
File metadata and controls
506 lines (422 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
name="description"
content="A live playground for converting JSX or TSX code to JS using Babel then to HTML using React"
/>
<title>JSX to JS to HTML</title>
<!-- Defer favicon https://stackoverflow.com/a/13416784/865719 -->
<link id="favicon" rel="icon" href="data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=" sizes="any" type="image/svg+xml" />
<link href="https://cdn.jsdelivr.net/npm/modern-normalize@3.0.1/modern-normalize.min.css" rel="stylesheet" />
<style>
:root {
--bg: #f8f8f8;
--fg: #000000;
--border: #e5e5e5;
}
html[data-theme="dark"] {
--bg: #181818;
--fg: #e0e0e0;
--border: #2b2b2b;
}
body {
background: var(--bg);
color: var(--fg);
}
body,
html {
height: 100%;
}
body {
display: flex;
flex-direction: column;
padding: 0.75rem 1rem 1rem 1rem;
}
.container {
height: 100%;
width: 100%;
flex: 1;
display: flex;
gap: 1rem;
/* Flexbox shrink fix:
* Flex items default to min-height: auto, which prevents shrinking
* below their content's minimum size. Monaco's internal DOM has a
* ~100px min-content height, so without this, the editors won't shrink
* immediately when the window height decreases.
*/
min-height: 0;
}
.editor-panel {
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
min-width: 0;
}
.editor-label {
font-size: 0.72rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.06em;
padding-bottom: 0.3rem;
opacity: 0.55;
user-select: none;
white-space: nowrap;
}
.editor-label-sub {
font-weight: 400;
text-transform: none;
letter-spacing: 0;
opacity: 0.85;
}
.code-editor {
flex: 1;
border: 1px solid var(--border);
/* Allow editors to shrink fully inside a flex container */
min-height: 0;
min-width: 0;
}
#page-title {
font-size: 1rem;
font-weight: 600;
display: flex;
align-items: center;
justify-content: space-between;
margin: -0.25rem 0 0.25rem 0;
}
.header-actions {
display: flex;
align-items: center;
/* gap: 0.25rem; */
}
#theme-toggle,
#github-link {
padding: 0.35rem 0.75rem;
font-size: 0.9em;
border: none;
background: none;
color: var(--fg); /* theme-aware icon color */
cursor: pointer;
/* border-radius: 6px; */
/* subtle presence */
opacity: 0.7;
transition:
opacity 0.2s ease,
transform 0.1s ease;
}
#theme-toggle:hover,
#github-link:hover {
opacity: 1; /* gentle highlight */
}
</style>
<!-- Needed for monaco-editor https://stackoverflow.com/a/76553340/865719 -->
<!-- <link href="https://cdn.jsdelivr.net/npm/vscode-codicons@0.0.17/dist/codicon.min.css" rel="stylesheet"> -->
<link href="https://cdn.jsdelivr.net/npm/@vscode/codicons@0.0.44/dist/codicon.min.css" rel="stylesheet" />
<style>
/* (re)Fetch the "codicon" font from the CDN because codicon.css tries to fetch it from "./codicon.ttf"
* which fails because we don't serve it (nor do we want to serve it).
* The following is a copy/paste of the relevant part of "codicon.css" but with the CDN URL instead of "./"
*/
@font-face {
font-family: "codicon";
src: url("https://cdn.jsdelivr.net/npm/@vscode/codicons@0.0.44/dist/codicon.ttf?c7330ef9199d97dc5b8aae3449a5dc27")
format("truetype");
font-display: block;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/@babel/standalone@7.29.1/babel.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prettier@3.8.1/standalone.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prettier@3.8.1/plugins/html.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prettier@3.8.1/plugins/babel.min.js"></script>
<!-- estree seems to be needed by prettier/plugins/babel -->
<script src="https://cdn.jsdelivr.net/npm/prettier@3.8.1/plugins/estree.min.js"></script>
</head>
<body>
<h1 id="page-title">
JSX to JS to HTML Converter
<div class="header-actions">
<a
id="github-link"
href="https://github.com/maddouri/jsx2html"
target="_blank"
rel="noopener noreferrer"
aria-label="View source on GitHub"
></a>
<button id="theme-toggle" aria-label="Toggle theme">Toggle Theme</button>
</div>
</h1>
<div class="container">
<div class="editor-panel">
<div class="editor-label">1. JSX / TSX <span class="editor-label-sub">— you write this</span></div>
<div class="code-editor" id="code-editor-input"></div>
</div>
<div class="editor-panel">
<div class="editor-label">2. Compiled JS <span class="editor-label-sub">— Babel outputs this</span></div>
<div class="code-editor" id="code-editor-output-js"></div>
</div>
<div class="editor-panel">
<div class="editor-label">
3. Rendered HTML <span class="editor-label-sub">— the browser renders this</span>
</div>
<div class="code-editor" id="code-editor-output-html"></div>
</div>
</div>
<script type="module">
// Set up favicon ////////////////////////////////////////////////////////////////////////////
const faviconSvg = await loadIcon("https://cdn.jsdelivr.net/npm/lucide-static@0.575.0/icons/code-xml.svg");
// https://stackoverflow.com/a/75608527/865719
function setupFavicon(color) {
const iconlink = document.querySelector("link#favicon");
var iconcolor = "blue";
const serialized = faviconSvg.replace("currentColor", color);
const dataurl = "data:image/svg+xml;base64," + btoa(serialized);
iconlink.setAttribute("href", dataurl);
}
// The favicon will be properly set in the theme handling logic
// Set up the editors ////////////////////////////////////////////////////////////////////////
// https://stackoverflow.com/a/76553340/865719
// https://stackoverflow.com/a/79568880/865719
import * as monaco from "https://cdn.jsdelivr.net/npm/monaco-editor@0.55.1/+esm";
// Editor creation options
// https://microsoft.github.io/monaco-editor/docs.html#functions/editor_editor_api.editor.create.html
// https://microsoft.github.io/monaco-editor/docs.html#interfaces/editor_editor_api.editor.IStandaloneEditorConstructionOptions.html
const editorDefaultOptions = {
value: "",
automaticLayout: true,
stickyScroll: { enabled: false },
tabSize: 2,
insertSpaces: true,
};
const monacoInputEditor = await monaco.editor.create(document.getElementById("code-editor-input"), {
// Using JS syntax until JSX support is added upstream https://github.com/microsoft/monaco-editor/issues/264
language: "typescript",
readOnly: false,
...editorDefaultOptions,
});
const monacoOutputEditorJs = await monaco.editor.create(document.getElementById("code-editor-output-js"), {
language: "javascript",
readOnly: true,
...editorDefaultOptions,
});
const monacoOutputEditorHtml = await monaco.editor.create(document.getElementById("code-editor-output-html"), {
language: "html",
readOnly: true,
...editorDefaultOptions,
});
// Misc //////////////////////////////////////////////////////////////////////////////////////
async function loadIcon(url) {
const res = await fetch(url);
return await res.text(); // returns raw <svg>...</svg>
}
document.getElementById("github-link").innerHTML = await loadIcon(
"https://cdn.jsdelivr.net/npm/lucide-static@0.575.0/icons/github.svg",
);
// Set up the theme switching logic //////////////////////////////////////////////////////////
async function setupThemeToggle() {
const sunIconSvg = await loadIcon("https://cdn.jsdelivr.net/npm/lucide-static@0.575.0/icons/sun.svg");
const moonIconSvg = await loadIcon("https://cdn.jsdelivr.net/npm/lucide-static@0.575.0/icons/moon.svg");
const toggleBtn = document.getElementById("theme-toggle");
function setTheme(mode) {
document.documentElement.setAttribute("data-theme", mode);
monaco.editor.setTheme(mode === "dark" ? "vs-dark" : "vs");
toggleBtn.innerHTML = mode === "dark" ? sunIconSvg : moonIconSvg;
// Generate a random favicon color while we wait for an official non-hacky way of getting colors from monaco's theme
// https://github.com/microsoft/monaco-editor/issues/4031#issuecomment-1596150839
// https://css-tricks.com/snippets/javascript/random-hex-color/
const faviconColor = Math.floor(Math.random() * 16777215).toString(16);
console.log(`New favicon color: #${faviconColor}`);
setupFavicon(`#${faviconColor}`);
console.log(`Theme changed to ${mode}`);
}
let currentTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
setTheme(currentTheme);
toggleBtn.addEventListener("click", () => {
currentTheme = currentTheme === "light" ? "dark" : "light";
setTheme(currentTheme);
});
}
await setupThemeToggle();
// Set up the rendering sandbox //////////////////////////////////////////////////////////////
const appOrigin = window.location.origin;
let renderingSandboxIFrame = null;
function setupRenderingSandbox() {
const iframeHtml = `<!doctype html>
<html lang="en">
<body>
<div id="root"></div>
<script crossorigin src="https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js"><\/script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js"><\/script>
<script>
console.log("Rendering sandbox loading...");
const root = ReactDOM.createRoot(document.getElementById('root'));
window.addEventListener('message', (event) => {
try {
const fn = new Function("React", event.data.code);
const element = fn(React);
ReactDOM.flushSync(() => {
root.render(element);
});
parent.postMessage(
{ html: document.getElementById('root').innerHTML },
'${appOrigin}'
);
} catch (e) {
parent.postMessage({ error: e.message }, '${appOrigin}');
}
});
<\/script>
</body>
</html>
`;
// https://stackoverflow.com/a/44211814/865719
const blob = new Blob([iframeHtml], { type: "text/html" });
const url = URL.createObjectURL(blob);
const i = document.createElement("iframe");
i.src = url;
i.sandbox = "allow-scripts";
i.id = "rendering-sandbox";
i.style = "display:none;";
document.body.appendChild(i);
i.addEventListener("load", () => {
URL.revokeObjectURL(url);
renderingSandboxIFrame = i;
console.log("Rendering sandbox loaded");
});
console.log("Rendering sandbox configured", url);
}
setupRenderingSandbox();
// Set up the conversion system //////////////////////////////////////////////////////////////
// Wait for all libraries to load
function checkReady() {
if (Babel && renderingSandboxIFrame) {
console.log("All libraries loaded");
setupConverter();
} else {
setTimeout(checkReady, 10);
}
}
async function asyncWriteToEditor(monacoEditor, prettierParser, content) {
// https://prettier.io/docs/api
if (prettierParser) {
const prettyContent = await prettier.format(content, {
parser: prettierParser,
plugins: prettierPlugins,
htmlWhitespaceSensitivity: "ignore",
});
monacoEditor.setValue(prettyContent);
} else {
monacoEditor.setValue(content);
}
}
async function convertInputEditorContent() {
console.log("New conversion");
try {
const inputEditorContent = monacoInputEditor.getValue();
// const transformedToAutomaticRuntime = Babel.transform(inputEditorContent, {
// // presets: ['react']
// // presets: [ ["react", { runtime: "classic" }] ]
// presets: [ ["react", { runtime: "automatic" }], ["typescript", { allExtensions: true, isTSX: true }] ]
// }).code;
// await asyncWriteToEditor(monacoOutputEditorJs, "babel", transformedToAutomaticRuntime);
const transformedToClassicRuntime = Babel.transform(inputEditorContent, {
presets: [
["react", { runtime: "classic" }],
["typescript", { allExtensions: true, isTSX: true }],
],
}).code;
await asyncWriteToEditor(monacoOutputEditorJs, "babel", transformedToClassicRuntime);
function wrapTransformedCode(code) {
const ast = Babel.packages.parser.parse(code, {
sourceType: "module",
// plugins: ["javascript"]
});
const t = Babel.packages.types;
const body = ast.program.body;
if (body.length < 1) {
body.push(
t.returnStatement(t.stringLiteral('Nothing to render. Did you write an "expression" to render?')),
);
}
const last = body[body.length - 1];
if (t.isExpressionStatement(last)) {
// Replace last statement with a return
body[body.length - 1] = t.returnStatement(last.expression);
} else {
body.push(
t.returnStatement(t.stringLiteral('Nothing to render. Did you write an "expression" to render?')),
);
}
// We could wrap everything in a function
// const func = t.functionExpression(
// t.identifier("renderMe"),
// [],
// t.blockStatement(body)
// );
//
// const call = t.callExpression(func, []);
//
// return Babel.packages.generator.generate(call).code;
// For now, simply return the code block.
// It will be wrapped in a "new Function()" by the rendering sandbox
return Babel.packages.generator.generate(t.blockStatement(body)).code;
}
const transformedToClassicRuntimeWithReturn = wrapTransformedCode(transformedToClassicRuntime);
// Using appOrigin as the target origin doesn't seem to work.
// Perhaps due to the iframe's use of sandbox="allow-scripts" without "allow-same-origin" ? I'm not sure.
renderingSandboxIFrame.contentWindow.postMessage({ code: transformedToClassicRuntimeWithReturn }, "*");
} catch (e) {
monacoOutputEditorJs.setValue("Error: " + e.message);
monacoOutputEditorHtml.setValue("Error: " + e.message);
}
}
function setupConverter() {
window.addEventListener("message", async (event) => {
// The iframe uses sandbox="allow-scripts" without "allow-same-origin".
// Therefore:
// * It has an opaque origin and event.origin will be "null"
// * For now, we'll check event.source only
// Ignore unrelated messages
if (event.source !== renderingSandboxIFrame.contentWindow) {
return;
}
console.log("Received html result:", event);
console.log("event.data:", event.data);
if (event.data.error) {
monacoOutputEditorHtml.setValue("Error: " + event.data.error);
} else if (event.data.html !== undefined) {
await asyncWriteToEditor(monacoOutputEditorHtml, "html", event.data.html);
}
});
monacoInputEditor.onDidChangeModelContent(convertInputEditorContent);
// Some sample JSX
monacoInputEditor.setValue(
`const Foo = () => {
return <button>Click me from Foo</button>;
}
type BarProps = {
name: string;
};
function Bar({name}: BarProps) {
return (
<div>
<label>Hello {name} from Bar</label>
<Foo/>
</div>
);
}
// Add a JSX expression to render as HTML, e.g.:
<>
<h1>This is some JSX to render as HTML</h1>
<Bar name={"My Name"}/>
<p>Footer</p>
</>
`,
);
}
checkReady();
</script>
</body>
</html>