/
Editor.vue
313 lines (295 loc) · 8.82 KB
/
Editor.vue
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
<script setup lang="ts">
// @ts-expect-error missing types
import { Pane, Splitpanes } from 'splitpanes'
import { isDark } from '../logics/dark'
import { customConfigError, customConfigRaw, getHint, inputHTML, output, showPreflights, transformedHTML } from '../logics/uno'
import { defaultConfigRaw, defaultHTML } from '../defaults'
import { STORAGE_KEY, options } from '../logics/url'
import { version } from '../../../package.json'
import { useCSSPrettify, useHTMLPrettify, useJSPrettify } from '../../../packages/inspector/client/composables/usePrettify'
const { copy, copied } = useClipboard()
const panel = ref()
const loading = ref(true)
const TITLE_HEIGHT = 30
const { height: vh } = useElementSize(panel)
const titleHeightPercent = computed(() => TITLE_HEIGHT / vh.value * 100)
const getInitialPanelSizes = (percent: number): number[] => {
return [
100 - percent * 2,
percent,
percent,
]
}
if (!inputHTML.value)
inputHTML.value = defaultHTML
if (!customConfigRaw.value)
customConfigRaw.value = defaultConfigRaw
const panelSizes = useLocalStorage<number[]>(
'unocss-panel-sizes',
getInitialPanelSizes(titleHeightPercent.value),
{ listenToStorageChanges: false },
)
function handleShare() {
const url = new URL(window.location.href)
const lastSearch = localStorage.getItem(STORAGE_KEY)
if (!url.search && lastSearch)
url.search = lastSearch
copy(url.toString())
}
function handleReset() {
// eslint-disable-next-line no-alert
if (confirm('Reset all settings? It can NOT be undone.')) {
inputHTML.value = defaultHTML
customConfigRaw.value = defaultConfigRaw
options.value.transform = false
}
}
function handleResize(event: ({ size: number })[]) {
panelSizes.value = event.map(({ size }) => size)
}
function isCollapsed(index: number) {
return panelSizes.value[index] <= titleHeightPercent.value + 3
}
function togglePanel(index: number) {
if (isCollapsed(index))
panelSizes.value[index] = 100 / panelSizes.value.length
else
panelSizes.value[index] = titleHeightPercent.value
normalizePanels()
}
function normalizePanels() {
const ignoredIndex: number[] = []
let originalSum = 0
let ignoredSum = 0
panelSizes.value.forEach((v, idx) => {
if (v <= titleHeightPercent.value) {
ignoredIndex.push(idx)
ignoredSum += v
}
else {
originalSum += v
}
})
const resize = (100 - ignoredSum) / originalSum
panelSizes.value.forEach((v, idx) => {
if (ignoredIndex.includes(idx))
return
panelSizes.value[idx] *= resize
})
}
const formatHTML = () => {
inputHTML.value = useHTMLPrettify(options.value.transform ? transformedHTML : inputHTML).value
}
const formatConfig = () => {
customConfigRaw.value = useJSPrettify(customConfigRaw).value
}
const isCSSPrettify = ref(false)
const cssFormatted = useCSSPrettify(
computed(() => output.value?.getLayers(undefined,
showPreflights.value
? undefined
: ['preflights'],
)),
isCSSPrettify,
)
watch(
titleHeightPercent,
(value: number) => {
panelSizes.value = getInitialPanelSizes(value)
},
)
onMounted(() => {
// prevent init transition
setTimeout(() => {
loading.value = false
}, 200)
})
</script>
<template>
<Splitpanes ref="panel" :class="{ loading }" horizontal @resize="handleResize">
<Pane :min-size="titleHeightPercent * 2" :size="panelSizes[0]" flex flex-col min-h-65px>
<div class="flex flex-wrap bg-$cm-background">
<div
class="flex items-center px-2 op-60 bg-gray/10"
border="l t gray-400/20" h-36px w-full
>
<div flex items-center gap-2>
<img src="/icon-gray.svg" w-4 h-4alt="">
<div text-sm>
UnoCSS Playground
</div>
<div text-xs op50>
v{{ version }}
</div>
</div>
<div class="pl-1 ml-auto space-x-2 text-sm md:text-base flex items-center flex-nowrap">
<button
:class="copied ? 'i-ri-checkbox-circle-line text-green' : 'i-ri-share-line'"
icon-btn
title="Share Link"
@click="handleShare"
/>
<button
i-ri-eraser-line
icon-btn
title="Reset To Default"
@click="handleReset"
/>
<a
i-ri-search-line icon-btn
href="https://uno.antfu.me"
target="_blank"
title="Interactive Docs"
/>
<button
i-ri-device-line
icon-btn
title="Responsive"
@click="options.responsive = !options.responsive"
/>
<a
i-ri-github-line icon-btn
href="https://github.com/unocss/unocss"
target="_blank"
title="GitHub"
/>
<button
i-ri-sun-line
dark:i-ri-moon-line
icon-btn
title="Toggle Color Mode"
@click="isDark = !isDark"
/>
</div>
</div>
<TitleBar
title="HTML" w-full relative
@title-click="togglePanel(0)"
>
<template #before>
<div
class="flex-shrink-0 i-ri-arrow-right-s-line mr-1 transition-transform transform"
:class="isCollapsed(0) ? '' : 'rotate-90'"
/>
</template>
<div
flex justify-end items-center w-full gap2
transition duration-400
:class="isCollapsed(0) ? 'op0' : ''"
un-children="inline-flex items-center cursor-pointer gap-1"
>
<label>
<input v-model="options.transform" type="checkbox">
<span text-sm>Transform</span>
</label>
<div w-1px h-28px my--1 bg-gray:20 />
<button
i-ri-mist-line icon-btn
title="Format"
@click="formatHTML"
/>
</div>
</TitleBar>
</div>
<CodeMirror
flex-auto
mode="htmlmixed"
class="scrolls border-(l gray-400/20)"
:matched="output?.matched || new Set()"
:get-hint="getHint"
:read-only="options.transform"
:model-value="options.transform ? transformedHTML : inputHTML"
@update:model-value="inputHTML = $event"
/>
</Pane>
<Pane :min-size="titleHeightPercent" :size="panelSizes[1]" flex flex-col min-h-28px relative>
<TitleBar
title="Config"
@title-click="togglePanel(1)"
>
<template #before>
<div
class="flex-shrink-0 i-ri-arrow-right-s-line mr-1 transition-transform transform"
:class="isCollapsed(1) ? '' : 'rotate-90'"
/>
</template>
<div
flex flex-1 justify-end items-center w-full gap2
transition duration-400
:class="isCollapsed(1) ? 'op0' : ''"
un-children="inline-flex items-center cursor-pointer gap1"
>
<div w-1px h-28px my--1 bg-gray:20 />
<button
i-ri-mist-line icon-btn
title="Format"
@click="formatConfig"
/>
</div>
</TitleBar>
<CodeMirror
v-model="customConfigRaw"
flex-auto
mode="javascript"
border="l gray-400/20"
class="scrolls"
/>
<div
v-if="customConfigError"
absolute
left-0
right-0
bottom-0
p="x-2 y-1"
bg="red-400/20"
text="red-400 sm"
>
{{ customConfigError.toString() }}
</div>
</Pane>
<Pane :min-size="titleHeightPercent" :size="panelSizes[2]" flex flex-col min-h-28px>
<TitleBar
title="Output CSS"
@title-click="togglePanel(2)"
>
<template #before>
<div
class="flex-shrink-0 i-ri-arrow-right-s-line mr-1 transition-transform transform"
:class="isCollapsed(2) ? '' : 'rotate-90'"
/>
</template>
<div
flex justify-end items-center w-full gap2
transition duration-400
:class="isCollapsed(2) ? 'op0' : ''"
un-children="inline-flex items-center cursor-pointer gap1"
>
<label>
<input v-model="showPreflights" type="checkbox">
<span text-sm>Preflights</span>
</label>
<label>
<input v-model="isCSSPrettify" type="checkbox">
<span text-sm>Prettify</span>
</label>
</div>
</TitleBar>
<CodeMirror
:model-value="cssFormatted"
flex-auto
mode="css"
border="l gray-400/20"
class="scrolls"
:read-only="true"
/>
</Pane>
</Splitpanes>
</template>
<style>
.splitpanes.loading .splitpanes__pane {
transition: none !important;
}
[icon-btn=""] {
--at-apply: text-xl op75 hover:op100;
}
</style>