@coda-console/resource-manager 0.0.0-stage → 0.3.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/view/tree.mjs ADDED
@@ -0,0 +1,169 @@
1
+ // 目录树:visibleRows 把模型压成扁平行(纯逻辑);mountTree 只画可见的那一段,上万条也不卡。
2
+ import { EntryKind } from './protocol.mjs';
3
+ import { icon, iconFor } from './icons.mjs';
4
+ import { isHidden, joinPath } from './model.mjs';
5
+
6
+ const OVERSCAN = 8;
7
+ // 长按:按住这么久、移动不超过这个距离才算。
8
+ export const HOLD_MS = 500;
9
+ export const HOLD_SLOP = 10;
10
+
11
+ // 行:{ type: 'entry' | 'status' | 'more', path, name, depth, kind, link, open, busy, failed, text }
12
+ export function visibleRows(model) {
13
+ const rows = [];
14
+ const walk = (dir, depth) => {
15
+ const state = model.dirs.get(dir);
16
+ if (!state || (!state.loaded && !state.error)) return rows.push({ type: 'status', path: dir, depth, text: '正在读取…' });
17
+ if (!state.loaded) return rows.push({ type: 'status', path: dir, depth, text: `读取失败:${state.error.message}`, retry: true });
18
+ // 刷新失败:旧条目保留,最上方加一行原因,点它重试。这一行不算内容,空目录的判断在它之后。
19
+ if (state.error) rows.push({ type: 'status', path: dir, depth, text: `更新失败:${state.error.message}`, retry: true });
20
+ const before = rows.length;
21
+ for (const entry of state.entries) {
22
+ if (!model.showHidden && isHidden(entry.name)) continue;
23
+ const path = joinPath(dir, entry.name), open = entry.kind === EntryKind.DIR && model.expanded.has(path);
24
+ rows.push({
25
+ type: 'entry', path, name: entry.name, depth, kind: entry.kind, link: entry.link === true, open,
26
+ busy: open && model.dirs.get(path)?.loading === true, failed: open ? model.dirs.get(path)?.error?.message : undefined,
27
+ });
28
+ if (open) walk(path, depth + 1);
29
+ }
30
+ if (state.next) rows.push({ type: 'more', path: dir, depth, text: state.loading ? '正在读取…' : '再显示' });
31
+ else if (rows.length === before) rows.push({ type: 'status', path: dir, depth, text: state.entries.length ? '只有隐藏项' : '空目录' });
32
+ };
33
+ walk('', 0);
34
+ return rows;
35
+ }
36
+
37
+ // 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
38
+ export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
39
+
40
+ const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
41
+
42
+ // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, path, kind) }
43
+ // draggable:触屏上拖行没有用处,还会抢滚动,关掉。
44
+ // longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
45
+ export function mountTree(el, model, handlers, { draggable = true, longPress = false } = {}) {
46
+ const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
47
+ title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
48
+ title.hidden = true;
49
+ spacer.append(win);
50
+ el.replaceChildren(title, spacer);
51
+ let rows = [], rowH = 24;
52
+
53
+ // 探针放在 spacer 里:量到的是行在真实结构里的高度。
54
+ const measure = () => {
55
+ const probe = document.createElement('div');
56
+ probe.className = 'row';
57
+ probe.style.visibility = 'hidden';
58
+ spacer.append(probe);
59
+ rowH = probe.getBoundingClientRect().height || rowH;
60
+ probe.remove();
61
+ };
62
+
63
+ // 分区标题:根目录的名字(root.path 的最后一段),不是 ready 时不显示。iPhone 上由 CSS 隐藏。
64
+ const paintTitle = () => {
65
+ const text = model.ready ? rootName(model.root.path) : '';
66
+ if (title.textContent !== text) title.textContent = text;
67
+ title.hidden = text === '';
68
+ };
69
+
70
+ // 标题在滚动容器里,把 spacer 推下去 offsetTop:行的位置按 spacer 内部算,滚动值(保存和恢复的)仍是容器的。
71
+ function paint() {
72
+ const top = el.scrollTop - (spacer.offsetTop || 0);
73
+ const first = Math.max(0, Math.floor(top / rowH) - OVERSCAN);
74
+ const last = Math.min(rows.length, Math.ceil((top + el.clientHeight) / rowH) + OVERSCAN);
75
+ win.style.transform = `translateY(${first * rowH}px)`;
76
+ win.replaceChildren(...rows.slice(first, last).map((row, i) => rowNode(row, first + i)));
77
+ }
78
+
79
+ function rowNode(row, index) {
80
+ const node = document.createElement('div');
81
+ node.className = `row ${row.type}`;
82
+ node.dataset.i = index;
83
+ node.style.cssText = `--d: ${row.depth}`; // 缩进由 CSS 按 --d 算,分隔线的起点也用它
84
+ if (row.type !== 'entry') {
85
+ node.textContent = row.text;
86
+ if (row.retry) { node.classList.add('retry'); node.title = row.text; } // 长原因在行里被截断,Mac 悬停看全
87
+ return node;
88
+ }
89
+ const dead = row.kind === EntryKind.LINK || row.kind === EntryKind.OTHER;
90
+ node.classList.add(row.kind);
91
+ if (dead) node.classList.add('dead');
92
+ if (row.path === model.selected) node.classList.add('sel');
93
+ if (row.kind === EntryKind.DIR) node.setAttribute('aria-expanded', row.open);
94
+ if (!dead && draggable) node.draggable = true;
95
+ // 一份 DOM:Mac 是 箭头、图标、名字、尾标,iPhone 的箭头用 CSS order 排到最后。
96
+ const chevron = document.createElement('span'), glyph = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
97
+ chevron.className = 'chev';
98
+ if (row.busy) chevron.textContent = '…';
99
+ else if (!dead) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
100
+ if (row.open) node.classList.add('open');
101
+ glyph.className = 'ico';
102
+ glyph.append(icon(iconFor(row)));
103
+ label.className = 'name truncate';
104
+ label.textContent = row.name;
105
+ tail.className = row.failed ? 'tail warn' : 'tail';
106
+ if (row.failed) tail.append(icon('warning', 14));
107
+ else if (row.link) tail.append(icon('turn', 14));
108
+ node.title = row.failed ? `${row.path}\n读取失败:${row.failed}` : TITLES[row.kind] ? `${row.path}\n${TITLES[row.kind]}` : row.path;
109
+ node.append(chevron, glyph, label, tail);
110
+ return node;
111
+ }
112
+
113
+ const rowOf = event => rows[event.target.closest?.('.row')?.dataset.i];
114
+
115
+ // 长按:按下只记一个等待;移动超限、抬起、取消、滚动、第二根手指都作废。
116
+ // 触发后吞掉紧接着的那次点击;没有点击跟来时,下一次按下清掉,不会误吞后面真正的点击。
117
+ let hold = null, swallow = false;
118
+ const cancelHold = () => { if (hold) { clearTimeout(hold.timer); hold = null; } };
119
+ if (longPress) {
120
+ el.addEventListener('pointerdown', event => {
121
+ cancelHold();
122
+ swallow = false;
123
+ const row = rowOf(event);
124
+ if (event.isPrimary === false || row?.type !== 'entry') return;
125
+ const { clientX: x, clientY: y, pointerId: id } = event;
126
+ hold = { id, x, y, timer: setTimeout(() => {
127
+ hold = null;
128
+ swallow = true;
129
+ handlers.menu({ clientX: x, clientY: y }, row.path);
130
+ }, HOLD_MS) };
131
+ });
132
+ el.addEventListener('pointermove', event => {
133
+ if (hold && event.pointerId === hold.id && Math.hypot(event.clientX - hold.x, event.clientY - hold.y) > HOLD_SLOP) cancelHold();
134
+ });
135
+ for (const type of ['pointerup', 'pointercancel']) el.addEventListener(type, cancelHold);
136
+ el.addEventListener('scroll', cancelHold, { passive: true });
137
+ }
138
+
139
+ el.addEventListener('click', event => {
140
+ if (swallow) { swallow = false; return; }
141
+ const row = rowOf(event);
142
+ if (!row) return;
143
+ if (row.type === 'more') return handlers.more(row.path);
144
+ if (row.type === 'status') return row.retry ? handlers.retry(row.path) : undefined;
145
+ if (row.kind === EntryKind.DIR) handlers.toggle(row.path);
146
+ else if (row.kind === EntryKind.FILE) handlers.open(row.path);
147
+ });
148
+ el.addEventListener('contextmenu', event => {
149
+ const row = rowOf(event);
150
+ if (row?.type === 'entry') { event.preventDefault(); handlers.menu(event, row.path); }
151
+ });
152
+ el.addEventListener('dragstart', event => {
153
+ const row = rowOf(event);
154
+ if (row?.type !== 'entry') return event.preventDefault();
155
+ event.dataTransfer.setData('text/plain', row.path); // 相对路径
156
+ event.dataTransfer.effectAllowed = 'copy';
157
+ });
158
+ el.addEventListener('scroll', paint, { passive: true });
159
+ new ResizeObserver(paint).observe(el);
160
+
161
+ return {
162
+ render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
163
+ remeasure() { measure(); this.render(); },
164
+ scroll: () => el.scrollTop,
165
+ scrollTo(top) { el.scrollTop = top; },
166
+ // 切会话时调用:长按的等待和吞点击的标记都属于上一个会话。
167
+ reset() { cancelHold(); swallow = false; },
168
+ };
169
+ }