delete any selection

This commit is contained in:
vcoppe
2024-05-23 16:35:20 +02:00
parent d8fa8500da
commit 9626a71515
4 changed files with 97 additions and 49 deletions

View File

@@ -80,6 +80,10 @@ export class SelectionTreeType {
return this.children[id];
}
deleteChild(id: string | number) {
delete this.children[id];
}
get size(): number {
let size = this.selected ? 1 : 0;
for (let key in this.children) {

View File

@@ -127,13 +127,25 @@
}
afterUpdate(() => {
syncFileOrder();
if (sortableLevel === 'file') {
syncFileOrder();
Object.keys(elements).forEach((fileId) => {
if (!get(fileObservers).has(fileId)) {
delete elements[fileId];
}
});
} else if (sortableLevel === 'waypoints') {
if (node.wpt.length === 0) {
delete elements['waypoints'];
}
} else {
Object.keys(elements).forEach((index) => {
if ((node instanceof GPXFile || node instanceof Track) && node.children.length <= index) {
delete elements[index];
} else if (Array.isArray(node) && node.length <= index) {
delete elements[index];
}
});
}
});

View File

@@ -1,6 +1,6 @@
import { get, writable } from "svelte/store";
import { ListFileItem, ListItem, ListRootItem, ListTrackItem, ListTrackSegmentItem, ListWaypointItem, SelectionTreeType } from "./FileList";
import { fileObservers } from "$lib/db";
import { ListFileItem, ListItem, ListRootItem, ListTrackItem, ListTrackSegmentItem, ListWaypointItem, SelectionTreeType, type ListLevel } from "./FileList";
import { fileObservers, settings } from "$lib/db";
export const selection = writable<SelectionTreeType>(new SelectionTreeType(new ListRootItem()));
@@ -60,4 +60,32 @@ export function selectAll() {
return $selection;
});
}
export function applyToOrderedSelectedItemsFromFile(callback: (fileId: string, level: ListLevel | undefined, items: ListItem[]) => void) {
get(settings.fileOrder).forEach((fileId) => {
let level: ListLevel | undefined = undefined;
let items: ListItem[] = [];
get(selection).forEach((item) => {
if (item.getFileId() === fileId) {
level = item.level;
if (item instanceof ListTrackItem || item instanceof ListTrackSegmentItem || item instanceof ListWaypointItem) {
items.push(item);
}
}
});
items.sort((a, b) => { // Process the items in reverse order to avoid index conflicts
if (a instanceof ListTrackItem && b instanceof ListTrackItem) {
return b.getTrackIndex() - a.getTrackIndex();
} else if (a instanceof ListTrackSegmentItem && b instanceof ListTrackSegmentItem) {
return b.getSegmentIndex() - a.getSegmentIndex();
} else if (a instanceof ListWaypointItem && b instanceof ListWaypointItem) {
return b.getWaypointIndex() - a.getWaypointIndex();
}
return 0;
});
callback(fileId, level, items);
});
}