forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (168 loc) · 5.67 KB
/
index.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
import React from 'react';
import { connect } from 'react-redux';
import InputRange from 'react-input-range';
import axios from 'axios';
import faPlay from '@fortawesome/fontawesome-free-solid/faPlay';
import faChevronLeft from '@fortawesome/fontawesome-free-solid/faChevronLeft';
import faChevronRight from '@fortawesome/fontawesome-free-solid/faChevronRight';
import faPause from '@fortawesome/fontawesome-free-solid/faPause';
import faWrench from '@fortawesome/fontawesome-free-solid/faWrench';
import { classes, extension } from 'common/util';
import { TracerApi } from 'apis';
import { actions } from 'reducers';
import { BaseComponent, Button, ProgressBar } from 'components';
import styles from './Player.module.scss';
class Player extends BaseComponent {
constructor(props) {
super(props);
this.state = {
speed: 2,
playing: false,
building: false,
};
this.tracerApiSource = null;
this.reset();
}
componentDidMount() {
const { editingFile, shouldBuild } = this.props.current;
if (shouldBuild) this.build(editingFile);
}
componentWillReceiveProps(nextProps) {
const { editingFile, shouldBuild } = nextProps.current;
if (editingFile !== this.props.current.editingFile) {
if (shouldBuild) this.build(editingFile);
}
}
reset(commands = []) {
const chunks = [{
commands: [],
lineNumber: undefined,
}];
while (commands.length) {
const command = commands.shift();
const { key, method, args } = command;
if (key === null && method === 'delay') {
const [lineNumber] = args;
chunks[chunks.length - 1].lineNumber = lineNumber;
chunks.push({
commands: [],
lineNumber: undefined,
});
} else {
chunks[chunks.length - 1].commands.push(command);
}
}
this.props.setChunks(chunks);
this.props.setCursor(0);
this.pause();
this.props.setLineIndicator(undefined);
}
build(file) {
this.reset();
if (!file) return;
if (this.tracerApiSource) this.tracerApiSource.cancel();
this.tracerApiSource = axios.CancelToken.source();
this.setState({ building: true });
const ext = extension(file.name);
if (ext in TracerApi) {
TracerApi[ext]({ code: file.content }, undefined, this.tracerApiSource.token)
.then(commands => {
this.tracerApiSource = null;
this.setState({ building: false });
this.reset(commands);
this.next();
})
.catch(error => {
if (axios.isCancel(error)) return;
this.tracerApiSource = null;
this.setState({ building: false });
this.handleError(error);
});
} else {
this.setState({ building: false });
this.handleError(new Error('Language Not Supported'));
}
}
isValidCursor(cursor) {
const { chunks } = this.props.player;
return 1 <= cursor && cursor <= chunks.length;
}
prev() {
this.pause();
const cursor = this.props.player.cursor - 1;
if (!this.isValidCursor(cursor)) return false;
this.props.setCursor(cursor);
return true;
}
resume(wrap = false) {
this.pause();
if (this.next() || (wrap && this.props.setCursor(1))) {
const interval = 4000 / Math.pow(Math.E, this.state.speed);
this.timer = window.setTimeout(() => this.resume(), interval);
this.setState({ playing: true });
}
}
pause() {
if (this.timer) {
window.clearTimeout(this.timer);
this.timer = undefined;
this.setState({ playing: false });
}
}
next() {
this.pause();
const cursor = this.props.player.cursor + 1;
if (!this.isValidCursor(cursor)) return false;
this.props.setCursor(cursor);
return true;
}
handleChangeSpeed(speed) {
this.setState({ speed });
}
handleChangeProgress(progress) {
const { chunks } = this.props.player;
const cursor = Math.max(1, Math.min(chunks.length, Math.round(progress * chunks.length)));
this.pause();
this.props.setCursor(cursor);
}
render() {
const { className } = this.props;
const { editingFile } = this.props.current;
const { chunks, cursor } = this.props.player;
const { speed, playing, building } = this.state;
return (
<div className={classes(styles.player, className)}>
<Button icon={faWrench} primary disabled={building} inProgress={building}
onClick={() => this.build(editingFile)}>
{building ? 'Building' : 'Build'}
</Button>
{
playing ? (
<Button icon={faPause} primary active onClick={() => this.pause()}>Pause</Button>
) : (
<Button icon={faPlay} primary onClick={() => this.resume(true)}>Play</Button>
)
}
<Button icon={faChevronLeft} primary disabled={!this.isValidCursor(cursor - 1)} onClick={() => this.prev()}/>
<ProgressBar className={styles.progress_bar} current={cursor} total={chunks.length}
onChangeProgress={progress => this.handleChangeProgress(progress)}/>
<Button icon={faChevronRight} reverse primary disabled={!this.isValidCursor(cursor + 1)}
onClick={() => this.next()}/>
<div className={styles.speed}>
Speed
<InputRange
classNames={{
inputRange: styles.range,
labelContainer: styles.range_label_container,
slider: styles.range_slider,
track: styles.range_track,
}} minValue={0} maxValue={4} step={.5} value={speed}
onChange={speed => this.handleChangeSpeed(speed)}/>
</div>
</div>
);
}
}
export default connect(({ current, player }) => ({ current, player }), actions)(
Player,
);