Peach: Local-first video metadata scraper and media manager.
perf(web): 资料页作品网格的屏外卡片跳过渲染
longmeidaob30de5ec2939
diff --git a/frontend/e2e/offscreen-cards.test.ts b/frontend/e2e/offscreen-cards.test.tsindex 63dbb2f3f..b32562480 100644--- a/frontend/e2e/offscreen-cards.test.ts+++ b/frontend/e2e/offscreen-cards.test.ts@@ -2,7 +2,7 @@** 这几条守的是跳过渲染的副作用:屏外卡确实被跳过、没渲染过的卡按估计高度排进来之后* 整页高度和卡片位置不跳、Ctrl+F 仍找得到屏外卡里的字。演示库只有十来条,一屏就放完,- * 这里把目录响应放大成一长列,编号改成互不相同,封面请求再改写回原来那条。 */+ * 这里把目录和女优资料页的作品响应放大成一长列,编号改成互不相同,封面请求再改写回原来那条。 */import assert from 'node:assert/strict';import { after, before, describe, it } from 'node:test';@@ -20,10 +20,9 @@ interface CatalogPayload {items: Array<Record<string, unknown>>;}-/** 目录首屏换成 `CLONES` 张卡:每三张有一张带标签,最后一张带一枚独有的标签给查找用。 */-async function openLongCatalog(browser: Browser): Promise<Visit> {- const opened = await visit(browser, '/', DESKTOP);- const { page } = opened;+/** `/api/items` 的首页换成 `CLONES` 条:每三条有一条带标签,最后一条带一枚独有的标签给查找用。+ * 克隆源是去掉 `performer` 筛选之后的目录首页:演示库里没有人物实体,资料页按名字筛出来是空的。 */+async function serveLongItems(page: Page): Promise<void> {const original = new Map<number, number>();let baseline: CatalogPayload | undefined;await page.route(/\/api\/items\?/, async (route) => {@@ -32,7 +31,8 @@ async function openLongCatalog(browser: Browser): Promise<Visit> {await route.continue();return;}- baseline ??= await (await route.fetch()).json() as CatalogPayload;+ url.searchParams.delete('performer');+ baseline ??= await (await route.fetch({ url: url.toString() })).json() as CatalogPayload;const source = baseline.items.filter((item) => !item.part_group && !item.edition_group);const items = Array.from({ length: CLONES }, (_, index) => {const item = structuredClone(source[index % source.length]);@@ -42,13 +42,19 @@ async function openLongCatalog(browser: Browser): Promise<Visit> {return item;});await route.fulfill({ status: 200, contentType: 'application/json',- body: JSON.stringify({ ...baseline, total: CLONES, items }) });+ body: JSON.stringify({ ...baseline, total: CLONES, has_more: false, items }) });});await page.route((url) => original.has(Number(url.searchParams.get('id'))), async (route) => {const url = new URL(route.request().url());url.searchParams.set('id', String(original.get(Number(url.searchParams.get('id')))));await route.continue({ url: url.toString() });});+}++async function openLongCatalog(browser: Browser): Promise<Visit> {+ const opened = await visit(browser, '/', DESKTOP);+ const { page } = opened;+ await serveLongItems(page);await page.reload({ waitUntil: 'load' });await page.waitForFunction((count) => document.querySelectorAll('#grid .card').length >= count, CLONES,{ timeout: 15_000 });@@ -56,6 +62,25 @@ async function openLongCatalog(browser: Browser): Promise<Visit> {return opened;}+/** 女优资料页的作品区放大成 `CLONES` 部:资料由这里给,字段照 `/api/entity` 的形状写。 */+async function openLongEntity(browser: Browser): Promise<Visit> {+ const name = '七沢みあ';+ const opened = await visit(browser, '/', DESKTOP);+ const { page } = opened;+ await page.route(/\/api\/entity\?/, (route) => route.fulfill({ json: {+ id: 90_001, kind: 'performer', canonical_name: name, aliases: [], display_aliases: [],+ user_aliases: [], asset_count: CLONES, tags: [], related_performers: [], links: [],+ metadata: {}, has_image: false, has_avatar: false, avatar_focus: null,+ representative_asset_id: null, entry_links: [], feed: { following: false },+ } }));+ await serveLongItems(page);+ await page.goto(new URL(`/performers/${encodeURIComponent(name)}`, page.url()).href, { waitUntil: 'load' });+ await page.waitForFunction((count) => document.querySelectorAll('.entitysection>.grid>.card').length >= count,+ CLONES, { timeout: 15_000 });+ await settle(page);+ return opened;+}+/** 关注页换成 `CLONES` 条合成的更新:演示库没有关注来源。字段照 `/api/follow` 的形状,* 不带缩略图,不发外站请求;每三条有一条带标签,屏外卡因此有高矮两种元信息区。 */async function openLongFollow(browser: Browser): Promise<Visit> {@@ -120,43 +145,49 @@ describe('视口外的卡片', () => {await browser.close();});- it('跳过封面与元信息区的渲染,Ctrl+F 仍找得到屏外卡里的字', { timeout: 60_000 }, async () => {- const opened = await openLongCatalog(browser);- try {- const state = await opened.page.evaluate((tag) => {- const last = [...document.querySelectorAll<HTMLElement>('#grid .card')].at(-1)!;- const parts = [last.querySelector('.pic')!, last.querySelector('.meta')!];- // 跳过的是元素的内容,元素自己仍在排版里:查它的第一个子元素。- const skipped = parts.map((part) => !part.firstElementChild!.checkVisibility({ contentVisibilityAuto: true }));- const box = last.getBoundingClientRect();- /* 侧栏的标签列表里也有这枚标签,排在文档前面;往下接着找,直到落进那张卡。 */- const find = (window as unknown as { find(text: string): boolean }).find.bind(window);- let hits = 0;- while (hits < 5 && find(tag)) {- hits++;- const node = getSelection()?.anchorNode;- if (node && last.contains(node)) break;- }- const hit = getSelection()?.anchorNode;- return {- values: parts.map((part) => getComputedStyle(part).contentVisibility),- skipped, below: box.top - innerHeight, height: box.height,- hits, inLast: !!hit && last.contains(hit),- };- }, PROBE_TAG);- assert.deepEqual(state.values, ['auto', 'auto'], '屏外卡的封面和元信息区没有交给浏览器按视口取舍');- assert.ok(state.below > 1000, `最后一张卡离视口只有 ${state.below}px,放大的列表不够长`);- assert.deepEqual(state.skipped, [true, true], '屏外卡的封面或元信息区仍在渲染');- assert.ok(state.height > 0, '屏外卡的 getBoundingClientRect 量出了零高');- assert.ok(state.inLast, `页内查找没有落到屏外那张卡的标签上(命中 ${state.hits} 次)`);- assert.deepEqual(opened.problems, []);- } finally {- await opened.close();- }- });+ for (const [where, open, selector] of [+ ['馆藏', openLongCatalog, '#grid .card'],+ ['女优资料页', openLongEntity, '.entitysection>.grid>.card'],+ ] as const) {+ it(`${where}:跳过封面与元信息区的渲染,Ctrl+F 仍找得到屏外卡里的字`, { timeout: 60_000 }, async () => {+ const opened = await open(browser);+ try {+ const state = await opened.page.evaluate(([tag, cardSelector]) => {+ const last = [...document.querySelectorAll<HTMLElement>(cardSelector)].at(-1)!;+ const parts = [last.querySelector('.pic')!, last.querySelector('.meta')!];+ // 跳过的是元素的内容,元素自己仍在排版里:查它的第一个子元素。+ const skipped = parts.map((part) => !part.firstElementChild!.checkVisibility({ contentVisibilityAuto: true }));+ const box = last.getBoundingClientRect();+ /* 侧栏的标签列表里也有这枚标签,排在文档前面;往下接着找,直到落进那张卡。 */+ const find = (window as unknown as { find(text: string): boolean }).find.bind(window);+ let hits = 0;+ while (hits < 5 && find(tag)) {+ hits++;+ const node = getSelection()?.anchorNode;+ if (node && last.contains(node)) break;+ }+ const hit = getSelection()?.anchorNode;+ return {+ values: parts.map((part) => getComputedStyle(part).contentVisibility),+ skipped, below: box.top - innerHeight, height: box.height,+ hits, inLast: !!hit && last.contains(hit),+ };+ }, [PROBE_TAG, selector] as const);+ assert.deepEqual(state.values, ['auto', 'auto'], '屏外卡的封面和元信息区没有交给浏览器按视口取舍');+ assert.ok(state.below > 1000, `最后一张卡离视口只有 ${state.below}px,放大的列表不够长`);+ assert.deepEqual(state.skipped, [true, true], '屏外卡的封面或元信息区仍在渲染');+ assert.ok(state.height > 0, '屏外卡的 getBoundingClientRect 量出了零高');+ assert.ok(state.inLast, `页内查找没有落到屏外那张卡的标签上(命中 ${state.hits} 次)`);+ assert.deepEqual(opened.problems, []);+ } finally {+ await opened.close();+ }+ });+ }for (const [where, open, selector] of [['馆藏', openLongCatalog, '#grid .card'],+ ['女优资料页', openLongEntity, '.entitysection>.grid>.card'],['关注', openLongFollow, '.followlist>.card'],] as const) {it(`${where}:长距离往返之后整页高度和深处那张卡的位置不跳,屏外量到的几何与渲染后一致`, { timeout: 90_000 }, async () => {diff --git a/web/css/12-cards.css b/web/css/12-cards.cssindex c22116206..3dacc1093 100644--- a/web/css/12-cards.css+++ b/web/css/12-cards.css@@ -142,7 +142,10 @@ body.refreshing #batchAction svg,.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(var(--tile),1fr))}.card{cursor:pointer;display:flex;flex-direction:column;gap:8px;min-width:0;border-radius:var(--surface-radius);position:relative;transition:transform .18s ease,filter .18s ease,background-color .15s ease,box-shadow .15s ease;transform-origin:center}-/* 馆藏和关注网格里,视口外的卡片跳过渲染:滚动和侧栏开合时要算的只剩屏上那几十张。+/* 馆藏、资料页作品和关注网格里,视口外的卡片跳过渲染:滚动和侧栏开合时要算的只剩屏上那几十张。+ 资料页(女优、厂牌、创作者、系列、事务所五种共用一处)的作品区是 `.entitysection>.grid`,+ 卡片出自首页同一个 `cardHtml()`、同一个 `--tile` 列宽和 JAV 版式开关,下面的估计原样成立;+ 标签页点进去落在首页 `#grid`,回收站也是,不另列。跳过的是卡里的两块,封面 `.pic` 和元信息区 `.meta`;卡片盒本身照常排版,所以屏外卡的高度里只有元信息区一项是估计值。- 封面高度由 `aspect-ratio` 从列宽推出,跳过渲染时照样成立,列宽随视口和侧栏开合怎么@@ -158,17 +161,20 @@ body.refreshing #batchAction svg,悬停,那圈 2px 焦点环就缺一截。`overflow-clip-margin` 把裁切边往外放 8px,和整张卡的悬停面同宽,`frontend/e2e/design.test.ts` 按几何检查;Safari 不认这个属性,那边贴边的描边和焦点环仍会被裁掉一截。- 图片墙不在其内:它的封面没有固定比例,高度取自图片本身,跳过渲染时会按零高排版。 */+ 图片墙不在其内:它的封面没有固定比例,高度取自图片本身,跳过渲染时会按零高排版。+ 资料页的照片墙 `.photowall` 与名册 `.igrid` 同理不在其内,它们也不是 `.card`。资料页+ 新作那一行(`.feednew`)和详情页的接着看(`#nrow`)只有一行、往横向排,屏外的卡不改变+ 整页高度,标题字号也不是 `--fs-md`(`.feednewcard .meta .t` 是 `--fs-sm`),不套这份估计。 */.meta{--card-meta-byline:1lh;--card-meta-tags:0px;--card-meta-badges:0px;--card-meta-block:calc(var(--fs-md)*2.7 + 3px + var(--card-meta-byline) + var(--card-meta-badges) + var(--card-meta-tags))}.meta:has(>.mtext>.ctags){--card-meta-tags:calc(var(--fs-xs)*1.45 + var(--tag-pad-y)*2 + 2px + 3px)}.followitem .meta{--card-meta-byline:max(1lh,21px)}.followitem .meta:has(>.mtext>.fbadges){--card-meta-badges:calc(1lh + 6px + 3px)}-:is(#grid,.followlist:not(.followphotowall)) .card :is(.pic,.meta){content-visibility:auto}+:is(#grid,.entitysection>.grid,.followlist:not(.followphotowall)) .card :is(.pic,.meta){content-visibility:auto}/* `flex-shrink:0`:跳过渲染时元信息区的内容不参与最小尺寸,卡里有别的子元素要抢空间时它会被压扁。关注卡的 `.fstate` 是 `flex:1 1 100%`,不钉住的话屏外关注卡的元信息区只剩 21px。渲染时它本来就压不动(自动最小尺寸就是内容高度),钉住不改变屏上的排版。 */-:is(#grid,.followlist:not(.followphotowall)) .card .meta{flex-shrink:0;+:is(#grid,.entitysection>.grid,.followlist:not(.followphotowall)) .card .meta{flex-shrink:0;contain-intrinsic-block-size:auto var(--card-meta-block);overflow-clip-margin:8px}/* 叠层往上溢出到行间距里,卡片本体不为它留白。留白的写法(padding-top:7px)会把整张卡压低 7px 并加高 7px,同一行里 Mix、分卷、版次卡的封面和下面的文字就比