agent-loft 0.2.132 → 0.2.133
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 +3 -0
- package/package.json +1 -1
- package/src/channels/web/memo-storage.ts +29 -2
- package/src/channels/web/memos-api.ts +34 -3
- package/src/core/memo-index.ts +1 -1
- package/src/core/memo-versions.ts +4 -0
- package/web/css/memos.css +138 -32
- package/web/lib/dom.js +4 -0
- package/web/lib/markdown.js +4 -2
- package/web/views/file-editor.js +16 -0
- package/web/views/memo-dialogs.js +18 -0
- package/web/views/memo-find.js +124 -0
- package/web/views/memo-organize.js +40 -0
- package/web/views/memo-outline.js +84 -0
- package/web/views/memos.js +448 -86
package/README.md
CHANGED
|
@@ -103,6 +103,9 @@ Logo 是白底的三向汇合:橘红、暖橙和珊瑚红三个单元,表达
|
|
|
103
103
|
- 管理员能用;成员要管理员在 设置 → 用户 里单独「开通工作区面板」。
|
|
104
104
|
- 分支栏下面可以获取、拉取、推送,用的是服务器上配置的 Git 凭据。拉取只做快进,推送从不强制。成员默认没有这个权限,要管理员再单独「开通 Git 远程操作」。
|
|
105
105
|
- **备忘录**。从侧栏进入「备忘录 → 连接笔记库」,选择 WebDAV 目录,或填写 S3 存储桶、区域和可选前缀。可连接多个笔记库,按文件夹浏览、新建和编辑 Markdown、TXT、JSON、YAML、CSV 等 UTF-8 文本。编辑时粘贴图片会写入当前文件夹的 `assets/`,并插入相对链接。拥有读写权限的成员可以实时合写同一文件,阅读视图也会跟随更新。预览支持相对链接。「版本历史」可以查看并恢复这条连接下每份文本最近 50 个修改前的版本,保存在 Agent Loft 的本地数据库。浏览器通过 Agent Loft 的 HTTP 接口提交修改,用长轮询接收协作更新。Obsidian 等外部客户端直接写入存储时,重新打开文件会读取最新正文,写回冲突时可保留草稿并另存副本。文本上限 2 MB,每张图片上限 12 MB。连接凭据按账号保存在服务端,内网端点可用管理员账号连接。
|
|
106
|
+
- **快速打开**。在文件树点放大镜,或按 `⌘/Ctrl + O`,按文件名或路径查找。空搜索显示此浏览器里的收藏和最近笔记。点「搜正文」可查看命中片段并跳转。已有全文索引时使用索引,其余库按需从存储读取,关闭面板可取消。
|
|
107
|
+
- **清单和双链**。可写库支持在阅读模式直接勾选 Markdown 待办;支持 `[[笔记]]`、`[[笔记#章节]]`、`[[笔记|显示文字]]`。
|
|
108
|
+
- **整理文件**。在目录右键或长按文件,可以修改名称、移动到已有目录,或移入库内的 `.agent-loft-trash`。整理面板会列出可能需要更新的引用。在「笔记库设置 → 回收站」选择文件可恢复原路径;目标重名时会提示冲突。
|
|
106
109
|
- **共享给成员**。在「备忘录」选中自己的笔记库,点击「共享给成员」,为同一台 Agent Loft 上的账号选择「只读」或「读写」并保存。对方重新进入「备忘录」即可看到共享库,无需填写存储凭据。只读成员可以查看文件和版本历史,读写成员还可以编辑和新建文件。只有库主可以修改共享范围。把成员改为「关闭」即可撤回访问。成员的专家不会自动获得权限。
|
|
107
110
|
- **让智能体查笔记或改笔记**。在「设置 → 专家」中新建或编辑专家,在「权限和能力 → 备忘录权限」为每个自己的笔记库选择「沿用笔记库默认」「关闭」「只读」或「读写」,随专家一起保存。内置专家在卡片的「设置」里配置,别人共享的专家在「我的工具」里配置。库的默认权限和全文索引仍在「备忘录 → 智能体访问」里管理。已有库沿用原设置,新库默认关闭。备忘录工具随个人对话自动接入,直接在聊天里说「帮我搜笔记」即可。授权后可在自己的对话里按文件名和正文搜索、浏览目录、读取笔记;读写库还支持新建、追加和精确替换一处原文。只有你表达了留存或更新笔记的意图,智能体才会写入。比如你说「把刚才的排查过程留着下次用」,智能体会根据对话整理正文,优先更新已有笔记,也可以自己起名新建,并在保存后告诉你位置。保存后,正在编辑的网页会收到更新,原文可在版本历史中查看。计划模式只读。
|
|
108
111
|
- 授权后服务器在本地建立全文索引。网页和智能体保存后更新索引;外部修改每 10 分钟扫描一次,也可以在「智能体访问」里查看进度和最近成功时间,或立即刷新。首次扫描期间搜索只覆盖已收录内容。默认权限和所有专家权限都关闭后,或断开连接时,会清除该库的搜索索引。
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@ export const TEXT_LIMIT = 2 * 1024 * 1024;
|
|
|
16
16
|
export const IMAGE_LIMIT = 12 * 1024 * 1024;
|
|
17
17
|
export const editable = (path: string) => /\.(?:md|markdown|txt|json|ya?ml|toml|ini|csv|tsv|log|css|js|ts|xml|html)$/i.test(path);
|
|
18
18
|
const encode = (path: string) => path.split("/").map(s => encodeURIComponent(s).replace(/[!'()*]/g, c => "%" + c.charCodeAt(0).toString(16).toUpperCase())).join("/");
|
|
19
|
-
type Reply = { status: number; body: Buffer; etag?: string };
|
|
19
|
+
type Reply = { status: number; body: Buffer; etag?: string; contentType?: string };
|
|
20
20
|
const conflict = () => new HttpError(409, "文件已存在或已被其他设备修改。请保留草稿并重新打开远端版本。");
|
|
21
21
|
function checked(r: Reply) {
|
|
22
22
|
if (r.status === 412 || r.status === 409) throw conflict();
|
|
@@ -79,7 +79,7 @@ export class MemoStorage {
|
|
|
79
79
|
if (size > limit) throw new HttpError(413, "文件或目录超出预览大小限制");
|
|
80
80
|
chunks.push(chunk);
|
|
81
81
|
}
|
|
82
|
-
resolve({ body: Buffer.concat(chunks), status: res.statusCode ?? 502, etag: res.headers.etag });
|
|
82
|
+
resolve({ body: Buffer.concat(chunks), status: res.statusCode ?? 502, etag: res.headers.etag, contentType: res.headers["content-type"] });
|
|
83
83
|
} catch (err) { res.destroy(); reject(err); }
|
|
84
84
|
});
|
|
85
85
|
req.on("error", reject);
|
|
@@ -159,6 +159,33 @@ export class MemoStorage {
|
|
|
159
159
|
return revision({ ...result, body: bytes });
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
+
async moveFile(from: string, to: string, check: () => unknown) {
|
|
163
|
+
const original = this.objectURL(from), target = this.objectURL(to);
|
|
164
|
+
const parent = from.split("/").slice(0, -1).join("/");
|
|
165
|
+
const name = from.split("/").pop();
|
|
166
|
+
let cursor = "", found = false;
|
|
167
|
+
const seen = new Set<string>();
|
|
168
|
+
do {
|
|
169
|
+
if (seen.has(cursor)) throw new HttpError(502, "目录分页重复");
|
|
170
|
+
seen.add(cursor);
|
|
171
|
+
const page = await this.list(parent, cursor);
|
|
172
|
+
found = page.entries.some(entry => entry.name === name && !entry.directory);
|
|
173
|
+
cursor = page.next;
|
|
174
|
+
} while (!found && cursor);
|
|
175
|
+
if (!found) throw new HttpError(404, "文件已移动或不存在");
|
|
176
|
+
check();
|
|
177
|
+
if (this.source.kind === "webdav") {
|
|
178
|
+
checked(await this.request("MOVE", original, { destination: target.href, overwrite: "F" }));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const file = checked(await this.request("GET", original, {}, undefined, IMAGE_LIMIT));
|
|
182
|
+
if (!file.etag || !/^"[^"\r\n]*"$/.test(file.etag)) throw new HttpError(409, "存储未提供可校验的版本,无法移动文件");
|
|
183
|
+
check();
|
|
184
|
+
checked(await this.request("PUT", target, { "if-none-match": "*", "content-type": file.contentType ?? "application/octet-stream" }, file.body));
|
|
185
|
+
try { check(); checked(await this.request("DELETE", original, { "if-match": file.etag })); }
|
|
186
|
+
catch { throw new HttpError(409, "目标副本已建立,原文件移除未确认,请刷新目录核对两处文件"); }
|
|
187
|
+
}
|
|
188
|
+
|
|
162
189
|
async mkdir(path: string, existingOK = false) {
|
|
163
190
|
if (this.source.kind === "s3") {
|
|
164
191
|
try { await this.put(path + "/", Buffer.alloc(0), "application/x-directory", null); }
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { memoFile } from "./memo-file.js";
|
|
2
|
-
import { clearMemoIndex, memoIndexStatus, refreshMemoIndex, syncMemoIndexAccess } from "../../core/memo-index.js";
|
|
2
|
+
import { clearMemoIndex, memoIndexStatus, refreshMemoIndex, searchMemos, syncMemoIndexAccess } from "../../core/memo-index.js";
|
|
3
3
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
import { invalidateMemo, joinMemo, pullMemo, pushMemo, withMemoLock } from "./memo-collab.js";
|
|
6
6
|
import { posix } from "node:path";
|
|
7
7
|
import { addMemoSource, canReadMemo, parseMemoAccess, setMemoAccess, memberAccess, memoHistoryKey, memoSources, parseMemoSource, sourceSettings, removeMemoSource, setMemoSharing, validMemoPath, visibleMemoSources, type MemoAccess } from "../../core/memo-sources.js";
|
|
8
8
|
import { canUseExpert } from "../../core/experts.js";
|
|
9
|
-
import { memoVersions, memoVersionText } from "../../core/memo-versions.js";
|
|
9
|
+
import { moveMemoVersions, memoVersions, memoVersionText } from "../../core/memo-versions.js";
|
|
10
10
|
import { sniff } from "../../core/uploads.js";
|
|
11
11
|
import { getUser, isAdmin, listUsers, type User } from "../../core/users.js";
|
|
12
12
|
import { HttpError, readBody, readJsonBody, SECURITY_HEADERS, sendJson } from "./http.js";
|
|
@@ -50,7 +50,7 @@ export async function handleMemosApi(req: IncomingMessage, res: ServerResponse,
|
|
|
50
50
|
if (!mine) {
|
|
51
51
|
const permission = memberAccess(source, user.id);
|
|
52
52
|
if (!permission) throw new HttpError(404, "笔记库不存在或共享已撤回");
|
|
53
|
-
if (!["tree", "file", "changes", "history", "image", "folder", "attachment"].includes(action)) throw new HttpError(403, "只有库主可以管理笔记库");
|
|
53
|
+
if (!["tree", "file", "changes", "history", "image", "folder", "attachment", "organize", "search"].includes(action)) throw new HttpError(403, "只有库主可以管理笔记库");
|
|
54
54
|
if (writing && permission !== "write") throw new HttpError(403, "此笔记库共享为只读");
|
|
55
55
|
}
|
|
56
56
|
return source;
|
|
@@ -100,8 +100,39 @@ export async function handleMemosApi(req: IncomingMessage, res: ServerResponse,
|
|
|
100
100
|
}
|
|
101
101
|
const storage = new MemoStorage(source, isAdmin(owner));
|
|
102
102
|
const path = url.searchParams.get("path") ?? "";
|
|
103
|
+
if (action === "search" && method === "GET") {
|
|
104
|
+
const ready = canReadMemo(source) && !!memoIndexStatus(owner.id, source.id).success;
|
|
105
|
+
const hits = ready ? searchMemos(owner.id, [source], url.searchParams.get("q") ?? "", "").hits : [];
|
|
106
|
+
return sendJson(res, 200, {ready, hits});
|
|
107
|
+
}
|
|
103
108
|
const listing = action === "tree" && method === "GET";
|
|
104
109
|
if (!(listing && path === "") && !validMemoPath(path)) throw new HttpError(400, "笔记路径无效");
|
|
110
|
+
if (action === "organize" && method === "POST") {
|
|
111
|
+
const body = await readJsonBody(req, 16_384);
|
|
112
|
+
const bin = ".agent-loft-trash/";
|
|
113
|
+
const trashed = path.startsWith(bin);
|
|
114
|
+
let dest: string;
|
|
115
|
+
if (body?.op === "restore" && trashed && /^\.agent-loft-trash\/[0-9a-f-]{36}\/.+/.test(path)) dest = path.split("/").slice(2).join("/");
|
|
116
|
+
else if (body?.op === "trash" && !trashed) dest = bin + randomUUID() + "/" + path;
|
|
117
|
+
else if (body?.op === "move" && !trashed && typeof body.to === "string" && !body.to.startsWith(bin)) dest = body.to;
|
|
118
|
+
else throw new HttpError(400, "文件操作无效");
|
|
119
|
+
if (!validMemoPath(dest) || dest === path || dest === ".agent-loft-trash") throw new HttpError(400, "目标路径无效");
|
|
120
|
+
const keys = [storage.objectURL(path).href, storage.objectURL(dest).href].sort();
|
|
121
|
+
await withMemoLock(keys[0]!, () => withMemoLock(keys[1]!, async () => {
|
|
122
|
+
checkAccess();
|
|
123
|
+
if (body.op !== "move") {
|
|
124
|
+
const parts = dest.split("/");
|
|
125
|
+
for (let n = 1; n < parts.length; n++) { checkAccess(); await storage.mkdir(parts.slice(0,n).join("/"), true); }
|
|
126
|
+
}
|
|
127
|
+
await storage.moveFile(path, dest, checkAccess);
|
|
128
|
+
moveMemoVersions(owner.id, memoHistoryKey(source), path, dest);
|
|
129
|
+
for (const file of [path, dest]) {
|
|
130
|
+
if (editable(file)) invalidateMemo(memoFile(user,source,file,checkAccess,owner).key);
|
|
131
|
+
}
|
|
132
|
+
}));
|
|
133
|
+
if (canReadMemo(source)) void refreshMemoIndex(owner.id, source);
|
|
134
|
+
return sendJson(res, 200, { path: dest });
|
|
135
|
+
}
|
|
105
136
|
if (listing) {
|
|
106
137
|
const page = await storage.list(path, url.searchParams.get("cursor") ?? "");
|
|
107
138
|
checkAccess();
|
package/src/core/memo-index.ts
CHANGED
|
@@ -56,7 +56,7 @@ async function* listTextFiles(storage: MemoStorage, current: () => boolean) {
|
|
|
56
56
|
const page = await storage.list(folder, cursor);
|
|
57
57
|
for (const entry of page.entries) {
|
|
58
58
|
const path = folder ? folder + "/" + entry.name : entry.name;
|
|
59
|
-
if (!validMemoPath(path)) continue;
|
|
59
|
+
if (path === ".agent-loft-trash" || !validMemoPath(path)) continue;
|
|
60
60
|
if (entry.directory) folders.add(path);
|
|
61
61
|
else if (editable(path)) yield { path, etag: entry.etag };
|
|
62
62
|
}
|
|
@@ -24,3 +24,7 @@ export function memoVersionText(owner: string, source: string, path: string, id:
|
|
|
24
24
|
const row = sql(`SELECT content FROM memo_versions WHERE ${scope} AND id = ?`).get(owner, source, path, id);
|
|
25
25
|
return row ? gunzipSync(row.content as Uint8Array).toString("utf8") : null;
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
export function moveMemoVersions(owner: string, source: string, from: string, to: string) {
|
|
29
|
+
sql("UPDATE memo_versions SET path = ? WHERE " + scope).run(to, owner, source, from);
|
|
30
|
+
}
|
package/web/css/memos.css
CHANGED
|
@@ -1,37 +1,114 @@
|
|
|
1
|
-
.main
|
|
2
|
-
.memo-page
|
|
1
|
+
.main:has(.memo-page) > .topbar { position: sticky; padding-bottom: 8px; background: var(--bg); }
|
|
2
|
+
.main:has(.memo-page) > .topbar::before, .main:has(.memo-page) > .topbar::after { display: none; }
|
|
3
|
+
.main .messages-inner:has(> .memo-page) { max-width: none; height: 100%; padding: 0; }
|
|
4
|
+
.memo-page {
|
|
5
|
+
--memo-accent: #7154ab;
|
|
6
|
+
--memo-tint: color-mix(in srgb, var(--memo-accent) 12%, var(--bg-side));
|
|
7
|
+
--memo-side-bg: color-mix(in srgb, var(--memo-accent) 4%, var(--bg-side));
|
|
8
|
+
--memo-measure: 680px;
|
|
9
|
+
--memo-gutter: clamp(18px, 5%, 64px);
|
|
10
|
+
--memo-page-top: clamp(26px, 5vh, 60px);
|
|
11
|
+
flex: 1;
|
|
12
|
+
min-height: 0;
|
|
13
|
+
display: grid;
|
|
14
|
+
position: relative;
|
|
15
|
+
isolation: isolate;
|
|
16
|
+
grid-template-columns: var(--memo-tree-width, 248px) minmax(0, 1fr);
|
|
17
|
+
overflow: hidden;
|
|
18
|
+
border-top: 1px solid var(--border);
|
|
19
|
+
}
|
|
20
|
+
@media (prefers-color-scheme: dark) {
|
|
21
|
+
.memo-page { --memo-accent: #b59ae8; }
|
|
22
|
+
}
|
|
3
23
|
.memo-page.no-source { grid-template-columns: 1fr; }
|
|
4
24
|
.memo-side, .memo-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
|
|
5
|
-
.memo-side { background: var(--
|
|
6
|
-
.memo-
|
|
7
|
-
.memo-
|
|
8
|
-
.memo-
|
|
9
|
-
.memo-
|
|
10
|
-
.memo-
|
|
11
|
-
.memo-tree .memo-
|
|
12
|
-
.memo-tree
|
|
13
|
-
.memo-
|
|
14
|
-
.memo-
|
|
15
|
-
.memo-
|
|
25
|
+
.memo-side { position: relative; background: var(--memo-side-bg); border-right: 1px solid var(--border); }
|
|
26
|
+
.memo-scrim { display: none; }
|
|
27
|
+
.memo-divider { position: absolute; inset: 0 -5px 0 auto; width: 9px; z-index: 2; cursor: col-resize; touch-action: none; }
|
|
28
|
+
.memo-divider::after { content: ""; position: absolute; inset: 0 3px; }
|
|
29
|
+
.memo-divider:hover::after, .memo-divider:focus-visible::after, .resizing .memo-divider::after { background: var(--memo-accent); }
|
|
30
|
+
.memo-page.resizing { user-select: none; cursor: col-resize; }
|
|
31
|
+
.memo-tree-tools, .memo-bar, .memo-sources, .memo-footer { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
|
|
32
|
+
.memo-tree-tools, .memo-bar { min-height: 46px; padding: 5px 12px; border-bottom: 1px solid var(--border); }
|
|
33
|
+
.memo-tree-tools strong { flex: 1; font-size: 13px; font-weight: 600; }
|
|
34
|
+
.memo-page .icon-btn { width: 29px; height: 29px; flex-shrink: 0; border-radius: 5px; }
|
|
35
|
+
.memo-panel-button svg { transform: scaleX(-1); }
|
|
36
|
+
.memo-page .icon-btn svg, .memo-page .btn svg, .memo-settings svg, .memo-bar svg { width: 16px; height: 16px; }
|
|
37
|
+
.memo-tree { flex: 1; overflow: auto; padding: 10px 8px; }
|
|
38
|
+
.memo-tree .memo-branch { margin-left: 12px; padding-left: 4px; border-left: 1px solid var(--border); }
|
|
39
|
+
.memo-branch > small { color: var(--fg-soft); padding: 4px 9px; font-size: 12px; }
|
|
40
|
+
.memo-row { position: relative; display: flex; align-items: center; gap: 7px; text-align: left; width: 100%; min-height: 32px; padding: 4px 9px; border-radius: 5px; font-size: 13px; line-height: 1.45; color: var(--fg-soft); }
|
|
41
|
+
.memo-extension { color: var(--fg-soft); font-size: .86em; font-weight: 400; }
|
|
42
|
+
.memo-row span:last-child, .memo-path, .memo-breadcrumb > * { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
43
|
+
.memo-row svg { width: 15px; height: 15px; }
|
|
44
|
+
.memo-row > span:first-of-type { flex-shrink: 0; }
|
|
16
45
|
.memo-row:hover { background: var(--bg-hover); }
|
|
17
|
-
.memo-row.
|
|
18
|
-
.memo-row
|
|
19
|
-
.memo-row[aria-
|
|
20
|
-
.memo-
|
|
21
|
-
|
|
22
|
-
.memo-
|
|
46
|
+
.memo-row.current-dir, .memo-root { color: var(--fg); font-weight: 600; }
|
|
47
|
+
.memo-row.selected { background: var(--memo-tint); color: var(--memo-accent); font-weight: 600; }
|
|
48
|
+
.memo-row[aria-current="page"]::before { content: ""; position: absolute; left: 0; inset-block: 7px; width: 3px; border-radius: 3px; background: var(--memo-accent); }
|
|
49
|
+
.memo-row[aria-busy="true"]::after { content: ""; flex: none; margin-left: auto; width: 9px; height: 9px; border: 1.5px solid var(--border); border-top-color: var(--memo-accent); border-radius: 50%; }
|
|
50
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
51
|
+
.memo-row[aria-busy="true"]::after { animation: memo-loading .75s linear infinite; }
|
|
52
|
+
@keyframes memo-loading { to { transform: rotate(1turn); } }
|
|
53
|
+
}
|
|
54
|
+
.memo-row[aria-expanded]::before { content: ""; width: 5px; height: 5px; flex: none; border: solid currentColor; border-width: 1px 1px 0 0; transform: rotate(45deg); }
|
|
55
|
+
.memo-row[aria-expanded=true]::before { transform: rotate(135deg); }
|
|
56
|
+
.memo-sources { border-top: 1px solid var(--border); padding: 9px 10px; }
|
|
57
|
+
.memo-sources > span { padding: 6px; border-radius: 6px; background: var(--memo-tint); color: var(--memo-accent); }
|
|
58
|
+
.memo-sources svg { width: 16px; height: 16px; }
|
|
59
|
+
.memo-sources select.input { flex: 1; min-width: 0; height: 30px; line-height: normal; border: 0; border-radius: 4px; font-size: 13px; font-weight: 600; background-color: transparent; padding: 0 21px 0 5px; background-position: right 4px center; }
|
|
60
|
+
.memo-sources select.input:hover { background-color: var(--bg-hover); }
|
|
61
|
+
.memo-page :is(button, select):focus-visible { outline: 2px solid var(--memo-accent); outline-offset: -2px; }
|
|
62
|
+
.memo-settings button { display: flex; align-items: center; gap: 9px; }
|
|
63
|
+
.memo-outline { width: min(288px, calc(100vw - 16px)); }
|
|
64
|
+
.memo-outline nav button { display: block; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
65
|
+
.memo-outline button[aria-current="location"] { background: var(--ask-bg); color: var(--fg); font-weight: 600; }
|
|
66
|
+
.memo-paper :is(h1,h2,h3,h4,h5,h6):focus-visible { outline: 2px solid var(--memo-accent); outline-offset: 3px; border-radius: 2px; }
|
|
67
|
+
.memo-bar { color: var(--fg-soft); font-size: 13px; padding-inline: 16px; gap: 8px; }
|
|
68
|
+
.memo-modes { display: inline-flex; flex: none; gap: 2px; padding: 3px; border-radius: 8px; background: var(--bg-soft); }
|
|
69
|
+
.memo-modes button { display: inline-flex; align-items: center; justify-content: center; gap: 5px; min-height: 26px; padding: 3px 9px; border-radius: 5px; font-size: 12px; color: var(--fg-soft); }
|
|
70
|
+
.memo-modes button:hover { background: var(--bg-hover); }
|
|
71
|
+
.memo-modes button[aria-pressed="true"] { color: var(--memo-accent); font-weight: 600; background: var(--bg); box-shadow: 0 1px 3px #0002; }
|
|
72
|
+
.memo-breadcrumb { min-width: 0; flex: 1; display: flex; align-items: baseline; gap: 8px; }
|
|
73
|
+
.memo-breadcrumb > span:first-child { flex: 0 2 auto; }
|
|
74
|
+
.memo-breadcrumb strong { color: var(--fg); font-weight: 550; }
|
|
75
|
+
.memo-footer .btn { min-height: 28px; padding: 3px 8px; border-radius: 5px; font-size: 12px; }
|
|
76
|
+
.memo-footer { min-height: 27px; padding: 2px 12px; justify-content: flex-end; border-top: 1px solid var(--border); }
|
|
77
|
+
.memo-status { display: inline-flex; align-items: center; gap: 6px; color: var(--fg-soft); font-size: 12px; }
|
|
78
|
+
.memo-status:not(:empty)::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--fg-faint); }
|
|
79
|
+
.memo-status[data-state="saved"]::before { background: var(--ok); }
|
|
80
|
+
.memo-status[data-state="pending"]::before { background: var(--memo-accent); }
|
|
81
|
+
.memo-status[data-state="error"] { color: var(--danger); font-weight: 600; }
|
|
82
|
+
.memo-status[data-state="error"]::before { background: currentColor; }
|
|
23
83
|
.memo-editor, .memo-reading { flex: 1; min-height: 0; overflow: auto; }
|
|
24
84
|
.memo-editor .cm-editor { height: 100%; background: var(--bg); }
|
|
25
|
-
.memo-editor .cm-content { padding:
|
|
85
|
+
.memo-editor .cm-content { padding: 18px 8px 40px; }
|
|
26
86
|
.memo-editor .cm-scroller { font-family: var(--mono); }
|
|
27
|
-
.memo-reading { padding:
|
|
28
|
-
.memo-
|
|
87
|
+
.memo-reading { padding: var(--memo-page-top) var(--memo-gutter) 64px; overflow-wrap: anywhere; }
|
|
88
|
+
.memo-paper, .memo-reading > pre { max-width: var(--memo-measure); margin: 0 auto; }
|
|
89
|
+
.memo-paper, .memo-prose .cm-scroller { font: 16px/1.8 var(--font); }
|
|
90
|
+
.memo-prose .cm-content { padding: var(--memo-page-top) max(var(--memo-gutter), calc((100% - var(--memo-measure)) / 2)) 64px; caret-color: var(--memo-accent); }
|
|
91
|
+
.memo-prose .cm-gutters { display: none; }
|
|
92
|
+
.memo-prose .cm-line { padding: 0; }
|
|
93
|
+
.memo-prose .cm-editor .cm-activeLine { background: transparent; }
|
|
94
|
+
.memo-prose .cm-editor.cm-focused { outline: none; }
|
|
95
|
+
.memo-note-title, .memo-paper .md > h1:first-child { font-size: clamp(26px, 2.6vw, 36px); line-height: 1.25; letter-spacing: -.03em; margin: 0 0 .85em; padding-bottom: .55em; border-bottom: 1px solid var(--border); }
|
|
96
|
+
.memo-paper .md :is(h2, h3):not(:first-child) { margin-top: 1.55em; }
|
|
97
|
+
.memo-paper .md blockquote { margin: 1em 0; padding: .55em 1em; border-left-color: var(--memo-accent); border-radius: 0 5px 5px 0; background: color-mix(in srgb, var(--memo-accent) 6%, var(--bg)); }
|
|
98
|
+
.memo-paper .md blockquote > :first-child { margin-top: 0; }
|
|
99
|
+
.memo-paper .md blockquote > :last-child { margin-bottom: 0; }
|
|
100
|
+
.memo-paper .md li::marker { color: var(--memo-accent); }
|
|
101
|
+
.memo-paper .md a { color: var(--memo-accent); }
|
|
29
102
|
.memo-reading pre { white-space: pre-wrap; }
|
|
30
103
|
.memo-reading img { max-width: 100%; height: auto; }
|
|
31
|
-
.memo-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap:
|
|
32
|
-
.memo-empty >
|
|
33
|
-
.memo-empty
|
|
34
|
-
.memo-empty
|
|
104
|
+
.memo-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 20px; text-align: center; }
|
|
105
|
+
.memo-empty > span { background: var(--memo-tint); color: var(--memo-accent); padding: 12px; border-radius: 12px; }
|
|
106
|
+
.memo-empty svg { width: 24px; height: 24px; }
|
|
107
|
+
.memo-empty h2 { font-size: 19px; font-weight: 600; margin: 6px 0 0; }
|
|
108
|
+
.memo-empty p { font-size: 13px; margin: 0; color: var(--fg-soft); }
|
|
109
|
+
.memo-empty .btn { margin-top: 8px; }
|
|
110
|
+
.memo-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
|
|
111
|
+
.memo-fields p { margin: 0; }
|
|
35
112
|
.memo-fields, .memo-fields label { display: flex; flex-direction: column; gap: 5px; }
|
|
36
113
|
.memo-fields { gap: 12px; }
|
|
37
114
|
.memo-fields label { font-size: 13px; }
|
|
@@ -39,14 +116,43 @@
|
|
|
39
116
|
.memo-fields .memo-expert-access { display: grid; grid-template-columns: minmax(0, 1fr) minmax(8em, 40%); align-items: center; overflow-wrap: anywhere; }
|
|
40
117
|
.memo-fields .btn { align-self: flex-end; }
|
|
41
118
|
.memo-error { color: var(--danger); }
|
|
42
|
-
.memo-page [hidden], .memo-fields[hidden], .memo-
|
|
119
|
+
.memo-page [hidden], .memo-fields[hidden], .memo-page .memo-mobile, .memo-page .memo-return { display: none; }
|
|
120
|
+
@media (min-width: 769px) {
|
|
121
|
+
.memo-page.has-file.tree-collapsed { grid-template-columns: minmax(0, 1fr); }
|
|
122
|
+
.memo-page.has-file.tree-collapsed .memo-side { display: none; }
|
|
123
|
+
}
|
|
43
124
|
@media (max-width: 768px) {
|
|
44
|
-
.
|
|
45
|
-
.memo-page { display: flex; flex-direction: column; }
|
|
125
|
+
.memo-page { display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }
|
|
46
126
|
.memo-side, .memo-pane { flex: 1; }
|
|
47
|
-
.memo-side { display: none;
|
|
48
|
-
.memo-
|
|
49
|
-
.memo-page.show-tree:not(
|
|
127
|
+
.memo-side, .memo-divider, .memo-page .memo-desktop { display: none; }
|
|
128
|
+
.memo-side { border-right: none; }
|
|
129
|
+
.memo-page.show-tree .memo-side:not([hidden]) { display: flex; }
|
|
130
|
+
.memo-page .memo-mobile, .memo-page.has-file .memo-return { display: inline-grid; }
|
|
131
|
+
.memo-page.show-tree:not(.no-source):not(.has-file) .memo-pane { display: none; }
|
|
132
|
+
.memo-page.has-file.show-tree .memo-side { position: absolute; inset: 0 auto 0 0; z-index: 4; width: min(82%, 320px); box-shadow: 6px 0 22px #0002; padding-bottom: env(safe-area-inset-bottom); }
|
|
133
|
+
.memo-page.has-file.show-tree .memo-scrim { display: block; position: absolute; inset: 0; z-index: 3; background: #19162252; cursor: default; }
|
|
134
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
135
|
+
.memo-page.has-file.show-tree .memo-side { animation: memo-drawer-in 150ms ease-out; }
|
|
136
|
+
@keyframes memo-drawer-in { from { transform: translateX(-100%); } }
|
|
137
|
+
}
|
|
138
|
+
.memo-page .icon-btn { width: 44px; height: 44px; }
|
|
139
|
+
.memo-tree-tools { padding: 2px 6px; }
|
|
140
|
+
.memo-row { min-height: 42px; font-size: 14px; }
|
|
141
|
+
.memo-bar { gap: 4px; padding-inline: 6px; }
|
|
142
|
+
.memo-breadcrumb > span { display: none; }
|
|
143
|
+
.memo-modes button { gap: 0; min-width: 42px; min-height: 38px; padding: 4px; }
|
|
144
|
+
.memo-modes button svg { display: none; }
|
|
50
145
|
.memo-fields input, .memo-editor { font-size: 16px; }
|
|
51
146
|
}
|
|
52
147
|
.memo-snapshot { min-height: 5rem; max-height: 45vh; overflow: auto; margin: 0; padding: 12px; background: var(--bg-soft); white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
148
|
+
.memo-find-tools { display: flex; align-items: center; gap: 8px; }
|
|
149
|
+
.memo-find-tools .btn { white-space: nowrap; }
|
|
150
|
+
.memo-find-tools [aria-pressed=true], .memo-result .icon-btn[aria-pressed=true] { color: var(--link); background: var(--ask-bg); }
|
|
151
|
+
.memo-results, .memo-refs { max-height: 45vh; overflow: auto; }
|
|
152
|
+
.memo-result { display: flex; align-items: center; gap: 6px; padding: 4px; border-radius: 6px; }
|
|
153
|
+
.memo-result.active, .memo-result:hover { background: var(--bg-soft); }
|
|
154
|
+
.memo-found { flex: 1; min-width: 0; display: grid; text-align: start; padding: 4px 8px; overflow-wrap: anywhere; }
|
|
155
|
+
.memo-found small { color: var(--fg-soft); }
|
|
156
|
+
.memo-paper input[type=checkbox]:enabled { accent-color: var(--memo-accent); cursor: pointer; }
|
|
157
|
+
|
|
158
|
+
::highlight(memo-search) { background: #fee184; color: #241c0b; }
|
package/web/lib/dom.js
CHANGED
|
@@ -55,6 +55,10 @@ export const ICON = {
|
|
|
55
55
|
library: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="7" width="14" height="14" rx="2"/><path d="M7 3h12a2 2 0 0 1 2 2v12"/><path d="m3 17 4-4 3 3 2-2 5 5"/></svg>',
|
|
56
56
|
check: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="m5 12.5 4.5 4.5L19 7.5"/></svg>',
|
|
57
57
|
folder: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7.5A2.5 2.5 0 0 1 5.5 5h3.6l2 2.2h7.4A2.5 2.5 0 0 1 21 9.7v7.8a2.5 2.5 0 0 1-2.5 2.5h-13A2.5 2.5 0 0 1 3 17.5Z"/><path d="M3 10h18"/></svg>',
|
|
58
|
+
folderPlus: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z"/><path d="M12 10v7m-3.5-3.5h7"/></svg>',
|
|
59
|
+
bookmark: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"><path d="M6 4h12v17l-6-4-6 4z"/></svg>',
|
|
60
|
+
notebook: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="3" width="15" height="18" rx="2"/><path d="M9 3v18m3-13h5"/></svg>',
|
|
61
|
+
outline: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><path d="M4 6h3m4 0h9M7 12h3m4 0h6M4 18h3m4 0h9"/></svg>',
|
|
58
62
|
folderOpen: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 17.5V7.5A2.5 2.5 0 0 1 5.5 5h3.6l2 2.2h6.4A2.5 2.5 0 0 1 20 9.7V11"/><path d="M3 17.5 5.6 12a2 2 0 0 1 1.8-1.1h13a1 1 0 0 1 .9 1.4l-2.4 6.2a2.2 2.2 0 0 1-2 1.5H5.5A2.5 2.5 0 0 1 3 17.5Z"/></svg>',
|
|
59
63
|
file: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8Z"/><path d="M14 3v5h5"/></svg>',
|
|
60
64
|
panel: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M15 4v16"/></svg>',
|
package/web/lib/markdown.js
CHANGED
|
@@ -47,6 +47,7 @@ export function patchMarkdown(div, text, { live = false, enhance, prepare, sanit
|
|
|
47
47
|
n.remove();
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
+
let offset = blocks.at(-1)?.end ?? 0;
|
|
50
51
|
for (; i <= last; i++) {
|
|
51
52
|
const final = !(live && i === last);
|
|
52
53
|
const chunk = chunks[i];
|
|
@@ -58,14 +59,15 @@ export function patchMarkdown(div, text, { live = false, enhance, prepare, sanit
|
|
|
58
59
|
// Resolve vault links while the document is inert, before the browser fetches images.
|
|
59
60
|
const template = document.createElement("template");
|
|
60
61
|
template.innerHTML = html;
|
|
61
|
-
prepare(template.content);
|
|
62
|
+
prepare(template.content, { raw: raws[i], offset, chunk });
|
|
62
63
|
box.append(template.content);
|
|
63
64
|
} else box.innerHTML = html;
|
|
64
65
|
decorate(box, final);
|
|
65
66
|
enhance?.(box, { final });
|
|
66
67
|
const nodes = [...box.childNodes];
|
|
67
68
|
div.append(...nodes);
|
|
68
|
-
|
|
69
|
+
offset += raws[i].length;
|
|
70
|
+
blocks.push({ raw: raws[i], final, nodes, end: offset });
|
|
69
71
|
}
|
|
70
72
|
}
|
|
71
73
|
|
package/web/views/file-editor.js
CHANGED
|
@@ -167,6 +167,22 @@ export async function createEditor({ parent, text, name, onChange, onSave, colla
|
|
|
167
167
|
}
|
|
168
168
|
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: editorText(next) } });
|
|
169
169
|
},
|
|
170
|
+
toggleTask(offset, expected) {
|
|
171
|
+
const doc = view.state.doc;
|
|
172
|
+
// Marked counts normalized LF offsets. Mixed-ending editors retain literal CR characters.
|
|
173
|
+
if (eol === "mixed") {
|
|
174
|
+
const source = doc.toString();
|
|
175
|
+
let raw = 0, normalized = 0;
|
|
176
|
+
while (raw < source.length && normalized < offset) {
|
|
177
|
+
if (source[raw] === "\r" && source[raw+1] === "\n") raw++;
|
|
178
|
+
raw++; normalized++;
|
|
179
|
+
}
|
|
180
|
+
offset = raw;
|
|
181
|
+
}
|
|
182
|
+
if (offset < 1 || offset + 2 > doc.length || doc.sliceString(offset - 1, offset + 2) !== "[" + expected + "]") return false;
|
|
183
|
+
view.dispatch({ changes: { from: offset, to: offset + 1, insert: expected === " " ? "x" : " " } });
|
|
184
|
+
return true;
|
|
185
|
+
},
|
|
170
186
|
insertAtSelection: (text) => view.dispatch(view.state.replaceSelection(text)),
|
|
171
187
|
focus: () => view.focus(),
|
|
172
188
|
/** 编辑器被藏起来再放回来之后要重新量尺寸 */
|
|
@@ -129,6 +129,24 @@ export function connectSource(onConnect) {
|
|
|
129
129
|
name.focus();
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
export function confirmDisconnect(source) {
|
|
133
|
+
return new Promise(resolve => {
|
|
134
|
+
const input = h("input", { class: "input", autocomplete: "off", spellcheck: "false" });
|
|
135
|
+
const submit = h("button", { type: "submit", class: "btn danger-fill", disabled: true }, "断开连接");
|
|
136
|
+
const cancel = h("button", { type: "button", class: "btn ghost", onclick: () => finish(false) }, "取消");
|
|
137
|
+
const form = h("form", { class: "modal-body memo-fields" },
|
|
138
|
+
h("p", {}, "断开会移除连接配置,并撤回此库的成员共享和智能体访问权限。远端文件和本地版本历史会保留。"),
|
|
139
|
+
h("label", {}, "输入库名「" + source.name + "」确认", input),
|
|
140
|
+
h("div", { class: "memo-dialog-actions" }, cancel, submit));
|
|
141
|
+
const close = modal({ title: "断开笔记库", body: form, beforeClose: () => resolve(false) });
|
|
142
|
+
function finish(result) { close(); resolve(result); }
|
|
143
|
+
input.oninput = () => { submit.disabled = input.value !== source.name; };
|
|
144
|
+
form.onsubmit = e => { e.preventDefault(); if (input.value === source.name) finish(true); };
|
|
145
|
+
form.onkeydown = e => { if (e.key === "Escape") finish(false); };
|
|
146
|
+
input.focus();
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
132
150
|
export function askMemoName(title, initial, directory) {
|
|
133
151
|
return new Promise(resolve => {
|
|
134
152
|
const input = h("input", { class: "input", required: true, value: initial, "aria-label": "名称" });
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { api } from "../lib/api.js";
|
|
2
|
+
import { h, fill, icon } from "../lib/dom.js";
|
|
3
|
+
import { modal } from "../lib/overlay.js";
|
|
4
|
+
|
|
5
|
+
export const trashDir = ".agent-loft-trash";
|
|
6
|
+
const textFile = /\.(md|markdown|txt|json|ya?ml|toml|ini|csv|tsv|log|css|js|ts|xml|html)$/i;
|
|
7
|
+
export async function* vaultFiles(endpoint, signal, start = "") {
|
|
8
|
+
const dirs = new Set([start]);
|
|
9
|
+
for (const dir of dirs) {
|
|
10
|
+
const seen = new Set();
|
|
11
|
+
let cursor = "";
|
|
12
|
+
do {
|
|
13
|
+
if (seen.has(cursor)) throw new Error("目录分页重复");
|
|
14
|
+
seen.add(cursor);
|
|
15
|
+
const page = await api(endpoint("tree", dir, cursor), { signal });
|
|
16
|
+
for (const item of page.entries) {
|
|
17
|
+
const path = dir ? dir + "/" + item.name : item.name;
|
|
18
|
+
if (path === trashDir) continue;
|
|
19
|
+
if (item.directory) dirs.add(path);
|
|
20
|
+
else if (start === trashDir || textFile.test(path) || /\.(png|jpe?g|gif|webp)$/i.test(path)) yield path;
|
|
21
|
+
}
|
|
22
|
+
cursor = page.next;
|
|
23
|
+
} while (cursor);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function load(key) {
|
|
27
|
+
try { const a = JSON.parse(localStorage.getItem(key)); return Array.isArray(a) ? a.filter(v => typeof v === "string") : []; }
|
|
28
|
+
catch { return []; }
|
|
29
|
+
}
|
|
30
|
+
function save(key, values) { try { localStorage.setItem(key, JSON.stringify(values)); } catch { /* optional preference */ } }
|
|
31
|
+
export function rememberFile(key, file) {
|
|
32
|
+
save(key + ".recent", [file, ...load(key + ".recent").filter(v => v !== file)].slice(0, 30));
|
|
33
|
+
}
|
|
34
|
+
export function relocatePreference(key, from, to) {
|
|
35
|
+
for (const suffix of [".recent", ".stars"]) save(key + suffix, load(key + suffix).flatMap(v => v !== from ? [v] : to ? [to] : []));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function findMemo(endpoint, key, initial = "", trash = false) {
|
|
39
|
+
return new Promise(resolve => {
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
let stars = load(key + ".stars"), recent = load(key + ".recent");
|
|
42
|
+
const files = [], hits = [], list = h("div", {class:"memo-results"}), status = h("small", {role:"status"});
|
|
43
|
+
const query = h("input", {class:"input", type:"search", value:initial, maxlength:512, placeholder:"输入文件名或路径", "aria-label":"搜索关键词"});
|
|
44
|
+
let bodyMode = false, active = 0, scan, complete = false;
|
|
45
|
+
const bodySearch = h("button", {class:"btn", hidden:trash, onclick:() => {
|
|
46
|
+
bodyMode = !bodyMode;
|
|
47
|
+
scan?.abort(); hits.length = 0; active = 0;
|
|
48
|
+
bodySearch.setAttribute("aria-pressed", String(bodyMode));
|
|
49
|
+
query.placeholder = bodyMode ? "输入正文关键词,回车搜索" : "输入文件名或路径";
|
|
50
|
+
status.textContent = ""; draw(); query.focus();
|
|
51
|
+
if (bodyMode && query.value.trim()) void search();
|
|
52
|
+
}}, "搜正文");
|
|
53
|
+
const close = modal({title:trash ? "回收站" : "快速打开", body:h("div",{class:"modal-body memo-fields"},
|
|
54
|
+
h("div",{class:"memo-find-tools"},query,bodySearch),list,status), beforeClose:() => finish(null)});
|
|
55
|
+
function finish(result) { controller.abort(); scan?.abort(); close(); resolve(result); }
|
|
56
|
+
document.addEventListener("aim:leave-view", () => finish(null), {once:true,signal:controller.signal});
|
|
57
|
+
function draw() {
|
|
58
|
+
const term = query.value.trim().toLowerCase();
|
|
59
|
+
const paths = term || trash ? files.filter(p => p.toLowerCase().includes(term)) : [...new Set([...stars,...recent])];
|
|
60
|
+
const rows = bodyMode ? hits : paths.map(path => ({path}));
|
|
61
|
+
active = Math.max(0, Math.min(active, Math.min(rows.length,100)-1));
|
|
62
|
+
fill(list,rows.slice(0,100).map((hit,i) => {
|
|
63
|
+
const display = trash ? hit.path.split("/").slice(2).join("/") : hit.path;
|
|
64
|
+
const open = h("button",{class:"memo-found",onclick:() => finish(hit)},h("strong",{},display.split("/").pop()),
|
|
65
|
+
h("small",{},display),hit.snippet && h("span",{},hit.snippet));
|
|
66
|
+
const star = h("button",{class:"icon-btn","aria-label":(stars.includes(hit.path) ? "取消收藏 " : "收藏 ")+display,
|
|
67
|
+
"aria-pressed":String(stars.includes(hit.path)),onclick:() => {
|
|
68
|
+
stars = stars.includes(hit.path) ? stars.filter(p => p !== hit.path) : [...stars,hit.path];
|
|
69
|
+
save(key+".stars",stars); draw(); query.focus();
|
|
70
|
+
}},icon("bookmark"));
|
|
71
|
+
return h("div",{class:"memo-result"+(i===active?" active":"")},open,trash ? null : star);
|
|
72
|
+
}));
|
|
73
|
+
if (!bodyMode) status.textContent = rows.length > 100 ? "显示前 100 项,请缩小搜索范围" :
|
|
74
|
+
rows.length ? "" : term || trash ? (complete ? "没有匹配的文件" : "正在读取目录…") : "输入文件名搜索,收藏和最近打开的笔记会显示在这里。";
|
|
75
|
+
}
|
|
76
|
+
async function search() {
|
|
77
|
+
scan?.abort(); const request = scan = new AbortController();
|
|
78
|
+
const term = query.value.trim(); hits.length = 0; draw();
|
|
79
|
+
if (!term) return;
|
|
80
|
+
let skipped = 0;
|
|
81
|
+
status.textContent = "正在搜索正文…";
|
|
82
|
+
try {
|
|
83
|
+
const indexed = await api(endpoint("search")+"&q="+encodeURIComponent(term),{signal:request.signal});
|
|
84
|
+
if (indexed.ready) {
|
|
85
|
+
for (const hit of indexed.hits) {
|
|
86
|
+
const lower = hit.snippet.toLowerCase();
|
|
87
|
+
const needle = [term,...term.split(/\s+/)].find(word => lower.includes(word.toLowerCase()));
|
|
88
|
+
hits.push({...hit, needle});
|
|
89
|
+
}
|
|
90
|
+
draw(); status.textContent = hits.length ? "全文索引 · "+hits.length+" 项结果(最多 20 项)" : "没有匹配的正文";
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
for await (const path of vaultFiles(endpoint, request.signal)) {
|
|
94
|
+
if (!textFile.test(path)) continue;
|
|
95
|
+
let data;
|
|
96
|
+
try { data = await api(endpoint("file",path).replace("collab=1","collab=0"),{signal:request.signal}); }
|
|
97
|
+
catch (err) { if (request.signal.aborted || [401,403].includes(err.status)) throw err; skipped++; continue; }
|
|
98
|
+
const ix = data.text.toLowerCase().indexOf(term.toLowerCase());
|
|
99
|
+
if (ix >= 0) { hits.push({path,needle:term, snippet:data.text.slice(Math.max(0,ix-32),ix+80)}); draw(); }
|
|
100
|
+
}
|
|
101
|
+
status.textContent = hits.length+" 篇命中"+(skipped ? ","+skipped+" 个文件未能读取" : "");
|
|
102
|
+
} catch (err) { if (!request.signal.aborted) status.textContent = err.message; }
|
|
103
|
+
}
|
|
104
|
+
query.oninput = () => { active = 0; scan?.abort(); hits.length = 0; status.textContent = bodyMode ? "按回车搜索正文" : ""; draw(); };
|
|
105
|
+
query.onkeydown = e => {
|
|
106
|
+
if (e.isComposing) return;
|
|
107
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
108
|
+
e.preventDefault(); active += e.key === "ArrowDown" ? 1 : -1; draw(); list.children[active]?.scrollIntoView({block:"nearest"});
|
|
109
|
+
} else if (e.key === "Enter") {
|
|
110
|
+
e.preventDefault(); if (bodyMode && !hits.length) void search(); else list.children[active]?.querySelector("button").click();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
list.parentElement.onkeydown = e => { if (e.key === "Escape") finish(null); };
|
|
114
|
+
draw(); query.focus();
|
|
115
|
+
void (async () => {
|
|
116
|
+
try {
|
|
117
|
+
for await (const file of vaultFiles(endpoint,controller.signal,trash ? trashDir : "")) {
|
|
118
|
+
files.push(file); if (files.length % 40 === 0 && !bodyMode) draw();
|
|
119
|
+
}
|
|
120
|
+
complete = true; draw();
|
|
121
|
+
} catch (err) { if (!controller.signal.aborted) status.textContent = trash && err.status === 404 ? "回收站为空" : err.message; }
|
|
122
|
+
})();
|
|
123
|
+
});
|
|
124
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { h } from "../lib/dom.js";
|
|
2
|
+
import { modal } from "../lib/overlay.js";
|
|
3
|
+
import { api } from "../lib/api.js";
|
|
4
|
+
import { vaultFiles } from "./memo-find.js";
|
|
5
|
+
|
|
6
|
+
export function organizeDialog(endpoint, path) {
|
|
7
|
+
return new Promise(resolve => {
|
|
8
|
+
const controller = new AbortController();
|
|
9
|
+
const field = h("input",{class:"input",value:path,required:true,"aria-label":"目标路径"});
|
|
10
|
+
const status = h("p",{role:"status"},"正在检查可能引用此文件的笔记…");
|
|
11
|
+
const refs = h("ul",{class:"memo-refs"});
|
|
12
|
+
const submit = h("button",{class:"btn primary",type:"submit"},"移动或重命名");
|
|
13
|
+
const trash = h("button",{class:"btn danger-fill",type:"button",onclick:() => finish({op:"trash"})},"移入回收站");
|
|
14
|
+
const form = h("form",{class:"modal-body memo-fields"},h("label",{},"目标路径(含文件名)",field),status,refs,
|
|
15
|
+
h("div",{class:"memo-dialog-actions"},trash,submit));
|
|
16
|
+
function finish(value) { controller.abort(); close(); resolve(value); }
|
|
17
|
+
const close = modal({title:"整理文件 · "+path.split("/").pop(),body:form,beforeClose:() => finish(null)});
|
|
18
|
+
document.addEventListener("aim:leave-view",() => finish(null),{once:true,signal:controller.signal});
|
|
19
|
+
form.onsubmit = e => { e.preventDefault(); if (field.value.trim() !== path) finish({op:"move",to:field.value.trim()}); };
|
|
20
|
+
field.focus(); field.setSelectionRange(path.lastIndexOf("/")+1,path.lastIndexOf(".")>path.lastIndexOf("/") ? path.lastIndexOf(".") : path.length);
|
|
21
|
+
const name = path.split("/").pop().replace(/\.(md|markdown)$/i,"").toLowerCase();
|
|
22
|
+
void (async () => {
|
|
23
|
+
let skipped = 0;
|
|
24
|
+
try {
|
|
25
|
+
for await (const file of vaultFiles(endpoint,controller.signal)) {
|
|
26
|
+
if (file === path || !/\.(md|markdown)$/i.test(file)) continue;
|
|
27
|
+
let data;
|
|
28
|
+
try { data = await api(endpoint("file",file).replace("collab=1","collab=0"),{signal:controller.signal}); }
|
|
29
|
+
catch (err) { if (controller.signal.aborted || [401,403].includes(err.status)) throw err; skipped++; continue; }
|
|
30
|
+
const matches = data.text.matchAll(/\[\[([^\]|#]+)|\]\(<?([^)>]+)/g);
|
|
31
|
+
if ([...matches].some(m => {
|
|
32
|
+
let target = m[1] || m[2]; try { target = decodeURIComponent(target); } catch {}
|
|
33
|
+
return target.split("#")[0].trim().split("/").pop().replace(/\.(md|markdown)$/i,"").toLowerCase() === name;
|
|
34
|
+
})) refs.append(h("li",{},file));
|
|
35
|
+
}
|
|
36
|
+
status.textContent = (refs.childElementCount ? "可能需要更新链接的笔记" : "未发现引用此文件的链接") + (skipped ? "(有文件未能检查)" : "");
|
|
37
|
+
} catch (err) { if (!controller.signal.aborted) status.textContent = "引用检查未完成 · "+err.message; }
|
|
38
|
+
})();
|
|
39
|
+
});
|
|
40
|
+
}
|