@noy4/docserve 0.3.3 → 0.3.5

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.
Files changed (79) hide show
  1. package/bin.mjs +8 -213
  2. package/dist/gallery/assets/index-CGV64tuk.css +1 -0
  3. package/dist/gallery/assets/index-Cf8Uf0vA.js +10 -0
  4. package/dist/gallery/assets/index-Cf8Uf0vA.js.map +1 -0
  5. package/dist/gallery/index.html +15 -0
  6. package/dist/node/cli.mjs +4095 -0
  7. package/dist/node/cli.mjs.map +7 -0
  8. package/dist/widget/index.js +3410 -2527
  9. package/package.json +15 -5
  10. package/src/node/cli.mjs +155 -0
  11. package/src/node/{files.mjs → docs/files.mjs} +1 -1
  12. package/src/node/{inject.mjs → docs/inject.mjs} +1 -1
  13. package/src/node/{meta.mjs → docs/meta.mjs} +1 -1
  14. package/src/node/http/app.mjs +20 -0
  15. package/src/node/{assets.mjs → http/assets.mjs} +3 -2
  16. package/src/node/http/gallery.mjs +45 -0
  17. package/src/node/http/response.mjs +31 -0
  18. package/src/node/http/routes.mjs +59 -0
  19. package/src/node/{serve-static.mjs → http/serve-static.mjs} +1 -1
  20. package/src/node/{server.mjs → http/server.mjs} +10 -10
  21. package/src/node/{state.mjs → instances/state.mjs} +9 -5
  22. package/src/node/instances/view.mjs +19 -0
  23. package/src/node/{watch.mjs → live/watch.mjs} +1 -1
  24. package/src/node/paths.mjs +10 -0
  25. package/src/node/rpc/files.mjs +5 -0
  26. package/src/node/rpc/index.mjs +39 -0
  27. package/src/node/rpc/route.mjs +34 -0
  28. package/src/node/rpc/servers.mjs +16 -0
  29. package/src/plugins/collapse/client.js +45 -0
  30. package/src/plugins/collapse/collapse.css +74 -0
  31. package/src/plugins/collapse/commands.ts +81 -0
  32. package/src/plugins/collapse/model.ts +123 -0
  33. package/src/plugins/collapse/sections.ts +105 -0
  34. package/src/plugins/collapse/view.ts +84 -0
  35. package/src/plugins/servers/client.js +77 -0
  36. package/src/plugins/servers/commands.ts +30 -0
  37. package/src/plugins/servers/model.ts +106 -0
  38. package/src/plugins/servers/servers.css +144 -0
  39. package/src/plugins/servers/ui/ServersPopover.tsx +71 -0
  40. package/src/plugins/sidebar/client.js +1 -0
  41. package/src/plugins/sidebar/icons.js +1 -1
  42. package/src/plugins/sidebar/ui/PageList.tsx +1 -1
  43. package/src/plugins/sidebar/ui/RadioMenu.tsx +1 -1
  44. package/src/plugins/sidebar/ui/Sidebar.tsx +3 -3
  45. package/src/plugins/sidebar/ui/TreeList.tsx +10 -3
  46. package/src/plugins/toc/client.js +55 -0
  47. package/src/plugins/toc/commands.ts +49 -0
  48. package/src/plugins/toc/heading-html.ts +40 -0
  49. package/src/plugins/toc/model.ts +178 -0
  50. package/src/plugins/toc/outline.js +94 -0
  51. package/src/plugins/toc/scroll-spy.ts +35 -0
  52. package/src/plugins/toc/toc.css +214 -0
  53. package/src/plugins/toc/ui/TocPanel.tsx +87 -0
  54. package/src/{plugins/sidebar/ui → shared/client}/preview-model.ts +18 -4
  55. package/src/{plugins/sidebar/ui/useModel.ts → shared/client/use-model.ts} +1 -8
  56. package/src/shared/types.d.ts +17 -1
  57. package/src/widget/{pages.js → api/pages.js} +4 -7
  58. package/src/widget/api/rpc-client.js +25 -0
  59. package/src/widget/api/servers.js +46 -0
  60. package/src/widget/host.js +10 -8
  61. package/src/widget/index.js +11 -1
  62. package/src/widget/lib/headings.js +132 -0
  63. package/src/widget/{docks.js → shell/docks.js} +2 -2
  64. package/src/widget/{palette.js → shell/palette.js} +3 -3
  65. package/src/widget/{placement.js → shell/placement.js} +1 -1
  66. package/src/widget/{ui.js → shell/ui.js} +1 -1
  67. package/src/widget/widget.css +15 -2
  68. package/index.html +0 -365
  69. package/src/node/adapter.mjs +0 -67
  70. package/src/node/gallery.mjs +0 -33
  71. package/src/node/routes.mjs +0 -105
  72. /package/src/node/{dates.mjs → docs/dates.mjs} +0 -0
  73. /package/src/node/{docs-info.mjs → docs/info.mjs} +0 -0
  74. /package/src/node/{scan.mjs → docs/scan.mjs} +0 -0
  75. /package/src/node/{websocket.mjs → live/websocket.mjs} +0 -0
  76. /package/src/widget/{live.js → api/live.js} +0 -0
  77. /package/src/widget/{rpc.js → api/rpc.js} +0 -0
  78. /package/src/widget/{palette.css → shell/palette.css} +0 -0
  79. /package/src/widget/{styles.js → shell/styles.js} +0 -0
