-
-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathrepl-page.jsx
More file actions
88 lines (79 loc) 路 2.01 KB
/
Copy pathrepl-page.jsx
File metadata and controls
88 lines (79 loc) 路 2.01 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
import { useRoute } from 'preact-iso';
import { Repl } from './repl';
import { useExample } from './repl/examples';
import { useContent, useResource } from '../../lib/use-resource';
import { useTitle, useDescription } from './utils';
import { useLanguage } from '../../lib/i18n';
import style from './repl/style.module.css';
export default function ReplPage() {
const { query } = useRoute();
const [lang] = useLanguage();
const { meta } = useContent([lang, 'repl']);
useTitle(meta.title);
useDescription(meta.description);
const [code, slug] = initialCode(query);
return (
<div class={style.repl}>
<style>{`
main {
height: 100% !important;
overflow: hidden !important;
}
footer {
display: none !important;
}
`}</style>
<Repl code={code} slug={slug} />
</div>
);
}
/**
* Go down the list of fallbacks to get initial code
*
* ?code -> ?example -> localStorage -> simple counter example
*/
function initialCode(query) {
let code, slug;
if (query.code) {
try {
code = useResource(() => querySafetyCheck(atob(query.code)), [query.code]);
} catch (e) {}
} else if (query.example) {
code = useExample([query.example]);
if (code) {
slug = query.example;
history.replaceState(
null,
null,
`/repl?example=${encodeURIComponent(slug)}`
);
}
else history.replaceState(null, null, '/repl');
}
if (!code) {
if (typeof window !== 'undefined' && localStorage.getItem('preact-www-repl-code')) {
code = localStorage.getItem('preact-www-repl-code');
} else {
slug = 'counter';
if (typeof window !== 'undefined') {
history.replaceState(
null,
null,
`/repl?example=${encodeURIComponent(slug)}`
);
}
code = useExample([slug]);
}
}
return [code, slug];
}
async function querySafetyCheck(code) {
return (
!document.referrer ||
document.referrer.indexOf(location.origin) === 0 ||
// eslint-disable-next-line no-alert
confirm('Are you sure you want to run the code contained in this link?')
)
? code
: undefined;
}