Fix Web 2.0.3 log panel layout
This commit is contained in:
+46
-31
@@ -3,7 +3,7 @@ import {apiPath, webPath} from './paths.js';
|
||||
import {el, $, $$, clone, getPath, setPath, pathKey, textValue, number, chinaTime, download, csvBlob, optionsList, detailsList, tabKeys} from './dom.js';
|
||||
import {PageView, PAGES} from './pages.js';
|
||||
import {notice, dialog, confirm, filePicker, fileBase64} from './ui.js';
|
||||
import {createLogState, mergeLogs, visibleLogs, syncLogLines, logText, attachTaskLog} from './task-logs.js';
|
||||
import {createLogState, logView, mergeLogs, visibleLogs, syncLogLines, logText} from './task-logs.js';
|
||||
|
||||
const state = {accounts:new Map(),active:null,schema:null,meta:null,ready:false,polling:false,seq:0,redirecting:false,webStatusError:null,statusMessage:'连接中'};
|
||||
const current = () => state.accounts.get(state.active);
|
||||
@@ -174,7 +174,7 @@ function editConfig(account,path,value,field={},row) {
|
||||
syncSaveStatus(account);
|
||||
// Update other controls, while every control protects its own focused input.
|
||||
account.pages.forEach(page=>page.data&&page.render(page.data));
|
||||
if (path[0]==='log_visible'||path[0]==='log_sizes') applyLogLayout(account);
|
||||
if (path[0]==='log_sizes') applyLogLayout(account);
|
||||
}
|
||||
|
||||
async function flushConfig(account=current()) {
|
||||
@@ -261,8 +261,8 @@ async function perform(account,operation,payload={},options={}) {
|
||||
const page=options.page || account.pages.get(account.page);if(page)await loadPage(page,true);
|
||||
if(operation==='login'&&result)account.pages.get('login')?.login?.update(result);
|
||||
const message=loginOperations.has(operation)?account.summary.running?'已读取角色,已自动开启策略,执行已配置且已勾选的功能;可在顶部“停止”。':'已读取角色。策略已停止,可在顶部“开启”继续执行已勾选功能。':result?.message;
|
||||
if(message)notice(message);
|
||||
setStatus(message || `${operationLabels[operation] || '操作'}已完成`);
|
||||
if(message && operation!=='refresh')notice(message);
|
||||
setStatus(operation==='refresh'?'状态已刷新':message || `${operationLabels[operation] || '操作'}已完成`);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -271,7 +271,6 @@ function getPage(account,key) {
|
||||
const page=new PageView(account,key,{edit:editConfig,perform,load:loadPage,action:pageAction,command,
|
||||
schema:pageKey=>state.schema?.pages?.find(page=>page.key===pageKey)?.form || []});
|
||||
account.pages.set(key,page);$('#page-host').append(page.root);
|
||||
attachTaskLog(page);
|
||||
}
|
||||
return account.pages.get(key);
|
||||
}
|
||||
@@ -292,18 +291,19 @@ async function loadPage(page,force=false) {
|
||||
|
||||
function selectPage(key) {
|
||||
const account=current();if(!account)return;
|
||||
saveLogScroll(account);
|
||||
if(account.page!==key)account.pages.get(account.page)?.login?.clearPassword();
|
||||
account.page=key;
|
||||
state.accounts.forEach(other=>other.pages.forEach(page=>{page.root.hidden=other.id!==account.id||page.key!==key;}));
|
||||
const page=getPage(account,key);page.root.hidden=false;
|
||||
page.taskLog?.render();
|
||||
renderLogs();
|
||||
$$('.page-tab').forEach(tab=>{const selected=tab.dataset.page===key;tab.setAttribute('aria-selected',String(selected));tab.tabIndex=selected?0:-1;});
|
||||
loadPage(page,true);
|
||||
}
|
||||
|
||||
async function selectAccount(id) {
|
||||
const previous=current();previous?.pages.get('login')?.login?.clearPassword();
|
||||
if(previous && !$('#log-panel').hidden && $('#log-lines').dataset.account===previous.id)previous.logs.total.scrollTop=$('#log-lines').scrollTop;
|
||||
saveLogScroll(previous);
|
||||
if(previous?.edits.size)flushConfig(previous).catch(error);
|
||||
state.active=id;const account=current();if(!account)return;
|
||||
$('#page-host').querySelector('.initial-state')?.remove();
|
||||
@@ -378,11 +378,11 @@ async function exportYields(account=current()) {
|
||||
async function command(name,account=current()) {
|
||||
try {
|
||||
if(name==='add-account'||name==='add-demo')return await addAccount(name==='add-demo');
|
||||
if(name==='about')return dialog('关于牧场助手',el('div',{},el('p',{text:`牧羊人之心牧场助手 Web ${state.meta?.version || '2.0.2'}`}),el('p',{text:`游戏数据版本 ${state.meta?.gameVersion || '1.9.68'}`}),el('p',{class:'info-note',text:'与桌面助手共用核心策略。配置在服务端自动保存;每次成功登录后自动执行已勾选功能,可在顶部“停止”。服务重启后须先成功登录才执行。'})),{info:true});
|
||||
if(name==='about')return dialog('关于牧场助手',el('div',{},el('p',{text:`牧羊人之心牧场助手 Web ${state.meta?.version || '2.0.3'}`}),el('p',{text:`游戏数据版本 ${state.meta?.gameVersion || '1.9.68'}`}),el('p',{class:'info-note',text:'与桌面助手共用核心策略。配置在服务端自动保存;每次成功登录后自动执行已勾选功能,可在顶部“停止”。服务重启后须先成功登录才执行。'})),{info:true});
|
||||
if(name==='updates')return await showUpdates();
|
||||
if(name==='admin-logout'){hideWorkbench();try{await api.logout();}finally{requireAuth();}return;}
|
||||
if(!account)throw new Error('请先新增或选择账号。');
|
||||
if(name==='debug'){account.logs.debug=!account.logs.debug;account.logs.next=0;account.logs.generation=(account.logs.generation || 0)+1;$('#debug-log').checked=account.logs.debug;setLogVisible(true);renderLogs(true);currentPage()?.taskLog?.render();await fetchLogs(account);return;}
|
||||
if(name==='debug'){account.logs.debug=!account.logs.debug;account.logs.next=0;account.logs.generation=(account.logs.generation || 0)+1;$('#debug-log').checked=account.logs.debug;renderLogs(true);await fetchLogs(account);return;}
|
||||
if(name==='save'){await flushConfig(account);notice('策略已保存。');return;}
|
||||
if(name==='stop'||name==='start')return await perform(account,name);
|
||||
if(name==='start-all'||name==='stop-all'){
|
||||
@@ -475,17 +475,24 @@ async function showUpdates() {
|
||||
}
|
||||
|
||||
function setLogVisible(visible) {
|
||||
const account=current();if(account?.config)editConfig(account,['log_visible'],visible);
|
||||
else applyLogLayout(null,visible);
|
||||
const account=current();if(!account)return;
|
||||
saveLogScroll(account);
|
||||
account.logs.visible=Boolean(visible);
|
||||
applyLogLayout(account);
|
||||
}
|
||||
|
||||
function applyLogLayout(account=current(),visibleOverride) {
|
||||
function saveLogScroll(account=current()) {
|
||||
const root=$('#log-lines');
|
||||
if(account && !$('#log-panel').hidden && root.dataset.account===account.id && root.dataset.page===account.page)
|
||||
logView(account.logs,account.page).scrollTop=root.scrollTop;
|
||||
}
|
||||
|
||||
function applyLogLayout(account=current()) {
|
||||
if(account&&account.id!==state.active)return;
|
||||
const visible=visibleOverride ?? account?.config?.log_visible ?? true;
|
||||
if(!visible && account && !$('#log-panel').hidden && $('#log-lines').dataset.account===account.id)account.logs.total.scrollTop=$('#log-lines').scrollTop;
|
||||
$('#log-panel').hidden=!visible;$('#log-resizer').hidden=!visible;$('#log-toggle').textContent=visible?'隐藏总日志':'显示总日志';$('#log-toggle').setAttribute('aria-expanded',String(visible));
|
||||
const visible=Boolean(account?.logs.visible);
|
||||
$('#log-panel').hidden=!visible;$('#log-resizer').hidden=!visible;$('#log-toggle').textContent=visible?'隐藏日志':'显示日志';$('#log-toggle').setAttribute('aria-expanded',String(visible));
|
||||
const size=account?.config?.log_sizes?.[1];if(size)setLogHeight(size);
|
||||
if(visible)renderLogs();
|
||||
renderLogs();
|
||||
}
|
||||
|
||||
function setLogHeight(height) {
|
||||
@@ -494,28 +501,32 @@ function setLogHeight(height) {
|
||||
}
|
||||
|
||||
function renderLogs(reset=false) {
|
||||
const account=current(),root=$('#log-lines');if(!account){root.replaceChildren();return;}
|
||||
const account=current(),root=$('#log-lines');
|
||||
const title=`${PAGES.find(([key])=>key===account?.page)?.[1] || ''}日志`;
|
||||
$('#log-heading').textContent=title;$('#log-panel').setAttribute('aria-label',title);root.setAttribute('aria-label',`${title}内容`);
|
||||
$('#log-search').setAttribute('aria-label',`${title}搜索`);
|
||||
if(!account){root.replaceChildren();delete root.dataset.account;delete root.dataset.page;$('#log-count').textContent='';return;}
|
||||
const log=account.logs,view=logView(log,account.page);
|
||||
$('#log-follow').checked=view.follow;$('#debug-log').checked=log.debug;
|
||||
if($('#log-search').value!==view.search)$('#log-search').value=view.search;
|
||||
if($('#log-panel').hidden)return;
|
||||
const log=account.logs,view=log.total;
|
||||
const switching=root.dataset.account!==account.id;
|
||||
const switching=root.dataset.account!==account.id || root.dataset.page!==account.page;
|
||||
const scrollTop=switching?view.scrollTop:root.scrollTop;
|
||||
reset=reset || switching;
|
||||
const token=JSON.stringify([log.revision,view.clearedThrough,log.debug]);
|
||||
const token=JSON.stringify([log.revision,view.clearedThrough,log.debug,view.search]);
|
||||
if(!reset && view.rendered===token)return;
|
||||
const entries=visibleLogs(log,view);
|
||||
const entries=visibleLogs(log,view,account.page);
|
||||
syncLogLines(root,entries,{reset,follow:view.follow,scrollTop:reset?scrollTop:undefined,inspect:log.debug?entry=>showDebugEntry(account,entry):null});
|
||||
root.dataset.account=account.id;view.rendered=token;
|
||||
$('#log-follow').checked=view.follow;
|
||||
root.dataset.account=account.id;root.dataset.page=account.page;view.rendered=token;
|
||||
const count=`${Math.min(600,entries.length)} 条`;
|
||||
if($('#log-count').textContent!==count)$('#log-count').textContent=count;
|
||||
$('#debug-log').checked=log.debug;$('#log-heading').textContent=log.debug?'总日志(调试 / 已脱敏)':'总日志';
|
||||
}
|
||||
|
||||
async function fetchLogs(account) {
|
||||
if(account.logs.loading)return;account.logs.loading=true;const generation=account.logs.generation || 0;
|
||||
try{const response=await api.logs(account.id,account.logs.next,account.logs.debug);if(generation!==(account.logs.generation || 0))return;
|
||||
mergeLogs(account.logs,response.entries || [],response.next);
|
||||
if(account.id===state.active){renderLogs();currentPage()?.taskLog?.render();}
|
||||
if(account.id===state.active)renderLogs();
|
||||
transportError(account,'logs','日志',null);
|
||||
}catch(failure){if(generation===(account.logs.generation || 0))transportError(account,'logs','日志',failure);}finally{account.logs.loading=false;}
|
||||
}
|
||||
@@ -533,7 +544,7 @@ async function showDebugEntry(account,entry) {
|
||||
|
||||
function showNoAccounts() {
|
||||
state.active=null;$('#page-host').replaceChildren(el('div',{class:'initial-state'},el('p',{text:'尚未添加账号。新增账号后登录游戏,或使用离线演示查看工作台。'}),el('div',{class:'page-toolbar'},el('button',{text:'新增账号…',onClick:()=>command('add-account')}),el('button',{text:'新增离线演示账号',onClick:()=>command('add-demo')}))));
|
||||
renderAccountTabs();renderSummary();renderLogs(true);setStatus('等待新增账号');
|
||||
renderAccountTabs();renderSummary();applyLogLayout();setStatus('等待新增账号');
|
||||
}
|
||||
|
||||
async function syncAccounts() {
|
||||
@@ -562,6 +573,9 @@ async function tick() {
|
||||
}
|
||||
|
||||
function hideWorkbench() {
|
||||
saveLogScroll();
|
||||
state.accounts.forEach(account=>account.logs.visible=false);
|
||||
applyLogLayout();
|
||||
state.ready=false;
|
||||
$('#app').hidden=true;$('#notice').hidden=true;
|
||||
$$('dialog[open]').forEach(root=>root.close());
|
||||
@@ -581,7 +595,7 @@ async function bootstrap() {
|
||||
$('#app').hidden=false;
|
||||
const [meta,schema]=await Promise.all([api.meta(),api.schema()]);state.meta=meta;state.schema=schema;
|
||||
if(state.redirecting)return;
|
||||
$('#app-version').textContent=meta.version || '2.0.2';$('#game-version').textContent=meta.gameVersion || '1.9.68';$('#access-mode').textContent=auth.access_mode==='local'||auth.access_mode==='loopback'?'本机工作台':auth.required?'管理工作台':'本机工作台';
|
||||
$('#app-version').textContent=meta.version || '2.0.3';$('#game-version').textContent=meta.gameVersion || '1.9.68';$('#access-mode').textContent=auth.access_mode==='local'||auth.access_mode==='loopback'?'本机工作台':auth.required?'管理工作台':'本机工作台';
|
||||
await syncAccounts();if(state.redirecting)return;state.ready=true;setStatus('工作台已连接');
|
||||
}catch(failure){if(failure.status===401||$('#app').hidden){requireAuth();return;}if(state.redirecting)return;setStatus(failure.message);$('#page-host').replaceChildren(el('div',{class:'initial-state'},el('p',{text:failure.message}),el('button',{text:'重新连接',onClick:bootstrap})));}
|
||||
}
|
||||
@@ -599,10 +613,11 @@ $('#refresh-interval').addEventListener('change',event=>{if(current())editConfig
|
||||
$('#save-status').tabIndex=0;$('#save-status').addEventListener('click',()=>command('save'));$('#save-status').addEventListener('keydown',event=>{if(event.key==='Enter')command('save');});
|
||||
$('#debug-toggle').addEventListener('click',()=>command('debug'));$('#debug-log').addEventListener('change',()=>command('debug'));
|
||||
$('#log-toggle').addEventListener('click',()=>setLogVisible($('#log-panel').hidden));
|
||||
$('#clear-log').addEventListener('click',()=>{const log=current()?.logs;if(log){log.total.clearedThrough=log.highWater;renderLogs();}});
|
||||
$('#download-log').addEventListener('click',()=>{const log=current()?.logs;if(log)download(new Blob([logText(visibleLogs(log,log.total))],{type:'text/plain;charset=utf-8'}),'牧场日志.txt');});
|
||||
$('#log-lines').addEventListener('scroll',()=>{const node=$('#log-lines'),account=current();if(!account || $('#log-panel').hidden || node.dataset.account!==account.id)return;account.logs.total.scrollTop=node.scrollTop;if(node.scrollHeight-node.scrollTop-node.clientHeight>55){$('#log-follow').checked=false;account.logs.total.follow=false;}});
|
||||
$('#log-follow').addEventListener('change',()=>{const account=current(),node=$('#log-lines');if(!account || $('#log-panel').hidden || node.dataset.account!==account.id)return;account.logs.total.follow=$('#log-follow').checked;if($('#log-follow').checked)node.scrollTop=node.scrollHeight;});
|
||||
$('#clear-log').addEventListener('click',()=>{const account=current();if(account){logView(account.logs,account.page).clearedThrough=account.logs.highWater;renderLogs();}});
|
||||
$('#download-log').addEventListener('click',()=>{const account=current();if(account)download(new Blob([logText(visibleLogs(account.logs,logView(account.logs,account.page),account.page))],{type:'text/plain;charset=utf-8'}),`${$('#log-heading').textContent}.txt`);});
|
||||
$('#log-search').addEventListener('input',event=>{const account=current();if(account){logView(account.logs,account.page).search=event.target.value;renderLogs();}});
|
||||
$('#log-lines').addEventListener('scroll',()=>{const node=$('#log-lines'),account=current();if(!account || $('#log-panel').hidden || node.dataset.account!==account.id || node.dataset.page!==account.page)return;const view=logView(account.logs,account.page);view.scrollTop=node.scrollTop;if(node.scrollHeight-node.scrollTop-node.clientHeight>55){$('#log-follow').checked=false;view.follow=false;}});
|
||||
$('#log-follow').addEventListener('change',()=>{const account=current(),node=$('#log-lines');if(!account || $('#log-panel').hidden || node.dataset.account!==account.id || node.dataset.page!==account.page)return;const view=logView(account.logs,account.page);view.follow=$('#log-follow').checked;if(view.follow)node.scrollTop=node.scrollHeight;view.scrollTop=node.scrollTop;});
|
||||
let resize;
|
||||
$('#log-resizer').addEventListener('pointerdown',event=>{resize={y:event.clientY,height:$('#log-panel').getBoundingClientRect().height};event.currentTarget.setPointerCapture(event.pointerId);event.preventDefault();});
|
||||
$('#log-resizer').addEventListener('pointermove',event=>{if(resize)setLogHeight(resize.height+resize.y-event.clientY);});
|
||||
|
||||
Reference in New Issue
Block a user