@coda-console/resource-manager 0.7.0 → 0.7.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.
package/README.md CHANGED
@@ -3,12 +3,12 @@
3
3
  Coda 插件:会话旁的文件浏览器。浏览这个会话的工作目录,读文本和图片,把路径交给对话。只读。
4
4
 
5
5
  - **Mac**:在会话侧栏里,和对话并排。
6
- - **iPhone**:在会话右上角菜单里打开,点文件推入一层。iPad 和 iPhone Duo 用同一套界面,没有单独适配。
6
+ - **iPhone**:在会话右上角菜单里打开,点文件在资源页里切到预览,系统返回(左上「‹」、左边缘侧滑)回到打开它的那一层。iPad 和 iPhone Duo 用同一套界面,没有单独适配。
7
7
 
8
8
  ## 功能
9
9
 
10
10
  - 两种视图:**列表**(目录树,按需展开,超大目录分页显示)和**图标**(一次看一层,每个条目是类型图标加名字,名字最多两行,`package.json` 这样的名字优先在扩展名前折行)。在「更多」菜单(Mac)或「⋯」菜单(iPhone)的「显示」›「视图」里切换,全局一份,两种视图各自保留滚动位置。
11
- - **Mac 照 Finder**:单击只选中,**双击**打开——文件在侧栏里切到预览,文件夹在列表里展开或收起、在图标视图里进入;单击展开箭头只展开,选中不变;单击空白取消选中;右键菜单第一项是「打开」(文件夹在列表里写「展开」「收起」),不能打开的条目没有。**iPhone 不变**:轻点就打开(文件夹展开或进入,文件推入文件页)。
11
+ - **Mac 照 Finder**:单击只选中,**双击**打开——文件在侧栏里切到预览,文件夹在列表里展开或收起、在图标视图里进入;单击展开箭头只展开,选中不变;单击空白取消选中;右键菜单第一项是「打开」(文件夹在列表里写「展开」「收起」),不能打开的条目没有。**iPhone** 轻点就打开(文件夹展开或进入,文件在页内切到预览,不另开页面)。
12
12
  - 图标视图回上一层:iPhone 用系统的返回,Mac 点头部最左边的返回键或按 ⌘↑,退回的那一层里选中刚才进去的文件夹。**没有预览面板**:只有一个侧栏,也没有面包屑。
13
13
  - **Mac 的键盘**:点一下目录树或图标视图让它拿到焦点(Tab 也能进来),↑ ↓ 移动选中(图标视图按行列走),列表里 → 展开、← 收起或选中所在的文件夹,图标视图里 ← → 选左右的条目;⌘↓ 同双击(打开、展开、进入);⌘↑ 在图标视图里回上一层,在预览里返回目录树或图标视图。不做多选、按字母跳转,Return 和空格也没绑定。读屏的 `aria-activedescendant` 没做(见 `docs/backlog.md`)。
14
14
  - 文本预览(带行号,可跨行选中复制,最多读 1 MiB)和图片预览(PNG、JPEG、GIF、WebP、SVG,最多 8 MiB)。
@@ -22,7 +22,7 @@ Coda 插件:会话旁的文件浏览器。浏览这个会话的工作目录,
22
22
  - 属性页:行菜单里选「属性」,在当前页里打开一层,用系统的返回退出。显示种类、大小、链接到、位置、完整路径、修改和创建时间、权限;文件夹的大小在打开后统计,不跟随链接,最多 20 万项或 5 秒,超出时显示下限(如「超过 3.2 GiB(已统计 20 万项)」),一轮结束后已有的结果标「上次统计」,可以重新统计。
23
23
  - **写入输入框用的是 Coda 没有公开的接口**,以后可能失效或改变;失效时「添加到聊天」会改为复制路径或文字。**已知风险**:输入框里的附件还在上传时添加,上传完成后,这段时间里打的字和那个附件可能消失;刚打完字的一瞬间添加,也可能丢掉这几个字。
24
24
  - Mac:把文件拖到输入框也行;预览可以开到独立窗口;工具栏只有一个「更多」按钮,按下弹出菜单:刷新、显示隐藏文件、「显示」›。鼠标移到「显示」或点它,在菜单左边弹出二级菜单(视图和排序);侧栏太窄放不下时,在菜单里切到下一层,顶上一行「返回」。侧栏预览头部最左边的「返回」回到目录树或图标视图。
25
- - iPhone:长按出同样的菜单;目录树页(或图标视图)右上角「⋯」里有刷新、显示隐藏文件和「显示」,点「显示」在菜单里切到下一层(视图和排序),顶上一行「返回」;文件页底部工具条有刷新和「复制路径」。
25
+ - iPhone:长按出同样的菜单;目录树页(或图标视图)右上角「⋯」里有刷新、显示隐藏文件和「显示」,点「显示」在菜单里切到下一层(视图和排序),顶上一行「返回」;预览时只有一行标题(预览头部),「⋯」在头部右边,里面只有刷新和「复制路径」。
26
26
  - 默认隐藏 `.git`、`.DS_Store`,Mac 在工具栏的「更多」菜单里、iPhone 在目录树页右上角的「⋯」里可切换显示全部。
