File tree Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ let intervalId = null;
14
14
class MainContainer extends Component {
15
15
constructor ( props ) {
16
16
super ( props ) ;
17
+
17
18
this . state = {
18
19
snapshots : [ ] ,
19
20
snapshotIndex : 0 ,
@@ -129,24 +130,36 @@ class MainContainer extends Component {
129
130
130
131
importSnapshots ( ) {
131
132
const { snapshots } = this . state ;
133
+
134
+ // create invisible download anchor link
132
135
const fileDownload = document . createElement ( 'a' ) ;
133
- fileDownload . style . display = 'none' ;
134
- document . body . appendChild ( fileDownload ) ;
136
+
137
+ // set file in anchor link
135
138
fileDownload . href = URL . createObjectURL (
136
139
new Blob ( [ JSON . stringify ( snapshots ) ] , { type : 'application/json' } ) ,
137
140
) ;
141
+
142
+ // set anchor as file download and click it
138
143
fileDownload . setAttribute ( 'download' , 'snapshot.json' ) ;
139
144
fileDownload . click ( ) ;
145
+
146
+ // remove file url
140
147
URL . revokeObjectURL ( fileDownload . href ) ;
141
- document . body . removeChild ( fileDownload ) ;
142
148
}
143
149
144
150
exportSnapshots ( ) {
145
151
const fileUpload = document . createElement ( 'input' ) ;
146
152
fileUpload . setAttribute ( 'type' , 'file' ) ;
147
153
154
+ fileUpload . onchange = ( event ) => {
155
+ const reader = new FileReader ( ) ;
156
+ reader . onload = ( ) => {
157
+ this . setState ( { snapshots : JSON . parse ( reader . result ) } ) ;
158
+ } ;
159
+ reader . readAsText ( event . target . files [ 0 ] ) ;
160
+ } ;
161
+
148
162
fileUpload . click ( ) ;
149
- this ;
150
163
}
151
164
152
165
toggleMode ( targetMode ) {
You can’t perform that action at this time.
0 commit comments