fix(卡片): 车牌被省略时按钮收文字,修「有时不生效」
根因:_fitCardActions() 在 showBikeDetail 早期就被调用,而此时卡片还是隐藏的
(classList.add("show") 在更后面),clientWidth 为 0 → 量不出「是否被 ellipsis 截断」,
所以 compact-actions 没加上,按钮继续显示「导航/寻车」文字;等卡片上屏后没有再量一次。
因为是异步渲染 + 打开时机不同,表现就是"有时候正常、有时候不收"。
改法
- _fitCardActions 增加可见性判断:卡片不可见(offsetParent 为 null / clientWidth 为 0)
时用 requestAnimationFrame 重试最多 4 帧,仍不可见就交给观察者
- 卡片上屏后(classList.add("show") 之后)再量一次,并补一帧 rAF
- 新增 installCardFitObservers():ResizeObserver 监听 #bike-card 尺寸变化(60ms 防抖)
+ document.fonts.ready 后重量(字体回退会改变文字宽度),只安装一次
实测(390px)
- 长车牌 300001234567890123:compact-actions=true,按钮文字 display=none,图标保留
- 短车牌 51287:compact-actions=false,文字正常显示
- JS 异常 0
测试:169 pytest + 32 node 全绿
This commit is contained in:
@@ -39,16 +39,47 @@ export function viewBikeDetailFromOrder() {
|
||||
}
|
||||
|
||||
// static/js/map.js:3265-3275
|
||||
export function _fitCardActions() {
|
||||
export function _fitCardActions(retry = 0) {
|
||||
const card = document.getElementById("bike-card");
|
||||
const codeEl = document.getElementById("card-code");
|
||||
const title = card && card.querySelector(".bike-card-title");
|
||||
if (!card || !codeEl) return;
|
||||
// 卡片还没上屏(隐藏/过渡中)时 clientWidth 是 0,量不出是否截断 ——
|
||||
// 之前就是在这里漏判:车牌已经显示成 "#30000...",按钮却还带着文字。
|
||||
// 等一两帧再量,仍然不可见就放弃(等 ResizeObserver 再触发)。
|
||||
if ((card as HTMLElement).offsetParent === null || card.clientWidth === 0) {
|
||||
if (retry < 4) requestAnimationFrame(() => _fitCardActions(retry + 1));
|
||||
return;
|
||||
}
|
||||
card.classList.remove("compact-actions");
|
||||
// 读一次布局,看车牌是否被 ellipsis 截断
|
||||
const truncated = (title && title.scrollWidth > title.clientWidth + 1)
|
||||
|| (codeEl.scrollWidth > codeEl.clientWidth + 1);
|
||||
if (truncated) card.classList.add("compact-actions");
|
||||
// 车牌被 ellipsis 截断 → 把按钮收成纯图标,给车牌让出空间
|
||||
const titleOverflow = title ? title.scrollWidth > title.clientWidth + 1 : false;
|
||||
const codeOverflow = codeEl.scrollWidth > codeEl.clientWidth + 1;
|
||||
if (titleOverflow || codeOverflow) card.classList.add("compact-actions");
|
||||
}
|
||||
|
||||
let _cardFitObserver: any = null;
|
||||
let _cardFitTimer: any = null;
|
||||
|
||||
/** 卡片尺寸/字体变化后重新判断按钮要不要收成图标(只装一次)。 */
|
||||
export function installCardFitObservers() {
|
||||
if (typeof ResizeObserver !== "undefined" && !_cardFitObserver) {
|
||||
const card = document.getElementById("bike-card");
|
||||
if (card) {
|
||||
_cardFitObserver = new ResizeObserver(() => {
|
||||
if (_cardFitTimer) clearTimeout(_cardFitTimer);
|
||||
_cardFitTimer = setTimeout(() => _fitCardActions(), 60);
|
||||
});
|
||||
_cardFitObserver.observe(card);
|
||||
}
|
||||
}
|
||||
// 字体加载完宽度会变(数字/中文回退字体不同),重新量一次
|
||||
try {
|
||||
const fonts: any = (document as any).fonts;
|
||||
if (fonts && fonts.ready && typeof fonts.ready.then === "function") {
|
||||
fonts.ready.then(() => _fitCardActions());
|
||||
}
|
||||
} catch (e) { /* 老浏览器没有 document.fonts */ }
|
||||
}
|
||||
|
||||
// static/js/map.js:3285-3428
|
||||
@@ -181,6 +212,10 @@ export function showBikeDetail(id) {
|
||||
(stateText === "使用中" ? " warning" : stateText === "空闲" ? " highlight" : "");
|
||||
|
||||
document.getElementById("bike-card").classList.add("show");
|
||||
// 上屏之后再量:隐藏状态下量不出宽度,之前就是这里漏判
|
||||
installCardFitObservers();
|
||||
_fitCardActions();
|
||||
requestAnimationFrame(() => _fitCardActions());
|
||||
const overlay = document.getElementById("card-overlay");
|
||||
if (overlay) overlay.classList.add("show");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user