@yaks/ux 0.0.0-stage → 0.2.36

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 (286) hide show
  1. package/Answer.js +43 -0
  2. package/Answer.js.map +1 -0
  3. package/Answer.ts +84 -0
  4. package/Conversation.js +520 -0
  5. package/Conversation.js.map +1 -0
  6. package/Conversation.ts +649 -0
  7. package/ConversationBox.js +106 -0
  8. package/ConversationBox.js.map +1 -0
  9. package/ConversationBox.ts +198 -0
  10. package/ConversationLines.js +29 -0
  11. package/ConversationLines.js.map +1 -0
  12. package/ConversationLines.ts +48 -0
  13. package/Disclosure.js +79 -0
  14. package/Disclosure.js.map +1 -0
  15. package/Disclosure.ts +130 -0
  16. package/Edit.js +330 -0
  17. package/Edit.js.map +1 -0
  18. package/Edit.ts +488 -0
  19. package/EntityPopup.js +35 -0
  20. package/EntityPopup.js.map +1 -0
  21. package/EntityPopup.ts +52 -0
  22. package/Fields.js +161 -0
  23. package/Fields.js.map +1 -0
  24. package/Fields.ts +276 -0
  25. package/LICENSE +202 -0
  26. package/Order.js +105 -0
  27. package/Order.js.map +1 -0
  28. package/Order.ts +159 -0
  29. package/README.md +1023 -2
  30. package/Save.js +50 -0
  31. package/Save.js.map +1 -0
  32. package/Save.ts +115 -0
  33. package/Sort.js +49 -0
  34. package/Sort.js.map +1 -0
  35. package/Sort.ts +98 -0
  36. package/Submit.js +115 -0
  37. package/Submit.js.map +1 -0
  38. package/Submit.ts +174 -0
  39. package/Text.js +114 -0
  40. package/Text.js.map +1 -0
  41. package/Text.ts +140 -0
  42. package/TokenPicker.js +65 -0
  43. package/TokenPicker.js.map +1 -0
  44. package/TokenPicker.ts +117 -0
  45. package/_dist/Answer.d.ts +39 -0
  46. package/_dist/Answer.d.ts.map +1 -0
  47. package/_dist/Conversation.d.ts +131 -0
  48. package/_dist/Conversation.d.ts.map +1 -0
  49. package/_dist/ConversationBox.d.ts +29 -0
  50. package/_dist/ConversationBox.d.ts.map +1 -0
  51. package/_dist/ConversationLines.d.ts +13 -0
  52. package/_dist/ConversationLines.d.ts.map +1 -0
  53. package/_dist/Disclosure.d.ts +36 -0
  54. package/_dist/Disclosure.d.ts.map +1 -0
  55. package/_dist/Edit.d.ts +87 -0
  56. package/_dist/Edit.d.ts.map +1 -0
  57. package/_dist/EntityPopup.d.ts +12 -0
  58. package/_dist/EntityPopup.d.ts.map +1 -0
  59. package/_dist/Fields.d.ts +29 -0
  60. package/_dist/Fields.d.ts.map +1 -0
  61. package/_dist/Order.d.ts +40 -0
  62. package/_dist/Order.d.ts.map +1 -0
  63. package/_dist/Save.d.ts +28 -0
  64. package/_dist/Save.d.ts.map +1 -0
  65. package/_dist/Sort.d.ts +28 -0
  66. package/_dist/Sort.d.ts.map +1 -0
  67. package/_dist/Submit.d.ts +48 -0
  68. package/_dist/Submit.d.ts.map +1 -0
  69. package/_dist/Text.d.ts +27 -0
  70. package/_dist/Text.d.ts.map +1 -0
  71. package/_dist/TokenPicker.d.ts +19 -0
  72. package/_dist/TokenPicker.d.ts.map +1 -0
  73. package/_dist/anchor.d.ts +52 -0
  74. package/_dist/anchor.d.ts.map +1 -0
  75. package/_dist/anchored.d.ts +30 -0
  76. package/_dist/anchored.d.ts.map +1 -0
  77. package/_dist/completion-state.d.ts +65 -0
  78. package/_dist/completion-state.d.ts.map +1 -0
  79. package/_dist/completion.d.ts +73 -0
  80. package/_dist/completion.d.ts.map +1 -0
  81. package/_dist/composer-browser.d.ts +1 -0
  82. package/_dist/composer-browser.d.ts.map +1 -0
  83. package/_dist/composer.d.ts +31 -0
  84. package/_dist/composer.d.ts.map +1 -0
  85. package/_dist/context-menu-browser.d.ts +4 -0
  86. package/_dist/context-menu-browser.d.ts.map +1 -0
  87. package/_dist/context-menu.d.ts +95 -0
  88. package/_dist/context-menu.d.ts.map +1 -0
  89. package/_dist/drag.d.ts +29 -0
  90. package/_dist/drag.d.ts.map +1 -0
  91. package/_dist/edit-keys.d.ts +42 -0
  92. package/_dist/edit-keys.d.ts.map +1 -0
  93. package/_dist/emit.d.ts +7 -0
  94. package/_dist/emit.d.ts.map +1 -0
  95. package/_dist/entity-popup-browser.d.ts +3 -0
  96. package/_dist/entity-popup-browser.d.ts.map +1 -0
  97. package/_dist/face/FaceTagger.d.ts +25 -0
  98. package/_dist/face/FaceTagger.d.ts.map +1 -0
  99. package/_dist/face/review.d.ts +5 -0
  100. package/_dist/face/review.d.ts.map +1 -0
  101. package/_dist/face/tags.d.ts +99 -0
  102. package/_dist/face/tags.d.ts.map +1 -0
  103. package/_dist/field-values.d.ts +4 -0
  104. package/_dist/field-values.d.ts.map +1 -0
  105. package/_dist/fit-list.d.ts +4 -0
  106. package/_dist/fit-list.d.ts.map +1 -0
  107. package/_dist/hits.d.ts +27 -0
  108. package/_dist/hits.d.ts.map +1 -0
  109. package/_dist/host.d.ts +91 -0
  110. package/_dist/host.d.ts.map +1 -0
  111. package/_dist/image-fallback.d.ts +5 -0
  112. package/_dist/image-fallback.d.ts.map +1 -0
  113. package/_dist/keys.d.ts +95 -0
  114. package/_dist/keys.d.ts.map +1 -0
  115. package/_dist/kit.d.ts +23 -0
  116. package/_dist/kit.d.ts.map +1 -0
  117. package/_dist/lightbox-browser.d.ts +2 -0
  118. package/_dist/lightbox-browser.d.ts.map +1 -0
  119. package/_dist/lightbox.d.ts +42 -0
  120. package/_dist/lightbox.d.ts.map +1 -0
  121. package/_dist/live.d.ts +21 -0
  122. package/_dist/live.d.ts.map +1 -0
  123. package/_dist/mod.d.ts +67 -0
  124. package/_dist/mod.d.ts.map +1 -0
  125. package/_dist/read.d.ts +12 -0
  126. package/_dist/read.d.ts.map +1 -0
  127. package/_dist/resize.d.ts +43 -0
  128. package/_dist/resize.d.ts.map +1 -0
  129. package/_dist/rows.d.ts +31 -0
  130. package/_dist/rows.d.ts.map +1 -0
  131. package/_dist/save-browser.d.ts +29 -0
  132. package/_dist/save-browser.d.ts.map +1 -0
  133. package/_dist/select-browser.d.ts +1 -0
  134. package/_dist/select-browser.d.ts.map +1 -0
  135. package/_dist/select.d.ts +12 -0
  136. package/_dist/select.d.ts.map +1 -0
  137. package/_dist/sending.d.ts +10 -0
  138. package/_dist/sending.d.ts.map +1 -0
  139. package/_dist/sort-browser.d.ts +11 -0
  140. package/_dist/sort-browser.d.ts.map +1 -0
  141. package/_dist/state.d.ts +74 -0
  142. package/_dist/state.d.ts.map +1 -0
  143. package/_dist/suggest.d.ts +18 -0
  144. package/_dist/suggest.d.ts.map +1 -0
  145. package/_dist/swipe.d.ts +60 -0
  146. package/_dist/swipe.d.ts.map +1 -0
  147. package/_dist/ui.d.ts +6 -0
  148. package/_dist/ui.d.ts.map +1 -0
  149. package/_dist/upload-browser.d.ts +13 -0
  150. package/_dist/upload-browser.d.ts.map +1 -0
  151. package/_dist/views.d.ts +8 -0
  152. package/_dist/views.d.ts.map +1 -0
  153. package/_dist/vocab.d.ts +6 -0
  154. package/_dist/vocab.d.ts.map +1 -0
  155. package/anchor.js +58 -0
  156. package/anchor.js.map +1 -0
  157. package/anchor.ts +87 -0
  158. package/anchored.js +180 -0
  159. package/anchored.js.map +1 -0
  160. package/anchored.ts +232 -0
  161. package/completion-state.js +76 -0
  162. package/completion-state.js.map +1 -0
  163. package/completion-state.ts +120 -0
  164. package/completion.js +301 -0
  165. package/completion.js.map +1 -0
  166. package/completion.ts +433 -0
  167. package/composer-browser.js +4 -0
  168. package/composer-browser.js.map +1 -0
  169. package/composer-browser.ts +5 -0
  170. package/composer.js +42 -0
  171. package/composer.js.map +1 -0
  172. package/composer.ts +53 -0
  173. package/context-menu-browser.js +221 -0
  174. package/context-menu-browser.js.map +1 -0
  175. package/context-menu-browser.ts +231 -0
  176. package/context-menu.js +146 -0
  177. package/context-menu.js.map +1 -0
  178. package/context-menu.ts +194 -0
  179. package/deno.json +1 -0
  180. package/drag.js +33 -0
  181. package/drag.js.map +1 -0
  182. package/drag.ts +41 -0
  183. package/edit-keys.js +51 -0
  184. package/edit-keys.js.map +1 -0
  185. package/edit-keys.ts +81 -0
  186. package/emit.js +34 -0
  187. package/emit.js.map +1 -0
  188. package/emit.ts +55 -0
  189. package/entity-popup-browser.js +94 -0
  190. package/entity-popup-browser.js.map +1 -0
  191. package/entity-popup-browser.ts +100 -0
  192. package/face/FaceTagger.js +361 -0
  193. package/face/FaceTagger.js.map +1 -0
  194. package/face/FaceTagger.ts +507 -0
  195. package/face/NameProposal.js +61 -0
  196. package/face/NameProposal.js.map +1 -0
  197. package/face/NameProposal.ts +100 -0
  198. package/face/review.js +33 -0
  199. package/face/review.js.map +1 -0
  200. package/face/review.ts +49 -0
  201. package/face/tags.js +344 -0
  202. package/face/tags.js.map +1 -0
  203. package/face/tags.ts +357 -0
  204. package/field-values.js +20 -0
  205. package/field-values.js.map +1 -0
  206. package/field-values.ts +35 -0
  207. package/fit-list.js +66 -0
  208. package/fit-list.js.map +1 -0
  209. package/fit-list.ts +81 -0
  210. package/hits.js +65 -0
  211. package/hits.js.map +1 -0
  212. package/hits.ts +75 -0
  213. package/host.js +88 -0
  214. package/host.js.map +1 -0
  215. package/host.ts +212 -0
  216. package/image-fallback.js +32 -0
  217. package/image-fallback.js.map +1 -0
  218. package/image-fallback.ts +37 -0
  219. package/keys.js +418 -0
  220. package/keys.js.map +1 -0
  221. package/keys.ts +444 -0
  222. package/kit.js +41 -0
  223. package/kit.js.map +1 -0
  224. package/kit.ts +71 -0
  225. package/lightbox-browser.js +248 -0
  226. package/lightbox-browser.js.map +1 -0
  227. package/lightbox-browser.ts +232 -0
  228. package/lightbox.js +84 -0
  229. package/lightbox.js.map +1 -0
  230. package/lightbox.ts +103 -0
  231. package/live.js +111 -0
  232. package/live.js.map +1 -0
  233. package/live.ts +139 -0
  234. package/mod.js +67 -0
  235. package/mod.js.map +1 -0
  236. package/mod.ts +180 -0
  237. package/package.json +143 -4
  238. package/read.js +33 -0
  239. package/read.js.map +1 -0
  240. package/read.ts +56 -0
  241. package/resize.js +159 -0
  242. package/resize.js.map +1 -0
  243. package/resize.ts +203 -0
  244. package/rows.js +73 -0
  245. package/rows.js.map +1 -0
  246. package/rows.ts +97 -0
  247. package/save-browser.js +469 -0
  248. package/save-browser.js.map +1 -0
  249. package/save-browser.ts +530 -0
  250. package/select-browser.js +4 -0
  251. package/select-browser.js.map +1 -0
  252. package/select-browser.ts +5 -0
  253. package/select.js +296 -0
  254. package/select.js.map +1 -0
  255. package/select.ts +343 -0
  256. package/sending.js +36 -0
  257. package/sending.js.map +1 -0
  258. package/sending.ts +44 -0
  259. package/sort-browser.js +51 -0
  260. package/sort-browser.js.map +1 -0
  261. package/sort-browser.ts +70 -0
  262. package/state.js +99 -0
  263. package/state.js.map +1 -0
  264. package/state.ts +121 -0
  265. package/suggest.js +28 -0
  266. package/suggest.js.map +1 -0
  267. package/suggest.ts +32 -0
  268. package/swipe.js +186 -0
  269. package/swipe.js.map +1 -0
  270. package/swipe.ts +250 -0
  271. package/ui.js +507 -0
  272. package/ui.js.map +1 -0
  273. package/ui.ts +378 -0
  274. package/upload-browser.js +195 -0
  275. package/upload-browser.js.map +1 -0
  276. package/upload-browser.ts +303 -0
  277. package/upload-files.js +41 -0
  278. package/upload-files.js.map +1 -0
  279. package/upload-files.ts +49 -0
  280. package/views.js +8 -0
  281. package/views.js.map +1 -0
  282. package/views.ts +9 -0
  283. package/vocab.js +16 -0
  284. package/vocab.js.map +1 -0
  285. package/vocab.json +287 -0
  286. package/vocab.ts +17 -0
