@coda-console/resource-manager 0.0.0-stage → 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 sealiu1997
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,70 @@
1
- # Temporary Holding Version
1
+ # 资源管理器
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Coda 插件:会话旁的文件浏览器。浏览这个会话的工作目录,读文本和图片,把路径交给对话。只读。
4
+
5
+ - **Mac**:在会话侧栏里,和对话并排。
6
+ - **iPhone**:在会话右上角菜单里打开,点文件推入一层。iPad 和 iPhone Duo 用同一套界面,没有单独适配。
7
+
8
+ ## 功能
9
+
10
+ - 按需展开目录,超大目录分页显示。
11
+ - 文本预览(带行号,可跨行选中复制,最多读 1 MiB)和图片预览(PNG、JPEG、GIF、WebP、SVG,最多 8 MiB)。
12
+ - Agent 一轮结束后,已展开的目录和正在看的文件自动更新。
13
+ - 切换会话时换成新会话的内容,切回来恢复展开状态和滚动位置。
14
+ - Mac:右键复制相对或绝对路径,或把文件拖到输入框;预览可以开到独立窗口;工具栏只有一个「更多」按钮,按下弹出菜单(刷新、显示隐藏文件),侧栏预览头部最左边的「返回」回到目录树。
15
+ - iPhone:长按复制路径;目录树页右上角「⋯」里有刷新和显示隐藏文件;文件页底部工具条有刷新和「复制路径」。
16
+ - 默认隐藏 `.git`、`.DS_Store`,Mac 在工具栏的「更多」菜单里、iPhone 在目录树页右上角的「⋯」里可切换显示全部。
17
+
18
+ 只能读这个会话的工作目录。指向工作目录之外的符号链接只显示,不能打开。
19
+
20
+ ## 安装
21
+
22
+ 在 Coda 的「设置 › 插件 › 商店」里找到「资源管理器」安装;也可以用「从 npm 添加…」填 `@coda-console/resource-manager`。
23
+
24
+ 也可以在「设置 › 插件 › 从仓库添加…」里填 `sealiu1997/coda_plugins_resource_manager`,版本留空即跟随默认分支。
25
+
26
+ 装好后,在会话侧栏的标签里打开「资源管理器」。
27
+
28
+ ## 开发
29
+
30
+ 不需要安装依赖,也没有构建步骤。Node 由 Coda 提供(`$CODA_NODE`)。
31
+
32
+ ```sh
33
+ # 测试,也可以用 npm test。Node 24 上不能写成 node --test test/
34
+ "$CODA_NODE" --test test/*.test.mjs
35
+
36
+ # 不进 Coda,用假的 window.coda 在浏览器里看真实页面
37
+ "$CODA_NODE" test/harness/server.mjs <会话A的目录> [会话B的目录] [端口]
38
+
39
+ # 真实布局的断言和截图:无头 Chrome 打开 Mac 侧栏、iPhone 竖屏和横屏的目录树与文件页(浅色、深色),
40
+ # 量行高、工具条、菜单的位置和尺寸。自己起服务和临时目录,用完删掉;截图在系统临时目录里,路径在最后打印。
41
+ # 失败时退出码非 0。Chrome 不在默认位置时把路径作参数传入
42
+ "$CODA_NODE" test/harness/layout.mjs [Chrome 的路径]
43
+
44
+ # 契约检查,<提交> 用 coda-console/coda-plugins 的提交号
45
+ npx --yes github:coda-console/coda-plugins#<提交> . --manifest
46
+ ```
47
+
48
+ 在这台机器上试改时,「从仓库添加…」填本机仓库的绝对路径,版本填分支名。Coda 只读取已提交的内容,未提交的改动看不到。
49
+
50
+ `test/harness/` 用的是普通浏览器,不是 Coda 客户端里的 WebKit,改完还要在客户端里看一遍。
51
+
52
+ ## 文档
53
+
54
+ - [一期设计](docs/design/phase-1.md):消息协议、路径校验和页面规则。
55
+ - [二期设计](docs/design/phase-2.md):iPhone 适配和独立文件视图。
56
+ - [可选需求清单](docs/backlog.md):之后可能做的需求。
57
+ - [评审准则](.coda/review.md)。
58
+
59
+ ## 发布
60
+
61
+ 1. 改 `package.json` 的 `version`,合并到 `main`。
62
+ 2. 运行 `gh workflow run publish.yml --ref main`。工作流先看这个版本是不是已在 npm 上,已有就跳过。
63
+
64
+ 工作流用 npm 可信发布,不需要令牌。只有手动运行才会发布,合并到 `main` 不会自动发布。
65
+
66
+ 首次发布要先有包,可信发布才能配置:在本机 `npm login` 后手动运行 `npm publish --access public`,再到 npm 上 `@coda-console/resource-manager` 的设置里配置可信发布,仓库填 `sealiu1997/coda_plugins_resource_manager`,流程文件填 `publish.yml`,并勾选允许 `npm publish`(Allow npm publish)。新配置默认只允许 `npm stage publish`,不勾选的话工作流的发布步骤会被拒绝。之后就按上面的步骤走。
67
+
68
+ ## 许可
69
+
70
+ MIT
@@ -0,0 +1,36 @@
1
+ // 分块读文件并判断类型。页面一块一块要,模块不记状态。
2
+ import { open } from 'node:fs/promises';
3
+ import {
4
+ CHUNK_BYTES, ContentKind, ErrorCode, IMAGE_LIMIT, SNIFF_BYTES, TEXT_LIMIT, fail, imageType,
5
+ } from '../view/protocol.mjs';
6
+ import { fromFsError, parseRelative, resolveInRoot } from './paths.mjs';
7
+
8
+ async function readAt(handle, position, length) {
9
+ const buffer = Buffer.allocUnsafe(length);
10
+ const { bytesRead } = length ? await handle.read(buffer, 0, length, position) : { bytesRead: 0 };
11
+ return buffer.subarray(0, bytesRead);
12
+ }
13
+
14
+ // 返回 { data, offset, size, mtimeMs, kind, eof }。文本最多给前 TEXT_LIMIT 字节,
15
+ // 超过限制的图片和二进制文件不给内容(页面看 size 和 kind 说明)。
16
+ export async function readChunk(root, path, offset) {
17
+ if (!Number.isSafeInteger(offset) || offset < 0) throw fail(ErrorCode.INVALID_OFFSET, '偏移无效');
18
+ const real = await resolveInRoot(root, path);
19
+ let handle;
20
+ try { handle = await open(real, 'r'); } catch (error) { throw fromFsError(error); }
21
+ try {
22
+ const info = await handle.stat();
23
+ if (!info.isFile()) throw fail(ErrorCode.NOT_FILE, '不是文件');
24
+ const { size, mtimeMs } = info;
25
+ const image = imageType(parseRelative(path).at(-1) ?? '') !== null;
26
+ const head = image ? null : await readAt(handle, 0, Math.min(SNIFF_BYTES, size));
27
+ const kind = image ? ContentKind.IMAGE : head.includes(0) ? ContentKind.BINARY : ContentKind.TEXT;
28
+ const limit = kind === ContentKind.TEXT ? TEXT_LIMIT : kind === ContentKind.IMAGE ? IMAGE_LIMIT : 0;
29
+ const end = kind === ContentKind.IMAGE && size > limit ? 0 : Math.min(size, limit);
30
+ const bytes = offset < end ? await readAt(handle, offset, Math.min(CHUNK_BYTES, end - offset)) : Buffer.alloc(0);
31
+ return {
32
+ data: bytes.toString('base64'), offset, size, mtimeMs, kind,
33
+ eof: bytes.length === 0 || offset + bytes.length >= end,
34
+ };
35
+ } catch (error) { throw fromFsError(error); } finally { await handle.close(); }
36
+ }
package/lib/paths.mjs ADDED
@@ -0,0 +1,47 @@
1
+ // 路径校验:页面只传相对路径,读取只用这里返回的规范路径。
2
+ import { realpath } from 'node:fs/promises';
3
+ import { join, sep } from 'node:path';
4
+ import { ErrorCode, fail } from '../view/protocol.mjs';
5
+
6
+ const CONTROL = /[\u0000-\u001f\u007f]/;
7
+ const MAX_PATH = 4096;
8
+
9
+ // 名字里能不能带进路径(含控制字符的名字只显示,不能打开)。
10
+ export const isSafeName = name => !CONTROL.test(name);
11
+
12
+ // 相对路径 → 片段。空串是根目录;拒绝绝对路径、`..`、控制字符。
13
+ export function parseRelative(path) {
14
+ if (typeof path !== 'string' || path.length > MAX_PATH) throw fail(ErrorCode.INVALID_PATH, '路径无效');
15
+ if (CONTROL.test(path)) throw fail(ErrorCode.INVALID_PATH, '路径含控制字符');
16
+ if (path.startsWith('/')) throw fail(ErrorCode.INVALID_PATH, '只接受相对路径');
17
+ const segments = path.split('/').filter(part => part !== '' && part !== '.');
18
+ if (segments.includes('..')) throw fail(ErrorCode.INVALID_PATH, '路径不能含 ..');
19
+ return segments;
20
+ }
21
+
22
+ // 等于根目录,或以「根目录 + 分隔符」开头;不能只比前缀,否则 /repo-other 会误匹配 /repo。
23
+ export function isInside(root, target) {
24
+ return target === root || target.startsWith(root.endsWith(sep) ? root : root + sep);
25
+ }
26
+
27
+ // 文件系统错误 → 带 code 的错误,不把带路径的原文交给页面。
28
+ export function fromFsError(error) {
29
+ if (error?.protocol) return error;
30
+ switch (error?.code) {
31
+ case 'ENOENT': return fail(ErrorCode.NOT_FOUND, '不存在');
32
+ case 'ENOTDIR': return fail(ErrorCode.NOT_DIR, '不是目录');
33
+ case 'EISDIR': return fail(ErrorCode.NOT_FILE, '不是文件');
34
+ case 'EACCES': case 'EPERM': return fail(ErrorCode.DENIED, '没有权限');
35
+ case 'ELOOP': return fail(ErrorCode.NOT_FOUND, '链接循环');
36
+ default: return fail(ErrorCode.IO, '读取失败');
37
+ }
38
+ }
39
+
40
+ // 把相对路径解析成根目录内的规范路径;`root` 须是已取过 realpath 的根目录。
41
+ export async function resolveInRoot(root, path) {
42
+ const segments = parseRelative(path);
43
+ let real;
44
+ try { real = await realpath(join(root, ...segments)); } catch (error) { throw fromFsError(error); }
45
+ if (!isInside(root, real)) throw fail(ErrorCode.OUTSIDE_ROOT, '链接指向工作目录之外');
46
+ return real;
47
+ }
package/lib/root.mjs ADDED
@@ -0,0 +1,26 @@
1
+ // 从会话取根目录。这是唯一读取 `threads/<id>` 返回结构(checkout)的地方,它不是文档写明的契约。
2
+ import { realpath, stat } from 'node:fs/promises';
3
+ import { RootState } from '../view/protocol.mjs';
4
+
5
+ export const NO_ROOT = Object.freeze({ state: RootState.NONE, path: null });
6
+
7
+ export const sameRoot = (a, b) => a?.state === b?.state && a?.path === b?.path;
8
+
9
+ // checkout.state 是 preparing、ready、failed。failed 没有可看的目录,按 none;
10
+ // ready 但目录已不在(归档后工作树被回收)是 gone。
11
+ // 查询失败(接口出错、读目录权限)直接抛出,由调用方保留上一次的结果。
12
+ export async function queryRoot(coda, session) {
13
+ if (!session) return NO_ROOT;
14
+ const thread = await coda.api(`threads/${encodeURIComponent(session)}`);
15
+ const checkout = thread?.checkout;
16
+ if (!checkout || typeof checkout !== 'object') return NO_ROOT;
17
+ if (checkout.state === 'preparing') return { state: RootState.PREPARING, path: null };
18
+ if (checkout.state !== 'ready' || typeof checkout.path !== 'string' || !checkout.path) return NO_ROOT;
19
+ try {
20
+ const real = await realpath(checkout.path);
21
+ if ((await stat(real)).isDirectory()) return { state: RootState.READY, path: real };
22
+ } catch (error) {
23
+ if (error?.code !== 'ENOENT' && error?.code !== 'ENOTDIR') throw error;
24
+ }
25
+ return { state: RootState.GONE, path: checkout.path };
26
+ }
package/lib/tree.mjs ADDED
@@ -0,0 +1,74 @@
1
+ // 读目录:先目录后文件,按名字不分大小写排序,按序列化字节数分页。模块回答全部条目,隐藏规则在页面。
2
+ import { readdir, realpath, stat } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { EntryKind, ENVELOPE_BYTES, ErrorCode, PAGE_BYTES, fail } from '../view/protocol.mjs';
5
+ import { fromFsError, isInside, isSafeName, resolveInRoot } from './paths.mjs';
6
+
7
+ const MAX_ENTRIES = 100_000;
8
+ const LINK_CONCURRENCY = 8;
9
+
10
+ const rank = entry => (entry.kind === EntryKind.DIR ? 0 : 1);
11
+ function compare(a, b) {
12
+ if (rank(a) !== rank(b)) return rank(a) - rank(b);
13
+ const x = a.name.toLowerCase(), y = b.name.toLowerCase();
14
+ if (x !== y) return x < y ? -1 : 1;
15
+ return a.name === b.name ? 0 : a.name < b.name ? -1 : 1;
16
+ }
17
+
18
+ const encodeCursor = entry => Buffer.from(JSON.stringify([rank(entry), entry.name])).toString('base64url');
19
+ function decodeCursor(cursor) {
20
+ try {
21
+ const [group, name] = JSON.parse(Buffer.from(String(cursor), 'base64url').toString('utf8'));
22
+ if ((group === 0 || group === 1) && typeof name === 'string') return { kind: group === 0 ? EntryKind.DIR : EntryKind.FILE, name };
23
+ } catch { /* 下面统一报错 */ }
24
+ throw fail(ErrorCode.INVALID_CURSOR, '游标无效');
25
+ }
26
+
27
+ async function mapLimit(items, limit, fn) {
28
+ const results = new Array(items.length);
29
+ let next = 0;
30
+ await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
31
+ while (next < items.length) { const index = next++; results[index] = await fn(items[index]); }
32
+ }));
33
+ return results;
34
+ }
35
+
36
+ // 只有符号链接要额外解析:目标在根目录内按目标显示,在外或已失效显示为 link。
37
+ async function classify(root, dir, dirent) {
38
+ const { name } = dirent;
39
+ if (!isSafeName(name)) return { name, kind: EntryKind.OTHER };
40
+ if (dirent.isDirectory()) return { name, kind: EntryKind.DIR };
41
+ if (dirent.isFile()) return { name, kind: EntryKind.FILE };
42
+ if (!dirent.isSymbolicLink()) return { name, kind: EntryKind.OTHER };
43
+ try {
44
+ const real = await realpath(join(dir, name));
45
+ if (!isInside(root, real)) return { name, kind: EntryKind.LINK };
46
+ const info = await stat(real);
47
+ const kind = info.isDirectory() ? EntryKind.DIR : info.isFile() ? EntryKind.FILE : EntryKind.OTHER;
48
+ return { name, kind, link: true };
49
+ } catch { return { name, kind: EntryKind.LINK }; }
50
+ }
51
+
52
+ // 游标是上一页最后一条的排序键,所以每页都重新读目录;条目增减不会造成重复,也不会漏掉游标之后的。
53
+ export async function listDirectory(root, path, cursor, { maxEntries = MAX_ENTRIES, pageBytes = PAGE_BYTES } = {}) {
54
+ const dir = await resolveInRoot(root, path);
55
+ let dirents;
56
+ try { dirents = await readdir(dir, { withFileTypes: true }); } catch (error) { throw fromFsError(error); }
57
+ if (dirents.length > maxEntries) throw fail(ErrorCode.TOO_LARGE, `目录条目超过 ${maxEntries} 个`);
58
+ const entries = await mapLimit(dirents, LINK_CONCURRENCY, dirent => classify(root, dir, dirent));
59
+ entries.sort(compare);
60
+
61
+ const after = cursor == null ? null : decodeCursor(cursor);
62
+ let index = 0;
63
+ if (after) while (index < entries.length && compare(entries[index], after) <= 0) index++;
64
+
65
+ const page = [];
66
+ let used = 0;
67
+ while (index < entries.length) {
68
+ const size = Buffer.byteLength(JSON.stringify(entries[index])) + 1;
69
+ if (page.length && used + size > pageBytes - ENVELOPE_BYTES) break;
70
+ used += size;
71
+ page.push(entries[index++]);
72
+ }
73
+ return { entries: page, nextCursor: index < entries.length ? encodeCursor(page[page.length - 1]) : null };
74
+ }
package/main.mjs ADDED
@@ -0,0 +1,137 @@
1
+ // 资源管理器的模块:跟踪侧栏的会话、代次和根目录,把页面的请求交给 lib/*,把变化推给页面。
2
+ // 协议见 docs/design/phase-1.md 第 6 节,消息格式见 view/protocol.mjs。
3
+ import { listDirectory } from './lib/tree.mjs';
4
+ import { readChunk } from './lib/content.mjs';
5
+ import { NO_ROOT, queryRoot, sameRoot } from './lib/root.mjs';
6
+ import {
7
+ ErrorCode, MESSAGE_LIMIT, REQUEST_ID_MAX, RootState, Type, fail, messageSize,
8
+ } from './view/protocol.mjs';
9
+
10
+ const POLL_MS = 2000;
11
+
12
+ // 按视图解析会话(设计见 docs/design/phase-2.md 第 6 节):
13
+ // 树只跟随侧栏旁的会话,不看 args;文件视图只取 args.session,须是非空字符串,否则当作没有会话。
14
+ const sessionOfTree = context => context?.session ?? null;
15
+ const sessionOfFile = context => {
16
+ const session = context?.args?.session;
17
+ return typeof session === 'string' && session !== '' ? session : null;
18
+ };
19
+
20
+ export function activate(coda) {
21
+ coda.views.register('files', { open: view => serve(coda, view, sessionOfTree) });
22
+ coda.views.register('file', { open: view => serve(coda, view, sessionOfFile) });
23
+ }
24
+
25
+ const validRequestId = id => (typeof id === 'string' && id.length <= REQUEST_ID_MAX) || Number.isSafeInteger(id);
26
+ const errorOf = error => (error?.protocol ? { code: error.code, message: error.message } : { code: ErrorCode.IO, message: '读取失败' });
27
+
28
+ function serve(coda, view, sessionOf) {
29
+ // 代次:每换一次会话加 1,所有异步出口回来后都要核对。
30
+ const s = {
31
+ session: sessionOf(view.context), generation: 1, root: NO_ROOT, sent: null,
32
+ hello: false, closed: false, rootSeq: 0, poll: null,
33
+ };
34
+ const turns = new Map();
35
+ const stale = generation => s.closed || generation !== s.generation;
36
+
37
+ function post(message) {
38
+ if (s.closed) return;
39
+ if (messageSize(message) > MESSAGE_LIMIT) {
40
+ // 兜底:各模块自己守着上限,到这里说明有遗漏;有请求号的换成错误,其余丢弃。
41
+ if (message.requestId === undefined) return;
42
+ const { type, generation, requestId } = message;
43
+ message = { type, generation, requestId, error: { code: ErrorCode.TOO_LARGE, message: '回答超过消息上限' } };
44
+ }
45
+ try { view.postMessage(message); } catch { /* 视图已关 */ }
46
+ }
47
+
48
+ const postSession = () => post({ type: Type.SESSION, generation: s.generation, session: s.session });
49
+ function postRoot() {
50
+ s.sent = s.root;
51
+ post({ type: Type.ROOT, generation: s.generation, state: s.root.state, path: s.root.path });
52
+ }
53
+
54
+ // 重新查询根目录。结果和上次推给页面的不同才推送;查询失败保留上一次的。
55
+ // 被后发的查询取代的,结果丢掉,由后发的那个去推。
56
+ async function refreshRoot() {
57
+ const { generation } = s, seq = ++s.rootSeq;
58
+ let next = null;
59
+ try { next = await queryRoot(coda, s.session); } catch { /* 保留上一次 */ }
60
+ if (stale(generation) || seq !== s.rootSeq) return;
61
+ if (next) s.root = next;
62
+ syncPoll();
63
+ if (s.hello && !sameRoot(s.sent, s.root)) postRoot();
64
+ }
65
+
66
+ const stopPoll = () => { if (s.poll) clearTimeout(s.poll.handle); s.poll = null; };
67
+ // 只有当前代次、没关闭、状态是 preparing 时才挂着一个计时器。
68
+ function syncPoll() {
69
+ if (s.closed || !s.session || s.root.state !== RootState.PREPARING) return stopPoll();
70
+ if (s.poll?.generation === s.generation) return;
71
+ stopPoll();
72
+ const { generation } = s;
73
+ const poll = { generation, handle: setTimeout(() => {
74
+ if (s.poll === poll) s.poll = null;
75
+ if (!stale(generation)) refreshRoot();
76
+ }, POLL_MS) };
77
+ s.poll = poll;
78
+ }
79
+
80
+ function switchSession(session) {
81
+ stopPoll();
82
+ s.session = session ?? null;
83
+ s.generation++;
84
+ s.root = NO_ROOT;
85
+ s.sent = null;
86
+ if (s.hello) postSession();
87
+ refreshRoot();
88
+ }
89
+
90
+ async function request(message) {
91
+ const { type, generation, requestId } = message;
92
+ if (stale(generation) || !validRequestId(requestId)) return;
93
+ const reply = type === Type.LIST ? Type.LISTED : Type.CHUNK;
94
+ try {
95
+ if (s.root.state !== RootState.READY) throw fail(ErrorCode.NO_ROOT, '没有可浏览的工作目录');
96
+ const body = type === Type.LIST
97
+ ? await listDirectory(s.root.path, message.path, message.cursor)
98
+ : await readChunk(s.root.path, message.path, message.offset);
99
+ if (!stale(generation)) post({ type: reply, generation, requestId, ...body });
100
+ } catch (error) {
101
+ if (!stale(generation)) post({ type: reply, generation, requestId, error: errorOf(error) });
102
+ }
103
+ }
104
+
105
+ view.onMessage(async message => {
106
+ if (!message || typeof message !== 'object') return;
107
+ switch (message.type) {
108
+ // 页面(重新)就绪:它什么都不知道,所以不管有没有变,下一次查询的结果都要推。
109
+ case Type.HELLO: s.hello = true; s.sent = null; postSession(); return refreshRoot();
110
+ case Type.REFRESH: return stale(message.generation) ? undefined : refreshRoot();
111
+ case Type.LIST: case Type.READ: return request(message);
112
+ }
113
+ });
114
+
115
+ view.onContext(context => {
116
+ const session = sessionOf(context);
117
+ if (session !== s.session) switchSession(session);
118
+ });
119
+
120
+ // 这个会话一轮开始或结束:根目录先更新;结束时再通知页面重新取。
121
+ const watching = Promise.resolve(coda.watch('threads', ({ type, id, object }) => {
122
+ const before = turns.get(id), after = type === 'DELETED' ? undefined : object?.turn ?? null;
123
+ if (after === undefined) turns.delete(id); else turns.set(id, after);
124
+ if (s.closed || id !== s.session || before === undefined || after === undefined || before === after) return;
125
+ const { generation } = s;
126
+ const refreshed = refreshRoot();
127
+ if (before) refreshed.then(() => { if (s.hello && !stale(generation)) post({ type: Type.CHANGED, generation }); });
128
+ })).catch(() => null);
129
+
130
+ view.onClose(async () => {
131
+ s.closed = true;
132
+ stopPoll();
133
+ try { await (await watching)?.dispose(); } catch { /* 已经取消 */ }
134
+ });
135
+
136
+ refreshRoot();
137
+ }
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
+ "id": "resource-manager",
2
3
  "name": "@coda-console/resource-manager",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
4
+ "displayName": "资源管理器",
5
+ "description": "会话旁的只读文件浏览器:浏览会话工作目录,预览文本和图片,把路径交给对话。",
6
+ "version": "0.3.0",
7
+ "categories": ["工作流程"],
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/sealiu1997/coda_plugins_resource_manager.git"
12
+ },
13
+ "publishConfig": { "access": "public" },
14
+ "files": ["main.mjs", "lib", "view"],
15
+ "scripts": {
16
+ "test": "node --test test/*.test.mjs",
17
+ "check:coda": "npx --yes @coda-console/plugin-check@0.5.31 . --manifest"
18
+ },
19
+ "main": "main.mjs",
20
+ "contributes": {
21
+ "views": [
22
+ { "id": "files", "title": "资源管理器", "entry": "view/index.html", "in": ["side"], "icon": "folder" },
23
+ { "id": "file", "title": "文件", "entry": "view/file.html" }
24
+ ]
25
+ }
26
+ }