马一丁

Fixed the Issue of HOST Information not being Displayed in Forum Engine

@@ -1379,6 +1379,12 @@ @@ -1379,6 +1379,12 @@
1379 const consoleLayers = {}; 1379 const consoleLayers = {};
1380 let activeConsoleLayer = currentApp; 1380 let activeConsoleLayer = currentApp;
1381 const logRenderers = {}; 1381 const logRenderers = {};
  1382 + const FORUM_SCROLL_REATTACH_DELAY = 3000;
  1383 + const FORUM_SCROLL_BOTTOM_THRESHOLD = 60;
  1384 + let forumMessagesCache = [];
  1385 + let forumAutoScrollEnabled = true;
  1386 + let forumScrollRestTimer = null;
  1387 + let forumScrollHandlerAttached = false;
1382 1388
1383 // 页面可见性状态管理 1389 // 页面可见性状态管理
1384 let isPageVisible = !document.hidden; 1390 let isPageVisible = !document.hidden;
@@ -3685,6 +3691,69 @@ function getConsoleContainer() { @@ -3685,6 +3691,69 @@ function getConsoleContainer() {
3685 console.log('[测试API] ===== 测试完成 ====='); 3691 console.log('[测试API] ===== 测试完成 =====');
3686 }; 3692 };
3687 3693
  3694 + function attachForumScrollHandler() {
  3695 + const chatArea = document.getElementById('forumChatArea');
  3696 + if (!chatArea || forumScrollHandlerAttached) return;
  3697 + forumScrollHandlerAttached = true;
  3698 +
  3699 + chatArea.addEventListener('scroll', () => {
  3700 + const nearBottom = chatArea.scrollHeight - chatArea.scrollTop - chatArea.clientHeight < FORUM_SCROLL_BOTTOM_THRESHOLD;
  3701 +
  3702 + if (nearBottom) {
  3703 + forumAutoScrollEnabled = true;
  3704 + if (forumScrollRestTimer) {
  3705 + clearTimeout(forumScrollRestTimer);
  3706 + forumScrollRestTimer = null;
  3707 + }
  3708 + } else {
  3709 + forumAutoScrollEnabled = false;
  3710 + if (forumScrollRestTimer) {
  3711 + clearTimeout(forumScrollRestTimer);
  3712 + }
  3713 + forumScrollRestTimer = setTimeout(() => {
  3714 + forumAutoScrollEnabled = true;
  3715 + scrollForumViewToBottom(true);
  3716 + }, FORUM_SCROLL_REATTACH_DELAY);
  3717 + }
  3718 + });
  3719 + }
  3720 +
  3721 + function applyForumMessages(parsedMessages, { reset = false } = {}) {
  3722 + const chatArea = document.getElementById('forumChatArea');
  3723 + if (!chatArea) return;
  3724 +
  3725 + const incoming = parsedMessages || [];
  3726 +
  3727 + // 文件被重置或主动要求刷新时清空
  3728 + if (reset || incoming.length < forumMessagesCache.length) {
  3729 + chatArea.innerHTML = '';
  3730 + forumMessagesCache = [];
  3731 + }
  3732 +
  3733 + if (incoming.length === 0) {
  3734 + forumMessagesCache = [];
  3735 + return;
  3736 + }
  3737 +
  3738 + // 初次渲染或缓存为空
  3739 + if (forumMessagesCache.length === 0) {
  3740 + forumMessagesCache = incoming.slice();
  3741 + incoming.forEach(msg => addForumMessage(msg, { suppressScroll: true }));
  3742 + scrollForumViewToBottom(true);
  3743 + return;
  3744 + }
  3745 +
  3746 + // 只追加新增的消息,避免滚动条跳动
  3747 + if (incoming.length > forumMessagesCache.length) {
  3748 + const newMessages = incoming.slice(forumMessagesCache.length);
  3749 + forumMessagesCache = incoming.slice();
  3750 + newMessages.forEach(msg => addForumMessage(msg, { suppressScroll: true }));
  3751 + if (forumAutoScrollEnabled) {
  3752 + scrollForumViewToBottom();
  3753 + }
  3754 + }
  3755 + }
  3756 +
