Skip to content
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
11 changes: 6 additions & 5 deletions apps/svelte.dev/src/routes/(packages)/packages/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@ export const prerender = false;
export async function load({ url }) {
const query = url.searchParams.get('query');
const tags = (url.searchParams.get('tags') ?? '').split(',').filter(Boolean);
let page = +(url.searchParams.get('page')?.toString() ?? 0);
let page = Math.max(1, +(url.searchParams.get('page')?.toString() ?? 1));

init(registry);

const current_results = search(query, { tags, sort_by: 'popularity' });

const total_pages = Math.ceil(current_results.length / REGISTRY_PAGE_LIMIT);
console.log(page, total_pages);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover debugging code?


if (page + 1 > total_pages) {
page = 0;
if (page > total_pages) {
page = 1;

const new_url = new URL(url);
new_url.searchParams.set('page', page + '');
redirect(303, new_url);
}

const current_results_paged = current_results.slice(
page * REGISTRY_PAGE_LIMIT,
page * REGISTRY_PAGE_LIMIT + REGISTRY_PAGE_LIMIT
(page - 1) * REGISTRY_PAGE_LIMIT,
(page - 1) * REGISTRY_PAGE_LIMIT + REGISTRY_PAGE_LIMIT
);

return {
Expand Down
195 changes: 117 additions & 78 deletions apps/svelte.dev/src/routes/(packages)/packages/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import { Box, ReactiveQueryParam } from '@sveltejs/site-kit/reactivity';
import { onMount } from 'svelte';
import SearchWorker from './packages-worker.ts?worker';
import { stopPropagation } from 'svelte/legacy';
import Pagination from './pagination.svelte';
const { data } = $props();
const query_qp = new ReactiveQueryParam<string>('query');
const page_qp = new ReactiveQueryParam<number>('page', {
encode: (v) => v.toString(),
decode: (v) => +v
decode: (v) => Math.max(1, v.length ? +v : 1)
});
const tags_qp = new ReactiveQueryParam<string[]>('tags', {
encode: (v) => v.join(','),
Expand Down Expand Up @@ -63,24 +65,25 @@
tags_qp.current;
page_qp.current;
if (ready) {
if (worker_first_run) {
worker_first_run = false;
} else {
const id = uid++;
pending.add(id);
worker.postMessage({
type: 'get',
id,
payload: {
query: query_qp.current,
page: page_qp.current,
tags: $state.snapshot(tags_qp.current)
}
});
}
if (!ready) return;
if (worker_first_run) {
worker_first_run = false;
return;
}
const id = uid++;
pending.add(id);
worker.postMessage({
type: 'get',
id,
payload: {
query: query_qp.current,
page: page_qp.current,
tags: $state.snapshot(tags_qp.current)
}
});
});
const number_formatter = Intl.NumberFormat();
Expand All @@ -103,7 +106,7 @@
<h1 class="visually-hidden">Packages</h1>

<div class="container">
<div class="toc-container" style="order: 1">
<div class="toc-container">
<nav aria-label="Docs">
<ul class="sidebar">
{#each [{ tag: 'all', short_title: 'All' }].concat(data.tags) as tag}
Expand Down Expand Up @@ -165,56 +168,48 @@
</li>
{/each}
</ul>

<div class="pagination">
{#each Array(total_pages.current), i}
{@const link = new URL(page.url)}
{@const _ = link.searchParams.set('page', i + '')}
<a
href={link.pathname + link.search}
aria-current={page_qp.current === i}
onclick={(e) => {
e.preventDefault();
page_qp.current = i;
}}>{i + 1}</a
>&nbsp;
{/each}
</div>
</nav>
</div>

<div class="page content">
<h1>Packages</h1>

<div class="posts">
<div class="controls">
<div class="input-group">
<input
use:forcefocus
onkeydown={(e) => {
if (e.key === 'Enter' && !e.isComposing) {
// const element = modal.querySelector('a[data-has-node]') as HTMLElement | undefined;
// element?.click();
}
}}
bind:value={query_qp.current}
placeholder="Search"
aria-describedby="search-description"
aria-label={'Search'}
spellcheck="false"
/>

<button aria-label="Clear" onclick={() => (query_qp.current = '')}>
<Icon name="close" />
</button>
</div>

<!-- <button class="raised" aria-label="Close" onclick={close}>
<div class="controls">
<label class="input-group">
<Icon name="search" />
<input
use:forcefocus
onkeydown={(e) => {
if (e.key === 'Enter' && !e.isComposing) {
// const element = modal.querySelector('a[data-has-node]') as HTMLElement | undefined;
// element?.click();
}
}}
bind:value={query_qp.current}
placeholder="Search"
aria-describedby="search-description"
aria-label={'Search'}
spellcheck="false"
/>

<button
aria-label="Clear"
onclick={(e) => {
e.stopPropagation();
query_qp.current = '';
}}
>
<Icon name="close" />
</button>
</label>

<!-- <button class="raised" aria-label="Close" onclick={close}>
<Icon name="close" />
<kbd>Esc</kbd>
</button> -->
</div>
</div>

<div class="posts">
{#each registry.current as pkg}
<article data-pubdate={pkg.updated}>
<a
Expand Down Expand Up @@ -286,6 +281,25 @@
{/each}
</div>

<div class="pagination">
<Pagination total={total_pages.current} bind:page={page_qp.current}>
{#snippet children(pageItem)}
{#if pageItem.type === 'ellipsis'}
<span>-</span>
{:else}
<button
aria-current={page_qp.current === pageItem.value}
onclick={() => {
page_qp.current = pageItem.value;
}}
>
{pageItem.value}
</button>
{/if}
{/snippet}
</Pagination>
</div>

<!-- <ul class="feed">
{#each [{ tag: 'all', short_title: 'All' }].concat(data.tags) as tag}
{@const link = new URL(page.url)}
Expand Down Expand Up @@ -391,28 +405,44 @@
}
.input-group {
position: relative;
display: flex;
align-items: center;
gap: 1rem;
flex: 1;
border-radius: 0.5rem;
padding: 0.5rem 0.5rem 0.5rem 0rem;
margin-block-start: 1rem;
position: relative;
&:has(:focus-visible) {
outline: 2px solid var(--sk-fg-accent);
outline-offset: 4px;
}
/* Border that is not rounded */
&::after {
content: '';
position: absolute;
inset-block-end: 0;
inset-inline: 0;
height: 1px;
background: var(--sk-border);
}
input {
font: var(--sk-font-ui-large);
font: var(--sk-font-ui-medium);
width: 100%;
padding: var(--padding) 6rem var(--padding) calc(var(--padding) - 0.5rem);
height: 6rem;
border: none;
flex-shrink: 0;
color: var(--sk-fg-1);
border-bottom: 1px solid var(--sk-border);
background: inherit;
border: none;
outline: none;
&::placeholder {
color: var(--sk-fg-4);
opacity: 0.5;
}

&:focus-visible {
outline-offset: -2px;
}
}
button {
Expand All @@ -425,7 +455,7 @@
&:hover,
&:focus {
color: var(--sk-fg-3);
color: var(--sk-fg-1);
}
&:focus-visible {
Expand All @@ -434,6 +464,12 @@
}
}
.posts {
display: flex;
flex-direction: column;
margin-block-start: 4rem;
}
h2 {
display: inline-block;
color: var(--sk-fg-1);
Expand Down Expand Up @@ -500,12 +536,20 @@
.pagination {
display: flex;
justify-content: center;
a {
span {
opacity: 0.5;
}
button,
span {
width: 2rem;
text-align: center;
font: var(--sk-font-ui-medium);
}
a[aria-current='true'] {
button[aria-current='true'] {
color: var(--sk-fg-accent);
text-decoration: underline;
}
Expand All @@ -525,12 +569,7 @@
}
.toc-container {
background: var(--sk-bg-2);
display: none;

:root.dark & {
background: var(--sk-bg-0);
}
}
@media (min-width: 832px) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ addEventListener('message', async (event) => {
}

if (type === 'get') {
let { query, page = 0, tags = [] } = payload;
let { query, page = 1, tags = [] } = payload;

const current_results = search(query, { tags });
const total_pages = Math.ceil(current_results.length / REGISTRY_PAGE_LIMIT);

if (page + 1 > total_pages) {
page = 0;
if (page > total_pages) {
page = 1;

console.log('Redirecting to page 0');
postMessage({
Expand All @@ -34,8 +34,8 @@ addEventListener('message', async (event) => {
type: 'results',
payload: {
results: current_results.slice(
page * REGISTRY_PAGE_LIMIT,
page * REGISTRY_PAGE_LIMIT + REGISTRY_PAGE_LIMIT
(page - 1) * REGISTRY_PAGE_LIMIT,
(page - 1) * REGISTRY_PAGE_LIMIT + REGISTRY_PAGE_LIMIT
),
total_pages,
query
Expand Down
Loading
Loading