Skip to content

Commit 5695a52

Browse files
committed
修复注释
1 parent 66d351d commit 5695a52

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

‎frontend/js/components/chat-ui.js‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const dom = {
1212
};
1313

1414
let onSend = null;
15-
/** 初始化聊天 UI 所需的 DOM 引用 */
15+
16+
/**
17+
* 初始化聊天 UI,接收 DOM 引用和回调函数。
18+
*/
1619
function initChatUI({
1720
chatContainer,
1821
messageInput,
@@ -35,12 +38,12 @@ function initChatUI({
3538
syncComposerState();
3639
}
3740

38-
/** 根据输入内容与忙碌状态同步发送按钮 */
41+
/** 根据输入内容与忙碌状态同步发送按钮的禁用状态 */
3942
function syncComposerState() {
4043
dom.sendButton.disabled = dom.messageInput.disabled || !dom.messageInput.value.trim();
4144
}
4245

43-
/** 根据内容自动调整输入框高度 */
46+
/** 根据内容自动调整输入框高度,支持多行输入 */
4447
function resizeComposer() {
4548
dom.messageInput.style.height = 'auto';
4649
dom.messageInput.style.height = `${dom.messageInput.scrollHeight}px`;
@@ -53,7 +56,7 @@ function submitComposer() {
5356
onSend(content);
5457
}
5558

56-
/** 绑定输入区的局部交互 */
59+
/** 绑定输入区的局部交互事件(点击发送、回车发送、输入监听) */
5760
function bindComposerEvents() {
5861
dom.sendButton.addEventListener('click', submitComposer);
5962

@@ -77,7 +80,7 @@ function clearComposer() {
7780
syncComposerState();
7881
}
7982

80-
/** 设置输入区忙碌状态 */
83+
/** 设置输入区忙碌状态(禁用/启用输入框) */
8184
function setComposerBusy(isBusy) {
8285
dom.messageInput.disabled = isBusy;
8386
syncComposerState();
@@ -99,25 +102,20 @@ function setConnectionStatus(isConnected) {
99102

100103
let scrollScheduled = false;
101104

102-
/** 滚动聊天容器到底部 */
103-
function scrollToBottom() {
104-
dom.chatContainer.scrollTop = dom.chatContainer.scrollHeight;
105-
}
106-
107-
/** 节流滚动到底部,避免流式更新时频繁触发 */
105+
/** 节流滚动到底部,通过 requestAnimationFrame 避免频繁触发 */
108106
function scheduleScrollToBottom() {
109107
if (scrollScheduled) return;
110108
scrollScheduled = true;
111109
requestAnimationFrame(() => {
112110
scrollScheduled = false;
113-
scrollToBottom();
111+
dom.chatContainer.scrollTop = dom.chatContainer.scrollHeight;
114112
});
115113
}
116114

117-
/** 追加消息到聊天区并滚到底部 */
115+
/** 追加消息到聊天区并触发节流滚动 */
118116
function appendMessage(element) {
119117
dom.chatContainer.appendChild(element);
120-
scrollToBottom();
118+
scheduleScrollToBottom();
121119
}
122120

123121
/** 创建 assistant 消息元素(历史、流式、错误消息统一入口) */
@@ -139,7 +137,7 @@ function createAssistantMessageElement({ content = '', reasoning = '' } = {}) {
139137
return { el, messageContent, thinkingSection, thinkingContent };
140138
}
141139

142-
/** 创建用户消息元素。 */
140+
/** 创建用户消息元素 */
143141
function createUserMessageElement(message) {
144142
const el = document.createElement('div');
145143
el.className = 'message user-message';
@@ -150,7 +148,7 @@ function createUserMessageElement(message) {
150148
return el;
151149
}
152150

153-
/** 更新流式 assistant 消息(流式阶段用纯文本展示)。 */
151+
/** 更新流式 assistant 消息(流式阶段用纯文本展示) */
154152
function updateAssistantMessage(refs, { content, reasoning }) {
155153
if (reasoning) {
156154
refs.thinkingSection.hidden = false;
@@ -162,14 +160,15 @@ function updateAssistantMessage(refs, { content, reasoning }) {
162160
scheduleScrollToBottom();
163161
}
164162

165-
/** 流式结束后保留纯文本显示。 */
163+
/** 流式结束后保留纯文本显示 */
166164
function finalizeAssistantMessage(refs, content) {
167165
if (content) {
168166
refs.messageContent.textContent = content;
169167
}
170-
scrollToBottom();
168+
scheduleScrollToBottom();
171169
}
172170

171+
/** 创建并显示"正在思考"指示器 */
173172
function createTypingIndicator() {
174173
const indicator = dom.typingIndicatorTemplate.content.firstElementChild.cloneNode(true);
175174
appendMessage(indicator);
@@ -215,7 +214,7 @@ function showAssistantError(message) {
215214
appendMessage(createAssistantMessageElement({ content: message }).el);
216215
}
217216

218-
/** 追加用户消息到聊天区(自动移除空状态)。 */
217+
/** 追加用户消息到聊天区(自动移除空状态) */
219218
function addUserMessage(message) {
220219
const emptyState = dom.chatContainer.querySelector('.empty-state');
221220
if (emptyState) emptyState.remove();
@@ -231,4 +230,4 @@ export {
231230
setConnectionStatus,
232231
showAssistantError,
233232
createAssistantResponseView
234-
};
233+
};

0 commit comments

Comments
 (0)