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
|
@@ -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
|
+
}
|