-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add PlayMode, PlayVolume and play logic
- Loading branch information
Showing
14 changed files
with
368 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react' | ||
import { Tooltip, Icon, IconName } from '@blueprintjs/core' | ||
|
||
import { getPlayMode, MODE } from 'helpers/play' | ||
import { PlayMusicDispatchContext, ACTIONS } from 'reducers/playMusic' | ||
|
||
const MODE_ORDER = [MODE.PLAY_IN_ORDER, MODE.SINGLE_CYCLE, MODE.SHUFFLE_PLAYBACK] | ||
|
||
const MODE_MAP: IDictionary<{ | ||
label: string, | ||
icon: IconName | ||
}> = { | ||
[MODE.PLAY_IN_ORDER]: { | ||
label: '顺序播放', | ||
icon: 'sort' | ||
}, | ||
[MODE.SINGLE_CYCLE]: { | ||
label: '单曲循环', | ||
icon: 'repeat' | ||
}, | ||
[MODE.SHUFFLE_PLAYBACK]: { | ||
label: '随机播放', | ||
icon: 'random' | ||
} | ||
} | ||
|
||
const { useState, useContext } = React | ||
|
||
const PlayMode = () => { | ||
const dispatch = useContext(PlayMusicDispatchContext) | ||
const [mode, setMode] = useState(getPlayMode()) | ||
|
||
const handleClick = () => { | ||
const idx = MODE_ORDER.findIndex(m => m === mode) | ||
const nextMode = MODE_ORDER[(idx + 1) % (MODE_ORDER.length)] | ||
setMode(nextMode) | ||
|
||
dispatch({ | ||
type: ACTIONS.SET_PLAY_MODE, | ||
payload: { | ||
playMode: nextMode | ||
} | ||
}) | ||
} | ||
|
||
return ( | ||
<Tooltip content={MODE_MAP[mode].label}> | ||
<Icon icon={MODE_MAP[mode].icon} onClick={handleClick} /> | ||
</Tooltip> | ||
) | ||
} | ||
|
||
export default PlayMode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react' | ||
import { Icon } from '@blueprintjs/core' | ||
|
||
import { PlayMusicStateContext, PlayMusicDispatchContext, AudioContext, ACTIONS } from 'reducers/playMusic' | ||
import styles from './style.module.css' | ||
|
||
const { useContext } = React | ||
|
||
const PlayOperations = () => { | ||
const audioInfo = useContext(AudioContext) | ||
const state = useContext(PlayMusicStateContext) | ||
const dispatch = useContext(PlayMusicDispatchContext) | ||
const { musicId, playList } = state | ||
|
||
const togglePlayStatus = () => { | ||
if (audioInfo.state?.paused) { | ||
audioInfo.controls?.play() | ||
} else { | ||
audioInfo.controls?.pause() | ||
} | ||
} | ||
|
||
const play = (prev?: boolean) => { | ||
const len = playList.length | ||
if (!len) { | ||
return | ||
} | ||
|
||
const index = playList.findIndex(({ id }) => id === musicId) | ||
let nextIndex = -1 | ||
|
||
if (index > -1) { | ||
nextIndex = prev ? (index - 1 + len) % len : (index + 1) % len | ||
} else { | ||
nextIndex = 0 | ||
} | ||
|
||
dispatch({ | ||
type: ACTIONS.PLAY, | ||
payload: { | ||
musicId: playList[nextIndex].id, | ||
music: playList[nextIndex] | ||
} | ||
}) | ||
} | ||
|
||
const playPrev = () => play(true) | ||
const playNext = () => play() | ||
|
||
return ( | ||
<> | ||
<div className={styles.prev} onClick={playPrev}> | ||
<Icon icon='step-backward' /> | ||
</div> | ||
<div className={styles.pause} onClick={togglePlayStatus}> | ||
<Icon icon={audioInfo.state?.paused ? 'play' : 'pause'} /> | ||
</div> | ||
<div className={styles.next} onClick={playNext}> | ||
<Icon icon='step-forward' /> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default PlayOperations |
28 changes: 28 additions & 0 deletions
28
src/components/Layout/Footer/PlayOperations/style.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@value colors: "~styles/colors.module.css"; | ||
@value red from colors; | ||
|
||
.prev, .next, .pause { | ||
cursor: pointer; | ||
} | ||
|
||
.prev svg, | ||
.next svg { | ||
width: 20px; | ||
height: 20px; | ||
color: red; | ||
} | ||
|
||
.pause { | ||
width: 40px; | ||
height: 40px; | ||
border-radius: 50%; | ||
background-color: red; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
margin: 0 20px; | ||
|
||
& svg { | ||
color: #fff; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react' | ||
import { Icon } from '@blueprintjs/core' | ||
|
||
import ProgressBar from 'components/ProgressBar' | ||
import { AudioContext } from 'reducers/playMusic' | ||
import styles from './style.module.css' | ||
|
||
const { useContext } = React | ||
|
||
const PlayVolume = () => { | ||
const audioInfo = useContext(AudioContext) | ||
|
||
const handleBarClick = (percent: number) => { | ||
audioInfo.controls?.volume(percent) | ||
} | ||
|
||
const donePercent = Number((audioInfo.state?.volume || 0).toFixed(2)) | ||
|
||
return ( | ||
<div className={styles.root}> | ||
<Icon icon='volume-off' /> | ||
<div className={styles.progress}> | ||
<ProgressBar | ||
className={styles.bar} | ||
donePercent={donePercent} | ||
onBarClick={handleBarClick} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default PlayVolume |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@value colors: "~styles/colors.module.css"; | ||
@value tipsColor from colors; | ||
|
||
.root { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
} | ||
|
||
.progress { | ||
width: 130px; | ||
margin-left: 3px; | ||
border-bottom: 1px solid tipsColor; | ||
position: relative; | ||
} | ||
|
||
.bar { | ||
position: absolute; | ||
top: -1px; | ||
left: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.