27
27
 
28
28
  只能读这个会话的工作目录。指向工作目录之外的符号链接只显示,不能打开。
@@ -46,7 +46,7 @@ Coda 插件:会话旁的文件浏览器。浏览这个会话的工作目录,
46
46
  # 不进 Coda,用假的 window.coda 在浏览器里看真实页面
47
47
  "$CODA_NODE" test/harness/server.mjs <会话A的目录> [会话B的目录] [端口]
48
48
 
49
- # 真实布局的断言和截图:无头 Chrome 打开 Mac 侧栏、iPhone 竖屏和横屏的目录树、图标视图与文件页(浅色、深色),
49
+ # 真实布局的断言和截图:无头 Chrome 打开 Mac 侧栏、iPhone 竖屏和横屏的目录树、图标视图与资源页里的预览(浅色、深色),
50
50
  # 量行高、工具条、菜单和二级菜单的位置和尺寸,图标视图的单元格和滚动。自己起服务和临时目录,用完删掉;截图在系统临时目录里,路径在最后打印。
51
51
  # 失败时退出码非 0。Chrome 不在默认位置时把路径作参数传入
52
52
  "$CODA_NODE" test/harness/layout.mjs [Chrome 的路径]
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "@coda-console/resource-manager",
4
4
  "displayName": "资源管理器",
5
5
  "description": "会话旁的只读文件浏览器:浏览会话工作目录,预览文本和图片,把路径交给对话。",
6
- "version": "0.7.0",
6
+ "version": "0.7.1",
7
7
  "categories": ["工作流程"],
8
8
  "license": "MIT",
