-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-ui.js
More file actions
83 lines (76 loc) · 2.65 KB
/
progress-ui.js
File metadata and controls
83 lines (76 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function (root, factory) {
const api = factory(root || {});
if (typeof module === "object" && module.exports) module.exports = api;
if (root) root.CodexProgressUi = Object.freeze(api);
})(typeof window !== "undefined" ? window : globalThis, function (root) {
const fallbackEscapeHtml = (value) => String(value ?? "").replace(/[&<>"']/g, (char) => ({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
})[char]);
const runningStatuses = new Set(["处理中", "刷新中", "读取中", "拉取中"]);
function createProgressUi(deps = {}) {
const escapeHtml = deps.escapeHtml
|| root.CodexFormatCore?.escapeHtml
|| fallbackEscapeHtml;
function progressStats(progress = {}) {
const items = Array.isArray(progress.items) ? progress.items : [];
const total = items.length;
const completed = items.filter((item) => item.status === "已完成").length;
const failed = items.filter((item) => item.status === "失败").length;
const running = progress.done
? 0
: items.filter((item) => runningStatuses.has(item.status)).length;
const done = completed + failed;
const percent = progress.done
? 100
: total ? Math.min(96, Math.round(((done + running * 0.35) / total) * 100)) : 0;
return {
total,
completed,
failed,
running,
done,
percent,
};
}
function renderProgressList(items = []) {
return items.map((item) => {
const status = item.status || "等待";
const className = status === "失败"
? "bad"
: status === "已完成" ? "ok"
: runningStatuses.has(status) ? "running" : "";
return `
<div class="progress-item ${className}">
<strong>${escapeHtml(item.label)}</strong>
<span>${escapeHtml(status)}${item.detail ? ` · ${escapeHtml(item.detail)}` : ""}</span>
</div>
`;
}).join("");
}
function renderOperationProgress(progress = {}) {
const stats = progressStats(progress);
return {
title: progress.title || "正在处理",
summary: progress.done
? (progress.summary || "处理完成。")
: `${stats.done}/${stats.total} 已处理,失败 ${stats.failed}`,
percent: stats.percent,
closeDisabled: !progress.done,
listHtml: renderProgressList(Array.isArray(progress.items) ? progress.items : []),
stats,
};
}
return Object.freeze({
progressStats,
renderProgressList,
renderOperationProgress,
});
}
return Object.freeze({
createProgressUi,
});
});