Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(web): 骨架只盖会变的计数,筛选条与排序条留在原位

longmeidao97bdd56f9842
diff --git a/tests/test_web_ui.py b/tests/test_web_ui.py
index 670334c45..5765f4d9f 100644
--- a/tests/test_web_ui.py
+++ b/tests/test_web_ui.py
@@ -1494,6 +1494,66 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageContains(".insighttablerow b{font-weight:500;color:var(--ink-2);font-variant-numeric:tabular-nums}")
self.assertPageContains(".insightdatatable td{font-variant-numeric:tabular-nums}")
+ def test_loading_state_only_covers_the_count_and_leaves_the_filter_bar_in_place(self):
+ """骨架只盖会变的计数,筛选条照常画成最终样子并接上事件。
+
+ 筛选条完全由当前 state 决定,这次请求不会改变它,所以没有可占位的东西:
+ 连它一起清空的话,刚点下的那一枚会在等数据的整段时间里失去高亮,看着像
+ 没点上;`.count:empty` 还会把整行折叠,网格跟着往上跳一截。
+ """
+ self.assertPageContains("const countSortsHtml=()=>!state?'':")
+ # 加载态与最终态取同一份筛选条,两边不可能画得不一样。
+ self.assertPageContains(
+ "count.innerHTML=state&&state.state==='trash'?''\n"
+ " :`<span class=\"mono\"><span class=\"countskeleton\"></span></span>`+countSortsHtml();\n"
+ " wireCountRow();")
+ self.assertPageContains(" +(trash?'':countSortsHtml());\n wireCountRow();")
+ self.assertPageLacks("count.textContent=''",
+ "加载态不能清空整行,筛选条要留在原位")
+ # 数据到位后摘掉忙碌标记,屏幕阅读器不再把这一行当成还在读取。
+ self.assertPageContains(
+ "$('#count').removeAttribute('aria-busy');$('#count').removeAttribute('aria-label');")
+
+ def test_the_count_placeholder_and_the_spinning_refresh_key_keep_the_row_height(self):
+ """计数骨架宽高定死,等数据时只有换批键在转,行高不变。
+
+ 骨架跟着真实文本走的话,数字回来那一刻整行会横向弹一下;14px 远低于筛选条
+ 按钮的 32px,而两条计数行都有 `min-height:var(--sortH)` 兜底,所以换成真
+ 数字既不改行高也不产生位移。
+ """
+ self.assertPageContains(
+ ".countskeleton{display:inline-block;width:150px;height:14px;"
+ "border-radius:var(--control-radius);\n"
+ " background:var(--hover);animation:skeleton-pulse 1.4s ease-in-out infinite}")
+ self.assertPageContains(".entitycollectionhead h3 .countskeleton{width:96px}")
+ self.assertPageContains(" min-height:var(--sortH);color:var(--ink-2);margin:0 -16px 16px;padding:8px 16px;")
+ self.assertPageContains(" min-height:var(--sortH);margin:0 -16px 12px;padding:8px 16px;min-width:0;")
+ # 转动复用既有的转圈关键帧;全局 prefers-reduced-motion 规则会把它关掉。
+ self.assertPageContains(
+ '.count[aria-busy="true"] #batchAction svg,\n'
+ '.entitycollectionhead[aria-busy="true"] .entitybatch svg{\n'
+ " animation:peach-spinner-linspin .9s linear infinite}")
+ self.assertPageContains("@keyframes peach-spinner-linspin{to{transform:rotate(1turn)}}")
+
+ def test_the_profile_collection_head_switches_sort_before_the_request_returns(self):
+ """资料页表头与首页同规矩:排序条立刻到位,只有 `视频 · N` 换成骨架。
+
+ 换列和翻方向在点下的那一刻就已确定,等一次请求才生效等于让选中态迟到一整
+ 个网络往返。标题两侧的标签条和已经铺好的网格不属于这次变化,一律不动。
+ """
+ self.assertPageContains(
+ " markEntityCollectionBusy(kind,name,filters);\n"
+ " const items=await fetchEntityItems(kind,name,filters);")
+ self.assertPageContains("head.setAttribute('aria-busy','true');")
+ self.assertPageContains("head.querySelector('.sorts').outerHTML=entityCollectionSortsHtml(filters);")
+ self.assertPageContains(
+ "head.querySelector('h3').innerHTML='<span class=\"countskeleton\"></span>';")
+ # 表头只有一份写法和一处接线,重画忙碌态和重画结果不会走岔。
+ self.assertPageContains(
+ 'section.innerHTML=`<div class="entitycollectionhead"><h3></h3>'
+ "${entityCollectionSortsHtml(filters)}</div>")
+ self.assertPageContains(" wireEntityCollectionHead(section,kind,name,filters);\n }else{")
+
def test_offline_sources_drop_out_of_the_default_filter(self):
"""脱盘的来源要从默认筛选里摘掉。
@@ -3700,7 +3760,7 @@ class WebUiSourceTests(unittest.TestCase):
def test_page_loading_uses_one_structural_skeleton_phase(self):
self.assertPageContains("function renderCatalogLoading(label='正在读取作品')")
self.assertPageContains("$('#grid').innerHTML=pageSkeletonHtml(label,{cards:true,className:'catalog-skeleton'})")
- self.assertPageContains("count.textContent='';count.setAttribute('aria-label',label)")
+ self.assertPageContains("count.setAttribute('aria-label',label);")
self.assertPageContains(".grid>.skeletonpanel{grid-column:1/-1;width:100%;min-width:0}")
self.assertPageContains("function renderInitialSurfaceLoading()")
self.assertPageContains("const followSkeletonHtml=(label='正在读取关注内容')")
@@ -4933,7 +4993,9 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageLacks(".photosetcover{display:block;aspect-ratio:3/4")
def test_jav_entity_pages_render_and_wire_the_same_layout_buttons(self):
- self.assertPageContains('<div class="entitycollectionhead"><h3></h3><span class="sorts">')
+ self.assertPageContains(
+ 'section.innerHTML=`<div class="entitycollectionhead"><h3></h3>')
+ self.assertPageContains('entityCollectionSortsHtml=filters=>`<span class="sorts">')
self.assertPageContains("${javActive()?javLayoutButtons():''}")
self.assertPageContains("wireJavLayoutButtons(section)")
self.assertPageContains("renderEntityCollection(kind,name,{...entityCollectionPage,items:[...entityCollectionPage.items]}")
diff --git a/web/app.css b/web/app.css
index 2439a5439..330673d7d 100644
--- a/web/app.css
+++ b/web/app.css
@@ -869,6 +869,17 @@ main{position:relative;z-index:1;padding:14px 16px 90px}
-webkit-backdrop-filter:saturate(1.35) blur(16px)}
.count:empty{display:none}
.count>span:first-child{flex:none}
+/* 计数骨架的宽高定死:跟着真实文本走的话,数字回来那一刻整行会横向弹一下。
+ 14px 远低于筛选条按钮的 32px,而 `.count` 有 min-height 兜底,所以换成真数字
+ 不改变行高。`视频 · N` 用的是同一枚。 */
+.countskeleton{display:inline-block;width:150px;height:14px;border-radius:var(--control-radius);
+ background:var(--hover);animation:skeleton-pulse 1.4s ease-in-out infinite}
+.entitycollectionhead h3 .countskeleton{width:96px}
+/* 等数据时换批键自己转:它是这一行唯一还在动的东西,其余部分必须保持静止。
+ 全局的 prefers-reduced-motion 规则会关掉它。 */
+.count[aria-busy="true"] #batchAction svg,
+.entitycollectionhead[aria-busy="true"] .entitybatch svg{
+ animation:peach-spinner-linspin .9s linear infinite}
.count .sorts,.entitycollectionhead .sorts{display:flex;gap:5px;margin-left:auto;flex-wrap:nowrap;
overflow-x:auto;scrollbar-width:none;min-width:0}
.count .sorts::-webkit-scrollbar,.entitycollectionhead .sorts::-webkit-scrollbar{display:none}
diff --git a/web/app.js b/web/app.js
index 306522809..e2e63976d 100644
--- a/web/app.js
+++ b/web/app.js
@@ -140,10 +140,40 @@ const followSkeletonHtml=(label='正在读取关注内容')=>`<div class="follow
${pageSkeletonHtml(label,{cards:true,className:'follow-content-skeleton'})}</div>`;
$('#loadSentinel').innerHTML=loadingDotsHtml('继续载入中…');
$('#tokLoader').insertAdjacentHTML('afterbegin',spinnerHtml('媒体加载中'));
+/* 筛选条只由当前 state 决定,这次加载不会改变它,所以它现在就能画成最终样子。
+ `state` 在启动 URL 解析之后才赋值,冷启动第一张骨架比它早,那一次只画计数骨架。 */
+const countSortsHtml=()=>!state?'':`<span class="sorts"><button class="batchaction" id="batchAction" type="button"
+ title="换一批" aria-label="换一批">${icon('refresh-cw')}</button>`
+ // JAV 版式紧跟换批动作,和排序连成一条。
+ +(javActive()?javLayoutButtons():'')
+ +sortOptions().map(([k,l])=>sortButtonHtml(k,l,state.sort,state.dir,'data-sort')).join('')+`</span>`;
+function wireCountRow(){
+ const batch=$('#batchAction');
+ if(batch)batch.onclick=async()=>{
+ if(batch.getAttribute('aria-busy')==='true')return;
+ const old=batch.innerHTML;setActionBusy(batch);batch.innerHTML=spinnerHtml('换一批');
+ try{await refreshAll()}finally{setActionBusy(batch,false);batch.innerHTML=old}
+ };
+ wireJavLayoutButtons($('#count'));
+ $('#count').querySelectorAll('[data-sort]').forEach(b=>b.onclick=()=>{
+ const next=nextSortState(b.dataset.sort,state.sort,state.dir);
+ if(!next)return;
+ state.sort=next.sort;state.dir=next.dir;
+ load(true)});
+}
+/* 骨架只盖这次加载真会变的东西,也就是计数那一串数字。筛选条照常画出来并接上事件:
+ 连它一起清空的话,点下去的那一枚会在等数据的这段时间里失去高亮,看着像没点上;
+ `.count:empty` 还会让整行折叠,网格跟着往上跳一截。计数骨架宽高固定、行本身有
+ `min-height:var(--sortH)` 兜底,所以数字回来时不发生位移。上方的标签条和已选条件
+ 同理不动——它们本来就不随这次请求变。 */
function renderCatalogLoading(label='正在读取作品'){
const count=$('#count');
count.setAttribute('aria-busy','true');
- count.textContent='';count.setAttribute('aria-label',label);
+ count.setAttribute('aria-label',label);
+ // 回收站的计数挂在说明行上,这一行只剩「清空回收站」,没有会变的数字可占位。
+ count.innerHTML=state&&state.state==='trash'?''
+ :`<span class="mono"><span class="countskeleton"></span></span>`+countSortsHtml();
+ wireCountRow();
$('#grid').innerHTML=pageSkeletonHtml(label,{cards:true,className:'catalog-skeleton'});
}
/* 每个管理表面的加载态只有一份定义,深链启动和路由到位后都从这里取。
@@ -2344,24 +2374,12 @@ function renderCount(){
if(trash)paintManageLede(`${total.toLocaleString()} 个符合 · 显示 ${n}`,
total?`<button class="batchaction danger" id="emptyTrash" type="button" title="永久删除回收站内容">清空回收站</button>`:'');
$('#count').classList.toggle('count-actions-only',trash);
- $('#count').removeAttribute('aria-busy');
+ $('#count').removeAttribute('aria-busy');$('#count').removeAttribute('aria-label');
$('#count').innerHTML=
(trash?'':`<span class="mono">${total.toLocaleString()} 个符合 · 显示 ${n}</span>`)
- +(trash
- // 回收站是待清理队列,不是浏览列表:换一批和排序在这里没有意义。
- ? ''
- : `<span class="sorts"><button class="batchaction" id="batchAction" type="button"
- title="换一批" aria-label="换一批">${icon('refresh-cw')}</button>`
- // JAV 版式紧跟换批动作,和排序连成一条。
- +(javActive()?javLayoutButtons():'')
- +sortOptions().map(([k,l])=>sortButtonHtml(k,l,state.sort,state.dir,'data-sort')).join('')+`</span>`);
- const batch=$('#batchAction');
- if(batch)batch.onclick=async()=>{
- if(batch.getAttribute('aria-busy')==='true')return;
- const old=batch.innerHTML;setActionBusy(batch);batch.innerHTML=spinnerHtml('换一批');
- try{await refreshAll()}finally{setActionBusy(batch,false);batch.innerHTML=old}
- };
- wireJavLayoutButtons($('#count'));
+ // 回收站是待清理队列,不是浏览列表:换一批和排序在这里没有意义。
+ +(trash?'':countSortsHtml());
+ wireCountRow();
const emptyTrash=$('#emptyTrash');
if(emptyTrash)emptyTrash.onclick=async(e)=>{
if(!confirm('永久删除回收站中的全部文件和账本记录?此操作不可恢复。'))return;
@@ -2376,11 +2394,6 @@ function renderCount(){
}catch(error){actionFailure('清空回收站',error);
}finally{await load(true)}
};
- $('#count').querySelectorAll('[data-sort]').forEach(b=>b.onclick=()=>{
- const next=nextSortState(b.dataset.sort,state.sort,state.dir);
- if(!next)return;
- state.sort=next.sort;state.dir=next.dir;
- load(true)});
}
/* ── 组合筛选:多个标签同时生效 ── */
@@ -5198,6 +5211,31 @@ async function fetchEntityItems(kind,name,filters,offset=0){
const items=await api('/api/items?'+p);cache(items.items);return items
}
let entityCollectionPage={items:[],total:0,has_more:false};
+/* 资料页作品集的表头与首页计数行同源:排序条由 filters 决定,`视频 · N` 由响应决定。 */
+const entityCollectionSortsHtml=filters=>`<span class="sorts">
+ <button class="batchaction entitybatch" type="button" title="换一批" aria-label="换一批">${icon('refresh-cw')}</button>
+ ${javActive()?javLayoutButtons():''}
+ ${sortOptions().map(([key,label])=>sortButtonHtml(
+ key,label,filters.sort||'new',filters.dir,'data-entity-sort')).join('')}</span>`;
+function wireEntityCollectionHead(section,kind,name,filters){
+ wireJavLayoutButtons(section);
+ section.querySelector('.entitybatch').onclick=()=>{
+ state.seed=rollSeed();updateEntityCollection(kind,name,{...filters,sort:'seed'},true)};
+ section.querySelectorAll('[data-entity-sort]').forEach(button=>button.onclick=()=>{
+ const next=nextSortState(button.dataset.entitySort,filters.sort||'new',filters.dir);
+ if(next)updateEntityCollection(kind,name,{...filters,...next},true)});
+}
+/* 换列和翻方向此刻就已确定,用不着等一次请求才在界面上生效:先把排序条重画成
+ 最终样子,只让会变的 `视频 · N` 换成骨架。标题、标签条和已经铺好的网格不动。 */
+function markEntityCollectionBusy(kind,name,filters){
+ const section=$('#index').querySelector('.entitysection');
+ const head=section&&section.querySelector('.entitycollectionhead');
+ if(!head)return;
+ head.setAttribute('aria-busy','true');
+ head.querySelector('.sorts').outerHTML=entityCollectionSortsHtml(filters);
+ head.querySelector('h3').innerHTML='<span class="countskeleton"></span>';
+ wireEntityCollectionHead(section,kind,name,filters);
+}
function renderEntityCollection(kind,name,items,filters,append=false){
const entityTag=filters.tag||'';
const section=$('#index').querySelector('.entitysection');if(!section)return;
@@ -5205,20 +5243,11 @@ function renderEntityCollection(kind,name,items,filters,append=false){
renderedPartGroups.clear();renderedEditionGroups.clear();
entityCollectionPage={items:[...(items.items||[])],total:items.total||0,
has_more:items.has_more==null?(items.items||[]).length<(items.total||0):!!items.has_more};
- section.innerHTML=`<div class="entitycollectionhead"><h3></h3><span class="sorts">
- <button class="batchaction entitybatch" type="button" title="换一批" aria-label="换一批">${icon('refresh-cw')}</button>
- ${javActive()?javLayoutButtons():''}
- ${sortOptions().map(([key,label])=>sortButtonHtml(
- key,label,filters.sort||'new',filters.dir,'data-entity-sort')).join('')}</span></div>
+ section.innerHTML=`<div class="entitycollectionhead"><h3></h3>${entityCollectionSortsHtml(filters)}</div>
<div class="grid"></div><button class="entitymore" type="button">载入更多</button>`;
section.dataset.total=String(items.total||0);
section.querySelector('h3').textContent=`视频 · ${(items.total||0).toLocaleString()}${entityTag?' · '+entityTag:''}`;
- wireJavLayoutButtons(section);
- section.querySelector('.entitybatch').onclick=()=>{
- state.seed=rollSeed();updateEntityCollection(kind,name,{...filters,sort:'seed'},true)};
- section.querySelectorAll('[data-entity-sort]').forEach(button=>button.onclick=()=>{
- const next=nextSortState(button.dataset.entitySort,filters.sort||'new',filters.dir);
- if(next)updateEntityCollection(kind,name,{...filters,...next},true)});
+ wireEntityCollectionHead(section,kind,name,filters);
}else{
entityCollectionPage.items.push(...(items.items||[]));
entityCollectionPage.has_more=!!items.has_more;
@@ -5245,6 +5274,7 @@ async function updateEntityCollection(kind,name,filters,push=true){
if(push)route(entityPath(kind,name)+(search?'?'+search:''));
barsContext={type:'entity',kind,name,filters:{...filters}};
const seq=++entityRequestSeq;
+ markEntityCollectionBusy(kind,name,filters);
const items=await fetchEntityItems(kind,name,filters);
if(seq!==entityRequestSeq)return;
renderEntityMediaToggle(kind,name,filters);