2024-05-21 13:22:14 +02:00
|
|
|
<script lang="ts">
|
2024-05-21 17:47:08 +02:00
|
|
|
import { Button } from '$lib/components/ui/button';
|
2024-06-18 15:32:54 +02:00
|
|
|
import { Input } from '$lib/components/ui/input';
|
|
|
|
import { Textarea } from '$lib/components/ui/textarea';
|
|
|
|
import { Label } from '$lib/components/ui/label/index.js';
|
2024-05-21 17:47:08 +02:00
|
|
|
import * as ContextMenu from '$lib/components/ui/context-menu';
|
2024-06-18 15:32:54 +02:00
|
|
|
import * as Popover from '$lib/components/ui/popover';
|
2024-05-21 17:47:08 +02:00
|
|
|
import Shortcut from '$lib/components/Shortcut.svelte';
|
2024-06-15 18:44:17 +02:00
|
|
|
import { dbUtils, getFile, settings } from '$lib/db';
|
2024-06-18 15:32:54 +02:00
|
|
|
import { Copy, Info, MapPin, Plus, Save, Trash2, Waypoints } from 'lucide-svelte';
|
2024-06-13 19:11:54 +02:00
|
|
|
import {
|
|
|
|
ListFileItem,
|
|
|
|
ListLevel,
|
|
|
|
ListTrackItem,
|
|
|
|
ListWaypointItem,
|
|
|
|
type ListItem
|
|
|
|
} from './FileList';
|
2024-05-23 14:44:07 +02:00
|
|
|
import { selectItem, selection } from './Selection';
|
2024-05-21 17:47:08 +02:00
|
|
|
import { _ } from 'svelte-i18n';
|
2024-06-18 15:32:54 +02:00
|
|
|
import { getContext, onMount } from 'svelte';
|
2024-05-23 14:44:07 +02:00
|
|
|
import { get } from 'svelte/store';
|
2024-05-24 13:16:41 +02:00
|
|
|
import { gpxLayers } from '$lib/stores';
|
2024-06-18 15:32:54 +02:00
|
|
|
import {
|
|
|
|
GPXTreeElement,
|
|
|
|
Track,
|
|
|
|
TrackSegment,
|
|
|
|
type AnyGPXTreeElement,
|
|
|
|
Waypoint,
|
|
|
|
GPXFile
|
|
|
|
} from 'gpx';
|
2024-05-21 13:22:14 +02:00
|
|
|
|
2024-06-18 15:32:54 +02:00
|
|
|
export let node:
|
|
|
|
| GPXTreeElement<AnyGPXTreeElement>
|
|
|
|
| ReadonlyArray<Readonly<Waypoint>>
|
|
|
|
| Readonly<Waypoint>;
|
2024-05-22 16:05:31 +02:00
|
|
|
export let item: ListItem;
|
2024-05-21 13:22:14 +02:00
|
|
|
export let label: string | undefined;
|
|
|
|
|
2024-05-21 17:47:08 +02:00
|
|
|
let orientation = getContext<'vertical' | 'horizontal'>('orientation');
|
2024-06-13 19:11:54 +02:00
|
|
|
|
|
|
|
const { verticalFileView } = settings;
|
2024-06-18 15:32:54 +02:00
|
|
|
|
|
|
|
$: singleSelection = $selection.size === 1;
|
|
|
|
|
|
|
|
let openEditMetadata: boolean = false;
|
|
|
|
let name: string =
|
|
|
|
node instanceof GPXFile
|
|
|
|
? node.metadata.name ?? ''
|
|
|
|
: node instanceof Track
|
|
|
|
? node.name ?? ''
|
|
|
|
: '';
|
|
|
|
let description: string =
|
|
|
|
node instanceof GPXFile
|
|
|
|
? node.metadata.desc ?? ''
|
|
|
|
: node instanceof Track
|
|
|
|
? node.desc ?? ''
|
|
|
|
: '';
|
2024-05-21 13:22:14 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
|
|
|
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
2024-05-23 14:44:07 +02:00
|
|
|
<ContextMenu.Root
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
if (open && !get(selection).has(item)) {
|
|
|
|
selectItem(item);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2024-05-21 17:47:08 +02:00
|
|
|
<ContextMenu.Trigger class="grow truncate">
|
|
|
|
<Button
|
|
|
|
variant="ghost"
|
|
|
|
class="w-full p-0 px-1 border-none focus-visible:ring-0 focus-visible:ring-offset-0 {orientation ===
|
|
|
|
'vertical'
|
|
|
|
? 'h-fit'
|
|
|
|
: 'h-9 px-1.5 shadow-md'}"
|
|
|
|
>
|
2024-06-18 15:32:54 +02:00
|
|
|
{#if item instanceof ListFileItem || item instanceof ListTrackItem}
|
|
|
|
<Popover.Root bind:open={openEditMetadata}>
|
|
|
|
<Popover.Trigger />
|
|
|
|
<Popover.Content side="top" sideOffset={22} class="flex flex-col gap-3">
|
|
|
|
<Label for="name">{$_('menu.metadata.name')}</Label>
|
|
|
|
<Input bind:value={name} id="name" class="font-semibold h-8" />
|
|
|
|
<Label for="description">{$_('menu.metadata.description')}</Label>
|
|
|
|
<Textarea bind:value={description} id="description" />
|
|
|
|
<Button
|
|
|
|
variant="outline"
|
|
|
|
on:click={() => {
|
|
|
|
dbUtils.applyToFile(item.getFileId(), (file) => {
|
|
|
|
if (item instanceof ListFileItem && node instanceof GPXFile) {
|
|
|
|
file.metadata.name = name;
|
|
|
|
file.metadata.desc = description;
|
|
|
|
} else if (item instanceof ListTrackItem && node instanceof Track) {
|
|
|
|
file.trk[item.getTrackIndex()].name = name;
|
|
|
|
file.trk[item.getTrackIndex()].desc = description;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
openEditMetadata = false;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Save size="16" class="mr-1" />
|
|
|
|
{$_('menu.metadata.save')}
|
|
|
|
</Button>
|
|
|
|
</Popover.Content>
|
|
|
|
</Popover.Root>
|
|
|
|
{/if}
|
2024-05-21 17:47:08 +02:00
|
|
|
<span
|
2024-05-24 13:16:41 +02:00
|
|
|
class="w-full text-left truncate py-1 flex flex-row items-center"
|
2024-05-21 17:47:08 +02:00
|
|
|
on:contextmenu={(e) => {
|
|
|
|
if (e.ctrlKey) {
|
|
|
|
// Add to selection instead of opening context menu
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2024-05-22 16:05:31 +02:00
|
|
|
$selection.toggle(item);
|
2024-05-21 17:47:08 +02:00
|
|
|
}
|
|
|
|
}}
|
2024-05-24 13:16:41 +02:00
|
|
|
on:mouseenter={() => {
|
|
|
|
if (item instanceof ListWaypointItem) {
|
2024-05-24 20:23:49 +02:00
|
|
|
let layer = gpxLayers.get(item.getFileId());
|
2024-06-15 18:44:17 +02:00
|
|
|
let file = getFile(item.getFileId());
|
|
|
|
if (layer && file) {
|
|
|
|
let waypoint = file.wpt[item.getWaypointIndex()];
|
2024-05-24 13:16:41 +02:00
|
|
|
if (waypoint) {
|
|
|
|
layer.showWaypointPopup(waypoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
on:mouseleave={() => {
|
|
|
|
if (item instanceof ListWaypointItem) {
|
2024-05-24 20:23:49 +02:00
|
|
|
let layer = gpxLayers.get(item.getFileId());
|
2024-05-24 13:16:41 +02:00
|
|
|
if (layer) {
|
|
|
|
layer.hideWaypointPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2024-05-21 17:47:08 +02:00
|
|
|
>
|
2024-05-24 13:16:41 +02:00
|
|
|
{#if item.level === ListLevel.SEGMENT}
|
|
|
|
<Waypoints size="16" class="mr-1 shrink-0" />
|
|
|
|
{:else if item.level === ListLevel.WAYPOINT}
|
|
|
|
<MapPin size="16" class="mr-1 shrink-0" />
|
|
|
|
{/if}
|
|
|
|
<span class="grow truncate">
|
|
|
|
{label}
|
|
|
|
</span>
|
2024-05-21 17:47:08 +02:00
|
|
|
</span>
|
|
|
|
</Button>
|
|
|
|
</ContextMenu.Trigger>
|
|
|
|
<ContextMenu.Content>
|
2024-06-18 15:32:54 +02:00
|
|
|
{#if $verticalFileView}
|
2024-06-13 19:11:54 +02:00
|
|
|
{#if item instanceof ListFileItem}
|
|
|
|
<ContextMenu.Item
|
2024-06-18 15:32:54 +02:00
|
|
|
disabled={!singleSelection}
|
2024-06-13 19:11:54 +02:00
|
|
|
on:click={() =>
|
|
|
|
dbUtils.applyToFile(
|
|
|
|
item.getFileId(),
|
|
|
|
(file) => file.replaceTracks(file.trk.length, file.trk.length, [new Track()])[0]
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Plus size="16" class="mr-1" />
|
|
|
|
{$_('menu.new_track')}
|
|
|
|
</ContextMenu.Item>
|
|
|
|
{:else if item instanceof ListTrackItem}
|
|
|
|
<ContextMenu.Item
|
2024-06-18 15:32:54 +02:00
|
|
|
disabled={!singleSelection}
|
2024-06-13 19:11:54 +02:00
|
|
|
on:click={() => {
|
|
|
|
let trackIndex = item.getTrackIndex();
|
|
|
|
dbUtils.applyToFile(
|
|
|
|
item.getFileId(),
|
|
|
|
(file) =>
|
|
|
|
file.replaceTrackSegments(
|
|
|
|
trackIndex,
|
|
|
|
file.trk[trackIndex].trkseg.length,
|
|
|
|
file.trk[trackIndex].trkseg.length,
|
|
|
|
[new TrackSegment()]
|
|
|
|
)[0]
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Plus size="16" class="mr-1" />
|
|
|
|
{$_('menu.new_segment')}
|
|
|
|
</ContextMenu.Item>
|
|
|
|
{/if}
|
|
|
|
{/if}
|
2024-06-18 15:32:54 +02:00
|
|
|
{#if item instanceof ListFileItem || item instanceof ListTrackItem}
|
|
|
|
<ContextMenu.Item disabled={!singleSelection} on:click={() => (openEditMetadata = true)}>
|
|
|
|
<Info size="16" class="mr-1" />
|
|
|
|
{$_('menu.metadata.button')}
|
|
|
|
</ContextMenu.Item>
|
|
|
|
<ContextMenu.Separator />
|
|
|
|
{/if}
|
2024-05-24 13:16:41 +02:00
|
|
|
{#if item.level !== ListLevel.WAYPOINTS}
|
2024-05-23 14:44:07 +02:00
|
|
|
<ContextMenu.Item on:click={dbUtils.duplicateSelection}>
|
|
|
|
<Copy size="16" class="mr-1" />
|
|
|
|
{$_('menu.duplicate')}
|
|
|
|
<Shortcut key="D" ctrl={true} /></ContextMenu.Item
|
|
|
|
>
|
|
|
|
<ContextMenu.Separator />
|
|
|
|
{/if}
|
2024-05-22 16:05:31 +02:00
|
|
|
<ContextMenu.Item on:click={dbUtils.deleteSelection}
|
2024-05-21 17:47:08 +02:00
|
|
|
><Trash2 size="16" class="mr-1" />
|
|
|
|
{$_('menu.delete')}
|
|
|
|
<Shortcut key="⌫" ctrl={true} /></ContextMenu.Item
|
|
|
|
>
|
|
|
|
</ContextMenu.Content>
|
|
|
|
</ContextMenu.Root>
|