9
9
  "repository": {
package/view/app.mjs CHANGED
@@ -19,13 +19,12 @@ const coda = window.coda;
19
19
  const $ = id => document.getElementById(id);
20
20
  const quiet = fn => { try { return fn(); } catch { return undefined; } };
21
21
  const hasToolbar = typeof coda.toolbar === 'function' && coda.platform === 'mac';
22
- // iPhone 上打开文件推入新视图,树页不进预览模式;opened 一直是 null,selected 只用来高亮。
23
- // Mac 照 Finder:单击选中、双击打开,键盘接在目录树和图标视图的滚动容器上。
22
+ // 两端都在资源页里打开文件、切到预览(第 16 节):iPhone 轻点打开、系统返回关掉,Mac 照 Finder 单击选中、双击打开,键盘接在目录树和图标视图的滚动容器上。
24
23
  const isIOS = coda.platform === 'ios';
25
24
 
26
25
  let persisted = parseState(quiet(() => coda.getState()));
27
- // 恢复出来的预览滚动位置,等内容到了再用一次。目录树和图标视图的在 restore 里。
28
- let pending = { preview: null };
26
+ // 恢复出来的预览滚动位置,等内容到了再用一次;path 是它属于的文件,opened 不是它了就作废。目录树和图标视图的在 restore 里。
27
+ let pending = { preview: null, path: null };
29
28
 
30
29
  const conn = connect(coda, { handlers: { session: onSession, root: onRoot, changed: onChanged }, onStatus: () => schedule() });
31
30
  const model = new Model({ link: conn.link, onChange: () => schedule(), onLost: () => hint.show('文件夹已不存在', { warn: false }) });
@@ -59,7 +58,8 @@ const restore = createRestore(model, { tree, grid });
59
58
  const trail = createTrail();
60
59
  const preview = mountPreview($('preview'), {
61
60
  read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text),
62
- actions: isIOS ? [] : [{ label: '窗口', icon: 'window', run: openInView }], copyInHead: !isIOS,
61
+ // iPhone 预览头部右边的 ⋯(刷新、复制路径):顶部工具条在预览时只有「返回」,只留头部一行标题
62
+ actions: isIOS ? [{ label: '更多', icon: 'ellipsis', run: (_, button) => toggleMore(button) }] : [{ label: '窗口', icon: 'window', run: openInView }], copyInHead: !isIOS,
63
63
  back: isIOS ? null : closePreview,
64
64
  // 选中文字的右键菜单只有 Mac:iPhone 用系统的选区菜单。
65
65
  selectionMenu: isIOS ? null : (event, selection) => menu.open(event, { items: chat.selectionItems(selection) }),
@@ -75,35 +75,22 @@ const info = mountInfo($('info'), {
75
75
  back: nativeBack ? null : () => { backPressed = true; closeInfo(); },
76
76
  });
77
77
 
78
- // ---- 在独立视图里打开:iPhone 点文件,Mac 预览头部的「窗口」 ----
78
+ // ---- 在独立视图里打开:Mac 预览头部的「窗口」 ----
79
79
  // 会话 ID 放进 args:被打开的视图 context.session 是 null。失败(含同步抛出)在原地提示。
80
- // iPhone 防连点:open 进行中、成功后推入动画的这一小段都忽略重复点击;失败立刻放开。
81
- // epoch 每换一次会话加一:旧会话的 open 结果回来时已经作废,不碰新会话的提示和防连点。
82
- let opening = false, unlockTimer = null, epoch = 0;
83
- const REOPEN_MS = 1000;
80
+ // epoch 每换一次会话加一:旧会话的 open 结果回来时已经作废,不碰新会话的提示。
81
+ let epoch = 0;
84
82
  function openInView(path) {
85
- if (isIOS) opening = true;
86
83
  const mine = epoch;
87
84
  return Promise.resolve().then(() => coda.open({ view: 'file', args: { session: model.session, path } }))
88
- .then(() => {
89
- if (mine !== epoch) return;
90
- hint.hide();
91
- if (isIOS) { clearTimeout(unlockTimer); unlockTimer = setTimeout(() => { opening = false; }, REOPEN_MS); }
92
- }, error => {
93
- if (mine !== epoch) return;
94
- opening = false;
95
- hint.show(`打不开文件:${error?.message ?? '未知原因'}`);
85
+ .then(() => { if (mine === epoch) hint.hide(); }, error => {
86
+ if (mine === epoch) hint.show(`打不开文件:${error?.message ?? '未知原因'}`);
96
87
  });
97
88
  }
89
+ // 两端都在页内打开:model.openFile 设 selected 和 opened,经 onChange 切到预览。iPhone 触屏没有键盘焦点,不交焦点;
90
+ // 快速两次轻点:第一次已切到预览,目录树 .off 不接指针事件,第二次落不到条目上。
98
91
  function openFile(path) {
99
- if (isIOS && opening) return; // 先于 select 和保存:被忽略的点击什么都不改
100
- if (!isIOS) {
101
- openPressed = true; // 用户打开的:render 把焦点交给预览面板
102
- return model.openFile(path);
103
- }
104
- selectEntry(path);
105
- saveNow(); // 视图随即卸载,这一刻的展开状态、条数和滚动位置先存下
106
- openInView(path);
92
+ openPressed = !isIOS; // 用户在 Mac 上打开的:render 把焦点交给预览面板
93
+ model.openFile(path);
107
94
  }
108
95
 
109
96
  // ---- 渲染:同一批变化合并成一次 ----
@@ -131,7 +118,7 @@ function closePreview() {
131
118
  // 用户点了预览头部的返回:render 据此决定焦点要不要交给浏览区,别的模式变化不动焦点。
132
119
  let backPressed = false;
133
120
  let openPressed = false; // 用户打开了文件:同返回对称,页面持有焦点时 render 把焦点交给预览面板
134
- const mode = () => (infoOpen ? 'info' : !model.ready ? 'notice' : model.opened !== null && !isIOS ? 'preview' : model.view === View.ICONS ? 'grid' : 'tree');
121
+ const mode = () => (infoOpen ? 'info' : !model.ready ? 'notice' : model.opened !== null ? 'preview' : model.view === View.ICONS ? 'grid' : 'tree');
135
122
 
136
123
  // 说明区只在内容变了才重画,免得重试按钮在点击中途被换掉。
137
124
  let noticeKey = null;
@@ -156,9 +143,12 @@ function render() {
156
143
  (model.view === View.ICONS ? grid : tree).render(); // 只画正在用的视图,另一个留着它的内容和滚动位置
157
144
  landed();
158
145
  restore.settle();
146
+ // 待恢复的位置只属于它那个文件:opened 清了或换成别的文件,位置作废。
147
+ if (pending.preview !== null && pending.path !== model.opened) pending.preview = null;
159
148
  if (current === 'preview' && preview.path !== model.opened) {
160
- preview.open(model.opened, pending.preview);
161
- pending.preview = null;
149
+ // 位置要等内容到了、真的设上才清:之前离开页面或换会话,saveNow 还存它。作废的读取不清,由接替它的读取用上。
150
+ const scroll = pending.preview;
151
+ preview.open(model.opened, scroll).then(used => { if (used && scroll !== null && pending.preview === scroll) pending.preview = null; });
162
152
  } else if ((browsing || current === 'notice') && preview.path !== null) preview.close(); // 属性页和预览一样只切可见性,不动别的面板的内容
163
153
  // 返回按钮随预览一起 inert,焦点会掉到页面外:用户刚点了返回、页面又持有焦点时交给目录树或网格。
164
154
  // 后台刷新、切会话、root 变化造成的切换不动焦点,免得抢走客户端输入框的焦点。
@@ -179,9 +169,10 @@ const toolbarItems = () => infoOpen ? [] : hasToolbar
179
169
  { id: 'hidden', label: '显示隐藏文件', icon: 'eye', kind: 'toggle', on: model.showHidden },
180
170
  ];
181
171
  // iPhone 树页顶部:返回(仅横屏)、根目录名、⋯。刷新、显示隐藏文件和排序都在 ⋯ 的菜单里。
172
+ // 预览时和属性页一样只留返回(竖屏整条不显示):标题和 ⋯ 在预览头部,⋯ 里只有刷新和复制路径。
182
173
  // 属性页上只留返回:竖屏有原生导航栏,整条工具条不显示;横屏导航栏隐藏,页面自己给返回(显示与否都由 style.css 的横屏媒体查询决定)。
183
174
  const MORE = { id: 'more', label: '更多', kind: 'button', glyph: 'ellipsis', iconOnly: true, menu: true };
184
- const phoneItems = () => infoOpen ? [EXIT] : [EXIT, { kind: 'title', label: model.ready ? dirTitle() : '' }, MORE];
175
+ const phoneItems = () => infoOpen || mode() === 'preview' ? [EXIT] : [EXIT, { kind: 'title', label: model.ready ? dirTitle() : '' }, MORE];
185
176
  // 比较键就是要画的项:根目录名变了(换会话)工具条跟着重画。
186
177
  let lastItems = '';
187
178
  function syncToolbar() {
@@ -198,7 +189,10 @@ function syncToolbar() {
198
189
  // 原生按钮没有 DOM 元素可以当锚点:用一个固定在页面右上角的假锚点,同时也是「谁打开的菜单」的标记。
199
190
  const TOOLBAR = { getBoundingClientRect: () => ({ right: window.innerWidth, bottom: 0 }) };
200
191
  // 「更多」菜单的内容只写这一处,iPhone 的 ⋯ 和 Mac 的原生按钮共用:刷新、显示隐藏文件、「显示」›;「显示」里是视图和排序,当前项有对勾。
201
- const moreMenu = () => ({ items: [
192
+ const moreMenu = () => isIOS && mode() === 'preview' ? { items: [
193
+ { label: '刷新', run: () => act('refresh') },
194
+ { label: '复制路径', run: () => preview.copyPath() }, // 相对路径
195
+ ] } : ({ items: [
202
196
  { label: '刷新', run: () => act('refresh') },
203
197
  { label: '显示隐藏文件', checked: model.showHidden, run: () => act('hidden') },
204
198
  { label: '显示', submenu: { items: [
@@ -240,13 +234,17 @@ function changeView(view) {
240
234
  schedule();
241
235
  }
242
236
 
237
+ // 键盘激活没有 pointerdown,菜单外点击那条规则不管用:同一个按钮再次激活就关闭。
238
+ function toggleMore(button) {
239
+ if (!menu.hidden && menu.opener === button) menu.hide();
240
+ else menu.open(button, moreMenu());
241
+ }
242
+
243
243
  function act(id, button) {
244
244
  // 横屏时导航栏隐藏,原生返回看不到:这里的「返回」和原生返回走同一个退层逻辑,在根层才关视图。
245
245
  if (id === EXIT.id) { if (goBack(1)) backPressed = true; else quiet(() => coda.close()); }
246
246
  else if (id === 'more') {
247
- // 键盘激活没有 pointerdown,菜单外点击那条规则不管用:同一个按钮再次激活就关闭。
248
- if (!menu.hidden && menu.opener === button) menu.hide();
249
- else menu.open(button, moreMenu());
247
+ toggleMore(button);
250
248
  }
251
249
  else if (id === 'hidden') {
252
250
  model.setShowHidden(!model.showHidden);
@@ -368,13 +366,16 @@ function landed() {
368
366
  }
369
367
 
370
368
  // ---- 上一层:iPhone 用原生返回,路径只在这里声明 ----
371
- // 路径是上层的标题,由远到近,不含当前页:图标视图在 a/b 是 [根目录名, 'a'];列表视图没有。属性页多一层当前页:列表视图是 '资源管理器',图标视图是当前目录名。
369
+ // 路径是上层的标题,由远到近,不含当前页:图标视图在 a/b 是 [根目录名, 'a'];列表视图没有。属性页、预览各多一层当前页(要回到的那一页):列表视图是 '资源管理器',图标视图是当前目录名。
370
+ // 预览开着是 opened 不为空、且根目录就绪;没就绪时页面是说明区,路径为空。
372
371
  // 标题是当前目录名:根目录是根目录名。
373
372
  const dirTitle = () => (model.cwd === '' || model.view !== View.ICONS ? rootName(model.root.path) : model.cwd.slice(model.cwd.lastIndexOf('/') + 1));
373
+ const previewOpen = () => model.ready && model.opened !== null;
374
374
  function backPath() {
375
375
  const icons = model.view === View.ICONS && model.ready;
376
376
  const upper = icons && model.cwd !== '' ? [rootName(model.root.path), ...model.cwd.split('/').slice(0, -1)] : [];
377
- return infoOpen ? [...upper, icons ? dirTitle() : '资源管理器'] : upper;
377
+ const here = icons ? dirTitle() : '资源管理器';
378
+ return [...upper, ...(infoOpen ? [here] : []), ...(previewOpen() ? [here] : [])];
378
379
  }
379
380
  let declared = [];
380
381
  // 路径没变不重复声明;force:客户端已经退了一层,路径没变也要重新告诉它现在在哪。
@@ -385,9 +386,12 @@ function declareBack(force = false) {
385
386
  declared = path;
386
387
  quiet(() => coda.back(path));
387
388
  }
388
- // 退 steps 层:先关属性页,再退图标视图的目录;返回是不是真的退了。横屏的返回和原生返回共用。
389
+ // 退 steps 层:先关预览,再关属性页,再退图标视图的目录;返回是不是真的退了。横屏的返回和原生返回共用。
390
+ // 没就绪时页面是说明区,看不见预览和目录层:不消耗它们(保存的 opened、cwd 不变),横屏的返回直接关视图。
389
391
  function goBack(steps) {
392
+ if (!model.ready) return false;
390
393
  let used = 0;
394
+ if (previewOpen() && steps > used) { model.closeFile(); used++; } // 走和 Mac 一样的 closeFile:opened 清,selected 留着
391
395
  if (infoOpen && steps > used) { closeInfo(); used++; }
392
396
  const levels = Math.min(steps - used, model.view === View.ICONS && model.cwd !== '' ? model.cwd.split('/').length : 0);
393
397
  if (levels > 0) { leaveDir(levels); used += levels; }
@@ -421,11 +425,9 @@ function onSession(data) {
421
425
  tree.reset();
422
426
  grid.reset();
423
427
  epoch++;
424
- opening = false;
425
- clearTimeout(unlockTimer);
426
428
  hint.hide();
427
429
  const saved = persisted.sessions[data.session] ?? null;
428
- pending = { preview: saved?.opened ? { top: saved.scroll.top, left: saved.scroll.left } : null };
430
+ pending = saved?.opened ? { preview: { top: saved.scroll.top, left: saved.scroll.left }, path: saved.opened } : { preview: null, path: null };
429
431
  model.receive(data, saved);
430
432
  trail.clear();
431
433
  restore.clear();
@@ -442,7 +444,7 @@ function onRoot(data) {
442
444
  if (data.state !== RootState.READY || before?.path !== data.path) closeInfo(); // 属性页看的是旧根目录下的路径
443
445
  const scope = model.scope;
444
446
  const done = model.receive(data);
445
- if (model.scope !== scope) invalidateScroll(); // 换了根目录(同根重建不算):旧根目录的位置和记录不能给新的用
447
+ if (model.scope !== scope) { invalidateScroll(); pending.preview = null; } // 换了根目录(同根重建不算):旧根目录的位置和记录不能给新的用,待恢复的预览位置也是
446
448
  if (done && data.state === RootState.READY) {
447
449
  if (before?.path !== data.path) { if (model.opened !== null) preview.reload(); } else preview.refresh(); // 换了根目录,opened 已清空:旧预览随即关掉,不再读
448
450
  }
package/view/file-app.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  // 文件视图的页面入口:读一个文件。会话和路径来自 coda.args(启动值),之后的变化走 context 事件;
2
2
  // 连接在 connection.mjs,预览在 preview.mjs,这里不创建目录树。设计见 docs/design/phase-2.md 第 7 节。
3
3
  // Mac 的独立窗口里选中文字右键可以添加到聊天(第 13.7 节);iPhone 不接,用系统的选区菜单。
4
- import { EXIT, paintBar } from './bar.mjs';
4
+ // iPhone 点文件在资源页里预览(第 16 节),不再进这个视图;别的入口仍可能在 iPhone 上打开它:没有工具条,靠系统返回离开。
5
+ import { paintBar } from './bar.mjs';
5
6
  import { createChat } from './chat.mjs';
6
7
  import { connect } from './connection.mjs';
7
8
  import { mountHint } from './hint.mjs';
@@ -15,7 +16,7 @@ const coda = window.coda;
15
16
  const $ = id => document.getElementById(id);
16
17
  const quiet = fn => { try { return fn(); } catch { return undefined; } };
17
18
  const hasToolbar = typeof coda.toolbar === 'function' && coda.platform === 'mac';
18
- const isIOS = coda.platform === 'ios'; // iPhone:复制路径在底部工具条,不在预览头部
19
+ const isIOS = coda.platform === 'ios';
19
20
 
20
21
  // 参数里的会话和路径,形状不对按没有处理。
21
22
  const argsOf = args => ({
@@ -37,7 +38,7 @@ const hint = mountHint($('hint'));
37
38
  const menu = mountMenu($('menu'));
38
39
  const chat = createChat({ link: conn.link, copy: text => coda.copy(text), hint });
39
40
  const preview = mountPreview($('preview'), {
40
- read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text), copyInHead: !isIOS,
41
+ read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text),
41
42
  selectionMenu: isIOS ? null : (event, selection) => menu.open(event, { items: chat.selectionItems(selection) }),
42
43
  });
43
44
 
@@ -70,7 +71,8 @@ function render() {
70
71
  function openPreview() {
71
72
  const { path } = want, scroll = restore;
72
73
  // 没有要恢复的位置就明确从头开始,不靠清空正文后滚动条自己归零。
73
- preview.open(path, scroll ?? { top: 0, left: 0 }).then(() => { if (preview.path === path && restore === scroll) restore = null; });
74
+ // 位置真正用上才清;读取被刷新、重载作废时,预览自己让接替的那次读取接着用。
75
+ preview.open(path, scroll ?? { top: 0, left: 0 }).then(used => { if (used && preview.path === path && restore === scroll) restore = null; });
74
76
  }
75
77
 
76
78
  // 预览收起前记下滚动位置,恢复后接着用;内容还没到时 restore 还没用掉,不覆盖。
@@ -125,18 +127,13 @@ quiet(() => coda.on('context', ({ args }) => {
125
127
  schedule();
126
128
  }));
127
129
 
128
- // ---- 工具条:Mac 用 coda.toolbar;iPhone 在底部放返回(仅横屏)、刷新、复制路径;其他(harness)退成页内一条 ----
130
+ // ---- 工具条:Mac 用 coda.toolbar;iPhone 没有;其他(harness)退成页内一条 ----
129
131
  const refresh = () => { conn.refresh(); preview.refresh(); };
130
- // 说明区下预览是关着的,复制的是启动值里的路径:模块不可用时也能把路径交给对话。
131
- const copyPath = () => (preview.path !== null ? preview.copyPath() : want.path !== null && coda.copy(want.path));
132
132
  const items = [{ id: 'refresh', label: '刷新', icon: 'arrow.clockwise', kind: 'button' }];
133
133
  if (hasToolbar) {
134
134
  quiet(() => coda.toolbar(items));
135
135
  quiet(() => coda.on('toolbar', ({ id }) => { if (id === 'refresh') refresh(); }));
136
- } else {
137
- const shown = isIOS ? [EXIT, { ...items[0], glyph: 'refresh' }, { id: 'copy', label: '复制路径', kind: 'button', glyph: 'copy' }] : items;
138
- paintBar($('bar'), shown, id => (id === EXIT.id ? quiet(() => coda.close()) : id === 'copy' ? copyPath() : refresh()));
139
- }
136
+ } else if (!isIOS) paintBar($('bar'), items, refresh);
140
137
 
141
138
  // ---- 阅读位置:setState 按视图和 args 隔离,每个文件自然分开 ----
142
139
  let saveTimer = null;
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>
10
11
  <main id="stage">
11
12
  <div id="preview" class="pane preview"></div>
12
13
  <div id="notice" class="pane empty secondary"></div>
13
14
  </main>
14
- <div id="bar" class="bar bottom" hidden></div>
15
15
  <div id="menu" class="menu" role="menu" hidden></div>
16
16
  <div id="hint" class="hint" role="status" hidden></div>
17
17
  <script type="module" src="file-app.mjs"></script>
package/view/model.mjs CHANGED
@@ -43,7 +43,7 @@ export class Model {
43
43
  expanded = new Set(); // 不变量:展开的目录,它的每一级上层也展开
44
44
  wanted = new Map(); // 恢复状态时各目录要取够的条数,第一次取用后删掉
45
45
  selected = null; // 选中的条目(高亮和键盘移动用,文件夹、不能打开的条目都可以)
46
- opened = null; // Mac 侧栏预览中的文件;iPhone 一直是 null
46
+ opened = null; // 页内预览中的文件(两端一样);null 是在看目录
47
47
  showHidden = false;
48
48
  sort = DEFAULT_SORT; // 全局一份,由页面存进 persisted
49
49
  view = View.LIST; // 全局一份,由页面存进 persisted
package/view/preview.mjs CHANGED
@@ -53,9 +53,11 @@ const formatTime = ms => new Date(ms).toLocaleString('zh-CN', { hour12: false })
53
53
  export const lineCount = text => (text === '' ? 1 : text.split('\n').length - (text.endsWith('\n') ? 1 : 0));
54
54
 
55
55
  // read(path, offset) → Promise<chunk 回答>,会话换了会以 stale 错误拒绝;copy(text) 复制路径。
56
- // actions:头部在「复制路径」旁额外加的按钮 [{ label, icon, run(path) }],预览不知道它们做什么。
57
- // copyInHead:false 时头部不放「复制路径」(iPhone 放在底部工具条),由页面调 copyPath()。
56
+ // actions:头部在「复制路径」旁额外加的按钮 [{ label, icon, run(path, button) }],button 是被点的按钮(菜单用它定位),预览不知道它们做什么。
57
+ // copyInHead:false 时头部不放「复制路径」(iPhone 的复制路径在 ⋯ 菜单里),由页面调 copyPath()。
58
58
  // back:给了就在头部最左边放「返回」(Mac 侧栏预览:原生工具栏项目只能在右侧);文件页和 iPhone 不传。
59
+ // open(path, scroll) 带了要恢复的位置时,返回的 Promise 在「位置用上了」时变 true(正文到了、已设好位置;读取失败不算,位置留着等重试),
60
+ // 在「位置作废」时变 false(预览关了、改看别的文件)。读取被刷新、重载或 changed 作废时位置不丢:接替它的那次读取接着带上,用上才算完。
59
61
  // selectionMenu(event, { path, text, startLine, endLine }):给了就接管正文里选中文字的右键(只有 Mac 传);预览不知道菜单里是什么。
60
62
  // 选区为空、或裁到正文里之后为空时不接管,走系统默认。内容和行号在这里取好,交出去之后和选区无关。
61
63
  export function mountPreview(el, { read, copy, actions = [], copyInHead = true, back = null, selectionMenu = null }) {
@@ -64,13 +66,15 @@ export function mountPreview(el, { read, copy, actions = [], copyInHead = true,
64
66
  // 图标按钮:可访问名称和 Mac 的悬停提示都是 label;没有图标的退回文字。
65
67
  const action = (label, glyph, run) => h('button', { class: glyph ? 'icon' : 'link', 'aria-label': label, title: label, onclick: run }, glyph ? icon(glyph, 18) : label);
66
68
  const copyPath = () => path !== null && copy(path);
67
- const retryButton = h('button', { class: 'link', onclick: () => load(null, true), hidden: true }, '重试');
69
+ const retryButton = h('button', { class: 'link', onclick: () => load(true), hidden: true }, '重试');
68
70
  const body = h('div', { class: 'pbody' });
69
- const extra = actions.map(({ label, icon: glyph, run }) => action(label, glyph, () => path !== null && run(path)));
71
+ const extra = actions.map(({ label, icon: glyph, run }) => { const button = action(label, glyph, () => path !== null && run(path, button)); return button; });
70
72
  el.replaceChildren(h('div', { class: 'phead' }, ...(back ? [action('返回', 'back', back)] : []), fileIcon, h('div', { class: 'grow' }, name, meta), retryButton,
71
73
  ...(copyInHead ? [action('复制路径', 'copy', copyPath)] : []), ...extra), body);
72
74
 
73
75
  let path = null, token = 0, shown = null, objectUrl = null; // shown:当前显示的 { size, mtimeMs }
76
+ let carry = null; // 待恢复的位置 { scroll, settle }:open 带进来,读取真正用上或作废后才清
77
+ const settle = used => { const c = carry; carry = null; c?.settle(used); };
74
78
  let metaText = ' ', metaTitle = '', stale = null; // stale:后台刷新失败的原因,此时正文还是上次的
75
79
  // 更新失败的提示占用元信息那一行,重试按钮在头部,所以正文不会被顶动。
76
80
  const paintMeta = () => {
@@ -129,8 +133,8 @@ export function mountPreview(el, { read, copy, actions = [], copyInHead = true,
129
133
  notice('读不到这个文件', error.message, { label: '重试', run: retry });
130
134
  }
131
135
 
132
- // silent:刷新。没变化不重画;变了就换内容,并尽量留在原来的滚动位置。
133
- async function load(scroll, silent) {
136
+ // silent:刷新。没变化不重画;变了就换内容,并尽量留在原来的滚动位置;有待恢复的位置时用它。
137
+ async function load(silent) {
134
138
  const mine = ++token, mark = () => mine === token;
135
139
  if (!silent) { shown = null; setMeta('读取中…'); revoke(); body.replaceChildren(); }
136
140
  try {
@@ -140,28 +144,33 @@ export function mountPreview(el, { read, copy, actions = [], copyInHead = true,
140
144
  });
141
145
  if (!mark() || result.cancelled) return;
142
146
  if (result.same) return setStale(null);
143
- const keep = scroll ?? { top: body.scrollTop, left: body.scrollLeft };
147
+ // 刷新留在原位;整页重读(打开、重载、重试)没有要恢复的位置就明确从头开始,不靠清空正文后滚动条自己归零。
148
+ const keep = carry?.scroll ?? (silent ? { top: body.scrollTop, left: body.scrollLeft } : { top: 0, left: 0 });
144
149
  show(result);
145
150
  body.scrollTop = keep.top; body.scrollLeft = keep.left;
151
+ settle(true);
146
152
  } catch (error) {
147
- if (mark()) failure(error, silent, () => load(null, false));
153
+ if (mark()) failure(error, silent, () => load(false)); // 失败不算用上:待恢复的位置留着,重试或之后的刷新成功才用、才清
148
154
  }
149
155
  }
150
156
 
151
157
  return {
152
158
  open(next, scroll = null) {
159
+ settle(false);
153
160
  path = next;
154
161
  const base = next.split('/').pop();
155
162
  name.textContent = base;
156
163
  name.title = next;
157
164
  fileIcon.replaceChildren(icon(iconFor({ kind: EntryKind.FILE, name: base }), 22));
158
- return load(scroll, false);
165
+ const used = scroll ? new Promise(done => { carry = { scroll, settle: done }; }) : null;
166
+ const read = load(false);
167
+ return used ?? read;
159
168
  },
160
169
  copyPath,
161
- refresh() { return path === null ? undefined : load(null, true); },
170
+ refresh() { return path === null ? undefined : load(true); },
162
171
  // 内容的来源变了(根目录换了位置):旧正文不能留,也不能按元数据判断「没变」。
163
- reload() { return path === null ? undefined : load(null, false); },
164
- close() { token++; path = null; shown = null; setMeta(' '); revoke(); body.replaceChildren(); },
172
+ reload() { return path === null ? undefined : load(false); },
173
+ close() { settle(false); token++; path = null; shown = null; setMeta(' '); revoke(); body.replaceChildren(); },
165
174
  scroll: () => ({ top: body.scrollTop, left: body.scrollLeft }),
166
175
  onScroll: fn => body.addEventListener('scroll', fn, { passive: true }),
167
176
  get path() { return path; },
package/view/style.css CHANGED
@@ -59,13 +59,12 @@ p { margin: 0; }
59
59
  .notice-icon { display: grid; place-items: center; margin-bottom: 6px; color: var(--coda-secondary, #6e6e73); }
60
60
  .notice-icon svg { display: block; color: inherit; } /* 说明区的图标不上色 */
61
61
 
62
- /* 页内工具条:iPhone 树页在顶部(返回、根目录名、⋯),文件页在底部(.bottom:返回、刷新、复制路径)。没有 coda.toolbar 的其他平台(harness)是一条文字按钮。 */
62
+ /* 页内工具条:iPhone 资源页在顶部(返回、根目录名或文件名、⋯)。没有 coda.toolbar 的其他平台(harness)是一条文字按钮。 */
63
63
  .bar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
64
64
  .bar button { flex: none; white-space: nowrap; display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: var(--coda-radius, 6px); color: var(--coda-secondary, #6e6e73); }
65
65
  .bar button.on { color: var(--coda-accent, #0a84ff); }
66
66
  .bar svg { display: block; flex: none; }
67
67
  .bar-title { padding: 0 8px; font-weight: 600; }
68
- .bar.bottom { padding-bottom: var(--safe-bottom); border-top: 1px solid var(--line); border-bottom: 0; } /* 操作区 44 点(iPhone)之外只加安全区 */
69
68
 
70
69
  /* 树:只画可见的一段,行高固定(tree.mjs 量 .row 的高度)。
71
70
  Mac 是侧栏列表:左右缩进 8 点,行有圆角,选中用 --coda-selection。 */
@@ -104,7 +103,7 @@ p { margin: 0; }
104
103
  .tail.warn { color: var(--coda-orange, #b25000); }
105
104
 
106
105
  /* 预览:头部永远占一行名字加一行说明,加载和失败只换下面的内容。 */
107
- .preview { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归文件页的工具条 */
106
+ .preview { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归 .pbody(iPhone,见 .coda-ios .pbody) */
108
107
  .phead button, .ghead button { flex: none; white-space: nowrap; }
109
108
  .phead, .ghead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
110
109
  .pico, .ghico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
@@ -230,10 +229,9 @@ button.icon svg { display: block; }
230
229
  .coda-ios .bar { gap: 0; padding: 0 8px; }
231
230
  .coda-ios .bar button { gap: 6px; padding: 0 12px; border-radius: var(--coda-radius, 10px); color: var(--coda-accent, #0a84ff); }
232
231
  .coda-ios .bar button.icon { padding: 0; justify-content: center; }
233
- .coda-ios .bar.bottom { padding: 0 8px var(--safe-bottom); }
234
- .coda-ios .bar.bottom button { flex: 1 1 0; justify-content: center; } /* 横排单行:横屏只有 390 点高 */
235
232
  .coda-ios .phead, .coda-ios .ghead { padding: 4px 12px; gap: 4px; }
236
233
  .coda-ios .phead button, .coda-ios .ghead button { padding: 0 8px; }
234
+ .coda-ios .preview:not(.info) .pbody { padding-bottom: var(--safe-bottom); } /* 资源页里没有底部工具条:最后一行不被 Home 指示条盖住 */
237
235
  .coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
238
236
  .coda-ios .menu button { padding: 0 10px; }
239
237
  .coda-ios .menu button:active { background: var(--coda-selection, var(--hover)); }