@coda-console/resource-manager 0.7.2 → 0.8.1

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.
@@ -0,0 +1,144 @@
1
+ // 文件名搜索在页面这一侧的状态(设计见 docs/design/phase-3.md 第 18.3 节):搜索词、搜索轮次、防抖、在途的请求、结果和它所属的搜索词、结果的选中。
2
+ // 不碰 DOM,请求由 call 发。
3
+ //
4
+ // 每个异步机制的五样:
5
+ // - 搜索轮次 round:由这里持有。输入、清空、隐藏开关切换、changed 和手动刷新的重搜、换会话、换根目录、根目录不可用都立刻加一(bump),
6
+ // 同时取消防抖计时器和在途的请求(link 在请求已发出时发 search_cancel)。只有当前轮次的回答能提交,旧轮次迟到的成功和失败都作废。
7
+ // - 防抖计时器:输入或重搜时开始,停顿 DEBOUNCE_MS 才发;到点先核对轮次,不是这一轮就不发;发出时、轮次变了时结束;换会话后不会用新会话发旧词。
8
+ // - 在途的请求:发出时开始,回答、失败、被 bump 取消时结束;超时由 link 的 30 秒兜底(算失败,显示原因和重试);回答回来时还核对会话和根目录的身份。
9
+ // - 结果 shown:和它所属的搜索词存在一起;新结果到来之前旧结果留着,状态写「正在搜索…」;加粗用旧结果自己的词。
10
+ import { ErrorCode, SEARCH_RESULTS, foldName, matchSpan } from './protocol.mjs';
11
+
12
+ export const DEBOUNCE_MS = 300;
13
+
14
+ const dirOf = path => (path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '');
15
+ const baseOf = path => path.slice(path.lastIndexOf('/') + 1);
16
+
17
+ const STOPS = {
18
+ count: `结果太多,只显示先找到的 ${SEARCH_RESULTS} 个`,
19
+ bytes: '结果太多,只显示一部分',
20
+ items: '没搜完(超过 20 万项),只显示已找到的',
21
+ time: '没搜完(超过 5 秒),只显示已找到的',
22
+ queue: '没搜完(目录太多),只显示已找到的',
23
+ };
24
+
25
+ // 状态行(结果上面一行):{ text, retry? }。
26
+ export function statusOf({ status, error, shown }) {
27
+ if (status === 'error') return { text: `搜索失败:${error}`, retry: true };
28
+ if (status === 'searching') return { text: '正在搜索…' };
29
+ if (!shown) return { text: '' };
30
+ const base = shown.stop === 'done' ? (shown.items.length === 0 ? '没有找到' : `共 ${shown.items.length} 个`) : STOPS[shown.stop] ?? '';
31
+ return { text: shown.unreadable > 0 ? `${base} · ${shown.unreadable} 项读不到` : base };
32
+ }
33
+
34
+ // call(fields, signal) → Promise<searched 回答>;hidden():现在是不是显示隐藏文件;identity():会话和根目录的身份串;
35
+ // onChange():状态变了,页面据此重画。
36
+ export function createSearch({ call, hidden, identity, onChange, timers = globalThis }) {
37
+ let text = '', round = 0, timer = null, inflight = null;
38
+ let shown = null; // { term, folded, items, stop, scanned, unreadable }:结果和它所属的搜索词
39
+ let status = 'idle', error = '', selected = null, suspended = false;
40
+ let cache = null; // rows() 的缓存:同一份结果只算一次行
41
+
42
+ const bump = () => {
43
+ round++;
44
+ timers.clearTimeout(timer);
45
+ timer = null;
46
+ inflight?.abort();
47
+ inflight = null;
48
+ };
49
+
50
+ function send(mine) {
51
+ const term = text.trim(), controller = new AbortController(), id = identity();
52
+ inflight = controller;
53
+ status = 'searching';
54
+ onChange();
55
+ const current = () => round === mine && identity() === id;
56
+ call({ query: term, hidden: hidden() }, controller.signal).then(reply => {
57
+ if (!current()) return;
58
+ inflight = null;
59
+ shown = { term, folded: foldName(term), items: reply.results, stop: reply.stop, scanned: reply.scanned, unreadable: reply.unreadable };
60
+ cache = null;
61
+ status = 'idle';
62
+ if (selected !== null && !reply.results.some(item => item.path === selected)) selected = null;
63
+ onChange();
64
+ }, failure => {
65
+ if (failure?.stale || !current()) return;
66
+ inflight = null;
67
+ status = 'error';
68
+ error = failure?.code === ErrorCode.INVALID_QUERY ? '搜索词无效' : failure?.message ?? '未知原因';
69
+ onChange();
70
+ });
71
+ }
72
+
73
+ // 防抖:停顿才发。回调里核对轮次,不是这一轮就不发。
74
+ function schedule() {
75
+ const mine = round;
76
+ status = 'searching';
77
+ timer = timers.setTimeout(() => { timer = null; if (round === mine) send(mine); }, DEBOUNCE_MS);
78
+ }
79
+
80
+ return {
81
+ get text() { return text; },
82
+ get active() { return text.trim() !== ''; },
83
+ get status() { return status; },
84
+ get error() { return error; },
85
+ get shown() { return shown; },
86
+ get round() { return round; },
87
+ get selected() { return selected; },
88
+ get suspended() { return suspended; },
89
+ statusLine: () => statusOf({ status, error, shown }),
90
+
91
+ // 用户输入(含清空):换了词,旧结果留到新结果来;空词立刻回到浏览。
92
+ input(value) {
93
+ text = value;
94
+ bump();
95
+ if (text.trim() === '') { shown = null; cache = null; selected = null; status = 'idle'; }
96
+ else schedule();
97
+ onChange();
98
+ },
99
+ // changed、手动刷新、隐藏开关切换:有搜索词就重搜,旧结果留到新结果来。
100
+ research() {
101
+ if (text.trim() === '') return;
102
+ bump();
103
+ schedule();
104
+ onChange();
105
+ },
106
+ // 根目录暂不可用(scope 没变):取消,丢结果,留搜索词;再就绪时 resume。
107
+ suspend() {
108
+ bump();
109
+ shown = null; cache = null; selected = null; status = 'idle';
110
+ suspended = true;
111
+ onChange();
112
+ },
113
+ resume() {
114
+ if (!suspended) return;
115
+ suspended = false;
116
+ this.research();
117
+ },
118
+ // 换会话、换根目录、定位开始、用户清空:搜索词、结果、选中都清掉。
119
+ reset() {
120
+ bump();
121
+ text = ''; shown = null; cache = null; selected = null; status = 'idle'; error = ''; suspended = false;
122
+ onChange();
123
+ },
124
+ // 结果的选中:只改这里,不重画;返回是不是变了。
125
+ select(path) {
126
+ if (selected === path) return false;
127
+ selected = path;
128
+ return true;
129
+ },
130
+ // 结果列表的行(mountTree 复用的行格式):加粗用结果自己的词。
131
+ rows() {
132
+ if (!shown) return [];
133
+ if (cache?.shown !== shown) {
134
+ cache = { shown, rows: shown.items.map(item => {
135
+ const name = baseOf(item.path);
136
+ return { type: 'entry', path: item.path, name, depth: 0, kind: item.kind, link: item.link === true, open: false,
137
+ bold: matchSpan(name, shown.folded), place: dirOf(item.path) || '工作目录', chevron: false };
138
+ }) };
139
+ }
140
+ return cache.rows;
141
+ },
142
+ dispose() { bump(); },
143
+ };
144
+ }
@@ -0,0 +1,27 @@
1
+ // 搜索框:目录树和图标视图顶上常驻的一行(设计见 docs/design/phase-3.md 第 18.3 节)。只管这一个输入框和它的清除按钮,搜索词归 view/search.mjs。
2
+ import { h } from './dom.mjs';
3
+ import { icon } from './icons.mjs';
4
+ import { QUERY_MAX } from './protocol.mjs';
5
+
6
+ // onInput(text):用户输入;onClear():点清除按钮;onKey(event):输入框里的按键(Esc、↓ 由页面按分派表处理);
7
+ // onEnter():回车或 iPhone 键盘的「搜索」键。字号不小于 16 点(style.css):iPhone 上小于 16 点聚焦时页面会放大。
8
+ export function mountSearchbar(el, { onInput, onClear, onKey, onEnter }) {
9
+ const input = h('input', {
10
+ id: 'q', type: 'search', placeholder: '搜索文件名', enterkeyhint: 'search', autocomplete: 'off', autocorrect: 'off', autocapitalize: 'off', spellcheck: 'false',
11
+ maxlength: QUERY_MAX, 'aria-label': '搜索文件名',
12
+ oninput: () => onInput(input.value), onkeydown: event => { if (event.key === 'Enter' && !event.isComposing) onEnter(); else onKey(event); },
13
+ });
14
+ const clear = h('button', { class: 'icon sclear', 'aria-label': '清除', title: '清除', onclick: onClear }, icon('clear', 16));
15
+ clear.hidden = true;
16
+ el.replaceChildren(h('div', { class: 'sfield' }, h('span', { class: 'sico', 'aria-hidden': 'true' }, icon('search', 16)), input, clear));
17
+ return {
18
+ input,
19
+ // 页面把搜索词写回输入框(清空、换会话);有字时显示清除按钮。
20
+ set(text) { if (input.value !== text) input.value = text; clear.hidden = text === ''; },
21
+ focus() { input.focus(); },
22
+ // 聚焦并选中里面的字。
23
+ select() { input.focus(); input.select?.(); },
24
+ blur() { input.blur?.(); },
25
+ get focused() { return document.activeElement === input; },
26
+ };
27
+ }
package/view/style.css CHANGED
@@ -54,13 +54,42 @@ p { margin: 0; }
54
54
  .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
