@bigmistqke/workpad 0.0.0-stage → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1509 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1190 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2098 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +211 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +341 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +665 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +588 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +133 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +137 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +238 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +426 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +748 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import "node:os";
|
|
5
|
+
import net from "node:net";
|
|
6
|
+
import crypto from "node:crypto";
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
// plugins/terminal/server.ts
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
const HOST = fileURLToPath(new URL(`./host${path.extname(fileURLToPath(import.meta.url))}`, import.meta.url));
|
|
12
|
+
const hasPty = () => {
|
|
13
|
+
try {
|
|
14
|
+
require.resolve("node-pty");
|
|
15
|
+
return true;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Where the host listens: `host.sock` in the terminal's folder, or, when that path is too long for
|
|
22
|
+
* a Unix socket (about 100 bytes), a name in /tmp made from it.
|
|
23
|
+
*/
|
|
24
|
+
function hostSocket(home) {
|
|
25
|
+
const own = path.join(home, "host.sock");
|
|
26
|
+
return Buffer.byteLength(own) < 100 ? own : path.join("/tmp", `wp-term-${crypto.createHash("sha1").update(home).digest("hex").slice(0, 12)}.sock`);
|
|
27
|
+
}
|
|
28
|
+
/** `dir` the folder the sessions run in, `home` where the socket and log go */
|
|
29
|
+
function createTerminal({ dir, home }) {
|
|
30
|
+
const own = hostSocket(home);
|
|
31
|
+
const log = path.join(home, "host.log");
|
|
32
|
+
/** A connection to the host, or null when none runs. */
|
|
33
|
+
function connect() {
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
const conn = net.connect(own);
|
|
36
|
+
conn.once("connect", () => resolve(conn));
|
|
37
|
+
conn.once("error", () => resolve(null));
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/** Send one request; its one reply, or null when no host runs. */
|
|
41
|
+
async function ask(req, timeout = 2e3) {
|
|
42
|
+
const conn = await connect();
|
|
43
|
+
if (!conn) return null;
|
|
44
|
+
return new Promise((resolve) => {
|
|
45
|
+
let buf = "";
|
|
46
|
+
const done = (value) => {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
conn.destroy();
|
|
49
|
+
resolve(value);
|
|
50
|
+
};
|
|
51
|
+
const timer = setTimeout(() => done(null), timeout);
|
|
52
|
+
conn.setEncoding("utf8");
|
|
53
|
+
conn.on("data", (c) => {
|
|
54
|
+
buf += c;
|
|
55
|
+
const nl = buf.indexOf("\n");
|
|
56
|
+
if (nl >= 0) try {
|
|
57
|
+
done(JSON.parse(buf.slice(0, nl)));
|
|
58
|
+
} catch {
|
|
59
|
+
done(null);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
conn.on("error", () => done(null));
|
|
63
|
+
conn.on("end", () => done(null));
|
|
64
|
+
conn.write(JSON.stringify(req) + "\n");
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Start the host (detached: it outlives this server) and wait until it answers. */
|
|
68
|
+
async function startHost() {
|
|
69
|
+
const out = fs.openSync(log, "a");
|
|
70
|
+
spawn(process.execPath, [
|
|
71
|
+
HOST,
|
|
72
|
+
dir,
|
|
73
|
+
own
|
|
74
|
+
], {
|
|
75
|
+
cwd: dir,
|
|
76
|
+
detached: true,
|
|
77
|
+
stdio: [
|
|
78
|
+
"ignore",
|
|
79
|
+
out,
|
|
80
|
+
out
|
|
81
|
+
],
|
|
82
|
+
env: process.env
|
|
83
|
+
}).unref();
|
|
84
|
+
fs.closeSync(out);
|
|
85
|
+
for (let i = 0; i < 40; i++) {
|
|
86
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
87
|
+
if (await ask({ t: "list" }, 500)) return true;
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const shell = process.env.SHELL || "/bin/sh";
|
|
92
|
+
/** The sessions, as tabs: `{ id, label, running, exited, started }` each. */
|
|
93
|
+
async function sessions() {
|
|
94
|
+
const reply = await ask({ t: "list" });
|
|
95
|
+
return {
|
|
96
|
+
available: hasPty(),
|
|
97
|
+
sessions: reply?.sessions ?? []
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** The screens as HTML rows by session id, for a page's first render. */
|
|
101
|
+
async function screens(list) {
|
|
102
|
+
const out = {};
|
|
103
|
+
await Promise.all(list.map(async (s) => {
|
|
104
|
+
out[s.id] = (await ask({
|
|
105
|
+
t: "html",
|
|
106
|
+
id: s.id
|
|
107
|
+
}, 300))?.html ?? "";
|
|
108
|
+
}));
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
const failure = (message, status = 500) => Object.assign(new Error(message), { status });
|
|
112
|
+
/**
|
|
113
|
+
* Start a tab: the shell, or `program` in it (a new tab without `id`). In a running session it
|
|
114
|
+
* reports it (`already`) unless `restart`; restarting without a program runs what it ran before.
|
|
115
|
+
*/
|
|
116
|
+
async function start({ id, program = [], restart = false } = {}) {
|
|
117
|
+
if (!hasPty()) throw failure("the terminal needs node-pty: npm install node-pty");
|
|
118
|
+
const request = program.length ? {
|
|
119
|
+
t: "start",
|
|
120
|
+
id,
|
|
121
|
+
command: shell,
|
|
122
|
+
args: [
|
|
123
|
+
"-c",
|
|
124
|
+
"\"$@\"; exec \"$0\" -l",
|
|
125
|
+
shell,
|
|
126
|
+
...program
|
|
127
|
+
],
|
|
128
|
+
label: path.basename(program[0]),
|
|
129
|
+
restart
|
|
130
|
+
} : restart && id != null ? {
|
|
131
|
+
t: "start",
|
|
132
|
+
id,
|
|
133
|
+
restart
|
|
134
|
+
} : {
|
|
135
|
+
t: "start",
|
|
136
|
+
id,
|
|
137
|
+
command: shell,
|
|
138
|
+
args: ["-l"],
|
|
139
|
+
label: path.basename(shell),
|
|
140
|
+
restart
|
|
141
|
+
};
|
|
142
|
+
let reply = await ask(request);
|
|
143
|
+
if (!reply) {
|
|
144
|
+
if (!await startHost()) throw failure(`the terminal's host did not start; see ${log}`);
|
|
145
|
+
reply = await ask(request);
|
|
146
|
+
}
|
|
147
|
+
if (!reply || reply.error) throw failure(reply?.error ?? "no answer from the terminal host");
|
|
148
|
+
return {
|
|
149
|
+
available: true,
|
|
150
|
+
session: reply.session ?? "1",
|
|
151
|
+
already: !!reply.already
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const input = async (id, data) => {
|
|
155
|
+
await ask({
|
|
156
|
+
t: "input",
|
|
157
|
+
id,
|
|
158
|
+
d: data
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
/** The size that fits drawer `viewer`; `active`: the reader is using that drawer now (see host.ts, Sizes). */
|
|
162
|
+
const resize = async (id, viewer, cols, rows, active = false) => {
|
|
163
|
+
await ask({
|
|
164
|
+
t: "resize",
|
|
165
|
+
id,
|
|
166
|
+
viewer,
|
|
167
|
+
cols,
|
|
168
|
+
rows,
|
|
169
|
+
active
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
/** End one session and drop its tab. */
|
|
173
|
+
async function close(id) {
|
|
174
|
+
await ask({
|
|
175
|
+
t: "close",
|
|
176
|
+
id
|
|
177
|
+
});
|
|
178
|
+
return { ok: true };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Follow a session as server-sent events on `res`: `sessions` (the tabs) and `screen` first, then
|
|
182
|
+
* `data`, `exit` and `sessions` as they come.
|
|
183
|
+
*/
|
|
184
|
+
async function stream(req, res, id, viewer) {
|
|
185
|
+
res.writeHead(200, {
|
|
186
|
+
"content-type": "text/event-stream",
|
|
187
|
+
"cache-control": "no-store",
|
|
188
|
+
connection: "keep-alive"
|
|
189
|
+
});
|
|
190
|
+
res.write(": terminal\n\n");
|
|
191
|
+
const ping = setInterval(() => res.write(": ping\n\n"), 25e3);
|
|
192
|
+
const conn = await connect();
|
|
193
|
+
if (!conn) {
|
|
194
|
+
clearInterval(ping);
|
|
195
|
+
res.end("retry: 2000\nevent: none\ndata: {}\n\n");
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
let buf = "";
|
|
199
|
+
conn.setEncoding("utf8");
|
|
200
|
+
conn.on("data", (chunk) => {
|
|
201
|
+
buf += chunk;
|
|
202
|
+
let nl;
|
|
203
|
+
while ((nl = buf.indexOf("\n")) >= 0) {
|
|
204
|
+
const line = buf.slice(0, nl);
|
|
205
|
+
buf = buf.slice(nl + 1);
|
|
206
|
+
try {
|
|
207
|
+
const { t, ...data } = JSON.parse(line);
|
|
208
|
+
res.write(`event: ${t}\ndata: ${JSON.stringify(data)}\n\n`);
|
|
209
|
+
} catch {}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
conn.on("close", () => {
|
|
213
|
+
if (!res.writableEnded) res.write("event: exit\ndata: {\"code\":null}\n\n");
|
|
214
|
+
});
|
|
215
|
+
conn.on("error", () => {});
|
|
216
|
+
req.on("close", () => {
|
|
217
|
+
clearInterval(ping);
|
|
218
|
+
conn.destroy();
|
|
219
|
+
});
|
|
220
|
+
conn.write(JSON.stringify({
|
|
221
|
+
t: "attach",
|
|
222
|
+
id,
|
|
223
|
+
viewer
|
|
224
|
+
}) + "\n");
|
|
225
|
+
}
|
|
226
|
+
/** End the session and its host. */
|
|
227
|
+
async function stop() {
|
|
228
|
+
return { stopped: !!await ask({ t: "stop" }) };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Show the page at `path` (a path on this server) in the browser tab whose drawer the reader used
|
|
232
|
+
* last: for session `id`, or without one, of all sessions.
|
|
233
|
+
*/
|
|
234
|
+
async function open(id, path) {
|
|
235
|
+
if (!path.startsWith("/") || path.startsWith("//")) throw failure("open takes a path on this server, like /plan", 400);
|
|
236
|
+
const reply = await ask({
|
|
237
|
+
t: "open",
|
|
238
|
+
id,
|
|
239
|
+
path
|
|
240
|
+
});
|
|
241
|
+
if (!reply) throw failure("no terminal host runs", 404);
|
|
242
|
+
return { shown: Number(reply.shown ?? 0) };
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
sessions,
|
|
246
|
+
screens,
|
|
247
|
+
start,
|
|
248
|
+
input,
|
|
249
|
+
resize,
|
|
250
|
+
close,
|
|
251
|
+
open,
|
|
252
|
+
stream,
|
|
253
|
+
stop
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
export { createTerminal, hostSocket };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// plugins/terminal/sizes.ts
|
|
2
|
+
/**
|
|
3
|
+
* A drawer reports the size that fits it; `used`: the reader uses it now. Returns the size the
|
|
4
|
+
* session should have: its own when the drawer is (now) the active one, else the session's as it is.
|
|
5
|
+
*/
|
|
6
|
+
function report(s, viewer, cols, rows, used, now = Date.now()) {
|
|
7
|
+
const known = s.viewers.get(viewer);
|
|
8
|
+
s.viewers.set(viewer, {
|
|
9
|
+
cols,
|
|
10
|
+
rows,
|
|
11
|
+
used: used ? now : known?.used ?? 0
|
|
12
|
+
});
|
|
13
|
+
if (used || !s.active || !s.viewers.has(s.active)) s.active = viewer;
|
|
14
|
+
return s.active === viewer ? {
|
|
15
|
+
cols,
|
|
16
|
+
rows
|
|
17
|
+
} : {
|
|
18
|
+
cols: s.cols,
|
|
19
|
+
rows: s.rows
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** A drawer stopped showing the session. True when it was the active one: call `handOver` after a moment. */
|
|
23
|
+
function leave(s, viewer) {
|
|
24
|
+
s.viewers.delete(viewer);
|
|
25
|
+
return s.active === viewer;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* After the active drawer `viewer` left: the drawer used most recently of those left becomes the
|
|
29
|
+
* active one. Returns the size the session should take, or null to leave it as it is (another drawer
|
|
30
|
+
* claimed the session in the meantime, or none is left).
|
|
31
|
+
*/
|
|
32
|
+
function handOver(s, viewer) {
|
|
33
|
+
if (s.active !== viewer || s.viewers.has(viewer)) return null;
|
|
34
|
+
let next;
|
|
35
|
+
for (const viewer of s.viewers) if (!next || viewer[1].used > next[1].used) next = viewer;
|
|
36
|
+
s.active = next?.[0] ?? null;
|
|
37
|
+
return next ? {
|
|
38
|
+
cols: next[1].cols,
|
|
39
|
+
rows: next[1].rows
|
|
40
|
+
} : null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Of several sessions, the one whose active drawer the reader used last: where "the browser the
|
|
44
|
+
* reader is in" is, for a program that doesn't know its own tab. Undefined when no drawer is active.
|
|
45
|
+
*/
|
|
46
|
+
function lastUsed(sessions) {
|
|
47
|
+
let best;
|
|
48
|
+
let at = -1;
|
|
49
|
+
for (const s of sessions) {
|
|
50
|
+
const viewer = s.active ? s.viewers.get(s.active) : void 0;
|
|
51
|
+
if (viewer && viewer.used > at) {
|
|
52
|
+
best = s;
|
|
53
|
+
at = viewer.used;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return best;
|
|
57
|
+
}
|
|
58
|
+
export { handOver, lastUsed, leave, report };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Steps in order down a line, a dot per step: what was done and what it showed.
|
|
2
|
+
Each step has room for full sentences, unlike a diagram's boxes.
|
|
3
|
+
|
|
4
|
+
Each step is an `:::item[Title]` container, titled by its label; without a
|
|
5
|
+
label it is untitled. The timeline takes one colon more than its steps:
|
|
6
|
+
`::::timeline` around `:::item`. Headings in a step are the step's own, at
|
|
7
|
+
any level, and stay headings in its body ("What it does", "Result"). A
|
|
8
|
+
step's body is any markdown, blocks too: a takeaway that should stand out
|
|
9
|
+
can be an alert, `> [!TIP] The takeaway`. A badge ending a step's label sits
|
|
10
|
+
at its right. A `:when[2026-11-21 10:43]` in a step's label is when it
|
|
11
|
+
happened: shown above the title, as written. It works in any heading too,
|
|
12
|
+
on the page or in a step's body: wherever it is written in the heading, it
|
|
13
|
+
shows above it, and it is not in the heading's anchor. A `:::form` or a `:::details` directly in the timeline is a
|
|
14
|
+
step as it is, its own label its title.
|
|
15
|
+
|
|
16
|
+
**Sub steps.** A timeline inside a step is that step's sub steps: drawn a level
|
|
17
|
+
in, on a thinner line, and a timeline inside one of those again a level
|
|
18
|
+
further, to any depth. Each container takes one colon more than what it
|
|
19
|
+
holds: the step around a timeline five, the timeline around that step six.
|
|
20
|
+
That is also how steps are grouped: a step for the group, its steps in a
|
|
21
|
+
timeline inside it.
|
|
22
|
+
|
|
23
|
+
````markdown
|
|
24
|
+
::::::timeline[Steps so far]
|
|
25
|
+
:::item[:when[2026-11-21 10:43] Made a minimal repro :badge[done]]
|
|
26
|
+
A chain of 6,400 closures overflows the stack: the crash is the chain, not
|
|
27
|
+
Solid.
|
|
28
|
+
:::
|
|
29
|
+
|
|
30
|
+
:::::item[Counted the visits]
|
|
31
|
+
5,150 visits for 100 links: each link walks the whole chain again.
|
|
32
|
+
|
|
33
|
+
::::timeline
|
|
34
|
+
:::item[With the probe off]
|
|
35
|
+
#### What it does
|
|
36
|
+
Runs the same chain with the probe switched off.
|
|
37
|
+
|
|
38
|
+
#### Result
|
|
39
|
+
The same 5,150: the probe isn't what walks it.
|
|
40
|
+
:::
|
|
41
|
+
|
|
42
|
+
:::item[With the cache on]
|
|
43
|
+
100 visits: one per link.
|
|
44
|
+
:::
|
|
45
|
+
::::
|
|
46
|
+
:::::
|
|
47
|
+
::::::
|
|
48
|
+
````
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { builtinPublic } from "../../src/package.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { definePlugin, html, raw } from "@bigmistqke/workpad";
|
|
4
|
+
// plugins/timeline/index.ts
|
|
5
|
+
const text = (node) => node.value ?? (node.children ?? []).map(text).join("");
|
|
6
|
+
/** A when's text. Its `:59` in `19:59` parses as a directive named `59`: put it back as written. */
|
|
7
|
+
const whenText = (node) => (node.children ?? []).map((n) => n.type === "textDirective" ? `:${n.name}${n.children?.length ? `[${text(n)}]` : ""}` : text(n)).join("");
|
|
8
|
+
/** Takes a `:when[…]` out of a heading: its text, and the heading without it. */
|
|
9
|
+
function when(title) {
|
|
10
|
+
const found = title.find((n) => n.type === "textDirective" && n.name === "when");
|
|
11
|
+
if (!found) return {
|
|
12
|
+
when: null,
|
|
13
|
+
title
|
|
14
|
+
};
|
|
15
|
+
const rest = title.filter((n) => n !== found);
|
|
16
|
+
const last = rest[rest.length - 1];
|
|
17
|
+
if (last?.type === "text" && last.value) last.value = last.value.trimEnd();
|
|
18
|
+
const first = rest[0];
|
|
19
|
+
if (first?.type === "text" && first.value) first.value = first.value.trimStart();
|
|
20
|
+
return {
|
|
21
|
+
when: whenText(found).trim(),
|
|
22
|
+
title: rest
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A step (an item, DirectiveContext.items): when, title and body. Its label, as written, is its title,
|
|
27
|
+
* a `:when` in it taken out. A form or a details that is an item is the step's body as it is, its
|
|
28
|
+
* label its own title, never shown twice. A timeline in the body (its sub steps) renders as its own
|
|
29
|
+
* block.
|
|
30
|
+
*/
|
|
31
|
+
async function renderStep(ctx, item) {
|
|
32
|
+
const label = item.block ? null : item.label;
|
|
33
|
+
const split = label ? when(label) : {
|
|
34
|
+
when: null,
|
|
35
|
+
title: null
|
|
36
|
+
};
|
|
37
|
+
const title = split.title?.length ? await ctx.toHtml(split.title) : null;
|
|
38
|
+
return html`<li class="wp-tl-step">
|
|
39
|
+
${split.when && html`<time class="wp-tl-when">${split.when}</time>`}
|
|
40
|
+
${title?.trim() && html`<div class="wp-tl-title" data-wp-title>${raw(title)}</div>`}
|
|
41
|
+
${item.html.trim() && html`<div class="wp-tl-body">${raw(item.html)}</div>`}
|
|
42
|
+
</li>`;
|
|
43
|
+
}
|
|
44
|
+
/** `:when[…]` anywhere else, in any heading: shown above it (style.css), as in a step. */
|
|
45
|
+
function renderWhen(ctx) {
|
|
46
|
+
const at = whenText(ctx.node).trim();
|
|
47
|
+
return at ? html`<time class="wp-when">${at}</time>` : html``;
|
|
48
|
+
}
|
|
49
|
+
async function render(ctx) {
|
|
50
|
+
const steps = await Promise.all((await ctx.items()).map((item) => renderStep(ctx, item)));
|
|
51
|
+
return html`<div class="wp-timeline">
|
|
52
|
+
${ctx.label && html`<div class="wp-tl-caption">${raw(ctx.label)}</div>`}
|
|
53
|
+
<ol class="wp-tl-steps">
|
|
54
|
+
${steps}
|
|
55
|
+
</ol>
|
|
56
|
+
</div>`;
|
|
57
|
+
}
|
|
58
|
+
function timeline() {
|
|
59
|
+
return definePlugin({
|
|
60
|
+
name: "timeline",
|
|
61
|
+
public: builtinPublic("timeline"),
|
|
62
|
+
style: "style.css",
|
|
63
|
+
blocks: {
|
|
64
|
+
timeline: {
|
|
65
|
+
attrs: [],
|
|
66
|
+
html: render,
|
|
67
|
+
menu: {
|
|
68
|
+
group: "arrange",
|
|
69
|
+
summary: "in order, down a line: what happened when"
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
when: {
|
|
73
|
+
html: renderWhen,
|
|
74
|
+
menu: {
|
|
75
|
+
group: "mark",
|
|
76
|
+
summary: "a date or time, on a heading"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
docs: () => fs.readFileSync(new URL("./README.md", import.meta.url), "utf8")
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
export { timeline as default, renderWhen, when };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
.wp-timeline {
|
|
2
|
+
margin: var(--gap-block) 0;
|
|
3
|
+
text-align: left;
|
|
4
|
+
}
|
|
5
|
+
.wp-tl-caption {
|
|
6
|
+
font-family: var(--sans);
|
|
7
|
+
font-size: var(--ui-small);
|
|
8
|
+
color: var(--muted);
|
|
9
|
+
text-transform: uppercase;
|
|
10
|
+
letter-spacing: 0.06em;
|
|
11
|
+
margin-bottom: var(--space-2xl);
|
|
12
|
+
}
|
|
13
|
+
.wp-tl-steps {
|
|
14
|
+
list-style: none;
|
|
15
|
+
margin: 0;
|
|
16
|
+
padding: 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/* a line down the left, a dot per step */
|
|
20
|
+
.wp-tl-steps {
|
|
21
|
+
position: relative;
|
|
22
|
+
padding-left: 28px;
|
|
23
|
+
}
|
|
24
|
+
.wp-tl-steps::before {
|
|
25
|
+
content: '';
|
|
26
|
+
position: absolute;
|
|
27
|
+
left: 7px;
|
|
28
|
+
top: 6px;
|
|
29
|
+
bottom: 6px;
|
|
30
|
+
width: 2px;
|
|
31
|
+
background: var(--border);
|
|
32
|
+
}
|
|
33
|
+
.wp-tl-step {
|
|
34
|
+
position: relative;
|
|
35
|
+
}
|
|
36
|
+
/* between steps: a subsection's distance, at the rhythm inside a step (base.css), more than a
|
|
37
|
+
step's title is from its body */
|
|
38
|
+
.wp-tl-step:not(:last-child) > :last-child {
|
|
39
|
+
margin-bottom: var(--gap-subsection);
|
|
40
|
+
}
|
|
41
|
+
/* the dot sits on the title's first line; a step without a title has it at its top */
|
|
42
|
+
.wp-tl-step:has(> .wp-tl-title)::before {
|
|
43
|
+
display: none;
|
|
44
|
+
}
|
|
45
|
+
.wp-tl-title {
|
|
46
|
+
position: relative;
|
|
47
|
+
}
|
|
48
|
+
.wp-tl-step::before,
|
|
49
|
+
.wp-tl-title::before {
|
|
50
|
+
content: '';
|
|
51
|
+
position: absolute;
|
|
52
|
+
left: -27px;
|
|
53
|
+
top: 0.45em;
|
|
54
|
+
width: 12px;
|
|
55
|
+
height: 12px;
|
|
56
|
+
border-radius: 50%;
|
|
57
|
+
background: var(--bg);
|
|
58
|
+
border: 2px solid var(--accent);
|
|
59
|
+
box-sizing: border-box;
|
|
60
|
+
}
|
|
61
|
+
.wp-tl-when {
|
|
62
|
+
display: block;
|
|
63
|
+
font-family: var(--mono);
|
|
64
|
+
font-size: var(--ui-tiny);
|
|
65
|
+
color: var(--muted);
|
|
66
|
+
line-height: 1.4;
|
|
67
|
+
}
|
|
68
|
+
/* centred on the title's first line (line-height 1.4: its middle is 0.7em down) */
|
|
69
|
+
.wp-tl-title::before {
|
|
70
|
+
top: calc(0.7em - 6px);
|
|
71
|
+
}
|
|
72
|
+
.wp-tl-title {
|
|
73
|
+
font-weight: 600;
|
|
74
|
+
line-height: 1.4;
|
|
75
|
+
}
|
|
76
|
+
/* the body under its title as a heading's content under it; inside, the page's steps (base.css) */
|
|
77
|
+
.wp-tl-body {
|
|
78
|
+
font-size: var(--ui-text);
|
|
79
|
+
line-height: 1.6;
|
|
80
|
+
margin-top: var(--gap-heading);
|
|
81
|
+
}
|
|
82
|
+
.wp-timeline .wp-tl-body > :first-child {
|
|
83
|
+
margin-top: 0;
|
|
84
|
+
}
|
|
85
|
+
/* a body that starts with a heading: two headings in a row, closer */
|
|
86
|
+
.wp-tl-body:has(> :first-child:is(h2, h3, h4, h5, h6)) {
|
|
87
|
+
margin-top: var(--gap-heading-next);
|
|
88
|
+
}
|
|
89
|
+
.wp-timeline .wp-tl-body > :last-child {
|
|
90
|
+
margin-bottom: 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* a timeline inside a step is its sub steps: a level in, a thinner line and smaller rings, the
|
|
94
|
+
same at every depth */
|
|
95
|
+
/* its sub steps hold headings and steps of their own: around them the space between steps, so the
|
|
96
|
+
space outside them is no smaller than any inside */
|
|
97
|
+
.wp-tl-body .wp-timeline {
|
|
98
|
+
margin-block: var(--gap-subsection);
|
|
99
|
+
}
|
|
100
|
+
.wp-tl-body .wp-tl-steps {
|
|
101
|
+
padding-left: 20px;
|
|
102
|
+
}
|
|
103
|
+
.wp-tl-body .wp-tl-steps::before {
|
|
104
|
+
left: 4px;
|
|
105
|
+
width: 1px;
|
|
106
|
+
}
|
|
107
|
+
.wp-tl-body .wp-tl-step::before,
|
|
108
|
+
.wp-tl-body .wp-tl-title::before {
|
|
109
|
+
left: -20px;
|
|
110
|
+
width: 9px;
|
|
111
|
+
height: 9px;
|
|
112
|
+
border-width: 1.5px;
|
|
113
|
+
}
|
|
114
|
+
.wp-tl-body .wp-tl-title::before {
|
|
115
|
+
top: calc(0.7em - 4.5px);
|
|
116
|
+
}
|
|
117
|
+
.wp-tl-body .wp-tl-title {
|
|
118
|
+
font-size: var(--ui-text);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* a :when in any other heading: above it, wherever it was written in it; the room it takes comes
|
|
122
|
+
on top of the heading's own margin */
|
|
123
|
+
:is(h1, h2, h3, h4, h5, h6):has(> .wp-when) {
|
|
124
|
+
position: relative;
|
|
125
|
+
padding-top: calc(var(--ui-tiny) * 1.4);
|
|
126
|
+
}
|
|
127
|
+
:is(h1, h2, h3, h4, h5, h6) > .wp-when {
|
|
128
|
+
position: absolute;
|
|
129
|
+
top: 0;
|
|
130
|
+
left: 0;
|
|
131
|
+
font-family: var(--mono);
|
|
132
|
+
font-size: var(--ui-tiny);
|
|
133
|
+
font-weight: normal;
|
|
134
|
+
font-style: normal;
|
|
135
|
+
color: var(--muted);
|
|
136
|
+
line-height: 1.4;
|
|
137
|
+
white-space: nowrap;
|
|
138
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
A table of contents beside the page, from its headings (`##` down to
|
|
2
|
+
`######`), numbered as the page numbers them, each section's subsections on a
|
|
3
|
+
line under it. The section being read is marked, a click
|
|
4
|
+
goes to a section, and a section with an unanswered question or an unread
|
|
5
|
+
message has a dot. It sits in the margin left of the text when the window has
|
|
6
|
+
room; else a button in the header opens it. It is built in: a page has it
|
|
7
|
+
when its frontmatter asks, under `plugins:` (where every plugin's settings for
|
|
8
|
+
a page go, by the plugin's name):
|
|
9
|
+
|
|
10
|
+
```markdown
|
|
11
|
+
---
|
|
12
|
+
plugins:
|
|
13
|
+
toc: true
|
|
14
|
+
---
|
|
15
|
+
# A long page
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
To have it on every page, replace the built-in one in the config; a page then
|
|
19
|
+
goes without with `toc: false` there:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import toc from '@bigmistqke/workpad/toc'
|
|
23
|
+
export default defineConfig({ plugins: [toc({ always: true })] })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The panel fits its items, between a minimum and a maximum width (160px and
|
|
27
|
+
320px by default). A project sets its own in the options, and a page in its
|
|
28
|
+
frontmatter, which is then also how it asks for the contents:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
toc({ minWidth: 180, maxWidth: '24rem' }) // pixels, or a CSS length
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```markdown
|
|
35
|
+
---
|
|
36
|
+
plugins:
|
|
37
|
+
toc:
|
|
38
|
+
maxWidth: 240px
|
|
39
|
+
---
|
|
40
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { builtinPublic } from "../../src/package.js";
|
|
2
|
+
import { html } from "../../src/html.js";
|
|
3
|
+
// plugins/toc/index.ts
|
|
4
|
+
const DOCS = `## Table of contents
|
|
5
|
+
|
|
6
|
+
Pages here have a table of contents beside them, made from their headings,
|
|
7
|
+
\`##\` down to \`######\`: give a long page headings, and the reader can find
|
|
8
|
+
their way in it. Each section's subsections hang on a line under it, so the
|
|
9
|
+
nesting shows. A section with an unanswered question or an unread message has
|
|
10
|
+
a dot.
|
|
11
|
+
`;
|
|
12
|
+
function toc(options = {}) {
|
|
13
|
+
return {
|
|
14
|
+
name: "toc",
|
|
15
|
+
docs: () => options.always ? DOCS + "\nIn this project every page has it; a page goes without with `toc: false` under `plugins:` in its frontmatter:\n\n```markdown\n---\nplugins:\n toc: false\n---\n```\n" : DOCS + "\nA page has it when its frontmatter asks, under `plugins:`:\n\n```markdown\n---\nplugins:\n toc: true\n---\n# A long page\n```\n",
|
|
16
|
+
public: builtinPublic("toc"),
|
|
17
|
+
style: "style.css",
|
|
18
|
+
setup: () => ({
|
|
19
|
+
client: "client.js",
|
|
20
|
+
slots: () => ({ head: html`<meta
|
|
21
|
+
name="wp-toc"
|
|
22
|
+
content="${options.always ? "always" : "opt-in"}"
|
|
23
|
+
${options.minWidth != null ? html` data-min-width="${String(options.minWidth)}"` : ""}
|
|
24
|
+
${options.maxWidth != null ? html` data-max-width="${String(options.maxWidth)}"` : ""}
|
|
25
|
+
/>` })
|
|
26
|
+
})
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export { toc as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=document.createElement(`nav`);e.className=`wp-toc`,e.setAttribute(`aria-label`,`Contents`);const t=document.createElement(`ol`);e.append(t);const n=document.createElement(`button`);n.type=`button`,n.className=`wp-toc-toggle`,n.title=n.ariaLabel=`Contents`,n.setAttribute(`aria-expanded`,`false`),n.innerHTML=`<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><path d="M2.5 4h11M2.5 8h11M2.5 12h7"/></svg>`;let r,i,a=[];const o=()=>document.querySelector(`.wp-nav`)?.getBoundingClientRect().height??0,s=()=>document.documentElement.clientWidth-r.offsetWidth-16-40;function c(){let t=parseFloat(getComputedStyle(e).minWidth)||0;return s()>=t}const l=e=>{let t=/^(\d+(?:\.\d+)?)(px|em|rem|ch|vw)?$/.exec(String(e??``).trim());return t&&Number(t[1])>0?t[1]+(t[2]??`px`):null};function u(){let t=document.querySelector(`meta[name=wp-toc]`),n=f.settings(),r=n&&typeof n==`object`?n:{};for(let[n,i]of[[`minWidth`,`--wp-toc-min-width`],[`maxWidth`,`--wp-toc-max-width`]]){let a=l(r[n])??l(t?.dataset[n]);a?e.style.setProperty(i,a):e.style.removeProperty(i)}}function d(t){let n=document.documentElement.style;if(e.style.setProperty(`--wp-toc-room`,`${s()}px`),!t)return n.removeProperty(`--wp-aside`);n.setProperty(`--wp-aside`,`${16+e.getBoundingClientRect().width}px`)}let f;function p(t){e.classList.toggle(`wp-toc-open`,t),e.hidden=!t,n.setAttribute(`aria-expanded`,String(t))}function m(){let e=f.settings(),t=document.querySelector(`meta[name=wp-toc]`)?.getAttribute(`content`)===`always`;return e===!1?!1:e===!0||e&&typeof e==`object`?!0:t}function h(e){let t=e.cloneNode(!0);return t.querySelectorAll(`.wp-badge, .wp-when`).forEach(e=>e.remove()),t.textContent?.trim()??``}function g(){a=m()?[...r.querySelectorAll(`:scope > :is(h2, h3, h4, h5, h6)`)]:[];let i=[0,0,0,0,0],o=[{level:1,list:t}];t.replaceChildren(),a.forEach((e,t)=>{let n=Number(e.tagName[1]);for(i[n-2]++,i.fill(0,n-1);o.length>1&&o.at(-1).level>=n;)o.pop();let r=o.at(-1);r.item&&!r.item.querySelector(`:scope > ol`)&&r.item.append(document.createElement(`ol`));let a=r.item?r.item.querySelector(`:scope > ol`):r.list,s=document.createElement(`li`);s.dataset.level=String(n);let c=document.createElement(`a`);c.href=`#`,c.dataset.index=String(t),c.innerHTML=`<span class="wp-toc-number"></span><span class="wp-toc-text"></span>`,c.querySelector(`.wp-toc-number`).textContent=i.slice(0,n-1).join(`.`),c.querySelector(`.wp-toc-text`).textContent=h(e),s.append(c),a.append(s),o.push({level:n,list:a,item:s})});let s=a.length>1;u();let l=s&&c();n.hidden=!s||l,e.hidden=!s||!l&&!e.classList.contains(`wp-toc-open`),e.classList.toggle(`wp-toc-docked`,l),d(l),v(),y()}function _(e){let t=-1;for(let n=0;n<a.length&&a[n].compareDocumentPosition(e)&Node.DOCUMENT_POSITION_FOLLOWING;n++)t=n;return t}function v(){let e=/* @__PURE__ */ new Set,n=new Set(i.unread().map(e=>e.id)),o=[...r.querySelectorAll(`form[data-wp-open]`),...[...r.querySelectorAll(`[data-wp-message]`)].filter(e=>n.has(e.dataset.wpMessage))];for(let t of o)e.add(_(t));for(let t of document.querySelectorAll(`[data-wp-holds]`)){if(!t.dataset.wpHolds.split(` `).some(e=>n.has(e)))continue;let r=t.getBoundingClientRect().top,i=-1;a.forEach((e,t)=>{e.getBoundingClientRect().top<=r&&(i=t)}),e.add(i)}t.querySelectorAll(`a`).forEach((t,n)=>t.toggleAttribute(`data-waiting`,e.has(n)))}function y(){let e=o()+40,n=0;a.forEach((t,r)=>{t.getBoundingClientRect().top<=e&&(n=r)}),t.querySelectorAll(`a`).forEach((e,t)=>e.toggleAttribute(`aria-current`,t===n&&a.length>0))}function b(e){let t=a[e];t&&(scrollTo({top:Math.max(0,scrollY+t.getBoundingClientRect().top-o()-12),behavior:`smooth`}),c()||p(!1))}const x=(a,o,s)=>{if(o.kind!==`page`&&o.kind!==`file`)return;f=o,r=a.querySelector(`#wp-main`)??a,i=s,s.slot(`aside`,t=>(t.append(e),()=>e.remove())),s.slot(`header`,e=>(e.append(n),()=>n.remove())),t.addEventListener(`click`,e=>{let t=e.target.closest(`a[data-index]`);t&&(e.preventDefault(),b(Number(t.dataset.index)))}),n.addEventListener(`click`,()=>p(!e.classList.contains(`wp-toc-open`))),document.addEventListener(`keydown`,t=>{t.key===`Escape`&&e.classList.contains(`wp-toc-open`)&&n.click()});let c=0,l=e=>()=>{cancelAnimationFrame(c),c=requestAnimationFrame(e)};addEventListener(`scroll`,l(y),{passive:!0}),addEventListener(`resize`,l(g)),i.onRendered(g),o.settings.on(g),i.forms.on(()=>v()),i.events.on(()=>requestAnimationFrame(v)),g()};export{x as default};
|