@coda-console/resource-manager 0.3.0 → 0.5.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 +10 -2
- package/lib/content.mjs +15 -2
- package/lib/draft.mjs +57 -0
- package/lib/info.mjs +137 -0
- package/lib/snapshots.mjs +52 -0
- package/lib/tree.mjs +237 -48
- package/main.mjs +69 -14
- package/package.json +1 -1
- package/view/app.mjs +104 -82
- package/view/chat.mjs +45 -0
- package/view/connection.mjs +50 -24
- package/view/dom.mjs +9 -0
- package/view/file-app.mjs +19 -3
- package/view/file.html +2 -0
- package/view/format.mjs +67 -0
- package/view/hint.mjs +20 -0
- package/view/icons.mjs +139 -22
- package/view/index.html +1 -0
- package/view/info.mjs +117 -0
- package/view/menu.mjs +76 -0
- package/view/model.mjs +48 -19
- package/view/preview.mjs +16 -18
- package/view/protocol.mjs +24 -5
- package/view/selection.mjs +33 -0
- package/view/style.css +60 -13
- package/view/tree.mjs +39 -14
package/main.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// 资源管理器的模块:跟踪侧栏的会话、代次和根目录,把页面的请求交给 lib/*,把变化推给页面。
|
|
2
2
|
// 协议见 docs/design/phase-1.md 第 6 节,消息格式见 view/protocol.mjs。
|
|
3
|
-
import { listDirectory } from './lib/tree.mjs';
|
|
3
|
+
import { createListing, listDirectory } from './lib/tree.mjs';
|
|
4
4
|
import { readChunk } from './lib/content.mjs';
|
|
5
|
+
import { addText } from './lib/draft.mjs';
|
|
6
|
+
import { dirSize, statPath } from './lib/info.mjs';
|
|
5
7
|
import { NO_ROOT, queryRoot, sameRoot } from './lib/root.mjs';
|
|
6
8
|
import {
|
|
7
|
-
ErrorCode, MESSAGE_LIMIT, REQUEST_ID_MAX, RootState, Type, fail, messageSize,
|
|
9
|
+
ErrorCode, MESSAGE_LIMIT, REQUEST_ID_MAX, ReplyOf, RootState, Type, fail, messageSize,
|
|
8
10
|
} from './view/protocol.mjs';
|
|
9
11
|
|
|
10
12
|
const POLL_MS = 2000;
|
|
@@ -17,23 +19,32 @@ const sessionOfFile = context => {
|
|
|
17
19
|
return typeof session === 'string' && session !== '' ? session : null;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
// options 只给测试用,运行时不传:makeListing 换成带假文件系统的读取状态;sizeLimits 调文件夹大小的项数、用时上限。
|
|
23
|
+
export function activate(coda, { makeListing = createListing, sizeLimits = {} } = {}) {
|
|
24
|
+
const options = { makeListing, sizeLimits };
|
|
25
|
+
coda.views.register('files', { open: view => serve(coda, view, sessionOfTree, options) });
|
|
26
|
+
coda.views.register('file', { open: view => serve(coda, view, sessionOfFile, options) });
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
const validRequestId = id => (typeof id === 'string' && id.length <= REQUEST_ID_MAX) || Number.isSafeInteger(id);
|
|
26
30
|
const errorOf = error => (error?.protocol ? { code: error.code, message: error.message } : { code: ErrorCode.IO, message: '读取失败' });
|
|
27
31
|
|
|
28
|
-
function serve(coda, view, sessionOf) {
|
|
32
|
+
function serve(coda, view, sessionOf, { makeListing, sizeLimits }) {
|
|
29
33
|
// 代次:每换一次会话加 1,所有异步出口回来后都要核对。
|
|
30
34
|
const s = {
|
|
31
35
|
session: sessionOf(view.context), generation: 1, root: NO_ROOT, sent: null,
|
|
32
36
|
hello: false, closed: false, rootSeq: 0, poll: null,
|
|
37
|
+
listing: makeListing(), // 这个视图共用的并发上限和快照;视图关闭、换会话、根目录变化时丢弃
|
|
33
38
|
};
|
|
34
39
|
const turns = new Map();
|
|
35
40
|
const stale = generation => s.closed || generation !== s.generation;
|
|
36
41
|
|
|
42
|
+
// 文件夹大小:每个视图同时只有一次统计。新的开始、换会话、根目录变化、视图关闭时旧的静默停止(不回答)。
|
|
43
|
+
let sizing = null; // { generation, requestId, abort }
|
|
44
|
+
const stopSizing = () => { sizing?.abort.abort(); sizing = null; };
|
|
45
|
+
// 换会话、根目录变化、视图关闭:读取状态丢弃,统计也停。
|
|
46
|
+
const resetReads = () => { s.listing.clear(); stopSizing(); };
|
|
47
|
+
|
|
37
48
|
function post(message) {
|
|
38
49
|
if (s.closed) return;
|
|
39
50
|
if (messageSize(message) > MESSAGE_LIMIT) {
|
|
@@ -58,7 +69,10 @@ function serve(coda, view, sessionOf) {
|
|
|
58
69
|
let next = null;
|
|
59
70
|
try { next = await queryRoot(coda, s.session); } catch { /* 保留上一次 */ }
|
|
60
71
|
if (stale(generation) || seq !== s.rootSeq) return;
|
|
61
|
-
if (next)
|
|
72
|
+
if (next) {
|
|
73
|
+
if (next.path !== s.root.path) resetReads();
|
|
74
|
+
s.root = next;
|
|
75
|
+
}
|
|
62
76
|
syncPoll();
|
|
63
77
|
if (s.hello && !sameRoot(s.sent, s.root)) postRoot();
|
|
64
78
|
}
|
|
@@ -81,34 +95,68 @@ function serve(coda, view, sessionOf) {
|
|
|
81
95
|
stopPoll();
|
|
82
96
|
s.session = session ?? null;
|
|
83
97
|
s.generation++;
|
|
98
|
+
resetReads();
|
|
84
99
|
s.root = NO_ROOT;
|
|
85
100
|
s.sent = null;
|
|
86
101
|
if (s.hello) postSession();
|
|
87
102
|
refreshRoot();
|
|
88
103
|
}
|
|
89
104
|
|
|
105
|
+
// 各种请求的处理:返回回答的内容;返回 null 表示静默结束,不回答。
|
|
106
|
+
const handlers = {
|
|
107
|
+
[Type.LIST]: (message, { root, valid }) => listDirectory(root, message.path, message.cursor, { listing: s.listing, sort: message.sort, valid }),
|
|
108
|
+
[Type.READ]: (message, { root }) => readChunk(root, message.path, message.offset),
|
|
109
|
+
[Type.STAT]: (message, { root }) => statPath(s.listing, root, message.path),
|
|
110
|
+
async [Type.DIR_SIZE](message, { root, valid, generation, requestId }) {
|
|
111
|
+
stopSizing();
|
|
112
|
+
const mine = { generation, requestId, abort: new AbortController() };
|
|
113
|
+
sizing = mine;
|
|
114
|
+
try {
|
|
115
|
+
return await dirSize(s.listing, root, message.path, { ...sizeLimits, valid, signal: mine.abort.signal });
|
|
116
|
+
} finally { if (sizing === mine) sizing = null; }
|
|
117
|
+
},
|
|
118
|
+
async [Type.ADD_TEXT](message, { generation }) {
|
|
119
|
+
if (!s.session) throw fail(ErrorCode.NO_ROOT, '没有会话');
|
|
120
|
+
return await addText(coda, s.session, message, { valid: () => !stale(generation) }) ? {} : null;
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
// 回答依赖根目录的:读完回来根目录换了,不能带旧根目录的内容。
|
|
124
|
+
const rootBound = new Set([Type.LIST, Type.STAT]);
|
|
125
|
+
|
|
90
126
|
async function request(message) {
|
|
91
127
|
const { type, generation, requestId } = message;
|
|
92
128
|
if (stale(generation) || !validRequestId(requestId)) return;
|
|
93
|
-
const reply = type
|
|
129
|
+
const reply = ReplyOf[type];
|
|
94
130
|
try {
|
|
95
131
|
if (s.root.state !== RootState.READY) throw fail(ErrorCode.NO_ROOT, '没有可浏览的工作目录');
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
132
|
+
const root = s.root.path;
|
|
133
|
+
// 读的途中根目录换了(同一会话从 A 换到 B),回答不能带 A 的内容。
|
|
134
|
+
const valid = () => !stale(generation) && s.root.state === RootState.READY && s.root.path === root;
|
|
135
|
+
const body = await handlers[type](message, { root, valid, generation, requestId });
|
|
136
|
+
if (body === null || (type === Type.DIR_SIZE && !valid())) return; // 统计完成时根目录已变:不回答
|
|
137
|
+
if (rootBound.has(type) && !valid() && !stale(generation)) throw fail(ErrorCode.NO_ROOT, '工作目录已变化');
|
|
99
138
|
if (!stale(generation)) post({ type: reply, generation, requestId, ...body });
|
|
100
139
|
} catch (error) {
|
|
101
140
|
if (!stale(generation)) post({ type: reply, generation, requestId, error: errorOf(error) });
|
|
102
141
|
}
|
|
103
142
|
}
|
|
104
143
|
|
|
144
|
+
// 取消统计:代次和请求号都对上才停,迟到的取消不会误伤新的统计。
|
|
145
|
+
function cancelSizing({ generation, requestId }) {
|
|
146
|
+
if (!stale(generation) && sizing?.generation === generation && sizing.requestId === requestId) stopSizing();
|
|
147
|
+
}
|
|
148
|
+
|
|
105
149
|
view.onMessage(async message => {
|
|
106
150
|
if (!message || typeof message !== 'object') return;
|
|
107
151
|
switch (message.type) {
|
|
108
152
|
// 页面(重新)就绪:它什么都不知道,所以不管有没有变,下一次查询的结果都要推。
|
|
109
153
|
case Type.HELLO: s.hello = true; s.sent = null; postSession(); return refreshRoot();
|
|
110
|
-
case Type.REFRESH:
|
|
111
|
-
|
|
154
|
+
case Type.REFRESH:
|
|
155
|
+
if (stale(message.generation)) return undefined;
|
|
156
|
+
s.listing.detach();
|
|
157
|
+
return refreshRoot();
|
|
158
|
+
case Type.LIST: case Type.READ: case Type.STAT: case Type.DIR_SIZE: case Type.ADD_TEXT: return request(message);
|
|
159
|
+
case Type.DIR_SIZE_CANCEL: return cancelSizing(message);
|
|
112
160
|
}
|
|
113
161
|
});
|
|
114
162
|
|
|
@@ -124,11 +172,18 @@ function serve(coda, view, sessionOf) {
|
|
|
124
172
|
if (s.closed || id !== s.session || before === undefined || after === undefined || before === after) return;
|
|
125
173
|
const { generation } = s;
|
|
126
174
|
const refreshed = refreshRoot();
|
|
127
|
-
if (before)
|
|
175
|
+
if (before) {
|
|
176
|
+
refreshed.then(() => {
|
|
177
|
+
if (!s.hello || stale(generation)) return;
|
|
178
|
+
s.listing.detach(); // 页面接着重新取,进行中的旧读取不能再被共用
|
|
179
|
+
post({ type: Type.CHANGED, generation });
|
|
180
|
+
});
|
|
181
|
+
}
|
|
128
182
|
})).catch(() => null);
|
|
129
183
|
|
|
130
184
|
view.onClose(async () => {
|
|
131
185
|
s.closed = true;
|
|
186
|
+
resetReads();
|
|
132
187
|
stopPoll();
|
|
133
188
|
try { await (await watching)?.dispose(); } catch { /* 已经取消 */ }
|
|
134
189
|
});
|
package/package.json
CHANGED
package/view/app.mjs
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
// 资源管理器的页面入口:消息收发、状态保留、工具栏。状态在 model.mjs,渲染在 tree.mjs 和
|
|
1
|
+
// 资源管理器的页面入口:消息收发、状态保留、工具栏。状态在 model.mjs,渲染在 tree.mjs、preview.mjs 和 info.mjs,菜单和提示在 menu.mjs 和 hint.mjs。
|
|
2
2
|
import { EXIT, paintBar } from './bar.mjs';
|
|
3
|
+
import { createChat } from './chat.mjs';
|
|
3
4
|
import { connect } from './connection.mjs';
|
|
4
|
-
import {
|
|
5
|
+
import { infoText, sortName } from './format.mjs';
|
|
6
|
+
import { mountHint } from './hint.mjs';
|
|
7
|
+
import { mountInfo } from './info.mjs';
|
|
8
|
+
import { mountMenu } from './menu.mjs';
|
|
5
9
|
import { Model, STATE_LIMIT, noticeText, parseState, putSession, trimState } from './model.mjs';
|
|
6
10
|
import { showNotice } from './notice.mjs';
|
|
7
11
|
import { mountPreview } from './preview.mjs';
|
|
8
|
-
import { RootState } from './protocol.mjs';
|
|
12
|
+
import { EntryKind, RootState, SortKey, Type } from './protocol.mjs';
|
|
9
13
|
import { mountTree, rootName } from './tree.mjs';
|
|
10
14
|
|
|
11
15
|
const coda = window.coda;
|
|
@@ -22,28 +26,39 @@ let pending = { tree: null, preview: null };
|
|
|
22
26
|
const conn = connect(coda, { handlers: { session: onSession, root: onRoot, changed: onChanged }, onStatus: () => schedule() });
|
|
23
27
|
const model = new Model({ link: conn.link, onChange: () => schedule() });
|
|
24
28
|
model.showHidden = persisted.hidden;
|
|
29
|
+
model.sort = persisted.sort;
|
|
30
|
+
const hint = mountHint($('hint'));
|
|
31
|
+
const menu = mountMenu($('menu'));
|
|
32
|
+
const chat = createChat({ link: conn.link, copy: text => coda.copy(text), hint });
|
|
25
33
|
const tree = mountTree($('tree'), model, {
|
|
26
34
|
open: openFile,
|
|
27
35
|
toggle: path => model.toggle(path),
|
|
28
36
|
more: path => model.loadMore(path),
|
|
29
37
|
retry: path => model.retry(path),
|
|
30
|
-
menu: (event,
|
|
38
|
+
menu: (event, row) => pathMenu(event, row),
|
|
31
39
|
}, { draggable: !isIOS, longPress: isIOS });
|
|
32
40
|
const preview = mountPreview($('preview'), {
|
|
33
41
|
read: (path, offset) => conn.link.call('read', { path, offset }), copy: text => coda.copy(text),
|
|
34
42
|
actions: isIOS ? [] : [{ label: '窗口', icon: 'window', run: openInView }], copyInHead: !isIOS,
|
|
35
43
|
back: isIOS ? null : () => { backPressed = true; model.select(null); },
|
|
44
|
+
// 选中文字的右键菜单只有 Mac:iPhone 用系统的选区菜单。
|
|
45
|
+
selectionMenu: isIOS ? null : (event, selection) => menu.open(event, { items: chat.selectionItems(selection) }),
|
|
46
|
+
});
|
|
47
|
+
// 属性页:页内一层。iPhone 用原生返回(coda.back);Mac 的客户端会替声明了返回的页面画一个带小箭头的导航,
|
|
48
|
+
// 和侧栏标题重复,所以 Mac 和没有 coda.back 的环境一样,头部最左边自己放返回。
|
|
49
|
+
const nativeBack = isIOS && typeof coda.back === 'function';
|
|
50
|
+
let infoOpen = false;
|
|
51
|
+
let dataRound = 0; // 数据轮次:每收到一次 changed 加一,属性页用它判断文件夹的统计结果是不是上一轮的
|
|
52
|
+
const info = mountInfo($('info'), {
|
|
53
|
+
stat: path => conn.link.call(Type.STAT, { path }),
|
|
54
|
+
size: (path, signal) => conn.link.call(Type.DIR_SIZE, { path }, { signal, cancel: Type.DIR_SIZE_CANCEL }),
|
|
55
|
+
rootPath: () => model.root?.path ?? '',
|
|
56
|
+
round: () => dataRound,
|
|
57
|
+
back: nativeBack ? null : () => { backPressed = true; closeInfo(); },
|
|
36
58
|
});
|
|
37
59
|
|
|
38
60
|
// ---- 在独立视图里打开:iPhone 点文件,Mac 预览头部的「窗口」 ----
|
|
39
61
|
// 会话 ID 放进 args:被打开的视图 context.session 是 null。失败(含同步抛出)在原地提示。
|
|
40
|
-
let hintTimer = null;
|
|
41
|
-
function showHint(text) {
|
|
42
|
-
$('hint').textContent = text;
|
|
43
|
-
$('hint').hidden = false;
|
|
44
|
-
clearTimeout(hintTimer);
|
|
45
|
-
hintTimer = setTimeout(() => { $('hint').hidden = true; }, 5000);
|
|
46
|
-
}
|
|
47
62
|
// iPhone 防连点:open 进行中、成功后推入动画的这一小段都忽略重复点击;失败立刻放开。
|
|
48
63
|
// epoch 每换一次会话加一:旧会话的 open 结果回来时已经作废,不碰新会话的提示和防连点。
|
|
49
64
|
let opening = false, unlockTimer = null, epoch = 0;
|
|
@@ -54,12 +69,12 @@ function openInView(path) {
|
|
|
54
69
|
return Promise.resolve().then(() => coda.open({ view: 'file', args: { session: model.session, path } }))
|
|
55
70
|
.then(() => {
|
|
56
71
|
if (mine !== epoch) return;
|
|
57
|
-
|
|
72
|
+
hint.hide();
|
|
58
73
|
if (isIOS) { clearTimeout(unlockTimer); unlockTimer = setTimeout(() => { opening = false; }, REOPEN_MS); }
|
|
59
74
|
}, error => {
|
|
60
75
|
if (mine !== epoch) return;
|
|
61
76
|
opening = false;
|
|
62
|
-
|
|
77
|
+
hint.show(`打不开文件:${error?.message ?? '未知原因'}`);
|
|
63
78
|
});
|
|
64
79
|
}
|
|
65
80
|
function openFile(path) {
|
|
@@ -81,7 +96,7 @@ function schedule() {
|
|
|
81
96
|
|
|
82
97
|
// 用户点了预览头部的返回:render 据此决定焦点要不要交给目录树,别的模式变化不动焦点。
|
|
83
98
|
let backPressed = false;
|
|
84
|
-
const mode = () => (!model.ready ? 'notice' : model.selected !== null && !isIOS ? 'preview' : 'tree');
|
|
99
|
+
const mode = () => (infoOpen ? 'info' : !model.ready ? 'notice' : model.selected !== null && !isIOS ? 'preview' : 'tree');
|
|
85
100
|
|
|
86
101
|
// 说明区只在内容变了才重画,免得重试按钮在点击中途被换掉。
|
|
87
102
|
let noticeKey = null;
|
|
@@ -97,7 +112,7 @@ function render() {
|
|
|
97
112
|
const current = mode();
|
|
98
113
|
const refocus = backPressed && document.hasFocus();
|
|
99
114
|
backPressed = false;
|
|
100
|
-
for (const [id, name] of [['tree', 'tree'], ['preview', 'preview'], ['notice', 'notice']]) {
|
|
115
|
+
for (const [id, name] of [['tree', 'tree'], ['preview', 'preview'], ['notice', 'notice'], ['info', 'info']]) {
|
|
101
116
|
$(id).classList.toggle('off', current !== name);
|
|
102
117
|
$(id).inert = current !== name;
|
|
103
118
|
}
|
|
@@ -106,7 +121,7 @@ function render() {
|
|
|
106
121
|
if (current === 'preview' && preview.path !== model.selected) {
|
|
107
122
|
preview.open(model.selected, pending.preview);
|
|
108
123
|
pending.preview = null;
|
|
109
|
-
} else if (current
|
|
124
|
+
} else if ((current === 'tree' || current === 'notice') && preview.path !== null) preview.close(); // 属性页和预览一样只切可见性,不动别的面板的内容
|
|
110
125
|
// 返回按钮随预览一起 inert,焦点会掉到页面外:用户刚点了返回、页面又持有焦点时交给目录树。
|
|
111
126
|
// 后台刷新、切会话、root 变化造成的切换不动焦点,免得抢走客户端输入框的焦点。
|
|
112
127
|
if (refocus && current === 'tree') { $('tree').tabIndex = -1; $('tree').focus(); }
|
|
@@ -115,15 +130,17 @@ function render() {
|
|
|
115
130
|
|
|
116
131
|
// ---- 工具栏:Mac 用 coda.toolbar,没有的(iOS、harness)退成页内一条 ----
|
|
117
132
|
// Mac 只有一个原生按钮「更多」:原生菜单带向下的箭头,关不掉,所以按下后由页面弹菜单。返回键在预览头部,不在这里。
|
|
118
|
-
|
|
133
|
+
// 属性页上排序、刷新、显示隐藏文件都没有意义:工具栏不放任何项,退出属性页后恢复。
|
|
134
|
+
const toolbarItems = () => infoOpen ? [] : hasToolbar
|
|
119
135
|
? [{ id: 'more', label: '更多', icon: 'ellipsis', kind: 'button' }]
|
|
120
136
|
: [
|
|
121
137
|
{ id: 'refresh', label: '刷新', icon: 'arrow.clockwise', kind: 'button' },
|
|
122
138
|
{ id: 'hidden', label: '显示隐藏文件', icon: 'eye', kind: 'toggle', on: model.showHidden },
|
|
123
139
|
];
|
|
124
|
-
// iPhone
|
|
140
|
+
// iPhone 树页顶部:返回(仅横屏)、根目录名、⋯。刷新、显示隐藏文件和排序都在 ⋯ 的菜单里。
|
|
141
|
+
// 属性页上只留返回:竖屏有原生导航栏,整条工具条不显示;横屏导航栏隐藏,页面自己给返回(显示与否都由 style.css 的横屏媒体查询决定)。
|
|
125
142
|
const MORE = { id: 'more', label: '更多', kind: 'button', glyph: 'ellipsis', iconOnly: true, menu: true };
|
|
126
|
-
const phoneItems = () => [EXIT, { kind: 'title', label: model.ready ? rootName(model.root.path) : '' }, MORE];
|
|
143
|
+
const phoneItems = () => infoOpen ? [EXIT] : [EXIT, { kind: 'title', label: model.ready ? rootName(model.root.path) : '' }, MORE];
|
|
127
144
|
// 比较键就是要画的项:根目录名变了(换会话)工具条跟着重画。
|
|
128
145
|
let lastItems = '';
|
|
129
146
|
function syncToolbar() {
|
|
@@ -131,24 +148,40 @@ function syncToolbar() {
|
|
|
131
148
|
if (key === lastItems) return;
|
|
132
149
|
lastItems = key;
|
|
133
150
|
if (hasToolbar) return quiet(() => coda.toolbar(items));
|
|
134
|
-
|
|
151
|
+
menu.hide(); // 重画会换掉 ⋯ 按钮,开着的菜单认不得新按钮
|
|
135
152
|
paintBar($('bar'), items, act);
|
|
153
|
+
$('bar').hidden = items.length === 0; // 没有项(Mac 属性页上的页内工具条)就整条不显示
|
|
154
|
+
$('bar').classList.toggle('exit-only', items.length === 1 && items[0].id === EXIT.id); // 只有返回的工具条跟着返回走:竖屏整条不显示
|
|
136
155
|
}
|
|
137
156
|
|
|
138
157
|
// 原生按钮没有 DOM 元素可以当锚点:用一个固定在页面右上角的假锚点,同时也是「谁打开的菜单」的标记。
|
|
139
158
|
const TOOLBAR = { getBoundingClientRect: () => ({ right: window.innerWidth, bottom: 0 }) };
|
|
140
|
-
// 「更多」菜单的内容只写这一处,iPhone 的 ⋯ 和 Mac
|
|
159
|
+
// 「更多」菜单的内容只写这一处,iPhone 的 ⋯ 和 Mac 的原生按钮共用:刷新、显示隐藏文件,分隔线,小标题和平铺的四个排序项。
|
|
141
160
|
const moreMenu = () => ({ items: [
|
|
142
161
|
{ label: '刷新', run: () => act('refresh') },
|
|
143
162
|
{ label: '显示隐藏文件', checked: model.showHidden, run: () => act('hidden') },
|
|
163
|
+
{ separator: true },
|
|
164
|
+
{ heading: '排序' },
|
|
165
|
+
...Object.values(SortKey).map(key => ({ label: sortName(key), checked: key === model.sort, radio: true, run: () => changeSort(key) })),
|
|
144
166
|
] });
|
|
145
167
|
|
|
168
|
+
// 换排序:旧的滚动位置对新顺序没有意义,待恢复的作废,回到顶部;排序是全局的,存进 persisted。选当前项什么都不做。
|
|
169
|
+
function changeSort(sort) {
|
|
170
|
+
const before = model.sort;
|
|
171
|
+
model.setSort(sort);
|
|
172
|
+
if (model.sort === before) return; // 同值:什么都没换
|
|
173
|
+
persisted.sort = model.sort;
|
|
174
|
+
pending.tree = null;
|
|
175
|
+
tree.scrollTo(0);
|
|
176
|
+
}
|
|
177
|
+
|
|
146
178
|
function act(id, button) {
|
|
147
|
-
|
|
179
|
+
// 横屏时导航栏隐藏,属性页的原生返回看不到:这里的「返回」先关属性页,在根层才关视图。
|
|
180
|
+
if (id === EXIT.id) { if (infoOpen) { backPressed = true; closeInfo(); } else quiet(() => coda.close()); }
|
|
148
181
|
else if (id === 'more') {
|
|
149
182
|
// 键盘激活没有 pointerdown,菜单外点击那条规则不管用:同一个按钮再次激活就关闭。
|
|
150
|
-
if (!menu.hidden && opener === button)
|
|
151
|
-
else
|
|
183
|
+
if (!menu.hidden && menu.opener === button) menu.hide();
|
|
184
|
+
else menu.open(button, moreMenu());
|
|
152
185
|
}
|
|
153
186
|
else if (id === 'hidden') { model.setShowHidden(!model.showHidden); persisted.hidden = model.showHidden; }
|
|
154
187
|
else if (id === 'refresh') {
|
|
@@ -158,7 +191,7 @@ function act(id, button) {
|
|
|
158
191
|
}
|
|
159
192
|
}
|
|
160
193
|
// 原生按钮的事件是 { id }:只认 more,别的什么都不做。
|
|
161
|
-
quiet(() => coda.on('toolbar', event => { if (event?.id === 'more') act('more', TOOLBAR); }));
|
|
194
|
+
quiet(() => coda.on('toolbar', event => { if (event?.id === 'more' && !infoOpen) act('more', TOOLBAR); })); // 属性页上原生按钮已撤掉,迟到的事件不弹菜单
|
|
162
195
|
|
|
163
196
|
// ---- 状态保留 ----
|
|
164
197
|
let saveTimer = null;
|
|
@@ -173,7 +206,7 @@ function saveNow() {
|
|
|
173
206
|
const top = preview.scroll();
|
|
174
207
|
putSession(persisted, session, {
|
|
175
208
|
...model.snapshot(),
|
|
176
|
-
scroll: { tree: pending.tree ?? tree.scroll(), top: pending.preview?.top ?? top.top, left: pending.preview?.left ?? top.left },
|
|
209
|
+
scroll: { tree: pending.tree ?? tree.scroll(), top: pending.preview?.top ?? top.top, left: pending.preview?.left ?? top.left, sort: model.sort },
|
|
177
210
|
});
|
|
178
211
|
persisted = trimState(persisted, session);
|
|
179
212
|
// setState 超限会同步抛错:再收紧一次,仍失败就放弃这次,不影响界面。
|
|
@@ -182,79 +215,64 @@ function saveNow() {
|
|
|
182
215
|
quiet(() => coda.on('visibility', visible => { if (!visible) saveNow(); }));
|
|
183
216
|
$('tree').addEventListener('scroll', queueSave, { passive: true });
|
|
184
217
|
preview.onScroll(queueSave);
|
|
218
|
+
preview.onScroll(menu.hide);
|
|
185
219
|
|
|
186
|
-
// ----
|
|
187
|
-
//
|
|
188
|
-
//
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
name.textContent = label;
|
|
199
|
-
if (checkable) {
|
|
200
|
-
const mark = document.createElement('span');
|
|
201
|
-
mark.className = 'check';
|
|
202
|
-
if (checked) mark.append(icon('check', 16));
|
|
203
|
-
button.append(mark);
|
|
204
|
-
}
|
|
205
|
-
button.append(name);
|
|
206
|
-
button.setAttribute('role', checked === undefined ? 'menuitem' : 'menuitemcheckbox');
|
|
207
|
-
if (checked !== undefined) button.setAttribute('aria-checked', String(checked));
|
|
208
|
-
button.addEventListener('click', () => { run(); hideMenu(); });
|
|
209
|
-
return button;
|
|
210
|
-
}));
|
|
211
|
-
menu.hidden = false;
|
|
212
|
-
const box = anchor.getBoundingClientRect?.();
|
|
213
|
-
const x = box ? box.right - menu.offsetWidth : anchor.clientX, y = box ? box.bottom + 4 : anchor.clientY;
|
|
214
|
-
menu.style.left = `max(var(--safe-left), min(${x}px, calc(100vw - var(--safe-right) - ${menu.offsetWidth + 4}px)))`;
|
|
215
|
-
menu.style.top = `max(var(--safe-top), min(${y}px, calc(100vh - var(--safe-bottom) - ${menu.offsetHeight + 4}px)))`;
|
|
216
|
-
if (box) { opener = anchor; opener.setAttribute?.('aria-expanded', 'true'); }
|
|
220
|
+
// ---- 行菜单:Mac 右键、iPhone 长按;iPhone 的 ⋯ 和 Mac 的原生「更多」出刷新、显示隐藏文件和排序(菜单本身在 menu.mjs)----
|
|
221
|
+
// 行菜单顶上是路径,下面一行 大小 · 完整修改时间(文件夹只有修改时间,读不到就没有这一行);指向外面的链接和不能打开的条目没有元数据,不显示这一行。
|
|
222
|
+
// 不能打开的条目(指向外面或失效的链接、other)没有「添加到聊天」,和不能拖动一致;它们仍有复制路径和属性。
|
|
223
|
+
function pathMenu(event, row) {
|
|
224
|
+
const dead = row.kind === EntryKind.LINK || row.kind === EntryKind.OTHER;
|
|
225
|
+
menu.open(event, { title: row.path, detail: dead ? undefined : infoText(row) || undefined, items: [
|
|
226
|
+
...(dead ? [] : [{ label: '添加到聊天', run: () => chat.add(row.path) }]),
|
|
227
|
+
{ label: '复制相对路径', run: () => coda.copy(row.path) },
|
|
228
|
+
{ label: '复制绝对路径', run: () => coda.copy(`${model.root.path}/${row.path}`) },
|
|
229
|
+
{ separator: true },
|
|
230
|
+
{ label: '属性', run: () => openInfo(row) },
|
|
231
|
+
] });
|
|
217
232
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
233
|
+
$('tree').addEventListener('scroll', menu.hide, { passive: true });
|
|
234
|
+
|
|
235
|
+
// ---- 属性页:iPhone 进入时声明原生返回一层,所有关闭的路径(原生返回、横屏的返回、换会话、根目录变化)都重新声明回根层 ----
|
|
236
|
+
function openInfo(row) {
|
|
237
|
+
infoOpen = true;
|
|
238
|
+
info.open(row.path, row);
|
|
239
|
+
if (nativeBack) quiet(() => coda.back(['资源管理器']));
|
|
240
|
+
schedule();
|
|
241
|
+
}
|
|
242
|
+
function closeInfo() {
|
|
243
|
+
if (!infoOpen) return;
|
|
244
|
+
infoOpen = false;
|
|
245
|
+
info.close(); // 在统计的一并取消
|
|
246
|
+
if (nativeBack) quiet(() => coda.back([]));
|
|
247
|
+
schedule();
|
|
226
248
|
}
|
|
227
|
-
//
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}, true);
|
|
233
|
-
document.addEventListener('click', event => {
|
|
234
|
-
if (menuDismissed) { menuDismissed = false; event.stopPropagation(); }
|
|
235
|
-
}, true);
|
|
236
|
-
document.addEventListener('keydown', event => { if (event.key === 'Escape') hideMenu(); });
|
|
237
|
-
window.addEventListener('blur', hideMenu);
|
|
238
|
-
$('tree').addEventListener('scroll', hideMenu, { passive: true });
|
|
249
|
+
// steps 是退几级,属性页只有一级,不论几都回到目录树;已经不在属性页时也重新声明一次根层。
|
|
250
|
+
if (nativeBack) quiet(() => coda.on('back', () => {
|
|
251
|
+
if (infoOpen) closeInfo();
|
|
252
|
+
else quiet(() => coda.back([]));
|
|
253
|
+
}));
|
|
239
254
|
|
|
240
255
|
// ---- 模块的消息(代次和握手由 connection 处理)----
|
|
241
256
|
function onSession(data) {
|
|
242
257
|
saveNow(); // 先存旧会话
|
|
243
258
|
preview.close();
|
|
244
|
-
|
|
259
|
+
closeInfo();
|
|
260
|
+
menu.hide();
|
|
245
261
|
tree.reset();
|
|
246
262
|
epoch++;
|
|
247
263
|
opening = false;
|
|
248
264
|
clearTimeout(unlockTimer);
|
|
249
|
-
|
|
250
|
-
$('hint').hidden = true;
|
|
265
|
+
hint.hide();
|
|
251
266
|
const saved = persisted.sessions[data.session] ?? null;
|
|
252
|
-
|
|
267
|
+
// 目录树的滚动位置是按当时的排序存的;排序是全局的,在别的会话换过就对不上,等内容到了回到顶部。
|
|
268
|
+
pending = saved ? { tree: saved.scroll.sort === model.sort ? saved.scroll.tree : 0, preview: { top: saved.scroll.top, left: saved.scroll.left } } : { tree: null, preview: null };
|
|
253
269
|
model.receive(data, saved);
|
|
254
270
|
}
|
|
255
271
|
|
|
256
272
|
function onRoot(data) {
|
|
257
273
|
const before = model.root;
|
|
274
|
+
if (data.state !== before?.state || data.path !== before?.path) menu.hide(); // 开着的菜单里的路径是旧根目录下的
|
|
275
|
+
if (data.state !== RootState.READY || before?.path !== data.path) closeInfo(); // 属性页看的是旧根目录下的路径
|
|
258
276
|
const done = model.receive(data);
|
|
259
277
|
if (done && data.state === RootState.READY) {
|
|
260
278
|
if (before?.path !== data.path) preview.reload(); else preview.refresh();
|
|
@@ -268,7 +286,11 @@ function onRoot(data) {
|
|
|
268
286
|
}
|
|
269
287
|
|
|
270
288
|
function onChanged(data) {
|
|
271
|
-
if (model.receive(data))
|
|
289
|
+
if (!model.receive(data)) return;
|
|
290
|
+
menu.hide(); // 开着的菜单可能指着已经删除的路径
|
|
291
|
+
dataRound++;
|
|
292
|
+
preview.refresh();
|
|
293
|
+
info.refresh();
|
|
272
294
|
}
|
|
273
295
|
|
|
274
296
|
tree.remeasure();
|
package/view/chat.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// 把文字交给对话(页面这一侧):写进输入框;写不进去、放不下就改成复制并提示。树页和文件页共用。
|
|
2
|
+
// 写草稿本身在模块里(lib/draft.mjs),页面只发 add_text 并看回答。
|
|
3
|
+
import { MESSAGE_LIMIT, Type } from './protocol.mjs';
|
|
4
|
+
|
|
5
|
+
// 选区的位置文字:src/app.ts:12-18,只有一行时是 src/app.ts:12。
|
|
6
|
+
export const positionText = (path, startLine, endLine) => (endLine > startLine ? `${path}:${startLine}-${endLine}` : `${path}:${startLine}`);
|
|
7
|
+
|
|
8
|
+
export const MESSAGES = {
|
|
9
|
+
added: '已添加到输入框',
|
|
10
|
+
failed: '没能写入输入框,已复制',
|
|
11
|
+
tooLong: '选中内容太长,已复制',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// link:连接层的 Link;copy(text):复制到剪贴板;hint:view/hint.mjs 的提示。
|
|
15
|
+
export function createChat({ link, copy, hint }) {
|
|
16
|
+
const copied = (text, message) => {
|
|
17
|
+
try { Promise.resolve(copy(text)).catch(() => {}); } catch { /* 剪贴板不可用 */ }
|
|
18
|
+
hint.show(message);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// block:true 是内容(另起一行),false 是路径和位置(接在后面)。
|
|
22
|
+
// 会话换了、视图关了(Stale、Cancelled)静默:不提示、不复制。别的失败(含 draft_too_long、超时)都改成复制。
|
|
23
|
+
async function add(text, { block = false } = {}) {
|
|
24
|
+
const fields = { text, block };
|
|
25
|
+
// 按完整信封算:超过消息上限的发了会被宿主丢掉,页面只能等到超时。
|
|
26
|
+
if (link.size(Type.ADD_TEXT, fields) > MESSAGE_LIMIT) return copied(text, MESSAGES.tooLong);
|
|
27
|
+
try {
|
|
28
|
+
await link.call(Type.ADD_TEXT, fields);
|
|
29
|
+
hint.show(MESSAGES.added, { warn: false });
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error?.stale) return;
|
|
32
|
+
copied(text, MESSAGES.failed);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
add,
|
|
38
|
+
// 选中文字的菜单项。内容和位置在打开菜单前就取好了(selection),点菜单时不再读选区。
|
|
39
|
+
selectionItems: ({ path, text, startLine, endLine }) => [
|
|
40
|
+
{ label: '添加到聊天(内容)', run: () => add(text, { block: true }) },
|
|
41
|
+
{ label: '添加到聊天(位置)', run: () => add(positionText(path, startLine, endLine)) },
|
|
42
|
+
{ label: '复制', run: () => copy(text) },
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
}
|