@myun/gimi-chat 0.7.4 → 0.7.5

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.
@@ -5,6 +5,7 @@
5
5
  display: flex;
6
6
  gap: 20px;
7
7
  position: relative;
8
+ overflow-x: hidden;
8
9
  }
9
10
 
10
11
  .chat_content {
@@ -104,6 +104,7 @@ var useChatHistory = function useChatHistory(checkUnfinishedMessage, scrollBotto
104
104
  newChatList,
105
105
  lastUserRequestMessage,
106
106
  lastMessage,
107
+ waitForStableHeight,
107
108
  _lastUserRequestMessa,
108
109
  container,
109
110
  _args = arguments;
@@ -132,7 +133,7 @@ var useChatHistory = function useChatHistory(checkUnfinishedMessage, scrollBotto
132
133
  case 11:
133
134
  res = _context.sent;
134
135
  if (!(res && res.status === 0)) {
135
- _context.next = 37;
136
+ _context.next = 38;
136
137
  break;
137
138
  }
138
139
  resData = formatFields(res.result.data, res.result.first ? [] : currentMessageList) || [];
@@ -147,7 +148,7 @@ var useChatHistory = function useChatHistory(checkUnfinishedMessage, scrollBotto
147
148
  }
148
149
  hasMore.current = res.result.last === undefined ? false : !res.result.last;
149
150
  if (!res.result.first) {
150
- _context.next = 27;
151
+ _context.next = 28;
151
152
  break;
152
153
  }
153
154
  dispatch(setMessageList({
@@ -156,15 +157,44 @@ var useChatHistory = function useChatHistory(checkUnfinishedMessage, scrollBotto
156
157
  _context.next = 24;
157
158
  return checkUnfinishedMessage(_newResData);
158
159
  case 24:
159
- setTimeout(function () {
160
- scrollBottomForce(true);
161
- setTimeout(function () {
162
- scrollBottomForce(true);
163
- }, 50);
164
- }, 0);
165
- _context.next = 35;
160
+ // 3. 滚动到底部:待稳定高度后再滚动
161
+ waitForStableHeight = function waitForStableHeight() {
162
+ var maxFrames = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 32;
163
+ var lastHeight = -1;
164
+ var stableFrames = 0;
165
+ var frames = 0;
166
+ return new Promise(function (resolve) {
167
+ var tick = function tick() {
168
+ var container = containerRef.current;
169
+ if (!container) {
170
+ resolve();
171
+ return;
172
+ }
173
+ var h = container.scrollHeight;
174
+ if (h === lastHeight) {
175
+ stableFrames += 1;
176
+ } else {
177
+ stableFrames = 0;
178
+ lastHeight = h;
179
+ }
180
+ frames += 1;
181
+ if (stableFrames >= 2 || frames >= maxFrames) {
182
+ resolve();
183
+ return;
184
+ }
185
+ requestAnimationFrame(tick);
186
+ };
187
+ requestAnimationFrame(tick);
188
+ });
189
+ };
190
+ requestAnimationFrame(function () {
191
+ waitForStableHeight().then(function () {
192
+ scrollBottomForce(false);
193
+ });
194
+ });
195
+ _context.next = 36;
166
196
  break;
167
- case 27:
197
+ case 28:
168
198
  container = containerRef.current;
169
199
  if (container) {
170
200
  historyScrollRef.current = {
@@ -176,41 +206,41 @@ var useChatHistory = function useChatHistory(checkUnfinishedMessage, scrollBotto
176
206
  // 2. 更新数据
177
207
  // 判断是否需要重写推荐列表
178
208
  if (!((currentAgentObj === null || currentAgentObj === void 0 ? void 0 : currentAgentObj.isEnableRelated) === 1 && !((_lastUserRequestMessa = lastUserRequestMessage) !== null && _lastUserRequestMessa !== void 0 && _lastUserRequestMessa.relatedResourceList) && ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.moduleType) === 'end' || (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.moduleType) === 'COMPLETED'))) {
179
- _context.next = 34;
209
+ _context.next = 35;
180
210
  break;
181
211
  }
182
- _context.next = 32;
212
+ _context.next = 33;
183
213
  return rewriteRecommendListFormHistory(newChatList, lastUserRequestMessage.chatId);
184
- case 32:
185
- _context.next = 35;
214
+ case 33:
215
+ _context.next = 36;
186
216
  break;
187
- case 34:
217
+ case 35:
188
218
  dispatch(setMessageList({
189
219
  messageList: newChatList
190
220
  }));
191
- case 35:
192
- _context.next = 38;
221
+ case 36:
222
+ _context.next = 39;
193
223
  break;
194
- case 37:
224
+ case 38:
195
225
  if (res.status === 1034) {
196
226
  Toast.error(res.message || '获取历史记录失败');
197
227
  }
198
- case 38:
228
+ case 39:
199
229
  isMoreLoadingRef.current = false;
200
230
  setIsMoreLoading(false);
201
- _context.next = 47;
231
+ _context.next = 48;
202
232
  break;
203
- case 42:
204
- _context.prev = 42;
233
+ case 43:
234
+ _context.prev = 43;
205
235
  _context.t0 = _context["catch"](5);
206
236
  console.error(_context.t0);
207
237
  isMoreLoadingRef.current = false;
208
238
  setIsMoreLoading(false);
209
- case 47:
239
+ case 48:
210
240
  case "end":
211
241
  return _context.stop();
212
242
  }
213
- }, _callee, null, [[5, 42]]);
243
+ }, _callee, null, [[5, 43]]);
214
244
  })), [checkUnfinishedMessage, containerRef, dispatch, getMessageList, param.pageIndex, param.pageSize, rewriteRecommendListFormHistory, scrollBottomForce]);
