-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event-display): complete support to save and load labels
- Loading branch information
Showing
9 changed files
with
147 additions
and
90 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Save and download file with the given data. | ||
* @param data String data to save in the file. | ||
* @param fileName Name of the downloaded file. | ||
* @param contentType Content type of the file. | ||
*/ | ||
export const saveFile = (data: string, fileName: string, contentType: string = 'application/json') => { | ||
const blob = new Blob([data], { type: contentType }); | ||
const tempAnchor = document.createElement('a'); | ||
tempAnchor.style.display = 'none'; | ||
tempAnchor.href = URL.createObjectURL(blob); | ||
tempAnchor.download = fileName; | ||
tempAnchor.click(); | ||
tempAnchor.remove(); | ||
}; | ||
|
||
/** | ||
* Load a file from user by mocking an input element. | ||
* @param onFileRead Callback when the file is read. | ||
* @param contentType Content type of the file. Default 'application/json'. | ||
*/ | ||
export const loadFile = ( | ||
onFileRead: (data: string) => void, | ||
contentType: string = 'application/json' | ||
) => { | ||
// Create a mock input file element and use that to read the file | ||
let inputFile = document.createElement('input'); | ||
inputFile.type = 'file'; | ||
inputFile.accept = contentType; | ||
inputFile.onchange = (e: any) => { | ||
const configFile = e.target?.files[0]; | ||
const reader = new FileReader(); | ||
reader.onload = e => { | ||
onFileRead?.(e.target.result.toString()); | ||
|
||
inputFile.remove(); | ||
inputFile = null; | ||
}; | ||
reader.readAsText(configFile); | ||
} | ||
inputFile.click(); | ||
}; |
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
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
Oops, something went wrong.