-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add import/export functionality (#65)
- Loading branch information
Showing
5 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,6 @@ logs | |
*.map | ||
index.js | ||
index.d.ts | ||
file.js | ||
file.d.ts | ||
!*/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const filePickerOptions: FilePickerOptions = { | ||
types: [ | ||
{ | ||
accept: { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'application/json': '.json', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const isModern = typeof showOpenFilePicker === 'function'; | ||
|
||
async function loadFileOld(): Promise<string> { | ||
const input = document.createElement('input'); | ||
input.type = 'file'; | ||
input.accept = '.json'; | ||
const eventPromise = new Promise<Event>(resolve => { | ||
input.addEventListener('change', resolve, {once: true}); | ||
}); | ||
|
||
input.click(); | ||
const event = await eventPromise; | ||
const file = (event.target as HTMLInputElement).files![0]; | ||
if (!file) { | ||
throw new Error('No file selected'); | ||
} | ||
|
||
return file.text(); | ||
} | ||
|
||
async function saveFileOld(text: string, suggestedName: string): Promise<void> { | ||
// Use data URL because Safari doesn't support saving blob URLs | ||
// Use base64 or else linebreaks are lost | ||
const url = `data:application/json;base64,${btoa(text)}`; | ||
const link = document.createElement('a'); | ||
link.download = suggestedName; | ||
link.href = url; | ||
link.click(); | ||
} | ||
|
||
async function loadFileModern(): Promise<string> { | ||
const [fileHandle] = await showOpenFilePicker(filePickerOptions); | ||
const file = await fileHandle.getFile(); | ||
return file.text(); | ||
} | ||
|
||
async function saveFileModern(text: string, suggestedName: string) { | ||
const fileHandle = await showSaveFilePicker({ | ||
...filePickerOptions, | ||
suggestedName, | ||
}); | ||
|
||
const writable = await fileHandle.createWritable(); | ||
await writable.write(text); | ||
await writable.close(); | ||
} | ||
|
||
export const loadFile = isModern ? loadFileModern : loadFileOld; | ||
export const saveFile = isModern ? saveFileModern : saveFileOld; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters