@coda-console/resource-manager 0.6.0 → 0.7.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 +8 -3
- package/package.json +1 -1
- package/view/app.mjs +68 -15
- package/view/gestures.mjs +12 -3
- package/view/grid.mjs +64 -9
- package/view/highlight.mjs +93 -0
- package/view/icons.mjs +2 -3
- package/view/index.html +1 -1
- package/view/keys.mjs +15 -0
- package/view/languages.mjs +49 -0
- package/view/lookup.mjs +8 -0
- package/view/menu.mjs +7 -1
- package/view/model.mjs +16 -3
- package/view/preview.mjs +11 -2
- package/view/selection.mjs +1 -1
- package/view/style.css +32 -3
- package/view/tree.mjs +62 -9
- package/view/vendor/highlight/LICENSE +29 -0
- package/view/vendor/highlight/highlight.min.mjs +1260 -0
- package/view/vendor/highlight/languages/dart.min.mjs +27 -0
- package/view/vendor/highlight/languages/dockerfile.min.mjs +8 -0
- package/view/vendor/highlight/languages/scala.min.mjs +28 -0
package/view/model.mjs
CHANGED
|
@@ -42,7 +42,8 @@ export class Model {
|
|
|
42
42
|
dirs = new Map(); // path → { entries, next, loaded, loading, error, token }
|
|
43
43
|
expanded = new Set(); // 不变量:展开的目录,它的每一级上层也展开
|
|
44
44
|
wanted = new Map(); // 恢复状态时各目录要取够的条数,第一次取用后删掉
|
|
45
|
-
selected = null;
|
|
45
|
+
selected = null; // 选中的条目(高亮和键盘移动用,文件夹、不能打开的条目都可以)
|
|
46
|
+
opened = null; // Mac 侧栏预览中的文件;iPhone 一直是 null
|
|
46
47
|
showHidden = false;
|
|
47
48
|
sort = DEFAULT_SORT; // 全局一份,由页面存进 persisted
|
|
48
49
|
view = View.LIST; // 全局一份,由页面存进 persisted
|
|
@@ -88,6 +89,7 @@ export class Model {
|
|
|
88
89
|
const paths = (saved?.expanded ?? []).filter(path => path !== '');
|
|
89
90
|
this.expanded = new Set(paths.filter(path => !path.includes('/') || paths.includes(path.slice(0, path.lastIndexOf('/')))));
|
|
90
91
|
this.selected = saved?.selected ?? null;
|
|
92
|
+
this.opened = saved?.opened ?? null;
|
|
91
93
|
this.onChange();
|
|
92
94
|
}
|
|
93
95
|
|
|
@@ -110,6 +112,7 @@ export class Model {
|
|
|
110
112
|
this.scope++;
|
|
111
113
|
this.#nav++;
|
|
112
114
|
this.cwd = '';
|
|
115
|
+
this.selected = this.opened = null;
|
|
113
116
|
this.#quiet = false;
|
|
114
117
|
this.wanted = new Map();
|
|
115
118
|
}
|
|
@@ -279,7 +282,15 @@ export class Model {
|
|
|
279
282
|
}
|
|
280
283
|
}
|
|
281
284
|
|
|
282
|
-
|
|
285
|
+
// 选中只影响高亮,不发 onChange:重画会换掉全部行节点,双击的第二下点击之后 dblclick 就丢了。页面的 selectEntry 负责切高亮和保存。返回是不是变了。
|
|
286
|
+
select(path) {
|
|
287
|
+
if (this.selected === path) return false;
|
|
288
|
+
this.selected = path;
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
// Mac 打开文件:选中它并切到预览。返回(closeFile)只清 opened,选中留着。
|
|
292
|
+
openFile(path) { this.selected = this.opened = path; this.onChange(); }
|
|
293
|
+
closeFile() { this.opened = null; this.onChange(); }
|
|
283
294
|
setShowHidden(on) { this.showHidden = on; this.onChange(); }
|
|
284
295
|
// loaded:取过不止一页的目录已加载的条数,只存条数不存内容;第一页每次都会重新取,不用存。
|
|
285
296
|
snapshot() {
|
|
@@ -287,7 +298,7 @@ export class Model {
|
|
|
287
298
|
for (const [path, dir] of this.dirs) if (dir.pages > 1 && dir.entries.length > 0) loaded[path] = dir.entries.length;
|
|
288
299
|
// 还没收到这个会话就绪的 root 时,cwd 和根目录还是保存的那一份,原样存回去,不能被重建途中的一次保存冲掉。
|
|
289
300
|
const root = this.#resolved ? this.#rootPath : this.#savedRoot;
|
|
290
|
-
return { expanded: [...this.expanded], selected: this.selected, loaded, cwd: this.cwd, ...(root === null ? {} : { root }) };
|
|
301
|
+
return { expanded: [...this.expanded], selected: this.selected, opened: this.opened, loaded, cwd: this.cwd, ...(root === null ? {} : { root }) };
|
|
291
302
|
}
|
|
292
303
|
}
|
|
293
304
|
|
|
@@ -317,6 +328,8 @@ export function parseState(raw) {
|
|
|
317
328
|
state.sessions[id] = {
|
|
318
329
|
at: Number.isSafeInteger(item.at) ? item.at : 0, expanded: strings(item.expanded, 5000),
|
|
319
330
|
selected: typeof item.selected === 'string' && item.selected.length <= 1024 ? item.selected : null,
|
|
331
|
+
// 0.6.0 的状态没有 opened:selected 只当选中,不打开预览。
|
|
332
|
+
opened: typeof item.opened === 'string' && item.opened.length <= 1024 ? item.opened : null,
|
|
320
333
|
// 滚动位置是按当时的排序存的;旧版本没有这个字段,那时一定是默认排序。
|
|
321
334
|
// 两种视图各存各的位置和当时的排序:tree 和 sort 是列表的,grid 和 gridSort 是图标的(旧版本没有后两个)。
|
|
322
335
|
scroll: { tree: num(scroll.tree), top: num(scroll.top), left: num(scroll.left), sort: parseSort(scroll.sort) ?? DEFAULT_SORT,
|
package/view/preview.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// 文件预览:只接收路径和读块的能力,不依赖树的状态。readFile 是纯逻辑;mountPreview 才碰 DOM。
|
|
2
2
|
import { h } from './dom.mjs';
|
|
3
3
|
import { formatSize } from './format.mjs';
|
|
4
|
+
import { HIGHLIGHT_LIMIT, appendAll, hasLongLine, highlight } from './highlight.mjs';
|
|
4
5
|
import { icon, iconFor } from './icons.mjs';
|
|
6
|
+
import { languageOf } from './languages.mjs';
|
|
5
7
|
import { ContentKind, EntryKind, ErrorCode, IMAGE_LIMIT, TEXT_LIMIT, imageType } from './protocol.mjs';
|
|
6
8
|
import { clippedSelection, linesOf } from './selection.mjs';
|
|
7
9
|
|
|
@@ -88,13 +90,20 @@ export function mountPreview(el, { read, copy, actions = [], copyInHead = true,
|
|
|
88
90
|
revoke();
|
|
89
91
|
shown = { size: result.size, mtimeMs: result.mtimeMs };
|
|
90
92
|
// 元信息:目录 · 行数 · 大小;根目录下的文件没有目录,非文本没有行数。修改时间放进 title。
|
|
93
|
+
// 高亮:认得出语言才试;超过上限不试,库或解析出错就退回纯文本,都在元信息里注明。认不出语言的不加说明。
|
|
91
94
|
const dir = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '';
|
|
92
95
|
const lines = result.kind === ContentKind.TEXT ? `${lineCount(result.text)} 行` : '';
|
|
93
|
-
const
|
|
96
|
+
const language = result.kind === ContentKind.TEXT ? languageOf(path.split('/').pop()) : null;
|
|
97
|
+
// 先判大小,超过只写「不高亮」;没超再判行长。
|
|
98
|
+
const skip = !language ? '' : result.bytes.length > HIGHLIGHT_LIMIT ? '不高亮' : hasLongLine(result.text) ? '行太长,不高亮' : '';
|
|
99
|
+
const colored = language && !skip ? highlight(result.text, language) : null;
|
|
100
|
+
const plain = !language ? '' : skip || (colored ? '' : '没能高亮');
|
|
101
|
+
const note = [result.truncated ? `只显示前 ${formatSize(TEXT_LIMIT)}` : '', plain].filter(Boolean).join(',');
|
|
94
102
|
setMeta([dir, lines, formatSize(result.size), note].filter(Boolean).join(' · '), `修改于 ${formatTime(result.mtimeMs)}`);
|
|
95
103
|
if (result.kind === ContentKind.TEXT) {
|
|
96
104
|
const gutter = h('pre', { class: 'gutter', 'aria-hidden': 'true' }, Array.from({ length: lineCount(result.text) }, (_, i) => i + 1).join('\n'));
|
|
97
|
-
const text = h('pre', { class: 'text selectable' }, result.text);
|
|
105
|
+
const text = h('pre', { class: 'text selectable' }, ...(colored ? [] : [result.text]));
|
|
106
|
+
if (colored) appendAll(text, colored);
|
|
98
107
|
if (selectionMenu) text.addEventListener('contextmenu', event => onSelect(event, text));
|
|
99
108
|
body.replaceChildren(h('div', { class: 'code' }, gutter, text));
|
|
100
109
|
} else if (result.kind === ContentKind.IMAGE && result.bytes.length > 0) {
|
package/view/selection.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// 预览里选中的文字:裁到正文元素里面,算出覆盖的行。DOM 的部分只用 Range 的标准方法,行号是纯函数。
|
|
2
2
|
const START_TO_START = 0, END_TO_END = 2; // Range.START_TO_START、Range.END_TO_END
|
|
3
3
|
|
|
4
|
-
// el 是正文(一个 pre
|
|
4
|
+
// el 是正文(一个 pre,里面是文本节点,上色后还有嵌套的 span)。选区为空或和 el 不相交返回 null;
|
|
5
5
|
// 否则把选区裁到 el 里,返回 { text, start }:裁剪后的文字和它在 el 文字里的起点(UTF-16 偏移)。裁剪后为空也返回 null。
|
|
6
6
|
// 选区的起点或终点落在 el 外面(跨出正文、从行号列开始拖)时,裁掉外面的部分。
|
|
7
7
|
export function clippedSelection(el, selection = globalThis.getSelection?.()) {
|
package/view/style.css
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* 资源管理器的页面,颜色、字体、字号只用 App 给的 --coda-* 变量(插件视图的主题变量)。
|
|
2
|
-
|
|
2
|
+
两个例外是下面的 --tone-*(种类图标的色调)和 --hl-*(代码高亮),主题变量里没有这两组。 */
|
|
3
3
|
:root {
|
|
4
4
|
--hover: var(--coda-hover, var(--coda-fill, rgba(0,0,0,.05)));
|
|
5
5
|
--line: var(--coda-separator, rgba(0,0,0,.1));
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
--safe-bottom: env(safe-area-inset-bottom, 0px); --safe-left: env(safe-area-inset-left, 0px);
|
|
11
11
|
font: var(--coda-font-size, 13px)/1.45 var(--coda-font, -apple-system, system-ui, sans-serif);
|
|
12
12
|
color: var(--coda-foreground, #1d1d1f); background: transparent;
|
|
13
|
+
/* iOS WebKit 会自动放大文字,上色后正文是许多 span,被放大而行号列不放大,两列对不齐;整页关掉,字号只按 --coda-font-size。 */
|
|
14
|
+
-webkit-text-size-adjust: 100%; text-size-adjust: 100%;
|
|
13
15
|
}
|
|
14
16
|
/* 种类图标的色调。图标是 currentColor,.tone-* 把颜色给到 svg 上;文件夹用强调色,链接和特殊文件没有色调,跟随三级文字色。
|
|
15
17
|
两种外观下,在 --coda-background 和 --coda-card 上的对比度都不低于 3:1(harness 读真实计算值检查);改色值要重新跑。 */
|
|
@@ -21,6 +23,15 @@
|
|
|
21
23
|
--tone-gray: #a3a3a9; --tone-blue: #6aa9ec; --tone-yellow: #e3b63e; --tone-green: #63c77f; --tone-teal: #58c3dd;
|
|
22
24
|
--tone-orange: #f0964f; --tone-red: #f0717a; --tone-purple: #ac87ec; --tone-pink: #ee77ac;
|
|
23
25
|
}
|
|
26
|
+
/* 代码高亮的颜色,深浅各一套;自己定的,不照搬任何现成主题。每个 token 的最终颜色在代码区底色上不低于 4.5:1(harness 读真实计算值检查);改色值要重新跑。 */
|
|
27
|
+
:root {
|
|
28
|
+
--hl-keyword: #a2218f; --hl-string: #b82a1f; --hl-comment: #5f6b76; --hl-number: #2d3fc4; --hl-title: #0b62a0; --hl-type: #1b7280; --hl-attr: #7a5200;
|
|
29
|
+
--hl-meta: #8a4710; --hl-tag: #a02a5c; --hl-variable: #5a45b8; --hl-regexp: #9a3b00; --hl-addition: #1a7a2c; --hl-deletion: #c0262d; --hl-section: #0b5ca8;
|
|
30
|
+
}
|
|
31
|
+
:root.coda-dark {
|
|
32
|
+
--hl-keyword: #f58ac0; --hl-string: #f59a85; --hl-comment: #8f9ca8; --hl-number: #dcc46c; --hl-title: #62c0e8; --hl-type: #72d4d0; --hl-attr: #d8b46a;
|
|
33
|
+
--hl-meta: #f0a070; --hl-tag: #f58aa8; --hl-variable: #b3a4f5; --hl-regexp: #e6b27a; --hl-addition: #6fd185; --hl-deletion: #ff8a8f; --hl-section: #7cb8f5;
|
|
34
|
+
}
|
|
24
35
|
.tone-accent { color: var(--coda-accent, #0a84ff); }
|
|
25
36
|
.tone-gray { color: var(--tone-gray); } .tone-blue { color: var(--tone-blue); } .tone-yellow { color: var(--tone-yellow); }
|
|
26
37
|
.tone-green { color: var(--tone-green); } .tone-teal { color: var(--tone-teal); } .tone-orange { color: var(--tone-orange); }
|
|
@@ -107,10 +118,28 @@ button.icon svg { display: block; }
|
|
|
107
118
|
.gutter { position: sticky; left: 0; flex: none; padding: 6px 8px 6px 12px; text-align: right; background: var(--coda-background, #fff);
|
|
108
119
|
color: var(--coda-tertiary, #a1a1a6); -webkit-user-select: none; user-select: none; }
|
|
109
120
|
.text { flex: 1 0 auto; padding: 6px 12px 6px 4px; tab-size: 4; }
|
|
121
|
+
/* 高亮只设 color:不改字重、斜体、背景、字号,行高和行号对齐不变。highlight.js 的其他类不上色。 */
|
|
122
|
+
.code .text .hljs-keyword { color: var(--hl-keyword); }
|
|
123
|
+
.code .text .hljs-string { color: var(--hl-string); }
|
|
124
|
+
.code .text .hljs-comment { color: var(--hl-comment); }
|
|
125
|
+
.code .text .hljs-number, .code .text .hljs-literal { color: var(--hl-number); }
|
|
126
|
+
.code .text .hljs-title { color: var(--hl-title); }
|
|
127
|
+
.code .text .hljs-type, .code .text .hljs-built_in { color: var(--hl-type); }
|
|
128
|
+
.code .text .hljs-attr, .code .text .hljs-property, .code .text .hljs-attribute { color: var(--hl-attr); }
|
|
129
|
+
.code .text .hljs-meta { color: var(--hl-meta); }
|
|
130
|
+
.code .text .hljs-tag, .code .text .hljs-name, .code .text .hljs-selector-tag, .code .text .hljs-selector-class, .code .text .hljs-selector-id, .code .text .hljs-selector-pseudo, .code .text .hljs-selector-attr { color: var(--hl-tag); }
|
|
131
|
+
.code .text .hljs-variable { color: var(--hl-variable); }
|
|
132
|
+
.code .text .hljs-regexp { color: var(--hl-regexp); }
|
|
133
|
+
.code .text .hljs-addition { color: var(--hl-addition); }
|
|
134
|
+
.code .text .hljs-deletion { color: var(--hl-deletion); }
|
|
135
|
+
.code .text .hljs-section { color: var(--hl-section); }
|
|
110
136
|
.image { display: grid; place-items: start center; padding: 12px; }
|
|
111
137
|
.image img { max-width: 100%; height: auto; border-radius: var(--coda-radius, 6px); background: var(--coda-secondary-background, transparent); }
|
|
112
138
|
|
|
113
|
-
#notice:focus { outline: none; }
|
|
139
|
+
#notice:focus, #preview:focus { outline: none; }
|
|
140
|
+
/* 键盘焦点:选中的高亮已经表示位置,点击时不画外框;只在键盘操作(:focus-visible)时给容器内边一圈细线。 */
|
|
141
|
+
#tree:focus, .gbody:focus { outline: none; }
|
|
142
|
+
#tree:focus-visible, .gbody:focus-visible { outline: 1px solid var(--coda-accent, #0a84ff); outline-offset: -2px; }
|
|
114
143
|
.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);
|
|
115
144
|
background: var(--coda-card, var(--coda-background, #fff));
|
|
116
145
|
/* 高度有上限:内容多(长路径加五项)时在菜单里滚动,不超出安全区;位置由 menu.mjs 夹在安全区内。 */
|
|
@@ -127,7 +156,7 @@ button.icon svg { display: block; }
|
|
|
127
156
|
.menu-path { padding: 4px 8px 6px; overflow-wrap: anywhere; }
|
|
128
157
|
.menu-info { padding: 0 8px 6px; margin-top: -4px; white-space: nowrap; }
|
|
129
158
|
.menu-sep { margin: 4px 6px; border: 0; border-top: 1px solid var(--line); }
|
|
130
|
-
.menu-head { padding: 4px 8px 2px; font-weight: 600; }
|
|
159
|
+
.menu-head { display: flex; align-items: center; gap: 4px; padding: 4px 8px 2px; font-weight: 600; } /* 和 .menu button 同样的 flex 与 gap,空位用 .check */
|
|
131
160
|
.pmeta.warn { color: var(--coda-orange, #b25000); }
|
|
132
161
|
|
|
133
162
|
/* 底部的短提示:普通的是前景色(成功),.warn 是橙色(失败)。 */
|
package/view/tree.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { EntryKind, SortKey } from './protocol.mjs';
|
|
|
3
3
|
import { dateText, infoText, sizeText, subText } from './format.mjs';
|
|
4
4
|
import { mountGestures } from './gestures.mjs';
|
|
5
5
|
import { icon, iconFor } from './icons.mjs';
|
|
6
|
-
import { isHidden, joinPath } from './model.mjs';
|
|
6
|
+
import { isHidden, joinPath, parentOf } from './model.mjs';
|
|
7
7
|
|
|
8
8
|
const OVERSCAN = 8;
|
|
9
9
|
|
|
@@ -35,21 +35,44 @@ export function visibleRows(model) {
|
|
|
35
35
|
return rows;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
// 键盘在列表里的一步(纯逻辑):action 是 up、down、left、right。返回 { select: 序号 }(选中那一行)、{ toggle: 序号 }(展开或收起那一行)或 null。
|
|
39
|
+
// 选中的条目不在 rows 里(被删、被藏)按没有选中算:↓ 选第一个条目,↑ 选最后一个;说明行和「再显示」跳过。
|
|
40
|
+
export function stepRows(rows, selected, action) {
|
|
41
|
+
const entries = [];
|
|
42
|
+
rows.forEach((row, i) => { if (row.type === 'entry') entries.push(i); });
|
|
43
|
+
if (entries.length === 0) return null;
|
|
44
|
+
const at = selected === null ? -1 : rows.findIndex(row => row.type === 'entry' && row.path === selected);
|
|
45
|
+
if (action === 'up' || action === 'down') {
|
|
46
|
+
if (at < 0) return { select: action === 'down' ? entries[0] : entries[entries.length - 1] };
|
|
47
|
+
const next = entries[entries.indexOf(at) + (action === 'down' ? 1 : -1)];
|
|
48
|
+
return next === undefined ? null : { select: next };
|
|
49
|
+
}
|
|
50
|
+
if (at < 0) return null;
|
|
51
|
+
const row = rows[at], folder = row.kind === EntryKind.DIR;
|
|
52
|
+
if (action === 'right') return folder && !row.open ? { toggle: at } : null;
|
|
53
|
+
if (folder && row.open) return { toggle: at }; // ←:展开的文件夹先收起
|
|
54
|
+
const parent = parentOf(row.path); // 已收起或是文件:选中它所在的文件夹(它一定在 rows 里,不管有没有画出来)
|
|
55
|
+
const up = parent === '' ? -1 : rows.findIndex(item => item.type === 'entry' && item.path === parent);
|
|
56
|
+
return up < 0 ? null : { select: up };
|
|
57
|
+
}
|
|
58
|
+
|
|
38
59
|
// 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
|
|
39
60
|
export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
|
|
40
61
|
|
|
41
62
|
export const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
|
|
42
63
|
|
|
43
|
-
// handlers: { open(path), toggle(path), more(path), retry(path), menu(event, row) }
|
|
64
|
+
// handlers: { open(path), toggle(path), more(path), retry(path), menu(event, row), select(path | null) };select 是 Mac 单击选中,不给就是 iPhone 的轻点打开
|
|
44
65
|
// menu 收到整行(路径、种类、大小、修改时间)。
|
|
45
66
|
// draggable:触屏上拖行没有用处,还会抢滚动,关掉。now:当前时间,日期的相对格式用,测试可注入。
|
|
46
67
|
// longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
|
|
47
|
-
|
|
68
|
+
// keyboard:滚动容器可以获得焦点,键盘才接得上(Mac)。
|
|
69
|
+
export function mountTree(el, model, handlers, { draggable = true, longPress = false, keyboard = false, now = Date.now } = {}) {
|
|
48
70
|
const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
|
|
49
71
|
title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
|
|
50
72
|
title.hidden = true;
|
|
51
73
|
spacer.append(win);
|
|
52
74
|
el.replaceChildren(title, spacer);
|
|
75
|
+
if (keyboard) el.tabIndex = 0;
|
|
53
76
|
let rows = [], rowH = 24;
|
|
54
77
|
|
|
55
78
|
// 探针放在 spacer 里:量到的是行在真实结构里的高度。
|
|
@@ -133,19 +156,32 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
|
|
|
133
156
|
return node;
|
|
134
157
|
}
|
|
135
158
|
|
|
159
|
+
// 点击(iPhone)、双击、⌘↓ 和右键「打开」共用这一份:文件夹展开或收起,文件打开,说明行执行它的动作。
|
|
160
|
+
const activate = row => {
|
|
161
|
+
if (row.type === 'more') return handlers.more(row.path);
|
|
162
|
+
if (row.type === 'status') return row.retry ? handlers.retry(row.path) : undefined;
|
|
163
|
+
if (row.kind === EntryKind.DIR) handlers.toggle(row.path);
|
|
164
|
+
else if (row.kind === EntryKind.FILE) handlers.open(row.path);
|
|
165
|
+
};
|
|
136
166
|
const gestures = mountGestures(el, {
|
|
137
167
|
hit: event => rows[event.target.closest?.('.row')?.dataset.i],
|
|
138
168
|
pressable: row => row.type === 'entry',
|
|
139
|
-
activate
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (row.kind === EntryKind.DIR) handlers.toggle(row.path);
|
|
143
|
-
else if (row.kind === EntryKind.FILE) handlers.open(row.path);
|
|
144
|
-
},
|
|
169
|
+
activate,
|
|
170
|
+
select: handlers.select && (row => handlers.select(row ? row.path : null)),
|
|
171
|
+
direct: (event, row) => row.kind === EntryKind.DIR && !!event.target.closest?.('.chev'), // 展开箭头:展开或收起,选中不变
|
|
145
172
|
menu: (event, row) => handlers.menu(event, row),
|
|
146
173
|
dragText: row => row.path,
|
|
147
174
|
longPress,
|
|
148
175
|
});
|
|
176
|
+
// 选中变了只在现有节点上切 .sel,不重建:双击的 dblclick 要落在还在容器里的节点上。
|
|
177
|
+
const mark = () => { for (const node of win.children) node.classList.toggle('sel', rows[node.dataset.i]?.path === model.selected && rows[node.dataset.i]?.type === 'entry'); };
|
|
178
|
+
// 滚到这一行能看见,只滚最少的距离;按行号算位置,不找节点(没画出来的行也行)。
|
|
179
|
+
const reveal = index => {
|
|
180
|
+
const top = (spacer.offsetTop || 0) + index * rowH;
|
|
181
|
+
if (top < el.scrollTop) el.scrollTop = top;
|
|
182
|
+
else if (top + rowH > el.scrollTop + el.clientHeight) el.scrollTop = top + rowH - el.clientHeight;
|
|
183
|
+
paint();
|
|
184
|
+
};
|
|
149
185
|
el.addEventListener('scroll', paint, { passive: true });
|
|
150
186
|
new ResizeObserver(paint).observe(el);
|
|
151
187
|
|
|
@@ -154,6 +190,23 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
|
|
|
154
190
|
remeasure() { measure(); this.render(); },
|
|
155
191
|
scroll: () => el.scrollTop,
|
|
156
192
|
scrollTo(top) { el.scrollTop = top; },
|
|
193
|
+
select: mark,
|
|
194
|
+
activate,
|
|
195
|
+
focus() { el.focus(); },
|
|
196
|
+
onKey(fn) { el.addEventListener('keydown', fn); },
|
|
197
|
+
// 键盘一步:选中移动并滚到能看见,→ ← 展开收起。
|
|
198
|
+
key(action) {
|
|
199
|
+
const step = stepRows(rows, model.selected, action);
|
|
200
|
+
if (!step) return;
|
|
201
|
+
if (step.toggle !== undefined) return handlers.toggle(rows[step.toggle].path);
|
|
202
|
+
handlers.select(rows[step.select].path);
|
|
203
|
+
reveal(step.select);
|
|
204
|
+
},
|
|
205
|
+
// ⌘↓:和双击同一个动作;没有选中或选中的不在了什么都不做。
|
|
206
|
+
openSelected() {
|
|
207
|
+
const row = model.selected === null ? undefined : rows.find(item => item.type === 'entry' && item.path === model.selected);
|
|
208
|
+
if (row) activate(row);
|
|
209
|
+
},
|
|
157
210
|
// 切会话时调用:长按的等待和吞点击的标记都属于上一个会话。
|
|
158
211
|
reset() { gestures.reset(); },
|
|
159
212
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2006, Ivan Sagalaev.
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|