2025-10-17 23:54:45 +02:00
|
|
|
import { currentTool, Tool } from '$lib/components/toolbar/tools';
|
2026-10-11 11:18:36 +02:00
|
|
|
import { i18n } from '$lib/i18n/i18n.svelte';
|
2025-10-17 23:54:45 +02:00
|
|
|
import { get } from 'svelte/store';
|
2026-10-11 11:18:36 +02:00
|
|
|
import { boundsManager } from '$lib/state/bounds';
|
2026-10-04 16:11:44 +02:00
|
|
|
import { engine } from '$lib/engine';
|
2026-10-11 11:18:36 +02:00
|
|
|
import { defaultFileName } from '$lib/actions/default-file-name';
|
2025-10-05 19:34:05 +02:00
|
|
|
|
2026-10-04 16:11:44 +02:00
|
|
|
/** The name of a new file: the translated default name, numbered if other files have it. */
|
|
|
|
|
export function newFileName() {
|
|
|
|
|
const names = [...get(engine.files).values()].map((file) => get(file).structure.name);
|
|
|
|
|
return defaultFileName(i18n._('menu.new_file'), names);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 19:34:05 +02:00
|
|
|
export function createFile() {
|
2026-10-04 16:11:44 +02:00
|
|
|
// the engine selects the new file
|
|
|
|
|
engine.newFile(newFileName());
|
2025-10-18 00:46:59 +02:00
|
|
|
currentTool.set(Tool.ROUTING);
|
2025-10-05 19:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function triggerFileInput() {
|
|
|
|
|
const input = document.createElement('input');
|
|
|
|
|
input.type = 'file';
|
|
|
|
|
input.accept = '.gpx';
|
|
|
|
|
input.multiple = true;
|
|
|
|
|
input.className = 'hidden';
|
|
|
|
|
input.onchange = () => {
|
|
|
|
|
if (input.files) {
|
|
|
|
|
loadFiles(input.files);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
input.click();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 16:11:44 +02:00
|
|
|
/**
|
|
|
|
|
* Loads GPX files in the engine, as a single command: one undo step, the first file that could be
|
|
|
|
|
* read is selected, and the map fits the files. A file without name is named like the file on
|
|
|
|
|
* disk. Returns the ids of the files that were added.
|
|
|
|
|
*/
|
|
|
|
|
export async function loadFiles(list: FileList | File[]): Promise<string[]> {
|
|
|
|
|
const files = await Promise.all(
|
|
|
|
|
Array.from(list).map(async (file) => ({
|
|
|
|
|
data: new Uint8Array(await file.arrayBuffer()),
|
|
|
|
|
name: file.name.split('.').slice(0, -1).join('.'),
|
|
|
|
|
}))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const before = new Set(get(engine.order));
|
|
|
|
|
await engine.loadFiles(files);
|
|
|
|
|
|
|
|
|
|
const ids = get(engine.order).filter((id) => !before.has(id));
|
|
|
|
|
if (ids.length > 0) {
|
|
|
|
|
boundsManager.fitBoundsOnLoad(ids);
|
2025-10-05 19:34:05 +02:00
|
|
|
}
|
2026-10-04 16:11:44 +02:00
|
|
|
return ids;
|
2025-10-05 19:34:05 +02:00
|
|
|
}
|