diff --git a/src-ts/card/bike-card.ts b/src-ts/card/bike-card.ts index ff5d2ad..8070b6f 100644 --- a/src-ts/card/bike-card.ts +++ b/src-ts/card/bike-card.ts @@ -39,23 +39,85 @@ export function viewBikeDetailFromOrder() { } // static/js/map.js:3265-3275 +let _fitSig = ""; + +/** 用离屏 canvas 量文字自然宽度(不受当前布局/是否省略影响)。 */ +function _textWidth(text: string, font: string): number { + try { + const canvas = document.createElement("canvas"); + const ctx = canvas.getContext("2d"); + if (!ctx) return 0; + ctx.font = font; + return ctx.measureText(text || "").width; + } catch (e) { + return 0; + } +} + +/** 量「按钮带文字」时的总宽度:克隆一份到屏幕外(脱离 .compact-actions 作用域)。 */ +function _actionsFullWidth(btns: HTMLElement): number { + try { + const probe = btns.cloneNode(true) as HTMLElement; + probe.style.cssText = "position:absolute;left:-9999px;top:0;display:flex;" + + "align-items:center;gap:6px;visibility:hidden;pointer-events:none;"; + probe.querySelectorAll(".btn-label").forEach(el => { + el.style.display = "inline"; + el.style.position = "static"; + }); + (document.body || document.documentElement).appendChild(probe); + const w = probe.getBoundingClientRect().width; + probe.remove(); + return w; + } catch (e) { + return 0; + } +} + +/** + * 车牌放不下时把「导航 / 寻车」收成纯图标。 + * + * 判定必须**不依赖当前 class**:上一版是「先切 class 再量 scrollWidth」, + * 而切换本身会改变可用宽度 → 「有文字+车牌省略」和「纯图标+车牌完整」互相触发, + * 卡片就在这两个状态之间来回跳(用户看到的就是图标在跳)。 + * 现在改成:量内容的自然宽度 + 卡片可用宽度,算一次就落定; + * 只有宽度或内容变了才重算。 + */ export function _fitCardActions(retry = 0) { - const card = document.getElementById("bike-card"); + const card = document.getElementById("bike-card") as HTMLElement | null; 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) { + const header = card ? card.querySelector(".bike-card-header") as HTMLElement | null : null; + if (!card || !codeEl || !header) return; + if (card.offsetParent === null || card.clientWidth === 0 || header.clientWidth === 0) { if (retry < 4) requestAnimationFrame(() => _fitCardActions(retry + 1)); return; } - card.classList.remove("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"); + + const kindEl = document.getElementById("card-kind"); + const btns = card.querySelector(".bike-card-header-btns") as HTMLElement | null; + const closeBtn = card.querySelector(".bike-card-close") as HTMLElement | null; + if (!btns) return; + + const cs = getComputedStyle(codeEl); + const font = `${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`; + const codeW = _textWidth((codeEl.textContent || "").trim(), font); + const badgeW = kindEl ? kindEl.getBoundingClientRect().width : 0; + const closeW = closeBtn ? closeBtn.getBoundingClientRect().width : 0; + const fullW = _actionsFullWidth(btns); + let gap = 8; + try { + const g = parseFloat(getComputedStyle(header).columnGap || ""); + if (Number.isFinite(g)) gap = g; + } catch (e) {} + + const avail = header.clientWidth; + const need = badgeW + gap + codeW + gap + fullW + gap + closeW; + + // 内容/可用宽度没变就不重复判定(也避免 ResizeObserver 自触发) + const sig = `${Math.round(avail)}|${codeEl.textContent}|${kindEl ? kindEl.textContent : ""}|${Math.round(fullW)}`; + if (sig === _fitSig) return; + _fitSig = sig; + + card.classList.toggle("compact-actions", need > avail + 1); } let _cardFitObserver: any = null; @@ -67,6 +129,7 @@ export function installCardFitObservers() { const card = document.getElementById("bike-card"); if (card) { _cardFitObserver = new ResizeObserver(() => { + // 判定里有签名去重:class 切换引起的抖动不会形成回环 if (_cardFitTimer) clearTimeout(_cardFitTimer); _cardFitTimer = setTimeout(() => _fitCardActions(), 60); }); diff --git a/static/dist/app.js b/static/dist/app.js index d8624d1..19684a9 100644 --- a/static/dist/app.js +++ b/static/dist/app.js @@ -5194,19 +5194,65 @@ document.getElementById("scan-input").value = code; scanCode(); } + var _fitSig = ""; + function _textWidth(text, font) { + try { + const canvas = document.createElement("canvas"); + const ctx = canvas.getContext("2d"); + if (!ctx) return 0; + ctx.font = font; + return ctx.measureText(text || "").width; + } catch (e) { + return 0; + } + } + function _actionsFullWidth(btns) { + try { + const probe = btns.cloneNode(true); + probe.style.cssText = "position:absolute;left:-9999px;top:0;display:flex;align-items:center;gap:6px;visibility:hidden;pointer-events:none;"; + probe.querySelectorAll(".btn-label").forEach((el) => { + el.style.display = "inline"; + el.style.position = "static"; + }); + (document.body || document.documentElement).appendChild(probe); + const w = probe.getBoundingClientRect().width; + probe.remove(); + return w; + } catch (e) { + return 0; + } + } 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) { + const header = card ? card.querySelector(".bike-card-header") : null; + if (!card || !codeEl || !header) return; + if (card.offsetParent === null || card.clientWidth === 0 || header.clientWidth === 0) { if (retry < 4) requestAnimationFrame(() => _fitCardActions(retry + 1)); return; } - card.classList.remove("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"); + const kindEl = document.getElementById("card-kind"); + const btns = card.querySelector(".bike-card-header-btns"); + const closeBtn = card.querySelector(".bike-card-close"); + if (!btns) return; + const cs = getComputedStyle(codeEl); + const font = `${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`; + const codeW = _textWidth((codeEl.textContent || "").trim(), font); + const badgeW = kindEl ? kindEl.getBoundingClientRect().width : 0; + const closeW = closeBtn ? closeBtn.getBoundingClientRect().width : 0; + const fullW = _actionsFullWidth(btns); + let gap = 8; + try { + const g = parseFloat(getComputedStyle(header).columnGap || ""); + if (Number.isFinite(g)) gap = g; + } catch (e) { + } + const avail = header.clientWidth; + const need = badgeW + gap + codeW + gap + fullW + gap + closeW; + const sig = `${Math.round(avail)}|${codeEl.textContent}|${kindEl ? kindEl.textContent : ""}|${Math.round(fullW)}`; + if (sig === _fitSig) return; + _fitSig = sig; + card.classList.toggle("compact-actions", need > avail + 1); } var _cardFitObserver = null; var _cardFitTimer = null;