-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:NervJS/taro
- Loading branch information
Showing
23 changed files
with
1,299 additions
and
144 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
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
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
171 changes: 171 additions & 0 deletions
171
packages/taro-components/src/components/video/controls.js
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,171 @@ | ||
import Nerv, { Component } from 'nervjs' | ||
import classnames from 'classnames' | ||
import { formatTime, calcDist } from './utils' | ||
|
||
/** | ||
* @typedef {Object} ControlsProps | ||
* @property {Boolean} controls={controls} | ||
* @property {Number} currentTime={this.currentTime} | ||
* @property {Number} duration={this.state.duration} | ||
* @property {Boolean} isPlaying={this.state.isPlaying} | ||
* @property {Function} pauseFunc={this.pause} | ||
* @property {Function} playFunc={this.play} | ||
* @property {Function} seekFunc={this.seek} | ||
* @property {Boolean} showPlayBtn={showPlayBtn} | ||
* @property {Boolean} showProgress={showProgress} | ||
*/ | ||
class Controls extends Component { | ||
visible = false | ||
isDraggingProgressBall = false | ||
|
||
/** @type {number} */ | ||
hideControlsTimer | ||
|
||
/** @type {ControlsProps} */ | ||
props | ||
|
||
progressDimentions = { | ||
left: 0, | ||
right: 0, | ||
width: 0 | ||
} | ||
|
||
calcPercentage = pageX => { | ||
let pos = pageX - this.progressDimentions.left | ||
pos = Math.max(pos, 0) | ||
pos = Math.min(pos, this.progressDimentions.width) | ||
return pos / this.progressDimentions.width | ||
} | ||
|
||
getControlsRef = ref => { | ||
if (!ref) return | ||
this.controlsRef = ref | ||
} | ||
getCurrentTimeRef = ref => { | ||
if (!ref) return | ||
this.currentTimeRef = ref | ||
} | ||
getProgressBallRef = ref => { | ||
if (!ref) return | ||
this.progressBallRef = ref | ||
} | ||
|
||
setCurrentTime (time) { | ||
this.currentTimeRef.innerHTML = formatTime(time) | ||
} | ||
setProgressBall (percentage) { | ||
this.progressBallRef.style.left = `${percentage * 100}%` | ||
} | ||
|
||
toggleVisible (nextVisible) { | ||
const visible = nextVisible === undefined ? !this.visible : nextVisible | ||
if (visible) { | ||
this.hideControlsTimer && clearTimeout(this.hideControlsTimer) | ||
if (this.props.isPlaying) { | ||
this.hideControlsTimer = setTimeout(() => { | ||
this.toggleVisible(false) | ||
}, 2000) | ||
} | ||
this.controlsRef.style.visibility = 'visible' | ||
} else { | ||
this.controlsRef.style.visibility = 'hidden' | ||
} | ||
this.visible = !!visible | ||
} | ||
|
||
onDragProgressBallStart = () => { | ||
this.isDraggingProgressBall = true | ||
this.hideControlsTimer && clearTimeout(this.hideControlsTimer) | ||
} | ||
onClickProgress = e => { | ||
e.stopPropagation() | ||
const seekFunc = this.props.seekFunc | ||
const percentage = this.calcPercentage(e.pageX) | ||
seekFunc(percentage * this.props.duration) | ||
this.toggleVisible(true) | ||
} | ||
bindTouchEvents = () => { | ||
let percentage = 0 | ||
const touchMove = e => { | ||
if (!this.isDraggingProgressBall) return | ||
const touchX = e.touches[0].pageX | ||
percentage = this.calcPercentage(touchX) | ||
this.setProgressBall(percentage) | ||
} | ||
const touchEnd = e => { | ||
if (!this.isDraggingProgressBall) return | ||
const seekFunc = this.props.seekFunc | ||
this.isDraggingProgressBall = false | ||
seekFunc(percentage * this.props.duration) | ||
this.toggleVisible(true) | ||
} | ||
|
||
document.body.addEventListener('touchmove', touchMove) | ||
document.body.addEventListener('touchend', touchEnd) | ||
document.body.addEventListener('touchcancel', touchEnd) | ||
return () => { | ||
document.body.removeEventListener('touchmove', touchMove) | ||
document.body.removeEventListener('touchend', touchEnd) | ||
document.body.removeEventListener('touchcancel', touchEnd) | ||
} | ||
} | ||
|
||
componentDidMount () { | ||
this.unbindTouchEvents = this.bindTouchEvents() | ||
} | ||
componentWillUnmount () { | ||
this.unbindTouchEvents() | ||
} | ||
|
||
render () { | ||
const { controls, currentTime, duration, isPlaying, pauseFunc, playFunc, showPlayBtn, showProgress } = this.props | ||
const formattedDuration = formatTime(duration) | ||
let playBtn | ||
|
||
if (!showPlayBtn) { | ||
return null | ||
} else if (isPlaying) { | ||
playBtn = <div className='taro-video-control-button taro-video-control-button-pause' onClick={pauseFunc} /> | ||
} else { | ||
playBtn = <div className='taro-video-control-button taro-video-control-button-play' onClick={playFunc} /> | ||
} | ||
|
||
return ( | ||
<div className='taro-video-bar taro-video-bar-full' ref={this.getControlsRef}> | ||
{controls && ( | ||
<div className='taro-video-controls'> | ||
{playBtn} | ||
{showProgress && ( | ||
<div className='taro-video-current-time' ref={this.getCurrentTimeRef}> | ||
{formatTime(currentTime)} | ||
</div> | ||
)} | ||
{showProgress && ( | ||
<div className='taro-video-progress-container' onClick={this.onClickProgress}> | ||
<div | ||
className='taro-video-progress' | ||
ref={ref => { | ||
if (ref !== null) { | ||
const rect = ref.getBoundingClientRect() | ||
this.progressDimentions.left = rect.left | ||
this.progressDimentions.right = rect.right | ||
this.progressDimentions.width = rect.width | ||
} | ||
}}> | ||
<div className='taro-video-progress-buffered' style='width: 100%;' /> | ||
<div className='taro-video-ball' ref={this.getProgressBallRef} onTouchStart={this.onDragProgressBallStart} style={`left: ${formattedDuration ? (this.currentTime / duration) * 100 : 0}%`}> | ||
<div className='taro-video-inner' /> | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
{showProgress && <div className='taro-video-duration'>{formattedDuration}</div>} | ||
</div> | ||
)} | ||
{this.props.children} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default Controls |
Oops, something went wrong.