@svadmin/ui 0.111.0 → 0.113.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 +216 -0
- package/dist/app.theme.css +216 -0
- package/dist/components/AboutSettings.svelte +1 -1
- package/dist/components/AutoForm.svelte +24 -35
- package/dist/components/FormDependency.svelte +36 -0
- package/dist/components/FormDependency.svelte.d.ts +12 -0
- package/dist/components/Icon.svelte +57 -0
- package/dist/components/Icon.svelte.d.ts +10 -0
- package/dist/components/JsonSchemaForm.svelte +68 -10
- package/dist/components/PageContainer.svelte +263 -0
- package/dist/components/PageContainer.svelte.d.ts +53 -0
- package/dist/components/ProDescriptions.svelte +278 -0
- package/dist/components/ProDescriptions.svelte.d.ts +37 -0
- package/dist/components/QueryFilter.svelte +194 -0
- package/dist/components/QueryFilter.svelte.d.ts +34 -0
- package/dist/components/RealtimeMonitorView.svelte +245 -0
- package/dist/components/RealtimeMonitorView.svelte.d.ts +40 -0
- package/dist/components/RoleSwitcher.svelte +267 -0
- package/dist/components/RoleSwitcher.svelte.d.ts +40 -0
- package/dist/components/WorkflowDesigner.svelte +244 -0
- package/dist/components/WorkflowDesigner.svelte.d.ts +29 -0
- package/dist/components/auto-form-layout.d.ts +28 -0
- package/dist/components/auto-form-layout.js +70 -0
- package/dist/components/fields/UploadField.svelte +222 -0
- package/dist/components/fields/UploadField.svelte.d.ts +14 -0
- package/dist/components/icons/IconApprove.svelte +35 -0
- package/dist/components/icons/IconApprove.svelte.d.ts +4 -0
- package/dist/components/icons/IconAudit.svelte +38 -0
- package/dist/components/icons/IconAudit.svelte.d.ts +4 -0
- package/dist/components/icons/IconBilling.svelte +37 -0
- package/dist/components/icons/IconBilling.svelte.d.ts +4 -0
- package/dist/components/icons/IconCluster.svelte +41 -0
- package/dist/components/icons/IconCluster.svelte.d.ts +4 -0
- package/dist/components/icons/IconMonitor.svelte +37 -0
- package/dist/components/icons/IconMonitor.svelte.d.ts +4 -0
- package/dist/components/icons/IconReject.svelte +36 -0
- package/dist/components/icons/IconReject.svelte.d.ts +4 -0
- package/dist/components/icons/IconSeal.svelte +36 -0
- package/dist/components/icons/IconSeal.svelte.d.ts +4 -0
- package/dist/components/icons/IconSecurity.svelte +35 -0
- package/dist/components/icons/IconSecurity.svelte.d.ts +4 -0
- package/dist/components/icons/IconTenant.svelte +39 -0
- package/dist/components/icons/IconTenant.svelte.d.ts +4 -0
- package/dist/components/icons/IconWorkflow.svelte +38 -0
- package/dist/components/icons/IconWorkflow.svelte.d.ts +4 -0
- package/dist/components/icons/index.d.ts +11 -0
- package/dist/components/icons/index.js +10 -0
- package/dist/components/icons/types.d.ts +15 -0
- package/dist/components/icons/types.js +1 -0
- package/dist/components/json-schema-form-state.d.ts +3 -1
- package/dist/icon-registry.d.ts +27 -0
- package/dist/icon-registry.js +52 -0
- package/dist/icons.d.ts +5 -1
- package/dist/icons.js +4 -0
- package/dist/index.d.ts +27 -7
- package/dist/index.js +19 -6
- package/dist/intent-preload.d.ts +28 -0
- package/dist/intent-preload.js +76 -0
- package/package.json +5 -5
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { cn } from '../utils';
|
|
4
|
+
|
|
5
|
+
export interface WorkflowNode {
|
|
6
|
+
id: string;
|
|
7
|
+
type: 'start' | 'approver' | 'condition' | 'notifier';
|
|
8
|
+
title: string;
|
|
9
|
+
content?: string;
|
|
10
|
+
status?: 'pending' | 'active' | 'completed' | 'rejected';
|
|
11
|
+
error?: string;
|
|
12
|
+
assignees?: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface WorkflowDesignerProps {
|
|
16
|
+
/** List of workflow steps in linear or branching order */
|
|
17
|
+
nodes?: WorkflowNode[];
|
|
18
|
+
/** Whether the designer is in view-only / execution status mode */
|
|
19
|
+
readonly?: boolean;
|
|
20
|
+
/** Callback when a node card is clicked for editing */
|
|
21
|
+
onnodeclick?: (node: WorkflowNode) => void;
|
|
22
|
+
/** Callback when an add node button is clicked */
|
|
23
|
+
onaddnode?: (targetNodeId: string, type: 'approver' | 'condition' | 'notifier') => void;
|
|
24
|
+
/** Callback when a node is removed */
|
|
25
|
+
ondeletenode?: (nodeId: string) => void;
|
|
26
|
+
/** Extra header or toolbar slot */
|
|
27
|
+
extra?: Snippet;
|
|
28
|
+
/** Additional CSS classes */
|
|
29
|
+
class?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let {
|
|
33
|
+
nodes = [
|
|
34
|
+
{ id: '1', type: 'start', title: '发起人', content: '所有人' },
|
|
35
|
+
{ id: '2', type: 'approver', title: '直属主管审批', content: '主管审批' },
|
|
36
|
+
{ id: '3', type: 'notifier', title: '抄送人', content: '抄送发起人' },
|
|
37
|
+
],
|
|
38
|
+
readonly = false,
|
|
39
|
+
onnodeclick,
|
|
40
|
+
onaddnode,
|
|
41
|
+
ondeletenode,
|
|
42
|
+
extra,
|
|
43
|
+
class: className = '',
|
|
44
|
+
}: WorkflowDesignerProps = $props();
|
|
45
|
+
|
|
46
|
+
let activeAddMenuId = $state<string | null>(null);
|
|
47
|
+
|
|
48
|
+
function getNodeTypeColor(type: WorkflowNode['type']) {
|
|
49
|
+
switch (type) {
|
|
50
|
+
case 'start':
|
|
51
|
+
return {
|
|
52
|
+
headerBg: 'bg-primary',
|
|
53
|
+
headerText: 'text-primary-foreground',
|
|
54
|
+
badge: '发起',
|
|
55
|
+
};
|
|
56
|
+
case 'approver':
|
|
57
|
+
return {
|
|
58
|
+
headerBg: 'bg-warning',
|
|
59
|
+
headerText: 'text-warning-foreground',
|
|
60
|
+
badge: '审批人',
|
|
61
|
+
};
|
|
62
|
+
case 'condition':
|
|
63
|
+
return {
|
|
64
|
+
headerBg: 'bg-success',
|
|
65
|
+
headerText: 'text-success-foreground',
|
|
66
|
+
badge: '条件分支',
|
|
67
|
+
};
|
|
68
|
+
case 'notifier':
|
|
69
|
+
return {
|
|
70
|
+
headerBg: 'bg-info',
|
|
71
|
+
headerText: 'text-info-foreground',
|
|
72
|
+
badge: '抄送人',
|
|
73
|
+
};
|
|
74
|
+
default:
|
|
75
|
+
return {
|
|
76
|
+
headerBg: 'bg-muted-foreground',
|
|
77
|
+
headerText: 'text-background',
|
|
78
|
+
badge: '节点',
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getStatusTone(status?: WorkflowNode['status']) {
|
|
84
|
+
switch (status) {
|
|
85
|
+
case 'completed':
|
|
86
|
+
return 'bg-success/10 text-success border-success/30';
|
|
87
|
+
case 'active':
|
|
88
|
+
return 'bg-primary/10 text-primary border-primary/30';
|
|
89
|
+
case 'rejected':
|
|
90
|
+
return 'bg-destructive/10 text-destructive border-destructive/30';
|
|
91
|
+
case 'pending':
|
|
92
|
+
default:
|
|
93
|
+
return 'bg-muted text-muted-foreground border-border';
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function handleAdd(nodeId: string, type: 'approver' | 'condition' | 'notifier') {
|
|
98
|
+
onaddnode?.(nodeId, type);
|
|
99
|
+
activeAddMenuId = null;
|
|
100
|
+
}
|
|
101
|
+
</script>
|
|
102
|
+
|
|
103
|
+
<div
|
|
104
|
+
data-svadmin-component="WorkflowDesigner"
|
|
105
|
+
class={cn(
|
|
106
|
+
'svadmin-workflow-designer relative flex w-full flex-col items-center justify-start bg-background py-6',
|
|
107
|
+
className
|
|
108
|
+
)}
|
|
109
|
+
>
|
|
110
|
+
{#if extra}
|
|
111
|
+
<div class="mb-4 flex w-full max-w-md items-center justify-end px-4">
|
|
112
|
+
{@render extra()}
|
|
113
|
+
</div>
|
|
114
|
+
{/if}
|
|
115
|
+
|
|
116
|
+
<div class="relative flex flex-col items-center gap-0">
|
|
117
|
+
{#each nodes as node, index (node.id)}
|
|
118
|
+
{@const colors = getNodeTypeColor(node.type)}
|
|
119
|
+
<!-- Node Card -->
|
|
120
|
+
<div class="group/node relative flex w-72 flex-col rounded-[4px] border border-border bg-card shadow-xs transition-all hover:shadow-md">
|
|
121
|
+
<!-- Node Header -->
|
|
122
|
+
<div class={cn('flex items-center justify-between px-3 py-1.5 rounded-t-[3px]', colors.headerBg, colors.headerText)}>
|
|
123
|
+
<div class="flex items-center gap-1.5 text-xs font-medium">
|
|
124
|
+
<span>{node.title}</span>
|
|
125
|
+
</div>
|
|
126
|
+
|
|
127
|
+
<div class="flex items-center gap-1">
|
|
128
|
+
{#if node.status}
|
|
129
|
+
<span class={cn('rounded-[2px] border px-1.5 py-0.2 text-[10px] font-normal leading-tight', getStatusTone(node.status))}>
|
|
130
|
+
{node.status}
|
|
131
|
+
</span>
|
|
132
|
+
{/if}
|
|
133
|
+
|
|
134
|
+
{#if !readonly && node.type !== 'start'}
|
|
135
|
+
<button
|
|
136
|
+
type="button"
|
|
137
|
+
aria-label={`删除 ${node.title}`}
|
|
138
|
+
class="opacity-0 transition-opacity group-hover/node:opacity-100 p-0.5 text-white/80 hover:text-white"
|
|
139
|
+
onclick={(e) => {
|
|
140
|
+
e.stopPropagation();
|
|
141
|
+
ondeletenode?.(node.id);
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
<svg class="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
|
|
145
|
+
<line x1="18" y1="6" x2="6" y2="18" />
|
|
146
|
+
<line x1="6" y1="6" x2="18" y2="18" />
|
|
147
|
+
</svg>
|
|
148
|
+
</button>
|
|
149
|
+
{/if}
|
|
150
|
+
</div>
|
|
151
|
+
</div>
|
|
152
|
+
|
|
153
|
+
<!-- Node Body -->
|
|
154
|
+
<button
|
|
155
|
+
type="button"
|
|
156
|
+
class="flex w-full flex-col items-start p-3 text-left transition-colors hover:bg-muted/30 focus-visible:outline-none"
|
|
157
|
+
onclick={() => onnodeclick?.(node)}
|
|
158
|
+
>
|
|
159
|
+
<div class="text-xs text-foreground line-clamp-2">
|
|
160
|
+
{node.content || '请设置节点属性'}
|
|
161
|
+
</div>
|
|
162
|
+
|
|
163
|
+
{#if node.assignees && node.assignees.length > 0}
|
|
164
|
+
<div class="mt-2 flex flex-wrap gap-1">
|
|
165
|
+
{#each node.assignees as assignee (assignee)}
|
|
166
|
+
<span class="rounded-[2px] bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">
|
|
167
|
+
{assignee}
|
|
168
|
+
</span>
|
|
169
|
+
{/each}
|
|
170
|
+
</div>
|
|
171
|
+
{/if}
|
|
172
|
+
|
|
173
|
+
{#if node.error}
|
|
174
|
+
<span class="mt-1 text-[11px] text-destructive">
|
|
175
|
+
{node.error}
|
|
176
|
+
</span>
|
|
177
|
+
{/if}
|
|
178
|
+
</button>
|
|
179
|
+
</div>
|
|
180
|
+
|
|
181
|
+
<!-- Connecting line and Add Button -->
|
|
182
|
+
{#if index < nodes.length - 1 || !readonly}
|
|
183
|
+
<div class="relative flex h-14 w-full flex-col items-center justify-center">
|
|
184
|
+
<div class="h-full w-[1.5px] bg-border"></div>
|
|
185
|
+
|
|
186
|
+
{#if !readonly}
|
|
187
|
+
<div class="absolute inset-auto flex items-center justify-center">
|
|
188
|
+
<button
|
|
189
|
+
type="button"
|
|
190
|
+
aria-label="添加节点"
|
|
191
|
+
class="inline-flex h-6 w-6 items-center justify-center rounded-full bg-primary text-primary-foreground shadow-xs transition-transform hover:scale-110 active:scale-95 focus-visible:outline-none"
|
|
192
|
+
onclick={() => {
|
|
193
|
+
activeAddMenuId = activeAddMenuId === node.id ? null : node.id;
|
|
194
|
+
}}
|
|
195
|
+
>
|
|
196
|
+
<svg class="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
|
|
197
|
+
<line x1="12" y1="5" x2="12" y2="19" />
|
|
198
|
+
<line x1="5" y1="12" x2="19" y2="12" />
|
|
199
|
+
</svg>
|
|
200
|
+
</button>
|
|
201
|
+
|
|
202
|
+
{#if activeAddMenuId === node.id}
|
|
203
|
+
<!-- Add Node Menu Modal/Popover -->
|
|
204
|
+
<div class="absolute top-8 z-30 flex w-48 flex-col rounded-[4px] border border-border bg-card p-1.5 shadow-lg">
|
|
205
|
+
<button
|
|
206
|
+
type="button"
|
|
207
|
+
class="flex items-center gap-2 rounded-[2px] px-2.5 py-1.5 text-xs text-foreground transition-colors hover:bg-muted"
|
|
208
|
+
onclick={() => handleAdd(node.id, 'approver')}
|
|
209
|
+
>
|
|
210
|
+
<span class="h-2 w-2 rounded-full bg-warning"></span>
|
|
211
|
+
<span>审批人节点</span>
|
|
212
|
+
</button>
|
|
213
|
+
<button
|
|
214
|
+
type="button"
|
|
215
|
+
class="flex items-center gap-2 rounded-[2px] px-2.5 py-1.5 text-xs text-foreground transition-colors hover:bg-muted"
|
|
216
|
+
onclick={() => handleAdd(node.id, 'notifier')}
|
|
217
|
+
>
|
|
218
|
+
<span class="h-2 w-2 rounded-full bg-info"></span>
|
|
219
|
+
<span>抄送人节点</span>
|
|
220
|
+
</button>
|
|
221
|
+
<button
|
|
222
|
+
type="button"
|
|
223
|
+
class="flex items-center gap-2 rounded-[2px] px-2.5 py-1.5 text-xs text-foreground transition-colors hover:bg-muted"
|
|
224
|
+
onclick={() => handleAdd(node.id, 'condition')}
|
|
225
|
+
>
|
|
226
|
+
<span class="h-2 w-2 rounded-full bg-success"></span>
|
|
227
|
+
<span>条件分支</span>
|
|
228
|
+
</button>
|
|
229
|
+
</div>
|
|
230
|
+
{/if}
|
|
231
|
+
</div>
|
|
232
|
+
{/if}
|
|
233
|
+
</div>
|
|
234
|
+
{/if}
|
|
235
|
+
{/each}
|
|
236
|
+
|
|
237
|
+
<!-- End Node -->
|
|
238
|
+
<div class="flex items-center justify-center">
|
|
239
|
+
<div class="rounded-full bg-muted px-4 py-1 text-xs text-muted-foreground select-none">
|
|
240
|
+
流程结束
|
|
241
|
+
</div>
|
|
242
|
+
</div>
|
|
243
|
+
</div>
|
|
244
|
+
</div>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
export interface WorkflowNode {
|
|
3
|
+
id: string;
|
|
4
|
+
type: 'start' | 'approver' | 'condition' | 'notifier';
|
|
5
|
+
title: string;
|
|
6
|
+
content?: string;
|
|
7
|
+
status?: 'pending' | 'active' | 'completed' | 'rejected';
|
|
8
|
+
error?: string;
|
|
9
|
+
assignees?: string[];
|
|
10
|
+
}
|
|
11
|
+
export interface WorkflowDesignerProps {
|
|
12
|
+
/** List of workflow steps in linear or branching order */
|
|
13
|
+
nodes?: WorkflowNode[];
|
|
14
|
+
/** Whether the designer is in view-only / execution status mode */
|
|
15
|
+
readonly?: boolean;
|
|
16
|
+
/** Callback when a node card is clicked for editing */
|
|
17
|
+
onnodeclick?: (node: WorkflowNode) => void;
|
|
18
|
+
/** Callback when an add node button is clicked */
|
|
19
|
+
onaddnode?: (targetNodeId: string, type: 'approver' | 'condition' | 'notifier') => void;
|
|
20
|
+
/** Callback when a node is removed */
|
|
21
|
+
ondeletenode?: (nodeId: string) => void;
|
|
22
|
+
/** Extra header or toolbar slot */
|
|
23
|
+
extra?: Snippet;
|
|
24
|
+
/** Additional CSS classes */
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
declare const WorkflowDesigner: import("svelte").Component<WorkflowDesignerProps, {}, "">;
|
|
28
|
+
type WorkflowDesigner = ReturnType<typeof WorkflowDesigner>;
|
|
29
|
+
export default WorkflowDesigner;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layout helpers for `AutoForm`.
|
|
3
|
+
*
|
|
4
|
+
* Kept out of the component so the form stays under the module-size budget and
|
|
5
|
+
* the grid/span mapping can be tested without rendering.
|
|
6
|
+
*/
|
|
7
|
+
import type { FieldDefinition } from '@svadmin/core';
|
|
8
|
+
import type { SchemaFormLayoutPlan } from '@svadmin/core/schema-form';
|
|
9
|
+
export declare function gridClassFor(count: number): string;
|
|
10
|
+
export declare function spanClass(span: number | undefined): string;
|
|
11
|
+
export interface FormLayoutGroup {
|
|
12
|
+
name: string;
|
|
13
|
+
description: string | undefined;
|
|
14
|
+
columns: number;
|
|
15
|
+
fields: FieldDefinition[];
|
|
16
|
+
}
|
|
17
|
+
/** Map declarative layout spans into a lookup the field renderer can use. */
|
|
18
|
+
export declare function buildSpanByKey(layout: SchemaFormLayoutPlan | undefined): Map<string, number>;
|
|
19
|
+
/**
|
|
20
|
+
* Resolve render groups: declarative sections/steps win, then `field.group`,
|
|
21
|
+
* then a single ungrouped list.
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildFormGroups(input: {
|
|
24
|
+
layout: SchemaFormLayoutPlan | undefined;
|
|
25
|
+
fields: readonly FieldDefinition[];
|
|
26
|
+
columns: number;
|
|
27
|
+
}): FormLayoutGroup[];
|
|
28
|
+
export declare function isFullWidthField(field: Pick<FieldDefinition, 'type'>): boolean;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export function gridClassFor(count) {
|
|
2
|
+
if (count === 4)
|
|
3
|
+
return 'grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4';
|
|
4
|
+
if (count === 3)
|
|
5
|
+
return 'grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4';
|
|
6
|
+
if (count === 2)
|
|
7
|
+
return 'grid grid-cols-1 md:grid-cols-2 gap-4';
|
|
8
|
+
return 'space-y-5';
|
|
9
|
+
}
|
|
10
|
+
export function spanClass(span) {
|
|
11
|
+
if (span === 2)
|
|
12
|
+
return 'md:col-span-2';
|
|
13
|
+
if (span === 3)
|
|
14
|
+
return 'md:col-span-2 lg:col-span-3';
|
|
15
|
+
if (span !== undefined && span >= 4)
|
|
16
|
+
return 'md:col-span-2 lg:col-span-4';
|
|
17
|
+
return '';
|
|
18
|
+
}
|
|
19
|
+
/** Map declarative layout spans into a lookup the field renderer can use. */
|
|
20
|
+
export function buildSpanByKey(layout) {
|
|
21
|
+
const spans = new Map();
|
|
22
|
+
for (const group of layout?.groups ?? []) {
|
|
23
|
+
for (const field of group.fields)
|
|
24
|
+
spans.set(field.key, field.span);
|
|
25
|
+
}
|
|
26
|
+
return spans;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolve render groups: declarative sections/steps win, then `field.group`,
|
|
30
|
+
* then a single ungrouped list.
|
|
31
|
+
*/
|
|
32
|
+
export function buildFormGroups(input) {
|
|
33
|
+
const { layout, fields, columns } = input;
|
|
34
|
+
if (layout && (layout.kind === 'sections' || layout.kind === 'steps')) {
|
|
35
|
+
return layout.groups.map((group) => ({
|
|
36
|
+
name: group.step !== undefined ? `${group.step}. ${group.title ?? ''}`.trim() : (group.title ?? ''),
|
|
37
|
+
description: group.description,
|
|
38
|
+
columns: group.columns,
|
|
39
|
+
fields: group.fields.flatMap((item) => {
|
|
40
|
+
const field = fields.find((candidate) => candidate.key === item.key);
|
|
41
|
+
return field ? [field] : [];
|
|
42
|
+
}),
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
if (!fields.some((field) => field.group))
|
|
46
|
+
return [];
|
|
47
|
+
const order = [];
|
|
48
|
+
const map = new Map();
|
|
49
|
+
for (const field of fields) {
|
|
50
|
+
const name = field.group ?? '';
|
|
51
|
+
if (!map.has(name)) {
|
|
52
|
+
order.push(name);
|
|
53
|
+
map.set(name, []);
|
|
54
|
+
}
|
|
55
|
+
(map.get(name) ?? []).push(field);
|
|
56
|
+
}
|
|
57
|
+
return order.map((name) => ({
|
|
58
|
+
name,
|
|
59
|
+
description: undefined,
|
|
60
|
+
columns,
|
|
61
|
+
fields: map.get(name) ?? [],
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
export function isFullWidthField(field) {
|
|
65
|
+
return (field.type === 'textarea' ||
|
|
66
|
+
field.type === 'richtext' ||
|
|
67
|
+
field.type === 'json' ||
|
|
68
|
+
field.type === 'array' ||
|
|
69
|
+
field.type === 'images');
|
|
70
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useStorage } from '@svadmin/core';
|
|
3
|
+
import { Upload, X, FileText, AlertCircle, Loader2 } from '@lucide/svelte';
|
|
4
|
+
|
|
5
|
+
export interface UploadFieldProps {
|
|
6
|
+
value?: string | string[] | null;
|
|
7
|
+
multiple?: boolean;
|
|
8
|
+
accept?: string;
|
|
9
|
+
maxSizeBytes?: number;
|
|
10
|
+
path?: string;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
label?: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
onchange?: (value: string | string[] | null) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let {
|
|
18
|
+
value = $bindable(null),
|
|
19
|
+
multiple = false,
|
|
20
|
+
accept = '*/*',
|
|
21
|
+
maxSizeBytes,
|
|
22
|
+
path = 'uploads',
|
|
23
|
+
disabled = false,
|
|
24
|
+
label,
|
|
25
|
+
description,
|
|
26
|
+
onchange,
|
|
27
|
+
}: UploadFieldProps = $props();
|
|
28
|
+
|
|
29
|
+
const inputId = $props.id();
|
|
30
|
+
const storage = useStorage();
|
|
31
|
+
|
|
32
|
+
let isDragging = $state(false);
|
|
33
|
+
let errorMessage = $state<string | null>(null);
|
|
34
|
+
let uploadQueue = $state<Array<{ name: string; size: number; progress: number }>>([]);
|
|
35
|
+
|
|
36
|
+
// Normalize initial value to array for uniform internal rendering
|
|
37
|
+
let fileList = $derived.by<string[]>(() => {
|
|
38
|
+
if (!value) return [];
|
|
39
|
+
if (Array.isArray(value)) return value;
|
|
40
|
+
return [value];
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function updateValue(newList: string[]) {
|
|
44
|
+
const nextVal = multiple ? newList : (newList[0] ?? null);
|
|
45
|
+
value = nextVal;
|
|
46
|
+
onchange?.(nextVal);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function handleFiles(files: FileList | File[]) {
|
|
50
|
+
if (disabled || !files || files.length === 0) return;
|
|
51
|
+
errorMessage = null;
|
|
52
|
+
|
|
53
|
+
const fileArray = Array.from(files);
|
|
54
|
+
|
|
55
|
+
// Validation
|
|
56
|
+
for (const f of fileArray) {
|
|
57
|
+
if (maxSizeBytes && f.size > maxSizeBytes) {
|
|
58
|
+
errorMessage = `File "${f.name}" exceeds the maximum size limit (${(maxSizeBytes / (1024 * 1024)).toFixed(1)}MB).`;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const currentUrls = [...fileList];
|
|
64
|
+
|
|
65
|
+
for (const file of fileArray) {
|
|
66
|
+
const queueItem = { name: file.name, size: file.size, progress: 0 };
|
|
67
|
+
uploadQueue = [...uploadQueue, queueItem];
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const res = await storage.upload(file, {
|
|
71
|
+
path,
|
|
72
|
+
filename: file.name,
|
|
73
|
+
onProgress: (p) => {
|
|
74
|
+
uploadQueue = uploadQueue.map((item) => item === queueItem ? { ...item, progress: p.percentage } : item);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (multiple) {
|
|
79
|
+
currentUrls.push(res.url);
|
|
80
|
+
} else {
|
|
81
|
+
currentUrls.splice(0, currentUrls.length, res.url);
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
errorMessage = err instanceof Error ? err.message : 'Upload failed';
|
|
85
|
+
} finally {
|
|
86
|
+
uploadQueue = uploadQueue.filter((q) => q !== queueItem);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
updateValue(currentUrls);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function handleDrop(e: DragEvent) {
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
isDragging = false;
|
|
96
|
+
if (disabled) return;
|
|
97
|
+
if (e.dataTransfer?.files) {
|
|
98
|
+
handleFiles(e.dataTransfer.files);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function handleInputChange(e: Event) {
|
|
103
|
+
const input = e.target as HTMLInputElement;
|
|
104
|
+
if (input.files) {
|
|
105
|
+
handleFiles(input.files);
|
|
106
|
+
input.value = '';
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function removeItem(urlToRemove: string) {
|
|
111
|
+
if (disabled) return;
|
|
112
|
+
const nextUrls = fileList.filter((u) => u !== urlToRemove);
|
|
113
|
+
updateValue(nextUrls);
|
|
114
|
+
|
|
115
|
+
// Attempt backend delete if path is extractable
|
|
116
|
+
try {
|
|
117
|
+
const urlObj = new URL(urlToRemove, 'https://dummy.local');
|
|
118
|
+
const cleanPath = urlObj.pathname.replace(/^\/+/, '');
|
|
119
|
+
await storage.deleteFile(cleanPath);
|
|
120
|
+
} catch {
|
|
121
|
+
// Best-effort cleanup
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
</script>
|
|
125
|
+
|
|
126
|
+
<div class="svadmin-upload-field space-y-2">
|
|
127
|
+
{#if label}
|
|
128
|
+
<label for={inputId} class="block text-sm font-medium text-foreground">{label}</label>
|
|
129
|
+
{/if}
|
|
130
|
+
|
|
131
|
+
<!-- Drop Zone -->
|
|
132
|
+
<div
|
|
133
|
+
class="relative border-2 border-dashed rounded-lg p-4 text-center transition-colors
|
|
134
|
+
{isDragging ? 'border-primary bg-primary/5' : 'border-border hover:border-primary/50'}
|
|
135
|
+
{disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'}"
|
|
136
|
+
ondragover={(e) => { e.preventDefault(); if (!disabled) isDragging = true; }}
|
|
137
|
+
ondragleave={() => { isDragging = false; }}
|
|
138
|
+
ondrop={handleDrop}
|
|
139
|
+
role="region"
|
|
140
|
+
aria-label="File upload dropzone"
|
|
141
|
+
>
|
|
142
|
+
<input
|
|
143
|
+
id={inputId}
|
|
144
|
+
type="file"
|
|
145
|
+
{accept}
|
|
146
|
+
{multiple}
|
|
147
|
+
{disabled}
|
|
148
|
+
class="absolute inset-0 w-full h-full opacity-0 cursor-pointer disabled:cursor-not-allowed"
|
|
149
|
+
onchange={handleInputChange}
|
|
150
|
+
aria-label="Upload files"
|
|
151
|
+
/>
|
|
152
|
+
|
|
153
|
+
<div class="flex flex-col items-center justify-center gap-1.5 py-2 pointer-events-none">
|
|
154
|
+
{#if storage.isUploading}
|
|
155
|
+
<Loader2 class="h-6 w-6 text-primary animate-spin" />
|
|
156
|
+
<span class="text-xs font-medium text-muted-foreground">Uploading... ({storage.uploadProgress}%)</span>
|
|
157
|
+
{:else}
|
|
158
|
+
<Upload class="h-6 w-6 text-muted-foreground/80" />
|
|
159
|
+
<div class="text-xs text-foreground font-medium">
|
|
160
|
+
<span class="text-primary font-semibold">Click to upload</span> or drag and drop
|
|
161
|
+
</div>
|
|
162
|
+
{#if description}
|
|
163
|
+
<p class="text-[11px] text-muted-foreground">{description}</p>
|
|
164
|
+
{:else}
|
|
165
|
+
<p class="text-[11px] text-muted-foreground">Any file up to {maxSizeBytes ? `${(maxSizeBytes / (1024 * 1024)).toFixed(0)}MB` : '50MB'}</p>
|
|
166
|
+
{/if}
|
|
167
|
+
{/if}
|
|
168
|
+
</div>
|
|
169
|
+
</div>
|
|
170
|
+
|
|
171
|
+
<!-- Error feedback -->
|
|
172
|
+
{#if errorMessage}
|
|
173
|
+
<div class="flex items-center gap-1.5 text-xs text-destructive">
|
|
174
|
+
<AlertCircle class="h-3.5 w-3.5 shrink-0" />
|
|
175
|
+
<span>{errorMessage}</span>
|
|
176
|
+
</div>
|
|
177
|
+
{/if}
|
|
178
|
+
|
|
179
|
+
<!-- Upload Queue Progress -->
|
|
180
|
+
{#if uploadQueue.length > 0}
|
|
181
|
+
<div class="space-y-1 pt-1">
|
|
182
|
+
{#each uploadQueue as item (item.name)}
|
|
183
|
+
<div class="flex items-center justify-between text-xs p-1.5 bg-muted/40 rounded border border-border">
|
|
184
|
+
<span class="truncate max-w-[200px] font-medium">{item.name}</span>
|
|
185
|
+
<span class="text-[11px] text-muted-foreground">{item.progress}%</span>
|
|
186
|
+
</div>
|
|
187
|
+
{/each}
|
|
188
|
+
</div>
|
|
189
|
+
{/if}
|
|
190
|
+
|
|
191
|
+
<!-- Uploaded Files List -->
|
|
192
|
+
{#if fileList.length > 0}
|
|
193
|
+
<div class="grid grid-cols-1 sm:grid-cols-2 gap-2 pt-1">
|
|
194
|
+
{#each fileList as fileUrl (fileUrl)}
|
|
195
|
+
{@const fileName = fileUrl.split('/').pop()?.split('?')[0] ?? fileUrl}
|
|
196
|
+
<div class="flex items-center justify-between gap-2 p-2 rounded-md border border-border bg-card text-xs">
|
|
197
|
+
<div class="flex items-center gap-2 min-w-0">
|
|
198
|
+
<FileText class="h-4 w-4 text-primary shrink-0" />
|
|
199
|
+
<a
|
|
200
|
+
href={fileUrl}
|
|
201
|
+
target="_blank"
|
|
202
|
+
rel="noreferrer"
|
|
203
|
+
class="truncate font-medium text-foreground hover:underline"
|
|
204
|
+
title={fileName}
|
|
205
|
+
>
|
|
206
|
+
{fileName}
|
|
207
|
+
</a>
|
|
208
|
+
</div>
|
|
209
|
+
<button
|
|
210
|
+
type="button"
|
|
211
|
+
class="p-1 text-muted-foreground hover:text-destructive transition-colors rounded"
|
|
212
|
+
onclick={() => removeItem(fileUrl)}
|
|
213
|
+
{disabled}
|
|
214
|
+
aria-label={`Remove ${fileName}`}
|
|
215
|
+
>
|
|
216
|
+
<X class="h-3.5 w-3.5" />
|
|
217
|
+
</button>
|
|
218
|
+
</div>
|
|
219
|
+
{/each}
|
|
220
|
+
</div>
|
|
221
|
+
{/if}
|
|
222
|
+
</div>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface UploadFieldProps {
|
|
2
|
+
value?: string | string[] | null;
|
|
3
|
+
multiple?: boolean;
|
|
4
|
+
accept?: string;
|
|
5
|
+
maxSizeBytes?: number;
|
|
6
|
+
path?: string;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
label?: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
onchange?: (value: string | string[] | null) => void;
|
|
11
|
+
}
|
|
12
|
+
declare const UploadField: import("svelte").Component<UploadFieldProps, {}, "value">;
|
|
13
|
+
type UploadField = ReturnType<typeof UploadField>;
|
|
14
|
+
export default UploadField;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { IconProps } from './types.js';
|
|
3
|
+
import { cn } from '../../utils.js';
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
size = 16,
|
|
7
|
+
color = 'currentColor',
|
|
8
|
+
strokeWidth = 2,
|
|
9
|
+
spin = false,
|
|
10
|
+
class: className = '',
|
|
11
|
+
style = '',
|
|
12
|
+
...restProps
|
|
13
|
+
}: IconProps = $props();
|
|
14
|
+
|
|
15
|
+
const dimension = $derived(typeof size === 'number' ? `${size}px` : size);
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<svg
|
|
19
|
+
data-svadmin-component="IconApprove"
|
|
20
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
21
|
+
viewBox="0 0 24 24"
|
|
22
|
+
width={dimension}
|
|
23
|
+
height={dimension}
|
|
24
|
+
fill="none"
|
|
25
|
+
stroke={color}
|
|
26
|
+
stroke-width={strokeWidth}
|
|
27
|
+
stroke-linecap="round"
|
|
28
|
+
stroke-linejoin="round"
|
|
29
|
+
class={cn('svadmin-icon svadmin-icon-approve inline-block shrink-0', spin && 'animate-spin', className)}
|
|
30
|
+
style={style}
|
|
31
|
+
{...restProps}
|
|
32
|
+
>
|
|
33
|
+
<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
|
|
34
|
+
<polyline points="22 4 12 14.01 9 11.01" />
|
|
35
|
+
</svg>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { IconProps } from './types.js';
|
|
3
|
+
import { cn } from '../../utils.js';
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
size = 16,
|
|
7
|
+
color = 'currentColor',
|
|
8
|
+
strokeWidth = 2,
|
|
9
|
+
spin = false,
|
|
10
|
+
class: className = '',
|
|
11
|
+
style = '',
|
|
12
|
+
...restProps
|
|
13
|
+
}: IconProps = $props();
|
|
14
|
+
|
|
15
|
+
const dimension = $derived(typeof size === 'number' ? `${size}px` : size);
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<svg
|
|
19
|
+
data-svadmin-component="IconAudit"
|
|
20
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
21
|
+
viewBox="0 0 24 24"
|
|
22
|
+
width={dimension}
|
|
23
|
+
height={dimension}
|
|
24
|
+
fill="none"
|
|
25
|
+
stroke={color}
|
|
26
|
+
stroke-width={strokeWidth}
|
|
27
|
+
stroke-linecap="round"
|
|
28
|
+
stroke-linejoin="round"
|
|
29
|
+
class={cn('svadmin-icon svadmin-icon-audit inline-block shrink-0', spin && 'animate-spin', className)}
|
|
30
|
+
style={style}
|
|
31
|
+
{...restProps}
|
|
32
|
+
>
|
|
33
|
+
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
|
34
|
+
<polyline points="14 2 14 8 20 8" />
|
|
35
|
+
<line x1="16" y1="13" x2="8" y2="13" />
|
|
36
|
+
<line x1="16" y1="17" x2="8" y2="17" />
|
|
37
|
+
<polyline points="10 9 9 9 8 9" />
|
|
38
|
+
</svg>
|