@coda-console/resource-manager 0.7.1 → 0.8.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/app.mjs CHANGED
@@ -1,17 +1,23 @@
1
- // 资源管理器的页面入口:消息收发、状态保留、工具栏和「更多」菜单、图标视图的进入和返回。状态在 model.mjs,渲染在 tree.mjs、grid.mjs、preview.mjs 和 info.mjs,菜单和提示在 menu.mjs 和 hint.mjs,滚动位置的恢复在 restore.mjs。
1
+ // 资源管理器的页面入口:消息收发、状态保留、工具栏和「更多」菜单、图标视图的进入和返回、搜索。状态在 model.mjs,渲染在 tree.mjs、grid.mjs、preview.mjs 和 info.mjs,菜单和提示在 menu.mjs 和 hint.mjs,滚动位置的恢复在 restore.mjs,
2
+ // 文件名搜索的状态在 search.mjs、搜索框在 searchbar.mjs、「在目录中显示」在 locate.mjs。
2
3
  import { EXIT, paintBar } from './bar.mjs';
3
4
  import { createChat } from './chat.mjs';
4
5
  import { connect } from './connection.mjs';
5
6
  import { infoText, sortName } from './format.mjs';
7
+ import { h } from './dom.mjs';
6
8
  import { mountGrid } from './grid.mjs';
7
9
  import { mountHint } from './hint.mjs';
8
10
  import { mountInfo } from './info.mjs';
11
+ import { createLocator } from './locate.mjs';
9
12
  import { mountMenu } from './menu.mjs';
10
13
  import { Model, STATE_LIMIT, View, noticeText, parseState, putSession, trimState } from './model.mjs';
11
14
  import { showNotice } from './notice.mjs';
12
15
  import { mountPreview } from './preview.mjs';
16
+ import { mountPress, onMotionChange, reducedMotion } from './press.mjs';
13
17
  import { EntryKind, RootState, SortKey, Type } from './protocol.mjs';
14
18
  import { createRestore, createTrail } from './restore.mjs';
19
+ import { createSearch } from './search.mjs';
20
+ import { mountSearchbar } from './searchbar.mjs';
15
21
  import { browseAction, previewAction } from './keys.mjs';
16
22
  import { mountTree, rootName } from './tree.mjs';
17
23
 
@@ -32,7 +38,10 @@ model.showHidden = persisted.hidden;
32
38
  model.sort = persisted.sort;
33
39
  model.view = persisted.view;
34
40
  const hint = mountHint($('hint'));
35
- const menu = mountMenu($('menu'), $('submenu'), { flyout: !isIOS }); // Mac 的二级菜单在左边弹出,iPhone 在菜单里切到下一层
41
+ const menu = mountMenu($('menu'), $('submenu'), { flyout: !isIOS, animate: isIOS }); // Mac 的二级菜单在左边弹出,iPhone 在菜单里切到下一层,菜单出现时有动画
42
+ // 按下反馈:页面里只有一份(press.mjs);菜单开着时点外面的那一下已被菜单消费,不加反馈、不选中。
43
+ let menuFrom = null; // 现在开着的菜单从哪里打开的:'browse'(目录树、图标视图的行)、'result'(搜索结果的行)、null(别的);changed 时按它决定关不关
44
+ const press = mountPress({ identity: () => `${model.session}\n${model.scope}`, consumed: menu.consumed });
36
45
  const chat = createChat({ link: conn.link, copy: text => coda.copy(text), hint });
37
46
  // 属性页和图标视图的上一层:iPhone 用原生返回(coda.back);Mac 的客户端会替声明了返回的页面画一个带小箭头的导航,
38
47
  // 和侧栏标题重复,所以 Mac 和没有 coda.back 的环境一样,头部最左边自己放返回。
@@ -43,8 +52,8 @@ const tree = mountTree($('tree'), model, {
43
52
  more: path => model.loadMore(path),
44
53
  retry: path => model.retry(path),
45
54
  menu: (event, row) => pathMenu(event, row),
46
- select: isIOS ? null : path => selectEntry(path),
47
- }, { draggable: !isIOS, longPress: isIOS, keyboard: !isIOS });
55
+ select: isIOS ? null : path => mouseSelect(path),
56
+ }, { draggable: !isIOS, longPress: isIOS, keyboard: !isIOS, press, consumed: menu.consumed });
48
57
  const grid = mountGrid($('grid'), model, {
49
58
  open: openFile,
50
59
  enter: path => enterDir(path),
@@ -52,17 +61,87 @@ const grid = mountGrid($('grid'), model, {
52
61
  retry: path => model.retry(path),
53
62
  menu: (event, cell) => pathMenu(event, cell),
54
63
  back: nativeBack ? null : () => leaveDir(),
55
- select: isIOS ? null : path => selectEntry(path),
56
- }, { draggable: !isIOS, longPress: isIOS, keyboard: !isIOS });
64
+ select: isIOS ? null : path => mouseSelect(path),
65
+ }, { draggable: !isIOS, longPress: isIOS, keyboard: !isIOS, press, consumed: menu.consumed });
57
66
  const restore = createRestore(model, { tree, grid });
58
67
  const trail = createTrail();
