@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/protocol.mjs CHANGED
@@ -13,18 +13,22 @@ export const SNIFF_BYTES = 8000;
13
13
  export const REQUEST_ID_MAX = 64;
14
14
  // 输入框草稿的文字上限(UTF-16 单元);拼完超过就不写。
15
15
  export const DRAFT_LIMIT = 100_000;
16
+ // 文件名搜索(设计见 docs/design/phase-3.md 第 18 节):搜索词去掉首尾空白后的长度上限(UTF-16 单元)、结果条数上限。
17
+ export const QUERY_MAX = 256;
18
+ export const SEARCH_RESULTS = 500;
16
19
 
17
20
  export const Type = Object.freeze({
18
21
  HELLO: 'hello', REFRESH: 'refresh', LIST: 'list', READ: 'read', STAT: 'stat', DIR_SIZE: 'dir_size', ADD_TEXT: 'add_text',
19
- DIR_SIZE_CANCEL: 'dir_size_cancel', // 没有回答
22
+ SEARCH: 'search',
23
+ DIR_SIZE_CANCEL: 'dir_size_cancel', SEARCH_CANCEL: 'search_cancel', // 没有回答
20
24
  SESSION: 'session', ROOT: 'root', CHANGED: 'changed',
21
- LISTED: 'listed', CHUNK: 'chunk', STATTED: 'statted', SIZED: 'sized', ADDED: 'added',
25
+ LISTED: 'listed', CHUNK: 'chunk', STATTED: 'statted', SIZED: 'sized', ADDED: 'added', SEARCHED: 'searched',
22
26
  });
23
27
 
24
28
  // 请求 → 回答的类型。模块回答、页面分发都查这一张。
25
29
  export const ReplyOf = Object.freeze({
26
30
  [Type.LIST]: Type.LISTED, [Type.READ]: Type.CHUNK, [Type.STAT]: Type.STATTED,
27
- [Type.DIR_SIZE]: Type.SIZED, [Type.ADD_TEXT]: Type.ADDED,
31
+ [Type.DIR_SIZE]: Type.SIZED, [Type.ADD_TEXT]: Type.ADDED, [Type.SEARCH]: Type.SEARCHED,
28
32
  });
29
33
  export const isReply = type => Object.values(ReplyOf).includes(type);
30
34
 
@@ -34,7 +38,7 @@ export const ErrorCode = Object.freeze({
34
38
  NO_ROOT: 'no_root', INVALID_PATH: 'invalid_path', OUTSIDE_ROOT: 'outside_root', NOT_FOUND: 'not_found',
35
39
  NOT_DIR: 'not_dir', NOT_FILE: 'not_file', DENIED: 'denied', CURSOR_EXPIRED: 'cursor_expired',
36
40
  INVALID_OFFSET: 'invalid_offset', INVALID_SORT: 'invalid_sort', TOO_LARGE: 'too_large', IO: 'io_error',
37
- DRAFT_FAILED: 'draft_failed', DRAFT_TOO_LONG: 'draft_too_long',
41
+ DRAFT_FAILED: 'draft_failed', DRAFT_TOO_LONG: 'draft_too_long', INVALID_QUERY: 'invalid_query',
38
42
  });
39
43
 
40
44
  // 目录排序:值是下面四种之一,缺省(请求不带 sort)是名称。方向由排序键决定,只在 lib/tree.mjs 里定义一处。
@@ -74,6 +78,34 @@ export function splitRelative(path) {
74
78
  return { segments };
75
79
  }
76
80
 
