working file tree

This commit is contained in:
vcoppe
2024-05-21 13:22:14 +02:00
parent b997b7518a
commit 44b270e2c2
5 changed files with 156 additions and 42 deletions

View File

@@ -1,14 +1,13 @@
<script lang="ts"> <script lang="ts">
import GPXLayers from '$lib/components/gpx-layer/GPXLayers.svelte'; import GPXLayers from '$lib/components/gpx-layer/GPXLayers.svelte';
import ElevationProfile from '$lib/components/ElevationProfile.svelte'; import ElevationProfile from '$lib/components/ElevationProfile.svelte';
import FileList from '$lib/components/FileList.svelte'; import FileList from '$lib/components/file-list/FileList.svelte';
import GPXStatistics from '$lib/components/GPXStatistics.svelte'; import GPXStatistics from '$lib/components/GPXStatistics.svelte';
import Map from '$lib/components/Map.svelte'; import Map from '$lib/components/Map.svelte';
import Menu from '$lib/components/Menu.svelte'; import Menu from '$lib/components/Menu.svelte';
import Toolbar from '$lib/components/toolbar/Toolbar.svelte'; import Toolbar from '$lib/components/toolbar/Toolbar.svelte';
import LayerControl from '$lib/components/layer-control/LayerControl.svelte'; import LayerControl from '$lib/components/layer-control/LayerControl.svelte';
import { Toaster } from '$lib/components/ui/sonner'; import { Toaster } from '$lib/components/ui/sonner';
import FileList2 from './file-list/FileList2.svelte';
</script> </script>
<div class="flex flex-row w-screen h-screen"> <div class="flex flex-row w-screen h-screen">
@@ -26,7 +25,7 @@
<ElevationProfile /> <ElevationProfile />
</div> </div>
</div> </div>
<FileList2 /> <FileList />
</div> </div>
<style lang="postcss"> <style lang="postcss">

View File

@@ -3,10 +3,16 @@
import FileListNode from './FileListNode.svelte'; import FileListNode from './FileListNode.svelte';
import { fileObservers } from '$lib/db'; import { fileObservers } from '$lib/db';
import { setContext } from 'svelte';
import { writable } from 'svelte/store';
export let selected = writable(new Set<string>());
setContext('selected', selected);
</script> </script>
<ScrollArea class="w-fit p-1 pr-4 border"> <ScrollArea class="w-fit p-1 pr-4 border">
<div class="w-60 flex flex-col gap-1"> <div class="w-60 flex flex-col">
<FileListNode node={$fileObservers} id="root" /> <FileListNode node={$fileObservers} id="root" />
</div> </div>
</ScrollArea> </ScrollArea>

View File

