-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tsx
60 lines (54 loc) · 1.58 KB
/
main.tsx
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
import React from "npm:react";
import { renderToString } from "npm:react-dom/server";
import { html } from "https://deno.land/x/[email protected]/mod.ts";
import { htmlIndexText } from "./build/htmlIndexText.ts";
export function djSSR(reactNode: React.ReactNode) {
try {
return html`${renderToString(reactNode)}`;
} catch (error) {
return `Error: ${error}`;
}
}
/**
* pass in params
*/
export function djJSX(params: Record<string, any>) {
return function djJSXRaw(
strings: TemplateStringsArray,
...values: any[]
): ReturnType<typeof html> {
let result = "";
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
result += values[i];
}
}
return html`<iframe src="http://localhost:8578/?jsx=${
encodeURIComponent(result)
}¶ms=${
encodeURIComponent(JSON.stringify(params || {}))
}" frameBorder="1" height="200" width="100%" allow="clipboard-read *; clipboard-write *;"></iframe>`;
};
}
export function djSPA<TransferProps>(
transferProps: TransferProps,
Component: (transferProps: TransferProps) => React.ReactComponent,
) {
try {
let body = decodeURI(htmlIndexText);
body = body.replace(
`globalThis.src = "() => {}"`,
`globalThis.src = "${encodeURIComponent(Component.toString())}"`,
);
body = body.replace(
`globalThis.transferProps = "{}"`,
`globalThis.transferProps = "${
encodeURIComponent(JSON.stringify(transferProps))
}"`,
);
return html`${body}`;
} catch (error) {
console.error(`Error: ${error}`);
}
}