@@ -0,0 +1,649 @@
1
+ /**
2
+ * A conversation's lines, newest last, as a reader follows them: it stays at
3
+ * the newest line while lines arrive, until the reader scrolls up to read,
4
+ * and it draws the last `take` lines it is given, more when asked. Its state
5
+ * is `Conversation{take, away}` on the page entity its owner names
6
+ * (`conversationAt`): how many lines back it draws, and whether the reader
7
+ * has scrolled away from the newest. It knows no data names: its owner
8
+ * gives it lines already drawn, and what the conversation is doing now.
9
+ *
10
+ * `Conversation.Box` is where a person writes to it: their words are a draft
11
+ * (the host's `drafts`), Enter sends them and Shift+Enter starts a line, and
12
+ * Send emits them as the bundle its owner says they become, Stop a `Stop`
13
+ * event; the owner says where each goes, and spends the draft once the words
14
+ * are taken. Given `onFiles`, it takes files pasted or dropped anywhere on it
15
+ * and emits them, lit while they are held over it; the owner holds them and
16
+ * draws them in its slots.
17
+ *
18
+ * Where it scrolls in a place of its own rather than the page, it holds the
19
+ * page to what a person sees (`fit`): a phone's keyboard shortens the page,
20
+ * so its own scroll area gets shorter and the box sits on the keyboard, and
21
+ * the page itself never scrolls.
22
+ *
23
+ * Whether each fold in a line is open is `Disclosure{open}` in the same page
24
+ * graph, on the page entity of the line and the fold's place in it (`Folds`,
25
+ * `useFold`), so a line drawn again keeps its folds as the reader left them
26
+ * though its words changed ("Reading…" to "Read").
27
+ *
28
+ * Only `h` and hooks, so it paints under any renderer that has them.
29
+ *
30
+ * @module
31
+ */
32
+
33
+ /* @ts-types="npm:@types/react@19.3.0" */ import {
34
+ createContext,
35
+ createElement as h,
36
+ type JSX,
37
+ type ReactNode,
38
+ useContext,
39
+ useLayoutEffect,
40
+ useRef,
41
+ useState,
42
+ } from 'react'
43
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
44
+ import { derivedEid } from '@yaks/graph/identity'
45
+ import { Button, Transcript } from '@yaks/ui/parts'
46
+ import { useNow } from '@yaks/ui/Time'
47
+ import { type DatedRow, datedRows, type Timed } from './ConversationLines.js'
48
+ import { type Anchored, anchored, scroller } from './anchored.js'
49
+ export {
50
+ activityRuns,
51
+ type DatedRow,
52
+ DatedRows,
53
+ datedRows,
54
+ } from './ConversationLines.js'
55
+ import { Box, type BoxProps } from './ConversationBox.js'
56
+ import {
57
+ type Disclosed,
58
+ disclosed,
59
+ Disclosure,
60
+ disclosureAt,
61
+ DisclosureMark,
62
+ type DisclosureProps,
63
+ isOpen,
64
+ Loading,
65
+ UNFETCHED,
66
+ } from './Disclosure.js'
67
+
68
+ /** How many lines it draws before it is asked for more, and how many more
69
+ * each time: a screen or two, so moving to a page holding it lays over a few
70
+ * thousand nodes rather than tens of thousands. A reader scrolling up to the
71
+ * oldest drawn asks for the next. */
72
+ export let TAKE = 40
73
+
74
+ /** How close to the oldest line drawn, in px, a reader scrolling up brings
75
+ * the earlier ones in. */
76
+ export let NEAR = 120
77
+
78
+ /** A line, by the id it is put under (`#row-<id>`): drawn here (`node`), or
79
+ * as it was drawn elsewhere, the server's page (`html`), kept as it is until
80
+ * it is given drawn. */
81
+ export type Line = Timed & {
82
+ id: string
83
+ node?: ReactNode
84
+ html?: string
85
+ /** In-progress work, drawn in the reserved activity slot. */
86
+ transient?: boolean
87
+ /** The lines grouped in this row. */
88
+ of?: string[]
89
+ }
90
+
91
+ /** What a conversation holds of its own. */
92
+ export type Conversing = {
93
+ /** how many lines back it draws */
94
+ take?: number
95
+ /** the reader has scrolled up from the newest line */
96
+ away?: boolean
97
+ }
98
+
99
+ /** The page entity of `owner`'s conversation with `session`. */
100
+ export let conversationAt = (owner: string, session: string): string =>
101
+ derivedEid(['Conversation', owner, session].join('|'))
102
+
103
+ /** Its state, as `e` holds it. */
104
+ export let conversingOf = (e?: Bundle): Conversing =>
105
+ (e?.Conversation as Conversing | undefined) ?? {}
106
+
107
+ /** The bundle it emits: its state, changed. */
108
+ export let conversed = (e: Bundle, change: Conversing): Bundle => ({
109
+ entity: { eid: e.entity.eid },
110
+ Conversation: { ...conversingOf(e), ...change },
111
+ })
112
+
113
+ /** The lines it draws of those it is given, and how many earlier it does
114
+ * not: those before its `take`, and those its owner was not given
115
+ * (`earlier`). */
116
+ export let shown = (
117
+ lines: Line[],
118
+ take = TAKE,
119
+ earlier = 0,
120
+ ): { lines: Line[]; hidden: number } => {
121
+ let drawn = take >= lines.length ? lines : lines.slice(lines.length - take)
122
+ return { lines: drawn, hidden: lines.length - drawn.length + earlier }
123
+ }
124
+
125
+ /** What its owner gives it. Any other prop goes on the conversation itself. */
126
+ export type ConversationProps = {
127
+ e: Bundle
128
+ onChange: (b: Bundle) => void
129
+ /** the page graph's state an eid names: its folds', which it emits through
130
+ * `onChange` too; without it every fold is drawn closed */
131
+ state?: (eid: string) => Bundle | undefined
132
+ lines: Line[]
133
+ display?: (lines: Line[], row: DatedRow<Line>, now: number) => ReactNode
134
+ beforeLines?: ReactNode
135
+ /** what the conversation is doing now, said under its lines */
136
+ now?: ReactNode
137
+ /** how many lines came before these, that its owner can bring */
138
+ earlier?: number
139
+ /** older history is available, with its size unknown */
140
+ more?: boolean
141
+ /** what scrolls it: the nearest scrolling place around it, or the page */
142
+ pane?: () => HTMLElement | null
143
+ /** whether a line arriving below a reader away from the newest is news to
144
+ * them, which the way back to the newest says ("New message"): every line,
145
+ * unless told */
146
+ news?: (line: Line) => boolean
147
+ [attribute: string]: unknown
148
+ }
149
+
150
+ // The folds drawn under a conversation: where their state is read and goes;
151
+ // and under a line, which line it is and how many folds it has drawn.
152
+ type Held = {
153
+ state?: (eid: string) => Bundle | undefined
154
+ onChange?: (b: Bundle) => void
155
+ line?: { at: string; n: number }
156
+ }
157
+ let Held = createContext<Held>({})
158
+
159
+ /** What is drawn under it is a line's, `line` its id: each fold in it is
160
+ * owned by the line and its place there. A conversation puts every line it
161
+ * draws under one, and a line inside a fold (a call's turn) is under one of
162
+ * its own. */
163
+ export let Folds = (
164
+ { line, children }: { line: string; children?: ReactNode },
165
+ ): JSX.Element => {
166
+ let up = useContext(Held)
167
+ // How many folds it has drawn, kept while it is: a fold drawn again keeps
168
+ // its place though its line drew others before it.
169
+ let placed = useRef({ n: 0 })
170
+ return h(Held.Provider, {
171
+ value: { ...up, line: Object.assign(placed.current, { at: line }) },
172
+ children,
173
+ })
174
+ }
175
+
176
+ /** The state of a fold its line draws, for a `Disclosure` and its
177
+ * `DisclosureMark`: on `disclosureAt` its line and its place in it, the
178
+ * order its line first drew its folds in, which a server's drawing and the
179
+ * page's mounted over it share. Its conversation reads and keeps it; outside
180
+ * one it is closed, and outside a line it is none and the fold is a plain
181
+ * `details`. */
182
+ export let useFold = (): Disclosed => {
183
+ let held = useContext(Held)
184
+ let place = useRef<number | null>(null)
185
+ if (!held.line) return {}
186
+ place.current ??= held.line.n++
187
+ let eid = disclosureAt(`${held.line.at} ${place.current}`)
188
+ return {
189
+ e: held.state?.(eid) ?? { entity: { eid } },
190
+ onChange: held.onChange,
191
+ }
192
+ }
193
+
194
+ /** A fold in a line: a `Disclosure` on the state `useFold` gives it. */
195
+ export let Fold = (
196
+ props: Omit<DisclosureProps, keyof Disclosed>,
197
+ ): JSX.Element => h(Disclosure, { ...props, ...useFold() })
198
+
199
+ export { type BoxProps, stopped } from './ConversationBox.js'
200
+ export { disclosed, Disclosure, DisclosureMark, isOpen, Loading, UNFETCHED }
201
+
202
+ // What scrolls the page.
203
+ let paged = (doc: Document): HTMLElement =>
204
+ (doc.scrollingElement ?? doc.documentElement) as HTMLElement
205
+
206
+ // An address outside the conversation names a layer over it.
207
+ let addressed = (el: HTMLElement) => {
208
+ let doc = el.ownerDocument
209
+ let id = decodeURIComponent(doc.defaultView?.location?.hash?.slice(1) ?? '')
210
+ return id ? doc.getElementById(id) : null
211
+ }
212
+
213
+ // The lines a reader is held at: those drawn in its list, not the rows a
214
+ // grouped line holds of its own.
215
+ let rowsOf = (el: HTMLElement): Element[] =>
216
+ [...el.querySelectorAll('.Transcript_Lines [id^="row-"]')].filter((row) =>
217
+ !row.parentElement?.closest('[id^="row-"]')
218
+ )
219
+
220
+ export let transcriptRow = (
221
+ line: Line,
222
+ divider?: ReactNode,
223
+ before?: string,
224
+ ): JSX.Element => {
225
+ let props = {
226
+ id: `row-${line.id}`,
227
+ 'data-line-at': line.at,
228
+ 'data-line-of': line.of?.join(','),
229
+ 'data-transient': line.transient ? '' : undefined,
230
+ 'data-before': before,
231
+ }
232
+ return line.html == null
233
+ ? h(
234
+ Folds,
235
+ { key: line.id, line: line.id },
236
+ h(Transcript.Row, props, divider, h(Transcript.Content, {}, line.node)),
237
+ )
238
+ : h(
239
+ Transcript.Row,
240
+ { ...props, key: line.id },
241
+ divider,
242
+ h(Transcript.Content, {
243
+ dangerouslySetInnerHTML: { __html: line.html },
244
+ }),
245
+ )
246
+ }
247
+
248
+ let activityRows = (lines: Line[], visible: Set<string>) => {
249
+ let rows: ReactNode[] = []
250
+ let before: string | undefined
251
+ for (let i = lines.length - 1; i >= 0; i--) {
252
+ let line = lines[i]
253
+ if (line.transient) rows.push(transcriptRow(line, undefined, before))
254
+ else if (visible.has(line.id)) before = line.id
255
+ }
256
+ return rows.reverse()
257
+ }
258
+
259
+ /** The visual viewport, as much of it as `seen` reads. */
260
+ export type Seen = { height: number; scale: number; offsetTop: number }
261
+
262
+ /** What the page is held to, given the visual viewport, how far the page is
263
+ * scrolled, whether a field is typed in, and the window's height: while one
264
+ * is and a keyboard covers part of the window, the visual viewport's height
265
+ * in px at the page's own scale, and otherwise none, so it is the whole
266
+ * window; and whether the page goes back to its top, which it does unless a
267
+ * person pinched it larger, when where it is shown is theirs. A field typed
268
+ * in with nothing over the window holds no height: the root's property
269
+ * changing restyles every element on the page, so leaving the field would
270
+ * cost a press a frame of the whole page for nothing. */
271
+ export let seen = (
272
+ view: Seen,
273
+ scrollY: number,
274
+ typing: boolean,
275
+ whole: number,
276
+ ): { height?: number; top: boolean } => {
277
+ let height = Math.round(view.height * view.scale)
278
+ return {
279
+ ...(typing && height < whole && { height }),
280
+ top: view.scale == 1 && (scrollY > 0 || view.offsetTop > 0),
281
+ }
282
+ }
283
+
284
+ /** The property a page's frame takes its height from, set on its root:
285
+ * `height: var(--viewport-height, 100dvh)`. */
286
+ export let VIEWPORT = '--viewport-height'
287
+
288
+ // What a keyboard comes up for.
289
+ let EDITABLE = 'input, textarea, select, [contenteditable]'
290
+
291
+ /** Holds `doc`'s page to what a person sees as it changes (`seen`): a
292
+ * keyboard opening shortens it rather than scrolling it, which iOS Safari
293
+ * does whatever the viewport meta says. It holds a height only while a field
294
+ * is typed in, and reads it again whenever the visual viewport or window
295
+ * changes, focus moves and a finger lifts, since iOS does not always tell the
296
+ * visual viewport a keyboard closed (around a swipe). Returns what lets it
297
+ * go. */
298
+ export let fit = (doc: Document): () => void => {
299
+ let win = doc.defaultView
300
+ let view = win?.visualViewport
301
+ if (!win || !view) return () => {}
302
+ let root = doc.documentElement
303
+ // Focus leaving is heard before it has gone.
304
+ let held = (ev?: Event) => {
305
+ let at = doc.activeElement
306
+ let typing = !!at?.matches?.(EDITABLE) &&
307
+ !(ev?.type == 'focusout' && ev.target == at)
308
+ let { height, top } = seen(view, win.scrollY ?? 0, typing, win.innerHeight)
309
+ if (height == null) root.style.removeProperty(VIEWPORT)
310
+ else root.style.setProperty(VIEWPORT, `${height}px`)
311
+ if (top) win.scrollTo(0, 0)
312
+ }
313
+ let heard: [EventTarget, string][] = [
314
+ [view, 'resize'],
315
+ [view, 'scroll'],
316
+ [win, 'resize'],
317
+ [doc, 'focusin'],
318
+ [doc, 'focusout'],
319
+ [doc, 'touchend'],
320
+ [doc, 'touchcancel'],
321
+ ]
322
+ held()
323
+ for (let [at, type] of heard) at.addEventListener(type, held)
324
+ return () => {
325
+ for (let [at, type] of heard) at.removeEventListener(type, held)
326
+ root.style.removeProperty(VIEWPORT)
327
+ }
328
+ }
329
+
330
+ /** The lines, the newest last, with what it is doing now under them;
331
+ * `Conversation.Box` is where a person writes to it. Showing nothing at all,
332
+ * no line, no way to earlier ones and nothing going on, it is marked
333
+ * `data-empty`, for its owner's empty state. */
334
+ export let Conversation: ((p: ConversationProps) => JSX.Element) & {
335
+ Box: (p: BoxProps) => JSX.Element
336
+ } = Object.assign((
337
+ {
338
+ e,
339
+ onChange,
340
+ state,
341
+ lines,
342
+ display,
343
+ beforeLines,
344
+ now,
345
+ earlier,
346
+ more,
347
+ pane,
348
+ news,
349
+ ...rest
350
+ }: ConversationProps,
351
+ ): JSX.Element => {
352
+ let { take = TAKE, away = false } = conversingOf(e)
353
+ let clock = useNow()
354
+ let previous = useRef<
355
+ {
356
+ visible: Set<string>
357
+ durable: Set<string>
358
+ activity: Set<string>
359
+ } | null
360
+ >(null)
361
+ let permanent = lines.filter((line) => !line.transient)
362
+ // The initial window limits what a fresh view draws. Keep the rows this
363
+ // reader has already seen as messages arrive at its other end.
364
+ let start = permanent.findIndex((line) =>
365
+ previous.current?.visible.has(line.id)
366
+ )
367
+ let drawn = shown(
368
+ permanent,
369
+ start < 0 ? take : Math.max(take, permanent.length - start),
370
+ earlier,
371
+ )
372
+ let visible = new Set(drawn.lines.map((line) => line.id))
373
+ let durable = permanent.flatMap((line) => line.of ?? [line.id])
374
+ let activity = lines.filter((line) => line.transient)
375
+ let [unread, setUnread] = useState(false)
376
+ let kept = new Set(
377
+ [...drawn.lines, ...activity].filter((line) => line.html != null).map((
378
+ line,
379
+ ) => `row-${line.id}`),
380
+ )
381
+ let root = useRef<HTMLElement>(null)
382
+ // What the listeners read: the latest of what it was given, the place
383
+ // that scrolls, and where the reader last was in it.
384
+ let at = useRef({
385
+ e,
386
+ onChange,
387
+ state,
388
+ kept,
389
+ lines,
390
+ news,
391
+ away,
392
+ pane: null as HTMLElement | null,
393
+ list: null as Anchored | null,
394
+ y: 0,
395
+ hidden: 0,
396
+ take,
397
+ asked: 0,
398
+ older: () => {},
399
+ })
400
+ at.current = { ...at.current, e, onChange, state, kept, lines, news }
401
+ let fromServer = (target: Element) => {
402
+ // A grouped line can hold rows of its own, including a sub-thread.
403
+ let row = target.closest?.('.Transcript_Row')
404
+ while (row && root.current?.contains(row)) {
405
+ if (at.current.kept.has(row.id)) return true
406
+ row = row.parentElement?.closest('.Transcript_Row') ?? null
407
+ }
408
+ return false
409
+ }
410
+ let say = (away: boolean) => {
411
+ if (away == at.current.away) return
412
+ at.current.away = away
413
+ at.current.onChange(conversed(at.current.e, { away }))
414
+ }
415
+ // A conversation standing hidden (another view of its page showing) moves
416
+ // nothing: the pane it shares is that view's.
417
+ let unseen = () => !!root.current?.closest('[hidden]')
418
+ let covered = () => {
419
+ let el = root.current
420
+ if (!el) return false
421
+ let address = addressed(el)
422
+ return address && !el.contains(address)
423
+ }
424
+ let newest = (slide = false) => at.current.list?.newest(slide)
425
+ // A line kept as the server drew it holds folds and marks no state drew:
426
+ // each is opened or closed as the page holds it.
427
+ useLayoutEffect(() => {
428
+ let el = root.current
429
+ if (!el || !state) return
430
+ for (let fold of el.querySelectorAll('[data-disclosure]')) {
431
+ if (!fromServer(fold)) continue
432
+ let open = isOpen(state(fold.getAttribute('data-disclosure')!))
433
+ if (open != fold.hasAttribute('open')) fold.toggleAttribute('open', open)
434
+ }
435
+ for (let mark of el.querySelectorAll('[data-disclosure-mark]')) {
436
+ if (!fromServer(mark)) continue
437
+ let open = isOpen(state(mark.getAttribute('data-disclosure-mark')!))
438
+ mark.setAttribute('aria-expanded', String(open))
439
+ }
440
+ })
441
+ // Earlier lines are put above where the reader is, who stays there
442
+ // (`anchored`).
443
+ let older = () => {
444
+ // Away first, so leaving the newest line is said once, with the take.
445
+ at.current.away = true
446
+ at.current.list?.read(false)
447
+ onChange(
448
+ conversed(e, {
449
+ take: Math.max(take, drawn.lines.length) + TAKE,
450
+ away: true,
451
+ }),
452
+ )
453
+ }
454
+ at.current = {
455
+ ...at.current,
456
+ hidden: drawn.hidden + Number(!!more),
457
+ take,
458
+ older,
459
+ }
460
+ // Set up before the first paint, so it is first seen at the newest.
461
+ useLayoutEffect(() => {
462
+ let el = root.current
463
+ let win = el?.ownerDocument.defaultView
464
+ if (!el || !win) return
465
+ let p = pane?.() ?? scroller(el)
466
+ at.current.pane = p
467
+ let unfit = p == paged(el.ownerDocument) ? () => {} : fit(el.ownerDocument)
468
+ let list = anchored(p, {
469
+ content: el,
470
+ rows: () => rowsOf(el),
471
+ follow: !at.current.away,
472
+ onFollow: (follow) => say(!follow),
473
+ counts: (row) => {
474
+ let { lines, news } = at.current
475
+ let line = lines.find((l) => `row-${l.id}` == row.id)
476
+ return !!line && (news?.(line) ?? true)
477
+ },
478
+ onUnseen: setUnread,
479
+ idle: () => unseen() || !!covered(),
480
+ })
481
+ at.current.list = list
482
+ let opened = () => {
483
+ let line = addressed(el)
484
+ if (!line || !el.contains(line)) return void newest()
485
+ // A copied link can name an entry within an activity fold.
486
+ for (
487
+ let fold = line.closest('details');
488
+ fold;
489
+ fold = fold.parentElement?.closest('details') ?? null
490
+ ) {
491
+ if (fold.open) continue
492
+ fold.open = true
493
+ let eid = fold.getAttribute('data-disclosure')
494
+ if (eid) {
495
+ queueMicrotask(() => {
496
+ if (!root.current) return
497
+ let was = at.current.state?.(eid) ?? { entity: { eid } }
498
+ if (!isOpen(was)) at.current.onChange(disclosed(was, true))
499
+ })
500
+ }
501
+ }
502
+ line.scrollIntoView({ block: 'center', behavior: 'instant' })
503
+ at.current.y = p.scrollTop
504
+ list.read()
505
+ }
506
+ opened()
507
+ let scroll = () => {
508
+ if (covered() || unseen()) return
509
+ let now = p.scrollTop
510
+ let was = at.current.y
511
+ at.current.y = now
512
+ // Near the oldest drawn, the next earlier lines come in, once a take.
513
+ let { hidden, take, asked } = at.current
514
+ if (now < was && now < NEAR && hidden && asked != take) {
515
+ at.current.asked = take
516
+ at.current.older()
517
+ }
518
+ }
519
+ let hash = () => {
520
+ if (addressed(el)) opened()
521
+ else if (!at.current.away) newest()
522
+ }
523
+ // A fold of a line kept as the server drew it that the browser opened or
524
+ // closed by itself (a find in the page), which the page then holds so;
525
+ // one drawn from its state only says what it was told.
526
+ let toggled = (ev: Event) => {
527
+ let fold = ev.target as Element
528
+ if (!fromServer(fold)) return
529
+ let eid = fold.getAttribute?.('data-disclosure')
530
+ if (!eid) return
531
+ let open = fold.hasAttribute('open')
532
+ let was = at.current.state?.(eid) ?? { entity: { eid } }
533
+ if (isOpen(was) != open) at.current.onChange(disclosed(was, open))
534
+ }
535
+ // A press on such a fold's summary, or on a mark, which no script drawn
536
+ // there hears: the page holds the other state at once, as a fold drawn
537
+ // from its state does. Left to the browser, the fold would change first
538
+ // and the page hear of it later, and a drawing in between (a scroll, a
539
+ // word streamed) would put it back as the page last held it. Live
540
+ // components handle their own events through their renderer.
541
+ let pressed = (ev: Event) => {
542
+ if (ev.defaultPrevented) return
543
+ let target = ev.target as Element
544
+ if (!fromServer(target)) return
545
+ let mark = target.closest?.('[data-disclosure-mark]')
546
+ let summary = mark ? null : target.closest?.('summary')
547
+ let fold = summary?.parentElement
548
+ // A link or button in a summary is its own to press.
549
+ let own = summary?.contains(target.closest('a, button'))
550
+ let eid = mark?.getAttribute('data-disclosure-mark') ??
551
+ (own ? null : fold?.getAttribute('data-disclosure'))
552
+ if (!eid) return
553
+ if (!mark) ev.preventDefault()
554
+ let was = at.current.state?.(eid) ?? { entity: { eid } }
555
+ at.current.onChange(disclosed(was, !isOpen(was)))
556
+ }
557
+ el.addEventListener('toggle', toggled, true)
558
+ el.addEventListener('click', pressed)
559
+ let scrolls = p == paged(el.ownerDocument) ? el.ownerDocument : p
560
+ scrolls.addEventListener('scroll', scroll, { passive: true })
561
+ win.addEventListener('hashchange', hash)
562
+ return () => {
563
+ el.removeEventListener('toggle', toggled, true)
564
+ el.removeEventListener('click', pressed)
565
+ scrolls.removeEventListener('scroll', scroll)
566
+ win.removeEventListener('hashchange', hash)
567
+ list.stop()
568
+ at.current.list = null
569
+ unfit()
570
+ }
571
+ }, [])
572
+ useLayoutEffect(() => {
573
+ let was = previous.current
574
+ previous.current = {
575
+ visible,
576
+ durable: new Set(durable),
577
+ activity: new Set(activity.map((line) => line.id)),
578
+ }
579
+ // History may be prepended. It is an arrival only when a new line
580
+ // follows the first line we knew, or work in the slot has settled.
581
+ let known = durable.findIndex((id) => was?.durable.has(id))
582
+ let arrived = !!was &&
583
+ durable.some((id, i) =>
584
+ !was.durable.has(id) && (i > known || was.activity.has(id))
585
+ )
586
+ let returning = at.current.away && !away
587
+ at.current.away = away
588
+ if (away) {
589
+ if (at.current.list?.follows()) at.current.list.read(false)
590
+ at.current.list?.hold()
591
+ } else {
592
+ if (arrived || returning) newest(arrived)
593
+ else at.current.list?.hold()
594
+ }
595
+ })
596
+ return h(
597
+ Held.Provider,
598
+ { value: { state, onChange } },
599
+ h(
600
+ Transcript,
601
+ {
602
+ ...rest,
603
+ elRef: root,
604
+ // Nothing drawn, nothing earlier to bring and nothing going on: its
605
+ // owner's empty state shows only then.
606
+ 'data-empty':
607
+ drawn.lines.length || drawn.hidden || more || now != null ||
608
+ activity.length
609
+ ? undefined
610
+ : '',
611
+ },
612
+ drawn.hidden || more
613
+ ? h(
614
+ Button,
615
+ { type: 'button', mod: 'quiet', onClick: older },
616
+ more ? 'Show earlier' : `Show ${drawn.hidden} earlier`,
617
+ )
618
+ : null,
619
+ beforeLines,
620
+ h(
621
+ Transcript.Lines,
622
+ {},
623
+ display
624
+ ? display(drawn.lines, transcriptRow, clock)
625
+ : datedRows(drawn.lines, transcriptRow, clock),
626
+ ),
627
+ h(
628
+ Transcript.Activity,
629
+ {},
630
+ now == null ? null : h(Transcript.Status, { role: 'status' }, now),
631
+ activityRows(lines, visible),
632
+ ),
633
+ away
634
+ ? h(
635
+ Transcript.Jump,
636
+ {},
637
+ h(Transcript.Newest, {
638
+ type: 'button',
639
+ mod: 'quiet',
640
+ onClick: () => {
641
+ say(false)
642
+ newest()
643
+ },
644
+ }, unread ? 'New message' : 'Newest'),
645
+ )
646
+ : null,
647
+ ),
648
+ )
649
+ }, { Box })