panelui-native 0.110.0 → 0.112.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/lib/module/components/avatar-cropper/avatar-cropper-math.js +68 -28
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js.map +1 -1
- package/lib/module/components/avatar-cropper/index.js +190 -24
- package/lib/module/components/avatar-cropper/index.js.map +1 -1
- package/lib/module/components/file-upload/file-upload-core.js +317 -0
- package/lib/module/components/file-upload/file-upload-core.js.map +1 -0
- package/lib/module/components/file-upload/index.js +1214 -0
- package/lib/module/components/file-upload/index.js.map +1 -0
- package/lib/module/icons/index.js +10 -0
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts +32 -10
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/index.d.ts +30 -3
- package/lib/typescript/src/components/avatar-cropper/index.d.ts.map +1 -1
- package/lib/typescript/src/components/file-upload/file-upload-core.d.ts +136 -0
- package/lib/typescript/src/components/file-upload/file-upload-core.d.ts.map +1 -0
- package/lib/typescript/src/components/file-upload/index.d.ts +265 -0
- package/lib/typescript/src/components/file-upload/index.d.ts.map +1 -0
- package/lib/typescript/src/icons/index.d.ts +2 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +5 -1
- package/src/components/avatar-cropper/avatar-cropper-math.ts +78 -42
- package/src/components/avatar-cropper/index.tsx +251 -23
- package/src/components/file-upload/file-upload-core.ts +359 -0
- package/src/components/file-upload/index.tsx +1435 -0
- package/src/icons/index.tsx +4 -0
- package/src/index.ts +24 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of FileUpload that are arithmetic rather than drawing: what a file
|
|
3
|
+
* is, whether it is allowed, how its size reads, and which queued upload runs
|
|
4
|
+
* next. Kept apart from the component so they can be tested without a renderer.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Where a file comes from. */
|
|
8
|
+
export type FileUploadSource = 'camera' | 'library' | 'files';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Where a file is in its life. `idle` has been picked and nothing is sending it;
|
|
12
|
+
* `queued` is waiting for a free upload slot; `canceled` was stopped by the user
|
|
13
|
+
* and can be retried.
|
|
14
|
+
*/
|
|
15
|
+
export type FileUploadStatus =
|
|
16
|
+
| 'idle'
|
|
17
|
+
| 'queued'
|
|
18
|
+
| 'uploading'
|
|
19
|
+
| 'processing'
|
|
20
|
+
| 'done'
|
|
21
|
+
| 'error'
|
|
22
|
+
| 'canceled';
|
|
23
|
+
|
|
24
|
+
/** What a file is, for choosing a thumbnail or an icon. */
|
|
25
|
+
export type FileUploadKind = 'image' | 'video' | 'document';
|
|
26
|
+
|
|
27
|
+
/** One picked file and the state of its upload. */
|
|
28
|
+
export interface FileUploadFile {
|
|
29
|
+
/** Stable key for the file while it is in the list. */
|
|
30
|
+
id: string;
|
|
31
|
+
/** Local URI the picker returned, or the resized copy of it. */
|
|
32
|
+
uri: string;
|
|
33
|
+
/** File name with its extension. */
|
|
34
|
+
name: string;
|
|
35
|
+
/** Size in bytes, when the picker reports it. */
|
|
36
|
+
size?: number;
|
|
37
|
+
/** MIME type, when the picker reports it. */
|
|
38
|
+
mimeType?: string;
|
|
39
|
+
/** Pixel width, for images and videos. */
|
|
40
|
+
width?: number;
|
|
41
|
+
/** Pixel height, for images and videos. */
|
|
42
|
+
height?: number;
|
|
43
|
+
kind: FileUploadKind;
|
|
44
|
+
status: FileUploadStatus;
|
|
45
|
+
/** Upload progress 0–1, while uploading. */
|
|
46
|
+
progress?: number;
|
|
47
|
+
/** Why the last upload failed. */
|
|
48
|
+
error?: string;
|
|
49
|
+
/** Whatever the upload handler resolved with — a remote URL, a record id. */
|
|
50
|
+
response?: unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A file that was picked and turned away, and why. */
|
|
54
|
+
export interface FileUploadRejection {
|
|
55
|
+
file: Pick<FileUploadFile, 'name' | 'size' | 'mimeType' | 'uri'>;
|
|
56
|
+
reason: 'type' | 'size' | 'count' | 'custom';
|
|
57
|
+
/** A sentence that can be shown as it is. */
|
|
58
|
+
message: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The rules a picked file has to pass. */
|
|
62
|
+
export interface FileUploadRules {
|
|
63
|
+
accept?: readonly string[];
|
|
64
|
+
maxSize?: number;
|
|
65
|
+
minSize?: number;
|
|
66
|
+
maxFiles?: number;
|
|
67
|
+
validate?: (file: FileUploadFile) => string | null | undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Statuses that hold an upload slot. */
|
|
71
|
+
export function isActive(status: FileUploadStatus): boolean {
|
|
72
|
+
return status === 'uploading' || status === 'processing';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The extension of a file name, lower-cased and without the dot. */
|
|
76
|
+
export function extensionOf(name: string): string {
|
|
77
|
+
const dot = name.lastIndexOf('.');
|
|
78
|
+
return dot > 0 && dot < name.length - 1 ? name.slice(dot + 1).toLowerCase() : '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** MIME types for the extensions people most often name in `accept`. */
|
|
82
|
+
const EXTENSION_TYPES: Record<string, string> = {
|
|
83
|
+
pdf: 'application/pdf',
|
|
84
|
+
doc: 'application/msword',
|
|
85
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
86
|
+
xls: 'application/vnd.ms-excel',
|
|
87
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
88
|
+
ppt: 'application/vnd.ms-powerpoint',
|
|
89
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
90
|
+
csv: 'text/csv',
|
|
91
|
+
txt: 'text/plain',
|
|
92
|
+
json: 'application/json',
|
|
93
|
+
zip: 'application/zip',
|
|
94
|
+
png: 'image/png',
|
|
95
|
+
jpg: 'image/jpeg',
|
|
96
|
+
jpeg: 'image/jpeg',
|
|
97
|
+
heic: 'image/heic',
|
|
98
|
+
webp: 'image/webp',
|
|
99
|
+
gif: 'image/gif',
|
|
100
|
+
mp4: 'video/mp4',
|
|
101
|
+
mov: 'video/quicktime',
|
|
102
|
+
mp3: 'audio/mpeg',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** The MIME type a file reports, or the one its extension implies. */
|
|
106
|
+
export function mimeOf(file: { name: string; mimeType?: string }): string | undefined {
|
|
107
|
+
return file.mimeType ?? EXTENSION_TYPES[extensionOf(file.name)];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Image, video or anything else. */
|
|
111
|
+
export function kindOf(file: { name: string; mimeType?: string }): FileUploadKind {
|
|
112
|
+
const mime = mimeOf(file) ?? '';
|
|
113
|
+
if (mime.startsWith('image/')) return 'image';
|
|
114
|
+
if (mime.startsWith('video/')) return 'video';
|
|
115
|
+
return 'document';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether a file matches one of the `accept` patterns: an exact MIME type
|
|
120
|
+
* (`application/pdf`), a family (`image/*`) or an extension (`.pdf`). No
|
|
121
|
+
* patterns accepts everything.
|
|
122
|
+
*/
|
|
123
|
+
export function matchesAccept(
|
|
124
|
+
file: { name: string; mimeType?: string },
|
|
125
|
+
accept: readonly string[] | undefined
|
|
126
|
+
): boolean {
|
|
127
|
+
if (!accept || accept.length === 0) return true;
|
|
128
|
+
const mime = (mimeOf(file) ?? '').toLowerCase();
|
|
129
|
+
const ext = extensionOf(file.name);
|
|
130
|
+
return accept.some((raw) => {
|
|
131
|
+
const pattern = raw.trim().toLowerCase();
|
|
132
|
+
if (pattern === '*' || pattern === '*/*') return true;
|
|
133
|
+
if (pattern.startsWith('.')) return pattern.slice(1) === ext;
|
|
134
|
+
if (pattern.endsWith('/*')) return mime.startsWith(pattern.slice(0, -1));
|
|
135
|
+
return pattern === mime;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The `type` list for the system document picker. Extensions are turned into
|
|
141
|
+
* MIME types, since that is all the picker understands; an extension it has no
|
|
142
|
+
* type for widens the list to everything, and the validator catches the rest.
|
|
143
|
+
*/
|
|
144
|
+
export function pickerTypes(accept: readonly string[] | undefined): string[] {
|
|
145
|
+
if (!accept || accept.length === 0) return ['*/*'];
|
|
146
|
+
const types = new Set<string>();
|
|
147
|
+
for (const raw of accept) {
|
|
148
|
+
const pattern = raw.trim().toLowerCase();
|
|
149
|
+
if (pattern.startsWith('.')) {
|
|
150
|
+
const type = EXTENSION_TYPES[pattern.slice(1)];
|
|
151
|
+
if (!type) return ['*/*'];
|
|
152
|
+
types.add(type);
|
|
153
|
+
} else {
|
|
154
|
+
types.add(pattern);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return [...types];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Which of images and videos the photo library should offer, from `accept`.
|
|
162
|
+
* Empty when `accept` allows neither, which means the library has nothing to
|
|
163
|
+
* give and the source is left out of the chooser.
|
|
164
|
+
*/
|
|
165
|
+
export function libraryMediaTypes(accept: readonly string[] | undefined): Array<'images' | 'videos'> {
|
|
166
|
+
if (!accept || accept.length === 0) return ['images', 'videos'];
|
|
167
|
+
const out: Array<'images' | 'videos'> = [];
|
|
168
|
+
if (matchesAccept({ name: 'a.jpg', mimeType: 'image/jpeg' }, accept)) out.push('images');
|
|
169
|
+
if (matchesAccept({ name: 'a.mp4', mimeType: 'video/mp4' }, accept)) out.push('videos');
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A byte count as people read it: `820 KB`, `2.4 MB`, `10 MB`. One decimal
|
|
177
|
+
* below ten units, none above, never four digits, and binary multiples, so
|
|
178
|
+
* `10 * 1024 * 1024` reads as the round `10 MB` it was written as.
|
|
179
|
+
*/
|
|
180
|
+
export function formatFileSize(bytes: number): string {
|
|
181
|
+
if (!Number.isFinite(bytes) || bytes < 0) return '';
|
|
182
|
+
if (bytes < 1000) return `${Math.round(bytes)} B`;
|
|
183
|
+
let value = bytes;
|
|
184
|
+
let unit = 0;
|
|
185
|
+
// Four digits never show: 1003 KB reads as 1 MB.
|
|
186
|
+
while (value >= 1000 && unit < UNITS.length - 1) {
|
|
187
|
+
value /= 1024;
|
|
188
|
+
unit += 1;
|
|
189
|
+
}
|
|
190
|
+
const rounded = value < 10 ? Math.round(value * 10) / 10 : Math.round(value);
|
|
191
|
+
return `${rounded} ${UNITS[unit]}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Short names for the families and types a hint is written from. */
|
|
195
|
+
const PATTERN_NAMES: Record<string, string> = {
|
|
196
|
+
'image/*': 'images',
|
|
197
|
+
'video/*': 'videos',
|
|
198
|
+
'audio/*': 'audio',
|
|
199
|
+
'text/*': 'text files',
|
|
200
|
+
'application/pdf': 'PDF',
|
|
201
|
+
'image/jpeg': 'JPG',
|
|
202
|
+
'image/png': 'PNG',
|
|
203
|
+
'image/heic': 'HEIC',
|
|
204
|
+
'image/webp': 'WebP',
|
|
205
|
+
'image/gif': 'GIF',
|
|
206
|
+
'video/mp4': 'MP4',
|
|
207
|
+
'text/csv': 'CSV',
|
|
208
|
+
'text/plain': 'TXT',
|
|
209
|
+
'application/zip': 'ZIP',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** "PDF, JPG or PNG", from the accept list. Empty when anything goes. */
|
|
213
|
+
export function describeAccept(accept: readonly string[] | undefined): string {
|
|
214
|
+
if (!accept || accept.length === 0) return '';
|
|
215
|
+
const names: string[] = [];
|
|
216
|
+
for (const raw of accept) {
|
|
217
|
+
const pattern = raw.trim().toLowerCase();
|
|
218
|
+
const name = pattern.startsWith('.')
|
|
219
|
+
? pattern.slice(1).toUpperCase()
|
|
220
|
+
: (PATTERN_NAMES[pattern] ?? extensionFromMime(pattern));
|
|
221
|
+
if (name && !names.includes(name)) names.push(name);
|
|
222
|
+
}
|
|
223
|
+
if (names.length <= 1) return names[0] ?? '';
|
|
224
|
+
return `${names.slice(0, -1).join(', ')} or ${names[names.length - 1]}`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function extensionFromMime(mime: string): string {
|
|
228
|
+
const entry = Object.entries(EXTENSION_TYPES).find(([, type]) => type === mime);
|
|
229
|
+
return entry ? entry[0].toUpperCase() : '';
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** "PDF or images · up to 10 MB" — what a dropzone says under its title. */
|
|
233
|
+
export function describeRules({ accept, maxSize, maxFiles }: FileUploadRules): string {
|
|
234
|
+
const parts: string[] = [];
|
|
235
|
+
const types = describeAccept(accept);
|
|
236
|
+
if (types) parts.push(types.charAt(0).toUpperCase() + types.slice(1));
|
|
237
|
+
if (maxSize) parts.push(`up to ${formatFileSize(maxSize)}`);
|
|
238
|
+
if (maxFiles && maxFiles > 1) parts.push(`${maxFiles} files max`);
|
|
239
|
+
return parts.join(' · ');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Split picked files into those that pass the rules and those that do not.
|
|
244
|
+
*
|
|
245
|
+
* Checks run in the order a person would explain them — type, then size, then
|
|
246
|
+
* the app's own rule — and the count comes last, so a file that would have
|
|
247
|
+
* been turned away anyway does not use up one of the remaining slots.
|
|
248
|
+
*/
|
|
249
|
+
export function partitionFiles(
|
|
250
|
+
candidates: readonly FileUploadFile[],
|
|
251
|
+
current: number,
|
|
252
|
+
rules: FileUploadRules
|
|
253
|
+
): { accepted: FileUploadFile[]; rejected: FileUploadRejection[] } {
|
|
254
|
+
const accepted: FileUploadFile[] = [];
|
|
255
|
+
const rejected: FileUploadRejection[] = [];
|
|
256
|
+
const typeNames = describeAccept(rules.accept);
|
|
257
|
+
|
|
258
|
+
for (const file of candidates) {
|
|
259
|
+
const summary = { name: file.name, size: file.size, mimeType: file.mimeType, uri: file.uri };
|
|
260
|
+
if (!matchesAccept(file, rules.accept)) {
|
|
261
|
+
rejected.push({
|
|
262
|
+
file: summary,
|
|
263
|
+
reason: 'type',
|
|
264
|
+
message: typeNames ? `${file.name} isn't ${articleFor(typeNames)}.` : `${file.name} can't be added.`,
|
|
265
|
+
});
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (rules.maxSize !== undefined && file.size !== undefined && file.size > rules.maxSize) {
|
|
269
|
+
rejected.push({
|
|
270
|
+
file: summary,
|
|
271
|
+
reason: 'size',
|
|
272
|
+
message: `${file.name} is ${formatFileSize(file.size)}. The limit is ${formatFileSize(rules.maxSize)}.`,
|
|
273
|
+
});
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (rules.minSize !== undefined && file.size !== undefined && file.size < rules.minSize) {
|
|
277
|
+
rejected.push({
|
|
278
|
+
file: summary,
|
|
279
|
+
reason: 'size',
|
|
280
|
+
message: `${file.name} is smaller than ${formatFileSize(rules.minSize)}.`,
|
|
281
|
+
});
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const custom = rules.validate?.(file);
|
|
285
|
+
if (custom) {
|
|
286
|
+
rejected.push({ file: summary, reason: 'custom', message: custom });
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (rules.maxFiles !== undefined && current + accepted.length >= rules.maxFiles) {
|
|
290
|
+
rejected.push({
|
|
291
|
+
file: summary,
|
|
292
|
+
reason: 'count',
|
|
293
|
+
message: `Only ${rules.maxFiles} ${rules.maxFiles === 1 ? 'file' : 'files'} can be added.`,
|
|
294
|
+
});
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
accepted.push(file);
|
|
298
|
+
}
|
|
299
|
+
return { accepted, rejected };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** "a PDF or image" reads better than "PDF or images" inside a sentence. */
|
|
303
|
+
function articleFor(names: string): string {
|
|
304
|
+
const singular = names.replace(/\bimages\b/, 'image').replace(/\bvideos\b/, 'video')
|
|
305
|
+
.replace(/\btext files\b/, 'text file');
|
|
306
|
+
const first = singular.trim().charAt(0).toLowerCase();
|
|
307
|
+
// Letters whose spoken name starts with a vowel sound: "an MP4", "an image".
|
|
308
|
+
const vowel = 'aefhilmnorsx'.includes(first) && /^[A-Z]{2,}/.test(singular)
|
|
309
|
+
? true
|
|
310
|
+
: 'aeiou'.includes(first);
|
|
311
|
+
return `${vowel ? 'an' : 'a'} ${singular}`;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The queued files that may start now, in list order, given how many slots
|
|
316
|
+
* `concurrency` allows and how many are already busy.
|
|
317
|
+
*/
|
|
318
|
+
export function nextToStart(files: readonly FileUploadFile[], concurrency: number): string[] {
|
|
319
|
+
const limit = Math.max(1, Math.floor(concurrency));
|
|
320
|
+
const busy = files.filter((file) => isActive(file.status)).length;
|
|
321
|
+
const free = limit - busy;
|
|
322
|
+
if (free <= 0) return [];
|
|
323
|
+
return files.filter((file) => file.status === 'queued').slice(0, free).map((file) => file.id);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Scale a width and height down to fit inside a box, never up. */
|
|
327
|
+
export function fitWithin(
|
|
328
|
+
size: { width: number; height: number },
|
|
329
|
+
box: { maxWidth?: number; maxHeight?: number }
|
|
330
|
+
): { width: number; height: number } | null {
|
|
331
|
+
const scale = Math.min(
|
|
332
|
+
box.maxWidth ? box.maxWidth / size.width : Infinity,
|
|
333
|
+
box.maxHeight ? box.maxHeight / size.height : Infinity,
|
|
334
|
+
1
|
|
335
|
+
);
|
|
336
|
+
if (scale >= 1) return null;
|
|
337
|
+
return { width: Math.round(size.width * scale), height: Math.round(size.height * scale) };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Shorten a file name from the middle, keeping its start and its extension:
|
|
342
|
+
* `service-hist…2025.pdf`. A name cut at the end loses the part that says what
|
|
343
|
+
* kind of file it is.
|
|
344
|
+
*/
|
|
345
|
+
export function truncateMiddle(name: string, max: number): string {
|
|
346
|
+
if (name.length <= max || max < 8) return name;
|
|
347
|
+
const ext = extensionOf(name);
|
|
348
|
+
const tail = Math.min(name.length - 1, Math.max(ext.length + 5, Math.floor(max / 3)));
|
|
349
|
+
const head = max - tail - 1;
|
|
350
|
+
return `${name.slice(0, head)}…${name.slice(name.length - tail)}`;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
let counter = 0;
|
|
354
|
+
|
|
355
|
+
/** A key unique within the app's lifetime. */
|
|
356
|
+
export function createFileId(): string {
|
|
357
|
+
counter += 1;
|
|
358
|
+
return `file-${Date.now().toString(36)}-${counter.toString(36)}`;
|
|
359
|
+
}
|