dsh-transfer-dock 0.1.2 → 0.1.3
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/README.md +531 -512
- package/README.zh.md +531 -512
- package/client.js +348 -119
- package/docs/architecture.svg +1 -1
- package/docs/dock-preview.png +0 -0
- package/index.js +1 -1
- package/lib/detect.js +4 -1
- package/package.json +75 -74
- package/test/smoke.mjs +230 -155
package/client.js
CHANGED
|
@@ -16,6 +16,7 @@ window.__ModuleLoader__.load({
|
|
|
16
16
|
const STATE_PATH = '/plugin/dsh-transfer-dock/state'
|
|
17
17
|
const POLL_MS = 600
|
|
18
18
|
const FADE_MS = 2600
|
|
19
|
+
const BALL_FADE_MS = 8000
|
|
19
20
|
const MIN_LIVE_MS = 500
|
|
20
21
|
const MAX_TEXT = 16 * 1024
|
|
21
22
|
|
|
@@ -31,22 +32,86 @@ window.__ModuleLoader__.load({
|
|
|
31
32
|
})()
|
|
32
33
|
|
|
33
34
|
const EMPTY = { version: 1, now: 0, tasks: [] }
|
|
35
|
+
const GRAPHEMES = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
|
|
36
|
+
const MESSAGES = {
|
|
37
|
+
en: {
|
|
38
|
+
collapse: 'Collapse download to a floating button',
|
|
39
|
+
expand: 'Expand download',
|
|
40
|
+
unknown: 'Progress unknown',
|
|
41
|
+
downloading: 'Downloading…',
|
|
42
|
+
fetching: 'Fetching repository…',
|
|
43
|
+
stopping: 'Stopping…',
|
|
44
|
+
completed: 'Completed',
|
|
45
|
+
failed: 'Download failed',
|
|
46
|
+
killed: 'Cancelled',
|
|
47
|
+
written: 'Written:',
|
|
48
|
+
remaining: 'Left:',
|
|
49
|
+
},
|
|
50
|
+
zh: {
|
|
51
|
+
collapse: '收起为悬浮球',
|
|
52
|
+
expand: '展开下载详情',
|
|
53
|
+
unknown: '进度未知',
|
|
54
|
+
downloading: '正在下载…',
|
|
55
|
+
fetching: '正在获取仓库…',
|
|
56
|
+
stopping: '正在停止…',
|
|
57
|
+
completed: '已完成',
|
|
58
|
+
failed: '下载失败',
|
|
59
|
+
killed: '已取消',
|
|
60
|
+
written: '已写入',
|
|
61
|
+
remaining: '剩',
|
|
62
|
+
},
|
|
63
|
+
}
|
|
34
64
|
|
|
35
|
-
/**
|
|
65
|
+
/** 状态色与排版都走主题变量。 */
|
|
36
66
|
const S = {
|
|
37
67
|
wrap: {
|
|
38
68
|
position: 'fixed',
|
|
39
|
-
right:
|
|
40
|
-
bottom:
|
|
69
|
+
right: 12,
|
|
70
|
+
bottom: 12,
|
|
41
71
|
zIndex: 2147483000,
|
|
42
|
-
display: '
|
|
43
|
-
|
|
72
|
+
display: 'grid',
|
|
73
|
+
gridTemplateColumns: '1fr',
|
|
44
74
|
gap: 8,
|
|
45
|
-
width:
|
|
46
|
-
maxWidth: 'calc(100vw -
|
|
75
|
+
width: 378,
|
|
76
|
+
maxWidth: 'calc(100vw - 24px)',
|
|
77
|
+
maxHeight: 'calc(100dvh - 24px)',
|
|
78
|
+
padding: 6,
|
|
79
|
+
boxSizing: 'border-box',
|
|
80
|
+
overflowY: 'auto',
|
|
81
|
+
pointerEvents: 'none',
|
|
82
|
+
},
|
|
83
|
+
ball: {
|
|
84
|
+
pointerEvents: 'auto',
|
|
85
|
+
position: 'relative',
|
|
86
|
+
justifySelf: 'end',
|
|
87
|
+
alignSelf: 'center',
|
|
88
|
+
width: 48,
|
|
89
|
+
height: 48,
|
|
90
|
+
display: 'grid',
|
|
91
|
+
placeItems: 'center',
|
|
92
|
+
padding: 0,
|
|
93
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
94
|
+
borderRadius: '50%',
|
|
95
|
+
background: 'var(--dsw-alias-button-floating-fill)',
|
|
96
|
+
boxShadow: '0 3px 12px color-mix(in srgb, var(--dsw-alias-label-primary) 15%, transparent)',
|
|
97
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
98
|
+
font: 'inherit',
|
|
99
|
+
fontSize: 17,
|
|
100
|
+
lineHeight: 1,
|
|
101
|
+
fontWeight: 650,
|
|
102
|
+
cursor: 'pointer',
|
|
103
|
+
},
|
|
104
|
+
ring: {
|
|
105
|
+
position: 'absolute',
|
|
106
|
+
inset: 0,
|
|
107
|
+
width: '100%',
|
|
108
|
+
height: '100%',
|
|
47
109
|
pointerEvents: 'none',
|
|
48
110
|
},
|
|
49
111
|
card: {
|
|
112
|
+
width: '100%',
|
|
113
|
+
minWidth: 0,
|
|
114
|
+
boxSizing: 'border-box',
|
|
50
115
|
pointerEvents: 'auto',
|
|
51
116
|
borderRadius: 14,
|
|
52
117
|
border: '1px solid var(--dsw-alias-border-l2)',
|
|
@@ -54,7 +119,6 @@ window.__ModuleLoader__.load({
|
|
|
54
119
|
boxShadow: '0 18px 40px -18px rgba(0, 0, 0, .55), 0 2px 8px -2px rgba(0, 0, 0, .25)',
|
|
55
120
|
backdropFilter: 'blur(12px) saturate(140%)',
|
|
56
121
|
padding: '11px 12px 12px',
|
|
57
|
-
animation: 'dsh-transfer-dock-in 180ms ease-out both',
|
|
58
122
|
fontFamily: 'inherit',
|
|
59
123
|
},
|
|
60
124
|
head: { display: 'flex', alignItems: 'center', gap: 9, marginBottom: 9 },
|
|
@@ -68,11 +132,8 @@ window.__ModuleLoader__.load({
|
|
|
68
132
|
justifyContent: 'center',
|
|
69
133
|
fontSize: 11,
|
|
70
134
|
fontWeight: 700,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
background: 'linear-gradient(150deg, var(--dsw-alias-brand-primary), #7c5cff)',
|
|
74
|
-
boxShadow: 'inset 0 1px 0 rgba(255,255,255,.28)',
|
|
75
|
-
textTransform: 'uppercase',
|
|
135
|
+
color: 'var(--dsw-alias-brand-text)',
|
|
136
|
+
background: 'color-mix(in srgb, var(--dsw-alias-brand-primary) 14%, var(--dsw-alias-bg-layer-1))',
|
|
76
137
|
},
|
|
77
138
|
titles: { display: 'flex', flexDirection: 'column', gap: 1, minWidth: 0, flex: 1 },
|
|
78
139
|
name: {
|
|
@@ -115,8 +176,7 @@ window.__ModuleLoader__.load({
|
|
|
115
176
|
fill: {
|
|
116
177
|
height: '100%',
|
|
117
178
|
borderRadius: 999,
|
|
118
|
-
background:
|
|
119
|
-
'linear-gradient(90deg, var(--dsw-alias-brand-primary), color-mix(in srgb, var(--dsw-alias-brand-primary) 55%, #8b5cf6))',
|
|
179
|
+
background: 'var(--dsw-alias-brand-primary)',
|
|
120
180
|
transition: 'width 240ms ease-out',
|
|
121
181
|
},
|
|
122
182
|
fillDone: { background: 'var(--dsw-alias-state-success-primary)' },
|
|
@@ -149,8 +209,15 @@ window.__ModuleLoader__.load({
|
|
|
149
209
|
wordBreak: 'break-word',
|
|
150
210
|
},
|
|
151
211
|
keyframes: [
|
|
152
|
-
'@keyframes dsh-transfer-dock-in{from{opacity:0;transform:translateY(
|
|
212
|
+
'@keyframes dsh-transfer-dock-in{from{opacity:0;transform:translateY(4px) scale(.96)}to{opacity:1;transform:none}}',
|
|
153
213
|
'@keyframes dsh-transfer-dock-stripes{from{background-position:0 0}to{background-position:24px 0}}',
|
|
214
|
+
'@keyframes dsh-transfer-dock-spin{to{transform:rotate(360deg)}}',
|
|
215
|
+
'.dsh-transfer-dock-card,.dsh-transfer-dock-ball{animation:dsh-transfer-dock-in 150ms ease-out both}',
|
|
216
|
+
'.dsh-transfer-dock-spin{animation:dsh-transfer-dock-spin 1.3s linear infinite}',
|
|
217
|
+
'.dsh-transfer-dock-arc{transition:stroke-dashoffset 240ms ease-out}',
|
|
218
|
+
'.dsh-transfer-dock-ball:hover{background:var(--dsw-alias-button-floating-hover)!important}',
|
|
219
|
+
'.dsh-transfer-dock button:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:2px}',
|
|
220
|
+
'@media (prefers-reduced-motion: reduce){.dsh-transfer-dock-card,.dsh-transfer-dock-ball,.dsh-transfer-dock-spin,.dsh-transfer-dock-stripes{animation:none!important}.dsh-transfer-dock-arc{transition:none}}',
|
|
154
221
|
].join(''),
|
|
155
222
|
}
|
|
156
223
|
|
|
@@ -187,6 +254,24 @@ window.__ModuleLoader__.load({
|
|
|
187
254
|
total: bytes(match[4], match[5]),
|
|
188
255
|
}),
|
|
189
256
|
},
|
|
257
|
+
{
|
|
258
|
+
// tqdm(huggingface-cli / hf / modelscope / pip TTY):
|
|
259
|
+
// 45%|████▌ | 12.3M/27.3M [00:10<00:12, 1.20MB/s]
|
|
260
|
+
re: /(\d+)%\|[^|]*\|\s*([\d.]+)\s*([KMGT]?)\s*\/\s*([\d.]+)\s*([KMGT]?)\s*B?\s*\[([^\]]+)\]/i,
|
|
261
|
+
pick: match => {
|
|
262
|
+
const sizeUnit = unit => (unit === undefined || unit === '' ? undefined : `${unit}B`)
|
|
263
|
+
const bracket = match[6]
|
|
264
|
+
const speedMatch = bracket.match(/([\d.]+)\s*([KMGT]?B)\/s/i)
|
|
265
|
+
const etaMatch = bracket.match(/<(\d+:\d{2}(?::\d{2})?)/)
|
|
266
|
+
return {
|
|
267
|
+
percent: Number(match[1]),
|
|
268
|
+
downloaded: bytes(match[2], sizeUnit(match[3])),
|
|
269
|
+
total: bytes(match[4], sizeUnit(match[5])),
|
|
270
|
+
speed: speedMatch === null ? undefined : bytes(speedMatch[1], speedMatch[2]),
|
|
271
|
+
eta: etaMatch === null ? undefined : etaMatch[1],
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
},
|
|
190
275
|
{
|
|
191
276
|
// pip 收尾行: 12.9/12.9 MB 491.0 kB/s eta 0:00:00
|
|
192
277
|
re: /([\d.]+)\s*\/\s*([\d.]+)\s*([KMGT]?i?B)\s+([\d.]+)\s*([KMGT]?i?B)\/s(?:\s+eta\s+(\S+))?/i,
|
|
@@ -197,6 +282,22 @@ window.__ModuleLoader__.load({
|
|
|
197
282
|
eta: match[6],
|
|
198
283
|
}),
|
|
199
284
|
},
|
|
285
|
+
{
|
|
286
|
+
// yt-dlp: [download] 45.0% of 12.34MiB at 1.20MiB/s ETA 00:10
|
|
287
|
+
// 只有总量和百分比:已下载量按比例估算,避免把总大小当成已下载。
|
|
288
|
+
re: /\[download\]\s+([\d.]+)%\s+of\s+([\d.]+)\s*([KMGT]i?B)(?:\s+at\s+([\d.]+)\s*([KMGT]i?B)\/s)?(?:\s+ETA\s+(\S+))?/i,
|
|
289
|
+
pick: match => {
|
|
290
|
+
const percent = Number(match[1])
|
|
291
|
+
const total = bytes(match[2], match[3])
|
|
292
|
+
return {
|
|
293
|
+
percent,
|
|
294
|
+
total,
|
|
295
|
+
downloaded: total === undefined ? undefined : (total * percent) / 100,
|
|
296
|
+
speed: match[4] === undefined ? undefined : bytes(match[4], match[5]),
|
|
297
|
+
eta: match[6],
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
},
|
|
200
301
|
{
|
|
201
302
|
// wget(非 TTY):0K .......... .......... 45% 1.2M 3s
|
|
202
303
|
re: /(\d+)%\s+([\d.]+)\s*([KMGT])\s+([\d.]+s)$/i,
|
|
@@ -284,6 +385,9 @@ window.__ModuleLoader__.load({
|
|
|
284
385
|
if (clock !== null) {
|
|
285
386
|
return ((Number(clock[1]) * 60 + Number(clock[2])) * 60 + Number(clock[3])) * 1000
|
|
286
387
|
}
|
|
388
|
+
// yt-dlp / tqdm 的 mm:ss,例如 00:10。
|
|
389
|
+
const short = value.match(/^(\d+):(\d{2})$/)
|
|
390
|
+
if (short !== null) return (Number(short[1]) * 60 + Number(short[2])) * 1000
|
|
287
391
|
const simple = value.match(/^(\d+(?:\.\d+)?)\s*(s|m|h)?$/i)
|
|
288
392
|
if (simple === null) return undefined
|
|
289
393
|
const amount = Number(simple[1])
|
|
@@ -365,7 +469,7 @@ window.__ModuleLoader__.load({
|
|
|
365
469
|
? task.total
|
|
366
470
|
: undefined
|
|
367
471
|
let percent = Number.isFinite(parsed.percent) ? parsed.percent : undefined
|
|
368
|
-
if (percent === undefined && total !== undefined
|
|
472
|
+
if (percent === undefined && total !== undefined) {
|
|
369
473
|
percent = Math.min(100, (downloaded / total) * 100)
|
|
370
474
|
}
|
|
371
475
|
const speed = Number.isFinite(parsed.speed) && parsed.speed > 0 ? parsed.speed : task.speed
|
|
@@ -395,14 +499,32 @@ window.__ModuleLoader__.load({
|
|
|
395
499
|
* @param view - 展示模型。
|
|
396
500
|
* @returns 短文本。
|
|
397
501
|
*/
|
|
398
|
-
function subtitleOf(task, view) {
|
|
399
|
-
if (
|
|
400
|
-
if (task.status === 'killed') return '已取消'
|
|
401
|
-
if (task.status === 'completed') return '已完成'
|
|
402
|
-
if (task.status === 'stopping') return '正在停止…'
|
|
502
|
+
function subtitleOf(task, view, t) {
|
|
503
|
+
if (['failed', 'killed', 'completed', 'stopping'].includes(task.status)) return t(task.status)
|
|
403
504
|
if (view.total !== undefined) return `${formatBytes(view.downloaded)} / ${formatBytes(view.total)}`
|
|
404
|
-
if (view.downloaded > 0) return
|
|
405
|
-
return task.tool === 'git' ? '
|
|
505
|
+
if (view.downloaded > 0) return `${t('written')} ${formatBytes(view.downloaded)}`
|
|
506
|
+
return t(task.tool === 'git' ? 'fetching' : 'downloading')
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function fileGlyph(fileName) {
|
|
510
|
+
const name = typeof fileName === 'string' ? fileName.trim() : ''
|
|
511
|
+
return GRAPHEMES.segment(name)[Symbol.iterator]().next().value?.segment ?? '↓'
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function progressDisplay(task, view) {
|
|
515
|
+
const failed = task.status === 'failed' || task.status === 'killed'
|
|
516
|
+
const done = task.status === 'completed'
|
|
517
|
+
const percent = done ? 100 : Number.isFinite(view.percent) ? Math.max(0, Math.min(100, view.percent)) : undefined
|
|
518
|
+
return {
|
|
519
|
+
done,
|
|
520
|
+
failed,
|
|
521
|
+
percent,
|
|
522
|
+
// 刚起步时 0% 的环看不见;运行时给一段最小弧,读数仍是真实百分比。
|
|
523
|
+
drawn: percent === undefined ? undefined : view.running ? Math.max(1.5, percent) : percent,
|
|
524
|
+
spinning: view.running && percent === undefined,
|
|
525
|
+
// 失败/取消且从没拿到百分比:这是「出错了」,不是「下载完了」。
|
|
526
|
+
failedUnknown: failed && percent === undefined,
|
|
527
|
+
}
|
|
406
528
|
}
|
|
407
529
|
|
|
408
530
|
/**
|
|
@@ -411,21 +533,75 @@ window.__ModuleLoader__.load({
|
|
|
411
533
|
* @param view - 展示模型。
|
|
412
534
|
* @returns 短文本。
|
|
413
535
|
*/
|
|
414
|
-
function metaOf(
|
|
536
|
+
function metaOf(view, t) {
|
|
415
537
|
if (!view.running) return formatDuration(view.elapsed)
|
|
416
538
|
const parts = []
|
|
417
539
|
if (view.speed > 0) parts.push(`${formatBytes(view.speed)}/s`)
|
|
418
|
-
if (view.eta !== undefined && Number.isFinite(view.eta)) parts.push(
|
|
540
|
+
if (view.eta !== undefined && Number.isFinite(view.eta)) parts.push(`${t('remaining')} ${formatDuration(view.eta)}`)
|
|
419
541
|
parts.push(formatDuration(view.elapsed))
|
|
420
542
|
return parts.join(' · ')
|
|
421
543
|
}
|
|
422
544
|
|
|
545
|
+
/**
|
|
546
|
+
* 收起的球用一句话带上状态、百分比、速度与工具。
|
|
547
|
+
* @param task - 任务。
|
|
548
|
+
* @param view - 展示模型。
|
|
549
|
+
* @param t - locale 文案函数。
|
|
550
|
+
* @returns 短文本。
|
|
551
|
+
*/
|
|
552
|
+
function ballSummary(task, view, t) {
|
|
553
|
+
const status = !view.running || task.status === 'stopping' ? subtitleOf(task, view, t) : ''
|
|
554
|
+
const percent =
|
|
555
|
+
view.percent !== undefined ? `${Math.round(view.percent)}%` : view.running ? t('unknown') : ''
|
|
556
|
+
const parts = []
|
|
557
|
+
if (status) parts.push(status)
|
|
558
|
+
if (percent) parts.push(percent)
|
|
559
|
+
if (view.running) {
|
|
560
|
+
if (view.speed > 0) parts.push(`${formatBytes(view.speed)}/s`)
|
|
561
|
+
if (view.eta !== undefined && Number.isFinite(view.eta)) {
|
|
562
|
+
parts.push(`${t('remaining')} ${formatDuration(view.eta)}`)
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
parts.push(task.tool)
|
|
566
|
+
return parts.join(' · ')
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** 折叠状态按「会话 + 文件 + 开始时间」记住;刷新后仍能认出同一次下载。 */
|
|
570
|
+
function collapseId(task) {
|
|
571
|
+
const started = task.startedAt === null || task.startedAt === undefined ? '' : String(task.startedAt)
|
|
572
|
+
return `${task.sessionId ?? ''}|${task.fileName ?? task.tool ?? ''}|${started}`
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function readCollapsed(id) {
|
|
576
|
+
try {
|
|
577
|
+
if (typeof window === 'undefined' || window.localStorage === undefined) return false
|
|
578
|
+
return window.localStorage.getItem(`dsh-transfer-dock.collapsed.${id}`) === '1'
|
|
579
|
+
} catch {
|
|
580
|
+
return false
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function writeCollapsed(id, value) {
|
|
585
|
+
try {
|
|
586
|
+
if (typeof window === 'undefined' || window.localStorage === undefined) return
|
|
587
|
+
const key = `dsh-transfer-dock.collapsed.${id}`
|
|
588
|
+
if (value) window.localStorage.setItem(key, '1')
|
|
589
|
+
else window.localStorage.removeItem(key)
|
|
590
|
+
} catch {
|
|
591
|
+
// 隐私模式或存储被禁用:折叠仍有效,只是不跨刷新。
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
423
595
|
/** 面板本体。 */
|
|
424
596
|
function DownloadCards(props) {
|
|
425
597
|
const jobs = props.jobs
|
|
426
598
|
const [snapshot, setSnapshot] = React.useState(EMPTY)
|
|
427
599
|
const [clock, setClock] = React.useState(() => Date.now())
|
|
428
|
-
const
|
|
600
|
+
const { locale } = props
|
|
601
|
+
const t = React.useMemo(() => locale.bind('dsh-transfer-dock.ui'), [locale])
|
|
602
|
+
const subscribeLocale = React.useCallback(callback => locale.subscribe(callback), [locale])
|
|
603
|
+
const getLocale = React.useCallback(() => locale.getSnapshot(), [locale])
|
|
604
|
+
React.useSyncExternalStore(subscribeLocale, getLocale)
|
|
429
605
|
|
|
430
606
|
// 轮询 Host 状态;页面隐藏时暂停,避免无意义开销。
|
|
431
607
|
React.useEffect(() => {
|
|
@@ -458,26 +634,14 @@ window.__ModuleLoader__.load({
|
|
|
458
634
|
}
|
|
459
635
|
}, [])
|
|
460
636
|
|
|
461
|
-
// 注入一次动画关键帧。
|
|
462
|
-
React.useEffect(() => {
|
|
463
|
-
const id = 'dsh-transfer-dock-style'
|
|
464
|
-
if (document.getElementById(id) !== null) return
|
|
465
|
-
const style = document.createElement('style')
|
|
466
|
-
style.id = id
|
|
467
|
-
style.textContent = S.keyframes
|
|
468
|
-
document.head.appendChild(style)
|
|
469
|
-
}, [])
|
|
470
|
-
|
|
471
637
|
// 让耗时与 ETA 持续走动。
|
|
472
638
|
React.useEffect(() => {
|
|
473
639
|
const timer = setInterval(() => setClock(Date.now()), 500)
|
|
474
640
|
return () => clearInterval(timer)
|
|
475
641
|
}, [])
|
|
476
642
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
[snapshot, dismissed],
|
|
480
|
-
)
|
|
643
|
+
// 收起只改变显示,不把任务从进度订阅中剔除。
|
|
644
|
+
const tasks = snapshot.tasks
|
|
481
645
|
|
|
482
646
|
// 对运行中的任务打开输出观测,失败或结束即释放。
|
|
483
647
|
const watchKey = tasks
|
|
@@ -527,60 +691,175 @@ window.__ModuleLoader__.load({
|
|
|
527
691
|
const finished = !view.running
|
|
528
692
|
// 太短的任务不闪一下;结束的任务留一小段淡出时间。
|
|
529
693
|
if (!finished && view.elapsed < MIN_LIVE_MS) continue
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
694
|
+
const settledAge =
|
|
695
|
+
finished && task.finishedAt !== null && task.finishedAt !== undefined
|
|
696
|
+
? Math.max(0, clock - task.finishedAt)
|
|
697
|
+
: 0
|
|
698
|
+
// 过期由卡片自己处理:卡片 2.6s,收起的球留 8s;聚焦中的留到失焦,别把键盘焦点丢给 body。
|
|
699
|
+
cards.push(h(DownloadTile, { key: String(task.id), task, view, t, settledAge }))
|
|
534
700
|
}
|
|
535
701
|
if (cards.length === 0) return null
|
|
536
702
|
// 挂到 body:插槽容器上的 transform / overflow 会让 position: fixed 失效或被裁剪。
|
|
537
|
-
const panel = h('div', { style: S.wrap, className: 'dsh-transfer-dock' }, cards)
|
|
703
|
+
const panel = h('div', { style: S.wrap, className: 'dsh-transfer-dock' }, h('style', null, S.keyframes), cards)
|
|
538
704
|
if (ReactDOM !== null) return ReactDOM.createPortal(panel, document.body)
|
|
539
705
|
return panel
|
|
540
706
|
}
|
|
541
707
|
|
|
542
|
-
/**
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
708
|
+
/** 键盘焦点(:focus-visible)是否还停在这一项里。 */
|
|
709
|
+
function holdsKeyboardFocus(node) {
|
|
710
|
+
if (node === null || typeof document === 'undefined') return false
|
|
711
|
+
const active = document.activeElement
|
|
712
|
+
if (active === null || active === undefined || !node.contains(active)) return false
|
|
713
|
+
try {
|
|
714
|
+
return typeof active.matches === 'function' ? active.matches(':focus-visible') : true
|
|
715
|
+
} catch {
|
|
716
|
+
return true
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/** 同一个任务在卡片与球之间切换,保留订阅和键盘焦点。 */
|
|
721
|
+
function DownloadTile({ task, view, t, settledAge }) {
|
|
722
|
+
const id = collapseId(task)
|
|
723
|
+
const [collapsed, setCollapsed] = React.useState(() => readCollapsed(id))
|
|
724
|
+
const [, setFocusTick] = React.useState(0)
|
|
725
|
+
const nextFocus = React.useRef(false)
|
|
726
|
+
const control = React.useRef(null)
|
|
727
|
+
const root = React.useRef(null)
|
|
728
|
+
React.useLayoutEffect(() => {
|
|
729
|
+
if (nextFocus.current) {
|
|
730
|
+
nextFocus.current = false
|
|
731
|
+
control.current?.focus({ preventScroll: true })
|
|
732
|
+
}
|
|
733
|
+
}, [collapsed])
|
|
734
|
+
const toggle = () => {
|
|
735
|
+
nextFocus.current = true
|
|
736
|
+
setCollapsed(value => {
|
|
737
|
+
const next = !value
|
|
738
|
+
writeCollapsed(id, next)
|
|
739
|
+
return next
|
|
740
|
+
})
|
|
741
|
+
}
|
|
742
|
+
const focusProps = {
|
|
743
|
+
onFocus: () => setFocusTick(value => value + 1),
|
|
744
|
+
onBlur: event => {
|
|
745
|
+
// 卡片与球互换时控件会被替换,那一次失焦不算「人走开了」。
|
|
746
|
+
const next = event.relatedTarget
|
|
747
|
+
if (next !== null && typeof next === 'object' && event.currentTarget.contains(next)) return
|
|
748
|
+
setFocusTick(value => value + 1)
|
|
749
|
+
},
|
|
750
|
+
}
|
|
751
|
+
const progress = progressDisplay(task, view)
|
|
752
|
+
const glyph = fileGlyph(task.fileName)
|
|
753
|
+
const expired = !view.running && settledAge > (collapsed ? BALL_FADE_MS : FADE_MS)
|
|
754
|
+
React.useEffect(() => {
|
|
755
|
+
// 任务结束后不再恢复折叠状态,下一次下载重新从卡片开始。
|
|
756
|
+
if (!view.running) writeCollapsed(id, false)
|
|
757
|
+
}, [view.running, id])
|
|
758
|
+
const shared = { rootRef: root, task, view, t, progress, glyph, control, toggle, focusProps }
|
|
759
|
+
// 结束的卡片到时间就该消失;但键盘用户还停在这一项上时,留到失焦为止。
|
|
760
|
+
if (expired && !holdsKeyboardFocus(root.current)) return null
|
|
761
|
+
return collapsed ? h(ProgressBall, shared) : h(Card, shared)
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
function ProgressBall({ task, view, t, progress, glyph, control, toggle, focusProps, rootRef }) {
|
|
765
|
+
const { done, failed, drawn, spinning, failedUnknown } = progress
|
|
766
|
+
const fileName = task.fileName || task.tool
|
|
767
|
+
const summary = ballSummary(task, view, t)
|
|
768
|
+
const label = `${t('expand')}: ${fileName}${summary ? ` (${summary})` : ''}`
|
|
769
|
+
const stroke = done
|
|
770
|
+
? 'var(--dsw-alias-state-success-primary)'
|
|
771
|
+
: failed
|
|
772
|
+
? 'var(--dsw-alias-state-error-primary)'
|
|
773
|
+
: 'var(--dsw-alias-brand-primary)'
|
|
774
|
+
const circle = { cx: 24, cy: 24, r: 19.5, fill: 'none', strokeWidth: 3, pathLength: 100 }
|
|
775
|
+
const centered = { transform: 'rotate(-90 24 24)' }
|
|
776
|
+
let arc = null
|
|
777
|
+
if (spinning) {
|
|
778
|
+
// 不知道百分比:转圈,不假装有进度。
|
|
779
|
+
arc = h(
|
|
780
|
+
'g',
|
|
781
|
+
{ className: 'dsh-transfer-dock-spin', style: { transformOrigin: '24px 24px' } },
|
|
782
|
+
h('circle', { ...circle, ...centered, stroke, strokeLinecap: 'round', strokeDasharray: '24 76' }),
|
|
783
|
+
)
|
|
784
|
+
} else if (failedUnknown) {
|
|
785
|
+
// 失败且从未拿到百分比:虚线环说明「有问题」,不用满环冒充 100%。
|
|
786
|
+
arc = h('circle', { ...circle, ...centered, stroke, strokeDasharray: '5 7' })
|
|
787
|
+
} else if (drawn !== undefined) {
|
|
788
|
+
arc = h('circle', {
|
|
789
|
+
...circle,
|
|
790
|
+
...centered,
|
|
791
|
+
stroke,
|
|
792
|
+
strokeLinecap: 'round',
|
|
793
|
+
className: 'dsh-transfer-dock-arc',
|
|
794
|
+
strokeDasharray: '100 100',
|
|
795
|
+
strokeDashoffset: 100 - drawn,
|
|
796
|
+
})
|
|
797
|
+
}
|
|
798
|
+
return h('button', {
|
|
799
|
+
...focusProps,
|
|
800
|
+
type: 'button',
|
|
801
|
+
// 收起时这一项本身就是按钮:根节点与焦点控件是同一个元素。
|
|
802
|
+
ref: node => {
|
|
803
|
+
rootRef.current = node
|
|
804
|
+
control.current = node
|
|
805
|
+
},
|
|
806
|
+
onClick: toggle, style: S.ball, className: 'dsh-transfer-dock-ball',
|
|
807
|
+
title: label, 'aria-label': label, 'aria-expanded': false,
|
|
808
|
+
},
|
|
809
|
+
h('svg', { viewBox: '0 0 48 48', style: S.ring, 'aria-hidden': true, focusable: 'false' },
|
|
810
|
+
h('circle', {
|
|
811
|
+
...circle,
|
|
812
|
+
stroke: failedUnknown
|
|
813
|
+
? 'color-mix(in srgb, var(--dsw-alias-state-error-primary) 45%, transparent)'
|
|
814
|
+
: 'var(--dsw-alias-border-l2)',
|
|
815
|
+
}),
|
|
816
|
+
arc,
|
|
817
|
+
),
|
|
818
|
+
h('span', { 'aria-hidden': true }, glyph),
|
|
819
|
+
)
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
function Card({ task, view, t, progress, glyph, control, toggle, focusProps, rootRef }) {
|
|
823
|
+
const { done, failed, percent, drawn, spinning } = progress
|
|
551
824
|
const fillStyle = { ...S.fill }
|
|
552
825
|
if (done) Object.assign(fillStyle, S.fillDone)
|
|
553
826
|
if (failed) Object.assign(fillStyle, S.fillFailed)
|
|
554
|
-
if (percent !== undefined) fillStyle.width = `${Math.max(1.5, Math.min(100, percent))}%`
|
|
555
827
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
828
|
+
// 失败但没有百分比时不画满条:没有测量值可画,状态由副标题与错误行说明。
|
|
829
|
+
const bar = h('div', { style: S.track, 'aria-hidden': true },
|
|
830
|
+
spinning
|
|
831
|
+
? h('div', { style: S.indeterminate, className: 'dsh-transfer-dock-stripes' })
|
|
832
|
+
: drawn === undefined
|
|
833
|
+
? null
|
|
834
|
+
: h('div', { style: { ...fillStyle, width: `${drawn}%` } }),
|
|
835
|
+
)
|
|
560
836
|
|
|
561
|
-
const meta = metaOf(
|
|
837
|
+
const meta = metaOf(view, t)
|
|
562
838
|
const percentLabel = percent !== undefined ? `${Math.round(percent)}%` : ''
|
|
563
839
|
|
|
564
840
|
return h(
|
|
565
841
|
'div',
|
|
566
|
-
{ style: S.card },
|
|
842
|
+
{ ...focusProps, ref: rootRef, style: S.card, className: 'dsh-transfer-dock-card' },
|
|
567
843
|
h(
|
|
568
844
|
'div',
|
|
569
845
|
{ style: S.head },
|
|
570
|
-
h('div', { style: S.badge },
|
|
846
|
+
h('div', { style: S.badge, 'aria-hidden': true }, glyph),
|
|
571
847
|
h(
|
|
572
848
|
'div',
|
|
573
849
|
{ style: S.titles },
|
|
574
850
|
h('div', { style: S.name, title: task.label }, task.fileName || task.tool),
|
|
575
|
-
h('div', { style: S.sub }, subtitleOf(task, view)),
|
|
851
|
+
h('div', { style: S.sub }, subtitleOf(task, view, t)),
|
|
576
852
|
),
|
|
577
853
|
h(
|
|
578
854
|
'button',
|
|
579
855
|
{
|
|
580
856
|
type: 'button',
|
|
581
857
|
style: S.close,
|
|
582
|
-
|
|
583
|
-
|
|
858
|
+
ref: control,
|
|
859
|
+
title: t('collapse'),
|
|
860
|
+
'aria-label': `${t('collapse')}: ${task.fileName || task.tool}`,
|
|
861
|
+
'aria-expanded': true,
|
|
862
|
+
onClick: toggle,
|
|
584
863
|
},
|
|
585
864
|
'✕',
|
|
586
865
|
),
|
|
@@ -612,67 +891,17 @@ window.__ModuleLoader__.load({
|
|
|
612
891
|
return view !== undefined && typeof view.text === 'string' ? view.text : ''
|
|
613
892
|
}
|
|
614
893
|
|
|
615
|
-
/**
|
|
616
|
-
* 从列表移除一张卡。
|
|
617
|
-
* @param id - job id。
|
|
618
|
-
* @param setDismissed - 状态写入器。
|
|
619
|
-
*/
|
|
620
|
-
function dismiss(id, setDismissed) {
|
|
621
|
-
setDismissed(current => {
|
|
622
|
-
const next = new Set(current)
|
|
623
|
-
next.add(String(id))
|
|
624
|
-
return next
|
|
625
|
-
})
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
/**
|
|
629
|
-
* 工具徽标上的字母。
|
|
630
|
-
* @param tool - 工具名。
|
|
631
|
-
* @returns 一到两个字符。
|
|
632
|
-
*/
|
|
633
|
-
function toolGlyph(tool) {
|
|
634
|
-
switch (tool) {
|
|
635
|
-
case 'git':
|
|
636
|
-
return 'git'
|
|
637
|
-
case 'pip':
|
|
638
|
-
case 'uv':
|
|
639
|
-
return 'py'
|
|
640
|
-
case 'npm':
|
|
641
|
-
return 'npm'
|
|
642
|
-
case 'aria2':
|
|
643
|
-
return 'a2'
|
|
644
|
-
case 'curl':
|
|
645
|
-
return 'c'
|
|
646
|
-
case 'wget':
|
|
647
|
-
return 'w'
|
|
648
|
-
case 'http':
|
|
649
|
-
return 'web'
|
|
650
|
-
case 'winget':
|
|
651
|
-
case 'choco':
|
|
652
|
-
case 'scoop':
|
|
653
|
-
case 'nuget':
|
|
654
|
-
return 'app'
|
|
655
|
-
case 'cargo':
|
|
656
|
-
return 'rs'
|
|
657
|
-
case 'go':
|
|
658
|
-
return 'go'
|
|
659
|
-
case 'docker':
|
|
660
|
-
return 'dk'
|
|
661
|
-
default:
|
|
662
|
-
return '↓'
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
|
|
666
894
|
return {
|
|
667
|
-
inject: ['slots', 'jobs'],
|
|
895
|
+
inject: ['slots', 'jobs', 'locale'],
|
|
668
896
|
apply(ctx) {
|
|
897
|
+
ctx.effect(() => ctx.locale.register('dsh-transfer-dock.ui', MESSAGES))
|
|
669
898
|
ctx.slots.inject('shell.overlay', () =>
|
|
670
899
|
ctx.slots.register(
|
|
671
900
|
{
|
|
672
901
|
name: 'shell.overlay',
|
|
673
902
|
id: 'dsh-transfer-dock',
|
|
674
903
|
order: 60,
|
|
675
|
-
inject: () => ({ jobs: ctx.jobs }),
|
|
904
|
+
inject: () => ({ jobs: ctx.jobs, locale: ctx.locale }),
|
|
676
905
|
},
|
|
677
906
|
DownloadCards,
|
|
678
907
|
),
|
package/docs/architecture.svg
CHANGED
|
@@ -174,6 +174,6 @@
|
|
|
174
174
|
<text x="266" y="710" class="mono" font-size="10.5" fill="#b9b5ce">磁盘增量 ÷ HEAD 总量</text>
|
|
175
175
|
<text x="452" y="710" class="mono" font-size="10.5" fill="#6f6c86">→</text>
|
|
176
176
|
<text x="478" y="710" class="mono" font-size="10.5" fill="#b9b5ce">都没有:条纹不确定态</text>
|
|
177
|
-
<text x="1216" y="710" class="mono" font-size="10" fill="#6f6c86" text-anchor="end">dsh-transfer-dock 0.1.
|
|
177
|
+
<text x="1216" y="710" class="mono" font-size="10" fill="#6f6c86" text-anchor="end">dsh-transfer-dock 0.1.3</text>
|
|
178
178
|
</g>
|
|
179
179
|
</svg>
|
|
Binary file
|
package/index.js
CHANGED
|
@@ -117,7 +117,7 @@ function installStateRoute(ctx, tracker) {
|
|
|
117
117
|
let attempts = 0
|
|
118
118
|
let timer
|
|
119
119
|
// 浏览器半每 600ms 轮询一次;计数用来判断它到底有没有挂载。
|
|
120
|
-
const stats = { hits: 0, lastHitAt: 0, build: '0.1.
|
|
120
|
+
const stats = { hits: 0, lastHitAt: 0, build: '0.1.3' }
|
|
121
121
|
|
|
122
122
|
const attempt = () => {
|
|
123
123
|
if (dispose !== undefined) return
|