From a8d1b24dc9d7df3c770aab5f5bdb3b58b55a54be Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 1 Jun 2026 00:14:49 +0800 Subject: [PATCH] refactor(webui): extract log view behavior --- webui/app.js | 139 ------------------------------------------ webui/index.html | 9 +-- webui/js/log_views.js | 139 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 143 deletions(-) create mode 100644 webui/js/log_views.js diff --git a/webui/app.js b/webui/app.js index efa7314..cc87d5a 100644 --- a/webui/app.js +++ b/webui/app.js @@ -579,145 +579,6 @@ function initTabs() { } } -function classifyLogLine(line) { - const s = String(line || ""); - if (!s) return ""; - // 错误优先 - if (/\b(ERROR|FATAL|CRITICAL|TRACEBACK|EXCEPTION|FAILED|FAIL\b)\b/i.test(s)) return "logError"; - if (/(失败|错误|异常|未知错误|超时|崩溃)/.test(s)) return "logError"; - if (/❌/.test(s)) return "logError"; - // 警告 - if (/\b(WARN(ING)?)\b/i.test(s)) return "logWarn"; - if (/(警告|注意)/.test(s)) return "logWarn"; - if (/⚠️/.test(s)) return "logWarn"; - // 成功 - if (/\b(SUCCESS|OK|DONE|COMPLETED?)\b/i.test(s)) return "logSuccess"; - if (/(成功|已保存|完成|启动成功)/.test(s)) return "logSuccess"; - if (/(✅|✓)/.test(s)) return "logSuccess"; - // Debug - if (/\b(DEBUG|TRACE)\b/i.test(s)) return "logDebug"; - // INFO 默认 - if (/\b(INFO)\b/i.test(s)) return "logInfo"; - return ""; -} - -function renderLogLineNode(line) { - const cls = classifyLogLine(line); - if (!cls) { - // 普通行:直接文本节点(性能最佳) - return document.createTextNode(String(line ?? "") + "\n"); - } - const span = document.createElement("span"); - span.className = "logLine " + cls; - span.textContent = String(line ?? "") + "\n"; - return span; -} - -function appendLog(lines) { - const log = $("runLog"); - if (!lines || lines.length === 0) return; - - // 记录更新前与底部的距离,用于在非底部时保持视图位置 - const distanceFromBottom = log.scrollHeight - log.scrollTop - log.clientHeight; - // 是否严格位于底部(仅在几像素内视为底部),避免每次刷新都跳到底部 - const wasAtBottom = distanceFromBottom < UI_LIMITS.scrollBottomThresholdPx; - - // 直接追加到 DOM,同时维护 runLogBuffer 以便支持搜索 - for (const ln of lines) runLogBuffer.push(ln); - // 限制缓冲 - if (runLogBuffer.length > runLogMaxLines) runLogBuffer = runLogBuffer.slice(-runLogMaxLines); - - // 如果当前处于 run search 结果显示状态,则不改变显示(保持搜索结果不被实时追加打断) - if (runSearchActive && runLogMode === 'search') { - // 仍保持缓冲更新,但不修改 DOM - } else if (runLogFilter) { - // 有过滤器时重渲染整个视图 - renderRunLog(); - } else { - // 批量追加:用 fragment + 行级 span 着色 - const frag = document.createDocumentFragment(); - for (const ln of lines) frag.appendChild(renderLogLineNode(ln)); - log.appendChild(frag); - } - - // 性能优化:限制日志长度,避免内存溢出(保留最近较多字符以便桌面端查看) - const maxLength = UI_LIMITS.runLogMaxChars; - if (log.textContent.length > maxLength) { - // 直接基于缓冲重渲染(截断到最新行) - runLogBuffer = runLogBuffer.slice(-Math.ceil(runLogMaxLines * 0.6)); - renderRunLog(); - if (!wasAtBottom) { - const newScrollTop = log.scrollHeight - log.clientHeight - distanceFromBottom; - log.scrollTop = Math.max(0, newScrollTop); - } - } - - // 只有在确实位于底部时才自动滚动到最新内容,避免打断用户查看历史日志 - if (wasAtBottom) { - log.scrollTop = log.scrollHeight; - } -} - -function renderRunLog() { - const el = $("runLog"); - if (!el) return; - const renderInto = (lines) => { - el.textContent = ""; - if (!lines.length) return; - const frag = document.createDocumentFragment(); - for (const ln of lines) frag.appendChild(renderLogLineNode(ln)); - el.appendChild(frag); - }; - - // 如果处于搜索结果显示状态,优先显示 search 返回的结果 - if (runSearchActive && runLogMode === 'search') { - const lines = runSearchResultsBuffer || []; - const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight; - renderInto(lines); - if (prevScrollBottom < UI_LIMITS.scrollBottomThresholdPx) el.scrollTop = el.scrollHeight; - else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom); - return; - } - - const filter = (runLogFilter || "").toLowerCase(); - let lines = runLogBuffer; - if (filter) { - lines = runLogBuffer.filter(l => String(l || "").toLowerCase().includes(filter)); - } - const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight; - renderInto(lines); - if (prevScrollBottom < UI_LIMITS.scrollBottomThresholdPx) el.scrollTop = el.scrollHeight; - else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom); -} - -function appendRunLogLines(lines) { - if (!Array.isArray(lines) || lines.length === 0) return; - // 使用 appendLog 让现有的 UI 消息也能写入缓冲并利用已有行为 - appendLog(lines); -} - -async function fetchActionsLog() { - try { - const data = await apiGet('/api/actions?lines=500'); - const lines = Array.isArray(data.lines) ? data.lines : []; - actionsLogBuffer = lines.slice(-actionsLogMaxLines); - renderActionsLog(); - } catch (e) { - console.error('fetchActionsLog error', e); - } -} - -function renderActionsLog() { - const el = $("actionsLog"); - if (!el) return; - const filter = (actionsLogFilter || "").toLowerCase(); - let lines = actionsLogBuffer; - if (filter) lines = actionsLogBuffer.filter(l => String(l || "").toLowerCase().includes(filter)); - el.textContent = lines.join("\n") + (lines.length ? "\n" : ""); - // 自动滚到底部 - el.scrollTop = el.scrollHeight; -} - function attachNumberSteppers() { // 给所有 .input[type=number] 包裹 .numStepperWrap + 添加 +/- 按钮 const SVG_UP = ''; diff --git a/webui/index.html b/webui/index.html index 5cda70a..a28f609 100644 --- a/webui/index.html +++ b/webui/index.html @@ -1278,9 +1278,10 @@ - - - - + + + + + diff --git a/webui/js/log_views.js b/webui/js/log_views.js new file mode 100644 index 0000000..97e45dd --- /dev/null +++ b/webui/js/log_views.js @@ -0,0 +1,139 @@ +// Run and action log view helpers shared by WebUI classic scripts. +function classifyLogLine(line) { + const s = String(line || ""); + if (!s) return ""; + // 错误优先 + if (/\b(ERROR|FATAL|CRITICAL|TRACEBACK|EXCEPTION|FAILED|FAIL\b)\b/i.test(s)) return "logError"; + if (/(失败|错误|异常|未知错误|超时|崩溃)/.test(s)) return "logError"; + if (/❌/.test(s)) return "logError"; + // 警告 + if (/\b(WARN(ING)?)\b/i.test(s)) return "logWarn"; + if (/(警告|注意)/.test(s)) return "logWarn"; + if (/⚠️/.test(s)) return "logWarn"; + // 成功 + if (/\b(SUCCESS|OK|DONE|COMPLETED?)\b/i.test(s)) return "logSuccess"; + if (/(成功|已保存|完成|启动成功)/.test(s)) return "logSuccess"; + if (/(✅|✓)/.test(s)) return "logSuccess"; + // Debug + if (/\b(DEBUG|TRACE)\b/i.test(s)) return "logDebug"; + // INFO 默认 + if (/\b(INFO)\b/i.test(s)) return "logInfo"; + return ""; +} + +function renderLogLineNode(line) { + const cls = classifyLogLine(line); + if (!cls) { + // 普通行:直接文本节点(性能最佳) + return document.createTextNode(String(line ?? "") + "\n"); + } + const span = document.createElement("span"); + span.className = "logLine " + cls; + span.textContent = String(line ?? "") + "\n"; + return span; +} + +function appendLog(lines) { + const log = $("runLog"); + if (!lines || lines.length === 0) return; + + // 记录更新前与底部的距离,用于在非底部时保持视图位置 + const distanceFromBottom = log.scrollHeight - log.scrollTop - log.clientHeight; + // 是否严格位于底部(仅在几像素内视为底部),避免每次刷新都跳到底部 + const wasAtBottom = distanceFromBottom < UI_LIMITS.scrollBottomThresholdPx; + + // 直接追加到 DOM,同时维护 runLogBuffer 以便支持搜索 + for (const ln of lines) runLogBuffer.push(ln); + // 限制缓冲 + if (runLogBuffer.length > runLogMaxLines) runLogBuffer = runLogBuffer.slice(-runLogMaxLines); + + // 如果当前处于 run search 结果显示状态,则不改变显示(保持搜索结果不被实时追加打断) + if (runSearchActive && runLogMode === 'search') { + // 仍保持缓冲更新,但不修改 DOM + } else if (runLogFilter) { + // 有过滤器时重渲染整个视图 + renderRunLog(); + } else { + // 批量追加:用 fragment + 行级 span 着色 + const frag = document.createDocumentFragment(); + for (const ln of lines) frag.appendChild(renderLogLineNode(ln)); + log.appendChild(frag); + } + + // 性能优化:限制日志长度,避免内存溢出(保留最近较多字符以便桌面端查看) + const maxLength = UI_LIMITS.runLogMaxChars; + if (log.textContent.length > maxLength) { + // 直接基于缓冲重渲染(截断到最新行) + runLogBuffer = runLogBuffer.slice(-Math.ceil(runLogMaxLines * 0.6)); + renderRunLog(); + if (!wasAtBottom) { + const newScrollTop = log.scrollHeight - log.clientHeight - distanceFromBottom; + log.scrollTop = Math.max(0, newScrollTop); + } + } + + // 只有在确实位于底部时才自动滚动到最新内容,避免打断用户查看历史日志 + if (wasAtBottom) { + log.scrollTop = log.scrollHeight; + } +} + +function renderRunLog() { + const el = $("runLog"); + if (!el) return; + const renderInto = (lines) => { + el.textContent = ""; + if (!lines.length) return; + const frag = document.createDocumentFragment(); + for (const ln of lines) frag.appendChild(renderLogLineNode(ln)); + el.appendChild(frag); + }; + + // 如果处于搜索结果显示状态,优先显示 search 返回的结果 + if (runSearchActive && runLogMode === 'search') { + const lines = runSearchResultsBuffer || []; + const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight; + renderInto(lines); + if (prevScrollBottom < UI_LIMITS.scrollBottomThresholdPx) el.scrollTop = el.scrollHeight; + else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom); + return; + } + + const filter = (runLogFilter || "").toLowerCase(); + let lines = runLogBuffer; + if (filter) { + lines = runLogBuffer.filter(l => String(l || "").toLowerCase().includes(filter)); + } + const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight; + renderInto(lines); + if (prevScrollBottom < UI_LIMITS.scrollBottomThresholdPx) el.scrollTop = el.scrollHeight; + else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom); +} + +function appendRunLogLines(lines) { + if (!Array.isArray(lines) || lines.length === 0) return; + // 使用 appendLog 让现有的 UI 消息也能写入缓冲并利用已有行为 + appendLog(lines); +} + +async function fetchActionsLog() { + try { + const data = await apiGet('/api/actions?lines=500'); + const lines = Array.isArray(data.lines) ? data.lines : []; + actionsLogBuffer = lines.slice(-actionsLogMaxLines); + renderActionsLog(); + } catch (e) { + console.error('fetchActionsLog error', e); + } +} + +function renderActionsLog() { + const el = $("actionsLog"); + if (!el) return; + const filter = (actionsLogFilter || "").toLowerCase(); + let lines = actionsLogBuffer; + if (filter) lines = actionsLogBuffer.filter(l => String(l || "").toLowerCase().includes(filter)); + el.textContent = lines.join("\n") + (lines.length ? "\n" : ""); + // 自动滚到底部 + el.scrollTop = el.scrollHeight; +}