81
+ // 隐藏名单:按全名比。目录树、图标视图的「显示隐藏文件」和文件名搜索(模块和页面)共用这一处。
82
+ const HIDDEN = new Set(['.git', '.DS_Store']);
83
+ export const isHidden = name => HIDDEN.has(name);
84
+
85
+ // 文件名搜索的比较:名字和搜索词都先 NFC 规范化再 toLowerCase,包含就算。模块判断匹配、页面加粗用同一个函数。
86
+ export const foldName = text => text.normalize('NFC').toLowerCase();
87
+
88
+ // 页面加粗:搜索词(已 foldName)在名字里第一次出现的位置,返回原名字里的 [起点, 终点)(UTF-16 下标),没有或映射不出来返回 null。
89
+ // 比较是在规范化后的串上做的:把原名字切成「一个基字符加它的组合符号」,每段各自规范化、变小写,拼起来要等于整串的结果,
90
+ // 这样才能建「规范化后的位置 → 原名字的位置」的对照;拼不起来(如谚文字母的组合)、命中段的两端落在一段中间(一个字符变小写后变成两个)都不加粗。
91
+ const CLUSTER = /\P{M}\p{M}*|\p{M}+/gu;
92
+ export function matchSpan(name, folded) {
93
+ const whole = foldName(name), at = whole.indexOf(folded);
94
+ if (at < 0 || folded === '') return null;
95
+ const starts = new Map(), ends = new Map(); // 规范化后的位置 → 原名字的位置
96
+ let from = 0, to = 0, out = '';
97
+ for (const [piece] of name.matchAll(CLUSTER)) {
98
+ const folding = foldName(piece);
99
+ starts.set(from, to);
100
+ from += folding.length;
101
+ to += piece.length;
102
+ ends.set(from, to);
103
+ out += folding;
104
+ }
105
+ if (out !== whole || !starts.has(at) || !ends.has(at + folded.length)) return null;
106
+ return [starts.get(at), ends.get(at + folded.length)];
107
+ }
108
+
77
109
  export const messageSize = message => JSON.stringify({ message }).length;
78
110
 
79
111
  // 带 code 的错误,模块里到处抛,主模块转成回答里的 error。
@@ -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
@@ -23,6 +23,9 @@
23
23
  --tone-gray: #a3a3a9; --tone-blue: #6aa9ec; --tone-yellow: #e3b63e; --tone-green: #63c77f; --tone-teal: #58c3dd;
24
24
  --tone-orange: #f0964f; --tone-red: #f0717a; --tone-purple: #ac87ec; --tone-pink: #ee77ac;
25
25
  }
26
+ /* 菜单的阴影(第 9 节第 1 条的第三个例外):主题变量里没有阴影,深浅各一套,深色更重才看得出。 */
27
+ :root { --shadow-menu: 0 6px 20px rgba(0,0,0,.18), 0 1px 3px rgba(0,0,0,.12); }
28
+ :root.coda-dark { --shadow-menu: 0 8px 24px rgba(0,0,0,.55), 0 1px 3px rgba(0,0,0,.4); }
26
29
  /* 代码高亮的颜色,深浅各一套;自己定的,不照搬任何现成主题。每个 token 的最终颜色在代码区底色上不低于 4.5:1(harness 读真实计算值检查);改色值要重新跑。 */