@@ -5,6 +5,7 @@
import type { Readable } from 'svelte/store'; import type { Readable } from 'svelte/store';
import FileListNodeContent from './FileListNodeContent.svelte'; import FileListNodeContent from './FileListNodeContent.svelte';
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher } from 'svelte';
import FileListNodeLabel from './FileListNodeLabel.svelte';
export let node: export let node:
| Map<string, Readable<GPXFileWithStatistics | undefined>> | Map<string, Readable<GPXFileWithStatistics | undefined>>
@@ -24,15 +25,17 @@
<FileListNodeContent {node} {id} /> <FileListNodeContent {node} {id} />
{:else} {:else}
<CollapsibleTreeNode {id} on:click={forwardId}> <CollapsibleTreeNode {id} on:click={forwardId}>
<span slot="trigger" class="truncate"> <FileListNodeLabel
{#if node instanceof GPXFile} {id}
{node.metadata.name} label={node instanceof GPXFile
{:else if node instanceof Track} ? node.metadata.name
{node.name ?? `Track ${index + 1}`} : node instanceof Track
{:else if Array.isArray(node) && node.length > 0 && node[0] instanceof Waypoint} ? node.name ?? `Track ${index + 1}`
Waypoints : Array.isArray(node) && node.length > 0 && node[0] instanceof Waypoint
{/if} ? 'Waypoints'
</span> : ''}
slot="trigger"
/>
<div slot="content"> <div slot="content">
<FileListNodeContent {node} {id} /> <FileListNodeContent {node} {id} />
</div> </div>

View File

@@ -1,25 +1,30 @@
<script lang="ts"> <script lang="ts">
import { GPXFile, Track, Waypoint, type AnyGPXTreeElement, type GPXTreeElement } from 'gpx'; import { GPXFile, Track, Waypoint, type AnyGPXTreeElement, type GPXTreeElement } from 'gpx';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { onMount } from 'svelte'; import { getContext, onDestroy, onMount } from 'svelte';
import Sortable from 'sortablejs/Sortable'; import Sortable from 'sortablejs/Sortable';
import type { GPXFileWithStatistics } from '$lib/db'; import type { GPXFileWithStatistics } from '$lib/db';
import type { Readable } from 'svelte/store'; import type { Readable, Writable } from 'svelte/store';
import FileListNodeStore from './FileListNodeStore.svelte'; import FileListNodeStore from './FileListNodeStore.svelte';
import FileListNode from './FileListNode.svelte'; import FileListNode from './FileListNode.svelte';
import FileListNodeLabel from './FileListNodeLabel.svelte';
export let node: export let node:
| Map<string, Readable<GPXFileWithStatistics | undefined>> | Map<string, Readable<GPXFileWithStatistics | undefined>>
| GPXTreeElement<AnyGPXTreeElement> | GPXTreeElement<AnyGPXTreeElement>
| ReadonlyArray<Readonly<Waypoint>>; | ReadonlyArray<Readonly<Waypoint>>;
export let waypointRoot: boolean = false;
export let id: string; export let id: string;
let container: HTMLElement; let container: HTMLElement;
let items: { [id: string | number]: HTMLElement } = {};
let sortableLevel = let sortableLevel =
node instanceof Map node instanceof Map
? 'file' ? 'file'
: node instanceof GPXFile : node instanceof GPXFile
? 'track' ? waypointRoot
? 'waypoints'
: 'track'
: node instanceof Track : node instanceof Track
? 'segment' ? 'segment'
: 'waypoint'; : 'waypoint';
@@ -31,6 +36,20 @@
}; };
let sortable: Sortable; let sortable: Sortable;
let selected = getContext<Writable<Set<string>>>('selected');
function onSelectChange() {
selected.update(($selected) => {
$selected.clear();
Object.entries(items).forEach(([id, item]) => {
if (item.classList.contains('sortable-selected')) {
$selected.add(id);
}
});
return $selected;
});
}
onMount(() => { onMount(() => {
sortable = Sortable.create(container, { sortable = Sortable.create(container, {
group: { group: {
@@ -39,67 +58,119 @@
forceAutoScrollFallback: true, forceAutoScrollFallback: true,
multiDrag: true, multiDrag: true,
multiDragKey: 'Meta', multiDragKey: 'Meta',
selectedClass: 'sortable-selected', avoidImplicitDeselect: true,
avoidImplicitDeselect: true onSelect: onSelectChange,
onDeselect: onSelectChange,
sort: sortableLevel !== 'waypoint'
}); });
}); });
function handleClick(id: string) { function handleClick(id: string) {
console.log('handle click for', id); //console.log('handle click for', id);
}
const unsubscribe = selected.subscribe(($selected) => {
Object.entries(items).forEach(([id, item]) => {
if ($selected.has(id) && !item.classList.contains('sortable-selected')) {
Sortable.utils.select(item);
} else if (!$selected.has(id) && item.classList.contains('sortable-selected')) {
Sortable.utils.deselect(item);
}
});
});
onDestroy(() => {
unsubscribe();
});
function getChildId(i: number): string {
switch (sortableLevel) {
case 'track':
return `${id}-track-${i}`;
case 'segment':
return `${id}-seg-${i}`;
case 'waypoint':
return `${id}-${i}`;
}
return '';
} }
</script> </script>
<div bind:this={container} class="flex flex-col gap-0.5"> <div bind:this={container} class="sortable flex flex-col">
{#if node instanceof Map} {#if node instanceof Map}
{#each Array.from(node.values()) as file} {#each node as [fileId, file]}
<div> <div bind:this={items[fileId]}>
<FileListNodeStore {file} on:click={(e) => handleClick(e.detail.id)} /> <FileListNodeStore {file} on:click={(e) => handleClick(e.detail.id)} />
</div> </div>
{/each} {/each}
{:else if node instanceof GPXFile} {:else if node instanceof GPXFile}
{#each node.children as child, i} {#if waypointRoot}
<div> {#if node.wpt.length > 0}
<FileListNode <div bind:this={items[`${id}-wpt`]}>
node={child} <FileListNode
id={`${id}-track-${i}`} node={node.wpt}
index={i} id={`${id}-wpt`}
on:click={(e) => handleClick(e.detail.id)} on:click={(e) => handleClick(e.detail.id)}
/> />
</div> </div>
{/each} {/if}
{#if node.wpt.length > 0} {:else}
<FileListNode node={node.wpt} id={`${id}-wpt`} on:click={(e) => handleClick(e.detail.id)} /> {#each node.children as child, i}
<div bind:this={items[getChildId(i)]}>
<FileListNode
node={child}
id={getChildId(i)}
index={i}
on:click={(e) => handleClick(e.detail.id)}
/>
</div>
{/each}
{/if} {/if}
{:else if node instanceof Track} {:else if node instanceof Track}
{#each node.children as child, i} {#each node.children as child, i}
<div> <div bind:this={items[getChildId(i)]}>
<div> <div>
<Button <Button
variant="ghost" variant="ghost"
class="ml-1 truncate flex flex-row justify-start py-0 px-1 h-fit" class="p-0 px-1 h-fit w-full"
on:click={() => handleClick(`${id}-seg-${i + 1}`)}>{`Segment ${i + 1}`}</Button on:click={() => handleClick(getChildId(i))}
> >
<FileListNodeLabel id={getChildId(i)} label={`Segment ${i + 1}`} />
</Button>
</div> </div>
</div> </div>
{/each} {/each}
{:else if Array.isArray(node) && node.length > 0 && node[0] instanceof Waypoint} {:else if Array.isArray(node) && node.length > 0 && node[0] instanceof Waypoint}
{#each node as wpt, i} {#each node as wpt, i}
<div> <div bind:this={items[getChildId(i)]}>
<div> <div>
<Button <Button
variant="ghost" variant="ghost"
class="ml-1 flex flex-row justify-start py-0 px-1 h-fit" class="p-0 px-1 h-fit w-full"
on:click={() => handleClick(`${id}-${i + 1}`)} on:click={() => handleClick(getChildId(i))}
><span class="truncate">{wpt.name ?? `Waypoint ${i + 1}`}</span></Button
> >
<FileListNodeLabel id={getChildId(i)} label={wpt.name ?? `Waypoint ${i + 1}`} />
</Button>
</div> </div>
</div> </div>
{/each} {/each}
{/if} {/if}
</div> </div>
{#if node instanceof GPXFile}
{#if !waypointRoot}
<svelte:self {node} {id} waypointRoot={true} />
{/if}
{/if}
<style lang="postcss"> <style lang="postcss">
div :global(.sortable-selected > * > button) { .sortable > div {
@apply rounded-md;
@apply h-fit;
@apply leading-none;
}
div :global(.sortable-selected) {
@apply bg-accent; @apply bg-accent;
} }
</style> </style>

View File

@@ -0,0 +1,35 @@
<script lang="ts">
import { getContext } from 'svelte';
import { type Writable } from 'svelte/store';
export let id: string;
export let label: string | undefined;
let selected = getContext<Writable<Set<string>>>('selected');
</script>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<span
class="w-full text-left truncate py-1"
on:click={(e) => {
e.stopPropagation(); // Avoid toggling the collapsible element
}}
on:contextmenu={(e) => {
if (e.ctrlKey) {
// Add to selection instead of opening context menu
e.preventDefault();
e.stopPropagation();
selected.update((value) => {
if (value.has(id)) {
value.delete(id);
} else {
value.add(id);
}
return value;
});
}
}}
>
{label}
</span>