2026-01-18 15:55:07 +03:00
|
|
|
<!--
|
|
|
|
|
Component: FontVirtualList
|
|
|
|
|
- Renders a virtualized list of fonts
|
|
|
|
|
- Handles font registration with the manager
|
|
|
|
|
-->
|
2026-02-15 22:56:37 +03:00
|
|
|
<script lang="ts">
|
2026-04-20 22:19:51 +03:00
|
|
|
import { debounce } from '$shared/lib/utils';
|
2026-02-06 11:53:59 +03:00
|
|
|
import {
|
|
|
|
|
Skeleton,
|
|
|
|
|
VirtualList,
|
|
|
|
|
} from '$shared/ui';
|
2026-02-16 14:11:29 +03:00
|
|
|
import type {
|
|
|
|
|
ComponentProps,
|
|
|
|
|
Snippet,
|
|
|
|
|
} from 'svelte';
|
2026-02-06 11:53:59 +03:00
|
|
|
import { fade } from 'svelte/transition';
|
2026-02-05 11:44:16 +03:00
|
|
|
import { getFontUrl } from '../../lib';
|
2026-02-02 12:18:20 +03:00
|
|
|
import {
|
2026-04-03 12:25:38 +03:00
|
|
|
type FontLoadRequestConfig,
|
2026-02-02 12:18:20 +03:00
|
|
|
type UnifiedFont,
|
|
|
|
|
appliedFontsManager,
|
2026-04-16 11:05:09 +03:00
|
|
|
fontStore,
|
2026-02-02 12:18:20 +03:00
|
|
|
} from '../../model';
|
2026-01-18 12:55:25 +03:00
|
|
|
|
2026-02-06 11:53:59 +03:00
|
|
|
interface Props extends
|
|
|
|
|
Omit<
|
2026-02-15 22:56:37 +03:00
|
|
|
ComponentProps<typeof VirtualList<UnifiedFont>>,
|
|
|
|
|
'items' | 'total' | 'isLoading' | 'onVisibleItemsChange' | 'onNearBottom'
|
2026-02-06 11:53:59 +03:00
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
/**
|
2026-03-02 22:18:21 +03:00
|
|
|
* Visible items callback
|
2026-02-06 11:53:59 +03:00
|
|
|
*/
|
2026-02-15 22:56:37 +03:00
|
|
|
onVisibleItemsChange?: (items: UnifiedFont[]) => void;
|
2026-02-06 11:53:59 +03:00
|
|
|
/**
|
2026-03-02 22:18:21 +03:00
|
|
|
* Font weight
|
2026-02-06 11:53:59 +03:00
|
|
|
*/
|
2026-02-16 14:11:29 +03:00
|
|
|
weight: number;
|
2026-02-06 11:53:59 +03:00
|
|
|
/**
|
2026-02-16 14:11:29 +03:00
|
|
|
* Skeleton snippet
|
2026-02-06 11:53:59 +03:00
|
|
|
*/
|
2026-02-16 14:11:29 +03:00
|
|
|
skeleton?: Snippet;
|
2026-01-18 12:55:25 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-06 11:53:59 +03:00
|
|
|
let {
|
|
|
|
|
children,
|
|
|
|
|
onVisibleItemsChange,
|
|
|
|
|
weight,
|
2026-02-16 14:11:29 +03:00
|
|
|
skeleton,
|
2026-02-06 11:53:59 +03:00
|
|
|
...rest
|
|
|
|
|
}: Props = $props();
|
2026-01-18 12:55:25 +03:00
|
|
|
|
2026-02-15 22:56:37 +03:00
|
|
|
const isLoading = $derived(
|
2026-04-08 10:00:30 +03:00
|
|
|
fontStore.isFetching || fontStore.isLoading,
|
2026-02-15 22:56:37 +03:00
|
|
|
);
|
|
|
|
|
|
2026-04-16 11:05:09 +03:00
|
|
|
let visibleFonts = $state<UnifiedFont[]>([]);
|
2026-04-20 22:19:51 +03:00
|
|
|
let isCatchingUp = $state(false);
|
|
|
|
|
|
|
|
|
|
const showInitialSkeleton = $derived(!!skeleton && isLoading && fontStore.fonts.length === 0);
|
|
|
|
|
const showCatchupSkeleton = $derived(!!skeleton && isCatchingUp);
|
2026-02-05 11:44:16 +03:00
|
|
|
|
2026-04-16 11:05:09 +03:00
|
|
|
function handleInternalVisibleChange(items: UnifiedFont[]) {
|
|
|
|
|
visibleFonts = items;
|
|
|
|
|
// Forward the call to any external listener
|
|
|
|
|
onVisibleItemsChange?.(items);
|
|
|
|
|
}
|
2026-02-05 11:44:16 +03:00
|
|
|
|
2026-04-20 22:19:51 +03:00
|
|
|
/**
|
|
|
|
|
* Handle jump scroll — batch-load all missing pages then re-enable font loading.
|
|
|
|
|
* Suppresses appliedFontsManager.touch() during catch-up to avoid loading
|
|
|
|
|
* font files for thousands of intermediate fonts.
|
|
|
|
|
*/
|
|
|
|
|
async function handleJump(targetIndex: number) {
|
|
|
|
|
if (isCatchingUp || !fontStore.pagination.hasMore) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
isCatchingUp = true;
|
|
|
|
|
try {
|
|
|
|
|
await fontStore.fetchAllPagesTo(targetIndex);
|
|
|
|
|
} finally {
|
|
|
|
|
isCatchingUp = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const debouncedTouch = debounce((configs: FontLoadRequestConfig[]) => {
|
|
|
|
|
appliedFontsManager.touch(configs);
|
|
|
|
|
}, 150);
|
|
|
|
|
|
2026-04-16 11:05:09 +03:00
|
|
|
// Re-touch whenever visible set or weight changes — fixes weight-change gap
|
|
|
|
|
$effect(() => {
|
2026-04-20 22:19:51 +03:00
|
|
|
if (isCatchingUp) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-16 11:05:09 +03:00
|
|
|
const configs: FontLoadRequestConfig[] = visibleFonts.flatMap(item => {
|
|
|
|
|
const url = getFontUrl(item, weight);
|
2026-04-17 13:05:36 +03:00
|
|
|
if (!url) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2026-04-16 11:05:09 +03:00
|
|
|
return [{ id: item.id, name: item.name, weight, url, isVariable: item.features?.isVariable }];
|
2026-02-05 11:44:16 +03:00
|
|
|
});
|
2026-04-16 11:05:09 +03:00
|
|
|
if (configs.length > 0) {
|
2026-04-20 22:19:51 +03:00
|
|
|
debouncedTouch(configs);
|
2026-04-16 11:05:09 +03:00
|
|
|
}
|
|
|
|
|
});
|
2026-03-02 22:18:21 +03:00
|
|
|
|
2026-04-16 11:05:09 +03:00
|
|
|
// Pin visible fonts so the eviction policy never removes on-screen entries.
|
|
|
|
|
// Cleanup captures the snapshot values, so a weight change unpins the old
|
|
|
|
|
// weight before pinning the new one.
|
|
|
|
|
$effect(() => {
|
|
|
|
|
const w = weight;
|
|
|
|
|
const fonts = visibleFonts;
|
|
|
|
|
for (const f of fonts) {
|
|
|
|
|
appliedFontsManager.pin(f.id, w, f.features?.isVariable);
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
for (const f of fonts) {
|
|
|
|
|
appliedFontsManager.unpin(f.id, w, f.features?.isVariable);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-01-30 19:21:47 +03:00
|
|
|
|
2026-02-15 22:56:37 +03:00
|
|
|
/**
|
|
|
|
|
* Load more fonts by moving to the next page
|
|
|
|
|
*/
|
|
|
|
|
function loadMore() {
|
|
|
|
|
if (
|
2026-04-08 10:00:30 +03:00
|
|
|
!fontStore.pagination.hasMore
|
|
|
|
|
|| fontStore.isFetching
|
2026-02-15 22:56:37 +03:00
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-08 10:00:30 +03:00
|
|
|
fontStore.nextPage();
|
2026-02-15 22:56:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle scroll near bottom - auto-load next page
|
|
|
|
|
*
|
|
|
|
|
* Triggered by VirtualList when the user scrolls within 5 items of the end
|
|
|
|
|
* of the loaded items. Only fetches if there are more pages available.
|
|
|
|
|
*/
|
|
|
|
|
function handleNearBottom(_lastVisibleIndex: number) {
|
2026-04-08 10:00:30 +03:00
|
|
|
const { hasMore } = fontStore.pagination;
|
2026-02-15 22:56:37 +03:00
|
|
|
|
2026-04-20 22:19:51 +03:00
|
|
|
// VirtualList already checks if we're near the bottom of loaded items.
|
|
|
|
|
// Guard isCatchingUp: fetchAllPagesTo bypasses TQ so isFetching stays false
|
|
|
|
|
// during batch catch-up, which would otherwise let nextPage() race with it.
|
|
|
|
|
if (hasMore && !fontStore.isFetching && !isCatchingUp) {
|
2026-02-15 22:56:37 +03:00
|
|
|
loadMore();
|
|
|
|
|
}
|
2026-01-30 19:21:47 +03:00
|
|
|
}
|
2026-01-18 12:55:25 +03:00
|
|
|
</script>
|
|
|
|
|
|
2026-02-16 14:11:29 +03:00
|
|
|
<div class="relative w-full h-full">
|
2026-04-20 22:19:51 +03:00
|
|
|
{#if showInitialSkeleton && skeleton}
|
2026-02-16 14:11:29 +03:00
|
|
|
<!-- Show skeleton only on initial load when no fonts are loaded yet -->
|
2026-04-20 22:19:51 +03:00
|
|
|
<div class="overflow-hidden h-full" transition:fade={{ duration: 300 }}>
|
2026-02-16 14:11:29 +03:00
|
|
|
{@render skeleton()}
|
|
|
|
|
</div>
|
|
|
|
|
{:else}
|
|
|
|
|
<!-- VirtualList persists during pagination - no destruction/recreation -->
|
|
|
|
|
<VirtualList
|
2026-04-08 10:00:30 +03:00
|
|
|
items={fontStore.fonts}
|
|
|
|
|
total={fontStore.pagination.total}
|
2026-04-20 22:19:51 +03:00
|
|
|
isLoading={isLoading || isCatchingUp}
|
2026-02-16 14:11:29 +03:00
|
|
|
onVisibleItemsChange={handleInternalVisibleChange}
|
|
|
|
|
onNearBottom={handleNearBottom}
|
2026-04-20 22:19:51 +03:00
|
|
|
onJump={handleJump}
|
2026-02-16 14:11:29 +03:00
|
|
|
{...rest}
|
|
|
|
|
>
|
|
|
|
|
{#snippet children(scope)}
|
|
|
|
|
{@render children(scope)}
|
|
|
|
|
{/snippet}
|
|
|
|
|
</VirtualList>
|
2026-04-20 22:19:51 +03:00
|
|
|
{#if showCatchupSkeleton && skeleton}
|
|
|
|
|
<div class="absolute inset-0 overflow-hidden" transition:fade={{ duration: 150 }}>
|
|
|
|
|
{@render skeleton()}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2026-02-16 14:11:29 +03:00
|
|
|
{/if}
|
|
|
|
|
</div>
|