@@ -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
+ }
@@ -0,0 +1,123 @@
1
+ import { effect, signal } from "@preact/signals-react"
2
+ import type { Signal } from "@preact/signals-react"
3
+ import { persisted, persistedBool } from "../../widget/lib/persisted-signal.js"
4
+ import type { CollapseCollect, CollapseSection } from "./sections.js"
5
+ import { applyState, mountSections } from "./view.js"
6
+
7
+ export class CollapseModel {
8
+ readonly collapsed: Signal<string[]>
9
+ readonly enabled: Signal<boolean>
10
+ readonly sections = signal<CollapseSection[]>([])
11
+
12
+ #wrappers: HTMLElement[] = []
13
+ #mounted: CollapseSection[] | null = null
14
+ #unmount: (() => void) | null = null
15
+
16
+ constructor(page: string) {
17
+ this.collapsed = persisted<string[]>(`docserve:collapse:${page}`, [])
18
+ this.enabled = persistedBool("docserve:collapse:enabled", true)
19
+ }
20
+
21
+ static create(page: string): CollapseModel {
22
+ return new CollapseModel(page)
23
+ }
24
+
25
+ load(collect: CollapseCollect): void {
26
+ const ids = new Set(collect.sections.map((section) => section.id))
27
+ const current = this.collapsed.value
28
+ const next = current.filter((id) => ids.has(id))
29
+ if (next.length !== current.length) this.collapsed.value = next
30
+
31
+ this.#wrappers = collect.wrappers
32
+ this.sections.value = collect.sections
33
+ }
34
+
35
+ subscribe(): () => void {
36
+ const stop = effect(() => {
37
+ const sections = this.sections.value
38
+ const enabled = this.enabled.value
39
+
40
+ if (enabled) {
41
+ if (this.#mounted !== sections) {
42
+ this.#unmount?.()
43
+ this.#unmount = mountSections(sections, (id) => this.toggle(id))
44
+ this.#mounted = sections
45
+ }
46
+ } else if (this.#unmount) {
47
+ this.#unmount()
48
+ this.#unmount = null
49
+ this.#mounted = null
50
+ }
51
+
52
+ applyState(sections, this.collapsed.value, enabled)
53
+ })
54
+
55
+ return () => {
56
+ stop()
57
+ this.#unmount?.()
58
+ this.#unmount = null
59
+ this.#mounted = null
60
+
61
+ const wrappers = this.#wrappers
62
+ this.#wrappers = []
63
+ for (const wrapper of wrappers.reverse()) {
64
+ if (wrapper.isConnected) wrapper.replaceWith(...wrapper.childNodes)
65
+ }
66
+ }
67
+ }
68
+
69
+ toggleEnabled(): void {
70
+ this.enabled.value = !this.enabled.value
71
+ }
72
+
73
+ toggle(id: string): void {
74
+ const collapsed = new Set(this.collapsed.value)
75
+ if (collapsed.has(id)) collapsed.delete(id)
76
+ else collapsed.add(id)
77
+ this.collapsed.value = [...collapsed]
78
+ }
79
+
80
+ expand(id: string): void {
81
+ this.collapsed.value = this.collapsed.value.filter((item) => item !== id)
82
+ }
83
+
84
+ collapse(id: string): void {
85
+ if (this.isCollapsed(id)) return
86
+ this.collapsed.value = [...this.collapsed.value, id]
87
+ }
88
+
89
+ expandAncestors(id: string): void {
90
+ if (!this.enabled.value) return
91
+
92
+ let section = this.sections.value.find((item) => item.id === id)?.parent ?? null
93
+ if (!section) return
94
+
95
+ const collapsed = new Set(this.collapsed.value)
96
+ let changed = false
97
+ while (section) {
98
+ if (collapsed.delete(section.id)) changed = true
99
+ section = section.parent
100
+ }
101
+
102
+ if (changed) this.collapsed.value = [...collapsed]
103
+ }
104
+
105
+ toggleAll(): void {
106
+ const sections = this.sections.value
107
+ const collapsed = new Set(this.collapsed.value)
108
+ const allCollapsed = sections.every((section) => collapsed.has(section.id))
109
+ this.collapsed.value = allCollapsed ? [] : sections.map((section) => section.id)
110
+ }
111
+
112
+ collapseAll(): void {
113
+ this.collapsed.value = this.sections.value.map((section) => section.id)
114
+ }
115
+
116
+ expandAll(): void {
117
+ this.collapsed.value = []
118
+ }
119
+
120
+ isCollapsed(id: string): boolean {
121
+ return this.enabled.value && this.collapsed.value.includes(id)
122
+ }
123
+ }
@@ -0,0 +1,105 @@
1
+ import { collectHeadings, ensureIds, findRoot } from "../../widget/lib/headings.js"
2
+
3
+ export interface CollapseSection {
4
+ id: string
5
+ heading: HTMLHeadingElement
6
+ level: number
7
+ body: Element[]
8
+ button?: HTMLButtonElement
9
+ parent: CollapseSection | null
10
+ }
11
+
12
+ export interface CollapseCollect {
13
+ sections: CollapseSection[]
14
+ wrappers: HTMLElement[]
15
+ }
16
+
17
+ const EXCLUDED = "nav, aside, header, footer, [data-collapse-ignore], #docserve-dock-host, [data-docserve-widget]"
18
+ const DEFAULT_LEVELS: [number, number] = [2, 4]
19
+ const SKIP = new Set(["SCRIPT", "STYLE", "LINK", "TEMPLATE", "NOSCRIPT"])
20
+
21
+ export function collectSections(doc: Document = document): CollapseCollect {
22
+ const source = doc.body ?? doc.documentElement
23
+ if (!source) return { sections: [], wrappers: [] }
24
+
25
+ const root = findRoot(doc, "data-collapse-root")
26
+ if (!root) return { sections: [], wrappers: [] }
27
+ if ((root.getAttribute("data-collapse") ?? source.getAttribute("data-collapse")) === "false")
28
+ return { sections: [], wrappers: [] }
29
+
30
+ const raw = root.getAttribute("data-collapse-levels") ?? source.getAttribute("data-collapse-levels")
31
+ const range = parseLevels(raw)
32
+ if (!range) return { sections: [], wrappers: [] }
33
+
34
+ const headings = collectHeadings(root, range, EXCLUDED)
35
+ ensureIds(headings, doc)
36
+
37
+ const wrappers: HTMLElement[] = []
38
+ const sections: CollapseSection[] = headings
39
+ .map((heading) => ({
40
+ id: heading.id,
41
+ heading,
42
+ level: Number(heading.tagName[1]),
43
+ body: bodyNodes(heading, wrappers, doc),
44
+ parent: null,
45
+ }))
46
+ .filter((section) => section.body.length)
47
+
48
+ const stack: CollapseSection[] = []
49
+ for (const section of sections) {
50
+ while (stack.length && stack[stack.length - 1].level >= section.level) stack.pop()
51
+ section.parent = stack[stack.length - 1] ?? null
52
+ stack.push(section)
53
+ }
54
+
55
+ return { sections, wrappers }
56
+ }
57
+
58
+ function parseLevels(value: string | null | undefined): [number, number] | null {
59
+ if (value === "false") return null
60
+ if (!value || value.trim() === "") return [...DEFAULT_LEVELS]
61
+ if (value === "deep") return [2, 6]
62
+
63
+ const parts = value.split(/[,-]/).map((part) => Number(part.trim())).filter(Number.isFinite)
64
+ if (!parts.length) return [...DEFAULT_LEVELS]
65
+
66
+ const min = clampLevel(parts[0])
67
+ const max = clampLevel(parts.length > 1 ? parts[1] : parts[0])
68
+ return min <= max ? [min, max] : [max, min]
69
+ }
70
+
71
+ function clampLevel(value: number): number {
72
+ return Math.min(6, Math.max(1, Math.round(value)))
73
+ }
74
+
75
+ function bodyNodes(heading: HTMLHeadingElement, wrappers: HTMLElement[], doc: Document): Element[] {
76
+ const level = Number(heading.tagName[1])
77
+ const nodes: Element[] = []
78
+
79
+ let node = heading.nextSibling
80
+ while (node) {
81
+ const next = node.nextSibling
82
+
83
+ if (node.nodeType === Node.ELEMENT_NODE) {
84
+ const element = node as Element
85
+ if (element.matches("#docserve-dock-host, [data-docserve-widget]")) break
86
+ if (/^H[1-6]$/.test(element.tagName) && Number(element.tagName[1]) <= level) break
87
+ if (SKIP.has(element.tagName) || element.matches("[data-collapse-ignore]")) {
88
+ node = next
89
+ continue
90
+ }
91
+ nodes.push(element)
92
+ } else if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {
93
+ const wrapper = doc.createElement("span")
94
+ wrapper.className = "docserve-collapse__text"
95
+ node.replaceWith(wrapper)
96
+ wrapper.append(node)
97
+ wrappers.push(wrapper)
98
+ nodes.push(wrapper)
99
+ }
100
+
101
+ node = next
102
+ }
103
+
104
+ return nodes
105
+ }
@@ -0,0 +1,84 @@
1
+ import ICON_CHEVRON_RAW from "~icons/lucide/chevron-right?raw&width=16&height=16"
2
+ import { el, iconSvg } from "../../widget/lib/dom.js"
3
+ import type { CollapseSection } from "./sections.js"
4
+
5
+ const ICON_CHEVRON = iconSvg(ICON_CHEVRON_RAW)
6
+ const HIDDEN_CLASS = "docserve-collapse__hidden"
7
+ const SECTION_COLLAPSED_CLASS = "docserve-collapse__collapsed"
8
+
9
+ export function mountSections(sections: CollapseSection[], onToggle: (id: string) => void): () => void {
10
+ const controller = new AbortController()
11
+
12
+ for (const section of sections) {
13
+ const button = el("button", {
14
+ type: "button",
15
+ class: "docserve-collapse__toggle",
16
+ "data-collapse-toggle": "",
17
+ "aria-expanded": "true",
18
+ "aria-label": "Collapse section",
19
+ title: "Collapse section",
20
+ html: ICON_CHEVRON,
21
+ }) as HTMLButtonElement
22
+
23
+ button.addEventListener("click", () => onToggle(section.id), { signal: controller.signal })
24
+
25
+ section.heading.classList.add("docserve-collapse__heading")
26
+ section.heading.prepend(button)
27
+ section.button = button
28
+ }
29
+
30
+ return () => {
31
+ controller.abort()
32
+
33
+ for (const section of sections) {
34
+ section.button?.remove()
35
+ section.button = undefined
36
+ section.heading.classList.remove("docserve-collapse__heading")
37
+ section.heading.parentElement?.classList.remove(SECTION_COLLAPSED_CLASS)
38
+ for (const node of section.body) node.classList.remove(HIDDEN_CLASS)
39
+ }
40
+ }
41
+ }
42
+
43
+ export function applyState(sections: CollapseSection[], collapsed: string[], enabled: boolean): void {
44
+ const collapsedIds = new Set(collapsed)
45
+ const hidden = new Set<Element>()
46
+
47
+ if (enabled) {
48
+ for (const section of sections) {
49
+ if (!collapsedIds.has(section.id)) continue
50
+ for (const node of section.body) hidden.add(node)
51
+ }
52
+ }
53
+
54
+ for (const section of sections) {
55
+ for (const node of section.body)
56
+ node.classList.toggle(HIDDEN_CLASS, enabled && hidden.has(node))
57
+
58
+ section.heading.classList.toggle("docserve-collapse__heading", enabled)
59
+
60
+ const parent = section.heading.parentElement
61
+ if (!enabled)
62
+ parent?.classList.remove(SECTION_COLLAPSED_CLASS)
63
+ else if (parent?.tagName === "SECTION" && isSectionHead(section.heading))
64
+ parent.classList.toggle(SECTION_COLLAPSED_CLASS, collapsedIds.has(section.id))
65
+
66
+ const button = section.button
67
+ if (!button) continue
68
+
69
+ const open = !collapsedIds.has(section.id)
70
+ const label = open ? "Collapse section" : "Expand section"
71
+ button.setAttribute("aria-expanded", String(open))
72
+ button.title = label
73
+ button.setAttribute("aria-label", label)
74
+ }
75
+ }
76
+
77
+ function isSectionHead(heading: HTMLHeadingElement): boolean {
78
+ let node = heading.previousElementSibling
79
+ while (node) {
80
+ if (/^H[1-6]$/.test(node.tagName)) return false
81
+ node = node.previousElementSibling
82
+ }
83
+ return true
84
+ }
@@ -0,0 +1,77 @@
1
+ import { createElement } from "react"
2
+ import { createRoot } from "react-dom/client"
3
+ import ICON_SERVERS_RAW from "~icons/lucide/server?raw&width=18&height=18"
4
+ import { createDisposables } from "../../widget/lib/disposables.js"
5
+ import { iconSvg } from "../../widget/lib/dom.js"
6
+ import { registerServersCommands } from "./commands.js"
7
+ import { ServersModel } from "./model.js"
8
+ import { ServersPopover } from "./ui/ServersPopover.js"
9
+
10
+ const ICON_SERVERS = iconSvg(ICON_SERVERS_RAW)
11
+
12
+ /**
13
+ * @returns {DocservePlugin}
14
+ */
15
+ export function serversPlugin() {
16
+ return {
17
+ id: "servers",
18
+ setup(ctx) {
19
+ // state
20
+ const model = ServersModel.fromContext(ctx)
21
+ const cleanup = createDisposables()
22
+
23
+ // styles
24
+ ctx.ui.style("/__docserve/plugins/servers/servers.css")
25
+
26
+ // mount
27
+ const mount = ctx.ui.mountPoint()
28
+ const root = createRoot(mount)
29
+ cleanup.add(() => {
30
+ root.unmount()
31
+ mount.remove()
32
+ })
33
+ root.render(createElement(ServersPopover, { model }))
34
+
35
+ // commands
36
+ cleanup.add(registerServersCommands(ctx, model))
37
+
38
+ // dock
39
+ const dock = ctx.docks.register({
40
+ id: "servers",
41
+ title: "Toggle running servers",
42
+ icon: ICON_SERVERS,
43
+ category: "main",
44
+ order: 2,
45
+ command: "plugin:servers:toggle",
46
+ isActive: () => model.open.value,
47
+ })
48
+ cleanup.add(() => dock.dispose())
49
+ cleanup.add(model.open.subscribe(() => {
50
+ if (model.open.value) model.setAnchor(dock.element)
51
+ dock.update()
52
+ }))
53
+ model.setAnchor(dock.element)
54
+
55
+ // outside click
56
+ const onPointerDown = (event) => {
57
+ if (!model.open.value) return
58
+ const path = event.composedPath()
59
+ if (path.includes(dock.element)) return
60
+ if (path.some((node) => node instanceof Element && node.classList.contains("docserve-servers"))) return
61
+ model.close()
62
+ }
63
+ document.addEventListener("pointerdown", onPointerDown, true)
64
+ cleanup.add(() => document.removeEventListener("pointerdown", onPointerDown, true))
65
+
66
+ // events
67
+ cleanup.add(ctx.events.on("live:connected", () => {
68
+ model.load({ force: true })
69
+ }))
70
+
71
+ // teardown
72
+ cleanup.add(() => model.dispose())
73
+
74
+ return cleanup.dispose
75
+ },
76
+ }
77
+ }
@@ -0,0 +1,30 @@
1
+ import ICON_SERVERS_RAW from "~icons/lucide/server?raw&width=18&height=18"
2
+ import { iconSvg } from "../../widget/lib/dom.js"
3
+ import type { ServersModel } from "./model.js"
4
+
5
+ const ICON_SERVERS = iconSvg(ICON_SERVERS_RAW)
6
+
7
+ export function registerServersCommands(ctx: DocserveContext, model: ServersModel): () => void {
8
+ const offToggle = ctx.commands.register({
9
+ id: "plugin:servers:toggle",
10
+ title: "Toggle running servers",
11
+ category: "View",
12
+ icon: ICON_SERVERS,
13
+ keys: ["s"],
14
+ run: () => model.toggle(),
15
+ })
16
+
17
+ const offClose = ctx.commands.register({
18
+ id: "plugin:servers:close",
19
+ title: "Close running servers",
20
+ keys: ["Escape"],
21
+ showInPalette: false,
22
+ when: () => model.open.value,
23
+ run: () => model.close(),
24
+ })
25
+
26
+ return () => {
27
+ offClose()
28
+ offToggle()
29
+ }
30
+ }