@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.
Files changed (277) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +781 -2
  3. package/bin/workpad.ts +1509 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/workpad.js +1190 -0
  6. package/dist/client/runtime.js +1194 -0
  7. package/dist/define.js +27 -0
  8. package/dist/plugins/alert/README.md +18 -0
  9. package/dist/plugins/alert/index.js +40 -0
  10. package/dist/plugins/alert/public/style.css +40 -0
  11. package/dist/plugins/ansi/README.md +36 -0
  12. package/dist/plugins/ansi/index.js +63 -0
  13. package/dist/plugins/ansi/public/style.css +28 -0
  14. package/dist/plugins/badge/README.md +32 -0
  15. package/dist/plugins/badge/index.js +28 -0
  16. package/dist/plugins/badge/public/style.css +30 -0
  17. package/dist/plugins/chat/README.md +81 -0
  18. package/dist/plugins/chat/index.js +96 -0
  19. package/dist/plugins/chat/public/client.js +3 -0
  20. package/dist/plugins/chat/public/style.css +143 -0
  21. package/dist/plugins/claude/README.md +116 -0
  22. package/dist/plugins/claude/channel.js +130 -0
  23. package/dist/plugins/claude/doctor.js +316 -0
  24. package/dist/plugins/claude/index.js +199 -0
  25. package/dist/plugins/claude/session.js +68 -0
  26. package/dist/plugins/code/README.md +54 -0
  27. package/dist/plugins/code/diff.js +75 -0
  28. package/dist/plugins/code/index.js +105 -0
  29. package/dist/plugins/code/public/client.js +1 -0
  30. package/dist/plugins/code/public/style.css +99 -0
  31. package/dist/plugins/comments/README.md +30 -0
  32. package/dist/plugins/comments/index.js +39 -0
  33. package/dist/plugins/comments/public/client.js +24 -0
  34. package/dist/plugins/comments/public/style.css +224 -0
  35. package/dist/plugins/details/README.md +24 -0
  36. package/dist/plugins/details/index.js +28 -0
  37. package/dist/plugins/details/public/style.css +7 -0
  38. package/dist/plugins/draft/README.md +14 -0
  39. package/dist/plugins/draft/index.js +27 -0
  40. package/dist/plugins/draft/public/style.css +21 -0
  41. package/dist/plugins/forge/README.md +22 -0
  42. package/dist/plugins/forge/index.js +83 -0
  43. package/dist/plugins/form/README.md +126 -0
  44. package/dist/plugins/form/index.js +179 -0
  45. package/dist/plugins/form/public/style.css +172 -0
  46. package/dist/plugins/grid/README.md +45 -0
  47. package/dist/plugins/grid/index.js +35 -0
  48. package/dist/plugins/grid/public/style.css +98 -0
  49. package/dist/plugins/iframe/README.md +18 -0
  50. package/dist/plugins/iframe/index.js +49 -0
  51. package/dist/plugins/iframe/public/style.css +28 -0
  52. package/dist/plugins/index.js +44 -0
  53. package/dist/plugins/lightbox/README.md +24 -0
  54. package/dist/plugins/lightbox/index.js +20 -0
  55. package/dist/plugins/lightbox/public/client.js +4 -0
  56. package/dist/plugins/lightbox/public/style.css +66 -0
  57. package/dist/plugins/mermaid/README.md +21 -0
  58. package/dist/plugins/mermaid/index.js +42 -0
  59. package/dist/plugins/mermaid/public/style.css +21 -0
  60. package/dist/plugins/opencode/README.md +48 -0
  61. package/dist/plugins/opencode/channel.js +92 -0
  62. package/dist/plugins/opencode/index.js +64 -0
  63. package/dist/plugins/opencode/shim.js +150 -0
  64. package/dist/plugins/quiet/README.md +26 -0
  65. package/dist/plugins/quiet/index.js +23 -0
  66. package/dist/plugins/tabs/README.md +44 -0
  67. package/dist/plugins/tabs/index.js +75 -0
  68. package/dist/plugins/tabs/public/client.js +1 -0
  69. package/dist/plugins/tabs/public/style.css +98 -0
  70. package/dist/plugins/terminal/README.md +63 -0
  71. package/dist/plugins/terminal/host.js +338 -0
  72. package/dist/plugins/terminal/index.js +170 -0
  73. package/dist/plugins/terminal/modes.js +32 -0
  74. package/dist/plugins/terminal/public/client.js +41 -0
  75. package/dist/plugins/terminal/public/style.css +511 -0
  76. package/dist/plugins/terminal/server.js +256 -0
  77. package/dist/plugins/terminal/sizes.js +58 -0
  78. package/dist/plugins/timeline/README.md +48 -0
  79. package/dist/plugins/timeline/index.js +83 -0
  80. package/dist/plugins/timeline/public/style.css +138 -0
  81. package/dist/plugins/toc/README.md +40 -0
  82. package/dist/plugins/toc/index.js +29 -0
  83. package/dist/plugins/toc/public/client.js +1 -0
  84. package/dist/plugins/toc/public/style.css +117 -0
  85. package/dist/plugins/window/README.md +51 -0
  86. package/dist/plugins/window/index.js +91 -0
  87. package/dist/solid/index.js +41 -0
  88. package/dist/src/agents.js +81 -0
  89. package/dist/src/anchors.js +52 -0
  90. package/dist/src/api.js +282 -0
  91. package/dist/src/archive.js +49 -0
  92. package/dist/src/auth.js +67 -0
  93. package/dist/src/build.js +159 -0
  94. package/dist/src/client/page/base.css +1762 -0
  95. package/dist/src/client/page/main.js +2098 -0
  96. package/dist/src/config.js +76 -0
  97. package/dist/src/document.js +93 -0
  98. package/dist/src/entries.js +264 -0
  99. package/dist/src/events.js +322 -0
  100. package/dist/src/files.js +47 -0
  101. package/dist/src/formstate.js +65 -0
  102. package/dist/src/help.js +211 -0
  103. package/dist/src/html.js +34 -0
  104. package/dist/src/hub.js +341 -0
  105. package/dist/src/icons.js +6 -0
  106. package/dist/src/listen.js +132 -0
  107. package/dist/src/listeners.js +64 -0
  108. package/dist/src/load.js +81 -0
  109. package/dist/src/loader.js +179 -0
  110. package/dist/src/migrate.js +314 -0
  111. package/dist/src/package.js +52 -0
  112. package/dist/src/pages/blocks.js +67 -0
  113. package/dist/src/pages/board.js +59 -0
  114. package/dist/src/pages/index.js +27 -0
  115. package/dist/src/pages/markdown.js +863 -0
  116. package/dist/src/pages/render.js +665 -0
  117. package/dist/src/pages/window.js +29 -0
  118. package/dist/src/plugins.js +283 -0
  119. package/dist/src/public.js +24 -0
  120. package/dist/src/rename.js +130 -0
  121. package/dist/src/running.js +146 -0
  122. package/dist/src/serve.js +188 -0
  123. package/dist/src/server.js +1105 -0
  124. package/dist/src/site.js +179 -0
  125. package/dist/src/state.js +155 -0
  126. package/dist/src/status.js +323 -0
  127. package/dist/src/thread.js +68 -0
  128. package/dist/src/watchdog.js +38 -0
  129. package/dist/src/ws.js +128 -0
  130. package/package.json +154 -4
  131. package/plugins/README.md +304 -0
  132. package/plugins/alert/README.md +18 -0
  133. package/plugins/alert/index.ts +50 -0
  134. package/plugins/alert/public/style.css +40 -0
  135. package/plugins/ansi/README.md +36 -0
  136. package/plugins/ansi/index.ts +95 -0
  137. package/plugins/ansi/public/style.css +28 -0
  138. package/plugins/badge/README.md +32 -0
  139. package/plugins/badge/index.ts +36 -0
  140. package/plugins/badge/public/style.css +30 -0
  141. package/plugins/chat/README.md +81 -0
  142. package/plugins/chat/autosize.ts +139 -0
  143. package/plugins/chat/client.tsx +187 -0
  144. package/plugins/chat/index.ts +146 -0
  145. package/plugins/chat/public/style.css +143 -0
  146. package/plugins/claude/README.md +116 -0
  147. package/plugins/claude/channel.ts +136 -0
  148. package/plugins/claude/doctor.ts +274 -0
  149. package/plugins/claude/index.ts +248 -0
  150. package/plugins/claude/session.ts +70 -0
  151. package/plugins/code/README.md +54 -0
  152. package/plugins/code/client.ts +73 -0
  153. package/plugins/code/diff.ts +96 -0
  154. package/plugins/code/index.ts +136 -0
  155. package/plugins/code/public/style.css +99 -0
  156. package/plugins/comments/README.md +30 -0
  157. package/plugins/comments/client.ts +723 -0
  158. package/plugins/comments/index.ts +48 -0
  159. package/plugins/comments/public/style.css +224 -0
  160. package/plugins/details/README.md +24 -0
  161. package/plugins/details/index.ts +30 -0
  162. package/plugins/details/public/style.css +7 -0
  163. package/plugins/draft/README.md +14 -0
  164. package/plugins/draft/index.ts +23 -0
  165. package/plugins/draft/public/style.css +21 -0
  166. package/plugins/forge/README.md +22 -0
  167. package/plugins/forge/index.ts +84 -0
  168. package/plugins/form/README.md +126 -0
  169. package/plugins/form/index.ts +199 -0
  170. package/plugins/form/public/style.css +172 -0
  171. package/plugins/grid/README.md +45 -0
  172. package/plugins/grid/index.ts +41 -0
  173. package/plugins/grid/public/style.css +98 -0
  174. package/plugins/iframe/README.md +18 -0
  175. package/plugins/iframe/index.ts +53 -0
  176. package/plugins/iframe/public/style.css +28 -0
  177. package/plugins/index.ts +49 -0
  178. package/plugins/lightbox/README.md +24 -0
  179. package/plugins/lightbox/client.ts +109 -0
  180. package/plugins/lightbox/index.ts +25 -0
  181. package/plugins/lightbox/public/style.css +66 -0
  182. package/plugins/mermaid/README.md +21 -0
  183. package/plugins/mermaid/index.ts +46 -0
  184. package/plugins/mermaid/public/style.css +21 -0
  185. package/plugins/opencode/README.md +48 -0
  186. package/plugins/opencode/channel.ts +112 -0
  187. package/plugins/opencode/index.ts +91 -0
  188. package/plugins/opencode/shim.ts +166 -0
  189. package/plugins/quiet/README.md +26 -0
  190. package/plugins/quiet/index.ts +28 -0
  191. package/plugins/tabs/README.md +44 -0
  192. package/plugins/tabs/client.ts +96 -0
  193. package/plugins/tabs/index.ts +103 -0
  194. package/plugins/tabs/public/style.css +98 -0
  195. package/plugins/terminal/README.md +63 -0
  196. package/plugins/terminal/client.ts +391 -0
  197. package/plugins/terminal/host.ts +434 -0
  198. package/plugins/terminal/index.ts +199 -0
  199. package/plugins/terminal/modes.ts +30 -0
  200. package/plugins/terminal/public/style.css +225 -0
  201. package/plugins/terminal/server.ts +266 -0
  202. package/plugins/terminal/sizes.ts +72 -0
  203. package/plugins/timeline/README.md +48 -0
  204. package/plugins/timeline/index.ts +82 -0
  205. package/plugins/timeline/public/style.css +138 -0
  206. package/plugins/toc/README.md +40 -0
  207. package/plugins/toc/client.ts +236 -0
  208. package/plugins/toc/index.ts +55 -0
  209. package/plugins/toc/public/style.css +117 -0
  210. package/plugins/window/README.md +51 -0
  211. package/plugins/window/index.ts +92 -0
  212. package/skills/workpad/SKILL.md +588 -0
  213. package/solid/index.tsx +80 -0
  214. package/src/agents.ts +133 -0
  215. package/src/anchors.ts +85 -0
  216. package/src/api.ts +444 -0
  217. package/src/archive.ts +75 -0
  218. package/src/auth.ts +99 -0
  219. package/src/build.ts +213 -0
  220. package/src/client/base.ts +19 -0
  221. package/src/client/forms.ts +420 -0
  222. package/src/client/markdown.ts +147 -0
  223. package/src/client/page/base.css +1762 -0
  224. package/src/client/page/board.ts +137 -0
  225. package/src/client/page/elsewhere.ts +153 -0
  226. package/src/client/page/fresh.ts +343 -0
  227. package/src/client/page/heading-link.ts +32 -0
  228. package/src/client/page/live.ts +134 -0
  229. package/src/client/page/main.ts +398 -0
  230. package/src/client/page/orphans.ts +64 -0
  231. package/src/client/page/place.ts +260 -0
  232. package/src/client/page/sort.ts +49 -0
  233. package/src/client/page/sticky.ts +58 -0
  234. package/src/client/page/theme.ts +36 -0
  235. package/src/client/page/view.ts +9 -0
  236. package/src/client/page/waiting.ts +278 -0
  237. package/src/client/page/window.ts +40 -0
  238. package/src/client/reactive.ts +19 -0
  239. package/src/client/read.ts +111 -0
  240. package/src/client/runtime.ts +499 -0
  241. package/src/client/scroll.ts +90 -0
  242. package/src/client/toast.ts +99 -0
  243. package/src/config.ts +111 -0
  244. package/src/define.ts +997 -0
  245. package/src/document.ts +191 -0
  246. package/src/entries.ts +352 -0
  247. package/src/events.ts +463 -0
  248. package/src/files.ts +74 -0
  249. package/src/formstate.ts +72 -0
  250. package/src/help.ts +238 -0
  251. package/src/html.ts +44 -0
  252. package/src/hub.ts +426 -0
  253. package/src/icons.ts +18 -0
  254. package/src/listen.ts +171 -0
  255. package/src/listeners.ts +89 -0
  256. package/src/load.ts +113 -0
  257. package/src/loader.ts +227 -0
  258. package/src/migrate.ts +352 -0
  259. package/src/package.ts +62 -0
  260. package/src/pages/blocks.ts +106 -0
  261. package/src/pages/board.ts +89 -0
  262. package/src/pages/index.ts +79 -0
  263. package/src/pages/markdown.ts +976 -0
  264. package/src/pages/render.ts +748 -0
  265. package/src/pages/window.ts +69 -0
  266. package/src/plugins.ts +319 -0
  267. package/src/public.ts +39 -0
  268. package/src/rename.ts +147 -0
  269. package/src/running.ts +171 -0
  270. package/src/serve.ts +221 -0
  271. package/src/server.ts +1385 -0
  272. package/src/site.ts +170 -0
  273. package/src/state.ts +327 -0
  274. package/src/status.ts +466 -0
  275. package/src/thread.ts +98 -0
  276. package/src/watchdog.ts +53 -0
  277. package/src/ws.ts +155 -0
