nex_animation/scripts/ui/btnclustercheck.py

140 lines
7.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""通用「同一父元素下的按钮簇」尺子:不限 `.nex-action-group`(UI规范 §7 · 用户第 2 条)。
`btnrowcheck` / `footercheck` 量的是**已知的动作条**(前者认 kit 类名、后者认吸底选择器)。
这条尺子不认类名:**任何** DOM 父元素,只要它下面有 ≥2 颗**相邻的兄弟按钮**(同一概念的一组),
它们的垂直中心就必须落在同一条水平线上,或者整簇竖排等宽。
于是「页面自己手搓了一个 flex 摆按钮、忘了套 ActionGroup」这一族盲区第一次被量到——
结构上,前两道闸都扫不到它(没类名 / 不是浮层)。
判据(与 btnrowcheck 同一条尺子,只量渲染结果):
· 一簇 = 父元素的直接子节点里**连续**的按钮(`button` / `[role=button]`,中间只容得下空白与注释);
· 竖排的父容器(`flex-direction:column` / `display:grid`)与表格单元格(一行一格、格内本就一颗)不是它的对象;
· 胶囊流 / 标签流(`.nex-chipbar` / `.nex-taggroup`)不是它管的:那一排是**从固定集合里挑**的
选项(§7.1 表第三行),每个胶囊独立成行是设计,不是「一组协同动作被劈开」——与 btnrowcheck 同一条豁免;
· 同一簇按钮中心落在 ≥2 条水平线 → 立案;
· 唯一合法的多行 = **整簇竖排等宽**(每行一颗,且每颗宽 ≥ 父内容宽的 92%)。
口径(`BTNCLUSTER_CLUSTERS` 是**覆盖面**,不是违规数):每一簇都记一条,单行的也记。
第一版只 push 多行那几条,于是全站修干净之后 `PROBED` 恒等于 0,
`MIN_CLUSTERS` 那条「尺子自己瞎了」的底线**永远红**——底线把健康态当成了失灵(第 17 轮续踩过)。
用法:NEX_WIDTHS=1440,992,768,576,430 .venv/bin/python scripts/ui/btnclustercheck.py
输出:`BTNCLUSTER_CLUSTERS 全站量到的簇数 / MULTI 其中多行的 / BAD_TOTAL 违规`。
退出码:0 = 干净;1 = 有点名;2 = 一簇都没量到(尺子自己瞎了)。
"""
import json
import os
import sys
from playwright.sync_api import sync_playwright
BASE = os.environ.get("NEX_BASE", "http://127.0.0.1:5173")
WIDTHS = [int(x) for x in os.environ.get("NEX_WIDTHS", "1440,992,768,576,430").split(",")]
THEMES = os.environ.get("NEX_THEMES", "light").split(",")
USER, PWD = os.environ.get("NEX_USER", "admin"), os.environ.get("NEX_PASS", "admin1234")
ROUTES = os.environ.get("NEX_ROUTES",
"/,/assets,/models,/users,/account,/projects/10/overview,/projects/10/library,"
"/projects/10/storyboard,/projects/10/dispatch,/projects/10/timeline,"
"/projects/10/review,/projects/10/delivery,/help").split(",")
MIN_CLUSTERS = int(os.environ.get("NEX_MIN_CLUSTERS", "60"))
# dev(:5173) 首访每条路由都要现编 chunk,900ms 只够出壳 → 一簇都量不到;预览(:4188) 静态资源 900ms 够。
SETTLE = int(os.environ.get("NEX_SETTLE", "900"))
JS = r"""() => {
const out = [];
const isBtn = (el) => el && el.nodeType === 1 &&
(el.tagName === 'BUTTON' || el.getAttribute('role') === 'button') &&
!el.disabled && el.offsetParent !== null &&
// 绝对定位的角标不参与流内换行:素材封面的「右上删除 / 左下版本」本来就各占一角,
// 把它们算成一簇会永远报(第一版就是这么误伤 /assets 的 12 张封面)。
['absolute','fixed'].indexOf(getComputedStyle(el).position) < 0 &&
(() => { const r = el.getBoundingClientRect(); return r.width > 1 && r.height > 1; })();
for (const parent of document.querySelectorAll('body *')) {
const cs = getComputedStyle(parent);
if (cs.display === 'none' || cs.display === 'contents') continue;
// 竖排容器与栅格列:多行是设计,不是被劈开
if (cs.display === 'grid' || (cs.display === 'flex' && cs.flexDirection === 'column')) continue;
if (parent.closest('td,th,[role="cell"],[role="gridcell"]')) continue;
// 胶囊流 / 标签流不是「一组协同动作」,是选项集合:独立成行是设计(§7.1 第三行)
if (parent.closest('.nex-chipbar,.nex-taggroup')) continue;
// 一簇 = 直接子节点里**连续**的按钮段(中间隔着非按钮的,是两块不同的东西,分开算)。
// 「段」而不是「全部子节点都是按钮」才贴语义:素材行是 缩略图 + 标签 + 两颗按钮,
// 那两颗按钮仍然是一组,被劈开一样要报(第一版写成「全子节点皆按钮」,
// 13 条路由一簇都没量到,MIN_CLUSTERS 底线当场把它拦下——这就是底线的意义)。
const kids = [...parent.children].filter((c) => {
const s = getComputedStyle(c);
return s.display !== 'none' && s.display !== 'contents';
});
const runs = [];
let open = null;
for (const c of kids) {
if (isBtn(c)) {
if (!open) { open = []; runs.push(open); }
open.push(c);
} else open = null; // 隔着非按钮的子节点就是两簇,各自判
}
const direct = runs.filter((r) => r.length >= 2).sort((a, b) => b.length - a.length)[0];
if (!direct) continue;
const rects = direct.map((b) => b.getBoundingClientRect());
const cy = rects.map((r) => r.top + r.height / 2);
const lines = [];
for (const y of cy) if (!lines.some((l) => Math.abs(l - y) <= 2)) lines.push(y);
const pw = parent.clientWidth || 1;
const full = rects.every((r) => r.width >= pw * 0.92);
// 单行的那一簇也要记账:覆盖面(CLUSTERS)与违规(BAD)是两件事,
// 只记多行的话,页面修干净 = 尺子看起来瞎了。
out.push({
tag: parent.tagName.toLowerCase(), cls: String(parent.className).slice(0, 90),
n: direct.length, lines: lines.length,
inner: Math.round(pw), ws: rects.map((r) => Math.round(r.width)),
full, one: lines.length === 1,
bad: lines.length > 1 && !(full && lines.length === direct.length),
});
}
return out;
}"""
def main():
with sync_playwright() as p:
b = p.chromium.launch()
ctx = b.new_context(viewport={"width": WIDTHS[0], "height": 1400})
pg = ctx.new_page()
pg.goto(BASE + "/login")
pg.wait_for_selector("#login-user")
pg.fill("#login-user", USER)
pg.fill("#login-pass", PWD)
pg.click("button[type=submit]")
pg.wait_for_url(lambda u: "/login" not in u, timeout=20000)
total = multi = bad_total = 0
for theme in THEMES:
pg.evaluate("t => localStorage.setItem('nex-the', t)", theme)
for w in WIDTHS:
pg.set_viewport_size({"width": w, "height": 1400})
for route in ROUTES:
try:
pg.goto(BASE + route, wait_until="load")
pg.wait_for_timeout(SETTLE)
except Exception as e:
print(f" !! {route} @{w}: {e}")
continue
rows = pg.evaluate(JS)
bad = [r for r in rows if r["bad"]]
total += len(rows)
multi += len([r for r in rows if r["lines"] > 1])
bad_total += len(bad)
for r in bad:
print(f"SPLIT {theme}/{w} {route} <{r['tag']} class=\"{r['cls']}\"> "
f"{r['n']} 颗 / {r['lines']} 行 内宽{r['inner']} 宽{r['ws']}")
print(f"BTNCLUSTER_CLUSTERS {total} MULTI {multi} BAD_TOTAL {bad_total}")
if total < MIN_CLUSTERS:
print(f"BTNCLUSTER_CLUSTERS {total} < MIN {MIN_CLUSTERS} —— 一簇都没量到,尺子自己瞎了")
return 2
b.close()
return 1 if bad_total else 0
if __name__ == "__main__":
sys.exit(main())