create file when routing with no selection
This commit is contained in:
parent
ea53a82451
commit
fe4cafaa23
3 changed files with 36 additions and 4 deletions
|
@ -20,8 +20,8 @@
|
||||||
SquareArrowOutDownRight
|
SquareArrowOutDownRight
|
||||||
} from 'lucide-svelte';
|
} from 'lucide-svelte';
|
||||||
|
|
||||||
import { map, routingControls } from '$lib/stores';
|
import { map, newGPXFile, routingControls, selectFileWhenLoaded } from '$lib/stores';
|
||||||
import { dbUtils, settings } from '$lib/db';
|
import { dbUtils, getFileIds, settings } from '$lib/db';
|
||||||
import { brouterProfiles, routingProfileSelectItem } from './Routing';
|
import { brouterProfiles, routingProfileSelectItem } from './Routing';
|
||||||
|
|
||||||
import { _ } from 'svelte-i18n';
|
import { _ } from 'svelte-i18n';
|
||||||
|
@ -33,6 +33,9 @@
|
||||||
import { selection } from '$lib/components/file-list/Selection';
|
import { selection } from '$lib/components/file-list/Selection';
|
||||||
import { ListRootItem, type ListItem } from '$lib/components/file-list/FileList';
|
import { ListRootItem, type ListItem } from '$lib/components/file-list/FileList';
|
||||||
import { flyAndScale } from '$lib/utils';
|
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 popup: mapboxgl.Popup;
|
||||||
export let popupElement: HTMLElement;
|
export let popupElement: HTMLElement;
|
||||||
|
@ -64,6 +67,33 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$: validSelection = $selection.hasAnyChildren(new ListRootItem(), true, ['waypoints']);
|
$: 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);
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $minimizeRoutingMenu}
|
{#if $minimizeRoutingMenu}
|
||||||
|
|
|
@ -381,7 +381,9 @@ export function getFileIds(n: number) {
|
||||||
// Helper functions for file operations
|
// Helper functions for file operations
|
||||||
export const dbUtils = {
|
export const dbUtils = {
|
||||||
add: (file: GPXFile) => {
|
add: (file: GPXFile) => {
|
||||||
|
if (file._data.id === undefined) {
|
||||||
file._data.id = getFileIds(1)[0];
|
file._data.id = getFileIds(1)[0];
|
||||||
|
}
|
||||||
return applyGlobal((draft) => {
|
return applyGlobal((draft) => {
|
||||||
draft.set(file._data.id, freeze(file));
|
draft.set(file._data.id, freeze(file));
|
||||||
});
|
});
|
||||||
|
|
|
@ -193,7 +193,7 @@ export async function loadFile(file: File): Promise<GPXFile | null> {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectFileWhenLoaded(fileId: string) {
|
export function selectFileWhenLoaded(fileId: string) {
|
||||||
const unsubscribe = fileObservers.subscribe((files) => {
|
const unsubscribe = fileObservers.subscribe((files) => {
|
||||||
if (files.has(fileId)) {
|
if (files.has(fileId)) {
|
||||||
tick().then(() => {
|
tick().then(() => {
|
||||||
|
|
Loading…
Reference in a new issue