@celljs/ta-web 4.11.0 → 4.13.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 (281) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/esm/browser/assistant/assistant-extension.js +14 -0
  4. package/esm/browser/assistant/assistant-initiative.d.ts +27 -0
  5. package/esm/browser/assistant/assistant-initiative.js +76 -0
  6. package/esm/browser/assistant/assistant-task-source.d.ts +7 -0
  7. package/esm/browser/assistant/assistant-task-source.js +12 -2
  8. package/esm/browser/assistant/assistant-view.js +10 -6
  9. package/esm/browser/assistant/assistant.d.ts +5 -0
  10. package/esm/browser/assistant/assistant.js +25 -6
  11. package/esm/browser/chat/chat-view.js +6 -5
  12. package/esm/browser/components/app-nav.d.ts +3 -1
  13. package/esm/browser/components/app-nav.js +5 -2
  14. package/esm/browser/components/app-view.js +26 -13
  15. package/esm/browser/components/asks-badge.d.ts +5 -0
  16. package/esm/browser/components/asks-badge.js +8 -0
  17. package/esm/browser/components/chat-asks.d.ts +46 -0
  18. package/esm/browser/components/chat-asks.js +101 -0
  19. package/esm/browser/components/chat-list.d.ts +7 -1
  20. package/esm/browser/components/chat-list.js +46 -17
  21. package/esm/browser/components/composer-addon-util.d.ts +11 -0
  22. package/esm/browser/components/composer-addon-util.js +40 -0
  23. package/esm/browser/components/composer-addons.d.ts +1 -7
  24. package/esm/browser/components/composer-addons.js +1 -27
  25. package/esm/browser/components/composer-box.d.ts +1 -0
  26. package/esm/browser/components/composer-box.js +50 -28
  27. package/esm/browser/components/composer.d.ts +10 -1
  28. package/esm/browser/components/composer.js +61 -22
  29. package/esm/browser/components/connection-bar.js +31 -12
  30. package/esm/browser/components/conversation-scroll-hooks.d.ts +16 -0
  31. package/esm/browser/components/conversation-scroll-hooks.js +98 -0
  32. package/esm/browser/components/conversation-scroll-util.d.ts +35 -0
  33. package/esm/browser/components/conversation-scroll-util.js +74 -0
  34. package/esm/browser/components/conversation-scroll.d.ts +57 -33
  35. package/esm/browser/components/conversation-scroll.js +188 -123
  36. package/esm/browser/components/discover-view.js +5 -1
  37. package/esm/browser/components/earlier-messages.d.ts +10 -3
  38. package/esm/browser/components/earlier-messages.js +14 -8
  39. package/esm/browser/components/face.d.ts +24 -0
  40. package/esm/browser/components/face.js +42 -0
  41. package/esm/browser/components/folder-picker.d.ts +2 -0
  42. package/esm/browser/components/folder-picker.js +7 -0
  43. package/esm/browser/components/host-view.d.ts +1 -1
  44. package/esm/browser/components/markdown.js +4 -2
  45. package/esm/browser/components/media.js +25 -11
  46. package/esm/browser/components/mermaid-view.js +45 -3
  47. package/esm/browser/components/message-actions.js +1 -0
  48. package/esm/browser/components/message-footer.d.ts +4 -0
  49. package/esm/browser/components/message-footer.js +8 -1
  50. package/esm/browser/components/message-list.d.ts +15 -1
  51. package/esm/browser/components/message-list.js +204 -41
  52. package/esm/browser/components/phone-shell.d.ts +7 -2
  53. package/esm/browser/components/phone-shell.js +9 -3
  54. package/esm/browser/components/relay-services.d.ts +24 -0
  55. package/esm/browser/components/relay-services.js +249 -0
  56. package/esm/browser/components/relay-view.js +17 -1
  57. package/esm/browser/components/remote-hosts.js +1 -1
  58. package/esm/browser/components/session-view.d.ts +3 -1
  59. package/esm/browser/components/session-view.js +23 -7
  60. package/esm/browser/components/settings-controls.js +3 -3
  61. package/esm/browser/components/settings-parts.js +13 -1
  62. package/esm/browser/components/stop-times.d.ts +47 -0
  63. package/esm/browser/components/stop-times.js +81 -0
  64. package/esm/browser/components/ta-avatar.js +6 -0
  65. package/esm/browser/components/tool-result.d.ts +13 -0
  66. package/esm/browser/components/tool-result.js +138 -0
  67. package/esm/browser/components/tool-view.js +5 -15
  68. package/esm/browser/components/use-stop-times.d.ts +3 -0
  69. package/esm/browser/components/use-stop-times.js +17 -0
  70. package/esm/browser/components/web-composer.js +2 -1
  71. package/esm/browser/device/photo-picker.d.ts +14 -0
  72. package/esm/browser/device/photo-picker.js +48 -0
  73. package/esm/browser/device/row-swipe.d.ts +2 -0
  74. package/esm/browser/device/row-swipe.js +8 -2
  75. package/esm/browser/device/sheet-swipe.d.ts +1 -1
  76. package/esm/browser/device/sheet-swipe.js +17 -3
  77. package/esm/browser/extension/extension-view.d.ts +3 -1
  78. package/esm/browser/extension/extension-view.js +7 -0
  79. package/esm/browser/extension/icons.js +10 -1
  80. package/esm/browser/extension/web-api-components.d.ts +21 -3
  81. package/esm/browser/extension/web-api-contributions.d.ts +128 -0
  82. package/esm/browser/extension/web-extension-api.d.ts +45 -48
  83. package/esm/browser/extension/web-extension-context.d.ts +6 -1
  84. package/esm/browser/extension/web-extension-context.js +72 -7
  85. package/esm/browser/extension/web-extension-protocol.d.ts +18 -2
  86. package/esm/browser/extension/web-extension-protocol.js +2 -0
  87. package/esm/browser/extension/web-extension-run.d.ts +3 -2
  88. package/esm/browser/extension/web-extension-run.js +3 -0
  89. package/esm/browser/extension/web-extensions.d.ts +2 -8
  90. package/esm/browser/extension/web-extensions.js +12 -35
  91. package/esm/browser/extension/web-page-list.d.ts +25 -0
  92. package/esm/browser/extension/web-page-list.js +35 -0
  93. package/esm/browser/extension/web-session-notices.d.ts +6 -1
  94. package/esm/browser/extension/web-session-notices.js +30 -4
  95. package/esm/browser/extension/web-sharing.d.ts +5 -2
  96. package/esm/browser/extension/web-sharing.js +10 -1
  97. package/esm/browser/i18n/locales.d.ts +77 -1
  98. package/esm/browser/i18n/messages.d.ts +76 -0
  99. package/esm/browser/i18n/messages.js +154 -2
  100. package/esm/browser/links/links-protocol.d.ts +2 -0
  101. package/esm/browser/links/links.d.ts +7 -2
  102. package/esm/browser/links/links.js +16 -9
  103. package/esm/browser/markdown/code-renderers.d.ts +4 -0
  104. package/esm/browser/markdown/code-renderers.js +4 -0
  105. package/esm/browser/markdown/lazy.d.ts +8 -0
  106. package/esm/browser/markdown/lazy.js +35 -0
  107. package/esm/browser/markdown/mermaid.d.ts +15 -3
  108. package/esm/browser/markdown/mermaid.js +45 -6
  109. package/esm/browser/notices/notices-protocol.d.ts +5 -0
  110. package/esm/browser/notices/notices.d.ts +19 -0
  111. package/esm/browser/notices/notices.js +85 -2
  112. package/esm/browser/notices/session-notices.d.ts +5 -2
  113. package/esm/browser/notices/session-notices.js +10 -1
  114. package/esm/browser/style/base.d.ts +1 -1
  115. package/esm/browser/style/base.js +2 -1
  116. package/esm/browser/style/conversation.d.ts +1 -1
  117. package/esm/browser/style/conversation.js +53 -0
  118. package/esm/browser/style/device.js +3 -0
  119. package/esm/browser/style/layout.d.ts +1 -1
  120. package/esm/browser/style/layout.js +27 -9
  121. package/esm/browser/style/pages.d.ts +1 -1
  122. package/esm/browser/style/pages.js +5 -0
  123. package/esm/browser/style/phone.d.ts +1 -1
  124. package/esm/browser/style/phone.js +2 -0
  125. package/esm/browser/style/shell.d.ts +1 -1
  126. package/esm/browser/style/shell.js +16 -3
  127. package/esm/browser/style/surfaces.d.ts +1 -1
  128. package/esm/browser/style/surfaces.js +19 -0
  129. package/esm/browser/util/desktop.d.ts +18 -1
  130. package/esm/browser/util/desktop.js +13 -0
  131. package/esm/browser/util/frame.d.ts +2 -0
  132. package/esm/browser/util/frame.js +2 -0
  133. package/esm/browser/util/process.d.ts +69 -2
  134. package/esm/browser/util/process.js +163 -8
  135. package/esm/browser/util/progress.d.ts +6 -1
  136. package/esm/browser/util/progress.js +11 -2
  137. package/esm/browser/util/scroll-rest.d.ts +10 -0
  138. package/esm/browser/util/scroll-rest.js +44 -0
  139. package/esm/browser/util/tool-result.d.ts +32 -0
  140. package/esm/browser/util/tool-result.js +85 -0
  141. package/esm/browser/util/tool.js +7 -0
  142. package/lib/browser/assistant/assistant-extension.js +14 -0
  143. package/lib/browser/assistant/assistant-initiative.d.ts +28 -0
  144. package/lib/browser/assistant/assistant-initiative.js +80 -0
  145. package/lib/browser/assistant/assistant-task-source.d.ts +7 -0
  146. package/lib/browser/assistant/assistant-task-source.js +12 -2
  147. package/lib/browser/assistant/assistant-view.js +8 -4
  148. package/lib/browser/assistant/assistant.d.ts +5 -0
  149. package/lib/browser/assistant/assistant.js +24 -5
  150. package/lib/browser/chat/chat-view.js +6 -5
  151. package/lib/browser/components/app-nav.d.ts +3 -1
  152. package/lib/browser/components/app-nav.js +5 -2
  153. package/lib/browser/components/app-view.js +25 -12
  154. package/lib/browser/components/asks-badge.d.ts +6 -0
  155. package/lib/browser/components/asks-badge.js +44 -0
  156. package/lib/browser/components/chat-asks.d.ts +47 -0
  157. package/lib/browser/components/chat-asks.js +104 -0
  158. package/lib/browser/components/chat-list.d.ts +7 -1
  159. package/lib/browser/components/chat-list.js +46 -17
  160. package/lib/browser/components/composer-addon-util.d.ts +12 -0
  161. package/lib/browser/components/composer-addon-util.js +76 -0
  162. package/lib/browser/components/composer-addons.d.ts +1 -7
  163. package/lib/browser/components/composer-addons.js +2 -29
  164. package/lib/browser/components/composer-box.d.ts +1 -0
  165. package/lib/browser/components/composer-box.js +48 -26
  166. package/lib/browser/components/composer.d.ts +10 -1
  167. package/lib/browser/components/composer.js +61 -21
  168. package/lib/browser/components/connection-bar.js +30 -11
  169. package/lib/browser/components/conversation-scroll-hooks.d.ts +17 -0
  170. package/lib/browser/components/conversation-scroll-hooks.js +135 -0
  171. package/lib/browser/components/conversation-scroll-util.d.ts +36 -0
  172. package/lib/browser/components/conversation-scroll-util.js +78 -0
  173. package/lib/browser/components/conversation-scroll.d.ts +57 -33
  174. package/lib/browser/components/conversation-scroll.js +189 -158
  175. package/lib/browser/components/discover-view.js +5 -1
  176. package/lib/browser/components/earlier-messages.d.ts +10 -3
  177. package/lib/browser/components/earlier-messages.js +16 -9
  178. package/lib/browser/components/face.d.ts +25 -0
  179. package/lib/browser/components/face.js +79 -0
  180. package/lib/browser/components/folder-picker.d.ts +2 -0
  181. package/lib/browser/components/folder-picker.js +7 -0
  182. package/lib/browser/components/host-view.d.ts +1 -1
  183. package/lib/browser/components/markdown.js +3 -1
  184. package/lib/browser/components/media.js +24 -10
  185. package/lib/browser/components/mermaid-view.js +45 -3
  186. package/lib/browser/components/message-actions.js +1 -0
  187. package/lib/browser/components/message-footer.d.ts +4 -0
  188. package/lib/browser/components/message-footer.js +8 -0
  189. package/lib/browser/components/message-list.d.ts +15 -1
  190. package/lib/browser/components/message-list.js +200 -35
  191. package/lib/browser/components/phone-shell.d.ts +7 -2
  192. package/lib/browser/components/phone-shell.js +9 -3
  193. package/lib/browser/components/relay-services.d.ts +25 -0
  194. package/lib/browser/components/relay-services.js +286 -0
  195. package/lib/browser/components/relay-view.js +17 -1
  196. package/lib/browser/components/remote-hosts.js +1 -1
  197. package/lib/browser/components/session-view.d.ts +3 -1
  198. package/lib/browser/components/session-view.js +23 -7
  199. package/lib/browser/components/settings-controls.js +3 -3
  200. package/lib/browser/components/settings-parts.js +13 -1
  201. package/lib/browser/components/stop-times.d.ts +48 -0
  202. package/lib/browser/components/stop-times.js +85 -0
  203. package/lib/browser/components/ta-avatar.js +6 -0
  204. package/lib/browser/components/tool-result.d.ts +14 -0
  205. package/lib/browser/components/tool-result.js +175 -0
  206. package/lib/browser/components/tool-view.js +10 -20
  207. package/lib/browser/components/use-stop-times.d.ts +4 -0
  208. package/lib/browser/components/use-stop-times.js +53 -0
  209. package/lib/browser/components/web-composer.js +2 -1
  210. package/lib/browser/device/photo-picker.d.ts +15 -0
  211. package/lib/browser/device/photo-picker.js +51 -0
  212. package/lib/browser/device/row-swipe.d.ts +2 -0
  213. package/lib/browser/device/row-swipe.js +8 -2
  214. package/lib/browser/device/sheet-swipe.d.ts +1 -1
  215. package/lib/browser/device/sheet-swipe.js +17 -3
  216. package/lib/browser/extension/extension-view.d.ts +3 -1
  217. package/lib/browser/extension/extension-view.js +8 -1
  218. package/lib/browser/extension/icons.js +9 -0
  219. package/lib/browser/extension/web-api-components.d.ts +21 -3
  220. package/lib/browser/extension/web-api-contributions.d.ts +128 -0
  221. package/lib/browser/extension/web-extension-api.d.ts +45 -48
  222. package/lib/browser/extension/web-extension-context.d.ts +6 -1
  223. package/lib/browser/extension/web-extension-context.js +73 -8
  224. package/lib/browser/extension/web-extension-protocol.d.ts +18 -2
  225. package/lib/browser/extension/web-extension-protocol.js +2 -0
  226. package/lib/browser/extension/web-extension-run.d.ts +3 -2
  227. package/lib/browser/extension/web-extension-run.js +3 -0
  228. package/lib/browser/extension/web-extensions.d.ts +2 -8
  229. package/lib/browser/extension/web-extensions.js +12 -35
  230. package/lib/browser/extension/web-page-list.d.ts +26 -0
  231. package/lib/browser/extension/web-page-list.js +39 -0
  232. package/lib/browser/extension/web-session-notices.d.ts +6 -1
  233. package/lib/browser/extension/web-session-notices.js +30 -4
  234. package/lib/browser/extension/web-sharing.d.ts +5 -2
  235. package/lib/browser/extension/web-sharing.js +10 -1
  236. package/lib/browser/i18n/locales.d.ts +77 -1
  237. package/lib/browser/i18n/messages.d.ts +76 -0
  238. package/lib/browser/i18n/messages.js +154 -2
  239. package/lib/browser/links/links-protocol.d.ts +2 -0
  240. package/lib/browser/links/links.d.ts +7 -2
  241. package/lib/browser/links/links.js +16 -9
  242. package/lib/browser/markdown/code-renderers.d.ts +4 -0
  243. package/lib/browser/markdown/code-renderers.js +5 -1
  244. package/lib/browser/markdown/lazy.d.ts +8 -0
  245. package/lib/browser/markdown/lazy.js +37 -1
  246. package/lib/browser/markdown/mermaid.d.ts +15 -3
  247. package/lib/browser/markdown/mermaid.js +44 -5
  248. package/lib/browser/notices/notices-protocol.d.ts +5 -0
  249. package/lib/browser/notices/notices.d.ts +19 -0
  250. package/lib/browser/notices/notices.js +84 -1
  251. package/lib/browser/notices/session-notices.d.ts +5 -2
  252. package/lib/browser/notices/session-notices.js +10 -1
  253. package/lib/browser/style/base.d.ts +1 -1
  254. package/lib/browser/style/base.js +2 -1
  255. package/lib/browser/style/conversation.d.ts +1 -1
  256. package/lib/browser/style/conversation.js +53 -0
  257. package/lib/browser/style/device.js +3 -0
  258. package/lib/browser/style/layout.d.ts +1 -1
  259. package/lib/browser/style/layout.js +27 -9
  260. package/lib/browser/style/pages.d.ts +1 -1
  261. package/lib/browser/style/pages.js +5 -0
  262. package/lib/browser/style/phone.d.ts +1 -1
  263. package/lib/browser/style/phone.js +2 -0
  264. package/lib/browser/style/shell.d.ts +1 -1
  265. package/lib/browser/style/shell.js +16 -3
  266. package/lib/browser/style/surfaces.d.ts +1 -1
  267. package/lib/browser/style/surfaces.js +19 -0
  268. package/lib/browser/util/desktop.d.ts +18 -1
  269. package/lib/browser/util/desktop.js +13 -0
  270. package/lib/browser/util/frame.d.ts +2 -0
  271. package/lib/browser/util/frame.js +2 -0
  272. package/lib/browser/util/process.d.ts +69 -2
  273. package/lib/browser/util/process.js +165 -9
  274. package/lib/browser/util/progress.d.ts +6 -1
  275. package/lib/browser/util/progress.js +11 -2
  276. package/lib/browser/util/scroll-rest.d.ts +11 -0
  277. package/lib/browser/util/scroll-rest.js +47 -0
  278. package/lib/browser/util/tool-result.d.ts +33 -0
  279. package/lib/browser/util/tool-result.js +88 -0
  280. package/lib/browser/util/tool.js +7 -0
  281. package/package.json +7 -7
