@coda-console/resource-manager 0.5.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 +16 -9
- package/lib/paths.mjs +5 -11
- package/package.json +1 -1
- package/view/app.mjs +223 -59
- package/view/chat.mjs +7 -3
- package/view/gestures.mjs +71 -0
- package/view/grid.mjs +268 -0
- package/view/highlight.mjs +93 -0
- package/view/icons.mjs +2 -3
- package/view/index.html +3 -1
- package/view/keys.mjs +15 -0
- package/view/languages.mjs +49 -0
- package/view/lookup.mjs +8 -0
- package/view/menu.mjs +152 -27
- package/view/model.mjs +135 -16
- package/view/preview.mjs +11 -2
- package/view/protocol.mjs +14 -0
- package/view/restore.mjs +57 -0
- package/view/selection.mjs +1 -1
- package/view/style.css +77 -19
- package/view/tree.mjs +67 -48
- 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/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); }
|
|
@@ -40,7 +51,7 @@ p { margin: 0; }
|
|
|
40
51
|
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
41
52
|
[hidden] { display: none !important; }
|
|
42
53
|
|
|
43
|
-
/*
|
|
54
|
+
/* 面板(目录树、图标、预览、说明、属性页)叠在同一处,只切可见性:隐藏的树保留滚动位置,切换不改变布局。 */
|
|
44
55
|
#stage { position: relative; flex: 1 1 auto; min-height: 0; }
|
|
45
56
|
.pane { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
|
|
46
57
|
.pane.off { visibility: hidden; pointer-events: none; }
|
|
@@ -94,35 +105,58 @@ p { margin: 0; }
|
|
|
94
105
|
|
|
95
106
|
/* 预览:头部永远占一行名字加一行说明,加载和失败只换下面的内容。 */
|
|
96
107
|
.preview { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归文件页的工具条 */
|
|
97
|
-
.phead button { flex: none; white-space: nowrap; }
|
|
98
|
-
.phead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
|
|
99
|
-
.pico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
|
|
100
|
-
.pico svg { display: block; }
|
|
108
|
+
.phead button, .ghead button { flex: none; white-space: nowrap; }
|
|
109
|
+
.phead, .ghead { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
|
|
110
|
+
.pico, .ghico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
|
|
111
|
+
.pico svg, .ghico svg { display: block; }
|
|
101
112
|
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); }
|
|
102
113
|
button.icon svg { display: block; }
|
|
103
|
-
.pname { font-weight: 600; }
|
|
114
|
+
.pname, .gname-head { font-weight: 600; }
|
|
104
115
|
.pbody { flex: 1 1 auto; min-height: 0; overflow: auto; }
|
|
105
116
|
.code { display: flex; width: max-content; min-width: 100%; font: .9em/1.55 var(--coda-mono, ui-monospace, Menlo, monospace); }
|
|
106
117
|
.code pre { margin: 0; font: inherit; }
|
|
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 夹在安全区内。 */
|
|
117
146
|
max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 8px); overflow-y: auto; overscroll-behavior: contain; }
|
|
118
|
-
.menu button { display: flex; align-items: center; gap:
|
|
119
|
-
.check { flex: none; display: grid; place-items: center; width:
|
|
147
|
+
.menu button { display: flex; align-items: center; gap: 4px; width: 100%; padding: 5px 8px; border-radius: var(--coda-radius, 6px); }
|
|
148
|
+
.check { flex: none; display: grid; place-items: center; width: 14px; color: var(--coda-accent, #0a84ff); }
|
|
120
149
|
.check svg { display: block; }
|
|
150
|
+
.check.back { color: var(--coda-secondary, #6e6e73); }
|
|
151
|
+
/* 带二级菜单的项:右边的箭头;展开着的和悬停同色。 */
|
|
152
|
+
.sub-arrow { flex: none; display: grid; place-items: center; margin-left: auto; padding-left: 12px; color: var(--coda-secondary, #6e6e73); }
|
|
153
|
+
.sub-arrow svg { display: block; }
|
|
154
|
+
.menu button[aria-expanded="true"] { background: var(--coda-selection, var(--hover)); }
|
|
121
155
|
/* 菜单宽度随内容,上限见 .menu:路径超过上限折行,大小 · 完整修改时间不折。 */
|
|
122
|
-
.menu-path { padding: 4px
|
|
123
|
-
.menu-info { padding: 0
|
|
156
|
+
.menu-path { padding: 4px 8px 6px; overflow-wrap: anywhere; }
|
|
157
|
+
.menu-info { padding: 0 8px 6px; margin-top: -4px; white-space: nowrap; }
|
|
124
158
|
.menu-sep { margin: 4px 6px; border: 0; border-top: 1px solid var(--line); }
|
|
125
|
-
.menu-head { padding: 4px
|
|
159
|
+
.menu-head { display: flex; align-items: center; gap: 4px; padding: 4px 8px 2px; font-weight: 600; } /* 和 .menu button 同样的 flex 与 gap,空位用 .check */
|
|
126
160
|
.pmeta.warn { color: var(--coda-orange, #b25000); }
|
|
127
161
|
|
|
128
162
|
/* 底部的短提示:普通的是前景色(成功),.warn 是橙色(失败)。 */
|
|
@@ -140,11 +174,34 @@ button.icon svg { display: block; }
|
|
|
140
174
|
.coda-ios .irow { flex-direction: column; gap: 0; padding: 8px 4px; }
|
|
141
175
|
.coda-ios .ilabel { width: auto; text-align: left; font-size: .85em; }
|
|
142
176
|
|
|
177
|
+
/* 图标视图:头部同预览(样式和 .phead 一样,类名另取,页面和 harness 里按 .phead 查预览头部的不会撞上),下面一个滚动的 .gbody,只画可见的一段(grid.mjs 量 .cell 的宽和高)。
|
|
178
|
+
单元格宽度固定,Mac 约 72 点、iPhone 约 88 点;列数按容器宽度算,每列的宽度平分余下的空间,单元格在列里居中。说明项占满一整行。 */
|
|
179
|
+
:root { --cell-w: 72px; --gico: 32px; --gname-h: calc(var(--coda-font-size, 13px) * 2.4); --note-h: calc(var(--coda-font-size, 13px) * 2.4); }
|
|
180
|
+
.coda-ios { --cell-w: 88px; --gico: 40px; --note-h: 44px; }
|
|
181
|
+
.grid { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; } /* 底部的安全区归 .gbody */
|
|
182
|
+
.gbody { flex: 1 1 auto; min-height: 0; overflow: auto; overscroll-behavior: contain; padding-bottom: var(--safe-bottom); }
|
|
183
|
+
.gspacer { position: relative; margin: 8px 0; }
|
|
184
|
+
.gwin { position: absolute; left: 0; right: 0; top: 0; display: grid; justify-items: center; }
|
|
185
|
+
.gi { cursor: default; }
|
|
186
|
+
/* 单元格是固定高度:图标、两行名字(.gname 的行高 1.3、字号 .92em,两行 2.392 个字号,--gname-h 取 2.4)、上下各 4 点,刚好放下,只有一行名字时不多留空白。 */
|
|
187
|
+
.cell { display: flex; flex-direction: column; align-items: center; gap: 2px; width: var(--cell-w); height: calc(var(--gico) + 2px + var(--gname-h) + 8px); padding: 4px; border-radius: var(--coda-radius, 6px); }
|
|
188
|
+
.cell.sel { background: var(--coda-selection, var(--hover)); }
|
|
189
|
+
.cell .gico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
|
|
190
|
+
.cell .gico svg { display: block; width: var(--gico); height: var(--gico); }
|
|
191
|
+
.cell.dir .gico { color: var(--coda-accent, #0a84ff); }
|
|
192
|
+
.cell.dead .gname, .cell.dead .gico { color: var(--coda-tertiary, #a1a1a6); }
|
|
193
|
+
.gname { max-width: 100%; text-align: center; font-size: .92em; line-height: 1.3; overflow: hidden; overflow-wrap: anywhere; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
|
|
194
|
+
/* 说明行(空目录、读取中……)一行高,比单元格矮;grid.mjs 分别量两种高度,布局映射按两种算。 */
|
|
195
|
+
.gnote { grid-column: 1 / -1; justify-self: stretch; height: var(--note-h); line-height: var(--note-h); padding: 0 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: center; color: var(--coda-secondary, #6e6e73); }
|
|
196
|
+
.gnote.more, .gnote.retry { color: var(--coda-accent, #0a84ff); cursor: pointer; }
|
|
197
|
+
.coda-ios .cell { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单 */
|
|
198
|
+
.coda-ios .cell:active { background: var(--hover); } /* 按下:和列表的行一样 */
|
|
199
|
+
|
|
143
200
|
/* 悬停只在有鼠标时:触摸后不会留着。 */
|
|
144
201
|
@media (hover: hover) {
|
|
145
202
|
.bar button:hover, button.icon:hover { background: var(--hover); }
|
|
146
|
-
.row.entry:hover { background: var(--hover); }
|
|
147
|
-
.row.entry.sel:hover { background: var(--coda-selection, var(--hover)); }
|
|
203
|
+
.row.entry:hover, .cell:hover { background: var(--hover); }
|
|
204
|
+
.row.entry.sel:hover, .cell.sel:hover { background: var(--coda-selection, var(--hover)); }
|
|
148
205
|
.menu button:hover { background: var(--coda-selection, var(--hover)); }
|
|
149
206
|
}
|
|
150
207
|
|
|
@@ -175,11 +232,12 @@ button.icon svg { display: block; }
|
|
|
175
232
|
.coda-ios .bar button.icon { padding: 0; justify-content: center; }
|
|
176
233
|
.coda-ios .bar.bottom { padding: 0 8px var(--safe-bottom); }
|
|
177
234
|
.coda-ios .bar.bottom button { flex: 1 1 0; justify-content: center; } /* 横排单行:横屏只有 390 点高 */
|
|
178
|
-
.coda-ios .phead { padding: 4px 12px; gap: 4px; }
|
|
179
|
-
.coda-ios .phead button { padding: 0 8px; }
|
|
235
|
+
.coda-ios .phead, .coda-ios .ghead { padding: 4px 12px; gap: 4px; }
|
|
236
|
+
.coda-ios .phead button, .coda-ios .ghead button { padding: 0 8px; }
|
|
180
237
|
.coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
|
|
181
|
-
.coda-ios .menu button { padding: 0
|
|
182
|
-
.coda-ios .menu
|
|
238
|
+
.coda-ios .menu button { padding: 0 10px; }
|
|
239
|
+
.coda-ios .menu button:active { background: var(--coda-selection, var(--hover)); }
|
|
240
|
+
.coda-ios .menu-head { padding: 6px 10px 2px; }
|
|
183
241
|
|
|
184
242
|
/* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
|
|
185
243
|
.bar .exit, .bar.exit-only { display: none; } /* 只有返回的工具条(属性页):返回不显示,整条也不占高度 */
|
package/view/tree.mjs
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
// 目录树:visibleRows 把模型压成扁平行(纯逻辑);mountTree 只画可见的那一段,上万条也不卡。
|
|
2
2
|
import { EntryKind, SortKey } from './protocol.mjs';
|
|
3
3
|
import { dateText, infoText, sizeText, subText } from './format.mjs';
|
|
4
|
+
import { mountGestures } from './gestures.mjs';
|
|
4
5
|
import { icon, iconFor } from './icons.mjs';
|
|
5
|
-
import { isHidden, joinPath } from './model.mjs';
|
|
6
|
+
import { isHidden, joinPath, parentOf } from './model.mjs';
|
|
6
7
|
|
|
7
8
|
const OVERSCAN = 8;
|
|
8
|
-
// 长按:按住这么久、移动不超过这个距离才算。
|
|
9
|
-
export const HOLD_MS = 500;
|
|
10
|
-
export const HOLD_SLOP = 10;
|
|
11
9
|
|
|
12
10
|
// 行:{ type: 'entry' | 'status' | 'more', path, name, depth, kind, link, size, mtimeMs, open, busy, failed, text };size、mtimeMs 读不到时没有
|
|
13
11
|
export function visibleRows(model) {
|
|
@@ -37,21 +35,44 @@ export function visibleRows(model) {
|
|
|
37
35
|
return rows;
|
|
38
36
|
}
|
|
39
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
|
+
|
|
40
59
|
// 根目录的名字:路径的最后一段,分区标题和 iPhone 的页内工具条共用。
|
|
41
60
|
export const rootName = path => path.split('/').filter(Boolean).pop() ?? path;
|
|
42
61
|
|
|
43
|
-
const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
|
|
62
|
+
export const TITLES = { [EntryKind.LINK]: '指向工作目录外的链接,不能打开', [EntryKind.OTHER]: '不能打开的文件' };
|
|
44
63
|
|
|
45
|
-
// 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 的轻点打开
|
|
46
65
|
// menu 收到整行(路径、种类、大小、修改时间)。
|
|
47
66
|
// draggable:触屏上拖行没有用处,还会抢滚动,关掉。now:当前时间,日期的相对格式用,测试可注入。
|
|
48
67
|
// longPress:触屏上按住一行打开 handlers.menu(Mac 用右键)。不设 touch-action,滚动照旧,滚动接管时收到 pointercancel 作废。
|
|
49
|
-
|
|
68
|
+
// keyboard:滚动容器可以获得焦点,键盘才接得上(Mac)。
|
|
69
|
+
export function mountTree(el, model, handlers, { draggable = true, longPress = false, keyboard = false, now = Date.now } = {}) {
|
|
50
70
|
const title = document.createElement('div'), spacer = document.createElement('div'), win = document.createElement('div');
|
|
51
71
|
title.className = 'section truncate'; spacer.className = 'spacer'; win.className = 'win';
|
|
52
72
|
title.hidden = true;
|
|
53
73
|
spacer.append(win);
|
|
54
74
|
el.replaceChildren(title, spacer);
|
|
75
|
+
if (keyboard) el.tabIndex = 0;
|
|
55
76
|
let rows = [], rowH = 24;
|
|
56
77
|
|
|
57
78
|
// 探针放在 spacer 里:量到的是行在真实结构里的高度。
|
|
@@ -135,51 +156,32 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
|
|
|
135
156
|
return node;
|
|
136
157
|
}
|
|
137
158
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
// 长按:按下只记一个等待;移动超限、抬起、取消、滚动、第二根手指都作废。
|
|
141
|
-
// 触发后吞掉紧接着的那次点击;没有点击跟来时,下一次按下清掉,不会误吞后面真正的点击。
|
|
142
|
-
let hold = null, swallow = false;
|
|
143
|
-
const cancelHold = () => { if (hold) { clearTimeout(hold.timer); hold = null; } };
|
|
144
|
-
if (longPress) {
|
|
145
|
-
el.addEventListener('pointerdown', event => {
|
|
146
|
-
cancelHold();
|
|
147
|
-
swallow = false;
|
|
148
|
-
const row = rowOf(event);
|
|
149
|
-
if (event.isPrimary === false || row?.type !== 'entry') return;
|
|
150
|
-
const { clientX: x, clientY: y, pointerId: id } = event;
|
|
151
|
-
hold = { id, x, y, timer: setTimeout(() => {
|
|
152
|
-
hold = null;
|
|
153
|
-
swallow = true;
|
|
154
|
-
handlers.menu({ clientX: x, clientY: y }, row);
|
|
155
|
-
}, HOLD_MS) };
|
|
156
|
-
});
|
|
157
|
-
el.addEventListener('pointermove', event => {
|
|
158
|
-
if (hold && event.pointerId === hold.id && Math.hypot(event.clientX - hold.x, event.clientY - hold.y) > HOLD_SLOP) cancelHold();
|
|
159
|
-
});
|
|
160
|
-
for (const type of ['pointerup', 'pointercancel']) el.addEventListener(type, cancelHold);
|
|
161
|
-
el.addEventListener('scroll', cancelHold, { passive: true });
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
el.addEventListener('click', event => {
|
|
165
|
-
if (swallow) { swallow = false; return; }
|
|
166
|
-
const row = rowOf(event);
|
|
167
|
-
if (!row) return;
|
|
159
|
+
// 点击(iPhone)、双击、⌘↓ 和右键「打开」共用这一份:文件夹展开或收起,文件打开,说明行执行它的动作。
|
|
160
|
+
const activate = row => {
|
|
168
161
|
if (row.type === 'more') return handlers.more(row.path);
|
|
169
162
|
if (row.type === 'status') return row.retry ? handlers.retry(row.path) : undefined;
|
|
170
163
|
if (row.kind === EntryKind.DIR) handlers.toggle(row.path);
|
|
171
164
|
else if (row.kind === EntryKind.FILE) handlers.open(row.path);
|
|
165
|
+
};
|
|
166
|
+
const gestures = mountGestures(el, {
|
|
167
|
+
hit: event => rows[event.target.closest?.('.row')?.dataset.i],
|
|
168
|
+
pressable: row => row.type === 'entry',
|
|
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'), // 展开箭头:展开或收起,选中不变
|
|
172
|
+
menu: (event, row) => handlers.menu(event, row),
|
|
173
|
+
dragText: row => row.path,
|
|
174
|
+
longPress,
|
|
172
175
|
});
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
});
|
|
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
|
+
};
|
|
183
185
|
el.addEventListener('scroll', paint, { passive: true });
|
|
184
186
|
new ResizeObserver(paint).observe(el);
|
|
185
187
|
|
|
@@ -188,7 +190,24 @@ export function mountTree(el, model, handlers, { draggable = true, longPress = f
|
|
|
188
190
|
remeasure() { measure(); this.render(); },
|
|
189
191
|
scroll: () => el.scrollTop,
|
|
190
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
|
+
},
|
|
191
210
|
// 切会话时调用:长按的等待和吞点击的标记都属于上一个会话。
|
|
192
|
-
reset() {
|
|
211
|
+
reset() { gestures.reset(); },
|
|
193
212
|
};
|
|
194
213
|
}
|
|
@@ -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.
|