68
+
69
+ // ---- 文件名搜索(第 18 节):搜索框常驻在 #browse 顶上,有字时结果换掉目录树和图标视图 ----
70
+ // 搜索的身份是会话和根目录(model.scope):回答回来时对不上就作废。
71
+ const search = createSearch({
72
+ call: (fields, signal) => conn.link.call(Type.SEARCH, fields, { signal, cancel: Type.SEARCH_CANCEL }),
73
+ hidden: () => model.showHidden,
74
+ identity: () => `${model.session}\n${model.scope}`,
75
+ // 结果整体没了(搜索词清空、reset、根目录不可用 suspend 都走这里):结果行的菜单一并作废——它的操作指着旧结果。
76
+ // 非空重搜时旧结果留着(shown 不为空或在搜索中),菜单照旧留着,操作按打开菜单时的路径做。
77
+ onChange: () => {
78
+ if (menuFrom === 'result' && (!search.active || (search.shown === null && search.status === 'idle'))) { menu.hide(); menuFrom = null; }
79
+ schedule();
80
+ },
81
+ });
82
+ const searchbar = mountSearchbar($('searchbar'), { onInput: searchInput, onClear: () => { clearSearch(); searchbar.focus(); }, onKey: searchBoxKey, onEnter: () => { if (isIOS) searchbar.blur(); } });
83
+ const hitStatus = h('span', { class: 'sstatus-text' }), hitRetry = h('button', { class: 'link', onclick: () => search.research() }, '重试'), hitsEl = h('div', { class: 'shits', role: 'listbox' });
84
+ hitRetry.hidden = true;
85
+ $('search').replaceChildren(h('div', { class: 'sstatus secondary', role: 'status' }, hitStatus, hitRetry), hitsEl);
86
+ // 结果自己的选中(不改 model.selected)和键盘焦点;单击只切 .sel,不重画行(双击要落在还在容器里的节点上)。
87
+ const hits = mountTree(hitsEl, model, {
88
+ open: path => openResult(path),
89
+ toggle: path => revealResult(path, true), // 结果里的文件夹:同「在目录中显示」,然后展开它或进入它
90
+ more() {}, retry() {},
91
+ menu: (event, row) => resultMenu(event, row),
92
+ select: isIOS ? null : path => { if (search.select(path)) hits.select(); },
93
+ }, { draggable: !isIOS, longPress: isIOS, keyboard: !isIOS, press, consumed: menu.consumed, rows: () => search.rows(), selected: () => search.selected, view: 'search', title: false });
94
+
95
+ // 用户接管计数:浏览区上的 pointerdown、wheel、touchstart、keydown 各加一(点已选中的条目、点空白、右键和长按的起手都算,不管选中变没变);
96
+ // 经菜单、按钮执行的打开预览、属性页等在入口处另计一次。程序自己的展开、进入目录、滚动、选中都不经过这里。「在目录中显示」靠它判断用户有没有动过。
97
+ let takeover = 0;
98
+ for (const type of ['pointerdown', 'wheel', 'touchstart', 'keydown']) $('browse').addEventListener(type, () => { takeover++; }, true);
99
+ const taken = fn => (...args) => { takeover++; return fn(...args); };
100
+ const locator = createLocator({
101
+ model,
102
+ // 会话、根目录、排序、视图、隐藏开关和模型的刷新轮次:任何一个变了,定位就作废。
103
+ identity: () => [model.session, model.scope, model.sort, model.view, model.showHidden, model.round].join('\n'),
104
+ takeover: () => takeover,
105
+ ops: {
106
+ // 开始:清空搜索(搜索轮次加一);丢掉目标视图还在等的滚动恢复;图标视图进入目标所在的目录(和 enterDir 一样记 trail、滚到顶部,但不触发 landed 的补滚动)。
107
+ begin(parent) {
108
+ clearSearch();
109
+ restore.drop('tree');
110
+ restore.drop('grid');
111
+ if (parent === null || parent === model.cwd) return;
112
+ trail.remember(model.cwd, grid.scroll());
113
+ selectEntry(null);
114
+ model.enter(parent);
115
+ shownCwd = model.cwd;
116
+ grid.scrollTo(0);
117
+ declareBack();
118
+ },
119
+ render: () => browser().render(),
120
+ select: path => selectEntry(path),
121
+ center: path => browser().center(path),
122
+ // 文件夹结果:定位到它之后,列表视图展开它,图标视图进入它(程序自己的,不算接管)。
123
+ openFolder(path) { if (model.view === View.ICONS) enterInto(path); else if (!model.expanded.has(path)) model.toggle(path); },
124
+ notify: text => hint.show(text, { warn: false }),
125
+ },
126
+ });
127
+ function searchInput(value) {
128
+ locator.cancel(); // 重新输入搜索词:定位作废
129
+ search.input(value);
130
+ searchbar.set(value);
131
+ }
132
+ // 清空搜索:搜索词、结果、结果的选中都清掉(定位自己开始时、Esc、清除按钮)。
133
+ function clearSearch() {
134
+ search.reset();
135
+ searchbar.set('');
136
+ }
59
137
  const preview = mountPreview($('preview'), {
60
138
  read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text),
61
139
  // iPhone 预览头部右边的 ⋯(刷新、复制路径):顶部工具条在预览时只有「返回」,只留头部一行标题
62
140
  actions: isIOS ? [{ label: '更多', icon: 'ellipsis', run: (_, button) => toggleMore(button) }] : [{ label: '窗口', icon: 'window', run: openInView }], copyInHead: !isIOS,
63
141
  back: isIOS ? null : closePreview,
64
142
  // 选中文字的右键菜单只有 Mac:iPhone 用系统的选区菜单。
65
- selectionMenu: isIOS ? null : (event, selection) => menu.open(event, { items: chat.selectionItems(selection) }),
143
+ selectionMenu: isIOS ? null : (event, selection) => { menu.open(event, { items: chat.selectionItems(selection) }); menuFrom = null; },
144
+ consumed: menu.consumed, // 菜单开着时的 Esc 归菜单:查找条不处理
66
145
  });
67
146
  // 属性页:页内一层。
68
147
  let infoOpen = false;
@@ -88,9 +167,11 @@ function openInView(path) {
88
167
  }
89
168
  // 两端都在页内打开:model.openFile 设 selected 和 opened,经 onChange 切到预览。iPhone 触屏没有键盘焦点,不交焦点;
90
169
  // 快速两次轻点:第一次已切到预览,目录树 .off 不接指针事件,第二次落不到条目上。