@@ -1,10 +1,15 @@
1
- import * as React from 'react';
1
+ import { type FrameScheduler } from '../util/frame.js';
2
+ import { type ScrollRow } from './conversation-scroll-util.js';
2
3
  /** A message of the user in the thread: where the view goes when one is sent. */
3
4
  export declare const ASK_CLASS = "ta-thread-ask";
4
5
  /** A row of the thread under which earlier rows come in (what says they load): the row read is the one after it. */
5
6
  export declare const SCROLL_SKIP: {
6
7
  readonly 'data-scroll-skip': '';
7
8
  };
9
+ /** The thread while the mouse holds the scroll bar: what grows past its height is clipped, and no row shrinks for it. */
10
+ export declare const HELD_CLASS = "ta-scroll-held";
11
+ /** Whether motion is reduced in the settings: scrolls go at once. */
12
+ export declare function calm(): boolean;
8
13
  /** What the scrolling of a conversation reads of its view. */
9
14
  export interface ScrollView {
10
15
  scrollTop: number;
@@ -15,23 +20,16 @@ export interface ScrollView {
15
20
  top: number;
16
21
  };
17
22
  }
18
- /** A row of the thread. */
19
- export interface ScrollRow {
20
- /** From the top of the thread. */
21
- readonly offsetTop: number;
22
- readonly offsetHeight: number;
23
- readonly isConnected: boolean;
24
- /** `scrollSkip`: SCROLL_SKIP. */
25
- readonly dataset?: {
26
- readonly scrollSkip?: string;
27
- };
28
- }
29
23
  /** The thread in the view: its rows (HTML elements: ScrollRow), and the room under them for the answer to a message sent. */
