Files
gpx.studio/website/src/lib/components/file-list/FileListNodeLabel.svelte

328 lines
13 KiB
Svelte
Raw Normal View History

2024-05-21 13:22:14 +02:00
<script lang="ts">
import { Button } from '$lib/components/ui/button';
import * as ContextMenu from '$lib/components/ui/context-menu';
import Shortcut from '$lib/components/Shortcut.svelte';
import {
Copy,
Info,
MapPin,
PaintBucket,
Plus,
Trash2,
Waypoints,
Eye,
EyeOff,
ClipboardCopy,
ClipboardPaste,
Maximize,
Scissors,
FileStack,
2025-06-21 21:07:36 +02:00
} from '@lucide/svelte';
import {
ListFileItem,
ListLevel,
ListTrackItem,
ListWaypointItem,
type ListItem,
2025-10-05 19:34:05 +02:00
} from './file-list';
import { getContext } from 'svelte';
import { GPXTreeElement, Track, type AnyGPXTreeElement, Waypoint, GPXFile } from 'gpx';
2025-06-21 21:07:36 +02:00
import { i18n } from '$lib/i18n.svelte';
import MetadataDialog from '$lib/components/file-list/metadata/MetadataDialog.svelte';
import { editMetadata } from '$lib/components/file-list/metadata/utils.svelte';
2025-10-26 12:12:23 +01:00
import StyleDialog from '$lib/components/file-list/style/StyleDialog.svelte';
2025-06-21 21:07:36 +02:00
import { editStyle } from '$lib/components/file-list/style/utils.svelte';
import { getSymbolKey, symbols } from '$lib/assets/symbols';
2025-10-18 09:36:55 +02:00
import { selection, copied, cut } from '$lib/logic/selection';
2025-10-17 23:54:45 +02:00
import { map } from '$lib/components/map/map';
import { fileActions, pasteSelection } from '$lib/logic/file-actions';
2025-10-19 13:45:05 +02:00
import { allHidden } from '$lib/logic/hidden';
2025-10-19 16:14:05 +02:00
import { boundsManager } from '$lib/logic/bounds';
2025-10-26 12:12:23 +01:00
import { gpxLayers } from '$lib/components/map/gpx-layer/gpx-layers';
import { fileStateCollection } from '$lib/logic/file-state';
import { waypointPopup } from '$lib/components/map/gpx-layer/gpx-layer-popup';
import { allowedPastes } from './sortable-file-list';
2024-05-21 13:22:14 +02:00
2025-06-21 21:07:36 +02:00
let {
node,
item,
label,
}: {
node: GPXTreeElement<AnyGPXTreeElement> | Waypoint[] | Waypoint;
item: ListItem;
label: string | undefined;
} = $props();
2024-05-21 13:22:14 +02:00
let orientation = getContext<'vertical' | 'horizontal'>('orientation');
2025-10-05 19:34:05 +02:00
let embedding = getContext<boolean>('embedding');
2025-06-21 21:07:36 +02:00
let singleSelection = $derived($selection.size === 1);
2024-06-19 16:15:21 +02:00
2025-10-26 12:12:23 +01:00
let nodeColors: string[] = $state([]);
$effect.pre(() => {
2025-06-21 21:07:36 +02:00
let colors: string[] = [];
2025-10-26 12:12:23 +01:00
if (node && $map) {
2025-06-21 21:07:36 +02:00
if (node instanceof GPXFile) {
let defaultColor = undefined;
2024-06-19 16:15:21 +02:00
2025-10-26 12:12:23 +01:00
let layer = gpxLayers.getLayer(item.getFileId());
if (layer) {
2025-06-21 21:07:36 +02:00
defaultColor = layer.layerColor;
}
let style = node.getStyle(defaultColor);
style.color.forEach((c) => {
if (!colors.includes(c)) {
colors.push(c);
}
});
} else if (node instanceof Track) {
let style = node.getStyle();
if (style) {
2025-10-26 12:12:23 +01:00
if (style['gpx_style:color'] && !colors.includes(style['gpx_style:color'])) {
colors.push(style['gpx_style:color']);
2025-06-21 21:07:36 +02:00
}
}
if (colors.length === 0) {
2025-10-26 12:12:23 +01:00
let layer = gpxLayers.getLayer(item.getFileId());
2025-06-21 21:07:36 +02:00
if (layer) {
colors.push(layer.layerColor);
}
}
}
}
2025-10-26 12:12:23 +01:00
nodeColors = colors;
});
2024-06-27 18:23:11 +02:00
2025-06-21 21:07:36 +02:00
let symbolKey = $derived(node instanceof Waypoint ? getSymbolKey(node.sym) : undefined);
2025-01-26 12:48:23 +01:00
2025-10-17 23:54:45 +02:00
let openEditMetadata: boolean = $derived(
editMetadata.current && singleSelection && $selection.has(item)
);
let openEditStyle: boolean = $derived(
editStyle.current &&
2025-06-21 21:07:36 +02:00
$selection.has(item) &&
2025-10-17 23:54:45 +02:00
$selection.getSelected().findIndex((i) => i.getFullId() === item.getFullId()) === 0
);
2025-06-21 21:07:36 +02:00
let hidden = $derived(
item.level === ListLevel.WAYPOINTS ? node._data.hiddenWpt : node._data.hidden
);
2024-05-21 13:22:14 +02:00
</script>
2025-06-21 21:07:36 +02:00
<!-- 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) {
2025-10-26 12:12:23 +01:00
if (!$selection.has(item)) {
selection.selectItem(item);
}
}
}}
2024-05-23 14:44:07 +02:00
>
<ContextMenu.Trigger class="grow truncate">
<Button
variant="ghost"
2025-10-26 12:12:23 +01:00
class="relative w-full p-0 overflow-hidden focus-visible:ring-0 focus-visible:ring-offset-0 {orientation ===
'vertical'
? 'h-fit'
: 'h-9 px-1.5 shadow-md'} pointer-events-auto"
>
{#if item instanceof ListFileItem || item instanceof ListTrackItem}
<MetadataDialog bind:open={openEditMetadata} {node} {item} />
<StyleDialog bind:open={openEditStyle} {item} />
{/if}
{#if item.level === ListLevel.FILE || item.level === ListLevel.TRACK}
<div
class="absolute {orientation === 'vertical'
? 'top-0 bottom-0 right-1 w-1'
: 'top-0 h-1 left-0 right-0'}"
style="background:linear-gradient(to {orientation === 'vertical'
? 'bottom'
: 'right'},{nodeColors
.map(
(c, i) =>
`${c} ${Math.floor((100 * i) / nodeColors.length)}% ${Math.floor((100 * (i + 1)) / nodeColors.length)}%`
)
.join(',')})"
2025-06-08 16:32:41 +02:00
></div>
{/if}
<span
class="w-full text-left truncate py-1 flex flex-row items-center {hidden
? 'text-muted-foreground'
2025-10-18 09:36:55 +02:00
: ''} {$cut && $copied?.some((i) => i.getFullId() === item.getFullId())
? 'text-muted-foreground'
: ''}"
2025-06-21 21:07:36 +02:00
oncontextmenu={(e) => {
2025-10-05 19:34:05 +02:00
if (embedding) {
e.preventDefault();
e.stopPropagation();
return;
}
if (e.ctrlKey) {
// Add to selection instead of opening context menu
e.preventDefault();
e.stopPropagation();
$selection.toggle(item);
$selection = $selection;
}
}}
2025-06-21 21:07:36 +02:00
onmouseenter={() => {
if (item instanceof ListWaypointItem) {
2025-10-26 12:12:23 +01:00
let layer = gpxLayers.getLayer(item.getFileId());
let file = fileStateCollection.getFile(item.getFileId());
if (layer && file) {
let waypoint = file.wpt[item.getWaypointIndex()];
if (waypoint) {
waypointPopup?.setItem({
item: waypoint,
fileId: item.getFileId(),
});
}
}
}
}}
2025-06-21 21:07:36 +02:00
onmouseleave={() => {
if (item instanceof ListWaypointItem) {
2025-10-26 12:12:23 +01:00
let layer = gpxLayers.getLayer(item.getFileId());
if (layer) {
waypointPopup?.setItem(null);
}
}
}}
>
{#if item.level === ListLevel.SEGMENT}
2025-10-26 12:12:23 +01:00
<Waypoints size="16" class="mx-1 shrink-0" />
{:else if item.level === ListLevel.WAYPOINT}
{#if symbolKey && symbols[symbolKey].icon}
2025-06-21 21:07:36 +02:00
{@const SymbolIcon = symbols[symbolKey].icon}
2025-10-26 12:12:23 +01:00
<SymbolIcon size="16" class="mx-1 shrink-0" />
{:else}
2025-10-26 12:12:23 +01:00
<MapPin size="16" class="mx-1 shrink-0" />
{/if}
{/if}
<span
class="grow select-none truncate {orientation === 'vertical'
? 'last:mr-2'
: ''}"
>
{label}
</span>
{#if hidden}
<EyeOff
2025-10-26 12:12:23 +01:00
size="10"
class="shrink-0 size-3.5 ml-1 {orientation === 'vertical'
? 'mr-3'
2025-10-26 12:12:23 +01:00
: 'mt-0.5'}"
/>
{/if}
</span>
</Button>
</ContextMenu.Trigger>
<ContextMenu.Content>
{#if item instanceof ListFileItem || item instanceof ListTrackItem}
2025-06-21 21:07:36 +02:00
<ContextMenu.Item
disabled={!singleSelection}
onclick={() => (editMetadata.current = true)}
>
2025-10-26 12:12:23 +01:00
<Info size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.metadata.button')}
<Shortcut key="I" ctrl={true} />
</ContextMenu.Item>
2025-06-21 21:07:36 +02:00
<ContextMenu.Item onclick={() => (editStyle.current = true)}>
2025-10-26 12:12:23 +01:00
<PaintBucket size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.style.button')}
</ContextMenu.Item>
{/if}
<ContextMenu.Item
2025-06-21 21:07:36 +02:00
onclick={() => {
2025-10-19 13:45:05 +02:00
if ($allHidden) {
fileActions.setHiddenToSelection(false);
} else {
fileActions.setHiddenToSelection(true);
}
}}
>
2025-10-19 13:45:05 +02:00
{#if $allHidden}
2025-10-26 12:12:23 +01:00
<Eye size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.unhide')}
{:else}
2025-10-26 12:12:23 +01:00
<EyeOff size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.hide')}
2025-10-19 13:45:05 +02:00
{/if}
<Shortcut key="H" ctrl={true} />
</ContextMenu.Item>
<ContextMenu.Separator />
{#if orientation === 'vertical'}
{#if item instanceof ListFileItem}
<ContextMenu.Item
disabled={!singleSelection}
2025-10-17 23:54:45 +02:00
onclick={() => fileActions.addNewTrack(item.getFileId())}
>
2025-10-26 12:12:23 +01:00
<Plus size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.new_track')}
</ContextMenu.Item>
<ContextMenu.Separator />
{:else if item instanceof ListTrackItem}
<ContextMenu.Item
disabled={!singleSelection}
2025-10-17 23:54:45 +02:00
onclick={() =>
fileActions.addNewSegment(item.getFileId(), item.getTrackIndex())}
>
2025-10-26 12:12:23 +01:00
<Plus size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.new_segment')}
</ContextMenu.Item>
<ContextMenu.Separator />
{/if}
{/if}
{#if item.level !== ListLevel.WAYPOINTS}
2025-10-17 23:54:45 +02:00
<ContextMenu.Item onclick={() => selection.selectAll()}>
2025-10-26 12:12:23 +01:00
<FileStack size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.select_all')}
<Shortcut key="A" ctrl={true} />
</ContextMenu.Item>
{/if}
2025-10-19 16:14:05 +02:00
<ContextMenu.Item onclick={() => boundsManager.centerMapOnSelection()}>
2025-10-26 12:12:23 +01:00
<Maximize size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.center')}
<Shortcut key="⏎" ctrl={true} />
</ContextMenu.Item>
<ContextMenu.Separator />
2025-10-17 23:54:45 +02:00
<ContextMenu.Item onclick={fileActions.duplicateSelection}>
2025-10-26 12:12:23 +01:00
<Copy size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.duplicate')}
2025-10-26 12:12:23 +01:00
<Shortcut key="D" ctrl={true} />
</ContextMenu.Item>
{#if orientation === 'vertical'}
2025-10-17 23:54:45 +02:00
<ContextMenu.Item onclick={() => selection.copySelection()}>
2025-10-26 12:12:23 +01:00
<ClipboardCopy size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.copy')}
<Shortcut key="C" ctrl={true} />
</ContextMenu.Item>
2025-10-17 23:54:45 +02:00
<ContextMenu.Item onclick={() => selection.cutSelection()}>
2025-10-26 12:12:23 +01:00
<Scissors size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.cut')}
<Shortcut key="X" ctrl={true} />
</ContextMenu.Item>
<ContextMenu.Item
2025-10-18 09:36:55 +02:00
disabled={$copied === undefined ||
$copied.length === 0 ||
!allowedPastes[$copied[0].level].includes(item.level)}
2025-06-21 21:07:36 +02:00
onclick={pasteSelection}
>
2025-10-26 12:12:23 +01:00
<ClipboardPaste size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.paste')}
<Shortcut key="V" ctrl={true} />
</ContextMenu.Item>
{/if}
<ContextMenu.Separator />
2025-10-17 23:54:45 +02:00
<ContextMenu.Item onclick={fileActions.deleteSelection}>
2025-10-26 12:12:23 +01:00
<Trash2 size="16" />
{i18n._('menu.delete')}
<Shortcut key="⌫" ctrl={true} />
</ContextMenu.Item>
</ContextMenu.Content>
</ContextMenu.Root>