-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
258 lines (209 loc) · 6.6 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var jsEditor, htmlEditor, cssEditor;
var isMinimize = false
const localStorageSavedKey = 'SAVED_VALUES';
const localStorageCurrentKey = 'CURRENT_VALUE';
window.onload = function() {
document.addEventListener("keydown", function(e) {
if ((navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey) && e.keyCode == 83) {
e.preventDefault();
runCode()
}
}, false);
setSavedSelect();
setTextEditors();}
function setTextEditors () {
jsEditor = setTextEditor('js-textarea','javascript');
htmlEditor = setTextEditor('html-textarea','text/html');
cssEditor = setTextEditor('css-textarea','text/css');
jsEditor.on("change", (cm, change) => {
editorChanged('jsValue', cm.getValue())
});
jsEditor.on("focus", (cm, change) => {
onfocusEditor('js-area')
});
htmlEditor.on("change", (cm, change) => {
editorChanged('htmlValue', cm.getValue())
});
htmlEditor.on("focus", (cm, change) => {
onfocusEditor('html-area')
});
cssEditor.on("change", (cm, change) => {
editorChanged('cssValue', cm.getValue())
});
cssEditor.on("focus", (cm, change) => {
onfocusEditor('css-area')
});
setDefaultValues()
}
function setTextEditor (id, type) {
return CodeMirror.fromTextArea(document.getElementById(id), {
mode: type,
styleActiveLine: true,
lineNumbers: true,
theme: 'material-darker'
});
}
function setDefaultValues () {
let localStorageValue = localStorage.getItem(localStorageCurrentKey);
let savedValue = localStorageValue ? JSON.parse(localStorageValue) : {};
let defaultHtmlExist = false
if (savedValue.jsValue) {
jsEditor.setValue(savedValue.jsValue)
} else {
jsEditor.setValue('//You can set global variables\nvar variables = {\n}')
}
if (savedValue.htmlValue) {
htmlEditor.setValue(savedValue.htmlValue)
defaultHtmlExist = true
}
if (savedValue.cssValue) {
cssEditor.setValue(savedValue.cssValue)
}
if (defaultHtmlExist) {
runCode()
}
}
function setSavedSelect (list) {
if (!list) {
let savedValues = localStorage.getItem(localStorageSavedKey)
list = savedValues ? JSON.parse(savedValues) : []
}
let select = document.getElementById('saved-select')
select.innerHTML = '';
var opt = document.createElement('option');
opt.value = -1;
opt.innerHTML = 'History';
opt.selected = true;
opt.disabled = true;
opt.hidden = true;
select.appendChild(opt);
list.forEach(item => {
var opt = document.createElement('option');
opt.value = JSON.stringify(item.value);
opt.innerHTML = item.name;
select.appendChild(opt);
});
}
function runCode () {
let htmlContent = document.getElementById('html-content');
let jsValue = jsEditor.getValue();
let cssValue = cssEditor.getValue();
let htmlValue = htmlEditor.getValue();
let variables = getVariables(jsValue);
let style = document.createElement('style');
style.innerHTML = setValuesWithVariables(cssValue, variables);
let script = document.createElement('script');
script.innerHTML = jsValue;
htmlContent.innerHTML = setValuesWithVariables(htmlValue, variables);
htmlContent.appendChild(style);
htmlContent.appendChild(script);
}
function setValuesWithVariables(value, variables) {
const matched = value.match(new RegExp('\\{{.*\\}}', 'g'))
if (matched) {
matched.forEach(variable => {
let cleanVariable = variable.replaceAll('{', '').replaceAll(' ', '').replaceAll('}', '')
if (variables[cleanVariable]) {
value = value.replace(variable, variables[cleanVariable])
}
});
}
return value;
}
function getVariables (jsValue) {
try {
const matched = jsValue.match(new RegExp('var.*variables.*=.*{[^}]*}', 'g'));
if (matched && matched.length > 0) {
return eval(matched[0].replaceAll(' ', ''));
}
return {};
}
catch(err) {
alert('There is compiler error!')
}
}
function showModal (id) {
let modal = document.getElementById(id)
modal.classList.add('show')
setTimeout(() => {
modal.classList.add('display-block')
}, 100)
}
function hideModal (id) {
let modal = document.getElementById(id)
modal.classList.remove('show')
modal.classList.remove('display-block')
}
function save () {
const savedName = document.getElementById('saved-name').value;
const jsValue = jsEditor.getValue();
const cssValue = cssEditor.getValue();
const htmlValue = htmlEditor.getValue();
if (!savedName || savedName == '') {
alert('Name is required!');
return;
}
if (htmlValue == '') {
alert('Html area is empty!');
return;
}
let localStorageValue = localStorage.getItem(localStorageSavedKey)
let savedValues = localStorageValue ? JSON.parse(localStorageValue) : []
savedValues.push({
name: savedName,
value: {
jsValue: jsValue,
cssValue: cssValue,
htmlValue: htmlValue
}
})
localStorage.setItem(localStorageSavedKey, JSON.stringify(savedValues));
setSavedSelect(savedValues)
hideModal('save-modal');
document.getElementById('saved-name').value = ''
setTimeout(() => {
alert('Changes are saved!');
}, 100);
}
function selectChanged () {
let selectValue = document.getElementById('saved-select').value
if (selectValue != -1) {
let value = JSON.parse(selectValue)
jsEditor.setValue(value.jsValue)
htmlEditor.setValue(value.htmlValue)
cssEditor.setValue(value.cssValue)
runCode()
}
}
function editorChanged (valueKey, value) {
let localStorageValue = localStorage.getItem(localStorageCurrentKey);
let savedValue = localStorageValue ? JSON.parse(localStorageValue) : {};
savedValue[valueKey] = value;
localStorage.setItem(localStorageCurrentKey, JSON.stringify(savedValue))
}
function onfocusEditor (id) {
let jsArea = document.getElementById('js-area')
let htmlArea = document.getElementById('html-area')
let cssArea = document.getElementById('css-area')
let element = document.getElementById(id)
jsArea.classList.remove('code-area-transform')
htmlArea.classList.remove('code-area-transform')
cssArea.classList.remove('code-area-transform')
element.classList.add('code-area-transform')
}
function minimize () {
let displayContent = document.getElementById('display-content')
let codeContent = document.getElementById('code-content')
if (!isMinimize) {
displayContent.classList.remove('normal')
displayContent.classList.add('maximize')
codeContent.classList.remove('normal')
codeContent.classList.add('minimize')
} else {
displayContent.classList.add('normal')
displayContent.classList.remove('maximize')
codeContent.classList.add('normal')
codeContent.classList.remove('minimize')
}
isMinimize = !isMinimize
}