Skip to content

Commit 3b7c027

Browse files
authored
style: revise the badge char width (#908)
1 parent 15f35f5 commit 3b7c027

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

server/api/registry/badge/[type]/[...pkg].get.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ const COLORS = {
3131
white: '#ffffff',
3232
}
3333

34-
function measureTextWidth(text: string): number {
35-
const charWidth = 7
34+
const DEFAULT_CHAR_WIDTH = 7
35+
const CHARS_WIDTH = {
36+
engines: 5.5,
37+
}
38+
39+
function measureTextWidth(text: string, charWidth?: number): number {
40+
charWidth ??= DEFAULT_CHAR_WIDTH
3641
const paddingX = 8
3742
return Math.max(40, Math.round(text.length * charWidth) + paddingX * 2)
3843
}
@@ -288,7 +293,10 @@ export default defineCachedEventHandler(
288293
const finalLabelColor = rawLabelColor?.startsWith('#') ? rawLabelColor : `#${rawLabelColor}`
289294

290295
const leftWidth = measureTextWidth(finalLabel)
291-
const rightWidth = measureTextWidth(finalValue)
296+
const rightWidth = measureTextWidth(
297+
finalValue,
298+
CHARS_WIDTH[strategyKey as keyof typeof CHARS_WIDTH],
299+
)
292300
const totalWidth = leftWidth + rightWidth
293301
const height = 20
294302

0 commit comments

Comments
 (0)