This commit is contained in:
Harvey Zack committed 2018-10-21 14:20:35 +08:00
1 parent 1b3b2de8d4
commit 0c6a297ee7
12 files changed
+374 -308

No files matched your search

+18 -3
View File
@@ -1,3 +1,4 @@
import { secondToTime, clamp } from '../utils';
import validControl from '../verification/control';
import Danmu from './danmu';
import Fullscreen from './fullscreen';
@@ -8,6 +9,7 @@ import Subtitle from './subtitle';
import Time from './time';
import Volume from './volume';
import Setting from './setting';
import Thumbnails from './thumbnails';
let id = 0;
export default class Controls {
@@ -30,6 +32,13 @@ export default class Controls {
index: 10
});
this.add({
control: Thumbnails,
disable: !this.art.option.thumbnails.url,
position: 'top',
index: 20
});
this.add({
control: PlayAndPause,
disable: false,
@@ -155,9 +164,15 @@ export default class Controls {
}
});
Object.defineProperty(option.control.prototype, 'addMenu', {
value: menus => {
console.log(menus);
Object.defineProperty(option.control.prototype, 'getPosFromEvent', {
value: event => {
const { $video, $progress } = this.art.refs;
const { left } = $progress.getBoundingClientRect();
const width = clamp(event.x - left, 0, $progress.clientWidth);
const second = width / $progress.clientWidth * $video.duration;
const time = secondToTime(second);
const percentage = clamp(width / $progress.clientWidth, 0, 1);
return { second, time, width, percentage };
}
});
}
+12 -53
View File
@@ -1,4 +1,4 @@
import { append, secondToTime, clamp } from '../utils';
import { append } from '../utils';
export default class Progress {
constructor(art, option) {
@@ -7,7 +7,6 @@ export default class Progress {
this.isDroging = false;
this.getLoaded = this.getLoaded.bind(this);
this.getPlayed = this.getPlayed.bind(this);
this.getPos = this.getPos.bind(this);
this.set = this.set.bind(this);
this.init();
}
@@ -23,21 +22,19 @@ export default class Progress {
append(
this.option.ref,
`
<div class="art-control-progress-inner">
<div class="art-progress-loaded"></div>
<div class="art-progress-played" style="background: ${theme}"></div>
<div class="art-progress-highlight"></div>
<div class="art-progress-thumbnails"></div>
<div class="art-progress-indicator" style="background: ${theme}"></div>
<div class="art-progress-tip art-tip"></div>
</div>
`
<div class="art-control-progress-inner">
<div class="art-progress-loaded"></div>
<div class="art-progress-played" style="background: ${theme}"></div>
<div class="art-progress-highlight"></div>
<div class="art-progress-indicator" style="background: ${theme}"></div>
<div class="art-progress-tip art-tip"></div>
</div>
`
);
this.$loaded = this.option.ref.querySelector('.art-progress-loaded');
this.$played = this.option.ref.querySelector('.art-progress-played');
this.$highlight = this.option.ref.querySelector('.art-progress-highlight');
this.$thumbnails = this.option.ref.querySelector('.art-progress-thumbnails');
this.$indicator = this.option.ref.querySelector('.art-progress-indicator');
this.$tip = this.option.ref.querySelector('.art-progress-tip');
@@ -71,23 +68,15 @@ export default class Progress {
} else {
this.showTime(event);
}
if (this.art.option.thumbnails.url) {
this.$thumbnails.style.display = 'block';
this.showThumbnails(event);
}
});
proxy(this.option.ref, 'mouseout', () => {
this.$tip.style.display = 'none';
if (this.art.option.thumbnails.url) {
this.$thumbnails.style.display = 'none';
}
});
proxy(this.option.ref, 'click', event => {
if (event.target !== this.$indicator) {
const { second, percentage } = this.getPos(event);
const { second, percentage } = this.getPosFromEvent(event);
this.set('played', percentage);
player.seek(second);
}
@@ -99,7 +88,7 @@ export default class Progress {
proxy(document, 'mousemove', event => {
if (this.isDroging) {
const { second, percentage } = this.getPos(event);
const { second, percentage } = this.getPosFromEvent(event);
this.$indicator.classList.add('show-indicator');
this.set('played', percentage);
player.seek(second);
@@ -126,7 +115,7 @@ export default class Progress {
}
showTime(event) {
const { width, time } = this.getPos(event);
const { width, time } = this.getPosFromEvent(event);
const tipWidth = this.$tip.clientWidth;
this.$tip.innerHTML = time;
if (width <= tipWidth / 2) {
@@ -138,36 +127,6 @@ export default class Progress {
}
}
showThumbnails(event) {
const { width: posWidth } = this.getPos(event);
const { url, height, width, number } = this.art.option.thumbnails;
this.$thumbnails.style.backgroundImage = `url(${url})`;
this.$thumbnails.style.height = `${height}px`;
this.$thumbnails.style.width = `${width}px`;
if (posWidth <= width / 2) {
this.$thumbnails.style.left = 0;
} else if (posWidth > this.option.ref.clientWidth - width / 2) {
this.$thumbnails.style.left = `${this.option.ref.clientWidth - width}px`;
} else {
this.$thumbnails.style.left = `${posWidth - width / 2}px`;
}
const perWidth = this.option.ref.clientWidth / number;
const index = Math.ceil(posWidth / perWidth);
this.$thumbnails.style.backgroundPosition = `-${index * width}px 0`;
}
getPos(event) {
const { $video } = this.art.refs;
const { left } = this.option.ref.getBoundingClientRect();
const width = clamp(event.x - left, 0, this.option.ref.clientWidth);
const second = width / this.option.ref.clientWidth * $video.duration;
const time = secondToTime(second);
const percentage = clamp(width / this.option.ref.clientWidth, 0, 1);
return { second, time, width, percentage };
}
getPlayed() {
const { $video } = this.art.refs;
return $video.currentTime / $video.duration;
+43
View File
@@ -0,0 +1,43 @@
export default class Thumbnails {
constructor(art, option) {
this.art = art;
this.option = option;
this.init();
}
init() {
const {
refs: { $progress },
events: { proxy }
} = this.art;
proxy($progress, 'mousemove', event => {
this.option.ref.style.display = 'block';
this.showThumbnails(event);
});
proxy($progress, 'mouseout', () => {
this.option.ref.style.display = 'none';
});
}
showThumbnails(event) {
const { $progress } = this.art.refs;
const { width: posWidth } = this.getPosFromEvent(event);
const { url, height, width, number } = this.art.option.thumbnails;
const perWidth = $progress.clientWidth / number;
const index = Math.ceil(posWidth / perWidth);
this.option.ref.style.backgroundImage = `url(${url})`;
this.option.ref.style.height = `${height}px`;
this.option.ref.style.width = `${width}px`;
this.option.ref.style.backgroundPosition = `-${index * width}px 0`;
if (posWidth <= width / 2) {
this.option.ref.style.left = 0;
} else if (posWidth > $progress.clientWidth - width / 2) {
this.option.ref.style.left = `${$progress.clientWidth - width}px`;
} else {
this.option.ref.style.left = `${posWidth - width / 2}px`;
}
}
}
+2 -11
View File
@@ -9,8 +9,6 @@ export default class Hotkey {
init() {
const {
player,
notice,
i18n,
refs: { $player },
events: { proxy }
} = this.art;
@@ -29,29 +27,22 @@ export default class Hotkey {
editable !== '' &&
editable !== 'true'
) {
let percentage;
switch (event.keyCode) {
case 39:
event.preventDefault();
player.seek(player.currentTime() + 10);
notice.show(i18n.get('Fast forward 10 seconds'), true);
break;
case 37:
event.preventDefault();
player.seek(player.currentTime() - 10);
notice.show(i18n.get('Rewind 10 seconds'), true);
break;
case 38:
event.preventDefault();
percentage = player.volume() + 0.1;
player.volume(percentage);
notice.show(i18n.get('10% increase in volume'), true);
player.volume(player.volume() + 0.05);
break;
case 40:
event.preventDefault();
percentage = player.volume() - 0.1;
player.volume(percentage);
notice.show(i18n.get('10% reduction in volume'), true);
player.volume(player.volume() - 0.05);
break;
case 32:
event.preventDefault();
+20 -10
View File
@@ -1,32 +1,42 @@
const i18nMap = {
let i18nMap = {
'zh-cn': {
'About author': '关于作者',
'Video info': '视频统计信息',
'Close': '关闭',
'Video load failed': '视频加载失败',
'Fast forward 10 seconds': '快进10秒',
'Rewind 10 seconds': '快退10秒',
'10% increase in volume': '音量增加10%',
'10% reduction in volume': '音量减少10%'
'Volume': '音量',
'Start': '开始',
'Pause': '暂停',
'Rate': '速度'
},
'zh-tw': {
'About author': '關於作者',
'Video info': '影片統計訊息',
'Close': '關閉',
'Video load failed': '影片載入失敗',
'Fast forward 10 seconds': '快進10秒',
'Rewind 10 seconds': '快退10秒',
'10% increase in volume': '音量增加10%',
'10% reduction in volume': '音量減少10%'
'Volume': '音量',
'Start': '開始',
'Pause': '暫停',
'Rate': '速度'
}
};
export default class I18n {
constructor({ option }) {
this.language = i18nMap[option.lang.toLowerCase()] || {};
this.option = option;
this.init();
}
init() {
this.language = i18nMap[this.option.lang.toLowerCase()] || {};
}
get(key) {
return this.language[key] || key;
}
update(callback) {
i18nMap = callback(i18nMap);
this.init();
}
}
+2 -2
View File
@@ -4,7 +4,7 @@ export default class Notice {
this.timer = null;
}
show(msg, autoHide) {
show(msg, autoHide = true, time = 1000) {
const { $notice } = this.art.refs;
$notice.style.display = 'block';
$notice.innerHTML = msg instanceof Error ? msg.message.trim() : msg;
@@ -12,7 +12,7 @@ export default class Notice {
clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.hide();
}, 1000);
}, time);
}
this.art.emit('notice:show', $notice);
}
+22 -23
View File
@@ -1,4 +1,4 @@
import { clamp } from './utils';
import { clamp, secondToTime } from './utils';
import config from './config';
export default class Player {
@@ -78,19 +78,22 @@ export default class Player {
this.art.on('video:error', () => {
this.art.isPlaying = false;
this.art.loading.hide();
});
}
play() {
const { $video } = this.art.refs;
const { refs: { $video }, i18n, notice } = this.art;
const promise = $video.play();
notice.show(i18n.get('Start'));
this.art.emit('play', $video);
return promise;
}
pause() {
const { $video } = this.art.refs;
const { refs: { $video }, i18n, notice } = this.art;
$video.pause();
notice.show(i18n.get('Pause'));
this.art.emit('pause', $video);
}
@@ -103,43 +106,39 @@ export default class Player {
}
seek(time) {
const { $video } = this.art.refs;
const { refs: { $video }, notice } = this.art;
let newTime = Math.max(time, 0);
if ($video.duration) {
newTime = Math.min(newTime, $video.duration);
}
$video.currentTime = newTime;
notice.show(`${secondToTime(newTime)} / ${secondToTime($video.duration)}`);
this.art.emit('seek', newTime);
}
volume(percentage) {
const { $video } = this.art.refs;
const { refs: { $video }, i18n, notice } = this.art;
if (percentage) {
$video.volume = clamp(percentage, 0, 1);
notice.show(`${i18n.get('Volume')}: ${parseInt($video.volume * 100)}`);
this.art.emit('volume', $video.volume);
}
this.art.emit('volume', $video.volume);
return $video.volume;
}
switchVolumeIcon() {
//
currentTime() {
return this.art.refs.$video.currentTime;
}
switchVideo() {
//
duration() {
return this.art.refs.$video.duration;
}
switchQuality() {
//
}
resize() {
//
}
speed(rate) {
const { $video } = this.art.refs;
$video.playbackRate = rate;
this.art.emit('speed', rate);
playbackRate(rate) {
const { refs: { $video }, i18n, notice } = this.art;
const newRate = clamp(rate, 0.1, 10);
$video.playbackRate = newRate;
notice.show(`${i18n.get('Rate')}: ${newRate}x`);
this.art.emit('playbackRate', newRate);
}
}
+90 -87
View File
@@ -118,107 +118,109 @@
box-sizing: border-box;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAADGCAYAAAAT+OqFAAAAdklEQVQoz42QQQ7AIAgEF/T/D+kbq/RWAlnQyyazA4aoAB4FsBSA/bFjuF1EOL7VbrIrBuusmrt4ZZORfb6ehbWdnRHEIiITaEUKa5EJqUakRSaEYBJSCY2dEstQY7AuxahwXFrvZmWl2rh4JZ07z9dLtesfNj5q0FU3A5ObbwAAAABJRU5ErkJggg==) repeat-x bottom;
.art-control-progress {
.artplayer-progress {
position: relative;
height: 5px;
padding: 1px 0;
cursor: pointer;
.art-control-progress-inner {
.art-control-progress {
position: relative;
height: 100%;
background: rgba(255,255,255,.2);
.art-progress-loaded {
position: absolute;
z-index: 10;
left: 0;
top: 0;
right: 0;
bottom: 0;
height: 100%;
width: 0;
background: rgba(255,255,255,.4);
}
height: 5px;
padding: 1px 0;
cursor: pointer;
.art-progress-played {
position: absolute;
z-index: 20;
left: 0;
top: 0;
right: 0;
bottom: 0;
.art-control-progress-inner {
position: relative;
height: 100%;
width: 0;
}
.art-progress-highlight {
position: absolute;
z-index: 30;
left: 0;
top: 0;
right: 0;
bottom: 0;
height: 0;
span {
display: inline-block;
background: rgba(255,255,255,.2);
.art-progress-loaded {
position: absolute;
z-index: 10;
left: 0;
top: 0;
width: 7px;
height: 3px;
background: #fff;
right: 0;
bottom: 0;
height: 100%;
width: 0;
background: rgba(255,255,255,.4);
}
.art-progress-played {
position: absolute;
z-index: 20;
left: 0;
top: 0;
right: 0;
bottom: 0;
height: 100%;
width: 0;
}
.art-progress-highlight {
position: absolute;
z-index: 30;
left: 0;
top: 0;
right: 0;
bottom: 0;
height: 0;
span {
display: inline-block;
position: absolute;
left: 0;
top: 0;
width: 7px;
height: 3px;
background: #fff;
border-radius: 3px;
}
}
.art-progress-indicator {
display: none;
position: absolute;
z-index: 40;
top: -5px;
left: -6.5px;
width: 13px;
height: 13px;
border-radius: 50%;
&.show-indicator {
display: block;
}
}
.art-progress-tip {
display: none;
position: absolute;
z-index: 50;
top: -25px;
left: 0;
height: 20px;
padding: 0 5px;
line-height: 20px;
color: #fff;
font-size: 12px;
text-align: center;
background: rgba(0,0,0,.7);
border-radius: 3px;
font-weight: bold;
}
}
.art-progress-thumbnails {
// display: none;
position: absolute;
z-index: 50;
top: -95px;
left: 0;
pointer-events: none;
background-color: rgba(0,0,0,.7);
}
.art-progress-indicator {
display: none;
position: absolute;
z-index: 40;
top: -5px;
left: -6.5px;
width: 13px;
height: 13px;
border-radius: 50%;
&.show-indicator {
&:hover {
.art-progress-indicator, .art-progress-tip {
display: block;
}
}
.art-progress-tip {
display: none;
position: absolute;
z-index: 50;
top: -25px;
left: 0;
height: 20px;
padding: 0 5px;
line-height: 20px;
color: #fff;
font-size: 12px;
text-align: center;
background: rgba(0,0,0,.7);
border-radius: 3px;
font-weight: bold;
}
}
&:hover {
.art-progress-indicator, .art-progress-tip {
display: block;
}
.art-control-thumbnails {
display: none;
position: absolute;
top: -95px;
left: 0;
pointer-events: none;
background-color: rgba(0,0,0,.7);
}
}
}
@@ -232,7 +234,7 @@
.artplayer-notice {
display: none;
font-size: 13px;
font-size: 14px;
position: absolute;
z-index: 60;
left: 10px;
@@ -240,6 +242,7 @@
height: 30px;
line-height: 30px;
padding: 0 10px;
border-radius: 3px;
color: #fff;
background: rgba(0, 0, 0, 0.6);
}