forked from Starx-Quantum/Starxer-EditorX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (99 loc) · 2.83 KB
/
app.js
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
var html = document.getElementById('html');
var css = document.getElementById('css');
var js = document.getElementById('js');
var code = document.getElementById('output').contentWindow.document;
function compile() {
const PREFIX = 'livecode-';
const data = ['html', 'css', 'js'].map((key) => {
const prefixedKey = PREFIX + key;
const jsonValue = localStorage.getItem(prefixedKey);
if (jsonValue != null) return JSON.parse(jsonValue);
});
setInitial(data);
document.body.onkeyup = function () {
localStorage.setItem('livecode-html', JSON.stringify(html.value));
localStorage.setItem('livecode-css', JSON.stringify(css.value));
localStorage.setItem('livecode-js', JSON.stringify(js.value));
code.open();
code.writeln(
html.value +
'<style>' +
css.value +
'</style>' +
'<script>' +
js.value +
'</script>'
);
code.close();
};
}
function setInitial(data) {
let htmlContent = data[0] || '<h1> Welcome to Starxer-EditorX ! </h1>';
let cssContent =
data[1] ||
`body {
background-color: #222;
}
h1 {
color: #fff;
text-align: center;
margin-top: 10%;
}`;
let jsContent = data[2] || '';
css.value = cssContent;
js.value = jsContent;
html.value = htmlContent;
code.open();
code.writeln(
htmlContent +
'<style>' +
cssContent +
'</style>' +
'<script>' +
jsContent +
'</script>'
);
code.close();
}
compile();
document.querySelectorAll('.control').forEach((control) =>
control.addEventListener('click', (e) => {
e.target.parentElement.parentElement.classList.toggle('collapse');
e.target.classList.add('close');
e.target.parentElement.querySelector('h2').classList.toggle('hidden');
})
);
document.querySelectorAll('.clear').forEach((clear) =>
clear.addEventListener('click', (e) => {
const ele = e.target.classList[1];
document.querySelector(`#${ele}`).value = '';
localStorage.setItem(`livecode-${ele}`, JSON.stringify(''));
compile();
})
);
document.querySelectorAll('.copy-btn').forEach((copy) => {
copy.addEventListener('click', (e) => {
const temp = e.target.innerHTML;
e.target.innerText = 'Copied!';
setTimeout(function () {
e.target.innerHTML = temp;
}, 800);
});
});
document.querySelector('.copy-html').addEventListener('click', (e) => {
const code = document.querySelector('#html');
copyCode(code);
});
document.querySelector('.copy-css').addEventListener('click', (e) => {
const code = document.querySelector('#css');
copyCode(code);
});
document.querySelector('.copy-js').addEventListener('click', (e) => {
const code = document.querySelector('#js');
copyCode(code);
});
function copyCode(code) {
code.select();
document.execCommand('copy');
swal('Copied!', 'You are ready to rock', 'Darkbreaker');
}