@coda-console/resource-manager 0.3.0 → 0.5.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/README.md +10 -2
- package/lib/content.mjs +15 -2
- package/lib/draft.mjs +57 -0
- package/lib/info.mjs +137 -0
- package/lib/snapshots.mjs +52 -0
- package/lib/tree.mjs +237 -48
- package/main.mjs +69 -14
- package/package.json +1 -1
- package/view/app.mjs +104 -82
- package/view/chat.mjs +45 -0
- package/view/connection.mjs +50 -24
- package/view/dom.mjs +9 -0
- package/view/file-app.mjs +19 -3
- package/view/file.html +2 -0
- package/view/format.mjs +67 -0
- package/view/hint.mjs +20 -0
- package/view/icons.mjs +139 -22
- package/view/index.html +1 -0
- package/view/info.mjs +117 -0
- package/view/menu.mjs +76 -0
- package/view/model.mjs +48 -19
- package/view/preview.mjs +16 -18
- package/view/protocol.mjs +24 -5
- package/view/selection.mjs +33 -0
- package/view/style.css +60 -13
- package/view/tree.mjs +39 -14
package/view/connection.mjs
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
// 页面到模块的连接:请求与回答(Link)、hello 握手与超时、按代次分发协议消息。
|
|
2
2
|
// 树页和文件页共用。只管连接:树的 Model、状态保存、提示文案和 DOM 都在各自的页面里。
|
|
3
|
-
import { ErrorCode, Type } from './protocol.mjs';
|
|
3
|
+
import { ErrorCode, Type, isReply, messageSize } from './protocol.mjs';
|
|
4
4
|
|
|
5
5
|
export const TIMEOUT_MS = 30_000;
|
|
6
6
|
|
|
7
7
|
// 会话已经换了,请求作废。调用方一律吞掉,不显示。
|
|
8
8
|
export class Stale extends Error { constructor() { super('已过期'); this.stale = true; } }
|
|
9
|
+
// 页面自己取消了请求:和 Stale 一样静默,调用方吞掉。
|
|
10
|
+
export class Cancelled extends Stale { constructor() { super(); this.cancelled = true; } }
|
|
9
11
|
export class RequestError extends Error { constructor(code, message) { super(message); this.code = code; } }
|
|
10
12
|
|
|
11
13
|
// 请求与回答按 requestId 对应。回答要代次相同、请求号还在等待才算;换会话时 reset 作废所有等待并停掉超时。
|
|
14
|
+
// 一个请求只在 #finish 里结算:回答、发送失败、超时、代次失效、取消都走它,只结算一次,同时清掉定时器和取消监听。
|
|
12
15
|
export class Link {
|
|
13
16
|
generation = null;
|
|
14
17
|
pending = new Map();
|
|
@@ -18,45 +21,68 @@ export class Link {
|
|
|
18
21
|
Object.assign(this, { post, timers, timeoutMs });
|
|
19
22
|
}
|
|
20
23
|
|
|
24
|
+
// 结算 requestId 对应的等待:还在等才算,返回是否结算了。
|
|
25
|
+
#finish(requestId, settle) {
|
|
26
|
+
const entry = this.pending.get(requestId);
|
|
27
|
+
if (!entry) return false;
|
|
28
|
+
this.pending.delete(requestId);
|
|
29
|
+
this.timers.clearTimeout(entry.timer);
|
|
30
|
+
entry.signal?.removeEventListener('abort', entry.onAbort);
|
|
31
|
+
settle(entry);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
21
35
|
reset(generation) {
|
|
22
36
|
this.generation = generation;
|
|
23
|
-
for (const
|
|
24
|
-
|
|
37
|
+
for (const requestId of [...this.pending.keys()]) this.#finish(requestId, entry => entry.reject(new Stale()));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 这条请求发出去的消息有多大(含信封):用下一个请求号算,和 call 真正发出的一致。页面用它在发之前判断会不会超过消息上限。
|
|
41
|
+
size(type, fields) {
|
|
42
|
+
return messageSize({ type, generation: this.generation, requestId: `q${this.#next}`, ...fields });
|
|
25
43
|
}
|
|
26
44
|
|
|
27
|
-
|
|
45
|
+
// options.signal:中止后请求静默作废(Cancelled)。options.cancel:中止时还要告诉模块的消息类型,带 generation 和 requestId;
|
|
46
|
+
// 请求还没发出就中止的,两条都不发。
|
|
47
|
+
call(type, fields, { signal, cancel } = {}) {
|
|
28
48
|
if (this.generation === null) return Promise.reject(new Stale());
|
|
49
|
+
if (signal?.aborted) return Promise.reject(new Cancelled());
|
|
29
50
|
const { generation } = this, requestId = `q${this.#next++}`;
|
|
30
51
|
return new Promise((resolve, reject) => {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
const fire = error => this.#finish(requestId, entry => entry.reject(error));
|
|
53
|
+
const timer = this.timers.setTimeout(() => fire(new RequestError('timeout', '30 秒没有回答')), this.timeoutMs);
|
|
54
|
+
const entry = { resolve, reject, timer, signal, posted: false };
|
|
55
|
+
entry.onAbort = () => {
|
|
56
|
+
const { posted } = entry;
|
|
57
|
+
if (fire(new Cancelled()) && cancel && posted) this.#notify({ type: cancel, generation, requestId });
|
|
37
58
|
};
|
|
38
|
-
|
|
39
|
-
this.pending.set(requestId,
|
|
40
|
-
Promise.resolve().then(() =>
|
|
41
|
-
|
|
59
|
+
signal?.addEventListener('abort', entry.onAbort, { once: true });
|
|
60
|
+
this.pending.set(requestId, entry);
|
|
61
|
+
Promise.resolve().then(() => {
|
|
62
|
+
if (signal?.aborted) return; // 发出之前就中止了:不发,也不用告诉模块
|
|
63
|
+
entry.posted = true;
|
|
64
|
+
return this.post({ type, generation, requestId, ...fields });
|
|
65
|
+
}).catch(() => fire(new RequestError(ErrorCode.IO, '消息发不出去')));
|
|
42
66
|
});
|
|
43
67
|
}
|
|
44
68
|
|
|
69
|
+
// 没有回答的消息,发不出去也不影响页面。
|
|
70
|
+
#notify(message) {
|
|
71
|
+
try { Promise.resolve(this.post(message)).catch(() => {}); } catch { /* 宿主不可用 */ }
|
|
72
|
+
}
|
|
73
|
+
|
|
45
74
|
receive(message) {
|
|
46
75
|
if (message.generation !== this.generation) return false;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (message.error) entry.reject(new RequestError(message.error.code, message.error.message));
|
|
52
|
-
else entry.resolve(message);
|
|
53
|
-
return true;
|
|
76
|
+
return this.#finish(message.requestId, entry => {
|
|
77
|
+
if (message.error) entry.reject(new RequestError(message.error.code, message.error.message));
|
|
78
|
+
else entry.resolve(message);
|
|
79
|
+
});
|
|
54
80
|
}
|
|
55
81
|
}
|
|
56
82
|
|
|
57
83
|
// 建立连接并开始监听模块的消息;调用 hello() 才发出握手。
|
|
58
84
|
// handlers:session、root、changed。session 先作废旧代次的请求再交给页面;
|
|
59
|
-
// root、changed
|
|
85
|
+
// root、changed 只在代次等于当前时才交出;各种回答(见 protocol 的 ReplyOf)直接对应请求,页面不用管。
|
|
60
86
|
// 握手的结束条件是收到首个 root;之后不再提示超时。
|
|
61
87
|
// onStatus:握手状态变了(开始、失败、重试)时调用,页面据此重画说明。
|
|
62
88
|
export function connect(coda, { handlers, onStatus = () => {} }) {
|
|
@@ -84,8 +110,8 @@ export function connect(coda, { handlers, onStatus = () => {} }) {
|
|
|
84
110
|
failure = null;
|
|
85
111
|
}
|
|
86
112
|
return handlers[data.type](data);
|
|
87
|
-
|
|
88
|
-
link.receive(data);
|
|
113
|
+
default:
|
|
114
|
+
if (isReply(data.type)) link.receive(data);
|
|
89
115
|
}
|
|
90
116
|
});
|
|
91
117
|
|
package/view/dom.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// 小工具:造元素。on 开头的属性是事件监听,值为 null、undefined、false 的属性不设。
|
|
2
|
+
export const h = (tag, attributes = {}, ...children) => {
|
|
3
|
+
const node = document.createElement(tag);
|
|
4
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
5
|
+
if (key.startsWith('on')) node.addEventListener(key.slice(2), value); else if (value != null && value !== false) node.setAttribute(key, value);
|
|
6
|
+
}
|
|
7
|
+
node.append(...children.flat().filter(child => child != null));
|
|
8
|
+
return node;
|
|
9
|
+
};
|
package/view/file-app.mjs
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// 文件视图的页面入口:读一个文件。会话和路径来自 coda.args(启动值),之后的变化走 context 事件;
|
|
2
2
|
// 连接在 connection.mjs,预览在 preview.mjs,这里不创建目录树。设计见 docs/design/phase-2.md 第 7 节。
|
|
3
|
+
// Mac 的独立窗口里选中文字右键可以添加到聊天(第 13.7 节);iPhone 不接,用系统的选区菜单。
|
|
3
4
|
import { EXIT, paintBar } from './bar.mjs';
|
|
5
|
+
import { createChat } from './chat.mjs';
|
|
4
6
|
import { connect } from './connection.mjs';
|
|
7
|
+
import { mountHint } from './hint.mjs';
|
|
8
|
+
import { mountMenu } from './menu.mjs';
|
|
5
9
|
import { noticeText } from './model.mjs';
|
|
6
10
|
import { showNotice } from './notice.mjs';
|
|
7
11
|
import { mountPreview } from './preview.mjs';
|
|
@@ -29,7 +33,13 @@ let root = null;
|
|
|
29
33
|
let restore = parseScroll(quiet(() => coda.getState()));
|
|
30
34
|
|
|
31
35
|
const conn = connect(coda, { handlers: { session: onSession, root: onRoot, changed: onChanged }, onStatus: () => schedule() });
|
|
32
|
-
const
|
|
36
|
+
const hint = mountHint($('hint'));
|
|
37
|
+
const menu = mountMenu($('menu'));
|
|
38
|
+
const chat = createChat({ link: conn.link, copy: text => coda.copy(text), hint });
|
|
39
|
+
const preview = mountPreview($('preview'), {
|
|
40
|
+
read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text), copyInHead: !isIOS,
|
|
41
|
+
selectionMenu: isIOS ? null : (event, selection) => menu.open(event, { items: chat.selectionItems(selection) }),
|
|
42
|
+
});
|
|
33
43
|
|
|
34
44
|
// 模块跟随的会话和启动参数里的不是同一个时(换会话的两条消息没有先后保证),不能读,等对上。
|
|
35
45
|
const ready = () => want.path !== null && session === want.session && root?.state === RootState.READY;
|
|
@@ -84,6 +94,8 @@ function paintNotice() {
|
|
|
84
94
|
function onSession(data) {
|
|
85
95
|
park();
|
|
86
96
|
preview.close();
|
|
97
|
+
menu.hide();
|
|
98
|
+
hint.hide();
|
|
87
99
|
session = data.session ?? null;
|
|
88
100
|
root = null;
|
|
89
101
|
schedule();
|
|
@@ -91,6 +103,7 @@ function onSession(data) {
|
|
|
91
103
|
|
|
92
104
|
function onRoot(data) {
|
|
93
105
|
const before = root;
|
|
106
|
+
if (data.state !== before?.state || data.path !== before?.path) menu.hide(); // 选区和行号是旧内容上的
|
|
94
107
|
root = { state: data.state, path: data.path };
|
|
95
108
|
if (data.state === RootState.READY) {
|
|
96
109
|
if (before?.path !== data.path) preview.reload(); else preview.refresh();
|
|
@@ -99,13 +112,15 @@ function onRoot(data) {
|
|
|
99
112
|
}
|
|
100
113
|
|
|
101
114
|
function onChanged() {
|
|
102
|
-
if (root?.state
|
|
115
|
+
if (root?.state !== RootState.READY) return;
|
|
116
|
+
menu.hide(); // 内容可能变了,开着的选区菜单里的选区和行号作废
|
|
117
|
+
preview.refresh();
|
|
103
118
|
}
|
|
104
119
|
|
|
105
120
|
// ---- 参数变了:换文件或换会话,旧文件的滚动位置不带过去 ----
|
|
106
121
|
quiet(() => coda.on('context', ({ args }) => {
|
|
107
122
|
const next = argsOf(args);
|
|
108
|
-
if (next.path !== want.path || next.session !== want.session) { restore = null; preview.close(); }
|
|
123
|
+
if (next.path !== want.path || next.session !== want.session) { restore = null; preview.close(); menu.hide(); hint.hide(); }
|
|
109
124
|
want = next;
|
|
110
125
|
schedule();
|
|
111
126
|
}));
|
|
@@ -137,6 +152,7 @@ function saveNow() {
|
|
|
137
152
|
// visibility: false 之后只保证同步事件处理完,所以同步保存。
|
|
138
153
|
quiet(() => coda.on('visibility', visible => { if (!visible) saveNow(); }));
|
|
139
154
|
preview.onScroll(queueSave);
|
|
155
|
+
preview.onScroll(menu.hide);
|
|
140
156
|
|
|
141
157
|
render();
|
|
142
158
|
conn.hello();
|
package/view/file.html
CHANGED
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
<div id="notice" class="pane empty secondary"></div>
|
|
13
13
|
</main>
|
|
14
14
|
<div id="bar" class="bar bottom" hidden></div>
|
|
15
|
+
<div id="menu" class="menu" role="menu" hidden></div>
|
|
16
|
+
<div id="hint" class="hint" role="status" hidden></div>
|
|
15
17
|
<script type="module" src="file-app.mjs"></script>
|
|
16
18
|
</body>
|
|
17
19
|
</html>
|
package/view/format.mjs
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// 目录树和菜单里的文字格式:大小、修改日期。纯函数,不依赖运行环境的语言设置;now 由调用方给。
|
|
2
|
+
import { EntryKind, SortKey } from './protocol.mjs';
|
|
3
|
+
|
|
4
|
+
export function formatSize(bytes) {
|
|
5
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
6
|
+
const units = ['KiB', 'MiB', 'GiB'];
|
|
7
|
+
let value = bytes / 1024, unit = 0;
|
|
8
|
+
while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit++; }
|
|
9
|
+
return `${value.toFixed(value < 10 ? 1 : 0)} ${units[unit]}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const two = n => String(n).padStart(2, '0');
|
|
13
|
+
|
|
14
|
+
// 相对格式,按本地时间:今天 14:05,今年 11月4日,往年 2025年3月。
|
|
15
|
+
export function formatDate(ms, now) {
|
|
16
|
+
const date = new Date(ms), current = new Date(now);
|
|
17
|
+
if (date.getFullYear() !== current.getFullYear()) return `${date.getFullYear()}年${date.getMonth() + 1}月`;
|
|
18
|
+
if (date.getMonth() !== current.getMonth() || date.getDate() !== current.getDate()) return `${date.getMonth() + 1}月${date.getDate()}日`;
|
|
19
|
+
return `${two(date.getHours())}:${two(date.getMinutes())}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 完整的修改时间:2025-03-04 14:05:09。
|
|
23
|
+
export function formatFullDate(ms) {
|
|
24
|
+
const d = new Date(ms);
|
|
25
|
+
return `${d.getFullYear()}-${two(d.getMonth() + 1)}-${two(d.getDate())} ${two(d.getHours())}:${two(d.getMinutes())}:${two(d.getSeconds())}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const MISSING = '—';
|
|
29
|
+
const isDir = entry => entry.kind === EntryKind.DIR;
|
|
30
|
+
// 条目的大小和日期:文件读不到的部分是 —;文件夹不显示大小,读不到修改时间也不显示 —,返回空串。
|
|
31
|
+
export const sizeText = entry => (isDir(entry) ? '' : Number.isInteger(entry.size) ? formatSize(entry.size) : MISSING);
|
|
32
|
+
export const dateText = (entry, now) => (Number.isInteger(entry.mtimeMs) ? formatDate(entry.mtimeMs, now) : isDir(entry) ? '' : MISSING);
|
|
33
|
+
const joined = parts => parts.filter(Boolean).join(' · ');
|
|
34
|
+
// iPhone 第二行:日期 · 大小;文件夹只有日期。
|
|
35
|
+
export const subText = (entry, now) => joined([dateText(entry, now), sizeText(entry)]);
|
|
36
|
+
// 菜单信息行和 Mac 的 title:大小 · 完整修改时间;文件夹只有完整修改时间。没有内容时是空串。
|
|
37
|
+
export const infoText = entry => joined([sizeText(entry), Number.isInteger(entry.mtimeMs) ? formatFullDate(entry.mtimeMs) : isDir(entry) ? '' : MISSING]);
|
|
38
|
+
|
|
39
|
+
const NAMES = { [SortKey.NAME]: '名称', [SortKey.KIND]: '种类', [SortKey.DATE]: '日期', [SortKey.SIZE]: '大小' };
|
|
40
|
+
export const sortName = key => NAMES[key];
|
|
41
|
+
|
|
42
|
+
// ---- 属性页 ----
|
|
43
|
+
// 千分位:1204 → 1,204。
|
|
44
|
+
export const thousands = n => String(n).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
45
|
+
// 项数:整万的写成 20 万项,别的千分位 1,204 项。
|
|
46
|
+
export const countText = n => (n >= 10000 && n % 10000 === 0 ? `${n / 10000} 万项` : `${thousands(n)} 项`);
|
|
47
|
+
// 文件大小:2.5 KiB(2,560 字节);不到 1 KiB 只写字节数。
|
|
48
|
+
export const fileSizeText = bytes => (bytes < 1024 ? `${bytes} 字节` : `${formatSize(bytes)}(${thousands(bytes)} 字节)`);
|
|
49
|
+
|
|
50
|
+
// 文件夹大小,{ size, items, complete, unreadable } 是 dir_size 的回答。
|
|
51
|
+
// 统计完:3.2 GiB(1,204 项);有读不到的:至少 3.2 GiB(3 项读不到);没统计完:超过 3.2 GiB(已统计 20 万项);
|
|
52
|
+
// 一项都没读到就到时间(目录正忙):不写「超过 0 B」。
|
|
53
|
+
export function dirSizeText({ size, items, complete, unreadable = 0 }) {
|
|
54
|
+
if (!complete && items === 0) return '目录正忙,没统计完';
|
|
55
|
+
const bytes = formatSize(size);
|
|
56
|
+
if (!complete) return `超过 ${bytes}(${[`已统计 ${countText(items)}`, unreadable > 0 ? `${unreadable} 项读不到` : ''].filter(Boolean).join(',')})`;
|
|
57
|
+
return unreadable > 0 ? `至少 ${bytes}(${unreadable} 项读不到)` : `${bytes}(${countText(items)})`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 权限:rw-r--r--;setuid、setgid、sticky 照 ls 的写法。
|
|
61
|
+
export function modeText(mode) {
|
|
62
|
+
const chars = Array.from('rwxrwxrwx', (char, i) => (mode & (0o400 >> i) ? char : '-'));
|
|
63
|
+
for (const [bit, at, on] of [[0o4000, 2, 's'], [0o2000, 5, 's'], [0o1000, 8, 't']]) {
|
|
64
|
+
if (mode & bit) chars[at] = chars[at] === 'x' ? on : on.toUpperCase();
|
|
65
|
+
}
|
|
66
|
+
return chars.join('');
|
|
67
|
+
}
|
package/view/hint.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// 页面底部的一条短提示:失败(橙色)停 5 秒,成功(普通)停 2 秒,新的提示替换旧的。树页和文件页共用。
|
|
2
|
+
export const HINT_MS = { warn: 5000, ok: 2000 };
|
|
3
|
+
|
|
4
|
+
export function mountHint(el) {
|
|
5
|
+
let timer = null;
|
|
6
|
+
return {
|
|
7
|
+
// warn:失败的提示。ms 缺省按 warn 取 HINT_MS。
|
|
8
|
+
show(text, { warn = true, ms = warn ? HINT_MS.warn : HINT_MS.ok } = {}) {
|
|
9
|
+
el.textContent = text;
|
|
10
|
+
el.classList.toggle('warn', warn);
|
|
11
|
+
el.hidden = false;
|
|
12
|
+
clearTimeout(timer);
|
|
13
|
+
timer = setTimeout(() => { el.hidden = true; }, ms);
|
|
14
|
+
},
|
|
15
|
+
hide() {
|
|
16
|
+
clearTimeout(timer);
|
|
17
|
+
el.hidden = true;
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
package/view/icons.mjs
CHANGED
|
@@ -1,16 +1,52 @@
|
|
|
1
|
-
// 图标:页面里自己画的 SVG,形状近似 SF Symbol,不打包 Apple
|
|
1
|
+
// 图标:页面里自己画的 SVG,形状近似 SF Symbol,不打包 Apple 的符号,也不用任何第三方素材。颜色都是 currentColor,由 CSS 定:
|
|
2
|
+
// 种类图标带 tone-* 类(颜色见 style.css 的 --tone-*),没有色调的(工具条、菜单的图标)跟随所在位置的文字色。
|
|
2
3
|
import { EntryKind } from './protocol.mjs';
|
|
3
4
|
|
|
4
5
|
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
5
6
|
|
|
6
|
-
// 描边图标的 24×24 画法;folder
|
|
7
|
+
// 描边图标的 24×24 画法;folder 是填充的,单独标。形状 [标签, 属性, 文字?]:个别形状用自己的 fill、stroke、opacity 盖过根上的,文字放第三项。
|
|
8
|
+
const PAGE = [['path', { d: 'M7 3h7l5 5v11a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z' }], ['path', { d: 'M14 3v5h5' }]];
|
|
9
|
+
const FILLED = { fill: 'currentColor', stroke: 'none' };
|
|
10
|
+
// 语言类:淡底加字母,不是任何语言的徽标;字母用 text,字体跟页面。
|
|
11
|
+
const badge = (letters, size = 10.5) => [
|
|
12
|
+
['rect', { x: 2, y: 3, width: 20, height: 18, rx: 4.5, ...FILLED, opacity: 0.16 }],
|
|
13
|
+
['text', { x: 12, y: Number((12 + size * 0.36).toFixed(2)), 'text-anchor': 'middle', 'font-size': size, 'font-weight': 700, ...FILLED }, letters],
|
|
14
|
+
];
|
|
7
15
|
const SHAPES = {
|
|
8
16
|
folder: { fill: true, shapes: [['path', { d: 'M3 7.2C3 5.98 3.98 5 5.2 5h4.05c.6 0 1.17.24 1.59.66L12 6.8h6.8C20.02 6.8 21 7.78 21 9v8.8c0 1.22-.98 2.2-2.2 2.2H5.2C3.98 20 3 19.02 3 17.8V7.2z' }]] },
|
|
9
|
-
doc: { shapes:
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
doc: { shapes: PAGE },
|
|
18
|
+
text: { shapes: [...PAGE, ['path', { d: 'M9 13h6M9 17h4' }]] },
|
|
19
|
+
code: { shapes: [...PAGE, ['path', { d: 'M10.5 12.5 8.5 15l2 2.5M13.5 12.5l2 2.5-2 2.5' }]] },
|
|
20
|
+
ts: { shapes: badge('TS') },
|
|
21
|
+
js: { shapes: badge('JS') },
|
|
22
|
+
py: { shapes: badge('Py') },
|
|
23
|
+
go: { shapes: badge('Go') },
|
|
24
|
+
rs: { shapes: badge('Rs') },
|
|
25
|
+
swift: { shapes: badge('Sw') },
|
|
26
|
+
c: { shapes: badge('C', 12) },
|
|
27
|
+
java: { shapes: badge('Jv') },
|
|
28
|
+
ruby: { shapes: badge('Rb') },
|
|
29
|
+
md: { shapes: badge('MD', 9.5) },
|
|
30
|
+
pdf: { shapes: badge('PDF', 8) },
|
|
31
|
+
json: { shapes: [['path', { d: 'M9 4C6.5 4 7 7 7 9c0 2-1 3-2.5 3C6 12 7 13 7 15c0 2-.5 5 2 5M15 4c2.5 0 2 3 2 5 0 2 1 3 2.5 3-1.5 0-2.5 1-2.5 3 0 2 .5 5-2 5' }]] },
|
|
32
|
+
html: { shapes: [['path', { d: 'M8.5 7 3.5 12l5 5M15.5 7l5 5-5 5' }]] },
|
|
33
|
+
css: { shapes: [['path', { d: 'M9.5 4 8 20M16 4l-1.5 16M5 9h15M4 15h15' }]] },
|
|
34
|
+
config: { shapes: [['path', { d: 'M4 7h9M17 7h3M4 17h3M11 17h9' }], ['circle', { cx: 15, cy: 7, r: 2.3 }], ['circle', { cx: 9, cy: 17, r: 2.3 }]] },
|
|
35
|
+
shell: { shapes: [['rect', { x: 3, y: 4, width: 18, height: 16, rx: 3.5 }], ['path', { d: 'm7 9.5 3 2.5-3 2.5M12.5 15.5H17' }]] },
|
|
36
|
+
image: { shapes: [['rect', { x: 3, y: 4, width: 18, height: 16, rx: 3 }], ['circle', { cx: 8.7, cy: 9.5, r: 1.6, ...FILLED }], ['path', { d: 'm4 17.5 4.5-4.5 3.5 3.5 3-3 5 5' }]] },
|
|
37
|
+
video: { shapes: [['rect', { x: 3, y: 5, width: 18, height: 14, rx: 3 }], ['path', { d: 'M10 9v6l5-3z', ...FILLED }]] },
|
|
38
|
+
audio: { shapes: [['path', { d: 'M9 18V6l10-2v12' }], ['circle', { cx: 6.5, cy: 18, r: 2.5 }], ['circle', { cx: 16.5, cy: 16, r: 2.5 }]] },
|
|
39
|
+
archive: { shapes: [...PAGE, ['path', { d: 'M10 3h2v2h-2zM12 5h2v2h-2zM10 7h2v2h-2zM12 9h2v2h-2z', ...FILLED }], ['rect', { x: 10, y: 12, width: 4, height: 5, rx: 1, 'stroke-width': 1.5 }]] },
|
|
40
|
+
sheet: { shapes: [['rect', { x: 3, y: 4, width: 18, height: 16, rx: 3 }], ['path', { d: 'M3 10h18M3 15h18M10 10v10' }]] },
|
|
41
|
+
database: { shapes: [['ellipse', { cx: 12, cy: 6, rx: 8, ry: 3 }], ['path', { d: 'M4 6v12c0 1.7 3.6 3 8 3s8-1.3 8-3V6M4 12c0 1.7 3.6 3 8 3s8-1.3 8-3' }]] },
|
|
42
|
+
font: { shapes: [['text', { x: 12, y: 17.5, 'text-anchor': 'middle', 'font-size': 16, 'font-weight': 600, ...FILLED }, 'Aa']] },
|
|
43
|
+
lock: { shapes: [['rect', { x: 5, y: 10.5, width: 14, height: 10, rx: 2.5 }], ['path', { d: 'M8.5 10.5V8a3.5 3.5 0 0 1 7 0v2.5' }]] },
|
|
44
|
+
git: { shapes: [['circle', { cx: 7, cy: 6, r: 2.2 }], ['circle', { cx: 7, cy: 18, r: 2.2 }], ['circle', { cx: 17, cy: 8, r: 2.2 }], ['path', { d: 'M7 8.2v7.6M17 10.2c0 3-3 3.8-10 5' }]] },
|
|
45
|
+
env: { shapes: [['circle', { cx: 8, cy: 12, r: 4 }], ['path', { d: 'M12 12h9M18 12v3.5M21 12v2.5' }]] },
|
|
46
|
+
manifest: { shapes: [['path', { d: 'M12 3 4 7.5v9L12 21l8-4.5v-9L12 3zM4 7.5 12 12l8-4.5M12 12v9' }]] },
|
|
47
|
+
license: { shapes: [...PAGE, ['circle', { cx: 11.5, cy: 13.5, r: 2.5, 'stroke-width': 1.5 }], ['path', { d: 'm10 15.5-1 3.5 2.5-1 2.5 1-1-3.5', 'stroke-width': 1.5 }]] },
|
|
12
48
|
link: { shapes: [['path', { d: 'M10 14a4 4 0 0 0 5.66 0l3-3a4 4 0 0 0-5.66-5.66l-1 1' }], ['path', { d: 'M14 10a4 4 0 0 0-5.66 0l-3 3a4 4 0 0 0 5.66 5.66l1-1' }]] },
|
|
13
|
-
other: { shapes: [
|
|
49
|
+
other: { shapes: [...PAGE, ['path', { d: 'M9.5 14.5h5' }]] },
|
|
14
50
|
chevron: { shapes: [['path', { d: 'm9 5 7 7-7 7' }]] },
|
|
15
51
|
warning: { shapes: [['path', { d: 'M12 4 2.8 19.5h18.4L12 4z' }], ['path', { d: 'M12 10v4.5M12 17.2v.1' }]] },
|
|
16
52
|
back: { shapes: [['path', { d: 'm15 5-7 7 7 7' }]] },
|
|
@@ -22,35 +58,116 @@ const SHAPES = {
|
|
|
22
58
|
turn: { shapes: [['path', { d: 'M5 18v-4a4 4 0 0 1 4-4h10' }], ['path', { d: 'm15 6 4 4-4 4' }]] },
|
|
23
59
|
};
|
|
24
60
|
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
61
|
+
// 种类表:种类文字(属性页)和图标(目录树、预览头部)查同一张,不另写一份。
|
|
62
|
+
// glyph 是上面 SHAPES 里的图标名,每种一个图标;tone 是色调名,颜色在 style.css 的 --tone-*:accent 是强调色(文件夹),muted 不上色(链接、特殊文件不能打开,跟随三级文字色)。
|
|
63
|
+
const KINDS = {
|
|
64
|
+
dir: { glyph: 'folder', tone: 'accent', label: '文件夹' },
|
|
65
|
+
link: { glyph: 'link', tone: 'muted', label: '链接' },
|
|
66
|
+
other: { glyph: 'other', tone: 'muted', label: '特殊文件' },
|
|
67
|
+
file: { glyph: 'doc', tone: 'gray', label: '其他文件' },
|
|
68
|
+
text: { glyph: 'text', tone: 'gray', label: '文本' },
|
|
69
|
+
markdown: { glyph: 'md', tone: 'blue', label: 'Markdown 文档' },
|
|
70
|
+
typescript: { glyph: 'ts', tone: 'blue', label: 'TypeScript' },
|
|
71
|
+
javascript: { glyph: 'js', tone: 'yellow', label: 'JavaScript' },
|
|
72
|
+
python: { glyph: 'py', tone: 'green', label: 'Python' },
|
|
73
|
+
go: { glyph: 'go', tone: 'teal', label: 'Go' },
|
|
74
|
+
rust: { glyph: 'rs', tone: 'orange', label: 'Rust' },
|
|
75
|
+
swift: { glyph: 'swift', tone: 'orange', label: 'Swift' },
|
|
76
|
+
c: { glyph: 'c', tone: 'blue', label: 'C / C++' },
|
|
77
|
+
java: { glyph: 'java', tone: 'red', label: 'Java / Kotlin' },
|
|
78
|
+
ruby: { glyph: 'ruby', tone: 'red', label: 'Ruby' },
|
|
79
|
+
code: { glyph: 'code', tone: 'gray', label: '源代码' },
|
|
80
|
+
json: { glyph: 'json', tone: 'yellow', label: 'JSON' },
|
|
81
|
+
html: { glyph: 'html', tone: 'orange', label: 'HTML / XML' },
|
|
82
|
+
css: { glyph: 'css', tone: 'purple', label: 'CSS' },
|
|
83
|
+
yaml: { glyph: 'config', tone: 'pink', label: 'YAML / TOML' },
|
|
84
|
+
shell: { glyph: 'shell', tone: 'green', label: 'Shell 脚本' },
|
|
85
|
+
image: { glyph: 'image', tone: 'purple', label: '图片' },
|
|
86
|
+
video: { glyph: 'video', tone: 'pink', label: '视频' },
|
|
87
|
+
audio: { glyph: 'audio', tone: 'pink', label: '音频' },
|
|
88
|
+
pdf: { glyph: 'pdf', tone: 'red', label: 'PDF 文档' },
|
|
89
|
+
archive: { glyph: 'archive', tone: 'orange', label: '压缩包' },
|
|
90
|
+
sheet: { glyph: 'sheet', tone: 'green', label: '表格' },
|
|
91
|
+
database: { glyph: 'database', tone: 'teal', label: '数据库' },
|
|
92
|
+
font: { glyph: 'font', tone: 'gray', label: '字体' },
|
|
93
|
+
lock: { glyph: 'lock', tone: 'gray', label: '锁文件' },
|
|
94
|
+
git: { glyph: 'git', tone: 'orange', label: 'Git 配置' },
|
|
95
|
+
env: { glyph: 'env', tone: 'yellow', label: '环境变量' },
|
|
96
|
+
manifest: { glyph: 'manifest', tone: 'green', label: '包清单' },
|
|
97
|
+
license: { glyph: 'license', tone: 'gray', label: '许可证' },
|
|
30
98
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
99
|
+
|
|
100
|
+
// 文件名(小写)优先,再按扩展名(小写);都不在表里的是「其他文件」。
|
|
101
|
+
const NAMES = {
|
|
102
|
+
git: ['.gitignore', '.gitattributes', '.gitmodules', '.gitconfig', '.gitkeep', '.mailmap'],
|
|
103
|
+
env: ['.env', '.envrc'],
|
|
104
|
+
manifest: ['package.json', 'cargo.toml', 'go.mod', 'pyproject.toml', 'requirements.txt', 'gemfile', 'package.swift', 'pom.xml', 'build.gradle', 'composer.json', 'setup.py'],
|
|
105
|
+
lock: ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'cargo.lock', 'gemfile.lock', 'poetry.lock', 'go.sum', 'composer.lock', 'bun.lockb', 'podfile.lock'],
|
|
106
|
+
license: ['license', 'licence', 'copying', 'unlicense', 'license.md', 'license.txt', 'licence.md', 'licence.txt', 'copying.md', 'copying.txt'],
|
|
107
|
+
};
|
|
108
|
+
const EXTENSIONS = {
|
|
109
|
+
text: ['txt', 'text', 'log', 'rtf'],
|
|
110
|
+
markdown: ['md', 'markdown', 'mdx'],
|
|
111
|
+
typescript: ['ts', 'tsx', 'mts', 'cts'],
|
|
112
|
+
javascript: ['js', 'mjs', 'cjs', 'jsx'],
|
|
113
|
+
python: ['py', 'pyw', 'pyi'],
|
|
114
|
+
go: ['go'],
|
|
115
|
+
rust: ['rs'],
|
|
116
|
+
swift: ['swift'],
|
|
117
|
+
c: ['c', 'h', 'cc', 'cpp', 'cxx', 'hpp', 'hh'],
|
|
118
|
+
java: ['java', 'kt', 'kts'],
|
|
119
|
+
ruby: ['rb', 'erb'],
|
|
120
|
+
code: ['vue', 'scss', 'sass', 'less', 'cs', 'php', 'lua', 'sql', 'dart', 'scala', 'r', 'pl'],
|
|
121
|
+
json: ['json', 'jsonc', 'json5'],
|
|
122
|
+
html: ['html', 'htm', 'xml', 'xhtml', 'plist'],
|
|
123
|
+
css: ['css'],
|
|
124
|
+
yaml: ['yml', 'yaml', 'toml'],
|
|
125
|
+
shell: ['sh', 'bash', 'zsh', 'fish'],
|
|
126
|
+
image: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'ico', 'bmp', 'heic', 'avif', 'tif', 'tiff'],
|
|
127
|
+
video: ['mp4', 'mov', 'm4v', 'avi', 'mkv', 'webm'],
|
|
128
|
+
audio: ['mp3', 'wav', 'm4a', 'aac', 'flac', 'ogg', 'aiff'],
|
|
129
|
+
pdf: ['pdf'],
|
|
130
|
+
archive: ['zip', 'tar', 'gz', 'tgz', 'bz2', 'xz', '7z', 'rar'],
|
|
131
|
+
sheet: ['csv', 'tsv', 'xls', 'xlsx', 'numbers', 'ods'],
|
|
132
|
+
database: ['db', 'sqlite', 'sqlite3'],
|
|
133
|
+
font: ['ttf', 'otf', 'woff', 'woff2'],
|
|
134
|
+
};
|
|
135
|
+
const invert = table => new Map(Object.entries(table).flatMap(([id, keys]) => keys.map(key => [key, id])));
|
|
136
|
+
const BY_NAME = invert(NAMES), BY_EXT = invert(EXTENSIONS);
|
|
137
|
+
|
|
138
|
+
// 一个条目的种类:{ glyph, tone, label }。目录、链接、其他按种类;文件先按整个文件名,再按扩展名(点开头的名字没有扩展名)。
|
|
139
|
+
// 指向工作目录内的链接按目标的种类显示(条目自己的 kind 已经是目标的)。
|
|
140
|
+
export function kindOf({ kind, name }) {
|
|
141
|
+
if (kind === EntryKind.DIR) return KINDS.dir;
|
|
142
|
+
if (kind === EntryKind.LINK) return KINDS.link;
|
|
143
|
+
if (kind === EntryKind.OTHER) return KINDS.other;
|
|
144
|
+
const lower = String(name).toLowerCase(), dot = lower.lastIndexOf('.');
|
|
145
|
+
return KINDS[BY_NAME.get(lower) ?? (lower.startsWith('.env.') ? 'env' : undefined) ?? (dot > 0 ? BY_EXT.get(lower.slice(dot + 1)) : undefined) ?? 'file'];
|
|
40
146
|
}
|
|
41
147
|
|
|
42
|
-
//
|
|
148
|
+
// 图标名:种类表里的 glyph。
|
|
149
|
+
export const iconFor = entry => kindOf(entry).glyph;
|
|
150
|
+
|
|
151
|
+
// 图标名到色调:从种类表来,一个图标只能有一种色调(icons.test 查)。没有色调(muted)和不在表里的图标(箭头、返回这些)不带类。
|
|
152
|
+
const TONE_OF = new Map(Object.values(KINDS).filter(({ tone }) => tone !== 'muted').map(({ glyph, tone }) => [glyph, tone]));
|
|
153
|
+
|
|
154
|
+
// 造一个 SVG 元素;size 是点数,CSS 可以按平台覆盖。种类图标带 tone-* 类,颜色由 CSS 定。
|
|
43
155
|
export function icon(name, size = 16) {
|
|
44
156
|
const { fill = false, shapes } = SHAPES[name];
|
|
45
157
|
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
46
158
|
const attrs = { width: size, height: size, viewBox: '0 0 24 24', 'aria-hidden': 'true', ...(fill
|
|
47
159
|
? { fill: 'currentColor' }
|
|
48
160
|
: { fill: 'none', stroke: 'currentColor', 'stroke-width': 1.8, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }) };
|
|
161
|
+
if (TONE_OF.has(name)) attrs.class = `tone-${TONE_OF.get(name)}`;
|
|
49
162
|
for (const [key, value] of Object.entries(attrs)) svg.setAttribute(key, value);
|
|
50
|
-
for (const [tag, shape] of shapes) {
|
|
163
|
+
for (const [tag, shape, text] of shapes) {
|
|
51
164
|
const node = document.createElementNS(SVG_NS, tag);
|
|
52
165
|
for (const [key, value] of Object.entries(shape)) node.setAttribute(key, value);
|
|
166
|
+
if (text !== undefined) node.textContent = text;
|
|
53
167
|
svg.append(node);
|
|
54
168
|
}
|
|
55
169
|
return svg;
|
|
56
170
|
}
|
|
171
|
+
|
|
172
|
+
// 给测试:两张表本身,不是复制。
|
|
173
|
+
export { KINDS, SHAPES };
|
package/view/index.html
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
<div id="tree" class="pane tree" role="tree"></div>
|
|
13
13
|
<div id="preview" class="pane preview"></div>
|
|
14
14
|
<div id="notice" class="pane empty secondary"></div>
|
|
15
|
+
<div id="info" class="pane preview info"></div>
|
|
15
16
|
</main>
|
|
16
17
|
<div id="menu" class="menu" role="menu" hidden></div>
|
|
17
18
|
<div id="hint" class="hint" role="status" hidden></div>
|
package/view/info.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// 属性页:一个文件、文件夹或链接的属性。只管显示:属性和文件夹大小由页面经 stat、size 取,这里不知道聊天和目录树。
|
|
2
|
+
// 设计见 docs/design/phase-3.md 第 13.8 节。进入、返回和原生返回的声明在页面(app.mjs)。
|
|
3
|
+
import { h } from './dom.mjs';
|
|
4
|
+
import { dirSizeText, fileSizeText, formatFullDate, modeText } from './format.mjs';
|
|
5
|
+
import { icon, kindOf } from './icons.mjs';
|
|
6
|
+
import { EntryKind } from './protocol.mjs';
|
|
7
|
+
|
|
8
|
+
// el:#info 面板。选项:
|
|
9
|
+
// stat(path) → Promise<statted 回答>;size(path, signal) → Promise<sized 回答>,signal 中止后请求静默作废(连接层同时通知模块停止统计);
|
|
10
|
+
// rootPath():根目录的绝对路径,拼完整路径用;round():页面的数据轮次,每次 changed 加一;
|
|
11
|
+
// back:给了就在头部最左边放「返回」(Mac,和没有原生返回的环境)。
|
|
12
|
+
export function mountInfo(el, { stat, size, rootPath, round, back = null }) {
|
|
13
|
+
const head = h('div', { class: 'phead' }), body = h('div', { class: 'pbody ibody selectable' });
|
|
14
|
+
el.replaceChildren(head, body);
|
|
15
|
+
|
|
16
|
+
let path = null, seed = null;
|
|
17
|
+
let reply = null, failure = null; // 属性的回答;取属性失败的原因
|
|
18
|
+
let sizing = null; // 文件夹大小这一次统计:{ abort, round, phase: 'running' | 'done' | 'failed', result, message }
|
|
19
|
+
let token = 0; // 取属性的序号:新的取代旧的,旧的回答不再用
|
|
20
|
+
|
|
21
|
+
const entry = () => reply ?? seed;
|
|
22
|
+
// 指向工作目录内的链接按目标显示:种类和图标看目标的名字(链接自己叫什么不管),没有目标的(外链接)看自己。
|
|
23
|
+
const shownKind = () => {
|
|
24
|
+
const { kind, name } = entry(), target = reply?.target;
|
|
25
|
+
return kindOf({ kind, name: target ? target.slice(target.lastIndexOf('/') + 1) : name });
|
|
26
|
+
};
|
|
27
|
+
const kindText = () => {
|
|
28
|
+
const base = shownKind().label;
|
|
29
|
+
if (entry().kind === EntryKind.LINK) return `${base}(指向工作目录外或已失效)`;
|
|
30
|
+
return reply?.link ? `${base}(链接)` : base;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const row = (label, value) => h('div', { class: 'irow' }, h('span', { class: 'ilabel secondary' }, label), h('div', { class: 'ivalue' }, value));
|
|
34
|
+
const link = (label, run) => h('button', { class: 'link', onclick: run }, label);
|
|
35
|
+
const note = (title, detail, action) => h('div', { class: 'empty' }, h('p', {}, title), detail ? h('p', { class: 'secondary' }, detail) : null, action ? link(action.label, action.run) : null);
|
|
36
|
+
|
|
37
|
+
// 文件夹大小这一行:计算中、结果(上一轮的标「上次统计」)、失败。结果可能不全(没统计完),也可能是上一轮的,这两种都放「重新统计」。
|
|
38
|
+
function sizeRow() {
|
|
39
|
+
if (sizing.phase === 'running') return row('大小', '计算中…');
|
|
40
|
+
if (sizing.phase === 'failed') return row('大小', [h('span', { class: 'warn' }, `统计失败:${sizing.message}`), ' ', link('重新统计', startSize)]);
|
|
41
|
+
const old = sizing.round !== round();
|
|
42
|
+
return row('大小', [dirSizeText(sizing.result), old ? ' · 上次统计' : '', ...(old || !sizing.result.complete ? [' ', link('重新统计', startSize)] : [])]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function paint() {
|
|
46
|
+
const focused = body.contains(document.activeElement);
|
|
47
|
+
const { name, kind } = entry(), where = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '工作目录';
|
|
48
|
+
head.replaceChildren(...(back ? [h('button', { class: 'icon', 'aria-label': '返回', title: '返回', onclick: back }, icon('back', 18))] : []),
|
|
49
|
+
h('span', { class: 'pico', 'aria-hidden': 'true' }, icon(shownKind().glyph, 22)),
|
|
50
|
+
h('div', { class: 'grow' }, h('div', { class: 'pname truncate', title: path }, name), h('div', { class: 'pmeta caption secondary truncate' }, '属性')));
|
|
51
|
+
if (failure !== null) body.replaceChildren(note('读不到属性', failure, { label: '重试', run: () => load() }));
|
|
52
|
+
else if (reply === null) body.replaceChildren(h('div', { class: 'empty secondary' }, h('p', {}, '读取中…')));
|
|
53
|
+
else {
|
|
54
|
+
const { size: bytes, target, mtimeMs, birthtimeMs, mode } = reply;
|
|
55
|
+
body.replaceChildren(
|
|
56
|
+
row('种类', kindText()),
|
|
57
|
+
...(kind === EntryKind.DIR && sizing ? [sizeRow()] : Number.isInteger(bytes) ? [row('大小', fileSizeText(bytes))] : []),
|
|
58
|
+
...(target !== undefined ? [row('链接到', target)] : []),
|
|
59
|
+
row('位置', where),
|
|
60
|
+
row('完整路径', `${rootPath()}/${path}`),
|
|
61
|
+
...(Number.isInteger(mtimeMs) ? [row('修改时间', formatFullDate(mtimeMs))] : []),
|
|
62
|
+
...(Number.isInteger(birthtimeMs) ? [row('创建时间', formatFullDate(birthtimeMs))] : []),
|
|
63
|
+
...(Number.isInteger(mode) ? [row('权限', modeText(mode))] : []),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (focused) { body.tabIndex = -1; body.focus(); } // 重画换掉了聚焦的按钮:焦点留在属性页里
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 取属性。文件夹第一次取到时开始统计;之后(changed 重取)不自动重新统计,只把已有的结果标成上次统计。
|
|
70
|
+
async function load() {
|
|
71
|
+
const mine = ++token, asked = path;
|
|
72
|
+
failure = null;
|
|
73
|
+
if (reply === null) paint();
|
|
74
|
+
try {
|
|
75
|
+
const next = await stat(asked);
|
|
76
|
+
if (mine !== token) return;
|
|
77
|
+
reply = next;
|
|
78
|
+
if (next.kind === EntryKind.DIR && sizing === null) startSize();
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error.stale || mine !== token) return;
|
|
81
|
+
failure = error.message;
|
|
82
|
+
}
|
|
83
|
+
paint();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 统计文件夹:新的开始时旧的先中止(连接层告诉模块停)。关闭、换会话、根目录变化时也走中止,等待不会拖到超时。
|
|
87
|
+
function startSize() {
|
|
88
|
+
sizing?.abort.abort();
|
|
89
|
+
const abort = new AbortController(), mine = sizing = { abort, round: round(), phase: 'running' }, asked = path;
|
|
90
|
+
paint();
|
|
91
|
+
size(asked, abort.signal).then(result => Object.assign(mine, { phase: 'done', result }), error => {
|
|
92
|
+
if (error.stale) return null; // 取消、换会话:静默
|
|
93
|
+
return Object.assign(mine, { phase: 'failed', message: error.message });
|
|
94
|
+
}).then(done => { if (done && sizing === mine) paint(); });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
// seed:目录树里这一行(名字、种类),属性回来之前先画头部。
|
|
99
|
+
open(next, row) {
|
|
100
|
+
this.close();
|
|
101
|
+
path = next;
|
|
102
|
+
seed = { name: row.name, kind: row.kind };
|
|
103
|
+
load();
|
|
104
|
+
},
|
|
105
|
+
// 一轮结束:重取属性,不自动重新统计;已有的统计结果马上标成上次统计(页面的数据轮次变了)。
|
|
106
|
+
refresh() { if (path !== null) { if (reply !== null) paint(); load(); } },
|
|
107
|
+
close() {
|
|
108
|
+
sizing?.abort.abort();
|
|
109
|
+
sizing = null;
|
|
110
|
+
token++;
|
|
111
|
+
path = seed = reply = failure = null;
|
|
112
|
+
head.replaceChildren();
|
|
113
|
+
body.replaceChildren();
|
|
114
|
+
},
|
|
115
|
+
get path() { return path; },
|
|
116
|
+
};
|
|
117
|
+
}
|