-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.js
212 lines (187 loc) · 5.52 KB
/
script.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
// ********************************************************************
// Settings
var CODEMIRROR_SETTINGS = {
value: '',
mode: 'javascript',
tabSize: 2,
lineNumbers: true,
autofocus: true,
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
};
// ********************************************************************
// Helpers
var lsGetItem = function (key) {
if (typeof localStorage !== 'undefined') {
return localStorage.getItem(key);
}
return null;
};
var lsSetItem = function (key, value) {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(key, value);
}
};
var toggling = function (container, label, callback, group) {
var storageKey = 'cssx-' + label, oldValue;
var is = lsGetItem(storageKey) === 'true';
var updateUI = function () {
container.innerHTML = (is ? '✔' : '✘') + ' ' + label;
};
container.addEventListener('click', function () {
oldValue = is;
if (group) toggling.disableAllExcept();
lsSetItem(storageKey, is = !oldValue);
updateUI();
callback(is);
});
if (group) {
toggling.toggles[storageKey] = function () {
lsSetItem(storageKey, is = false);
updateUI();
callback(is);
};
}
updateUI();
if (is) {
callback(is);
}
};
toggling.toggles = {};
toggling.disableAllExcept = function (storageKey) {
for (var key in toggling.toggles) {
if (key !== storageKey) toggling.toggles[key]();
}
};
var el = function (sel) { return document.querySelector(sel); };
var clone = function (o) { return JSON.parse(JSON.stringify(o)); };
var saveCode = function (code) {
lsSetItem('cssx-playground-code', code);
return code;
};
var getCode = function () {
return lsGetItem('cssx-playground-code') || DefaultStyles || '';
};
// ********************************************************************
// Renders
var renderEditor = function (onChange) {
var container = el('.js-code-editor');
var editor = CodeMirror(container, CODEMIRROR_SETTINGS);
editor.on('change', function () {
onChange(editor.getValue());
});
editor.setValue(getCode());
container.addEventListener('click', function () {
editor.focus();
});
return editor;
};
var renderOutput = function () {
var settings = clone(CODEMIRROR_SETTINGS), output;
settings.readOnly = true;
settings.cursorBlinkRate = -1;
output = CodeMirror(el('.js-output-editor'), settings);
return output;
};
var renderMessage = function (message) {
var container = el('.js-message');
container.style.display = 'block';
container.innerHTML = message;
};
var renderOutMessage = function () {
var container = el('.js-message');
container.style.display = 'none';
container.innerHTML = '';
};
var renderError = function (message) {
el('.js-output-editor').setAttribute('data-status', 'error');
renderMessage(message);
};
var renderOutError = function () {
el('.js-output-editor').setAttribute('data-status', '');
renderOutMessage();
};
// ********************************************************************
// Boot
var init = function () {
var ast, transpiled, output, editor;
var transpilerOpts = { minified: false };
// cssx gobal settings
cssx.domChanges(false);
cssx.minify(false);
// printing
var printIfValid = function (value, fallback) { output.setValue(!!value ? value : fallback || ''); };
var printText = function (text) { printIfValid(text); };
var printJS = function () { printText(transpiled); };
var printAST = function () { printIfValid(JSON.stringify(ast, null, 2)); };
var printCompiledCSS = function () {
var func, generatedCSS, css;
cssx.clear();
try {
func = new Function(transpiled);
renderOutError();
} catch (err) {
renderError('Error while using the transpiled code:<br />' + err.message);
return false;
}
try {
func();
renderOutError();
} catch(err) {
renderError('Error while running the transpiled code:<br />' + err.message);
return false;
}
try {
generatedCSS = cssx.getStylesheets().map(function (stylesheet) {
return stylesheet.compileImmediate().getCSS();
});
css = generatedCSS.join('');
renderOutError();
} catch(err) {
renderError('Error while fetching the generated CSS:<br />' + err.message);
return false;
}
printIfValid(css, 'The generated JavaScript do not produce any CSS.');
return true;
};
var print = printCompiledCSS;
output = renderOutput();
editor = renderEditor(updateOutput);
// render in the right part of the screen
function updateOutput(value) {
CSSXTranspiler.reset();
try {
ast = CSSXTranspiler.ast(value);
} catch(err) {
renderOutError();
renderError('Error while generating the AST:<br />' + err.message);
return false;
}
try {
transpiled = CSSXTranspiler(value, transpilerOpts);
renderOutError();
if (print()) {
saveCode(value);
}
} catch(err) {
// console.log(err);
renderError('Error while transpiling:<br />' + err.message);
return false;
}
};
// toggles
toggling(el('.js-view-ast'), 'View AST', function (value) {
print = value ? printAST : printCompiledCSS;
print();
}, true);
toggling(el('.js-view-js'), 'View JS', function (value) {
print = value ? printJS : printCompiledCSS;
print();
}, true);
toggling(el('.js-minify'), 'Minify', function (value) {
transpilerOpts.minified = value;
cssx.minify(value);
updateOutput(editor.getValue());
}, false);
};
window.onload = init;