@spartan-ng/brain 1.5.0-beta.3 → 1.5.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,1440 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { InjectionToken, inject, signal, input, computed, afterNextRender, Directive, ElementRef, Renderer2, effect, DestroyRef, booleanAttribute, untracked, Injectable, numberAttribute } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
const BrnMessageScrollerToken = new InjectionToken('BrnMessageScrollerToken');
|
|
5
|
+
function injectBrnMessageScroller() {
|
|
6
|
+
return inject(BrnMessageScrollerToken);
|
|
7
|
+
}
|
|
8
|
+
function provideBrnMessageScroller(scroller) {
|
|
9
|
+
return { provide: BrnMessageScrollerToken, useExisting: scroller };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class BrnMessageScrollerButton {
|
|
13
|
+
_scroller = injectBrnMessageScroller();
|
|
14
|
+
_pending = signal(true, ...(ngDevMode ? [{ debugName: "_pending" }] : /* istanbul ignore next */ []));
|
|
15
|
+
/**
|
|
16
|
+
* Transcript edge to scroll toward.
|
|
17
|
+
* @default 'end'
|
|
18
|
+
*/
|
|
19
|
+
direction = input('end', ...(ngDevMode ? [{ debugName: "direction" }] : /* istanbul ignore next */ []));
|
|
20
|
+
/**
|
|
21
|
+
* Native scroll behavior when clicked.
|
|
22
|
+
* @default 'smooth'
|
|
23
|
+
*/
|
|
24
|
+
behavior = input('smooth', ...(ngDevMode ? [{ debugName: "behavior" }] : /* istanbul ignore next */ []));
|
|
25
|
+
/**
|
|
26
|
+
* Accessible name for the control.
|
|
27
|
+
* When omitted, defaults from `direction` (`Scroll to end` / `Scroll to start`).
|
|
28
|
+
*/
|
|
29
|
+
ariaLabel = input(undefined, { ...(ngDevMode ? { debugName: "ariaLabel" } : /* istanbul ignore next */ {}), alias: 'aria-label' });
|
|
30
|
+
/** Whether overflow exists toward this button's direction. */
|
|
31
|
+
active = computed(() => {
|
|
32
|
+
const state = this._scroller.scrollable();
|
|
33
|
+
return this.direction() === 'start' ? state.start : state.end;
|
|
34
|
+
}, ...(ngDevMode ? [{ debugName: "active" }] : /* istanbul ignore next */ []));
|
|
35
|
+
/** Resolved accessible name (explicit `aria-label` or direction default). */
|
|
36
|
+
resolvedAriaLabel = computed(() => this.ariaLabel() ?? (this.direction() === 'end' ? 'Scroll to end' : 'Scroll to start'), ...(ngDevMode ? [{ debugName: "resolvedAriaLabel" }] : /* istanbul ignore next */ []));
|
|
37
|
+
constructor() {
|
|
38
|
+
afterNextRender(() => {
|
|
39
|
+
requestAnimationFrame(() => this._pending.set(false));
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
onClick(event) {
|
|
43
|
+
if (!this.active()) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (event.defaultPrevented) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
event.currentTarget?.blur();
|
|
50
|
+
if (this.direction() === 'start') {
|
|
51
|
+
this._scroller.scrollToStart({ behavior: this.behavior() });
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this._scroller.scrollToEnd({ behavior: this.behavior() });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerButton, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
58
|
+
/** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.2.16", type: BrnMessageScrollerButton, isStandalone: true, selector: "button[brnMessageScrollerButton]", inputs: { direction: { classPropertyName: "direction", publicName: "direction", isSignal: true, isRequired: false, transformFunction: null }, behavior: { classPropertyName: "behavior", publicName: "behavior", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "aria-label", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "type": "button" }, listeners: { "click": "onClick($event)" }, properties: { "attr.aria-label": "resolvedAriaLabel()", "attr.data-active": "active() ? \"true\" : \"false\"", "attr.data-direction": "direction()", "attr.inert": "active() ? null : \"\"", "attr.tabindex": "active() ? null : -1", "style.opacity": "_pending() ? \"0\" : null", "style.transition": "_pending() ? \"none\" : null" } }, exportAs: ["brnMessageScrollerButton"], ngImport: i0 });
|
|
59
|
+
}
|
|
60
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerButton, decorators: [{
|
|
61
|
+
type: Directive,
|
|
62
|
+
args: [{
|
|
63
|
+
selector: 'button[brnMessageScrollerButton]',
|
|
64
|
+
exportAs: 'brnMessageScrollerButton',
|
|
65
|
+
host: {
|
|
66
|
+
type: 'button',
|
|
67
|
+
'[attr.aria-label]': 'resolvedAriaLabel()',
|
|
68
|
+
'[attr.data-active]': 'active() ? "true" : "false"',
|
|
69
|
+
'[attr.data-direction]': 'direction()',
|
|
70
|
+
'[attr.inert]': 'active() ? null : ""',
|
|
71
|
+
'[attr.tabindex]': 'active() ? null : -1',
|
|
72
|
+
// Keep the control painted hidden until after the first frame so remounts
|
|
73
|
+
// (reset/demoKey) cannot flash the visible button before scroller styles win.
|
|
74
|
+
'[style.opacity]': '_pending() ? "0" : null',
|
|
75
|
+
'[style.transition]': '_pending() ? "none" : null',
|
|
76
|
+
'(click)': 'onClick($event)',
|
|
77
|
+
},
|
|
78
|
+
}]
|
|
79
|
+
}], ctorParameters: () => [], propDecorators: { direction: [{ type: i0.Input, args: [{ isSignal: true, alias: "direction", required: false }] }], behavior: [{ type: i0.Input, args: [{ isSignal: true, alias: "behavior", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "aria-label", required: false }] }] } });
|
|
80
|
+
|
|
81
|
+
class BrnMessageScrollerContent {
|
|
82
|
+
/**
|
|
83
|
+
* aria-relevant for the live log region.
|
|
84
|
+
* @default 'additions'
|
|
85
|
+
*/
|
|
86
|
+
ariaRelevant = input('additions', { ...(ngDevMode ? { debugName: "ariaRelevant" } : /* istanbul ignore next */ {}), alias: 'aria-relevant' });
|
|
87
|
+
/**
|
|
88
|
+
* Optional class name applied to the internal tail spacer.
|
|
89
|
+
*/
|
|
90
|
+
spacerClassName = input(undefined, ...(ngDevMode ? [{ debugName: "spacerClassName" }] : /* istanbul ignore next */ []));
|
|
91
|
+
constructor() {
|
|
92
|
+
const scroller = injectBrnMessageScroller();
|
|
93
|
+
const content = inject(ElementRef).nativeElement;
|
|
94
|
+
const renderer = inject(Renderer2);
|
|
95
|
+
let spacer = null;
|
|
96
|
+
let mutationObserver = null;
|
|
97
|
+
let resizeObserver = null;
|
|
98
|
+
let resizeFrame = 0;
|
|
99
|
+
scroller.setContentElement(content);
|
|
100
|
+
effect(() => {
|
|
101
|
+
const spacerClass = this.spacerClassName();
|
|
102
|
+
if (!spacer) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (spacerClass) {
|
|
106
|
+
renderer.setAttribute(spacer, 'class', spacerClass);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
renderer.removeAttribute(spacer, 'class');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
// Create the spacer after hydration. Appending it in the constructor puts an
|
|
113
|
+
// extra node in SSR HTML and breaks Angular hydration (NG0500).
|
|
114
|
+
afterNextRender(() => {
|
|
115
|
+
const nextSpacer = renderer.createElement('div');
|
|
116
|
+
renderer.setAttribute(nextSpacer, 'aria-hidden', 'true');
|
|
117
|
+
renderer.setAttribute(nextSpacer, 'data-message-scroller-spacer', '');
|
|
118
|
+
renderer.setAttribute(nextSpacer, 'hidden', '');
|
|
119
|
+
renderer.appendChild(content, nextSpacer);
|
|
120
|
+
spacer = nextSpacer;
|
|
121
|
+
scroller.setSpacerElement(nextSpacer);
|
|
122
|
+
const spacerClass = this.spacerClassName();
|
|
123
|
+
if (spacerClass) {
|
|
124
|
+
renderer.setAttribute(nextSpacer, 'class', spacerClass);
|
|
125
|
+
}
|
|
126
|
+
scroller.handleContentChange();
|
|
127
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
128
|
+
mutationObserver = new MutationObserver(() => {
|
|
129
|
+
scroller.handleContentChange();
|
|
130
|
+
});
|
|
131
|
+
mutationObserver.observe(content, { childList: true });
|
|
132
|
+
}
|
|
133
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
134
|
+
// Coalesce into rAF: handleResize mutates the spacer inside this observed
|
|
135
|
+
// element, and resizing an observed element during delivery fires
|
|
136
|
+
// "ResizeObserver loop completed with undelivered notifications".
|
|
137
|
+
resizeObserver = new ResizeObserver(() => {
|
|
138
|
+
window.cancelAnimationFrame(resizeFrame);
|
|
139
|
+
resizeFrame = window.requestAnimationFrame(() => scroller.handleResize());
|
|
140
|
+
});
|
|
141
|
+
resizeObserver.observe(content);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
inject(DestroyRef).onDestroy(() => {
|
|
145
|
+
if (typeof window !== 'undefined') {
|
|
146
|
+
window.cancelAnimationFrame(resizeFrame);
|
|
147
|
+
}
|
|
148
|
+
mutationObserver?.disconnect();
|
|
149
|
+
mutationObserver = null;
|
|
150
|
+
resizeObserver?.disconnect();
|
|
151
|
+
resizeObserver = null;
|
|
152
|
+
if (spacer) {
|
|
153
|
+
renderer.removeChild(content, spacer);
|
|
154
|
+
scroller.clearSpacerElement(spacer);
|
|
155
|
+
spacer = null;
|
|
156
|
+
}
|
|
157
|
+
scroller.clearContentElement(content);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerContent, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
161
|
+
/** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.2.16", type: BrnMessageScrollerContent, isStandalone: true, selector: "[brnMessageScrollerContent],brn-message-scroller-content", inputs: { ariaRelevant: { classPropertyName: "ariaRelevant", publicName: "aria-relevant", isSignal: true, isRequired: false, transformFunction: null }, spacerClassName: { classPropertyName: "spacerClassName", publicName: "spacerClassName", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "role": "log" }, properties: { "attr.aria-relevant": "ariaRelevant()" } }, exportAs: ["brnMessageScrollerContent"], ngImport: i0 });
|
|
162
|
+
}
|
|
163
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerContent, decorators: [{
|
|
164
|
+
type: Directive,
|
|
165
|
+
args: [{
|
|
166
|
+
selector: '[brnMessageScrollerContent],brn-message-scroller-content',
|
|
167
|
+
exportAs: 'brnMessageScrollerContent',
|
|
168
|
+
host: {
|
|
169
|
+
role: 'log',
|
|
170
|
+
'[attr.aria-relevant]': 'ariaRelevant()',
|
|
171
|
+
},
|
|
172
|
+
}]
|
|
173
|
+
}], ctorParameters: () => [], propDecorators: { ariaRelevant: [{ type: i0.Input, args: [{ isSignal: true, alias: "aria-relevant", required: false }] }], spacerClassName: [{ type: i0.Input, args: [{ isSignal: true, alias: "spacerClassName", required: false }] }] } });
|
|
174
|
+
|
|
175
|
+
class BrnMessageScrollerItem {
|
|
176
|
+
/**
|
|
177
|
+
* Stable row id for scrollToMessage, visibility, and prepend preservation.
|
|
178
|
+
*/
|
|
179
|
+
messageId = input(undefined, ...(ngDevMode ? [{ debugName: "messageId" }] : /* istanbul ignore next */ []));
|
|
180
|
+
/**
|
|
181
|
+
* Marks a turn boundary that newly appended anchors and last-anchor restore use.
|
|
182
|
+
* @default false
|
|
183
|
+
*/
|
|
184
|
+
scrollAnchor = input(false, { ...(ngDevMode ? { debugName: "scrollAnchor" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
185
|
+
constructor() {
|
|
186
|
+
const scroller = injectBrnMessageScroller();
|
|
187
|
+
const elementRef = inject(ElementRef);
|
|
188
|
+
let registeredMessageId;
|
|
189
|
+
let registeredElement = null;
|
|
190
|
+
effect(() => {
|
|
191
|
+
const messageId = this.messageId();
|
|
192
|
+
const element = elementRef.nativeElement;
|
|
193
|
+
untracked(() => {
|
|
194
|
+
if (registeredMessageId && registeredElement) {
|
|
195
|
+
scroller.registerMessage(registeredMessageId, null, registeredElement);
|
|
196
|
+
registeredMessageId = undefined;
|
|
197
|
+
registeredElement = null;
|
|
198
|
+
}
|
|
199
|
+
if (messageId) {
|
|
200
|
+
scroller.registerMessage(messageId, element);
|
|
201
|
+
registeredMessageId = messageId;
|
|
202
|
+
registeredElement = element;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
inject(DestroyRef).onDestroy(() => {
|
|
207
|
+
if (registeredMessageId && registeredElement) {
|
|
208
|
+
scroller.registerMessage(registeredMessageId, null, registeredElement);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerItem, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
213
|
+
/** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.2.16", type: BrnMessageScrollerItem, isStandalone: true, selector: "[brnMessageScrollerItem],brn-message-scroller-item", inputs: { messageId: { classPropertyName: "messageId", publicName: "messageId", isSignal: true, isRequired: false, transformFunction: null }, scrollAnchor: { classPropertyName: "scrollAnchor", publicName: "scrollAnchor", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.data-message-id": "messageId() || null", "attr.data-scroll-anchor": "scrollAnchor() ? \"true\" : \"false\"" } }, exportAs: ["brnMessageScrollerItem"], ngImport: i0 });
|
|
214
|
+
}
|
|
215
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerItem, decorators: [{
|
|
216
|
+
type: Directive,
|
|
217
|
+
args: [{
|
|
218
|
+
selector: '[brnMessageScrollerItem],brn-message-scroller-item',
|
|
219
|
+
exportAs: 'brnMessageScrollerItem',
|
|
220
|
+
host: {
|
|
221
|
+
'[attr.data-message-id]': 'messageId() || null',
|
|
222
|
+
'[attr.data-scroll-anchor]': 'scrollAnchor() ? "true" : "false"',
|
|
223
|
+
},
|
|
224
|
+
}]
|
|
225
|
+
}], ctorParameters: () => [], propDecorators: { messageId: [{ type: i0.Input, args: [{ isSignal: true, alias: "messageId", required: false }] }], scrollAnchor: [{ type: i0.Input, args: [{ isSignal: true, alias: "scrollAnchor", required: false }] }] } });
|
|
226
|
+
|
|
227
|
+
function areScrollStatesEqual(current, next) {
|
|
228
|
+
return current.start === next.start && current.end === next.end;
|
|
229
|
+
}
|
|
230
|
+
function areVisibilityStatesEqual(current, next) {
|
|
231
|
+
if (current.currentAnchorId !== next.currentAnchorId) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
if (current.visibleMessageIds.length !== next.visibleMessageIds.length) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
return current.visibleMessageIds.every((messageId, index) => messageId === next.visibleMessageIds[index]);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Default scrollEdgeThreshold. Sub-pixel tolerance so edge detection does not
|
|
241
|
+
// flicker across engines that round scrollTop differently.
|
|
242
|
+
const DEFAULT_SCROLL_EDGE_THRESHOLD = 8;
|
|
243
|
+
// Default scrollPreviousItemPeek. Pixels of the previous item kept visible above
|
|
244
|
+
// a newly anchored row.
|
|
245
|
+
const DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK = 64;
|
|
246
|
+
// Default scrollMargin for scrollToMessage and programmatic targets.
|
|
247
|
+
const DEFAULT_SCROLL_MARGIN = 0;
|
|
248
|
+
// Two fractional scrollTop values within this range are treated as equal, to
|
|
249
|
+
// absorb zoom and HiDPI rounding drift.
|
|
250
|
+
const SCROLL_POSITION_EPSILON = 0.5;
|
|
251
|
+
// How long (ms) data-autoscrolling stays set during a programmatic smooth scroll
|
|
252
|
+
// before clearing.
|
|
253
|
+
const AUTOSCROLLING_CLEAR_DELAY = 180;
|
|
254
|
+
// Viewport keys that count as deliberate scroll intent and release follow-bottom.
|
|
255
|
+
const USER_SCROLL_KEYS = new Set([
|
|
256
|
+
'ArrowDown',
|
|
257
|
+
'ArrowUp',
|
|
258
|
+
'End',
|
|
259
|
+
'Home',
|
|
260
|
+
'PageDown',
|
|
261
|
+
'PageUp',
|
|
262
|
+
' ', // Space key.
|
|
263
|
+
]);
|
|
264
|
+
// Initial BrnMessageScrollerScrollable before measurement. Stable reference for the
|
|
265
|
+
// server and first-render snapshot.
|
|
266
|
+
const EMPTY_MESSAGE_SCROLLER_SCROLLABLE = {
|
|
267
|
+
start: false,
|
|
268
|
+
end: false,
|
|
269
|
+
};
|
|
270
|
+
// Shared empty array so empty visibility snapshots stay referentially stable.
|
|
271
|
+
const EMPTY_VISIBLE_MESSAGE_IDS = [];
|
|
272
|
+
// Initial BrnMessageScrollerVisibilityState. Nothing tracked, no current anchor.
|
|
273
|
+
const EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE = {
|
|
274
|
+
currentAnchorId: null,
|
|
275
|
+
visibleMessageIds: EMPTY_VISIBLE_MESSAGE_IDS,
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
function getMessageScrollerScrollable({ content, scrollEdgeThreshold, spacer, viewport, }) {
|
|
279
|
+
if (!viewport || !content || typeof viewport.getBoundingClientRect !== 'function') {
|
|
280
|
+
return EMPTY_MESSAGE_SCROLLER_SCROLLABLE;
|
|
281
|
+
}
|
|
282
|
+
const contentBottom = getContentBottom({ content, spacer, viewport });
|
|
283
|
+
return {
|
|
284
|
+
start: viewport.scrollTop > scrollEdgeThreshold,
|
|
285
|
+
end: contentBottom - viewport.scrollTop - viewport.clientHeight > scrollEdgeThreshold,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function getMessageScrollerVisibilityState({ content, scrollMargin, scrollPreviousItemPeek, spacer, viewport, visibleMessageIds, }) {
|
|
289
|
+
if (!content || !viewport || typeof viewport.getBoundingClientRect !== 'function') {
|
|
290
|
+
return EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE;
|
|
291
|
+
}
|
|
292
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
293
|
+
// The reading line sits scrollPreviousItemPeek below scrollMargin: anchored
|
|
294
|
+
// turns land there with the previous turn peeking above. A row only peeking in
|
|
295
|
+
// that band has not been read down to yet, so it counts as neither visible nor
|
|
296
|
+
// current.
|
|
297
|
+
const lineTop = viewportRect.top + scrollMargin + scrollPreviousItemPeek;
|
|
298
|
+
const trackByLayout = typeof IntersectionObserver === 'undefined';
|
|
299
|
+
const visible = [];
|
|
300
|
+
let currentAnchorId = null;
|
|
301
|
+
// Walk rows in document order so visible ids come out top-to-bottom.
|
|
302
|
+
for (const item of getMessageScrollerItems(content, spacer)) {
|
|
303
|
+
const messageId = item.dataset['messageId'];
|
|
304
|
+
if (!messageId) {
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const isAnchor = item.dataset['scrollAnchor'] === 'true';
|
|
308
|
+
// Anchors need a rect to place the current line; non-anchors lean on the
|
|
309
|
+
// observer set (or a rect in the no-observer fallback).
|
|
310
|
+
const rect = isAnchor || trackByLayout ? item.getBoundingClientRect() : null;
|
|
311
|
+
const isVisible = trackByLayout && rect
|
|
312
|
+
? rect.bottom > lineTop && rect.top < viewportRect.bottom
|
|
313
|
+
: visibleMessageIds.has(messageId);
|
|
314
|
+
if (isVisible) {
|
|
315
|
+
visible.push(messageId);
|
|
316
|
+
}
|
|
317
|
+
// Current is the last anchor to have reached the reading line: the turn you
|
|
318
|
+
// scrolled to (placed at the line) wins over newer turns lower down, the
|
|
319
|
+
// previous turn peeking above the line has been passed, and it stays current
|
|
320
|
+
// even after its header scrolls above the viewport.
|
|
321
|
+
if (isAnchor && rect && rect.top <= lineTop + SCROLL_POSITION_EPSILON) {
|
|
322
|
+
currentAnchorId = messageId;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (visible.length === 0 && currentAnchorId === null) {
|
|
326
|
+
return EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE;
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
currentAnchorId,
|
|
330
|
+
visibleMessageIds: visible,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function getMessageScrollerItems(content, spacer) {
|
|
334
|
+
return Array.from(content.children).filter((child) => child instanceof HTMLElement && child !== spacer);
|
|
335
|
+
}
|
|
336
|
+
function getNewScrollAnchor(items, previousItemCount) {
|
|
337
|
+
for (let index = previousItemCount; index < items.length; index++) {
|
|
338
|
+
const item = items[index];
|
|
339
|
+
if (item?.dataset['scrollAnchor'] === 'true') {
|
|
340
|
+
return item;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
function getUnanchoredScrollAnchor(items, handledAnchors) {
|
|
346
|
+
for (const item of items) {
|
|
347
|
+
if (item.dataset['scrollAnchor'] === 'true' && !handledAnchors.has(item)) {
|
|
348
|
+
return item;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
function hasMultipleNewScrollAnchors(items, previousItemCount) {
|
|
354
|
+
let count = 0;
|
|
355
|
+
for (let index = previousItemCount; index < items.length; index++) {
|
|
356
|
+
const item = items[index];
|
|
357
|
+
if (item?.dataset['scrollAnchor'] !== 'true') {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
count += 1;
|
|
361
|
+
if (count > 1) {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
function getLastScrollAnchor(items) {
|
|
368
|
+
for (let index = items.length - 1; index >= 0; index--) {
|
|
369
|
+
const item = items[index];
|
|
370
|
+
if (item?.dataset['scrollAnchor'] === 'true') {
|
|
371
|
+
return item;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
function getFirstVisibleMessageItem({ content, spacer, viewport, }) {
|
|
377
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
378
|
+
for (const item of getMessageScrollerItems(content, spacer)) {
|
|
379
|
+
if (!item.dataset['messageId']) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
const rect = item.getBoundingClientRect();
|
|
383
|
+
if (rect.bottom > viewportRect.top && rect.top < viewportRect.bottom) {
|
|
384
|
+
return item;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
function getElementScrollTop({ align, element, scrollMargin, spacer, viewport, }) {
|
|
390
|
+
const elementTop = getElementTop(element, viewport);
|
|
391
|
+
const elementHeight = element.getBoundingClientRect().height;
|
|
392
|
+
const contentPadding = getContentBlockPadding(spacer);
|
|
393
|
+
if (align === 'center') {
|
|
394
|
+
const insetHeight = Math.max(0, viewport.clientHeight - contentPadding.start - contentPadding.end);
|
|
395
|
+
return elementTop - contentPadding.start - (insetHeight - elementHeight) / 2 - scrollMargin;
|
|
396
|
+
}
|
|
397
|
+
if (align === 'end') {
|
|
398
|
+
return elementTop - viewport.clientHeight + elementHeight + contentPadding.end + scrollMargin;
|
|
399
|
+
}
|
|
400
|
+
if (align === 'nearest') {
|
|
401
|
+
const elementBottom = elementTop + elementHeight;
|
|
402
|
+
const viewportTop = viewport.scrollTop + contentPadding.start;
|
|
403
|
+
const viewportBottom = viewport.scrollTop + viewport.clientHeight - contentPadding.end;
|
|
404
|
+
if (elementTop >= viewportTop && elementBottom <= viewportBottom) {
|
|
405
|
+
return viewport.scrollTop;
|
|
406
|
+
}
|
|
407
|
+
if (elementTop < viewportTop) {
|
|
408
|
+
return elementTop - contentPadding.start - scrollMargin;
|
|
409
|
+
}
|
|
410
|
+
return elementBottom - viewport.clientHeight + contentPadding.end + scrollMargin;
|
|
411
|
+
}
|
|
412
|
+
return elementTop - contentPadding.start - scrollMargin;
|
|
413
|
+
}
|
|
414
|
+
function getElementTop(element, viewport) {
|
|
415
|
+
const elementRect = element.getBoundingClientRect();
|
|
416
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
417
|
+
return elementRect.top - viewportRect.top + viewport.scrollTop;
|
|
418
|
+
}
|
|
419
|
+
function getElementViewportTop(element, viewport) {
|
|
420
|
+
return element.getBoundingClientRect().top - viewport.getBoundingClientRect().top;
|
|
421
|
+
}
|
|
422
|
+
function getTailSpacerHeight({ content, scrollTop, spacer, viewport, }) {
|
|
423
|
+
const contentBottom = getContentBottom({ content, spacer, viewport });
|
|
424
|
+
return scrollTop + viewport.clientHeight - contentBottom;
|
|
425
|
+
}
|
|
426
|
+
function getContentBottom({ content, spacer, viewport, }) {
|
|
427
|
+
const items = getMessageScrollerItems(content, spacer);
|
|
428
|
+
const padding = getBlockPadding(content);
|
|
429
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
430
|
+
const scrollTop = viewport.scrollTop;
|
|
431
|
+
let contentBottom = padding.start + padding.end;
|
|
432
|
+
for (const item of items) {
|
|
433
|
+
const rect = item.getBoundingClientRect();
|
|
434
|
+
contentBottom = Math.max(contentBottom, rect.bottom - viewportRect.top + scrollTop + padding.end);
|
|
435
|
+
}
|
|
436
|
+
return contentBottom;
|
|
437
|
+
}
|
|
438
|
+
function getMaxScrollTop(viewport) {
|
|
439
|
+
return Math.max(0, viewport.scrollHeight - viewport.clientHeight);
|
|
440
|
+
}
|
|
441
|
+
function getBlockPadding(element) {
|
|
442
|
+
if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
|
|
443
|
+
return {
|
|
444
|
+
end: 0,
|
|
445
|
+
start: 0,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
const style = window.getComputedStyle(element);
|
|
449
|
+
return {
|
|
450
|
+
end: readCssPixel(style.paddingBlockEnd || style.paddingBottom),
|
|
451
|
+
start: readCssPixel(style.paddingBlockStart || style.paddingTop),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
function getContentBlockPadding(spacer) {
|
|
455
|
+
const content = spacer?.parentElement;
|
|
456
|
+
if (!content) {
|
|
457
|
+
return {
|
|
458
|
+
end: 0,
|
|
459
|
+
start: 0,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
return getBlockPadding(content);
|
|
463
|
+
}
|
|
464
|
+
function getFlexGap(element) {
|
|
465
|
+
if (!element || typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') {
|
|
466
|
+
return 0;
|
|
467
|
+
}
|
|
468
|
+
const style = window.getComputedStyle(element);
|
|
469
|
+
const gap = style.rowGap === 'normal' ? style.gap : style.rowGap;
|
|
470
|
+
return readCssPixel(gap);
|
|
471
|
+
}
|
|
472
|
+
function readCssPixel(value) {
|
|
473
|
+
if (!value) {
|
|
474
|
+
return 0;
|
|
475
|
+
}
|
|
476
|
+
const number = Number.parseFloat(value);
|
|
477
|
+
return Number.isFinite(number) ? number : 0;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Headless message-scroller controller. Owns scroll behavior and state; renders no DOM.
|
|
482
|
+
* Combine refs, commands, and controller policy from the shadcn React implementation.
|
|
483
|
+
*/
|
|
484
|
+
class BrnMessageScroller {
|
|
485
|
+
_autoScroll = false;
|
|
486
|
+
_autoscrolling = false;
|
|
487
|
+
_autoscrollingTimeout = null;
|
|
488
|
+
_streamingTurn = null;
|
|
489
|
+
_content = null;
|
|
490
|
+
_defaultScrollPosition = 'end';
|
|
491
|
+
_defaultScrollPositionApplied = false;
|
|
492
|
+
_firstItem = null;
|
|
493
|
+
_itemCount = 0;
|
|
494
|
+
_lastScrollTop = 0;
|
|
495
|
+
_messageElements = new Map();
|
|
496
|
+
_mode = 'free-scrolling';
|
|
497
|
+
_pendingScrollFrame = null;
|
|
498
|
+
_pendingScrollToMessage = null;
|
|
499
|
+
_prependRestore = null;
|
|
500
|
+
_preserveScrollOnPrepend = true;
|
|
501
|
+
_root = null;
|
|
502
|
+
_scrollEdgeThreshold = DEFAULT_SCROLL_EDGE_THRESHOLD;
|
|
503
|
+
_scrollMargin = DEFAULT_SCROLL_MARGIN;
|
|
504
|
+
_scrollPreviousItemPeek = DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK;
|
|
505
|
+
_spacerGap = 0;
|
|
506
|
+
_spacerHeight = 0;
|
|
507
|
+
_spacer = null;
|
|
508
|
+
/** Height queued before the spacer mounts (created after hydration). */
|
|
509
|
+
_pendingSpacerHeight = null;
|
|
510
|
+
_stateFrame = null;
|
|
511
|
+
_viewport = null;
|
|
512
|
+
_visibilityFrame = null;
|
|
513
|
+
_visibilityObserver = null;
|
|
514
|
+
_visibilityTracking = false;
|
|
515
|
+
_visibleMessageIds = new Set();
|
|
516
|
+
_handledScrollAnchors = new WeakSet();
|
|
517
|
+
_destroyed = false;
|
|
518
|
+
_scrollable = signal(EMPTY_MESSAGE_SCROLLER_SCROLLABLE, ...(ngDevMode ? [{ debugName: "_scrollable" }] : /* istanbul ignore next */ []));
|
|
519
|
+
_visibility = signal(EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE, ...(ngDevMode ? [{ debugName: "_visibility" }] : /* istanbul ignore next */ []));
|
|
520
|
+
/** Scrollable edges snapshot (start / end overflow). */
|
|
521
|
+
scrollable = this._scrollable.asReadonly();
|
|
522
|
+
/** Visibility snapshot (current anchor + visible message ids). Requires observeVisibility(). */
|
|
523
|
+
visibility = this._visibility.asReadonly();
|
|
524
|
+
/**
|
|
525
|
+
* @internal
|
|
526
|
+
* Whether prepend scroll preservation is enabled (mirrored from viewport).
|
|
527
|
+
*/
|
|
528
|
+
get preserveScrollOnPrepend() {
|
|
529
|
+
return this._preserveScrollOnPrepend;
|
|
530
|
+
}
|
|
531
|
+
/** @internal */
|
|
532
|
+
set preserveScrollOnPrepend(value) {
|
|
533
|
+
this._preserveScrollOnPrepend = value;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* @internal
|
|
537
|
+
* Apply provider configuration. Safe to call when inputs change.
|
|
538
|
+
*/
|
|
539
|
+
configure(props) {
|
|
540
|
+
if (this._destroyed) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
const previousAutoScroll = this._autoScroll;
|
|
544
|
+
const previousDefaultScrollPosition = this._defaultScrollPosition;
|
|
545
|
+
const previousScrollEdgeThreshold = this._scrollEdgeThreshold;
|
|
546
|
+
const previousScrollMargin = this._scrollMargin;
|
|
547
|
+
const previousScrollPreviousItemPeek = this._scrollPreviousItemPeek;
|
|
548
|
+
if (props.autoScroll !== undefined) {
|
|
549
|
+
this._autoScroll = props.autoScroll;
|
|
550
|
+
// Mirror React's initial modeRef: autoScroll starts in following-bottom.
|
|
551
|
+
if (this._autoScroll && this._itemCount === 0) {
|
|
552
|
+
this._mode = 'following-bottom';
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (props.defaultScrollPosition !== undefined) {
|
|
556
|
+
this._defaultScrollPosition = props.defaultScrollPosition;
|
|
557
|
+
}
|
|
558
|
+
if (props.scrollEdgeThreshold !== undefined) {
|
|
559
|
+
this._scrollEdgeThreshold = props.scrollEdgeThreshold;
|
|
560
|
+
}
|
|
561
|
+
if (props.scrollPreviousItemPeek !== undefined) {
|
|
562
|
+
this._scrollPreviousItemPeek = props.scrollPreviousItemPeek;
|
|
563
|
+
}
|
|
564
|
+
if (props.scrollMargin !== undefined) {
|
|
565
|
+
this._scrollMargin = props.scrollMargin;
|
|
566
|
+
}
|
|
567
|
+
if (previousDefaultScrollPosition !== this._defaultScrollPosition) {
|
|
568
|
+
this._defaultScrollPositionApplied = false;
|
|
569
|
+
this.applyDefaultScrollPosition();
|
|
570
|
+
}
|
|
571
|
+
// IntersectionObserver rootMargin is fixed at construction. Rebuild when the
|
|
572
|
+
// reading-line inputs change so visibleMessageIds stay aligned with geometry.
|
|
573
|
+
const visibilityMarginsChanged = this._scrollMargin !== previousScrollMargin || this._scrollPreviousItemPeek !== previousScrollPreviousItemPeek;
|
|
574
|
+
if (this._visibilityTracking && visibilityMarginsChanged) {
|
|
575
|
+
this._visibilityObserver?.disconnect();
|
|
576
|
+
this._visibilityObserver = null;
|
|
577
|
+
this.observeVisibility();
|
|
578
|
+
}
|
|
579
|
+
if (props.autoScroll !== undefined && props.autoScroll !== previousAutoScroll) {
|
|
580
|
+
if (this._autoScroll && this._mode === 'following-bottom' && this._itemCount > 0) {
|
|
581
|
+
this.scrollToEnd({ behavior: 'auto' });
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
this.commitScrollState();
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
// Edge threshold feeds data-scrollable / jump-button active state.
|
|
588
|
+
if (this._scrollEdgeThreshold !== previousScrollEdgeThreshold) {
|
|
589
|
+
this.commitScrollState();
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Scroll a registered `messageId` into view.
|
|
594
|
+
* Queues the request when the item is not mounted yet (empty transcript).
|
|
595
|
+
* @returns false when the id is unknown and items are already present
|
|
596
|
+
*/
|
|
597
|
+
scrollToMessage(messageId, options) {
|
|
598
|
+
if (this._destroyed) {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
const element = this._messageElements.get(messageId);
|
|
602
|
+
if (!element) {
|
|
603
|
+
if (this._itemCount === 0) {
|
|
604
|
+
this._pendingScrollToMessage = {
|
|
605
|
+
messageId,
|
|
606
|
+
options,
|
|
607
|
+
};
|
|
608
|
+
this._defaultScrollPositionApplied = true;
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
this._defaultScrollPositionApplied = true;
|
|
614
|
+
if (this.scrollToElement(element, options)) {
|
|
615
|
+
this._pendingScrollToMessage = null;
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
this._pendingScrollToMessage = {
|
|
619
|
+
messageId,
|
|
620
|
+
options,
|
|
621
|
+
};
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Jump to the live end of the transcript.
|
|
626
|
+
* With `autoScroll`, re-enters following-bottom mode.
|
|
627
|
+
*/
|
|
628
|
+
scrollToEnd({ behavior = 'auto' } = {}) {
|
|
629
|
+
if (this._destroyed) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
const viewport = this._viewport;
|
|
633
|
+
if (!viewport) {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
this.setTailSpacerHeight(0);
|
|
637
|
+
this._streamingTurn = null;
|
|
638
|
+
this._mode = this._autoScroll ? 'following-bottom' : 'free-scrolling';
|
|
639
|
+
this.scrollToPosition(getMaxScrollTop(viewport), {
|
|
640
|
+
autoscrolling: true,
|
|
641
|
+
behavior,
|
|
642
|
+
});
|
|
643
|
+
this.scheduleVisibilitySync();
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
/** Jump to the start of the transcript and leave follow/anchor modes. */
|
|
647
|
+
scrollToStart({ behavior = 'auto' } = {}) {
|
|
648
|
+
if (this._destroyed || !this._viewport) {
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
this.setTailSpacerHeight(0);
|
|
652
|
+
this._streamingTurn = null;
|
|
653
|
+
this._mode = 'free-scrolling';
|
|
654
|
+
this.scrollToPosition(0, { behavior });
|
|
655
|
+
this.scheduleVisibilitySync();
|
|
656
|
+
return true;
|
|
657
|
+
}
|
|
658
|
+
/** @internal */
|
|
659
|
+
registerMessage = (messageId, element, removedElement) => {
|
|
660
|
+
if (this._destroyed) {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (element) {
|
|
664
|
+
this._messageElements.set(messageId, element);
|
|
665
|
+
this._visibilityObserver?.observe(element);
|
|
666
|
+
this.scheduleVisibilitySync();
|
|
667
|
+
if (this._pendingScrollToMessage?.messageId === messageId) {
|
|
668
|
+
this.schedulePendingScrollToMessageFlush();
|
|
669
|
+
}
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
if (removedElement && this._messageElements.get(messageId) === removedElement) {
|
|
673
|
+
this._messageElements.delete(messageId);
|
|
674
|
+
this._visibleMessageIds.delete(messageId);
|
|
675
|
+
this._visibilityObserver?.unobserve(removedElement);
|
|
676
|
+
this.scheduleVisibilitySync();
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
/** @internal */
|
|
680
|
+
handleContentChange() {
|
|
681
|
+
if (this._destroyed) {
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
const content = this._content;
|
|
685
|
+
if (!content) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const items = getMessageScrollerItems(content, this._spacer);
|
|
689
|
+
const previousItemCount = this._itemCount;
|
|
690
|
+
const previousFirstItem = this._firstItem;
|
|
691
|
+
this._itemCount = items.length;
|
|
692
|
+
this._firstItem = items[0] ?? null;
|
|
693
|
+
// Reconcile the scroll position with the new content. Every path re-captures
|
|
694
|
+
// the prepend anchor afterward, so each branch just returns.
|
|
695
|
+
//
|
|
696
|
+
// Branch order is load-bearing: first-content, prepended, appended, updated.
|
|
697
|
+
const reconcileScrollPosition = () => {
|
|
698
|
+
if (this.flushPendingScrollToMessage()) {
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
if (previousItemCount === 0) {
|
|
702
|
+
if (this.applyDefaultScrollPosition()) {
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
if (items.length > 0 && this._autoScroll && this.scrollToEnd({ behavior: 'auto' })) {
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
this.commitScrollState();
|
|
709
|
+
this.scheduleVisibilitySync();
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
const previousFirstItemIndex = previousFirstItem ? items.indexOf(previousFirstItem) : -1;
|
|
713
|
+
const didPrepend = this._preserveScrollOnPrepend && previousFirstItemIndex > 0;
|
|
714
|
+
if (didPrepend) {
|
|
715
|
+
// Prepended rows are not new appends. Restore the prior scroll position.
|
|
716
|
+
// The restore is a no-op where native scroll anchoring already did it.
|
|
717
|
+
this.restorePrependedAnchor();
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
if (items.length > previousItemCount) {
|
|
721
|
+
const anchor = getNewScrollAnchor(items, previousItemCount);
|
|
722
|
+
if (anchor) {
|
|
723
|
+
// Following-bottom + multiple new anchors: stay at end (don't yank to the first).
|
|
724
|
+
if (this._autoScroll &&
|
|
725
|
+
this._mode === 'following-bottom' &&
|
|
726
|
+
hasMultipleNewScrollAnchors(items, previousItemCount)) {
|
|
727
|
+
this.scrollToEnd({ behavior: 'auto' });
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
this.scrollToElement(anchor, { align: 'start' }, { keepPreviousPeek: true });
|
|
731
|
+
this._handledScrollAnchors.add(anchor);
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (items.length === previousItemCount) {
|
|
736
|
+
const anchor = getUnanchoredScrollAnchor(items, this._handledScrollAnchors);
|
|
737
|
+
if (anchor) {
|
|
738
|
+
this.scrollToElement(anchor, { align: 'start' }, { keepPreviousPeek: true });
|
|
739
|
+
this._handledScrollAnchors.add(anchor);
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
// Appends with no new anchor (and content-only updates) fall through here:
|
|
744
|
+
// keep following the end if we still are, otherwise just recommit state.
|
|
745
|
+
if (this._mode === 'following-bottom' && this._autoScroll) {
|
|
746
|
+
this.scrollToEnd({ behavior: 'auto' });
|
|
747
|
+
}
|
|
748
|
+
else {
|
|
749
|
+
this.commitScrollState();
|
|
750
|
+
this.scheduleVisibilitySync();
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
reconcileScrollPosition();
|
|
754
|
+
this.capturePrependAnchor();
|
|
755
|
+
}
|
|
756
|
+
/** @internal */
|
|
757
|
+
handleResize() {
|
|
758
|
+
if (this._destroyed) {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
if (this._mode === 'following-bottom' && this._autoScroll) {
|
|
762
|
+
this.scrollToEnd({ behavior: 'auto' });
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
// Re-pin the anchored turn while content below it resizes (streaming reply).
|
|
766
|
+
const previousSpacerHeight = this._spacerHeight;
|
|
767
|
+
if (this.reanchorToAnchoredMessage()) {
|
|
768
|
+
// Spacer drained (>0 → 0): reply filled the viewport — hand off to follow-bottom.
|
|
769
|
+
// Skip when there was never a spacer (tall turn) so we don't yank off the hold.
|
|
770
|
+
if (this._autoScroll && previousSpacerHeight > 0 && this._spacerHeight === 0) {
|
|
771
|
+
this.scrollToEnd({ behavior: 'auto' });
|
|
772
|
+
}
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
this.scheduleStateCommit();
|
|
776
|
+
this.scheduleVisibilitySync();
|
|
777
|
+
}
|
|
778
|
+
/** @internal */
|
|
779
|
+
syncAfterScroll() {
|
|
780
|
+
if (this._destroyed) {
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
this.commitScrollState();
|
|
784
|
+
this.scheduleVisibilitySync();
|
|
785
|
+
this.capturePrependAnchor();
|
|
786
|
+
}
|
|
787
|
+
/** @internal */
|
|
788
|
+
userScrollIntent() {
|
|
789
|
+
if (this._destroyed) {
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
if (this._mode === 'following-bottom' || this._mode === 'anchored-to-message' || this._mode === 'settling-jump') {
|
|
793
|
+
// User gesture wins over follow / anchor / in-flight jump.
|
|
794
|
+
this._streamingTurn = null;
|
|
795
|
+
this._mode = 'free-scrolling';
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Start tracking `visibility` (current anchor + visible message ids).
|
|
800
|
+
* Uses IntersectionObserver when available; otherwise falls back to layout.
|
|
801
|
+
*/
|
|
802
|
+
observeVisibility() {
|
|
803
|
+
if (this._destroyed) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
this._visibilityTracking = true;
|
|
807
|
+
const viewport = this._viewport;
|
|
808
|
+
if (!viewport) {
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
812
|
+
this.scheduleVisibilitySync();
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
if (!this._visibilityObserver) {
|
|
816
|
+
this._visibilityObserver = new IntersectionObserver((entries) => {
|
|
817
|
+
for (const entry of entries) {
|
|
818
|
+
const messageId = entry.target.dataset['messageId'];
|
|
819
|
+
if (!messageId) {
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
if (entry.isIntersecting) {
|
|
823
|
+
this._visibleMessageIds.add(messageId);
|
|
824
|
+
}
|
|
825
|
+
else {
|
|
826
|
+
this._visibleMessageIds.delete(messageId);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
this.scheduleVisibilitySync();
|
|
830
|
+
}, {
|
|
831
|
+
root: viewport,
|
|
832
|
+
// Exclude the peek band above the reading line from "visible".
|
|
833
|
+
rootMargin: `${-(this._scrollMargin + this._scrollPreviousItemPeek)}px 0px 0px 0px`,
|
|
834
|
+
threshold: [0, 0.01, 0.5, 1],
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
this._messageElements.forEach((element) => {
|
|
838
|
+
this._visibilityObserver?.observe(element);
|
|
839
|
+
});
|
|
840
|
+
this.scheduleVisibilitySync();
|
|
841
|
+
}
|
|
842
|
+
/** Stop visibility tracking and reset the `visibility` signal. */
|
|
843
|
+
unobserveVisibility() {
|
|
844
|
+
if (this._visibilityFrame !== null) {
|
|
845
|
+
window.cancelAnimationFrame(this._visibilityFrame);
|
|
846
|
+
this._visibilityFrame = null;
|
|
847
|
+
}
|
|
848
|
+
this._visibilityObserver?.disconnect();
|
|
849
|
+
this._visibilityObserver = null;
|
|
850
|
+
this._visibleMessageIds.clear();
|
|
851
|
+
this._visibilityTracking = false;
|
|
852
|
+
this._visibility.set(EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE);
|
|
853
|
+
}
|
|
854
|
+
/** @internal */
|
|
855
|
+
setRootElement(element) {
|
|
856
|
+
// Ignore null clears on remount: Angular builds the new tree before destroying the old.
|
|
857
|
+
if (element === null) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
this._root = element;
|
|
861
|
+
this.writeStateAttributes(this._scrollable());
|
|
862
|
+
}
|
|
863
|
+
/** @internal */
|
|
864
|
+
clearRootElement(element) {
|
|
865
|
+
if (this._root === element) {
|
|
866
|
+
this._root = null;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
/** @internal */
|
|
870
|
+
setViewportElement(element) {
|
|
871
|
+
if (element === null) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
this._viewport = element;
|
|
875
|
+
this.writeStateAttributes(this._scrollable());
|
|
876
|
+
// Visibility may have been requested before the viewport mounted.
|
|
877
|
+
if (this._visibilityTracking) {
|
|
878
|
+
this._visibilityObserver?.disconnect();
|
|
879
|
+
this._visibilityObserver = null;
|
|
880
|
+
this.observeVisibility();
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
/** @internal */
|
|
884
|
+
clearViewportElement(element) {
|
|
885
|
+
if (this._viewport !== element) {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
this._visibilityObserver?.disconnect();
|
|
889
|
+
this._visibilityObserver = null;
|
|
890
|
+
this._viewport = null;
|
|
891
|
+
}
|
|
892
|
+
/** @internal */
|
|
893
|
+
setContentElement(element) {
|
|
894
|
+
if (element === null) {
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
if (this._content === element) {
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
this._content = element;
|
|
901
|
+
// Provider outlives keyed scroller remounts — reset reconcile counters so the
|
|
902
|
+
// next tree is treated as first content again (defaultScrollPosition, anchors).
|
|
903
|
+
this.resetForContentMount();
|
|
904
|
+
}
|
|
905
|
+
/** @internal */
|
|
906
|
+
clearContentElement(element) {
|
|
907
|
+
if (this._content === element) {
|
|
908
|
+
this._content = null;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
/** @internal */
|
|
912
|
+
setSpacerElement(element) {
|
|
913
|
+
if (element === null) {
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
this._spacer = element;
|
|
917
|
+
this._spacerGap = getFlexGap(element.parentElement ?? null);
|
|
918
|
+
if (this._pendingSpacerHeight !== null) {
|
|
919
|
+
const pendingHeight = this._pendingSpacerHeight;
|
|
920
|
+
this._pendingSpacerHeight = null;
|
|
921
|
+
// Force apply even when the logical height already matches.
|
|
922
|
+
this._spacerHeight = -1;
|
|
923
|
+
this.setTailSpacerHeight(pendingHeight);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
/** @internal */
|
|
927
|
+
clearSpacerElement(element) {
|
|
928
|
+
if (this._spacer === element) {
|
|
929
|
+
this._spacer = null;
|
|
930
|
+
this._spacerGap = 0;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
resetForContentMount() {
|
|
934
|
+
this._itemCount = 0;
|
|
935
|
+
this._firstItem = null;
|
|
936
|
+
this._defaultScrollPositionApplied = false;
|
|
937
|
+
this._streamingTurn = null;
|
|
938
|
+
this._mode = this._autoScroll ? 'following-bottom' : 'free-scrolling';
|
|
939
|
+
this._spacerHeight = 0;
|
|
940
|
+
this._pendingSpacerHeight = null;
|
|
941
|
+
this._prependRestore = null;
|
|
942
|
+
this._pendingScrollToMessage = null;
|
|
943
|
+
this._handledScrollAnchors = new WeakSet();
|
|
944
|
+
this._lastScrollTop = 0;
|
|
945
|
+
// Drop stale overflow flags so a remounted scroll button does not flash active.
|
|
946
|
+
this._scrollable.set(EMPTY_MESSAGE_SCROLLER_SCROLLABLE);
|
|
947
|
+
this.writeStateAttributes(EMPTY_MESSAGE_SCROLLER_SCROLLABLE);
|
|
948
|
+
}
|
|
949
|
+
/** @internal */
|
|
950
|
+
destroy() {
|
|
951
|
+
this._destroyed = true;
|
|
952
|
+
if (typeof window !== 'undefined') {
|
|
953
|
+
if (this._stateFrame !== null) {
|
|
954
|
+
window.cancelAnimationFrame(this._stateFrame);
|
|
955
|
+
this._stateFrame = null;
|
|
956
|
+
}
|
|
957
|
+
if (this._visibilityFrame !== null) {
|
|
958
|
+
window.cancelAnimationFrame(this._visibilityFrame);
|
|
959
|
+
this._visibilityFrame = null;
|
|
960
|
+
}
|
|
961
|
+
if (this._pendingScrollFrame !== null) {
|
|
962
|
+
window.cancelAnimationFrame(this._pendingScrollFrame);
|
|
963
|
+
this._pendingScrollFrame = null;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
else {
|
|
967
|
+
this._stateFrame = null;
|
|
968
|
+
this._visibilityFrame = null;
|
|
969
|
+
this._pendingScrollFrame = null;
|
|
970
|
+
}
|
|
971
|
+
if (this._autoscrollingTimeout !== null) {
|
|
972
|
+
clearTimeout(this._autoscrollingTimeout);
|
|
973
|
+
this._autoscrollingTimeout = null;
|
|
974
|
+
}
|
|
975
|
+
this._visibilityObserver?.disconnect();
|
|
976
|
+
this._visibilityObserver = null;
|
|
977
|
+
this._visibilityTracking = false;
|
|
978
|
+
this._visibleMessageIds.clear();
|
|
979
|
+
}
|
|
980
|
+
setAutoScrolling(autoscrolling) {
|
|
981
|
+
if (this._destroyed) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
if (this._autoscrollingTimeout !== null) {
|
|
985
|
+
clearTimeout(this._autoscrollingTimeout);
|
|
986
|
+
this._autoscrollingTimeout = null;
|
|
987
|
+
}
|
|
988
|
+
if (this._autoscrolling !== autoscrolling) {
|
|
989
|
+
this._autoscrolling = autoscrolling;
|
|
990
|
+
this.commitScrollState();
|
|
991
|
+
}
|
|
992
|
+
if (autoscrolling) {
|
|
993
|
+
this._autoscrollingTimeout = setTimeout(() => {
|
|
994
|
+
this._autoscrollingTimeout = null;
|
|
995
|
+
this._autoscrolling = false;
|
|
996
|
+
this.commitScrollState();
|
|
997
|
+
}, AUTOSCROLLING_CLEAR_DELAY);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
setTailSpacerHeight(height) {
|
|
1001
|
+
const nextHeight = Math.max(0, Math.ceil(height));
|
|
1002
|
+
const spacer = this._spacer;
|
|
1003
|
+
if (!spacer) {
|
|
1004
|
+
this._spacerHeight = nextHeight;
|
|
1005
|
+
this._pendingSpacerHeight = nextHeight;
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
this._pendingSpacerHeight = null;
|
|
1009
|
+
if (this._spacerHeight === nextHeight) {
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
this._spacerHeight = nextHeight;
|
|
1013
|
+
spacer.hidden = nextHeight === 0;
|
|
1014
|
+
spacer.style.height = `${nextHeight}px`;
|
|
1015
|
+
spacer.style.marginTop = nextHeight > 0 ? `${-this._spacerGap}px` : '';
|
|
1016
|
+
}
|
|
1017
|
+
scrollToPosition(scrollTop, { behavior = 'auto', autoscrolling = false, } = {}) {
|
|
1018
|
+
const viewport = this._viewport;
|
|
1019
|
+
if (!viewport) {
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
const nextScrollTop = Math.max(0, scrollTop);
|
|
1023
|
+
if (Math.abs(viewport.scrollTop - nextScrollTop) <= SCROLL_POSITION_EPSILON) {
|
|
1024
|
+
viewport.scrollTop = nextScrollTop;
|
|
1025
|
+
this.commitScrollState();
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
if (autoscrolling) {
|
|
1029
|
+
this.setAutoScrolling(true);
|
|
1030
|
+
}
|
|
1031
|
+
viewport.scrollTo({
|
|
1032
|
+
top: nextScrollTop,
|
|
1033
|
+
behavior,
|
|
1034
|
+
});
|
|
1035
|
+
this.scheduleStateCommit();
|
|
1036
|
+
}
|
|
1037
|
+
scrollToElement(element, { align = 'start', behavior = 'auto', scrollMargin = this._scrollMargin } = {}, { keepPreviousPeek = false, } = {}) {
|
|
1038
|
+
const content = this._content;
|
|
1039
|
+
const viewport = this._viewport;
|
|
1040
|
+
if (!content || !viewport || !content.contains(element)) {
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
const scrollTop = getElementScrollTop({
|
|
1044
|
+
align,
|
|
1045
|
+
element,
|
|
1046
|
+
scrollMargin: keepPreviousPeek ? scrollMargin + this._scrollPreviousItemPeek : scrollMargin,
|
|
1047
|
+
spacer: this._spacer,
|
|
1048
|
+
viewport,
|
|
1049
|
+
});
|
|
1050
|
+
const nextSpacerHeight = getTailSpacerHeight({
|
|
1051
|
+
content,
|
|
1052
|
+
scrollTop,
|
|
1053
|
+
spacer: this._spacer,
|
|
1054
|
+
viewport,
|
|
1055
|
+
});
|
|
1056
|
+
this.setTailSpacerHeight(nextSpacerHeight);
|
|
1057
|
+
// Seed the prepend anchor with the jump target so a prepend that lands
|
|
1058
|
+
// before this scroll settles still preserves the jumped-to row; once it
|
|
1059
|
+
// settles, syncAfterScroll's capturePrependAnchor re-captures it from the
|
|
1060
|
+
// first visible row.
|
|
1061
|
+
this._prependRestore = {
|
|
1062
|
+
element,
|
|
1063
|
+
viewportTop: getElementViewportTop(element, viewport),
|
|
1064
|
+
};
|
|
1065
|
+
this._mode = keepPreviousPeek ? 'anchored-to-message' : 'settling-jump';
|
|
1066
|
+
this._streamingTurn = keepPreviousPeek ? element : null;
|
|
1067
|
+
this.scrollToPosition(scrollTop, { behavior });
|
|
1068
|
+
this.scheduleVisibilitySync();
|
|
1069
|
+
return true;
|
|
1070
|
+
}
|
|
1071
|
+
reanchorToAnchoredMessage() {
|
|
1072
|
+
const element = this._streamingTurn;
|
|
1073
|
+
if (!element || !element.isConnected || this._mode !== 'anchored-to-message') {
|
|
1074
|
+
return false;
|
|
1075
|
+
}
|
|
1076
|
+
// Re-run the placement so the tail spacer is recomputed for the new content
|
|
1077
|
+
// height and the turn is held at the reading line.
|
|
1078
|
+
return this.scrollToElement(element, { align: 'start' }, { keepPreviousPeek: true });
|
|
1079
|
+
}
|
|
1080
|
+
flushPendingScrollToMessage() {
|
|
1081
|
+
const pending = this._pendingScrollToMessage;
|
|
1082
|
+
if (!pending) {
|
|
1083
|
+
return false;
|
|
1084
|
+
}
|
|
1085
|
+
const element = this._messageElements.get(pending.messageId);
|
|
1086
|
+
if (!element) {
|
|
1087
|
+
return false;
|
|
1088
|
+
}
|
|
1089
|
+
const handled = this.scrollToElement(element, pending.options);
|
|
1090
|
+
if (!handled) {
|
|
1091
|
+
return false;
|
|
1092
|
+
}
|
|
1093
|
+
this._pendingScrollToMessage = null;
|
|
1094
|
+
this._defaultScrollPositionApplied = true;
|
|
1095
|
+
return true;
|
|
1096
|
+
}
|
|
1097
|
+
writeStateAttributes(state) {
|
|
1098
|
+
const root = this._root;
|
|
1099
|
+
const viewport = this._viewport;
|
|
1100
|
+
const scrollable = [state.start && 'start', state.end && 'end'].filter(Boolean).join(' ');
|
|
1101
|
+
const autoScrolling = this._autoscrolling;
|
|
1102
|
+
for (const element of [root, viewport]) {
|
|
1103
|
+
if (!element) {
|
|
1104
|
+
continue;
|
|
1105
|
+
}
|
|
1106
|
+
if (scrollable) {
|
|
1107
|
+
element.setAttribute('data-scrollable', scrollable);
|
|
1108
|
+
}
|
|
1109
|
+
else {
|
|
1110
|
+
element.removeAttribute('data-scrollable');
|
|
1111
|
+
}
|
|
1112
|
+
element.toggleAttribute('data-autoscrolling', autoScrolling);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
/** Arm follow-bottom at the end; release only on real upward scroll (not content growth). */
|
|
1116
|
+
reconcileFollowMode(scrollable) {
|
|
1117
|
+
const scrollTop = this._viewport?.scrollTop ?? 0;
|
|
1118
|
+
// Growth can look like "not at end" before follow catches up — only scrollTop rising releases.
|
|
1119
|
+
const scrolledUp = scrollTop < this._lastScrollTop - SCROLL_POSITION_EPSILON;
|
|
1120
|
+
this._lastScrollTop = scrollTop;
|
|
1121
|
+
if (this._autoScroll && !scrollable.end && this._mode !== 'settling-jump' && this._mode !== 'anchored-to-message') {
|
|
1122
|
+
this._mode = 'following-bottom';
|
|
1123
|
+
}
|
|
1124
|
+
else if (this._mode === 'following-bottom' && scrollable.end && scrolledUp && !this._autoscrolling) {
|
|
1125
|
+
this._mode = 'free-scrolling';
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
commitScrollState() {
|
|
1129
|
+
if (typeof window === 'undefined' ||
|
|
1130
|
+
!this._viewport ||
|
|
1131
|
+
typeof this._viewport.getBoundingClientRect !== 'function') {
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
const nextState = getMessageScrollerScrollable({
|
|
1135
|
+
content: this._content,
|
|
1136
|
+
scrollEdgeThreshold: this._scrollEdgeThreshold,
|
|
1137
|
+
spacer: this._spacer,
|
|
1138
|
+
viewport: this._viewport,
|
|
1139
|
+
});
|
|
1140
|
+
this.reconcileFollowMode(nextState);
|
|
1141
|
+
// Suppress end-overflow while following so the jump button does not strobe per chunk.
|
|
1142
|
+
const publishedState = this._mode === 'following-bottom' ? { ...nextState, end: false } : nextState;
|
|
1143
|
+
this.writeStateAttributes(publishedState);
|
|
1144
|
+
if (!areScrollStatesEqual(this._scrollable(), publishedState)) {
|
|
1145
|
+
this._scrollable.set(publishedState);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
scheduleStateCommit() {
|
|
1149
|
+
if (this._stateFrame !== null || typeof window === 'undefined') {
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
this._stateFrame = window.requestAnimationFrame(() => {
|
|
1153
|
+
this._stateFrame = null;
|
|
1154
|
+
this.commitScrollState();
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
scheduleVisibilitySync() {
|
|
1158
|
+
if (!this._visibilityTracking) {
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
if (this._visibilityFrame !== null || typeof window === 'undefined') {
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
this._visibilityFrame = window.requestAnimationFrame(() => {
|
|
1165
|
+
this._visibilityFrame = null;
|
|
1166
|
+
// Skip if unsubscribed while this frame was queued.
|
|
1167
|
+
if (!this._visibilityTracking) {
|
|
1168
|
+
return;
|
|
1169
|
+
}
|
|
1170
|
+
const next = getMessageScrollerVisibilityState({
|
|
1171
|
+
content: this._content,
|
|
1172
|
+
scrollMargin: this._scrollMargin,
|
|
1173
|
+
scrollPreviousItemPeek: this._scrollPreviousItemPeek,
|
|
1174
|
+
spacer: this._spacer,
|
|
1175
|
+
viewport: this._viewport,
|
|
1176
|
+
visibleMessageIds: this._visibleMessageIds,
|
|
1177
|
+
});
|
|
1178
|
+
if (!areVisibilityStatesEqual(this._visibility(), next)) {
|
|
1179
|
+
this._visibility.set(next);
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
restorePrependedAnchor() {
|
|
1184
|
+
const anchor = this._prependRestore;
|
|
1185
|
+
const viewport = this._viewport;
|
|
1186
|
+
if (!anchor || !viewport || !anchor.element.isConnected) {
|
|
1187
|
+
return false;
|
|
1188
|
+
}
|
|
1189
|
+
// Viewport-relative delta: no-op when native anchoring already held; corrects Safari, etc.
|
|
1190
|
+
const nextViewportTop = getElementViewportTop(anchor.element, viewport);
|
|
1191
|
+
const delta = nextViewportTop - anchor.viewportTop;
|
|
1192
|
+
if (Math.abs(delta) <= SCROLL_POSITION_EPSILON) {
|
|
1193
|
+
return false;
|
|
1194
|
+
}
|
|
1195
|
+
viewport.scrollTop += delta;
|
|
1196
|
+
anchor.viewportTop = getElementViewportTop(anchor.element, viewport);
|
|
1197
|
+
this.scheduleStateCommit();
|
|
1198
|
+
this.scheduleVisibilitySync();
|
|
1199
|
+
return true;
|
|
1200
|
+
}
|
|
1201
|
+
capturePrependAnchor() {
|
|
1202
|
+
const content = this._content;
|
|
1203
|
+
const viewport = this._viewport;
|
|
1204
|
+
if (!content || !viewport) {
|
|
1205
|
+
this._prependRestore = null;
|
|
1206
|
+
return;
|
|
1207
|
+
}
|
|
1208
|
+
const anchor = getFirstVisibleMessageItem({
|
|
1209
|
+
content,
|
|
1210
|
+
spacer: this._spacer,
|
|
1211
|
+
viewport,
|
|
1212
|
+
});
|
|
1213
|
+
this._prependRestore = anchor
|
|
1214
|
+
? {
|
|
1215
|
+
element: anchor,
|
|
1216
|
+
viewportTop: getElementViewportTop(anchor, viewport),
|
|
1217
|
+
}
|
|
1218
|
+
: null;
|
|
1219
|
+
}
|
|
1220
|
+
schedulePendingScrollToMessageFlush() {
|
|
1221
|
+
if (this._pendingScrollFrame !== null || typeof window === 'undefined') {
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
this._pendingScrollFrame = window.requestAnimationFrame(() => {
|
|
1225
|
+
this._pendingScrollFrame = null;
|
|
1226
|
+
if (this.flushPendingScrollToMessage()) {
|
|
1227
|
+
this.capturePrependAnchor();
|
|
1228
|
+
}
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
applyDefaultScrollPosition() {
|
|
1232
|
+
if (!this._defaultScrollPosition || this._defaultScrollPositionApplied || this._itemCount === 0) {
|
|
1233
|
+
return false;
|
|
1234
|
+
}
|
|
1235
|
+
let handled = false;
|
|
1236
|
+
if (this._defaultScrollPosition === 'last-anchor') {
|
|
1237
|
+
const content = this._content;
|
|
1238
|
+
const viewport = this._viewport;
|
|
1239
|
+
const anchor = content && viewport ? getLastScrollAnchor(getMessageScrollerItems(content, this._spacer)) : null;
|
|
1240
|
+
if (!content || !viewport || !anchor) {
|
|
1241
|
+
handled = this.scrollToEnd({ behavior: 'auto' });
|
|
1242
|
+
}
|
|
1243
|
+
else {
|
|
1244
|
+
const anchorTop = getElementTop(anchor, viewport);
|
|
1245
|
+
const contentBottom = getContentBottom({
|
|
1246
|
+
content,
|
|
1247
|
+
spacer: this._spacer,
|
|
1248
|
+
viewport,
|
|
1249
|
+
});
|
|
1250
|
+
// A short last turn already fits below the anchor, so opening at the end
|
|
1251
|
+
// shows the whole turn without leaving a blank gap beneath it.
|
|
1252
|
+
const lastTurnFits = contentBottom - anchorTop <= viewport.clientHeight;
|
|
1253
|
+
handled = lastTurnFits
|
|
1254
|
+
? this.scrollToEnd({ behavior: 'auto' })
|
|
1255
|
+
: this.scrollToElement(anchor, { align: 'start' }, { keepPreviousPeek: true });
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
else {
|
|
1259
|
+
handled =
|
|
1260
|
+
this._defaultScrollPosition === 'end'
|
|
1261
|
+
? this.scrollToEnd({ behavior: 'auto' })
|
|
1262
|
+
: this.scrollToStart({ behavior: 'auto' });
|
|
1263
|
+
}
|
|
1264
|
+
if (!handled) {
|
|
1265
|
+
return false;
|
|
1266
|
+
}
|
|
1267
|
+
this._defaultScrollPositionApplied = true;
|
|
1268
|
+
return true;
|
|
1269
|
+
}
|
|
1270
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScroller, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1271
|
+
/** @nocollapse */ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScroller });
|
|
1272
|
+
}
|
|
1273
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScroller, decorators: [{
|
|
1274
|
+
type: Injectable
|
|
1275
|
+
}] });
|
|
1276
|
+
|
|
1277
|
+
class BrnMessageScrollerProvider {
|
|
1278
|
+
/**
|
|
1279
|
+
* Follow new content at the bottom while the viewport is already at the end.
|
|
1280
|
+
* @default false
|
|
1281
|
+
*/
|
|
1282
|
+
autoScroll = input(false, { ...(ngDevMode ? { debugName: "autoScroll" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
1283
|
+
/**
|
|
1284
|
+
* Opening position on the first non-empty render, applied once.
|
|
1285
|
+
* @default 'end'
|
|
1286
|
+
*/
|
|
1287
|
+
defaultScrollPosition = input('end', ...(ngDevMode ? [{ debugName: "defaultScrollPosition" }] : /* istanbul ignore next */ []));
|
|
1288
|
+
/**
|
|
1289
|
+
* Distance from an edge that still counts as at-top/at-bottom.
|
|
1290
|
+
* @default 8
|
|
1291
|
+
*/
|
|
1292
|
+
scrollEdgeThreshold = input(DEFAULT_SCROLL_EDGE_THRESHOLD, { ...(ngDevMode ? { debugName: "scrollEdgeThreshold" } : /* istanbul ignore next */ {}), transform: numberAttribute });
|
|
1293
|
+
/**
|
|
1294
|
+
* Extra top margin for a newly anchored row, added to scrollMargin.
|
|
1295
|
+
* @default 64
|
|
1296
|
+
*/
|
|
1297
|
+
scrollPreviousItemPeek = input(DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK, { ...(ngDevMode ? { debugName: "scrollPreviousItemPeek" } : /* istanbul ignore next */ {}), transform: numberAttribute });
|
|
1298
|
+
/**
|
|
1299
|
+
* Default margin on the aligned edge for commands and visibility.
|
|
1300
|
+
* @default 0
|
|
1301
|
+
*/
|
|
1302
|
+
scrollMargin = input(DEFAULT_SCROLL_MARGIN, { ...(ngDevMode ? { debugName: "scrollMargin" } : /* istanbul ignore next */ {}), transform: numberAttribute });
|
|
1303
|
+
constructor() {
|
|
1304
|
+
const scroller = inject(BrnMessageScroller);
|
|
1305
|
+
effect(() => {
|
|
1306
|
+
scroller.configure({
|
|
1307
|
+
autoScroll: this.autoScroll(),
|
|
1308
|
+
defaultScrollPosition: this.defaultScrollPosition(),
|
|
1309
|
+
scrollEdgeThreshold: this.scrollEdgeThreshold(),
|
|
1310
|
+
scrollPreviousItemPeek: this.scrollPreviousItemPeek(),
|
|
1311
|
+
scrollMargin: this.scrollMargin(),
|
|
1312
|
+
});
|
|
1313
|
+
});
|
|
1314
|
+
inject(DestroyRef).onDestroy(() => scroller.destroy());
|
|
1315
|
+
}
|
|
1316
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerProvider, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
1317
|
+
/** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.2.16", type: BrnMessageScrollerProvider, isStandalone: true, selector: "[brnMessageScrollerProvider]", inputs: { autoScroll: { classPropertyName: "autoScroll", publicName: "autoScroll", isSignal: true, isRequired: false, transformFunction: null }, defaultScrollPosition: { classPropertyName: "defaultScrollPosition", publicName: "defaultScrollPosition", isSignal: true, isRequired: false, transformFunction: null }, scrollEdgeThreshold: { classPropertyName: "scrollEdgeThreshold", publicName: "scrollEdgeThreshold", isSignal: true, isRequired: false, transformFunction: null }, scrollPreviousItemPeek: { classPropertyName: "scrollPreviousItemPeek", publicName: "scrollPreviousItemPeek", isSignal: true, isRequired: false, transformFunction: null }, scrollMargin: { classPropertyName: "scrollMargin", publicName: "scrollMargin", isSignal: true, isRequired: false, transformFunction: null } }, providers: [BrnMessageScroller, provideBrnMessageScroller(BrnMessageScroller)], exportAs: ["brnMessageScrollerProvider"], ngImport: i0 });
|
|
1318
|
+
}
|
|
1319
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerProvider, decorators: [{
|
|
1320
|
+
type: Directive,
|
|
1321
|
+
args: [{
|
|
1322
|
+
selector: '[brnMessageScrollerProvider]',
|
|
1323
|
+
exportAs: 'brnMessageScrollerProvider',
|
|
1324
|
+
providers: [BrnMessageScroller, provideBrnMessageScroller(BrnMessageScroller)],
|
|
1325
|
+
}]
|
|
1326
|
+
}], ctorParameters: () => [], propDecorators: { autoScroll: [{ type: i0.Input, args: [{ isSignal: true, alias: "autoScroll", required: false }] }], defaultScrollPosition: [{ type: i0.Input, args: [{ isSignal: true, alias: "defaultScrollPosition", required: false }] }], scrollEdgeThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "scrollEdgeThreshold", required: false }] }], scrollPreviousItemPeek: [{ type: i0.Input, args: [{ isSignal: true, alias: "scrollPreviousItemPeek", required: false }] }], scrollMargin: [{ type: i0.Input, args: [{ isSignal: true, alias: "scrollMargin", required: false }] }] } });
|
|
1327
|
+
|
|
1328
|
+
class BrnMessageScrollerRoot {
|
|
1329
|
+
constructor() {
|
|
1330
|
+
const scroller = injectBrnMessageScroller();
|
|
1331
|
+
const root = inject(ElementRef).nativeElement;
|
|
1332
|
+
scroller.setRootElement(root);
|
|
1333
|
+
inject(DestroyRef).onDestroy(() => scroller.clearRootElement(root));
|
|
1334
|
+
}
|
|
1335
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerRoot, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
1336
|
+
/** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.16", type: BrnMessageScrollerRoot, isStandalone: true, selector: "[brnMessageScroller],brn-message-scroller", exportAs: ["brnMessageScroller"], ngImport: i0 });
|
|
1337
|
+
}
|
|
1338
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerRoot, decorators: [{
|
|
1339
|
+
type: Directive,
|
|
1340
|
+
args: [{
|
|
1341
|
+
selector: '[brnMessageScroller],brn-message-scroller',
|
|
1342
|
+
exportAs: 'brnMessageScroller',
|
|
1343
|
+
}]
|
|
1344
|
+
}], ctorParameters: () => [] });
|
|
1345
|
+
|
|
1346
|
+
class BrnMessageScrollerViewport {
|
|
1347
|
+
_scroller = injectBrnMessageScroller();
|
|
1348
|
+
/**
|
|
1349
|
+
* Keep the first visible messageId row stable on prepend.
|
|
1350
|
+
* @default true
|
|
1351
|
+
*/
|
|
1352
|
+
preserveScrollOnPrepend = input(true, { ...(ngDevMode ? { debugName: "preserveScrollOnPrepend" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
1353
|
+
/**
|
|
1354
|
+
* Accessible name for the scroll region.
|
|
1355
|
+
* @default 'Messages'
|
|
1356
|
+
*/
|
|
1357
|
+
ariaLabel = input('Messages', { ...(ngDevMode ? { debugName: "ariaLabel" } : /* istanbul ignore next */ {}), alias: 'aria-label' });
|
|
1358
|
+
/**
|
|
1359
|
+
* Tab index for keyboard scrolling.
|
|
1360
|
+
* @default 0
|
|
1361
|
+
*/
|
|
1362
|
+
tabIndex = input(0, { ...(ngDevMode ? { debugName: "tabIndex" } : /* istanbul ignore next */ {}), alias: 'tabindex',
|
|
1363
|
+
transform: numberAttribute });
|
|
1364
|
+
constructor() {
|
|
1365
|
+
const scroller = this._scroller;
|
|
1366
|
+
const viewport = inject(ElementRef).nativeElement;
|
|
1367
|
+
let resizeFrame = 0;
|
|
1368
|
+
let resizeObserver = null;
|
|
1369
|
+
scroller.setViewportElement(viewport);
|
|
1370
|
+
effect(() => {
|
|
1371
|
+
scroller.preserveScrollOnPrepend = this.preserveScrollOnPrepend();
|
|
1372
|
+
});
|
|
1373
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
1374
|
+
// Coalesce into rAF: handleResize mutates the spacer inside the observed
|
|
1375
|
+
// content, and resizing an observed element during delivery fires
|
|
1376
|
+
// "ResizeObserver loop completed with undelivered notifications".
|
|
1377
|
+
resizeObserver = new ResizeObserver(() => {
|
|
1378
|
+
window.cancelAnimationFrame(resizeFrame);
|
|
1379
|
+
resizeFrame = window.requestAnimationFrame(() => scroller.handleResize());
|
|
1380
|
+
});
|
|
1381
|
+
resizeObserver.observe(viewport);
|
|
1382
|
+
}
|
|
1383
|
+
inject(DestroyRef).onDestroy(() => {
|
|
1384
|
+
if (typeof window !== 'undefined') {
|
|
1385
|
+
window.cancelAnimationFrame(resizeFrame);
|
|
1386
|
+
}
|
|
1387
|
+
resizeObserver?.disconnect();
|
|
1388
|
+
resizeObserver = null;
|
|
1389
|
+
scroller.clearViewportElement(viewport);
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
onScroll() {
|
|
1393
|
+
this._scroller.syncAfterScroll();
|
|
1394
|
+
}
|
|
1395
|
+
onWheel() {
|
|
1396
|
+
this._scroller.userScrollIntent();
|
|
1397
|
+
}
|
|
1398
|
+
onTouchMove() {
|
|
1399
|
+
this._scroller.userScrollIntent();
|
|
1400
|
+
}
|
|
1401
|
+
onKeyDown(event) {
|
|
1402
|
+
if (USER_SCROLL_KEYS.has(event.key)) {
|
|
1403
|
+
this._scroller.userScrollIntent();
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
/** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerViewport, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
1407
|
+
/** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.2.16", type: BrnMessageScrollerViewport, isStandalone: true, selector: "[brnMessageScrollerViewport],brn-message-scroller-viewport", inputs: { preserveScrollOnPrepend: { classPropertyName: "preserveScrollOnPrepend", publicName: "preserveScrollOnPrepend", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "aria-label", isSignal: true, isRequired: false, transformFunction: null }, tabIndex: { classPropertyName: "tabIndex", publicName: "tabindex", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "role": "region" }, listeners: { "scroll": "onScroll()", "wheel": "onWheel()", "touchmove": "onTouchMove()", "keydown": "onKeyDown($event)" }, properties: { "attr.aria-label": "ariaLabel()", "attr.tabindex": "tabIndex()" } }, exportAs: ["brnMessageScrollerViewport"], ngImport: i0 });
|
|
1408
|
+
}
|
|
1409
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnMessageScrollerViewport, decorators: [{
|
|
1410
|
+
type: Directive,
|
|
1411
|
+
args: [{
|
|
1412
|
+
selector: '[brnMessageScrollerViewport],brn-message-scroller-viewport',
|
|
1413
|
+
exportAs: 'brnMessageScrollerViewport',
|
|
1414
|
+
host: {
|
|
1415
|
+
role: 'region',
|
|
1416
|
+
'[attr.aria-label]': 'ariaLabel()',
|
|
1417
|
+
'[attr.tabindex]': 'tabIndex()',
|
|
1418
|
+
'(scroll)': 'onScroll()',
|
|
1419
|
+
'(wheel)': 'onWheel()',
|
|
1420
|
+
'(touchmove)': 'onTouchMove()',
|
|
1421
|
+
'(keydown)': 'onKeyDown($event)',
|
|
1422
|
+
},
|
|
1423
|
+
}]
|
|
1424
|
+
}], ctorParameters: () => [], propDecorators: { preserveScrollOnPrepend: [{ type: i0.Input, args: [{ isSignal: true, alias: "preserveScrollOnPrepend", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "aria-label", required: false }] }], tabIndex: [{ type: i0.Input, args: [{ isSignal: true, alias: "tabindex", required: false }] }] } });
|
|
1425
|
+
|
|
1426
|
+
const BrnMessageScrollerImports = [
|
|
1427
|
+
BrnMessageScrollerProvider,
|
|
1428
|
+
BrnMessageScrollerRoot,
|
|
1429
|
+
BrnMessageScrollerViewport,
|
|
1430
|
+
BrnMessageScrollerContent,
|
|
1431
|
+
BrnMessageScrollerItem,
|
|
1432
|
+
BrnMessageScrollerButton,
|
|
1433
|
+
];
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* Generated bundle index. Do not edit.
|
|
1437
|
+
*/
|
|
1438
|
+
|
|
1439
|
+
export { AUTOSCROLLING_CLEAR_DELAY, BrnMessageScroller, BrnMessageScrollerButton, BrnMessageScrollerContent, BrnMessageScrollerImports, BrnMessageScrollerItem, BrnMessageScrollerProvider, BrnMessageScrollerRoot, BrnMessageScrollerToken, BrnMessageScrollerViewport, DEFAULT_SCROLL_EDGE_THRESHOLD, DEFAULT_SCROLL_MARGIN, DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK, EMPTY_MESSAGE_SCROLLER_SCROLLABLE, EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE, EMPTY_VISIBLE_MESSAGE_IDS, SCROLL_POSITION_EPSILON, USER_SCROLL_KEYS, injectBrnMessageScroller, provideBrnMessageScroller };
|
|
1440
|
+
//# sourceMappingURL=spartan-ng-brain-message-scroller.mjs.map
|