215
245
  var loadMore = useCallback(function () {
216
246
  if (!hasMore.current || isMoreLoadingRef.current || isMsgRecievingRef.current || !conversationIdRef.current) {
@@ -26,8 +26,23 @@ var useScroll = function useScroll(containerRef) {
26
26
  scrollTimeout: 0,
27
27
  interactionTimer: null,
28
28
  lastScrollTop: 0,
29
- isUserInteracting: false
29
+ isUserInteracting: false,
30
+ wasAtBottom: true,
31
+ lastWheelAt: 0
30
32
  }).current;
33
+ var scrollBottomNow = React.useCallback(function () {
34
+ var tries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 4;
35
+ var count = 0;
36
+ var tick = function tick() {
37
+ var container = containerRef.current;
38
+ if (!container) return;
39
+ container.scrollTop = container.scrollHeight;
40
+ count += 1;
41
+ if (count < tries) requestAnimationFrame(tick);
42
+ };
43
+ requestAnimationFrame(tick);
44
+ setTimeout(tick, 0);
45
+ }, [containerRef]);
31
46
 
32
47
  // 标记用户交互状态
33
48
  var markUserInteraction = React.useCallback(function (isInteracting) {
@@ -50,18 +65,8 @@ var useScroll = function useScroll(containerRef) {
50
65
 
51
66
  // 基础滚动到底部
52
67
  var scrollBottom = React.useCallback(function () {
53
- var container = containerRef.current;
54
- if (container) {
55
- // 使用 requestAnimationFrame 确保在浏览器渲染前执行,
56
- // 并且如果一次不行,尝试在下一帧再做一次,以应对复杂的布局变化
57
- var doScroll = function doScroll() {
58
- container.scrollTop = container.scrollHeight;
59
- };
60
- requestAnimationFrame(doScroll);
61
- // 双重保证
62
- setTimeout(doScroll, 0);
63
- }
64
- }, [containerRef.current]);
68
+ scrollBottomNow(2);
69
+ }, [scrollBottomNow]);
65
70
 
66
71
  // 智能滚动到底部(只在非用户交互时执行)
67
72
  var throttledScrollToBottom = React.useCallback(throttle(function () {
@@ -78,15 +83,24 @@ var useScroll = function useScroll(containerRef) {
78
83
  markUserInteraction(false); // 重置为用户非交互状态
79
84
 
80
85
  if (animate) {
81
- container.scrollTo({
82
- top: container.scrollHeight,
83
- behavior: 'smooth'
84
- });
86
+ var smoothScroll = function smoothScroll() {
87
+ var current = containerRef.current;
88
+ if (!current) return;
89
+ current.scrollTo({
90
+ top: current.scrollHeight,
91
+ behavior: 'smooth'
92
+ });
93
+ };
94
+ smoothScroll();
95
+ setTimeout(smoothScroll, 120);
96
+ setTimeout(function () {
97
+ if (!refs.isUserInteracting) scrollBottomNow(3);
98
+ }, 420);
85
99
  } else {
86
100
  scrollBottom();
87
101
  }
88
102
  }
89
- }, [containerRef.current, markUserInteraction, scrollBottom]);
103
+ }, [containerRef.current, markUserInteraction, refs, scrollBottom, scrollBottomNow]);
90
104
 
91
105
  // 更新滚动位置
92
106
  var updateScrollHeight = React.useCallback(function () {
@@ -102,6 +116,7 @@ var useScroll = function useScroll(containerRef) {
102
116
  var handleWheel = React.useCallback(throttle(function (e) {
103
117
  var container = containerRef.current;
104
118
  if (!container) return;
119
+ refs.lastWheelAt = Date.now();
105
120
  var scrollTop = container.scrollTop,
106
121
  scrollHeight = container.scrollHeight,
107
122
  clientHeight = container.clientHeight;
@@ -113,7 +128,7 @@ var useScroll = function useScroll(containerRef) {
113
128
  } else if (e.deltaY > 2 && position <= bottomPosition) {
114
129
  markUserInteraction(false);
115
130
  }
116
- }, 100), [containerRef.current, bottomPosition, markUserInteraction]);
131
+ }, 100), [containerRef.current, bottomPosition, markUserInteraction, refs]);
117
132
  var handleScroll = React.useCallback(throttle(function () {
118
133
  if (!containerRef.current) return;
119
134
  var _containerRef$current2 = containerRef.current,
@@ -123,10 +138,14 @@ var useScroll = function useScroll(containerRef) {
123
138
  var position = Math.max(0, scrollHeight - clientHeight - scrollTop);
124
139
  setCurrentPosition(position);
125
140
  setIsScrolling(true);
141
+ refs.wasAtBottom = position <= bottomPosition * 2;
126
142
  var scrollDelta = scrollTop - refs.lastScrollTop;
127
143
  if (Math.abs(scrollDelta) > 2) {
128
144
  if (scrollDelta < 0) {
129
- markUserInteraction(true);
145
+ var now = Date.now();
146
+ if (refs.isUserInteracting || now - refs.lastWheelAt < 600) {
147
+ markUserInteraction(true);
148
+ }
130
149
  } else if (scrollDelta > 0 && position <= bottomPosition) {
131
150
  markUserInteraction(false);
132
151
  }
@@ -168,15 +187,8 @@ var useScroll = function useScroll(containerRef) {
168
187
  scrollHeight = container.scrollHeight,
169
188
  clientHeight = container.clientHeight;
170
189
  var distanceToBottom = scrollHeight - clientHeight - scrollTop;
171
-
172
- // 如果之前就在底部附近,则在新内容加入时保持在底部
173
- // 注意:这里的判定逻辑需要考虑到新内容可能还没撑开高度
174
- // 所以我们直接尝试在下一帧滚动
175
- if (distanceToBottom <= bottomPosition * 5) {
176
- // 稍微放宽判定范围,因为新内容可能已经改变了 scrollHeight
177
- requestAnimationFrame(function () {
178
- container.scrollTop = container.scrollHeight;
179
- });
190
+ if (refs.wasAtBottom || distanceToBottom <= bottomPosition * 5) {
191
+ scrollBottomNow(4);
180
192
  }
181
193
  });
182
194
  observer.observe(container, {
@@ -187,7 +199,7 @@ var useScroll = function useScroll(containerRef) {
187
199
  return function () {
188
200
  return observer.disconnect();
189
201
  };
190
- }, [containerRef.current, bottomPosition]);
202
+ }, [containerRef.current, bottomPosition, refs, scrollBottomNow]);
191
203
  return [throttledScrollToBottom, scrollBottomForce, currentPosition, scrollBottom, updateScrollHeight, isScrolling, isUserScroll];
192
204
  };
193
205
  export default useScroll;