@noy4/docserve 0.3.2 → 0.3.3

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 (61) hide show
  1. package/bin.mjs +2 -2
  2. package/dist/widget/index.js +11155 -0
  3. package/package.json +24 -3
  4. package/src/node/adapter.mjs +67 -0
  5. package/src/node/assets.mjs +36 -0
  6. package/src/node/dates.mjs +73 -0
  7. package/src/node/docs-info.mjs +39 -0
  8. package/src/node/files.mjs +43 -0
  9. package/src/node/gallery.mjs +33 -0
  10. package/src/node/inject.mjs +43 -0
  11. package/src/node/meta.mjs +137 -0
  12. package/src/node/routes.mjs +105 -0
  13. package/src/node/scan.mjs +87 -0
  14. package/src/node/serve-static.mjs +158 -0
  15. package/src/node/server.mjs +69 -0
  16. package/src/node/watch.mjs +119 -0
  17. package/src/plugins/copy/client.js +72 -0
  18. package/src/plugins/copy/copy.css +21 -0
  19. package/src/plugins/home/client.js +24 -0
  20. package/src/plugins/sidebar/client.js +105 -0
  21. package/src/plugins/sidebar/grouping.js +108 -0
  22. package/src/plugins/sidebar/icons.js +25 -0
  23. package/src/plugins/sidebar/model.ts +91 -0
  24. package/src/plugins/sidebar/resize-model.ts +47 -0
  25. package/src/plugins/sidebar/sidebar.css +663 -0
  26. package/src/plugins/sidebar/tree-model.ts +23 -0
  27. package/src/plugins/sidebar/tree.ts +69 -0
  28. package/src/plugins/sidebar/ui/PageList.tsx +153 -0
  29. package/src/plugins/sidebar/ui/RadioMenu.tsx +201 -0
  30. package/src/plugins/sidebar/ui/Sidebar.tsx +95 -0
  31. package/src/plugins/sidebar/ui/SidebarHeader.tsx +87 -0
  32. package/src/plugins/sidebar/ui/TreeList.tsx +121 -0
  33. package/src/plugins/sidebar/ui/preview-model.ts +134 -0
  34. package/src/plugins/sidebar/ui/useModel.ts +25 -0
  35. package/src/shared/types.d.ts +193 -0
  36. package/src/widget/commands.js +214 -0
  37. package/src/widget/docks.js +127 -0
  38. package/src/widget/host.js +187 -0
  39. package/src/widget/index.js +14 -0
  40. package/src/widget/lib/disposables.js +44 -0
  41. package/src/widget/lib/dom.js +106 -0
  42. package/src/widget/lib/events.js +65 -0
  43. package/src/widget/lib/persisted-signal.js +40 -0
  44. package/src/widget/lib/storage.js +70 -0
  45. package/src/widget/live.js +86 -0
  46. package/src/widget/pages.js +65 -0
  47. package/src/widget/palette.css +162 -0
  48. package/src/widget/palette.js +259 -0
  49. package/src/widget/placement.js +204 -0
  50. package/src/widget/plugins.js +39 -0
  51. package/src/widget/rpc.js +32 -0
  52. package/src/widget/styles.js +31 -0
  53. package/src/widget/ui.js +50 -0
  54. package/src/widget/widget.css +193 -0
  55. package/server/files.mjs +0 -261
  56. package/server/index.mjs +0 -198
  57. package/server/inject.html +0 -88
  58. package/server/inject.mjs +0 -28
  59. package/server/watch.mjs +0 -104
  60. /package/{server → src/node}/state.mjs +0 -0
  61. /package/{server → src/node}/websocket.mjs +0 -0
