@coda-console/resource-manager 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,9 +11,9 @@ Coda 插件:会话旁的文件浏览器。浏览这个会话的工作目录,
11
11
  - 文本预览(带行号,可跨行选中复制,最多读 1 MiB)和图片预览(PNG、JPEG、GIF、WebP、SVG,最多 8 MiB)。
12
12
  - Agent 一轮结束后,已展开的目录和正在看的文件自动更新。
13
13
  - 切换会话时换成新会话的内容,切回来恢复展开状态和滚动位置。
14
- - Mac:右键复制相对或绝对路径,或把文件拖到输入框;预览可以开到独立窗口。
15
- - iPhone:长按复制路径;文件页顶部有「复制路径」。
16
- - 默认隐藏 `.git`、`.DS_Store`,工具栏可切换显示全部。
14
+ - Mac:右键复制相对或绝对路径,或把文件拖到输入框;预览可以开到独立窗口;工具栏只有一个「更多」按钮,按下弹出菜单(刷新、显示隐藏文件),侧栏预览头部最左边的「返回」回到目录树。
15
+ - iPhone:长按复制路径;目录树页右上角「⋯」里有刷新和显示隐藏文件;文件页底部工具条有刷新和「复制路径」。
16
+ - 默认隐藏 `.git`、`.DS_Store`,Mac 在工具栏的「更多」菜单里、iPhone 在目录树页右上角的「⋯」里可切换显示全部。
17
17
 
18
18
  只能读这个会话的工作目录。指向工作目录之外的符号链接只显示,不能打开。
19
19
 
@@ -36,6 +36,11 @@ Coda 插件:会话旁的文件浏览器。浏览这个会话的工作目录,
36
36
  # 不进 Coda,用假的 window.coda 在浏览器里看真实页面
37
37
  "$CODA_NODE" test/harness/server.mjs <会话A的目录> [会话B的目录] [端口]
38
38
 
39
+ # 真实布局的断言和截图:无头 Chrome 打开 Mac 侧栏、iPhone 竖屏和横屏的目录树与文件页(浅色、深色),
40
+ # 量行高、工具条、菜单的位置和尺寸。自己起服务和临时目录,用完删掉;截图在系统临时目录里,路径在最后打印。
41
+ # 失败时退出码非 0。Chrome 不在默认位置时把路径作参数传入
42
+ "$CODA_NODE" test/harness/layout.mjs [Chrome 的路径]
43
+
39
44
  # 契约检查,<提交> 用 coda-console/coda-plugins 的提交号
40
45
  npx --yes github:coda-console/coda-plugins#<提交> . --manifest
