-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApp.vue
372 lines (353 loc) · 9.81 KB
/
App.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<script setup lang="ts">
import { Pane, Splitpanes } from 'splitpanes'
import { useCodeStore, useGlobalStore } from '@/store'
import { toggleDarkAnimate, useMobile } from '@/hooks'
import { isObject } from '@/utils'
import 'splitpanes/dist/splitpanes.css'
const ExportImage = defineAsyncComponent(() => import('@/components/async/ExportImage.vue'))
const FieldsCustom = defineAsyncComponent(() => import('@/components/async/FieldsCustom.vue'))
const NodeDialog = defineAsyncComponent(() => import('@/components/async/NodeDialog.vue'))
const LayoutCustom = defineAsyncComponent(() => import('@/components/async/LayoutCustom.vue'))
const { originCode, formatCode, json, jsonValid } = toRefs(useCodeStore())
const { isDark, paneSize, autoRender, isExpandEditor } = toRefs(
useGlobalStore(),
)
const { toggleEditor, toggleExecuteMode, toggleLanguage } = useGlobalStore()
const drawerVisible = ref(false)
function openLayoutConfig() {
drawerVisible.value = !drawerVisible.value
}
const ratio = ref(1)
const ratioText = computed(() => {
return `${(ratio.value * 100).toFixed(2)}%`
})
const isMobile = useMobile()
// 节点展开/收起
const [isExpandNode, toggleNode] = useToggle(true)
const editorIconStyle = computed(() => {
if (isMobile.value) {
return {
transform: `rotate(${isExpandEditor.value ? '90deg' : '270deg'})`,
}
}
else {
return { transform: `rotate(${isExpandEditor.value ? '0deg' : '180deg'})` }
}
})
function onUpdateCode(codeStr: string) {
originCode.value = codeStr
}
const debounceUpdate = useDebounceFn(onUpdateCode, 500)
// 导出json
function onExport() {
const filename = 'json-viewer.json'
const jsonStr = isObject(json.value)
? JSON.stringify(json.value, undefined, 2)
: json.value
const blob = new Blob([jsonStr as any], { type: 'text/plain' })
const link = document.createElement('a')
link.setAttribute('style', 'display: none')
link.href = URL.createObjectURL(blob)
link.download = filename
link.click()
}
// 导入json
function onImport() {
const input = document.createElement('input')
input.setAttribute('type', 'file')
input.setAttribute('accept', '.json')
input.click()
input.onchange = () => {
const file = input.files?.[0]
if (!file)
return
const reader = new FileReader()
reader.readAsText(file)
reader.onload = () => {
const jsonStr = reader.result
originCode.value = jsonStr as string
}
}
}
const jsonCanvasRef = ref()
const exportVisible = ref(false)
function onRender() {
jsonCanvasRef.value.render()
}
function showExportImage() {
exportVisible.value = true
}
function confirmExport(config: any) {
exportVisible.value = false
const { name, type, padding, backgroundColor } = config
jsonCanvasRef?.value?.graph?.downloadFullImage(name, type, {
padding,
backgroundColor,
})
}
function onZoomOut() {
if (jsonCanvasRef?.value) {
(jsonCanvasRef?.value?.toolbar as any).zoomOut()
ratio.value = jsonCanvasRef?.value?.graph.getZoom()
}
}
function onZoomIn() {
if (jsonCanvasRef?.value) {
(jsonCanvasRef?.value?.toolbar as any).zoomIn()
ratio.value = jsonCanvasRef?.value?.graph.getZoom()
}
}
function onAutoZoom() {
if (jsonCanvasRef?.value) {
(jsonCanvasRef?.value?.toolbar as any).autoZoom()
ratio.value = jsonCanvasRef?.value?.graph.getZoom()
}
}
// 关键词搜索
const nodeDetailVisible = ref(false)
const nodeDetail = ref({})
function nodeClickHandler(node: any) {
nodeDetail.value = node
nodeDetailVisible.value = true
}
// 全屏/退出全屏
const isFullScreen = ref(false)
function onFullScreen() {
isFullScreen.value = !isFullScreen.value
if (isFullScreen.value)
document.documentElement.requestFullscreen()
else document.exitFullscreen()
}
// 自定义需要额外解析的字段
const fieldsVisible = ref(false)
function openFieldsDialog() {
fieldsVisible.value = true
}
const editorIconList = [
{
icon: 'icon-node-layout',
content: 'layoutConfig',
onClick: openLayoutConfig,
},
{
icon: 'icon-zidingyi',
content: 'parseField',
onClick: openFieldsDialog,
},
{
icon: 'icon-import-json',
content: 'import',
onClick: onImport,
},
{
icon: 'icon-export-json',
content: 'export',
onClick: onExport,
},
{
icon: 'icon-clear-json',
content: 'clear',
onClick: () => {
originCode.value = ''
},
},
]
const canvasIconList = [
{
icon: 'icon-jia',
content: 'zoomIn',
onClick: onZoomOut,
},
{
icon: 'icon-jian',
content: 'zoomOut',
onClick: onZoomIn,
},
{
icon: 'icon-center-focus',
content: 'center',
onClick: onAutoZoom,
},
{
icon: 'icon-save-image',
content: 'saveImage',
onClick: showExportImage,
},
{
icon: 'icon-language',
content: 'language',
onClick: toggleLanguage,
},
]
</script>
<template>
<div>
<Splitpanes
class="default-theme"
style="height: 100vh"
:horizontal="isMobile"
>
<Pane max-size="50" :size="paneSize[0]">
<div
border="b gray/20"
class="h-10 flex items-center bg-gray/10 px-2 space-x-3"
>
<template v-for="item, idx in editorIconList" :key="idx">
<el-tooltip :content="$t(item.content)">
<span class="iconfont" :class="item.icon" @click="item.onClick" />
</el-tooltip>
</template>
<el-tooltip :content="$t(autoRender ? 'autoRender' : 'manualRender')">
<span
class="iconfont icon-auto"
:class="autoRender ? '!text-green-500' : ''"
@click="toggleExecuteMode()"
/>
</el-tooltip>
<el-tooltip content="渲染">
<Transition name="slide">
<el-button
v-show="!autoRender"
class="iconfont icon-execute"
:class="[
!autoRender && jsonValid ? '!text-green-500' : '',
jsonValid ? '' : '!text-gray-300',
]"
link
:disabled="!jsonValid"
@click="onRender"
/>
</Transition>
</el-tooltip>
</div>
<VueCodeMirror
:value="formatCode"
:style="{ height: 'calc(100% - 40px);' }"
@update-code="debounceUpdate"
/>
</Pane>
<Pane :size="paneSize[1]">
<div h-full>
<div
border="b gray/20"
class="bg-gray/10 px-2 md:(h-10 flex-between-center)"
>
<div class="flex-y-center space-x-3">
<el-tooltip
:content="`${
isExpandEditor ? $t('collapse') : $t('expand')
}${$t('editor')}`"
>
<span
class="iconfont icon-editor-expand"
:style="editorIconStyle"
@click="toggleEditor()"
/>
</el-tooltip>
<el-tooltip
:content="`${isExpandNode ? $t('collapse') : $t('expand')}${$t(
'nodes',
)}`"
>
<span
class="iconfont"
:class="
isExpandNode ? 'icon-node-collapse' : 'icon-node-expand'
"
@click="toggleNode()"
/>
</el-tooltip>
<template v-for="item, idx in canvasIconList" :key="idx">
<el-tooltip :content="$t(item.content)">
<span
class="iconfont"
:class="item.icon"
@click="item.onClick"
/>
</el-tooltip>
</template>
<el-tooltip
:content="`${isFullScreen ? $t('exit') : $t('enter')}${$t(
'fullscreen',
)}`"
>
<span
class="iconfont"
:class="
isFullScreen
? 'icon-exit-fullscreen'
: 'icon-enter-fullscreen'
"
@click="onFullScreen"
/>
</el-tooltip>
<span
class="iconfont"
:class="isDark ? 'icon-night' : 'icon-day'"
@click="toggleDarkAnimate"
/>
<ColorPicker>
<template #icon>
<span class="iconfont icon-theme" />
</template>
</ColorPicker>
<div op60>
{{ ratioText }}
</div>
</div>
<SearchInput />
</div>
<JsonCanvas
ref="jsonCanvasRef"
v-model:ratio="ratio"
:is-expand="isExpandNode"
:is-expand-editor="isExpandEditor"
@node-click="nodeClickHandler"
/>
</div>
</Pane>
</Splitpanes>
<ExportImage v-if="exportVisible" v-model="exportVisible" @confirm="confirmExport" />
<FieldsCustom v-if="fieldsVisible" v-model="fieldsVisible" />
<NodeDialog v-if="nodeDetailVisible" v-model="nodeDetailVisible" :node-detail="nodeDetail" />
<LayoutCustom v-if="drawerVisible" v-model="drawerVisible" />
</div>
</template>
<style lang="scss" scoped>
* {
font-size: 13px;
}
.splitpanes {
&.default-theme {
.splitpanes__pane {
background-color: transparent;
}
:deep(.splitpanes__splitter) {
background-color: #d1d5db26;
border-color: #d1d5db26 !important;
}
}
}
.iconfont {
font-size: 18px;
cursor: pointer;
transition: all 0.2s;
color: #9ca3af;
&:hover {
color: #222;
}
}
.dark .iconfont:hover {
color: #fff;
}
.slide-enter-active,
.slide-leave-active {
transition: all 0.15s ease-in-out;
}
.slide-enter-from {
transform: translateY(-100%);
}
.slide-leave-to {
transform: translateY(-100%);
}
</style>