Skip to content

Commit 8eed1b0

Browse files
committed
style: revise the badge char width
1 parent 1448d26 commit 8eed1b0

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 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

@@ -324,7 +332,7 @@ export default defineCachedEventHandler(
324332
}
325333
},
326334
{
327-
maxAge: CACHE_MAX_AGE_ONE_HOUR,
335+
maxAge: 0,
328336
swr: true,
329337
getKey: event => {
330338
const type = getRouterParam(event, 'type') ?? 'version'

0 commit comments

Comments
 (0)