|
|
@@ -102,6 +102,36 @@ class Board extends React.Component<{}, State> {
|
|
102
|
102
|
this.setState({ grid });
|
|
103
|
103
|
};
|
|
104
|
104
|
|
|
|
105
|
+ download = () => {
|
|
|
106
|
+ const state = JSON.stringify(this.state);
|
|
|
107
|
+ const element = document.createElement('a');
|
|
|
108
|
+ const file = new Blob([state], { type: 'text/plain' });
|
|
|
109
|
+ element.href = URL.createObjectURL(file);
|
|
|
110
|
+ element.download = 'export.json';
|
|
|
111
|
+ document.body.appendChild(element);
|
|
|
112
|
+ element.click();
|
|
|
113
|
+ };
|
|
|
114
|
+
|
|
|
115
|
+ upload = () => {
|
|
|
116
|
+ if (
|
|
|
117
|
+ window.confirm('This will replace any changes you have made. Continue?')
|
|
|
118
|
+ ) {
|
|
|
119
|
+ const element = document.createElement('input');
|
|
|
120
|
+ element.type = 'file';
|
|
|
121
|
+ element.click();
|
|
|
122
|
+ element.addEventListener('change', (e) => {
|
|
|
123
|
+ const files = (e.currentTarget as HTMLInputElement).files;
|
|
|
124
|
+ if (files && files.length > 0) {
|
|
|
125
|
+ const file = files[0];
|
|
|
126
|
+ file.text().then((text) => {
|
|
|
127
|
+ const newState = JSON.parse(text);
|
|
|
128
|
+ this.setState(newState);
|
|
|
129
|
+ });
|
|
|
130
|
+ }
|
|
|
131
|
+ });
|
|
|
132
|
+ }
|
|
|
133
|
+ };
|
|
|
134
|
+
|
|
105
|
135
|
render() {
|
|
106
|
136
|
const { grid, running, speed, width, height, cellSize } = this.state;
|
|
107
|
137
|
|
|
|
@@ -117,6 +147,8 @@ class Board extends React.Component<{}, State> {
|
|
117
|
147
|
updateDimensions={this.updateDimensions}
|
|
118
|
148
|
updateSpeed={this.updateSpeed}
|
|
119
|
149
|
reset={this.reset}
|
|
|
150
|
+ download={this.download}
|
|
|
151
|
+ upload={this.upload}
|
|
120
|
152
|
/>
|
|
121
|
153
|
<div className="board-container">
|
|
122
|
154
|
<div className="board">
|