Skip to content

Commit

Permalink
don't use showSaveFilePicker and just own global ghSaveFile
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Aug 7, 2023
1 parent c405ed4 commit d568ef2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
ghSaveFile: ({ fileName: string, mimeType: string, fileContents: xmlString }) => Promise<any>
}

declare module 'config' {
Expand Down
34 changes: 7 additions & 27 deletions src/sidebar/RoutingResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,40 +515,20 @@ 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)
tmpElement.download = fileName
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,
})
}
}

Expand Down

0 comments on commit d568ef2

Please sign in to comment.