Files
AIEC-new/AIEC-server/js/chat-input-height-observer.js
2025-10-17 09:31:28 +08:00

71 lines
2.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 动态监测聊天输入框高度变化
* 当输入框高度变化时如多行文本扩展自动更新CSS变量
*/
(function() {
'use strict';
document.addEventListener('DOMContentLoaded', function() {
// 获取聊天输入框容器
const chatInputContainer = document.getElementById('chatModeInput');
if (!chatInputContainer) {
console.log('Chat input container not found, will retry when chat mode is activated');
return;
}
// 创建 ResizeObserver 监测高度变化
const resizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
const height = entry.contentRect.height;
// 更新CSS变量
document.documentElement.style.setProperty('--chat-input-height', height + 'px');
// 调试信息
console.log('Chat input height updated:', height + 'px');
}
});
// 开始观察
resizeObserver.observe(chatInputContainer);
// 初始设置高度
const initialHeight = chatInputContainer.offsetHeight;
if (initialHeight > 0) {
document.documentElement.style.setProperty('--chat-input-height', initialHeight + 'px');
}
// 监听聊天模式切换
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
// 检查是否进入聊天模式
if (document.body.classList.contains('chat-mode')) {
// 重新计算高度
setTimeout(() => {
const chatInput = document.getElementById('chatModeInput');
if (chatInput) {
const height = chatInput.offsetHeight;
document.documentElement.style.setProperty('--chat-input-height', height + 'px');
}
}, 100);
}
}
});
});
// 观察body的class变化
observer.observe(document.body, { attributes: true });
});
// 处理窗口大小变化
window.addEventListener('resize', function() {
const chatInputContainer = document.getElementById('chatModeInput');
if (chatInputContainer && document.body.classList.contains('chat-mode')) {
const height = chatInputContainer.offsetHeight;
document.documentElement.style.setProperty('--chat-input-height', height + 'px');
}
});
})();