-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.svelte
325 lines (321 loc) · 8.57 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<script>
import { nanoid } from "nanoid";
import Color from "./components/Color.svelte";
import Tags from "./components/Tags.svelte";
import Card from "./components/Card.svelte";
import CheckBox from "./components/CheckBox.svelte";
import Input from "./components/Input.svelte";
import {
tags,
guiSizeEmpty,
guiSize,
guideline,
saveCardList,
} from "./common/variables";
import Button from "./components/Button.svelte";
import Empty from "./components/Empty.svelte";
import { on, emit } from "./common/events";
import { updateGuiSize,toast } from "./common/global";
import SaveCard from "./components/SaveCard.svelte";
import { onMount } from "svelte";
let active = 0;
let lineSelected = [];
let hasSelected = false;
let basedColor = "CCCCCC";
let _guideline = JSON.parse(JSON.stringify(guideline));
let _saveCardList = JSON.parse(JSON.stringify(saveCardList));
let isPreview = false;
let _guidelineName = "";
let storageActive = 1;
let defaultActive = 0;
let themes = "light";
// 重置选择
function resetSelection() {
lineSelected = [];
emit("clear-line");
}
// 导航栏选择
function handleActiveChange(event) {
active = event.detail.active;
isPreview = false;
_guideline = JSON.parse(JSON.stringify(guideline));
if(active === storageActive) {
emit("get-storage");
}else if(active === defaultActive ){
_saveCardList = JSON.parse(JSON.stringify(saveCardList));
}
resetSelection();
updateGuiSize(guiSize);
}
// 分割线选择【渲染生成分割线】
function lineActiveChange(event) {
const index = event.detail.index;
const lineIndex = lineSelected.indexOf(index);
// 判断是否已经预览
if (lineIndex === -1) {
emit("add-line", _saveCardList[index]);
lineSelected = [...lineSelected, index];
} else {
emit("delete-line", _saveCardList[index]);
lineSelected.splice(lineIndex, 1);
lineSelected = lineSelected;
}
}
// 预览更新
function previewChangeHandler(event) {
if(isPreview) {
emit('preview-line', _guideline)
} else {
emit('hide-preview-line')
}
}
// 自定义比例输入更新
function cardInputChangeHandler(event){
isPreview = false;
resetSelection();
}
// 颜色更新
function colorChange() {
emit("update-color", basedColor);
}
// 重置
function resetHandler(event) {
resetSelection();
}
// 取消选择
function clearHandler(event) {}
// 保存
function saveHandler(event) {
if(_guidelineName !== "") {
emit("save-guideline", {
id:nanoid(),
name:_guidelineName,
icon:"",
guideline:_guideline
});
resetCustomCard();
updateGuiSize(guiSize);
}else {
toast("分割线名称不能为空");
}
}
// 重置自定义画板
function resetCustomCard() {
_guideline = JSON.parse(JSON.stringify(guideline));
_guidelineName = "";
isPreview = false;
}
// 应用
function applyHandler(event) {
emit("apply-line");
}
// 监听图层选择情况
on("SELECTION_CHANGED", (hasSelection) => {
hasSelected = hasSelection;
isPreview = false;
if (!hasSelected) {
emit("CHANGE_GUI_SIZE", guiSizeEmpty);
} else {
updateGuiSize(guiSize);
}
});
// 清空所有选择
on("clear-active", () => {
resetSelection();
});
on("STORAGE", (saveCards)=>{
if(active === storageActive) {
_saveCardList = saveCards
updateGuiSize(guiSize);
}
})
onMount(()=>{
let node_dark = document.getElementsByClassName('jsdesign-dark');
if(node_dark.length !== 0) { themes = 'dark'}
})
// 监听明暗模式[切换]
window.addEventListener("message", e => {
if (e.source === window.parent.parent) {
if (e.data && e.data.themes) {
const classesToRemove = []
// 先移除原本的class
document.documentElement.classList.forEach(value => {
if (value.startsWith("jsdesign-")) {
classesToRemove.push(value)
}
})
for (const className of classesToRemove) {
document.documentElement.classList.remove(className)
}
themes = e.data.themes;
// 再添加class
document.documentElement.classList.add("jsdesign-" + e.data.themes)
}
}
})
</script>
{#if hasSelected}
<main id="main">
<!-- 头部选择栏 -->
<header>
<Tags on:activeChange={handleActiveChange} {tags} {active} />
</header>
<!-- 正文 -->
<div class="content">
{#if active === 2}
<div class="guide-lines">
<Card
bind:guideline={_guideline}
on:inputChange={cardInputChangeHandler}
/>
</div>
<div class="preview-container">
<div class="preview">
<CheckBox
on:checkboxChange={previewChangeHandler}
label={"预览"}
bind:guideline={_guideline}
bind:checked={isPreview}
/>
</div>
</div>
<!-- 输入保存名称 -->
<div class="save-name-container">
<div class="save-name">
<Input label={"分割线名称"} bind:value={_guidelineName} />
</div>
</div>
{:else if _saveCardList.length}
<SaveCard
bind:saveCardList={_saveCardList}
on:activeChange={lineActiveChange}
bind:selected={lineSelected}
{themes}
/>
<!-- 颜色选择器 -->
<div class="color-select-container">
<div class="color-select">
<Color on:colorChange={colorChange} bind:basedColor />
</div>
</div>
{:else}
<Empty text={`${active === 1 ? "自定义" : "默认"}数据为空`} />
{/if}
</div>
<footer>
<div class="manage-btn">
<div class="clear-btn">
{#if active === 0 || active === 1}
<Button
disabled={lineSelected.length === 0}
on:click={resetHandler}
text={"重置"}
/>
{:else}
<Button
disabled={lineSelected.length === 0}
on:click={clearHandler}
text={"取消"}
/>
{/if}
</div>
<div class="show-btn">
{#if active === 0 || active === 1}
<Button
disabled={lineSelected.length === 0}
class="show-btn"
on:click={applyHandler}
text={"应用"}
hasMasters
/>
{:else}
<Button
disabled={!(
(_guideline.row.scales.length > 1 &&
_guideline.column.scales.length > 0 &&
_guidelineName !== "") ||
(_guideline.column.scales.length > 1 &&
_guideline.row.scales.length > 0 &&
_guidelineName !== "")
)}
class="show-btn"
on:click={saveHandler}
text={`保存为预设`}
hasMasters
/>
{/if}
</div>
</div>
</footer>
</main>
{:else}
<!-- 没有选择任何组件时 -->
<main id="main">
<Empty text="请先选择一个画板" {themes} />
</main>
{/if}
<style>
.save-name-container {
justify-content: start !important;
}
.color-select {
width: 131px;
}
.save-name-container,
.color-select-container,
.preview-container {
display: flex;
justify-content: right;
padding: 0 12px;
margin-top: 12px;
}
.preview {
width: 48px;
}
:global(.toast) {
position: absolute;
bottom: 16px;
left: 16px;
right: 16px;
padding: 8px 16px;
border: 1px solid #909399;
border-radius: 4px;
background-color: #909399;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
color: #ffffff;
transition: all 0.5s ease-in-out;
text-align: center;
font-size: 12px;
}
:global(.toast--error) {
border: 1px solid #f56c6c;
background-color: #f56c6c;
}
:global(.toast--warning) {
border: 1px solid #e6a23c;
background-color: #e6a23c;
}
:global(.toast--success) {
border: 1px solid #67c23a;
background-color: #67c23a;
}
footer {
width: 100%;
height: 64px;
margin-top: 16px;
background-color: rgba(0, 0, 0, 0.03);
}
.manage-btn {
display: flex;
justify-content: space-between;
padding: 16px;
}
.manage-btn :global(.clear-btn) {
width: 58px;
height: 32px;
}
.manage-btn :global(.show-btn) {
flex: 1;
margin-left: 12px;
}
</style>