@coda-console/resource-manager 0.2.0 → 0.4.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/preview.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  // 文件预览:只接收路径和读块的能力,不依赖树的状态。readFile 是纯逻辑;mountPreview 才碰 DOM。
2
- import { ContentKind, ErrorCode, IMAGE_LIMIT, TEXT_LIMIT, imageType } from './protocol.mjs';
2
+ import { formatSize } from './format.mjs';
3
+ import { icon, iconFor } from './icons.mjs';
4
+ import { ContentKind, EntryKind, ErrorCode, IMAGE_LIMIT, TEXT_LIMIT, imageType } from './protocol.mjs';
3
5
 
4
6
  function fromBase64(data) {
5
7
  const binary = atob(data), bytes = new Uint8Array(binary.length);
@@ -43,13 +45,6 @@ export async function readFile(read, { prev = null, onProgress = () => {}, cance
43
45
  throw Object.assign(error, { code: ErrorCode.IO });
44
46
  }
45
47
 
46
- export function formatSize(bytes) {
47
- if (bytes < 1024) return `${bytes} B`;
48
- const units = ['KiB', 'MiB', 'GiB'];
49
- let value = bytes / 1024, unit = 0;
50
- while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit++; }
51
- return `${value.toFixed(value < 10 ? 1 : 0)} ${units[unit]}`;
52
- }
53
48
  const formatTime = ms => new Date(ms).toLocaleString('zh-CN', { hour12: false });
54
49
  export const lineCount = text => (text === '' ? 1 : text.split('\n').length - (text.endsWith('\n') ? 1 : 0));
55
50
 
@@ -63,25 +58,31 @@ const h = (tag, attributes = {}, ...children) => {
63
58
  };
64
59
 
65
60
  // 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) }, '复制路径');
61
+ // actions:头部在「复制路径」旁额外加的按钮 [{ label, icon, run(path) }],预览不知道它们做什么。
62
+ // copyInHead:false 时头部不放「复制路径」(iPhone 放在底部工具条),由页面调 copyPath()。
63
+ // back:给了就在头部最左边放「返回」(Mac 侧栏预览:原生工具栏项目只能在右侧);文件页和 iPhone 不传。
64
+ export function mountPreview(el, { read, copy, actions = [], copyInHead = true, back = null }) {
65
+ const name = h('div', { class: 'pname truncate' }), meta = h('div', { class: 'pmeta caption secondary truncate' }, ' ');
66
+ const fileIcon = h('span', { class: 'pico', 'aria-hidden': 'true' });
67
+ // 图标按钮:可访问名称和 Mac 的悬停提示都是 label;没有图标的退回文字。
68
+ const action = (label, glyph, run) => h('button', { class: glyph ? 'icon' : 'link', 'aria-label': label, title: label, onclick: run }, glyph ? icon(glyph, 18) : label);
69
+ const copyPath = () => path !== null && copy(path);
70
70
  const retryButton = h('button', { class: 'link', onclick: () => load(null, true), hidden: true }, '重试');
71
71
  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);
72
+ const extra = actions.map(({ label, icon: glyph, run }) => action(label, glyph, () => path !== null && run(path)));
73
+ el.replaceChildren(h('div', { class: 'phead' }, ...(back ? [action('返回', 'back', back)] : []), fileIcon, h('div', { class: 'grow' }, name, meta), retryButton,
74
+ ...(copyInHead ? [action('复制路径', 'copy', copyPath)] : []), ...extra), body);
74
75
 
75
76
  let path = null, token = 0, shown = null, objectUrl = null; // shown:当前显示的 { size, mtimeMs }
76
- let metaText = ' ', stale = null; // stale:后台刷新失败的原因,此时正文还是上次的
77
+ let metaText = ' ', metaTitle = '', stale = null; // stale:后台刷新失败的原因,此时正文还是上次的
77
78
  // 更新失败的提示占用元信息那一行,重试按钮在头部,所以正文不会被顶动。
78
79
  const paintMeta = () => {
79
80
  meta.textContent = stale ? `更新失败:${stale}` : metaText;
80
- meta.title = stale ?? '';
81
+ meta.title = stale ?? metaTitle;
81
82
  meta.classList.toggle('warn', stale !== null);
82
83
  retryButton.hidden = stale === null;
83
84
  };
84
- const setMeta = text => { metaText = text; stale = null; paintMeta(); };
85
+ const setMeta = (text, title = '') => { metaText = text; metaTitle = title; stale = null; paintMeta(); };
85
86
  const setStale = reason => { stale = reason; paintMeta(); };
86
87
  const revoke = () => { if (objectUrl) URL.revokeObjectURL(objectUrl); objectUrl = null; };
87
88
  const notice = (title, detail, action) => body.replaceChildren(h('div', { class: 'empty' },
@@ -91,8 +92,11 @@ export function mountPreview(el, { read, copy, actions = [] }) {
91
92
  function show(result) {
92
93
  revoke();
93
94
  shown = { size: result.size, mtimeMs: result.mtimeMs };
95
+ // 元信息:目录 · 行数 · 大小;根目录下的文件没有目录,非文本没有行数。修改时间放进 title。
96
+ const dir = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '';
97
+ const lines = result.kind === ContentKind.TEXT ? `${lineCount(result.text)} 行` : '';
94
98
  const note = result.truncated ? `只显示前 ${formatSize(TEXT_LIMIT)}` : '';
95
- setMeta([formatSize(result.size), formatTime(result.mtimeMs), note].filter(Boolean).join(' · '));
99
+ setMeta([dir, lines, formatSize(result.size), note].filter(Boolean).join(' · '), `修改于 ${formatTime(result.mtimeMs)}`);
96
100
  if (result.kind === ContentKind.TEXT) {
97
101
  const gutter = h('pre', { class: 'gutter', 'aria-hidden': 'true' }, Array.from({ length: lineCount(result.text) }, (_, i) => i + 1).join('\n'));
98
102
  body.replaceChildren(h('div', { class: 'code' }, gutter, h('pre', { class: 'text selectable' }, result.text)));
@@ -132,7 +136,15 @@ export function mountPreview(el, { read, copy, actions = [] }) {
132
136
  }
133
137
 
134
138
  return {
135
- open(next, scroll = null) { path = next; name.textContent = next.split('/').pop(); name.title = next; return load(scroll, false); },
139
+ open(next, scroll = null) {
140
+ path = next;
141
+ const base = next.split('/').pop();
142
+ name.textContent = base;
143
+ name.title = next;
144
+ fileIcon.replaceChildren(icon(iconFor({ kind: EntryKind.FILE, name: base }), 22));
145
+ return load(scroll, false);
146
+ },
147
+ copyPath,
136
148
  refresh() { return path === null ? undefined : load(null, true); },
137
149
  // 内容的来源变了(根目录换了位置):旧正文不能留,也不能按元数据判断「没变」。
138
150
  reload() { return path === null ? undefined : load(null, false); },
package/view/protocol.mjs CHANGED
@@ -8,7 +8,7 @@ export const PAGE_BYTES = 96 * 1024;
8
8
  export const ENVELOPE_BYTES = 4096;
9
9
  export const TEXT_LIMIT = 1024 * 1024;
10
10
  export const IMAGE_LIMIT = 8 * 1024 * 1024;
11
- // 前这么多字节里有 NUL 就当二进制(同 Coda 的 worktree-changes)。
11
+ // 前这么多字节里有 NUL 就当二进制(照 Coda 判断二进制的惯例)。
12
12
  export const SNIFF_BYTES = 8000;
13
13
  export const REQUEST_ID_MAX = 64;
14
14
 
@@ -21,12 +21,19 @@ export const RootState = Object.freeze({ READY: 'ready', NONE: 'none', PREPARING
21
21
 
22
22
  export const ErrorCode = Object.freeze({
23
23
  NO_ROOT: 'no_root', INVALID_PATH: 'invalid_path', OUTSIDE_ROOT: 'outside_root', NOT_FOUND: 'not_found',
24
- NOT_DIR: 'not_dir', NOT_FILE: 'not_file', DENIED: 'denied', INVALID_CURSOR: 'invalid_cursor',
25
- INVALID_OFFSET: 'invalid_offset', TOO_LARGE: 'too_large', IO: 'io_error',
24
+ NOT_DIR: 'not_dir', NOT_FILE: 'not_file', DENIED: 'denied', CURSOR_EXPIRED: 'cursor_expired',
25
+ INVALID_OFFSET: 'invalid_offset', INVALID_SORT: 'invalid_sort', TOO_LARGE: 'too_large', IO: 'io_error',
26
26
  });
27
27
 
28
+ // 目录排序:值是下面四种之一,缺省(请求不带 sort)是名称。方向由排序键决定,只在 lib/tree.mjs 里定义一处。
29
+ export const SortKey = Object.freeze({ NAME: 'name', KIND: 'kind', DATE: 'date', SIZE: 'size' });
30
+ export const DEFAULT_SORT = SortKey.NAME;
31
+ // 是四种之一的返回它,否则 null(含旧版本保存过的 { key, desc } 对象)。
32
+ export const parseSort = raw => (Object.values(SortKey).includes(raw) ? raw : null);
33
+
28
34
  // 目录条目的 kind:dir、file;link 是指向工作目录外或已失效的链接;other 是不能打开的(套接字、名字含控制字符等)。
29
35
  // 指向工作目录内的链接按目标显示成 dir 或 file,并带 link: true。
36
+ // 条目还可带整数 size(只有文件有)和 mtimeMs(向下取整);读不到时不带。
30
37
  export const EntryKind = Object.freeze({ DIR: 'dir', FILE: 'file', LINK: 'link', OTHER: 'other' });
31
38
  // 文件块的 kind。
32
39
  export const ContentKind = Object.freeze({ TEXT: 'text', IMAGE: 'image', BINARY: 'binary' });
package/view/style.css CHANGED
@@ -1,9 +1,9 @@
1
- /* 资源管理器的页面,颜色、字体、字号只用 App 给的 --coda-* 变量(docs/plugins.md「视图」)。 */
1
+ /* 资源管理器的页面,颜色、字体、字号只用 App 给的 --coda-* 变量(插件视图的主题变量)。 */
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,57 @@ 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 { 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); }
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
+ /* 标题行:根目录名,随内容滚走。它在滚动容器里占着自己的高度,tree.mjs 把 spacer 的 offsetTop 扣掉。 */
47
+ .section { padding: 10px 16px 4px; font-size: .85em; font-weight: 600; color: var(--coda-secondary, #6e6e73); cursor: default; }
48
+ /* 行宽按 #tree 的宽度查询:窄于约 220 点时 Mac 的 meta 不显示。 */
49
+ #tree { container: tree / inline-size; }
50
+ .spacer { position: relative; margin: 0 8px; }
40
51
  .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; }
52
+ /* 缩进按行的 --d(深度)算,iPhone 的分隔线起点也用它。 */
53
+ .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));
54
+ border-radius: var(--coda-radius, 6px); white-space: nowrap; cursor: default; }
42
55
  .row.sel { background: var(--coda-selection, var(--hover)); }
43
56
  .row.more, .row.retry { color: var(--coda-accent, #0a84ff); cursor: pointer; }
44
57
  .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); }
47
- .name { flex: 1 1 auto; min-width: 0; }
48
- .tail { flex: none; width: 1em; text-align: center; color: var(--coda-secondary, #6e6e73); }
58
+ .row.dead .name, .row.dead .ico { color: var(--coda-tertiary, #a1a1a6); }
59
+ .chev { flex: none; display: grid; place-items: center; width: 14px; font-size: .8em; color: var(--coda-secondary, #6e6e73); }
60
+ .chev svg { display: block; }
61
+ .row.open .chev svg { transform: rotate(90deg); }
62
+ .row.file .chev svg { visibility: hidden; } /* Mac 的文件只留空槽,名字才对齐 */
63
+ .ico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
64
+ .ico svg { display: block; }
65
+ .row.dir .ico { color: var(--coda-accent, #0a84ff); }
66
+ .main { flex: 1 1 auto; min-width: 0; }
67
+ .name { display: block; }
68
+ .sub { display: none; }
69
+ .meta { flex: none; padding-left: 4px; font-variant-numeric: tabular-nums; white-space: nowrap; }
70
+ @container tree (max-width: 220px) { .meta { display: none; } }
71
+ .tail { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
72
+ .tail:empty { display: none; }
73
+ .tail svg { display: block; }
49
74
  .tail.warn { color: var(--coda-orange, #b25000); }
50
75
 
51
76
  /* 预览:头部永远占一行名字加一行说明,加载和失败只换下面的内容。 */
52
- .preview { display: flex; flex-direction: column; overflow: hidden; }
77
+ .preview { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归文件页的工具条 */
53
78
  .phead button { flex: none; white-space: nowrap; }
54
79
  .phead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
80
+ .pico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
81
+ .pico svg { display: block; }
82
+ 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); }
83
+ button.icon svg { display: block; }
55
84
  .pname { font-weight: 600; }
56
85
  .pbody { flex: 1 1 auto; min-height: 0; overflow: auto; }
57
86
  .code { display: flex; width: max-content; min-width: 100%; font: .9em/1.55 var(--coda-mono, ui-monospace, Menlo, monospace); }
@@ -63,10 +92,16 @@ p { margin: 0; }
63
92
  .image img { max-width: 100%; height: auto; border-radius: var(--coda-radius, 6px); background: var(--coda-secondary-background, transparent); }
64
93
 
65
94
  #notice:focus { outline: none; }
66
- .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);
95
+ .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);
67
96
  background: var(--coda-card, var(--coda-background, #fff)); }
68
- .menu button { display: block; width: 100%; padding: 5px 10px; border-radius: var(--coda-radius, 6px); }
69
- .menu-path { max-width: 280px; padding: 4px 10px 6px; overflow-wrap: anywhere; }
97
+ .menu button { display: flex; align-items: center; gap: 6px; width: 100%; padding: 5px 10px; border-radius: var(--coda-radius, 6px); }
98
+ .check { flex: none; display: grid; place-items: center; width: 16px; color: var(--coda-accent, #0a84ff); }
99
+ .check svg { display: block; }
100
+ /* 菜单宽度随内容,上限见 .menu:路径超过上限折行,大小 · 完整修改时间不折。 */
101
+ .menu-path { padding: 4px 10px 6px; overflow-wrap: anywhere; }
102
+ .menu-info { padding: 0 10px 6px; margin-top: -4px; white-space: nowrap; }
103
+ .menu-sep { margin: 4px 6px; border: 0; border-top: 1px solid var(--line); }
104
+ .menu-head { padding: 4px 10px 2px; font-weight: 600; }
70
105
  .pmeta.warn { color: var(--coda-orange, #b25000); }
71
106
 
72
107
  .hint { position: fixed; left: calc(8px + var(--safe-left)); right: calc(8px + var(--safe-right)); bottom: calc(8px + var(--safe-bottom)); z-index: 10;
@@ -75,24 +110,45 @@ p { margin: 0; }
75
110
 
76
111
  /* 悬停只在有鼠标时:触摸后不会留着。 */
77
112
  @media (hover: hover) {
78
- .bar button:hover { background: var(--hover); }
113
+ .bar button:hover, button.icon:hover { background: var(--hover); }
79
114
  .row.entry:hover { background: var(--hover); }
115
+ .row.entry.sel:hover { background: var(--coda-selection, var(--hover)); }
80
116
  .menu button:hover { background: var(--coda-selection, var(--hover)); }
81
117
  }
82
118
 
83
- /* iPhone:行和按钮至少 44 点;行高在这里定,tree.mjs 量 .row 得到,不另写一份。 */
84
- .coda-ios { --row-h: 44px; --indent: 20px; --pad: 16px; }
85
- .coda-ios .row { padding-right: 16px; gap: 8px; }
86
- .coda-ios .chev { width: 24px; }
119
+ /* iPhone:行和按钮至少 44 点;行高 58 点(两行:名字、日期 · 大小)在这里定,tree.mjs 量 .row 得到,不另写一份。
120
+ 目录树是 Coda 卡片外形的分组列表:.spacer 就是卡片(高度已等于全部行高),首尾圆角不用逐行判断。 */
121
+ .coda-ios { --row-h: 58px; --indent: 20px; --pad: 16px; }
122
+ .coda-ios .section { display: none; } /* 根目录名归页内工具条 */
123
+ .coda-ios .spacer { margin: 8px 16px; overflow: hidden; border-radius: var(--coda-radius-card, 12px); background: var(--coda-card, var(--coda-secondary-background, #fff)); }
124
+ /* 描边用伪元素:不占尺寸,spacer 加 border 会让内容区少 2 点,末行被裁。 */
125
+ .coda-ios .spacer::after { content: ''; position: absolute; inset: 0; border: 1px solid var(--coda-outline, var(--line)); border-radius: inherit; pointer-events: none; }
126
+ .coda-ios .row { gap: 8px; padding-right: 16px; border-radius: 0; }
127
+ .coda-ios .row + .row::before { content: ''; position: absolute; top: 0; right: 0; height: 1px; background: var(--coda-separator, var(--line));
128
+ left: calc(var(--pad) + var(--indent) * var(--d, 0) + 22px + 8px); } /* 从图标之后开始 */
129
+ .coda-ios .row.sel + .row::before, .coda-ios .row.sel::before { background: transparent; }
87
130
  .coda-ios .row { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单;选择和拖动另外已关 */
88
131
  .coda-ios .row.entry:active { background: var(--hover); }
89
- .coda-ios .bar { gap: 8px; padding: 4px 8px; }
132
+ .coda-ios .ico svg { width: 22px; height: 22px; }
133
+ .coda-ios .main { display: flex; flex-direction: column; justify-content: center; } /* 没有第二行的条目,名字垂直居中 */
134
+ .coda-ios .sub { display: block; }
135
+ .coda-ios .meta { display: none; }
136
+ .coda-ios .chev { order: 9; width: 14px; }
137
+ .coda-ios .row.dir .chev { color: var(--coda-accent, #0a84ff); }
138
+ .coda-ios .row.file .chev svg { visibility: visible; }
139
+ .coda-ios .row.dead .chev { display: none; }
90
140
  .coda-ios button { min-height: 44px; min-width: 44px; }
91
- .coda-ios .bar button { padding: 0 12px; border-radius: var(--coda-radius, 10px); }
141
+ .coda-ios .bar { gap: 0; padding: 0 8px; }
142
+ .coda-ios .bar button { gap: 6px; padding: 0 12px; border-radius: var(--coda-radius, 10px); color: var(--coda-accent, #0a84ff); }
143
+ .coda-ios .bar button.icon { padding: 0; justify-content: center; }
144
+ .coda-ios .bar.bottom { padding: 0 8px var(--safe-bottom); }
145
+ .coda-ios .bar.bottom button { flex: 1 1 0; justify-content: center; } /* 横排单行:横屏只有 390 点高 */
92
146
  .coda-ios .phead { padding: 4px 12px; gap: 4px; }
93
147
  .coda-ios .phead button { padding: 0 8px; }
94
- .coda-ios .menu button { padding: 0 12px; text-align: left; }
148
+ .coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
149
+ .coda-ios .menu button { padding: 0 12px; }
150
+ .coda-ios .menu-head { padding: 6px 12px 2px; }
95
151
 
96
152
  /* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
97
- .exit { display: none; }
98
- @media (orientation: landscape) and (max-height: 500px) { .coda-ios .exit { display: inline-flex; align-items: center; } }
153
+ .bar .exit { display: none; }
154
+ @media (orientation: landscape) and (max-height: 500px) { .coda-ios .bar .exit { display: inline-flex; } }
package/view/tree.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  // 目录树:visibleRows 把模型压成扁平行(纯逻辑);mountTree 只画可见的那一段,上万条也不卡。
2
- import { EntryKind } from './protocol.mjs';
2
+ import { EntryKind, SortKey } from './protocol.mjs';
3
+ import { dateText, infoText, sizeText, subText } from './format.mjs';
4
+ import { icon, iconFor } from './icons.mjs';
3
5
  import { isHidden, joinPath } from './model.mjs';
4
6
 
5
7
  const OVERSCAN = 8;
@@ -7,7 +9,7 @@ const OVERSCAN = 8;
7
9
  export const HOLD_MS = 500;
8
10
  export const HOLD_SLOP = 10;
9
11
 
10
- // 行:{ type: 'entry' | 'status' | 'more', path, name, depth, kind, link, open, busy, failed, text }
12
+ // 行:{ type: 'entry' | 'status' | 'more', path, name, depth, kind, link, size, mtimeMs, open, busy, failed, text };size、mtimeMs 读不到时没有
11
13
  export function visibleRows(model) {
12
14
  const rows = [];
13
15
  const walk = (dir, depth) => {
@@ -16,12 +18,14 @@ export function visibleRows(model) {
16
18
  if (!state.loaded) return rows.push({ type: 'status', path: dir, depth, text: `读取失败:${state.error.message}`, retry: true });
17
19
  // 刷新失败:旧条目保留,最上方加一行原因,点它重试。这一行不算内容,空目录的判断在它之后。
18
20
  if (state.error) rows.push({ type: 'status', path: dir, depth, text: `更新失败:${state.error.message}`, retry: true });
21
+ // 条目太多按名称排了:说明行固定行高,不算内容,不影响「空目录」的判断。
22
+ if (state.sortFallback) rows.push({ type: 'status', path: dir, depth, text: '条目太多,只能按名称排列' });
19
23
  const before = rows.length;
20
24
  for (const entry of state.entries) {
21
25
  if (!model.showHidden && isHidden(entry.name)) continue;
22
26
  const path = joinPath(dir, entry.name), open = entry.kind === EntryKind.DIR && model.expanded.has(path);
23
27
  rows.push({
24
- type: 'entry', path, name: entry.name, depth, kind: entry.kind, link: entry.link === true, open,
28
+ type: 'entry', path, name: entry.name, depth, kind: entry.kind, link: entry.link === true, size: entry.size, mtimeMs: entry.mtimeMs, open,
25
29
  busy: open && model.dirs.get(path)?.loading === true, failed: open ? model.dirs.get(path)?.error?.message : undefined,
26
30
  });
27
31
  if (open) walk(path, depth + 1);
@@ -33,39 +37,55 @@ export function visibleRows(model) {
33
37
  return rows;
34
38
  }
35
39
 
40
+ // 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
41
+ export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
42
+
36
43
  const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
37
44
 
38
- // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, path, kind) }
39
- // draggable:触屏上拖行没有用处,还会抢滚动,关掉。
45
+ // handlers: { open(path), toggle(path), more(path), retry(path), menu(event, row) }
46
+ // menu 收到整行(路径、种类、大小、修改时间)。
47
+ // draggable:触屏上拖行没有用处,还会抢滚动,关掉。now:当前时间,日期的相对格式用,测试可注入。
40
48
  // longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
41
- 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';
49
+ export function mountTree(el, model, handlers, { draggable = true, longPress = false, now = Date.now } = {}) {
50
+ const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
51
+ title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
52
+ title.hidden = true;
44
53
  spacer.append(win);
45
- el.replaceChildren(spacer);
54
+ el.replaceChildren(title, spacer);
46
55
  let rows = [], rowH = 24;
47
56
 
57
+ // 探针放在 spacer 里:量到的是行在真实结构里的高度。
48
58
  const measure = () => {
49
59
  const probe = document.createElement('div');
50
60
  probe.className = 'row';
51
61
  probe.style.visibility = 'hidden';
52
- el.append(probe);
62
+ spacer.append(probe);
53
63
  rowH = probe.getBoundingClientRect().height || rowH;
54
64
  probe.remove();
55
65
  };
56
66
 
67
+ // 分区标题:根目录的名字(root.path 的最后一段),不是 ready 时不显示。iPhone 上由 CSS 隐藏。
68
+ const paintTitle = () => {
69
+ const text = model.ready ? rootName(model.root.path) : '';
70
+ if (title.textContent !== text) title.textContent = text;
71
+ title.hidden = text === '';
72
+ };
73
+
74
+ // 标题在滚动容器里,把 spacer 推下去 offsetTop:行的位置按 spacer 内部算,滚动值(保存和恢复的)仍是容器的。
57
75
  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);
76
+ const top = el.scrollTop - (spacer.offsetTop || 0);
77
+ const first = Math.max(0, Math.floor(top / rowH) - OVERSCAN);
78
+ const last = Math.min(rows.length, Math.ceil((top + el.clientHeight) / rowH) + OVERSCAN);
60
79
  win.style.transform = `translateY(${first * rowH}px)`;
61
- win.replaceChildren(...rows.slice(first, last).map((row, i) => rowNode(row, first + i)));
80
+ const time = now();
81
+ win.replaceChildren(...rows.slice(first, last).map((row, i) => rowNode(row, first + i, time)));
62
82
  }
63
83
 
64
- function rowNode(row, index) {
84
+ function rowNode(row, index, time) {
65
85
  const node = document.createElement('div');
66
86
  node.className = `row ${row.type}`;
67
87
  node.dataset.i = index;
68
- node.style.paddingLeft = `calc(var(--indent) * ${row.depth} + var(--pad))`;
88
+ node.style.cssText = `--d: ${row.depth}`; // 缩进由 CSS 按 --d 算,分隔线的起点也用它
69
89
  if (row.type !== 'entry') {
70
90
  node.textContent = row.text;
71
91
  if (row.retry) { node.classList.add('retry'); node.title = row.text; } // 长原因在行里被截断,Mac 悬停看全
@@ -77,15 +97,41 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
77
97
  if (row.path === model.selected) node.classList.add('sel');
78
98
  if (row.kind === EntryKind.DIR) node.setAttribute('aria-expanded', row.open);
79
99
  if (!dead && draggable) node.draggable = true;
80
- const chevron = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
100
+ // 一份 DOM:箭头、图标、名字(iPhone 下面跟日期 · 大小)、Mac 的 meta、尾标;iPhone 的箭头用 CSS order 排到最后,两端各用 CSS 藏起另一端的那段。
101
+ const chevron = document.createElement('span'), glyph = document.createElement('span'), main = document.createElement('span'), label = document.createElement('span'), tail = document.createElement('span');
81
102
  chevron.className = 'chev';
82
- chevron.textContent = row.kind === EntryKind.DIR ? (row.busy ? '…' : row.open ? '▾' : '▸') : '';
103
+ if (row.busy) chevron.textContent = '…';
104
+ else if (!dead) chevron.append(icon('chevron', 12)); // 文件的箭头 Mac 上由 CSS 藏起来,只占位
105
+ if (row.open) node.classList.add('open');
106
+ glyph.className = 'ico';
107
+ glyph.append(icon(iconFor(row)));
108
+ main.className = 'main';
83
109
  label.className = 'name truncate';
84
110
  label.textContent = row.name;
111
+ main.append(label);
112
+ const parts = [chevron, glyph, main];
113
+ if (!dead) {
114
+ // 能打开的条目带信息;link、other 没有。文件夹没有的部分整个不画:iPhone 名字垂直居中,Mac 右侧不留空位。
115
+ const subContent = subText(row, time), metaContent = model.sort === SortKey.DATE ? dateText(row, time) : sizeText(row);
116
+ if (subContent) {
117
+ const sub = document.createElement('span');
118
+ sub.className = 'sub caption secondary truncate';
119
+ sub.textContent = subContent;
120
+ main.append(sub);
121
+ }
122
+ if (metaContent) {
123
+ const meta = document.createElement('span');
124
+ meta.className = 'meta caption secondary';
125
+ meta.textContent = metaContent;
126
+ parts.push(meta);
127
+ }
128
+ }
85
129
  tail.className = row.failed ? 'tail warn' : 'tail';
86
- tail.textContent = row.failed ? '!' : row.link ? '↪' : '';
87
- 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);
130
+ if (row.failed) tail.append(icon('warning', 14));
131
+ else if (row.link) tail.append(icon('turn', 14));
132
+ const info = row.failed || TITLES[row.kind] ? '' : infoText(row);
133
+ 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;
134
+ node.append(...parts, tail);
89
135
  return node;
90
136
  }
91
137
 
@@ -105,7 +151,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
105
151
  hold = { id, x, y, timer: setTimeout(() => {
106
152
  hold = null;
107
153
  swallow = true;
108
- handlers.menu({ clientX: x, clientY: y }, row.path);
154
+ handlers.menu({ clientX: x, clientY: y }, row);
109
155
  }, HOLD_MS) };
110
156
  });
111
157
  el.addEventListener('pointermove', event => {
@@ -126,7 +172,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
126
172
  });
127
173
  el.addEventListener('contextmenu', event => {
128
174
  const row = rowOf(event);
129
- if (row?.type === 'entry') { event.preventDefault(); handlers.menu(event, row.path); }
175
+ if (row?.type === 'entry') { event.preventDefault(); handlers.menu(event, row); }
130
176
  });
131
177
  el.addEventListener('dragstart', event => {
132
178
  const row = rowOf(event);
@@ -138,7 +184,7 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
138
184
  new ResizeObserver(paint).observe(el);
139
185
 
140
186
  return {
141
- render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paint(); },
187
+ render() { rows = visibleRows(model); spacer.style.height = `${rows.length * rowH}px`; paintTitle(); paint(); },
142
188
  remeasure() { measure(); this.render(); },
143
189
  scroll: () => el.scrollTop,
144
190
  scrollTo(top) { el.scrollTop = top; },