-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpreviewer.js
More file actions
47 lines (47 loc) · 995 Bytes
/
previewer.js
File metadata and controls
47 lines (47 loc) · 995 Bytes
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
export const preview = (html, css, js) => {
const newWindow = window.open("");
const stylesheet = newWindow.document.createElement('STYLE');
stylesheet.innerHTML = `
body {
margin: 0;
padding: 0;
background: #fff;
}
iframe {
width:100vw;
height:100vh;
margin: 0;
padding: 0;
border: none;
}
`;
newWindow.document.head.appendChild(stylesheet)
let newJS = `
<script>
${js}
</script>
`;
if (js.includes('import')) {
newJS = `
<script type="module">
${js}
</script>
`
}
newWindow.document.body.innerHTML = `
<iframe></iframe>
`
newWindow.document.body.firstElementChild.srcdoc = `
<!DOCTYPE html>
<html>
<head>
<style>
${css}
</style>
</head>
<body>
${html}
${newJS}
</body>
</html>`
}