@meshagent/meshagent-tailwind 0.55.1 → 0.55.2
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/CHANGELOG.md +17 -0
- package/README.md +34 -3
- package/dist/cjs/chat/agent-thread.d.ts +42 -7
- package/dist/cjs/chat/agent-thread.js +422 -183
- package/dist/cjs/chat/chat-input-state.d.ts +2 -0
- package/dist/cjs/chat/chat-input-state.js +41 -0
- package/dist/cjs/chat/chat-input.d.ts +14 -2
- package/dist/cjs/chat/chat-input.js +41 -76
- package/dist/cjs/chat/chat-scroller.js +45 -43
- package/dist/cjs/chat/chat-thread-timeline.d.ts +2 -0
- package/dist/cjs/chat/chat-thread-timeline.js +83 -10
- package/dist/cjs/chat/composer-action-button.d.ts +11 -0
- package/dist/cjs/chat/composer-action-button.js +68 -0
- package/dist/cjs/chat/use-chat-virtualizer.d.ts +14 -17
- package/dist/cjs/chat/use-chat-virtualizer.js +288 -222
- package/dist/cjs/chat/virtualization.d.ts +0 -5
- package/dist/cjs/chat/virtualization.js +4 -6
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/esm/chat/agent-thread.d.ts +42 -7
- package/dist/esm/chat/agent-thread.js +424 -186
- package/dist/esm/chat/chat-input-state.d.ts +2 -0
- package/dist/esm/chat/chat-input-state.js +21 -0
- package/dist/esm/chat/chat-input.d.ts +14 -2
- package/dist/esm/chat/chat-input.js +41 -76
- package/dist/esm/chat/chat-scroller.js +46 -44
- package/dist/esm/chat/chat-thread-timeline.d.ts +2 -0
- package/dist/esm/chat/chat-thread-timeline.js +83 -10
- package/dist/esm/chat/composer-action-button.d.ts +11 -0
- package/dist/esm/chat/composer-action-button.js +48 -0
- package/dist/esm/chat/use-chat-virtualizer.d.ts +14 -17
- package/dist/esm/chat/use-chat-virtualizer.js +290 -226
- package/dist/esm/chat/virtualization.d.ts +0 -5
- package/dist/esm/chat/virtualization.js +4 -6
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.css +1 -1
- package/package.json +6 -5
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type Page, type VisiblePageRange } from "./virtualization.js";
|
|
1
|
+
import { type VisiblePageRange } from "./virtualization.js";
|
|
3
2
|
export type ChatVirtualizerState = {
|
|
4
3
|
visibleRange: VisiblePageRange;
|
|
5
4
|
measuredHeights: ReadonlyMap<number, number>;
|
|
6
|
-
bottomAdjustment: number;
|
|
7
5
|
};
|
|
8
6
|
export type ChatVirtualizerAction = {
|
|
9
7
|
type: "setVisibleRange";
|
|
@@ -13,12 +11,12 @@ export type ChatVirtualizerAction = {
|
|
|
13
11
|
pageCount: number;
|
|
14
12
|
overscanPages: number;
|
|
15
13
|
} | {
|
|
16
|
-
type: "
|
|
17
|
-
|
|
18
|
-
height: number;
|
|
19
|
-
shouldAdjustBottom: boolean;
|
|
14
|
+
type: "measurePages";
|
|
15
|
+
heights: ReadonlyMap<number, number>;
|
|
20
16
|
} | {
|
|
21
|
-
type: "
|
|
17
|
+
type: "reset";
|
|
18
|
+
pageCount: number;
|
|
19
|
+
overscanPages: number;
|
|
22
20
|
};
|
|
23
21
|
export type CreateChatVirtualizerStateArgs = {
|
|
24
22
|
pageCount: number;
|
|
@@ -31,17 +29,16 @@ type UseChatVirtualizerOptions<TMessage> = {
|
|
|
31
29
|
bottomThresholdPx: number;
|
|
32
30
|
getMessageId: (message: TMessage) => string;
|
|
33
31
|
};
|
|
34
|
-
type UseChatVirtualizerResult<TMessage> = {
|
|
35
|
-
scrollRef: RefObject<HTMLDivElement | null>;
|
|
36
|
-
visiblePages: Page<TMessage>[];
|
|
37
|
-
spacerHeight: number;
|
|
38
|
-
windowBottom: number;
|
|
39
|
-
handleScroll: () => void;
|
|
40
|
-
handlePageHeightChange: (pageIndex: number, height: number) => void;
|
|
41
|
-
};
|
|
42
32
|
export declare function getInitialVisibleRange(pageCount: number, overscanPages: number): VisiblePageRange;
|
|
43
33
|
export declare function clampVisibleRange(range: VisiblePageRange, pageCount: number): VisiblePageRange;
|
|
44
34
|
export declare function createChatVirtualizerState({ pageCount, overscanPages, }: CreateChatVirtualizerStateArgs): ChatVirtualizerState;
|
|
45
35
|
export declare function chatVirtualizerReducer(state: ChatVirtualizerState, action: ChatVirtualizerAction): ChatVirtualizerState;
|
|
46
|
-
export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>):
|
|
36
|
+
export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>): {
|
|
37
|
+
scrollRef: import("react").RefObject<HTMLDivElement | null>;
|
|
38
|
+
visiblePages: import("./virtualization.js").Page<TMessage>[];
|
|
39
|
+
spacerHeights: import("./virtualization.js").SpacerHeights;
|
|
40
|
+
handleScroll: () => void;
|
|
41
|
+
handleScrollIntent: () => void;
|
|
42
|
+
registerPage: (index: number, element: HTMLDivElement) => () => void;
|
|
43
|
+
};
|
|
47
44
|
export {};
|
|
@@ -27,20 +27,17 @@ __export(use_chat_virtualizer_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(use_chat_virtualizer_exports);
|
|
28
28
|
var import_react = require("react");
|
|
29
29
|
var import_virtualization = require("./virtualization.js");
|
|
30
|
+
const RESIZE_QUIET_MS = 100;
|
|
31
|
+
const RESIZE_MAX_WAIT_MS = 300;
|
|
32
|
+
const SCROLL_EPSILON = 1;
|
|
30
33
|
function getInitialVisibleRange(pageCount, overscanPages) {
|
|
31
|
-
|
|
32
|
-
return { first: 0, last: -1 };
|
|
33
|
-
}
|
|
34
|
-
return {
|
|
34
|
+
return pageCount === 0 ? { first: 0, last: -1 } : {
|
|
35
35
|
first: Math.max(0, pageCount - 1 - Math.max(0, overscanPages)),
|
|
36
36
|
last: pageCount - 1
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
function clampVisibleRange(range, pageCount) {
|
|
40
|
-
|
|
41
|
-
return { first: 0, last: -1 };
|
|
42
|
-
}
|
|
43
|
-
return {
|
|
40
|
+
return pageCount === 0 ? { first: 0, last: -1 } : {
|
|
44
41
|
first: Math.min(pageCount - 1, Math.max(0, range.first)),
|
|
45
42
|
last: Math.min(pageCount - 1, Math.max(0, range.last))
|
|
46
43
|
};
|
|
@@ -51,157 +48,103 @@ function createChatVirtualizerState({
|
|
|
51
48
|
}) {
|
|
52
49
|
return {
|
|
53
50
|
visibleRange: getInitialVisibleRange(pageCount, overscanPages),
|
|
54
|
-
measuredHeights: /* @__PURE__ */ new Map()
|
|
55
|
-
bottomAdjustment: 0
|
|
51
|
+
measuredHeights: /* @__PURE__ */ new Map()
|
|
56
52
|
};
|
|
57
53
|
}
|
|
58
|
-
function areRangesEqual(
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
function pruneMeasuredHeights(measuredHeights, pageCount) {
|
|
62
|
-
let didPrune = false;
|
|
63
|
-
const nextMeasuredHeights = /* @__PURE__ */ new Map();
|
|
64
|
-
for (const [pageIndex, height] of measuredHeights) {
|
|
65
|
-
if (pageIndex >= pageCount) {
|
|
66
|
-
didPrune = true;
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
nextMeasuredHeights.set(pageIndex, height);
|
|
70
|
-
}
|
|
71
|
-
return didPrune ? nextMeasuredHeights : measuredHeights;
|
|
54
|
+
function areRangesEqual(a, b) {
|
|
55
|
+
return a.first === b.first && a.last === b.last;
|
|
72
56
|
}
|
|
73
57
|
function chatVirtualizerReducer(state, action) {
|
|
74
58
|
switch (action.type) {
|
|
59
|
+
case "reset": {
|
|
60
|
+
return createChatVirtualizerState(action);
|
|
61
|
+
}
|
|
75
62
|
case "setVisibleRange": {
|
|
76
|
-
|
|
77
|
-
return state;
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
...state,
|
|
81
|
-
visibleRange: action.range
|
|
82
|
-
};
|
|
63
|
+
return areRangesEqual(state.visibleRange, action.range) ? state : { ...state, visibleRange: action.range };
|
|
83
64
|
}
|
|
84
65
|
case "syncPageWindow": {
|
|
85
66
|
const visibleRange = state.visibleRange.last === -1 ? getInitialVisibleRange(action.pageCount, action.overscanPages) : clampVisibleRange(state.visibleRange, action.pageCount);
|
|
86
|
-
const measuredHeights = pruneMeasuredHeights(state.measuredHeights, action.pageCount);
|
|
87
|
-
if (areRangesEqual(state.visibleRange, visibleRange) && measuredHeights === state.measuredHeights) {
|
|
88
|
-
return state;
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
...state,
|
|
92
|
-
visibleRange,
|
|
93
|
-
measuredHeights
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
case "measurePage": {
|
|
97
|
-
const roundedHeight = Math.ceil(action.height);
|
|
98
|
-
const previousHeight = state.measuredHeights.get(action.pageIndex) ?? 0;
|
|
99
|
-
if (previousHeight === roundedHeight) {
|
|
100
|
-
return state;
|
|
101
|
-
}
|
|
102
|
-
const heightDelta = roundedHeight - previousHeight;
|
|
103
67
|
const measuredHeights = new Map(state.measuredHeights);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
68
|
+
for (const index of measuredHeights.keys()) {
|
|
69
|
+
if (index >= action.pageCount) {
|
|
70
|
+
measuredHeights.delete(index);
|
|
71
|
+
}
|
|
108
72
|
}
|
|
109
|
-
return {
|
|
110
|
-
...state,
|
|
111
|
-
measuredHeights,
|
|
112
|
-
bottomAdjustment: state.bottomAdjustment + (action.shouldAdjustBottom ? heightDelta : 0)
|
|
113
|
-
};
|
|
73
|
+
return areRangesEqual(state.visibleRange, visibleRange) && measuredHeights.size === state.measuredHeights.size ? state : { visibleRange, measuredHeights };
|
|
114
74
|
}
|
|
115
|
-
case "
|
|
116
|
-
|
|
117
|
-
|
|
75
|
+
case "measurePages": {
|
|
76
|
+
const measuredHeights = new Map(state.measuredHeights);
|
|
77
|
+
for (const [index, height] of action.heights) {
|
|
78
|
+
measuredHeights.set(index, height);
|
|
118
79
|
}
|
|
119
|
-
return {
|
|
120
|
-
...state,
|
|
121
|
-
bottomAdjustment: 0
|
|
122
|
-
};
|
|
80
|
+
return { ...state, measuredHeights };
|
|
123
81
|
}
|
|
124
82
|
}
|
|
125
83
|
}
|
|
126
|
-
function
|
|
127
|
-
|
|
128
|
-
|
|
84
|
+
function cancelResizeTimers(runtime) {
|
|
85
|
+
window.clearTimeout(runtime.quietTimer);
|
|
86
|
+
window.clearTimeout(runtime.maxTimer);
|
|
87
|
+
runtime.quietTimer = void 0;
|
|
88
|
+
runtime.maxTimer = void 0;
|
|
129
89
|
}
|
|
130
|
-
function
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
90
|
+
function captureAnchors(element, runtime) {
|
|
91
|
+
const rect = element.getBoundingClientRect();
|
|
92
|
+
runtime.anchors = [];
|
|
93
|
+
runtime.lastScrollTop = element.scrollTop;
|
|
94
|
+
runtime.lastScrollHeight = element.scrollHeight;
|
|
95
|
+
if (runtime.isAtBottom) {
|
|
96
|
+
return;
|
|
134
97
|
}
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
);
|
|
147
|
-
const pageIndex = closestPageElement instanceof HTMLElement ? Number(closestPageElement.dataset.pageIndex) : Number.NaN;
|
|
148
|
-
if (messageId && Number.isInteger(pageIndex)) {
|
|
149
|
-
const anchor = {
|
|
150
|
-
messageId,
|
|
151
|
-
offsetTop: messageRect.top - scrollRect.top,
|
|
152
|
-
pageIndex
|
|
153
|
-
};
|
|
154
|
-
runtime.anchor = anchor;
|
|
155
|
-
return anchor;
|
|
156
|
-
}
|
|
157
|
-
runtime.anchor = void 0;
|
|
158
|
-
return void 0;
|
|
98
|
+
for (const message of Array.from(
|
|
99
|
+
element.querySelectorAll(
|
|
100
|
+
".chat-scroll__message[data-message-id]"
|
|
101
|
+
)
|
|
102
|
+
)) {
|
|
103
|
+
const messageRect = message.getBoundingClientRect();
|
|
104
|
+
if (messageRect.bottom > rect.top && messageRect.top < rect.bottom && message.dataset.messageId) {
|
|
105
|
+
runtime.anchors.push({
|
|
106
|
+
messageId: message.dataset.messageId,
|
|
107
|
+
offsetTop: messageRect.top - rect.top
|
|
108
|
+
});
|
|
159
109
|
}
|
|
160
110
|
}
|
|
161
|
-
runtime.anchor = void 0;
|
|
162
|
-
return void 0;
|
|
163
111
|
}
|
|
164
|
-
function
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return bottomAdjustment !== 0;
|
|
169
|
-
}
|
|
170
|
-
const messageElement = scrollElement.querySelector(
|
|
171
|
-
getMessageSelector(anchor.messageId)
|
|
112
|
+
function writeScrollTop(element, runtime, target) {
|
|
113
|
+
const clampedTarget = Math.max(
|
|
114
|
+
0,
|
|
115
|
+
Math.min(target, element.scrollHeight - element.clientHeight)
|
|
172
116
|
);
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
const scrollRect = scrollElement.getBoundingClientRect();
|
|
178
|
-
const messageRect = messageElement.getBoundingClientRect();
|
|
179
|
-
const nextOffsetTop = messageRect.top - scrollRect.top;
|
|
180
|
-
const offsetDelta = nextOffsetTop - anchor.offsetTop;
|
|
181
|
-
if (offsetDelta !== 0) {
|
|
182
|
-
scrollElement.scrollTop += offsetDelta;
|
|
117
|
+
if (Math.abs(element.scrollTop - clampedTarget) > SCROLL_EPSILON) {
|
|
118
|
+
element.scrollTop = clampedTarget;
|
|
119
|
+
runtime.expectedScrollTop = element.scrollTop;
|
|
183
120
|
}
|
|
184
|
-
if (bottomAdjustment !== 0) {
|
|
185
|
-
scrollElement.scrollTop += bottomAdjustment;
|
|
186
|
-
}
|
|
187
|
-
runtime.anchor = void 0;
|
|
188
|
-
return bottomAdjustment !== 0;
|
|
189
121
|
}
|
|
190
|
-
function
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
122
|
+
function restoreAnchors(element, runtime) {
|
|
123
|
+
if (runtime.isAtBottom) {
|
|
124
|
+
writeScrollTop(element, runtime, element.scrollHeight);
|
|
125
|
+
} else {
|
|
126
|
+
const messages = new Map(
|
|
127
|
+
Array.from(
|
|
128
|
+
element.querySelectorAll(
|
|
129
|
+
".chat-scroll__message[data-message-id]"
|
|
130
|
+
),
|
|
131
|
+
(message) => [message.dataset.messageId, message]
|
|
132
|
+
)
|
|
133
|
+
);
|
|
134
|
+
for (const anchor of runtime.anchors) {
|
|
135
|
+
const message = messages.get(anchor.messageId);
|
|
136
|
+
if (message) {
|
|
137
|
+
const offset = message.getBoundingClientRect().top - element.getBoundingClientRect().top;
|
|
138
|
+
writeScrollTop(
|
|
139
|
+
element,
|
|
140
|
+
runtime,
|
|
141
|
+
element.scrollTop + offset - anchor.offsetTop
|
|
142
|
+
);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
203
146
|
}
|
|
204
|
-
|
|
147
|
+
captureAnchors(element, runtime);
|
|
205
148
|
}
|
|
206
149
|
function useChatVirtualizer({
|
|
207
150
|
messages,
|
|
@@ -212,125 +155,248 @@ function useChatVirtualizer({
|
|
|
212
155
|
}) {
|
|
213
156
|
const scrollRef = (0, import_react.useRef)(null);
|
|
214
157
|
const runtimeRef = (0, import_react.useRef)({
|
|
158
|
+
active: true,
|
|
215
159
|
isAtBottom: true,
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
160
|
+
anchors: [],
|
|
161
|
+
expectedScrollTop: void 0,
|
|
162
|
+
lastScrollTop: 0,
|
|
163
|
+
lastScrollHeight: 0,
|
|
164
|
+
userScrollIntent: false,
|
|
165
|
+
quietTimer: void 0,
|
|
166
|
+
maxTimer: void 0,
|
|
219
167
|
frameId: void 0,
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
168
|
+
pages: /* @__PURE__ */ new Map(),
|
|
169
|
+
pending: /* @__PURE__ */ new Map(),
|
|
170
|
+
newPages: false,
|
|
171
|
+
needsRestore: true,
|
|
172
|
+
messageIds: /* @__PURE__ */ new Set(),
|
|
173
|
+
pageSize
|
|
225
174
|
});
|
|
226
175
|
const pages = (0, import_react.useMemo)(
|
|
227
176
|
() => (0, import_virtualization.createPages)([...messages].reverse(), pageSize),
|
|
228
177
|
[messages, pageSize]
|
|
229
178
|
);
|
|
179
|
+
const messageIds = (0, import_react.useMemo)(
|
|
180
|
+
() => new Set(messages.map(getMessageId)),
|
|
181
|
+
[messages, getMessageId]
|
|
182
|
+
);
|
|
230
183
|
const pageCount = pages.length;
|
|
231
184
|
const [state, dispatch] = (0, import_react.useReducer)(
|
|
232
185
|
chatVirtualizerReducer,
|
|
233
186
|
{ pageCount, overscanPages },
|
|
234
187
|
createChatVirtualizerState
|
|
235
188
|
);
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
() => (0, import_virtualization.buildPageHeights)(pageCount, state.measuredHeights),
|
|
189
|
+
const visibleRange = clampVisibleRange(state.visibleRange, pageCount);
|
|
190
|
+
const prefixHeights = (0, import_react.useMemo)(
|
|
191
|
+
() => (0, import_virtualization.buildPrefixHeights)((0, import_virtualization.buildPageHeights)(pageCount, state.measuredHeights)),
|
|
239
192
|
[pageCount, state.measuredHeights]
|
|
240
193
|
);
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
194
|
+
const geometryRef = (0, import_react.useRef)({
|
|
195
|
+
prefixHeights,
|
|
196
|
+
visibleRange,
|
|
197
|
+
pageCount,
|
|
198
|
+
overscanPages,
|
|
199
|
+
bottomThresholdPx
|
|
200
|
+
});
|
|
201
|
+
const flushMeasurements = (0, import_react.useCallback)(() => {
|
|
202
|
+
const runtime = runtimeRef.current;
|
|
203
|
+
const heights = /* @__PURE__ */ new Map();
|
|
204
|
+
cancelResizeTimers(runtime);
|
|
205
|
+
for (const [index, page] of runtime.pending) {
|
|
206
|
+
if (runtime.pages.get(index) === page) {
|
|
207
|
+
heights.set(index, page.height);
|
|
208
|
+
}
|
|
255
209
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
});
|
|
262
|
-
|
|
210
|
+
runtime.pending.clear();
|
|
211
|
+
if (!runtime.active || heights.size === 0) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
runtime.needsRestore = true;
|
|
215
|
+
dispatch({ type: "measurePages", heights });
|
|
216
|
+
return true;
|
|
217
|
+
}, []);
|
|
218
|
+
const registerPage = (0, import_react.useCallback)(
|
|
219
|
+
(index, element) => {
|
|
220
|
+
const runtime = runtimeRef.current;
|
|
221
|
+
const page = { element, height: element.getBoundingClientRect().height };
|
|
222
|
+
let observing = true;
|
|
223
|
+
runtime.active = true;
|
|
224
|
+
runtime.pages.set(index, page);
|
|
225
|
+
runtime.pending.set(index, page);
|
|
226
|
+
runtime.newPages = true;
|
|
227
|
+
runtime.needsRestore = true;
|
|
228
|
+
const observer = new ResizeObserver(() => {
|
|
229
|
+
if (!observing || !runtime.active || runtime.pages.get(index) !== page) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const height = element.getBoundingClientRect().height;
|
|
233
|
+
if (page.height === height) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
page.height = height;
|
|
237
|
+
runtime.pending.set(index, page);
|
|
238
|
+
window.clearTimeout(runtime.quietTimer);
|
|
239
|
+
runtime.quietTimer = window.setTimeout(
|
|
240
|
+
flushMeasurements,
|
|
241
|
+
RESIZE_QUIET_MS
|
|
242
|
+
);
|
|
243
|
+
if (runtime.maxTimer === void 0) {
|
|
244
|
+
runtime.maxTimer = window.setTimeout(
|
|
245
|
+
flushMeasurements,
|
|
246
|
+
RESIZE_MAX_WAIT_MS
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
observer.observe(element);
|
|
251
|
+
return () => {
|
|
252
|
+
observing = false;
|
|
253
|
+
observer.disconnect();
|
|
254
|
+
if (runtime.pages.get(index) === page) {
|
|
255
|
+
runtime.pages.delete(index);
|
|
256
|
+
runtime.pending.delete(index);
|
|
257
|
+
}
|
|
258
|
+
if (runtime.pending.size === 0) {
|
|
259
|
+
cancelResizeTimers(runtime);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
[flushMeasurements]
|
|
264
|
+
);
|
|
265
|
+
const updateVisibleRange = (0, import_react.useCallback)(() => {
|
|
266
|
+
const element = scrollRef.current;
|
|
267
|
+
const runtime = runtimeRef.current;
|
|
268
|
+
const geometry = geometryRef.current;
|
|
269
|
+
if (!element || runtime.pending.size > 0) {
|
|
263
270
|
return;
|
|
264
271
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
272
|
+
const range = clampVisibleRange(
|
|
273
|
+
(0, import_virtualization.getVisiblePageRange)({
|
|
274
|
+
prefixHeights: geometry.prefixHeights,
|
|
275
|
+
scrollTop: element.scrollTop,
|
|
276
|
+
viewportHeight: element.clientHeight,
|
|
277
|
+
overscanPages: geometry.overscanPages
|
|
278
|
+
}),
|
|
279
|
+
geometry.pageCount
|
|
280
|
+
);
|
|
281
|
+
if (!areRangesEqual(range, geometry.visibleRange)) {
|
|
282
|
+
runtime.needsRestore = true;
|
|
283
|
+
dispatch({ type: "setVisibleRange", range });
|
|
284
|
+
}
|
|
269
285
|
}, []);
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const shouldAdjustBottom = anchor ? (0, import_virtualization.shouldAdjustScrollForHeightChange)({
|
|
274
|
-
changedPageIndex: pageIndex,
|
|
275
|
-
firstVisiblePageIndex: anchor.pageIndex,
|
|
276
|
-
isAtBottom: currentRuntime.isAtBottom
|
|
277
|
-
}) : false;
|
|
278
|
-
dispatch({ type: "measurePage", pageIndex, height, shouldAdjustBottom });
|
|
286
|
+
const handleScrollIntent = (0, import_react.useCallback)(() => {
|
|
287
|
+
runtimeRef.current.userScrollIntent = true;
|
|
288
|
+
runtimeRef.current.expectedScrollTop = void 0;
|
|
279
289
|
}, []);
|
|
280
|
-
(0, import_react.
|
|
281
|
-
const
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
currentRuntime,
|
|
286
|
-
state.bottomAdjustment
|
|
287
|
-
);
|
|
288
|
-
if (shouldClearBottomAdjustment) {
|
|
289
|
-
dispatch({ type: "clearBottomAdjustment" });
|
|
290
|
+
const handleScroll = (0, import_react.useCallback)(() => {
|
|
291
|
+
const element = scrollRef.current;
|
|
292
|
+
const runtime = runtimeRef.current;
|
|
293
|
+
if (!element) {
|
|
294
|
+
return;
|
|
290
295
|
}
|
|
291
|
-
|
|
296
|
+
const expected = runtime.expectedScrollTop;
|
|
297
|
+
const layoutClamped = element.scrollHeight < runtime.lastScrollHeight && runtime.lastScrollTop > element.scrollHeight - element.clientHeight && Math.abs(
|
|
298
|
+
element.scrollTop - Math.max(0, element.scrollHeight - element.clientHeight)
|
|
299
|
+
) <= SCROLL_EPSILON;
|
|
300
|
+
if (!runtime.userScrollIntent && (expected !== void 0 && Math.abs(expected - element.scrollTop) <= SCROLL_EPSILON || layoutClamped)) {
|
|
301
|
+
runtime.expectedScrollTop = void 0;
|
|
292
302
|
return;
|
|
293
303
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
304
|
+
runtime.userScrollIntent = false;
|
|
305
|
+
runtime.expectedScrollTop = void 0;
|
|
306
|
+
runtime.isAtBottom = (0, import_virtualization.isNearBottom)({
|
|
307
|
+
scrollTop: element.scrollTop,
|
|
308
|
+
scrollHeight: element.scrollHeight,
|
|
309
|
+
clientHeight: element.clientHeight,
|
|
310
|
+
thresholdPx: geometryRef.current.bottomThresholdPx
|
|
311
|
+
});
|
|
312
|
+
captureAnchors(element, runtime);
|
|
313
|
+
if (runtime.pending.size > 0) {
|
|
314
|
+
flushMeasurements();
|
|
315
|
+
}
|
|
316
|
+
if (runtime.frameId === void 0) {
|
|
317
|
+
runtime.frameId = window.requestAnimationFrame(() => {
|
|
318
|
+
runtime.frameId = void 0;
|
|
319
|
+
updateVisibleRange();
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}, [flushMeasurements, updateVisibleRange]);
|
|
297
323
|
(0, import_react.useLayoutEffect)(() => {
|
|
298
|
-
const
|
|
299
|
-
const
|
|
300
|
-
const
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
324
|
+
const runtime = runtimeRef.current;
|
|
325
|
+
const element = scrollRef.current;
|
|
326
|
+
const replaced = runtime.pageSize !== pageSize || runtime.messageIds.size > 0 && !Array.from(messageIds).some((id) => runtime.messageIds.has(id));
|
|
327
|
+
const addedPageAtBottom = runtime.isAtBottom && pageCount > geometryRef.current.pageCount;
|
|
328
|
+
geometryRef.current = {
|
|
329
|
+
prefixHeights,
|
|
330
|
+
visibleRange,
|
|
331
|
+
pageCount,
|
|
332
|
+
overscanPages,
|
|
333
|
+
bottomThresholdPx
|
|
334
|
+
};
|
|
335
|
+
runtime.active = true;
|
|
336
|
+
runtime.messageIds = messageIds;
|
|
337
|
+
runtime.pageSize = pageSize;
|
|
338
|
+
if (replaced) {
|
|
339
|
+
cancelResizeTimers(runtime);
|
|
340
|
+
window.cancelAnimationFrame(runtime.frameId ?? 0);
|
|
341
|
+
runtime.frameId = void 0;
|
|
342
|
+
runtime.pending.clear();
|
|
343
|
+
runtime.anchors = [];
|
|
344
|
+
runtime.expectedScrollTop = void 0;
|
|
345
|
+
runtime.isAtBottom = true;
|
|
346
|
+
runtime.userScrollIntent = false;
|
|
347
|
+
runtime.needsRestore = true;
|
|
348
|
+
runtime.newPages = true;
|
|
349
|
+
for (const [index, page] of runtime.pages) {
|
|
350
|
+
page.height = page.element.getBoundingClientRect().height;
|
|
351
|
+
runtime.pending.set(index, page);
|
|
352
|
+
}
|
|
353
|
+
dispatch({ type: "reset", pageCount, overscanPages });
|
|
306
354
|
return;
|
|
307
355
|
}
|
|
308
|
-
if (!
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
updateVisibleRange(scrollElement, currentRuntime, dispatch);
|
|
313
|
-
return;
|
|
356
|
+
if (!areRangesEqual(state.visibleRange, visibleRange) || state.visibleRange.last === -1 && pageCount > 0 || Array.from(state.measuredHeights.keys()).some(
|
|
357
|
+
(index) => index >= pageCount
|
|
358
|
+
)) {
|
|
359
|
+
dispatch({ type: "syncPageWindow", pageCount, overscanPages });
|
|
314
360
|
}
|
|
315
|
-
if (
|
|
316
|
-
|
|
317
|
-
|
|
361
|
+
if (addedPageAtBottom) {
|
|
362
|
+
runtime.needsRestore = true;
|
|
363
|
+
dispatch({
|
|
364
|
+
type: "setVisibleRange",
|
|
365
|
+
range: {
|
|
366
|
+
first: visibleRange.first,
|
|
367
|
+
last: pageCount - 1
|
|
368
|
+
}
|
|
369
|
+
});
|
|
318
370
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
if (frameId !== void 0) {
|
|
324
|
-
window.cancelAnimationFrame(frameId);
|
|
371
|
+
if (runtime.newPages) {
|
|
372
|
+
runtime.newPages = false;
|
|
373
|
+
if (flushMeasurements()) {
|
|
374
|
+
return;
|
|
325
375
|
}
|
|
326
|
-
}
|
|
327
|
-
|
|
376
|
+
}
|
|
377
|
+
if (element && runtime.needsRestore && runtime.pending.size === 0) {
|
|
378
|
+
runtime.needsRestore = false;
|
|
379
|
+
restoreAnchors(element, runtime);
|
|
380
|
+
updateVisibleRange();
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
(0, import_react.useLayoutEffect)(
|
|
384
|
+
() => () => {
|
|
385
|
+
const runtime = runtimeRef.current;
|
|
386
|
+
runtime.active = false;
|
|
387
|
+
cancelResizeTimers(runtime);
|
|
388
|
+
window.cancelAnimationFrame(runtime.frameId ?? 0);
|
|
389
|
+
runtime.frameId = void 0;
|
|
390
|
+
runtime.pending.clear();
|
|
391
|
+
},
|
|
392
|
+
[]
|
|
393
|
+
);
|
|
328
394
|
return {
|
|
329
395
|
scrollRef,
|
|
330
|
-
visiblePages,
|
|
331
|
-
|
|
332
|
-
windowBottom: virtualWindowMetrics.bottom + state.bottomAdjustment,
|
|
396
|
+
visiblePages: visibleRange.last >= visibleRange.first ? pages.slice(visibleRange.first, visibleRange.last + 1) : [],
|
|
397
|
+
spacerHeights: (0, import_virtualization.getSpacerHeights)(prefixHeights, visibleRange),
|
|
333
398
|
handleScroll,
|
|
334
|
-
|
|
399
|
+
handleScrollIntent,
|
|
400
|
+
registerPage
|
|
335
401
|
};
|
|
336
402
|
}
|
|
@@ -37,8 +37,3 @@ export declare function isNearBottom(args: {
|
|
|
37
37
|
clientHeight: number;
|
|
38
38
|
thresholdPx: number;
|
|
39
39
|
}): boolean;
|
|
40
|
-
export declare function shouldAdjustScrollForHeightChange(args: {
|
|
41
|
-
changedPageIndex: number;
|
|
42
|
-
firstVisiblePageIndex: number;
|
|
43
|
-
isAtBottom: boolean;
|
|
44
|
-
}): boolean;
|