@svadmin/ui 0.120.0 → 0.121.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app.css +136 -0
- package/dist/app.theme.css +136 -0
- package/dist/components/AboutSettings.svelte +1 -1
- package/dist/components/ContextMenu.svelte +268 -0
- package/dist/components/ContextMenu.svelte.d.ts +22 -0
- package/dist/components/JsonViewer.svelte +323 -0
- package/dist/components/JsonViewer.svelte.d.ts +20 -0
- package/dist/components/KpiCard.svelte +250 -0
- package/dist/components/KpiCard.svelte.d.ts +44 -0
- package/dist/components/OtpInput.svelte +209 -0
- package/dist/components/OtpInput.svelte.d.ts +17 -0
- package/dist/components/RecordCompareModal.svelte +428 -0
- package/dist/components/RecordCompareModal.svelte.d.ts +42 -0
- package/dist/components/ResultPage.svelte +166 -0
- package/dist/components/ResultPage.svelte.d.ts +28 -0
- package/dist/components/SignaturePad.svelte +230 -66
- package/dist/components/SignaturePad.svelte.d.ts +6 -5
- package/dist/components/Tour.svelte +71 -13
- package/dist/components/Tour.svelte.d.ts +1 -1
- package/dist/components/TrendIndicator.svelte +21 -13
- package/dist/components/TrendIndicator.svelte.d.ts +4 -2
- package/dist/components/UnsavedChangesGuard.svelte +138 -0
- package/dist/components/UnsavedChangesGuard.svelte.d.ts +32 -0
- package/dist/components/WatermarkContainer.svelte +196 -0
- package/dist/components/WatermarkContainer.svelte.d.ts +20 -0
- package/dist/components/useUnsavedChanges.d.svelte.ts +1 -0
- package/dist/components/useUnsavedChanges.svelte.d.ts +22 -0
- package/dist/components/useUnsavedChanges.svelte.js +105 -0
- package/dist/index.d.ts +19 -19
- package/dist/index.js +14 -2
- package/package.json +1 -1
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount, onDestroy, type Snippet } from 'svelte';
|
|
3
|
+
import { cn } from '../utils.js';
|
|
4
|
+
|
|
5
|
+
export interface ContextMenuItem {
|
|
6
|
+
key: string;
|
|
7
|
+
label: string;
|
|
8
|
+
icon?: string | Snippet;
|
|
9
|
+
shortcut?: string;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
danger?: boolean;
|
|
12
|
+
divider?: boolean;
|
|
13
|
+
children?: ContextMenuItem[];
|
|
14
|
+
onclick?: (item: ContextMenuItem) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ContextMenuProps {
|
|
18
|
+
items: ContextMenuItem[];
|
|
19
|
+
disabled?: boolean;
|
|
20
|
+
class?: string;
|
|
21
|
+
onselect?: (item: ContextMenuItem) => void;
|
|
22
|
+
children?: Snippet;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let {
|
|
26
|
+
items = [],
|
|
27
|
+
disabled = false,
|
|
28
|
+
class: className = '',
|
|
29
|
+
onselect,
|
|
30
|
+
children,
|
|
31
|
+
}: ContextMenuProps = $props();
|
|
32
|
+
|
|
33
|
+
let isOpen = $state(false);
|
|
34
|
+
let menuX = $state(0);
|
|
35
|
+
let menuY = $state(0);
|
|
36
|
+
let activeSubmenuKey = $state<string | null>(null);
|
|
37
|
+
|
|
38
|
+
let containerEl = $state<HTMLDivElement | null>(null);
|
|
39
|
+
let menuEl = $state<HTMLDivElement | null>(null);
|
|
40
|
+
|
|
41
|
+
function closeMenu() {
|
|
42
|
+
isOpen = false;
|
|
43
|
+
activeSubmenuKey = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function handleContextMenu(e: MouseEvent) {
|
|
47
|
+
if (disabled || items.length === 0) return;
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
e.stopPropagation();
|
|
50
|
+
|
|
51
|
+
activeSubmenuKey = null;
|
|
52
|
+
const clientX = e.clientX;
|
|
53
|
+
const clientY = e.clientY;
|
|
54
|
+
|
|
55
|
+
const estimatedWidth = 190;
|
|
56
|
+
const estimatedHeight = items.length * 36;
|
|
57
|
+
const winW = typeof window !== 'undefined' ? window.innerWidth : 1024;
|
|
58
|
+
const winH = typeof window !== 'undefined' ? window.innerHeight : 768;
|
|
59
|
+
|
|
60
|
+
let posX = clientX;
|
|
61
|
+
let posY = clientY;
|
|
62
|
+
|
|
63
|
+
if (clientX + estimatedWidth > winW - 8) {
|
|
64
|
+
posX = Math.max(8, clientX - estimatedWidth);
|
|
65
|
+
}
|
|
66
|
+
if (clientY + estimatedHeight > winH - 8) {
|
|
67
|
+
posY = Math.max(8, clientY - estimatedHeight);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
menuX = posX;
|
|
71
|
+
menuY = posY;
|
|
72
|
+
isOpen = true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function handleItemClick(item: ContextMenuItem, e: MouseEvent) {
|
|
76
|
+
if (item.disabled || item.divider) return;
|
|
77
|
+
if (item.children && item.children.length > 0) {
|
|
78
|
+
// Toggle or keep submenu open
|
|
79
|
+
activeSubmenuKey = activeSubmenuKey === item.key ? null : item.key;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
e.stopPropagation();
|
|
84
|
+
item.onclick?.(item);
|
|
85
|
+
onselect?.(item);
|
|
86
|
+
closeMenu();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function handleSubItemClick(subItem: ContextMenuItem, e: MouseEvent) {
|
|
90
|
+
if (subItem.disabled || subItem.divider) return;
|
|
91
|
+
e.stopPropagation();
|
|
92
|
+
subItem.onclick?.(subItem);
|
|
93
|
+
onselect?.(subItem);
|
|
94
|
+
closeMenu();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function handleGlobalClick(e: MouseEvent) {
|
|
98
|
+
if (!isOpen) return;
|
|
99
|
+
if (menuEl && !menuEl.contains(e.target as Node)) {
|
|
100
|
+
closeMenu();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function handleGlobalKeyDown(e: KeyboardEvent) {
|
|
105
|
+
if (!isOpen) return;
|
|
106
|
+
if (e.key === 'Escape') {
|
|
107
|
+
closeMenu();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function handleGlobalScroll() {
|
|
112
|
+
if (isOpen) {
|
|
113
|
+
closeMenu();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
onMount(() => {
|
|
118
|
+
if (typeof window !== 'undefined') {
|
|
119
|
+
window.addEventListener('click', handleGlobalClick);
|
|
120
|
+
window.addEventListener('keydown', handleGlobalKeyDown);
|
|
121
|
+
window.addEventListener('scroll', handleGlobalScroll, true);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
onDestroy(() => {
|
|
126
|
+
if (typeof window !== 'undefined') {
|
|
127
|
+
window.removeEventListener('click', handleGlobalClick);
|
|
128
|
+
window.removeEventListener('keydown', handleGlobalKeyDown);
|
|
129
|
+
window.removeEventListener('scroll', handleGlobalScroll, true);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
</script>
|
|
133
|
+
|
|
134
|
+
<!-- Outer wrapper capturing right-click context menu -->
|
|
135
|
+
<div
|
|
136
|
+
bind:this={containerEl}
|
|
137
|
+
oncontextmenu={handleContextMenu}
|
|
138
|
+
class={cn('contents', className)}
|
|
139
|
+
data-svadmin-component="ContextMenu"
|
|
140
|
+
role="region"
|
|
141
|
+
aria-label="Context Menu Container"
|
|
142
|
+
>
|
|
143
|
+
{@render children?.()}
|
|
144
|
+
</div>
|
|
145
|
+
|
|
146
|
+
{#if isOpen}
|
|
147
|
+
<div
|
|
148
|
+
bind:this={menuEl}
|
|
149
|
+
class="fixed z-50 min-w-[180px] rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md outline-none animate-in fade-in-80 zoom-in-95"
|
|
150
|
+
style="left: {menuX}px; top: {menuY}px;"
|
|
151
|
+
data-svadmin-context-menu-popup="true"
|
|
152
|
+
role="menu"
|
|
153
|
+
tabindex="-1"
|
|
154
|
+
>
|
|
155
|
+
{#each items as item (item.key)}
|
|
156
|
+
{#if item.divider}
|
|
157
|
+
<div class="-mx-1 my-1 h-px bg-border" role="separator"></div>
|
|
158
|
+
{:else}
|
|
159
|
+
<div
|
|
160
|
+
class="relative"
|
|
161
|
+
role="none"
|
|
162
|
+
onmouseenter={() => {
|
|
163
|
+
if (item.children && item.children.length > 0) {
|
|
164
|
+
activeSubmenuKey = item.key;
|
|
165
|
+
}
|
|
166
|
+
}}
|
|
167
|
+
>
|
|
168
|
+
<button
|
|
169
|
+
type="button"
|
|
170
|
+
role="menuitem"
|
|
171
|
+
disabled={item.disabled}
|
|
172
|
+
aria-disabled={item.disabled}
|
|
173
|
+
aria-haspopup={item.children && item.children.length > 0 ? 'true' : undefined}
|
|
174
|
+
aria-expanded={activeSubmenuKey === item.key}
|
|
175
|
+
class={cn(
|
|
176
|
+
'flex w-full cursor-default select-none items-center rounded-sm px-2.5 py-1.5 text-sm outline-none transition-colors text-left',
|
|
177
|
+
item.disabled
|
|
178
|
+
? 'pointer-events-none opacity-50'
|
|
179
|
+
: item.danger
|
|
180
|
+
? 'text-destructive hover:bg-destructive/10 focus:bg-destructive/10'
|
|
181
|
+
: 'hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground'
|
|
182
|
+
)}
|
|
183
|
+
onclick={(e) => handleItemClick(item, e)}
|
|
184
|
+
>
|
|
185
|
+
{#if item.icon}
|
|
186
|
+
<span class="mr-2 inline-flex h-4 w-4 shrink-0 items-center justify-center">
|
|
187
|
+
{#if typeof item.icon === 'string'}
|
|
188
|
+
<span class="text-xs">{item.icon}</span>
|
|
189
|
+
{:else}
|
|
190
|
+
{@render item.icon()}
|
|
191
|
+
{/if}
|
|
192
|
+
</span>
|
|
193
|
+
{/if}
|
|
194
|
+
|
|
195
|
+
<span class="flex-1 truncate">{item.label}</span>
|
|
196
|
+
|
|
197
|
+
{#if item.shortcut}
|
|
198
|
+
<span class="ml-auto pl-3 font-mono text-xs text-muted-foreground">
|
|
199
|
+
{item.shortcut}
|
|
200
|
+
</span>
|
|
201
|
+
{/if}
|
|
202
|
+
|
|
203
|
+
{#if item.children && item.children.length > 0}
|
|
204
|
+
<svg
|
|
205
|
+
class="ml-auto h-3.5 w-3.5 shrink-0 text-muted-foreground"
|
|
206
|
+
viewBox="0 0 24 24"
|
|
207
|
+
fill="none"
|
|
208
|
+
stroke="currentColor"
|
|
209
|
+
stroke-width="2"
|
|
210
|
+
stroke-linecap="round"
|
|
211
|
+
stroke-linejoin="round"
|
|
212
|
+
>
|
|
213
|
+
<path d="m9 18 6-6-6-6" />
|
|
214
|
+
</svg>
|
|
215
|
+
{/if}
|
|
216
|
+
</button>
|
|
217
|
+
|
|
218
|
+
<!-- Nested Submenu -->
|
|
219
|
+
{#if item.children && item.children.length > 0 && activeSubmenuKey === item.key}
|
|
220
|
+
<div
|
|
221
|
+
class="absolute left-full top-0 ml-1 min-w-[160px] rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md outline-none animate-in fade-in-80 zoom-in-95"
|
|
222
|
+
role="menu"
|
|
223
|
+
tabindex="-1"
|
|
224
|
+
>
|
|
225
|
+
{#each item.children as subItem (subItem.key)}
|
|
226
|
+
{#if subItem.divider}
|
|
227
|
+
<div class="-mx-1 my-1 h-px bg-border" role="separator"></div>
|
|
228
|
+
{:else}
|
|
229
|
+
<button
|
|
230
|
+
type="button"
|
|
231
|
+
role="menuitem"
|
|
232
|
+
disabled={subItem.disabled}
|
|
233
|
+
aria-disabled={subItem.disabled}
|
|
234
|
+
class={cn(
|
|
235
|
+
'flex w-full cursor-default select-none items-center rounded-sm px-2.5 py-1.5 text-sm outline-none transition-colors text-left',
|
|
236
|
+
subItem.disabled
|
|
237
|
+
? 'pointer-events-none opacity-50'
|
|
238
|
+
: subItem.danger
|
|
239
|
+
? 'text-destructive hover:bg-destructive/10 focus:bg-destructive/10'
|
|
240
|
+
: 'hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground'
|
|
241
|
+
)}
|
|
242
|
+
onclick={(e) => handleSubItemClick(subItem, e)}
|
|
243
|
+
>
|
|
244
|
+
{#if subItem.icon}
|
|
245
|
+
<span class="mr-2 inline-flex h-4 w-4 shrink-0 items-center justify-center">
|
|
246
|
+
{#if typeof subItem.icon === 'string'}
|
|
247
|
+
<span class="text-xs">{subItem.icon}</span>
|
|
248
|
+
{:else}
|
|
249
|
+
{@render subItem.icon()}
|
|
250
|
+
{/if}
|
|
251
|
+
</span>
|
|
252
|
+
{/if}
|
|
253
|
+
<span class="flex-1 truncate">{subItem.label}</span>
|
|
254
|
+
{#if subItem.shortcut}
|
|
255
|
+
<span class="ml-auto pl-3 font-mono text-xs text-muted-foreground">
|
|
256
|
+
{subItem.shortcut}
|
|
257
|
+
</span>
|
|
258
|
+
{/if}
|
|
259
|
+
</button>
|
|
260
|
+
{/if}
|
|
261
|
+
{/each}
|
|
262
|
+
</div>
|
|
263
|
+
{/if}
|
|
264
|
+
</div>
|
|
265
|
+
{/if}
|
|
266
|
+
{/each}
|
|
267
|
+
</div>
|
|
268
|
+
{/if}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Snippet } from 'svelte';
|
|
2
|
+
export interface ContextMenuItem {
|
|
3
|
+
key: string;
|
|
4
|
+
label: string;
|
|
5
|
+
icon?: string | Snippet;
|
|
6
|
+
shortcut?: string;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
danger?: boolean;
|
|
9
|
+
divider?: boolean;
|
|
10
|
+
children?: ContextMenuItem[];
|
|
11
|
+
onclick?: (item: ContextMenuItem) => void;
|
|
12
|
+
}
|
|
13
|
+
export interface ContextMenuProps {
|
|
14
|
+
items: ContextMenuItem[];
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
class?: string;
|
|
17
|
+
onselect?: (item: ContextMenuItem) => void;
|
|
18
|
+
children?: Snippet;
|
|
19
|
+
}
|
|
20
|
+
declare const ContextMenu: import("svelte").Component<ContextMenuProps, {}, "">;
|
|
21
|
+
type ContextMenu = ReturnType<typeof ContextMenu>;
|
|
22
|
+
export default ContextMenu;
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { ChevronRight, ChevronDown, Copy, Check, Search } from '@lucide/svelte';
|
|
3
|
+
import { cn } from '../utils.js';
|
|
4
|
+
|
|
5
|
+
export interface JsonViewerProps {
|
|
6
|
+
/** 待解析展示的数据,可为对象、数组或合法的 JSON 字符串 */
|
|
7
|
+
data: unknown;
|
|
8
|
+
/** 初始展开深度,默认 2 */
|
|
9
|
+
initialDepth?: number | undefined;
|
|
10
|
+
/** 是否显示一键复制完整 JSON 按钮,默认 true */
|
|
11
|
+
copyable?: boolean | undefined;
|
|
12
|
+
/** 是否显示关键词搜索过滤栏,默认 true */
|
|
13
|
+
searchable?: boolean | undefined;
|
|
14
|
+
/** 字体大小:'sm' | 'md',默认 'sm' */
|
|
15
|
+
size?: 'sm' | 'md' | undefined;
|
|
16
|
+
/** 自定义最大高度类名,如 'max-h-96' */
|
|
17
|
+
maxHeight?: string | undefined;
|
|
18
|
+
/** 复制指定属性路径时的回调 */
|
|
19
|
+
onCopyPath?: ((path: string, value: unknown) => void) | undefined;
|
|
20
|
+
class?: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let {
|
|
24
|
+
data,
|
|
25
|
+
initialDepth = 2,
|
|
26
|
+
copyable = true,
|
|
27
|
+
searchable = true,
|
|
28
|
+
size = 'sm',
|
|
29
|
+
maxHeight = 'max-h-[32rem]',
|
|
30
|
+
onCopyPath,
|
|
31
|
+
class: className = '',
|
|
32
|
+
}: JsonViewerProps = $props();
|
|
33
|
+
|
|
34
|
+
let searchQuery = $state('');
|
|
35
|
+
let copiedAll = $state(false);
|
|
36
|
+
let copiedPath = $state<string | null>(null);
|
|
37
|
+
let collapsedPaths = $state<Set<string>>(new Set());
|
|
38
|
+
|
|
39
|
+
// 解析并规范化根对象
|
|
40
|
+
const parsedData = $derived.by(() => {
|
|
41
|
+
if (typeof data === 'string') {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(data);
|
|
44
|
+
} catch {
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return data;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const fullJsonString = $derived.by(() => {
|
|
52
|
+
try {
|
|
53
|
+
return JSON.stringify(parsedData, null, 2);
|
|
54
|
+
} catch {
|
|
55
|
+
return String(parsedData);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
function isObject(val: unknown): val is Record<string, unknown> {
|
|
60
|
+
return val !== null && typeof val === 'object' && !Array.isArray(val);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isArray(val: unknown): val is unknown[] {
|
|
64
|
+
return Array.isArray(val);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isPathCollapsed(path: string, depth: number): boolean {
|
|
68
|
+
if (collapsedPaths.has(path)) return true;
|
|
69
|
+
// 如果没有被显式收起,且层级超过初始深度,则默认收起
|
|
70
|
+
return depth > initialDepth && !collapsedPaths.has(`expanded:${path}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function handleToggle(path: string, depth: number) {
|
|
74
|
+
const isCurrentlyCollapsed = isPathCollapsed(path, depth);
|
|
75
|
+
const next = new Set(collapsedPaths);
|
|
76
|
+
if (isCurrentlyCollapsed) {
|
|
77
|
+
next.delete(path);
|
|
78
|
+
next.add(`expanded:${path}`);
|
|
79
|
+
} else {
|
|
80
|
+
next.add(path);
|
|
81
|
+
next.delete(`expanded:${path}`);
|
|
82
|
+
}
|
|
83
|
+
collapsedPaths = next;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function copyAll() {
|
|
87
|
+
if (typeof navigator !== 'undefined' && navigator.clipboard) {
|
|
88
|
+
navigator.clipboard.writeText(fullJsonString);
|
|
89
|
+
copiedAll = true;
|
|
90
|
+
setTimeout(() => {
|
|
91
|
+
copiedAll = false;
|
|
92
|
+
}, 1800);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function copySinglePath(path: string, value: unknown) {
|
|
97
|
+
if (typeof navigator !== 'undefined' && navigator.clipboard) {
|
|
98
|
+
navigator.clipboard.writeText(path);
|
|
99
|
+
copiedPath = path;
|
|
100
|
+
onCopyPath?.(path, value);
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
if (copiedPath === path) copiedPath = null;
|
|
103
|
+
}, 1500);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function matchesSearch(key: string, val: unknown): boolean {
|
|
108
|
+
if (!searchQuery.trim()) return true;
|
|
109
|
+
const q = searchQuery.toLowerCase();
|
|
110
|
+
if (key.toLowerCase().includes(q)) return true;
|
|
111
|
+
if (typeof val === 'string' && val.toLowerCase().includes(q)) return true;
|
|
112
|
+
if (typeof val === 'number' && String(val).includes(q)) return true;
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
</script>
|
|
116
|
+
|
|
117
|
+
<div
|
|
118
|
+
data-svadmin-component="JsonViewer"
|
|
119
|
+
class={cn(
|
|
120
|
+
'rounded-xl border border-border bg-card shadow-xs flex flex-col font-mono text-xs overflow-hidden',
|
|
121
|
+
className
|
|
122
|
+
)}
|
|
123
|
+
>
|
|
124
|
+
<!-- Top Toolbar -->
|
|
125
|
+
<div class="flex items-center justify-between gap-2 px-3 py-2 border-b border-border/70 bg-muted/30">
|
|
126
|
+
<div class="flex items-center gap-2 flex-1 max-w-sm">
|
|
127
|
+
{#if searchable}
|
|
128
|
+
<div class="relative w-full flex items-center">
|
|
129
|
+
<Search class="absolute left-2.5 h-3.5 w-3.5 text-muted-foreground pointer-events-none" />
|
|
130
|
+
<input
|
|
131
|
+
type="text"
|
|
132
|
+
bind:value={searchQuery}
|
|
133
|
+
placeholder="搜索 Key 或 Value..."
|
|
134
|
+
class="w-full pl-8 pr-2.5 py-1 text-xs rounded-md border border-border bg-background text-foreground placeholder:text-muted-foreground focus:outline-2 focus:outline-primary"
|
|
135
|
+
/>
|
|
136
|
+
</div>
|
|
137
|
+
{/if}
|
|
138
|
+
</div>
|
|
139
|
+
|
|
140
|
+
<div class="flex items-center gap-1.5 shrink-0">
|
|
141
|
+
{#if copyable}
|
|
142
|
+
<button
|
|
143
|
+
type="button"
|
|
144
|
+
onclick={copyAll}
|
|
145
|
+
class="inline-flex items-center gap-1 px-2 py-1 rounded-md text-xs font-sans border border-border bg-background text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
|
|
146
|
+
title="复制完整 JSON"
|
|
147
|
+
>
|
|
148
|
+
{#if copiedAll}
|
|
149
|
+
<Check class="h-3 w-3 text-success" />
|
|
150
|
+
<span class="text-success text-[11px]">已复制</span>
|
|
151
|
+
{:else}
|
|
152
|
+
<Copy class="h-3 w-3" />
|
|
153
|
+
<span class="text-[11px]">复制 JSON</span>
|
|
154
|
+
{/if}
|
|
155
|
+
</button>
|
|
156
|
+
{/if}
|
|
157
|
+
</div>
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
<!-- JSON Content Tree -->
|
|
161
|
+
<div class={cn('p-3.5 overflow-auto select-text space-y-1', maxHeight, size === 'sm' ? 'text-xs' : 'text-sm')}>
|
|
162
|
+
{#if parsedData === null}
|
|
163
|
+
<span class="text-muted-foreground italic">null</span>
|
|
164
|
+
{:else if typeof parsedData !== 'object'}
|
|
165
|
+
<span class="text-foreground">{String(parsedData)}</span>
|
|
166
|
+
{:else if isArray(parsedData)}
|
|
167
|
+
<!-- Array Root -->
|
|
168
|
+
<div class="space-y-0.5">
|
|
169
|
+
<span class="text-muted-foreground">[</span>
|
|
170
|
+
<div class="pl-4 border-l border-border/40 space-y-1 my-0.5">
|
|
171
|
+
{#each parsedData as item, idx (idx)}
|
|
172
|
+
{@const itemPath = `[${idx}]`}
|
|
173
|
+
{#if matchesSearch(String(idx), item)}
|
|
174
|
+
<div class="flex items-start gap-1 group py-0.5">
|
|
175
|
+
<span class="text-muted-foreground/60 select-none text-[10px] w-5 text-right">{idx}:</span>
|
|
176
|
+
{#if isObject(item) || isArray(item)}
|
|
177
|
+
{@const collapsed = isPathCollapsed(itemPath, 1)}
|
|
178
|
+
<button
|
|
179
|
+
type="button"
|
|
180
|
+
onclick={() => handleToggle(itemPath, 1)}
|
|
181
|
+
class="inline-flex items-center p-0.5 text-muted-foreground hover:text-foreground cursor-pointer"
|
|
182
|
+
aria-label="Toggle node"
|
|
183
|
+
>
|
|
184
|
+
{#if collapsed}
|
|
185
|
+
<ChevronRight class="h-3 w-3" />
|
|
186
|
+
{:else}
|
|
187
|
+
<ChevronDown class="h-3 w-3" />
|
|
188
|
+
{/if}
|
|
189
|
+
</button>
|
|
190
|
+
{#if collapsed}
|
|
191
|
+
<button
|
|
192
|
+
type="button"
|
|
193
|
+
class="text-muted-foreground text-[11px] cursor-pointer hover:underline bg-transparent border-0 p-0 text-left font-mono"
|
|
194
|
+
onclick={() => handleToggle(itemPath, 1)}
|
|
195
|
+
>
|
|
196
|
+
{isArray(item) ? `Array(${item.length})` : `{...}`}
|
|
197
|
+
</button>
|
|
198
|
+
{:else}
|
|
199
|
+
<div class="flex-1">
|
|
200
|
+
<div class="text-muted-foreground text-[11px]">{isArray(item) ? '[' : '{'}</div>
|
|
201
|
+
<div class="pl-4 border-l border-border/30 my-0.5 space-y-1">
|
|
202
|
+
{#each Object.entries(item) as [subKey, subVal] (subKey)}
|
|
203
|
+
<div class="flex items-baseline gap-1 py-0.5">
|
|
204
|
+
<span class="text-foreground font-semibold">"{subKey}":</span>
|
|
205
|
+
<span class={cn(
|
|
206
|
+
typeof subVal === 'string' && 'text-success',
|
|
207
|
+
typeof subVal === 'number' && 'text-primary font-medium',
|
|
208
|
+
typeof subVal === 'boolean' && 'text-warning font-medium',
|
|
209
|
+
subVal === null && 'text-muted-foreground italic'
|
|
210
|
+
)}>
|
|
211
|
+
{typeof subVal === 'string' ? `"${subVal}"` : String(subVal)}
|
|
212
|
+
</span>
|
|
213
|
+
</div>
|
|
214
|
+
{/each}
|
|
215
|
+
</div>
|
|
216
|
+
<div class="text-muted-foreground text-[11px]">{isArray(item) ? ']' : '}'}</div>
|
|
217
|
+
</div>
|
|
218
|
+
{/if}
|
|
219
|
+
{:else}
|
|
220
|
+
<span class={cn(
|
|
221
|
+
typeof item === 'string' && 'text-success',
|
|
222
|
+
typeof item === 'number' && 'text-primary font-medium',
|
|
223
|
+
typeof item === 'boolean' && 'text-warning font-medium',
|
|
224
|
+
item === null && 'text-muted-foreground italic'
|
|
225
|
+
)}>
|
|
226
|
+
{typeof item === 'string' ? `"${item}"` : String(item)}
|
|
227
|
+
</span>
|
|
228
|
+
{/if}
|
|
229
|
+
</div>
|
|
230
|
+
{/if}
|
|
231
|
+
{/each}
|
|
232
|
+
</div>
|
|
233
|
+
<span class="text-muted-foreground">]</span>
|
|
234
|
+
</div>
|
|
235
|
+
{:else if isObject(parsedData)}
|
|
236
|
+
<!-- Object Root -->
|
|
237
|
+
<div class="space-y-0.5">
|
|
238
|
+
<span class="text-muted-foreground">{</span>
|
|
239
|
+
<div class="pl-4 border-l border-border/40 space-y-1 my-0.5">
|
|
240
|
+
{#each Object.entries(parsedData) as [key, val] (key)}
|
|
241
|
+
{@const nodePath = key}
|
|
242
|
+
{#if matchesSearch(key, val)}
|
|
243
|
+
<div class="flex items-start gap-1 group py-0.5 hover:bg-muted/20 rounded px-1 -mx-1">
|
|
244
|
+
{#if isObject(val) || isArray(val)}
|
|
245
|
+
{@const collapsed = isPathCollapsed(nodePath, 1)}
|
|
246
|
+
<button
|
|
247
|
+
type="button"
|
|
248
|
+
onclick={() => handleToggle(nodePath, 1)}
|
|
249
|
+
class="inline-flex items-center p-0.5 text-muted-foreground hover:text-foreground cursor-pointer mt-0.5"
|
|
250
|
+
aria-label={`Toggle ${key}`}
|
|
251
|
+
>
|
|
252
|
+
{#if collapsed}
|
|
253
|
+
<ChevronRight class="h-3 w-3" />
|
|
254
|
+
{:else}
|
|
255
|
+
<ChevronDown class="h-3 w-3" />
|
|
256
|
+
{/if}
|
|
257
|
+
</button>
|
|
258
|
+
<span class="text-foreground font-semibold">"{key}":</span>
|
|
259
|
+
{#if collapsed}
|
|
260
|
+
<button
|
|
261
|
+
type="button"
|
|
262
|
+
class="text-muted-foreground text-[11px] cursor-pointer hover:underline bg-transparent border-0 p-0 text-left font-mono"
|
|
263
|
+
onclick={() => handleToggle(nodePath, 1)}
|
|
264
|
+
>
|
|
265
|
+
{isArray(val) ? `Array(${val.length})` : `{...}`}
|
|
266
|
+
</button>
|
|
267
|
+
{:else}
|
|
268
|
+
<div class="flex-1">
|
|
269
|
+
<span class="text-muted-foreground text-[11px]">{isArray(val) ? '[' : '{'}</span>
|
|
270
|
+
<div class="pl-4 border-l border-border/30 my-0.5 space-y-1">
|
|
271
|
+
{#each Object.entries(val) as [subK, subV] (subK)}
|
|
272
|
+
<div class="flex items-baseline gap-1 py-0.5">
|
|
273
|
+
<span class="text-foreground/90 font-medium">"{subK}":</span>
|
|
274
|
+
<span class={cn(
|
|
275
|
+
typeof subV === 'string' && 'text-success',
|
|
276
|
+
typeof subV === 'number' && 'text-primary font-medium',
|
|
277
|
+
typeof subV === 'boolean' && 'text-warning font-medium',
|
|
278
|
+
subV === null && 'text-muted-foreground italic'
|
|
279
|
+
)}>
|
|
280
|
+
{typeof subV === 'string' ? `"${subV}"` : typeof subV === 'object' && subV !== null ? (Array.isArray(subV) ? `Array(${subV.length})` : '{...}') : String(subV)}
|
|
281
|
+
</span>
|
|
282
|
+
</div>
|
|
283
|
+
{/each}
|
|
284
|
+
</div>
|
|
285
|
+
<span class="text-muted-foreground text-[11px]">{isArray(val) ? ']' : '}'}</span>
|
|
286
|
+
</div>
|
|
287
|
+
{/if}
|
|
288
|
+
{:else}
|
|
289
|
+
<div class="flex items-baseline gap-1 flex-1">
|
|
290
|
+
<span class="text-foreground font-semibold">"{key}":</span>
|
|
291
|
+
<span class={cn(
|
|
292
|
+
typeof val === 'string' && 'text-success',
|
|
293
|
+
typeof val === 'number' && 'text-primary font-medium',
|
|
294
|
+
typeof val === 'boolean' && 'text-warning font-medium',
|
|
295
|
+
val === null && 'text-muted-foreground italic'
|
|
296
|
+
)}>
|
|
297
|
+
{typeof val === 'string' ? `"${val}"` : String(val)}
|
|
298
|
+
</span>
|
|
299
|
+
</div>
|
|
300
|
+
{/if}
|
|
301
|
+
|
|
302
|
+
<!-- Quick Path Copy Button on Hover -->
|
|
303
|
+
<button
|
|
304
|
+
type="button"
|
|
305
|
+
onclick={() => copySinglePath(nodePath, val)}
|
|
306
|
+
class="opacity-0 group-hover:opacity-100 transition-opacity p-0.5 text-muted-foreground hover:text-foreground cursor-pointer"
|
|
307
|
+
title={`复制路径: ${nodePath}`}
|
|
308
|
+
>
|
|
309
|
+
{#if copiedPath === nodePath}
|
|
310
|
+
<Check class="h-3 w-3 text-success" />
|
|
311
|
+
{:else}
|
|
312
|
+
<Copy class="h-3 w-3" />
|
|
313
|
+
{/if}
|
|
314
|
+
</button>
|
|
315
|
+
</div>
|
|
316
|
+
{/if}
|
|
317
|
+
{/each}
|
|
318
|
+
</div>
|
|
319
|
+
<span class="text-muted-foreground">}</span>
|
|
320
|
+
</div>
|
|
321
|
+
{/if}
|
|
322
|
+
</div>
|
|
323
|
+
</div>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface JsonViewerProps {
|
|
2
|
+
/** 待解析展示的数据,可为对象、数组或合法的 JSON 字符串 */
|
|
3
|
+
data: unknown;
|
|
4
|
+
/** 初始展开深度,默认 2 */
|
|
5
|
+
initialDepth?: number | undefined;
|
|
6
|
+
/** 是否显示一键复制完整 JSON 按钮,默认 true */
|
|
7
|
+
copyable?: boolean | undefined;
|
|
8
|
+
/** 是否显示关键词搜索过滤栏,默认 true */
|
|
9
|
+
searchable?: boolean | undefined;
|
|
10
|
+
/** 字体大小:'sm' | 'md',默认 'sm' */
|
|
11
|
+
size?: 'sm' | 'md' | undefined;
|
|
12
|
+
/** 自定义最大高度类名,如 'max-h-96' */
|
|
13
|
+
maxHeight?: string | undefined;
|
|
14
|
+
/** 复制指定属性路径时的回调 */
|
|
15
|
+
onCopyPath?: ((path: string, value: unknown) => void) | undefined;
|
|
16
|
+
class?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
declare const JsonViewer: import("svelte").Component<JsonViewerProps, {}, "">;
|
|
19
|
+
type JsonViewer = ReturnType<typeof JsonViewer>;
|
|
20
|
+
export default JsonViewer;
|