kaizen-agent 1.56.1 → 1.57.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/cli/board.test.ts +56 -0
- package/cli/docs.test.ts +92 -0
- package/cli/docs.ts +84 -0
- package/cli/install.ts +13 -0
- package/cli/project-tree.test.ts +39 -0
- package/cli/pty.test.ts +1 -1
- package/cli/state.test.ts +18 -2
- package/cli/state.ts +21 -17
- package/cli/usage.test.ts +142 -0
- package/cli/usage.ts +66 -0
- package/cli/web.ts +194 -7
- package/package.json +1 -1
- package/skills/kaizen/SKILL.md +1 -1
- package/skills/kaizen/docs.md +6 -1
- package/skills/kaizen/spec-main.md +4 -2
- package/web/board/api.ts +2 -0
- package/web/board/badge.ts +41 -0
- package/web/board/cards.ts +58 -11
- package/web/board/context.ts +247 -0
- package/web/board/home.ts +3 -3
- package/web/board/ideas.ts +176 -0
- package/web/board/keys.ts +2 -5
- package/web/board/labels.ts +4 -0
- package/web/board/live.ts +3 -1
- package/web/board/main.ts +21 -0
- package/web/board/panel.ts +33 -8
- package/web/board/projects.ts +42 -2
- package/web/board/run-view.ts +691 -0
- package/web/board/settings.ts +1 -0
- package/web/board/store.ts +2 -0
- package/web/board/terms.ts +7 -3
- package/web/board/views.ts +21 -4
- package/web/board/workspace.ts +318 -0
- package/web/board.html +199 -35
- package/web/board.js +43 -40
- package/web/notebook.js +24 -23
- package/web/notebook.ts +7 -1
- package/web/notes.html +67 -21
- package/web/term.js +34 -34
- package/web/term.ts +10 -6
- package/web/video.html +65 -21
- package/web/video.js +54 -53
- package/web/video.ts +5 -1
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
// The run workspace (?view=run&dir=&id=, or &idea=<text> for an idea): the card's panel
|
|
2
|
+
// docked at the right as an icon strip (panel.ts draws it; open() fills it), and a tabbed
|
|
3
|
+
// centre: the run's agent, shells started with POST /shell, the project's localhost, the
|
|
4
|
+
// run's Preview and project notes. Terminal tabs are kzTerm mounts on /pty that live as
|
|
5
|
+
// long as their tab, so a tab switch or a /state tick never reconnects them. Split right
|
|
6
|
+
// or down shows a second tab beside the first, sized with the Terminal page's handles.
|
|
7
|
+
import { store } from "./store.ts";
|
|
8
|
+
import { toast } from "./toast.ts";
|
|
9
|
+
import { $, html, ico, raw } from "./dom.ts";
|
|
10
|
+
import { key } from "./cards.ts";
|
|
11
|
+
import { open } from "./panel.ts";
|
|
12
|
+
import { termLoad } from "./agent-term.ts";
|
|
13
|
+
import { load, post } from "./api.ts";
|
|
14
|
+
import { attach, md, preview } from "./markdown.ts";
|
|
15
|
+
import { layout } from "./terms.ts";
|
|
16
|
+
import { cardHref, railData } from "./workspace.ts";
|
|
17
|
+
import { go } from "./views.ts";
|
|
18
|
+
|
|
19
|
+
type Tab = { id: string; kind: "agent" | "shell" | "local" | "preview" | "note" | "wait"; title: string; key?: string; note?: string; agent?: string };
|
|
20
|
+
// Per run: the extra items opened (localhost, preview, notes), agent items closed, and the
|
|
21
|
+
// screens. A screen is one tab of the bar: one or more items side by side or stacked.
|
|
22
|
+
// A screen's layout is a tmux-style tree: a leaf is an item id; a split holds two or more
|
|
23
|
+
// children side by side ("cols") or stacked ("rows"), with their relative sizes.
|
|
24
|
+
type Node = string | { dir: "cols" | "rows"; kids: Node[]; sizes: number[] };
|
|
25
|
+
type Screen = { id: string; tree: Node; focus: string };
|
|
26
|
+
const leaves = (n: Node): string[] => (typeof n === "string" ? [n] : n.kids.flatMap(leaves));
|
|
27
|
+
// Drops ids no longer open; a split left with one child becomes that child.
|
|
28
|
+
function prune(n: Node, keep: (id: string) => boolean): Node | null {
|
|
29
|
+
if (typeof n === "string") return keep(n) ? n : null;
|
|
30
|
+
const kids: Node[] = [],
|
|
31
|
+
sizes: number[] = [];
|
|
32
|
+
n.kids.forEach((k, i) => {
|
|
33
|
+
const p = prune(k, keep);
|
|
34
|
+
if (p) (kids.push(p), sizes.push(n.sizes[i] ?? 1));
|
|
35
|
+
});
|
|
36
|
+
return !kids.length ? null : kids.length === 1 ? kids[0] : { dir: n.dir, kids, sizes };
|
|
37
|
+
}
|
|
38
|
+
// tmux: the focused pane splits in two, the new item right of or under it.
|
|
39
|
+
function splitAt(n: Node, at: string, add: string, dir: "cols" | "rows", before = false): Node {
|
|
40
|
+
if (typeof n === "string") return n === at ? { dir, kids: before ? [add, n] : [n, add], sizes: [1, 1] } : n;
|
|
41
|
+
return { ...n, kids: n.kids.map((k) => splitAt(k, at, add, dir, before)) };
|
|
42
|
+
}
|
|
43
|
+
type Saved = { tabs: Tab[]; hidden: string[]; screens: Screen[]; on: string | null };
|
|
44
|
+
const ALL: Record<string, Saved> = (() => {
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(localStorage.getItem("kz-runtabs") || "{}");
|
|
47
|
+
} catch {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
51
|
+
const save = () => {
|
|
52
|
+
try {
|
|
53
|
+
localStorage.setItem("kz-runtabs", JSON.stringify(ALL));
|
|
54
|
+
} catch {}
|
|
55
|
+
};
|
|
56
|
+
let card: any = null;
|
|
57
|
+
let shells: any[] = [];
|
|
58
|
+
let noPty = false;
|
|
59
|
+
// Every agent installed for this project (from /agents), each offered as a bare session.
|
|
60
|
+
let agents: { name: string; cmd: string }[] = [];
|
|
61
|
+
// The agents with their own mark in the sprite; the rest show the generic one.
|
|
62
|
+
const MARKS = ["claude", "codex", "gemini"];
|
|
63
|
+
// Tab id -> its pane, and for a terminal the function that unmounts it.
|
|
64
|
+
const panes = new Map<string, { el: HTMLElement; off: (() => void) | null }>();
|
|
65
|
+
const runKey = (c) => key(c);
|
|
66
|
+
const saved = (): Saved => {
|
|
67
|
+
const s = (ALL[runKey(card)] ||= { tabs: [], hidden: [], screens: [], on: null });
|
|
68
|
+
if (!Array.isArray(s.screens) || s.screens.some((sc: any) => !sc.tree)) (s.screens = []), delete (s as any).split; // older saves
|
|
69
|
+
return s;
|
|
70
|
+
};
|
|
71
|
+
// Where the next item goes: a new screen (null), or beside/below the items of a screen.
|
|
72
|
+
let target: { screen: string; dir: "cols" | "rows" } | null = null;
|
|
73
|
+
// Every item sits in exactly one screen; a new one goes to `target` or its own screen.
|
|
74
|
+
function screens(): Screen[] {
|
|
75
|
+
const s = saved(),
|
|
76
|
+
ids = tabs().map((t) => t.id);
|
|
77
|
+
s.screens = s.screens.flatMap((sc) => {
|
|
78
|
+
const tree = prune(sc.tree, (id) => ids.includes(id));
|
|
79
|
+
return tree ? [{ ...sc, tree, focus: leaves(tree).includes(sc.focus) ? sc.focus : leaves(tree)[0] }] : [];
|
|
80
|
+
});
|
|
81
|
+
for (const id of ids)
|
|
82
|
+
if (!s.screens.some((sc) => leaves(sc.tree).includes(id))) {
|
|
83
|
+
const into = target && s.screens.find((sc) => sc.id === target!.screen);
|
|
84
|
+
if (into) (into.tree = splitAt(into.tree, into.focus, id, target!.dir)), (into.focus = id), (s.on = into.id);
|
|
85
|
+
else s.screens.push({ id: "s" + Date.now().toString(36) + Math.random().toString(36).slice(2, 5), tree: id, focus: id });
|
|
86
|
+
target = null;
|
|
87
|
+
}
|
|
88
|
+
if (!s.screens.some((sc) => sc.id === s.on)) s.on = s.screens[0]?.id ?? null;
|
|
89
|
+
return s.screens;
|
|
90
|
+
}
|
|
91
|
+
const loopback = /^(localhost|127\.0\.0\.1|\[::1\])$/.test(location.hostname);
|
|
92
|
+
|
|
93
|
+
function tabs(): Tab[] {
|
|
94
|
+
const s = saved(),
|
|
95
|
+
out: Tab[] = [];
|
|
96
|
+
// Every agent session the run has on this board, each its own tab.
|
|
97
|
+
const keys: string[] = card.pty ? card.pty.all ?? [card.pty.key] : [];
|
|
98
|
+
const runAgent = ["claude", "codex", "gemini", "agy", "opencode", "cursor"].find((a) => card.tags?.includes(a)) ?? undefined;
|
|
99
|
+
const named = (a?: string) => agents.find((x) => x.cmd === a)?.name ?? "Agent";
|
|
100
|
+
keys.forEach((k, i) => out.push({ id: "agent:" + k, kind: "agent", agent: runAgent, title: named(runAgent) + (keys.length > 1 ? " " + (i + 1) : ""), key: k }));
|
|
101
|
+
for (const sh of shells.filter((x) => x.run === runKey(card))) out.push({ id: "sh:" + sh.key, kind: sh.agent ? "agent" : "shell", agent: sh.agent || undefined, title: sh.title, key: sh.key });
|
|
102
|
+
// The run's own session is running in another terminal: a tab that says so.
|
|
103
|
+
if (elsewhere.has(runKey(card))) out.push({ id: "wait", kind: "wait", agent: "claude", title: named("claude") });
|
|
104
|
+
return [...out.filter((t) => !s.hidden.includes(t.id)), ...s.tabs];
|
|
105
|
+
}
|
|
106
|
+
// An agent's tab shows the agent's own mark when the sprite has one.
|
|
107
|
+
const icon = (t: Tab) => (t.agent && MARKS.includes(t.agent) ? raw(`<svg><use href="#i-${t.agent}"/></svg>`) : ico({ agent: "ai", shell: "term", local: "link", preview: "eye", note: "note", wait: "ai" }[t.kind]));
|
|
108
|
+
const elsewhere = new Set<string>();
|
|
109
|
+
|
|
110
|
+
// Called by go("run") and on every /state: draws the tab strip and keeps the panes.
|
|
111
|
+
export async function showRunView() {
|
|
112
|
+
const p = new URLSearchParams(location.search),
|
|
113
|
+
dir = p.get("dir"),
|
|
114
|
+
id = p.get("id"),
|
|
115
|
+
idea = p.get("idea");
|
|
116
|
+
const find = (cs) => cs.find((x) => x.state === dir && (id ? x.kind === "run" && x.id === id : x.kind === "idea" && x.text === idea));
|
|
117
|
+
const c = find(store.S.cards);
|
|
118
|
+
if (!c) {
|
|
119
|
+
// A project this board is not showing: the all-projects board has it.
|
|
120
|
+
if (!store.all && find(railData().cards)) location.href = "/?all=1&" + p.toString();
|
|
121
|
+
else $("rtabs").innerHTML = String(html`<span class="rnone">That ${id ? "run" : "idea"} is not on this board.</span>`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const fresh = !card || runKey(card) !== runKey(c);
|
|
125
|
+
// Each run has its own centre: switching runs unmounts this one's panes (its shells keep running on the server).
|
|
126
|
+
if (fresh) stopPanes();
|
|
127
|
+
card = c;
|
|
128
|
+
if (fresh || store.openKey !== key(c)) open(c);
|
|
129
|
+
try {
|
|
130
|
+
const r = await fetch("/shells?dir=" + encodeURIComponent(c.state));
|
|
131
|
+
shells = r.ok ? await r.json() : [];
|
|
132
|
+
} catch {}
|
|
133
|
+
if (fresh) {
|
|
134
|
+
try {
|
|
135
|
+
agents = (await (await fetch("/agents?dir=" + encodeURIComponent(c.state))).json()).agents ?? [];
|
|
136
|
+
} catch {}
|
|
137
|
+
}
|
|
138
|
+
if (card !== c) return;
|
|
139
|
+
// Nothing to show (no board-launched agent, no shell, no tab saved): the run's own Claude
|
|
140
|
+
// session resumed here, else a shell in the project, once per run per visit, so the centre
|
|
141
|
+
// is never blank. Only the run's recorded session is ever resumed; nothing new is started.
|
|
142
|
+
if (!tabs().length && !noPty && !autoShell.has(runKey(c))) {
|
|
143
|
+
autoShell.add(runKey(c));
|
|
144
|
+
return void (c.kind === "run" ? openAgent() : newPty());
|
|
145
|
+
}
|
|
146
|
+
draw();
|
|
147
|
+
}
|
|
148
|
+
const autoShell = new Set<string>();
|
|
149
|
+
|
|
150
|
+
function draw() {
|
|
151
|
+
const s = saved(),
|
|
152
|
+
ts = tabs(),
|
|
153
|
+
scs = screens(),
|
|
154
|
+
cur = scs.find((sc) => sc.id === s.on);
|
|
155
|
+
const item = (id: string) => ts.find((t) => t.id === id)!;
|
|
156
|
+
const PTY = !noPty;
|
|
157
|
+
const menu = html`<div class="menu-pop">${PTY ? html`<button data-n="shell">${ico("term")}<span>Shell<small>a terminal in the project folder</small></span></button>` : ""}<button data-n="local">${ico("link")}<span>Localhost<small>the project's dev server</small></span></button><button data-n="preview">${ico("eye")}<span>Preview<small>the run's Preview</small></span></button>${PTY ? agents.map((a) => html`<button data-n="agent:${a.cmd}">${MARKS.includes(a.cmd) ? raw(`<svg><use href="#i-${a.cmd}"/></svg>`) : ico("ai")}<span>${a.name} session<small>the agent alone, no run</small></span></button>`) : ""}</div>`;
|
|
158
|
+
$("rtabs").innerHTML = String(
|
|
159
|
+
html`${scs.map((sc) => {
|
|
160
|
+
const ids = leaves(sc.tree),
|
|
161
|
+
first = item(ids[0]),
|
|
162
|
+
title = ids.map((id) => item(id).title).join(" + ");
|
|
163
|
+
return html`<button class="rtab${sc.id === s.on ? " on" : ""}" data-t="${sc.id}" title="${title}">${icon(first)}<span>${title}</span><i data-xs="${sc.id}" role="button" aria-label="Close ${title}" title="Close tab">×</i></button>`;
|
|
164
|
+
})}<details class="radd" data-mode="tab"><summary class="rbtn" title="New tab" aria-label="New tab">${ico("plus")}</summary>${menu}</details><span class="sp"></span>${cur ? html`<details class="radd rsplit" data-mode="cols"><summary class="rbtn" title="Split right: add to this tab" aria-label="Split right">${ico("lcols")}</summary>${menu}</details><details class="radd rsplit" data-mode="rows"><summary class="rbtn" title="Split down: add to this tab" aria-label="Split down">${ico("lrows")}</summary>${menu}</details>` : ""}<button class="rbtn" id="rside" title="Show or hide the run panel" aria-label="Show or hide the run panel" aria-pressed="${String(document.body.classList.contains("rflip"))}">${ico("open")}</button>`,
|
|
165
|
+
);
|
|
166
|
+
$("rtabs").querySelector(".rtab.on")?.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
167
|
+
// Panes: made once per item, kept (hidden) while it is open, so terminals never reconnect.
|
|
168
|
+
const box = $("rpanes");
|
|
169
|
+
for (const [id, p] of panes)
|
|
170
|
+
if (!ts.some((t) => t.id === id)) {
|
|
171
|
+
p.off?.();
|
|
172
|
+
p.el.remove();
|
|
173
|
+
panes.delete(id);
|
|
174
|
+
}
|
|
175
|
+
for (const t of ts) if (!panes.has(t.id)) makePane(t);
|
|
176
|
+
const shown = cur ? leaves(cur.tree) : [];
|
|
177
|
+
for (const [id, p] of panes) {
|
|
178
|
+
p.el.hidden = !shown.includes(id);
|
|
179
|
+
p.el.classList.toggle("split", shown.length > 1);
|
|
180
|
+
p.el.classList.toggle("focus", shown.length > 1 && id === cur?.focus);
|
|
181
|
+
p.el.querySelector(".ph span")!.textContent = item(id)?.title ?? "";
|
|
182
|
+
}
|
|
183
|
+
// The tree is rebuilt only when it changes: moving a pane in the DOM reloads its frame.
|
|
184
|
+
const sig = cur ? cur.id + JSON.stringify(cur.tree) : "";
|
|
185
|
+
if (sig !== shownSig) {
|
|
186
|
+
shownSig = sig;
|
|
187
|
+
for (const [, p] of panes) if (p.el.parentElement !== box) box.append(p.el);
|
|
188
|
+
box.querySelector(".rtree")?.remove();
|
|
189
|
+
if (cur) box.prepend(build(cur.tree, cur));
|
|
190
|
+
}
|
|
191
|
+
// One empty note at most: every draw used to append another.
|
|
192
|
+
box.querySelector(".rempty")?.remove();
|
|
193
|
+
if (!scs.length) box.insertAdjacentHTML("beforeend", String(html`<div class="empty rempty"><b>No tabs open</b><p>Open a shell, the project's localhost or the Preview with +.</p></div>`));
|
|
194
|
+
wire();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let shownSig = "";
|
|
198
|
+
// Moves an item out of its tab into another one (split beside a pane there, or next to its
|
|
199
|
+
// focused pane), or into a tab of its own (to = null). Terminals keep running; frames reload.
|
|
200
|
+
function moveItem(id: string, to: string | null, at?: { pane: string; dir: "cols" | "rows"; before: boolean }) {
|
|
201
|
+
const s = saved();
|
|
202
|
+
if (at?.pane === id) return;
|
|
203
|
+
// Dropped on the tab it is already in, with no pane edge to split at: nothing to do.
|
|
204
|
+
if (!at && to && s.screens.find((sc) => sc.id === to && leaves(sc.tree).includes(id))) return;
|
|
205
|
+
for (const sc of s.screens) {
|
|
206
|
+
sc.tree = prune(sc.tree, (x) => x !== id) ?? "";
|
|
207
|
+
if (sc.tree !== "" && !leaves(sc.tree).includes(sc.focus)) sc.focus = leaves(sc.tree)[0];
|
|
208
|
+
}
|
|
209
|
+
s.screens = s.screens.filter((sc) => sc.tree !== "");
|
|
210
|
+
const dest = to && s.screens.find((sc) => sc.id === to);
|
|
211
|
+
if (dest) {
|
|
212
|
+
dest.tree = splitAt(dest.tree, at?.pane ?? dest.focus, id, at?.dir ?? "cols", at?.before ?? false);
|
|
213
|
+
dest.focus = id;
|
|
214
|
+
s.on = dest.id;
|
|
215
|
+
} else {
|
|
216
|
+
const sid = "s" + Date.now().toString(36) + Math.random().toString(36).slice(2, 5);
|
|
217
|
+
s.screens.push({ id: sid, tree: id, focus: id });
|
|
218
|
+
s.on = sid;
|
|
219
|
+
}
|
|
220
|
+
save();
|
|
221
|
+
draw();
|
|
222
|
+
}
|
|
223
|
+
let dragging: string | null = null;
|
|
224
|
+
// One node of the layout tree as DOM: a pane, or a flex row/column of its children with a
|
|
225
|
+
// draggable gutter between each pair (sizes saved on the node).
|
|
226
|
+
function build(n: Node, sc: Screen): HTMLElement {
|
|
227
|
+
if (typeof n === "string") return panes.get(n)!.el;
|
|
228
|
+
const el = document.createElement("div");
|
|
229
|
+
el.className = "rtree " + n.dir;
|
|
230
|
+
n.kids.forEach((k, i) => {
|
|
231
|
+
if (i) {
|
|
232
|
+
const g = document.createElement("div");
|
|
233
|
+
g.className = "rgut";
|
|
234
|
+
g.onpointerdown = (e) => {
|
|
235
|
+
e.preventDefault();
|
|
236
|
+
const a = el.children[2 * i - 2] as HTMLElement,
|
|
237
|
+
b = el.children[2 * i] as HTMLElement,
|
|
238
|
+
horiz = n.dir === "cols",
|
|
239
|
+
ra = a.getBoundingClientRect(),
|
|
240
|
+
rb = b.getBoundingClientRect(),
|
|
241
|
+
total = horiz ? ra.width + rb.width : ra.height + rb.height,
|
|
242
|
+
sum = n.sizes[i - 1] + n.sizes[i],
|
|
243
|
+
start = horiz ? ra.left : ra.top;
|
|
244
|
+
g.setPointerCapture(e.pointerId);
|
|
245
|
+
g.onpointermove = (m) => {
|
|
246
|
+
const f = Math.min(0.9, Math.max(0.1, ((horiz ? m.clientX : m.clientY) - start) / total));
|
|
247
|
+
n.sizes[i - 1] = sum * f;
|
|
248
|
+
n.sizes[i] = sum * (1 - f);
|
|
249
|
+
a.style.flexGrow = String(n.sizes[i - 1]);
|
|
250
|
+
b.style.flexGrow = String(n.sizes[i]);
|
|
251
|
+
};
|
|
252
|
+
g.onpointerup = () => {
|
|
253
|
+
g.onpointermove = g.onpointerup = null;
|
|
254
|
+
shownSig = sc.id + JSON.stringify(sc.tree);
|
|
255
|
+
save();
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
el.append(g);
|
|
259
|
+
}
|
|
260
|
+
const c = build(k, sc);
|
|
261
|
+
c.style.flex = `${n.sizes[i] ?? 1} 1 0`;
|
|
262
|
+
el.append(c);
|
|
263
|
+
});
|
|
264
|
+
return el;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function makePane(t: Tab) {
|
|
268
|
+
const el = document.createElement("section");
|
|
269
|
+
el.className = "rpane";
|
|
270
|
+
el.dataset.t = t.id;
|
|
271
|
+
// The header is the handle: drag it onto a tab, onto + (a new tab) or onto a pane's edge;
|
|
272
|
+
// ⋯ does the same from a menu. × closes the item.
|
|
273
|
+
el.innerHTML = String(html`<header class="ph" draggable="true"><span>${t.title}</span><details class="pmove"><summary title="Move to another tab" aria-label="Move ${t.title}">⋯</summary><div class="menu-pop"></div></details><button data-x="${t.id}" title="Close" aria-label="Close ${t.title}">×</button></header>`);
|
|
274
|
+
const p = { el, off: null as null | (() => void) };
|
|
275
|
+
panes.set(t.id, p);
|
|
276
|
+
$("rpanes").append(el);
|
|
277
|
+
if (t.kind === "agent" || t.kind === "shell") {
|
|
278
|
+
const term = document.createElement("div");
|
|
279
|
+
term.className = "term";
|
|
280
|
+
el.append(term);
|
|
281
|
+
termLoad()
|
|
282
|
+
.then((m) => {
|
|
283
|
+
if (panes.get(t.id) === p) p.off = m(term, t.key, () => load(true));
|
|
284
|
+
})
|
|
285
|
+
.catch(() => (term.textContent = "The terminal could not load."));
|
|
286
|
+
} else if (t.kind === "wait")
|
|
287
|
+
el.insertAdjacentHTML(
|
|
288
|
+
"beforeend",
|
|
289
|
+
String(html`<div class="empty rwait"><b>This run's agent is running in another terminal</b><p>One conversation cannot run in two places. Close it there, then open it here.</p><button type="button" class="rwin" data-b="recheck">${ico("refresh")}<span>Open here</span></button></div>`),
|
|
290
|
+
);
|
|
291
|
+
else if (t.kind === "local") browser(el);
|
|
292
|
+
else if (t.kind === "note") el.insertAdjacentHTML("beforeend", String(html`<iframe class="rframe" title="${t.title}" src="/notebook?dir=${encodeURIComponent(card.state)}¬e=${encodeURIComponent(t.note!)}&embed=1"></iframe>`));
|
|
293
|
+
else void previewPane(el);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// The localhost tab: a URL bar over an iframe. The URL is kept per project.
|
|
297
|
+
function browser(el: HTMLElement) {
|
|
298
|
+
const k = "kz-browse:" + card.state;
|
|
299
|
+
let url = "http://localhost:3000";
|
|
300
|
+
try {
|
|
301
|
+
url = localStorage.getItem(k) || url;
|
|
302
|
+
} catch {}
|
|
303
|
+
const back: string[] = [],
|
|
304
|
+
fwd: string[] = [];
|
|
305
|
+
el.insertAdjacentHTML(
|
|
306
|
+
"beforeend",
|
|
307
|
+
String(html`<form class="urlbar"><button type="button" data-b="back" title="Back" aria-label="Back">‹</button><button type="button" data-b="fwd" title="Forward" aria-label="Forward">›</button><button type="button" data-b="reload" title="Reload" aria-label="Reload">${ico("refresh")}</button><input class="sel" aria-label="Address" spellcheck="false" value="${url}">${winBtn(url)}</form>`),
|
|
308
|
+
);
|
|
309
|
+
const f = el.querySelector("form")!,
|
|
310
|
+
inp = f.querySelector("input")!,
|
|
311
|
+
ext = f.querySelector<HTMLAnchorElement>("a.rwin");
|
|
312
|
+
// On a phone over the tailnet, localhost is the phone itself: framing it shows nothing useful.
|
|
313
|
+
const local = (u) => /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:|\/|$)/.test(u);
|
|
314
|
+
const frame = document.createElement("iframe");
|
|
315
|
+
frame.className = "rframe";
|
|
316
|
+
frame.title = "localhost";
|
|
317
|
+
const note = document.createElement("p");
|
|
318
|
+
note.className = "rnote";
|
|
319
|
+
const far = "This page is on the computer running the board; from this device localhost is the device itself. Use Open in browser on that computer.";
|
|
320
|
+
el.append(frame, note);
|
|
321
|
+
const go = (u, push = true) => {
|
|
322
|
+
if (push && url !== u) back.push(url), (fwd.length = 0);
|
|
323
|
+
url = u;
|
|
324
|
+
inp.value = u;
|
|
325
|
+
if (ext) ext.href = u;
|
|
326
|
+
try {
|
|
327
|
+
localStorage.setItem(k, u);
|
|
328
|
+
} catch {}
|
|
329
|
+
const hide = !loopback && local(u);
|
|
330
|
+
frame.hidden = hide;
|
|
331
|
+
note.hidden = !hide;
|
|
332
|
+
note.textContent = far;
|
|
333
|
+
if (hide) return;
|
|
334
|
+
frame.src = u;
|
|
335
|
+
// Most public sites refuse to be framed (X-Frame-Options / frame-ancestors), and the
|
|
336
|
+
// browser only shows its own error page. The board asks the site first and says so.
|
|
337
|
+
void fetch("/frameable?url=" + encodeURIComponent(u))
|
|
338
|
+
.then((r) => r.json())
|
|
339
|
+
.then((j) => {
|
|
340
|
+
if (url !== u || j.frameable !== false) return;
|
|
341
|
+
frame.hidden = true;
|
|
342
|
+
frame.removeAttribute("src");
|
|
343
|
+
note.hidden = false;
|
|
344
|
+
note.textContent = `${new URL(u).host} does not allow being shown inside another page. `;
|
|
345
|
+
note.insertAdjacentHTML("beforeend", String(winBtn(u)));
|
|
346
|
+
})
|
|
347
|
+
.catch(() => {});
|
|
348
|
+
};
|
|
349
|
+
f.onsubmit = (e) => {
|
|
350
|
+
e.preventDefault();
|
|
351
|
+
let u = inp.value.trim();
|
|
352
|
+
if (!/^https?:\/\//.test(u)) u = "http://" + u;
|
|
353
|
+
go(u);
|
|
354
|
+
};
|
|
355
|
+
el.onclick = (e) => {
|
|
356
|
+
const b = (e.target as Element).closest<HTMLElement>("[data-b]")?.dataset.b;
|
|
357
|
+
if (b === "win") void openWindow(url);
|
|
358
|
+
else if (b === "reload") go(url, false);
|
|
359
|
+
else if (b === "back" && back.length) fwd.push(url), go(back.pop()!, false);
|
|
360
|
+
else if (b === "fwd" && fwd.length) back.push(url), go(fwd.pop()!, false);
|
|
361
|
+
};
|
|
362
|
+
go(url, false);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// A Chromium app window on the board's computer (POST /open-url): for dev servers that
|
|
366
|
+
// refuse to be framed. Only offered when the page itself is on that computer.
|
|
367
|
+
// One labelled button: on the board's computer a Chromium app window, elsewhere a new tab.
|
|
368
|
+
const winBtn = (u: string) =>
|
|
369
|
+
loopback
|
|
370
|
+
? html`<button type="button" class="rwin" data-b="win" title="Open in a browser window">${ico("popout")}<span>Open in browser</span></button>`
|
|
371
|
+
: html`<a class="rwin" target="_blank" rel="noopener" href="${u}">${ico("popout")}<span>Open in browser</span></a>`;
|
|
372
|
+
async function openWindow(u: string) {
|
|
373
|
+
const r = await fetch("/open-url", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ url: u }) }).catch(() => null);
|
|
374
|
+
if (!r?.ok) toast("Could not open a browser window" + (r ? ": " + ((await r.json().catch(() => ({}))) as any).error : ""));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// The run's Preview: its screenshots (each opens the image viewer on click), with an Open
|
|
378
|
+
// app link when it names a localhost address. The address is framed only when the Preview
|
|
379
|
+
// has no images: the dev server a run used is often gone, and a dead frame hides the proof.
|
|
380
|
+
async function previewPane(el: HTMLElement) {
|
|
381
|
+
let doc = store.runDoc && store.runDoc.id === card.id ? store.runDoc : null;
|
|
382
|
+
if (!doc && card.kind === "run")
|
|
383
|
+
try {
|
|
384
|
+
doc = await (await fetch(`/run/${encodeURIComponent(card.id)}?dir=${encodeURIComponent(card.state)}`)).json();
|
|
385
|
+
} catch {}
|
|
386
|
+
const pv = doc && preview(doc.impl);
|
|
387
|
+
const url = pv && /https?:\/\/(?:localhost|127\.0\.0\.1)(?::\d+)?[^\s)`'"<>]*/.exec(pv)?.[0];
|
|
388
|
+
const shots = pv && /!\[[^\]]*\]\(/.test(pv);
|
|
389
|
+
if (url && loopback && !shots) return void el.insertAdjacentHTML("beforeend", String(html`<iframe class="rframe" title="Preview" src="${url}"></iframe>`));
|
|
390
|
+
const bar = url ? html`<div class="urlbar"><span class="sel">${url}</span>${winBtn(url)}</div>` : "";
|
|
391
|
+
el.insertAdjacentHTML("beforeend", String(html`${bar}<div class="rprev md">${pv ? raw(md(attach(pv, card))) : html`<p class="rnone">This run has no Preview yet.</p>`}</div>`));
|
|
392
|
+
el.querySelector<HTMLElement>(".urlbar [data-b=win]")?.addEventListener("click", () => void openWindow(url!));
|
|
393
|
+
// The screenshots as a grid, each captioned by its file name, after the Preview's text.
|
|
394
|
+
const box = el.querySelector(".rprev")!,
|
|
395
|
+
imgs = [...box.querySelectorAll("img")];
|
|
396
|
+
if (imgs.length) {
|
|
397
|
+
const grid = document.createElement("div");
|
|
398
|
+
grid.className = "shots";
|
|
399
|
+
for (const im of imgs) {
|
|
400
|
+
const p = im.parentElement!;
|
|
401
|
+
const fig = document.createElement("figure");
|
|
402
|
+
const cap = document.createElement("figcaption");
|
|
403
|
+
cap.textContent = im.alt || decodeURIComponent((im.getAttribute("src") || "").split("/").pop() || "");
|
|
404
|
+
fig.append(im.closest("a") ?? im, cap);
|
|
405
|
+
grid.append(fig);
|
|
406
|
+
if (p !== box && !p.textContent!.trim() && !p.querySelector("img")) p.remove();
|
|
407
|
+
}
|
|
408
|
+
box.append(grid);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function addTab(t: Tab) {
|
|
413
|
+
const s = saved();
|
|
414
|
+
if (!s.tabs.some((x) => x.id === t.id)) s.tabs.push(t);
|
|
415
|
+
// Already open somewhere: moving it into the target screen means taking it out of its old one.
|
|
416
|
+
else if (target) for (const sc of s.screens) sc.tree = prune(sc.tree, (id) => id !== t.id) ?? sc.tree;
|
|
417
|
+
show(t.id);
|
|
418
|
+
}
|
|
419
|
+
// After an item is added: run screens() (which places it) and show the screen holding it.
|
|
420
|
+
function show(id: string) {
|
|
421
|
+
const s = saved();
|
|
422
|
+
s.on = screens().find((sc) => leaves(sc.tree).includes(id))?.id ?? s.on;
|
|
423
|
+
save();
|
|
424
|
+
draw();
|
|
425
|
+
}
|
|
426
|
+
export const openNoteTab = (name: string) => addTab({ id: "note:" + name, kind: "note", title: name.slice(name.lastIndexOf("/") + 1), note: name });
|
|
427
|
+
const openPreview = () => addTab({ id: "preview", kind: "preview", title: "Preview" });
|
|
428
|
+
|
|
429
|
+
// The run's recorded session, resumed in a board terminal tied to it. While it still runs in
|
|
430
|
+
// another terminal the server says so (409 live) and a tab explains; with no session, a shell.
|
|
431
|
+
async function openAgent(again = false) {
|
|
432
|
+
const k = runKey(card);
|
|
433
|
+
const r = await fetch("/shell", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ dir: card.state, run: k, resume: true }) }).catch(() => null);
|
|
434
|
+
if (r?.ok) {
|
|
435
|
+
elsewhere.delete(k);
|
|
436
|
+
const sh = await r.json();
|
|
437
|
+
shells.push(sh);
|
|
438
|
+
return show("sh:" + sh.key);
|
|
439
|
+
}
|
|
440
|
+
const j: any = r ? await r.json().catch(() => ({})) : {};
|
|
441
|
+
if (r?.status === 409 && j.live) {
|
|
442
|
+
elsewhere.add(k);
|
|
443
|
+
if (again) toast("Still running in another terminal");
|
|
444
|
+
return draw();
|
|
445
|
+
}
|
|
446
|
+
elsewhere.delete(k);
|
|
447
|
+
if (again) toast(j.error || "Could not resume the session");
|
|
448
|
+
else if (!tabs().length) void newPty();
|
|
449
|
+
else draw();
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async function newPty(agent?: string) {
|
|
453
|
+
const r = await fetch("/shell", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ dir: card.state, run: runKey(card), ...(agent ? { agent } : {}) }) });
|
|
454
|
+
if (r.status === 501) {
|
|
455
|
+
noPty = true;
|
|
456
|
+
return draw();
|
|
457
|
+
}
|
|
458
|
+
if (!r.ok) {
|
|
459
|
+
// 429 (too many terminals), 403, anything else: say why, and keep the tab bar drawn.
|
|
460
|
+
let why = "";
|
|
461
|
+
try {
|
|
462
|
+
why = (await r.json()).error;
|
|
463
|
+
} catch {}
|
|
464
|
+
toast(why ? "No new terminal: " + why : "No new terminal (" + r.status + ")");
|
|
465
|
+
return draw();
|
|
466
|
+
}
|
|
467
|
+
const sh = await r.json();
|
|
468
|
+
shells.push(sh);
|
|
469
|
+
show("sh:" + sh.key);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async function closeTab(id: string) {
|
|
473
|
+
const s = saved();
|
|
474
|
+
if (id.startsWith("sh:")) {
|
|
475
|
+
await post("/pty/exit", { key: id.slice(3) });
|
|
476
|
+
shells = shells.filter((x) => "sh:" + x.key !== id);
|
|
477
|
+
} else if (id.startsWith("agent:")) s.hidden.push(id);
|
|
478
|
+
else if (id === "wait") elsewhere.delete(runKey(card));
|
|
479
|
+
else s.tabs = s.tabs.filter((t) => t.id !== id);
|
|
480
|
+
save();
|
|
481
|
+
draw();
|
|
482
|
+
}
|
|
483
|
+
// A tab's ×: everything in it closes (shells end, the agent is only hidden).
|
|
484
|
+
async function closeScreen(sid: string) {
|
|
485
|
+
const sc = saved().screens.find((x) => x.id === sid);
|
|
486
|
+
for (const id of sc ? leaves(sc.tree) : []) await closeTab(id);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// A pane's ⋯ menu, filled when it opens: every other tab, and a tab of its own.
|
|
490
|
+
function onMoveMenu(e: Event) {
|
|
491
|
+
const d = e.target as HTMLDetailsElement;
|
|
492
|
+
if (!d.classList?.contains("pmove") || !d.open) return;
|
|
493
|
+
const s = saved(),
|
|
494
|
+
id = d.closest<HTMLElement>(".rpane")!.dataset.t!,
|
|
495
|
+
ts = tabs(),
|
|
496
|
+
name = (sc: Screen) => leaves(sc.tree).map((x) => ts.find((t) => t.id === x)?.title ?? "").join(" + ");
|
|
497
|
+
const m = d.querySelector<HTMLElement>(".menu-pop")!;
|
|
498
|
+
m.innerHTML = String(
|
|
499
|
+
html`<b class="mh">Move to</b>${s.screens.filter((sc) => !leaves(sc.tree).includes(id)).map((sc) => html`<button data-mv="${sc.id}">${ico("arrow")}<span>${name(sc)}</span></button>`)}<button data-mv="">${ico("plus")}<span>New tab</span></button>`,
|
|
500
|
+
);
|
|
501
|
+
const r = d.querySelector("summary")!.getBoundingClientRect();
|
|
502
|
+
m.style.top = r.bottom + 4 + "px";
|
|
503
|
+
m.style.left = Math.max(8, r.right - m.offsetWidth) + "px";
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function wire() {
|
|
507
|
+
const bar = $("rtabs"),
|
|
508
|
+
s = saved();
|
|
509
|
+
bar.onclick = (e) => {
|
|
510
|
+
const t = e.target as HTMLElement;
|
|
511
|
+
const xs = t.closest<HTMLElement>("[data-xs]")?.dataset.xs;
|
|
512
|
+
if (xs) return void closeScreen(xs);
|
|
513
|
+
const tab = t.closest<HTMLElement>("[data-t]")?.dataset.t;
|
|
514
|
+
if (tab) {
|
|
515
|
+
s.on = tab;
|
|
516
|
+
save();
|
|
517
|
+
return draw();
|
|
518
|
+
}
|
|
519
|
+
const n = t.closest<HTMLElement>("[data-n]")?.dataset.n;
|
|
520
|
+
if (n) {
|
|
521
|
+
const d = t.closest<HTMLDetailsElement>(".radd")!;
|
|
522
|
+
const mode = d.dataset.mode;
|
|
523
|
+
d.open = false;
|
|
524
|
+
// + makes a new tab; split right/down adds to the tab shown.
|
|
525
|
+
target = mode === "tab" || !s.on ? null : { screen: s.on, dir: mode as "cols" | "rows" };
|
|
526
|
+
if (n === "shell") void newPty();
|
|
527
|
+
else if (n.startsWith("agent:")) void newPty(n.slice(6));
|
|
528
|
+
else if (n === "local") addTab({ id: "local", kind: "local", title: "localhost" });
|
|
529
|
+
else openPreview();
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (t.closest("#rside")) {
|
|
533
|
+
document.body.classList.toggle("rflip");
|
|
534
|
+
draw();
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
// The +, split right and split down menus open under their button, kept inside the
|
|
538
|
+
// centre panel; one open at a time. A pane's own × closes just that item.
|
|
539
|
+
const adds = [...bar.querySelectorAll<HTMLDetailsElement>(".radd")];
|
|
540
|
+
for (const add of adds)
|
|
541
|
+
add.ontoggle = () => {
|
|
542
|
+
if (!add.open) return;
|
|
543
|
+
for (const o of adds) if (o !== add) o.open = false;
|
|
544
|
+
const m = add.querySelector<HTMLElement>(".menu-pop")!,
|
|
545
|
+
r = add.querySelector("summary")!.getBoundingClientRect(),
|
|
546
|
+
box = $("runv").getBoundingClientRect();
|
|
547
|
+
m.style.top = r.bottom + 4 + "px";
|
|
548
|
+
m.style.left = Math.max(box.left + 4, Math.min(r.left, box.right - m.offsetWidth - 4)) + "px";
|
|
549
|
+
};
|
|
550
|
+
$("rpanes").onclick = (e) => {
|
|
551
|
+
const x = (e.target as HTMLElement).closest<HTMLElement>(".ph [data-x]")?.dataset.x;
|
|
552
|
+
if (x) return void closeTab(x);
|
|
553
|
+
if ((e.target as HTMLElement).closest("[data-b=recheck]")) return void openAgent(true);
|
|
554
|
+
const mv = (e.target as HTMLElement).closest<HTMLElement>("[data-mv]");
|
|
555
|
+
if (mv) {
|
|
556
|
+
const id = mv.closest<HTMLElement>(".rpane")!.dataset.t!;
|
|
557
|
+
mv.closest("details")!.open = false;
|
|
558
|
+
return moveItem(id, mv.dataset.mv || null);
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
// Moving panes: the header's ⋯ menu lists the other tabs; dragging the header drops it on a
|
|
562
|
+
// tab, on + (a tab of its own) or on a pane's edge (split there on that side).
|
|
563
|
+
const box = $("rpanes");
|
|
564
|
+
box.ontoggle = null;
|
|
565
|
+
box.addEventListener("toggle", onMoveMenu, true);
|
|
566
|
+
box.ondragstart = (e) => {
|
|
567
|
+
const id = (e.target as HTMLElement).closest<HTMLElement>(".ph")?.closest<HTMLElement>(".rpane")?.dataset.t;
|
|
568
|
+
if (!id) return;
|
|
569
|
+
dragging = id;
|
|
570
|
+
e.dataTransfer!.setData("text/plain", id);
|
|
571
|
+
e.dataTransfer!.effectAllowed = "move";
|
|
572
|
+
};
|
|
573
|
+
box.ondragend = bar.ondragend = () => {
|
|
574
|
+
dragging = null;
|
|
575
|
+
document.querySelectorAll(".rdrop,.rdrop-l,.rdrop-r,.rdrop-t,.rdrop-b").forEach((x) => x.classList.remove("rdrop", "rdrop-l", "rdrop-r", "rdrop-t", "rdrop-b"));
|
|
576
|
+
};
|
|
577
|
+
const edge = (el: HTMLElement, e: DragEvent) => {
|
|
578
|
+
const r = el.getBoundingClientRect(),
|
|
579
|
+
x = (e.clientX - r.left) / r.width,
|
|
580
|
+
y = (e.clientY - r.top) / r.height;
|
|
581
|
+
const d = [["l", x], ["r", 1 - x], ["t", y], ["b", 1 - y]].sort((a, b) => (a[1] as number) - (b[1] as number))[0][0] as string;
|
|
582
|
+
return d;
|
|
583
|
+
};
|
|
584
|
+
box.ondragover = (e) => {
|
|
585
|
+
const pane = (e.target as HTMLElement).closest<HTMLElement>(".rpane");
|
|
586
|
+
if (!dragging || !pane || pane.dataset.t === dragging) return;
|
|
587
|
+
e.preventDefault();
|
|
588
|
+
const d = edge(pane, e);
|
|
589
|
+
pane.classList.remove("rdrop-l", "rdrop-r", "rdrop-t", "rdrop-b");
|
|
590
|
+
pane.classList.add("rdrop-" + d);
|
|
591
|
+
};
|
|
592
|
+
box.ondragleave = (e) => (e.target as HTMLElement).closest(".rpane")?.classList.remove("rdrop-l", "rdrop-r", "rdrop-t", "rdrop-b");
|
|
593
|
+
box.ondrop = (e) => {
|
|
594
|
+
const pane = (e.target as HTMLElement).closest<HTMLElement>(".rpane"),
|
|
595
|
+
id = dragging;
|
|
596
|
+
if (!id || !pane || pane.dataset.t === id) return;
|
|
597
|
+
e.preventDefault();
|
|
598
|
+
const d = edge(pane, e);
|
|
599
|
+
moveItem(id, s.on, { pane: pane.dataset.t!, dir: d === "l" || d === "r" ? "cols" : "rows", before: d === "l" || d === "t" });
|
|
600
|
+
};
|
|
601
|
+
bar.ondragover = (e) => {
|
|
602
|
+
const t = (e.target as HTMLElement).closest<HTMLElement>(".rtab,.radd[data-mode=tab]");
|
|
603
|
+
if (!dragging || !t) return;
|
|
604
|
+
e.preventDefault();
|
|
605
|
+
bar.querySelectorAll(".rdrop").forEach((x) => x.classList.remove("rdrop"));
|
|
606
|
+
t.classList.add("rdrop");
|
|
607
|
+
};
|
|
608
|
+
bar.ondrop = (e) => {
|
|
609
|
+
const t = (e.target as HTMLElement).closest<HTMLElement>(".rtab,.radd[data-mode=tab]"),
|
|
610
|
+
id = dragging;
|
|
611
|
+
if (!id || !t) return;
|
|
612
|
+
e.preventDefault();
|
|
613
|
+
moveItem(id, t.classList.contains("rtab") ? t.dataset.t! : null);
|
|
614
|
+
};
|
|
615
|
+
// The pane last clicked is the one a split divides.
|
|
616
|
+
$("rpanes").onpointerdown = (e) => {
|
|
617
|
+
const id = (e.target as HTMLElement).closest<HTMLElement>(".rpane")?.dataset.t,
|
|
618
|
+
sc = saved().screens.find((x) => x.id === saved().on);
|
|
619
|
+
if (id && sc && sc.focus !== id) {
|
|
620
|
+
sc.focus = id;
|
|
621
|
+
save();
|
|
622
|
+
for (const [k, p] of panes) p.el.classList.toggle("focus", p.el.classList.contains("split") && k === id);
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// Leaving the workspace: its terminals unmount; the shells keep running on the server.
|
|
628
|
+
function stopPanes() {
|
|
629
|
+
for (const p of panes.values()) p.off?.();
|
|
630
|
+
panes.clear();
|
|
631
|
+
$("rpanes").innerHTML = "";
|
|
632
|
+
}
|
|
633
|
+
export function runStop() {
|
|
634
|
+
stopPanes();
|
|
635
|
+
card = null;
|
|
636
|
+
$("panel").classList.remove("pn");
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// The strip's Project notes: the project's notes as a tree in the panel; a note opens as a centre tab.
|
|
640
|
+
async function projectNotes() {
|
|
641
|
+
const pn = $("panel");
|
|
642
|
+
if (pn.classList.toggle("pn") === false) return;
|
|
643
|
+
const box = $("pnlist");
|
|
644
|
+
box.innerHTML = "";
|
|
645
|
+
try {
|
|
646
|
+
const d = await (await fetch("/vault?dir=" + encodeURIComponent(card.state))).json();
|
|
647
|
+
const names: string[] = (d.notes ?? []).map((n) => n.name).sort();
|
|
648
|
+
// Folders first-level as headings, notes under them: "Project/Web board" shows under Project.
|
|
649
|
+
const groups = new Map<string, string[]>();
|
|
650
|
+
for (const n of names) {
|
|
651
|
+
const i = n.lastIndexOf("/"),
|
|
652
|
+
g = i < 0 ? "" : n.slice(0, i);
|
|
653
|
+
if (!groups.has(g)) groups.set(g, []);
|
|
654
|
+
groups.get(g)!.push(n);
|
|
655
|
+
}
|
|
656
|
+
box.innerHTML = String(
|
|
657
|
+
html`<h3>Project notes</h3>${names.length ? [...groups].map(([g, ns]) => html`${g ? html`<div class="tf dir">${ico("side")}${g}</div>` : ""}<div class="${g ? "tree" : ""}">${ns.map((n) => html`<button class="tf" data-note="${n}">${ico("note")}${n.slice(n.lastIndexOf("/") + 1)}</button>`)}</div>`) : html`<p class="rnone">No notes in this project yet.</p>`}`,
|
|
658
|
+
);
|
|
659
|
+
} catch {
|
|
660
|
+
box.textContent = "The notes could not load.";
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
export function initRunView() {
|
|
665
|
+
// A phone's sheet closes with the panel's ×, which here hides the sheet rather than closing the card.
|
|
666
|
+
document.addEventListener(
|
|
667
|
+
"click",
|
|
668
|
+
(e) => {
|
|
669
|
+
if (!document.body.classList.contains("runv") || !(e.target as Element).closest?.("#pclose")) return;
|
|
670
|
+
e.stopPropagation();
|
|
671
|
+
document.body.classList.remove("rflip");
|
|
672
|
+
},
|
|
673
|
+
true,
|
|
674
|
+
);
|
|
675
|
+
addEventListener("kz-run-preview", openPreview);
|
|
676
|
+
$("pnotes").onclick = () => void projectNotes();
|
|
677
|
+
$("pnlist").onclick = (e) => {
|
|
678
|
+
const n = (e.target as HTMLElement).closest<HTMLElement>("[data-note]")?.dataset.note;
|
|
679
|
+
if (n) openNoteTab(n);
|
|
680
|
+
};
|
|
681
|
+
// A /state change: new or ended sessions, the card's own state. Panes stay mounted.
|
|
682
|
+
addEventListener("kz-state", () => {
|
|
683
|
+
if (card && document.body.classList.contains("runv")) void showRunView();
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
// Opens a card's workspace, its address in the history.
|
|
687
|
+
export function openWorkspace(c) {
|
|
688
|
+
const u = cardHref(c);
|
|
689
|
+
if (location.pathname + location.search !== u) history.pushState(null, "", u);
|
|
690
|
+
go("run");
|
|
691
|
+
}
|