@svadmin/ui 0.119.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 +139 -0
- package/dist/app.theme.css +139 -0
- package/dist/components/AboutSettings.svelte +1 -1
- package/dist/components/Barcode.svelte +219 -0
- package/dist/components/Barcode.svelte.d.ts +15 -0
- 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/NumberRoll.svelte +149 -0
- package/dist/components/NumberRoll.svelte.d.ts +17 -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/VirtualList.svelte +165 -0
- package/dist/components/VirtualList.svelte.d.ts +43 -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 +30 -35
- package/dist/index.js +18 -2
- package/package.json +1 -1
|
@@ -15,10 +15,12 @@
|
|
|
15
15
|
suffix?: string | undefined;
|
|
16
16
|
/** Number of decimal places when auto computing rate (default: 1) */
|
|
17
17
|
precision?: number | undefined;
|
|
18
|
+
/** Whether to explicitly prefix positive numbers with '+' (default: true) */
|
|
19
|
+
showSign?: boolean | undefined;
|
|
18
20
|
/** Invert color semantics (useful for error rate, latency, queue backlog where lower is better) */
|
|
19
21
|
reverseColor?: boolean | undefined;
|
|
20
|
-
/** Visual style variant: 'text' (inline text), 'badge' (pill badge with subtle background), 'solid' */
|
|
21
|
-
variant?: 'text' | 'badge' | 'solid' | undefined;
|
|
22
|
+
/** Visual style variant: 'text' (inline text), 'badge' (pill badge with subtle background), 'solid', 'icon' (compact icon only) */
|
|
23
|
+
variant?: 'text' | 'badge' | 'solid' | 'icon' | undefined;
|
|
22
24
|
/** Size: 'sm' | 'md' | 'lg' */
|
|
23
25
|
size?: 'sm' | 'md' | 'lg' | undefined;
|
|
24
26
|
/** Whether to render trending arrow icon (default: true) */
|
|
@@ -34,6 +36,7 @@
|
|
|
34
36
|
prefix,
|
|
35
37
|
suffix = '%',
|
|
36
38
|
precision = 1,
|
|
39
|
+
showSign = true,
|
|
37
40
|
reverseColor = false,
|
|
38
41
|
variant = 'text',
|
|
39
42
|
size = 'md',
|
|
@@ -48,9 +51,10 @@
|
|
|
48
51
|
const rateNum = (diff / Math.abs(prevValue)) * 100;
|
|
49
52
|
const dir: 'up' | 'down' | 'neutral' = diff > 0 ? 'up' : diff < 0 ? 'down' : 'neutral';
|
|
50
53
|
const formatted = Math.abs(rateNum).toFixed(precision);
|
|
54
|
+
const sign = showSign && dir === 'up' ? '+' : dir === 'down' ? '-' : '';
|
|
51
55
|
return {
|
|
52
56
|
direction: direction ?? dir,
|
|
53
|
-
text: `${formatted}${suffix}`,
|
|
57
|
+
text: `${sign}${formatted}${suffix}`,
|
|
54
58
|
};
|
|
55
59
|
}
|
|
56
60
|
|
|
@@ -58,9 +62,10 @@
|
|
|
58
62
|
if (typeof value === 'number') {
|
|
59
63
|
const dir: 'up' | 'down' | 'neutral' = value > 0 ? 'up' : value < 0 ? 'down' : 'neutral';
|
|
60
64
|
const formatted = Math.abs(value).toFixed(precision);
|
|
65
|
+
const sign = showSign && dir === 'up' ? '+' : dir === 'down' ? '-' : '';
|
|
61
66
|
return {
|
|
62
67
|
direction: direction ?? dir,
|
|
63
|
-
text: `${formatted}${suffix}`,
|
|
68
|
+
text: `${sign}${formatted}${suffix}`,
|
|
64
69
|
};
|
|
65
70
|
}
|
|
66
71
|
|
|
@@ -101,12 +106,12 @@
|
|
|
101
106
|
const sizeClasses = $derived.by(() => {
|
|
102
107
|
switch (size) {
|
|
103
108
|
case 'sm':
|
|
104
|
-
return 'text-[11px] gap-0.5 px-1.5 py-0.5';
|
|
109
|
+
return variant === 'icon' ? 'p-1' : 'text-[11px] gap-0.5 px-1.5 py-0.5';
|
|
105
110
|
case 'lg':
|
|
106
|
-
return 'text-sm gap-1 px-2.5 py-1';
|
|
111
|
+
return variant === 'icon' ? 'p-2' : 'text-sm gap-1 px-2.5 py-1';
|
|
107
112
|
case 'md':
|
|
108
113
|
default:
|
|
109
|
-
return 'text-xs gap-1 px-2 py-0.5';
|
|
114
|
+
return variant === 'icon' ? 'p-1.5' : 'text-xs gap-1 px-2 py-0.5';
|
|
110
115
|
}
|
|
111
116
|
});
|
|
112
117
|
|
|
@@ -126,7 +131,7 @@
|
|
|
126
131
|
if (isPositive === null) {
|
|
127
132
|
return variant === 'solid'
|
|
128
133
|
? 'bg-muted text-muted-foreground'
|
|
129
|
-
: variant === 'badge'
|
|
134
|
+
: variant === 'badge' || variant === 'icon'
|
|
130
135
|
? 'bg-muted/50 text-muted-foreground'
|
|
131
136
|
: 'text-muted-foreground';
|
|
132
137
|
}
|
|
@@ -134,14 +139,14 @@
|
|
|
134
139
|
if (isPositive) {
|
|
135
140
|
return variant === 'solid'
|
|
136
141
|
? 'bg-success text-success-foreground'
|
|
137
|
-
: variant === 'badge'
|
|
142
|
+
: variant === 'badge' || variant === 'icon'
|
|
138
143
|
? 'bg-success/15 text-success border border-success/20'
|
|
139
144
|
: 'text-success';
|
|
140
145
|
}
|
|
141
146
|
|
|
142
147
|
return variant === 'solid'
|
|
143
148
|
? 'bg-destructive text-destructive-foreground'
|
|
144
|
-
: variant === 'badge'
|
|
149
|
+
: variant === 'badge' || variant === 'icon'
|
|
145
150
|
? 'bg-destructive/15 text-destructive border border-destructive/20'
|
|
146
151
|
: 'text-destructive';
|
|
147
152
|
});
|
|
@@ -149,6 +154,7 @@
|
|
|
149
154
|
|
|
150
155
|
<span
|
|
151
156
|
data-svadmin-component="TrendIndicator"
|
|
157
|
+
title={variant === 'icon' ? (prefix ? `${prefix} ${computed.text}` : computed.text) : undefined}
|
|
152
158
|
class={cn(
|
|
153
159
|
'inline-flex items-center font-medium font-mono select-none transition-colors',
|
|
154
160
|
variant !== 'text' && 'rounded-full font-semibold',
|
|
@@ -157,13 +163,13 @@
|
|
|
157
163
|
className
|
|
158
164
|
)}
|
|
159
165
|
>
|
|
160
|
-
{#if prefix}
|
|
166
|
+
{#if prefix && variant !== 'icon'}
|
|
161
167
|
<span class="font-sans text-[11px] font-normal text-muted-foreground mr-0.5">
|
|
162
168
|
{prefix}
|
|
163
169
|
</span>
|
|
164
170
|
{/if}
|
|
165
171
|
|
|
166
|
-
{#if showIcon}
|
|
172
|
+
{#if showIcon || variant === 'icon'}
|
|
167
173
|
{#if computed.direction === 'up'}
|
|
168
174
|
<TrendingUp class={cn(iconSizeClasses, 'shrink-0')} />
|
|
169
175
|
{:else if computed.direction === 'down'}
|
|
@@ -173,5 +179,7 @@
|
|
|
173
179
|
{/if}
|
|
174
180
|
{/if}
|
|
175
181
|
|
|
176
|
-
|
|
182
|
+
{#if variant !== 'icon'}
|
|
183
|
+
<span>{computed.text}</span>
|
|
184
|
+
{/if}
|
|
177
185
|
</span>
|
|
@@ -11,10 +11,12 @@ export interface TrendIndicatorProps {
|
|
|
11
11
|
suffix?: string | undefined;
|
|
12
12
|
/** Number of decimal places when auto computing rate (default: 1) */
|
|
13
13
|
precision?: number | undefined;
|
|
14
|
+
/** Whether to explicitly prefix positive numbers with '+' (default: true) */
|
|
15
|
+
showSign?: boolean | undefined;
|
|
14
16
|
/** Invert color semantics (useful for error rate, latency, queue backlog where lower is better) */
|
|
15
17
|
reverseColor?: boolean | undefined;
|
|
16
|
-
/** Visual style variant: 'text' (inline text), 'badge' (pill badge with subtle background), 'solid' */
|
|
17
|
-
variant?: 'text' | 'badge' | 'solid' | undefined;
|
|
18
|
+
/** Visual style variant: 'text' (inline text), 'badge' (pill badge with subtle background), 'solid', 'icon' (compact icon only) */
|
|
19
|
+
variant?: 'text' | 'badge' | 'solid' | 'icon' | undefined;
|
|
18
20
|
/** Size: 'sm' | 'md' | 'lg' */
|
|
19
21
|
size?: 'sm' | 'md' | 'lg' | undefined;
|
|
20
22
|
/** Whether to render trending arrow icon (default: true) */
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { AlertTriangle, X } from '@lucide/svelte';
|
|
3
|
+
import { onDestroy, type Snippet } from 'svelte';
|
|
4
|
+
import { cn } from '../utils.js';
|
|
5
|
+
import { useUnsavedChanges } from './useUnsavedChanges.svelte.js';
|
|
6
|
+
|
|
7
|
+
export interface UnsavedChangesGuardProps {
|
|
8
|
+
/** Controlled dirty flag indicating whether current form has unsaved modifications */
|
|
9
|
+
isDirty?: boolean | undefined;
|
|
10
|
+
/** Whether protection is actively enabled (default: true) */
|
|
11
|
+
enabled?: boolean | undefined;
|
|
12
|
+
/** Modal dialog title (default: 'Unsaved Changes') */
|
|
13
|
+
title?: string | undefined;
|
|
14
|
+
/** Modal dialog description / warning text */
|
|
15
|
+
message?: string | undefined;
|
|
16
|
+
/** Stay button label (default: 'Stay on Page') */
|
|
17
|
+
stayText?: string | undefined;
|
|
18
|
+
/** Leave button label (default: 'Discard & Leave') */
|
|
19
|
+
leaveText?: string | undefined;
|
|
20
|
+
/** Callback when user confirms discard */
|
|
21
|
+
onleave?: (() => void) | undefined;
|
|
22
|
+
/** Callback when user cancels and stays */
|
|
23
|
+
onstay?: (() => void) | undefined;
|
|
24
|
+
/** Extra classes for wrapper */
|
|
25
|
+
class?: string | undefined;
|
|
26
|
+
/** Page or form content */
|
|
27
|
+
children?: Snippet | undefined;
|
|
28
|
+
/** Custom modal replacement snippet */
|
|
29
|
+
modal?: Snippet<[{ proceed: () => void; cancel: () => void; isBlocked: boolean }]> | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let {
|
|
33
|
+
isDirty = false,
|
|
34
|
+
enabled = true,
|
|
35
|
+
title = 'Unsaved Changes',
|
|
36
|
+
message = 'You have unsaved changes that will be lost if you leave this page. Are you sure you want to discard them?',
|
|
37
|
+
stayText = 'Stay on Page',
|
|
38
|
+
leaveText = 'Discard & Leave',
|
|
39
|
+
onleave,
|
|
40
|
+
onstay,
|
|
41
|
+
class: className = '',
|
|
42
|
+
children,
|
|
43
|
+
modal,
|
|
44
|
+
}: UnsavedChangesGuardProps = $props();
|
|
45
|
+
|
|
46
|
+
const guard = useUnsavedChanges({
|
|
47
|
+
isDirty: () => isDirty,
|
|
48
|
+
enabled: () => enabled,
|
|
49
|
+
message: () => message,
|
|
50
|
+
onLeave: () => onleave?.(),
|
|
51
|
+
onStay: () => onstay?.(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
onDestroy(() => {
|
|
55
|
+
guard.destroy();
|
|
56
|
+
});
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<div
|
|
60
|
+
data-svadmin-component="UnsavedChangesGuard"
|
|
61
|
+
data-dirty={isDirty ? 'true' : 'false'}
|
|
62
|
+
class={cn('w-full', className)}
|
|
63
|
+
>
|
|
64
|
+
{#if children}
|
|
65
|
+
{@render children()}
|
|
66
|
+
{/if}
|
|
67
|
+
|
|
68
|
+
{#if guard.isBlocked}
|
|
69
|
+
{#if modal}
|
|
70
|
+
{@render modal({ proceed: guard.proceed, cancel: guard.cancel, isBlocked: guard.isBlocked })}
|
|
71
|
+
{:else}
|
|
72
|
+
<div
|
|
73
|
+
role="alertdialog"
|
|
74
|
+
aria-modal="true"
|
|
75
|
+
aria-labelledby="unsaved-changes-title"
|
|
76
|
+
aria-describedby="unsaved-changes-desc"
|
|
77
|
+
class="fixed inset-0 z-50 flex items-center justify-center overflow-hidden p-4"
|
|
78
|
+
>
|
|
79
|
+
<!-- Backdrop -->
|
|
80
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
81
|
+
<div
|
|
82
|
+
role="button"
|
|
83
|
+
tabindex="-1"
|
|
84
|
+
class="fixed inset-0 bg-background/80 backdrop-blur-sm transition-opacity"
|
|
85
|
+
onclick={guard.cancel}
|
|
86
|
+
aria-label="Dismiss warning"
|
|
87
|
+
></div>
|
|
88
|
+
|
|
89
|
+
<!-- Alert Modal Box -->
|
|
90
|
+
<div
|
|
91
|
+
class="relative z-50 w-full max-w-md rounded-2xl border border-border bg-card p-6 text-card-foreground shadow-2xl transition-all animate-in fade-in zoom-in-95 duration-150"
|
|
92
|
+
>
|
|
93
|
+
<div class="flex items-start gap-4">
|
|
94
|
+
<div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-warning/15 text-warning border border-warning/20">
|
|
95
|
+
<AlertTriangle class="h-6 w-6" />
|
|
96
|
+
</div>
|
|
97
|
+
|
|
98
|
+
<div class="flex-1 space-y-1.5">
|
|
99
|
+
<h3 id="unsaved-changes-title" class="text-base font-semibold text-foreground">
|
|
100
|
+
{title}
|
|
101
|
+
</h3>
|
|
102
|
+
<p id="unsaved-changes-desc" class="text-xs text-muted-foreground leading-relaxed">
|
|
103
|
+
{message}
|
|
104
|
+
</p>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
<button
|
|
108
|
+
type="button"
|
|
109
|
+
onclick={guard.cancel}
|
|
110
|
+
class="rounded-lg p-1 text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
|
|
111
|
+
aria-label="Close"
|
|
112
|
+
>
|
|
113
|
+
<X class="h-4 w-4" />
|
|
114
|
+
</button>
|
|
115
|
+
</div>
|
|
116
|
+
|
|
117
|
+
<div class="mt-6 flex items-center justify-end gap-2.5">
|
|
118
|
+
<button
|
|
119
|
+
type="button"
|
|
120
|
+
onclick={guard.cancel}
|
|
121
|
+
class="rounded-lg border border-border bg-background px-4 py-2 text-xs font-medium text-foreground hover:bg-muted transition-colors"
|
|
122
|
+
>
|
|
123
|
+
{stayText}
|
|
124
|
+
</button>
|
|
125
|
+
|
|
126
|
+
<button
|
|
127
|
+
type="button"
|
|
128
|
+
onclick={guard.proceed}
|
|
129
|
+
class="rounded-lg bg-destructive px-4 py-2 text-xs font-medium text-destructive-foreground hover:bg-destructive/90 transition-colors shadow-sm"
|
|
130
|
+
>
|
|
131
|
+
{leaveText}
|
|
132
|
+
</button>
|
|
133
|
+
</div>
|
|
134
|
+
</div>
|
|
135
|
+
</div>
|
|
136
|
+
{/if}
|
|
137
|
+
{/if}
|
|
138
|
+
</div>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Snippet } from 'svelte';
|
|
2
|
+
export interface UnsavedChangesGuardProps {
|
|
3
|
+
/** Controlled dirty flag indicating whether current form has unsaved modifications */
|
|
4
|
+
isDirty?: boolean | undefined;
|
|
5
|
+
/** Whether protection is actively enabled (default: true) */
|
|
6
|
+
enabled?: boolean | undefined;
|
|
7
|
+
/** Modal dialog title (default: 'Unsaved Changes') */
|
|
8
|
+
title?: string | undefined;
|
|
9
|
+
/** Modal dialog description / warning text */
|
|
10
|
+
message?: string | undefined;
|
|
11
|
+
/** Stay button label (default: 'Stay on Page') */
|
|
12
|
+
stayText?: string | undefined;
|
|
13
|
+
/** Leave button label (default: 'Discard & Leave') */
|
|
14
|
+
leaveText?: string | undefined;
|
|
15
|
+
/** Callback when user confirms discard */
|
|
16
|
+
onleave?: (() => void) | undefined;
|
|
17
|
+
/** Callback when user cancels and stays */
|
|
18
|
+
onstay?: (() => void) | undefined;
|
|
19
|
+
/** Extra classes for wrapper */
|
|
20
|
+
class?: string | undefined;
|
|
21
|
+
/** Page or form content */
|
|
22
|
+
children?: Snippet | undefined;
|
|
23
|
+
/** Custom modal replacement snippet */
|
|
24
|
+
modal?: Snippet<[{
|
|
25
|
+
proceed: () => void;
|
|
26
|
+
cancel: () => void;
|
|
27
|
+
isBlocked: boolean;
|
|
28
|
+
}]> | undefined;
|
|
29
|
+
}
|
|
30
|
+
declare const UnsavedChangesGuard: import("svelte").Component<UnsavedChangesGuardProps, {}, "">;
|
|
31
|
+
type UnsavedChangesGuard = ReturnType<typeof UnsavedChangesGuard>;
|
|
32
|
+
export default UnsavedChangesGuard;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
<script lang="ts" generics="T">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { cn } from '../utils.js';
|
|
4
|
+
|
|
5
|
+
export interface VirtualListProps<T = unknown> {
|
|
6
|
+
items: T[];
|
|
7
|
+
itemHeight?: number;
|
|
8
|
+
height?: number | string;
|
|
9
|
+
overscan?: number;
|
|
10
|
+
endReachedThreshold?: number;
|
|
11
|
+
class?: string;
|
|
12
|
+
keyField?: string | keyof T | ((item: T, index: number) => string | number);
|
|
13
|
+
onendreached?: () => void;
|
|
14
|
+
children?: Snippet<[T, number]>;
|
|
15
|
+
empty?: Snippet;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let {
|
|
19
|
+
items = [],
|
|
20
|
+
itemHeight = 48,
|
|
21
|
+
height = 400,
|
|
22
|
+
overscan = 5,
|
|
23
|
+
endReachedThreshold = 50,
|
|
24
|
+
class: className = '',
|
|
25
|
+
keyField,
|
|
26
|
+
onendreached,
|
|
27
|
+
children,
|
|
28
|
+
empty,
|
|
29
|
+
}: VirtualListProps<T> = $props();
|
|
30
|
+
|
|
31
|
+
let containerElement = $state<HTMLDivElement | null>(null);
|
|
32
|
+
let scrollTop = $state(0);
|
|
33
|
+
let measuredViewportHeight = $state<number | null>(null);
|
|
34
|
+
|
|
35
|
+
const fallbackHeight = $derived(typeof height === 'number' ? height : 400);
|
|
36
|
+
const viewportHeight = $derived(measuredViewportHeight ?? fallbackHeight);
|
|
37
|
+
|
|
38
|
+
// 计算视口可见行范围
|
|
39
|
+
const totalCount = $derived(items.length);
|
|
40
|
+
const totalHeight = $derived(totalCount * itemHeight);
|
|
41
|
+
|
|
42
|
+
const startIndex = $derived(
|
|
43
|
+
Math.max(0, Math.floor(scrollTop / itemHeight) - overscan)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const endIndex = $derived(
|
|
47
|
+
Math.min(totalCount, Math.ceil((scrollTop + viewportHeight) / itemHeight) + overscan)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const visibleItems = $derived.by(() => {
|
|
51
|
+
const list: { item: T; index: number }[] = [];
|
|
52
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
53
|
+
const currentItem = items[i];
|
|
54
|
+
if (currentItem !== undefined) {
|
|
55
|
+
list.push({ item: currentItem as T, index: i });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return list;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const offsetY = $derived(startIndex * itemHeight);
|
|
62
|
+
|
|
63
|
+
function getItemKey(item: T, index: number): string | number {
|
|
64
|
+
if (typeof keyField === 'function') {
|
|
65
|
+
return keyField(item, index);
|
|
66
|
+
}
|
|
67
|
+
if (typeof keyField === 'string' && item && typeof item === 'object') {
|
|
68
|
+
const record = item as Record<string, unknown>;
|
|
69
|
+
if (record[keyField] !== undefined) {
|
|
70
|
+
return String(record[keyField]);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return index;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function handleScroll(e: Event) {
|
|
77
|
+
const target = e.currentTarget as HTMLDivElement;
|
|
78
|
+
scrollTop = target.scrollTop;
|
|
79
|
+
measuredViewportHeight = target.clientHeight;
|
|
80
|
+
|
|
81
|
+
// 触底检测
|
|
82
|
+
if (onendreached && totalCount > 0) {
|
|
83
|
+
const distanceToBottom = target.scrollHeight - (target.scrollTop + target.clientHeight);
|
|
84
|
+
if (distanceToBottom <= endReachedThreshold) {
|
|
85
|
+
onendreached();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 供外部调用的滚动方法
|
|
91
|
+
export function scrollToIndex(index: number, behavior: 'auto' | 'smooth' = 'smooth') {
|
|
92
|
+
if (!containerElement) return;
|
|
93
|
+
const clampedIndex = Math.max(0, Math.min(index, totalCount - 1));
|
|
94
|
+
containerElement.scrollTo({
|
|
95
|
+
top: clampedIndex * itemHeight,
|
|
96
|
+
behavior,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function scrollToTop(behavior: 'auto' | 'smooth' = 'smooth') {
|
|
101
|
+
if (!containerElement) return;
|
|
102
|
+
containerElement.scrollTo({ top: 0, behavior });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const containerHeightStyle = $derived(
|
|
106
|
+
typeof height === 'number' ? `${height}px` : height
|
|
107
|
+
);
|
|
108
|
+
</script>
|
|
109
|
+
|
|
110
|
+
<div
|
|
111
|
+
bind:this={containerElement}
|
|
112
|
+
data-svadmin-component="VirtualList"
|
|
113
|
+
class={cn(
|
|
114
|
+
'relative w-full overflow-y-auto rounded-lg border border-border bg-background shadow-xs',
|
|
115
|
+
className
|
|
116
|
+
)}
|
|
117
|
+
style="height: {containerHeightStyle};"
|
|
118
|
+
onscroll={handleScroll}
|
|
119
|
+
>
|
|
120
|
+
{#if totalCount === 0}
|
|
121
|
+
{#if empty}
|
|
122
|
+
{@render empty()}
|
|
123
|
+
{:else}
|
|
124
|
+
<div class="flex h-full min-h-32 items-center justify-center p-6 text-sm text-muted-foreground">
|
|
125
|
+
暂无数据
|
|
126
|
+
</div>
|
|
127
|
+
{/if}
|
|
128
|
+
{:else}
|
|
129
|
+
<!-- 撑高总虚拟高度容器 -->
|
|
130
|
+
<div
|
|
131
|
+
class="relative w-full"
|
|
132
|
+
style="height: {totalHeight}px;"
|
|
133
|
+
>
|
|
134
|
+
<!-- 视口当前切片内容,使用 transform 移动到对应偏移 -->
|
|
135
|
+
<div
|
|
136
|
+
class="absolute left-0 top-0 w-full"
|
|
137
|
+
style="transform: translateY({offsetY}px);"
|
|
138
|
+
>
|
|
139
|
+
{#each visibleItems as { item, index } (getItemKey(item, index))}
|
|
140
|
+
<div
|
|
141
|
+
class="flex items-center w-full"
|
|
142
|
+
style="height: {itemHeight}px;"
|
|
143
|
+
data-index={index}
|
|
144
|
+
>
|
|
145
|
+
{#if children}
|
|
146
|
+
{@render children(item, index)}
|
|
147
|
+
{:else}
|
|
148
|
+
<div class="flex h-full w-full items-center border-b border-border/40 px-4 text-xs text-foreground">
|
|
149
|
+
{#if typeof item === 'object' && item !== null && 'title' in item}
|
|
150
|
+
{(item as Record<string, unknown>)['title']}
|
|
151
|
+
{:else if typeof item === 'object' && item !== null && 'name' in item}
|
|
152
|
+
{(item as Record<string, unknown>)['name']}
|
|
153
|
+
{:else if typeof item === 'object' && item !== null && 'label' in item}
|
|
154
|
+
{(item as Record<string, unknown>)['label']}
|
|
155
|
+
{:else}
|
|
156
|
+
Item {index + 1}: {String(item)}
|
|
157
|
+
{/if}
|
|
158
|
+
</div>
|
|
159
|
+
{/if}
|
|
160
|
+
</div>
|
|
161
|
+
{/each}
|
|
162
|
+
</div>
|
|
163
|
+
</div>
|
|
164
|
+
{/if}
|
|
165
|
+
</div>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
export interface VirtualListProps<T = unknown> {
|
|
3
|
+
items: T[];
|
|
4
|
+
itemHeight?: number;
|
|
5
|
+
height?: number | string;
|
|
6
|
+
overscan?: number;
|
|
7
|
+
endReachedThreshold?: number;
|
|
8
|
+
class?: string;
|
|
9
|
+
keyField?: string | keyof T | ((item: T, index: number) => string | number);
|
|
10
|
+
onendreached?: () => void;
|
|
11
|
+
children?: Snippet<[T, number]>;
|
|
12
|
+
empty?: Snippet;
|
|
13
|
+
}
|
|
14
|
+
declare function $$render<T>(): {
|
|
15
|
+
props: VirtualListProps<T>;
|
|
16
|
+
exports: {
|
|
17
|
+
scrollToIndex: (index: number, behavior?: "auto" | "smooth") => void;
|
|
18
|
+
scrollToTop: (behavior?: "auto" | "smooth") => void;
|
|
19
|
+
};
|
|
20
|
+
bindings: "";
|
|
21
|
+
slots: {};
|
|
22
|
+
events: {};
|
|
23
|
+
};
|
|
24
|
+
declare class __sveltets_Render<T> {
|
|
25
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
26
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
27
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
28
|
+
bindings(): "";
|
|
29
|
+
exports(): {
|
|
30
|
+
scrollToIndex: (index: number, behavior?: "auto" | "smooth") => void;
|
|
31
|
+
scrollToTop: (behavior?: "auto" | "smooth") => void;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
interface $$IsomorphicComponent {
|
|
35
|
+
new <T>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
36
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
37
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
38
|
+
<T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
39
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
40
|
+
}
|
|
41
|
+
declare const VirtualList: $$IsomorphicComponent;
|
|
42
|
+
type VirtualList<T> = InstanceType<typeof VirtualList<T>>;
|
|
43
|
+
export default VirtualList;
|