-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
42 lines (37 loc) · 1.28 KB
/
app.js
File metadata and controls
42 lines (37 loc) · 1.28 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
document.addEventListener('DOMContentLoaded', () => {
// Elements
const header = document.querySelector('.codeplayer__nav');
const playerContainers = Array.from(document.querySelectorAll('.codeplayer__player'));
const playerToggles = Array.from(document.querySelectorAll('.codeplayer__toggle'));
const windowHeight = window.innerHeight;
const headerHeight = header.clientHeight;
const runButton = document.querySelector('#runButton');
const iframe = document.querySelector('.codeplayer__results-frame');
// Update iframe upon clicking the run button
runButton.addEventListener('click', () => {
let cssCode = document.querySelector('#cssCode').value;
let jsCode = document.querySelector('#jsCode').value;
let htmlCode = document.querySelector('#htmlCode').value;
let result = `<style>${cssCode}</style>${htmlCode}<script>${jsCode}</script>`;
let doc;
if (iframe.contentDocument) {
doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
doc = iframe.contentWindow.document;
}
else {
doc = iframe.document;
}
doc.open();
doc.writeln(result);
doc.close();
});
playerContainers.forEach(container => {
container.addEventListener('keyup', (e) => {
if (e.shiftKey && e.key == "Enter") {
runButton.click();
e.preventDefault();
}
})
});
});