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:
Vendored
+32
-3
@@ -5139,14 +5139,40 @@
|
||||
document.getElementById("scan-input").value = code;
|
||||
scanCode();
|
||||
}
|
||||
function _fitCardActions() {
|
||||
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;
|
||||
if (card.offsetParent === null || card.clientWidth === 0) {
|
||||
if (retry < 4) requestAnimationFrame(() => _fitCardActions(retry + 1));
|
||||
return;
|
||||
}
|
||||
card.classList.remove("compact-actions");
|
||||
const truncated = title && title.scrollWidth > title.clientWidth + 1 || codeEl.scrollWidth > codeEl.clientWidth + 1;
|
||||
if (truncated) card.classList.add("compact-actions");
|
||||
const titleOverflow = title ? title.scrollWidth > title.clientWidth + 1 : false;
|
||||
const codeOverflow = codeEl.scrollWidth > codeEl.clientWidth + 1;
|
||||
if (titleOverflow || codeOverflow) card.classList.add("compact-actions");
|
||||
}
|
||||
var _cardFitObserver = null;
|
||||
var _cardFitTimer = null;
|
||||
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 = document.fonts;
|
||||
if (fonts && fonts.ready && typeof fonts.ready.then === "function") {
|
||||
fonts.ready.then(() => _fitCardActions());
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
function showBikeDetail(id) {
|
||||
let bike = S.markersData[id];
|
||||
@@ -5250,6 +5276,9 @@
|
||||
document.getElementById("card-state").textContent = stateText;
|
||||
document.getElementById("card-state").className = "bike-status-value" + (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");
|
||||
if (bike.latitude && bike.longitude) {
|
||||
|
||||
Reference in New Issue
Block a user