From d568ef26b33cdb66792a839f1dc690cd67c7b1b6 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 7 Aug 2023 22:10:14 +0200 Subject: [PATCH] don't use showSaveFilePicker and just own global ghSaveFile --- src/custom.d.ts | 2 +- src/sidebar/RoutingResults.tsx | 34 +++++++--------------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/custom.d.ts b/src/custom.d.ts index a6ed0f40..997235ed 100644 --- a/src/custom.d.ts +++ b/src/custom.d.ts @@ -5,7 +5,7 @@ declare module 'heightgraph/src/heightgraph' declare module 'custom-model-editor/src/index' interface Window { - showSaveFilePicker: ({ suggestedName: string, types: any, fileContents: xmlString }) => Promise + ghSaveFile: ({ fileName: string, mimeType: string, fileContents: xmlString }) => Promise } declare module 'config' { diff --git a/src/sidebar/RoutingResults.tsx b/src/sidebar/RoutingResults.tsx index 5ff54050..e29799f8 100644 --- a/src/sidebar/RoutingResults.tsx +++ b/src/sidebar/RoutingResults.tsx @@ -515,7 +515,7 @@ function downloadGPX(path: Path, showDistanceInMiles: boolean) { )}-${metersToTextForFile(path.distance, showDistanceInMiles)}.gpx` // window.Filesystem.writeFile({ data: xmlString, path: fileName }) - if (!window.showSaveFilePicker) { + if (!window.ghSaveFile) { const tmpElement = document.createElement('a') const file = new Blob([xmlString], { type: mimeType }) tmpElement.href = URL.createObjectURL(file) @@ -523,32 +523,12 @@ function downloadGPX(path: Path, showDistanceInMiles: boolean) { tmpElement.click() // URL.revokeObjectURL(tmpElement.href) } else { - // window.showSaveFilePicker is only supported from Chrome (and createWritable is not supported from Safari) - // Also used for CapacitorJS where it is overwritten in src/app.js - window - .showSaveFilePicker({ - suggestedName: fileName, - types: [ - { - description: 'GPX/XML Files', - accept: { [mimeType]: ['.gpx'] }, - }, - ], - fileContents: xmlString /* not part of the Chrome API, but necessary for CapacitorJS */, - }) - .then((fileHandle: any) => { - return fileHandle.createWritable() - }) - .then((writable: any) => { - const writer = writable.getWriter() - writer.write(xmlString) - writer.close() - - console.log('file saved successfully.') - }) - .catch((error: any) => { - console.error('Error saving file:', error) - }) + // method used for CapacitorJS and assigned in src/app.js + window.ghSaveFile({ + fileName: fileName, + mimeType: mimeType, + fileContents: xmlString, + }) } }