@coda-console/resource-manager 0.4.0 → 0.6.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/icons.mjs CHANGED
@@ -1,16 +1,52 @@
1
- // 图标:页面里自己画的 SVG,形状近似 SF Symbol,不打包 Apple 的符号。颜色都是 currentColor,由 CSS 定。
1
+ // 图标:页面里自己画的 SVG,形状近似 SF Symbol,不打包 Apple 的符号,也不用任何第三方素材。颜色都是 currentColor,由 CSS 定:
2
+ // 种类图标带 tone-* 类(颜色见 style.css 的 --tone-*),没有色调的(工具条、菜单的图标)跟随所在位置的文字色。
2
3
  import { EntryKind } from './protocol.mjs';
3
4
 
4
5
  const SVG_NS = 'http://www.w3.org/2000/svg';
5
6
 
6
- // 描边图标的 24×24 画法;folder 是填充的,单独标。
7
+ // 描边图标的 24×24 画法;folder 是填充的,单独标。形状 [标签, 属性, 文字?]:个别形状用自己的 fill、stroke、opacity 盖过根上的,文字放第三项。
8
+ const PAGE = [['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' }]];
9
+ const FILLED = { fill: 'currentColor', stroke: 'none' };
10
+ // 语言类:淡底加字母,不是任何语言的徽标;字母用 text,字体跟页面。
11
+ const badge = (letters, size = 10.5) => [
12
+ ['rect', { x: 2, y: 3, width: 20, height: 18, rx: 4.5, ...FILLED, opacity: 0.16 }],
13
+ ['text', { x: 12, y: Number((12 + size * 0.36).toFixed(2)), 'text-anchor': 'middle', 'font-size': size, 'font-weight': 700, ...FILLED }, letters],
14
+ ];
7
15
  const SHAPES = {
8
16
  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' }]] },
17
+ doc: { shapes: PAGE },
18
+ text: { shapes: [...PAGE, ['path', { d: 'M9 13h6M9 17h4' }]] },
19
+ code: { shapes: [...PAGE, ['path', { d: 'M10.5 12.5 8.5 15l2 2.5M13.5 12.5l2 2.5-2 2.5' }]] },
20
+ ts: { shapes: badge('TS') },
21
+ js: { shapes: badge('JS') },
22
+ py: { shapes: badge('Py') },
23
+ go: { shapes: badge('Go') },
24
+ rs: { shapes: badge('Rs') },
25
+ swift: { shapes: badge('Sw') },
26
+ c: { shapes: badge('C', 12) },
27
+ java: { shapes: badge('Jv') },
28
+ ruby: { shapes: badge('Rb') },
29
+ md: { shapes: badge('MD', 9.5) },
30
+ pdf: { shapes: badge('PDF', 8) },
31
+ json: { shapes: [['path', { d: 'M9 4C6.5 4 7 7 7 9c0 2-1 3-2.5 3C6 12 7 13 7 15c0 2-.5 5 2 5M15 4c2.5 0 2 3 2 5 0 2 1 3 2.5 3-1.5 0-2.5 1-2.5 3 0 2 .5 5-2 5' }]] },
32
+ html: { shapes: [['path', { d: 'M8.5 7 3.5 12l5 5M15.5 7l5 5-5 5' }]] },
33
+ css: { shapes: [['path', { d: 'M9.5 4 8 20M16 4l-1.5 16M5 9h15M4 15h15' }]] },
34
+ config: { shapes: [['path', { d: 'M4 7h9M17 7h3M4 17h3M11 17h9' }], ['circle', { cx: 15, cy: 7, r: 2.3 }], ['circle', { cx: 9, cy: 17, r: 2.3 }]] },
35
+ shell: { shapes: [['rect', { x: 3, y: 4, width: 18, height: 16, rx: 3.5 }], ['path', { d: 'm7 9.5 3 2.5-3 2.5M12.5 15.5H17' }]] },
36
+ image: { shapes: [['rect', { x: 3, y: 4, width: 18, height: 16, rx: 3 }], ['circle', { cx: 8.7, cy: 9.5, r: 1.6, ...FILLED }], ['path', { d: 'm4 17.5 4.5-4.5 3.5 3.5 3-3 5 5' }]] },
37
+ video: { shapes: [['rect', { x: 3, y: 5, width: 18, height: 14, rx: 3 }], ['path', { d: 'M10 9v6l5-3z', ...FILLED }]] },
38
+ audio: { shapes: [['path', { d: 'M9 18V6l10-2v12' }], ['circle', { cx: 6.5, cy: 18, r: 2.5 }], ['circle', { cx: 16.5, cy: 16, r: 2.5 }]] },
39
+ archive: { shapes: [...PAGE, ['path', { d: 'M10 3h2v2h-2zM12 5h2v2h-2zM10 7h2v2h-2zM12 9h2v2h-2z', ...FILLED }], ['rect', { x: 10, y: 12, width: 4, height: 5, rx: 1, 'stroke-width': 1.5 }]] },
40
+ sheet: { shapes: [['rect', { x: 3, y: 4, width: 18, height: 16, rx: 3 }], ['path', { d: 'M3 10h18M3 15h18M10 10v10' }]] },
41
+ database: { shapes: [['ellipse', { cx: 12, cy: 6, rx: 8, ry: 3 }], ['path', { d: 'M4 6v12c0 1.7 3.6 3 8 3s8-1.3 8-3V6M4 12c0 1.7 3.6 3 8 3s8-1.3 8-3' }]] },
42
+ font: { shapes: [['text', { x: 12, y: 17.5, 'text-anchor': 'middle', 'font-size': 16, 'font-weight': 600, ...FILLED }, 'Aa']] },
43
+ lock: { shapes: [['rect', { x: 5, y: 10.5, width: 14, height: 10, rx: 2.5 }], ['path', { d: 'M8.5 10.5V8a3.5 3.5 0 0 1 7 0v2.5' }]] },
44
+ git: { shapes: [['circle', { cx: 7, cy: 6, r: 2.2 }], ['circle', { cx: 7, cy: 18, r: 2.2 }], ['circle', { cx: 17, cy: 8, r: 2.2 }], ['path', { d: 'M7 8.2v7.6M17 10.2c0 3-3 3.8-10 5' }]] },
45
+ env: { shapes: [['circle', { cx: 8, cy: 12, r: 4 }], ['path', { d: 'M12 12h9M18 12v3.5M21 12v2.5' }]] },
46
+ manifest: { shapes: [['path', { d: 'M12 3 4 7.5v9L12 21l8-4.5v-9L12 3zM4 7.5 12 12l8-4.5M12 12v9' }]] },
47
+ license: { shapes: [...PAGE, ['circle', { cx: 11.5, cy: 13.5, r: 2.5, 'stroke-width': 1.5 }], ['path', { d: 'm10 15.5-1 3.5 2.5-1 2.5 1-1-3.5', 'stroke-width': 1.5 }]] },
12
48
  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' }]] },
49
+ other: { shapes: [...PAGE, ['path', { d: 'M9.5 14.5h5' }]] },
14
50
  chevron: { shapes: [['path', { d: 'm9 5 7 7-7 7' }]] },
15
51
  warning: { shapes: [['path', { d: 'M12 4 2.8 19.5h18.4L12 4z' }], ['path', { d: 'M12 10v4.5M12 17.2v.1' }]] },
16
52
  back: { shapes: [['path', { d: 'm15 5-7 7 7 7' }]] },
@@ -22,35 +58,116 @@ const SHAPES = {
22
58
  turn: { shapes: [['path', { d: 'M5 18v-4a4 4 0 0 1 4-4h10' }], ['path', { d: 'm15 6 4 4-4 4' }]] },
23
59
  };
24
60
 
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'],
61
+ // 种类表:种类文字(属性页)和图标(目录树、预览头部)查同一张,不另写一份。
62
+ // glyph 是上面 SHAPES 里的图标名,每种一个图标;tone 是色调名,颜色在 style.css 的 --tone-*:accent 是强调色(文件夹),muted 不上色(链接、特殊文件不能打开,跟随三级文字色)。
63
+ const KINDS = {
64
+ dir: { glyph: 'folder', tone: 'accent', label: '文件夹' },
65
+ link: { glyph: 'link', tone: 'muted', label: '链接' },
66
+ other: { glyph: 'other', tone: 'muted', label: '特殊文件' },
67
+ file: { glyph: 'doc', tone: 'gray', label: '其他文件' },
68
+ text: { glyph: 'text', tone: 'gray', label: '文本' },
69
+ markdown: { glyph: 'md', tone: 'blue', label: 'Markdown 文档' },
70
+ typescript: { glyph: 'ts', tone: 'blue', label: 'TypeScript' },
71
+ javascript: { glyph: 'js', tone: 'yellow', label: 'JavaScript' },
72
+ python: { glyph: 'py', tone: 'green', label: 'Python' },
73
+ go: { glyph: 'go', tone: 'teal', label: 'Go' },
74
+ rust: { glyph: 'rs', tone: 'orange', label: 'Rust' },
75
+ swift: { glyph: 'swift', tone: 'orange', label: 'Swift' },
76
+ c: { glyph: 'c', tone: 'blue', label: 'C / C++' },
77
+ java: { glyph: 'java', tone: 'red', label: 'Java / Kotlin' },
78
+ ruby: { glyph: 'ruby', tone: 'red', label: 'Ruby' },
79
+ code: { glyph: 'code', tone: 'gray', label: '源代码' },
80
+ json: { glyph: 'json', tone: 'yellow', label: 'JSON' },
81
+ html: { glyph: 'html', tone: 'orange', label: 'HTML / XML' },
82
+ css: { glyph: 'css', tone: 'purple', label: 'CSS' },
83
+ yaml: { glyph: 'config', tone: 'pink', label: 'YAML / TOML' },
84
+ shell: { glyph: 'shell', tone: 'green', label: 'Shell 脚本' },
85
+ image: { glyph: 'image', tone: 'purple', label: '图片' },
86
+ video: { glyph: 'video', tone: 'pink', label: '视频' },
87
+ audio: { glyph: 'audio', tone: 'pink', label: '音频' },
88
+ pdf: { glyph: 'pdf', tone: 'red', label: 'PDF 文档' },
89
+ archive: { glyph: 'archive', tone: 'orange', label: '压缩包' },
90
+ sheet: { glyph: 'sheet', tone: 'green', label: '表格' },
91
+ database: { glyph: 'database', tone: 'teal', label: '数据库' },
92
+ font: { glyph: 'font', tone: 'gray', label: '字体' },
93
+ lock: { glyph: 'lock', tone: 'gray', label: '锁文件' },
94
+ git: { glyph: 'git', tone: 'orange', label: 'Git 配置' },
95
+ env: { glyph: 'env', tone: 'yellow', label: '环境变量' },
96
+ manifest: { glyph: 'manifest', tone: 'green', label: '包清单' },
97
+ license: { glyph: 'license', tone: 'gray', label: '许可证' },
30
98
  };
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';
99
+
100
+ // 文件名(小写)优先,再按扩展名(小写);都不在表里的是「其他文件」。
101
+ const NAMES = {
102
+ git: ['.gitignore', '.gitattributes', '.gitmodules', '.gitconfig', '.gitkeep', '.mailmap'],
103
+ env: ['.env', '.envrc'],
104
+ manifest: ['package.json', 'cargo.toml', 'go.mod', 'pyproject.toml', 'requirements.txt', 'gemfile', 'package.swift', 'pom.xml', 'build.gradle', 'composer.json', 'setup.py'],
105
+ lock: ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'cargo.lock', 'gemfile.lock', 'poetry.lock', 'go.sum', 'composer.lock', 'bun.lockb', 'podfile.lock'],
106
+ license: ['license', 'licence', 'copying', 'unlicense', 'license.md', 'license.txt', 'licence.md', 'licence.txt', 'copying.md', 'copying.txt'],
107
+ };
108
+ const EXTENSIONS = {
109
+ text: ['txt', 'text', 'log', 'rtf'],
110
+ markdown: ['md', 'markdown', 'mdx'],
111
+ typescript: ['ts', 'tsx', 'mts', 'cts'],
112
+ javascript: ['js', 'mjs', 'cjs', 'jsx'],
113
+ python: ['py', 'pyw', 'pyi'],
114
+ go: ['go'],
115
+ rust: ['rs'],
116
+ swift: ['swift'],
117
+ c: ['c', 'h', 'cc', 'cpp', 'cxx', 'hpp', 'hh'],
118
+ java: ['java', 'kt', 'kts'],
119
+ ruby: ['rb', 'erb'],
120
+ code: ['vue', 'scss', 'sass', 'less', 'cs', 'php', 'lua', 'sql', 'dart', 'scala', 'r', 'pl'],
121
+ json: ['json', 'jsonc', 'json5'],
122
+ html: ['html', 'htm', 'xml', 'xhtml', 'plist'],
123
+ css: ['css'],
124
+ yaml: ['yml', 'yaml', 'toml'],
125
+ shell: ['sh', 'bash', 'zsh', 'fish'],
126
+ image: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'ico', 'bmp', 'heic', 'avif', 'tif', 'tiff'],
127
+ video: ['mp4', 'mov', 'm4v', 'avi', 'mkv', 'webm'],
128
+ audio: ['mp3', 'wav', 'm4a', 'aac', 'flac', 'ogg', 'aiff'],
129
+ pdf: ['pdf'],
130
+ archive: ['zip', 'tar', 'gz', 'tgz', 'bz2', 'xz', '7z', 'rar'],
131
+ sheet: ['csv', 'tsv', 'xls', 'xlsx', 'numbers', 'ods'],
132
+ database: ['db', 'sqlite', 'sqlite3'],
133
+ font: ['ttf', 'otf', 'woff', 'woff2'],
134
+ };
135
+ const invert = table => new Map(Object.entries(table).flatMap(([id, keys]) => keys.map(key => [key, id])));
136
+ const BY_NAME = invert(NAMES), BY_EXT = invert(EXTENSIONS);
137
+
138
+ // 一个条目的种类:{ glyph, tone, label }。目录、链接、其他按种类;文件先按整个文件名,再按扩展名(点开头的名字没有扩展名)。
139
+ // 指向工作目录内的链接按目标的种类显示(条目自己的 kind 已经是目标的)。
140
+ export function kindOf({ kind, name }) {
141
+ if (kind === EntryKind.DIR) return KINDS.dir;
142
+ if (kind === EntryKind.LINK) return KINDS.link;
143
+ if (kind === EntryKind.OTHER) return KINDS.other;
144
+ const lower = String(name).toLowerCase(), dot = lower.lastIndexOf('.');
145
+ return KINDS[BY_NAME.get(lower) ?? (lower.startsWith('.env.') ? 'env' : undefined) ?? (dot > 0 ? BY_EXT.get(lower.slice(dot + 1)) : undefined) ?? 'file'];
40
146
  }
