agent-loft 0.2.131 → 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/composer-models.js +59 -31
- 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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { h, icon, toast } from "../lib/dom.js";
|
|
2
|
+
import { closeMenu, placePopover, showPopover } from "../lib/overlay.js";
|
|
3
|
+
|
|
4
|
+
export function createMemoOutline(reading) {
|
|
5
|
+
let sections = [], pop;
|
|
6
|
+
const trigger = h("button", { class: "icon-btn", title: "章节大纲", "aria-label": "章节大纲",
|
|
7
|
+
"aria-expanded": "false", hidden: true, onclick: open }, icon("outline"));
|
|
8
|
+
|
|
9
|
+
function update(body) {
|
|
10
|
+
const used = new Set(), suffixes = new Map();
|
|
11
|
+
const next = [];
|
|
12
|
+
for (const node of body?.querySelectorAll("h1,h2,h3,h4,h5,h6") ?? []) {
|
|
13
|
+
const title = node.textContent.trim();
|
|
14
|
+
if (!title) continue;
|
|
15
|
+
const base = title.toLowerCase().replace(/[^\p{L}\p{N}\p{M}\s_-]/gu, "").replace(/\s+/g, "-") || title;
|
|
16
|
+
let key = base, suffix = suffixes.get(base) || 0;
|
|
17
|
+
while (used.has(key)) key = base + "-" + ++suffix;
|
|
18
|
+
suffixes.set(base, suffix);
|
|
19
|
+
used.add(key);
|
|
20
|
+
next.push({ title, key, node, level: Number(node.tagName[1]) });
|
|
21
|
+
}
|
|
22
|
+
// Keep the open outline usable while collaborators edit paragraphs.
|
|
23
|
+
if (pop && (sections.length !== next.length || next.some((s, i) =>
|
|
24
|
+
s.key !== sections[i].key || s.title !== sections[i].title || s.level !== sections[i].level))) closeMenu();
|
|
25
|
+
sections = next;
|
|
26
|
+
trigger.hidden = sections.length < 2;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function jump(key) {
|
|
30
|
+
const section = sections.find(s => s.key === key.toLowerCase()) ?? sections.find(s => s.title === key);
|
|
31
|
+
if (!section) { toast("找不到章节「" + key + "」"); return; }
|
|
32
|
+
const { node } = section;
|
|
33
|
+
for (let el = node.parentElement; el && el !== reading; el = el.parentElement) {
|
|
34
|
+
if (el.tagName === "DETAILS") el.open = true;
|
|
35
|
+
}
|
|
36
|
+
reading.scrollTop += node.getBoundingClientRect().top - reading.getBoundingClientRect().top - 16;
|
|
37
|
+
node.tabIndex = -1;
|
|
38
|
+
node.focus({ preventScroll: true });
|
|
39
|
+
}
|
|
40
|
+
function currentSection() {
|
|
41
|
+
const top = reading.getBoundingClientRect().top + 20;
|
|
42
|
+
return sections.findLast(s => s.node.getClientRects().length && s.node.getBoundingClientRect().top <= top);
|
|
43
|
+
}
|
|
44
|
+
function position() {
|
|
45
|
+
const top = reading.scrollTop;
|
|
46
|
+
const section = top > 0 && currentSection();
|
|
47
|
+
if (!section) return { top };
|
|
48
|
+
return { top, section: section.key, offset: reading.getBoundingClientRect().top - section.node.getBoundingClientRect().top };
|
|
49
|
+
}
|
|
50
|
+
function restore(position) {
|
|
51
|
+
const section = sections.find(s => s.key === position.section);
|
|
52
|
+
if (section?.node.getClientRects().length && Number.isFinite(position.offset)) {
|
|
53
|
+
reading.scrollTop += section.node.getBoundingClientRect().top - reading.getBoundingClientRect().top + position.offset;
|
|
54
|
+
} else reading.scrollTop = Number.isFinite(position.top) ? position.top : 0;
|
|
55
|
+
}
|
|
56
|
+
function open() {
|
|
57
|
+
if (pop) { closeMenu(); return; }
|
|
58
|
+
const current = currentSection();
|
|
59
|
+
const baseLevel = sections.reduce((min, s) => Math.min(min, s.level), 6);
|
|
60
|
+
pop = showPopover(trigger, { place: "below" });
|
|
61
|
+
const nav = h("nav", { "aria-label": "章节大纲" }, sections.map(s =>
|
|
62
|
+
h("button", { type: "button", title: s.title, "aria-current": s === current ? "location" : null,
|
|
63
|
+
style: { paddingInlineStart: (12 + (s.level - baseLevel) * 12) + "px" },
|
|
64
|
+
onclick: () => { closeMenu(); jump(s.key); } }, s.title)));
|
|
65
|
+
pop.classList.add("memo-outline");
|
|
66
|
+
pop.append(h("div", { class: "pop-head" }, "章节大纲"), nav);
|
|
67
|
+
trigger.setAttribute("aria-expanded", "true");
|
|
68
|
+
document.addEventListener("aim:leave-view", closeMenu);
|
|
69
|
+
pop.onMenuClose = () => {
|
|
70
|
+
document.removeEventListener("aim:leave-view", closeMenu);
|
|
71
|
+
if (pop.contains(document.activeElement)) trigger.focus({ preventScroll: true });
|
|
72
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
73
|
+
pop = null;
|
|
74
|
+
};
|
|
75
|
+
nav.onkeydown = e => {
|
|
76
|
+
const next = e.key === "ArrowDown" ? e.target.nextElementSibling :
|
|
77
|
+
e.key === "ArrowUp" ? e.target.previousElementSibling : null;
|
|
78
|
+
if (next) { e.preventDefault(); next.focus(); }
|
|
79
|
+
};
|
|
80
|
+
placePopover(pop, trigger);
|
|
81
|
+
(nav.querySelector("[aria-current]") || nav.firstElementChild).focus();
|
|
82
|
+
}
|
|
83
|
+
return { trigger, update, jump, position, restore };
|
|
84
|
+
}
|