-
Notifications
You must be signed in to change notification settings - Fork 70
/
Dashboard.js
324 lines (308 loc) · 9.76 KB
/
Dashboard.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import React, { useState, useEffect, useRef } from 'react';
import styled from 'styled-components';
import Mock from 'mockjs';
// import { Link } from 'react-router-dom';
import BulletScreen, { StyledBullet } from 'rc-bullets';
import {
Button,
InputLabel,
TextField,
Select,
MenuItem,
Popper,
IconButton,
Paper,
Grid,
Avatar,
Tooltip,
FormControlLabel,
Checkbox
} from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { Settings, Close, Send, PlayCircleFilled, PauseCircleFilled } from '@material-ui/icons';
import BulletsScreen from './Screen';
import {
getRandomTheme,
themes,
animateFuns,
getRandomAniFun,
heads,
getRandomHead
} from '../helper';
import GithubLink from '../components/GithubLink';
const useStyles = makeStyles(theme => ({
root: {
padding: theme.spacing(3, 2),
marginBottom: theme.spacing(3)
}
}));
const StyledWrapper = styled.section`
.opts {
z-index: 998;
position: fixed;
width: 100%;
bottom: 0;
left: 0;
background: rgba(2, 2, 2, 0.2);
padding: 1rem 0;
}
`;
let mockingInter = 0;
export default function Dashboard() {
const [currScreen, setCurrScreen] = useState(null);
const [mocking, setMocking] = useState(false);
const [bullet, setBullet] = useState('');
const [theme, setTheme] = useState('random');
const [animateFun, setAnimateFun] = useState('random');
const [duration, setDuration] = useState(20);
const [loopCount, setLoopCount] = useState(3);
const [isInfinite, setIsInfinite] = useState(false);
const [img, setImg] = useState('random');
const [paramsOpen, setParamsOpen] = useState(false);
const popperAnchorEl = useRef(null);
const classes = useStyles();
const togglePopper = () => {
setParamsOpen(prev => !prev);
};
useEffect(() => {
let tmp = new BulletScreen('.screen');
setCurrScreen(tmp);
}, []);
const handleInput = ({ target: { value } }) => {
console.log(value);
setBullet(value);
};
const handleMocking = () => {
if (mocking) {
clearInterval(mockingInter);
} else {
mockingInter = setInterval(() => {
let currThemeKey = getRandomTheme();
let { color, bgColor } = themes[currThemeKey];
currScreen.push(
<StyledBullet
color={color}
bgColor={bgColor}
head={getRandomHead()}
msg={Mock.Random.csentence(3, 28)}
/>,
{
duration: Math.random() * 50
}
);
}, 500);
}
setMocking(prev => !prev);
};
const handleSend = () => {
console.log('current bullet', bullet);
if (bullet) {
console.log('start send');
let currAnimateKey = animateFun === 'random' ? getRandomAniFun() : animateFun;
let currThemeKey = theme === 'random' ? getRandomTheme() : theme;
let currImg = img === 'random' ? getRandomHead() : img;
let { color, bgColor } = themes[currThemeKey];
let obj = {
loopCount: isInfinite ? 'infinite' : loopCount,
animateTimeFun: currAnimateKey,
txt: bullet,
duration,
head: currImg,
color,
bgColor,
ts: new Date().getTime()
};
console.log({ obj });
const { txt, head, ...opts } = obj;
currScreen.push(<StyledBullet msg={txt} head={head} color={color} bgColor={bgColor} />, {
...opts
});
// currScreen.push({ msg: txt, head: newImg }, { ...opts });
// send by localStorage
let newV = JSON.stringify(obj);
console.log({ newV });
localStorage.setItem('BULLET', newV);
setBullet('');
}
};
const handleImgSelect = ({ target: { value } }) => {
console.log({ value });
setImg(value);
};
const handleThemeSelect = ({ target: { value } }) => {
console.log({ value });
setTheme(value);
};
const handleAnimateFunSelect = ({ target: { value } }) => {
console.log({ value });
setAnimateFun(value);
};
const handleDurChange = ({ target: { value } }) => {
console.log({ value });
setDuration(value);
};
const handleLoopCountChange = ({ target: { value } }) => {
console.log({ value });
setLoopCount(value);
};
const handleInfiniteChange = ({ target: { value } }) => {
console.log({ value });
setIsInfinite(prev => !prev);
};
return (
<StyledWrapper>
<GithubLink />
<BulletsScreen screen={currScreen} />
{/* <Link className="demo" target="_blank" to="/preview">
preview
</Link> */}
<div className="opts">
<Popper anchorEl={popperAnchorEl.current} open={paramsOpen} placement="top-start">
<Paper className={classes.root}>
<Grid container spacing={2}>
<Grid item xs={6}>
<InputLabel shrink id="img-label">
头像
</InputLabel>
<Select labelId="img-label" value={img} onChange={handleImgSelect}>
<MenuItem value="random">
<em>随机</em>
</MenuItem>
{heads.map(head => {
const { title, path } = head;
return (
<MenuItem key={path} value={path}>
<Avatar src={path} alt={title} />
</MenuItem>
);
})}
</Select>
</Grid>
<Grid item xs={6}>
<InputLabel shrink id="theme-label">
主题色
</InputLabel>
<Select labelId="theme-label" value={theme} onChange={handleThemeSelect}>
<MenuItem value="random">
<em>随机</em>
</MenuItem>
{Object.keys(themes).map(key => {
return (
<MenuItem key={key} value={key}>
<span
style={{
width: '100%',
padding: '.2rem',
fontSize: '.6rem',
color: themes[key].color,
background: themes[key].bgColor
}}
title={themes[key].title}
>
{themes[key].title}
</span>
</MenuItem>
);
})}
</Select>
</Grid>
</Grid>
<Grid container spacing={2}>
<Grid item xs={6}>
<InputLabel shrink id="animate-fun-label">
运动函数
</InputLabel>
<Select labelId="animate-fun" value={animateFun} onChange={handleAnimateFunSelect}>
<MenuItem value="random">
<em>随机</em>
</MenuItem>
{Object.keys(animateFuns).map(key => {
return (
<MenuItem key={key} value={key}>
{animateFuns[key].title}
</MenuItem>
);
})}
</Select>
</Grid>
<Grid item xs={6}>
<TextField
InputProps={{
inputProps: { min: 1 }
}}
label="时长/秒"
type="number"
value={duration}
onChange={handleDurChange}
/>
</Grid>
</Grid>
<Grid container spacing={2}>
<Grid item xs={6}>
<TextField
disabled={isInfinite}
InputProps={{
inputProps: { min: 1 }
}}
label="循环次数"
type="number"
value={isInfinite ? 9999 : loopCount}
onChange={handleLoopCountChange}
/>
</Grid>
<Grid item xs={6}>
<FormControlLabel
control={
<Checkbox
checked={isInfinite}
onChange={handleInfiniteChange}
value="infinite"
color="secondary"
/>
}
label="无限循环"
/>
</Grid>
</Grid>
</Paper>
</Popper>
<Grid container spacing={2} alignItems="center" justify="center">
<Grid item>
<Tooltip placement="bottom" title={mocking ? '停止模拟' : '开始模拟'} arrow>
<IconButton onClick={handleMocking}>
{mocking ? (
<PauseCircleFilled color="primary" />
) : (
<PlayCircleFilled color="primary" />
)}
</IconButton>
</Tooltip>
</Grid>
<Grid item>
<Tooltip placement="bottom" title={paramsOpen ? '关闭设置' : '设置'} arrow>
<IconButton ref={popperAnchorEl} onClick={togglePopper}>
{paramsOpen ? <Close color="primary" /> : <Settings color="primary" />}
</IconButton>
</Tooltip>
</Grid>
<Grid item xs={6}>
<TextField
value={bullet}
label="弹幕内容"
fullWidth
multiline
placeholder="请输入内容"
variant="outlined"
onChange={handleInput}
/>
</Grid>
<Grid item>
<Button endIcon={<Send />} variant="contained" color="primary" onClick={handleSend}>
发送
</Button>
</Grid>
</Grid>
</div>
</StyledWrapper>
);
}