-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.svelte
221 lines (187 loc) · 4.37 KB
/
app.svelte
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
<div class="container">
<div class="theme-switcher">
<ThemeSwitcher {theme} onClick={handleThemeChange} />
</div>
<div class="graph">
<Graph
bind:this={graph}
onColorChange={handleColorChange}
onContainerClick={handleContainerClick}
color={graphColor}
/>
</div>
<div class="editor" class:fixed={mode == 'css'}>
<Editor
content={content}
onChange={handleChange}
onToggle={handleToggle}
bind:this={editor}
/>
</div>
<div class="nav">
<Nav onSelect={handleSelect} bind:this={nav} />
</div>
</div>
<script>
import { onMount } from 'svelte';
import Graph from './components/graph.svelte';
import Editor from './components/editor.svelte';
import Nav from './components/nav.svelte';
import ThemeSwitcher from './components/theme-switcher.svelte';
import shapes from './shapes';
import Theme from './theme';
import throttle from 'lodash/throttle';
let theme = Theme.getTheme();
let graph;
let editor;
let mode = 'rule';
let colors = {
dark: '#eeeeee',
light: '#222222'
};
let excludedColor = {
light: ['#ffffff', '#fff', '#eee', '#eeeeee'],
dark: ['#000000', '#000', '#222222', '#222']
};
let nav;
let content = shapes[Math.floor(Math.random() * shapes.length)].value;
$: graphColor = setGraphColor(theme);
function setGraphColor(theme) {
let { _, color } = parseQueryString(location.search);
if (color && !excludedColor[theme].includes(color)) {
return color;
} else {
return colors[theme];
}
}
let updateHash = throttle(_updateHash, 500);
function _updateHash(value, color) {
if (history.replaceState && value.length) {
history.replaceState({}, '',
'?rule=' + encodeURIComponent(value) +
'&color=' + encodeURIComponent(color)
);
}
}
let updateCSSEditor = throttle(_updateCSSEditor, 500);
function _updateCSSEditor() {
let value = graph.getShapeStyle();
editor.update(value, { triggerChange: false });
}
function handleColorChange(value) {
graphColor = value;
updateHash(content, graphColor);
if (mode === 'css') {
updateCSSEditor();
}
}
function handleChange(value, initial) {
content = value;
graph.update(`
clip-path: @shape(
${ value }
);
`);
if (!initial) {
updateHash(value, graphColor);
}
}
function handleToggle(name) {
mode = name;
if (name == 'css') {
let value = graph.getShapeStyle();
editor.update(value, { triggerChange: false });
} else {
editor.update(content, { triggerChange: false });
}
}
function handleSelect(shape) {
mode = 'rule';
editor.setToggle('rule');
editor.update(shape);
if (history.replaceState) {
history.replaceState({}, '', '?');
}
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
function handleContainerClick() {
nav.display('collection', true);
}
function parseQueryString(str) {
let groups = str.substr(1).split('&');
let ret = {};
groups.forEach(group => {
let [name, value] = group.split('=');
ret[name] = decodeURIComponent(value);
});
return ret;
}
function handleThemeChange(value) {
theme = value;
Theme.saveTheme(value);
}
onMount(() => {
Theme.writeAttribute();
Theme.onChange(data => {
if (data.type === 'local') {
theme = data.theme;
Theme.writeAttribute();
} else {
theme = Theme.getTheme();
}
});
let { rule, color } = parseQueryString(location.search);
if (rule) {
handleChange(rule, true);
editor.update(rule);
} else {
handleChange(content, true);
}
document.body.classList.add('ready');
});
</script>
<style>
.container {
height: 100%;
padding-top: 9em;
display: flex;
flex-direction: column;
}
.editor {
margin: 0 auto;
min-width: 420px;
max-width: 90%;
}
.fixed {
max-width: 500px;
}
.graph {
margin: 0 auto 6em;
}
.nav {
margin-top: auto;
padding-top: 5em;
}
.theme-switcher {
position: absolute;
top: 0;
right: 0;
z-index: 3;
}
@media screen and (max-width: 26.25em) {
.container {
padding: 0;
}
.editor {
min-width: 95%;
width: 95%;
max-width: 95%;
}
.graph {
margin: 4.5em auto;
}
}
</style>