55
55
  [hidden] { display: none !important; }
56
56
 
57
- /* 面板(目录树、图标、预览、说明、属性页)叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。 */
57
+ /* 面板(目录树、图标、预览、说明、属性页)叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。
58
+ 两级:外层是层(#browse、#preview、#notice、#info),#browse 里再叠三个面板(目录树、图标、搜索结果),搜索框在它们上面。 */
58
59
  #stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; } /* 裁掉层级过渡时横向溢出的那一层;菜单、提示是它的兄弟节点,不受裁切 */
59
60
  .pane { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
60
61
  .pane.off { visibility: hidden; pointer-events: none; }
61
62
  /* iPhone 层级切换(第 17.6 节):深的一层(预览、属性页)永远在上,打开时它滑入盖住浅层,返回时它滑出露出浅层。
62
63
  .layer 只在过渡期间加:两层都要不透明,否则透出对面;.leaving 是离开的那一层,不接指针,点在它上面等于点在下面的目标页。 */
63
64
  #preview, #info { z-index: 1; }
65
+ .browse { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归里面的三个面板 */
66
+ #panes { position: relative; flex: 1 1 auto; min-height: 0; }
67
+ /* 搜索框(第 18.3 节):常驻在 #browse 顶上,不随列表滚动。字号不小于 16 点:iPhone 上小于 16 点聚焦时页面会放大。颜色只用 --coda-*。 */
68
+ .searchbar { flex: none; padding: 6px 8px; }
69
+ /* 胶囊形(第 18.3 节,仿 Codex 的搜索栏):底色 --coda-fill,平时留 1px 透明边(聚焦时换色不跳动);放大镜和 placeholder 用三级文字色。
70
+ 聚焦:卡片色底、强调色约 55% 的边、3 点强调色约 18% 的光晕,没有 outline。Mac 的字跟正文,iPhone 不小于 16 点(小于 16 点聚焦时页面会放大)。 */
71
+ .sfield { display: flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px; border: 1px solid transparent; border-radius: 999px; background: var(--coda-fill, var(--hover));
72
+ transition: border-color .12s, box-shadow .12s, background-color .12s; }
73
+ .sico { flex: none; display: grid; place-items: center; color: var(--coda-tertiary, #a1a1a6); }
74
+ .sico svg { display: block; width: 14px; height: 14px; }
75
+ .sfield input { flex: 1 1 auto; min-width: 0; height: 100%; padding: 0; border: 0; outline: 0; background: transparent; color: inherit; font: inherit; font-size: 1em; -webkit-user-select: text; user-select: text; -webkit-appearance: none; appearance: none; }
76
+ .sfield input::placeholder { color: var(--coda-tertiary, #a1a1a6); }
77
+ .sfield input::-webkit-search-cancel-button, .sfield input::-webkit-search-decoration { -webkit-appearance: none; display: none; }
78
+ .sfield:focus-within { background: var(--coda-card, var(--coda-background, #fff)); border-color: color-mix(in srgb, var(--coda-accent, #0a84ff) 55%, transparent);
79
+ box-shadow: 0 0 0 3px color-mix(in srgb, var(--coda-accent, #0a84ff) 18%, transparent); }
80
+ .sfield button.sclear { width: 22px; height: 22px; color: var(--coda-tertiary, #a1a1a6); } /* 比 button.icon 的灰色优先 */
81
+ .coda-ios .sfield { height: 36px; padding: 0 12px; gap: 8px; }
82
+ .coda-ios .sfield input { font-size: max(16px, 1em); }
83
+ .coda-ios .sico svg { width: 16px; height: 16px; }
84
+ .coda-ios .searchbar { padding: 8px 16px 4px; }
85
+ @media (prefers-reduced-motion: reduce) { .sfield { transition: none; } }
86
+ /* 结果面板:状态行一行,下面是结果列表(复用目录树的行和虚拟列表)。结果行的名字里匹配的部分加粗。 */
87
+ .search { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; }
88
+ .sstatus { flex: none; display: flex; align-items: center; gap: 8px; min-height: calc(var(--row-h) * .7); padding: 2px 16px; font-size: .85em; }
89
+ .shits { flex: 1 1 auto; min-height: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); container: tree / inline-size; }
90
+ .shits:focus { outline: none; }
91
+ .shits:focus-visible { outline: 1px solid var(--coda-accent, #0a84ff); outline-offset: -2px; }
92
+ .row .name b { font-weight: 700; }
64
93
  .pane.layer { background: var(--coda-background, #fff); }
65
94
  .pane.leaving { pointer-events: none; }
66
95
  /* 滑动期间(约 280 毫秒)整页不响应点按;app.mjs 在过渡开始和结束(含强制结束)时加上和去掉。 */
@@ -79,7 +108,12 @@ html.moving, html.moving body, html.moving #stage { pointer-events: none; }
79
108
  /* 树:只画可见的一段,行高固定(tree.mjs 量 .row 的高度)。
80
109
  Mac 是侧栏列表:左右缩进 8 点,行有圆角,选中用 --coda-selection。 */
81
110
  /* 标题行:根目录名,随内容滚走。它在滚动容器里占着自己的高度,tree.mjs 把 spacer 的 offsetTop 扣掉。 */
82
- .section { padding: 10px 16px 4px; font-size: .85em; font-weight: 600; color: var(--coda-secondary, #6e6e73); cursor: default; }
111
+ /* 字号比 0.8.0 之前(.85em,约 11 点)大一号(.92em,约 12 点):worktree 的项目名是主,后缀 .rn-tag 用回原来的字号(第 19.6 节);普通文件夹名同样大一号,标题不因会话不同忽大忽小。 */
112
+ .section { padding: 10px 16px 4px; font-size: .92em; font-weight: 600; color: var(--coda-secondary, #6e6e73); cursor: default; }
113
+ /* worktree 根目录名的后缀(`worktree-c037`、`wt-c037`,前面的空格也在里面):常规字重、三级灰、比项目名小一号;em 跟着标题走(跟 --coda-font-size,不写死点数)。
114
+ 后缀的颜色在浅色下对比度约 2.6:1(作者选的,第 19.6 节第 4 条),它是次要信息。 */
115
+ .rn-tag { font-weight: 400; font-size: .88em; color: var(--coda-tertiary, #a1a1a6); }
116
+ .section .rn-tag { font-size: .924em; } /* .92em 的标题里 .924em 是约 11 点,即原来标题的字号 */
83
117
  /* 行宽按 #tree 的宽度查询:窄于约 220 点时 Mac 的 meta 不显示。 */
84
118
  #tree { container: tree / inline-size; }
85
119
  .spacer { position: relative; margin: 0 8px; }
@@ -256,3 +290,23 @@ button.icon svg { display: block; }
256
290
  /* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
257
291
  .bar .exit, .bar.exit-only { display: none; } /* 只有返回的工具条(属性页):返回不显示,整条也不占高度 */
258
292
  @media (orientation: landscape) and (max-height: 500px) { .coda-ios .bar .exit { display: inline-flex; } .coda-ios .bar.exit-only { display: flex; } }
293
+
294
+ /* 文件内查找(第 18.6 节):查找条在预览头部下面、正文上面,不随正文滚动。输入框字号不小于 16 点,按钮至少 44 点(iPhone 的最小触摸尺寸在 .coda-ios button 里)。
295
+ 高亮全部匹配和当前这处:颜色从 --coda-accent 用 color-mix 派生(第 9 节),不改文字颜色。 */
296
+ .fbar { flex: none; display: flex; align-items: center; gap: 4px; padding: 4px 12px; border-bottom: 1px solid var(--line); }
297
+ .finput { flex: 1 1 auto; min-width: 0; height: 28px; padding: 0 8px; border: 0; outline: 0; border-radius: var(--coda-radius, 6px); background: var(--coda-fill, var(--hover)); color: inherit; font: inherit; font-size: 1em;
298
+ -webkit-user-select: text; user-select: text; -webkit-appearance: none; appearance: none; }
299
+ .finput::-webkit-search-cancel-button, .finput::-webkit-search-decoration { -webkit-appearance: none; display: none; }
300
+ .finput:focus { outline: 2px solid var(--coda-accent, #0a84ff); outline-offset: -1px; }
301
+ .fcount { flex: none; min-width: 3.2em; text-align: center; white-space: nowrap; font-variant-numeric: tabular-nums; }
302
+ .fbar button { flex: none; white-space: nowrap; }
303
+ .fbar button:disabled { opacity: .35; }
304
+ .fprev svg { transform: rotate(-90deg); } .fnext svg { transform: rotate(90deg); }
305
+ .coda-ios .fbar button { min-height: 44px; min-width: 44px; }
306
+ .coda-ios .finput { height: 36px; font-size: max(16px, 1em); }
307
+ .pbody:focus { outline: none; }
308
+ /* 只在查找开着时生效(.pbody.finding,find.mjs 随查找条的开关加去):这两条规则只要存在,浏览器就要给页面里每个文本节点算高亮样式,
309
+ 实测让打开 128 KiB 的密集上色文件慢了约 27 毫秒(119 → 147),不管规则里写的是背景还是文字色。 */
310
+ .pbody.finding ::highlight(find) { background-color: color-mix(in srgb, var(--coda-accent, #0a84ff) 25%, transparent); }
311
+ /* 当前这处:底色 50%,文字改成正文色(--coda-foreground,和 :root 一样),不显示代码着色,同系统浏览器的查找。全部匹配不改文字颜色。 */
312
+ .pbody.finding ::highlight(find-current) { background-color: color-mix(in srgb, var(--coda-accent, #0a84ff) 50%, transparent); color: var(--coda-foreground, #1d1d1f); }
package/view/tree.mjs CHANGED
@@ -1,9 +1,10 @@
1
1
  // 目录树:visibleRows 把模型压成扁平行(纯逻辑);mountTree 只画可见的那一段,上万条也不卡。
2
- import { EntryKind, SortKey } from './protocol.mjs';
2
+ import { EntryKind, SortKey, isHidden } from './protocol.mjs';
3
3
  import { dateText, infoText, sizeText, subText } from './format.mjs';
4
4
  import { mountGestures } from './gestures.mjs';
5
5
  import { icon, iconFor } from './icons.mjs';
6
- import { isHidden, joinPath, parentOf } from './model.mjs';
6
+ import { joinPath, parentOf } from './model.mjs';
7
+ import { paintRootName } from './rootname.mjs';
7
8
 
8
9
  const OVERSCAN = 8;
9
10
 
@@ -56,18 +57,21 @@ export function stepRows(rows, selected, action) {
56
57
  return up < 0 ? null : { select: up };
57
58
  }
58
59
 
59
- // 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
60
- export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
61
-
62
60
  export const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
63
61
 
64
62
  // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, row), select(path | null) };select 是 Mac 单击选中,不给就是 iPhone 的轻点打开
63
+ // 搜索结果复用这份画行和虚拟列表(设计见 docs/design/phase-3.md 第 18.3 节):rows() 给行(缺省是目录树的 visibleRows),selected() 给选中的路径(缺省 model.selected),
64
+ // view 是按下反馈里的画面名('tree' 或 'search',search 的行带 .hit),title 为假时没有根目录名的分区标题。
65
+ // 结果行多三个字段:place(所在目录,Mac 在右侧、iPhone 在第二行)、bold(名字里要加粗的 [起点, 终点))、chevron: false(没有展开箭头的位置)。
65
66
  // menu 收到整行(路径、种类、大小、修改时间)。
66
67
  // draggable:触屏上拖行没有用处,还会抢滚动,关掉。now:当前时间,日期的相对格式用,测试可注入。
67
68
  // longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
68
69
  // keyboard:滚动容器可以获得焦点,键盘才接得上(Mac)。
69
70
  // press:按下反馈(press.mjs),画行时问它这一行是不是正在按下的;consumed(event):菜单消费掉的按下(gestures.mjs)。
70
- export function mountTree(el, model, handlers, { draggable = true, longPress = false, keyboard = false, now = Date.now, press = null, consumed = () => false } = {}) {
71
+ export function mountTree(el, model, handlers, {
72
+ draggable = true, longPress = false, keyboard = false, now = Date.now, press = null, consumed = () => false,
73
+ rows: rowsOf = () => visibleRows(model), selected = () => model.selected, view = 'tree', title: showTitle = true,
74
+ } = {}) {
71
75
  const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
72
76
  title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
73
77
  title.hidden = true;
@@ -86,11 +90,14 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
86
90
  probe.remove();
87
91
  };
88
92
 
89
- // 分区标题:根目录的名字(root.path 的最后一段),不是 ready 时不显示。iPhone 上由 CSS 隐藏。
93
+ // 分区标题:根目录的名字(第 19 节:worktree 写成「项目名-wt-编号」,放不下时按宽度省略;普通目录是路径的最后一段,交给 CSS),不是 ready 时不显示。iPhone 上由 CSS 隐藏。
94
+ // worktree 的标题悬停显示完整路径。先写完整写法、取消隐藏,再按有了排版的宽度省略。
90
95
  const paintTitle = () => {
91
- const text = model.ready ? rootName(model.root.path) : '';
92
- if (title.textContent !== text) title.textContent = text;
93
- title.hidden = text === '';
96
+ const shown = showTitle && model.ready;
97
+ if (!shown) { title.textContent = ''; title.hidden = true; title.title = ''; return; }
98
+ title.hidden = false;
99
+ paintRootName(title, model.root);
100
+ title.title = model.root.project ? model.root.path : '';
94
101
  };
95
102
 
96
103
  // 标题在滚动容器里,把 spacer 推下去 offsetTop:行的位置按 spacer 内部算,滚动值(保存和恢复的)仍是容器的。
@@ -112,32 +119,35 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
112
119
  if (row.type !== 'entry') {
113
120
  node.textContent = row.text;
114
121
  if (row.retry) { node.classList.add('retry'); node.title = row.text; } // 长原因在行里被截断,Mac 悬停看全
115
- if (row.retry || row.type === 'more') press?.paint(node, 'tree', row.retry ? 'retry' : 'more', row.path);
122
+ if (row.retry || row.type === 'more') press?.paint(node, view, row.retry ? 'retry' : 'more', row.path);
116
123
  return node;
117
124
  }
118
- press?.paint(node, 'tree', 'entry', row.path);
125
+ press?.paint(node, view, 'entry', row.path);
126
+ if (view === 'search') node.classList.add('hit');
119
127
  const dead = row.kind === EntryKind.LINK || row.kind === EntryKind.OTHER;
120
128
  node.classList.add(row.kind);
121
129
  if (dead) node.classList.add('dead');
122
- if (row.path === model.selected) node.classList.add('sel');
130
+ if (row.path === selected()) node.classList.add('sel');
123
131
  if (row.kind === EntryKind.DIR) node.setAttribute('aria-expanded', row.open);
124
132
  if (!dead && draggable) node.draggable = true;
125
133
  // 一份 DOM:箭头、图标、名字(iPhone 下面跟日期 · 大小)、Mac 的 meta、尾标;iPhone 的箭头用 CSS order 排到最后,两端各用 CSS 藏起另一端的那段。
126
134
  const chevron = document.createElement('span'), glyph = document.createElement('span'), main = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
127
135
  chevron.className = 'chev';
128
136
  if (row.busy) chevron.textContent = '…';
129
- else if (!dead) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
137
+ else if (!dead && row.chevron !== false) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
130
138
  if (row.open) node.classList.add('open');
131
139
  glyph.className = 'ico';
132
140
  glyph.append(icon(iconFor(row)));
133
141
  main.className = 'main';
134
142
  label.className = 'name truncate';
135
- label.textContent = row.name;
143
+ if (row.bold) label.append(row.name.slice(0, row.bold[0]), Object.assign(document.createElement('b'), { textContent: row.name.slice(...row.bold) }), row.name.slice(row.bold[1]));
144
+ else label.textContent = row.name;
136
145
  main.append(label);
137
- const parts = [chevron, glyph, main];
138
- if (!dead) {
146
+ const parts = row.chevron === false ? [glyph, main] : [chevron, glyph, main];
147
+ {
139
148
  // 能打开的条目带信息;link、other 没有。文件夹没有的部分整个不画:iPhone 名字垂直居中,Mac 右侧不留空位。
140
- const subContent = subText(row, time), metaContent = model.sort === SortKey.DATE ? dateText(row, time) : sizeText(row);
149
+ // 搜索结果的所在目录(row.place)不受 dead 影响:失效的链接和 other 也要看得出在哪。
150
+ const subContent = row.place ?? (dead ? '' : subText(row, time)), metaContent = row.place ?? (dead ? '' : model.sort === SortKey.DATE ? dateText(row, time) : sizeText(row));
141
151
  if (subContent) {
142
152
  const sub = document.createElement('span');
143
153
  sub.className = 'sub caption secondary truncate';
@@ -154,7 +164,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
154
164
  tail.className = row.failed ? 'tail warn' : 'tail';
155
165
  if (row.failed) tail.append(icon('warning', 14));
156
166
  else if (row.link) tail.append(icon('turn', 14));
157
- const info = row.failed || TITLES[row.kind] ? '' : infoText(row);
167
+ const info = row.failed || TITLES[row.kind] || row.place !== undefined ? '' : infoText(row);
158
168
  node.title = row.failed ? `${row.path}\n读取失败:${row.failed}` : TITLES[row.kind] ? `${row.path}\n${TITLES[row.kind]}` : info ? `${row.path}\n${info}` : row.path;
159
169
  node.append(...parts, tail);
160
170
  return node;
@@ -179,7 +189,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
179
189
  consumed,
180
190
  });
181
191
  // 选中变了只在现有节点上切 .sel,不重建:双击的 dblclick 要落在还在容器里的节点上。
182
- const mark = () => { for (const node of win.children) node.classList.toggle('sel', rows[node.dataset.i]?.path === model.selected && rows[node.dataset.i]?.type === 'entry'); };
192
+ const mark = () => { for (const node of win.children) node.classList.toggle('sel', rows[node.dataset.i]?.path === selected() && rows[node.dataset.i]?.type === 'entry'); };
183
193
  // 滚到这一行能看见,只滚最少的距离;按行号算位置,不找节点(没画出来的行也行)。
184
194
  const reveal = index => {
185
195
  const top = (spacer.offsetTop || 0) + index * rowH;
@@ -187,21 +197,30 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
187
197
  else if (top + rowH > el.scrollTop + el.clientHeight) el.scrollTop = top + rowH - el.clientHeight;
188
198
  paint();
189
199
  };
200
+ // 滚到这一行在可见区中间(定位操作的最后一步);找不到返回 false。rows 要是最新的:调用前先 render()。
201
+ const center = path => {
202
+ const index = rows.findIndex(row => row.type === 'entry' && row.path === path);
203
+ if (index < 0) return false;
204
+ el.scrollTop = Math.max(0, (spacer.offsetTop || 0) + index * rowH - (el.clientHeight - rowH) / 2);
205
+ paint();
206
+ return true;
207
+ };
190
208
  el.addEventListener('scroll', paint, { passive: true });
191
- new ResizeObserver(paint).observe(el);
209
+ new ResizeObserver(() => { paintTitle(); paint(); }).observe(el); // 侧栏宽度变了,标题的省略重算
192
210
 
193
211
  return {
194
- render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
212
+ render() { rows = rowsOf(); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
195
213
  remeasure() { measure(); this.render(); },
196
214
  scroll: () => el.scrollTop,
197
- scrollTo(top) { el.scrollTop = top; },
215
+ scrollTo(top) { el.scrollTop = top; paint(); },
198
216
  select: mark,
217
+ center,
199
218
  activate,
200
219
  focus() { el.focus(); },
201
220
  onKey(fn) { el.addEventListener('keydown', fn); },
202
221
  // 键盘一步:选中移动并滚到能看见,→ ← 展开收起。
203
222
  key(action) {
204
- const step = stepRows(rows, model.selected, action);
223
+ const step = stepRows(rows, selected(), action);
205
224
  if (!step) return;
206
225
  if (step.toggle !== undefined) return handlers.toggle(rows[step.toggle].path);
207
226
  handlers.select(rows[step.select].path);
@@ -209,7 +228,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
209
228
  },
210
229
  // ⌘↓:和双击同一个动作;没有选中或选中的不在了什么都不做。
211
230
  openSelected() {
212
- const row = model.selected === null ? undefined : rows.find(item => item.type === 'entry' && item.path === model.selected);
231
+ const row = selected() === null ? undefined : rows.find(item => item.type === 'entry' && item.path === selected());
213
232
  if (row) activate(row);
214
233
  },
215
234
  // 切会话时调用:长按的等待和吞点击的标记都属于上一个会话。