Skip to content

Commit 63f3d63

Browse files
committed
use filesaver
1 parent 0d03ebf commit 63f3d63

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

website/package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@sveltejs/vite-plugin-svelte": "^3.1.2",
2121
"@types/eslint": "^8.56.12",
2222
"@types/events": "^3.0.3",
23+
"@types/file-saver": "^2.0.7",
2324
"@types/mapbox__mapbox-gl-geocoder": "^5.0.0",
2425
"@types/mapbox__tilebelt": "^1.0.4",
2526
"@types/mapbox-gl": "^3.4.0",
@@ -61,6 +62,7 @@
6162
"chartjs-plugin-zoom": "^2.0.1",
6263
"clsx": "^2.1.1",
6364
"dexie": "^4.0.8",
65+
"file-saver": "^2.0.5",
6466
"gpx": "file:../gpx",
6567
"immer": "^10.1.1",
6668
"jszip": "^3.10.1",

website/src/lib/stores.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '$lib/components/file-list/FileList';
2424
import type { RoutingControls } from '$lib/components/toolbar/tools/routing/RoutingControls';
2525
import { SplitType } from '$lib/components/toolbar/tools/scissors/Scissors.svelte';
26+
import FileSaver from 'file-saver';
2627
import JSZip from 'jszip';
2728

2829
const { fileOrder } = settings;
@@ -407,28 +408,6 @@ export function updateSelectionFromKey(down: boolean, shift: boolean) {
407408
}
408409
}
409410

410-
async function exportFilesAsZip(fileIds: string[], exclude: string[]) {
411-
const zip = new JSZip();
412-
for (const fileId of fileIds) {
413-
const file = getFile(fileId);
414-
if (file) {
415-
const gpx = buildGPX(file, exclude);
416-
zip.file(file.metadata.name + '.gpx', gpx);
417-
}
418-
}
419-
if (Object.keys(zip.files).length > 0) {
420-
const content = await zip.generateAsync({ type: 'blob' });
421-
const link = document.createElement('a');
422-
link.href = URL.createObjectURL(content);
423-
link.download = 'gpx-export.zip';
424-
link.style.display = 'none';
425-
document.body.appendChild(link);
426-
link.click();
427-
document.body.removeChild(link);
428-
URL.revokeObjectURL(link.href);
429-
}
430-
}
431-
432411
async function exportFiles(fileIds: string[], exclude: string[]) {
433412
if (fileIds.length > 1) {
434413
await exportFilesAsZip(fileIds, exclude)
@@ -455,14 +434,24 @@ export async function exportAllFiles(exclude: string[]) {
455434
await exportFiles(get(fileOrder), exclude);
456435
}
457436

458-
export function exportFile(file: GPXFile, exclude: string[]) {
459-
let blob = new Blob([buildGPX(file, exclude)], { type: 'application/gpx+xml' });
460-
let url = URL.createObjectURL(blob);
461-
let a = document.createElement('a');
462-
a.href = url;
463-
a.download = file.metadata.name + '.gpx';
464-
a.click();
465-
URL.revokeObjectURL(url);
437+
function exportFile(file: GPXFile, exclude: string[]) {
438+
const blob = new Blob([buildGPX(file, exclude)], { type: 'application/gpx+xml' });
439+
FileSaver.saveAs(blob, `${file.metadata.name}.gpx`);
440+
}
441+
442+
async function exportFilesAsZip(fileIds: string[], exclude: string[]) {
443+
const zip = new JSZip();
444+
for (const fileId of fileIds) {
445+
const file = getFile(fileId);
446+
if (file) {
447+
const gpx = buildGPX(file, exclude);
448+
zip.file(file.metadata.name + '.gpx', gpx);
449+
}
450+
}
451+
if (Object.keys(zip.files).length > 0) {
452+
const blob = await zip.generateAsync({ type: 'blob' });
453+
FileSaver.saveAs(blob, 'gpx-files.zip');
454+
}
466455
}
467456

468457
export const allHidden = writable(false);

0 commit comments

Comments
 (0)