41
147
 
42
- // 造一个 SVG 元素;size 是点数,CSS 可以按平台覆盖。
148
+ // 图标名:种类表里的 glyph。
149
+ export const iconFor = entry => kindOf(entry).glyph;
150
+
151
+ // 图标名到色调:从种类表来,一个图标只能有一种色调(icons.test 查)。没有色调(muted)和不在表里的图标(箭头、返回这些)不带类。
152
+ const TONE_OF = new Map(Object.values(KINDS).filter(({ tone }) => tone !== 'muted').map(({ glyph, tone }) => [glyph, tone]));
153
+
154
+ // 造一个 SVG 元素;size 是点数,CSS 可以按平台覆盖。种类图标带 tone-* 类,颜色由 CSS 定。
43
155
  export function icon(name, size = 16) {
44
156
  const { fill = false, shapes } = SHAPES[name];
45
157
  const svg = document.createElementNS(SVG_NS, 'svg');
46
158
  const attrs = { width: size, height: size, viewBox: '0 0 24 24', 'aria-hidden': 'true', ...(fill
47
159
  ? { fill: 'currentColor' }
48
160
  : { fill: 'none', stroke: 'currentColor', 'stroke-width': 1.8, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }) };
161
+ if (TONE_OF.has(name)) attrs.class = `tone-${TONE_OF.get(name)}`;
49
162
  for (const [key, value] of Object.entries(attrs)) svg.setAttribute(key, value);
50
- for (const [tag, shape] of shapes) {
163
+ for (const [tag, shape, text] of shapes) {
51
164
  const node = document.createElementNS(SVG_NS, tag);
52
165
  for (const [key, value] of Object.entries(shape)) node.setAttribute(key, value);
166
+ if (text !== undefined) node.textContent = text;
53
167
  svg.append(node);
54
168
  }
55
169
  return svg;
56
170
  }
171
+
172
+ // 给测试:两张表本身,不是复制。
173
+ export { KINDS, SHAPES };
package/view/index.html CHANGED
@@ -10,10 +10,13 @@
10
10
  <div id="bar" class="bar" hidden></div>
11
11
  <main id="stage">
12
12
  <div id="tree" class="pane tree" role="tree"></div>
13
+ <div id="grid" class="pane grid"></div>
13
14
  <div id="preview" class="pane preview"></div>
14
15
  <div id="notice" class="pane empty secondary"></div>
16
+ <div id="info" class="pane preview info"></div>
15
17
  </main>
16
18
  <div id="menu" class="menu" role="menu" hidden></div>
19
+ <div id="submenu" class="menu" role="menu" hidden></div>
17
20
  <div id="hint" class="hint" role="status" hidden></div>
18
21
  <script type="module" src="app.mjs"></script>
19
22
  </body>
package/view/info.mjs ADDED
@@ -0,0 +1,117 @@
1
+ // 属性页:一个文件、文件夹或链接的属性。只管显示:属性和文件夹大小由页面经 stat、size 取,这里不知道聊天和目录树。
2
+ // 设计见 docs/design/phase-3.md 第 13.8 节。进入、返回和原生返回的声明在页面(app.mjs)。
3
+ import { h } from './dom.mjs';
4
+ import { dirSizeText, fileSizeText, formatFullDate, modeText } from './format.mjs';
5
+ import { icon, kindOf } from './icons.mjs';
6
+ import { EntryKind } from './protocol.mjs';
7
+
8
+ // el:#info 面板。选项:
9
+ // stat(path) → Promise<statted 回答>;size(path, signal) → Promise<sized 回答>,signal 中止后请求静默作废(连接层同时通知模块停止统计);
10
+ // rootPath():根目录的绝对路径,拼完整路径用;round():页面的数据轮次,每次 changed 加一;
11
+ // back:给了就在头部最左边放「返回」(Mac,和没有原生返回的环境)。
12
+ export function mountInfo(el, { stat, size, rootPath, round, back = null }) {
13
+ const head = h('div', { class: 'phead' }), body = h('div', { class: 'pbody ibody selectable' });
14
+ el.replaceChildren(head, body);
15
+
16
+ let path = null, seed = null;
17
+ let reply = null, failure = null; // 属性的回答;取属性失败的原因
18
+ let sizing = null; // 文件夹大小这一次统计:{ abort, round, phase: 'running' | 'done' | 'failed', result, message }
19
+ let token = 0; // 取属性的序号:新的取代旧的,旧的回答不再用
20
+
21
+ const entry = () => reply ?? seed;
22
+ // 指向工作目录内的链接按目标显示:种类和图标看目标的名字(链接自己叫什么不管),没有目标的(外链接)看自己。
23
+ const shownKind = () => {
24
+ const { kind, name } = entry(), target = reply?.target;
25
+ return kindOf({ kind, name: target ? target.slice(target.lastIndexOf('/') + 1) : name });
26
+ };
27
+ const kindText = () => {
28
+ const base = shownKind().label;
29
+ if (entry().kind === EntryKind.LINK) return `${base}(指向工作目录外或已失效)`;
30
+ return reply?.link ? `${base}(链接)` : base;
31
+ };
32
+
33
+ const row = (label, value) => h('div', { class: 'irow' }, h('span', { class: 'ilabel secondary' }, label), h('div', { class: 'ivalue' }, value));
34
+ const link = (label, run) => h('button', { class: 'link', onclick: run }, label);
35
+ const note = (title, detail, action) => h('div', { class: 'empty' }, h('p', {}, title), detail ? h('p', { class: 'secondary' }, detail) : null, action ? link(action.label, action.run) : null);
36
+
37
+ // 文件夹大小这一行:计算中、结果(上一轮的标「上次统计」)、失败。结果可能不全(没统计完),也可能是上一轮的,这两种都放「重新统计」。
38
+ function sizeRow() {
39
+ if (sizing.phase === 'running') return row('大小', '计算中…');
40
+ if (sizing.phase === 'failed') return row('大小', [h('span', { class: 'warn' }, `统计失败:${sizing.message}`), ' ', link('重新统计', startSize)]);
41
+ const old = sizing.round !== round();
42
+ return row('大小', [dirSizeText(sizing.result), old ? ' · 上次统计' : '', ...(old || !sizing.result.complete ? [' ', link('重新统计', startSize)] : [])]);
43
+ }
44
+
45
+ function paint() {
46
+ const focused = body.contains(document.activeElement);
47
+ const { name, kind } = entry(), where = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '工作目录';
48
+ head.replaceChildren(...(back ? [h('button', { class: 'icon', 'aria-label': '返回', title: '返回', onclick: back }, icon('back', 18))] : []),
49
+ h('span', { class: 'pico', 'aria-hidden': 'true' }, icon(shownKind().glyph, 22)),
50
+ h('div', { class: 'grow' }, h('div', { class: 'pname truncate', title: path }, name), h('div', { class: 'pmeta caption secondary truncate' }, '属性')));
51
+ if (failure !== null) body.replaceChildren(note('读不到属性', failure, { label: '重试', run: () => load() }));
52
+ else if (reply === null) body.replaceChildren(h('div', { class: 'empty secondary' }, h('p', {}, '读取中…')));
53
+ else {
54
+ const { size: bytes, target, mtimeMs, birthtimeMs, mode } = reply;
55
+ body.replaceChildren(
56
+ row('种类', kindText()),
57
+ ...(kind === EntryKind.DIR && sizing ? [sizeRow()] : Number.isInteger(bytes) ? [row('大小', fileSizeText(bytes))] : []),
58
+ ...(target !== undefined ? [row('链接到', target)] : []),
59
+ row('位置', where),
60
+ row('完整路径', `${rootPath()}/${path}`),
61
+ ...(Number.isInteger(mtimeMs) ? [row('修改时间', formatFullDate(mtimeMs))] : []),
62
+ ...(Number.isInteger(birthtimeMs) ? [row('创建时间', formatFullDate(birthtimeMs))] : []),
63
+ ...(Number.isInteger(mode) ? [row('权限', modeText(mode))] : []),
64
+ );
65
+ }
66
+ if (focused) { body.tabIndex = -1; body.focus(); } // 重画换掉了聚焦的按钮:焦点留在属性页里
67
+ }
68
+
69
+ // 取属性。文件夹第一次取到时开始统计;之后(changed 重取)不自动重新统计,只把已有的结果标成上次统计。
70
+ async function load() {
71
+ const mine = ++token, asked = path;
72
+ failure = null;
73
+ if (reply === null) paint();
74
+ try {
75
+ const next = await stat(asked);
76
+ if (mine !== token) return;
77
+ reply = next;
78
+ if (next.kind === EntryKind.DIR && sizing === null) startSize();
79
+ } catch (error) {
80
+ if (error.stale || mine !== token) return;
81
+ failure = error.message;
82
+ }
83
+ paint();
84
+ }
85
+
86
+ // 统计文件夹:新的开始时旧的先中止(连接层告诉模块停)。关闭、换会话、根目录变化时也走中止,等待不会拖到超时。
87
+ function startSize() {
88
+ sizing?.abort.abort();
89
+ const abort = new AbortController(), mine = sizing = { abort, round: round(), phase: 'running' }, asked = path;
90
+ paint();
91
+ size(asked, abort.signal).then(result => Object.assign(mine, { phase: 'done', result }), error => {
92
+ if (error.stale) return null; // 取消、换会话:静默
93
+ return Object.assign(mine, { phase: 'failed', message: error.message });
94
+ }).then(done => { if (done && sizing === mine) paint(); });
95
+ }
96
+
97
+ return {
98
+ // seed:目录树里这一行(名字、种类),属性回来之前先画头部。
99
+ open(next, row) {
100
+ this.close();
101
+ path = next;
102
+ seed = { name: row.name, kind: row.kind };
103
+ load();
104
+ },
105
+ // 一轮结束:重取属性,不自动重新统计;已有的统计结果马上标成上次统计(页面的数据轮次变了)。
106
+ refresh() { if (path !== null) { if (reply !== null) paint(); load(); } },
107
+ close() {
108
+ sizing?.abort.abort();
109
+ sizing = null;
110
+ token++;
111
+ path = seed = reply = failure = null;
112
+ head.replaceChildren();
113
+ body.replaceChildren();
114
+ },
115
+ get path() { return path; },
116
+ };
117
+ }
package/view/menu.mjs ADDED
@@ -0,0 +1,195 @@
1
+ // 菜单:Mac 右键、iPhone 长按的行菜单,Mac 选中文字的右键菜单,iPhone 的 ⋯ 和 Mac 原生「更多」弹出的菜单都用它,树页和文件页各挂一个。
2
+ // 菜单开着时点菜单外只关菜单、紧接着的那次点击不交给页面;Escape、窗口失焦、旋转(窗口尺寸变化)关闭;页面里别的滚动区域滚动时由页面调 hide。
3
+ // 菜单可以有一层二级菜单(项里写 submenu)。「整次菜单」(current)和「当前层」(layer、fly)分开记:
4
+ // 在面板里切到下一层、退回来,整次菜单还是同一次,但上一层的按钮不再响应。第二个面板(sub)也算菜单里面,点它不是点外面。
5
+ import { icon } from './icons.mjs';
6
+
7
+ const CLOSE_MS = 150; // 鼠标移到别的项之后,侧面板收起前等多久:斜着移过去不会误关
8
+ const GAP = 2; // 侧面板和根面板之间留的空隙
9
+
10
+ // el:页面里的 #menu。sub:侧面板(#submenu),没有就不能弹出,二级菜单总是在 el 里切到下一层。
11
+ // flyout:true 时(Mac)鼠标移上去或点击「显示」这类项,侧面板在根面板左边弹出;放不下就退成切到下一层(iPhone 总是这样)。
12
+ // open 之后的位置按安全区夹住(横屏时客户端不替页面留);offsetWidth 在显示后才量得到。
13
+ // 高度有上限(style.css 的 .menu):内容多时在菜单里滚动,不超出安全区。
14
+ export function mountMenu(el, sub = null, { flyout = false } = {}) {
15
+ let opener = null; // 打开菜单的按钮;用点打开的没有
16
+ let current = null; // 这一次打开的标记:关闭之后留在手里的按钮不再响应
17
+ let layer = 0; // el 里现在是哪一层:每次换层(进下一层、退回、重新打开、关闭)加一,旧层的按钮据此作废
18
+ let fly = 0; // 侧面板的第几次展开:收起、重开都加一
19
+ let spec = null, anchor = null; // 根层的内容和锚点:退回根层、换层后重新定位要用
20
+ let flyButton = null; // 展开着侧面板的那个项
21
+ let closeTimer = null;
22
+
23
+ // items:[{ label, run, checked?, radio? } | { label, submenu: { items } } | { separator: true } | { heading }],checked 不是 undefined 的带对勾(radio 的是单选,其余是开关);
24
+ // 分隔线和小标题不是菜单项,不可点。hover:这一层的项在鼠标移上去时管侧面板(只有根层,且 flyout)。
25
+ // alive:这一层还有没有效,点击时先问它。
26
+ function build(items, alive, hover) {
27
+ const checkable = items.some(item => item.checked !== undefined); // 有开关项时,每一项都留出对勾的位置,文字才对齐
28
+ return items.map(item => {
29
+ const { label, run, checked, radio, separator, heading, submenu } = item;
30
+ if (separator) return Object.assign(document.createElement('hr'), { className: 'menu-sep' });
31
+ if (heading !== undefined) return Object.assign(document.createElement('p'), { className: 'menu-head secondary caption', textContent: heading });
32
+ const button = document.createElement('button'), name = document.createElement('span');
33
+ name.textContent = label;
34
+ if (checkable) {
35
+ const mark = document.createElement('span');
36
+ mark.className = 'check';
37
+ if (checked) mark.append(icon('check', 14));
38
+ button.append(mark);
39
+ }
40
+ button.append(name);
41
+ if (submenu) {
42
+ const arrow = document.createElement('span');
43
+ arrow.className = 'sub-arrow';
44
+ arrow.append(icon('chevron', 14));
45
+ button.append(arrow);
46
+ button.setAttribute('role', 'menuitem');
47
+ button.setAttribute('aria-haspopup', 'menu');
48
+ button.setAttribute('aria-expanded', 'false');
49
+ // 进下一层不是叶子项:不关菜单,也不执行什么。
50
+ button.addEventListener('click', () => { if (alive()) show(button, item, true); });
51
+ } else {
52
+ button.setAttribute('role', checked === undefined ? 'menuitem' : radio ? 'menuitemradio' : 'menuitemcheckbox');
53
+ if (checked !== undefined) button.setAttribute('aria-checked', String(checked));
54
+ button.addEventListener('click', () => {
55
+ if (!alive()) return; // 菜单已经关了(换成了别的、退回了上一层):留在手里的按钮不再响应
56
+ hide();
57
+ run();
58
+ });
59
+ }
60
+ if (hover) button.addEventListener('pointerenter', event => {
61
+ if (!alive() || event.pointerType === 'touch') return;
62
+ if (submenu) show(button, item, false); else later();
63
+ });
64
+ return button;
65
+ });
66
+ }
67
+
68
+ // 根层:路径标题、信息行,和项。
69
+ function paintRoot() {
70
+ const { title, detail, items } = spec, mine = current, mineLayer = ++layer;
71
+ const head = [];
72
+ if (title !== undefined) head.push(Object.assign(document.createElement('p'), { className: 'menu-path secondary', textContent: title }));
73
+ if (detail !== undefined) head.push(Object.assign(document.createElement('p'), { className: 'menu-info secondary caption', textContent: detail }));
74
+ el.replaceChildren(...head, ...build(items, () => current === mine && layer === mineLayer, flyout && sub !== null));
75
+ el.scrollTop = 0;
76
+ }
77
+
78
+ // 贴在锚点下面、右对齐(锚点是按钮元素或同样有 getBoundingClientRect 的假锚点),或放在点上;每次换层内容宽度变了都要重来。
79
+ function place() {
80
+ const box = anchor.getBoundingClientRect?.();
81
+ const x = box ? box.right - el.offsetWidth : anchor.clientX, y = box ? box.bottom + 4 : anchor.clientY;
82
+ el.style.left = `max(var(--safe-left), min(${x}px, calc(100vw - var(--safe-right) - ${el.offsetWidth + 4}px)))`;
83
+ el.style.top = `max(var(--safe-top), min(${y}px, calc(100vh - var(--safe-bottom) - ${el.offsetHeight + 4}px)))`;
84
+ }
85
+
86
+ // anchor:按钮元素或同样有 getBoundingClientRect 的假锚点(原生按钮:只代表根层,子菜单贴的是真实的项)或 { clientX, clientY } 的点。
87
+ // title 写在菜单顶上:iPhone 没有 title 悬停,长文件名在行里被截断,这里看全;detail 是它下面的一行小字。
88
+ function open(at, content) {
89
+ hide();
90
+ current = {};
91
+ spec = content;
92
+ anchor = at;
93
+ paintRoot();
94
+ el.hidden = false;
95
+ place();
96
+ if (at.getBoundingClientRect) { opener = at; opener.setAttribute?.('aria-expanded', 'true'); }
97
+ }
98
+
99
+ // 二级菜单:click 是点击(键盘激活也是);鼠标移上去只在放得下时弹出,放不下什么都不做,免得内容在鼠标底下突然换掉。
100
+ function show(button, item, click) {
101
+ keep();
102
+ if (flyout && sub !== null) {
103
+ if (flyButton === button) return; // 已经开着
104
+ collapse();
105
+ const mine = current, mineFly = ++fly;
106
+ const nodes = build(item.submenu.items, () => current === mine && fly === mineFly, false);
107
+ sub.replaceChildren(...nodes);
108
+ sub.hidden = false;
109
+ sub.style.visibility = 'hidden'; // 先量尺寸再决定放不放得下,量的时候别让人看到
110
+ sub.scrollTop = 0;
111
+ const { offsetWidth, offsetHeight } = sub, root = el.getBoundingClientRect(), at = button.getBoundingClientRect();
112
+ const x = root.left - offsetWidth - GAP;
113
+ if (x >= safeLeft()) {
114
+ sub.style.left = `${x}px`;
115
+ sub.style.top = `max(var(--safe-top), min(${at.top - 5}px, calc(100vh - var(--safe-bottom) - ${offsetHeight + 4}px)))`; // 第一项和「显示」对齐:减去面板的边框和内边距
116
+ sub.style.visibility = '';
117
+ flyButton = button;
118
+ button.setAttribute('aria-expanded', 'true');
119
+ if (click) nodes.find(node => node.tagName === 'BUTTON')?.focus?.(); // 键盘激活:焦点进侧面板
120
+ return;
121
+ }
122
+ collapse(); // 放不下:和 iPhone 一样切到下一层
123
+ }
124
+ if (click) enter(item);
125
+ }
126
+
127
+ // 在 el 里切到下一层:顶上一行返回;点返回回到根层,下一层的按钮此后不再响应。
128
+ function enter(item) {
129
+ const mine = current, mineLayer = ++layer, alive = () => current === mine && layer === mineLayer;
130
+ const back = document.createElement('button'), mark = document.createElement('span'), name = document.createElement('span');
131
+ mark.className = 'check back';
132
+ mark.append(icon('back', 14));
133
+ name.textContent = '返回';
134
+ back.className = 'menu-back';
135
+ back.append(mark, name);
136
+ back.setAttribute('role', 'menuitem');
137
+ back.addEventListener('click', () => {
138
+ if (!alive()) return;
139
+ paintRoot();
140
+ place();
141
+ });
142
+ el.replaceChildren(back, Object.assign(document.createElement('hr'), { className: 'menu-sep' }), ...build(item.submenu.items, alive, false));
143
+ el.scrollTop = 0;
144
+ place();
145
+ }
146
+
147
+ // 鼠标回到「显示」或侧面板:取消正在等的收起。
148
+ function keep() { clearTimeout(closeTimer); closeTimer = null; }
149
+ // 鼠标移到别的项:等一会儿再收起侧面板。
150
+ function later() {
151
+ if (flyButton === null || closeTimer !== null) return;
152
+ const mine = current;
153
+ closeTimer = setTimeout(() => { closeTimer = null; if (current === mine) collapse(); }, CLOSE_MS);
154
+ }
155
+ function collapse() {
156
+ keep();
157
+ fly++; // 侧面板里的旧按钮不再响应
158
+ if (sub) sub.hidden = true;
159
+ flyButton?.setAttribute('aria-expanded', 'false');
160
+ flyButton = null;
161
+ }
162
+ // 安全区的左边:body 的左内边距就是它(style.css),算出来的是像素。读不到按 0。
163
+ const safeLeft = () => { const px = parseFloat(globalThis.getComputedStyle?.(document.body)?.paddingLeft); return Number.isFinite(px) ? px : 0; };
164
+
165
+ function hide() {
166
+ current = null;
167
+ layer++;
168
+ collapse();
169
+ el.hidden = true;
170
+ opener?.setAttribute?.('aria-expanded', 'false');
171
+ opener = null;
172
+ }
173
+
174
+ // 两个面板都算菜单里面。菜单开着时点菜单外:只关菜单,紧接着的那次点击不交给页面。
175
+ const inside = target => el.contains(target) || (sub !== null && sub.contains(target));
176
+ let dismissed = false;
177
+ document.addEventListener('pointerdown', event => {
178
+ dismissed = !el.hidden && !inside(event.target);
179
+ if (!inside(event.target)) hide();
180
+ }, true);
181
+ document.addEventListener('click', event => {
182
+ if (dismissed) { dismissed = false; event.stopPropagation(); }
183
+ }, true);
184
+ document.addEventListener('keydown', event => { if (event.key === 'Escape') hide(); });
185
+ window.addEventListener('blur', hide);
186
+ window.addEventListener('resize', hide); // 旋转、拖窄侧栏:位置对不上了
187
+ sub?.addEventListener('pointerenter', keep);
188
+
189
+ return {
190
+ open, hide,
191
+ get hidden() { return el.hidden; },
192
+ // 谁打开的菜单:同一个按钮再次激活就关闭(键盘激活没有 pointerdown,上面那条规则管不到)。
193
+ get opener() { return opener; },
194
+ };
195
+ }