@molecule/app-file-card-svelte 0.0.0-stage → 1.1.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/LICENSE +202 -0
- package/README.md +282 -2
- package/dist/FileCard.svelte +306 -0
- package/dist/FileCard.svelte.d.ts +5 -0
- package/dist/FileCard.svelte.d.ts.map +1 -0
- package/dist/FileIcon.svelte +60 -0
- package/dist/FileIcon.svelte.d.ts +5 -0
- package/dist/FileIcon.svelte.d.ts.map +1 -0
- package/dist/components.d.ts +9 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +8 -0
- package/dist/format.d.ts +80 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +87 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/style.d.ts +28 -0
- package/dist/style.d.ts.map +1 -0
- package/dist/style.js +115 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/package.json +73 -4
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
FileCard: the Svelte counterpart of `@molecule/app-file-card-react`'s
|
|
3
|
+
`FileCard` — file representation card (icon/thumbnail + name + size +
|
|
4
|
+
relative modified date + optional actions slot) in `'grid'` (square tile)
|
|
5
|
+
or `'row'` (horizontal line) layout. Same props, same DOM for the same
|
|
6
|
+
input.
|
|
7
|
+
|
|
8
|
+
Whitespace is deliberate: React's JSX strips whitespace-only text between
|
|
9
|
+
elements, so every element opens glued to the previous tag's close and no
|
|
10
|
+
stray whitespace text nodes render.
|
|
11
|
+
-->
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { useTranslation } from '@molecule/app-svelte'
|
|
14
|
+
import { getClassMap } from '@molecule/app-ui'
|
|
15
|
+
|
|
16
|
+
import FileIcon from './FileIcon.svelte'
|
|
17
|
+
import { relativeBucket, type SizeParts, sizeParts } from './format.js'
|
|
18
|
+
import { reactStyle } from './style.js'
|
|
19
|
+
import type { FileCardProps, FileKind } from './types.js'
|
|
20
|
+
|
|
21
|
+
/** The translation function shape we use here — matches `useTranslation().t`. */
|
|
22
|
+
type TFn = (
|
|
23
|
+
key: string,
|
|
24
|
+
values?: Record<string, string | number | boolean | Date>,
|
|
25
|
+
options?: { defaultValue?: string },
|
|
26
|
+
) => string
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Resolve the relative-time text for a file via `t()`. Keeps the template
|
|
30
|
+
* clean and centralizes the bucket → key mapping.
|
|
31
|
+
*
|
|
32
|
+
* @param at - Modified-at timestamp.
|
|
33
|
+
* @param t - The `useTranslation()` `t` function.
|
|
34
|
+
* @param now - Reference "now".
|
|
35
|
+
* @returns The localized relative-time string.
|
|
36
|
+
*/
|
|
37
|
+
function relativeText(at: string | number | Date, t: TFn, now: Date | undefined): string {
|
|
38
|
+
const bucket = relativeBucket(at, now)
|
|
39
|
+
switch (bucket.kind) {
|
|
40
|
+
case 'just-now':
|
|
41
|
+
return t('file-card.modified.just-now', {}, { defaultValue: 'just now' })
|
|
42
|
+
case 'minutes':
|
|
43
|
+
return t(
|
|
44
|
+
bucket.n === 1 ? 'file-card.modified.minute-one' : 'file-card.modified.minute-other',
|
|
45
|
+
{ count: bucket.n },
|
|
46
|
+
{ defaultValue: bucket.n === 1 ? '1 min ago' : `${bucket.n} min ago` },
|
|
47
|
+
)
|
|
48
|
+
case 'hours':
|
|
49
|
+
return t(
|
|
50
|
+
bucket.n === 1 ? 'file-card.modified.hour-one' : 'file-card.modified.hour-other',
|
|
51
|
+
{ count: bucket.n },
|
|
52
|
+
{ defaultValue: bucket.n === 1 ? '1 hr ago' : `${bucket.n} hr ago` },
|
|
53
|
+
)
|
|
54
|
+
case 'days':
|
|
55
|
+
return t(
|
|
56
|
+
bucket.n === 1 ? 'file-card.modified.day-one' : 'file-card.modified.day-other',
|
|
57
|
+
{ count: bucket.n },
|
|
58
|
+
{ defaultValue: bucket.n === 1 ? 'yesterday' : `${bucket.n} days ago` },
|
|
59
|
+
)
|
|
60
|
+
case 'weeks':
|
|
61
|
+
return t(
|
|
62
|
+
bucket.n === 1 ? 'file-card.modified.week-one' : 'file-card.modified.week-other',
|
|
63
|
+
{ count: bucket.n },
|
|
64
|
+
{ defaultValue: bucket.n === 1 ? '1 wk ago' : `${bucket.n} wk ago` },
|
|
65
|
+
)
|
|
66
|
+
case 'months':
|
|
67
|
+
return t(
|
|
68
|
+
bucket.n === 1 ? 'file-card.modified.month-one' : 'file-card.modified.month-other',
|
|
69
|
+
{ count: bucket.n },
|
|
70
|
+
{ defaultValue: bucket.n === 1 ? '1 mo ago' : `${bucket.n} mo ago` },
|
|
71
|
+
)
|
|
72
|
+
case 'absolute':
|
|
73
|
+
return bucket.iso
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Resolve the human-readable kind label via `t()`. One literal key per
|
|
79
|
+
* `FileKind` so the i18n gate can verify every key exists in the locale
|
|
80
|
+
* bond (a runtime-built `` `file-card.kind.${kind}` `` cannot be checked).
|
|
81
|
+
*
|
|
82
|
+
* @param kind - File kind.
|
|
83
|
+
* @param t - The `useTranslation()` `t` function.
|
|
84
|
+
* @returns The localized kind label.
|
|
85
|
+
*/
|
|
86
|
+
function kindText(kind: FileKind, t: TFn): string {
|
|
87
|
+
switch (kind) {
|
|
88
|
+
case 'image':
|
|
89
|
+
return t('file-card.kind.image', {}, { defaultValue: 'Image file' })
|
|
90
|
+
case 'video':
|
|
91
|
+
return t('file-card.kind.video', {}, { defaultValue: 'Video file' })
|
|
92
|
+
case 'audio':
|
|
93
|
+
return t('file-card.kind.audio', {}, { defaultValue: 'Audio file' })
|
|
94
|
+
case 'document':
|
|
95
|
+
return t('file-card.kind.document', {}, { defaultValue: 'Document' })
|
|
96
|
+
case 'archive':
|
|
97
|
+
return t('file-card.kind.archive', {}, { defaultValue: 'Archive' })
|
|
98
|
+
case 'code':
|
|
99
|
+
return t('file-card.kind.code', {}, { defaultValue: 'Code file' })
|
|
100
|
+
case 'folder':
|
|
101
|
+
return t('file-card.kind.folder', {}, { defaultValue: 'Folder' })
|
|
102
|
+
case 'other':
|
|
103
|
+
return t('file-card.kind.other', {}, { defaultValue: 'File' })
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Resolve the size's unit word via `t()`, with the number {@link sizeParts}
|
|
109
|
+
* already displayed: one literal key per `SizeUnit` — the same pattern as
|
|
110
|
+
* {@link kindText} — so the i18n gate can verify every key exists in the
|
|
111
|
+
* locale bond, and the `{{n}}` placeholder lets a translation move the number
|
|
112
|
+
* or rename the token (French octets, Russian КБ).
|
|
113
|
+
*
|
|
114
|
+
* @param size - The size's displayed number and unit (`sizeParts(file.size)`).
|
|
115
|
+
* @param t - The `useTranslation()` `t` function.
|
|
116
|
+
* @returns The localized size text.
|
|
117
|
+
*/
|
|
118
|
+
function sizeUnitText(size: SizeParts, t: TFn): string {
|
|
119
|
+
switch (size.unit) {
|
|
120
|
+
case 'B':
|
|
121
|
+
return t('file-card.size.b', { n: size.n }, { defaultValue: '{{n}} B' })
|
|
122
|
+
case 'KB':
|
|
123
|
+
return t('file-card.size.kb', { n: size.n }, { defaultValue: '{{n}} KB' })
|
|
124
|
+
case 'MB':
|
|
125
|
+
return t('file-card.size.mb', { n: size.n }, { defaultValue: '{{n}} MB' })
|
|
126
|
+
case 'GB':
|
|
127
|
+
return t('file-card.size.gb', { n: size.n }, { defaultValue: '{{n}} GB' })
|
|
128
|
+
case 'TB':
|
|
129
|
+
return t('file-card.size.tb', { n: size.n }, { defaultValue: '{{n}} TB' })
|
|
130
|
+
case 'PB':
|
|
131
|
+
return t('file-card.size.pb', { n: size.n }, { defaultValue: '{{n}} PB' })
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let {
|
|
136
|
+
file,
|
|
137
|
+
selected,
|
|
138
|
+
onClick,
|
|
139
|
+
onContextMenu,
|
|
140
|
+
layout = 'grid',
|
|
141
|
+
actions,
|
|
142
|
+
now,
|
|
143
|
+
className,
|
|
144
|
+
dataMolId,
|
|
145
|
+
}: FileCardProps = $props()
|
|
146
|
+
|
|
147
|
+
const { t } = useTranslation()
|
|
148
|
+
|
|
149
|
+
const cm = $derived(getClassMap())
|
|
150
|
+
const kindLabel = $derived(kindText(file.kind, t))
|
|
151
|
+
const size = $derived(file.kind === 'folder' ? undefined : sizeParts(file.size))
|
|
152
|
+
const sizeText = $derived(size ? sizeUnitText(size, t) : '')
|
|
153
|
+
const sizeLabel = $derived(
|
|
154
|
+
sizeText ? t('file-card.aria.size', { size: sizeText }, { defaultValue: 'Size {{size}}' }) : '',
|
|
155
|
+
)
|
|
156
|
+
const modifiedText = $derived(file.modifiedAt ? relativeText(file.modifiedAt, t, now) : '')
|
|
157
|
+
const modifiedLabel = $derived(
|
|
158
|
+
modifiedText
|
|
159
|
+
? t('file-card.aria.modified', { when: modifiedText }, { defaultValue: 'Modified {{when}}' })
|
|
160
|
+
: '',
|
|
161
|
+
)
|
|
162
|
+
const ariaLabel = $derived(
|
|
163
|
+
t(
|
|
164
|
+
'file-card.aria.root',
|
|
165
|
+
{ name: file.name, kind: kindLabel },
|
|
166
|
+
{
|
|
167
|
+
defaultValue: '{{name}}, {{kind}}',
|
|
168
|
+
},
|
|
169
|
+
),
|
|
170
|
+
)
|
|
171
|
+
const rootMolId = $derived(dataMolId ?? `file-card-${file.id}`)
|
|
172
|
+
const isRow = $derived(layout === 'row')
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Forward a body activation (a click on the select button, or Enter / Space
|
|
176
|
+
* on it) to the consumer.
|
|
177
|
+
*
|
|
178
|
+
* @param event - DOM mouse event.
|
|
179
|
+
*/
|
|
180
|
+
function handleClick(event: MouseEvent): void {
|
|
181
|
+
onClick?.(file, event)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Forward right-click to the consumer.
|
|
186
|
+
*
|
|
187
|
+
* @param event - DOM mouse event.
|
|
188
|
+
*/
|
|
189
|
+
function handleContextMenu(event: MouseEvent): void {
|
|
190
|
+
if (onContextMenu) onContextMenu(file, event)
|
|
191
|
+
}
|
|
192
|
+
</script>
|
|
193
|
+
|
|
194
|
+
{#if isRow}<div
|
|
195
|
+
role="group"
|
|
196
|
+
aria-label={ariaLabel}
|
|
197
|
+
data-mol-id={rootMolId}
|
|
198
|
+
data-selected={selected ? 'true' : undefined}
|
|
199
|
+
class={cm.cn(
|
|
200
|
+
cm.flex({ align: 'center', gap: 'md' }),
|
|
201
|
+
cm.sp('p', 2),
|
|
202
|
+
onClick ? cm.position('relative') : undefined,
|
|
203
|
+
className,
|
|
204
|
+
)}
|
|
205
|
+
oncontextmenu={handleContextMenu}
|
|
206
|
+
>
|
|
207
|
+
<div class={cm.cn(cm.shrink0)} data-mol-id="file-card-icon">
|
|
208
|
+
{#if file.thumbnail}<img
|
|
209
|
+
src={file.thumbnail}
|
|
210
|
+
alt=""
|
|
211
|
+
use:reactStyle={{ width: 40, height: 40, objectFit: 'cover', borderRadius: 4 }}
|
|
212
|
+
/>{:else}<FileIcon kind={file.kind} size={28} />{/if}
|
|
213
|
+
</div>
|
|
214
|
+
<div class={cm.cn(cm.stack(0))} use:reactStyle={{ flex: 1, minWidth: 0 }}>
|
|
215
|
+
<div
|
|
216
|
+
class={cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'))}
|
|
217
|
+
data-mol-id="file-card-name"
|
|
218
|
+
use:reactStyle={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}
|
|
219
|
+
>
|
|
220
|
+
{file.name}
|
|
221
|
+
</div>
|
|
222
|
+
{#if sizeText || modifiedText}<div
|
|
223
|
+
class={cm.cn(cm.flex({ align: 'center', gap: 'sm' }), cm.textSize('xs'))}
|
|
224
|
+
data-mol-id="file-card-meta"
|
|
225
|
+
>
|
|
226
|
+
{#if sizeText}<span aria-label={sizeLabel} data-mol-id="file-card-size">{sizeText}</span
|
|
227
|
+
>{/if}{#if sizeText && modifiedText}<span aria-hidden="true">·</span
|
|
228
|
+
>{/if}{#if modifiedText}<span
|
|
229
|
+
aria-label={modifiedLabel}
|
|
230
|
+
data-mol-id="file-card-modified">{modifiedText}</span
|
|
231
|
+
>{/if}
|
|
232
|
+
</div>{/if}
|
|
233
|
+
</div>
|
|
234
|
+
{#if onClick}<button
|
|
235
|
+
type="button"
|
|
236
|
+
aria-label={ariaLabel}
|
|
237
|
+
aria-pressed={selected ? 'true' : 'false'}
|
|
238
|
+
data-mol-id="file-card-select"
|
|
239
|
+
onclick={handleClick}
|
|
240
|
+
class={cm.cn(cm.position('absolute'), cm.inset0, cm.cursorPointer)}
|
|
241
|
+
use:reactStyle={{ background: 'transparent', border: 'none', padding: 0 }}
|
|
242
|
+
></button>{/if}{#if actions}<div
|
|
243
|
+
class={cm.cn(cm.shrink0, cm.position('relative'))}
|
|
244
|
+
data-mol-id="file-card-actions"
|
|
245
|
+
>
|
|
246
|
+
{@render actions()}
|
|
247
|
+
</div>{/if}
|
|
248
|
+
</div>{:else}<div
|
|
249
|
+
role="group"
|
|
250
|
+
aria-label={ariaLabel}
|
|
251
|
+
data-mol-id={rootMolId}
|
|
252
|
+
data-selected={selected ? 'true' : undefined}
|
|
253
|
+
class={cm.cn(
|
|
254
|
+
cm.stack(1),
|
|
255
|
+
cm.sp('p', 3),
|
|
256
|
+
onClick ? cm.position('relative') : undefined,
|
|
257
|
+
className,
|
|
258
|
+
)}
|
|
259
|
+
oncontextmenu={handleContextMenu}
|
|
260
|
+
>
|
|
261
|
+
<div
|
|
262
|
+
class={cm.cn(cm.flex({ align: 'center', justify: 'center' }))}
|
|
263
|
+
data-mol-id="file-card-icon"
|
|
264
|
+
use:reactStyle={{ aspectRatio: '1 / 1', width: '100%' }}
|
|
265
|
+
>
|
|
266
|
+
{#if file.thumbnail}<img
|
|
267
|
+
src={file.thumbnail}
|
|
268
|
+
alt=""
|
|
269
|
+
use:reactStyle={{ width: '100%', height: '100%', objectFit: 'cover', borderRadius: 4 }}
|
|
270
|
+
/>{:else}<FileIcon kind={file.kind} size={48} />{/if}
|
|
271
|
+
</div>
|
|
272
|
+
<div
|
|
273
|
+
class={cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'))}
|
|
274
|
+
data-mol-id="file-card-name"
|
|
275
|
+
use:reactStyle={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}
|
|
276
|
+
>
|
|
277
|
+
{file.name}
|
|
278
|
+
</div>
|
|
279
|
+
{#if sizeText || modifiedText}<div
|
|
280
|
+
class={cm.cn(cm.flex({ align: 'center', gap: 'sm' }), cm.textSize('xs'))}
|
|
281
|
+
data-mol-id="file-card-meta"
|
|
282
|
+
>
|
|
283
|
+
{#if sizeText}<span aria-label={sizeLabel} data-mol-id="file-card-size">{sizeText}</span
|
|
284
|
+
>{/if}{#if sizeText && modifiedText}<span aria-hidden="true">·</span
|
|
285
|
+
>{/if}{#if modifiedText}<span aria-label={modifiedLabel} data-mol-id="file-card-modified"
|
|
286
|
+
>{modifiedText}</span
|
|
287
|
+
>{/if}
|
|
288
|
+
</div>{/if}
|
|
289
|
+
{#if onClick}<button
|
|
290
|
+
type="button"
|
|
291
|
+
aria-label={ariaLabel}
|
|
292
|
+
aria-pressed={selected ? 'true' : 'false'}
|
|
293
|
+
data-mol-id="file-card-select"
|
|
294
|
+
onclick={handleClick}
|
|
295
|
+
class={cm.cn(cm.position('absolute'), cm.inset0, cm.cursorPointer)}
|
|
296
|
+
use:reactStyle={{ background: 'transparent', border: 'none', padding: 0 }}
|
|
297
|
+
></button>{/if}{#if actions}<div
|
|
298
|
+
class={cm.cn(
|
|
299
|
+
cm.flex({ align: 'center', justify: 'end', gap: 'sm' }),
|
|
300
|
+
cm.position('relative'),
|
|
301
|
+
)}
|
|
302
|
+
data-mol-id="file-card-actions"
|
|
303
|
+
>
|
|
304
|
+
{@render actions()}
|
|
305
|
+
</div>{/if}
|
|
306
|
+
</div>{/if}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileCard.svelte.d.ts","sourceRoot":"","sources":["../src/FileCard.svelte"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,aAAa,EAAY,MAAM,YAAY,CAAC;AAsO1D,QAAA,MAAM,QAAQ,mDAAwC,CAAC;AACvD,KAAK,QAAQ,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC;AAC5C,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
FileIcon: the Svelte counterpart of `@molecule/app-file-card-react`'s
|
|
3
|
+
`FileIcon` — a stroke-currentColor SVG glyph keyed by `FileKind`. Same
|
|
4
|
+
props, same DOM for the same input.
|
|
5
|
+
-->
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
import type { FileIconProps } from './types.js'
|
|
8
|
+
|
|
9
|
+
let { kind, size = 24, ariaLabel }: FileIconProps = $props()
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<svg
|
|
13
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
14
|
+
width={size}
|
|
15
|
+
height={size}
|
|
16
|
+
viewBox="0 0 24 24"
|
|
17
|
+
fill="none"
|
|
18
|
+
stroke="currentColor"
|
|
19
|
+
stroke-width="1.75"
|
|
20
|
+
stroke-linecap="round"
|
|
21
|
+
stroke-linejoin="round"
|
|
22
|
+
role={ariaLabel ? 'img' : undefined}
|
|
23
|
+
aria-label={ariaLabel}
|
|
24
|
+
aria-hidden={ariaLabel ? undefined : true}
|
|
25
|
+
data-mol-id="file-icon-{kind}"
|
|
26
|
+
>
|
|
27
|
+
{#if kind === 'image'}<rect x="3" y="5" width="18" height="14" rx="2" /><circle
|
|
28
|
+
cx="8.5"
|
|
29
|
+
cy="10.5"
|
|
30
|
+
r="1.5"
|
|
31
|
+
/><path d="m21 15-5-5L5 21" />{:else if kind === 'video'}<rect
|
|
32
|
+
x="3"
|
|
33
|
+
y="5"
|
|
34
|
+
width="18"
|
|
35
|
+
height="14"
|
|
36
|
+
rx="2"
|
|
37
|
+
/><path d="m10 9 5 3-5 3z" fill="currentColor" />{:else if kind === 'audio'}<path
|
|
38
|
+
d="M9 18V5l12-2v13"
|
|
39
|
+
/><circle cx="6" cy="18" r="3" /><circle
|
|
40
|
+
cx="18"
|
|
41
|
+
cy="16"
|
|
42
|
+
r="3"
|
|
43
|
+
/>{:else if kind === 'document'}<path
|
|
44
|
+
d="M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"
|
|
45
|
+
/><path d="M14 3v6h6" />{:else if kind === 'archive'}<rect
|
|
46
|
+
x="3"
|
|
47
|
+
y="3"
|
|
48
|
+
width="18"
|
|
49
|
+
height="6"
|
|
50
|
+
rx="1"
|
|
51
|
+
/><path d="M3 9v10a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V9" /><path
|
|
52
|
+
d="M10 13h4"
|
|
53
|
+
/>{:else if kind === 'code'}<path d="m9 8-5 4 5 4" /><path d="m15 8 5 4-5 4" /><path
|
|
54
|
+
d="m13 5-2 14"
|
|
55
|
+
/>{:else if kind === 'folder'}<path
|
|
56
|
+
d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"
|
|
57
|
+
/>{:else}<path d="M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z" /><path
|
|
58
|
+
d="M14 3v6h6"
|
|
59
|
+
/><path d="M9 14h6" /><path d="M9 17h4" />{/if}
|
|
60
|
+
</svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileIcon.svelte.d.ts","sourceRoot":"","sources":["../src/FileIcon.svelte"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAehD,QAAA,MAAM,QAAQ,mDAAwC,CAAC;AACvD,KAAK,QAAQ,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC;AAC5C,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's components: one Svelte 5 component per React component in
|
|
3
|
+
* `@molecule/app-file-card-react`, under the same React name.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export { default as FileCard } from './FileCard.svelte';
|
|
8
|
+
export { default as FileIcon } from './FileIcon.svelte';
|
|
9
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's components: one Svelte 5 component per React component in
|
|
3
|
+
* `@molecule/app-file-card-react`, under the same React name.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export { default as FileCard } from './FileCard.svelte';
|
|
8
|
+
export { default as FileIcon } from './FileIcon.svelte';
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format helpers for the file card. Pure functions — UI-agnostic so they can
|
|
3
|
+
* be unit-tested directly.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/** The unit token a formatted byte count ends in (`B` plus the binary steps). */
|
|
8
|
+
export type SizeUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB';
|
|
9
|
+
/** A formatted size's displayed number and its unit token, split for `t()`. */
|
|
10
|
+
export interface SizeParts {
|
|
11
|
+
/** The displayed number, rounded the way {@link bytes} displays it. */
|
|
12
|
+
n: string;
|
|
13
|
+
/** The unit token the number counts. */
|
|
14
|
+
unit: SizeUnit;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Split a non-negative byte count into its displayed number and unit token
|
|
18
|
+
* (e.g. `420` + `B`, `1.4` + `KB`, `471` + `MB`) — the two halves
|
|
19
|
+
* {@link bytes} joins, exposed so the card can translate the unit word via
|
|
20
|
+
* `t()` (one literal key per {@link SizeUnit}, as the kind labels do).
|
|
21
|
+
*
|
|
22
|
+
* Uses a 1024 base (binary) and 1 fractional digit for KB+. Returns the
|
|
23
|
+
* `0 B` parts for non-finite or negative inputs so callers do not need to
|
|
24
|
+
* pre-validate.
|
|
25
|
+
*
|
|
26
|
+
* @param value - Byte count (>= 0).
|
|
27
|
+
* @returns The displayed number and unit token.
|
|
28
|
+
*/
|
|
29
|
+
export declare function sizeParts(value: number): SizeParts;
|
|
30
|
+
/**
|
|
31
|
+
* Format a non-negative byte count as a short human-readable string
|
|
32
|
+
* (e.g. `420 B`, `1.4 KB`, `2.3 MB`, `4.7 GB`, `8.1 TB`).
|
|
33
|
+
*
|
|
34
|
+
* The unlocalized join of {@link sizeParts}'s two halves, for callers that
|
|
35
|
+
* want the raw SI shorthand. The card itself renders the unit through `t()`
|
|
36
|
+
* (`file-card.size.<unit>` keys in the locale bond) so a locale switch
|
|
37
|
+
* translates it; the unit token is universal SI shorthand — a language that
|
|
38
|
+
* names bytes differently (French octets, Russian КБ) changes only the token,
|
|
39
|
+
* never the magnitude.
|
|
40
|
+
*
|
|
41
|
+
* @param value - Byte count (>= 0).
|
|
42
|
+
* @returns Formatted size string.
|
|
43
|
+
*/
|
|
44
|
+
export declare function bytes(value: number): string;
|
|
45
|
+
/**
|
|
46
|
+
* A relative-time bucket used to pick a translation key. Higher granularity
|
|
47
|
+
* buckets first; falls through to absolute date when older than ~1 year.
|
|
48
|
+
*/
|
|
49
|
+
export type RelativeBucket = {
|
|
50
|
+
kind: 'just-now';
|
|
51
|
+
} | {
|
|
52
|
+
kind: 'minutes';
|
|
53
|
+
n: number;
|
|
54
|
+
} | {
|
|
55
|
+
kind: 'hours';
|
|
56
|
+
n: number;
|
|
57
|
+
} | {
|
|
58
|
+
kind: 'days';
|
|
59
|
+
n: number;
|
|
60
|
+
} | {
|
|
61
|
+
kind: 'weeks';
|
|
62
|
+
n: number;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'months';
|
|
65
|
+
n: number;
|
|
66
|
+
} | {
|
|
67
|
+
kind: 'absolute';
|
|
68
|
+
iso: string;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Bucket a `Date` (or ISO string / ms timestamp) against `now` into a
|
|
72
|
+
* relative-time bucket. The bucket is purely structural — translation keys
|
|
73
|
+
* and pluralization are applied by the consumer.
|
|
74
|
+
*
|
|
75
|
+
* @param at - Modified-at timestamp (ISO string, ms epoch, or `Date`).
|
|
76
|
+
* @param now - Reference "now" (defaults to `new Date()`). Injected for tests.
|
|
77
|
+
* @returns A `RelativeBucket` describing the offset from `now`.
|
|
78
|
+
*/
|
|
79
|
+
export declare function relativeBucket(at: string | number | Date, now?: Date): RelativeBucket;
|
|
80
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,iFAAiF;AACjF,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAE7D,+EAA+E;AAC/E,MAAM,WAAW,SAAS;IACxB,uEAAuE;IACvE,CAAC,EAAE,MAAM,CAAA;IACT,wCAAwC;IACxC,IAAI,EAAE,QAAQ,CAAA;CACf;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAalD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAG3C;AAED;;;GAGG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,GACpB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAC5B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAC3B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAC5B;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAC7B;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAA;AAErC;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,EAAE,GAAG,GAAE,IAAiB,GAAG,cAAc,CAiBjG"}
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format helpers for the file card. Pure functions — UI-agnostic so they can
|
|
3
|
+
* be unit-tested directly.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Split a non-negative byte count into its displayed number and unit token
|
|
9
|
+
* (e.g. `420` + `B`, `1.4` + `KB`, `471` + `MB`) — the two halves
|
|
10
|
+
* {@link bytes} joins, exposed so the card can translate the unit word via
|
|
11
|
+
* `t()` (one literal key per {@link SizeUnit}, as the kind labels do).
|
|
12
|
+
*
|
|
13
|
+
* Uses a 1024 base (binary) and 1 fractional digit for KB+. Returns the
|
|
14
|
+
* `0 B` parts for non-finite or negative inputs so callers do not need to
|
|
15
|
+
* pre-validate.
|
|
16
|
+
*
|
|
17
|
+
* @param value - Byte count (>= 0).
|
|
18
|
+
* @returns The displayed number and unit token.
|
|
19
|
+
*/
|
|
20
|
+
export function sizeParts(value) {
|
|
21
|
+
if (!Number.isFinite(value) || value < 0)
|
|
22
|
+
return { n: '0', unit: 'B' };
|
|
23
|
+
if (value < 1024)
|
|
24
|
+
return { n: String(Math.round(value)), unit: 'B' };
|
|
25
|
+
const units = ['KB', 'MB', 'GB', 'TB', 'PB'];
|
|
26
|
+
let n = value / 1024;
|
|
27
|
+
let unitIndex = 0;
|
|
28
|
+
while (n >= 1024 && unitIndex < units.length - 1) {
|
|
29
|
+
n /= 1024;
|
|
30
|
+
unitIndex += 1;
|
|
31
|
+
}
|
|
32
|
+
// 1 decimal place when < 100, else round
|
|
33
|
+
const display = n >= 100 ? Math.round(n).toString() : n.toFixed(1).replace(/\.0$/, '');
|
|
34
|
+
return { n: display, unit: units[unitIndex] };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Format a non-negative byte count as a short human-readable string
|
|
38
|
+
* (e.g. `420 B`, `1.4 KB`, `2.3 MB`, `4.7 GB`, `8.1 TB`).
|
|
39
|
+
*
|
|
40
|
+
* The unlocalized join of {@link sizeParts}'s two halves, for callers that
|
|
41
|
+
* want the raw SI shorthand. The card itself renders the unit through `t()`
|
|
42
|
+
* (`file-card.size.<unit>` keys in the locale bond) so a locale switch
|
|
43
|
+
* translates it; the unit token is universal SI shorthand — a language that
|
|
44
|
+
* names bytes differently (French octets, Russian КБ) changes only the token,
|
|
45
|
+
* never the magnitude.
|
|
46
|
+
*
|
|
47
|
+
* @param value - Byte count (>= 0).
|
|
48
|
+
* @returns Formatted size string.
|
|
49
|
+
*/
|
|
50
|
+
export function bytes(value) {
|
|
51
|
+
const { n, unit } = sizeParts(value);
|
|
52
|
+
return `${n} ${unit}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Bucket a `Date` (or ISO string / ms timestamp) against `now` into a
|
|
56
|
+
* relative-time bucket. The bucket is purely structural — translation keys
|
|
57
|
+
* and pluralization are applied by the consumer.
|
|
58
|
+
*
|
|
59
|
+
* @param at - Modified-at timestamp (ISO string, ms epoch, or `Date`).
|
|
60
|
+
* @param now - Reference "now" (defaults to `new Date()`). Injected for tests.
|
|
61
|
+
* @returns A `RelativeBucket` describing the offset from `now`.
|
|
62
|
+
*/
|
|
63
|
+
export function relativeBucket(at, now = new Date()) {
|
|
64
|
+
const then = at instanceof Date ? at : new Date(at);
|
|
65
|
+
const ms = now.getTime() - then.getTime();
|
|
66
|
+
if (!Number.isFinite(ms))
|
|
67
|
+
return { kind: 'absolute', iso: String(at) };
|
|
68
|
+
if (ms < 60_000)
|
|
69
|
+
return { kind: 'just-now' };
|
|
70
|
+
const minute = 60_000;
|
|
71
|
+
const hour = 60 * minute;
|
|
72
|
+
const day = 24 * hour;
|
|
73
|
+
const week = 7 * day;
|
|
74
|
+
const month = 30 * day;
|
|
75
|
+
const year = 365 * day;
|
|
76
|
+
if (ms < hour)
|
|
77
|
+
return { kind: 'minutes', n: Math.floor(ms / minute) };
|
|
78
|
+
if (ms < day)
|
|
79
|
+
return { kind: 'hours', n: Math.floor(ms / hour) };
|
|
80
|
+
if (ms < week)
|
|
81
|
+
return { kind: 'days', n: Math.floor(ms / day) };
|
|
82
|
+
if (ms < month)
|
|
83
|
+
return { kind: 'weeks', n: Math.floor(ms / week) };
|
|
84
|
+
if (ms < year)
|
|
85
|
+
return { kind: 'months', n: Math.floor(ms / month) };
|
|
86
|
+
return { kind: 'absolute', iso: then.toISOString().slice(0, 10) };
|
|
87
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte file card primitives.
|
|
3
|
+
*
|
|
4
|
+
* Exports:
|
|
5
|
+
* - `<FileCard>` — file representation card (icon/thumbnail + name + size + modified date + optional actions).
|
|
6
|
+
* Two layouts: `'grid'` (square tile, default) and `'row'` (horizontal list line).
|
|
7
|
+
* - `<FileIcon>` — stroke-currentColor SVG glyph keyed by `FileKind`.
|
|
8
|
+
* - `bytes(value)` — pure helper that formats a byte count as `420 B` / `1.4 KB` / `2.3 MB` / etc.
|
|
9
|
+
* - `sizeParts(value)` — the same split as `{ n, unit }`, for rendering the unit through `t()`.
|
|
10
|
+
* - `relativeBucket(at, now?)` — pure helper that buckets a timestamp into `just-now` / `minutes` /
|
|
11
|
+
* `hours` / `days` / `weeks` / `months` / `absolute`.
|
|
12
|
+
* - `FileSummary` / `FileKind` / `SizeParts` types — stable shape for the consumer's domain model.
|
|
13
|
+
*
|
|
14
|
+
* Used by cloud-file-manager (grid + list views), social-media + email-client
|
|
15
|
+
* (attachment previews), and document-collaboration (sidebars).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```svelte
|
|
19
|
+
* <script lang="ts">
|
|
20
|
+
* import { FileCard, type FileSummary } from '@molecule/app-file-card-svelte'
|
|
21
|
+
*
|
|
22
|
+
* const file: FileSummary = {
|
|
23
|
+
* id: 'f-1',
|
|
24
|
+
* name: 'Q3-report.pdf',
|
|
25
|
+
* size: 482_137,
|
|
26
|
+
* kind: 'document',
|
|
27
|
+
* modifiedAt: '2026-04-30T18:23:00Z',
|
|
28
|
+
* }
|
|
29
|
+
* </script>
|
|
30
|
+
*
|
|
31
|
+
* <FileCard {file} layout="grid" onClick={(f) => openFile(f.id)}>
|
|
32
|
+
* {#snippet actions()}
|
|
33
|
+
* <KebabMenu fileId={file.id} />
|
|
34
|
+
* {/snippet}
|
|
35
|
+
* </FileCard>
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* All styling routes through `getClassMap()` from `@molecule/app-ui` —
|
|
40
|
+
* swap the ClassMap bond to restyle without touching this package.
|
|
41
|
+
*
|
|
42
|
+
* All user-visible text routes through `t()` (via `@molecule/app-svelte`'s
|
|
43
|
+
* `useTranslation`) and translates via the companion
|
|
44
|
+
* `@molecule/app-locales-file-card` locale bond. English fallbacks are
|
|
45
|
+
* inlined so the card works without the locale bond present.
|
|
46
|
+
*
|
|
47
|
+
* @module
|
|
48
|
+
*/
|
|
49
|
+
export * from './components.js';
|
|
50
|
+
export * from './format.js';
|
|
51
|
+
export * from './types.js';
|
|
52
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
|