30
24
  export interface ScrollThread {
31
25
  readonly children: ArrayLike<object>;
32
26
  readonly offsetHeight: number;
33
27
  readonly style: {
34
28
  paddingBottom: string;
29
+ height: string;
30
+ };
31
+ readonly classList: {
32
+ toggle(token: string, force?: boolean): boolean;
35
33
  };
36
34
  }
37
35
  /**
@@ -39,7 +37,9 @@ export interface ScrollThread {
39
37
  * holds a row for each message.
40
38
  *
41
39
  * - At the bottom, the view follows what comes in, after layout, on a change of the size of the thread or of
42
- * the view. The user scrolling up, by however little, leaves it where they took it: a wheel, a finger, a key,
40
+ * the view. What grew in sight glides into the view (GLIDE_MS); what grew above it moves the view with it at once,
41
+ * so that no row in sight moves for it; the view changing its height (the message box, the keyboard) goes at once.
42
+ * The user scrolling up, by however little, leaves it where they took it: a wheel, a finger, a key,
43
43
  * the scroll bar, whatever else moved the view up. Back down near the bottom, it follows again.
44
44
  * - Away from the bottom, what the user reads stays where it is: the row at the top of the view keeps its place
45
45
  * as rows above it change their height (earlier messages coming in, pictures, rows laid out as they come into
@@ -47,7 +47,8 @@ export interface ScrollThread {
47
47
  * keyboard of a phone), as in a chat app.
48
48
  * - A part of a message opened or closed by a click (what has `aria-expanded`, a `summary`) stays where it was
49
49
  * clicked; opened past the bottom, the view stops following, for the user to read it.
50
- * - While the user holds the view (a finger on it, the mouse on its scroll bar), nothing moves it under them.
50
+ * - While the user holds the view (a finger on it, the mouse on its scroll bar), nothing moves it under them; the
51
+ * thread keeps its height while the scroll bar is held (`HELD_CLASS`), so that the thumb does not move under the mouse.
51
52
  * - A message the user sends goes to the top of the view, and the answer comes in under it: the thread keeps
52
53
  * the room for that until the answer fills it (`send`), and the message keeps to the top as rows above it
53
54
  * change their height (the turn it was sent into folding its steps).
@@ -69,11 +70,19 @@ export declare class ConversationScroller {
69
70
  /** Where the view was when it last moved, and how high it was. */
