2024-06-15 18:44:17 +02:00
|
|
|
<script lang="ts">
|
2025-02-02 11:17:22 +01:00
|
|
|
import { Button } from '$lib/components/ui/button';
|
2025-06-21 21:07:36 +02:00
|
|
|
import { Ungroup } from '@lucide/svelte';
|
2025-02-02 11:17:22 +01:00
|
|
|
import Help from '$lib/components/Help.svelte';
|
2026-10-11 11:18:36 +02:00
|
|
|
import { i18n } from '$lib/i18n/i18n.svelte';
|
2025-02-02 11:17:22 +01:00
|
|
|
import { getURLForLanguage } from '$lib/utils';
|
2026-10-07 22:20:40 +02:00
|
|
|
import { get } from 'svelte/store';
|
|
|
|
|
import { engine } from '$lib/engine';
|
2024-06-15 18:44:17 +02:00
|
|
|
|
2025-10-05 19:34:05 +02:00
|
|
|
let props: {
|
|
|
|
|
class?: string;
|
|
|
|
|
} = $props();
|
|
|
|
|
|
2026-10-07 22:20:40 +02:00
|
|
|
const selection = engine.selection;
|
|
|
|
|
const files = engine.files;
|
|
|
|
|
const statistics = engine.statistics;
|
|
|
|
|
|
2026-10-26 18:51:56 +01:00
|
|
|
/** The number of segments of a file (its routes have one each) or of one of its tracks. */
|
2026-10-07 22:20:40 +02:00
|
|
|
function segmentCount(fileId: string, trackId?: string) {
|
|
|
|
|
const file = $files.get(fileId);
|
2026-10-26 18:51:56 +01:00
|
|
|
const structure = file ? get(file).structure : undefined;
|
|
|
|
|
const routes = trackId === undefined ? (structure?.routes.length ?? 0) : 0;
|
|
|
|
|
return (structure?.tracks ?? [])
|
2026-10-07 22:20:40 +02:00
|
|
|
.filter((track) => trackId === undefined || track.id === trackId)
|
2026-10-26 18:51:56 +01:00
|
|
|
.reduce((count, track) => count + track.segments.length, routes);
|
2026-10-07 22:20:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// files or tracks, which all have several segments
|
|
|
|
|
let validSelection = $derived.by(() => {
|
|
|
|
|
// what is selected can change without the selection: the files are edited
|
|
|
|
|
void $statistics;
|
|
|
|
|
switch ($selection.type) {
|
|
|
|
|
case 'file':
|
|
|
|
|
return (
|
|
|
|
|
$selection.fileIds.length > 0 &&
|
|
|
|
|
$selection.fileIds.every((fileId) => segmentCount(fileId) > 1)
|
|
|
|
|
);
|
|
|
|
|
case 'track': {
|
|
|
|
|
const { fileId, trackIds } = $selection;
|
|
|
|
|
return (
|
|
|
|
|
trackIds.length > 0 &&
|
|
|
|
|
trackIds.every((trackId) => segmentCount(fileId, trackId) > 1)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
default:
|
2025-10-05 19:34:05 +02:00
|
|
|
return false;
|
2026-10-07 22:20:40 +02:00
|
|
|
}
|
|
|
|
|
});
|
2024-06-15 18:44:17 +02:00
|
|
|
</script>
|
|
|
|
|
|
2025-10-05 19:34:05 +02:00
|
|
|
<div class="flex flex-col gap-3 w-full max-w-80 {props.class ?? ''}">
|
2026-10-07 22:20:40 +02:00
|
|
|
<Button variant="outline" disabled={!validSelection} onclick={() => engine.extract()}>
|
2025-11-10 11:51:16 +01:00
|
|
|
<Ungroup size="16" />
|
2025-06-21 21:07:36 +02:00
|
|
|
{i18n._('toolbar.extract.button')}
|
2025-02-02 11:17:22 +01:00
|
|
|
</Button>
|
2025-06-21 21:07:36 +02:00
|
|
|
<Help link={getURLForLanguage(i18n.lang, '/help/toolbar/extract')}>
|
2025-02-02 11:17:22 +01:00
|
|
|
{#if validSelection}
|
2025-06-21 21:07:36 +02:00
|
|
|
{i18n._('toolbar.extract.help')}
|
2025-02-02 11:17:22 +01:00
|
|
|
{:else}
|
2025-06-21 21:07:36 +02:00
|
|
|
{i18n._('toolbar.extract.help_invalid_selection')}
|
2025-02-02 11:17:22 +01:00
|
|
|
{/if}
|
|
|
|
|
</Help>
|
2024-06-15 18:44:17 +02:00
|
|
|
</div>
|