-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathApp.jsx
More file actions
218 lines (196 loc) · 7.87 KB
/
Copy pathApp.jsx
File metadata and controls
218 lines (196 loc) · 7.87 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
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
import React from "react";
import ReactDOM from "react-dom";
import classNames from "classnames";
import {Brand} from "./components/Brand.jsx";
import {Button} from "./components/Button.jsx";
import {FileTab} from "./components/FileTab.jsx";
import {Editor} from "./components/Editor.jsx";
import {Preview} from "./components/Preview.jsx";
import {ShareModal} from "./components/ShareModal.jsx";
import {defaultHtml, defaultConfig} from "./defaults.js";
import {useEditor} from "./hooks/useEditor.js";
import {
loadPlayground,
savePlayground,
sharePlayground,
} from "./actions/playground.js";
import {compile} from "./actions/worker.js";
// Update preview hanlder
const inject = (preview, html, css) => {
const data = {
source: "siimple-playground",
html: html,
css: css,
};
return preview.contentWindow.postMessage(data, "*");
};
const debounce = (wait, fn) => {
let timer = null;
return () => {
clearTimeout(timer);
timer = setTimeout(fn, wait || 25);
};
};
// Playground app
const App = () => {
const worker = React.useRef();
const htmlRef = React.useRef();
const configRef = React.useRef();
const previewRef = React.useRef();
// Playground data reference
const playground = React.useRef({
version: "latest",
html: defaultHtml + "\n",
config: defaultConfig + "\n",
});
const [ready, setReady] = React.useState(false);
const [tab, setTab] = React.useState("html");
const [shareUrl, setShareUrl] = React.useState("");
const [error, setError] = React.useState("");
const [theme, setTheme] = React.useState(() => localStorage.getItem("siimple:playground:theme") || "light");
// const [layout, setLayout] = React.useState(() => localStorage.getItem("siimple:playground:layout") || "both");
const htmlEditor = useEditor(htmlRef, "html");
const configEditor = useEditor(configRef, "js")
// Request CSS compile
const requestCompile = React.useMemo(() => {
return debounce(1000, () => {
setError("");
playground.current.html = htmlEditor.current.getCode();
playground.current.config = configEditor.current.getCode();
savePlayground(playground.current);
if (!worker.current) {
return;
}
compile(worker.current, playground.current.config).then(data => {
if (data.error) {
return setError(data.error.message || data.error);
}
// Inject html and css in current preview
inject(previewRef.current, playground.current.html, data.css);
});
});
}, []);
// Initialize plugins and worker
React.useEffect(() => {
htmlEditor.current.addPlugin(() => requestCompile());
configEditor.current.addPlugin(() => requestCompile());
worker.current = new Worker(new URL('./worker.js', import.meta.url));
return () => {
worker.current.terminate();
};
}, []);
React.useEffect(() => ready && requestCompile(), [ready]);
// Handle theme toggle
const handleThemeToggle = () => {
const newTheme = theme === "dark" ? "light" : "dark";
localStorage.setItem("siimple:playground:theme", newTheme);
setTheme(newTheme);
};
// Handle tab change
const handleTabChange = newTab => {
if (newTab === "config") {
playground.current.html = htmlEditor.current.getCode();
}
if (newTab === "html") {
playground.current.config = configEditor.current.getCode();
}
savePlayground(playground.current);
setTab(newTab);
};
// Handle preview loaded
const handlePreviewLoad = () => {
loadPlayground(playground.current).then(() => {
htmlEditor.current.setCode(playground.current.html);
configEditor.current.setCode(playground.current.config);
// requestCompile();
setReady(true);
});
};
// Handle share click --> generate share url
const handleShareClick = () => {
playground.current.html = htmlEditor.current.getCode();
playground.current.config = configEditor.current.getCode();
sharePlayground(playground.current).then(url => {
setShareUrl(url);
});
};
const editorPanelClass = classNames({
"is-flex has-direction-column has-w-full has-h-full has-p-6": true,
"has-bg-dark has-text-white": theme === "dark",
"has-bg-light": theme === "light",
});
const previewPanelClass = classNames({
"has-w-full has-h-full has-p-none has-bg-white": true,
"is-flex has-direction-column": true,
});
// Render app component
return (
<div className="is-flex has-direction-column-mobile has-w-screen has-h-screen">
<div className={editorPanelClass} style={{minWidth:"0px",minHeight:"0px"}}>
<div className="is-flex has-justify-between has-mb-8">
<div className="is-flex">
<Brand theme={theme} />
</div>
<div className="is-flex">
<FileTab
theme={theme}
text="HTML"
icon="code"
active={tab === "html"}
onClick={() => handleTabChange("html")}
/>
<FileTab
theme={theme}
text="Config"
icon="mug"
active={tab === "config"}
onClick={() => handleTabChange("config")}
/>
</div>
<div className="is-flex is-hidden-mobile">
<Button
theme={theme}
icon="send"
onClick={handleShareClick}
active={!!shareUrl}
/>
<Button
theme={theme}
icon="moon"
onClick={handleThemeToggle}
active={theme === "dark"}
/>
</div>
</div>
<Editor theme={theme} visible={tab === "html"} ref={htmlRef} />
<Editor theme={theme} visible={tab === "config"} ref={configRef} />
<div className="is-flex has-size-0 has-pt-4 is-semitransparent is-hidden-mobile">
<div className="has-mr-auto">
Made with <i className="si-heart" /> and <i className="si-coffee" /> using <b>siimple</b>.
</div>
<div className="">
Playground <b>v{process.env.VERSION}</b>
</div>
</div>
</div>
<div className={previewPanelClass} style={{minWidth:"0px",minHeight:"0px"}}>
{!!error && (
<div className="alert is-secondary is-radiusless">
<i className="si-exclamation-circle has-size-8" />
<div className="has-pl-4">
<h5 className="has-text-white has-mb-1">Error in siimple.config.js</h5>
<div className="has-weight-normal">{error}</div>
</div>
</div>
)}
<Preview ref={previewRef} onLoad={handlePreviewLoad} />
</div>
{/* Share modal */}
{!!shareUrl && (
<ShareModal url={shareUrl} theme={theme} onClose={() => setShareUrl("")} />
)}
</div>
);
};
// Mount app
ReactDOM.render(<App />, document.getElementById("root"));