91
- function openFile(path) {
170
+ // 打开文件的统一入口(目录树、图标视图和搜索结果):焦点交接和过渡意图在这里记,不随调用方复制。select 为假(搜索结果)时只设预览,不改 model.selected。
171
+ function openFile(path, { select = true } = {}) {
92
172
  openPressed = !isIOS; // 用户在 Mac 上打开的:render 把焦点交给预览面板
93
- model.openFile(path);
173
+ note({ kind: 'push', from: mode(), to: 'preview' });
174
+ model.openFile(path, { select });
94
175
  }
95
176
 
96
177
  // ---- 渲染:同一批变化合并成一次 ----
@@ -109,6 +190,21 @@ function selectEntry(path) {
109
190
  queueSave();
110
191
  }
111
192
  const browser = () => (model.view === View.ICONS ? grid : tree);
193
+ // 浏览区里键盘该落在哪:搜索时是结果,否则是目录树或图标视图。
194
+ const focusBrowse = () => (searching() ? hits : browser()).focus();
195
+
196
+ // 结果上面的状态行。
197
+ function paintSearch() {
198
+ const { text, retry } = search.statusLine();
199
+ if (hitStatus.textContent !== text) hitStatus.textContent = text;
200
+ hitRetry.hidden = !retry;
201
+ hits.render();
202
+ }
203
+ // Mac 在按下时选中(选中条目或在空白处取消):和键盘一样算用户接管,这个视图还在等的滚动恢复作废,免得恢复到达时把刚选中的条目滚走。
204
+ function mouseSelect(path) {
205
+ restore.drop(model.view === View.ICONS ? 'grid' : 'tree');
206
+ selectEntry(path);
207
+ }
112
208
 
113
209
  // 预览头部的返回和预览里的 ⌘↑ 是同一个动作:opened 清掉,selected 留着。
114
210
  function closePreview() {
@@ -117,8 +213,14 @@ function closePreview() {
117
213
  }
118
214
  // 用户点了预览头部的返回:render 据此决定焦点要不要交给浏览区,别的模式变化不动焦点。
119
215
  let backPressed = false;
216
+ let wantFocus = false; // 清空搜索后,等重画把目录树或图标视图放出来再把焦点交给它
120
217
  let openPressed = false; // 用户打开了文件:同返回对称,页面持有焦点时 render 把焦点交给预览面板
121
- const mode = () => (infoOpen ? 'info' : !model.ready ? 'notice' : model.opened !== null ? 'preview' : model.view === View.ICONS ? 'grid' : 'tree');
218
+ // 两级(第 18.3 节):外层是层(browse、preview、notice、info),管过渡、inert 和离开那层的释放;内层是 #browse 里当前的面板(search、grid、tree),只管三个面板的可见性。
219
+ // 预览或属性页盖在结果上面时,内层仍是结果:搜索不改外层。
220
+ const mode = () => (infoOpen ? 'info' : !model.ready ? 'notice' : model.opened !== null ? 'preview' : 'browse');
221
+ const searching = () => model.ready && search.active;
222
+ const pane = () => (searching() ? 'search' : model.view === View.ICONS ? 'grid' : 'tree');
223
+ const INNER = ['tree', 'grid', 'search'];
122
224
 
123
225
  // 说明区只在内容变了才重画,免得重试按钮在点击中途被换掉。
124
226
  let noticeKey = null;
@@ -130,34 +232,134 @@ function paintNotice() {
130
232
  showNotice($('notice'), failed ? { failure: conn.failure, retry: conn.hello } : { text: key });
131
233
  }
132
234
 
133
- function render() {
134
- const current = mode();
135
- const refocus = backPressed && document.hasFocus(), handoff = openPressed && document.hasFocus();
136
- backPressed = openPressed = false;
137
- for (const id of ['tree', 'grid', 'preview', 'notice', 'info']) {
138
- $(id).classList.toggle('off', current !== id);
235
+ // ---- iPhone 层级切换的过渡(第 17.6 节)----
236
+ // 只在用户操作引起时做:入口(openFile、openInfo、goBack、enterDir、leaveDir)在改模型之前记一个一次性的「过渡意图」,render 消费它:
237
+ // 前后对得上就播,对不上就丢掉。换会话、换根目录、恢复、notice、changed 的刷新都没有意图,不播。意图由最外层的入口记,goBack 内部调用的 closeInfo、leaveDir 不记。
238
+ // 层级的深浅:浏览区(tree、grid)浅,预览和属性页深。深的一层永远在上:打开时滑入盖住浅层,返回时滑出、露出浅层。
239
+ const PANES = ['browse', 'preview', 'notice', 'info'];
240
+ const DEEP = new Set(['preview', 'info']);
241
+ const SLIDE_MS = 280, FADE_IN_MS = 150;
242
+ let intent = null; // { kind: 'push' | 'pop' | 'fade', from?, to }:from、to 是模式;fade 的 to 是目标 cwd
243
+ let lastMode = null; // 上一次画出的模式
244
+ let running = null; // 正在播的过渡:{ leaving, after, end }
245
+ const note = next => { if (isIOS) intent = next; };
246
+
247
+ // 结束正在播的过渡到终点,立刻。结束和自然播完走同一个函数,幂等:强制结束之后迟到的 finish 事件什么都不做。
248
+ function endRunning() { running?.end(); }
249
+ function begin(anims, { ids = [], leaving = null, after = [] } = {}) {
250
+ let ended = false, pending = anims.length;
251
+ const slides = ids.length > 0; // 滑动期间整页不响应点按(同 iOS 原生的导航切换);目录淡入不算
252
+ if (slides) document.documentElement?.classList.add('moving');
253
+ const mine = running = { leaving, after, end() {
254
+ if (ended) return;
255
+ ended = true;
256
+ if (slides) document.documentElement?.classList.remove('moving');
257
+ for (const anim of anims) anim.cancel();
258
+ for (const id of ids) $(id).classList.remove('layer');
259
+ if (running === mine) running = null;
260
+ for (const fn of mine.after) fn();
261
+ applyPanes(mode());
262
+ } };
263
+ // 浏览器自己取消一条动画时只触发 cancel、不触发 finish:任一动画取消都结束整次过渡(end 幂等,它自己 cancel 引出的 oncancel 回来什么都不做)。
264
+ for (const anim of anims) { anim.onfinish = () => { if (--pending === 0) mine.end(); }; anim.oncancel = () => mine.end(); }
265
+ return mine;
266
+ }
267
+
268
+ // 面板的可见和可操作:只有当前的一层能操作;离开的那一层在退场期间还看得见,但 inert 且不接指针。
269
+ // 内层(#browse 里的三个面板)按状态直接换,不做过渡,也不管外层在不在播过渡。
270
+ function applyPanes(current) {
271
+ const leaving = running?.leaving ?? null;
272
+ for (const id of PANES) {
273
+ $(id).classList.toggle('off', current !== id && leaving !== id);
274
+ $(id).classList.toggle('leaving', leaving === id);
139
275
  $(id).inert = current !== id;
140
276
  }
277
+ const inner = pane();
278
+ for (const id of INNER) {
279
+ $(id).classList.toggle('off', inner !== id);
280
+ $(id).inert = current !== 'browse' || inner !== id; // 外层不是浏览区(被预览、属性页盖住)时里面的面板也不能操作
281
+ }
282
+ }
283
+
284
+ // 这次 render 要不要播层级切换:消费意图。fade 要等内容到了,不在这里。
285
+ function takeSlide(current) {
286
+ const taken = intent;
287
+ if (!taken || taken.kind === 'fade') return null;
288
+ intent = null;
289
+ if (!isIOS || reducedMotion() || taken.from !== lastMode || taken.to !== current || lastMode === current) return null;
290
+ if (DEEP.has(taken.from) === DEEP.has(taken.to) || typeof $(current).animate !== 'function') return null;
291
+ return taken;
292
+ }
293
+
294
+ function slide({ kind, from, to }, after) {
295
+ const leaving = $(from), entering = $(to), deep = kind === 'push' ? entering : leaving, shallow = kind === 'push' ? leaving : entering;
296
+ const options = { duration: SLIDE_MS, easing: 'cubic-bezier(.2,.8,.2,1)' };
297
+ const shift = kind === 'push' ? ['translateX(0)', 'translateX(-25%)'] : ['translateX(-25%)', 'translateX(0)'];
298
+ const dim = kind === 'push' ? ['brightness(1)', 'brightness(.9)'] : ['brightness(.9)', 'brightness(1)'];
299
+ const away = ['translateX(100%)', 'translateX(0)'];
300
+ // 参与过渡的两层都要不透明,否则透出对面(只在过渡期间);变暗不加遮罩元素,不挡指针。
301
+ leaving.classList.add('layer');
302
+ entering.classList.add('layer');
303
+ begin([
304
+ deep.animate({ transform: kind === 'push' ? away : [...away].reverse() }, options),
305
+ shallow.animate({ transform: shift, filter: dim }, options),
306
+ ], { ids: [from, to], leaving: from, after });
307
+ }
308
+
309
+ // 图标视图进出目录:内容到了才播,只给内容播,不给「正在读取」播。
310
+ function takeFade(current) {
311
+ if (intent?.kind !== 'fade') return;
312
+ if (current !== 'browse' || pane() !== 'grid' || model.cwd !== intent.to) { intent = null; return; } // 中途换了目录、或打开了文件:作废
313
+ const state = model.dirs.get(model.cwd);
314
+ if (!state || !(state.loaded || state.error)) return; // 还在读取:意图留着,内容到了的那次 render 播
315
+ intent = null;
316
+ if (reducedMotion()) return;
317
+ const anim = grid.fadeIn(FADE_IN_MS);
318
+ if (anim) { endRunning(); begin([anim]); }
319
+ }
320
+
321
+ // 属性页是逻辑上关了但画面还在(退场期间):render 决定什么时候释放。
322
+ let infoStopped = false;
323
+
324
+ function render() {
325
+ const current = mode();
326
+ const refocus = (backPressed || wantFocus) && document.hasFocus(), handoff = openPressed && document.hasFocus();
327
+ backPressed = openPressed = wantFocus = false;
328
+ const slideIn = takeSlide(current);
141
329
  if (current === 'notice') paintNotice();
142
- const browsing = current === 'tree' || current === 'grid';
330
+ const browsing = current === 'browse';
143
331
  (model.view === View.ICONS ? grid : tree).render(); // 只画正在用的视图,另一个留着它的内容和滚动位置
332
+ searchbar.set(search.text);
333
+ if (searching()) paintSearch();
144
334
  landed();
145
335
  restore.settle();
146
336
  // 待恢复的位置只属于它那个文件:opened 清了或换成别的文件,位置作废。
147
337
  if (pending.preview !== null && pending.path !== model.opened) pending.preview = null;
338
+ const released = []; // 这次停止的面板:画面的释放等退场结束(有过渡时),否则马上
148
339
  if (current === 'preview' && preview.path !== model.opened) {
149
340
  // 位置要等内容到了、真的设上才清:之前离开页面或换会话,saveNow 还存它。作废的读取不清,由接替它的读取用上。
150
341
  const scroll = pending.preview;
151
342
  preview.open(model.opened, scroll).then(used => { if (used && scroll !== null && pending.preview === scroll) pending.preview = null; });
152
- } else if ((browsing || current === 'notice') && preview.path !== null) preview.close(); // 属性页和预览一样只切可见性,不动别的面板的内容
343
+ } else if ((browsing || current === 'notice') && preview.path !== null) { preview.stop(); released.push(() => preview.release()); } // 属性页和预览一样只切可见性,不动别的面板的内容
344
+ if (infoStopped && current !== 'info') { infoStopped = false; released.push(() => info.release()); }
345
+ // 上一个过渡:模式变了或又有新的过渡就立刻结束到终点(它的释放比对打开次数,又打开过就不释放)。
346
+ if (running && (slideIn || current !== lastMode)) endRunning();
347
+ if (slideIn) slide(slideIn, slideIn.kind === 'pop' ? released.splice(0) : []); // 返回:被停止的那层到退场结束才释放
348
+ for (const fn of released) fn();
349
+ applyPanes(current);
350
+ takeFade(current);
351
+ lastMode = current;
153
352
  // 返回按钮随预览一起 inert,焦点会掉到页面外:用户刚点了返回、页面又持有焦点时交给目录树或网格。
154
353
  // 后台刷新、切会话、root 变化造成的切换不动焦点,免得抢走客户端输入框的焦点。
155
- if (refocus && browsing) browser().focus();
354
+ if (refocus && browsing) focusBrowse();
156
355
  // 打开时浏览区变 inert,焦点本来就会掉:交给预览面板,⌘↑ 才收得到。
157
356
  if (handoff && current === 'preview') $('preview').focus();
158
357
  syncToolbar();
159
358
  declareBack();
160
359
  }
360
+ // 窗口尺寸变了(旋转)、系统的减弱动态效果改了:正在播的动画结束到终点。
361
+ window.addEventListener('resize', endRunning);
362
+ onMotionChange(endRunning);
161
363
 
162
364
  // ---- 工具栏:Mac 用 coda.toolbar,没有的(iOS、harness)退成页内一条 ----
163
365
  // Mac 只有一个原生按钮「更多」:原生菜单带向下的箭头,关不掉,所以按下后由页面弹菜单。返回键在预览头部,不在这里。
@@ -191,6 +393,7 @@ const TOOLBAR = { getBoundingClientRect: () => ({ right: window.innerWidth, bott
191
393
  // 「更多」菜单的内容只写这一处,iPhone 的 ⋯ 和 Mac 的原生按钮共用:刷新、显示隐藏文件、「显示」›;「显示」里是视图和排序,当前项有对勾。
192
394
  const moreMenu = () => isIOS && mode() === 'preview' ? { items: [
193
395
  { label: '刷新', run: () => act('refresh') },
396
+ ...(preview.canFind ? [{ label: '查找', run: () => preview.openFind() }] : []), // 只有显示着的文本有查找
194
397
  { label: '复制路径', run: () => preview.copyPath() }, // 相对路径
195
398
  ] } : ({ items: [
196
399
  { label: '刷新', run: () => act('refresh') },
@@ -237,7 +440,7 @@ function changeView(view) {
237
440
  // 键盘激活没有 pointerdown,菜单外点击那条规则不管用:同一个按钮再次激活就关闭。
238
441
  function toggleMore(button) {
239
442
  if (!menu.hidden && menu.opener === button) menu.hide();
240
- else menu.open(button, moreMenu());
443
+ else { menu.open(button, moreMenu()); menuFrom = null; }
241
444
  }
242
445
 
243
446
  function act(id, button) {
@@ -250,11 +453,13 @@ function act(id, button) {
250
453
  model.setShowHidden(!model.showHidden);
251
454
  persisted.hidden = model.showHidden;
252
455
  invalidateScroll(); // 不管在哪种视图切换:显示的条目变了,两种视图的旧位置都对不上
456
+ search.research(); // 隐藏开关变了:有搜索词就按新的 hidden 重搜
253
457
  }
254
458
  else if (id === 'refresh') {
255
459
  conn.refresh();
256
460
  model.refresh();
257
461
  preview.refresh();
462
+ search.research();
258
463
  }
259
464
  }
260
465
  // 原生按钮的事件是 { id }:只认 more,别的什么都不做。
@@ -281,7 +486,7 @@ function saveNow() {
281
486
  // setState 超限会同步抛错:再收紧一次,仍失败就放弃这次,不影响界面。
282
487
  if (quiet(() => (coda.setState(persisted), true)) === undefined) quiet(() => coda.setState(trimState(persisted, session, STATE_LIMIT / 4)));
283
488
  }
284
- quiet(() => coda.on('visibility', visible => { if (!visible) saveNow(); }));
489
+ quiet(() => coda.on('visibility', visible => { if (!visible) { press.reset(); saveNow(); } }));
285
490
  $('tree').addEventListener('scroll', queueSave, { passive: true });
286
491
  $('tree').addEventListener('scroll', () => restore.scrolled('tree'), { passive: true });
287
492
  grid.onScroll(queueSave);
@@ -293,21 +498,51 @@ preview.onScroll(menu.hide);
293
498
  // 行菜单顶上是路径,下面一行 大小 · 完整修改时间(文件夹只有修改时间,读不到就没有这一行);指向外面的链接和不能打开的条目没有元数据,不显示这一行。
294
499
  // 不能打开的条目(指向外面或失效的链接、other)没有「添加到聊天」,和不能拖动一致;它们仍有复制路径和属性。
295
500
  function pathMenu(event, row) {
501
+ press.reset(); // 长按弹出菜单:按下反馈立即去掉
296
502
  const dead = row.kind === EntryKind.LINK || row.kind === EntryKind.OTHER;
297
503
  // Mac 的第一项和双击是同一个动作(右键不改选中);文件夹在列表里写展开或收起,别的写打开。iPhone 轻点就是打开,菜单不变。
298
504
  const folder = row.kind === EntryKind.DIR, listed = model.view !== View.ICONS;
299
- const open = isIOS || dead ? [] : [{ label: folder && listed ? (row.open ? '收起' : '展开') : '打开', run: () => browser().activate(row) }, { separator: true }];
505
+ const open = isIOS || dead ? [] : [{ label: folder && listed ? (row.open ? '收起' : '展开') : '打开', run: taken(() => browser().activate(row)) }, { separator: true }];
506
+ menuFrom = 'browse';
300
507
  menu.open(event, { title: row.path, detail: dead ? undefined : infoText(row) || undefined, items: [
301
508
  ...open,
302
- ...(dead ? [] : [{ label: '添加到聊天', run: () => chat.add(row.path) }]),
303
- { label: '复制相对路径', run: () => coda.copy(row.path) },
304
- { label: '复制绝对路径', run: () => coda.copy(`${model.root.path}/${row.path}`) },
509
+ ...(dead ? [] : [{ label: '添加到聊天', run: taken(() => chat.add(row.path)) }]),
510
+ { label: '复制相对路径', run: taken(() => coda.copy(row.path)) },
511
+ { label: '复制绝对路径', run: taken(() => coda.copy(`${model.root.path}/${row.path}`)) },
305
512
  { separator: true },
306
- { label: '属性', run: () => openInfo(row) },
513
+ { label: '属性', run: taken(() => openInfo(row)) },
307
514
  ] });
308
515
  }
516
+
517
+ // 结果的行菜单:同目录树(打开、添加到聊天、复制路径、属性),另加「在目录中显示」。
518
+ // 菜单是按打开菜单时那一行的路径和来源(结果)做的:菜单开着时结果被换掉,执行的操作仍按那时的路径,来源仍算结果。
519
+ // iPhone 轻点就是打开,菜单里不放「打开」。
520
+ function resultMenu(event, row) {
521
+ press.reset();
522
+ const dead = row.kind === EntryKind.LINK || row.kind === EntryKind.OTHER, { path } = row;
523
+ menuFrom = 'result';
524
+ menu.open(event, { title: path, items: [
525
+ ...(isIOS || dead ? [] : [{ label: '打开', run: taken(() => (row.kind === EntryKind.DIR ? revealResult(path, true) : openResult(path))) }, { separator: true }]),
526
+ ...(dead ? [] : [{ label: '添加到聊天', run: taken(() => chat.add(path)) }]),
527
+ { label: '复制相对路径', run: taken(() => coda.copy(path)) },
528
+ { label: '复制绝对路径', run: taken(() => coda.copy(`${model.root.path}/${path}`)) },
529
+ { separator: true },
530
+ { label: '在目录中显示', run: taken(() => revealResult(path)) },
531
+ { label: '属性', run: taken(() => openInfo(row)) },
532
+ ] });
533
+ }
534
+ // 结果里打开文件:设预览,不设 selected;结果自己的选中是刚打开的那个,返回后回到结果。
535
+ function openResult(path) {
536
+ if (search.select(path)) hits.select();
537
+ openFile(path, { select: false });
538
+ }
539
+ // 在目录中显示(open 为真:文件夹结果,找到之后再展开它或进入它)。
540
+ function revealResult(path, open = false) { locator.show(path, { open }); }
309
541
  $('tree').addEventListener('scroll', menu.hide, { passive: true });
310
542
  grid.onScroll(menu.hide);
543
+ hitsEl.addEventListener('scroll', menu.hide, { passive: true });
544
+ // iPhone:开始滚动结果时收起键盘。
545
+ hitsEl.addEventListener('scroll', () => { if (isIOS && searchbar.focused) searchbar.blur(); }, { passive: true });
311
546
 
312
547
  // ---- 键盘(Mac)----
313
548
  // 两个作用域各管各的,匹配的键同步 preventDefault,不匹配的不拦;菜单开着不处理,两处都不主动抢焦点。
@@ -324,6 +559,46 @@ function browseKey(event) {
324
559
  }
325
560
  tree.onKey(browseKey);
326
561
  grid.onKey(browseKey);
562
+ // 搜索框和结果的键盘(第 18.5 节的分派表):输入法组字中的按键一律不管;菜单开着时 Esc 归菜单(关菜单、消费掉这个 Esc),这里不处理。
563
+ // ⌘F 在浏览区(含结果和页面本身)聚焦搜索框并选中里面的字;预览、属性页、说明区不拦;菜单开着时先关菜单。
564
+ function searchBoxKey(event) {
565
+ if (event.key === 'Escape' && menu.consumed(event)) event.preventDefault(); // 菜单关掉的 Esc:搜索框是 type=search,默认动作会清空它,要拦下
566
+ if (isIOS || event.isComposing || !menu.hidden || menu.consumed(event)) return;
567
+ if (event.key === 'Escape') {
568
+ event.preventDefault();
569
+ const clearing = search.text !== '';
570
+ if (clearing) clearSearch();
571
+ if (clearing) wantFocus = true; else focusBrowse(); // 清空之后结果让位给目录树:等重画把它放出来再交焦点
572
+ } else if (event.key === 'ArrowDown' && !event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey && searching() && search.rows().length > 0) {
573
+ event.preventDefault();
574
+ hits.focus();
575
+ const first = search.rows()[0].path; // 明确选中第一条(不是相对移动:有旧选中时 ↓ 会选到下一条)
576
+ if (search.select(first)) hits.select();
577
+ hits.scrollTo(0);
578
+ }
579
+ }
580
+ hits.onKey(event => {
581
+ if (isIOS || event.isComposing || !menu.hidden || menu.consumed(event)) return;
582
+ if (event.key === 'Escape') { event.preventDefault(); searchbar.focus(); return; }
583
+ const action = browseAction(event);
584
+ if (!action) return;
585
+ event.preventDefault();
586
+ // ← →、⌘↑ 什么都不做:不改被遮住的目录树和图标视图的目录
587
+ if (action === 'up' || action === 'down') hits.key(action);
588
+ else if (action === 'open') hits.openSelected();
589
+ });
590
+ document.addEventListener('keydown', event => {
591
+ if (isIOS || event.isComposing || !event.metaKey || event.ctrlKey || event.altKey || event.shiftKey || event.key?.toLowerCase() !== 'f') return;
592
+ if (!menu.hidden) menu.hide();
593
+ const layer = mode();
594
+ if (layer === 'preview') { // 能查找的文本:打开查找条并聚焦(已开就选中输入框的字);图片、二进制、读取中、读不到不拦
595
+ if (preview.canFind) { event.preventDefault(); preview.openFind(); }
596
+ return;
597
+ }
598
+ if (layer !== 'browse') return;
599
+ event.preventDefault();
600
+ searchbar.select();
601
+ });
327
602
  // 侧栏预览:只有 ⌘↑,返回。焦点要在面板里才收得到(点正文、打开时交接)。
328
603
  $('preview').addEventListener('keydown', event => {
329
604
  if (isIOS || infoOpen || !menu.hidden || !previewAction(event)) return;
@@ -335,20 +610,24 @@ $('preview').addEventListener('keydown', event => {
335
610
  // 进入子目录滚到顶部,先记下上层的位置;回上一层恢复进去之前的位置(记录在内存里,页面卸载后没有,回到顶部)。
336
611
  // shownCwd:页面已经处理过的 cwd;它被模型自己换掉(目录没了退到上一层、根目录变了回到根目录)时,在 landed 里补上滚动。
337
612
  let shownCwd = '';
338
- function enterDir(path) {
613
+ const enterDir = taken(path => enterInto(path));
614
+ function enterInto(path) {
339
615
  trail.remember(model.cwd, grid.scroll());
340
616
  selectEntry(null); // 进入新的一层:选中清空
341
617
  model.enter(path);
618
+ note({ kind: 'fade', to: model.cwd });
342
619
  shownCwd = model.cwd;
343
620
  restore.drop('grid');
344
621
  grid.scrollTo(0);
345
622
  declareBack();
346
623
  }
347
- // 退 levels 层(到根为止);已经在根目录返回 false。
348
- function leaveDir(levels = 1) {
624
+ // 退 levels 层(到根为止);已经在根目录返回 false。record 为假时不记过渡意图(goBack 里的内部调用:意图由它记)。
625
+ function leaveDir(levels = 1, record = true) {
626
+ takeover++; // 用户经按钮、键盘、返回退出图标视图的一层
349
627
  if (model.cwd === '') return false;
350
628
  const from = model.cwd.split('/');
351
629
  model.leave(levels);
630
+ if (record) note({ kind: 'fade', to: model.cwd });
352
631
  land(trail.take(model.cwd) ?? 0);
353
632
  // 选中来时经过的那个文件夹(退到的那一层的直接子项)。目录被删、模型自己退回上层走 landed,不改选中。
354
633
  selectEntry(from.slice(0, model.cwd === '' ? 1 : model.cwd.split('/').length + 1).join('/'));
@@ -369,12 +648,14 @@ function landed() {
369
648
  // 路径是上层的标题,由远到近,不含当前页:图标视图在 a/b 是 [根目录名, 'a'];列表视图没有。属性页、预览各多一层当前页(要回到的那一页):列表视图是 '资源管理器',图标视图是当前目录名。
370
649
  // 预览开着是 opened 不为空、且根目录就绪;没就绪时页面是说明区,路径为空。
371
650
  // 标题是当前目录名:根目录是根目录名。
372
- const dirTitle = () => (model.cwd === '' || model.view !== View.ICONS ? rootName(model.root.path) : model.cwd.slice(model.cwd.lastIndexOf('/') + 1));
651
+ // 搜索时结果盖住了图标视图的目录:标题是根目录名。
652
+ const dirTitle = () => (model.cwd === '' || model.view !== View.ICONS || searching() ? rootName(model.root.path) : model.cwd.slice(model.cwd.lastIndexOf('/') + 1));
373
653
  const previewOpen = () => model.ready && model.opened !== null;
654
+ // 搜索本身不是一级:搜索模式下图标视图的目录层级不声明(搜索盖住了它们),从结果打开的预览和属性页要回到的那一页叫「搜索」(第 18.5 节的表)。
374
655
  function backPath() {
375
- const icons = model.view === View.ICONS && model.ready;
656
+ const icons = model.view === View.ICONS && model.ready && !searching();
376
657
  const upper = icons && model.cwd !== '' ? [rootName(model.root.path), ...model.cwd.split('/').slice(0, -1)] : [];
377
- const here = icons ? dirTitle() : '资源管理器';
658
+ const here = searching() ? '搜索' : icons ? dirTitle() : '资源管理器';
378
659
  return [...upper, ...(infoOpen ? [here] : []), ...(previewOpen() ? [here] : [])];
379
660
  }
380
661
  let declared = [];
@@ -388,13 +669,16 @@ function declareBack(force = false) {
388
669
  }
389
670
  // 退 steps 层:先关预览,再关属性页,再退图标视图的目录;返回是不是真的退了。横屏的返回和原生返回共用。
390
671
  // 没就绪时页面是说明区,看不见预览和目录层:不消耗它们(保存的 opened、cwd 不变),横屏的返回直接关视图。
672
+ // 一次返回只记一个意图,按它消耗了什么:关了预览或属性页记 pop(目标是退完之后的模式),只退了目录记 fade;内部调用的 closeInfo、leaveDir 不记。
391
673
  function goBack(steps) {
392
674
  if (!model.ready) return false;
393
- let used = 0;
394
- if (previewOpen() && steps > used) { model.closeFile(); used++; } // 走和 Mac 一样的 closeFile:opened 清,selected 留着
395
- if (infoOpen && steps > used) { closeInfo(); used++; }
396
- const levels = Math.min(steps - used, model.view === View.ICONS && model.cwd !== '' ? model.cwd.split('/').length : 0);
397
- if (levels > 0) { leaveDir(levels); used += levels; }
675
+ const from = mode();
676
+ let used = 0, layer = false;
677
+ if (previewOpen() && steps > used) { model.closeFile(); used++; layer = true; } // 走和 Mac 一样的 closeFile:opened 清,selected 留着
678
+ if (infoOpen && steps > used) { closeInfo(false); used++; layer = true; }
679
+ const levels = Math.min(steps - used, model.view === View.ICONS && model.cwd !== '' && !searching() ? model.cwd.split('/').length : 0); // 搜索模式下不消耗被遮住的图标视图目录
680
+ if (levels > 0) { leaveDir(levels, false); used += levels; }
681
+ if (used > 0) note(layer ? { kind: 'pop', from, to: mode() } : { kind: 'fade', to: model.cwd });
398
682
  return used > 0;
399
683
  }
400
684
  if (nativeBack) quiet(() => coda.on('back', event => {
@@ -403,15 +687,20 @@ if (nativeBack) quiet(() => coda.on('back', event => {
403
687
 
404
688
  // ---- 属性页:所有关闭的路径(原生返回、横屏的返回、换会话、根目录变化)都重新声明回上层 ----
405
689
  function openInfo(row) {
690
+ note({ kind: 'push', from: mode(), to: 'info' });
406
691
  infoOpen = true;
692
+ infoStopped = false;
407
693
  info.open(row.path, row);
408
694
  declareBack();
409
695
  schedule();
410
696
  }
411
- function closeInfo() {
697
+ // 停止是立即的(取消统计、作废在途的读取);画面留到退场结束,由 render 释放。换会话、换根目录要立即释放(immediate),也不记意图。
698
+ function closeInfo(record = true, immediate = false) {
412
699
  if (!infoOpen) return;
413
700
  infoOpen = false;
414
- info.close(); // 在统计的一并取消
701
+ if (immediate) info.close();
702
+ else { info.stop(); infoStopped = true; } // 在统计的一并取消
703
+ if (record) note({ kind: 'pop', from: 'info', to: mode() });
415
704
  declareBack();
416
705
  schedule();
417
706
  }
@@ -419,11 +708,17 @@ function closeInfo() {
419
708
  // ---- 模块的消息(代次和握手由 connection 处理)----
420
709
  function onSession(data) {
421
710
  saveNow(); // 先存旧会话
711
+ intent = null;
712
+ endRunning();
713
+ press.reset();
422
714
  preview.close();
423
- closeInfo();
715
+ closeInfo(false, true);
424
716
  menu.hide();
425
717
  tree.reset();
426
718
  grid.reset();
719
+ hits.reset();
720
+ locator.cancel();
721
+ clearSearch(); // 换会话:搜索词、结果、结果的选中、定位都清掉
427
722
  epoch++;
428
723
  hint.hide();
429
724
  const saved = persisted.sessions[data.session] ?? null;
@@ -440,11 +735,13 @@ function onSession(data) {
440
735
 
441
736
  function onRoot(data) {
442
737
  const before = model.root;
443
- if (data.state !== before?.state || data.path !== before?.path) menu.hide(); // 开着的菜单里的路径是旧根目录下的
444
- if (data.state !== RootState.READY || before?.path !== data.path) closeInfo(); // 属性页看的是旧根目录下的路径
738
+ if (data.state !== before?.state || data.path !== before?.path) { menu.hide(); intent = null; endRunning(); } // 开着的菜单里的路径是旧根目录下的;换根目录不播过渡
739
+ if (data.state !== RootState.READY || before?.path !== data.path) closeInfo(false, true); // 属性页看的是旧根目录下的路径
445
740
  const scope = model.scope;
741
+ if (data.state !== RootState.READY) { search.suspend(); locator.cancel(); } // 根目录暂不可用:取消在途搜索,丢掉结果,搜索词留着;定位作废(再就绪也不接着做)
446
742
  const done = model.receive(data);
447
- if (model.scope !== scope) { invalidateScroll(); pending.preview = null; } // 换了根目录(同根重建不算):旧根目录的位置和记录不能给新的用,待恢复的预览位置也是
743
+ if (model.scope !== scope) { invalidateScroll(); pending.preview = null; press.reset(); clearSearch(); locator.cancel(); } // 换了根目录(同根重建不算):旧根目录的位置和记录不能给新的用,待恢复的预览位置也是;搜索词和结果清空
744
+ else if (data.state === RootState.READY) search.resume(); // 再就绪时 scope 没变:重搜
448
745
  if (done && data.state === RootState.READY) {
449
746
  if (before?.path !== data.path) { if (model.opened !== null) preview.reload(); } else preview.refresh(); // 换了根目录,opened 已清空:旧预览随即关掉,不再读
450
747
  }
@@ -452,13 +749,15 @@ function onRoot(data) {
452
749
 
453
750
  function onChanged(data) {
454
751
  if (!model.receive(data)) return;
455
- menu.hide(); // 开着的菜单可能指着已经删除的路径
752
+ // 开着的菜单可能指着已经删除的路径:目录树、图标视图的行菜单照旧关。结果行的菜单留着(第 18.3、18.4 节:重搜替换结果时菜单留着,操作按打开菜单时的路径做)。
753
+ if (menuFrom !== 'result') menu.hide();
456
754
  dataRound++;
457
755
  preview.refresh();
458
756
  info.refresh();
757
+ search.research(); // 搜索期间目录模型照常刷新;有搜索词就重搜,旧结果留到新结果来
459
758
  }
460
759
 
461
- const remeasure = () => { tree.remeasure(); grid.remeasure(); };
760
+ const remeasure = () => { tree.remeasure(); grid.remeasure(); hits.remeasure(); };
462
761
  remeasure();
463
762
  quiet(() => coda.on('theme', remeasure));
464
763
  render();