27
30
  :root {
28
31
  --hl-keyword: #a2218f; --hl-string: #b82a1f; --hl-comment: #5f6b76; --hl-number: #2d3fc4; --hl-title: #0b62a0; --hl-type: #1b7280; --hl-attr: #7a5200;
@@ -38,12 +41,12 @@
38
41
  .tone-red { color: var(--tone-red); } .tone-purple { color: var(--tone-purple); } .tone-pink { color: var(--tone-pink); }
39
42
  * { box-sizing: border-box; }
40
43
  html, body { margin: 0; height: 100%; }
41
- body { display: flex; flex-direction: column; overflow: hidden; -webkit-user-select: none; user-select: none;
44
+ body { display: flex; flex-direction: column; overflow: hidden; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; /* 系统默认的点击闪烁是不带圆角的灰块:按下反馈归 .pressed */
42
45
  padding: var(--safe-top) var(--safe-right) 0 var(--safe-left); }
43
46
  .selectable { -webkit-user-select: text; user-select: text; }
44
- button { font: inherit; color: inherit; background: none; border: 0; padding: 0; text-align: inherit; cursor: pointer; }
47
+ button { font: inherit; color: inherit; background: none; border: 0; padding: 0; text-align: inherit; cursor: default; } /* 原生 Mac 应用的按钮是箭头 */
45
48
  button:focus-visible { outline: 2px solid var(--coda-accent, #0a84ff); outline-offset: 2px; border-radius: 6px; }
46
- button.link { color: var(--coda-accent, #0a84ff); }
49
+ button.link { color: var(--coda-accent, #0a84ff); border-radius: var(--coda-radius, 6px); }
47
50
  p { margin: 0; }
48
51
  .secondary { color: var(--coda-secondary, #6e6e73); }
49
52
  .caption { font-size: .85em; }
@@ -51,10 +54,46 @@ p { margin: 0; }
51
54
  .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
52
55
  [hidden] { display: none !important; }
53
56
 
54
- /* 面板(目录树、图标、预览、说明、属性页)叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。 */
55
- #stage { position: relative; flex: 1 1 auto; min-height: 0; }
57
+ /* 面板(目录树、图标、预览、说明、属性页)叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。
58
+ 两级:外层是层(#browse、#preview、#notice、#info),#browse 里再叠三个面板(目录树、图标、搜索结果),搜索框在它们上面。 */
59
+ #stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; } /* 裁掉层级过渡时横向溢出的那一层;菜单、提示是它的兄弟节点,不受裁切 */
56
60
  .pane { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
57
61
  .pane.off { visibility: hidden; pointer-events: none; }
62
+ /* iPhone 层级切换(第 17.6 节):深的一层(预览、属性页)永远在上,打开时它滑入盖住浅层,返回时它滑出露出浅层。
63
+ .layer 只在过渡期间加:两层都要不透明,否则透出对面;.leaving 是离开的那一层,不接指针,点在它上面等于点在下面的目标页。 */
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; }
93
+ .pane.layer { background: var(--coda-background, #fff); }
94
+ .pane.leaving { pointer-events: none; }
95
+ /* 滑动期间(约 280 毫秒)整页不响应点按;app.mjs 在过渡开始和结束(含强制结束)时加上和去掉。 */
96
+ html.moving, html.moving body, html.moving #stage { pointer-events: none; }
58
97
  .empty { display: grid; align-content: start; justify-items: center; gap: 6px; padding: 32px 20px; text-align: center; }
59
98
  .notice-icon { display: grid; place-items: center; margin-bottom: 6px; color: var(--coda-secondary, #6e6e73); }
60
99
  .notice-icon svg { display: block; color: inherit; } /* 说明区的图标不上色 */
@@ -139,7 +178,7 @@ button.icon svg { display: block; }
139
178
  /* 键盘焦点:选中的高亮已经表示位置,点击时不画外框;只在键盘操作(:focus-visible)时给容器内边一圈细线。 */
140
179
  #tree:focus, .gbody:focus { outline: none; }
141
180
  #tree:focus-visible, .gbody:focus-visible { outline: 1px solid var(--coda-accent, #0a84ff); outline-offset: -2px; }
142
- .menu { position: fixed; z-index: 10; width: max-content; max-width: min(280px, calc(100vw - var(--safe-left) - var(--safe-right) - 8px)); padding: 4px; border: 1px solid var(--coda-outline, var(--line)); border-radius: var(--coda-radius-card, 10px);
181
+ .menu { position: fixed; z-index: 10; box-shadow: var(--shadow-menu); width: max-content; max-width: min(280px, calc(100vw - var(--safe-left) - var(--safe-right) - 8px)); padding: 4px; border: 1px solid var(--coda-outline, var(--line)); border-radius: var(--coda-radius-card, 10px);
143
182
  background: var(--coda-card, var(--coda-background, #fff));
144
183
  /* 高度有上限:内容多(长路径加五项)时在菜单里滚动,不超出安全区;位置由 menu.mjs 夹在安全区内。 */
145
184
  max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 8px); overflow-y: auto; overscroll-behavior: contain; }
@@ -194,7 +233,6 @@ button.icon svg { display: block; }
194
233
  .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); }
195
234
  .gnote.more, .gnote.retry { color: var(--coda-accent, #0a84ff); cursor: pointer; }
196
235
  .coda-ios .cell { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单 */
197
- .coda-ios .cell:active { background: var(--hover); } /* 按下:和列表的行一样 */
198
236
 
199
237
  /* 悬停只在有鼠标时:触摸后不会留着。 */
200
238
  @media (hover: hover) {
@@ -204,6 +242,15 @@ button.icon svg { display: block; }
204
242
  .menu button:hover { background: var(--coda-selection, var(--hover)); }
205
243
  }
206
244
 
245
+ /* 按下(press.mjs 加 .pressed,第 17.3 节):只改底色,不改尺寸和文字色。最终的颜色是原底色上再叠一层 --hover:
246
+ 普通的行和按钮是两层 --hover,选中的是 --coda-selection 加一层 --hover。第二层用内阴影而不是 background-image,淡出时两层都能过渡。
247
+ 放在悬停之后、选择器的优先级不低于悬停:按在悬停着的元素上也是这个颜色。 */
248
+ .row.entry.pressed, .row.more.pressed, .row.retry.pressed, .cell.pressed, .gnote.pressed, button.pressed, .bar button.pressed, button.icon.pressed {
249
+ background-color: var(--hover); box-shadow: inset 0 0 0 100vmax var(--hover); }
250
+ .row.entry.sel.pressed, .cell.sel.pressed, .menu button.pressed { background-color: var(--coda-selection, var(--hover)); }
251
+ /* iPhone 正常抬起时约 200 毫秒淡出(.fade 由 press.mjs 在去掉 .pressed 的同时加上,减弱动态效果时不加);Mac 立刻去掉。 */
252
+ .coda-ios .fade { transition: background-color .2s ease-out, box-shadow .2s ease-out; }
253
+
207
254
  /* iPhone:行和按钮至少 44 点;行高 58 点(两行:名字、日期 · 大小)在这里定,tree.mjs 量 .row 得到,不另写一份。
208
255
  目录树是 Coda 卡片外形的分组列表:.spacer 就是卡片(高度已等于全部行高),首尾圆角不用逐行判断。 */
209
256
  .coda-ios { --row-h: 58px; --indent: 20px; --pad: 16px; }
@@ -216,7 +263,6 @@ button.icon svg { display: block; }
216
263
  left: calc(var(--pad) + var(--indent) * var(--d, 0) + 22px + 8px); } /* 从图标之后开始 */
217
264
  .coda-ios .row.sel + .row::before, .coda-ios .row.sel::before { background: transparent; }
218
265
  .coda-ios .row { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单;选择和拖动另外已关 */
219
- .coda-ios .row.entry:active { background: var(--hover); }
220
266
  .coda-ios .ico svg { width: 22px; height: 22px; }
221
267
  .coda-ios .main { display: flex; flex-direction: column; justify-content: center; } /* 没有第二行的条目,名字垂直居中 */
222
268
  .coda-ios .sub { display: block; }
@@ -234,9 +280,28 @@ button.icon svg { display: block; }
234
280
  .coda-ios .preview:not(.info) .pbody { padding-bottom: var(--safe-bottom); } /* 资源页里没有底部工具条:最后一行不被 Home 指示条盖住 */
235
281
  .coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
236
282
  .coda-ios .menu button { padding: 0 10px; }
237
- .coda-ios .menu button:active { background: var(--coda-selection, var(--hover)); }
238
283
  .coda-ios .menu-head { padding: 6px 10px 2px; }
239
284
 
240
285
  /* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
241
286
  .bar .exit, .bar.exit-only { display: none; } /* 只有返回的工具条(属性页):返回不显示,整条也不占高度 */
242
287
  @media (orientation: landscape) and (max-height: 500px) { .coda-ios .bar .exit { display: inline-flex; } .coda-ios .bar.exit-only { display: flex; } }
288
+
289
+ /* 文件内查找(第 18.6 节):查找条在预览头部下面、正文上面,不随正文滚动。输入框字号不小于 16 点,按钮至少 44 点(iPhone 的最小触摸尺寸在 .coda-ios button 里)。
290
+ 高亮全部匹配和当前这处:颜色从 --coda-accent 用 color-mix 派生(第 9 节),不改文字颜色。 */
291
+ .fbar { flex: none; display: flex; align-items: center; gap: 4px; padding: 4px 12px; border-bottom: 1px solid var(--line); }
292
+ .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;
293
+ -webkit-user-select: text; user-select: text; -webkit-appearance: none; appearance: none; }
294
+ .finput::-webkit-search-cancel-button, .finput::-webkit-search-decoration { -webkit-appearance: none; display: none; }
295
+ .finput:focus { outline: 2px solid var(--coda-accent, #0a84ff); outline-offset: -1px; }
296
+ .fcount { flex: none; min-width: 3.2em; text-align: center; white-space: nowrap; font-variant-numeric: tabular-nums; }
297
+ .fbar button { flex: none; white-space: nowrap; }
298
+ .fbar button:disabled { opacity: .35; }
299
+ .fprev svg { transform: rotate(-90deg); } .fnext svg { transform: rotate(90deg); }
300
+ .coda-ios .fbar button { min-height: 44px; min-width: 44px; }
301
+ .coda-ios .finput { height: 36px; font-size: max(16px, 1em); }
302
+ .pbody:focus { outline: none; }
303
+ /* 只在查找开着时生效(.pbody.finding,find.mjs 随查找条的开关加去):这两条规则只要存在,浏览器就要给页面里每个文本节点算高亮样式,
304
+ 实测让打开 128 KiB 的密集上色文件慢了约 27 毫秒(119 → 147),不管规则里写的是背景还是文字色。 */
305
+ .pbody.finding ::highlight(find) { background-color: color-mix(in srgb, var(--coda-accent, #0a84ff) 25%, transparent); }
306
+ /* 当前这处:底色 50%,文字改成正文色(--coda-foreground,和 :root 一样),不显示代码着色,同系统浏览器的查找。全部匹配不改文字颜色。 */
307
+ .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,9 @@
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
7
 
8
8
  const OVERSCAN = 8;
9
9
 
@@ -62,11 +62,18 @@ export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
62
62
  export const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
63
63
 
64
64
  // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, row), select(path | null) };select 是 Mac 单击选中,不给就是 iPhone 的轻点打开
65
+ // 搜索结果复用这份画行和虚拟列表(设计见 docs/design/phase-3.md 第 18.3 节):rows() 给行(缺省是目录树的 visibleRows),selected() 给选中的路径(缺省 model.selected),
66
+ // view 是按下反馈里的画面名('tree' 或 'search',search 的行带 .hit),title 为假时没有根目录名的分区标题。
67
+ // 结果行多三个字段:place(所在目录,Mac 在右侧、iPhone 在第二行)、bold(名字里要加粗的 [起点, 终点))、chevron: false(没有展开箭头的位置)。
65
68
  // menu 收到整行(路径、种类、大小、修改时间)。
66
69
  // draggable:触屏上拖行没有用处,还会抢滚动,关掉。now:当前时间,日期的相对格式用,测试可注入。
67
70
  // longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
68
71
  // keyboard:滚动容器可以获得焦点,键盘才接得上(Mac)。
69
- export function mountTree(el, model, handlers, { draggable = true, longPress = false, keyboard = false, now = Date.now } = {}) {
72
+ // press:按下反馈(press.mjs),画行时问它这一行是不是正在按下的;consumed(event):菜单消费掉的按下(gestures.mjs)。
73
+ export function mountTree(el, model, handlers, {
74
+ draggable = true, longPress = false, keyboard = false, now = Date.now, press = null, consumed = () => false,
75
+ rows: rowsOf = () => visibleRows(model), selected = () => model.selected, view = 'tree', title: showTitle = true,
76
+ } = {}) {
70
77
  const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
71
78
  title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
72
79
  title.hidden = true;
@@ -87,7 +94,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
87
94
 
88
95
  // 分区标题:根目录的名字(root.path 的最后一段),不是 ready 时不显示。iPhone 上由 CSS 隐藏。
89
96
  const paintTitle = () => {
90
- const text = model.ready ? rootName(model.root.path) : '';
97
+ const text = showTitle && model.ready ? rootName(model.root.path) : '';
91
98
  if (title.textContent !== text) title.textContent = text;
92
99
  title.hidden = text === '';
93
100
  };
@@ -107,33 +114,39 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
107
114
  node.className = `row ${row.type}`;
108
115
  node.dataset.i = index;
109
116
  node.style.cssText = `--d: ${row.depth}`; // 缩进由 CSS 按 --d 算,分隔线的起点也用它
117
+ node.dataset.path = row.path; // 按下反馈认的身份:同一路径的条目、「重试」说明行、「再显示」靠种类区分
110
118
  if (row.type !== 'entry') {
111
119
  node.textContent = row.text;
112
120
  if (row.retry) { node.classList.add('retry'); node.title = row.text; } // 长原因在行里被截断,Mac 悬停看全
121
+ if (row.retry || row.type === 'more') press?.paint(node, view, row.retry ? 'retry' : 'more', row.path);
113
122
  return node;
114
123
  }
124
+ press?.paint(node, view, 'entry', row.path);
125
+ if (view === 'search') node.classList.add('hit');
115
126
  const dead = row.kind === EntryKind.LINK || row.kind === EntryKind.OTHER;
116
127
  node.classList.add(row.kind);
117
128
  if (dead) node.classList.add('dead');
118
- if (row.path === model.selected) node.classList.add('sel');
129
+ if (row.path === selected()) node.classList.add('sel');
119
130
  if (row.kind === EntryKind.DIR) node.setAttribute('aria-expanded', row.open);
120
131
  if (!dead && draggable) node.draggable = true;
121
132
  // 一份 DOM:箭头、图标、名字(iPhone 下面跟日期 · 大小)、Mac 的 meta、尾标;iPhone 的箭头用 CSS order 排到最后,两端各用 CSS 藏起另一端的那段。
122
133
  const chevron = document.createElement('span'), glyph = document.createElement('span'), main = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
123
134
  chevron.className = 'chev';
124
135
  if (row.busy) chevron.textContent = '…';
125
- else if (!dead) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
136
+ else if (!dead && row.chevron !== false) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
126
137
  if (row.open) node.classList.add('open');
127
138
  glyph.className = 'ico';
128
139
  glyph.append(icon(iconFor(row)));
129
140
  main.className = 'main';
130
141
  label.className = 'name truncate';
131
- label.textContent = row.name;
142
+ 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]));
143
+ else label.textContent = row.name;
132
144
  main.append(label);
133
- const parts = [chevron, glyph, main];
134
- if (!dead) {
145
+ const parts = row.chevron === false ? [glyph, main] : [chevron, glyph, main];
146
+ {
135
147
  // 能打开的条目带信息;link、other 没有。文件夹没有的部分整个不画:iPhone 名字垂直居中,Mac 右侧不留空位。
136
- const subContent = subText(row, time), metaContent = model.sort === SortKey.DATE ? dateText(row, time) : sizeText(row);
148
+ // 搜索结果的所在目录(row.place)不受 dead 影响:失效的链接和 other 也要看得出在哪。
149
+ const subContent = row.place ?? (dead ? '' : subText(row, time)), metaContent = row.place ?? (dead ? '' : model.sort === SortKey.DATE ? dateText(row, time) : sizeText(row));
137
150
  if (subContent) {
138
151
  const sub = document.createElement('span');
139
152
  sub.className = 'sub caption secondary truncate';
@@ -150,7 +163,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
150
163
  tail.className = row.failed ? 'tail warn' : 'tail';
151
164
  if (row.failed) tail.append(icon('warning', 14));
152
165
  else if (row.link) tail.append(icon('turn', 14));
153
- const info = row.failed || TITLES[row.kind] ? '' : infoText(row);
166
+ const info = row.failed || TITLES[row.kind] || row.place !== undefined ? '' : infoText(row);
154
167
  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;
155
168
  node.append(...parts, tail);
156
169
  return node;
@@ -172,9 +185,10 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
172
185
  menu: (event, row) => handlers.menu(event, row),
173
186
  dragText: row => row.path,
174
187
  longPress,
188
+ consumed,
175
189
  });
176
190
  // 选中变了只在现有节点上切 .sel,不重建:双击的 dblclick 要落在还在容器里的节点上。
177
- 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'); };
191
+ const mark = () => { for (const node of win.children) node.classList.toggle('sel', rows[node.dataset.i]?.path === selected() && rows[node.dataset.i]?.type === 'entry'); };
178
192
  // 滚到这一行能看见,只滚最少的距离;按行号算位置,不找节点(没画出来的行也行)。
179
193
  const reveal = index => {
180
194
  const top = (spacer.offsetTop || 0) + index * rowH;
@@ -182,21 +196,30 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
182
196
  else if (top + rowH > el.scrollTop + el.clientHeight) el.scrollTop = top + rowH - el.clientHeight;
183
197
  paint();
184
198
  };
199
+ // 滚到这一行在可见区中间(定位操作的最后一步);找不到返回 false。rows 要是最新的:调用前先 render()。
200
+ const center = path => {
201
+ const index = rows.findIndex(row => row.type === 'entry' && row.path === path);
202
+ if (index < 0) return false;
203
+ el.scrollTop = Math.max(0, (spacer.offsetTop || 0) + index * rowH - (el.clientHeight - rowH) / 2);
204
+ paint();
205
+ return true;
206
+ };
185
207
  el.addEventListener('scroll', paint, { passive: true });
186
208
  new ResizeObserver(paint).observe(el);
187
209
 
188
210
  return {
189
- render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
211
+ render() { rows = rowsOf(); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
190
212
  remeasure() { measure(); this.render(); },
191
213
  scroll: () => el.scrollTop,
192
- scrollTo(top) { el.scrollTop = top; },
214
+ scrollTo(top) { el.scrollTop = top; paint(); },
193
215
  select: mark,
216
+ center,
194
217
  activate,
195
218
  focus() { el.focus(); },
196
219
  onKey(fn) { el.addEventListener('keydown', fn); },
197
220
  // 键盘一步:选中移动并滚到能看见,→ ← 展开收起。
198
221
  key(action) {
199
- const step = stepRows(rows, model.selected, action);
222
+ const step = stepRows(rows, selected(), action);
200
223
  if (!step) return;
201
224
  if (step.toggle !== undefined) return handlers.toggle(rows[step.toggle].path);
202
225
  handlers.select(rows[step.select].path);
@@ -204,7 +227,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
204
227
  },
205
228
  // ⌘↓:和双击同一个动作;没有选中或选中的不在了什么都不做。
206
229
  openSelected() {
207
- const row = model.selected === null ? undefined : rows.find(item => item.type === 'entry' && item.path === model.selected);
230
+ const row = selected() === null ? undefined : rows.find(item => item.type === 'entry' && item.path === selected());
208
231
  if (row) activate(row);
209
232
  },
210
233
  // 切会话时调用:长按的等待和吞点击的标记都属于上一个会话。