@@ -0,0 +1,108 @@
1
+ // Month buckets for the sidebar page list.
2
+
3
+ /**
4
+ * @typedef {object} PageGroup
5
+ * @property {string} key Month `YYYY-MM`, or folder path with a trailing slash.
6
+ * @property {string} label Heading text, e.g. `2026/04`, `guides/` or `/`.
7
+ * @property {DocPage[]} pages
8
+ */
9
+
10
+ const NO_DATE = "No date"
11
+
12
+ /**
13
+ * Local-time month key for a timestamp.
14
+ * @param {number | string} value Unix seconds, or a parseable date string.
15
+ * @returns {string}
16
+ */
17
+ export function monthKey(value) {
18
+ const date = toDate(value)
19
+ if (!date) return ""
20
+ const year = date.getFullYear()
21
+ const month = String(date.getMonth() + 1).padStart(2, "0")
22
+ return `${year}-${month}`
23
+ }
24
+
25
+ /**
26
+ * Heading text for a month key.
27
+ * @param {string} key
28
+ * @returns {string}
29
+ */
30
+ export function monthLabel(key) {
31
+ const match = /^(\d{4})-(\d{2})$/.exec(key)
32
+ if (!match) return NO_DATE
33
+ return `${match[1]}/${match[2]}`
34
+ }
35
+
36
+ /**
37
+ * Partition pages into month groups, newest first. Order inside a group is
38
+ * preserved; pages without a valid date go last.
39
+ * @param {DocPage[]} pages
40
+ * @returns {PageGroup[]}
41
+ */
42
+ export function groupByMonth(pages) {
43
+ /** @type {Map<string, PageGroup>} */
44
+ const groups = new Map()
45
+
46
+ for (const page of pages) {
47
+ const key = monthKey(page.created)
48
+ let group = groups.get(key)
49
+ if (!group) {
50
+ group = { key, label: key ? monthLabel(key) : NO_DATE, pages: [] }
51
+ groups.set(key, group)
52
+ }
53
+ group.pages.push(page)
54
+ }
55
+
56
+ return [...groups.values()].sort((a, b) => {
57
+ if (!a.key) return b.key ? 1 : 0
58
+ if (!b.key) return -1
59
+ return b.key.localeCompare(a.key)
60
+ })
61
+ }
62
+
63
+ /**
64
+ * @param {number | string} value
65
+ * @returns {Date | null}
66
+ */
67
+ function toDate(value) {
68
+ const date = new Date(typeof value === "number" ? value * 1000 : value)
69
+ return Number.isNaN(date.getTime()) ? null : date
70
+ }
71
+
72
+ /**
73
+ * Folder that directly holds a page, with a trailing slash; `""` for the root.
74
+ * @param {string} url Root-absolute page URL, e.g. `/guides/intro.html`.
75
+ * @returns {string}
76
+ */
77
+ export function folderOf(url) {
78
+ const parts = url.split("/").filter(Boolean)
79
+ parts.pop()
80
+ return parts.length ? `${parts.join("/")}/` : ""
81
+ }
82
+
83
+ /**
84
+ * Group pages by the folder that directly holds them, root first. Folders
85
+ * without pages of their own never appear, and groups do not nest.
86
+ * @param {DocPage[]} pages
87
+ * @returns {PageGroup[]}
88
+ */
89
+ export function groupByFolder(pages) {
90
+ /** @type {Map<string, PageGroup>} */
91
+ const groups = new Map()
92
+
93
+ for (const page of pages) {
94
+ const key = folderOf(page.url)
95
+ let group = groups.get(key)
96
+ if (!group) {
97
+ group = { key, label: key || "/", pages: [] }
98
+ groups.set(key, group)
99
+ }
100
+ group.pages.push(page)
101
+ }
102
+
103
+ return [...groups.values()].sort((a, b) => {
104
+ if (!a.key) return b.key ? -1 : 0
105
+ if (!b.key) return 1
106
+ return a.key.localeCompare(b.key, undefined, { numeric: true, sensitivity: "base" })
107
+ })
108
+ }
@@ -0,0 +1,25 @@
1
+ // Inline SVG for the dock item, the header and the view tabs.
2
+ import { iconSvg } from "../../widget/lib/dom.js"
3
+ import ICON_CALENDAR_RAW from "~icons/lucide/calendar?raw&width=18&height=18"
4
+ import ICON_CHECK_RAW from "~icons/lucide/check?raw&width=18&height=18"
5
+ import ICON_CHEVRON_RAW from "~icons/lucide/chevron-right?raw&width=18&height=18"
6
+ import ICON_CHEVRON_DOWN_RAW from "~icons/lucide/chevron-down?raw&width=18&height=18"
7
+ import ICON_FILE_RAW from "~icons/lucide/file-text?raw&width=18&height=18"
8
+ import ICON_FOLDER_RAW from "~icons/lucide/folder?raw&width=18&height=18"
9
+ import ICON_GRID_RAW from "~icons/lucide/layout-grid?raw&width=18&height=18"
10
+ import ICON_LIST_RAW from "~icons/lucide/list?raw&width=18&height=18"
11
+ import ICON_UNGROUP_RAW from "~icons/lucide/ungroup?raw&width=18&height=18"
12
+ import ICON_SIDEBAR_RAW from "~icons/lucide/panel-left?raw&width=18&height=18"
13
+ import ICON_TREE_RAW from "~icons/lucide/list-tree?raw&width=18&height=18"
14
+
15
+ export const ICON_CALENDAR = iconSvg(ICON_CALENDAR_RAW)
16
+ export const ICON_CHECK = iconSvg(ICON_CHECK_RAW)
17
+ export const ICON_CHEVRON = iconSvg(ICON_CHEVRON_RAW)
18
+ export const ICON_CHEVRON_DOWN = iconSvg(ICON_CHEVRON_DOWN_RAW)
19
+ export const ICON_FILE = iconSvg(ICON_FILE_RAW)
20
+ export const ICON_FOLDER = iconSvg(ICON_FOLDER_RAW)
21
+ export const ICON_GRID = iconSvg(ICON_GRID_RAW)
22
+ export const ICON_LIST = iconSvg(ICON_LIST_RAW)
23
+ export const ICON_UNGROUP = iconSvg(ICON_UNGROUP_RAW)
24
+ export const ICON_SIDEBAR = iconSvg(ICON_SIDEBAR_RAW)
25
+ export const ICON_TREE = iconSvg(ICON_TREE_RAW)
@@ -0,0 +1,91 @@
1
+ import { signal } from "@preact/signals-react"
2
+ import { persisted, persistedBool } from "../../widget/lib/persisted-signal.js"
3
+ import { ResizeModel } from "./resize-model.js"
4
+ import { TreeModel } from "./tree-model.js"
5
+
6
+ export type SidebarView = "list" | "gallery" | "tree"
7
+ export type SidebarGrouping = "none" | "month" | "folder"
8
+ export type SidebarStatus = "loading" | "ready" | "error"
9
+
10
+ export interface SidebarModelOptions {
11
+ rpc: RpcContext
12
+ docs?: DocsInfo
13
+ currentPage: string
14
+ }
15
+
16
+ export class SidebarModel {
17
+ readonly docs?: DocsInfo
18
+ readonly currentPage: string
19
+ readonly pages = signal<DocPage[]>([])
20
+ readonly status = signal<SidebarStatus>("loading")
21
+ readonly view = persisted<SidebarView>("docserve:sidebar:view", "list")
22
+ readonly grouping = persisted<SidebarGrouping>("docserve:sidebar:grouping", "month")
23
+ readonly tree = new TreeModel(this.pages)
24
+ readonly resizer = new ResizeModel()
25
+ readonly open = persistedBool("docserve:sidebar:open")
26
+
27
+ #rpc: RpcContext
28
+ #request = 0
29
+
30
+ constructor(options: SidebarModelOptions) {
31
+ this.#rpc = options.rpc
32
+ this.docs = options.docs
33
+ this.currentPage = options.currentPage
34
+ }
35
+
36
+ static fromContext(ctx: DocserveContext): SidebarModel {
37
+ return new SidebarModel({
38
+ rpc: ctx.rpc,
39
+ docs: ctx.docs,
40
+ currentPage: ctx.page,
41
+ })
42
+ }
43
+
44
+ setView(next: SidebarView): void {
45
+ this.view.value = next
46
+ }
47
+
48
+ toggleView(): void {
49
+ this.setView(nextView(this.view.value))
50
+ }
51
+
52
+ setGrouping(next: SidebarGrouping): void {
53
+ this.grouping.value = next
54
+ }
55
+
56
+ toggleGrouping(): void {
57
+ this.setGrouping(nextGrouping(this.grouping.value))
58
+ }
59
+
60
+ setOpen(next: boolean): void {
61
+ this.open.value = next
62
+ }
63
+
64
+ async load({ force = false }: { force?: boolean } = {}): Promise<void> {
65
+ const id = ++this.#request
66
+ if (!this.pages.value.length) this.status.value = "loading"
67
+
68
+ try {
69
+ const result = await this.#rpc.call("docserve:pages:list", { force })
70
+ if (id !== this.#request) return
71
+ this.pages.value = Array.isArray(result) ? (result as DocPage[]) : []
72
+ this.status.value = "ready"
73
+ } catch {
74
+ if (id !== this.#request) return
75
+ this.status.value = "error"
76
+ }
77
+ }
78
+ }
79
+
80
+ const VIEWS: SidebarView[] = ["list", "gallery", "tree"]
81
+ const GROUPINGS: SidebarGrouping[] = ["none", "month", "folder"]
82
+
83
+ function nextView(current: SidebarView): SidebarView {
84
+ const index = VIEWS.indexOf(current)
85
+ return VIEWS[(index + 1) % VIEWS.length]
86
+ }
87
+
88
+ function nextGrouping(current: SidebarGrouping): SidebarGrouping {
89
+ const index = GROUPINGS.indexOf(current)
90
+ return GROUPINGS[(index + 1) % GROUPINGS.length]
91
+ }
@@ -0,0 +1,47 @@
1
+ // Sidebar width, persisted and clamped to the allowed range.
2
+ import { signal } from "@preact/signals-react"
3
+ import type { PointerEvent } from "react"
4
+ import { persisted } from "../../widget/lib/persisted-signal.js"
5
+
6
+ export const DEFAULT_WIDTH = 280
7
+ export const MIN_WIDTH = 220
8
+ export const MAX_WIDTH = 560
9
+
10
+ export class ResizeModel {
11
+ readonly width = persisted<number>("docserve:sidebar:width", DEFAULT_WIDTH, {
12
+ parse: (raw) => {
13
+ const value = Number(raw)
14
+ return Number.isFinite(value) ? clampWidth(value) : DEFAULT_WIDTH
15
+ },
16
+ })
17
+ readonly resizing = signal(false)
18
+
19
+ setWidth(next: number): void {
20
+ this.width.value = clampWidth(next)
21
+ }
22
+
23
+ readonly start = (event: PointerEvent<HTMLElement>): void => {
24
+ event.preventDefault()
25
+ event.currentTarget.setPointerCapture(event.pointerId)
26
+ this.resizing.value = true
27
+ }
28
+
29
+ readonly move = (event: PointerEvent<HTMLElement>): void => {
30
+ if (!this.resizing.value) return
31
+ this.setWidth(Math.min(event.clientX, window.innerWidth * 0.86))
32
+ }
33
+
34
+ readonly stop = (event: PointerEvent<HTMLElement>): void => {
35
+ if (event.currentTarget.hasPointerCapture(event.pointerId))
36
+ event.currentTarget.releasePointerCapture(event.pointerId)
37
+ this.end()
38
+ }
39
+
40
+ readonly end = (): void => {
41
+ this.resizing.value = false
42
+ }
43
+ }
44
+
45
+ export function clampWidth(width: number): number {
46
+ return Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, Math.round(width)))
47
+ }