@copilotkit/core 2.0.0-rc.4 → 2.0.0-rc.41
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.
- package/dist/a2ui-markdown-BxZr1mEU.mjs +1360 -0
- package/dist/a2ui-markdown-BxZr1mEU.mjs.map +1 -0
- package/dist/a2ui-markdown-DkMXFQih.cjs +1419 -0
- package/dist/a2ui-markdown-DkMXFQih.cjs.map +1 -0
- package/dist/chat-configuration.cjs +269 -0
- package/dist/chat-configuration.cjs.map +1 -0
- package/dist/chat-configuration.d.cts +123 -0
- package/dist/chat-configuration.d.cts.map +1 -0
- package/dist/chat-configuration.d.mts +123 -0
- package/dist/chat-configuration.d.mts.map +1 -0
- package/dist/chat-configuration.mjs +268 -0
- package/dist/chat-configuration.mjs.map +1 -0
- package/dist/generative-ui/index.cjs +3474 -0
- package/dist/generative-ui/index.cjs.map +1 -0
- package/dist/generative-ui/index.d.cts +1041 -0
- package/dist/generative-ui/index.d.cts.map +1 -0
- package/dist/generative-ui/index.d.mts +1041 -0
- package/dist/generative-ui/index.d.mts.map +1 -0
- package/dist/generative-ui/index.mjs +3425 -0
- package/dist/generative-ui/index.mjs.map +1 -0
- package/dist/index.cjs +10892 -2231
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3908
- package/dist/index.d.mts +3 -3908
- package/dist/index.mjs +10613 -2222
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +14459 -2201
- package/dist/index.umd.js.map +1 -1
- package/dist/learning-bridge-Cqc7VJuV.d.cts +10919 -0
- package/dist/learning-bridge-Cqc7VJuV.d.cts.map +1 -0
- package/dist/learning-bridge-y2bnVsjp.d.mts +10919 -0
- package/dist/learning-bridge-y2bnVsjp.d.mts.map +1 -0
- package/dist/styles.cjs +1 -1
- package/dist/styles.cjs.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/styles.mjs +1 -1
- package/dist/styles.mjs.map +1 -1
- package/dist/viewport-CksL5y8p.mjs +954 -0
- package/dist/viewport-CksL5y8p.mjs.map +1 -0
- package/dist/viewport-D-tuKCeb.cjs +1182 -0
- package/dist/viewport-D-tuKCeb.cjs.map +1 -0
- package/package.json +36 -8
- package/stryker.config.mjs +6 -0
- package/tsdown.config.ts +16 -2
- package/vitest.mutation.config.mjs +6 -0
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
|
@@ -0,0 +1,1182 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/chat/dom.ts
|
|
3
|
+
/**
|
|
4
|
+
* Turn a width or height prop into a CSS value.
|
|
5
|
+
*
|
|
6
|
+
* @param value - A number of pixels, or a CSS length such as `"30rem"`.
|
|
7
|
+
* @param fallback - The pixels to use when `value` is missing, not finite,
|
|
8
|
+
* or an empty string.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* ɵdimensionToCss(420, 480); // "420px"
|
|
13
|
+
* ɵdimensionToCss(" 30rem ", 480); // "30rem"
|
|
14
|
+
* ɵdimensionToCss(undefined, 480); // "480px"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function ɵdimensionToCss(value, fallback) {
|
|
18
|
+
if (typeof value === "number" && Number.isFinite(value)) return `${value}px`;
|
|
19
|
+
if (typeof value === "string" && value.trim().length > 0) return value.trim();
|
|
20
|
+
return `${fallback}px`;
|
|
21
|
+
}
|
|
22
|
+
const ONLY_ONE_DOCKED_SIDEBAR = "[CopilotKit] Only one docked CopilotSidebar may be open per document.";
|
|
23
|
+
/**
|
|
24
|
+
* Gives one docked sidebar at a time the page's inline margin on its side,
|
|
25
|
+
* and puts back the margins the page had before when it lets go.
|
|
26
|
+
*
|
|
27
|
+
* Get the registry of a document with {@link ɵdockedSidebarRegistryFor}.
|
|
28
|
+
*/
|
|
29
|
+
var ɵDockedSidebarRegistry = class {
|
|
30
|
+
owner;
|
|
31
|
+
original = {
|
|
32
|
+
start: "",
|
|
33
|
+
end: "",
|
|
34
|
+
transition: ""
|
|
35
|
+
};
|
|
36
|
+
restoreTimer;
|
|
37
|
+
warned = /* @__PURE__ */ new Set();
|
|
38
|
+
constructor(document) {
|
|
39
|
+
this.document = document;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Take the page margins for `owner`.
|
|
43
|
+
*
|
|
44
|
+
* @returns `true` when `owner` now has them. `false`, with a warning, when
|
|
45
|
+
* another sidebar has them.
|
|
46
|
+
*/
|
|
47
|
+
acquire(owner) {
|
|
48
|
+
if (this.owner === owner) return true;
|
|
49
|
+
if (this.owner !== void 0) {
|
|
50
|
+
if (!this.warned.has(owner)) {
|
|
51
|
+
this.warned.add(owner);
|
|
52
|
+
console.warn(ONLY_ONE_DOCKED_SIDEBAR);
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const style = this.document.body.style;
|
|
57
|
+
const transition = this.restoreTimer === void 0 ? style.transition : this.original.transition;
|
|
58
|
+
this.original = {
|
|
59
|
+
start: style.marginInlineStart,
|
|
60
|
+
end: style.marginInlineEnd,
|
|
61
|
+
transition
|
|
62
|
+
};
|
|
63
|
+
this.cancelRestore();
|
|
64
|
+
this.owner = owner;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
/** Set the margin on the sidebar's side. Does nothing unless `owner` has it. */
|
|
68
|
+
update(owner, placement, options = {}) {
|
|
69
|
+
if (this.owner !== owner) return;
|
|
70
|
+
const style = this.document.body.style;
|
|
71
|
+
this.applyTransition(options.transitionMs);
|
|
72
|
+
style.marginInlineStart = placement.position === "left" ? placement.width : this.original.start;
|
|
73
|
+
style.marginInlineEnd = placement.position === "right" ? placement.width : this.original.end;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Put back the page's own margins and let go. Does nothing unless `owner`
|
|
77
|
+
* has the margins.
|
|
78
|
+
*/
|
|
79
|
+
release(owner, options = {}) {
|
|
80
|
+
this.warned.delete(owner);
|
|
81
|
+
if (this.owner !== owner) return;
|
|
82
|
+
const style = this.document.body.style;
|
|
83
|
+
this.applyTransition(options.transitionMs);
|
|
84
|
+
style.marginInlineStart = this.original.start;
|
|
85
|
+
style.marginInlineEnd = this.original.end;
|
|
86
|
+
this.owner = void 0;
|
|
87
|
+
if (options.transitionMs === void 0) return;
|
|
88
|
+
this.restoreTimer = setTimeout(() => {
|
|
89
|
+
this.restoreTimer = void 0;
|
|
90
|
+
if (this.owner === void 0) style.transition = this.original.transition;
|
|
91
|
+
}, options.transitionMs);
|
|
92
|
+
}
|
|
93
|
+
applyTransition(transitionMs) {
|
|
94
|
+
const style = this.document.body.style;
|
|
95
|
+
this.cancelRestore();
|
|
96
|
+
style.transition = transitionMs === void 0 ? this.original.transition : `margin-inline-start ${transitionMs}ms ease, margin-inline-end ${transitionMs}ms ease`;
|
|
97
|
+
}
|
|
98
|
+
cancelRestore() {
|
|
99
|
+
if (this.restoreTimer === void 0) return;
|
|
100
|
+
clearTimeout(this.restoreTimer);
|
|
101
|
+
this.restoreTimer = void 0;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const registries = /* @__PURE__ */ new WeakMap();
|
|
105
|
+
/** The one {@link ɵDockedSidebarRegistry} of `document`. */
|
|
106
|
+
function ɵdockedSidebarRegistryFor(document) {
|
|
107
|
+
let registry = registries.get(document);
|
|
108
|
+
if (!registry) {
|
|
109
|
+
registry = new ɵDockedSidebarRegistry(document);
|
|
110
|
+
registries.set(document, registry);
|
|
111
|
+
}
|
|
112
|
+
return registry;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Makes room on the page for an open sidebar, on desktop viewports only
|
|
116
|
+
* (wider than 767px). It follows viewport changes, animates every change
|
|
117
|
+
* after the first, and gives the page its margins back on `dispose()`.
|
|
118
|
+
*
|
|
119
|
+
* Create it with {@link ɵcreateDockedSidebar}.
|
|
120
|
+
*/
|
|
121
|
+
var ɵDockedSidebar = class {
|
|
122
|
+
owner = Symbol("copilot-sidebar");
|
|
123
|
+
registry;
|
|
124
|
+
media;
|
|
125
|
+
state;
|
|
126
|
+
owns = false;
|
|
127
|
+
hasApplied = false;
|
|
128
|
+
constructor(document, transitionMs) {
|
|
129
|
+
this.transitionMs = transitionMs;
|
|
130
|
+
this.registry = ɵdockedSidebarRegistryFor(document);
|
|
131
|
+
this.media = document.defaultView?.matchMedia?.(ɵMOBILE_MAX_WIDTH_QUERY);
|
|
132
|
+
this.media?.addEventListener("change", this.apply);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* `true` while this sidebar has the page margin. `false` when it is closed,
|
|
136
|
+
* on a mobile viewport, or when another docked sidebar has the margin.
|
|
137
|
+
*/
|
|
138
|
+
get isDocked() {
|
|
139
|
+
return this.owns;
|
|
140
|
+
}
|
|
141
|
+
/** Apply the sidebar's open state, side, and width. */
|
|
142
|
+
set(state) {
|
|
143
|
+
this.state = state;
|
|
144
|
+
this.apply();
|
|
145
|
+
}
|
|
146
|
+
/** Give the margins back at once and stop following the viewport. */
|
|
147
|
+
dispose() {
|
|
148
|
+
this.media?.removeEventListener("change", this.apply);
|
|
149
|
+
this.registry.release(this.owner);
|
|
150
|
+
this.owns = false;
|
|
151
|
+
}
|
|
152
|
+
apply = () => {
|
|
153
|
+
const state = this.state;
|
|
154
|
+
if (!state) return;
|
|
155
|
+
const transition = this.hasApplied ? { transitionMs: this.transitionMs } : {};
|
|
156
|
+
this.hasApplied = true;
|
|
157
|
+
if (!(state.open && !this.media?.matches)) {
|
|
158
|
+
if (this.owns) this.registry.release(this.owner, transition);
|
|
159
|
+
this.owns = false;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
this.owns = this.owns || this.registry.acquire(this.owner);
|
|
163
|
+
if (this.owns) this.registry.update(this.owner, state, transition);
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Create the controller that reserves the page margin for a docked sidebar.
|
|
168
|
+
*
|
|
169
|
+
* @param document - The page.
|
|
170
|
+
* @param options - `transitionMs`: how long margin changes animate.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* const dock = ɵcreateDockedSidebar(document, { transitionMs: 260 });
|
|
175
|
+
* dock.set({ open: true, position: "right", width: "480px" });
|
|
176
|
+
* dock.dispose(); // on unmount
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function ɵcreateDockedSidebar(document, options) {
|
|
180
|
+
return new ɵDockedSidebar(document, options.transitionMs);
|
|
181
|
+
}
|
|
182
|
+
/** How long the popup's open animation runs, in ms. */
|
|
183
|
+
const ɵPOPUP_ANIMATION_MS = 200;
|
|
184
|
+
/** The popup's width when the `width` prop is missing, in px. */
|
|
185
|
+
const ɵPOPUP_DEFAULT_WIDTH = 420;
|
|
186
|
+
/** The popup's height when the `height` prop is missing, in px. */
|
|
187
|
+
const ɵPOPUP_DEFAULT_HEIGHT = 560;
|
|
188
|
+
/** The sidebar's width when the `width` prop is missing, in px. */
|
|
189
|
+
const ɵSIDEBAR_DEFAULT_WIDTH = 480;
|
|
190
|
+
/** How long the docked sidebar's page margin animates, in ms. */
|
|
191
|
+
const ɵSIDEBAR_TRANSITION_MS = 260;
|
|
192
|
+
/**
|
|
193
|
+
* Keeps a closed panel in the DOM for its exit animation, then removes it.
|
|
194
|
+
* Opening again during the animation cancels the removal.
|
|
195
|
+
*
|
|
196
|
+
* Create it with {@link ɵcreateExitPresence}. Each change makes a new state
|
|
197
|
+
* object, so `getState` works with React's `useSyncExternalStore`.
|
|
198
|
+
*/
|
|
199
|
+
var ɵExitPresence = class {
|
|
200
|
+
state;
|
|
201
|
+
timer;
|
|
202
|
+
listeners = /* @__PURE__ */ new Set();
|
|
203
|
+
constructor(open, durationMs) {
|
|
204
|
+
this.durationMs = durationMs;
|
|
205
|
+
this.state = {
|
|
206
|
+
isRendered: open,
|
|
207
|
+
isClosing: false
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/** The current state. */
|
|
211
|
+
getState = () => this.state;
|
|
212
|
+
/**
|
|
213
|
+
* Call `listener` on each change.
|
|
214
|
+
*
|
|
215
|
+
* @returns The unsubscribe function.
|
|
216
|
+
*/
|
|
217
|
+
subscribe = (listener) => {
|
|
218
|
+
this.listeners.add(listener);
|
|
219
|
+
return () => {
|
|
220
|
+
this.listeners.delete(listener);
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
/** Apply the panel's open state. */
|
|
224
|
+
setOpen(open) {
|
|
225
|
+
if (open) {
|
|
226
|
+
this.clearTimer();
|
|
227
|
+
this.set({
|
|
228
|
+
isRendered: true,
|
|
229
|
+
isClosing: false
|
|
230
|
+
});
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (!this.state.isRendered || this.timer !== void 0) return;
|
|
234
|
+
this.set({
|
|
235
|
+
isRendered: true,
|
|
236
|
+
isClosing: true
|
|
237
|
+
});
|
|
238
|
+
this.timer = setTimeout(() => {
|
|
239
|
+
this.timer = void 0;
|
|
240
|
+
this.set({
|
|
241
|
+
isRendered: false,
|
|
242
|
+
isClosing: false
|
|
243
|
+
});
|
|
244
|
+
}, this.durationMs);
|
|
245
|
+
}
|
|
246
|
+
/** Cancel a pending removal. */
|
|
247
|
+
dispose() {
|
|
248
|
+
this.clearTimer();
|
|
249
|
+
}
|
|
250
|
+
set(next) {
|
|
251
|
+
const current = this.state;
|
|
252
|
+
if (current.isRendered === next.isRendered && current.isClosing === next.isClosing) return;
|
|
253
|
+
this.state = next;
|
|
254
|
+
for (const listener of this.listeners) listener(next);
|
|
255
|
+
}
|
|
256
|
+
clearTimer() {
|
|
257
|
+
if (this.timer === void 0) return;
|
|
258
|
+
clearTimeout(this.timer);
|
|
259
|
+
this.timer = void 0;
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* Create the exit-animation state of a popup or another panel.
|
|
264
|
+
*
|
|
265
|
+
* @param open - Whether the panel starts open.
|
|
266
|
+
* @param options - `durationMs`: how long the exit animation runs. Default:
|
|
267
|
+
* {@link ɵPOPUP_ANIMATION_MS}.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* const presence = ɵcreateExitPresence(isOpen);
|
|
272
|
+
* presence.subscribe(({ isRendered, isClosing }) => render(isRendered, isClosing));
|
|
273
|
+
* presence.setOpen(false); // renders until the animation ends
|
|
274
|
+
* presence.dispose(); // on unmount
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
function ɵcreateExitPresence(open, options = {}) {
|
|
278
|
+
return new ɵExitPresence(open, options.durationMs ?? ɵPOPUP_ANIMATION_MS);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Close an open popup on Escape and, when asked, on a pointer down outside
|
|
282
|
+
* it, and move focus into it once its open animation ends.
|
|
283
|
+
*
|
|
284
|
+
* A pointer down on the chat toggle button, or in a chat popover (which
|
|
285
|
+
* renders in a portal, outside the panel), does not close the popup. Focus
|
|
286
|
+
* moves to the panel only when nothing inside it has focus yet.
|
|
287
|
+
*
|
|
288
|
+
* Call it when the popup opens, and call the returned function when it
|
|
289
|
+
* closes or unmounts.
|
|
290
|
+
*
|
|
291
|
+
* @param options - The panel, the close callback, and whether a click
|
|
292
|
+
* outside closes.
|
|
293
|
+
*/
|
|
294
|
+
function ɵbindPopupDismiss(options) {
|
|
295
|
+
if (typeof window === "undefined") return () => {};
|
|
296
|
+
const handleKeyDown = (event) => {
|
|
297
|
+
if (event.key !== "Escape") return;
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
options.onDismiss();
|
|
300
|
+
};
|
|
301
|
+
const handlePointerDown = (event) => {
|
|
302
|
+
const target = event.target;
|
|
303
|
+
if (!(target instanceof Node)) return;
|
|
304
|
+
if (options.getContainer()?.contains(target)) return;
|
|
305
|
+
if ((target instanceof Element ? target : target.parentElement)?.closest("[data-slot='chat-toggle-button'], [data-copilotkit-popover]")) return;
|
|
306
|
+
options.onDismiss();
|
|
307
|
+
};
|
|
308
|
+
const focusTimer = setTimeout(() => {
|
|
309
|
+
const container = options.getContainer();
|
|
310
|
+
if (container && !container.contains(container.ownerDocument.activeElement)) container.focus({ preventScroll: true });
|
|
311
|
+
}, ɵPOPUP_ANIMATION_MS);
|
|
312
|
+
window.addEventListener("keydown", handleKeyDown);
|
|
313
|
+
if (options.clickOutsideToClose) document.addEventListener("pointerdown", handlePointerDown);
|
|
314
|
+
return () => {
|
|
315
|
+
clearTimeout(focusTimer);
|
|
316
|
+
window.removeEventListener("keydown", handleKeyDown);
|
|
317
|
+
document.removeEventListener("pointerdown", handlePointerDown);
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Open or close a lightbox with the View Transition API, so the thumbnail
|
|
322
|
+
* morphs into the preview and back.
|
|
323
|
+
*
|
|
324
|
+
* `view-transition-name` must be on one element at a time: on the
|
|
325
|
+
* thumbnail before the change and on the preview after it. Bind `name` to
|
|
326
|
+
* the preview element. Without the API, or without a thumbnail, `apply`
|
|
327
|
+
* runs with no animation.
|
|
328
|
+
*
|
|
329
|
+
* @param thumbnail - The element the preview grows from.
|
|
330
|
+
* @param name - A `view-transition-name`, unique on the page.
|
|
331
|
+
* @param apply - Renders the change. It must finish the DOM update before
|
|
332
|
+
* it returns or resolves (React: `flushSync`; Vue: `await nextTick()`).
|
|
333
|
+
* @param direction - `"open"` or `"close"`.
|
|
334
|
+
*/
|
|
335
|
+
function ɵmorphViewTransition(thumbnail, name, apply, direction) {
|
|
336
|
+
const doc = typeof document === "undefined" ? void 0 : document;
|
|
337
|
+
if (!(!!thumbnail && typeof doc?.startViewTransition === "function") || !thumbnail || !doc) {
|
|
338
|
+
apply();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (direction === "open") {
|
|
342
|
+
thumbnail.style.viewTransitionName = name;
|
|
343
|
+
doc.startViewTransition(async () => {
|
|
344
|
+
thumbnail.style.viewTransitionName = "";
|
|
345
|
+
await apply();
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const transition = doc.startViewTransition(async () => {
|
|
350
|
+
await apply();
|
|
351
|
+
thumbnail.style.viewTransitionName = name;
|
|
352
|
+
});
|
|
353
|
+
const clear = () => {
|
|
354
|
+
thumbnail.style.viewTransitionName = "";
|
|
355
|
+
};
|
|
356
|
+
transition.finished.then(clear, clear);
|
|
357
|
+
}
|
|
358
|
+
const FOCUSABLE_ELEMENTS = [
|
|
359
|
+
"a[href]",
|
|
360
|
+
"button:not([disabled])",
|
|
361
|
+
"input:not([disabled])",
|
|
362
|
+
"select:not([disabled])",
|
|
363
|
+
"textarea:not([disabled])",
|
|
364
|
+
"audio[controls]",
|
|
365
|
+
"video[controls]",
|
|
366
|
+
"iframe",
|
|
367
|
+
"[tabindex]"
|
|
368
|
+
];
|
|
369
|
+
const FOCUSABLE = FOCUSABLE_ELEMENTS.map((selector) => selector === "[tabindex]" ? "[tabindex]:not([tabindex=\"-1\"])" : selector).join(", ");
|
|
370
|
+
/**
|
|
371
|
+
* Elements Tab stops at: the focusable ones without `tabindex="-1"`. Radix
|
|
372
|
+
* focus guards, which only pass focus on, are left out.
|
|
373
|
+
*/
|
|
374
|
+
const ɵTAB_STOP_SELECTOR = FOCUSABLE_ELEMENTS.map((selector) => `${selector}:not([tabindex="-1"]):not([data-radix-focus-guard])`).join(", ");
|
|
375
|
+
/**
|
|
376
|
+
* Approximately the element Tab reaches after `element`, in document order.
|
|
377
|
+
* Elements inside `skip` (for example a popover in a portal) and inside
|
|
378
|
+
* `[hidden]` or `[inert]` are passed over.
|
|
379
|
+
*
|
|
380
|
+
* @returns The next tab stop, or `null` when there is none.
|
|
381
|
+
*/
|
|
382
|
+
function ɵnextTabStop(element, skip) {
|
|
383
|
+
const all = Array.from(element.ownerDocument.querySelectorAll(ɵTAB_STOP_SELECTOR)).filter((candidate) => candidate === element || !skip?.contains(candidate) && !candidate.closest("[hidden], [inert]"));
|
|
384
|
+
const index = all.findIndex((candidate) => candidate === element);
|
|
385
|
+
return index === -1 ? null : all[index + 1] ?? null;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Whether focus on `element` came from the keyboard. A pointer click also
|
|
389
|
+
* focuses buttons, and this tells the two apart.
|
|
390
|
+
*/
|
|
391
|
+
function ɵisFocusVisible(element) {
|
|
392
|
+
try {
|
|
393
|
+
return element.matches(":focus-visible");
|
|
394
|
+
} catch {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Make `root` behave as a modal dialog for keyboard users: focus moves into
|
|
400
|
+
* it, Tab and Shift+Tab wrap inside it, and focus goes back to the opener
|
|
401
|
+
* when the returned function runs.
|
|
402
|
+
*
|
|
403
|
+
* @param root - The dialog element.
|
|
404
|
+
* @param options - Where focus goes on open and on close.
|
|
405
|
+
* @returns A function that removes the trap and returns focus.
|
|
406
|
+
*/
|
|
407
|
+
function ɵbindDialogFocus(root, options = {}) {
|
|
408
|
+
const doc = root.ownerDocument;
|
|
409
|
+
const active = doc.activeElement;
|
|
410
|
+
const opener = active instanceof HTMLElement ? active : null;
|
|
411
|
+
const focusables = () => Array.from(root.querySelectorAll(FOCUSABLE));
|
|
412
|
+
const handleKeyDown = (event) => {
|
|
413
|
+
if (event.key !== "Tab") return;
|
|
414
|
+
const elements = focusables();
|
|
415
|
+
const first = elements[0];
|
|
416
|
+
const last = elements[elements.length - 1];
|
|
417
|
+
if (!first || !last) {
|
|
418
|
+
event.preventDefault();
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const current = doc.activeElement;
|
|
422
|
+
const isOutside = !root.contains(current);
|
|
423
|
+
if (event.shiftKey && (current === first || isOutside)) {
|
|
424
|
+
event.preventDefault();
|
|
425
|
+
last.focus();
|
|
426
|
+
} else if (!event.shiftKey && (current === last || isOutside)) {
|
|
427
|
+
event.preventDefault();
|
|
428
|
+
first.focus();
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
root.addEventListener("keydown", handleKeyDown);
|
|
432
|
+
(options.initialFocus ?? focusables()[0])?.focus();
|
|
433
|
+
return () => {
|
|
434
|
+
root.removeEventListener("keydown", handleKeyDown);
|
|
435
|
+
const target = options.returnFocusTo ?? opener;
|
|
436
|
+
if (target?.isConnected) target.focus();
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Which in-browser preview a document gets.
|
|
441
|
+
*
|
|
442
|
+
* @param mimeType - The document's MIME type.
|
|
443
|
+
* @returns `"pdf"`, `"text"` (for `text/*`), or `null` when the browser
|
|
444
|
+
* cannot preview it.
|
|
445
|
+
*/
|
|
446
|
+
function ɵcanPreviewDocument(mimeType) {
|
|
447
|
+
if (!mimeType) return null;
|
|
448
|
+
if (mimeType.includes("pdf")) return "pdf";
|
|
449
|
+
if (mimeType.startsWith("text/")) return "text";
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
function decodeBase64(value) {
|
|
453
|
+
const binary = atob(value);
|
|
454
|
+
const bytes = new Uint8Array(binary.length);
|
|
455
|
+
for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
|
|
456
|
+
return bytes;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* A URL a browser can show in an `<iframe>` for an attachment's source.
|
|
460
|
+
*
|
|
461
|
+
* Browsers block `data:` URLs for PDFs in frames, so base64 data becomes a
|
|
462
|
+
* `blob:` URL. Call `revoke()` when the preview closes.
|
|
463
|
+
*
|
|
464
|
+
* @param source - The attachment's source.
|
|
465
|
+
* @returns `{ url, revoke }`, or `null` when the data is not valid base64.
|
|
466
|
+
*/
|
|
467
|
+
function ɵcreateObjectUrlFromSource(source) {
|
|
468
|
+
if (source.type === "url") return {
|
|
469
|
+
url: source.value,
|
|
470
|
+
revoke: () => {}
|
|
471
|
+
};
|
|
472
|
+
try {
|
|
473
|
+
const blob = new Blob([decodeBase64(source.value)], { type: source.mimeType || "application/octet-stream" });
|
|
474
|
+
const url = URL.createObjectURL(blob);
|
|
475
|
+
return {
|
|
476
|
+
url,
|
|
477
|
+
revoke: () => URL.revokeObjectURL(url)
|
|
478
|
+
};
|
|
479
|
+
} catch (error) {
|
|
480
|
+
console.error("[CopilotKit] Failed to decode attachment data:", error);
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* The text of a text attachment, for a `<pre>` preview.
|
|
486
|
+
*
|
|
487
|
+
* @param source - The attachment's source.
|
|
488
|
+
* @returns The base64 data decoded as UTF-8, the raw value when it is not
|
|
489
|
+
* valid base64, or `null` for a URL source (show it in a frame instead).
|
|
490
|
+
*/
|
|
491
|
+
function ɵdecodeTextSource(source) {
|
|
492
|
+
if (source.type !== "data") return null;
|
|
493
|
+
try {
|
|
494
|
+
return new TextDecoder().decode(decodeBase64(source.value));
|
|
495
|
+
} catch {
|
|
496
|
+
return source.value;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
/** How close to the end, in pixels, still counts as scrolled to the bottom. */
|
|
500
|
+
const ɵSCROLL_BOTTOM_THRESHOLD_PX = 12;
|
|
501
|
+
/**
|
|
502
|
+
* Whether a scroll container shows the end of its content.
|
|
503
|
+
*
|
|
504
|
+
* @param element - The scroll container.
|
|
505
|
+
* @param threshold - The pixels from the end that still count. Default:
|
|
506
|
+
* {@link ɵSCROLL_BOTTOM_THRESHOLD_PX}.
|
|
507
|
+
*/
|
|
508
|
+
function ɵisScrolledToBottom(element, threshold = ɵSCROLL_BOTTOM_THRESHOLD_PX) {
|
|
509
|
+
return element.scrollHeight - element.scrollTop - element.clientHeight <= threshold;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Whether a scroll container shows the end of `content`, an element inside
|
|
513
|
+
* it. Use it when empty space follows the messages, such as the
|
|
514
|
+
* `pin-to-send` spacer: scrolling into that space shows no more messages.
|
|
515
|
+
*
|
|
516
|
+
* @param element - The scroll container.
|
|
517
|
+
* @param content - The element that holds the messages.
|
|
518
|
+
* @param threshold - The pixels of `content` below the viewport that still
|
|
519
|
+
* count. Default: {@link ɵSCROLL_BOTTOM_THRESHOLD_PX}.
|
|
520
|
+
*/
|
|
521
|
+
function ɵisContentScrolledToBottom(element, content, threshold = ɵSCROLL_BOTTOM_THRESHOLD_PX) {
|
|
522
|
+
return content.getBoundingClientRect().bottom - element.getBoundingClientRect().bottom <= threshold;
|
|
523
|
+
}
|
|
524
|
+
/** How long an element counts as resizing after its last size change, in ms. */
|
|
525
|
+
const ɵRESIZE_SETTLE_MS = 250;
|
|
526
|
+
/**
|
|
527
|
+
* Report an element's height now and on each change. `isResizing` is `true`
|
|
528
|
+
* from a change until the height stays the same for `settleMs`, so callers
|
|
529
|
+
* can hide UI, such as the scroll-to-bottom button, while a layout moves.
|
|
530
|
+
*
|
|
531
|
+
* The first call reads `offsetHeight`. Later calls read the
|
|
532
|
+
* `ResizeObserver` content box. Without `ResizeObserver`, only the first
|
|
533
|
+
* call happens.
|
|
534
|
+
*
|
|
535
|
+
* @param element - The element to measure.
|
|
536
|
+
* @param listener - Gets the height and the resizing state.
|
|
537
|
+
* @param options - `settleMs`: default {@link ɵRESIZE_SETTLE_MS}.
|
|
538
|
+
* @returns A function that stops the watch.
|
|
539
|
+
*/
|
|
540
|
+
function ɵwatchElementHeight(element, listener, options = {}) {
|
|
541
|
+
const settleMs = options.settleMs ?? ɵRESIZE_SETTLE_MS;
|
|
542
|
+
let height = element.offsetHeight;
|
|
543
|
+
let timer;
|
|
544
|
+
listener({
|
|
545
|
+
height,
|
|
546
|
+
isResizing: false
|
|
547
|
+
});
|
|
548
|
+
if (typeof ResizeObserver === "undefined") return () => {};
|
|
549
|
+
const observer = new ResizeObserver((entries) => {
|
|
550
|
+
const next = entries[entries.length - 1]?.contentRect.height;
|
|
551
|
+
if (next === void 0 || next === height) return;
|
|
552
|
+
height = next;
|
|
553
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
554
|
+
if (settleMs <= 0) {
|
|
555
|
+
listener({
|
|
556
|
+
height,
|
|
557
|
+
isResizing: false
|
|
558
|
+
});
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
listener({
|
|
562
|
+
height,
|
|
563
|
+
isResizing: true
|
|
564
|
+
});
|
|
565
|
+
timer = setTimeout(() => {
|
|
566
|
+
timer = void 0;
|
|
567
|
+
listener({
|
|
568
|
+
height,
|
|
569
|
+
isResizing: false
|
|
570
|
+
});
|
|
571
|
+
}, settleMs);
|
|
572
|
+
});
|
|
573
|
+
observer.observe(element);
|
|
574
|
+
return () => {
|
|
575
|
+
observer.disconnect();
|
|
576
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Report an element's border-box width now and on each change, skipping
|
|
581
|
+
* zero widths (a hidden element). Uses `ResizeObserver`, or the window's
|
|
582
|
+
* `resize` event without it.
|
|
583
|
+
*
|
|
584
|
+
* @param element - The element to measure, for example a sidebar.
|
|
585
|
+
* @param listener - Gets the width in px.
|
|
586
|
+
* @returns A function that stops the watch.
|
|
587
|
+
*/
|
|
588
|
+
function ɵwatchElementWidth(element, listener) {
|
|
589
|
+
const update = () => {
|
|
590
|
+
const width = element.getBoundingClientRect().width;
|
|
591
|
+
if (width > 0) listener(width);
|
|
592
|
+
};
|
|
593
|
+
update();
|
|
594
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
595
|
+
const observer = new ResizeObserver(update);
|
|
596
|
+
observer.observe(element);
|
|
597
|
+
return () => observer.disconnect();
|
|
598
|
+
}
|
|
599
|
+
const view = element.ownerDocument.defaultView;
|
|
600
|
+
if (!view) return () => {};
|
|
601
|
+
view.addEventListener("resize", update);
|
|
602
|
+
return () => view.removeEventListener("resize", update);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
//#endregion
|
|
606
|
+
//#region src/chat/viewport.ts
|
|
607
|
+
const AUTO_SCROLL_MODES = [
|
|
608
|
+
"pin-to-bottom",
|
|
609
|
+
"pin-to-send",
|
|
610
|
+
"none"
|
|
611
|
+
];
|
|
612
|
+
const isAutoScrollMode = (value) => AUTO_SCROLL_MODES.includes(value);
|
|
613
|
+
/**
|
|
614
|
+
* Turns the `autoScroll` prop into a mode. `true` and a missing value mean
|
|
615
|
+
* `"pin-to-bottom"`, `false` means `"none"`, and an unknown string falls back
|
|
616
|
+
* to `"pin-to-bottom"`.
|
|
617
|
+
*/
|
|
618
|
+
function ɵnormalizeAutoScroll(value) {
|
|
619
|
+
if (value === false) return "none";
|
|
620
|
+
if (value === void 0 || value === true) return "pin-to-bottom";
|
|
621
|
+
return isAutoScrollMode(value) ? value : "pin-to-bottom";
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Media query for the mobile range, where the threads drawer and the chat
|
|
625
|
+
* modal are mutually exclusive.
|
|
626
|
+
*/
|
|
627
|
+
const ɵMOBILE_MAX_WIDTH_QUERY = "(max-width: 767px)";
|
|
628
|
+
/** `window.matchMedia` for the mobile query, or `null` without a DOM. */
|
|
629
|
+
const mobileMediaQuery = () => {
|
|
630
|
+
return typeof window !== "undefined" && typeof window.matchMedia === "function" ? window.matchMedia(ɵMOBILE_MAX_WIDTH_QUERY) : null;
|
|
631
|
+
};
|
|
632
|
+
/**
|
|
633
|
+
* Whether the viewport is in the mobile range (`<768px`). `false` during SSR
|
|
634
|
+
* and where `window.matchMedia` is missing (some test runners and webviews).
|
|
635
|
+
*/
|
|
636
|
+
function ɵisMobileViewport() {
|
|
637
|
+
return mobileMediaQuery()?.matches ?? false;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Calls `listener` with {@link ɵisMobileViewport} now and each time the
|
|
641
|
+
* viewport enters or leaves the mobile range. Does nothing without
|
|
642
|
+
* `window.matchMedia`. Returns the unsubscribe function.
|
|
643
|
+
*/
|
|
644
|
+
function ɵwatchMobileViewport(listener) {
|
|
645
|
+
const query = mobileMediaQuery();
|
|
646
|
+
if (query === null) return () => {};
|
|
647
|
+
const update = () => listener(query.matches);
|
|
648
|
+
update();
|
|
649
|
+
query.addEventListener("change", update);
|
|
650
|
+
return () => query.removeEventListener("change", update);
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* The on-screen keyboard is treated as open once it covers more than this
|
|
654
|
+
* many pixels of the layout viewport.
|
|
655
|
+
*/
|
|
656
|
+
const ɵKEYBOARD_OPEN_THRESHOLD_PX = 150;
|
|
657
|
+
/**
|
|
658
|
+
* Reads the keyboard state with the Visual Viewport API: the keyboard height
|
|
659
|
+
* is `innerHeight - visualViewport.height`, clamped to 0. Without the API (or
|
|
660
|
+
* during SSR) it reports a closed keyboard and the full window height.
|
|
661
|
+
*/
|
|
662
|
+
function ɵreadKeyboardState() {
|
|
663
|
+
if (typeof window === "undefined") return closedKeyboard(0);
|
|
664
|
+
const visualViewport = window.visualViewport;
|
|
665
|
+
if (!visualViewport) return closedKeyboard(window.innerHeight);
|
|
666
|
+
const layoutHeight = window.innerHeight;
|
|
667
|
+
const keyboardHeight = Math.max(0, layoutHeight - visualViewport.height);
|
|
668
|
+
return {
|
|
669
|
+
isKeyboardOpen: keyboardHeight > ɵKEYBOARD_OPEN_THRESHOLD_PX,
|
|
670
|
+
keyboardHeight,
|
|
671
|
+
availableHeight: visualViewport.height,
|
|
672
|
+
viewportHeight: layoutHeight
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
const closedKeyboard = (height) => ({
|
|
676
|
+
isKeyboardOpen: false,
|
|
677
|
+
keyboardHeight: 0,
|
|
678
|
+
availableHeight: height,
|
|
679
|
+
viewportHeight: height
|
|
680
|
+
});
|
|
681
|
+
/**
|
|
682
|
+
* The state for the first render, before {@link ɵwatchKeyboardState} reads
|
|
683
|
+
* the visual viewport: a closed keyboard over the full window height (0
|
|
684
|
+
* during SSR).
|
|
685
|
+
*/
|
|
686
|
+
function ɵinitialKeyboardState() {
|
|
687
|
+
return closedKeyboard(typeof window === "undefined" ? 0 : window.innerHeight);
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Calls `listener` with {@link ɵreadKeyboardState} now and on each visual
|
|
691
|
+
* viewport resize or scroll. Does nothing during SSR or without the Visual
|
|
692
|
+
* Viewport API. Returns the unsubscribe function.
|
|
693
|
+
*/
|
|
694
|
+
function ɵwatchKeyboardState(listener) {
|
|
695
|
+
if (typeof window === "undefined") return () => {};
|
|
696
|
+
const visualViewport = window.visualViewport;
|
|
697
|
+
if (!visualViewport) return () => {};
|
|
698
|
+
const update = () => listener(ɵreadKeyboardState());
|
|
699
|
+
update();
|
|
700
|
+
visualViewport.addEventListener("resize", update);
|
|
701
|
+
visualViewport.addEventListener("scroll", update);
|
|
702
|
+
return () => {
|
|
703
|
+
visualViewport.removeEventListener("resize", update);
|
|
704
|
+
visualViewport.removeEventListener("scroll", update);
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
/** Escapes `id` for a CSS attribute selector, also without `CSS.escape`. */
|
|
708
|
+
const escapeSelectorValue = (id) => typeof CSS !== "undefined" && typeof CSS.escape === "function" ? CSS.escape(id) : id.replace(/[!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g, "\\$&");
|
|
709
|
+
/**
|
|
710
|
+
* Offset of `el` from the top of `stopAt`'s content, including `stopAt`'s
|
|
711
|
+
* scroll. Uses `getBoundingClientRect`, so it works for any CSS position.
|
|
712
|
+
*/
|
|
713
|
+
const offsetTopWithin = (el, stopAt) => el.getBoundingClientRect().top - stopAt.getBoundingClientRect().top + stopAt.scrollTop;
|
|
714
|
+
/**
|
|
715
|
+
* Pins a sent message to the top of the chat: sizes the spacer so the
|
|
716
|
+
* message bubble can scroll to `topOffset` from the viewport top, scrolls
|
|
717
|
+
* there on the next frame, and then shrinks the spacer as the reply below
|
|
718
|
+
* grows, so the bubble stays put.
|
|
719
|
+
*
|
|
720
|
+
* The message element's top padding is scrolled off-screen: the bubble, not
|
|
721
|
+
* its padded box, is pinned. Does nothing when no element in `contentEl` has
|
|
722
|
+
* `data-message-id` equal to `messageId`. Skips the scroll without
|
|
723
|
+
* `requestAnimationFrame` and the spacer updates without `ResizeObserver`.
|
|
724
|
+
*
|
|
725
|
+
* @returns A function that cancels the scroll and stops the spacer updates.
|
|
726
|
+
* It returns `false` when it cancelled a scroll that had not run yet, so the
|
|
727
|
+
* caller can pin the same send again. React's StrictMode tears an effect
|
|
728
|
+
* down before the next frame, so the first run never scrolls.
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```ts
|
|
732
|
+
* const unpin = ɵpinMessageToTop({ scrollEl, contentEl, spacerEl, messageId });
|
|
733
|
+
* // On the next send or on unmount:
|
|
734
|
+
* unpin();
|
|
735
|
+
* ```
|
|
736
|
+
*/
|
|
737
|
+
function ɵpinMessageToTop({ scrollEl, contentEl, spacerEl, messageId, topOffset = 16 }) {
|
|
738
|
+
const targetEl = contentEl.querySelector(`[data-message-id="${escapeSelectorValue(messageId)}"]`);
|
|
739
|
+
if (!targetEl) return () => true;
|
|
740
|
+
const viewportHeight = scrollEl.clientHeight;
|
|
741
|
+
const userMessageHeight = targetEl.getBoundingClientRect().height;
|
|
742
|
+
const paddingTop = parseFloat(getComputedStyle(targetEl).paddingTop) || 0;
|
|
743
|
+
const bubbleHeight = Math.max(0, userMessageHeight - paddingTop);
|
|
744
|
+
const spacerHeight = Math.max(0, viewportHeight - bubbleHeight - topOffset);
|
|
745
|
+
spacerEl.style.height = `${spacerHeight}px`;
|
|
746
|
+
let scrolled = false;
|
|
747
|
+
const frame = typeof requestAnimationFrame === "function" ? requestAnimationFrame(() => {
|
|
748
|
+
scrolled = true;
|
|
749
|
+
const targetTop = offsetTopWithin(targetEl, scrollEl) + paddingTop - topOffset;
|
|
750
|
+
scrollEl.scrollTo({
|
|
751
|
+
top: Math.max(0, targetTop),
|
|
752
|
+
behavior: "smooth"
|
|
753
|
+
});
|
|
754
|
+
}) : null;
|
|
755
|
+
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => {
|
|
756
|
+
const consumedBelow = contentEl.getBoundingClientRect().height - offsetTopWithin(targetEl, contentEl) - userMessageHeight;
|
|
757
|
+
const remaining = Math.max(0, spacerHeight - consumedBelow);
|
|
758
|
+
spacerEl.style.height = `${remaining}px`;
|
|
759
|
+
});
|
|
760
|
+
observer?.observe(contentEl);
|
|
761
|
+
return () => {
|
|
762
|
+
if (frame !== null && typeof cancelAnimationFrame === "function") cancelAnimationFrame(frame);
|
|
763
|
+
observer?.disconnect();
|
|
764
|
+
return frame === null || scrolled;
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Keeps a message list at its bottom while content arrives, and stops when
|
|
769
|
+
* the reader scrolls up. Scrolling back to the bottom, or a call to
|
|
770
|
+
* {@link ɵStickToBottom.scrollToBottom}, starts it again.
|
|
771
|
+
*
|
|
772
|
+
* Content changes come from a `ResizeObserver` on both elements and a
|
|
773
|
+
* `MutationObserver` on the content. Without them, only scroll events and
|
|
774
|
+
* explicit calls update it.
|
|
775
|
+
*
|
|
776
|
+
* Create it with {@link ɵcreateStickToBottom}.
|
|
777
|
+
*/
|
|
778
|
+
var ɵStickToBottom = class {
|
|
779
|
+
scrollElement;
|
|
780
|
+
contentElement;
|
|
781
|
+
threshold;
|
|
782
|
+
followBehavior;
|
|
783
|
+
onChange;
|
|
784
|
+
follow;
|
|
785
|
+
/** The reader has not scrolled away from the bottom. */
|
|
786
|
+
stuck = true;
|
|
787
|
+
/** A smooth scroll to the bottom is on its way. */
|
|
788
|
+
smoothPending = false;
|
|
789
|
+
/** `scrollTop` right after the last instant scroll to the bottom. */
|
|
790
|
+
programmaticTop = null;
|
|
791
|
+
lastTop;
|
|
792
|
+
/** The largest `scrollTop` at the last read, to tell when content shrank. */
|
|
793
|
+
lastMaxTop;
|
|
794
|
+
reported = true;
|
|
795
|
+
frame = null;
|
|
796
|
+
observers = [];
|
|
797
|
+
constructor(options) {
|
|
798
|
+
this.scrollElement = options.scrollElement;
|
|
799
|
+
this.contentElement = options.contentElement ?? options.scrollElement;
|
|
800
|
+
this.threshold = options.threshold ?? ɵSCROLL_BOTTOM_THRESHOLD_PX;
|
|
801
|
+
this.followBehavior = options.followBehavior ?? "instant";
|
|
802
|
+
this.onChange = options.onChange;
|
|
803
|
+
this.follow = options.follow ?? true;
|
|
804
|
+
this.lastTop = this.scrollElement.scrollTop;
|
|
805
|
+
this.lastMaxTop = this.maxTop();
|
|
806
|
+
this.scrollElement.addEventListener("scroll", this.update);
|
|
807
|
+
this.scrollElement.addEventListener("scrollend", this.handleScrollEnd);
|
|
808
|
+
this.observeContent();
|
|
809
|
+
if (this.follow) {
|
|
810
|
+
this.scrollToEnd(options.initialBehavior ?? "instant");
|
|
811
|
+
if (typeof requestAnimationFrame === "function") this.frame = requestAnimationFrame(() => {
|
|
812
|
+
this.frame = null;
|
|
813
|
+
this.followContent();
|
|
814
|
+
});
|
|
815
|
+
} else this.report();
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* `true` when the list shows its end, or when it follows new content and
|
|
819
|
+
* is on its way there.
|
|
820
|
+
*/
|
|
821
|
+
get isAtBottom() {
|
|
822
|
+
return this.atBottom() || this.follow && this.stuck;
|
|
823
|
+
}
|
|
824
|
+
/** Turn following new content on or off. */
|
|
825
|
+
setFollow(follow) {
|
|
826
|
+
if (this.follow === follow) return;
|
|
827
|
+
this.follow = follow;
|
|
828
|
+
this.followContent();
|
|
829
|
+
this.report();
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Scroll to the end and follow new content again.
|
|
833
|
+
*
|
|
834
|
+
* @param behavior - Default: `"smooth"`.
|
|
835
|
+
*/
|
|
836
|
+
scrollToBottom(behavior = "smooth") {
|
|
837
|
+
this.stuck = true;
|
|
838
|
+
this.scrollToEnd(behavior);
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Read the scroll position again, as a scroll event does. Call it when the
|
|
842
|
+
* framework handles scroll events itself.
|
|
843
|
+
*/
|
|
844
|
+
update = () => {
|
|
845
|
+
const top = this.scrollElement.scrollTop;
|
|
846
|
+
if (this.movedUp(top)) this.leave();
|
|
847
|
+
else if (this.atBottom()) {
|
|
848
|
+
this.stuck = true;
|
|
849
|
+
this.smoothPending = false;
|
|
850
|
+
} else if (top === this.programmaticTop) {} else if (this.smoothPending && top >= this.lastTop) {} else this.leave();
|
|
851
|
+
this.remember(top);
|
|
852
|
+
this.report();
|
|
853
|
+
};
|
|
854
|
+
/** Stop listening and observing. */
|
|
855
|
+
dispose() {
|
|
856
|
+
this.scrollElement.removeEventListener("scroll", this.update);
|
|
857
|
+
this.scrollElement.removeEventListener("scrollend", this.handleScrollEnd);
|
|
858
|
+
for (const observer of this.observers) observer.disconnect();
|
|
859
|
+
this.observers.length = 0;
|
|
860
|
+
if (this.frame !== null && typeof cancelAnimationFrame === "function") cancelAnimationFrame(this.frame);
|
|
861
|
+
this.frame = null;
|
|
862
|
+
}
|
|
863
|
+
handleScrollEnd = () => {
|
|
864
|
+
this.smoothPending = false;
|
|
865
|
+
};
|
|
866
|
+
followContent = () => {
|
|
867
|
+
const top = this.scrollElement.scrollTop;
|
|
868
|
+
if (this.movedUp(top)) this.leave();
|
|
869
|
+
this.remember(top);
|
|
870
|
+
if (this.follow && this.stuck) this.scrollToEnd(this.followBehavior);
|
|
871
|
+
else this.report();
|
|
872
|
+
};
|
|
873
|
+
observeContent() {
|
|
874
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
875
|
+
const resize = new ResizeObserver(this.followContent);
|
|
876
|
+
resize.observe(this.scrollElement);
|
|
877
|
+
if (this.contentElement !== this.scrollElement) resize.observe(this.contentElement);
|
|
878
|
+
this.observers.push(resize);
|
|
879
|
+
}
|
|
880
|
+
if (typeof MutationObserver !== "undefined") {
|
|
881
|
+
const mutation = new MutationObserver(this.followContent);
|
|
882
|
+
mutation.observe(this.contentElement, {
|
|
883
|
+
childList: true,
|
|
884
|
+
subtree: true,
|
|
885
|
+
characterData: true
|
|
886
|
+
});
|
|
887
|
+
this.observers.push(mutation);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
scrollToEnd(behavior) {
|
|
891
|
+
const element = this.scrollElement;
|
|
892
|
+
const smooth = behavior === "smooth";
|
|
893
|
+
if (typeof element.scrollTo === "function") element.scrollTo({
|
|
894
|
+
top: element.scrollHeight,
|
|
895
|
+
behavior
|
|
896
|
+
});
|
|
897
|
+
else element.scrollTop = element.scrollHeight;
|
|
898
|
+
this.smoothPending = smooth && !this.atBottom();
|
|
899
|
+
this.programmaticTop = smooth ? null : element.scrollTop;
|
|
900
|
+
this.remember(element.scrollTop);
|
|
901
|
+
this.report();
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* `scrollTop` went down since the last read because the reader scrolled
|
|
905
|
+
* up. Not when the content (or the viewport) changed so that the browser
|
|
906
|
+
* clamped `scrollTop`, and not for the controller's own scroll.
|
|
907
|
+
*/
|
|
908
|
+
movedUp(top) {
|
|
909
|
+
const shrank = this.maxTop() < this.lastMaxTop;
|
|
910
|
+
return top < this.lastTop && !shrank && top !== this.programmaticTop;
|
|
911
|
+
}
|
|
912
|
+
leave() {
|
|
913
|
+
this.stuck = false;
|
|
914
|
+
this.smoothPending = false;
|
|
915
|
+
this.programmaticTop = null;
|
|
916
|
+
}
|
|
917
|
+
remember(top) {
|
|
918
|
+
this.lastTop = top;
|
|
919
|
+
this.lastMaxTop = this.maxTop();
|
|
920
|
+
}
|
|
921
|
+
maxTop() {
|
|
922
|
+
const element = this.scrollElement;
|
|
923
|
+
return element.scrollHeight - element.clientHeight;
|
|
924
|
+
}
|
|
925
|
+
atBottom() {
|
|
926
|
+
return ɵisScrolledToBottom(this.scrollElement, this.threshold);
|
|
927
|
+
}
|
|
928
|
+
report() {
|
|
929
|
+
const next = this.isAtBottom;
|
|
930
|
+
if (next === this.reported) return;
|
|
931
|
+
this.reported = next;
|
|
932
|
+
this.onChange?.(next);
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
/**
|
|
936
|
+
* Create the controller that keeps a message list at its bottom.
|
|
937
|
+
*
|
|
938
|
+
* @example
|
|
939
|
+
* ```ts
|
|
940
|
+
* const stick = ɵcreateStickToBottom({
|
|
941
|
+
* scrollElement,
|
|
942
|
+
* contentElement,
|
|
943
|
+
* onChange: (isAtBottom) => (showButton = !isAtBottom),
|
|
944
|
+
* });
|
|
945
|
+
* button.onclick = () => stick.scrollToBottom();
|
|
946
|
+
* stick.dispose(); // on unmount
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
function ɵcreateStickToBottom(options) {
|
|
950
|
+
return new ɵStickToBottom(options);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
//#endregion
|
|
954
|
+
Object.defineProperty(exports, 'ɵDockedSidebar', {
|
|
955
|
+
enumerable: true,
|
|
956
|
+
get: function () {
|
|
957
|
+
return ɵDockedSidebar;
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
Object.defineProperty(exports, 'ɵDockedSidebarRegistry', {
|
|
961
|
+
enumerable: true,
|
|
962
|
+
get: function () {
|
|
963
|
+
return ɵDockedSidebarRegistry;
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
Object.defineProperty(exports, 'ɵExitPresence', {
|
|
967
|
+
enumerable: true,
|
|
968
|
+
get: function () {
|
|
969
|
+
return ɵExitPresence;
|
|
970
|
+
}
|
|
971
|
+
});
|
|
972
|
+
Object.defineProperty(exports, 'ɵKEYBOARD_OPEN_THRESHOLD_PX', {
|
|
973
|
+
enumerable: true,
|
|
974
|
+
get: function () {
|
|
975
|
+
return ɵKEYBOARD_OPEN_THRESHOLD_PX;
|
|
976
|
+
}
|
|
977
|
+
});
|
|
978
|
+
Object.defineProperty(exports, 'ɵMOBILE_MAX_WIDTH_QUERY', {
|
|
979
|
+
enumerable: true,
|
|
980
|
+
get: function () {
|
|
981
|
+
return ɵMOBILE_MAX_WIDTH_QUERY;
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
Object.defineProperty(exports, 'ɵPOPUP_ANIMATION_MS', {
|
|
985
|
+
enumerable: true,
|
|
986
|
+
get: function () {
|
|
987
|
+
return ɵPOPUP_ANIMATION_MS;
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
Object.defineProperty(exports, 'ɵPOPUP_DEFAULT_HEIGHT', {
|
|
991
|
+
enumerable: true,
|
|
992
|
+
get: function () {
|
|
993
|
+
return ɵPOPUP_DEFAULT_HEIGHT;
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
Object.defineProperty(exports, 'ɵPOPUP_DEFAULT_WIDTH', {
|
|
997
|
+
enumerable: true,
|
|
998
|
+
get: function () {
|
|
999
|
+
return ɵPOPUP_DEFAULT_WIDTH;
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
Object.defineProperty(exports, 'ɵRESIZE_SETTLE_MS', {
|
|
1003
|
+
enumerable: true,
|
|
1004
|
+
get: function () {
|
|
1005
|
+
return ɵRESIZE_SETTLE_MS;
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
Object.defineProperty(exports, 'ɵSCROLL_BOTTOM_THRESHOLD_PX', {
|
|
1009
|
+
enumerable: true,
|
|
1010
|
+
get: function () {
|
|
1011
|
+
return ɵSCROLL_BOTTOM_THRESHOLD_PX;
|
|
1012
|
+
}
|
|
1013
|
+
});
|
|
1014
|
+
Object.defineProperty(exports, 'ɵSIDEBAR_DEFAULT_WIDTH', {
|
|
1015
|
+
enumerable: true,
|
|
1016
|
+
get: function () {
|
|
1017
|
+
return ɵSIDEBAR_DEFAULT_WIDTH;
|
|
1018
|
+
}
|
|
1019
|
+
});
|
|
1020
|
+
Object.defineProperty(exports, 'ɵSIDEBAR_TRANSITION_MS', {
|
|
1021
|
+
enumerable: true,
|
|
1022
|
+
get: function () {
|
|
1023
|
+
return ɵSIDEBAR_TRANSITION_MS;
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
Object.defineProperty(exports, 'ɵStickToBottom', {
|
|
1027
|
+
enumerable: true,
|
|
1028
|
+
get: function () {
|
|
1029
|
+
return ɵStickToBottom;
|
|
1030
|
+
}
|
|
1031
|
+
});
|
|
1032
|
+
Object.defineProperty(exports, 'ɵTAB_STOP_SELECTOR', {
|
|
1033
|
+
enumerable: true,
|
|
1034
|
+
get: function () {
|
|
1035
|
+
return ɵTAB_STOP_SELECTOR;
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
Object.defineProperty(exports, 'ɵbindDialogFocus', {
|
|
1039
|
+
enumerable: true,
|
|
1040
|
+
get: function () {
|
|
1041
|
+
return ɵbindDialogFocus;
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
Object.defineProperty(exports, 'ɵbindPopupDismiss', {
|
|
1045
|
+
enumerable: true,
|
|
1046
|
+
get: function () {
|
|
1047
|
+
return ɵbindPopupDismiss;
|
|
1048
|
+
}
|
|
1049
|
+
});
|
|
1050
|
+
Object.defineProperty(exports, 'ɵcanPreviewDocument', {
|
|
1051
|
+
enumerable: true,
|
|
1052
|
+
get: function () {
|
|
1053
|
+
return ɵcanPreviewDocument;
|
|
1054
|
+
}
|
|
1055
|
+
});
|
|
1056
|
+
Object.defineProperty(exports, 'ɵcreateDockedSidebar', {
|
|
1057
|
+
enumerable: true,
|
|
1058
|
+
get: function () {
|
|
1059
|
+
return ɵcreateDockedSidebar;
|
|
1060
|
+
}
|
|
1061
|
+
});
|
|
1062
|
+
Object.defineProperty(exports, 'ɵcreateExitPresence', {
|
|
1063
|
+
enumerable: true,
|
|
1064
|
+
get: function () {
|
|
1065
|
+
return ɵcreateExitPresence;
|
|
1066
|
+
}
|
|
1067
|
+
});
|
|
1068
|
+
Object.defineProperty(exports, 'ɵcreateObjectUrlFromSource', {
|
|
1069
|
+
enumerable: true,
|
|
1070
|
+
get: function () {
|
|
1071
|
+
return ɵcreateObjectUrlFromSource;
|
|
1072
|
+
}
|
|
1073
|
+
});
|
|
1074
|
+
Object.defineProperty(exports, 'ɵcreateStickToBottom', {
|
|
1075
|
+
enumerable: true,
|
|
1076
|
+
get: function () {
|
|
1077
|
+
return ɵcreateStickToBottom;
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
Object.defineProperty(exports, 'ɵdecodeTextSource', {
|
|
1081
|
+
enumerable: true,
|
|
1082
|
+
get: function () {
|
|
1083
|
+
return ɵdecodeTextSource;
|
|
1084
|
+
}
|
|
1085
|
+
});
|
|
1086
|
+
Object.defineProperty(exports, 'ɵdimensionToCss', {
|
|
1087
|
+
enumerable: true,
|
|
1088
|
+
get: function () {
|
|
1089
|
+
return ɵdimensionToCss;
|
|
1090
|
+
}
|
|
1091
|
+
});
|
|
1092
|
+
Object.defineProperty(exports, 'ɵdockedSidebarRegistryFor', {
|
|
1093
|
+
enumerable: true,
|
|
1094
|
+
get: function () {
|
|
1095
|
+
return ɵdockedSidebarRegistryFor;
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
Object.defineProperty(exports, 'ɵinitialKeyboardState', {
|
|
1099
|
+
enumerable: true,
|
|
1100
|
+
get: function () {
|
|
1101
|
+
return ɵinitialKeyboardState;
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
Object.defineProperty(exports, 'ɵisContentScrolledToBottom', {
|
|
1105
|
+
enumerable: true,
|
|
1106
|
+
get: function () {
|
|
1107
|
+
return ɵisContentScrolledToBottom;
|
|
1108
|
+
}
|
|
1109
|
+
});
|
|
1110
|
+
Object.defineProperty(exports, 'ɵisFocusVisible', {
|
|
1111
|
+
enumerable: true,
|
|
1112
|
+
get: function () {
|
|
1113
|
+
return ɵisFocusVisible;
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
Object.defineProperty(exports, 'ɵisMobileViewport', {
|
|
1117
|
+
enumerable: true,
|
|
1118
|
+
get: function () {
|
|
1119
|
+
return ɵisMobileViewport;
|
|
1120
|
+
}
|
|
1121
|
+
});
|
|
1122
|
+
Object.defineProperty(exports, 'ɵisScrolledToBottom', {
|
|
1123
|
+
enumerable: true,
|
|
1124
|
+
get: function () {
|
|
1125
|
+
return ɵisScrolledToBottom;
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
Object.defineProperty(exports, 'ɵmorphViewTransition', {
|
|
1129
|
+
enumerable: true,
|
|
1130
|
+
get: function () {
|
|
1131
|
+
return ɵmorphViewTransition;
|
|
1132
|
+
}
|
|
1133
|
+
});
|
|
1134
|
+
Object.defineProperty(exports, 'ɵnextTabStop', {
|
|
1135
|
+
enumerable: true,
|
|
1136
|
+
get: function () {
|
|
1137
|
+
return ɵnextTabStop;
|
|
1138
|
+
}
|
|
1139
|
+
});
|
|
1140
|
+
Object.defineProperty(exports, 'ɵnormalizeAutoScroll', {
|
|
1141
|
+
enumerable: true,
|
|
1142
|
+
get: function () {
|
|
1143
|
+
return ɵnormalizeAutoScroll;
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
Object.defineProperty(exports, 'ɵpinMessageToTop', {
|
|
1147
|
+
enumerable: true,
|
|
1148
|
+
get: function () {
|
|
1149
|
+
return ɵpinMessageToTop;
|
|
1150
|
+
}
|
|
1151
|
+
});
|
|
1152
|
+
Object.defineProperty(exports, 'ɵreadKeyboardState', {
|
|
1153
|
+
enumerable: true,
|
|
1154
|
+
get: function () {
|
|
1155
|
+
return ɵreadKeyboardState;
|
|
1156
|
+
}
|
|
1157
|
+
});
|
|
1158
|
+
Object.defineProperty(exports, 'ɵwatchElementHeight', {
|
|
1159
|
+
enumerable: true,
|
|
1160
|
+
get: function () {
|
|
1161
|
+
return ɵwatchElementHeight;
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
Object.defineProperty(exports, 'ɵwatchElementWidth', {
|
|
1165
|
+
enumerable: true,
|
|
1166
|
+
get: function () {
|
|
1167
|
+
return ɵwatchElementWidth;
|
|
1168
|
+
}
|
|
1169
|
+
});
|
|
1170
|
+
Object.defineProperty(exports, 'ɵwatchKeyboardState', {
|
|
1171
|
+
enumerable: true,
|
|
1172
|
+
get: function () {
|
|
1173
|
+
return ɵwatchKeyboardState;
|
|
1174
|
+
}
|
|
1175
|
+
});
|
|
1176
|
+
Object.defineProperty(exports, 'ɵwatchMobileViewport', {
|
|
1177
|
+
enumerable: true,
|
|
1178
|
+
get: function () {
|
|
1179
|
+
return ɵwatchMobileViewport;
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
//# sourceMappingURL=viewport-D-tuKCeb.cjs.map
|