@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/view/menu.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// 菜单:Mac 右键、iPhone 长按的行菜单,Mac 选中文字的右键菜单,iPhone 的 ⋯ 和 Mac 原生「更多」弹出的菜单都用它,树页和文件页各挂一个。
|
|
2
|
+
// 菜单开着时点菜单外只关菜单、紧接着的那次点击不交给页面;Escape、窗口失焦关闭;页面里别的滚动区域滚动时由页面调 hide。
|
|
3
|
+
import { icon } from './icons.mjs';
|
|
4
|
+
|
|
5
|
+
// el:页面里的 #menu。open 之后的位置按安全区夹住(横屏时客户端不替页面留);offsetWidth 在显示后才量得到。
|
|
6
|
+
// 高度有上限(style.css 的 .menu):内容多时在菜单里滚动,不超出安全区。
|
|
7
|
+
export function mountMenu(el) {
|
|
8
|
+
let opener = null; // 打开菜单的按钮;用点打开的没有
|
|
9
|
+
let current = null; // 这一次打开的标记:关闭之后留在手里的按钮不再响应
|
|
10
|
+
|
|
11
|
+
// anchor:按钮元素或同样有 getBoundingClientRect 的假锚点(菜单贴在它下面、右对齐)或 { clientX, clientY } 的点。
|
|
12
|
+
// items:[{ label, run, checked?, radio? } | { separator: true } | { heading }],checked 不是 undefined 的带对勾(radio 的是单选,其余是开关);
|
|
13
|
+
// 分隔线和小标题不是菜单项,不可点。title 写在菜单顶上:iPhone 没有 title 悬停,长文件名在行里被截断,这里看全;detail 是它下面的一行小字。
|
|
14
|
+
function open(anchor, { title, detail, items }) {
|
|
15
|
+
hide();
|
|
16
|
+
const mine = current = {};
|
|
17
|
+
const head = [];
|
|
18
|
+
if (title !== undefined) head.push(Object.assign(document.createElement('p'), { className: 'menu-path secondary', textContent: title }));
|
|
19
|
+
if (detail !== undefined) head.push(Object.assign(document.createElement('p'), { className: 'menu-info secondary caption', textContent: detail }));
|
|
20
|
+
const checkable = items.some(item => item.checked !== undefined); // 有开关项时,每一项都留出对勾的位置,文字才对齐
|
|
21
|
+
el.replaceChildren(...head, ...items.map(({ label, run, checked, radio, separator, heading }) => {
|
|
22
|
+
if (separator) return Object.assign(document.createElement('hr'), { className: 'menu-sep' });
|
|
23
|
+
if (heading !== undefined) return Object.assign(document.createElement('p'), { className: 'menu-head secondary caption', textContent: heading });
|
|
24
|
+
const button = document.createElement('button'), name = document.createElement('span');
|
|
25
|
+
name.textContent = label;
|
|
26
|
+
if (checkable) {
|
|
27
|
+
const mark = document.createElement('span');
|
|
28
|
+
mark.className = 'check';
|
|
29
|
+
if (checked) mark.append(icon('check', 16));
|
|
30
|
+
button.append(mark);
|
|
31
|
+
}
|
|
32
|
+
button.append(name);
|
|
33
|
+
button.setAttribute('role', checked === undefined ? 'menuitem' : radio ? 'menuitemradio' : 'menuitemcheckbox');
|
|
34
|
+
if (checked !== undefined) button.setAttribute('aria-checked', String(checked));
|
|
35
|
+
button.addEventListener('click', () => {
|
|
36
|
+
if (current !== mine) return; // 菜单已经关了(或换成了别的):留在手里的按钮不再响应
|
|
37
|
+
hide();
|
|
38
|
+
run();
|
|
39
|
+
});
|
|
40
|
+
return button;
|
|
41
|
+
}));
|
|
42
|
+
el.hidden = false;
|
|
43
|
+
el.scrollTop = 0;
|
|
44
|
+
const box = anchor.getBoundingClientRect?.();
|
|
45
|
+
const x = box ? box.right - el.offsetWidth : anchor.clientX, y = box ? box.bottom + 4 : anchor.clientY;
|
|
46
|
+
el.style.left = `max(var(--safe-left), min(${x}px, calc(100vw - var(--safe-right) - ${el.offsetWidth + 4}px)))`;
|
|
47
|
+
el.style.top = `max(var(--safe-top), min(${y}px, calc(100vh - var(--safe-bottom) - ${el.offsetHeight + 4}px)))`;
|
|
48
|
+
if (box) { opener = anchor; opener.setAttribute?.('aria-expanded', 'true'); }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function hide() {
|
|
52
|
+
current = null;
|
|
53
|
+
el.hidden = true;
|
|
54
|
+
opener?.setAttribute?.('aria-expanded', 'false');
|
|
55
|
+
opener = null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 菜单开着时点菜单外:只关菜单,紧接着的那次点击不交给页面。
|
|
59
|
+
let dismissed = false;
|
|
60
|
+
document.addEventListener('pointerdown', event => {
|
|
61
|
+
dismissed = !el.hidden && !el.contains(event.target);
|
|
62
|
+
if (!el.contains(event.target)) hide();
|
|
63
|
+
}, true);
|
|
64
|
+
document.addEventListener('click', event => {
|
|
65
|
+
if (dismissed) { dismissed = false; event.stopPropagation(); }
|
|
66
|
+
}, true);
|
|
67
|
+
document.addEventListener('keydown', event => { if (event.key === 'Escape') hide(); });
|
|
68
|
+
window.addEventListener('blur', hide);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
open, hide,
|
|
72
|
+
get hidden() { return el.hidden; },
|
|
73
|
+
// 谁打开的菜单:同一个按钮再次激活就关闭(键盘激活没有 pointerdown,上面那条规则管不到)。
|
|
74
|
+
get opener() { return opener; },
|
|
75
|
+
};
|
|
76
|
+
}
|
package/view/model.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// 页面的纯状态:目录树、状态保留。不碰 DOM,请求与代次在 connection.mjs,设计见 docs/design/phase-1.md 第 6、7 节。
|
|
2
|
-
import { ErrorCode, RootState, Type } from './protocol.mjs';
|
|
2
|
+
import { DEFAULT_SORT, ErrorCode, RootState, Type, parseSort } from './protocol.mjs';
|
|
3
3
|
|
|
4
4
|
export const STATE_LIMIT = 96 * 1024;
|
|
5
5
|
export const MAX_PARALLEL = 8;
|
|
@@ -39,6 +39,8 @@ export class Model {
|
|
|
39
39
|
wanted = new Map(); // 恢复状态时各目录要取够的条数,第一次取用后删掉
|
|
40
40
|
selected = null;
|
|
41
41
|
showHidden = false;
|
|
42
|
+
sort = DEFAULT_SORT; // 全局一份,由页面存进 persisted
|
|
43
|
+
#round = 0; // refresh 的轮次:新一轮开始后,旧一轮不再派发排队的目录
|
|
42
44
|
|
|
43
45
|
constructor({ link, onChange = () => {} }) { Object.assign(this, { link, onChange }); }
|
|
44
46
|
|
|
@@ -57,6 +59,7 @@ export class Model {
|
|
|
57
59
|
// 换会话:清空一切,展开状态换成这个会话保存的。
|
|
58
60
|
onSession({ generation, session }, saved = null) {
|
|
59
61
|
this.link.reset(generation);
|
|
62
|
+
this.#round++;
|
|
60
63
|
this.session = session ?? null;
|
|
61
64
|
this.root = null;
|
|
62
65
|
this.dirs = new Map();
|
|
@@ -79,34 +82,55 @@ export class Model {
|
|
|
79
82
|
return generation === this.link.generation && this.ready ? this.refresh() : null;
|
|
80
83
|
}
|
|
81
84
|
|
|
82
|
-
// 重新取根和全部已展开的目录,并行不超过 8 个。
|
|
83
|
-
|
|
85
|
+
// 重新取根和全部已展开的目录,并行不超过 8 个。cap:这一轮每个目录最多取够这么多条(换排序时用)。
|
|
86
|
+
// 每一轮带递增的轮次:只查 current() 不够,旧一轮的池子还会启动排队中的目录,递增它们的 token,作废较新的加载。
|
|
87
|
+
refresh({ cap = Infinity } = {}) {
|
|
84
88
|
if (!this.ready) return Promise.resolve();
|
|
85
|
-
const { generation } = this.link;
|
|
86
|
-
return pool(['', ...this.expanded], MAX_PARALLEL, path => this.#reload(path, generation));
|
|
89
|
+
const { generation } = this.link, round = ++this.#round;
|
|
90
|
+
return pool(['', ...this.expanded], MAX_PARALLEL, path => (round === this.#round ? this.#reload(path, generation, { cap }) : undefined));
|
|
87
91
|
}
|
|
88
92
|
|
|
89
|
-
//
|
|
90
|
-
|
|
93
|
+
// 换排序:已加载的目录按新顺序重新取,取够现在的条数,这次最多 RESTORE_LIMIT 条;每个目录一次替换。
|
|
94
|
+
setSort(sort) {
|
|
95
|
+
const next = parseSort(sort);
|
|
96
|
+
if (!next || next === this.sort) return Promise.resolve();
|
|
97
|
+
this.sort = next;
|
|
98
|
+
this.onChange();
|
|
99
|
+
return this.refresh({ cap: RESTORE_LIMIT });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 取够已加载的条数,一次替换;同目录有更新的请求就作废这一个。一次加载固定一种排序。
|
|
103
|
+
// more、expired:「再显示」遇到游标过期转过来时,条数要比现在多一条,而且已经算重取过一次,不再重取。
|
|
104
|
+
async #reload(path, generation, { cap = Infinity, more = false, expired = false } = {}) {
|
|
91
105
|
if (this.link.generation !== generation) return; // 排队时会话已换,旧任务不碰新会话
|
|
92
106
|
let dir = this.dirs.get(path);
|
|
93
|
-
if (!dir) this.dirs.set(path, dir = { entries: [], next: null, loaded: false, loading: false, error: null, token: 0, pages: 0 });
|
|
94
|
-
const token = ++dir.token,
|
|
107
|
+
if (!dir) this.dirs.set(path, dir = { entries: [], next: null, loaded: false, loading: false, error: null, token: 0, pages: 0, sortFallback: false });
|
|
108
|
+
const token = ++dir.token, { sort } = this;
|
|
109
|
+
const want = more ? dir.entries.length + 1 : Math.min(Math.max(dir.entries.length, this.wanted.get(path) ?? 0), cap);
|
|
95
110
|
dir.loading = true;
|
|
96
111
|
this.onChange();
|
|
97
112
|
const current = () => this.link.generation === generation && this.dirs.get(path) === dir && dir.token === token;
|
|
98
113
|
try {
|
|
99
|
-
|
|
100
|
-
let next = null, pages = 0;
|
|
114
|
+
let entries = [], next = null, pages = 0, sortFallback = false;
|
|
101
115
|
do {
|
|
102
|
-
|
|
103
|
-
|
|
116
|
+
let reply;
|
|
117
|
+
try {
|
|
118
|
+
reply = await this.link.call('list', { path, cursor: next, sort });
|
|
119
|
+
} catch (error) {
|
|
120
|
+
// 游标过期(快照被淘汰、释放,或根目录变了):丢掉这一轮累积的,从第一页重取一次,再失败走失败行。
|
|
121
|
+
if (error.code !== ErrorCode.CURSOR_EXPIRED || expired || !current()) throw error;
|
|
122
|
+
expired = true;
|
|
123
|
+
entries = []; next = null; pages = 0; sortFallback = false;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (!current()) return; // 回答到了、继续前会话换了或有更新的加载,不再取下一页
|
|
127
|
+
if (pages === 0) sortFallback = reply.sortFallback === true;
|
|
104
128
|
entries.push(...reply.entries);
|
|
105
129
|
next = reply.nextCursor ?? null;
|
|
106
130
|
pages++;
|
|
107
|
-
} while (next && entries.length < want);
|
|
131
|
+
} while (pages === 0 || (next && entries.length < want));
|
|
108
132
|
if (!current()) return;
|
|
109
|
-
Object.assign(dir, { entries, next, loaded: true, error: null, pages });
|
|
133
|
+
Object.assign(dir, { entries, next, loaded: true, error: null, pages, sortFallback });
|
|
110
134
|
this.wanted.delete(path); // 成功后才删:取页失败后重试还要取够保存的条数
|
|
111
135
|
} catch (error) {
|
|
112
136
|
if (error.stale || !current()) return;
|
|
@@ -145,12 +169,15 @@ export class Model {
|
|
|
145
169
|
this.onChange();
|
|
146
170
|
const current = () => this.link.generation === generation && this.dirs.get(path) === dir && dir.token === token;
|
|
147
171
|
try {
|
|
148
|
-
const reply = await this.link.call('list', { path, cursor: dir.next });
|
|
172
|
+
const reply = await this.link.call('list', { path, cursor: dir.next, sort: this.sort });
|
|
149
173
|
if (!current()) return;
|
|
150
174
|
dir.entries = dir.entries.concat(reply.entries);
|
|
151
175
|
Object.assign(dir, { next: reply.nextCursor ?? null, error: null, pages: dir.pages + 1 });
|
|
152
176
|
} catch (error) {
|
|
153
|
-
if (
|
|
177
|
+
if (error.stale || !current()) return;
|
|
178
|
+
// 游标过期:从第一页重取,条数比现在多一条,所以还会接着往下取。重取自己管 loading。
|
|
179
|
+
if (error.code === ErrorCode.CURSOR_EXPIRED) return this.#reload(path, generation, { more: true, expired: true });
|
|
180
|
+
dir.error = { code: error.code, message: error.message };
|
|
154
181
|
} finally {
|
|
155
182
|
if (current()) dir.loading = false;
|
|
156
183
|
this.onChange();
|
|
@@ -178,9 +205,10 @@ const parseLoaded = raw => Object.fromEntries(Object.entries(raw && typeof raw =
|
|
|
178
205
|
|
|
179
206
|
// 读回来的值可能是别的版本或坏的,只认自己的形状。
|
|
180
207
|
export function parseState(raw) {
|
|
181
|
-
const state = { v: 1, seq: 0, hidden: false, sessions: {} };
|
|
208
|
+
const state = { v: 1, seq: 0, hidden: false, sort: DEFAULT_SORT, sessions: {} };
|
|
182
209
|
if (!raw || typeof raw !== 'object' || raw.v !== 1) return state;
|
|
183
210
|
state.hidden = raw.hidden === true;
|
|
211
|
+
state.sort = parseSort(raw.sort) ?? DEFAULT_SORT;
|
|
184
212
|
state.seq = Number.isSafeInteger(raw.seq) ? raw.seq : 0;
|
|
185
213
|
for (const [id, item] of Object.entries(raw.sessions ?? {})) {
|
|
186
214
|
if (!item || typeof item !== 'object') continue;
|
|
@@ -188,7 +216,8 @@ export function parseState(raw) {
|
|
|
188
216
|
state.sessions[id] = {
|
|
189
217
|
at: Number.isSafeInteger(item.at) ? item.at : 0, expanded: strings(item.expanded, 5000),
|
|
190
218
|
selected: typeof item.selected === 'string' && item.selected.length <= 1024 ? item.selected : null,
|
|
191
|
-
|
|
219
|
+
// 滚动位置是按当时的排序存的;旧版本没有这个字段,那时一定是默认排序。
|
|
220
|
+
scroll: { tree: num(scroll.tree), top: num(scroll.top), left: num(scroll.left), sort: parseSort(scroll.sort) ?? DEFAULT_SORT },
|
|
192
221
|
};
|
|
193
222
|
const loaded = parseLoaded(item.loaded);
|
|
194
223
|
if (Object.keys(loaded).length > 0) state.sessions[id].loaded = loaded;
|
package/view/preview.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// 文件预览:只接收路径和读块的能力,不依赖树的状态。readFile 是纯逻辑;mountPreview 才碰 DOM。
|
|
2
|
+
import { h } from './dom.mjs';
|
|
3
|
+
import { formatSize } from './format.mjs';
|
|
2
4
|
import { icon, iconFor } from './icons.mjs';
|
|
3
5
|
import { ContentKind, EntryKind, ErrorCode, IMAGE_LIMIT, TEXT_LIMIT, imageType } from './protocol.mjs';
|
|
6
|
+
import { clippedSelection, linesOf } from './selection.mjs';
|
|
4
7
|
|
|
5
8
|
function fromBase64(data) {
|
|
6
9
|
const binary = atob(data), bytes = new Uint8Array(binary.length);
|
|
@@ -44,30 +47,16 @@ export async function readFile(read, { prev = null, onProgress = () => {}, cance
|
|
|
44
47
|
throw Object.assign(error, { code: ErrorCode.IO });
|
|
45
48
|
}
|
|
46
49
|
|
|
47
|
-
export function formatSize(bytes) {
|
|
48
|
-
if (bytes < 1024) return `${bytes} B`;
|
|
49
|
-
const units = ['KiB', 'MiB', 'GiB'];
|
|
50
|
-
let value = bytes / 1024, unit = 0;
|
|
51
|
-
while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit++; }
|
|
52
|
-
return `${value.toFixed(value < 10 ? 1 : 0)} ${units[unit]}`;
|
|
53
|
-
}
|
|
54
50
|
const formatTime = ms => new Date(ms).toLocaleString('zh-CN', { hour12: false });
|
|
55
51
|
export const lineCount = text => (text === '' ? 1 : text.split('\n').length - (text.endsWith('\n') ? 1 : 0));
|
|
56
52
|
|
|
57
|
-
const h = (tag, attributes = {}, ...children) => {
|
|
58
|
-
const node = document.createElement(tag);
|
|
59
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
60
|
-
if (key.startsWith('on')) node.addEventListener(key.slice(2), value); else if (value != null && value !== false) node.setAttribute(key, value);
|
|
61
|
-
}
|
|
62
|
-
node.append(...children.flat().filter(child => child != null));
|
|
63
|
-
return node;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
53
|
// read(path, offset) → Promise<chunk 回答>,会话换了会以 stale 错误拒绝;copy(text) 复制路径。
|
|
67
54
|
// actions:头部在「复制路径」旁额外加的按钮 [{ label, icon, run(path) }],预览不知道它们做什么。
|
|
68
55
|
// copyInHead:false 时头部不放「复制路径」(iPhone 放在底部工具条),由页面调 copyPath()。
|
|
69
56
|
// back:给了就在头部最左边放「返回」(Mac 侧栏预览:原生工具栏项目只能在右侧);文件页和 iPhone 不传。
|
|
70
|
-
|
|
57
|
+
// selectionMenu(event, { path, text, startLine, endLine }):给了就接管正文里选中文字的右键(只有 Mac 传);预览不知道菜单里是什么。
|
|
58
|
+
// 选区为空、或裁到正文里之后为空时不接管,走系统默认。内容和行号在这里取好,交出去之后和选区无关。
|
|
59
|
+
export function mountPreview(el, { read, copy, actions = [], copyInHead = true, back = null, selectionMenu = null }) {
|
|
71
60
|
const name = h('div', { class: 'pname truncate' }), meta = h('div', { class: 'pmeta caption secondary truncate' }, ' ');
|
|
72
61
|
const fileIcon = h('span', { class: 'pico', 'aria-hidden': 'true' });
|
|
73
62
|
// 图标按钮:可访问名称和 Mac 的悬停提示都是 label;没有图标的退回文字。
|
|
@@ -105,7 +94,9 @@ export function mountPreview(el, { read, copy, actions = [], copyInHead = true,
|
|
|
105
94
|
setMeta([dir, lines, formatSize(result.size), note].filter(Boolean).join(' · '), `修改于 ${formatTime(result.mtimeMs)}`);
|
|
106
95
|
if (result.kind === ContentKind.TEXT) {
|
|
107
96
|
const gutter = h('pre', { class: 'gutter', 'aria-hidden': 'true' }, Array.from({ length: lineCount(result.text) }, (_, i) => i + 1).join('\n'));
|
|
108
|
-
|
|
97
|
+
const text = h('pre', { class: 'text selectable' }, result.text);
|
|
98
|
+
if (selectionMenu) text.addEventListener('contextmenu', event => onSelect(event, text));
|
|
99
|
+
body.replaceChildren(h('div', { class: 'code' }, gutter, text));
|
|
109
100
|
} else if (result.kind === ContentKind.IMAGE && result.bytes.length > 0) {
|
|
110
101
|
objectUrl = URL.createObjectURL(new Blob([result.bytes], { type: imageType(path) }));
|
|
111
102
|
body.replaceChildren(h('div', { class: 'image' }, h('img', { src: objectUrl, alt: path, draggable: 'false' })));
|
|
@@ -115,6 +106,13 @@ export function mountPreview(el, { read, copy, actions = [], copyInHead = true,
|
|
|
115
106
|
}
|
|
116
107
|
}
|
|
117
108
|
|
|
109
|
+
function onSelect(event, text) {
|
|
110
|
+
const picked = clippedSelection(text);
|
|
111
|
+
if (!picked) return;
|
|
112
|
+
event.preventDefault();
|
|
113
|
+
selectionMenu(event, { path, text: picked.text, ...linesOf(text.textContent, picked.start, picked.start + picked.text.length) });
|
|
114
|
+
}
|
|
115
|
+
|
|
118
116
|
function failure(error, silent, retry) {
|
|
119
117
|
if (error.stale) return;
|
|
120
118
|
if (error.code === ErrorCode.NOT_FOUND) { shown = null; setMeta(' '); return notice('文件已不存在', path); }
|
package/view/protocol.mjs
CHANGED
|
@@ -8,25 +8,44 @@ export const PAGE_BYTES = 96 * 1024;
|
|
|
8
8
|
export const ENVELOPE_BYTES = 4096;
|
|
9
9
|
export const TEXT_LIMIT = 1024 * 1024;
|
|
10
10
|
export const IMAGE_LIMIT = 8 * 1024 * 1024;
|
|
11
|
-
// 前这么多字节里有 NUL
|
|
11
|
+
// 前这么多字节里有 NUL 就当二进制(照 Coda 判断二进制的惯例)。
|
|
12
12
|
export const SNIFF_BYTES = 8000;
|
|
13
13
|
export const REQUEST_ID_MAX = 64;
|
|
14
|
+
// 输入框草稿的文字上限(UTF-16 单元);拼完超过就不写。
|
|
15
|
+
export const DRAFT_LIMIT = 100_000;
|
|
14
16
|
|
|
15
17
|
export const Type = Object.freeze({
|
|
16
|
-
HELLO: 'hello', REFRESH: 'refresh', LIST: 'list', READ: 'read',
|
|
17
|
-
|
|
18
|
+
HELLO: 'hello', REFRESH: 'refresh', LIST: 'list', READ: 'read', STAT: 'stat', DIR_SIZE: 'dir_size', ADD_TEXT: 'add_text',
|
|
19
|
+
DIR_SIZE_CANCEL: 'dir_size_cancel', // 没有回答
|
|
20
|
+
SESSION: 'session', ROOT: 'root', CHANGED: 'changed',
|
|
21
|
+
LISTED: 'listed', CHUNK: 'chunk', STATTED: 'statted', SIZED: 'sized', ADDED: 'added',
|
|
18
22
|
});
|
|
19
23
|
|
|
24
|
+
// 请求 → 回答的类型。模块回答、页面分发都查这一张。
|
|
25
|
+
export const ReplyOf = Object.freeze({
|
|
26
|
+
[Type.LIST]: Type.LISTED, [Type.READ]: Type.CHUNK, [Type.STAT]: Type.STATTED,
|
|
27
|
+
[Type.DIR_SIZE]: Type.SIZED, [Type.ADD_TEXT]: Type.ADDED,
|
|
28
|
+
});
|
|
29
|
+
export const isReply = type => Object.values(ReplyOf).includes(type);
|
|
30
|
+
|
|
20
31
|
export const RootState = Object.freeze({ READY: 'ready', NONE: 'none', PREPARING: 'preparing', GONE: 'gone' });
|
|
21
32
|
|
|
22
33
|
export const ErrorCode = Object.freeze({
|
|
23
34
|
NO_ROOT: 'no_root', INVALID_PATH: 'invalid_path', OUTSIDE_ROOT: 'outside_root', NOT_FOUND: 'not_found',
|
|
24
|
-
NOT_DIR: 'not_dir', NOT_FILE: 'not_file', DENIED: 'denied',
|
|
25
|
-
INVALID_OFFSET: 'invalid_offset', TOO_LARGE: 'too_large', IO: 'io_error',
|
|
35
|
+
NOT_DIR: 'not_dir', NOT_FILE: 'not_file', DENIED: 'denied', CURSOR_EXPIRED: 'cursor_expired',
|
|
36
|
+
INVALID_OFFSET: 'invalid_offset', INVALID_SORT: 'invalid_sort', TOO_LARGE: 'too_large', IO: 'io_error',
|
|
37
|
+
DRAFT_FAILED: 'draft_failed', DRAFT_TOO_LONG: 'draft_too_long',
|
|
26
38
|
});
|
|
27
39
|
|
|
40
|
+
// 目录排序:值是下面四种之一,缺省(请求不带 sort)是名称。方向由排序键决定,只在 lib/tree.mjs 里定义一处。
|
|
41
|
+
export const SortKey = Object.freeze({ NAME: 'name', KIND: 'kind', DATE: 'date', SIZE: 'size' });
|
|
42
|
+
export const DEFAULT_SORT = SortKey.NAME;
|
|
43
|
+
// 是四种之一的返回它,否则 null(含旧版本保存过的 { key, desc } 对象)。
|
|
44
|
+
export const parseSort = raw => (Object.values(SortKey).includes(raw) ? raw : null);
|
|
45
|
+
|
|
28
46
|
// 目录条目的 kind:dir、file;link 是指向工作目录外或已失效的链接;other 是不能打开的(套接字、名字含控制字符等)。
|
|
29
47
|
// 指向工作目录内的链接按目标显示成 dir 或 file,并带 link: true。
|
|
48
|
+
// 条目还可带整数 size(只有文件有)和 mtimeMs(向下取整);读不到时不带。
|
|
30
49
|
export const EntryKind = Object.freeze({ DIR: 'dir', FILE: 'file', LINK: 'link', OTHER: 'other' });
|
|
31
50
|
// 文件块的 kind。
|
|
32
51
|
export const ContentKind = Object.freeze({ TEXT: 'text', IMAGE: 'image', BINARY: 'binary' });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// 预览里选中的文字:裁到正文元素里面,算出覆盖的行。DOM 的部分只用 Range 的标准方法,行号是纯函数。
|
|
2
|
+
const START_TO_START = 0, END_TO_END = 2; // Range.START_TO_START、Range.END_TO_END
|
|
3
|
+
|
|
4
|
+
// el 是正文(一个 pre 加一个文本节点)。选区为空或和 el 不相交返回 null;
|
|
5
|
+
// 否则把选区裁到 el 里,返回 { text, start }:裁剪后的文字和它在 el 文字里的起点(UTF-16 偏移)。裁剪后为空也返回 null。
|
|
6
|
+
// 选区的起点或终点落在 el 外面(跨出正文、从行号列开始拖)时,裁掉外面的部分。
|
|
7
|
+
export function clippedSelection(el, selection = globalThis.getSelection?.()) {
|
|
8
|
+
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) return null;
|
|
9
|
+
const range = selection.getRangeAt(0);
|
|
10
|
+
if (!range.intersectsNode(el)) return null;
|
|
11
|
+
const whole = document.createRange();
|
|
12
|
+
whole.selectNodeContents(el);
|
|
13
|
+
const clipped = range.cloneRange();
|
|
14
|
+
if (range.compareBoundaryPoints(START_TO_START, whole) < 0) clipped.setStart(whole.startContainer, whole.startOffset);
|
|
15
|
+
if (range.compareBoundaryPoints(END_TO_END, whole) > 0) clipped.setEnd(whole.endContainer, whole.endOffset);
|
|
16
|
+
const text = clipped.toString();
|
|
17
|
+
if (text === '') return null;
|
|
18
|
+
const before = whole.cloneRange();
|
|
19
|
+
before.setEnd(clipped.startContainer, clipped.startOffset);
|
|
20
|
+
return { text, start: before.toString().length };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const lineAt = (text, offset) => {
|
|
24
|
+
let line = 1;
|
|
25
|
+
for (let at = text.indexOf('\n'); at !== -1 && at < offset; at = text.indexOf('\n', at + 1)) line++;
|
|
26
|
+
return line;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// 选中 text[start, end) 覆盖的行(从 1 数)。选区的结尾正好在行首时(如三击选行,选区包含那一行的换行)不算下一行。
|
|
30
|
+
export function linesOf(text, start, end) {
|
|
31
|
+
const last = end > start && text[end - 1] === '\n' ? end - 1 : end;
|
|
32
|
+
return { startLine: lineAt(text, start), endLine: lineAt(text, Math.max(start, last)) };
|
|
33
|
+
}
|
package/view/style.css
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
/* 资源管理器的页面,颜色、字体、字号只用 App 给的 --coda-*
|
|
1
|
+
/* 资源管理器的页面,颜色、字体、字号只用 App 给的 --coda-* 变量(插件视图的主题变量)。
|
|
2
|
+
唯一的例外是下面的 --tone-*:种类图标的色调,主题变量里没有这一组。 */
|
|
2
3
|
:root {
|
|
3
4
|
--hover: var(--coda-hover, var(--coda-fill, rgba(0,0,0,.05)));
|
|
4
5
|
--line: var(--coda-separator, rgba(0,0,0,.1));
|
|
@@ -10,6 +11,20 @@
|
|
|
10
11
|
font: var(--coda-font-size, 13px)/1.45 var(--coda-font, -apple-system, system-ui, sans-serif);
|
|
11
12
|
color: var(--coda-foreground, #1d1d1f); background: transparent;
|
|
12
13
|
}
|
|
14
|
+
/* 种类图标的色调。图标是 currentColor,.tone-* 把颜色给到 svg 上;文件夹用强调色,链接和特殊文件没有色调,跟随三级文字色。
|
|
15
|
+
两种外观下,在 --coda-background 和 --coda-card 上的对比度都不低于 3:1(harness 读真实计算值检查);改色值要重新跑。 */
|
|
16
|
+
:root {
|
|
17
|
+
--tone-gray: #6b6b72; --tone-blue: #2a68b8; --tone-yellow: #946a00; --tone-green: #26803f; --tone-teal: #12788f;
|
|
18
|
+
--tone-orange: #b85a14; --tone-red: #c0303a; --tone-purple: #7447bd; --tone-pink: #b8326f;
|
|
19
|
+
}
|
|
20
|
+
:root.coda-dark {
|
|
21
|
+
--tone-gray: #a3a3a9; --tone-blue: #6aa9ec; --tone-yellow: #e3b63e; --tone-green: #63c77f; --tone-teal: #58c3dd;
|
|
22
|
+
--tone-orange: #f0964f; --tone-red: #f0717a; --tone-purple: #ac87ec; --tone-pink: #ee77ac;
|
|
23
|
+
}
|
|
24
|
+
.tone-accent { color: var(--coda-accent, #0a84ff); }
|
|
25
|
+
.tone-gray { color: var(--tone-gray); } .tone-blue { color: var(--tone-blue); } .tone-yellow { color: var(--tone-yellow); }
|
|
26
|
+
.tone-green { color: var(--tone-green); } .tone-teal { color: var(--tone-teal); } .tone-orange { color: var(--tone-orange); }
|
|
27
|
+
.tone-red { color: var(--tone-red); } .tone-purple { color: var(--tone-purple); } .tone-pink { color: var(--tone-pink); }
|
|
13
28
|
* { box-sizing: border-box; }
|
|
14
29
|
html, body { margin: 0; height: 100%; }
|
|
15
30
|
body { display: flex; flex-direction: column; overflow: hidden; -webkit-user-select: none; user-select: none;
|
|
@@ -31,11 +46,11 @@ p { margin: 0; }
|
|
|
31
46
|
.pane.off { visibility: hidden; pointer-events: none; }
|
|
32
47
|
.empty { display: grid; align-content: start; justify-items: center; gap: 6px; padding: 32px 20px; text-align: center; }
|
|
33
48
|
.notice-icon { display: grid; place-items: center; margin-bottom: 6px; color: var(--coda-secondary, #6e6e73); }
|
|
34
|
-
.notice-icon svg { display: block; }
|
|
49
|
+
.notice-icon svg { display: block; color: inherit; } /* 说明区的图标不上色 */
|
|
35
50
|
|
|
36
51
|
/* 页内工具条:iPhone 树页在顶部(返回、根目录名、⋯),文件页在底部(.bottom:返回、刷新、复制路径)。没有 coda.toolbar 的其他平台(harness)是一条文字按钮。 */
|
|
37
52
|
.bar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
|
|
38
|
-
.bar button { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: var(--coda-radius, 6px); color: var(--coda-secondary, #6e6e73); }
|
|
53
|
+
.bar button { flex: none; white-space: nowrap; display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: var(--coda-radius, 6px); color: var(--coda-secondary, #6e6e73); }
|
|
39
54
|
.bar button.on { color: var(--coda-accent, #0a84ff); }
|
|
40
55
|
.bar svg { display: block; flex: none; }
|
|
41
56
|
.bar-title { padding: 0 8px; font-weight: 600; }
|
|
@@ -43,13 +58,20 @@ p { margin: 0; }
|
|
|
43
58
|
|
|
44
59
|
/* 树:只画可见的一段,行高固定(tree.mjs 量 .row 的高度)。
|
|
45
60
|
Mac 是侧栏列表:左右缩进 8 点,行有圆角,选中用 --coda-selection。 */
|
|
61
|
+
/* 标题行:根目录名,随内容滚走。它在滚动容器里占着自己的高度,tree.mjs 把 spacer 的 offsetTop 扣掉。 */
|
|
46
62
|
.section { padding: 10px 16px 4px; font-size: .85em; font-weight: 600; color: var(--coda-secondary, #6e6e73); cursor: default; }
|
|
63
|
+
/* 行宽按 #tree 的宽度查询:窄于约 220 点时 Mac 的 meta 不显示。 */
|
|
64
|
+
#tree { container: tree / inline-size; }
|
|
47
65
|
.spacer { position: relative; margin: 0 8px; }
|
|
48
66
|
.win { position: absolute; left: 0; right: 0; top: 0; }
|
|
49
67
|
/* 缩进按行的 --d(深度)算,iPhone 的分隔线起点也用它。 */
|
|
50
68
|
.row { position: relative; display: flex; align-items: center; gap: 4px; height: var(--row-h); padding: 0 8px 0 calc(var(--indent) * var(--d, 0) + var(--pad));
|
|
51
69
|
border-radius: var(--coda-radius, 6px); white-space: nowrap; cursor: default; }
|
|
52
70
|
.row.sel { background: var(--coda-selection, var(--hover)); }
|
|
71
|
+
/* Mac 的缩进线:每一层一条,在祖先箭头(14 点宽)的中心,按行的 --d 画重复渐变,不增加 DOM。
|
|
72
|
+
用 ::after(::before 是 iPhone 的分隔线),画在行背景的上面,悬停、选中的 background 盖不掉。 */
|
|
73
|
+
.coda-mac .row::after { content: ''; position: absolute; top: 0; bottom: 0; left: calc(var(--pad) + 6.5px); width: calc(var(--indent) * var(--d, 0)); pointer-events: none;
|
|
74
|
+
background: repeating-linear-gradient(to right, var(--coda-separator, var(--line)) 0 1px, transparent 1px var(--indent)); }
|
|
53
75
|
.row.more, .row.retry { color: var(--coda-accent, #0a84ff); cursor: pointer; }
|
|
54
76
|
.row.status { display: block; overflow: hidden; text-overflow: ellipsis; line-height: var(--row-h); color: var(--coda-secondary, #6e6e73); }
|
|
55
77
|
.row.dead .name, .row.dead .ico { color: var(--coda-tertiary, #a1a1a6); }
|
|
@@ -60,7 +82,11 @@ p { margin: 0; }
|
|
|
60
82
|
.ico { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
|
|
61
83
|
.ico svg { display: block; }
|
|
62
84
|
.row.dir .ico { color: var(--coda-accent, #0a84ff); }
|
|
63
|
-
.
|
|
85
|
+
.main { flex: 1 1 auto; min-width: 0; }
|
|
86
|
+
.name { display: block; }
|
|
87
|
+
.sub { display: none; }
|
|
88
|
+
.meta { flex: none; padding-left: 4px; font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
89
|
+
@container tree (max-width: 220px) { .meta { display: none; } }
|
|
64
90
|
.tail { flex: none; display: grid; place-items: center; color: var(--coda-secondary, #6e6e73); }
|
|
65
91
|
.tail:empty { display: none; }
|
|
66
92
|
.tail svg { display: block; }
|
|
@@ -85,17 +111,34 @@ button.icon svg { display: block; }
|
|
|
85
111
|
.image img { max-width: 100%; height: auto; border-radius: var(--coda-radius, 6px); background: var(--coda-secondary-background, transparent); }
|
|
86
112
|
|
|
87
113
|
#notice:focus { outline: none; }
|
|
88
|
-
.menu { position: fixed; z-index: 10;
|
|
89
|
-
background: var(--coda-card, var(--coda-background, #fff));
|
|
114
|
+
.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
|
+
background: var(--coda-card, var(--coda-background, #fff));
|
|
116
|
+
/* 高度有上限:内容多(长路径加五项)时在菜单里滚动,不超出安全区;位置由 menu.mjs 夹在安全区内。 */
|
|
117
|
+
max-height: calc(100vh - var(--safe-top) - var(--safe-bottom) - 8px); overflow-y: auto; overscroll-behavior: contain; }
|
|
90
118
|
.menu button { display: flex; align-items: center; gap: 6px; width: 100%; padding: 5px 10px; border-radius: var(--coda-radius, 6px); }
|
|
91
119
|
.check { flex: none; display: grid; place-items: center; width: 16px; color: var(--coda-accent, #0a84ff); }
|
|
92
120
|
.check svg { display: block; }
|
|
93
|
-
.menu
|
|
121
|
+
/* 菜单宽度随内容,上限见 .menu:路径超过上限折行,大小 · 完整修改时间不折。 */
|
|
122
|
+
.menu-path { padding: 4px 10px 6px; overflow-wrap: anywhere; }
|
|
123
|
+
.menu-info { padding: 0 10px 6px; margin-top: -4px; white-space: nowrap; }
|
|
124
|
+
.menu-sep { margin: 4px 6px; border: 0; border-top: 1px solid var(--line); }
|
|
125
|
+
.menu-head { padding: 4px 10px 2px; font-weight: 600; }
|
|
94
126
|
.pmeta.warn { color: var(--coda-orange, #b25000); }
|
|
95
127
|
|
|
128
|
+
/* 底部的短提示:普通的是前景色(成功),.warn 是橙色(失败)。 */
|
|
96
129
|
.hint { position: fixed; left: calc(8px + var(--safe-left)); right: calc(8px + var(--safe-right)); bottom: calc(8px + var(--safe-bottom)); z-index: 10;
|
|
97
130
|
padding: 8px 12px; border: 1px solid var(--coda-outline, var(--line)); border-radius: var(--coda-radius-card, 10px);
|
|
98
|
-
background: var(--coda-card, var(--coda-background, #fff)); color: var(--coda-
|
|
131
|
+
background: var(--coda-card, var(--coda-background, #fff)); color: var(--coda-foreground, #1d1d1f); }
|
|
132
|
+
.hint.warn { color: var(--coda-orange, #b25000); }
|
|
133
|
+
|
|
134
|
+
/* 属性页:头部和正文同预览(.preview 的 .phead、.pbody);正文是 标签 值 两列,值可以折行、可以选中。 */
|
|
135
|
+
.ibody { padding: 8px 12px calc(12px + var(--safe-bottom)); }
|
|
136
|
+
.irow { display: flex; gap: 12px; padding: 5px 0; }
|
|
137
|
+
.ilabel { flex: none; width: 5.5em; text-align: right; }
|
|
138
|
+
.ivalue { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; user-select: text; -webkit-user-select: text; }
|
|
139
|
+
.ivalue .warn { color: var(--coda-orange, #b25000); }
|
|
140
|
+
.coda-ios .irow { flex-direction: column; gap: 0; padding: 8px 4px; }
|
|
141
|
+
.coda-ios .ilabel { width: auto; text-align: left; font-size: .85em; }
|
|
99
142
|
|
|
100
143
|
/* 悬停只在有鼠标时:触摸后不会留着。 */
|
|
101
144
|
@media (hover: hover) {
|
|
@@ -105,9 +148,9 @@ button.icon svg { display: block; }
|
|
|
105
148
|
.menu button:hover { background: var(--coda-selection, var(--hover)); }
|
|
106
149
|
}
|
|
107
150
|
|
|
108
|
-
/* iPhone:行和按钮至少 44
|
|
151
|
+
/* iPhone:行和按钮至少 44 点;行高 58 点(两行:名字、日期 · 大小)在这里定,tree.mjs 量 .row 得到,不另写一份。
|
|
109
152
|
目录树是 Coda 卡片外形的分组列表:.spacer 就是卡片(高度已等于全部行高),首尾圆角不用逐行判断。 */
|
|
110
|
-
.coda-ios { --row-h:
|
|
153
|
+
.coda-ios { --row-h: 58px; --indent: 20px; --pad: 16px; }
|
|
111
154
|
.coda-ios .section { display: none; } /* 根目录名归页内工具条 */
|
|
112
155
|
.coda-ios .spacer { margin: 8px 16px; overflow: hidden; border-radius: var(--coda-radius-card, 12px); background: var(--coda-card, var(--coda-secondary-background, #fff)); }
|
|
113
156
|
/* 描边用伪元素:不占尺寸,spacer 加 border 会让内容区少 2 点,末行被裁。 */
|
|
@@ -119,9 +162,12 @@ button.icon svg { display: block; }
|
|
|
119
162
|
.coda-ios .row { -webkit-touch-callout: none; } /* 长按由页面接管,不弹系统菜单;选择和拖动另外已关 */
|
|
120
163
|
.coda-ios .row.entry:active { background: var(--hover); }
|
|
121
164
|
.coda-ios .ico svg { width: 22px; height: 22px; }
|
|
165
|
+
.coda-ios .main { display: flex; flex-direction: column; justify-content: center; } /* 没有第二行的条目,名字垂直居中 */
|
|
166
|
+
.coda-ios .sub { display: block; }
|
|
167
|
+
.coda-ios .meta { display: none; }
|
|
122
168
|
.coda-ios .chev { order: 9; width: 14px; }
|
|
123
169
|
.coda-ios .row.dir .chev { color: var(--coda-accent, #0a84ff); }
|
|
124
|
-
.coda-ios .row.file .chev svg { visibility:
|
|
170
|
+
.coda-ios .row.file .chev svg { visibility: inherit; } /* 不写 visible:目录树被属性页盖住(visibility: hidden)时,箭头不能透出来 */
|
|
125
171
|
.coda-ios .row.dead .chev { display: none; }
|
|
126
172
|
.coda-ios button { min-height: 44px; min-width: 44px; }
|
|
127
173
|
.coda-ios .bar { gap: 0; padding: 0 8px; }
|
|
@@ -133,7 +179,8 @@ button.icon svg { display: block; }
|
|
|
133
179
|
.coda-ios .phead button { padding: 0 8px; }
|
|
134
180
|
.coda-ios .code { font-size: .8em; } /* 窄屏多看些:17 点下约 13.6 点 */
|
|
135
181
|
.coda-ios .menu button { padding: 0 12px; }
|
|
182
|
+
.coda-ios .menu-head { padding: 6px 12px 2px; }
|
|
136
183
|
|
|
137
184
|
/* 横屏:客户端隐藏导航栏,页面自己给返回。近似 verticalSizeClass 的判断,不等价(iPad、Duo 不单独适配)。 */
|
|
138
|
-
.bar .exit { display: none; }
|
|
139
|
-
@media (orientation: landscape) and (max-height: 500px) { .coda-ios .bar .exit { display: inline-flex; } }
|
|
185
|
+
.bar .exit, .bar.exit-only { display: none; } /* 只有返回的工具条(属性页):返回不显示,整条也不占高度 */
|
|
186
|
+
@media (orientation: landscape) and (max-height: 500px) { .coda-ios .bar .exit { display: inline-flex; } .coda-ios .bar.exit-only { display: flex; } }
|