70
71
  private top;
71
72
  private height;
73
+ /** How high the thread was when the view last moved or it changed its size: a move up as it changes is the browser's. */
74
+ private extent;
72
75
  private held;
76
+ /**
77
+ * The mouse holds the scroll bar: the thread keeps its height until let go. The browser puts the view where
78
+ * the thumb is at each move of the mouse, by the height of the thread: a reply growing under it would move
79
+ * the view back and forth, and the thumb with it.
80
+ */
81
+ private frozen;
73
82
  /** When the user last moved the view by hand (HAND_MS). */
74
83
  private handAt;
75
84
  private touchY;
76
- /** The row at the top of the view while it does not follow, and how far it starts below the top of the view. */
85
+ /** What the user reads while the view does not follow (ScrollAnchor). */
77
86
  private anchor;
78
87
  /** What the user just opened or closed, and how far it was below the top of the view. */
79
88
  private toggled;
@@ -86,7 +95,11 @@ export declare class ConversationScroller {
86
95
  /** The `scrollend` of a browser without one (SCROLL_END_MS). */
87
96
  private endTimer;
88
97
  private readonly nativeEnd;
89
- constructor(view: ScrollView, thread: ScrollThread, now?: () => number);
98
+ /** The view on its way to the bottom (`attach`, or given frames); without it the view goes there at once. */
99
+ private glider;
100
+ /** The row at the top of the view when it last followed, and how far it started below the top of the view. */
101
+ private seen;
102
+ constructor(view: ScrollView, thread: ScrollThread, now?: () => number, frames?: FrameScheduler);
90
103
  /** Whether the view follows what comes in. */
91
104
  get atLatest(): boolean;
92
105
  /** Listens to `view` (the view of this scroller) and its thread; returns what stops it. */
@@ -106,40 +119,51 @@ export declare class ConversationScroller {
106
119
  key(key: string, shift: boolean): void;
107
120
  touchStart(y: number | undefined): void;
108
121
  touchMove(y: number | undefined, target: EventTarget | null): void;
109
- hold(): void;
122
+ /** `bar`: the mouse on the scroll bar, rather than a finger on the view. */
123
+ hold(bar?: boolean): void;
110
124
  /** Let go: what came in meanwhile shows, if the view follows. */
111
125
  release(): void;
112
126
  clicked(target: EventTarget | null): void;
113
127
  /** The thread or the view changed its size. */
114
128
  resized(): void;
129
+ /**
130
+ * The row of the message sent, while it is there. Rows are drawn by the place of their message: one coming in
131
+ * before it (what an extension adds to the turn) gives its row to that one, which may show nothing, and the
132
+ * message to the next row; the view would else go to the top of the thread.
133
+ */
134
+ private sentRow;
115
135
  /** Where the view goes for the message sent to be at its top, with the room under the thread for that. */
116
136
  private place;
137
+ private freeze;
117
138
  private makeRoom;
118
139
  private arrive;
119
140
  /** The user moves the view: a smooth scroll on its way is theirs now, the message sent no longer holds the top. */
120
141
  private byUser;
121
142
  /** The user moves the view up: it stops following before the scroll comes, which a follow would undo. */
122
143
  private leave;
144
+ /**
145
+ * To the bottom, following what grew: at once when `now` (the view changed its height) or the view cannot glide;
146
+ * else the row at the top of the view keeps its place (what grew above it is out of sight), and the rest glides.
147
+ */
148
+ private follow;
149
+ private glide;
150
+ /** The row at the top of the view now (`seen`). */
151
+ private see;
123
152
  private toBottomNow;
124
153
  private moveTo;
125
- /** The first row in the view, while it does not follow. */
154
+ /** What the user reads, while the view does not follow (ScrollAnchor). */
126
155
  private remember;
156
+ /** Where the anchor is now, from the top of the thread; undefined once it is gone. */
157
+ private anchorAt;
158
+ /**
159
+ * What moved above the anchor before the view heard of it, kept out of sight. The scroll of a frame comes before
160
+ * the observer of sizes: rows drawn in front of the view meanwhile (earlier messages, a diagram drawn) would
161
+ * else be taken as where the user is, and what they read would go down by all of them.
162
+ */
163
+ private keepAnchor;
164
+ /** The first part of `row` (a row begun above the view) that starts in the view, as deep as it takes to find one. */
165
+ private partIn;
127
166
  private offsetOf;
128
167
  /** Whether what `element` opened goes on past the bottom of the view. */
129
168
  private past;
130
169
  }
131
- export interface ConversationScrollOptions {
132
- /** Where a message sent goes: to the top of the view with its answer under it (a conversation), or to the bottom (a group chat). */
133
- sentTo?: 'top' | 'bottom';
134
- /** Another key (another chat in the same view) goes to the bottom. */
135
- key?: string;
136
- }
137
- /**
138
- * The scrolling of a conversation (ConversationScroller), whose thread is the first child of the element of the
139
- * returned ref. Opened, and when messages of the user go (a rewind), the view goes to the bottom.
140
- *
141
- * `asked`: how many messages of the user there are; the thread marks them with ASK_CLASS.
142
- */
143
- export declare function useConversationScroll(asked: number, options?: ConversationScrollOptions): React.RefObject<HTMLDivElement | null>;
144
- /** Whether the conversation of `ref` is scrolled up from its latest, and a way back to it. */
145
- export declare function useAwayFromBottom(ref: React.RefObject<HTMLDivElement | null>): [boolean, () => void];
@@ -1,5 +1,5 @@
1
- import * as React from 'react';
2
1
  import { FrameUtil } from '../util/frame.js';
2
+ import { ScrollGlide, ScrollUtil } from './conversation-scroll-util.js';
3
3
  /** A message of the user in the thread: where the view goes when one is sent. */
4
4
  export const ASK_CLASS = 'ta-thread-ask';
5
5
  /** How long a part of a message opened or closed by a click keeps its place (a motion of its height). */
@@ -24,12 +24,19 @@ const TOGGLE = '[aria-expanded], summary';
24
24
  * follow its answer.
25
25
  */
26
26
  const SCROLL_END_MS = 150;
27
+ /** How deep in a row taller than the view its part read is looked for. */
28
+ const PART_DEPTH = 8;
29
+ /** At most this share of the view is left to glide: more coming at once (a block, what came while away) jumps the rest. */
30
+ const GLIDE_MAX = 0.5;
27
31
  function hasScrollEnd() {
28
32
  return typeof window !== 'undefined' && 'onscrollend' in window;
29
33
  }
30
34
  /** A row of the thread under which earlier rows come in (what says they load): the row read is the one after it. */
31
35
  export const SCROLL_SKIP = { 'data-scroll-skip': '' };
32
- function calm() {
36
+ /** The thread while the mouse holds the scroll bar: what grows past its height is clipped, and no row shrinks for it. */
37
+ export const HELD_CLASS = 'ta-scroll-held';
38
+ /** Whether motion is reduced in the settings: scrolls go at once. */
39
+ export function calm() {
33
40
  return typeof document !== 'undefined' && document.documentElement.hasAttribute('data-reduce-motion');
34
41
  }
35
42
  function isElement(value) {
@@ -52,7 +59,9 @@ function scrollsInside(target, view, up) {
52
59
  * holds a row for each message.
53
60
  *
54
61
  * - At the bottom, the view follows what comes in, after layout, on a change of the size of the thread or of
55
- * the view. The user scrolling up, by however little, leaves it where they took it: a wheel, a finger, a key,
62
+ * the view. What grew in sight glides into the view (GLIDE_MS); what grew above it moves the view with it at once,
63
+ * so that no row in sight moves for it; the view changing its height (the message box, the keyboard) goes at once.
64
+ * The user scrolling up, by however little, leaves it where they took it: a wheel, a finger, a key,
56
65
  * the scroll bar, whatever else moved the view up. Back down near the bottom, it follows again.
57
66
  * - Away from the bottom, what the user reads stays where it is: the row at the top of the view keeps its place
58
67
  * as rows above it change their height (earlier messages coming in, pictures, rows laid out as they come into
@@ -60,7 +69,8 @@ function scrollsInside(target, view, up) {
60
69
  * keyboard of a phone), as in a chat app.
61
70
  * - A part of a message opened or closed by a click (what has `aria-expanded`, a `summary`) stays where it was
62
71
  * clicked; opened past the bottom, the view stops following, for the user to read it.
63
- * - While the user holds the view (a finger on it, the mouse on its scroll bar), nothing moves it under them.
72
+ * - While the user holds the view (a finger on it, the mouse on its scroll bar), nothing moves it under them; the
73
+ * thread keeps its height while the scroll bar is held (`HELD_CLASS`), so that the thumb does not move under the mouse.
64
74
  * - A message the user sends goes to the top of the view, and the answer comes in under it: the thread keeps
65
75
  * the room for that until the answer fills it (`send`), and the message keeps to the top as rows above it
66
76
  * change their height (the turn it was sent into folding its steps).
@@ -70,7 +80,7 @@ export class ConversationScroller {
70
80
  static of(view) {
71
81
  return ConversationScroller.views.get(view);
72
82
  }
73
- constructor(view, thread, now = () => performance.now()) {
83
+ constructor(view, thread, now = () => performance.now(), frames) {
74
84
  this.view = view;
75
85
  this.thread = thread;
76
86
  this.now = now;
@@ -78,6 +88,12 @@ export class ConversationScroller {
78
88
  /** On a smooth way to the bottom (`toBottom`): what comes in meanwhile moves the end of the way. */
79
89
  this.chasing = false;
80
90
  this.held = false;
91
+ /**
92
+ * The mouse holds the scroll bar: the thread keeps its height until let go. The browser puts the view where
93
+ * the thumb is at each move of the mouse, by the height of the thread: a reply growing under it would move
94
+ * the view back and forth, and the thumb with it.
95
+ */
96
+ this.frozen = false;
81
97
  /** When the user last moved the view by hand (HAND_MS). */
82
98
  this.handAt = Number.NEGATIVE_INFINITY;
83
99
  /**
@@ -87,8 +103,10 @@ export class ConversationScroller {
87
103
  this.room = 0;
88
104
  this.padding = 0;
89
105
  this.nativeEnd = hasScrollEnd();
106
+ this.glider = frames && new ScrollGlide(frames, now);
90
107
  this.top = view.scrollTop;
91
108
  this.height = view.clientHeight;
109
+ this.extent = view.scrollHeight;
92
110
  }
93
111
  /** Whether the view follows what comes in. */
94
112
  get atLatest() {
@@ -96,7 +114,9 @@ export class ConversationScroller {
96
114
  }
97
115
  /** Listens to `view` (the view of this scroller) and its thread; returns what stops it. */
98
116
  attach(view, thread) {
117
+ var _a;
99
118
  ConversationScroller.views.set(view, this);
119
+ (_a = this.glider) !== null && _a !== void 0 ? _a : (this.glider = new ScrollGlide(FrameUtil.animation, this.now));
100
120
  thread.style.paddingBottom = '';
101
121
  this.padding = Number.parseFloat(getComputedStyle(thread).paddingBottom) || 0;
102
122
  const onScroll = () => {
@@ -123,10 +143,16 @@ export class ConversationScroller {
123
143
  // The mouse on the scroll bar (or beside the thread): the view itself is the target.
124
144
  const onPointerDown = (event) => {
125
145
  if (event.pointerType === 'mouse' && event.button === 0 && event.target === view) {
126
- this.hold();
146
+ this.hold(true);
127
147
  }
128
148
  };
129
149
  const onPointerUp = () => this.release();
150
+ // Let go outside the window, where no pointerup may come: the mouse moves with no button held.
151
+ const onPointerMove = (event) => {
152
+ if (this.frozen && event.buttons === 0) {
153
+ this.release();
154
+ }
155
+ };
130
156
  // Captured: before the click opens or closes what it is on (React handles it after, on the root).
131
157
  const onClick = (event) => this.clicked(event.target);
132
158
  const passive = { passive: true };
@@ -142,11 +168,15 @@ export class ConversationScroller {
142
168
  view.addEventListener('click', onClick, true);
143
169
  window.addEventListener('pointerup', onPointerUp, passive);
144
170
  window.addEventListener('pointercancel', onPointerUp, passive);
171
+ window.addEventListener('pointermove', onPointerMove, passive);
145
172
  const observer = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(() => this.resized()) : undefined;
146
173
  observer === null || observer === void 0 ? void 0 : observer.observe(thread);
147
174
  // The view itself changes its height under what is around it (the message box, the keyboard of a phone).
148
175
  observer === null || observer === void 0 ? void 0 : observer.observe(view);
149
176
  return () => {
177
+ var _a;
178
+ this.freeze(false);
179
+ (_a = this.glider) === null || _a === void 0 ? void 0 : _a.stop();
150
180
  ConversationScroller.views.delete(view);
151
181
  view.removeEventListener('scroll', onScroll);
152
182
  view.removeEventListener('scrollend', onScrollEnd);
@@ -161,11 +191,13 @@ export class ConversationScroller {
161
191
  view.removeEventListener('click', onClick, true);
162
192
  window.removeEventListener('pointerup', onPointerUp);
163
193
  window.removeEventListener('pointercancel', onPointerUp);
194
+ window.removeEventListener('pointermove', onPointerMove);
164
195
  observer === null || observer === void 0 ? void 0 : observer.disconnect();
165
196
  };
166
197
  }
167
198
  /** To the bottom at once, following from there: a conversation opened, loaded again or rewound. */
168
199
  reset() {
200
+ this.freeze(false);
169
201
  this.makeRoom(0);
170
202
  this.sent = undefined;
171
203
  this.heading = undefined;
@@ -207,6 +239,7 @@ export class ConversationScroller {
207
239
  }
208
240
  scrolled() {
209
241
  const { view } = this;
242
+ this.keepAnchor();
210
243
  const top = view.scrollTop;
211
244
  const moved = top - this.top;
212
245
  this.top = top;
@@ -218,10 +251,13 @@ export class ConversationScroller {
218
251
  return;
219
252
  }
220
253
  const distance = view.scrollHeight - top - view.clientHeight;
254
+ // More than a screen up with the thread as it was: a search in the page. As it changed, the browser's: in
255
+ // WebKit the view put back to the top while the steps of a turn fold into its reply, the event after.
256
+ const searched = -moved > view.clientHeight && view.scrollHeight === this.extent;
221
257
  if (distance < 1) {
222
258
  this.following = true;
223
259
  }
224
- else if (moved <= -1 && (this.held || this.now() - this.handAt < HAND_MS || -moved > view.clientHeight)) {
260
+ else if (moved <= -1 && (this.held || this.now() - this.handAt < HAND_MS || searched)) {
225
261
  // Up, by the user (the scroll bar dragged, a fling), or by more than a screen for them (a search in the
226
262
  // page, which goes to what is out of sight): the view stays where it was taken.
227
263
  this.following = false;
@@ -283,8 +319,12 @@ export class ConversationScroller {
283
319
  }
284
320
  this.touchY = y;
285
321
  }
286
- hold() {
322
+ /** `bar`: the mouse on the scroll bar, rather than a finger on the view. */
323
+ hold(bar = false) {
287
324
  this.held = true;
325
+ if (bar) {
326
+ this.freeze(true);
327
+ }
288
328
  this.byUser();
289
329
  }
290
330
  /** Let go: what came in meanwhile shows, if the view follows. */
@@ -293,6 +333,7 @@ export class ConversationScroller {
293
333
  return;
294
334
  }
295
335
  this.held = false;
336
+ this.freeze(false);
296
337
  this.touchY = undefined;
297
338
  if (this.following && this.heading === undefined && !this.chasing) {
298
339
  this.toBottomNow();
@@ -307,11 +348,12 @@ export class ConversationScroller {
307
348
  /** The thread or the view changed its size. */
308
349
  resized() {
309
350
  const { view } = this;
351
+ this.extent = view.scrollHeight;
310
352
  const height = view.clientHeight;
311
353
  const shrunk = this.height - height;
312
354
  this.height = height;
313
- const sent = this.sent;
314
- if (sent === null || sent === void 0 ? void 0 : sent.isConnected) {
355
+ const sent = this.sentRow();
356
+ if (sent) {
315
357
  const top = this.place(sent);
316
358
  if (this.heading !== undefined && Math.abs(top - this.heading) >= 1) {
317
359
  this.heading = top;
@@ -336,16 +378,36 @@ export class ConversationScroller {
336
378
  view.scrollTo({ top: view.scrollHeight - view.clientHeight, behavior: 'smooth' });
337
379
  }
338
380
  else if (!this.held) {
339
- this.toBottomNow();
381
+ this.follow(shrunk !== 0);
340
382
  }
341
383
  return;
342
384
  }
343
385
  // The row read where it was, the bottom of the view kept to the message box. From the row rather than the
344
386
  // view: the browser may have pulled the view up meanwhile, the thread shorter than where it was.
345
- const anchor = this.anchor;
346
- this.moveTo(((anchor === null || anchor === void 0 ? void 0 : anchor.row.isConnected) ? anchor.row.offsetTop - anchor.offset : this.top) + shrunk);
387
+ const at = this.anchor && this.anchorAt(this.anchor);
388
+ this.moveTo((at === undefined ? this.top : at - this.anchor.offset) + shrunk);
347
389
  this.remember();
348
390
  }
391
+ /**
392
+ * The row of the message sent, while it is there. Rows are drawn by the place of their message: one coming in
393
+ * before it (what an extension adds to the turn) gives its row to that one, which may show nothing, and the
394
+ * message to the next row; the view would else go to the top of the thread.
395
+ */
396
+ sentRow() {
397
+ var _a;
398
+ const sent = this.sent;
399
+ if (!(sent === null || sent === void 0 ? void 0 : sent.isConnected) || sent.classList.contains(ASK_CLASS)) {
400
+ return (sent === null || sent === void 0 ? void 0 : sent.isConnected) ? sent : undefined;
401
+ }
402
+ const rows = this.thread.children;
403
+ for (let index = rows.length - 1; index >= 0; index--) {
404
+ if ((_a = rows[index].classList) === null || _a === void 0 ? void 0 : _a.contains(ASK_CLASS)) {
405
+ this.sent = rows[index];
406
+ return rows[index];
407
+ }
408
+ }
409
+ return undefined;
410
+ }
349
411
  /** Where the view goes for the message sent to be at its top, with the room under the thread for that. */
350
412
  place(message) {
351
413
  // offsetTop: from the thread; the transform of the message coming in does not count.
@@ -353,6 +415,14 @@ export class ConversationScroller {
353
415
  this.makeRoom(room);
354
416
  return top;
355
417
  }
418
+ freeze(on) {
419
+ if (on === this.frozen) {
420
+ return;
421
+ }
422
+ this.frozen = on;
423
+ this.thread.style.height = on ? `${this.thread.offsetHeight}px` : '';
424
+ this.thread.classList.toggle(HELD_CLASS, on);
425
+ }
356
426
  makeRoom(room) {
357
427
  if (room !== this.room) {
358
428
  this.room = room;
@@ -363,6 +433,7 @@ export class ConversationScroller {
363
433
  this.heading = undefined;
364
434
  this.following = true;
365
435
  this.top = this.view.scrollTop;
436
+ this.see();
366
437
  }
367
438
  /** The user moves the view: a smooth scroll on its way is theirs now, the message sent no longer holds the top. */
368
439
  byUser() {
@@ -382,7 +453,49 @@ export class ConversationScroller {
382
453
  this.remember();
383
454
  }
384
455
  }
456
+ /**
457
+ * To the bottom, following what grew: at once when `now` (the view changed its height) or the view cannot glide;
458
+ * else the row at the top of the view keeps its place (what grew above it is out of sight), and the rest glides.
459
+ */
460
+ follow(now) {
461
+ const { view } = this;
462
+ const seen = this.seen;
463
+ if (now || !this.glider || calm() || !(seen === null || seen === void 0 ? void 0 : seen.row.isConnected)) {
464
+ this.toBottomNow();
465
+ return;
466
+ }
467
+ const bottom = view.scrollHeight - view.clientHeight;
468
+ // Never up: a move up of more than a screen would count as the user's (a search in the page).
469
+ const top = Math.max(seen.row.offsetTop - seen.offset, view.scrollTop, bottom - view.clientHeight * GLIDE_MAX);
470
+ this.moveTo(top);
471
+ this.see();
472
+ if (bottom - view.scrollTop >= 1) {
473
+ this.glide();
474
+ }
475
+ }
476
+ glide() {
477
+ var _a;
478
+ (_a = this.glider) === null || _a === void 0 ? void 0 : _a.start(share => {
479
+ const { view } = this;
480
+ const behind = view.scrollHeight - view.clientHeight - view.scrollTop;
481
+ if (!this.following || this.held || this.chasing || this.heading !== undefined || behind < 1) {
482
+ return false;
483
+ }
484
+ // The last pixel of the way at once: the view lands on the bottom, not short of it.
485
+ const move = Math.max(1, behind * share);
486
+ this.moveTo(behind - move < 1 ? view.scrollHeight - view.clientHeight : view.scrollTop + move);
487
+ this.see();
488
+ return true;
489
+ });
490
+ }
491
+ /** The row at the top of the view now (`seen`). */
492
+ see() {
493
+ const row = ScrollUtil.firstRow(this.thread.children, this.view.scrollTop);
494
+ this.seen = row ? { row, offset: row.offsetTop - this.view.scrollTop } : undefined;
495
+ }
385
496
  toBottomNow() {
497
+ var _a;
498
+ (_a = this.glider) === null || _a === void 0 ? void 0 : _a.stop();
386
499
  const { view } = this;
387
500
  const bottom = view.scrollHeight - view.clientHeight;
388
501
  if (bottom - view.scrollTop >= 1) {
@@ -390,6 +503,7 @@ export class ConversationScroller {
390
503
  }
391
504
  this.top = view.scrollTop;
392
505
  this.anchor = undefined;
506
+ this.see();
393
507
  }
394
508
  moveTo(top) {
395
509
  const { view } = this;
@@ -399,29 +513,75 @@ export class ConversationScroller {
399
513
  }
400
514
  this.top = view.scrollTop;
401
515
  }
402
- /** The first row in the view, while it does not follow. */
516
+ /** What the user reads, while the view does not follow (ScrollAnchor). */
403
517
  remember() {
404
- var _a, _b;
405
518
  if (this.following) {
406
519
  this.anchor = undefined;
520
+ this.see();
407
521
  return;
408
522
  }
409
- const rows = this.thread.children;
410
- const top = this.view.scrollTop;
411
- let low = 0;
412
- let high = rows.length - 1;
413
- while (low < high) {
414
- const middle = (low + high) >> 1;
415
- const row = rows[middle];
416
- if (row.offsetTop + row.offsetHeight > top) {
417
- high = middle;
523
+ const { view } = this;
524
+ const top = view.scrollTop;
525
+ const row = ScrollUtil.readRow(this.thread.children, top, view.clientHeight);
526
+ if (!row) {
527
+ this.anchor = undefined;
528
+ return;
529
+ }
530
+ // A row taller than the view, begun above it: what of it starts in the view keeps its place, so that what
531
+ // grows in the part of the row above it (a diagram drawn, a part laid out) does not move what is read.
532
+ const part = row.offsetTop < top && isElement(row) ? this.partIn(row) : undefined;
533
+ const at = part ? top + this.offsetOf(part) : row.offsetTop;
534
+ this.anchor = { row, part, offset: at - top, at };
535
+ }
536
+ /** Where the anchor is now, from the top of the thread; undefined once it is gone. */
537
+ anchorAt(anchor) {
538
+ var _a;
539
+ if (((_a = anchor.part) === null || _a === void 0 ? void 0 : _a.isConnected) && anchor.row.isConnected) {
540
+ return this.view.scrollTop + this.offsetOf(anchor.part);
541
+ }
542
+ return anchor.row.isConnected ? anchor.row.offsetTop : undefined;
543
+ }
544
+ /**
545
+ * What moved above the anchor before the view heard of it, kept out of sight. The scroll of a frame comes before
546
+ * the observer of sizes: rows drawn in front of the view meanwhile (earlier messages, a diagram drawn) would
547
+ * else be taken as where the user is, and what they read would go down by all of them.
548
+ */
549
+ keepAnchor() {
550
+ const anchor = this.anchor;
551
+ if (this.following || !anchor || this.toggled || this.heading !== undefined) {
552
+ return;
553
+ }
554
+ const at = this.anchorAt(anchor);
555
+ const drift = at === undefined ? 0 : at - anchor.at;
556
+ if (Math.abs(drift) >= 1) {
557
+ this.view.scrollTop += drift;
558
+ this.top += drift;
559
+ }
560
+ }
561
+ /** The first part of `row` (a row begun above the view) that starts in the view, as deep as it takes to find one. */
562
+ partIn(row) {
563
+ const top = this.view.getBoundingClientRect().top;
564
+ const bottom = top + this.view.clientHeight;
565
+ let node = row;
566
+ for (let depth = 0; depth < PART_DEPTH; depth++) {
567
+ let inside;
568
+ for (const child of node.children) {
569
+ const box = child.getBoundingClientRect();
570
+ if (box.bottom <= top || box.height === 0) {
571
+ continue;
572
+ }
573
+ if (box.top >= top) {
574
+ return box.top < bottom ? child : undefined;
575
+ }
576
+ inside = child;
577
+ break;
418
578
  }
419
- else {
420
- low = middle + 1;
579
+ if (!inside) {
580
+ return undefined;
421
581
  }
582
+ node = inside;
422
583
  }
423
- const row = ((_b = (_a = rows[low]) === null || _a === void 0 ? void 0 : _a.dataset) === null || _b === void 0 ? void 0 : _b.scrollSkip) !== undefined && low + 1 < rows.length ? rows[low + 1] : rows[low];
424
- this.anchor = row ? { row, offset: row.offsetTop - top } : undefined;
584
+ return undefined;
425
585
  }
426
586
  offsetOf(element) {
427
587
  return element.getBoundingClientRect().top - this.view.getBoundingClientRect().top;
@@ -434,99 +594,4 @@ export class ConversationScroller {
434
594
  }
435
595
  }
436
596
  ConversationScroller.views = new WeakMap();
437
- /**
438
- * The scrolling of a conversation (ConversationScroller), whose thread is the first child of the element of the
439
- * returned ref. Opened, and when messages of the user go (a rewind), the view goes to the bottom.
440
- *
441
- * `asked`: how many messages of the user there are; the thread marks them with ASK_CLASS.
442
- */
443
- export function useConversationScroll(asked, options = {}) {
444
- const { sentTo = 'top', key } = options;
445
- const ref = React.useRef(null);
446
- const attached = React.useRef(undefined);
447
- const before = React.useRef(undefined);
448
- // After each render, the view may only now be there (a page that shows it once its data came).
449
- React.useLayoutEffect(() => {
450
- var _a, _b;
451
- const view = ref.current;
452
- const thread = view === null || view === void 0 ? void 0 : view.firstElementChild;
453
- if (((_a = attached.current) === null || _a === void 0 ? void 0 : _a.view) === view) {
454
- return;
455
- }
456
- (_b = attached.current) === null || _b === void 0 ? void 0 : _b.detach();
457
- attached.current = undefined;
458
- if (view && thread instanceof HTMLElement) {
459
- const scroller = new ConversationScroller(view, thread);
460
- attached.current = { view, scroller, detach: scroller.attach(view, thread) };
461
- }
462
- });
463
- React.useLayoutEffect(() => () => {
464
- var _a;
465
- (_a = attached.current) === null || _a === void 0 ? void 0 : _a.detach();
466
- attached.current = undefined;
467
- }, []);
468
- React.useLayoutEffect(() => {
469
- var _a, _b;
470
- const previous = before.current;
471
- before.current = { asked, key };
472
- const current = (_a = attached.current) === null || _a === void 0 ? void 0 : _a.scroller;
473
- const thread = (_b = ref.current) === null || _b === void 0 ? void 0 : _b.firstElementChild;
474
- if (!current || !thread) {
475
- return;
476
- }
477
- // One more: sent now. More at once is the conversation loaded again, which opens at its bottom.
478
- const sent = previous !== undefined && previous.key === key && asked === previous.asked + 1;
479
- const asks = sent && sentTo === 'top' ? thread.getElementsByClassName(ASK_CLASS) : undefined;
480
- const message = asks === null || asks === void 0 ? void 0 : asks[asks.length - 1];
481
- if (message) {
482
- current.send(message);
483
- }
484
- else {
485
- current.reset();
486
- }
487
- }, [asked, key, sentTo]);
488
- return ref;
489
- }
490
- /** Away from the bottom by more than half of the view: the button back to the latest shows. */
491
- const AWAY_SHARE = 0.5;
492
- /** Lower than this (a phone held sideways, its keyboard open) the view has no room for the button. */
493
- const AWAY_MIN_PX = 240;
494
- /** Whether the conversation of `ref` is scrolled up from its latest, and a way back to it. */
495
- export function useAwayFromBottom(ref) {
496
- const [away, setAway] = React.useState(false);
497
- React.useEffect(() => {
498
- const element = ref.current;
499
- if (!element) {
500
- return;
501
- }
502
- let frame = 0;
503
- const measure = () => {
504
- cancelAnimationFrame(frame);
505
- frame = requestAnimationFrame(() => {
506
- const { scrollHeight, scrollTop, clientHeight } = element;
507
- setAway(clientHeight >= AWAY_MIN_PX && scrollHeight - scrollTop - clientHeight > clientHeight * AWAY_SHARE);
508
- });
509
- };
510
- element.addEventListener('scroll', measure, { passive: true });
511
- // The keyboard and a turn of the phone change the height of the view, not where it is.
512
- const observer = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(measure) : undefined;
513
- observer === null || observer === void 0 ? void 0 : observer.observe(element);
514
- return () => {
515
- cancelAnimationFrame(frame);
516
- observer === null || observer === void 0 ? void 0 : observer.disconnect();
517
- element.removeEventListener('scroll', measure);
518
- };
519
- }, [ref]);
520
- const back = React.useCallback(() => {
521
- const element = ref.current;
522
- const scroller = element ? ConversationScroller.of(element) : undefined;
523
- if (scroller) {
524
- scroller.toBottom();
525
- }
526
- else {
527
- element === null || element === void 0 ? void 0 : element.scrollTo({ top: element.scrollHeight, behavior: calm() ? 'auto' : 'smooth' });
528
- }
529
- }, [ref]);
530
- return [away, back];
531
- }
532
597
  //# sourceMappingURL=conversation-scroll.js.map