@svadmin/ui 0.119.0 → 0.120.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 +3 -0
- package/dist/app.theme.css +3 -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/NumberRoll.svelte +149 -0
- package/dist/components/NumberRoll.svelte.d.ts +17 -0
- package/dist/components/VirtualList.svelte +165 -0
- package/dist/components/VirtualList.svelte.d.ts +43 -0
- package/dist/index.d.ts +11 -16
- package/dist/index.js +4 -0
- package/package.json +1 -1
package/dist/app.css
CHANGED
package/dist/app.theme.css
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
const resources = $derived(adminContext.resources.filter((resource) => resource.meta?.['aboutVisible'] !== false));
|
|
12
12
|
const providerNames = $derived(adminContext.getDataProviderNames());
|
|
13
13
|
let { version: suppliedVersion, headingLevel = 'h1' }: { version?: string; headingLevel?: 'h1' | 'h2' } = $props();
|
|
14
|
-
const buildVersion = "0.
|
|
14
|
+
const buildVersion = "0.120.0";
|
|
15
15
|
const version = $derived(suppliedVersion ?? (
|
|
16
16
|
buildVersion.startsWith('__')
|
|
17
17
|
? (i18n.locale === 'zh-CN' ? '开发构建(未提供版本)' : 'Development build (version unavailable)')
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { cn } from '../utils.js';
|
|
3
|
+
import { Copy, Check, Printer } from '@lucide/svelte';
|
|
4
|
+
|
|
5
|
+
export type BarcodeFormat = 'CODE128' | 'CODE39' | 'EAN13';
|
|
6
|
+
|
|
7
|
+
export interface BarcodeProps {
|
|
8
|
+
value: string;
|
|
9
|
+
format?: BarcodeFormat;
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
showText?: boolean;
|
|
13
|
+
text?: string;
|
|
14
|
+
showActions?: boolean;
|
|
15
|
+
margin?: number;
|
|
16
|
+
class?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let {
|
|
20
|
+
value,
|
|
21
|
+
format = 'CODE128',
|
|
22
|
+
width = 2,
|
|
23
|
+
height = 60,
|
|
24
|
+
showText = true,
|
|
25
|
+
text,
|
|
26
|
+
showActions = false,
|
|
27
|
+
margin = 8,
|
|
28
|
+
class: className = '',
|
|
29
|
+
}: BarcodeProps = $props();
|
|
30
|
+
|
|
31
|
+
let copied = $state(false);
|
|
32
|
+
|
|
33
|
+
// CODE39 模式表: 每个字符由 9 个单元组成(5条4空),其中3个为宽单元(值为1表示宽,0表示窄)
|
|
34
|
+
const CODE39_ENCODINGS: Record<string, string> = {
|
|
35
|
+
'0': '000110100', '1': '100100001', '2': '001100001', '3': '101100000',
|
|
36
|
+
'4': '000110001', '5': '100110000', '6': '001110000', '7': '000100101',
|
|
37
|
+
'8': '100100100', '9': '001100100', 'A': '100001001', 'B': '001001001',
|
|
38
|
+
'C': '101001000', 'D': '000011001', 'E': '100011000', 'F': '001011000',
|
|
39
|
+
'G': '000001101', 'H': '100001100', 'I': '001001100', 'J': '000011100',
|
|
40
|
+
'K': '100000011', 'L': '001000011', 'M': '101000010', 'N': '000010011',
|
|
41
|
+
'O': '100010010', 'P': '001010010', 'Q': '000000111', 'R': '100000110',
|
|
42
|
+
'S': '001000110', 'T': '000010110', 'U': '110000001', 'V': '011000001',
|
|
43
|
+
'W': '111000000', 'X': '010010001', 'Y': '110010000', 'Z': '011010000',
|
|
44
|
+
'-': '010000101', '.': '110000100', ' ': '011000100', '$': '010101000',
|
|
45
|
+
'/': '010100010', '+': '010001010', '%': '000101010', '*': '010010100'
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// CODE128 模式表 (常用模式)
|
|
49
|
+
const CODE128_PATTERNS: string[] = [
|
|
50
|
+
'212222', '222122', '222221', '121223', '121322', '131222', '122213', '122312', '132212', '221213',
|
|
51
|
+
'221312', '231212', '112232', '122132', '122231', '113222', '123122', '123221', '223211', '221132',
|
|
52
|
+
'221231', '213212', '223112', '312131', '311222', '321122', '321221', '312212', '322112', '322211',
|
|
53
|
+
'212123', '212321', '232121', '111323', '131123', '131321', '112313', '132113', '132311', '211313',
|
|
54
|
+
'231113', '231311', '112133', '112331', '132131', '113123', '113321', '133121', '313121', '211331',
|
|
55
|
+
'231131', '213113', '213311', '213131', '311123', '311321', '331121', '312113', '312311', '332111',
|
|
56
|
+
'314111', '221411', '431111', '111224', '111422', '121124', '121421', '141122', '141221', '112214',
|
|
57
|
+
'112412', '122114', '122411', '142112', '142211', '241211', '221114', '413111', '241112', '134111',
|
|
58
|
+
'111242', '121142', '121241', '114212', '124112', '124211', '411212', '421112', '421211', '212141',
|
|
59
|
+
'214121', '412121', '111143', '111341', '131141', '114113', '114311', '411113', '411311', '113141',
|
|
60
|
+
'114131', '311141', '411131', '211412', '211214', '211232', '2331112'
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
interface BarSegment {
|
|
64
|
+
x: number;
|
|
65
|
+
width: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const barcodeBars = $derived.by<BarSegment[]>(() => {
|
|
69
|
+
if (!value) return [];
|
|
70
|
+
const bars: BarSegment[] = [];
|
|
71
|
+
let currentX = margin;
|
|
72
|
+
|
|
73
|
+
if (format === 'CODE39') {
|
|
74
|
+
const cleanValue = `*${value.toUpperCase()}*`;
|
|
75
|
+
for (let i = 0; i < cleanValue.length; i++) {
|
|
76
|
+
const char = cleanValue[i] ?? '-';
|
|
77
|
+
const pattern = CODE39_ENCODINGS[char] ?? CODE39_ENCODINGS['-'] ?? '010000101';
|
|
78
|
+
// 9 个单元:偶数索引是条,奇数索引是空
|
|
79
|
+
for (let j = 0; j < 9; j++) {
|
|
80
|
+
const isBar = j % 2 === 0;
|
|
81
|
+
const isWide = pattern[j] === '1';
|
|
82
|
+
const segmentWidth = (isWide ? 3 : 1) * width;
|
|
83
|
+
if (isBar) {
|
|
84
|
+
bars.push({ x: currentX, width: segmentWidth });
|
|
85
|
+
}
|
|
86
|
+
currentX += segmentWidth;
|
|
87
|
+
}
|
|
88
|
+
// 字符间间隔
|
|
89
|
+
currentX += width;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
// CODE128 (默认 Code B 模式)
|
|
93
|
+
const startBIndex = 104;
|
|
94
|
+
const stopIndex = 106;
|
|
95
|
+
let checksum = startBIndex;
|
|
96
|
+
const codes: number[] = [startBIndex];
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < value.length; i++) {
|
|
99
|
+
const charCode = value.charCodeAt(i);
|
|
100
|
+
const codeIndex = charCode >= 32 && charCode <= 126 ? charCode - 32 : 0;
|
|
101
|
+
codes.push(codeIndex);
|
|
102
|
+
checksum += codeIndex * (i + 1);
|
|
103
|
+
}
|
|
104
|
+
codes.push(checksum % 103);
|
|
105
|
+
codes.push(stopIndex);
|
|
106
|
+
|
|
107
|
+
for (const code of codes) {
|
|
108
|
+
const pattern = CODE128_PATTERNS[code] ?? CODE128_PATTERNS[0] ?? '212222';
|
|
109
|
+
let isBar = true;
|
|
110
|
+
for (let j = 0; j < pattern.length; j++) {
|
|
111
|
+
const char = pattern[j] ?? '1';
|
|
112
|
+
const runLength = parseInt(char, 10) * width;
|
|
113
|
+
if (isBar) {
|
|
114
|
+
bars.push({ x: currentX, width: runLength });
|
|
115
|
+
}
|
|
116
|
+
currentX += runLength;
|
|
117
|
+
isBar = !isBar;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return bars;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const totalWidth = $derived.by(() => {
|
|
126
|
+
if (barcodeBars.length === 0) return 200;
|
|
127
|
+
const lastBar = barcodeBars[barcodeBars.length - 1];
|
|
128
|
+
return lastBar ? lastBar.x + lastBar.width + margin : 200;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const displayLabel = $derived(text ?? value);
|
|
132
|
+
|
|
133
|
+
async function copyValue() {
|
|
134
|
+
try {
|
|
135
|
+
await navigator.clipboard.writeText(value);
|
|
136
|
+
copied = true;
|
|
137
|
+
setTimeout(() => { copied = false; }, 2000);
|
|
138
|
+
} catch {
|
|
139
|
+
// ignore
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function handlePrint() {
|
|
144
|
+
window.print();
|
|
145
|
+
}
|
|
146
|
+
</script>
|
|
147
|
+
|
|
148
|
+
<div
|
|
149
|
+
data-svadmin-component="Barcode"
|
|
150
|
+
class={cn(
|
|
151
|
+
'inline-flex flex-col items-center justify-center rounded-lg border border-border bg-card p-4 shadow-xs select-none transition-all',
|
|
152
|
+
className
|
|
153
|
+
)}
|
|
154
|
+
>
|
|
155
|
+
<!-- 条形码 SVG 渲染 -->
|
|
156
|
+
<svg
|
|
157
|
+
width={totalWidth}
|
|
158
|
+
height={height + (showText ? 24 : 0)}
|
|
159
|
+
viewBox="0 0 {totalWidth} {height + (showText ? 24 : 0)}"
|
|
160
|
+
class="max-w-full overflow-visible"
|
|
161
|
+
aria-label="Barcode for {value}"
|
|
162
|
+
>
|
|
163
|
+
<!-- 背景填充 -->
|
|
164
|
+
<rect width={totalWidth} height={height + (showText ? 24 : 0)} fill="transparent" />
|
|
165
|
+
|
|
166
|
+
<!-- 条形码黑条 -->
|
|
167
|
+
{#each barcodeBars as bar, idx (`bar-${idx}`)}
|
|
168
|
+
<rect
|
|
169
|
+
x={bar.x}
|
|
170
|
+
y={margin}
|
|
171
|
+
width={bar.width}
|
|
172
|
+
height={height - margin * 0.5}
|
|
173
|
+
class="fill-foreground"
|
|
174
|
+
/>
|
|
175
|
+
{/each}
|
|
176
|
+
|
|
177
|
+
<!-- 下方文本 -->
|
|
178
|
+
{#if showText}
|
|
179
|
+
<text
|
|
180
|
+
x={totalWidth / 2}
|
|
181
|
+
y={height + 16}
|
|
182
|
+
text-anchor="middle"
|
|
183
|
+
class="fill-muted-foreground font-mono text-xs tracking-wider"
|
|
184
|
+
>
|
|
185
|
+
{displayLabel}
|
|
186
|
+
</text>
|
|
187
|
+
{/if}
|
|
188
|
+
</svg>
|
|
189
|
+
|
|
190
|
+
<!-- 快捷操作栏(复制、打印) -->
|
|
191
|
+
{#if showActions}
|
|
192
|
+
<div class="mt-3 flex items-center gap-2 border-t border-border pt-2.5 w-full justify-center">
|
|
193
|
+
<button
|
|
194
|
+
type="button"
|
|
195
|
+
onclick={copyValue}
|
|
196
|
+
class="inline-flex items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground transition-colors cursor-pointer"
|
|
197
|
+
title="复制条码内容"
|
|
198
|
+
>
|
|
199
|
+
{#if copied}
|
|
200
|
+
<Check class="h-3.5 w-3.5 text-primary" />
|
|
201
|
+
<span>已复制</span>
|
|
202
|
+
{:else}
|
|
203
|
+
<Copy class="h-3.5 w-3.5" />
|
|
204
|
+
<span>复制</span>
|
|
205
|
+
{/if}
|
|
206
|
+
</button>
|
|
207
|
+
|
|
208
|
+
<button
|
|
209
|
+
type="button"
|
|
210
|
+
onclick={handlePrint}
|
|
211
|
+
class="inline-flex items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground transition-colors cursor-pointer"
|
|
212
|
+
title="打印条形码"
|
|
213
|
+
>
|
|
214
|
+
<Printer class="h-3.5 w-3.5" />
|
|
215
|
+
<span>打印</span>
|
|
216
|
+
</button>
|
|
217
|
+
</div>
|
|
218
|
+
{/if}
|
|
219
|
+
</div>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type BarcodeFormat = 'CODE128' | 'CODE39' | 'EAN13';
|
|
2
|
+
export interface BarcodeProps {
|
|
3
|
+
value: string;
|
|
4
|
+
format?: BarcodeFormat;
|
|
5
|
+
width?: number;
|
|
6
|
+
height?: number;
|
|
7
|
+
showText?: boolean;
|
|
8
|
+
text?: string;
|
|
9
|
+
showActions?: boolean;
|
|
10
|
+
margin?: number;
|
|
11
|
+
class?: string;
|
|
12
|
+
}
|
|
13
|
+
declare const Barcode: import("svelte").Component<BarcodeProps, {}, "">;
|
|
14
|
+
type Barcode = ReturnType<typeof Barcode>;
|
|
15
|
+
export default Barcode;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount } from 'svelte';
|
|
3
|
+
import { cn } from '../utils.js';
|
|
4
|
+
import { TrendingUp, TrendingDown, Minus } from '@lucide/svelte';
|
|
5
|
+
|
|
6
|
+
export interface NumberRollProps {
|
|
7
|
+
value: number;
|
|
8
|
+
from?: number;
|
|
9
|
+
duration?: number;
|
|
10
|
+
decimals?: number;
|
|
11
|
+
separator?: string;
|
|
12
|
+
decimalSeparator?: string;
|
|
13
|
+
prefix?: string;
|
|
14
|
+
suffix?: string;
|
|
15
|
+
trendColor?: boolean;
|
|
16
|
+
showTrendIcon?: boolean;
|
|
17
|
+
class?: string;
|
|
18
|
+
oncomplete?: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let {
|
|
22
|
+
value,
|
|
23
|
+
from = 0,
|
|
24
|
+
duration = 1200,
|
|
25
|
+
decimals = 0,
|
|
26
|
+
separator = ',',
|
|
27
|
+
decimalSeparator = '.',
|
|
28
|
+
prefix = '',
|
|
29
|
+
suffix = '',
|
|
30
|
+
trendColor = false,
|
|
31
|
+
showTrendIcon = false,
|
|
32
|
+
class: className = '',
|
|
33
|
+
oncomplete,
|
|
34
|
+
}: NumberRollProps = $props();
|
|
35
|
+
|
|
36
|
+
let displayValue = $state<number | null>(null);
|
|
37
|
+
let animationFrameId: number | null = null;
|
|
38
|
+
let previousValue = $state<number | null>(null);
|
|
39
|
+
|
|
40
|
+
// 四次缓动减速函数 (Ease Out Quart)
|
|
41
|
+
function easeOutQuart(x: number): number {
|
|
42
|
+
return 1 - Math.pow(1 - x, 4);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function startAnimation(startVal: number, endVal: number) {
|
|
46
|
+
if (animationFrameId !== null) {
|
|
47
|
+
cancelAnimationFrame(animationFrameId);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (duration <= 0) {
|
|
51
|
+
displayValue = endVal;
|
|
52
|
+
previousValue = endVal;
|
|
53
|
+
oncomplete?.();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const startTime = performance.now();
|
|
58
|
+
|
|
59
|
+
function step(currentTime: number) {
|
|
60
|
+
const elapsed = currentTime - startTime;
|
|
61
|
+
const progress = Math.min(elapsed / duration, 1);
|
|
62
|
+
const easedProgress = easeOutQuart(progress);
|
|
63
|
+
|
|
64
|
+
displayValue = startVal + (endVal - startVal) * easedProgress;
|
|
65
|
+
|
|
66
|
+
if (progress < 1) {
|
|
67
|
+
animationFrameId = requestAnimationFrame(step);
|
|
68
|
+
} else {
|
|
69
|
+
displayValue = endVal;
|
|
70
|
+
previousValue = endVal;
|
|
71
|
+
animationFrameId = null;
|
|
72
|
+
oncomplete?.();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
animationFrameId = requestAnimationFrame(step);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 监听 value 变化并平滑缓动
|
|
80
|
+
$effect(() => {
|
|
81
|
+
const currentTarget = value;
|
|
82
|
+
const startVal = previousValue ?? from;
|
|
83
|
+
if (displayValue === null) {
|
|
84
|
+
displayValue = startVal;
|
|
85
|
+
}
|
|
86
|
+
startAnimation(startVal, currentTarget);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
onMount(() => {
|
|
90
|
+
return () => {
|
|
91
|
+
if (animationFrameId !== null) {
|
|
92
|
+
cancelAnimationFrame(animationFrameId);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// 格式化输出数字(带千分位、小数位)
|
|
98
|
+
const formattedNumber = $derived.by(() => {
|
|
99
|
+
const current = displayValue ?? value;
|
|
100
|
+
const fixed = current.toFixed(decimals);
|
|
101
|
+
const parts = fixed.split('.');
|
|
102
|
+
const intPart = parts[0] ?? '0';
|
|
103
|
+
const decPart = parts[1];
|
|
104
|
+
const formattedInt = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
105
|
+
return decPart !== undefined ? `${formattedInt}${decimalSeparator}${decPart}` : formattedInt;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// 趋势计算 (对比 0)
|
|
109
|
+
const isPositive = $derived(value > 0);
|
|
110
|
+
const isNegative = $derived(value < 0);
|
|
111
|
+
|
|
112
|
+
const trendColorClass = $derived.by(() => {
|
|
113
|
+
if (!trendColor) return 'text-foreground';
|
|
114
|
+
if (isPositive) return 'text-primary';
|
|
115
|
+
if (isNegative) return 'text-destructive';
|
|
116
|
+
return 'text-muted-foreground';
|
|
117
|
+
});
|
|
118
|
+
</script>
|
|
119
|
+
|
|
120
|
+
<span
|
|
121
|
+
data-svadmin-component="NumberRoll"
|
|
122
|
+
class={cn(
|
|
123
|
+
'inline-flex items-center gap-1 font-mono font-semibold tracking-tight tabular-nums select-none transition-colors',
|
|
124
|
+
trendColorClass,
|
|
125
|
+
className
|
|
126
|
+
)}
|
|
127
|
+
>
|
|
128
|
+
{#if showTrendIcon}
|
|
129
|
+
<span class="inline-flex shrink-0 items-center justify-center">
|
|
130
|
+
{#if isPositive}
|
|
131
|
+
<TrendingUp class="h-4 w-4 text-primary" />
|
|
132
|
+
{:else if isNegative}
|
|
133
|
+
<TrendingDown class="h-4 w-4 text-destructive" />
|
|
134
|
+
{:else}
|
|
135
|
+
<Minus class="h-4 w-4 text-muted-foreground" />
|
|
136
|
+
{/if}
|
|
137
|
+
</span>
|
|
138
|
+
{/if}
|
|
139
|
+
|
|
140
|
+
{#if prefix}
|
|
141
|
+
<span class="text-xs font-normal text-muted-foreground">{prefix}</span>
|
|
142
|
+
{/if}
|
|
143
|
+
|
|
144
|
+
<span class="text-base font-bold">{formattedNumber}</span>
|
|
145
|
+
|
|
146
|
+
{#if suffix}
|
|
147
|
+
<span class="text-xs font-normal text-muted-foreground">{suffix}</span>
|
|
148
|
+
{/if}
|
|
149
|
+
</span>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface NumberRollProps {
|
|
2
|
+
value: number;
|
|
3
|
+
from?: number;
|
|
4
|
+
duration?: number;
|
|
5
|
+
decimals?: number;
|
|
6
|
+
separator?: string;
|
|
7
|
+
decimalSeparator?: string;
|
|
8
|
+
prefix?: string;
|
|
9
|
+
suffix?: string;
|
|
10
|
+
trendColor?: boolean;
|
|
11
|
+
showTrendIcon?: boolean;
|
|
12
|
+
class?: string;
|
|
13
|
+
oncomplete?: () => void;
|
|
14
|
+
}
|
|
15
|
+
declare const NumberRoll: import("svelte").Component<NumberRollProps, {}, "">;
|
|
16
|
+
type NumberRoll = ReturnType<typeof NumberRoll>;
|
|
17
|
+
export default NumberRoll;
|
|
@@ -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;
|
package/dist/index.d.ts
CHANGED
|
@@ -557,21 +557,16 @@ export { default as ConcurrentConflictModal } from './components/ConcurrentConfl
|
|
|
557
557
|
export type { ConcurrentConflictModalProps, ConflictFieldDiff } from './components/ConcurrentConflictModal.svelte';
|
|
558
558
|
export { default as TrendIndicator, default as Trend } from './components/TrendIndicator.svelte';
|
|
559
559
|
export type { TrendIndicatorProps } from './components/TrendIndicator.svelte';
|
|
560
|
-
export { default as VirtualTreeSelect } from './components/VirtualTreeSelect.svelte';
|
|
561
|
-
export
|
|
562
|
-
export { default as
|
|
563
|
-
export type { CronExpressionBuilderProps, CronPreset } from './components/CronExpressionBuilder.svelte';
|
|
564
|
-
export { default as OrgChart } from './components/OrgChart.svelte';
|
|
565
|
-
export type { OrgChartProps, OrgNode } from './components/OrgChart.svelte';
|
|
560
|
+
export { default as VirtualTreeSelect, type VirtualTreeSelectProps, type VirtualTreeNode } from './components/VirtualTreeSelect.svelte';
|
|
561
|
+
export { default as CronExpressionBuilder, type CronExpressionBuilderProps, type CronPreset } from './components/CronExpressionBuilder.svelte';
|
|
562
|
+
export { default as OrgChart, type OrgChartProps, type OrgNode } from './components/OrgChart.svelte';
|
|
566
563
|
export { GaugeChart as SpeedometerGauge } from './components/charts/index.js';
|
|
567
564
|
export type { GaugeChartProps, GaugeThreshold, GaugeTone, GaugeType, GaugeMode } from './components/charts/GaugeChart.svelte';
|
|
568
|
-
export { default as MetricGrid, default as DashboardMetricGrid } from './components/MetricGrid.svelte';
|
|
569
|
-
export type
|
|
570
|
-
export { default as
|
|
571
|
-
export type
|
|
572
|
-
export { default as
|
|
573
|
-
export
|
|
574
|
-
export {
|
|
575
|
-
export
|
|
576
|
-
export { default as OfflineGuard } from './components/OfflineGuard.svelte';
|
|
577
|
-
export type { OfflineGuardProps } from './components/OfflineGuard.svelte';
|
|
565
|
+
export { default as MetricGrid, default as DashboardMetricGrid, type MetricGridProps } from './components/MetricGrid.svelte';
|
|
566
|
+
export { default as MicroAppFrame, default as EmbeddedSandbox, type MicroAppFrameProps, type MicroAppMessage } from './components/MicroAppFrame.svelte';
|
|
567
|
+
export { default as TaskCenter, default as BackgroundTaskDrawer, type TaskCenterProps, type BackgroundTask, type BackgroundTaskStatus } from './components/TaskCenter.svelte';
|
|
568
|
+
export { useNetworkStatus, type NetworkInformationLike, type NetworkStatusState, type UseNetworkStatusOptions } from './components/useNetworkStatus.svelte.js';
|
|
569
|
+
export { default as OfflineGuard, type OfflineGuardProps } from './components/OfflineGuard.svelte';
|
|
570
|
+
export { default as Barcode, type BarcodeProps, type BarcodeFormat } from './components/Barcode.svelte';
|
|
571
|
+
export { default as NumberRoll, type NumberRollProps } from './components/NumberRoll.svelte';
|
|
572
|
+
export { default as VirtualList, type VirtualListProps } from './components/VirtualList.svelte';
|
package/dist/index.js
CHANGED
|
@@ -444,3 +444,7 @@ export { default as TaskCenter, default as BackgroundTaskDrawer } from './compon
|
|
|
444
444
|
// Enterprise offline resilience & network monitoring suite
|
|
445
445
|
export { useNetworkStatus } from './components/useNetworkStatus.svelte.js';
|
|
446
446
|
export { default as OfflineGuard } from './components/OfflineGuard.svelte';
|
|
447
|
+
// Enterprise barcode, dynamic number roll & virtual list suite
|
|
448
|
+
export { default as Barcode } from './components/Barcode.svelte';
|
|
449
|
+
export { default as NumberRoll } from './components/NumberRoll.svelte';
|
|
450
|
+
export { default as VirtualList } from './components/VirtualList.svelte';
|