Files
kefu/deploy/ui-performance-20260916/fix_search_focus.py
T
2026-09-21 10:34:06 +08:00

54 lines
3.0 KiB
Python

from pathlib import Path
folder=Path(__file__).resolve().parent
for root in [folder/'payload',Path('C:/wechat_rpa')]:
p=root/'assets/ui/console-app.js';s=p.read_text(encoding='utf-8')
old=''' const scrollHost = document.getElementById("main");
if (scrollHost) pageScroll.set(previousView,scrollHost.scrollTop);'''
new=''' const scrollHost = document.getElementById("main");
const active=document.activeElement;
const keepInput=sameView && active && ["archive-search","event-search","wechat-db-search"].includes(active.id)
? {element:active,start:active.selectionStart,end:active.selectionEnd} : null;
if (scrollHost) pageScroll.set(previousView,scrollHost.scrollTop);'''
assert old in s;s=s.replace(old,new)
old=''' restoreDrafts(name);
normalizeReferenceView();'''
new=''' restoreDrafts(name);
if (keepInput){
const placeholder=document.getElementById(keepInput.element.id);
if (placeholder){
placeholder.replaceWith(keepInput.element);
keepInput.element.focus({preventScroll:true});
if (keepInput.start!=null) keepInput.element.setSelectionRange(keepInput.start,keepInput.end);
}
}
normalizeReferenceView();'''
assert old in s;s=s.replace(old,new)
old=''' if ((typing || composing) && (STATE.view || prev)===prev) {'''
new=''' const liveSearch=typing && ["archive-search","event-search","wechat-db-search"].includes(active.id);
if ((composing || (typing && !liveSearch)) && (STATE.view || prev)===prev) {'''
assert old in s;s=s.replace(old,new)
old='''window.showConsoleView = function(name){
if (name && name === STATE.view'''
new='''window.showConsoleView = function(name){
window.__pendingNavigation=null;
window.__navigationSerial=(window.__navigationSerial || 0)+1;
flushEdits();
if (name && name === STATE.view'''
assert old in s;s=s.replace(old,new)
p.write_text(s,encoding='utf-8',newline='\n')
p=folder/'browser.mjs';s=p.read_text(encoding='utf-8')
point=" const p=await page('archive');\n const benchmark="
new=''' await check('搜索结果刷新保留正在输入的搜索框和光标',async()=>{
const p=await page('wechatdb');await p.locator('#wechat-db-search').fill('模拟查询');
const ok=await p.evaluate(()=>{const input=document.getElementById('wechat-db-search');input.setSelectionRange(2,2);
applyState({view:'wechatdb',wechatDb:{query:'模拟查询',loading:false,refreshedAt:'刚刚',accountCount:1,
accounts:[{id:'a',name:'命中账号',databaseCount:1,messageCount:10,conversationCount:1}],conversations:[],messages:[],files:[]}});
return document.getElementById('wechat-db-search')===input&&document.activeElement===input&&input.selectionStart===2&&document.body.textContent.includes('命中账号');
});await p.close();return ok;
});
const p=await page('archive');
const benchmark='''
assert point in s;s=s.replace(point,new)
p.write_text(s,encoding='utf-8',newline='\n')
print('Search updates now retain the active control and cursor')