Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devtools): display file size in devtools #462

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions client/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,40 @@ function prettyURL(font: FontFaceData) {
<NSectionBlock
text="Fonts"
icon="i-carbon-text-align-left"
container-class="font-mono text-xs"
container-class="font-mono text-xs flex flex-col gap-y-2"
>
<div
v-for="font, index of selected.fonts"
:key="index"
class="flex justify-between items-center gap-2"
:key="`${selected.fontFamily}-${index}`"
class="flex justify-between items-center gap-2 mt-2"
>
<div class="flex flex-col gap-1">
<div class="flex flex-col gap-1 min-w-0">
<span class="line-clamp-1">
{{ prettyURL(font) }}
</span>
<span class="flex flex-row gap-1 opacity-75">
{{ font.style || 'normal' }}
<span class="flex flex-row gap-1">
<div class="shrink-0">
{{ font.style || 'normal' }}
{{ Array.isArray(font.weight) ? font.weight.join('-') : font.weight }}
</div>
<span
v-if="font.unicodeRange"
class="flex gap-1"
>
<span class="opacity-75">
|
</span>
<span
class="line-clamp-1"
>
{{ font.unicodeRange?.join(', ') }}
</span>
</span>
</span>
<span
v-if="font.unicodeRange"
class="line-clamp-1 opacity-75"
>
<NIcon icon="i-carbon:language" />
{{ font.unicodeRange?.join(', ') }}
<span class="flex flex-row">
<Suspense>
<FontFileSize :font="font" />
</Suspense>
</span>
</div>
<NButton
Expand All @@ -216,7 +227,7 @@ function prettyURL(font: FontFaceData) {
v-if="selected.css"
:code="selected.css"
lang="css"
class="overflow-x-scroll border border-base rounded-lg"
class="overflow-x-scroll border border-base rounded-lg text-xs py-2"
/>
</NSectionBlock>
</div>
Expand Down
40 changes: 40 additions & 0 deletions client/components/FontFileSize.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<script lang="ts" setup>
import type { FontFaceData } from '../../src/types'

const props = defineProps<{
font: FontFaceData
}>()

// TODO: Should just use HEAD. But seems like Vite devserver is not handling HEADs properly. Needs investigation.
const { headers, status } = await $fetch.raw(props.font.src.find(i => 'url' in i)!.url, { method: 'GET', baseURL: '/', ignoreResponseError: true })

const fileSize = Number(headers.get('content-length')!)

function formatBytes(bytes: number) {
if (bytes === 0) return '0 Bytes'

const sizes = ['Bytes', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(1000))
const formattedSize = (bytes / Math.pow(1000, i)).toFixed(2)

return `${formattedSize}${sizes[i]}`
}

const badgeColor = status !== 200 ? 'bg-red-600 text-white' : fileSize < 30000 ? '' : fileSize < 100000 ? 'text-yellow' : 'text-red'
</script>

<template>
<NBadge
class="flex space-x-1 text-[0.6rem] rounded-full"
:class="[badgeColor]"
>
<div
v-if="status !== 200"
>
{{ status }}
</div>
<div v-else>
{{ formatBytes(fileSize) }}
</div>
</NBadge>
</template>
Loading