rework bounds logic, related to #8

This commit is contained in:
vcoppe
2024-07-17 10:23:04 +02:00
parent fe100f9247
commit 004f08a8d3
2 changed files with 26 additions and 28 deletions

View File

@@ -2,7 +2,7 @@ import Dexie, { liveQuery } from 'dexie';
import { GPXFile, GPXStatistics, Track, TrackSegment, Waypoint, TrackPoint, type Coordinates, distance, type LineStyleExtension } from 'gpx'; import { GPXFile, GPXStatistics, Track, TrackSegment, Waypoint, TrackPoint, type Coordinates, distance, type LineStyleExtension } from 'gpx';
import { enableMapSet, enablePatches, applyPatches, type Patch, type WritableDraft, freeze, produceWithPatches } from 'immer'; import { enableMapSet, enablePatches, applyPatches, type Patch, type WritableDraft, freeze, produceWithPatches } from 'immer';
import { writable, get, derived, type Readable, type Writable } from 'svelte/store'; import { writable, get, derived, type Readable, type Writable } from 'svelte/store';
import { gpxStatistics, initTargetMapBounds, splitAs, updateAllHidden, updateTargetMapBounds } from './stores'; import { Tool, currentTool, gpxStatistics, initTargetMapBounds, splitAs, updateAllHidden, updateTargetMapBounds } from './stores';
import { defaultBasemap, defaultBasemapTree, defaultOverlayTree, defaultOverlays, type CustomLayer, defaultOpacities } from './assets/layers'; import { defaultBasemap, defaultBasemapTree, defaultOverlayTree, defaultOverlays, type CustomLayer, defaultOpacities } from './assets/layers';
import { applyToOrderedItemsFromFile, applyToOrderedSelectedItemsFromFile, selection } from '$lib/components/file-list/Selection'; import { applyToOrderedItemsFromFile, applyToOrderedSelectedItemsFromFile, selection } from '$lib/components/file-list/Selection';
import { ListFileItem, ListItem, ListTrackItem, ListLevel, ListTrackSegmentItem, ListWaypointItem, ListRootItem } from '$lib/components/file-list/FileList'; import { ListFileItem, ListItem, ListTrackItem, ListLevel, ListTrackSegmentItem, ListWaypointItem, ListRootItem } from '$lib/components/file-list/FileList';
@@ -295,8 +295,8 @@ export function observeFilesFromDatabase() {
// Update the store // Update the store
if (newFiles.length > 0 || deletedFiles.length > 0) { if (newFiles.length > 0 || deletedFiles.length > 0) {
fileObservers.update($files => { fileObservers.update($files => {
if (newFiles.length > 0) { // Reset the target map bounds when new files are added if (newFiles.length > 0 && get(currentTool) !== Tool.SCISSORS) { // Reset the target map bounds when new files are added
initTargetMapBounds($files.size === 0); initTargetMapBounds(newFiles.length);
} }
newFiles.forEach(id => { newFiles.forEach(id => {
$files.set(id, dexieGPXFileStore(id)); $files.set(id, dexieGPXFileStore(id));

View File

@@ -1,4 +1,4 @@
import { writable, get, type Writable } from 'svelte/store'; import { writable, get, type Writable, derived } from 'svelte/store';
import mapboxgl from 'mapbox-gl'; import mapboxgl from 'mapbox-gl';
import { GPXFile, buildGPX, parseGPX, GPXStatistics, type Coordinates } from 'gpx'; import { GPXFile, buildGPX, parseGPX, GPXStatistics, type Coordinates } from 'gpx';
@@ -68,42 +68,40 @@ gpxStatistics.subscribe(() => {
const targetMapBounds = writable({ const targetMapBounds = writable({
bounds: new mapboxgl.LngLatBounds([180, 90, -180, -90]), bounds: new mapboxgl.LngLatBounds([180, 90, -180, -90]),
count: 0 count: 0,
total: -1
}); });
targetMapBounds.subscribe((bounds) => { derived([targetMapBounds, map], x => x).subscribe(([bounds, $map]) => {
if (bounds.count === 0) { if ($map === null || bounds.count !== bounds.total) {
return; return;
} }
let currentBounds = get(map)?.getBounds(); let currentBounds = $map.getBounds();
if (bounds.count !== get(fileObservers).size && if (bounds.count !== get(fileObservers).size && currentBounds) {
currentBounds && currentBounds.contains(bounds.bounds.getSouthEast()) && currentBounds.contains(bounds.bounds.getNorthWest())) { // There are other files on the map
return;
}
map.subscribe((m) => { if (currentBounds.contains(bounds.bounds.getSouthEast()) && currentBounds.contains(bounds.bounds.getNorthWest())) {
if (m) { return;
m.fitBounds(bounds.bounds, {
padding: 80,
linear: true,
easing: () => 1
});
} }
bounds.bounds.extend(currentBounds.getSouthWest());
bounds.bounds.extend(currentBounds.getNorthEast());
}
$map.fitBounds(bounds.bounds, {
padding: 80,
linear: true,
easing: () => 1
}); });
}); });
export function initTargetMapBounds(first: boolean) { export function initTargetMapBounds(total: number) {
let bounds = new mapboxgl.LngLatBounds([180, 90, -180, -90]);
let mapBounds = new mapboxgl.LngLatBounds([180, 90, -180, -90]);
if (!first) { // Some files are already loaded
mapBounds = get(map)?.getBounds() ?? mapBounds;
bounds.extend(mapBounds);
}
targetMapBounds.set({ targetMapBounds.set({
bounds: bounds, bounds: new mapboxgl.LngLatBounds([180, 90, -180, -90]),
count: 0 count: 0,
total: total
}); });
} }