Skip to content

Commit

Permalink
Fixed #258, more performant gallery previews.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Nov 3, 2023
1 parent fb2b03f commit 0b14917
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
100 changes: 59 additions & 41 deletions src/components/app/GalleryPreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,78 @@
import getProjectLink from './getProjectLink';
import { browser } from '$app/environment';
import MarkupHtmlView from '../concepts/MarkupHTMLView.svelte';
import { onMount } from 'svelte';
import type Project from '@models/Project';
export let gallery: Gallery;
const Count = 5;
let index = 0;
let projectID = gallery.getProjects()[0];
let project: Project | undefined = undefined;
let timeoutID: NodeJS.Timeout;
/** Projects in the gallery to highlight */
$: highlights = gallery.getProjects().slice(0, Count);
$: hidden = Math.max(0, gallery.getProjects().length - Count);
async function loadNext() {
index = (index + 1) % gallery.getProjects().length;
projectID = gallery.getProjects()[index];
if (projectID) project = await Projects.get(projectID);
timeoutID = setTimeout(loadNext, Math.random() * 3000 + 3000);
}
onMount(() => {
loadNext();
return () =>
timeoutID !== undefined ? clearTimeout(timeoutID) : undefined;
});
</script>

<div class="gallery">
<Link to={`gallery/${gallery.getID()}`}
><Subheader>{gallery.getName($locales)}</Subheader></Link
>
<MarkupHtmlView
markup={gallery.getDescription($locales).split('\n').join('\n\n')}
/>
<!-- We have to guard this since we haven't structured the project database to run server side fetches, so SvelteKit builds fail. -->
{#if browser}
<div class="previews">
{#each highlights as projectID, index}
<div class="highlight">
{#await Projects.get(projectID)}
<Spinning
label={$locales.get(
(l) => l.ui.widget.loading.message
)}
/>
{:then project}
{#if project}
<ProjectPreview
{project}
name={true}
action={() =>
goto(getProjectLink(project, true))}
delay={index * 300}
/>
{/if}
{/await}
</div>
{/each}
{#if hidden > 0}<Link to={`gallery/${gallery.getID()}`}
><span class="dots">{''.repeat(hidden)}</span></Link
>{/if}
{#if project === undefined}
<Spinning
label={$locales.get((l) => l.ui.widget.loading.message)}
/>
{:else}
<ProjectPreview
{project}
name={false}
action={() =>
project
? goto(getProjectLink(project, true))
: undefined}
delay={Math.random() * 1000}
size={8}
/>
{/if}
</div>
{/if}
<div class="description">
<Link to={`gallery/${gallery.getID()}`}
><Subheader
>{gallery.getName($locales)}
<sub
><span class="dots"
>{''.repeat(gallery.getProjects().length)}</span
></sub
></Subheader
></Link
>
<MarkupHtmlView
markup={gallery.getDescription($locales).split('\n').join('\n\n')}
/>
</div>
</div>

<style>
.gallery {
padding: var(--wordplay-spacing);
border-radius: var(--wordplay-border-radius);
margin-block-end: 2em;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
margin-block-start: 2em;
gap: var(--wordplay-spacing);
align-items: top;
}
.dots {
Expand All @@ -71,11 +90,10 @@
.previews {
display: flex;
flex-direction: row;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
gap: var(--wordplay-spacing);
row-gap: var(--wordplay-spacing);
margin-block-start: calc(2 * var(--wordplay-spacing));
width: 8em;
height: 8em;
}
</style>
5 changes: 3 additions & 2 deletions src/components/app/ProjectPreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
export let action: (() => void) | undefined = undefined;
export let delay: number;
export let name = true;
export let size = 4;
// Clone the project and get its initial value, then stop the project's evaluator.
let evaluator: Evaluator;
Expand Down Expand Up @@ -42,6 +43,8 @@
<div class="project" class:named={name}>
<a
class="preview"
style:width={`${size}rem`}
style:height={`${size}rem`}
href={getProjectLink(project, true)}
on:pointerdown={(event) =>
action && event.button === 0 ? action() : undefined}
Expand Down Expand Up @@ -119,8 +122,6 @@
.preview {
cursor: pointer;
width: 4rem;
height: 4rem;
overflow: hidden;
border: var(--wordplay-border-color) solid var(--wordplay-border-width);
border-radius: var(--wordplay-border-radius);
Expand Down

0 comments on commit 0b14917

Please sign in to comment.