/
playground.js
207 lines (189 loc) · 6.02 KB
/
playground.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
import React, { useState } from 'react';
import Layout from '@theme/Layout';
import prettier from 'prettier/standalone';
import cssParser from 'prettier/parser-postcss';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import className from 'classnames';
import { CssSyntaxError } from 'postcss';
import { RingSpinner as Loader } from '../components/editor/RingSpinner.js';
import MainEditor from '../components/editor/main';
import OutputEditor from '../components/editor/output';
import unicode from '../helper/unicode';
import { getUrlState } from '../helper/url';
import ConfigEditor from '../components/editor/config';
import InnerNav from '../components/editor/innerNav.';
import runner from '../components/editor/postcss_runner';
import editorStyles from '../components/editor/editor.module.css';
import CarbonAds from '../components/carbonAds';
import InputError from '../components/editor/inputError.js';
import styles from './styles.module.css';
export default () => {
const storedState = JSON.parse(
(typeof window !== 'undefined' &&
window.localStorage.getItem('cssnano_editor_state')) ||
null
);
const urlState = getUrlState();
const intializedState = urlState ||
storedState || {
input: '/* write your css below */',
config: `// cssnano config
{
"preset" : "lite", // online playground can use only the "lite" preset
}
`,
};
const context = useDocusaurusContext();
const { siteConfig = {} } = context;
const [theme, setTheme] = useState('vs-dark');
const [editorLoading, setEditorLoading] = useState(false);
const [output, setOutput] = useState('/* your optimized output here */');
const [input, setInput] = useState(intializedState.input);
const [config, setConfig] = useState(intializedState.config);
const [error, setError] = useState('');
function toggleTheme() {
setTheme(theme === 'light' ? 'vs-dark' : 'light');
}
function handleConfigChange(value) {
setConfig(value);
}
function handleOnInput(value) {
setInput(value);
}
function handleError(err) {
switch (err.constructor) {
case CssSyntaxError:
setError(`CssSyntaxError: ${err.reason} (${err.line}:${err.column})`);
break;
case SyntaxError:
setError(err.message.split('\n').shift());
break;
default:
setError('Unknown error. See browser console for details.');
console.error(err);
}
}
function resetError() {
setError('');
}
function format() {
try {
resetError();
const formattedInput = prettier.format(input, {
parser: 'css',
plugins: [cssParser],
});
setInput(formattedInput);
} catch (err) {
handleError(err);
}
}
function saveState() {
const serializedState = JSON.stringify({
input: input,
config: config,
});
if (typeof window !== 'undefined') {
if (window.localStorage) {
window.localStorage.setItem('cssnano_editor_state', serializedState);
}
window.location.hash = unicode.encodeToBase64(serializedState);
}
}
async function runOptimizer() {
// show the loading. editor panel loader not each editor's loader
setEditorLoading(true);
const configToSend = JSON.parse(
JSON.stringify(config.split('\n').slice(1).join('\n'))
);
const resolvedConfig = resolveConfigs(configToSend);
runner(input, resolvedConfig)
.then((res) => {
resetError();
setOutput(res.css);
})
.catch((err) => {
handleError(err);
});
setEditorLoading(false);
}
/**
* it converts the playground specific config to postcss plugins
* @param {object} playgroundConfig - config from config editor panel
* @returns {object} postcssConfig
*/
function resolveConfigs(playgroundConfig) {
if (typeof playgroundConfig !== 'object') {
// throw error
}
if (
typeof playgroundConfig.preset !== 'undefined' &&
typeof playgroundConfig.preset !== 'string'
) {
// throw toaster error
}
if (typeof playgroundConfig.plugins === 'undefined') {
if (Array.isArray(playgroundConfig.preset)) {
return [
playgroundConfig.preset[0] === 'advance'
? 'cssnano-preset-advance'
: playgroundConfig.preset[0] === 'default'
? 'cssnano-preset-default'
: 'cssnano-preset-lite',
playgroundConfig.preset.length > 1 ? playgroundConfig.preset[1] : {},
];
}
[
playgroundConfig.preset === 'advance'
? 'cssnano-preset-advance'
: playgroundConfig.preset === 'default'
? 'cssnano-preset-default'
: 'cssnano-preset-lite',
{},
];
} else {
// eslint-disable-next-line no-warning-comments
// TODO
}
return ['cssnano-preset-default', {}];
}
return (
<Layout title={`${siteConfig.title}`} description="CSSNANO - Playground">
<CarbonAds customClass="playground_position" />
<InnerNav
toggleTheme={toggleTheme}
runHandler={runOptimizer}
format={format}
save={saveState}
/>
{error && <InputError error={error} theme={theme} />}
<div
className={editorStyles.panelLoaderPlaceholder}
style={{ display: editorLoading ? 'block' : 'none' }}
>
<div className={editorStyles.panelLoaderHolder}>
<Loader />
</div>
</div>
<div className="row" style={{ margin: '0' }}>
<div className={className('col col--4', styles.editorCol)}>
<MainEditor
theme={theme}
input={input}
handleOnChange={handleOnInput}
/>
</div>
<div className={className('col col--4', styles.editorCol)}>
<ConfigEditor
theme={theme}
config={config}
handleOnChange={handleConfigChange}
/>
</div>
<div className={className('col col--4', styles.editorCol)}>
<OutputEditor theme={theme} output={output} />
</div>
</div>
</Layout>
);
};