/
Table.js
189 lines (164 loc) · 4.74 KB
/
Table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, { Component, Fragment } from "react"
import { Grid } from "./Grid"
import { makeWorld, mapWorld, worldNextState } from "./modules/game-of-life"
const toggle = cell => (cell ? 0 : 1)
const alive = () => 1
const handlersWithCell = ({ x, y }, cellHandlers) =>
Object.entries(cellHandlers).reduce(
(fns, [key, fn]) => ({
...fns,
[key]: () => fn({ x, y }),
}),
{}
)
// TODO counter of generations (without repetitions)
// TODO slider (with accelerating the speed on edges) for time-traveling
class Table extends Component {
static modes = {
default: "default",
creator: "creator",
}
static defaultProps = {
rows: 8,
cols: 8,
speed: 500,
mode: "default",
}
handleCellMouseDown = cell => {
this.setState({ isDrawing: true })
this.toggleCell(cell)
this.clearInterval()
}
handleCellMouseUp = cell => {
this.setState({ isDrawing: false })
}
handleCellMouseEnter = cell => {
if (this.state.isDrawing) {
this.riseCell(cell)
}
}
state = {
world: makeWorld(this.props.rows, this.props.cols),
isDrawing: false,
cellHandlers: mapWorld(
(x, y) =>
handlersWithCell(
{ x, y },
{
onMouseDown: this.handleCellMouseDown,
onMouseUp: this.handleCellMouseUp,
onMouseEnter: this.handleCellMouseEnter,
}
),
makeWorld(this.props.rows, this.props.cols)
),
speed: this.props.speed,
}
setWorld = fn => this.setState(({ world }) => ({ world: fn(world) }))
toggleCell = ({ x, y }) =>
this.setWorld(world =>
mapWorld(
(row, col, cell) => (row === x && col === y ? toggle(cell) : cell),
world
)
)
riseCell = ({ x, y }) =>
this.setWorld(world =>
mapWorld(
(row, col, cell) => (row === x && col === y ? alive(cell) : cell),
world
)
)
nextGeneration = () => this.setWorld(worldNextState)
interval = null
clearInterval = () => {
if (this.interval) {
window.clearTimeout(this.interval)
}
}
play = () => {
this.clearInterval()
this.interval = setTimeout(() => {
window.requestAnimationFrame(this.nextGeneration)
this.play()
}, this.state.speed)
}
pause = () => this.clearInterval()
reset = () => {
const { rows, cols, speed } = this.props
this.setWorld(() => makeWorld(rows, cols))
this.clearInterval()
this.setState({ speed })
}
handleGridLeave = () => this.setState({ isDrawing: false })
speedFromValue = value => 1100 - value
valueFromSpeed = speed => 1100 - speed
handleSpeedChange = e =>
this.setState({ speed: this.speedFromValue(e.target.value) }, this.play())
copyWorld = () =>
navigator.permissions
.query({ name: "clipboard-write" })
.then(({ state }) => {
if (["prompt", "granted"].includes(state)) {
console.log("clipboard-write persmission is granted")
return
}
throw new Error("permission not granted")
})
.then(() =>
navigator.clipboard.writeText(JSON.stringify(this.state.world))
)
.then(() => console.log("Successfully wrote to the clipboard"))
.catch(err => console.log(`Failed to write to clipboard: ${err}.`))
pasteWorld = () =>
navigator.permissions
.query({ name: "clipboard-read" })
.then(({ state }) => {
if (["prompt", "granted"].includes(state)) {
console.log("clipboard-read persmission is granted")
return
}
throw new Error("permission not granted")
})
.then(() => navigator.clipboard.readText())
.then(JSON.parse)
.then(world => {
this.setState({ world })
})
.catch(err => console.log(`Failed to write to clipboard: ${err}.`))
render() {
const { world } = this.state
const { cellSize, mode } = this.props
return (
<div>
<label>
speed:{" "}
<input
type="range"
value={this.valueFromSpeed(this.state.speed)}
min={100}
max={1000}
onChange={this.handleSpeedChange}
/>
</label>
<Grid
world={world}
size={cellSize}
onMouseLeave={this.handleGridLeave}
cellHandlers={this.state.cellHandlers}
/>
{mode === "creator" && (
<Fragment>
<button onClick={this.copyWorld}>copy</button>
<button onClick={this.pasteWorld}>paste</button>
</Fragment>
)}
<button onClick={this.nextGeneration}>next generation</button>
<button onClick={this.play}>play</button>
<button onClick={this.pause}>pause</button>
<button onClick={this.reset}>reset</button>
</div>
)
}
}
export { Table }