Skip to content

Commit 640adc0

Browse files
committed
fix: 优化scroll height 计算
1 parent 600b392 commit 640adc0

File tree

5 files changed

+44
-54
lines changed

5 files changed

+44
-54
lines changed

apps/miniprogram-agent-ui/miniprogram/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ App({
99
// env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
1010
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
1111
// 如不填则使用默认环境(第一个创建的环境)
12-
env: "lowcode-1gm3ep4m027742a2",
12+
env: "luke-agent-dev-7g1nc8tqc2ab76af",
1313
traceUser: true,
1414
});
1515
}

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.js

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,14 @@ Component({
4343
showFeatureList: showWebSearchSwitch,
4444
});
4545
},
46-
showTools: function (isShow) {
47-
// console.log('showTools', isShow)
48-
if (isShow) {
49-
this.setData({
50-
footerHeight: this.data.footerHeight + 80,
51-
});
52-
} else {
53-
this.setData({
54-
footerHeight: this.data.footerHeight - 80,
55-
});
56-
}
46+
showTools: function () {
47+
wx.nextTick(() => this.calcScrollHeight())
5748
},
58-
showFileList: function (isShow) {
59-
console.log("showFileList", isShow);
60-
if (isShow) {
61-
this.setData({
62-
footerHeight: this.data.footerHeight + 80,
63-
});
64-
} else {
65-
this.setData({
66-
footerHeight: this.data.footerHeight - 80,
67-
});
68-
}
49+
showFileList: function () {
50+
wx.nextTick(() => this.calcScrollHeight())
6951
},
70-
showFeatureList: function (isShow) {
71-
console.log("showFeatureList", isShow);
72-
if (isShow) {
73-
this.setData({
74-
footerHeight: this.data.footerHeight + 30,
75-
});
76-
} else {
77-
const subHeight = this.data.footerHeight - 30;
78-
this.setData({
79-
footerHeight: subHeight >= 80 ? subHeight : 80,
80-
});
81-
}
52+
showFeatureList: function () {
53+
wx.nextTick(() => this.calcScrollHeight())
8254
},
8355
},
8456

@@ -103,7 +75,6 @@ Component({
10375
showFileList: false, // 展示输入框顶部文件行
10476
showTopBar: false, // 展示顶部bar
10577
sendFileList: [],
106-
footerHeight: 73,
10778
lastScrollTop: 0,
10879
showUploadFile: true,
10980
showUploadImg: true,
@@ -125,6 +96,7 @@ Component({
12596
textareaHeight: 50,
12697
curLineCount: 1,
12798
defaultErrorMsg: "网络繁忙,请稍后重试!",
99+
curScrollHeight: 0
128100
},
129101
attached: async function () {
130102
const chatMode = this.data.chatMode;
@@ -182,8 +154,30 @@ Component({
182154
this.setData({
183155
contentHeightInScrollViewTop: topHeight,
184156
});
157+
this.calcScrollHeight()
185158
},
186159
methods: {
160+
calcScrollHeight: async function () {
161+
// windowHeight - topHeight - footerHeight
162+
const topAndFooterHeight = await new Promise((resolve) => {
163+
const query = wx.createSelectorQuery().in(this);
164+
query
165+
.selectAll(".agent-ui >>> .navBar, .agent-ui >>> .footer")
166+
.boundingClientRect((rects) => {
167+
let totalHeight = 0;
168+
rects.forEach((rect) => {
169+
totalHeight += rect.height;
170+
});
171+
// console.log('top height', totalHeight);
172+
resolve(totalHeight);
173+
})
174+
.exec();
175+
});
176+
// console.log('this.data.windowInfo.windowHeight - topAndFooterHeight', this.data.windowInfo.windowHeight - topAndFooterHeight)
177+
this.setData({
178+
curScrollHeight: this.data.windowInfo.windowHeight - topAndFooterHeight
179+
})
180+
},
187181
showErrorMsg: function (e) {
188182
const { content } = e.currentTarget.dataset;
189183
console.log("content", content);
@@ -201,19 +195,10 @@ Component({
201195
.select(".foot_function")
202196
.boundingClientRect(function (res) {
203197
if (res) {
204-
if (res.height < self.data.textareaHeight) {
205-
self.setData({
206-
footerHeight: self.data.footerHeight - (self.data.textareaHeight - res.height),
207-
});
208-
}
209-
if (res.height > self.data.textareaHeight) {
210-
self.setData({
211-
footerHeight: self.data.footerHeight + (res.height - self.data.textareaHeight),
212-
});
213-
}
214198
self.setData({
215199
textareaHeight: res.height,
216200
});
201+
self.calcScrollHeight()
217202
} else {
218203
console.log("未找到指定元素");
219204
}
@@ -333,12 +318,10 @@ Component({
333318
});
334319
},
335320
autoToBottom: function () {
336-
// console.log("autoToBottom");
337321
this.setData({
338322
manualScroll: false,
339323
scrollTo: "scroll-bottom",
340324
});
341-
// console.log('scrollTop', this.data.scrollTop);
342325
},
343326
bindInputFocus: function (e) {
344327
this.setData({
@@ -1019,8 +1002,9 @@ Component({
10191002
}
10201003

10211004
// 只有当内容高度接近scroll 区域视口高度时才开始增加 scrollTop
1022-
const clientHeight =
1023-
this.data.windowInfo.windowHeight - this.data.footerHeight - (this.data.chatMode === "bot" ? 40 : 0); // 视口高度
1005+
// const clientHeight =
1006+
// this.data.windowInfo.windowHeight - this.data.footerHeight - (this.data.chatMode === "bot" ? 40 : 0); // 视口高度
1007+
const clientHeight = this.data.curScrollHeight
10241008
const contentHeight =
10251009
(await this.calculateContentHeight()) +
10261010
(this.data.contentHeightInScrollViewTop || (await this.calculateContentInTop())); // 内容总高度

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</view>
88
</view>
99
<!-- 聊天对话区 -->
10-
<scroll-view bindwheel="onWheel" enhanced="{{true}}" bindscroll="onScroll" binddragstart="handleScrollStart" class="main" style="height: {{windowInfo.windowHeight-footerHeight-(chatMode === 'bot' ? 40 : 0)}}px;" scroll-y="{{true}}" scroll-top="{{viewTop}}" scroll-into-view="{{ scrollTo }}" lower-threshold="1" bindscrolltolower="handleScrollToLower" show-scrollbar="{{false}}" refresher-enabled="{{showPullRefresh}}" refresher-threshold="{{80}}" bindrefresherrefresh="handelRefresh" refresher-triggered="{{triggered}}" bounces="{{false}}">
10+
<scroll-view bindwheel="onWheel" enhanced="{{true}}" bindscroll="onScroll" binddragstart="handleScrollStart" class="main" style="height: {{curScrollHeight}}px;" scroll-y="{{true}}" scroll-top="{{viewTop}}" scroll-into-view="{{ scrollTo }}" lower-threshold="1" bindscrolltolower="handleScrollToLower" show-scrollbar="{{false}}" refresher-enabled="{{showPullRefresh}}" refresher-threshold="{{80}}" bindrefresherrefresh="handelRefresh" refresher-triggered="{{triggered}}" bounces="{{false}}">
1111
<view wx:if="{{chatMode === 'bot' && showPullRefresh}}" class="tips">
1212
{{refreshText}}
1313
</view>

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.wxss

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
width: 750rpx;
44
height: 100vh;
55
position: relative;
6+
display: flex;
7+
flex-direction: column;
8+
overflow:hidden;
69
}
710

811
.nav {
@@ -11,6 +14,7 @@
1114
display: flex;
1215
flex-direction: column;
1316
align-items: center;
17+
flex-shrink: 0;
1418
}
1519

1620
.navBar {
@@ -88,6 +92,7 @@
8892
.main {
8993
background-color: #fff;
9094
/* padding-top:10px; */
95+
flex: 1;
9196
}
9297

9398
.share_btn {
@@ -122,9 +127,10 @@
122127
width: 100%;
123128
min-height: 80px;
124129
max-height: 380px;
130+
flex-shrink: 0;
125131
/* background-color: aquamarine; */
126-
position: absolute;
127-
bottom: 0;
132+
/* position: absolute; */
133+
/* bottom: 0; */
128134
}
129135

130136
.foot_function {

apps/miniprogram-agent-ui/miniprogram/pages/chatBot/chatBot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Page({
44
* 页面的初始数据
55
*/
66
data: {
7-
chatMode: "model", // bot 表示使用agent,model 表示使用大模型
7+
chatMode: "bot", // bot 表示使用agent,model 表示使用大模型
88
showBotAvatar: true, // 是否在对话框左侧显示头像
99
agentConfig: {
1010
botId: "bot-db3cab4a", // agent id,

0 commit comments

Comments
 (0)