Files
gpx.studio/website/src/lib/components/toolbar/tools/reduce/Reduce.svelte
T

78 lines
2.7 KiB
Svelte
Raw Normal View History

2025-10-18 16:10:08 +02:00
<script lang="ts">
import { Label } from '$lib/components/ui/label/index.js';
import { Button } from '$lib/components/ui/button';
import { Slider } from '$lib/components/ui/slider';
import Help from '$lib/components/Help.svelte';
import { Funnel } from '@lucide/svelte';
2026-10-11 11:18:36 +02:00
import { i18n } from '$lib/i18n/i18n.svelte';
2025-10-18 16:10:08 +02:00
import WithUnits from '$lib/components/WithUnits.svelte';
import { onDestroy } from 'svelte';
import { getURLForLanguage } from '$lib/utils';
2026-10-08 08:36:29 +02:00
import { engine } from '$lib/engine';
import { minTolerance, ReducedLayer, tolerance } from './utils.svelte';
2025-10-18 16:10:08 +02:00
let props: { class?: string } = $props();
2026-04-06 18:22:01 +02:00
let sliderValue = $state(50);
2025-10-18 16:10:08 +02:00
const maxTolerance = 10000;
2026-10-08 08:36:29 +02:00
const selection = engine.selection;
const statistics = engine.statistics;
// the preview needs the start of each segment
const request = engine.requestStatistics();
request.set(['anchors']);
2025-10-18 16:10:08 +02:00
let validSelection = $derived(
2026-10-08 08:36:29 +02:00
$selection.type === 'file' || $selection.type === 'track' || $selection.type === 'segment'
2025-10-18 16:10:08 +02:00
);
2026-10-08 08:36:29 +02:00
let reducedLayer = new ReducedLayer();
2026-10-08 08:47:14 +02:00
// read again only when the selection or the files change, not with the tolerance
let distances = $derived.by(() => {
void $statistics;
return engine.reductionDistances();
});
2026-10-08 08:36:29 +02:00
$effect(() => {
2026-10-08 08:47:14 +02:00
reducedLayer.update($statistics, distances, $tolerance);
2026-10-08 08:36:29 +02:00
});
2025-10-18 16:10:08 +02:00
$effect(() => {
tolerance.set(
2026-04-06 18:22:01 +02:00
minTolerance * 2 ** (sliderValue / (100 / Math.log2(maxTolerance / minTolerance)))
2025-10-18 16:10:08 +02:00
);
});
onDestroy(() => {
2026-10-08 08:36:29 +02:00
request.release();
reducedLayer.destroy();
2025-10-18 16:10:08 +02:00
});
</script>
<div class="flex flex-col gap-3 w-full max-w-80 {props.class ?? ''}">
<div class="p-2">
2026-04-06 18:22:01 +02:00
<Slider bind:value={sliderValue} min={0} max={100} step={1} type="single" />
2025-10-18 16:10:08 +02:00
</div>
<Label class="flex flex-row justify-between">
<span>{i18n._('toolbar.reduce.tolerance')}</span>
<WithUnits value={$tolerance / 1000} type="distance" decimals={4} class="font-normal" />
</Label>
<Label class="flex flex-row justify-between">
<span>{i18n._('toolbar.reduce.number_of_points')}</span>
2026-10-08 08:36:29 +02:00
<span class="font-normal">{reducedLayer.currentPoints}/{reducedLayer.maxPoints}</span>
2025-10-18 16:10:08 +02:00
</Label>
2026-10-08 08:36:29 +02:00
<Button variant="outline" disabled={!validSelection} onclick={() => engine.reduce($tolerance)}>
2025-11-10 11:51:16 +01:00
<Funnel size="16" />
2025-10-18 16:10:08 +02:00
{i18n._('toolbar.reduce.button')}
</Button>
<Help link={getURLForLanguage(i18n.lang, '/help/toolbar/minify')}>
{#if validSelection}
{i18n._('toolbar.reduce.help')}
{:else}
{i18n._('toolbar.reduce.help_no_selection')}
{/if}
</Help>
</div>