Browse Source

Add import and export

master
Dylan Baker 3 years ago
parent
commit
01c0f02e74
3 changed files with 51 additions and 3 deletions
  1. 32
    0
      src/Board.tsx
  2. 16
    0
      src/Controls.tsx
  3. 3
    3
      src/index.css

+ 32
- 0
src/Board.tsx View File

@@ -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">

+ 16
- 0
src/Controls.tsx View File

@@ -7,9 +7,11 @@ interface Props {
7 7
   height: number;
8 8
   reset: () => void;
9 9
   toggle: () => void;
10
+  download: () => void;
10 11
   updateCellSize: (f: (size: number) => number) => void;
11 12
   updateDimensions: (dimensions: { width?: number; height?: number }) => void;
12 13
   updateSpeed: (speed: number) => void;
14
+  upload: () => void;
13 15
 }
14 16
 
15 17
 const Controls = (props: Props) => {
@@ -23,6 +25,8 @@ const Controls = (props: Props) => {
23 25
     reset,
24 26
     updateCellSize,
25 27
     updateDimensions,
28
+    download,
29
+    upload,
26 30
   } = props;
27 31
 
28 32
   const goSlower = () => {
@@ -93,6 +97,18 @@ const Controls = (props: Props) => {
93 97
           Bigger
94 98
         </button>
95 99
       </div>
100
+      <div className="controls__section import-export">
101
+        <div className="upload">
102
+          <button className="button" onClick={upload}>
103
+            Import
104
+          </button>
105
+        </div>
106
+        <div className="download">
107
+          <button className="button" onClick={download}>
108
+            Export
109
+          </button>
110
+        </div>
111
+      </div>
96 112
       <div className="controls__section play-reset">
97 113
         <div className="reset">
98 114
           <button className="button" onClick={reset}>

+ 3
- 3
src/index.css View File

@@ -75,7 +75,7 @@ code {
75 75
   border: 1px solid white;
76 76
   color: white;
77 77
   cursor: pointer;
78
-  font-size: 18px;
78
+  font-size: 16px;
79 79
   margin-right: 1em;
80 80
   padding: 0.25em 0.5em;
81 81
 }
@@ -88,6 +88,8 @@ code {
88 88
 .controls__section {
89 89
   align-items: center;
90 90
   display: flex;
91
+  flex: 1;
92
+  justify-content: center;
91 93
   margin: 0.5em 0;
92 94
 }
93 95
 
@@ -95,7 +97,6 @@ code {
95 97
 .speed {
96 98
   align-items: center;
97 99
   display: flex;
98
-  flex: 1;
99 100
   justify-content: center;
100 101
 }
101 102
 
@@ -139,6 +140,5 @@ code {
139 140
 }
140 141
 
141 142
 .play-reset {
142
-  flex: 1;
143 143
   justify-content: flex-end;
144 144
 }

Loading…
Cancel
Save