feat(FontVirtualList): suppress font loading during jump scroll catch-up
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
- Handles font registration with the manager
|
- Handles font registration with the manager
|
||||||
-->
|
-->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { debounce } from '$shared/lib/utils';
|
||||||
import {
|
import {
|
||||||
Skeleton,
|
Skeleton,
|
||||||
VirtualList,
|
VirtualList,
|
||||||
@@ -54,6 +55,10 @@ const isLoading = $derived(
|
|||||||
);
|
);
|
||||||
|
|
||||||
let visibleFonts = $state<UnifiedFont[]>([]);
|
let visibleFonts = $state<UnifiedFont[]>([]);
|
||||||
|
let isCatchingUp = $state(false);
|
||||||
|
|
||||||
|
const showInitialSkeleton = $derived(!!skeleton && isLoading && fontStore.fonts.length === 0);
|
||||||
|
const showCatchupSkeleton = $derived(!!skeleton && isCatchingUp);
|
||||||
|
|
||||||
function handleInternalVisibleChange(items: UnifiedFont[]) {
|
function handleInternalVisibleChange(items: UnifiedFont[]) {
|
||||||
visibleFonts = items;
|
visibleFonts = items;
|
||||||
@@ -61,8 +66,32 @@ function handleInternalVisibleChange(items: UnifiedFont[]) {
|
|||||||
onVisibleItemsChange?.(items);
|
onVisibleItemsChange?.(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
// Re-touch whenever visible set or weight changes — fixes weight-change gap
|
// Re-touch whenever visible set or weight changes — fixes weight-change gap
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
if (isCatchingUp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const configs: FontLoadRequestConfig[] = visibleFonts.flatMap(item => {
|
const configs: FontLoadRequestConfig[] = visibleFonts.flatMap(item => {
|
||||||
const url = getFontUrl(item, weight);
|
const url = getFontUrl(item, weight);
|
||||||
if (!url) {
|
if (!url) {
|
||||||
@@ -71,7 +100,7 @@ $effect(() => {
|
|||||||
return [{ id: item.id, name: item.name, weight, url, isVariable: item.features?.isVariable }];
|
return [{ id: item.id, name: item.name, weight, url, isVariable: item.features?.isVariable }];
|
||||||
});
|
});
|
||||||
if (configs.length > 0) {
|
if (configs.length > 0) {
|
||||||
appliedFontsManager.touch(configs);
|
debouncedTouch(configs);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -113,17 +142,19 @@ function loadMore() {
|
|||||||
function handleNearBottom(_lastVisibleIndex: number) {
|
function handleNearBottom(_lastVisibleIndex: number) {
|
||||||
const { hasMore } = fontStore.pagination;
|
const { hasMore } = fontStore.pagination;
|
||||||
|
|
||||||
// VirtualList already checks if we're near the bottom of loaded items
|
// VirtualList already checks if we're near the bottom of loaded items.
|
||||||
if (hasMore && !fontStore.isFetching) {
|
// 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) {
|
||||||
loadMore();
|
loadMore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="relative w-full h-full">
|
<div class="relative w-full h-full">
|
||||||
{#if skeleton && isLoading && fontStore.fonts.length === 0}
|
{#if showInitialSkeleton && skeleton}
|
||||||
<!-- Show skeleton only on initial load when no fonts are loaded yet -->
|
<!-- Show skeleton only on initial load when no fonts are loaded yet -->
|
||||||
<div transition:fade={{ duration: 300 }}>
|
<div class="overflow-hidden h-full" transition:fade={{ duration: 300 }}>
|
||||||
{@render skeleton()}
|
{@render skeleton()}
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
@@ -131,14 +162,20 @@ function handleNearBottom(_lastVisibleIndex: number) {
|
|||||||
<VirtualList
|
<VirtualList
|
||||||
items={fontStore.fonts}
|
items={fontStore.fonts}
|
||||||
total={fontStore.pagination.total}
|
total={fontStore.pagination.total}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading || isCatchingUp}
|
||||||
onVisibleItemsChange={handleInternalVisibleChange}
|
onVisibleItemsChange={handleInternalVisibleChange}
|
||||||
onNearBottom={handleNearBottom}
|
onNearBottom={handleNearBottom}
|
||||||
|
onJump={handleJump}
|
||||||
{...rest}
|
{...rest}
|
||||||
>
|
>
|
||||||
{#snippet children(scope)}
|
{#snippet children(scope)}
|
||||||
{@render children(scope)}
|
{@render children(scope)}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</VirtualList>
|
</VirtualList>
|
||||||
|
{#if showCatchupSkeleton && skeleton}
|
||||||
|
<div class="absolute inset-0 overflow-hidden" transition:fade={{ duration: 150 }}>
|
||||||
|
{@render skeleton()}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user