@coda-console/resource-manager 0.5.0 → 0.6.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/model.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // 页面的纯状态:目录树、状态保留。不碰 DOM,请求与代次在 connection.mjs,设计见 docs/design/phase-1.md 第 6、7 节。
2
- import { DEFAULT_SORT, ErrorCode, RootState, Type, parseSort } from './protocol.mjs';
2
+ import { DEFAULT_SORT, ErrorCode, RootState, Type, parseSort, splitRelative } from './protocol.mjs';
3
3
 
4
4
  export const STATE_LIMIT = 96 * 1024;
5
5
  export const MAX_PARALLEL = 8;
@@ -11,8 +11,13 @@ const MAX_LOADED_DIRS = 500;
11
11
  const HIDDEN = new Set(['.git', '.DS_Store']);
12
12
  export const isHidden = name => HIDDEN.has(name);
13
13
  export const joinPath = (dir, name) => (dir ? `${dir}/${name}` : name);
14
+ export const parentOf = path => (path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '');
14
15
  const inside = (path, dir) => path === dir || path.startsWith(`${dir}/`);
15
16
 
17
+ // 两种视图:列表(目录树,可以展开多层)和图标(一次看一层,看的那一层是 cwd)。全局一份,和排序一样。
18
+ export const View = Object.freeze({ LIST: 'list', ICONS: 'icons' });
19
+ export const parseView = raw => (raw === View.ICONS ? View.ICONS : View.LIST);
20
+
16
21
  async function pool(items, limit, fn) {
17
22
  let index = 0;
18
23
  await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
@@ -40,9 +45,19 @@ export class Model {
40
45
  selected = null;
41
46
  showHidden = false;
42
47
  sort = DEFAULT_SORT; // 全局一份,由页面存进 persisted
48
+ view = View.LIST; // 全局一份,由页面存进 persisted
49
+ cwd = ''; // 图标视图正在看的目录,'' 是根目录;每个会话一份,连同当时的根目录存进会话状态
50
+ scope = 0; // 根目录的身份:这个会话认定「换了根目录」时加一,依赖根目录的东西(待恢复的滚动)据此作废
43
51
  #round = 0; // refresh 的轮次:新一轮开始后,旧一轮不再派发排队的目录
52
+ #nav = 0; // 导航上下文:进入、返回、换会话、回到根目录、回退各加一;一次请求只有发出时的上下文还在,才能改 cwd
53
+ #savedRoot = null; // 会话状态里存的根目录;收到这个会话第一条就绪的 root 之前,是否「同一个根目录」靠它比
54
+ #resolved = false; // 这个会话已经收到过就绪的 root
55
+ #rootPath = null; // 就绪的根目录路径,之后的变化靠它比
56
+ #quiet = false; // cwd 是从保存的状态里恢复的,还没确认过:它没了不提示
57
+ #noticed = -1; // 提示过「文件夹已不存在」的轮次,一轮只提示一次
44
58
 
45
- constructor({ link, onChange = () => {} }) { Object.assign(this, { link, onChange }); }
59
+ // onLost(path):cwd 没了、已经退到上一层时调用一次(恢复时不调用)。
60
+ constructor({ link, onChange = () => {}, onLost = () => {} }) { Object.assign(this, { link, onChange, onLost }); }
46
61
 
47
62
  get ready() { return this.root?.state === RootState.READY; }
48
63
 
@@ -63,6 +78,12 @@ export class Model {
63
78
  this.session = session ?? null;
64
79
  this.root = null;
65
80
  this.dirs = new Map();
81
+ this.#nav++;
82
+ this.#resolved = false;
83
+ this.#rootPath = null;
84
+ this.#savedRoot = typeof saved?.root === 'string' ? saved.root : null;
85
+ this.cwd = typeof saved?.cwd === 'string' ? saved.cwd : '';
86
+ this.#quiet = this.cwd !== '';
66
87
  this.wanted = new Map(Object.entries(saved?.loaded ?? {}).map(([path, count]) => [path, Math.min(count, RESTORE_LIMIT)]));
67
88
  const paths = (saved?.expanded ?? []).filter(path => path !== '');
68
89
  this.expanded = new Set(paths.filter(path => !path.includes('/') || paths.includes(path.slice(0, path.lastIndexOf('/')))));
@@ -73,11 +94,26 @@ export class Model {
73
94
  // 只换根目录状态,不动会话状态;ready 时重新取全部。
74
95
  onRoot({ generation, state, path }) {
75
96
  if (generation !== this.link.generation) return null;
97
+ if (state === RootState.READY) this.#identify(path);
76
98
  this.root = { state, path };
77
99
  this.onChange();
78
100
  return state === RootState.READY ? this.refresh() : Promise.resolve();
79
101
  }
80
102
 
103
+ // 根目录有没有换:重建页面后 model.root 是 null,不能靠「路径变了」,否则每次重建都算换了。
104
+ // 这个会话第一条就绪的 root 和保存的根目录比(没存根目录的旧状态当作没换),之后的 root 和上一次比。换了就全部回到根目录。
105
+ #identify(path) {
106
+ const changed = this.#resolved ? this.#rootPath !== path : this.#savedRoot !== null && this.#savedRoot !== path;
107
+ this.#resolved = true;
108
+ this.#rootPath = path;
109
+ if (!changed) return;
110
+ this.scope++;
111
+ this.#nav++;
112
+ this.cwd = '';
113
+ this.#quiet = false;
114
+ this.wanted = new Map();
115
+ }
116
+
81
117
  onChanged({ generation }) {
82
118
  return generation === this.link.generation && this.ready ? this.refresh() : null;
83
119
  }
@@ -87,7 +123,40 @@ export class Model {
87
123
  refresh({ cap = Infinity } = {}) {
88
124
  if (!this.ready) return Promise.resolve();
89
125
  const { generation } = this.link, round = ++this.#round;
90
- return pool(['', ...this.expanded], MAX_PARALLEL, path => (round === this.#round ? this.#reload(path, generation, { cap }) : undefined));
126
+ return pool(this.requested(), MAX_PARALLEL, path => (round === this.#round ? this.#reload(path, generation, { cap }) : undefined));
127
+ }
128
+
129
+ // 一轮要取的目录:列表是根和展开的,图标是根和 cwd;同一路径只取一次。
130
+ requested() { return this.view === View.ICONS ? [...new Set(['', this.cwd])] : ['', ...this.expanded]; }
131
+
132
+ // 换视图:马上按新的集合刷新,作废旧的排队轮次;回来之前显示已有的内容,没有就显示读取中。展开状态不动。
133
+ setView(view) {
134
+ const next = parseView(view);
135
+ if (next === this.view) return Promise.resolve();
136
+ this.view = next;
137
+ this.onChange();
138
+ return this.refresh();
139
+ }
140
+
141
+ // 图标视图进入一层:cwd 换成它并加载。返回 levels 层(缺省一层,到根为止)。
142
+ enter(path) {
143
+ this.cwd = path;
144
+ return this.#go();
145
+ }
146
+
147
+ leave(levels = 1) {
148
+ let next = this.cwd;
149
+ for (let i = 0; i < levels && next !== ''; i++) next = parentOf(next);
150
+ if (next === this.cwd) return Promise.resolve();
151
+ this.cwd = next;
152
+ return this.#go();
153
+ }
154
+
155
+ #go() {
156
+ this.#nav++;
157
+ this.#quiet = false;
158
+ this.onChange();
159
+ return this.retry(this.cwd);
91
160
  }
92
161
 
93
162
  // 换排序:已加载的目录按新顺序重新取,取够现在的条数,这次最多 RESTORE_LIMIT 条;每个目录一次替换。
@@ -105,7 +174,7 @@ export class Model {
105
174
  if (this.link.generation !== generation) return; // 排队时会话已换,旧任务不碰新会话
106
175
  let dir = this.dirs.get(path);
107
176
  if (!dir) this.dirs.set(path, dir = { entries: [], next: null, loaded: false, loading: false, error: null, token: 0, pages: 0, sortFallback: false });
108
- const token = ++dir.token, { sort } = this;
177
+ const token = ++dir.token, { sort } = this, nav = this.#nav;
109
178
  const want = more ? dir.entries.length + 1 : Math.min(Math.max(dir.entries.length, this.wanted.get(path) ?? 0), cap);
110
179
  dir.loading = true;
111
180
  this.onChange();
@@ -130,27 +199,50 @@ export class Model {
130
199
  pages++;
131
200
  } while (pages === 0 || (next && entries.length < want));
132
201
  if (!current()) return;
133
- Object.assign(dir, { entries, next, loaded: true, error: null, pages, sortFallback });
202
+ Object.assign(dir, { entries, next, loaded: true, error: null, pages, sortFallback, sort });
134
203
  this.wanted.delete(path); // 成功后才删:取页失败后重试还要取够保存的条数
204
+ if (path === this.cwd) this.#quiet = false;
135
205
  } catch (error) {
136
206
  if (error.stale || !current()) return;
137
- if (error.code === ErrorCode.NOT_FOUND && path !== '') this.#drop(path);
138
- else if (error.code !== ErrorCode.NO_ROOT) dir.error = { code: error.code, message: error.message };
207
+ if (error.code === ErrorCode.NOT_FOUND && path !== '') this.#lost(path, nav);
208
+ else if (error.code !== ErrorCode.NO_ROOT) {
209
+ dir.error = { code: error.code, message: error.message };
210
+ if (path === this.cwd) this.#quiet = false;
211
+ }
139
212
  } finally {
140
213
  if (current()) dir.loading = false;
141
214
  this.onChange();
142
215
  }
143
216
  }
144
217
 
145
- #drop(path) {
146
- for (const key of [...this.dirs.keys()]) if (inside(key, path)) this.dirs.delete(key);
218
+ // 目录没了(not_found):所有列目录入口都走这里。先清掉失效的子树;cwd 在里面、而且发出请求以来没有导航过,就退到上一层。
219
+ // 退到哪一层靠新发的请求:它再 not_found 又走到这里,所以每级只试一次;别的错误停在那一级,显示失败行和重试。
220
+ // 清掉子树后原目录对象没了,原请求的检查必然为假,但回退不靠它:回退看的是导航上下文,每一级有自己的请求。
221
+ #lost(path, nav) {
222
+ const covers = nav === this.#nav && inside(this.cwd, path);
223
+ // 迟到的上层失败(发出后用户已经进了别处):清它的子树,但正在加载的 cwd 的条目留着,不然它的回答提交不上。
224
+ this.#drop(path, covers ? null : this.cwd);
225
+ if (!covers) return;
226
+ this.#nav++;
227
+ this.cwd = parentOf(path);
228
+ if (!this.#quiet && this.#noticed !== this.#round && this.view === View.ICONS) {
229
+ this.#noticed = this.#round;
230
+ this.onLost(path);
231
+ }
232
+ this.onChange();
233
+ this.retry(this.cwd);
234
+ }
235
+
236
+ #drop(path, keep = null) {
237
+ for (const key of [...this.dirs.keys()]) if (key !== keep && inside(key, path)) this.dirs.delete(key);
147
238
  for (const key of [...this.expanded]) if (inside(key, path)) this.expanded.delete(key);
148
239
  }
149
240
 
150
241
  toggle(path) {
151
242
  if (this.expanded.has(path)) {
152
243
  for (const key of [...this.expanded]) if (inside(key, path)) this.expanded.delete(key);
153
- for (const key of [...this.dirs.keys()]) if (key !== '' && inside(key, path)) this.dirs.delete(key);
244
+ // cwd 的条目留着:图标视图还在看它
245
+ for (const key of [...this.dirs.keys()]) if (key !== '' && key !== this.cwd && inside(key, path)) this.dirs.delete(key);
154
246
  this.onChange();
155
247
  } else {
156
248
  this.expanded.add(path);
@@ -164,7 +256,9 @@ export class Model {
164
256
  async loadMore(path) {
165
257
  const dir = this.dirs.get(path), { generation } = this.link;
166
258
  if (!dir?.next || dir.loading || !this.ready) return;
167
- const token = ++dir.token;
259
+ // 游标是按另一种排序取的(换排序时这个目录没重建过):不用它,从第一页重取,条数比现在多一条。
260
+ if (dir.sort !== this.sort) return this.#reload(path, generation, { more: true, expired: true });
261
+ const token = ++dir.token, nav = this.#nav;
168
262
  dir.loading = true;
169
263
  this.onChange();
170
264
  const current = () => this.link.generation === generation && this.dirs.get(path) === dir && dir.token === token;
@@ -177,6 +271,7 @@ export class Model {
177
271
  if (error.stale || !current()) return;
178
272
  // 游标过期:从第一页重取,条数比现在多一条,所以还会接着往下取。重取自己管 loading。
179
273
  if (error.code === ErrorCode.CURSOR_EXPIRED) return this.#reload(path, generation, { more: true, expired: true });
274
+ if (error.code === ErrorCode.NOT_FOUND && path !== '') return this.#lost(path, nav);
180
275
  dir.error = { code: error.code, message: error.message };
181
276
  } finally {
182
277
  if (current()) dir.loading = false;
@@ -190,7 +285,9 @@ export class Model {
190
285
  snapshot() {
191
286
  const loaded = {};
192
287
  for (const [path, dir] of this.dirs) if (dir.pages > 1 && dir.entries.length > 0) loaded[path] = dir.entries.length;
193
- return { expanded: [...this.expanded], selected: this.selected, loaded };
288
+ // 还没收到这个会话就绪的 root 时,cwd 和根目录还是保存的那一份,原样存回去,不能被重建途中的一次保存冲掉。
289
+ const root = this.#resolved ? this.#rootPath : this.#savedRoot;
290
+ return { expanded: [...this.expanded], selected: this.selected, loaded, cwd: this.cwd, ...(root === null ? {} : { root }) };
194
291
  }
195
292
  }
196
293
 
@@ -203,12 +300,16 @@ const strings = (value, max) => (Array.isArray(value) ? value.filter(s => typeof
203
300
  const parseLoaded = raw => Object.fromEntries(Object.entries(raw && typeof raw === 'object' ? raw : {})
204
301
  .filter(([path, count]) => path.length <= 1024 && Number.isSafeInteger(count) && count > 0 && count <= MAX_LOADED).slice(0, MAX_LOADED_DIRS));
205
302
 
303
+ // cwd 要是后端认的相对路径(规则同 lib/paths.mjs),而且写法规整:model 里的路径都是 joinPath 拼出来的,不带空段和 `.`。
304
+ const cleanRelative = path => { const { segments } = splitRelative(path); return segments !== undefined && segments.join('/') === path; };
305
+
206
306
  // 读回来的值可能是别的版本或坏的,只认自己的形状。
207
307
  export function parseState(raw) {
208
- const state = { v: 1, seq: 0, hidden: false, sort: DEFAULT_SORT, sessions: {} };
308
+ const state = { v: 1, seq: 0, hidden: false, sort: DEFAULT_SORT, view: View.LIST, sessions: {} };
209
309
  if (!raw || typeof raw !== 'object' || raw.v !== 1) return state;
210
310
  state.hidden = raw.hidden === true;
211
311
  state.sort = parseSort(raw.sort) ?? DEFAULT_SORT;
312
+ state.view = parseView(raw.view);
212
313
  state.seq = Number.isSafeInteger(raw.seq) ? raw.seq : 0;
213
314
  for (const [id, item] of Object.entries(raw.sessions ?? {})) {
214
315
  if (!item || typeof item !== 'object') continue;
@@ -217,8 +318,13 @@ export function parseState(raw) {
217
318
  at: Number.isSafeInteger(item.at) ? item.at : 0, expanded: strings(item.expanded, 5000),
218
319
  selected: typeof item.selected === 'string' && item.selected.length <= 1024 ? item.selected : null,
219
320
  // 滚动位置是按当时的排序存的;旧版本没有这个字段,那时一定是默认排序。
220
- scroll: { tree: num(scroll.tree), top: num(scroll.top), left: num(scroll.left), sort: parseSort(scroll.sort) ?? DEFAULT_SORT },
321
+ // 两种视图各存各的位置和当时的排序:tree 和 sort 是列表的,grid 和 gridSort 是图标的(旧版本没有后两个)。
322
+ scroll: { tree: num(scroll.tree), top: num(scroll.top), left: num(scroll.left), sort: parseSort(scroll.sort) ?? DEFAULT_SORT,
323
+ grid: num(scroll.grid), gridSort: parseSort(scroll.gridSort) ?? DEFAULT_SORT },
221
324
  };
325
+ // cwd 和存它时的根目录:超长或形状不对当作没有,回到根目录。旧版本没有这两个字段。
326
+ const path = value => (typeof value === 'string' && value.length <= 1024 ? value : undefined);
327
+ if (path(item.cwd) !== undefined && path(item.root) !== undefined && cleanRelative(item.cwd)) Object.assign(state.sessions[id], { cwd: item.cwd, root: item.root });
222
328
  const loaded = parseLoaded(item.loaded);
223
329
  if (Object.keys(loaded).length > 0) state.sessions[id].loaded = loaded;
224
330
  }
package/view/protocol.mjs CHANGED
@@ -60,6 +60,20 @@ export function imageType(name) {
60
60
  return dot < 0 ? null : IMAGE_TYPES[name.slice(dot + 1).toLowerCase()] ?? null;
61
61
  }
62
62
 
63
+ // 相对路径的规则只在这一处:模块(lib/paths.mjs)校验请求,页面恢复保存的 cwd 时用同一个函数。
64
+ // 空串是根目录;拒绝过长、控制字符、绝对路径和 `..`。返回 { segments }(去掉空段和 `.`)或 { problem }(给人看的原因)。
65
+ export const MAX_PATH = 4096;
66
+ const CONTROL = /[\u0000-\u001f\u007f]/;
67
+ export const hasControl = text => CONTROL.test(text);
68
+ export function splitRelative(path) {
69
+ if (typeof path !== 'string' || path.length > MAX_PATH) return { problem: '路径无效' };
70
+ if (hasControl(path)) return { problem: '路径含控制字符' };
71
+ if (path.startsWith('/')) return { problem: '只接受相对路径' };
72
+ const segments = path.split('/').filter(part => part !== '' && part !== '.');
73
+ if (segments.includes('..')) return { problem: '路径不能含 ..' };
74
+ return { segments };
75
+ }
76
+
63
77
  export const messageSize = message => JSON.stringify({ message }).length;
64
78
 
65
79
  // 带 code 的错误,模块里到处抛,主模块转成回答里的 error。
@@ -0,0 +1,57 @@
1
+ // 滚动位置的两样记录:待恢复的位置(页面重建、换会话、返回上一层之后,等内容到了才用)和上层的位置(图标视图进入子目录前的位置,只在内存里)。
2
+ // 不碰 DOM:滚动的元素由页面通过 panes 给。设计见 docs/design/phase-3.md 第 14.5 节。
3
+ import { View } from './model.mjs';
4
+
5
+ export const TRAIL_LIMIT = 32;
6
+
7
+ // 图标视图进入子目录之前,上层的滚动位置:按目录存,最多 limit 层,满了丢最久没用的。不存进 setState:页面卸载后就没了。
8
+ export function createTrail(limit = TRAIL_LIMIT) {
9
+ const tops = new Map();
10
+ return {
11
+ remember(dir, top) { tops.delete(dir); tops.set(dir, top); while (tops.size > limit) tops.delete(tops.keys().next().value); },
12
+ // 取出并删掉:回到上一层时用一次。
13
+ take(dir) { const top = tops.get(dir); tops.delete(dir); return top; },
14
+ clear() { tops.clear(); },
15
+ get size() { return tops.size; },
16
+ };
17
+ }
18
+
19
+ const VIEW = { tree: View.LIST, grid: View.ICONS };
20
+
21
+ // 待恢复的滚动:每个视图(tree、grid)最多一份。不能只等一次 refresh() 完成:旧的一轮完成不等于内容到了,太早设置会被夹回顶部。
22
+ // 一份恢复绑定会话、根目录的身份(model.scope)、排序和目录(图标视图是 cwd),任何一个对不上就作废;
23
+ // 绑定的视图不是正在看的视图时等着,切过去才用。目标目录的内容到了才用,条数不够时浏览器夹到能到的最大值;加载失败就结束;用户自己滚动了就放弃。
24
+ // panes:{ tree, grid },各有 scroll() 和 scrollTo(top)。
25
+ export function createRestore(model, panes) {
26
+ const plans = { tree: null, grid: null };
27
+ // 内容到了:列表是根和展开的目录,图标是 cwd;每个都已经加载(或失败了,失败也算有结果),空着的、没发出的不算。
28
+ const dirsOf = key => (key === 'grid' ? [model.cwd] : ['', ...model.expanded]);
29
+ const bound = (key, plan) => plan.session === model.session && plan.scope === model.scope && plan.sort === model.sort && (key === 'tree' || plan.dir === model.cwd);
30
+
31
+ return {
32
+ // 现在的滚动位置就是用户没动过的基准:调用前页面先把滚动元素滚到顶部。
33
+ plan(key, top) {
34
+ plans[key] = { session: model.session, scope: model.scope, sort: model.sort, dir: key === 'grid' ? model.cwd : '', top, at: panes[key].scroll() };
35
+ },
36
+ drop(key) { plans[key] = null; },
37
+ clear() { plans.tree = plans.grid = null; },
38
+ // 还在等的位置(保存状态时用它,别存成还没恢复的 0);对不上的不算。
39
+ top(key) { return plans[key] && bound(key, plans[key]) ? plans[key].top : null; },
40
+ // 滚动事件:位置不是计划时的基准,就是用户自己滚的,不再跳回去。
41
+ scrolled(key) { if (plans[key] && panes[key].scroll() !== plans[key].at) plans[key] = null; },
42
+ // 每次重画之后调用。
43
+ settle() {
44
+ for (const key of Object.keys(plans)) {
45
+ const plan = plans[key];
46
+ if (!plan) continue;
47
+ if (!bound(key, plan)) { plans[key] = null; continue; }
48
+ if (model.view !== VIEW[key]) continue;
49
+ const states = dirsOf(key).map(dir => model.dirs.get(dir));
50
+ if (key === 'grid' && states[0] && !states[0].loaded && states[0].error) { plans[key] = null; continue; }
51
+ if (!states.every(state => state && (state.loaded || state.error))) continue;
52
+ plans[key] = null;
53
+ panes[key].scrollTo(plan.top);
54
+ }
55
+ },
56
+ };
57
+ }
package/view/style.css CHANGED
@@ -40,7 +40,7 @@ p { margin: 0; }
40
40
  .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
41
41
  [hidden] { display: none !important; }
42
42
 
43
- /* 三个面板叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。 */
43
+ /* 面板(目录树、图标、预览、说明、属性页)叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。 */
44
44
  #stage { position: relative; flex: 1 1 auto; min-height: 0; }
45
45
  .pane { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
46
46
  .pane.off { visibility: hidden; pointer-events: none; }
@@ -94,13 +94,13 @@ p { margin: 0; }
94
94
 
95
95
  /* 预览:头部永远占一行名字加一行说明,加载和失败只换下面的内容。 */
96
96
  .preview { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归文件页的工具条 */
97
- .phead button { flex: none; white-space: nowrap; }
98
- .phead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
99
- .pico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
100
- .pico svg { display: block; }
97
+ .phead button, .ghead button { flex: none; white-space: nowrap; }
98
+ .phead, .ghead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
99
+ .pico, .ghico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
100
+ .pico svg, .ghico svg { display: block; }
101
101
  button.icon { flex: none; display: inline-grid; place-items: center; width: 28px; height: 28px; padding: 0; border-radius: var(--coda-radius, 6px); color: var(--coda-secondary, #6e6e73); }
102
102
  button.icon svg { display: block; }
103
- .pname { font-weight: 600; }
103
+ .pname, .gname-head { font-weight: 600; }
104
104
  .pbody { flex: 1 1 auto; min-height: 0; overflow: auto; }
105
105
  .code { display: flex; width: max-content; min-width: 100%; font: .9em/1.55 var(--coda-mono, ui-monospace, Menlo, monospace); }
106
106
  .code pre { margin: 0; font: inherit; }
@@ -115,14 +115,19 @@ button.icon svg { display: block; }
115
115
  background: var(--coda-card, var(--coda-background, #fff));
116
116
  /* 高度有上限:内容多(长路径加五项)时在菜单里滚动,不超出安全区;位置由 menu.mjs 夹在安全区内。 */
117
117
  max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 8px); overflow-y: auto; overscroll-behavior: contain; }
118
- .menu button { display: flex; align-items: center; gap: 6px; width: 100%; padding: 5px 10px; border-radius: var(--coda-radius, 6px); }
119
- .check { flex: none; display: grid; place-items: center; width: 16px; color: var(--coda-accent, #0a84ff); }
118
+ .menu button { display: flex; align-items: center; gap: 4px; width: 100%; padding: 5px 8px; border-radius: var(--coda-radius, 6px); }
119
+ .check { flex: none; display: grid; place-items: center; width: 14px; color: var(--coda-accent, #0a84ff); }
120
120
  .check svg { display: block; }
121
+ .check.back { color: var(--coda-secondary, #6e6e73); }
122
+ /* 带二级菜单的项:右边的箭头;展开着的和悬停同色。 */
123
+ .sub-arrow { flex: none; display: grid; place-items: center; margin-left: auto; padding-left: 12px; color: var(--coda-secondary, #6e6e73); }
124
+ .sub-arrow svg { display: block; }
125
+ .menu button[aria-expanded="true"] { background: var(--coda-selection, var(--hover)); }
121
126
  /* 菜单宽度随内容,上限见 .menu:路径超过上限折行,大小 · 完整修改时间不折。 */
122
- .menu-path { padding: 4px 10px 6px; overflow-wrap: anywhere; }
123
- .menu-info { padding: 0 10px 6px; margin-top: -4px; white-space: nowrap; }
127
+ .menu-path { padding: 4px 8px 6px; overflow-wrap: anywhere; }
128
+ .menu-info { padding: 0 8px 6px; margin-top: -4px; white-space: nowrap; }
124
129
  .menu-sep { margin: 4px 6px; border: 0; border-top: 1px solid var(--line); }
125
- .menu-head { padding: 4px 10px 2px; font-weight: 600; }
130
+ .menu-head { padding: 4px 8px 2px; font-weight: 600; }
126
131
  .pmeta.warn { color: var(--coda-orange, #b25000); }
127
132
 
128
133
  /* 底部的短提示:普通的是前景色(成功),.warn 是橙色(失败)。 */
@@ -140,11 +145,34 @@ button.icon svg { display: block; }
140
145
  .coda-ios .irow { flex-direction: column; gap: 0; padding: 8px 4px; }
141
146
  .coda-ios .ilabel { width: auto; text-align: left; font-size: .85em; }
142
147
 
148
+ /* 图标视图:头部同预览(样式和 .phead 一样,类名另取,页面和 harness 里按 .phead 查预览头部的不会撞上),下面一个滚动的 .gbody,只画可见的一段(grid.mjs 量 .cell 的宽和高)。
149
+ 单元格宽度固定,Mac 约 72 点、iPhone 约 88 点;列数按容器宽度算,每列的宽度平分余下的空间,单元格在列里居中。说明项占满一整行。 */
150
+ :root { --cell-w: 72px; --gico: 32px; --gname-h: calc(var(--coda-font-size, 13px) * 2.4); --note-h: calc(var(--coda-font-size, 13px) * 2.4); }
151
+ .coda-ios { --cell-w: 88px; --gico: 40px; --note-h: 44px; }
152
+ .grid { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归 .gbody */
153
+ .gbody { flex: 1 1 auto; min-height: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
154
+ .gspacer { position: relative; margin: 8px 0; }
155
+ .gwin { position: absolute; left: 0; right: 0; top: 0; display: grid; justify-items: center; }
156
+ .gi { cursor: default; }
157
+ /* 单元格是固定高度:图标、两行名字(.gname 的行高 1.3、字号 .92em,两行 2.392 个字号,--gname-h 取 2.4)、上下各 4 点,刚好放下,只有一行名字时不多留空白。 */
158
+ .cell { display: flex; flex-direction: column; align-items: center; gap: 2px; width: var(--cell-w); height: calc(var(--gico) + 2px + var(--gname-h) + 8px); padding: 4px; border-radius: var(--coda-radius, 6px); }
159
+ .cell.sel { background: var(--coda-selection, var(--hover)); }
160
+ .cell .gico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
161
+ .cell .gico svg { display: block; width: var(--gico); height: var(--gico); }
162
+ .cell.dir .gico { color: var(--coda-accent, #0a84ff); }
163
+ .cell.dead .gname, .cell.dead .gico { color: var(--coda-tertiary, #a1a1a6); }
164
+ .gname { max-width: 100%; text-align: center; font-size: .92em; line-height: 1.3; overflow: hidden; overflow-wrap: anywhere; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
165
+ /* 说明行(空目录、读取中……)一行高,比单元格矮;grid.mjs 分别量两种高度,布局映射按两种算。 */
166
+ .gnote { grid-column: 1 / -1; justify-self: stretch; height: var(--note-h); line-height: var(--note-h); padding: 0 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: center; color: var(--coda-secondary, #6e6e73); }
167
+ .gnote.more, .gnote.retry { color: var(--coda-accent, #0a84ff); cursor: pointer; }
168
+ .coda-ios .cell { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单 */
169
+ .coda-ios .cell:active { background: var(--hover); } /* 按下:和列表的行一样 */
170
+
143
171
  /* 悬停只在有鼠标时:触摸后不会留着。 */
144
172
  @media (hover: hover) {
145
173
  .bar button:hover, button.icon:hover { background: var(--hover); }
146
- .row.entry:hover { background: var(--hover); }
147
- .row.entry.sel:hover { background: var(--coda-selection, var(--hover)); }
174
+ .row.entry:hover, .cell:hover { background: var(--hover); }
175
+ .row.entry.sel:hover, .cell.sel:hover { background: var(--coda-selection, var(--hover)); }
148
176
  .menu button:hover { background: var(--coda-selection, var(--hover)); }
149
177
  }
150
178
 
@@ -175,11 +203,12 @@ button.icon svg { display: block; }
175
203
  .coda-ios .bar button.icon { padding: 0; justify-content: center; }
176
204
  .coda-ios .bar.bottom { padding: 0 8px var(--safe-bottom); }
177
205
  .coda-ios .bar.bottom button { flex: 1 1 0; justify-content: center; } /* 横排单行:横屏只有 390 点高 */
178
- .coda-ios .phead { padding: 4px 12px; gap: 4px; }
179
- .coda-ios .phead button { padding: 0 8px; }
206
+ .coda-ios .phead, .coda-ios .ghead { padding: 4px 12px; gap: 4px; }
207
+ .coda-ios .phead button, .coda-ios .ghead button { padding: 0 8px; }
180
208
  .coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
181
- .coda-ios .menu button { padding: 0 12px; }
182
- .coda-ios .menu-head { padding: 6px 12px 2px; }
209
+ .coda-ios .menu button { padding: 0 10px; }
210
+ .coda-ios .menu button:active { background: var(--coda-selection, var(--hover)); }
211
+ .coda-ios .menu-head { padding: 6px 10px 2px; }
183
212
 
184
213
  /* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
185
214
  .bar .exit, .bar.exit-only { display: none; } /* 只有返回的工具条(属性页):返回不显示,整条也不占高度 */
package/view/tree.mjs CHANGED
@@ -1,13 +1,11 @@
1
1
  // 目录树:visibleRows 把模型压成扁平行(纯逻辑);mountTree 只画可见的那一段,上万条也不卡。
2
2
  import { EntryKind, SortKey } from './protocol.mjs';
3
3
  import { dateText, infoText, sizeText, subText } from './format.mjs';
4
+ import { mountGestures } from './gestures.mjs';
4
5
  import { icon, iconFor } from './icons.mjs';
5
6
  import { isHidden, joinPath } from './model.mjs';
6
7
 
7
8
  const OVERSCAN = 8;
8
- // 长按:按住这么久、移动不超过这个距离才算。
9
- export const HOLD_MS = 500;
10
- export const HOLD_SLOP = 10;
11
9
 
12
10
  // 行:{ type: 'entry' | 'status' | 'more', path, name, depth, kind, link, size, mtimeMs, open, busy, failed, text };size、mtimeMs 读不到时没有
13
11
  export function visibleRows(model) {
@@ -40,7 +38,7 @@ export function visibleRows(model) {
40
38
  // 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
41
39
  export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
42
40
 
43
- const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
41
+ export const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
44
42
 
45
43
  // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, row) }
46
44
  // menu 收到整行(路径、种类、大小、修改时间)。
@@ -135,50 +133,18 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
135
133
  return node;
136
134
  }
137
135
 
138
- const rowOf = event => rows[event.target.closest?.('.row')?.dataset.i];
139
-
140
- // 长按:按下只记一个等待;移动超限、抬起、取消、滚动、第二根手指都作废。
141
- // 触发后吞掉紧接着的那次点击;没有点击跟来时,下一次按下清掉,不会误吞后面真正的点击。
142
- let hold = null, swallow = false;
143
- const cancelHold = () => { if (hold) { clearTimeout(hold.timer); hold = null; } };
144
- if (longPress) {
145
- el.addEventListener('pointerdown', event => {
146
- cancelHold();
147
- swallow = false;
148
- const row = rowOf(event);
149
- if (event.isPrimary === false || row?.type !== 'entry') return;
150
- const { clientX: x, clientY: y, pointerId: id } = event;
151
- hold = { id, x, y, timer: setTimeout(() => {
152
- hold = null;
153
- swallow = true;
154
- handlers.menu({ clientX: x, clientY: y }, row);
155
- }, HOLD_MS) };
156
- });
157
- el.addEventListener('pointermove', event => {
158
- if (hold && event.pointerId === hold.id && Math.hypot(event.clientX - hold.x, event.clientY - hold.y) > HOLD_SLOP) cancelHold();
159
- });
160
- for (const type of ['pointerup', 'pointercancel']) el.addEventListener(type, cancelHold);
161
- el.addEventListener('scroll', cancelHold, { passive: true });
162
- }
163
-
164
- el.addEventListener('click', event => {
165
- if (swallow) { swallow = false; return; }
166
- const row = rowOf(event);
167
- if (!row) return;
168
- if (row.type === 'more') return handlers.more(row.path);
169
- if (row.type === 'status') return row.retry ? handlers.retry(row.path) : undefined;
170
- if (row.kind === EntryKind.DIR) handlers.toggle(row.path);
171
- else if (row.kind === EntryKind.FILE) handlers.open(row.path);
172
- });
173
- el.addEventListener('contextmenu', event => {
174
- const row = rowOf(event);
175
- if (row?.type === 'entry') { event.preventDefault(); handlers.menu(event, row); }
176
- });
177
- el.addEventListener('dragstart', event => {
178
- const row = rowOf(event);
179
- if (row?.type !== 'entry') return event.preventDefault();
180
- event.dataTransfer.setData('text/plain', row.path); // 相对路径
181
- event.dataTransfer.effectAllowed = 'copy';
136
+ const gestures = mountGestures(el, {
137
+ hit: event => rows[event.target.closest?.('.row')?.dataset.i],
138
+ pressable: row => row.type === 'entry',
139
+ activate: row => {
140
+ if (row.type === 'more') return handlers.more(row.path);
141
+ if (row.type === 'status') return row.retry ? handlers.retry(row.path) : undefined;
142
+ if (row.kind === EntryKind.DIR) handlers.toggle(row.path);
143
+ else if (row.kind === EntryKind.FILE) handlers.open(row.path);
144
+ },
145
+ menu: (event, row) => handlers.menu(event, row),
146
+ dragText: row => row.path,
147
+ longPress,
182
148
  });
183
149
  el.addEventListener('scroll', paint, { passive: true });
184
150
  new ResizeObserver(paint).observe(el);
@@ -189,6 +155,6 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
189
155
  scroll: () => el.scrollTop,
190
156
  scrollTo(top) { el.scrollTop = top; },
191
157
  // 切会话时调用:长按的等待和吞点击的标记都属于上一个会话。
192
- reset() { cancelHold(); swallow = false; },
158
+ reset() { gestures.reset(); },
193
159
  };
194
160
  }