Commit bc6404edaa
bc6404edaa5fc4b18cf95dc3e95cdfcd4833b707
parent: 5cbd851c3f
Unsigned
Duck <57977673+woducku@users.noreply.github.com> · 2026-07-13 12:20 UTC
lazyicon
Layout: unified · split
docs/.vitepress/theme/components/extrepo/LazyIcon.vue
added
+39
| @@ -0,0 +1,39 @@ |
| 1 | <script setup lang="ts"> |
| 2 | import { onBeforeUnmount, onMounted, ref } from 'vue' |
| 3 | import { scheduleIconLoad } from './iconQueue' |
| 4 | |
| 5 | const props = defineProps<{ |
| 6 | src?: string |
| 7 | alt: string |
| 8 | }>() |
| 9 | |
| 10 | const rootRef = ref<HTMLElement>() |
| 11 | const loaded = ref(false) |
| 12 | let observer: IntersectionObserver | undefined |
| 13 | |
| 14 | onMounted(() => { |
| 15 | const el = rootRef.value |
| 16 | if (!el || !props.src) return |
| 17 | // Preload a bit before the card is actually visible so it's ready by the time it scrolls in. |
| 18 | observer = new IntersectionObserver((entries) => { |
| 19 | if (!entries.some(entry => entry.isIntersecting)) return |
| 20 | observer?.disconnect() |
| 21 | scheduleIconLoad(() => new Promise<void>((resolve) => { |
| 22 | const img = new Image() |
| 23 | img.onload = img.onerror = () => { |
| 24 | loaded.value = true |
| 25 | resolve() |
| 26 | } |
| 27 | img.src = props.src! |
| 28 | })) |
| 29 | }, { rootMargin: '200px' }) |
| 30 | observer.observe(el) |
| 31 | }) |
| 32 | |
| 33 | onBeforeUnmount(() => observer?.disconnect()) |
| 34 | </script> |
| 35 | |
| 36 | <template> |
| 37 | <img v-if="loaded" ref="rootRef" :src="src" :alt="alt" /> |
| 38 | <span v-else ref="rootRef" class="ext-site-icon-fallback" :class="{ 'i-lucide:globe': !src, 'ext-site-icon-loading': src }" /> |
| 39 | </template> |
docs/.vitepress/theme/components/extrepo/SiteCell.vue
+6 −2
| @@ -1,5 +1,6 @@ |
| 1 | 1 | <script setup lang="ts"> |
| 2 | 2 | import { langFlagClass, ratingIcon, ratingTitle } from './helpers' |
| 3 | import LazyIcon from './LazyIcon.vue' |
| 3 | 4 | import type { RepoSite } from './types' |
| 4 | 5 | |
| 5 | 6 | defineProps<{ |
| @@ -17,8 +18,7 @@ defineProps<{ |
| 17 | 18 | :rel="site.url ? 'noopener noreferrer' : undefined" |
| 18 | 19 | > |
| 19 | 20 | <div class="ext-site-row"> |
| 20 | | <img v-if="site.icon" class="ext-site-icon" :src="site.icon" :alt="site.name" loading="lazy" /> |
| 21 | | <span v-else class="ext-site-icon ext-site-icon-fallback i-lucide:globe" /> |
| 21 | <LazyIcon class="ext-site-icon" :src="site.icon" :alt="site.name" /> |
| 22 | 22 | <span class="ext-site-name-text" :title="site.name">{{ site.name }}</span> |
| 23 | 23 | <span v-if="site.rating !== 'safe'" class="ext-site-nsfw" :class="ratingIcon(site.rating)" :title="ratingTitle(site.rating)" /> |
| 24 | 24 | <span class="ext-site-lang" :class="langFlagClass(site.lang)" :title="site.lang" /> |
| @@ -90,6 +90,10 @@ defineProps<{ |
| 90 | 90 | color: var(--vp-c-text-3); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | .ext-site-icon-loading { |
| 94 | background-color: var(--vp-c-bg-mute, var(--vp-c-bg-soft)); |
| 95 | } |
| 96 | |
| 93 | 97 | .ext-site-name-text { |
| 94 | 98 | flex: 1; |
| 95 | 99 | min-width: 0; |
docs/.vitepress/theme/components/extrepo/SiteGroupCell.vue
+6 −2
| @@ -1,6 +1,7 @@ |
| 1 | 1 | <script setup lang="ts"> |
| 2 | 2 | import { computed } from 'vue' |
| 3 | 3 | import { ratingIcon, ratingTitle } from './helpers' |
| 4 | import LazyIcon from './LazyIcon.vue' |
| 4 | 5 | import type { RepoSite } from './types' |
| 5 | 6 | |
| 6 | 7 | const props = defineProps<{ |
| @@ -27,8 +28,7 @@ const highestRated = computed(() => props.sites.find(site => site.rating === 'ns |
| 27 | 28 | @click="$emit('toggle')" |
| 28 | 29 | > |
| 29 | 30 | <div class="ext-site-row"> |
| 30 | | <img v-if="primary.icon" class="ext-site-icon" :src="primary.icon" :alt="primary.name" loading="lazy" /> |
| 31 | | <span v-else class="ext-site-icon ext-site-icon-fallback i-lucide:globe" /> |
| 31 | <LazyIcon class="ext-site-icon" :src="primary.icon" :alt="primary.name" /> |
| 32 | 32 | <span class="ext-site-name-text" :title="primary.name">{{ primary.name }}</span> |
| 33 | 33 | <span |
| 34 | 34 | v-if="highestRated" |
| @@ -92,6 +92,10 @@ const highestRated = computed(() => props.sites.find(site => site.rating === 'ns |
| 92 | 92 | color: var(--vp-c-text-3); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | .ext-site-icon-loading { |
| 96 | background-color: var(--vp-c-bg-mute, var(--vp-c-bg-soft)); |
| 97 | } |
| 98 | |
| 95 | 99 | .ext-site-name-text { |
| 96 | 100 | flex: 1; |
| 97 | 101 | min-width: 0; |
docs/.vitepress/theme/components/extrepo/iconQueue.ts
added
+26
| @@ -0,0 +1,26 @@ |
| 1 | const MAX_CONCURRENT_MOBILE = 6 |
| 2 | const MAX_CONCURRENT_DESKTOP = 24 |
| 3 | |
| 4 | function maxConcurrent(): number { |
| 5 | if (typeof window === 'undefined') return MAX_CONCURRENT_DESKTOP |
| 6 | return window.matchMedia('(max-width: 767px)').matches ? MAX_CONCURRENT_MOBILE : MAX_CONCURRENT_DESKTOP |
| 7 | } |
| 8 | |
| 9 | let active = 0 |
| 10 | const queue: (() => Promise<void>)[] = [] |
| 11 | |
| 12 | function pump() { |
| 13 | while (active < maxConcurrent() && queue.length) { |
| 14 | const job = queue.shift()! |
| 15 | active++ |
| 16 | job().finally(() => { |
| 17 | active-- |
| 18 | pump() |
| 19 | }) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | export function scheduleIconLoad(load: () => Promise<void>) { |
| 24 | queue.push(load) |
| 25 | pump() |
| 26 | } |