-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.js
151 lines (141 loc) · 4.26 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
import React, { useState, useEffect, useCallback, lazy, Suspense } from 'react';
import styled from 'styled-components';
import Mock from 'mockjs';
// import { Link } from 'react-router-dom';
import BulletScreen, { StyledBullet } from 'rc-bullets';
import BulletsScreen from '../Screen';
import ParamsPanel from './ParamsPanel';
import audioStart from '../../assets/start.mp3';
import audioEnd from '../../assets/end.mp3';
import { getRandomTheme, getRandomHead, getRandomAniFun } from '../../helper';
import Loading from '../../components/Loading';
import useParams from './useParams';
const GithubLink = lazy(() => import('../../components/GithubLink'));
const OptsArea = lazy(() => import('./OptsArea'));
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;
}
`;
const startSoundEle = document.createElement('audio');
startSoundEle.src = audioStart;
const endSoundEle = document.createElement('audio');
endSoundEle.src = audioEnd;
let mockingInter = 0;
let currScreen = null;
export default function Dashboard() {
const [bullet, setBullet] = useState('');
const { params, states, toggleStates, handleChange } = useParams();
const { mocking, isInfinite, open, soundEffect } = states;
const { theme, loopCount, head, duration, animateFun } = params;
const handleStart = useCallback(() => {
if (soundEffect) {
startSoundEle.play();
}
}, [soundEffect]);
const handleEnd = useCallback(() => {
if (soundEffect) {
endSoundEle.play();
}
}, [soundEffect]);
useEffect(() => {
if (!currScreen) {
currScreen = new BulletScreen('.screen');
currScreen.push(
<StyledBullet
size="huge"
head="assets/img/heads/girl.jpg"
msg="欢迎体验rc-bullets弹幕功能~~"
/>,
{ duration: 40, top: '45%' }
);
}
}, []);
const handleInput = ({ target: { value } }) => {
console.log(value);
setBullet(value);
};
const toggleSendMocking = () => {
console.log('handle mocking start', mocking);
if (!mocking) {
mockingInter = setInterval(() => {
const randomTxt = Mock.Random.csentence(10, 12);
handleSend(randomTxt, {
// duration: Math.random() * 50
// onStart: null,
// onEnd: null
});
}, 500);
} else {
clearInterval(mockingInter);
}
toggleStates('mocking')();
};
const handleSend = (mssg = '', opts = {}) => {
console.log('current bullet', bullet, mssg);
if (bullet || mssg) {
console.log('start send');
let currHead = head === 'random' ? getRandomHead() : head;
let currAnimteFun = animateFun === 'random' ? getRandomAniFun() : animateFun;
let bgColor = theme === 'random' ? getRandomTheme() : theme;
let newOpts = Object.assign(
{
onStart: handleStart,
onEnd: handleEnd,
loopCount: isInfinite ? 'infinite' : loopCount,
animateTimeFun: currAnimteFun,
duration
},
opts
);
console.log({ opts });
// currScreen.push(bullet || mssg, newOpts);
currScreen.push(
<StyledBullet msg={bullet || mssg} head={currHead} backgroundColor={bgColor} />,
newOpts
);
console.log({ mssg });
if (!mssg) {
setBullet('');
}
}
};
return (
<>
<ParamsPanel
{...params}
isInfinite={isInfinite}
soundEffect={soundEffect}
handleChange={handleChange}
toggleStates={toggleStates}
open={open}
/>
<StyledWrapper>
<Suspense fallback={<Loading />}>
<GithubLink />
</Suspense>
<BulletsScreen screen={currScreen} />
<Suspense fallback={<Loading />}>
<div className="opts">
<Suspense fallback={<Loading />}>
<OptsArea
bullet={bullet}
mocking={mocking}
toggleStates={toggleStates}
handleMocking={toggleSendMocking}
handleInput={handleInput}
handleSend={handleSend.bind(this, '', {})}
/>
</Suspense>
</div>
</Suspense>
</StyledWrapper>
</>
);
}