3688 // 实时刷新论坛消息(适用于所有页面) 3757 // 实时刷新论坛消息(适用于所有页面)
3689 function refreshForumMessages() { 3758 function refreshForumMessages() {
3690 fetch('/api/forum/log') 3759 fetch('/api/forum/log')
@@ -3695,6 +3764,8 @@ function getConsoleContainer() { @@ -3695,6 +3764,8 @@ function getConsoleContainer() {
3695 const logLines = data.log_lines || []; 3764 const logLines = data.log_lines || [];
3696 const parsedMessages = data.parsed_messages || []; 3765 const parsedMessages = data.parsed_messages || [];
3697 3766
  3767 + const logShrunk = logLines.length < forumLogLineCount || parsedMessages.length < forumMessagesCache.length;
  3768 +
3698 if (logLines.length > forumLogLineCount) { 3769 if (logLines.length > forumLogLineCount) {
3699 const newLines = logLines.slice(forumLogLineCount); 3770 const newLines = logLines.slice(forumLogLineCount);
3700 newLines.forEach(line => { 3771 newLines.forEach(line => {
@@ -3702,15 +3773,7 @@ function getConsoleContainer() { @@ -3702,15 +3773,7 @@ function getConsoleContainer() {
3702 }); 3773 });
3703 } 3774 }
3704 3775
3705 - if (parsedMessages.length > 0) {  
3706 - const chatArea = document.getElementById('forumChatArea');  
3707 - if (chatArea) {  
3708 - chatArea.innerHTML = '';  
3709 - parsedMessages.forEach(message => {  
3710 - addForumMessage(message);  
3711 - });  
3712 - }  
3713 - } 3776 + applyForumMessages(parsedMessages, { reset: logShrunk });
3714 3777
3715 forumLogLineCount = logLines.length; 3778 forumLogLineCount = logLines.length;
3716 }) 3779 })
@@ -3723,6 +3786,7 @@ function getConsoleContainer() { @@ -3723,6 +3786,7 @@ function getConsoleContainer() {
3723 function initializeForum() { 3786 function initializeForum() {
3724 // 初始化时加载一次论坛日志 3787 // 初始化时加载一次论坛日志
3725 refreshForumMessages(); 3788 refreshForumMessages();
  3789 + attachForumScrollHandler();
3726 } 3790 }
3727 3791
3728 // 加载论坛日志 3792 // 加载论坛日志
@@ -3791,16 +3855,8 @@ function getConsoleContainer() { @@ -3791,16 +3855,8 @@ function getConsoleContainer() {
3791 .then(data => { 3855 .then(data => {
3792 if (!data.success) return; 3856 if (!data.success) return;
3793 3857
3794 - const chatArea = document.getElementById('forumChatArea');  
3795 - if (chatArea) {  
3796 - chatArea.innerHTML = '';  
3797 - }  
3798 -  
3799 const parsedMessages = data.parsed_messages || []; 3858 const parsedMessages = data.parsed_messages || [];
3800 - if (parsedMessages.length > 0) {  
3801 - parsedMessages.forEach(message => addForumMessage(message));  
3802 - }  
3803 - 3859 + applyForumMessages(parsedMessages, { reset: true });
3804 forumLogLineCount = data.log_lines ? data.log_lines.length : 0; 3860 forumLogLineCount = data.log_lines ? data.log_lines.length : 0;
3805 }); 3861 });
3806 }) 3862 })
@@ -3826,19 +3882,14 @@ function getConsoleContainer() { @@ -3826,19 +3882,14 @@ function getConsoleContainer() {
3826 3882
3827 const logLines = data.log_lines || []; 3883 const logLines = data.log_lines || [];
3828 const parsedMessages = data.parsed_messages || []; 3884 const parsedMessages = data.parsed_messages || [];
  3885 + const logShrunk = logLines.length < forumLogLineCount || parsedMessages.length < forumMessagesCache.length;
3829 3886
3830 if (logLines.length > forumLogLineCount) { 3887 if (logLines.length > forumLogLineCount) {
3831 const newLines = logLines.slice(forumLogLineCount); 3888 const newLines = logLines.slice(forumLogLineCount);
3832 newLines.forEach(line => appendConsoleTextLine('forum', line)); 3889 newLines.forEach(line => appendConsoleTextLine('forum', line));
3833 } 3890 }
3834 3891
3835 - if (parsedMessages.length && parsedMessages.length !== getForumMessageCount()) {  
3836 - const chatArea = document.getElementById('forumChatArea');  
3837 - if (chatArea) {  
3838 - chatArea.innerHTML = '';  
3839 - parsedMessages.forEach(message => addForumMessage(message));  
3840 - }  
3841 - } 3892 + applyForumMessages(parsedMessages, { reset: logShrunk });
3842 3893
3843 forumLogLineCount = logLines.length; 3894 forumLogLineCount = logLines.length;
3844 }) 3895 })
@@ -3992,8 +4043,10 @@ function getConsoleContainer() { @@ -3992,8 +4043,10 @@ function getConsoleContainer() {
3992 } 4043 }
3993 4044
3994 // 添加论坛消息到对话区 4045 // 添加论坛消息到对话区
3995 - function addForumMessage(data) { 4046 + function addForumMessage(data, options = {}) {
  4047 + const { prepend = false, suppressScroll = false } = options;
3996 const chatArea = document.getElementById('forumChatArea'); 4048 const chatArea = document.getElementById('forumChatArea');
  4049 + if (!chatArea) return;
3997 const messageDiv = document.createElement('div'); 4050 const messageDiv = document.createElement('div');
3998 4051
3999 const messageType = data.type || 'system'; 4052 const messageType = data.type || 'system';
@@ -4025,17 +4078,30 @@ function getConsoleContainer() { @@ -4025,17 +4078,30 @@ function getConsoleContainer() {
4025 <div class="forum-timestamp">${data.timestamp || new Date().toLocaleTimeString('zh-CN')}</div> 4078 <div class="forum-timestamp">${data.timestamp || new Date().toLocaleTimeString('zh-CN')}</div>
4026 `; 4079 `;
4027 4080
4028 - chatArea.appendChild(messageDiv); 4081 + if (prepend && chatArea.firstChild) {
  4082 + chatArea.insertBefore(messageDiv, chatArea.firstChild);
  4083 + } else {
  4084 + chatArea.appendChild(messageDiv);
  4085 + }
4029 4086
4030 - // 自动滚动到底部  
4031 - chatArea.scrollTop = chatArea.scrollHeight; 4087 + // 自动滚动到底部(除非用户正在浏览历史)
  4088 + if (!suppressScroll && forumAutoScrollEnabled) {
  4089 + scrollForumViewToBottom();
  4090 + }
4032 } 4091 }
4033 4092
4034 - function scrollForumViewToBottom() { 4093 + function scrollForumViewToBottom(force = false) {
4035 const renderer = logRenderers['forum']; 4094 const renderer = logRenderers['forum'];
4036 if (renderer) { 4095 if (renderer) {
4037 requestAnimationFrame(() => renderer.scrollToBottom()); 4096 requestAnimationFrame(() => renderer.scrollToBottom());
4038 } 4097 }
  4098 +
  4099 + if (force) {
  4100 + forumAutoScrollEnabled = true;
  4101 + } else if (!forumAutoScrollEnabled) {
  4102 + return;
  4103 + }
  4104 +
4039 const chatArea = document.getElementById('forumChatArea'); 4105 const chatArea = document.getElementById('forumChatArea');
4040 if (chatArea) { 4106 if (chatArea) {
4041 requestAnimationFrame(() => { 4107 requestAnimationFrame(() => {