@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.
Files changed (76) hide show
  1. package/bin.mjs +2 -2
  2. package/dist/widget/index.js +11803 -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/collapse/client.js +45 -0
  18. package/src/plugins/collapse/collapse.css +74 -0
  19. package/src/plugins/collapse/commands.ts +81 -0
  20. package/src/plugins/collapse/model.ts +123 -0
  21. package/src/plugins/collapse/sections.ts +105 -0
  22. package/src/plugins/collapse/view.ts +84 -0
  23. package/src/plugins/copy/client.js +72 -0
  24. package/src/plugins/copy/copy.css +21 -0
  25. package/src/plugins/home/client.js +24 -0
  26. package/src/plugins/sidebar/client.js +106 -0
  27. package/src/plugins/sidebar/grouping.js +108 -0
  28. package/src/plugins/sidebar/icons.js +25 -0
  29. package/src/plugins/sidebar/model.ts +91 -0
  30. package/src/plugins/sidebar/resize-model.ts +47 -0
  31. package/src/plugins/sidebar/sidebar.css +663 -0
  32. package/src/plugins/sidebar/tree-model.ts +23 -0
  33. package/src/plugins/sidebar/tree.ts +69 -0
  34. package/src/plugins/sidebar/ui/PageList.tsx +153 -0
  35. package/src/plugins/sidebar/ui/RadioMenu.tsx +201 -0
  36. package/src/plugins/sidebar/ui/Sidebar.tsx +95 -0
  37. package/src/plugins/sidebar/ui/SidebarHeader.tsx +87 -0
  38. package/src/plugins/sidebar/ui/TreeList.tsx +121 -0
  39. package/src/plugins/sidebar/ui/preview-model.ts +134 -0
  40. package/src/plugins/sidebar/ui/useModel.ts +25 -0
  41. package/src/plugins/toc/client.js +55 -0
  42. package/src/plugins/toc/commands.ts +49 -0
  43. package/src/plugins/toc/heading-html.ts +40 -0
  44. package/src/plugins/toc/model.ts +178 -0
  45. package/src/plugins/toc/outline.js +94 -0
  46. package/src/plugins/toc/scroll-spy.ts +35 -0
  47. package/src/plugins/toc/toc.css +214 -0
  48. package/src/plugins/toc/ui/TocPanel.tsx +87 -0
  49. package/src/shared/types.d.ts +193 -0
  50. package/src/widget/commands.js +214 -0
  51. package/src/widget/docks.js +127 -0
  52. package/src/widget/host.js +187 -0
  53. package/src/widget/index.js +22 -0
  54. package/src/widget/lib/disposables.js +44 -0
  55. package/src/widget/lib/dom.js +106 -0
  56. package/src/widget/lib/events.js +65 -0
  57. package/src/widget/lib/headings.js +132 -0
  58. package/src/widget/lib/persisted-signal.js +40 -0
  59. package/src/widget/lib/storage.js +70 -0
  60. package/src/widget/live.js +86 -0
  61. package/src/widget/pages.js +65 -0
  62. package/src/widget/palette.css +162 -0
  63. package/src/widget/palette.js +259 -0
  64. package/src/widget/placement.js +204 -0
  65. package/src/widget/plugins.js +39 -0
  66. package/src/widget/rpc.js +32 -0
  67. package/src/widget/styles.js +31 -0
  68. package/src/widget/ui.js +50 -0
  69. package/src/widget/widget.css +206 -0
  70. package/server/files.mjs +0 -261
  71. package/server/index.mjs +0 -198
  72. package/server/inject.html +0 -88
  73. package/server/inject.mjs +0 -28
  74. package/server/watch.mjs +0 -104
  75. /package/{server → src/node}/state.mjs +0 -0
  76. /package/{server → src/node}/websocket.mjs +0 -0
@@ -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
+ }