41
46
  ```
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "@coda-console/resource-manager",
4
4
  "displayName": "资源管理器",
5
5
  "description": "会话旁的只读文件浏览器:浏览会话工作目录,预览文本和图片,把路径交给对话。",
6
- "version": "0.2.0",
6
+ "version": "0.3.0",
7
7
  "categories": ["工作流程"],
8
8
  "license": "MIT",
9
9
  "repository": {
package/view/app.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
  // 资源管理器的页面入口:消息收发、状态保留、工具栏。状态在 model.mjs,渲染在 tree.mjs 和 preview.mjs。
2
2
  import { EXIT, paintBar } from './bar.mjs';
3
3
  import { connect } from './connection.mjs';
4
+ import { icon } from './icons.mjs';
4
5
  import { Model, STATE_LIMIT, noticeText, parseState, putSession, trimState } from './model.mjs';
5
6
  import { showNotice } from './notice.mjs';
6
7
  import { mountPreview } from './preview.mjs';
7
8
  import { RootState } from './protocol.mjs';
8
- import { mountTree } from './tree.mjs';
9
+ import { mountTree, rootName } from './tree.mjs';
9
10
 
10
11
  const coda = window.coda;
11
12
  const $ = id => document.getElementById(id);
@@ -26,11 +27,12 @@ const tree = mountTree($('tree'), model, {
26
27
  toggle: path => model.toggle(path),
27
28
  more: path => model.loadMore(path),
28
29
  retry: path => model.retry(path),
29
- menu: (event, path) => showMenu(event, path),
30
+ menu: (event, path) => pathMenu(event, path),
30
31
  }, { draggable: !isIOS, longPress: isIOS });
31
32
  const preview = mountPreview($('preview'), {
32
33
  read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text),
33
- actions: isIOS ? [] : [{ label: '窗口', run: openInView }],
34
+ actions: isIOS ? [] : [{ label: '窗口', icon: 'window', run: openInView }], copyInHead: !isIOS,
35
+ back: isIOS ? null : () => { backPressed = true; model.select(null); },
34
36
  });
35
37
 
36
38
  // ---- 在独立视图里打开:iPhone 点文件,Mac 预览头部的「窗口」 ----
@@ -77,6 +79,8 @@ function schedule() {
77
79
  queueMicrotask(() => { queued = false; render(); });
78
80
  }
79
81
 
82
+ // 用户点了预览头部的返回:render 据此决定焦点要不要交给目录树,别的模式变化不动焦点。
83
+ let backPressed = false;
80
84
  const mode = () => (!model.ready ? 'notice' : model.selected !== null && !isIOS ? 'preview' : 'tree');
81
85
 
82
86
  // 说明区只在内容变了才重画,免得重试按钮在点击中途被换掉。
@@ -91,6 +95,8 @@ function paintNotice() {
91
95
 
92
96
  function render() {
93
97
  const current = mode();
98
+ const refocus = backPressed && document.hasFocus();
99
+ backPressed = false;
94
100
  for (const [id, name] of [['tree', 'tree'], ['preview', 'preview'], ['notice', 'notice']]) {
95
101
  $(id).classList.toggle('off', current !== name);
96
102
  $(id).inert = current !== name;
@@ -101,27 +107,49 @@ function render() {
101
107
  preview.open(model.selected, pending.preview);
102
108
  pending.preview = null;
103
109
  } else if (current !== 'preview' && preview.path !== null) preview.close();
104
- syncToolbar(current);
110
+ // 返回按钮随预览一起 inert,焦点会掉到页面外:用户刚点了返回、页面又持有焦点时交给目录树。
111
+ // 后台刷新、切会话、root 变化造成的切换不动焦点,免得抢走客户端输入框的焦点。
112
+ if (refocus && current === 'tree') { $('tree').tabIndex = -1; $('tree').focus(); }
113
+ syncToolbar();
105
114
  }
106
115
 
107
- // ---- 工具栏:Mac 用 coda.toolbar,没有的(iOS)退成页内一条 ----
108
- const toolbarItems = current => [
109
- ...(current === 'preview' ? [{ id: 'back', label: '返回', icon: 'chevron.left', kind: 'button' }] : []),
110
- { id: 'refresh', label: '刷新', icon: 'arrow.clockwise', kind: 'button' },
111
- { id: 'hidden', label: '显示隐藏文件', icon: 'eye', kind: 'toggle', on: model.showHidden },
112
- ];
116
+ // ---- 工具栏:Mac 用 coda.toolbar,没有的(iOS、harness)退成页内一条 ----
117
+ // Mac 只有一个原生按钮「更多」:原生菜单带向下的箭头,关不掉,所以按下后由页面弹菜单。返回键在预览头部,不在这里。
118
+ const toolbarItems = () => hasToolbar
119
+ ? [{ id: 'more', label: '更多', icon: 'ellipsis', kind: 'button' }]
120
+ : [
121
+ { id: 'refresh', label: '刷新', icon: 'arrow.clockwise', kind: 'button' },
122
+ { id: 'hidden', label: '显示隐藏文件', icon: 'eye', kind: 'toggle', on: model.showHidden },
123
+ ];
124
+ // iPhone 树页顶部:返回(仅横屏)、根目录名、⋯。刷新和显示隐藏文件在 ⋯ 的菜单里。
125
+ const MORE = { id: 'more', label: '更多', kind: 'button', glyph: 'ellipsis', iconOnly: true, menu: true };
126
+ const phoneItems = () => [EXIT, { kind: 'title', label: model.ready ? rootName(model.root.path) : '' }, MORE];
127
+ // 比较键就是要画的项:根目录名变了(换会话)工具条跟着重画。
113
128
  let lastItems = '';
114
- function syncToolbar(current) {
115
- const items = toolbarItems(current), key = JSON.stringify(items);
129
+ function syncToolbar() {
130
+ const items = isIOS ? phoneItems() : toolbarItems(), key = JSON.stringify(items);
116
131
  if (key === lastItems) return;
117
132
  lastItems = key;
118
133
  if (hasToolbar) return quiet(() => coda.toolbar(items));
119
- paintBar($('bar'), isIOS ? [EXIT, ...items] : items, act);
134
+ hideMenu(); // 重画会换掉 ⋯ 按钮,开着的菜单认不得新按钮
135
+ paintBar($('bar'), items, act);
120
136
  }
121
137
 
122
- function act(id) {
138
+ // 原生按钮没有 DOM 元素可以当锚点:用一个固定在页面右上角的假锚点,同时也是「谁打开的菜单」的标记。
139
+ const TOOLBAR = { getBoundingClientRect: () => ({ right: window.innerWidth, bottom: 0 }) };
140
+ // 「更多」菜单的内容只写这一处,iPhone 的 ⋯ 和 Mac 的原生按钮共用。
141
+ const moreMenu = () => ({ items: [
142
+ { label: '刷新', run: () => act('refresh') },
143
+ { label: '显示隐藏文件', checked: model.showHidden, run: () => act('hidden') },
144
+ ] });
145
+
146
+ function act(id, button) {
123
147
  if (id === EXIT.id) quiet(() => coda.close());
124
- else if (id === 'back') model.select(null);
148
+ else if (id === 'more') {
149
+ // 键盘激活没有 pointerdown,菜单外点击那条规则不管用:同一个按钮再次激活就关闭。
150
+ if (!menu.hidden && opener === button) hideMenu();
151
+ else openMenu(button, moreMenu());
152
+ }
125
153
  else if (id === 'hidden') { model.setShowHidden(!model.showHidden); persisted.hidden = model.showHidden; }
126
154
  else if (id === 'refresh') {
127
155
  conn.refresh();
@@ -129,7 +157,8 @@ function act(id) {
129
157
  preview.refresh();
130
158
  }
131
159
  }
132
- quiet(() => coda.on('toolbar', ({ id }) => act(id)));
160
+ // 原生按钮的事件是 { id }:只认 more,别的什么都不做。
161
+ quiet(() => coda.on('toolbar', event => { if (event?.id === 'more') act('more', TOOLBAR); }));
133
162
 
134
163
  // ---- 状态保留 ----
135
164
  let saveTimer = null;
@@ -154,26 +183,47 @@ quiet(() => coda.on('visibility', visible => { if (!visible) saveNow(); }));
154
183
  $('tree').addEventListener('scroll', queueSave, { passive: true });
155
184
  preview.onScroll(queueSave);
156
185
 
157
- // ---- 复制路径的菜单:Mac 右键、iPhone 长按 ----
158
- // 菜单顶上写完整的相对路径:iPhone 没有 title 悬停,长文件名在行里被截断,这里看全。
186
+ // ---- 菜单:Mac 右键、iPhone 长按出复制路径,iPhone 的 ⋯ 和 Mac 的原生「更多」出刷新和显示隐藏文件 ----
187
+ // anchor:按钮元素或同样有 getBoundingClientRect 的假锚点(菜单贴在它下面、右对齐)或 { clientX, clientY } 的点。items:[{ label, run, checked? }],
188
+ // checked 不是 undefined 的是带对勾的开关项。title 写在菜单顶上:iPhone 没有 title 悬停,长文件名在行里被截断,这里看全。
159
189
  // 位置按安全区夹住(横屏时客户端不替页面留);offsetWidth 在显示后才量得到。
160
190
  const menu = $('menu');
161
- function showMenu(event, path) {
162
- const entries = [['复制相对路径', path], ['复制绝对路径', `${model.root.path}/${path}`]];
163
- const title = document.createElement('p');
164
- title.className = 'menu-path secondary';
165
- title.textContent = path;
166
- menu.replaceChildren(title, ...entries.map(([label, text]) => {
167
- const button = document.createElement('button');
168
- button.textContent = label;
169
- button.addEventListener('click', () => { coda.copy(text); hideMenu(); });
191
+ let opener = null; // 打开菜单的按钮;用点打开的没有
192
+ function openMenu(anchor, { title, items }) {
193
+ hideMenu();
194
+ const head = title === undefined ? [] : [Object.assign(document.createElement('p'), { className: 'menu-path secondary', textContent: title })];
195
+ const checkable = items.some(item => item.checked !== undefined); // 有开关项时,每一项都留出对勾的位置,文字才对齐
196
+ menu.replaceChildren(...head, ...items.map(({ label, run, checked }) => {
197
+ const button = document.createElement('button'), name = document.createElement('span');
198
+ name.textContent = label;
199
+ if (checkable) {
200
+ const mark = document.createElement('span');
201
+ mark.className = 'check';
202
+ if (checked) mark.append(icon('check', 16));
203
+ button.append(mark);
204
+ }
205
+ button.append(name);
206
+ button.setAttribute('role', checked === undefined ? 'menuitem' : 'menuitemcheckbox');
207
+ if (checked !== undefined) button.setAttribute('aria-checked', String(checked));
208
+ button.addEventListener('click', () => { run(); hideMenu(); });
170
209
  return button;
171
210
  }));
172
211
  menu.hidden = false;
173
- menu.style.left = `max(var(--safe-left), min(${event.clientX}px, calc(100vw - var(--safe-right) - ${menu.offsetWidth + 4}px)))`;
174
- menu.style.top = `max(var(--safe-top), min(${event.clientY}px, calc(100vh - var(--safe-bottom) - ${menu.offsetHeight + 4}px)))`;
212
+ const box = anchor.getBoundingClientRect?.();
213
+ const x = box ? box.right - menu.offsetWidth : anchor.clientX, y = box ? box.bottom + 4 : anchor.clientY;
214
+ menu.style.left = `max(var(--safe-left), min(${x}px, calc(100vw - var(--safe-right) - ${menu.offsetWidth + 4}px)))`;
215
+ menu.style.top = `max(var(--safe-top), min(${y}px, calc(100vh - var(--safe-bottom) - ${menu.offsetHeight + 4}px)))`;
216
+ if (box) { opener = anchor; opener.setAttribute?.('aria-expanded', 'true'); }
217
+ }
218
+ const pathMenu = (event, path) => openMenu(event, { title: path, items: [
219
+ { label: '复制相对路径', run: () => coda.copy(path) },
220
+ { label: '复制绝对路径', run: () => coda.copy(`${model.root.path}/${path}`) },
221
+ ] });
222
+ function hideMenu() {
223
+ menu.hidden = true;
224
+ opener?.setAttribute?.('aria-expanded', 'false');
225
+ opener = null;
175
226
  }
176
- const hideMenu = () => { menu.hidden = true; };
177
227
  // 菜单开着时点菜单外:只关菜单,紧接着的那次点击不交给树。
178
228
  let menuDismissed = false;
179
229
  document.addEventListener('pointerdown', event => {
package/view/bar.mjs CHANGED
@@ -1,16 +1,26 @@
1
- // 页内工具条:没有 coda.toolbar 的平台(iPhone)用。两个页面共用,按钮由页面给。
1
+ // 页内工具条:没有 coda.toolbar 的平台(iPhone)用,两个页面共用,按钮由页面给。
2
2
  // 「返回」只在横屏出现(导航栏隐藏时),显示与否由 style.css 的媒体查询决定,这里一律画出来。
3
- export const EXIT = { id: 'exit', label: '返回', kind: 'button' };
3
+ // 项:{ id, label, kind: 'button' | 'toggle' | 'title', glyph?, iconOnly?, on?, menu? };menu 的按钮打开菜单。title 不是按钮,只显示 label。
4
+ // 有 glyph 画图标;iconOnly 的只留图标,label 当可访问名称;否则图标后面跟文字(没有 glyph 就只有文字)。
5
+ import { icon } from './icons.mjs';
4
6
 
7
+ export const EXIT = { id: 'exit', label: '返回', kind: 'button', glyph: 'back' };
8
+
9
+ // act(id, button):button 是被点的元素,菜单要用它定位。
5
10
  export function paintBar(el, items, act) {
6
11
  el.hidden = false;
7
12
  el.replaceChildren(...items.map(item => {
13
+ if (item.kind === 'title') return Object.assign(document.createElement('span'), { className: 'bar-title grow truncate', textContent: item.label });
8
14
  const button = document.createElement('button');
9
- button.textContent = item.label;
15
+ if (item.glyph) button.append(icon(item.glyph, 20));
16
+ if (item.iconOnly) button.setAttribute('aria-label', item.label);
17
+ else button.append(Object.assign(document.createElement('span'), { textContent: item.label }));
10
18
  if (item.id === EXIT.id) button.classList.add('exit');
11
- button.setAttribute('aria-pressed', item.kind === 'toggle' ? String(item.on) : 'false');
19
+ if (item.iconOnly) button.classList.add('icon');
20
+ if (item.kind === 'toggle') button.setAttribute('aria-pressed', String(item.on));
21
+ if (item.menu) { button.setAttribute('aria-haspopup', 'menu'); button.setAttribute('aria-expanded', 'false'); }
12
22
  if (item.on) button.classList.add('on');
13
- button.addEventListener('click', () => act(item.id));
23
+ button.addEventListener('click', () => act(item.id, button));
14
24
  return button;
15
25
  }));
16
26
  }
package/view/file-app.mjs CHANGED
@@ -11,6 +11,7 @@ const coda = window.coda;
11
11
  const $ = id => document.getElementById(id);
12
12
  const quiet = fn => { try { return fn(); } catch { return undefined; } };
13
13
  const hasToolbar = typeof coda.toolbar === 'function' && coda.platform === 'mac';
14
+ const isIOS = coda.platform === 'ios'; // iPhone:复制路径在底部工具条,不在预览头部
14
15
 
15
16
  // 参数里的会话和路径,形状不对按没有处理。
16
17
  const argsOf = args => ({
@@ -28,7 +29,7 @@ let root = null;
28
29
  let restore = parseScroll(quiet(() => coda.getState()));
29
30
 
30
31
  const conn = connect(coda, { handlers: { session: onSession, root: onRoot, changed: onChanged }, onStatus: () => schedule() });
31
- const preview = mountPreview($('preview'), { read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text) });
32
+ const preview = mountPreview($('preview'), { read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text), copyInHead: !isIOS });
32
33
 
33
34
  // 模块跟随的会话和启动参数里的不是同一个时(换会话的两条消息没有先后保证),不能读,等对上。
34
35
  const ready = () => want.path !== null && session === want.session && root?.state === RootState.READY;
@@ -76,7 +77,7 @@ function paintNotice() {
76
77
  : noticeText(session === want.session ? root : null, session);
77
78
  if (key === noticeKey) return;
78
79
  noticeKey = key;
79
- showNotice($('notice'), failed && want.path !== null ? { failure: conn.failure, retry: conn.hello } : { text: key });
80
+ showNotice($('notice'), failed && want.path !== null ? { failure: conn.failure, retry: conn.hello } : { text: key, glyph: 'doc' });
80
81
  }
81
82
 
82
83
  // ---- 模块的消息(代次和握手由 connection 处理)----
@@ -109,15 +110,17 @@ quiet(() => coda.on('context', ({ args }) => {
109
110
  schedule();
110
111
  }));
111
112
 
112
- // ---- 工具条:Mac 用 coda.toolbar,没有的(iOS)退成页内一条 ----
113
+ // ---- 工具条:Mac 用 coda.toolbar;iPhone 在底部放返回(仅横屏)、刷新、复制路径;其他(harness)退成页内一条 ----
113
114
  const refresh = () => { conn.refresh(); preview.refresh(); };
115
+ // 说明区下预览是关着的,复制的是启动值里的路径:模块不可用时也能把路径交给对话。
116
+ const copyPath = () => (preview.path !== null ? preview.copyPath() : want.path !== null && coda.copy(want.path));
114
117
  const items = [{ id: 'refresh', label: '刷新', icon: 'arrow.clockwise', kind: 'button' }];
115
118
  if (hasToolbar) {
116
119
  quiet(() => coda.toolbar(items));
117
120
  quiet(() => coda.on('toolbar', ({ id }) => { if (id === 'refresh') refresh(); }));
118
121
  } else {
119
- const exit = coda.platform === 'ios';
120
- paintBar($('bar'), exit ? [EXIT, ...items] : items, id => (id === EXIT.id ? quiet(() => coda.close()) : refresh()));
122
+ const shown = isIOS ? [EXIT, { ...items[0], glyph: 'refresh' }, { id: 'copy', label: '复制路径', kind: 'button', glyph: 'copy' }] : items;
123
+ paintBar($('bar'), shown, id => (id === EXIT.id ? quiet(() => coda.close()) : id === 'copy' ? copyPath() : refresh()));
121
124
  }
122
125
 
123
126
  // ---- 阅读位置:setState 按视图和 args 隔离,每个文件自然分开 ----
package/view/file.html CHANGED
@@ -7,11 +7,11 @@
7
7
  <link rel="stylesheet" href="style.css">
8
8
  </head>
9
9
  <body>
10
- <div id="bar" class="bar" hidden></div>
11
10
  <main id="stage">
12
11
  <div id="preview" class="pane preview"></div>
13
12
  <div id="notice" class="pane empty secondary"></div>
14
13
  </main>
14
+ <div id="bar" class="bar bottom" hidden></div>
15
15
  <script type="module" src="file-app.mjs"></script>
16
16
  </body>
17
17
  </html>
package/view/icons.mjs ADDED
@@ -0,0 +1,56 @@
1
+ // 图标:页面里自己画的 SVG,形状近似 SF Symbol,不打包 Apple 的符号。颜色都是 currentColor,由 CSS 定。
2
+ import { EntryKind } from './protocol.mjs';
3
+
4
+ const SVG_NS = 'http://www.w3.org/2000/svg';
5
+
6
+ // 描边图标的 24×24 画法;folder 是填充的,单独标。
7
+ const SHAPES = {
8
+ folder: { fill: true, shapes: [['path', { d: 'M3 7.2C3 5.98 3.98 5 5.2 5h4.05c.6 0 1.17.24 1.59.66L12 6.8h6.8C20.02 6.8 21 7.78 21 9v8.8c0 1.22-.98 2.2-2.2 2.2H5.2C3.98 20 3 19.02 3 17.8V7.2z' }]] },
9
+ doc: { shapes: [['path', { d: 'M7 3h7l5 5v11a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z' }], ['path', { d: 'M14 3v5h5' }], ['path', { d: 'M9 13h6M9 17h4' }]] },
10
+ code: { shapes: [['path', { d: 'M7 3h7l5 5v11a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z' }], ['path', { d: 'M14 3v5h5' }], ['path', { d: 'M10.5 12.5 8.5 15l2 2.5M13.5 12.5l2 2.5-2 2.5' }]] },
11
+ image: { shapes: [['rect', { x: 3.5, y: 4.5, width: 17, height: 15, rx: 2.5 }], ['circle', { cx: 9, cy: 10, r: 1.6 }], ['path', { d: 'm4 17 4.5-4.5 3.5 3.5 3-3 5 5' }]] },
12
+ link: { shapes: [['path', { d: 'M10 14a4 4 0 0 0 5.66 0l3-3a4 4 0 0 0-5.66-5.66l-1 1' }], ['path', { d: 'M14 10a4 4 0 0 0-5.66 0l-3 3a4 4 0 0 0 5.66 5.66l1-1' }]] },
13
+ other: { shapes: [['path', { d: 'M7 3h7l5 5v11a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z' }], ['path', { d: 'M14 3v5h5' }], ['path', { d: 'M9.5 14.5h5' }]] },
14
+ chevron: { shapes: [['path', { d: 'm9 5 7 7-7 7' }]] },
15
+ warning: { shapes: [['path', { d: 'M12 4 2.8 19.5h18.4L12 4z' }], ['path', { d: 'M12 10v4.5M12 17.2v.1' }]] },
16
+ back: { shapes: [['path', { d: 'm15 5-7 7 7 7' }]] },
17
+ check: { shapes: [['path', { d: 'm5 12.5 4.5 4.5L19 7.5' }]] },
18
+ ellipsis: { fill: true, shapes: [['circle', { cx: 5.5, cy: 12, r: 1.7 }], ['circle', { cx: 12, cy: 12, r: 1.7 }], ['circle', { cx: 18.5, cy: 12, r: 1.7 }]] },
19
+ refresh: { shapes: [['path', { d: 'M19 12a7 7 0 1 1-2.05-4.95' }], ['path', { d: 'M19 4.5v3.8h-3.8' }]] },
20
+ copy: { shapes: [['rect', { x: 8.5, y: 8.5, width: 11, height: 11, rx: 2.2 }], ['path', { d: 'M15.5 8.5V6.7a2.2 2.2 0 0 0-2.2-2.2H6.7a2.2 2.2 0 0 0-2.2 2.2v6.6a2.2 2.2 0 0 0 2.2 2.2h1.8' }]] },
21
+ window: { shapes: [['rect', { x: 3.5, y: 5, width: 17, height: 14, rx: 2.5 }], ['path', { d: 'M3.5 9.5h17' }]] },
22
+ turn: { shapes: [['path', { d: 'M5 18v-4a4 4 0 0 1 4-4h10' }], ['path', { d: 'm15 6 4 4-4 4' }]] },
23
+ };
24
+
25
+ // 扩展名表:不在表里的文件都是文档。
26
+ const BY_EXT = {
27
+ code: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'yml', 'yaml', 'toml', 'html', 'css', 'scss', 'vue', 'py', 'rb', 'go', 'rs', 'java', 'kt', 'swift',
28
+ 'c', 'h', 'cpp', 'hpp', 'cs', 'php', 'lua', 'sh', 'bash', 'zsh', 'sql', 'xml'],
29
+ image: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'ico', 'bmp', 'heic', 'avif'],
30
+ };
31
+ const EXT_ICON = new Map(Object.entries(BY_EXT).flatMap(([name, exts]) => exts.map(ext => [ext, name])));
32
+
33
+ // 图标名:目录、链接、其他按种类;文件按扩展名(不分大小写;.gitignore 这类点开头的没有扩展名),没有匹配的是文档。
34
+ export function iconFor({ kind, name }) {
35
+ if (kind === EntryKind.DIR) return 'folder';
36
+ if (kind === EntryKind.LINK) return 'link';
37
+ if (kind === EntryKind.OTHER) return 'other';
38
+ const dot = name.lastIndexOf('.');
39
+ return (dot > 0 && EXT_ICON.get(name.slice(dot + 1).toLowerCase())) || 'doc';
40
+ }
41
+
42
+ // 造一个 SVG 元素;size 是点数,CSS 可以按平台覆盖。
43
+ export function icon(name, size = 16) {
44
+ const { fill = false, shapes } = SHAPES[name];
45
+ const svg = document.createElementNS(SVG_NS, 'svg');
46
+ const attrs = { width: size, height: size, viewBox: '0 0 24 24', 'aria-hidden': 'true', ...(fill
47
+ ? { fill: 'currentColor' }
48
+ : { fill: 'none', stroke: 'currentColor', 'stroke-width': 1.8, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }) };
49
+ for (const [key, value] of Object.entries(attrs)) svg.setAttribute(key, value);
50
+ for (const [tag, shape] of shapes) {
51
+ const node = document.createElementNS(SVG_NS, tag);
52
+ for (const [key, value] of Object.entries(shape)) node.setAttribute(key, value);
53
+ svg.append(node);
54
+ }
55
+ return svg;
56
+ }
package/view/notice.mjs CHANGED
@@ -1,18 +1,25 @@
1
- // 说明区:一段文字,或「没连上 + 原因 + 重试」。树页和文件页共用。
1
+ // 说明区:大号图标加一段文字,或「没连上 + 原因 + 重试」。树页和文件页共用。
2
2
  // 重画会换掉正在聚焦的按钮:焦点留在说明区里(有重试按钮就落在它上面),不丢到页面外。
3
- export function showNotice(el, { text, failure, retry }) {
3
+ import { icon } from './icons.mjs';
4
+
5
+ const text = content => Object.assign(document.createElement('p'), { textContent: content });
6
+
7
+ export function showNotice(el, { text: message, failure, retry, glyph = 'folder' }) {
4
8
  const focused = el.contains(document.activeElement);
5
9
  let target = el;
6
- if (failure === undefined) el.textContent = text;
7
- else {
8
- const button = document.createElement('button'), title = document.createElement('p'), reason = document.createElement('p');
9
- title.textContent = '没有连上资源管理器的后台';
10
+ const mark = document.createElement('span');
11
+ mark.className = 'notice-icon';
12
+ if (failure === undefined) {
13
+ mark.append(icon(glyph, 44));
14
+ el.replaceChildren(mark, text(message));
15
+ } else {
16
+ const button = document.createElement('button'), reason = text(failure);
17
+ mark.append(icon('warning', 44));
10
18
  reason.className = 'secondary';
11
- reason.textContent = failure;
12
19
  button.className = 'link';
13
20
  button.textContent = '重试';
14
21
  button.addEventListener('click', retry);
15
- el.replaceChildren(title, reason, button);
22
+ el.replaceChildren(mark, text('没有连上资源管理器的后台'), reason, button);
16
23
  target = button;
17
24
  }
18
25
  if (focused) { el.tabIndex = -1; target.focus(); }
package/view/preview.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // 文件预览:只接收路径和读块的能力,不依赖树的状态。readFile 是纯逻辑;mountPreview 才碰 DOM。
2
- import { ContentKind, ErrorCode, IMAGE_LIMIT, TEXT_LIMIT, imageType } from './protocol.mjs';
2
+ import { icon, iconFor } from './icons.mjs';
3
+ import { ContentKind, EntryKind, ErrorCode, IMAGE_LIMIT, TEXT_LIMIT, imageType } from './protocol.mjs';
3
4
 
4
5
  function fromBase64(data) {
5
6
  const binary = atob(data), bytes = new Uint8Array(binary.length);
@@ -63,25 +64,31 @@ const h = (tag, attributes = {}, ...children) => {
63
64
  };
64
65
 
65
66
  // read(path, offset) → Promise<chunk 回答>,会话换了会以 stale 错误拒绝;copy(text) 复制路径。
66
- // actions:头部在「复制路径」旁额外加的按钮 [{ label, run(path) }],预览不知道它们做什么。
67
- export function mountPreview(el, { read, copy, actions = [] }) {
68
- const name = h('div', { class: 'pname truncate' }), meta = h('div', { class: 'pmeta caption secondary truncate' }, ' ');
69
- const copyButton = h('button', { class: 'link', onclick: () => path !== null && copy(path) }, '复制路径');
67
+ // actions:头部在「复制路径」旁额外加的按钮 [{ label, icon, run(path) }],预览不知道它们做什么。
68
+ // copyInHead:false 时头部不放「复制路径」(iPhone 放在底部工具条),由页面调 copyPath()。
69
+ // back:给了就在头部最左边放「返回」(Mac 侧栏预览:原生工具栏项目只能在右侧);文件页和 iPhone 不传。
70
+ export function mountPreview(el, { read, copy, actions = [], copyInHead = true, back = null }) {
71
+ const name = h('div', { class: 'pname truncate' }), meta = h('div', { class: 'pmeta caption secondary truncate' }, ' ');
72
+ const fileIcon = h('span', { class: 'pico', 'aria-hidden': 'true' });
73
+ // 图标按钮:可访问名称和 Mac 的悬停提示都是 label;没有图标的退回文字。
74
+ const action = (label, glyph, run) => h('button', { class: glyph ? 'icon' : 'link', 'aria-label': label, title: label, onclick: run }, glyph ? icon(glyph, 18) : label);
75
+ const copyPath = () => path !== null && copy(path);
70
76
  const retryButton = h('button', { class: 'link', onclick: () => load(null, true), hidden: true }, '重试');
71
77
  const body = h('div', { class: 'pbody' });
72
- const extra = actions.map(({ label, run }) => h('button', { class: 'link', onclick: () => path !== null && run(path) }, label));
73
- el.replaceChildren(h('div', { class: 'phead' }, h('div', { class: 'grow' }, name, meta), retryButton, copyButton, ...extra), body);
78
+ const extra = actions.map(({ label, icon: glyph, run }) => action(label, glyph, () => path !== null && run(path)));
79
+ el.replaceChildren(h('div', { class: 'phead' }, ...(back ? [action('返回', 'back', back)] : []), fileIcon, h('div', { class: 'grow' }, name, meta), retryButton,
80
+ ...(copyInHead ? [action('复制路径', 'copy', copyPath)] : []), ...extra), body);
74
81
 
75
82
  let path = null, token = 0, shown = null, objectUrl = null; // shown:当前显示的 { size, mtimeMs }
76
- let metaText = ' ', stale = null; // stale:后台刷新失败的原因,此时正文还是上次的
83
+ let metaText = ' ', metaTitle = '', stale = null; // stale:后台刷新失败的原因,此时正文还是上次的
77
84
  // 更新失败的提示占用元信息那一行,重试按钮在头部,所以正文不会被顶动。
78
85
  const paintMeta = () => {
79
86
  meta.textContent = stale ? `更新失败:${stale}` : metaText;
80
- meta.title = stale ?? '';
87
+ meta.title = stale ?? metaTitle;
81
88
  meta.classList.toggle('warn', stale !== null);
82
89
  retryButton.hidden = stale === null;
83
90
  };
84
- const setMeta = text => { metaText = text; stale = null; paintMeta(); };
91
+ const setMeta = (text, title = '') => { metaText = text; metaTitle = title; stale = null; paintMeta(); };
85
92
  const setStale = reason => { stale = reason; paintMeta(); };
86
93
  const revoke = () => { if (objectUrl) URL.revokeObjectURL(objectUrl); objectUrl = null; };
87
94
  const notice = (title, detail, action) => body.replaceChildren(h('div', { class: 'empty' },
@@ -91,8 +98,11 @@ export function mountPreview(el, { read, copy, actions = [] }) {
91
98
  function show(result) {
92
99
  revoke();
93
100
  shown = { size: result.size, mtimeMs: result.mtimeMs };
101
+ // 元信息:目录 · 行数 · 大小;根目录下的文件没有目录,非文本没有行数。修改时间放进 title。
102
+ const dir = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '';
103
+ const lines = result.kind === ContentKind.TEXT ? `${lineCount(result.text)} 行` : '';
94
104
  const note = result.truncated ? `只显示前 ${formatSize(TEXT_LIMIT)}` : '';
95
- setMeta([formatSize(result.size), formatTime(result.mtimeMs), note].filter(Boolean).join(' · '));
105
+ setMeta([dir, lines, formatSize(result.size), note].filter(Boolean).join(' · '), `修改于 ${formatTime(result.mtimeMs)}`);
96
106
  if (result.kind === ContentKind.TEXT) {
97
107
  const gutter = h('pre', { class: 'gutter', 'aria-hidden': 'true' }, Array.from({ length: lineCount(result.text) }, (_, i) => i + 1).join('\n'));
98
108
  body.replaceChildren(h('div', { class: 'code' }, gutter, h('pre', { class: 'text selectable' }, result.text)));
@@ -132,7 +142,15 @@ export function mountPreview(el, { read, copy, actions = [] }) {
132
142
  }
133
143
 
134
144
  return {
135
- open(next, scroll = null) { path = next; name.textContent = next.split('/').pop(); name.title = next; return load(scroll, false); },
145
+ open(next, scroll = null) {
146
+ path = next;
147
+ const base = next.split('/').pop();
148
+ name.textContent = base;
149
+ name.title = next;
150
+ fileIcon.replaceChildren(icon(iconFor({ kind: EntryKind.FILE, name: base }), 22));
151
+ return load(scroll, false);
152
+ },
153
+ copyPath,
136
154
  refresh() { return path === null ? undefined : load(null, true); },
137
155
  // 内容的来源变了(根目录换了位置):旧正文不能留,也不能按元数据判断「没变」。
138
156
  reload() { return path === null ? undefined : load(null, false); },
package/view/style.css CHANGED
@@ -2,8 +2,8 @@
2
2
  :root {
3
3
  --hover: var(--coda-hover, var(--coda-fill, rgba(0,0,0,.05)));
4
4
  --line: var(--coda-separator, rgba(0,0,0,.1));
5
- --row-h: calc(var(--coda-font-size, 13px) * 1.9);
6
- --indent: 12px; --pad: 8px;
5
+ --row-h: calc(var(--coda-font-size, 13px) * 2);
6
+ --indent: 12px; --pad: 6px;
7
7
  /* 安全区:横屏时客户端不替页面留,页面自己按这几个值留白;变量便于 harness 模拟。 */
8
8
  --safe-top: env(safe-area-inset-top, 0px); --safe-right: env(safe-area-inset-right, 0px);
9
9
  --safe-bottom: env(safe-area-inset-bottom, 0px); --safe-left: env(safe-area-inset-left, 0px);
@@ -30,28 +30,50 @@ p { margin: 0; }
30
30
  .pane { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
31
31
  .pane.off { visibility: hidden; pointer-events: none; }
32
32
  .empty { display: grid; align-content: start; justify-items: center; gap: 6px; padding: 32px 20px; text-align: center; }
33
+ .notice-icon { display: grid; place-items: center; margin-bottom: 6px; color: var(--coda-secondary, #6e6e73); }
34
+ .notice-icon svg { display: block; }
33
35
 
34
- .bar { display: flex; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
35
- .bar button { padding: 3px 8px; border-radius: var(--coda-radius, 6px); color: var(--coda-secondary, #6e6e73); }
36
+ /* 页内工具条:iPhone 树页在顶部(返回、根目录名、⋯),文件页在底部(.bottom:返回、刷新、复制路径)。没有 coda.toolbar 的其他平台(harness)是一条文字按钮。 */
37
+ .bar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
38
+ .bar button { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: var(--coda-radius, 6px); color: var(--coda-secondary, #6e6e73); }
36
39
  .bar button.on { color: var(--coda-accent, #0a84ff); }
40
+ .bar svg { display: block; flex: none; }
41
+ .bar-title { padding: 0 8px; font-weight: 600; }
42
+ .bar.bottom { padding-bottom: var(--safe-bottom); border-top: 1px solid var(--line); border-bottom: 0; } /* 操作区 44 点(iPhone)之外只加安全区 */
37
43
 
38
- /* 树:只画可见的一段,行高固定(tree.mjs 量 .row 的高度)。 */
39
- .spacer { position: relative; }
44
+ /* 树:只画可见的一段,行高固定(tree.mjs 量 .row 的高度)。
45
+ Mac 是侧栏列表:左右缩进 8 点,行有圆角,选中用 --coda-selection。 */
46
+ .section { padding: 10px 16px 4px; font-size: .85em; font-weight: 600; color: var(--coda-secondary, #6e6e73); cursor: default; }
47
+ .spacer { position: relative; margin: 0 8px; }
40
48
  .win { position: absolute; left: 0; right: 0; top: 0; }
41
- .row { display: flex; align-items: center; gap: 4px; height: var(--row-h); padding-right: 8px; white-space: nowrap; cursor: default; }
49
+ /* 缩进按行的 --d(深度)算,iPhone 的分隔线起点也用它。 */
50
+ .row { position: relative; display: flex; align-items: center; gap: 4px; height: var(--row-h); padding: 0 8px 0 calc(var(--indent) * var(--d, 0) + var(--pad));
51
+ border-radius: var(--coda-radius, 6px); white-space: nowrap; cursor: default; }
42
52
  .row.sel { background: var(--coda-selection, var(--hover)); }
43
53
  .row.more, .row.retry { color: var(--coda-accent, #0a84ff); cursor: pointer; }
44
54
  .row.status { display: block; overflow: hidden; text-overflow: ellipsis; line-height: var(--row-h); color: var(--coda-secondary, #6e6e73); }
45
- .row.dead .name { color: var(--coda-tertiary, #a1a1a6); }
46
- .chev { flex: none; width: 14px; text-align: center; font-size: .8em; color: var(--coda-secondary, #6e6e73); }
55
+ .row.dead .name, .row.dead .ico { color: var(--coda-tertiary, #a1a1a6); }
56
+ .chev { flex: none; display: grid; place-items: center; width: 14px; font-size: .8em; color: var(--coda-secondary, #6e6e73); }
57
+ .chev svg { display: block; }
58
+ .row.open .chev svg { transform: rotate(90deg); }
59
+ .row.file .chev svg { visibility: hidden; } /* Mac 的文件只留空槽,名字才对齐 */
60
+ .ico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
61
+ .ico svg { display: block; }
62
+ .row.dir .ico { color: var(--coda-accent, #0a84ff); }
47
63
  .name { flex: 1 1 auto; min-width: 0; }
48
- .tail { flex: none; width: 1em; text-align: center; color: var(--coda-secondary, #6e6e73); }
64
+ .tail { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
65
+ .tail:empty { display: none; }
66
+ .tail svg { display: block; }
49
67
  .tail.warn { color: var(--coda-orange, #b25000); }
50
68
 
51
69
  /* 预览:头部永远占一行名字加一行说明,加载和失败只换下面的内容。 */
52
- .preview { display: flex; flex-direction: column; overflow: hidden; }
70
+ .preview { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归文件页的工具条 */
53
71
  .phead button { flex: none; white-space: nowrap; }
54
72
  .phead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
73
+ .pico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
74
+ .pico svg { display: block; }
75
+ 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); }
76
+ button.icon svg { display: block; }
55
77
  .pname { font-weight: 600; }
56
78
  .pbody { flex: 1 1 auto; min-height: 0; overflow: auto; }
57
79
  .code { display: flex; width: max-content; min-width: 100%; font: .9em/1.55 var(--coda-mono, ui-monospace, Menlo, monospace); }
@@ -65,7 +87,9 @@ p { margin: 0; }
65
87
  #notice:focus { outline: none; }
66
88
  .menu { position: fixed; z-index: 10; min-width: 150px; padding: 4px; border: 1px solid var(--coda-outline, var(--line)); border-radius: var(--coda-radius-card, 10px);
67
89
  background: var(--coda-card, var(--coda-background, #fff)); }
68
- .menu button { display: block; width: 100%; padding: 5px 10px; border-radius: var(--coda-radius, 6px); }
90
+ .menu button { display: flex; align-items: center; gap: 6px; width: 100%; padding: 5px 10px; border-radius: var(--coda-radius, 6px); }
91
+ .check { flex: none; display: grid; place-items: center; width: 16px; color: var(--coda-accent, #0a84ff); }
92
+ .check svg { display: block; }
69
93
  .menu-path { max-width: 280px; padding: 4px 10px 6px; overflow-wrap: anywhere; }
70
94
  .pmeta.warn { color: var(--coda-orange, #b25000); }
71
95
 
@@ -75,24 +99,41 @@ p { margin: 0; }
75
99
 
76
100
  /* 悬停只在有鼠标时:触摸后不会留着。 */
77
101
  @media (hover: hover) {
78
- .bar button:hover { background: var(--hover); }
102
+ .bar button:hover, button.icon:hover { background: var(--hover); }
79
103
  .row.entry:hover { background: var(--hover); }
104
+ .row.entry.sel:hover { background: var(--coda-selection, var(--hover)); }
80
105
  .menu button:hover { background: var(--coda-selection, var(--hover)); }
81
106
  }
82
107
 
83
- /* iPhone:行和按钮至少 44 点;行高在这里定,tree.mjs 量 .row 得到,不另写一份。 */
108
+ /* iPhone:行和按钮至少 44 点;行高在这里定,tree.mjs 量 .row 得到,不另写一份。
109
+ 目录树是 Coda 卡片外形的分组列表:.spacer 就是卡片(高度已等于全部行高),首尾圆角不用逐行判断。 */
84
110
  .coda-ios { --row-h: 44px; --indent: 20px; --pad: 16px; }
85
- .coda-ios .row { padding-right: 16px; gap: 8px; }
86
- .coda-ios .chev { width: 24px; }
111
+ .coda-ios .section { display: none; } /* 根目录名归页内工具条 */
112
+ .coda-ios .spacer { margin: 8px 16px; overflow: hidden; border-radius: var(--coda-radius-card, 12px); background: var(--coda-card, var(--coda-secondary-background, #fff)); }
113
+ /* 描边用伪元素:不占尺寸,spacer 加 border 会让内容区少 2 点,末行被裁。 */
114
+ .coda-ios .spacer::after { content: ''; position: absolute; inset: 0; border: 1px solid var(--coda-outline, var(--line)); border-radius: inherit; pointer-events: none; }
115
+ .coda-ios .row { gap: 8px; padding-right: 16px; border-radius: 0; }
116
+ .coda-ios .row + .row::before { content: ''; position: absolute; top: 0; right: 0; height: 1px; background: var(--coda-separator, var(--line));
117
+ left: calc(var(--pad) + var(--indent) * var(--d, 0) + 22px + 8px); } /* 从图标之后开始 */
118
+ .coda-ios .row.sel + .row::before, .coda-ios .row.sel::before { background: transparent; }
87
119
  .coda-ios .row { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单;选择和拖动另外已关 */
88
120
  .coda-ios .row.entry:active { background: var(--hover); }
89
- .coda-ios .bar { gap: 8px; padding: 4px 8px; }
121
+ .coda-ios .ico svg { width: 22px; height: 22px; }
122
+ .coda-ios .chev { order: 9; width: 14px; }
123
+ .coda-ios .row.dir .chev { color: var(--coda-accent, #0a84ff); }
124
+ .coda-ios .row.file .chev svg { visibility: visible; }
125
+ .coda-ios .row.dead .chev { display: none; }
90
126
  .coda-ios button { min-height: 44px; min-width: 44px; }
91
- .coda-ios .bar button { padding: 0 12px; border-radius: var(--coda-radius, 10px); }
127
+ .coda-ios .bar { gap: 0; padding: 0 8px; }
128
+ .coda-ios .bar button { gap: 6px; padding: 0 12px; border-radius: var(--coda-radius, 10px); color: var(--coda-accent, #0a84ff); }
129
+ .coda-ios .bar button.icon { padding: 0; justify-content: center; }
130
+ .coda-ios .bar.bottom { padding: 0 8px var(--safe-bottom); }
131
+ .coda-ios .bar.bottom button { flex: 1 1 0; justify-content: center; } /* 横排单行:横屏只有 390 点高 */
92
132
  .coda-ios .phead { padding: 4px 12px; gap: 4px; }
93
133
  .coda-ios .phead button { padding: 0 8px; }
94
- .coda-ios .menu button { padding: 0 12px; text-align: left; }
134
+ .coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
135
+ .coda-ios .menu button { padding: 0 12px; }
95
136
 
96
137
  /* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
97
- .exit { display: none; }
98
- @media (orientation: landscape) and (max-height: 500px) { .coda-ios .exit { display: inline-flex; align-items: center; } }
138
+ .bar .exit { display: none; }
139
+ @media (orientation: landscape) and (max-height: 500px) { .coda-ios .bar .exit { display: inline-flex; } }
package/view/tree.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // 目录树:visibleRows 把模型压成扁平行(纯逻辑);mountTree 只画可见的那一段,上万条也不卡。
2
2
  import { EntryKind } from './protocol.mjs';
3
+ import { icon, iconFor } from './icons.mjs';
3
4
  import { isHidden, joinPath } from './model.mjs';
4
5
 
5
6
  const OVERSCAN = 8;
@@ -33,30 +34,44 @@ export function visibleRows(model) {
33
34
  return rows;
34
35
  }
35
36
 
37
+ // 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
38
+ export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
39
+
36
40
  const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
37
41
 
38
42
  // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, path, kind) }
39
43
  // draggable:触屏上拖行没有用处,还会抢滚动,关掉。
40
44
  // longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
41
45
  export function mountTree(el, model, handlers, { draggable = true, longPress = false } = {}) {
42
- const spacer = document.createElement('div'), win = document.createElement('div');
43
- spacer.className = 'spacer'; win.className = 'win';
46
+ const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
47
+ title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
48
+ title.hidden = true;
44
49
  spacer.append(win);
45
- el.replaceChildren(spacer);
50
+ el.replaceChildren(title, spacer);
46
51
  let rows = [], rowH = 24;
47
52
 
53
+ // 探针放在 spacer 里:量到的是行在真实结构里的高度。
48
54
  const measure = () => {
49
55
  const probe = document.createElement('div');
50
56
  probe.className = 'row';
51
57
  probe.style.visibility = 'hidden';
52
- el.append(probe);
58
+ spacer.append(probe);
53
59
  rowH = probe.getBoundingClientRect().height || rowH;
54
60
  probe.remove();
55
61
  };
56
62
 
63
+ // 分区标题:根目录的名字(root.path 的最后一段),不是 ready 时不显示。iPhone 上由 CSS 隐藏。
64
+ const paintTitle = () => {
65
+ const text = model.ready ? rootName(model.root.path) : '';
66
+ if (title.textContent !== text) title.textContent = text;
67
+ title.hidden = text === '';
68
+ };
69
+
70
+ // 标题在滚动容器里,把 spacer 推下去 offsetTop:行的位置按 spacer 内部算,滚动值(保存和恢复的)仍是容器的。
57
71
  function paint() {
58
- const first = Math.max(0, Math.floor(el.scrollTop / rowH) - OVERSCAN);
59
- const last = Math.min(rows.length, Math.ceil((el.scrollTop + el.clientHeight) / rowH) + OVERSCAN);
72
+ const top = el.scrollTop - (spacer.offsetTop || 0);
73
+ const first = Math.max(0, Math.floor(top / rowH) - OVERSCAN);
74
+ const last = Math.min(rows.length, Math.ceil((top + el.clientHeight) / rowH) + OVERSCAN);
60
75
  win.style.transform = `translateY(${first * rowH}px)`;
61
76
  win.replaceChildren(...rows.slice(first, last).map((row, i) => rowNode(row, first + i)));
62
77
  }
@@ -65,7 +80,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
65
80
  const node = document.createElement('div');
66
81
  node.className = `row ${row.type}`;
67
82
  node.dataset.i = index;
68
- node.style.paddingLeft = `calc(var(--indent) * ${row.depth} + var(--pad))`;
83
+ node.style.cssText = `--d: ${row.depth}`; // 缩进由 CSS 按 --d 算,分隔线的起点也用它
69
84
  if (row.type !== 'entry') {
70
85
  node.textContent = row.text;
71
86
  if (row.retry) { node.classList.add('retry'); node.title = row.text; } // 长原因在行里被截断,Mac 悬停看全
@@ -77,15 +92,21 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
77
92
  if (row.path === model.selected) node.classList.add('sel');
78
93
  if (row.kind === EntryKind.DIR) node.setAttribute('aria-expanded', row.open);
79
94
  if (!dead && draggable) node.draggable = true;
80
- const chevron = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
95
+ // 一份 DOM:Mac 是 箭头、图标、名字、尾标,iPhone 的箭头用 CSS order 排到最后。
96
+ const chevron = document.createElement('span'), glyph = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
81
97
  chevron.className = 'chev';
82
- chevron.textContent = row.kind === EntryKind.DIR ? (row.busy ? '…' : row.open ? '▾' : '▸') : '';
98
+ if (row.busy) chevron.textContent = '…';
99
+ else if (!dead) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
100
+ if (row.open) node.classList.add('open');
101
+ glyph.className = 'ico';
102
+ glyph.append(icon(iconFor(row)));
83
103
  label.className = 'name truncate';
84
104
  label.textContent = row.name;
85
105
  tail.className = row.failed ? 'tail warn' : 'tail';
86
- tail.textContent = row.failed ? '!' : row.link ? '↪' : '';
106
+ if (row.failed) tail.append(icon('warning', 14));
107
+ else if (row.link) tail.append(icon('turn', 14));
87
108
  node.title = row.failed ? `${row.path}\n读取失败:${row.failed}` : TITLES[row.kind] ? `${row.path}\n${TITLES[row.kind]}` : row.path;
88
- node.append(chevron, label, tail);
109
+ node.append(chevron, glyph, label, tail);
89
110
  return node;
90
111
  }
91
112
 
@@ -138,7 +159,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
138
159
  new ResizeObserver(paint).observe(el);
139
160
 
140
161
  return {
141
- render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paint(); },
162
+ render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
142
163
  remeasure() { measure(); this.render(); },
143
164
  scroll: () => el.scrollTop,
144
165
  scrollTo(top) { el.scrollTop = top; },