-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Save / Load result functionality
- Loading branch information
Showing
9 changed files
with
233 additions
and
52 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 |
---|---|---|
|
@@ -89,4 +89,4 @@ | |
"build": "yarn build:clean && yarn typescript:build", | ||
"prepublishOnly": "yarn build" | ||
} | ||
} | ||
} |
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,34 @@ | ||
import { hiddenFileAnchorId } from '../selectors'; | ||
import { RunContext } from './machine'; | ||
|
||
export function saveFile(storyName: string, current: RunContext) { | ||
const anchor = document.getElementById(hiddenFileAnchorId); | ||
|
||
if (anchor) { | ||
anchor.setAttribute( | ||
'href', | ||
'data:text/json,' + encodeURIComponent(JSON.stringify(current, null, 2)), | ||
); | ||
anchor.setAttribute('download', `${storyName}.json`); | ||
anchor.click(); | ||
} | ||
} | ||
|
||
export function readFile( | ||
e: React.FormEvent<HTMLInputElement>, | ||
callback: (context: RunContext, storyFile: string) => void, | ||
) { | ||
const reader = new FileReader(); | ||
const { files } = e.currentTarget; | ||
|
||
if (files && files.length) { | ||
reader.readAsText(files[0]); | ||
reader.onload = ({ target }) => { | ||
if (target) { | ||
const context: RunContext = JSON.parse(target.result as string); | ||
const [storyName] = files[0].name.split('.json'); | ||
callback(context, storyName); | ||
} | ||
}; | ||
} | ||
} |
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.