reactive modifications

This commit is contained in:
vcoppe
2024-04-24 22:35:53 +02:00
parent ae7806a553
commit e7dbb07ace
7 changed files with 88 additions and 71 deletions

View File

@@ -15,6 +15,7 @@ abstract class GPXTreeElement<T extends GPXTreeElement<any>> {
abstract getChildren(): T[]; abstract getChildren(): T[];
abstract computeStatistics(): GPXStatistics; abstract computeStatistics(): GPXStatistics;
abstract refreshStatistics(): void;
abstract append(points: TrackPoint[]): void; abstract append(points: TrackPoint[]): void;
abstract reverse(originalNextTimestamp?: Date, newPreviousTimestamp?: Date): void; abstract reverse(originalNextTimestamp?: Date, newPreviousTimestamp?: Date): void;
@@ -34,15 +35,19 @@ abstract class GPXTreeNode<T extends GPXTreeElement<any>> extends GPXTreeElement
} }
computeStatistics(): GPXStatistics { computeStatistics(): GPXStatistics {
let statistics = new GPXStatistics();
for (let child of this.getChildren()) { for (let child of this.getChildren()) {
statistics.mergeWith(child.computeStatistics()); child.computeStatistics();
} }
this.refreshStatistics();
return this.statistics;
}
this.statistics = statistics; refreshStatistics(): void {
this.statistics = new GPXStatistics();
return statistics; for (let child of this.getChildren()) {
child.refreshStatistics();
this.statistics.mergeWith(child.statistics);
}
} }
append(points: TrackPoint[]): void { append(points: TrackPoint[]): void {
@@ -53,6 +58,8 @@ abstract class GPXTreeNode<T extends GPXTreeElement<any>> extends GPXTreeElement
} }
children[children.length - 1].append(points); children[children.length - 1].append(points);
this.refreshStatistics();
} }
reverse(originalNextTimestamp?: Date, newPreviousTimestamp?: Date): void { reverse(originalNextTimestamp?: Date, newPreviousTimestamp?: Date): void {
@@ -73,6 +80,8 @@ abstract class GPXTreeNode<T extends GPXTreeElement<any>> extends GPXTreeElement
originalNextTimestamp = originalStartTimestamp; originalNextTimestamp = originalStartTimestamp;
newPreviousTimestamp = children[i].getEndTimestamp(); newPreviousTimestamp = children[i].getEndTimestamp();
} }
this.refreshStatistics();
} }
getStartTimestamp(): Date { getStartTimestamp(): Date {
@@ -352,6 +361,9 @@ export class TrackSegment extends GPXTreeLeaf {
return statistics; return statistics;
} }
// Do nothing, recompute statistics after modifying the segment only
refreshStatistics(): void { }
computeSmoothedElevation(): number[] { computeSmoothedElevation(): number[] {
const points = this.trkpt; const points = this.trkpt;
@@ -373,7 +385,7 @@ export class TrackSegment extends GPXTreeLeaf {
append(points: TrackPoint[]): void { append(points: TrackPoint[]): void {
this.trkpt = this.trkpt.concat(points); this.trkpt = this.trkpt.concat(points);
//this.computeStatistics(); this.computeStatistics();
} }
reverse(originalNextTimestamp: Date | undefined, newPreviousTimestamp: Date | undefined): void { reverse(originalNextTimestamp: Date | undefined, newPreviousTimestamp: Date | undefined): void {
@@ -393,6 +405,7 @@ export class TrackSegment extends GPXTreeLeaf {
} else { } else {
this.trkpt.reverse(); this.trkpt.reverse();
} }
this.computeStatistics();
} }
getStartTimestamp(): Date { getStartTimestamp(): Date {

View File

@@ -230,7 +230,7 @@
}); });
$: if (chart && $settings) { $: if (chart && $settings) {
let gpxFiles = new GPXFiles(Array.from($selectedFiles).map((file) => get(file))); let gpxFiles = new GPXFiles(Array.from($selectedFiles));
gpxFiles.files.sort(function (a, b) { gpxFiles.files.sort(function (a, b) {
return get(fileOrder).indexOf(a) - get(fileOrder).indexOf(b); return get(fileOrder).indexOf(a) - get(fileOrder).indexOf(b);
}); });

View File

@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { fileOrder, files, selectedFiles, selectFiles } from '$lib/stores'; import { fileOrder, files, getFileIndex, selectedFiles, selectFiles } from '$lib/stores';
import { ScrollArea } from '$lib/components/ui/scroll-area/index'; import { ScrollArea } from '$lib/components/ui/scroll-area/index';
import Sortable from 'sortablejs/Sortable'; import Sortable from 'sortablejs/Sortable';
@@ -7,13 +7,13 @@
import type { GPXFile } from 'gpx'; import type { GPXFile } from 'gpx';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { get, type Writable } from 'svelte/store'; import { get } from 'svelte/store';
let tabs: HTMLDivElement; let tabs: HTMLDivElement;
let buttons: HTMLButtonElement[] = []; let buttons: HTMLButtonElement[] = [];
let sortable: Sortable; let sortable: Sortable;
function selectFile(file: Writable<GPXFile>) { function selectFile(file: GPXFile) {
selectedFiles.update((selectedFiles) => { selectedFiles.update((selectedFiles) => {
selectedFiles.clear(); selectedFiles.clear();
selectedFiles.add(file); selectedFiles.add(file);
@@ -21,7 +21,7 @@
}); });
} }
function addSelectFile(file: Writable<GPXFile>) { function addSelectFile(file: GPXFile) {
selectedFiles.update((selectedFiles) => { selectedFiles.update((selectedFiles) => {
selectedFiles.add(file); selectedFiles.add(file);
return selectedFiles; return selectedFiles;
@@ -31,13 +31,13 @@
function selectAllFiles() { function selectAllFiles() {
selectedFiles.update((selectedFiles) => { selectedFiles.update((selectedFiles) => {
get(files).forEach((file) => { get(files).forEach((file) => {
selectedFiles.add(file); selectedFiles.add(get(file));
}); });
return selectedFiles; return selectedFiles;
}); });
} }
function deselectFile(file: Writable<GPXFile>) { function deselectFile(file: GPXFile) {
selectedFiles.update((selectedFiles) => { selectedFiles.update((selectedFiles) => {
selectedFiles.delete(file); selectedFiles.delete(file);
return selectedFiles; return selectedFiles;
@@ -53,12 +53,12 @@
avoidImplicitDeselect: true, avoidImplicitDeselect: true,
onSelect: (e) => { onSelect: (e) => {
const index = parseInt(e.item.getAttribute('data-id')); const index = parseInt(e.item.getAttribute('data-id'));
addSelectFile($files[index]); addSelectFile(get($files[index]));
if (!e.originalEvent.shiftKey && $selectedFiles.size > 1) { if (!e.originalEvent.shiftKey && $selectedFiles.size > 1) {
$selectedFiles.forEach((file) => { $selectedFiles.forEach((file) => {
if (file !== $files[index]) { if (file !== get($files[index])) {
deselectFile(file); deselectFile(file);
const index = $files.indexOf(file); const index = getFileIndex(file);
Sortable.utils.deselect(buttons[index]); Sortable.utils.deselect(buttons[index]);
} }
}); });
@@ -66,7 +66,7 @@
}, },
onDeselect: (e) => { onDeselect: (e) => {
const index = parseInt(e.item.getAttribute('data-id')); const index = parseInt(e.item.getAttribute('data-id'));
deselectFile($files[index]); deselectFile(get($files[index]));
}, },
onSort: () => { onSort: () => {
$fileOrder = sortable.toArray().map((index: string) => $files[parseInt(index)]); $fileOrder = sortable.toArray().map((index: string) => $files[parseInt(index)]);
@@ -76,18 +76,18 @@
selectFiles.update(() => { selectFiles.update(() => {
return { return {
select: (file: Writable<GPXFile>) => { select: (file: GPXFile) => {
buttons.forEach((button) => { buttons.forEach((button) => {
if (button) { if (button) {
Sortable.utils.deselect(button); Sortable.utils.deselect(button);
} }
}); });
const index = $files.indexOf(file); const index = getFileIndex(file);
Sortable.utils.select(buttons[index]); Sortable.utils.select(buttons[index]);
selectFile(file); selectFile(file);
}, },
addSelect: (file: Writable<GPXFile>) => { addSelect: (file: GPXFile) => {
const index = $files.indexOf(file); const index = getFileIndex(file);
Sortable.utils.select(buttons[index]); Sortable.utils.select(buttons[index]);
addSelectFile(file); addSelectFile(file);
}, },
@@ -97,8 +97,8 @@
}); });
selectAllFiles(); selectAllFiles();
}, },
removeSelect: (file: Writable<GPXFile>) => { removeSelect: (file: GPXFile) => {
const index = $files.indexOf(file); const index = getFileIndex(file);
Sortable.utils.deselect(buttons[index]); Sortable.utils.deselect(buttons[index]);
deselectFile(file); deselectFile(file);
} }

View File

@@ -5,7 +5,7 @@
import { GPXStatistics } from 'gpx'; import { GPXStatistics } from 'gpx';
import { selectedFiles, settings } from '$lib/stores'; import { getFileStore, selectedFiles, settings } from '$lib/stores';
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import { MoveDownRight, MoveUpRight, Ruler, Timer, Zap } from 'lucide-svelte'; import { MoveDownRight, MoveUpRight, Ruler, Timer, Zap } from 'lucide-svelte';
@@ -14,12 +14,16 @@
let gpxData: GPXStatistics = new GPXStatistics(); let gpxData: GPXStatistics = new GPXStatistics();
$: { function updateGPXData() {
gpxData = new GPXStatistics(); gpxData = new GPXStatistics();
$selectedFiles.forEach((file) => { $selectedFiles.forEach((file) => {
gpxData.mergeWith(get(file).statistics); gpxData.mergeWith(file.statistics);
}); });
} }
$: if ($selectedFiles) {
updateGPXData();
}
</script> </script>
<Card.Root class="h-full overflow-hidden border-none min-w-48 pl-4"> <Card.Root class="h-full overflow-hidden border-none min-w-48 pl-4">

View File

@@ -59,9 +59,9 @@
function selectOnClick(e: any) { function selectOnClick(e: any) {
if (e.originalEvent.shiftKey) { if (e.originalEvent.shiftKey) {
get(selectFiles).addSelect(file); get(selectFiles).addSelect(get(file));
} else { } else {
get(selectFiles).select(file); get(selectFiles).select(get(file));
} }
} }
@@ -125,16 +125,19 @@
} }
} }
$: if ($map) { $: if ($selectedFiles.has(get(file))) {
$map.on('style.load', addGPXLayer);
}
$: if ($selectedFiles.has(file)) {
if ($map) { if ($map) {
$map.moveLayer(layerId); $map.moveLayer(layerId);
} }
} }
$: if ($map) {
let source = $map.getSource(layerId);
if (source) {
source.setData(extendGeoJSON($file.toGeoJSON()));
}
}
onMount(() => { onMount(() => {
addGPXLayer(); addGPXLayer();
if ($map) { if ($map) {
@@ -178,16 +181,11 @@
} }
); );
} }
$map.on('style.load', addGPXLayer);
} }
}); });
$: if ($map) {
let source = $map.getSource(layerId);
if (source) {
source.setData(extendGeoJSON($file.toGeoJSON()));
}
}
onDestroy(() => { onDestroy(() => {
if ($map) { if ($map) {
$map.off('click', layerId, selectOnClick); $map.off('click', layerId, selectOnClick);

View File

@@ -7,8 +7,7 @@
import * as Alert from '$lib/components/ui/alert'; import * as Alert from '$lib/components/ui/alert';
import { CircleHelp } from 'lucide-svelte'; import { CircleHelp } from 'lucide-svelte';
import { map, selectedFiles } from '$lib/stores'; import { map, selectedFiles, getFileStore, applyToFile } from '$lib/stores';
import { get, type Writable } from 'svelte/store';
import { AnchorPointHierarchy, getMarker, route } from './routing'; import { AnchorPointHierarchy, getMarker, route } from './routing';
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
import mapboxgl from 'mapbox-gl'; import mapboxgl from 'mapbox-gl';
@@ -36,7 +35,7 @@
let anchorPointHierarchy: AnchorPointHierarchy | null = null; let anchorPointHierarchy: AnchorPointHierarchy | null = null;
let markers: mapboxgl.Marker[] = []; let markers: mapboxgl.Marker[] = [];
let file: Writable<GPXFile> | null = null; let file: GPXFile | null = null;
let kdbush: KDBush | null = null; let kdbush: KDBush | null = null;
function toggleMarkersForZoomLevelAndBounds() { function toggleMarkersForZoomLevelAndBounds() {
@@ -65,11 +64,7 @@
privateRoads, privateRoads,
routing routing
); );
console.log(response); applyToFile(file, (f) => f.append(response));
file.update((file) => {
file.append(response);
return file;
});
} }
} }
@@ -111,7 +106,7 @@
$map.off('move', toggleMarkersForZoomLevelAndBounds); $map.off('move', toggleMarkersForZoomLevelAndBounds);
$map.off('click', extendFile); $map.off('click', extendFile);
if (file) { if (file) {
$map.off('mouseover', get(file).layerId, showInsertableMarker); $map.off('mouseover', file.layerId, showInsertableMarker);
} }
if (insertableMarker) { if (insertableMarker) {
insertableMarker.remove(); insertableMarker.remove();
@@ -127,7 +122,7 @@
// record time // record time
let start = performance.now(); let start = performance.now();
anchorPointHierarchy = AnchorPointHierarchy.create(get(file)); anchorPointHierarchy = AnchorPointHierarchy.create(file);
// record time // record time
let end = performance.now(); let end = performance.now();
console.log('Time to create anchor points: ' + (end - start) + 'ms'); console.log('Time to create anchor points: ' + (end - start) + 'ms');
@@ -138,9 +133,9 @@
$map.on('zoom', toggleMarkersForZoomLevelAndBounds); $map.on('zoom', toggleMarkersForZoomLevelAndBounds);
$map.on('move', toggleMarkersForZoomLevelAndBounds); $map.on('move', toggleMarkersForZoomLevelAndBounds);
$map.on('click', extendFile); $map.on('click', extendFile);
$map.on('mouseover', get(file).layerId, showInsertableMarker); $map.on('mouseover', file.layerId, showInsertableMarker);
let points = get(file).getTrackPoints(); let points = file.getTrackPoints();
start = performance.now(); start = performance.now();
kdbush = new KDBush(points.length); kdbush = new KDBush(points.length);

View File

@@ -5,15 +5,32 @@ import { GPXFile, buildGPX, parseGPX } from 'gpx';
export const map = writable<mapboxgl.Map | null>(null); export const map = writable<mapboxgl.Map | null>(null);
export const files = writable<Writable<GPXFile>[]>([]); export const files = writable<Writable<GPXFile>[]>([]);
export const fileOrder = writable<Writable<GPXFile>[]>([]); export const fileOrder = writable<GPXFile[]>([]);
export const selectedFiles = writable<Set<Writable<GPXFile>>>(new Set()); export const selectedFiles = writable<Set<GPXFile>>(new Set());
export const selectFiles = writable<{ [key: string]: (file?: Writable<GPXFile>) => void }>({}); export const selectFiles = writable<{ [key: string]: (file?: GPXFile) => void }>({});
export const settings = writable<{ [key: string]: any }>({ export const settings = writable<{ [key: string]: any }>({
distanceUnits: 'metric', distanceUnits: 'metric',
velocityUnits: 'speed', velocityUnits: 'speed',
temperatureUnits: 'celsius', temperatureUnits: 'celsius',
}); });
export function getFileStore(file: GPXFile): Writable<GPXFile> {
return get(files).find(store => get(store) === file) ?? addFile(file);
}
export function getFileIndex(file: GPXFile): number {
return get(files).findIndex(store => get(store) === file);
}
export function applyToFile(file: GPXFile, callback: (file: GPXFile) => void) {
let store = getFileStore(file);
store.update($file => {
callback($file)
return $file;
});
selectedFiles.update($selected => $selected);
}
export function addFile(file: GPXFile): Writable<GPXFile> { export function addFile(file: GPXFile): Writable<GPXFile> {
let fileStore = writable(file); let fileStore = writable(file);
files.update($files => { files.update($files => {
@@ -41,7 +58,7 @@ export async function loadFiles(list: FileList) {
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
let file = await loadFile(list[i]); let file = await loadFile(list[i]);
if (i == 0 && file) { if (i == 0 && file) {
get(selectFiles).select(file); get(selectFiles).select(get(file));
} }
} }
} }
@@ -68,7 +85,7 @@ export async function loadFile(file: File) {
export function duplicateSelectedFiles() { export function duplicateSelectedFiles() {
let selected: GPXFile[] = []; let selected: GPXFile[] = [];
get(selectedFiles).forEach(file => selected.push(get(file))); get(selectedFiles).forEach(file => selected.push(file));
selected.forEach(file => duplicateFile(file)); selected.forEach(file => duplicateFile(file));
} }
@@ -81,7 +98,7 @@ export function removeSelectedFiles() {
files.update($files => { files.update($files => {
let index = 0; let index = 0;
while (index < $files.length) { while (index < $files.length) {
if (get(selectedFiles).has($files[index])) { if (get(selectedFiles).has(get($files[index]))) {
$files.splice(index, 1); $files.splice(index, 1);
} else { } else {
index++; index++;
@@ -107,7 +124,7 @@ export function removeAllFiles() {
} }
export function exportSelectedFiles() { export function exportSelectedFiles() {
get(selectedFiles).forEach(file => exportFile(get(file))); get(selectedFiles).forEach(file => exportFile(file));
} }
export async function exportAllFiles() { export async function exportAllFiles() {
@@ -128,15 +145,5 @@ export function exportFile(file: GPXFile) {
} }
export function reverseSelectedFiles() { export function reverseSelectedFiles() {
files.update($files => { get(selectedFiles).forEach(file => applyToFile(file, file => file.reverse()));
$files.forEach(file => {
if (get(selectedFiles).has(file)) {
file.update($file => {
$file.reverse();
return $file;
});
}
});
return $files;
});
} }