@noy4/docserve 0.3.2 → 0.3.4
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/bin.mjs +2 -2
- package/dist/widget/index.js +11803 -0
- package/package.json +24 -3
- package/src/node/adapter.mjs +67 -0
- package/src/node/assets.mjs +36 -0
- package/src/node/dates.mjs +73 -0
- package/src/node/docs-info.mjs +39 -0
- package/src/node/files.mjs +43 -0
- package/src/node/gallery.mjs +33 -0
- package/src/node/inject.mjs +43 -0
- package/src/node/meta.mjs +137 -0
- package/src/node/routes.mjs +105 -0
- package/src/node/scan.mjs +87 -0
- package/src/node/serve-static.mjs +158 -0
- package/src/node/server.mjs +69 -0
- package/src/node/watch.mjs +119 -0
- package/src/plugins/collapse/client.js +45 -0
- package/src/plugins/collapse/collapse.css +74 -0
- package/src/plugins/collapse/commands.ts +81 -0
- package/src/plugins/collapse/model.ts +123 -0
- package/src/plugins/collapse/sections.ts +105 -0
- package/src/plugins/collapse/view.ts +84 -0
- package/src/plugins/copy/client.js +72 -0
- package/src/plugins/copy/copy.css +21 -0
- package/src/plugins/home/client.js +24 -0
- package/src/plugins/sidebar/client.js +106 -0
- package/src/plugins/sidebar/grouping.js +108 -0
- package/src/plugins/sidebar/icons.js +25 -0
- package/src/plugins/sidebar/model.ts +91 -0
- package/src/plugins/sidebar/resize-model.ts +47 -0
- package/src/plugins/sidebar/sidebar.css +663 -0
- package/src/plugins/sidebar/tree-model.ts +23 -0
- package/src/plugins/sidebar/tree.ts +69 -0
- package/src/plugins/sidebar/ui/PageList.tsx +153 -0
- package/src/plugins/sidebar/ui/RadioMenu.tsx +201 -0
- package/src/plugins/sidebar/ui/Sidebar.tsx +95 -0
- package/src/plugins/sidebar/ui/SidebarHeader.tsx +87 -0
- package/src/plugins/sidebar/ui/TreeList.tsx +121 -0
- package/src/plugins/sidebar/ui/preview-model.ts +134 -0
- package/src/plugins/sidebar/ui/useModel.ts +25 -0
- package/src/plugins/toc/client.js +55 -0
- package/src/plugins/toc/commands.ts +49 -0
- package/src/plugins/toc/heading-html.ts +40 -0
- package/src/plugins/toc/model.ts +178 -0
- package/src/plugins/toc/outline.js +94 -0
- package/src/plugins/toc/scroll-spy.ts +35 -0
- package/src/plugins/toc/toc.css +214 -0
- package/src/plugins/toc/ui/TocPanel.tsx +87 -0
- package/src/shared/types.d.ts +193 -0
- package/src/widget/commands.js +214 -0
- package/src/widget/docks.js +127 -0
- package/src/widget/host.js +187 -0
- package/src/widget/index.js +22 -0
- package/src/widget/lib/disposables.js +44 -0
- package/src/widget/lib/dom.js +106 -0
- package/src/widget/lib/events.js +65 -0
- package/src/widget/lib/headings.js +132 -0
- package/src/widget/lib/persisted-signal.js +40 -0
- package/src/widget/lib/storage.js +70 -0
- package/src/widget/live.js +86 -0
- package/src/widget/pages.js +65 -0
- package/src/widget/palette.css +162 -0
- package/src/widget/palette.js +259 -0
- package/src/widget/placement.js +204 -0
- package/src/widget/plugins.js +39 -0
- package/src/widget/rpc.js +32 -0
- package/src/widget/styles.js +31 -0
- package/src/widget/ui.js +50 -0
- package/src/widget/widget.css +206 -0
- package/server/files.mjs +0 -261
- package/server/index.mjs +0 -198
- package/server/inject.html +0 -88
- package/server/inject.mjs +0 -28
- package/server/watch.mjs +0 -104
- /package/{server → src/node}/state.mjs +0 -0
- /package/{server → src/node}/websocket.mjs +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Document discovery and the path guards every other node module shares.
|
|
2
|
+
import { realpathSync } from "node:fs"
|
|
3
|
+
import { readdir } from "node:fs/promises"
|
|
4
|
+
import { isAbsolute, join, relative, sep } from "node:path"
|
|
5
|
+
|
|
6
|
+
// Walk docsDir recursively and collect .html files; only the root index.html is
|
|
7
|
+
// excluded from the gallery.
|
|
8
|
+
const MAX_DEPTH = 3
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} docsDir
|
|
12
|
+
* @returns {Promise<string[]>} absolute paths
|
|
13
|
+
*/
|
|
14
|
+
export async function listHtmlFiles(docsDir) {
|
|
15
|
+
/** @type {string[]} */
|
|
16
|
+
const out = []
|
|
17
|
+
await walk(docsDir, docsDir, 0)
|
|
18
|
+
return out
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {string} dir
|
|
22
|
+
* @param {string} root
|
|
23
|
+
* @param {number} depth
|
|
24
|
+
*/
|
|
25
|
+
async function walk(dir, root, depth) {
|
|
26
|
+
if (depth > MAX_DEPTH) return
|
|
27
|
+
let entries
|
|
28
|
+
try {
|
|
29
|
+
entries = await readdir(dir, { withFileTypes: true })
|
|
30
|
+
} catch {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
const full = join(dir, entry.name)
|
|
35
|
+
if (shouldIgnore(entry.name)) continue
|
|
36
|
+
if (entry.isDirectory()) {
|
|
37
|
+
await walk(full, root, depth + 1)
|
|
38
|
+
} else if (entry.name.endsWith(".html") && !(dir === root && entry.name === "index.html")) {
|
|
39
|
+
out.push(full)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {string} base
|
|
47
|
+
* @param {string} path
|
|
48
|
+
* @returns {boolean}
|
|
49
|
+
*/
|
|
50
|
+
export function isWithin(base, path) {
|
|
51
|
+
const rel = relative(base, path)
|
|
52
|
+
return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {string} base
|
|
57
|
+
* @param {string} path
|
|
58
|
+
* @returns {string}
|
|
59
|
+
*/
|
|
60
|
+
export function normalizedRelative(base, path) {
|
|
61
|
+
return relative(base, path).split(sep).join("/")
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {string} relPath
|
|
66
|
+
* @returns {boolean}
|
|
67
|
+
*/
|
|
68
|
+
export function shouldIgnore(relPath) {
|
|
69
|
+
return relPath
|
|
70
|
+
.split(/[/\\]/)
|
|
71
|
+
.filter(Boolean)
|
|
72
|
+
.some((part) => part.startsWith(".") || part === "node_modules")
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// On macOS /var is a symlink to /private/var; canonicalize to real paths so
|
|
76
|
+
// lookups match git output.
|
|
77
|
+
/**
|
|
78
|
+
* @param {string} p
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
export function realpath(p) {
|
|
82
|
+
try {
|
|
83
|
+
return realpathSync(p)
|
|
84
|
+
} catch {
|
|
85
|
+
return p
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Static file serving shared by the docs root, the widget tree and the plugin
|
|
2
|
+
// tree: one containment check, one MIME table, streaming bodies.
|
|
3
|
+
//
|
|
4
|
+
// Path flow: decode once → resolve inside root → canonical (realpath) check →
|
|
5
|
+
// directory index → 301 for a directory without its trailing slash.
|
|
6
|
+
import { createReadStream } from "node:fs"
|
|
7
|
+
import { readFile, realpath, stat } from "node:fs/promises"
|
|
8
|
+
import { extname, resolve, sep } from "node:path"
|
|
9
|
+
import { Readable } from "node:stream"
|
|
10
|
+
import { isWithin, normalizedRelative } from "./scan.mjs"
|
|
11
|
+
|
|
12
|
+
export const MIME = {
|
|
13
|
+
".html": "text/html",
|
|
14
|
+
".htm": "text/html",
|
|
15
|
+
".js": "text/javascript",
|
|
16
|
+
".mjs": "text/javascript",
|
|
17
|
+
".css": "text/css",
|
|
18
|
+
".json": "application/json",
|
|
19
|
+
".txt": "text/plain",
|
|
20
|
+
".md": "text/markdown",
|
|
21
|
+
".xml": "application/xml",
|
|
22
|
+
".png": "image/png",
|
|
23
|
+
".jpg": "image/jpeg",
|
|
24
|
+
".jpeg": "image/jpeg",
|
|
25
|
+
".gif": "image/gif",
|
|
26
|
+
".svg": "image/svg+xml",
|
|
27
|
+
".webp": "image/webp",
|
|
28
|
+
".ico": "image/x-icon",
|
|
29
|
+
".pdf": "application/pdf",
|
|
30
|
+
".woff": "font/woff",
|
|
31
|
+
".woff2": "font/woff2",
|
|
32
|
+
".mp4": "video/mp4",
|
|
33
|
+
".webm": "video/webm",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} path
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
export function contentTypeFor(path) {
|
|
41
|
+
const type = MIME[extname(path).toLowerCase()] || "application/octet-stream"
|
|
42
|
+
return type === "text/html" ? "text/html; charset=utf-8" : type
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @typedef {object} StaticHandlerOptions
|
|
47
|
+
* @property {string} root Directory files are served from.
|
|
48
|
+
* @property {string[]} [indexNames] Directory index candidates.
|
|
49
|
+
* @property {Record<string, string>} [headers] Extra response headers.
|
|
50
|
+
* @property {(body: string, relative: string) => string} [transform] HTML rewrite.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns a handler that maps a Request to a Response, or null on a miss so a
|
|
55
|
+
* router can fall through to its own 404.
|
|
56
|
+
* @param {StaticHandlerOptions} options
|
|
57
|
+
* @returns {(request: Request) => Promise<Response | null>}
|
|
58
|
+
*/
|
|
59
|
+
export function createStaticHandler({ root: rootDir, indexNames = ["index.html"], headers = {}, transform }) {
|
|
60
|
+
const root = resolve(rootDir)
|
|
61
|
+
const realRoot = realpath(root).catch(() => root)
|
|
62
|
+
|
|
63
|
+
return async function serve(request) {
|
|
64
|
+
const method = request.method || "GET"
|
|
65
|
+
if (method !== "GET" && method !== "HEAD") return plain("Method Not Allowed", 405)
|
|
66
|
+
|
|
67
|
+
const url = new URL(request.url)
|
|
68
|
+
let pathname
|
|
69
|
+
try {
|
|
70
|
+
pathname = decodeURIComponent(url.pathname)
|
|
71
|
+
} catch {
|
|
72
|
+
return plain("Bad Request", 400)
|
|
73
|
+
}
|
|
74
|
+
if (pathname.includes("\0")) return plain("Bad Request", 400)
|
|
75
|
+
|
|
76
|
+
const abs = resolve(root, pathname.replace(/^\/+/, ""))
|
|
77
|
+
if (!isWithin(root, abs)) return plain("Forbidden", 403)
|
|
78
|
+
|
|
79
|
+
const real = await realRoot
|
|
80
|
+
const hasSlash = pathname.endsWith("/")
|
|
81
|
+
let file = await statFile(abs, real)
|
|
82
|
+
|
|
83
|
+
if (!file) {
|
|
84
|
+
const isDir = await isDirectory(abs)
|
|
85
|
+
// /sub → /sub/ so relative links in index.html resolve; /sub/ serves it.
|
|
86
|
+
if (isDir && !hasSlash) return redirect(url)
|
|
87
|
+
if (isDir || hasSlash) {
|
|
88
|
+
for (const name of indexNames) {
|
|
89
|
+
file = await statFile(resolve(abs, name), real)
|
|
90
|
+
if (file) break
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (!file) return null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const responseHeaders = new Headers(headers)
|
|
97
|
+
responseHeaders.set("content-type", contentTypeFor(file.abs))
|
|
98
|
+
responseHeaders.set("content-length", String(file.size))
|
|
99
|
+
responseHeaders.set("last-modified", file.mtime.toUTCString())
|
|
100
|
+
if (method === "HEAD") return new Response(null, { status: 200, headers: responseHeaders })
|
|
101
|
+
|
|
102
|
+
const type = responseHeaders.get("content-type") || ""
|
|
103
|
+
if (transform && type.startsWith("text/html")) {
|
|
104
|
+
const html = transform(await readFile(file.abs, "utf8"), normalizedRelative(root, file.abs))
|
|
105
|
+
responseHeaders.set("content-length", String(Buffer.byteLength(html)))
|
|
106
|
+
return new Response(html, { status: 200, headers: responseHeaders })
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return new Response(Readable.toWeb(createReadStream(file.abs)), { status: 200, headers: responseHeaders })
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {string} abs
|
|
115
|
+
* @param {string} realRoot
|
|
116
|
+
* @returns {Promise<{ abs: string, size: number, mtime: Date } | null>}
|
|
117
|
+
*/
|
|
118
|
+
async function statFile(abs, realRoot) {
|
|
119
|
+
try {
|
|
120
|
+
const s = await stat(abs)
|
|
121
|
+
if (!s.isFile()) return null
|
|
122
|
+
const real = await realpath(abs)
|
|
123
|
+
if (real !== realRoot && !real.startsWith(realRoot + sep)) return null
|
|
124
|
+
return { abs, size: s.size, mtime: s.mtime }
|
|
125
|
+
} catch {
|
|
126
|
+
return null
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {string} abs
|
|
132
|
+
* @returns {Promise<boolean>}
|
|
133
|
+
*/
|
|
134
|
+
async function isDirectory(abs) {
|
|
135
|
+
try {
|
|
136
|
+
return (await stat(abs)).isDirectory()
|
|
137
|
+
} catch {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {URL} url
|
|
144
|
+
* @returns {Response}
|
|
145
|
+
*/
|
|
146
|
+
function redirect(url) {
|
|
147
|
+
const location = new URL(`${url.pathname}/${url.search}`, url)
|
|
148
|
+
return new Response(null, { status: 301, headers: { location: location.href } })
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {string} body
|
|
153
|
+
* @param {number} status
|
|
154
|
+
* @returns {Response}
|
|
155
|
+
*/
|
|
156
|
+
function plain(body, status) {
|
|
157
|
+
return new Response(body, { status, headers: { "content-type": "text/plain; charset=utf-8" } })
|
|
158
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Server lifecycle: bind with port fallback, wire the watcher and socket, and
|
|
2
|
+
// write the state file the CLI and desktop app read.
|
|
3
|
+
//
|
|
4
|
+
// ┌──────────────────────────────────────────────────────────┐
|
|
5
|
+
// │ runServer() │
|
|
6
|
+
// │ ├─ createServer ──▶ adapter(createHandler()) │
|
|
7
|
+
// │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
|
|
8
|
+
// │ └─ listen ──▶ watcher.start() + writeState() │
|
|
9
|
+
// └──────────────────────────────────────────────────────────┘
|
|
10
|
+
import { exec } from "node:child_process"
|
|
11
|
+
import { createServer } from "node:http"
|
|
12
|
+
import { createNodeServerAdapter } from "./adapter.mjs"
|
|
13
|
+
import { INDEX_TEMPLATE } from "./gallery.mjs"
|
|
14
|
+
import { createHandler } from "./routes.mjs"
|
|
15
|
+
import { clearState, writeState } from "./state.mjs"
|
|
16
|
+
import { FileWatcher } from "./watch.mjs"
|
|
17
|
+
import { WebSocketServer } from "./websocket.mjs"
|
|
18
|
+
import { WS_PATH } from "./inject.mjs"
|
|
19
|
+
|
|
20
|
+
const MAX_PORT_TRIES = 20 // on conflict, try the next port up to 20 times
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {{ docsDir: string, port: number, open?: boolean }} options
|
|
24
|
+
*/
|
|
25
|
+
export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
26
|
+
const wss = new WebSocketServer({ path: WS_PATH })
|
|
27
|
+
const fileWatcher = new FileWatcher({ wss, docsDir, templatePath: INDEX_TEMPLATE })
|
|
28
|
+
let port = initialPort
|
|
29
|
+
let closed = false
|
|
30
|
+
|
|
31
|
+
const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
|
|
32
|
+
wss.attach(server)
|
|
33
|
+
|
|
34
|
+
const close = () => {
|
|
35
|
+
if (closed) return
|
|
36
|
+
closed = true
|
|
37
|
+
process.off("SIGINT", onSignal)
|
|
38
|
+
process.off("SIGTERM", onSignal)
|
|
39
|
+
fileWatcher.close()
|
|
40
|
+
wss.close()
|
|
41
|
+
clearState()
|
|
42
|
+
server.closeAllConnections()
|
|
43
|
+
if (server.listening) server.close()
|
|
44
|
+
}
|
|
45
|
+
const onSignal = () => {
|
|
46
|
+
close()
|
|
47
|
+
process.exit(0)
|
|
48
|
+
}
|
|
49
|
+
process.on("SIGINT", onSignal)
|
|
50
|
+
process.on("SIGTERM", onSignal)
|
|
51
|
+
|
|
52
|
+
server.on("error", (/** @type {NodeJS.ErrnoException} */ err) => {
|
|
53
|
+
if (err.code === "EADDRINUSE" && port < initialPort + MAX_PORT_TRIES) {
|
|
54
|
+
port += 1
|
|
55
|
+
server.listen(port, "127.0.0.1")
|
|
56
|
+
} else {
|
|
57
|
+
console.error(err)
|
|
58
|
+
process.exit(1)
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
server.listen(port, "127.0.0.1", async () => {
|
|
62
|
+
await fileWatcher.start()
|
|
63
|
+
const state = { pid: process.pid, port, url: `http://localhost:${port}/`, docsDir }
|
|
64
|
+
writeState(state)
|
|
65
|
+
console.log(state.url)
|
|
66
|
+
if (open && process.platform === "darwin") exec(`open ${state.url}`)
|
|
67
|
+
})
|
|
68
|
+
return { server, wss, fileWatcher, close }
|
|
69
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Watches docsDir for *.html / *.css and the gallery template, broadcasting
|
|
2
|
+
// debounced reload batches. The gallery set is every *.html except index.html;
|
|
3
|
+
// only additions/removals flip contentSetChanged.
|
|
4
|
+
import { existsSync, watch } from "node:fs"
|
|
5
|
+
import { basename, dirname, resolve } from "node:path"
|
|
6
|
+
import { isWithin, listHtmlFiles, normalizedRelative, shouldIgnore } from "./scan.mjs"
|
|
7
|
+
|
|
8
|
+
const DEBOUNCE_MS = 50
|
|
9
|
+
|
|
10
|
+
export class FileWatcher {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
this.wss = options.wss
|
|
13
|
+
this.docsDir = options.docsDir
|
|
14
|
+
this.templatePath = options.templatePath
|
|
15
|
+
this.debounceMs = options.debounceMs ?? DEBOUNCE_MS
|
|
16
|
+
this.knownFiles = new Set()
|
|
17
|
+
this.queue = Promise.resolve()
|
|
18
|
+
this.watchers = []
|
|
19
|
+
this.pending = { pages: new Set(), css: new Set(), template: false }
|
|
20
|
+
|
|
21
|
+
// html/css/template edits in one burst flush together
|
|
22
|
+
this.queueFlush = debounce(() => {
|
|
23
|
+
const { pages, css, template } = this.pending
|
|
24
|
+
this.pending = { pages: new Set(), css: new Set(), template: false }
|
|
25
|
+
if (pages.size || template) {
|
|
26
|
+
this.enqueue(() => this.broadcastChanges({ pages: [...pages], template }))
|
|
27
|
+
}
|
|
28
|
+
if (css.size)
|
|
29
|
+
this.enqueue(() => this.broadcastCss([...css]))
|
|
30
|
+
}, this.debounceMs)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
enqueue(task) {
|
|
34
|
+
this.queue = this.queue.then(task)
|
|
35
|
+
.catch((error) => console.error(error))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async start() {
|
|
39
|
+
try {
|
|
40
|
+
this.knownFiles = new Set(
|
|
41
|
+
(await listHtmlFiles(this.docsDir)).map((p) => normalizedRelative(this.docsDir, p)),
|
|
42
|
+
)
|
|
43
|
+
} catch {
|
|
44
|
+
this.knownFiles = new Set()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this.watchers.push(
|
|
48
|
+
watch(this.docsDir, { recursive: true }, (event, filename) => {
|
|
49
|
+
if (!filename || shouldIgnore(filename)) return
|
|
50
|
+
const full = resolve(this.docsDir, filename)
|
|
51
|
+
if (!isWithin(this.docsDir, full)) return
|
|
52
|
+
const rel = normalizedRelative(this.docsDir, full)
|
|
53
|
+
|
|
54
|
+
if (filename.endsWith(".html")) {
|
|
55
|
+
this.pending.pages.add(rel)
|
|
56
|
+
this.queueFlush()
|
|
57
|
+
} else if (filename.endsWith(".css")) {
|
|
58
|
+
this.pending.css.add(rel)
|
|
59
|
+
this.queueFlush()
|
|
60
|
+
}
|
|
61
|
+
}).on("error", () => { }),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if (this.templatePath) {
|
|
65
|
+
// Watch the directory
|
|
66
|
+
const templateName = basename(this.templatePath)
|
|
67
|
+
this.watchers.push(
|
|
68
|
+
watch(dirname(this.templatePath), (event, filename) => {
|
|
69
|
+
if (filename === templateName) {
|
|
70
|
+
this.pending.template = true
|
|
71
|
+
this.queueFlush()
|
|
72
|
+
}
|
|
73
|
+
}).on("error", () => { }),
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async broadcastChanges({ pages, template }) {
|
|
79
|
+
this.wss.broadcast({
|
|
80
|
+
type: "change",
|
|
81
|
+
pages,
|
|
82
|
+
contentSetChanged: syncKnownFiles(this.knownFiles, this.docsDir, pages),
|
|
83
|
+
templateChanged: template,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Refresh linked stylesheets in place.
|
|
88
|
+
broadcastCss(files) {
|
|
89
|
+
this.wss.broadcast({ type: "css", files })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
close() {
|
|
93
|
+
this.watchers.forEach((w) => w.close())
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// True when pages were added to or removed from the gallery.
|
|
98
|
+
function syncKnownFiles(knownFiles, docsDir, pages) {
|
|
99
|
+
let changed = false
|
|
100
|
+
for (const rel of pages) {
|
|
101
|
+
if (basename(rel) === "index.html") continue // not a gallery card
|
|
102
|
+
const exists = existsSync(resolve(docsDir, rel))
|
|
103
|
+
const wasKnown = knownFiles.has(rel)
|
|
104
|
+
if (exists === wasKnown) continue
|
|
105
|
+
if (exists) knownFiles.add(rel)
|
|
106
|
+
else knownFiles.delete(rel)
|
|
107
|
+
changed = true
|
|
108
|
+
}
|
|
109
|
+
return changed
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Trailing-edge debounce.
|
|
113
|
+
function debounce(fn, ms) {
|
|
114
|
+
let timer = null
|
|
115
|
+
return () => {
|
|
116
|
+
clearTimeout(timer)
|
|
117
|
+
timer = setTimeout(fn, ms)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createDisposables } from "../../widget/lib/disposables.js"
|
|
2
|
+
import { el } from "../../widget/lib/dom.js"
|
|
3
|
+
import { registerCollapseCommands, registerCollapseDock } from "./commands.js"
|
|
4
|
+
import { CollapseModel } from "./model.js"
|
|
5
|
+
import { collectSections } from "./sections.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @returns {DocservePlugin}
|
|
9
|
+
*/
|
|
10
|
+
export function collapsePlugin() {
|
|
11
|
+
return {
|
|
12
|
+
id: "collapse",
|
|
13
|
+
setup(ctx) {
|
|
14
|
+
// sections
|
|
15
|
+
const collect = collectSections(document)
|
|
16
|
+
|
|
17
|
+
// state
|
|
18
|
+
const model = CollapseModel.create(ctx.page)
|
|
19
|
+
const cleanup = createDisposables()
|
|
20
|
+
|
|
21
|
+
// styles
|
|
22
|
+
const link = el("link", {
|
|
23
|
+
rel: "stylesheet",
|
|
24
|
+
href: "/__docserve/plugins/collapse/collapse.css",
|
|
25
|
+
"data-docserve-collapse": "",
|
|
26
|
+
})
|
|
27
|
+
document.head.append(link)
|
|
28
|
+
cleanup.add(() => link.remove())
|
|
29
|
+
|
|
30
|
+
// sections
|
|
31
|
+
model.load(collect)
|
|
32
|
+
cleanup.add(model.subscribe())
|
|
33
|
+
|
|
34
|
+
// commands
|
|
35
|
+
cleanup.add(registerCollapseCommands(ctx, model))
|
|
36
|
+
cleanup.add(registerCollapseDock(ctx, model))
|
|
37
|
+
|
|
38
|
+
// rpc
|
|
39
|
+
cleanup.add(ctx.rpc.register("plugin:collapse:is-collapsed", (id) => model.isCollapsed(id)))
|
|
40
|
+
cleanup.add(ctx.rpc.register("plugin:collapse:expand-ancestors", (id) => model.expandAncestors(id)))
|
|
41
|
+
|
|
42
|
+
return cleanup.dispose
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
.docserve-collapse__heading {
|
|
2
|
+
position: relative;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.docserve-collapse__toggle {
|
|
6
|
+
position: absolute;
|
|
7
|
+
top: 50%;
|
|
8
|
+
left: -24px;
|
|
9
|
+
display: grid;
|
|
10
|
+
place-items: center;
|
|
11
|
+
width: 18px;
|
|
12
|
+
height: 18px;
|
|
13
|
+
padding: 0;
|
|
14
|
+
border: 0;
|
|
15
|
+
border-radius: 4px;
|
|
16
|
+
background: transparent;
|
|
17
|
+
color: inherit;
|
|
18
|
+
font: inherit;
|
|
19
|
+
line-height: 0;
|
|
20
|
+
opacity: 0;
|
|
21
|
+
cursor: pointer;
|
|
22
|
+
appearance: none;
|
|
23
|
+
transform: translateY(-50%);
|
|
24
|
+
transition:
|
|
25
|
+
opacity 0.15s ease,
|
|
26
|
+
background-color 0.15s ease;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.docserve-collapse__heading:hover .docserve-collapse__toggle,
|
|
30
|
+
.docserve-collapse__heading:focus-within .docserve-collapse__toggle {
|
|
31
|
+
opacity: 0.55;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.docserve-collapse__toggle svg {
|
|
35
|
+
width: 15px;
|
|
36
|
+
height: 15px;
|
|
37
|
+
transition: transform 0.15s ease;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.docserve-collapse__toggle[aria-expanded="true"] svg {
|
|
41
|
+
transform: rotate(90deg);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.docserve-collapse__toggle:hover {
|
|
45
|
+
background: color-mix(in srgb, currentColor 10%, transparent);
|
|
46
|
+
opacity: 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.docserve-collapse__toggle:focus-visible {
|
|
50
|
+
outline: 2px solid currentColor;
|
|
51
|
+
outline-offset: 1px;
|
|
52
|
+
opacity: 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@media (hover: none) {
|
|
56
|
+
.docserve-collapse__toggle {
|
|
57
|
+
opacity: 0.45;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.docserve-collapse__hidden {
|
|
62
|
+
display: none !important;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
section.docserve-collapse__collapsed + section {
|
|
66
|
+
margin-top: var(--docserve-collapse-section-gap, 12px);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@media (prefers-reduced-motion: reduce) {
|
|
70
|
+
.docserve-collapse__toggle,
|
|
71
|
+
.docserve-collapse__toggle svg {
|
|
72
|
+
transition: none;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import ICON_FOLD_RAW from "~icons/lucide/fold-vertical?raw&width=18&height=18"
|
|
2
|
+
import ICON_UNFOLD_RAW from "~icons/lucide/unfold-vertical?raw&width=18&height=18"
|
|
3
|
+
import { iconSvg } from "../../widget/lib/dom.js"
|
|
4
|
+
import type { CollapseModel } from "./model.js"
|
|
5
|
+
|
|
6
|
+
const ICON_FOLD = iconSvg(ICON_FOLD_RAW)
|
|
7
|
+
const ICON_UNFOLD = iconSvg(ICON_UNFOLD_RAW)
|
|
8
|
+
|
|
9
|
+
export function registerCollapseCommands(ctx: DocserveContext, model: CollapseModel): () => void {
|
|
10
|
+
const offEnabled = ctx.commands.register({
|
|
11
|
+
id: "plugin:collapse:toggle-enabled",
|
|
12
|
+
title: "Enable/disable collapse",
|
|
13
|
+
category: "View",
|
|
14
|
+
icon: ICON_FOLD,
|
|
15
|
+
keys: ["Alt+K"],
|
|
16
|
+
run: () => model.toggleEnabled(),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const offToggle = ctx.commands.register({
|
|
20
|
+
id: "plugin:collapse:toggle-all",
|
|
21
|
+
title: "Toggle all sections",
|
|
22
|
+
category: "View",
|
|
23
|
+
icon: ICON_FOLD,
|
|
24
|
+
keys: ["k"],
|
|
25
|
+
when: () => model.enabled.value,
|
|
26
|
+
run: () => model.toggleAll(),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const offExpand = ctx.commands.register({
|
|
30
|
+
id: "plugin:collapse:expand-all",
|
|
31
|
+
title: "Expand all sections",
|
|
32
|
+
category: "View",
|
|
33
|
+
icon: ICON_UNFOLD,
|
|
34
|
+
when: () => model.enabled.value,
|
|
35
|
+
run: () => model.expandAll(),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const offCollapse = ctx.commands.register({
|
|
39
|
+
id: "plugin:collapse:collapse-all",
|
|
40
|
+
title: "Collapse all sections",
|
|
41
|
+
category: "View",
|
|
42
|
+
icon: ICON_FOLD,
|
|
43
|
+
when: () => model.enabled.value,
|
|
44
|
+
run: () => model.collapseAll(),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return () => {
|
|
48
|
+
offCollapse()
|
|
49
|
+
offExpand()
|
|
50
|
+
offToggle()
|
|
51
|
+
offEnabled()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function registerCollapseDock(ctx: DocserveContext, model: CollapseModel): () => void {
|
|
56
|
+
/** @type {DocksItemHandle | null} */
|
|
57
|
+
let dock = null
|
|
58
|
+
|
|
59
|
+
const offEnabled = model.enabled.subscribe(sync)
|
|
60
|
+
|
|
61
|
+
return () => {
|
|
62
|
+
offEnabled()
|
|
63
|
+
dock?.dispose()
|
|
64
|
+
dock = null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function sync(): void {
|
|
68
|
+
if (model.enabled.value && !dock) {
|
|
69
|
+
dock = ctx.docks.register({
|
|
70
|
+
id: "collapse",
|
|
71
|
+
title: "Toggle all sections",
|
|
72
|
+
icon: ICON_FOLD,
|
|
73
|
+
category: "actions",
|
|
74
|
+
command: "plugin:collapse:toggle-all",
|
|
75
|
+
})
|
|
76
|
+
} else if (!model.enabled.value && dock) {
|
|
77
|
+
dock.dispose()
|
|
78
|
+
dock = null
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|