From fe4cafaa23aff6990bd65bbe369535aaec9d9c10 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Sat, 15 Jun 2024 19:17:27 +0200 Subject: [PATCH] create file when routing with no selection --- .../toolbar/tools/routing/Routing.svelte | 34 +++++++++++++++++-- website/src/lib/db.ts | 4 ++- website/src/lib/stores.ts | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/website/src/lib/components/toolbar/tools/routing/Routing.svelte b/website/src/lib/components/toolbar/tools/routing/Routing.svelte index 22371600..09bfbc4f 100644 --- a/website/src/lib/components/toolbar/tools/routing/Routing.svelte +++ b/website/src/lib/components/toolbar/tools/routing/Routing.svelte @@ -20,8 +20,8 @@ SquareArrowOutDownRight } from 'lucide-svelte'; - import { map, routingControls } from '$lib/stores'; - import { dbUtils, settings } from '$lib/db'; + import { map, newGPXFile, routingControls, selectFileWhenLoaded } from '$lib/stores'; + import { dbUtils, getFileIds, settings } from '$lib/db'; import { brouterProfiles, routingProfileSelectItem } from './Routing'; import { _ } from 'svelte-i18n'; @@ -33,6 +33,9 @@ import { selection } from '$lib/components/file-list/Selection'; import { ListRootItem, type ListItem } from '$lib/components/file-list/FileList'; import { flyAndScale } from '$lib/utils'; + import { onDestroy, onMount } from 'svelte'; + import { TrackPoint } from 'gpx'; + import { produce } from 'immer'; export let popup: mapboxgl.Popup; export let popupElement: HTMLElement; @@ -64,6 +67,33 @@ } $: validSelection = $selection.hasAnyChildren(new ListRootItem(), true, ['waypoints']); + + function createFileWithPoint(e: any) { + if ($selection.size === 0) { + let file = newGPXFile(); + file = file.replaceTrackPoints(0, 0, 0, 0, [ + new TrackPoint({ + attributes: { + lat: e.lngLat.lat, + lon: e.lngLat.lng + } + }) + ]); + file = produce(file, (draft) => { + draft._data.id = getFileIds(1)[0]; + }); + dbUtils.add(file); + selectFileWhenLoaded(file._data.id); + } + } + + onMount(() => { + $map?.on('click', createFileWithPoint); + }); + + onDestroy(() => { + $map?.off('click', createFileWithPoint); + }); {#if $minimizeRoutingMenu} diff --git a/website/src/lib/db.ts b/website/src/lib/db.ts index 2070b90d..1d42c5a4 100644 --- a/website/src/lib/db.ts +++ b/website/src/lib/db.ts @@ -381,7 +381,9 @@ export function getFileIds(n: number) { // Helper functions for file operations export const dbUtils = { add: (file: GPXFile) => { - file._data.id = getFileIds(1)[0]; + if (file._data.id === undefined) { + file._data.id = getFileIds(1)[0]; + } return applyGlobal((draft) => { draft.set(file._data.id, freeze(file)); }); diff --git a/website/src/lib/stores.ts b/website/src/lib/stores.ts index f951898f..b17d163b 100644 --- a/website/src/lib/stores.ts +++ b/website/src/lib/stores.ts @@ -193,7 +193,7 @@ export async function loadFile(file: File): Promise { return result; } -function selectFileWhenLoaded(fileId: string) { +export function selectFileWhenLoaded(fileId: string) { const unsubscribe = fileObservers.subscribe((files) => { if (files.has(fileId)) { tick().then(() => {