@@ -0,0 +1,260 @@
1
+ // The reader's place stays where it is on screen, whatever changes around it: a page update, a chat
2
+ // message drawn by its island above them, a block's markdown or diagram drawn after it showed. The
3
+ // place is one element: the field the reader is typing in, if any, else the innermost element at the
4
+ // middle of the view (below the header), where the reader reads: a change between the top of the view
5
+ // and there moves the top instead, never what they are reading. Whenever anything on the page changes or resizes and it moved
6
+ // without the reader scrolling, the page scrolls by as much. The reader's own scrolling sets the
7
+ // new place. At the very top of the page there is no place to keep: what comes in above shows.
8
+ //
9
+ // What the reader is on comes before the top of the view: the field they type in, or the control
10
+ // they last used (a radio they clicked in the form they answer, which WebKit doesn't focus), while
11
+ // it is in view. Otherwise a form answered above them would be the place, and what changes in it (a
12
+ // reply in its chat, it folding) would move the form they are in.
13
+ //
14
+ // This is the browser's scroll anchoring, done by the page: WebKit has none, and Chromium's loses its
15
+ // anchor when an update replaces elements, so the browser's own is turned off to keep the two from
16
+ // both correcting. A page update replaces the blocks it changed; the place in one is found again in
17
+ // its new render, by where it was in the block (see `beforeUpdate`).
18
+
19
+ type Sigs = string[]
20
+ interface Place {
21
+ el: Element
22
+ top: number
23
+ /** where the page was scrolled to then: scrolled since, by the reader, before its scroll event */
24
+ scrolled: number
25
+ /**
26
+ * The elements around it up to its block, and where each was: should something take the place
27
+ * out of the page (an island that draws its content again), the nearest one still there is held
28
+ * instead, so what moved with it is put back all the same
29
+ */
30
+ around: { el: Element; top: number }[]
31
+ }
32
+
33
+ let main: HTMLElement
34
+ let place: Place | null = null
35
+ /** The control of the page the reader last clicked or changed. */
36
+ let used: Element | null = null
37
+
38
+ const typing = (el: Element | null): el is HTMLElement =>
39
+ el instanceof HTMLElement &&
40
+ el.matches(
41
+ 'input:not([type=radio], [type=checkbox], [type=button], [type=submit]), textarea, select, [contenteditable]',
42
+ )
43
+
44
+ /** Laid out: not inside a closed <details> or otherwise not drawn. */
45
+ const shown = (el: Element) => el.isConnected && el.getClientRects().length > 0
46
+
47
+ /** The top of the view, below the header (which sticks there). */
48
+ function viewTop() {
49
+ return Math.max(0, document.querySelector('.wp-nav')?.getBoundingClientRect().bottom ?? 0)
50
+ }
51
+
52
+ /** A sticky title (an open details' summary) stays at the top while what is under it moves: never the place. */
53
+ const stuck = (el: Element) => {
54
+ const position = getComputedStyle(el).position
55
+ return position === 'sticky' || position === 'fixed'
56
+ }
57
+
58
+ /** The middle of the view, below the header: where the reader reads. */
59
+ const viewMiddle = () => (viewTop() + innerHeight) / 2
60
+
61
+ /** The innermost element at height `y` of the view: the first one reaching below it, and inside it, the same. */
62
+ function atLine(y: number): Element | null {
63
+ let found: Element | null = null
64
+ for (let parent: Element = main; ;) {
65
+ const next = [...parent.children].find(c => {
66
+ if (stuck(c)) return false
67
+ const r = c.getBoundingClientRect()
68
+ return r.height > 0 && r.bottom > y
69
+ })
70
+ if (!next) return found
71
+ found = next
72
+ // starts below the line already (the line is in a gap): the first thing after it
73
+ if (next.getBoundingClientRect().top >= y) return found
74
+ parent = next
75
+ }
76
+ }
77
+
78
+ /** The elements around `el` in the page's content, innermost first, and where each is now. */
79
+ function aroundOf(el: Element) {
80
+ const around: Place['around'] = []
81
+ for (let at = el.parentElement; at && at !== main && main.contains(at); at = at.parentElement)
82
+ around.push({ el: at, top: at.getBoundingClientRect().top })
83
+ return around
84
+ }
85
+
86
+ const placeAt = (el: Element, top = el.getBoundingClientRect().top): Place => ({
87
+ el,
88
+ top,
89
+ scrolled: scrollY,
90
+ around: aroundOf(el),
91
+ })
92
+
93
+ /** In view, below the header: some of it between the top of the view and its bottom. */
94
+ function inView(el: Element) {
95
+ const r = el.getBoundingClientRect()
96
+ return r.bottom > viewTop() && r.top < innerHeight
97
+ }
98
+
99
+ /**
100
+ * What the reader is on: the field they type in, else the control they last used, each while in
101
+ * view. Scrolled away from it (a chat box keeps the focus), they read elsewhere: the middle then.
102
+ */
103
+ function onNow(): Element | null {
104
+ const focused = document.activeElement
105
+ if (typing(focused) && shown(focused) && inView(focused)) return focused
106
+ for (const el of [focused, used])
107
+ if (el instanceof HTMLElement && el !== document.body && main.contains(el) && shown(el) && inView(el)) return el
108
+ return null
109
+ }
110
+
111
+ function pick() {
112
+ const el = onNow() ?? (scrollY > 0 ? atLine(viewMiddle()) : null)
113
+ place = el ? placeAt(el) : null
114
+ }
115
+
116
+ /** Put the place back where it was on screen, if it moved; then it is where it is. */
117
+ function keep() {
118
+ if (!place || place.scrolled !== scrollY) return pick()
119
+ if (!shown(place.el)) {
120
+ // gone from the page (drawn again): the nearest element around it that is still there, put
121
+ // back where it was, then a place afresh. Hidden (a details closed): nothing to keep
122
+ const held = place.el.isConnected ? null : place.around.find(a => shown(a.el))
123
+ if (held) {
124
+ const moved = held.el.getBoundingClientRect().top - held.top
125
+ if (Math.abs(moved) >= 1) scrollBy(0, moved)
126
+ }
127
+ return pick()
128
+ }
129
+ const moved = place.el.getBoundingClientRect().top - place.top
130
+ // moves of less than a pixel left be, but not forgotten: they would add up
131
+ if (Math.abs(moved) >= 1) {
132
+ scrollBy(0, moved)
133
+ place.scrolled = scrollY
134
+ // the page can't scroll that far (its end): where it is is the place now
135
+ const now = place.el.getBoundingClientRect().top
136
+ if (Math.abs(now - place.top) >= 1) place.top = now
137
+ }
138
+ // what is around it may have moved apart from it: where each is now
139
+ place.around = aroundOf(place.el)
140
+ }
141
+
142
+ /** The top-level block an element is in, and the way down to it: the index of each child taken. */
143
+ function pathOf(el: Element) {
144
+ const path: number[] = []
145
+ let at = el
146
+ while (at.parentElement && at.parentElement !== main) {
147
+ path.unshift([...at.parentElement.children].indexOf(at))
148
+ at = at.parentElement
149
+ }
150
+ return at.parentElement === main ? { block: [...main.children].indexOf(at), path } : null
151
+ }
152
+
153
+ /** Which of the blocks with its signature block `i` is. */
154
+ const nthOf = (sigs: Sigs, i: number) => sigs.slice(0, i).filter(s => s === sigs[i]).length
155
+ /** The index of the `nth` block with signature `sig`, or -1. */
156
+ function indexOf(sigs: Sigs, sig: string, nth: number) {
157
+ let seen = 0
158
+ return sigs.findIndex(s => s === sig && seen++ === nth)
159
+ }
160
+
161
+ /**
162
+ * Before a page update that turns blocks with signatures `from` into `to`: note the place, and how to
163
+ * find it if the update replaces it. Call what it returns once the update is in.
164
+ */
165
+ export function beforeUpdate(from: Sigs, to: Sigs): () => void {
166
+ // a focused element the update takes (a button clicked, a field) stays where it is too, while in
167
+ // view: scrolled away from it, the reader reads elsewhere (the focus stays long after)
168
+ const focused = document.activeElement
169
+ if (focused instanceof HTMLElement && focused !== document.body && main.contains(focused) && shown(focused) && inView(focused))
170
+ place = placeAt(focused)
171
+ else pick()
172
+ const before = place
173
+ const at = before && main.contains(before.el) ? pathOf(before.el) : null
174
+ const blockTop = at ? main.children[at.block].getBoundingClientRect().top : 0
175
+ return () => {
176
+ if (!before) return pick()
177
+ if (before.el.isConnected) {
178
+ place = before
179
+ return keep()
180
+ }
181
+ // replaced, the field with the focus: the one it went to
182
+ const now = document.activeElement
183
+ if (before.el === focused && now instanceof HTMLElement && now !== document.body && main.contains(now)) {
184
+ place = { ...placeAt(now, before.top), scrolled: before.scrolled }
185
+ return keep()
186
+ }
187
+ if (!at) return pick()
188
+ // its block, in the new render: the same block if the update kept it, else the block in its
189
+ // place, counted from the kept block before it
190
+ const keptTo = new Set(to)
191
+ let block = keptTo.has(from[at.block]) ? indexOf(to, from[at.block], nthOf(from, at.block)) : -1
192
+ if (block < 0) {
193
+ let prev = at.block - 1
194
+ while (prev >= 0 && !keptTo.has(from[prev])) prev--
195
+ const base = prev < 0 ? -1 : indexOf(to, from[prev], nthOf(from, prev))
196
+ if (prev < 0 || base >= 0) block = base + (at.block - prev)
197
+ }
198
+ const el = main.children[block]
199
+ if (!el) return pick()
200
+ // down the same way, as far as it goes
201
+ let target: Element = el
202
+ let whole = true
203
+ for (const i of at.path) {
204
+ const child = target.children[i]
205
+ if (!child) {
206
+ whole = false
207
+ break
208
+ }
209
+ target = child
210
+ }
211
+ place = { ...(whole ? placeAt(target, before.top) : placeAt(el, blockTop)), scrolled: before.scrolled }
212
+ keep()
213
+ }
214
+ }
215
+
216
+ export function trackPlace(root: HTMLElement) {
217
+ main = root
218
+ document.documentElement.style.overflowAnchor = 'none'
219
+ // the reader scrolled: the place now. The page's own scrolling, to keep it, is not the reader's:
220
+ // its scroll event comes later, when more can have moved, which is kept as well (see `keep`)
221
+ addEventListener('scroll', keep, { passive: true })
222
+ // typed somewhere else, a control used, or resized: the place now
223
+ addEventListener('resize', pick)
224
+ const use = (e: Event) => {
225
+ const el = e.target instanceof Element ? e.target.closest('input, button, select, textarea, label, summary') : null
226
+ if (!el || !main.contains(el)) return
227
+ // a label stands for its control, unless the control is not drawn (a styled one)
228
+ used = el instanceof HTMLLabelElement && el.control && shown(el.control) ? el.control : el
229
+ pick()
230
+ }
231
+ document.addEventListener('click', use, true)
232
+ document.addEventListener('change', use, true)
233
+ document.addEventListener('focusin', pick)
234
+ document.addEventListener('focusout', () => queueMicrotask(pick))
235
+ // anything changed: before it is drawn, the place back where it was, and for a few frames after,
236
+ // as what changed can lay out again later (a transition, a module that draws once it loaded)
237
+ let frames = 0
238
+ let frame = 0
239
+ const follow = () => {
240
+ keep()
241
+ frame = --frames > 0 ? requestAnimationFrame(follow) : 0
242
+ }
243
+ const changed = () => {
244
+ keep()
245
+ frames = 30
246
+ if (!frame) frame = requestAnimationFrame(follow)
247
+ }
248
+ new MutationObserver(changed).observe(document.documentElement, {
249
+ childList: true,
250
+ subtree: true,
251
+ characterData: true,
252
+ attributes: true,
253
+ })
254
+ // what resizes without a change to the page: a stylesheet, an image or a font that loaded. Not a
255
+ // ResizeObserver on the page: as an ancestor of the plugins' own, it would miss the sizes they
256
+ // change in theirs, and WebKit reports a loop
257
+ document.addEventListener('load', changed, true)
258
+ document.fonts?.addEventListener('loadingdone', changed)
259
+ pick()
260
+ }
@@ -0,0 +1,49 @@
1
+ // A list the reader can sort: a `<select data-wp-sort="<name>">` sorts the `ul` after its heading
2
+ // by what each `li` carries (`data-order`, `data-updated`, `data-title`: the overview's documents).
3
+ // The choice is kept per list in this browser, and put back after a live update, which writes the
4
+ // list again in the server's order.
5
+ import { storageKey } from '../base.ts'
6
+ const KEY = (name: string) => storageKey(`wp-sort:${name}`)
7
+
8
+ function sortList(select: HTMLSelectElement) {
9
+ const list = select.closest('section')?.querySelector('ul')
10
+ if (!list) return
11
+ const by = select.value
12
+ const value = (li: HTMLElement) =>
13
+ by === 'title' ? (li.dataset.title ?? '') : Number(by === 'updated' ? li.dataset.updated : li.dataset.order)
14
+ const rows = [...list.children] as HTMLElement[]
15
+ rows.sort((a, b) => {
16
+ const x = value(a),
17
+ y = value(b)
18
+ if (by === 'updated') return (y as number) - (x as number)
19
+ return typeof x === 'string' ? x.localeCompare(y as string) : (x as number) - (y as number)
20
+ })
21
+ list.append(...rows)
22
+ }
23
+
24
+ export function applySorts() {
25
+ for (const select of document.querySelectorAll<HTMLSelectElement>('select[data-wp-sort]')) {
26
+ let kept: string | null = null
27
+ try {
28
+ kept = localStorage.getItem(KEY(select.dataset.wpSort!))
29
+ } catch {
30
+ /* private window */
31
+ }
32
+ if (kept && [...select.options].some(o => o.value === kept)) select.value = kept
33
+ sortList(select)
34
+ }
35
+ }
36
+
37
+ export function trackSorts() {
38
+ document.addEventListener('change', e => {
39
+ const select = e.target as HTMLSelectElement
40
+ if (!select.matches?.('select[data-wp-sort]')) return
41
+ try {
42
+ localStorage.setItem(KEY(select.dataset.wpSort!), select.value)
43
+ } catch {
44
+ /* private window */
45
+ }
46
+ sortList(select)
47
+ })
48
+ applySorts()
49
+ }
@@ -0,0 +1,58 @@
1
+ // An open <details>'s summary stays at the top while its content is read, below the page's header;
2
+ // a summary of details inside details stacks below the summaries of the details around it. CSS
3
+ // makes them sticky (base.css); this says how far from the top each sticks: the header's height,
4
+ // then each open summary around it, measured, as a summary can take more than one line. Measured
5
+ // again when a details opens or closes, the window resizes, or the page updates. Closing a details by
6
+ // its title while the title is stuck (its start is above the view) scrolls back to it, so the
7
+ // reader lands at the title, not somewhere far below where the content was.
8
+
9
+ function place(body: HTMLElement) {
10
+ const nav = document.querySelector<HTMLElement>('.wp-nav')
11
+ const base = nav ? nav.getBoundingClientRect().height : 0
12
+ for (const summary of body.querySelectorAll<HTMLElement>('details > summary')) {
13
+ let top = base,
14
+ depth = 0
15
+ for (
16
+ let d = summary.parentElement!.parentElement?.closest('details');
17
+ d && body.contains(d);
18
+ d = d.parentElement?.closest('details')
19
+ ) {
20
+ const outer = d.querySelector<HTMLElement>(':scope > summary')
21
+ if (outer && d.open) top += outer.getBoundingClientRect().height
22
+ depth++
23
+ }
24
+ summary.style.setProperty('--wp-sticky-top', `${top}px`)
25
+ // an inner summary that scrolls away goes under the ones around it, all under the header
26
+ summary.style.setProperty('--wp-sticky-z', String(Math.max(1, 4 - depth)))
27
+ }
28
+ }
29
+
30
+ /** A click on a stuck title: once its details has closed, the title where it was stuck. */
31
+ function keepTitle(e: MouseEvent) {
32
+ const summary = (e.target as HTMLElement).closest?.('summary')
33
+ const details = summary?.parentElement
34
+ if (!summary || !(details instanceof HTMLDetailsElement) || !details.open) return
35
+ const top = parseFloat(summary.style.getPropertyValue('--wp-sticky-top')) || 0
36
+ // stuck: the details starts above where its title is held
37
+ if (details.getBoundingClientRect().top >= top - 1) return
38
+ requestAnimationFrame(() => scrollBy({ top: details.getBoundingClientRect().top - top }))
39
+ }
40
+
41
+ export function trackSticky(main: HTMLElement, onRendered: (fn: () => void) => void) {
42
+ // the reading area: the content and the lists after it (a page without one: the content alone)
43
+ const body = main.closest<HTMLElement>('.wp-body') ?? main
44
+ body.addEventListener('click', keepTitle)
45
+ let frame = 0
46
+ const later = () => {
47
+ cancelAnimationFrame(frame)
48
+ frame = requestAnimationFrame(() => place(body))
49
+ }
50
+ // toggle doesn't bubble: listened for on the way down
51
+ body.addEventListener('toggle', later, true)
52
+ addEventListener('resize', later)
53
+ onRendered(later)
54
+ // a list after the content draws itself again as the log changes (chats no longer on the page),
55
+ // and its summary with it: measured again then too
56
+ new MutationObserver(later).observe(body, { childList: true, subtree: true })
57
+ place(body)
58
+ }
@@ -0,0 +1,36 @@
1
+ // The page's light and dark theme: the reader's choice (kept in localStorage), else the system's.
2
+ import type { Theme } from '../../define.ts'
3
+ import { reactive } from '../reactive'
4
+
5
+ export type ThemePreference = Theme | 'auto'
6
+
7
+ const media = matchMedia('(prefers-color-scheme: dark)')
8
+ const resolve = (): Theme =>
9
+ (document.documentElement.dataset.theme as Theme | undefined) ?? (media.matches ? 'dark' : 'light')
10
+ let theme = resolve()
11
+
12
+ function update() {
13
+ const next = resolve()
14
+ if (next === theme) return
15
+ theme = next
16
+ currentTheme.notify()
17
+ }
18
+ media.addEventListener('change', update)
19
+
20
+ /** The theme in effect, as it changes (api.theme). */
21
+ export const currentTheme = reactive(() => theme)
22
+
23
+ export const themePreference = (): ThemePreference =>
24
+ (document.documentElement.dataset.theme as Theme | undefined) ?? 'auto'
25
+
26
+ export function setThemePreference(pref: ThemePreference) {
27
+ if (pref === 'auto') delete document.documentElement.dataset.theme
28
+ else document.documentElement.dataset.theme = pref
29
+ try {
30
+ if (pref === 'auto') localStorage.removeItem('wp-theme')
31
+ else localStorage.setItem('wp-theme', pref)
32
+ } catch {
33
+ /* the choice still applies to this visit */
34
+ }
35
+ update()
36
+ }
@@ -0,0 +1,9 @@
1
+ // The part of the window the page is seen in. The core may put something at the bottom of the
2
+ // window (the terminal drawer) and says how tall it is in `--wp-inset-bottom`; what is behind it is
3
+ // not in view, so measurements of what the reader sees stop above it.
4
+
5
+ /** The bottom of the visible part of the page, in window coordinates. */
6
+ export function viewBottom(): number {
7
+ const inset = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--wp-inset-bottom')) || 0
8
+ return Math.max(0, innerHeight - inset)
9
+ }