mirror of
https://github.com/pheralb/svgl.git
synced 2025-02-11 09:40:31 +08:00
✨ Search improvements.
This commit is contained in:
parent
37d40fe59c
commit
46701ad161
@ -1,9 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { iSVG } from '@/types/svg';
|
import type { iSVG } from '@/types/svg';
|
||||||
|
import { cn } from '@/utils/cn';
|
||||||
|
|
||||||
// Get all svgs:
|
// Get all svgs:
|
||||||
import { svgs } from '@/data/svgs';
|
import { svgsData } from '@/data';
|
||||||
const allSvgs = JSON.parse(JSON.stringify(svgs));
|
const allSvgs = JSON.parse(JSON.stringify(svgsData));
|
||||||
|
|
||||||
// Components:
|
// Components:
|
||||||
import Search from '@/components/search.svelte';
|
import Search from '@/components/search.svelte';
|
||||||
@ -11,18 +12,23 @@
|
|||||||
import SvgCard from '@/components/svgCard.svelte';
|
import SvgCard from '@/components/svgCard.svelte';
|
||||||
import Grid from '@/components/grid.svelte';
|
import Grid from '@/components/grid.svelte';
|
||||||
import NotFound from '@/components/notFound.svelte';
|
import NotFound from '@/components/notFound.svelte';
|
||||||
import { cn } from '@/utils/cn';
|
import { ArrowDownUpIcon, ArrowUpDownIcon } from 'lucide-svelte';
|
||||||
|
|
||||||
|
let sorted: boolean = false;
|
||||||
|
|
||||||
// Search:
|
// Search:
|
||||||
let searchTerm = '';
|
let searchTerm = '';
|
||||||
let filteredSvgs: iSVG[] = [];
|
let filteredSvgs: iSVG[] = [];
|
||||||
|
|
||||||
|
// Order by last added:
|
||||||
|
|
||||||
if (searchTerm.length === 0) {
|
if (searchTerm.length === 0) {
|
||||||
filteredSvgs = allSvgs.sort((a: iSVG, b: iSVG) => {
|
filteredSvgs = allSvgs.sort((a: iSVG, b: iSVG) => {
|
||||||
return b.id - a.id;
|
return b.id! - a.id!;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search svgs:
|
||||||
const searchSvgs = () => {
|
const searchSvgs = () => {
|
||||||
return (filteredSvgs = allSvgs.filter((svg: iSVG) => {
|
return (filteredSvgs = allSvgs.filter((svg: iSVG) => {
|
||||||
let svgTitle = svg.title.toLowerCase();
|
let svgTitle = svg.title.toLowerCase();
|
||||||
@ -30,10 +36,35 @@
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Clear search:
|
||||||
const clearSearch = () => {
|
const clearSearch = () => {
|
||||||
searchTerm = '';
|
searchTerm = '';
|
||||||
searchSvgs();
|
searchSvgs();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Sort:
|
||||||
|
const sort = () => {
|
||||||
|
if (sorted) {
|
||||||
|
sortByLatest();
|
||||||
|
} else {
|
||||||
|
sortAlphabetically();
|
||||||
|
}
|
||||||
|
sorted = !sorted;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Sort alphabetically:
|
||||||
|
const sortAlphabetically = () => {
|
||||||
|
filteredSvgs = allSvgs.sort((a: iSVG, b: iSVG) => {
|
||||||
|
return a.title.localeCompare(b.title);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Sort by latest:
|
||||||
|
const sortByLatest = () => {
|
||||||
|
filteredSvgs = allSvgs.sort((a: iSVG, b: iSVG) => {
|
||||||
|
return b.id! - a.id!;
|
||||||
|
});
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
@ -48,6 +79,22 @@
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<Container>
|
<Container>
|
||||||
|
<div class="flex items-center justify-end mb-4">
|
||||||
|
<button
|
||||||
|
class={cn(
|
||||||
|
'flex items-center justify-center space-x-1 rounded-md px-3 py-1.5 text-sm font-medium',
|
||||||
|
filteredSvgs.length === 0 && 'hidden'
|
||||||
|
)}
|
||||||
|
on:click={() => sort()}
|
||||||
|
>
|
||||||
|
{#if sorted}
|
||||||
|
<ArrowDownUpIcon size={16} strokeWidth={2} class="mr-1" />
|
||||||
|
{:else}
|
||||||
|
<ArrowUpDownIcon size={16} strokeWidth={2} class="mr-1" />
|
||||||
|
{/if}
|
||||||
|
<span>{sorted ? 'Sort by latest' : 'Sort alphabetically'}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<Grid>
|
<Grid>
|
||||||
{#each filteredSvgs as svg}
|
{#each filteredSvgs as svg}
|
||||||
<SvgCard svgInfo={svg} />
|
<SvgCard svgInfo={svg} />
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
if (searchTerm.length === 0) {
|
if (searchTerm.length === 0) {
|
||||||
filteredSvgs = svgsByCategory.sort((a: iSVG, b: iSVG) => {
|
filteredSvgs = svgsByCategory.sort((a: iSVG, b: iSVG) => {
|
||||||
return b.id - a.id;
|
return b.id! - a.id!;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user