mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-18 02:21:20 -08:00
优化代码
This commit is contained in:
1 parent
d058fc318c
commit
e0a2a2c594
25 files changed
+288
-172
No files matched your search
@@ -33,7 +33,7 @@ export default class Contextmenu {
|
||||
});
|
||||
|
||||
proxy(refs.$player, 'click', event => {
|
||||
if (refs.$contextmenu && !event.path.includes(refs.$contextmenu)) {
|
||||
if (refs.$contextmenu && !event.composedPath().includes(refs.$contextmenu)) {
|
||||
this.hide();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -10,16 +10,19 @@ export default class Fullscreen {
|
||||
const { events: { proxy }, i18n, player } = art;
|
||||
this.$fullscreen = append($control, icons.fullscreen);
|
||||
tooltip(this.$fullscreen, i18n.get('Fullscreen'));
|
||||
|
||||
proxy($control, 'click', () => {
|
||||
if (player.fullscreenState) {
|
||||
player.fullscreenExit();
|
||||
setStyle(this.$fullscreen, 'opacity', '1');
|
||||
tooltip(this.$fullscreen, i18n.get('Fullscreen'));
|
||||
} else {
|
||||
player.fullscreenEnabled();
|
||||
setStyle(this.$fullscreen, 'opacity', '0.8');
|
||||
tooltip(this.$fullscreen, i18n.get('Exit fullscreen'));
|
||||
}
|
||||
player.fullscreenToggle();
|
||||
});
|
||||
|
||||
art.on('fullscreen:enabled', () => {
|
||||
setStyle(this.$fullscreen, 'opacity', '0.8');
|
||||
tooltip(this.$fullscreen, i18n.get('Exit fullscreen'));
|
||||
});
|
||||
|
||||
art.on('fullscreen:exit', () => {
|
||||
setStyle(this.$fullscreen, 'opacity', '1');
|
||||
tooltip(this.$fullscreen, i18n.get('Fullscreen'));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -10,16 +10,19 @@ export default class FullscreenWeb {
|
||||
const { events: { proxy }, i18n, player } = art;
|
||||
this.$fullscreenWeb = append($control, icons.fullscreenWeb);
|
||||
tooltip(this.$fullscreenWeb, i18n.get('Web fullscreen'));
|
||||
|
||||
proxy($control, 'click', () => {
|
||||
if (player.fullscreenWebState) {
|
||||
player.fullscreenWebExit();
|
||||
setStyle(this.$fullscreenWeb, 'opacity', '1');
|
||||
tooltip(this.$fullscreenWeb, i18n.get('Web fullscreen'));
|
||||
} else {
|
||||
player.fullscreenWebEnabled();
|
||||
setStyle(this.$fullscreenWeb, 'opacity', '0.8');
|
||||
tooltip(this.$fullscreenWeb, i18n.get('Exit web fullscreen'));
|
||||
}
|
||||
player.fullscreenWebToggle();
|
||||
});
|
||||
|
||||
art.on('fullscreenWeb:enabled', () => {
|
||||
setStyle(this.$fullscreenWeb, 'opacity', '0.8');
|
||||
tooltip(this.$fullscreenWeb, i18n.get('Exit web fullscreen'));
|
||||
});
|
||||
|
||||
art.on('fullscreenWeb:exit', () => {
|
||||
setStyle(this.$fullscreenWeb, 'opacity', '1');
|
||||
tooltip(this.$fullscreenWeb, i18n.get('Web fullscreen'));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ export default class Progress {
|
||||
|
||||
proxy($control, 'mousemove', event => {
|
||||
setStyle(this.$tip, 'display', 'block');
|
||||
if (event.path.indexOf(this.$highlight) > -1) {
|
||||
if (event.composedPath().indexOf(this.$highlight) > -1) {
|
||||
this.showHighlight(event);
|
||||
} else {
|
||||
this.showTime(event);
|
||||
|
||||
+12
-9
@@ -10,16 +10,19 @@ export default class Setting {
|
||||
const { events: { proxy }, i18n, setting } = art;
|
||||
this.$setting = append($control, icons.setting);
|
||||
tooltip(this.$setting, i18n.get('Show setting'));
|
||||
|
||||
proxy($control, 'click', () => {
|
||||
if (setting.isOpen) {
|
||||
setting.hide();
|
||||
setStyle(this.$setting, 'opacity', '1');
|
||||
tooltip(this.$setting, i18n.get('Show setting'));
|
||||
} else {
|
||||
setting.show();
|
||||
setStyle(this.$setting, 'opacity', '0.8');
|
||||
tooltip(this.$setting, i18n.get('Hide setting'));
|
||||
}
|
||||
setting.toggle();
|
||||
});
|
||||
|
||||
art.on('setting:show', () => {
|
||||
setStyle(this.$setting, 'opacity', '0.8');
|
||||
tooltip(this.$setting, i18n.get('Hide setting'));
|
||||
});
|
||||
|
||||
art.on('setting:hide', () => {
|
||||
setStyle(this.$setting, 'opacity', '1');
|
||||
tooltip(this.$setting, i18n.get('Show setting'));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -10,16 +10,19 @@ export default class Subtitle {
|
||||
const { events: { proxy }, i18n, subtitle } = art;
|
||||
this.$subtitle = append($control, icons.subtitle);
|
||||
tooltip(this.$subtitle, i18n.get('Hide subtitle'));
|
||||
|
||||
proxy($control, 'click', () => {
|
||||
if (subtitle.isOpen) {
|
||||
subtitle.hide();
|
||||
setStyle(this.$subtitle, 'opacity', '1');
|
||||
tooltip(this.$subtitle, i18n.get('Show subtitle'));
|
||||
} else {
|
||||
subtitle.show();
|
||||
setStyle(this.$subtitle, 'opacity', '0.8');
|
||||
tooltip(this.$subtitle, i18n.get('Hide subtitle'));
|
||||
}
|
||||
subtitle.toggle();
|
||||
});
|
||||
|
||||
art.on('subtitle:show', () => {
|
||||
setStyle(this.$subtitle, 'opacity', '0.8');
|
||||
tooltip(this.$subtitle, i18n.get('Hide subtitle'));
|
||||
});
|
||||
|
||||
art.on('subtitle:hide', () => {
|
||||
setStyle(this.$subtitle, 'opacity', '1');
|
||||
tooltip(this.$subtitle, i18n.get('Show subtitle'));
|
||||
});
|
||||
}
|
||||
}
|
||||
+10
-13
@@ -6,25 +6,22 @@ export default class Time {
|
||||
}
|
||||
|
||||
apply(art, $control) {
|
||||
this.art = art;
|
||||
this.$control = $control;
|
||||
$control.innerHTML = '00:00 / 00:00';
|
||||
|
||||
this.art.on('video:canplay', () => {
|
||||
this.getTime();
|
||||
function setTime() {
|
||||
$control.innerHTML = `${secondToTime(art.player.currentTime())} / ${secondToTime(art.player.duration())}`;
|
||||
}
|
||||
|
||||
art.on('video:canplay', () => {
|
||||
setTime();
|
||||
});
|
||||
|
||||
this.art.on('video:timeupdate', () => {
|
||||
this.getTime();
|
||||
art.on('video:timeupdate', () => {
|
||||
setTime();
|
||||
});
|
||||
|
||||
this.art.on('video:seeking', () => {
|
||||
this.getTime();
|
||||
art.on('video:seeking', () => {
|
||||
setTime();
|
||||
});
|
||||
}
|
||||
|
||||
getTime() {
|
||||
const { player } = this.art;
|
||||
this.$control.innerHTML = `${secondToTime(player.currentTime())} / ${secondToTime(player.duration())}`;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export default class Events {
|
||||
|
||||
init() {
|
||||
const { refs: { $player } } = this.art;
|
||||
|
||||
this.hover($player, () => {
|
||||
$player.classList.add('artplayer-hover');
|
||||
this.art.emit('hoverenter');
|
||||
@@ -20,6 +21,15 @@ export default class Events {
|
||||
this.art.emit('hoverleave');
|
||||
});
|
||||
|
||||
this.proxy(document, ['click', 'contextmenu'], event => {
|
||||
if (event.composedPath().indexOf($player) > -1) {
|
||||
this.art.isFocus = true;
|
||||
} else {
|
||||
this.art.isFocus = false;
|
||||
this.art.contextmenu.hide();
|
||||
}
|
||||
});
|
||||
|
||||
const hideCursor = debounce(() => {
|
||||
$player.classList.add('artplayer-hide-cursor');
|
||||
if (this.art.player.fullscreenState || this.art.player.fullscreenWebState) {
|
||||
@@ -27,11 +37,14 @@ export default class Events {
|
||||
this.art.controls.hide();
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
this.proxy($player, 'mousemove', () => {
|
||||
$player.classList.remove('artplayer-hide-cursor');
|
||||
this.art.controls.show();
|
||||
if (!this.art.player.pipState) {
|
||||
hideCursor();
|
||||
} else {
|
||||
hideCursor.clearTimeout();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+1
-5
@@ -7,11 +7,7 @@ export default class Hotkey {
|
||||
}
|
||||
|
||||
init() {
|
||||
const { player, refs: { $player }, events: { proxy } } = this.art;
|
||||
|
||||
proxy(document, ['click', 'contextmenu'], event => {
|
||||
this.art.isFocus = event.path.indexOf($player) > -1;
|
||||
});
|
||||
const { player, events: { proxy } } = this.art;
|
||||
|
||||
proxy(window, 'keydown', event => {
|
||||
if (this.art.isFocus) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { clamp } from '../utils';
|
||||
import { clamp, sleep } from '../utils';
|
||||
|
||||
export default function attrInit(art) {
|
||||
export default function attrInit(art, player) {
|
||||
const { option, refs: { $video } } = art;
|
||||
Object.keys(option.moreVideoAttr).forEach(key => {
|
||||
$video[key] = option.moreVideoAttr[key];
|
||||
@@ -8,5 +8,8 @@ export default function attrInit(art) {
|
||||
$video.volume = clamp(option.volume, 0, 1);
|
||||
$video.poster = option.poster;
|
||||
$video.autoplay = option.autoplay;
|
||||
$video.src = option.url;
|
||||
sleep().then(() => {
|
||||
art.emit('beforeMountUrl', option.url);
|
||||
$video.src = player.mountUrl(option.url);
|
||||
});
|
||||
}
|
||||
@@ -30,6 +30,7 @@ export default function fullscreenMix(art, player) {
|
||||
}
|
||||
$player.classList.add('artplayer-fullscreen');
|
||||
screenfull.request($player);
|
||||
art.emit('fullscreen:enabled');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -40,6 +41,7 @@ export default function fullscreenMix(art, player) {
|
||||
}
|
||||
$player.classList.remove('artplayer-fullscreen');
|
||||
screenfull.exit();
|
||||
art.emit('fullscreen:exit');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ export default function fullscreenWebMix(art, player) {
|
||||
}
|
||||
$player.classList.add('artplayer-web-fullscreen');
|
||||
player.aspectRatioReset();
|
||||
art.emit('fullscreenWeb', true);
|
||||
art.emit('fullscreenWeb:enabled');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function fullscreenWebMix(art, player) {
|
||||
}
|
||||
$player.classList.remove('artplayer-web-fullscreen');
|
||||
player.aspectRatioReset();
|
||||
art.emit('fullscreenWeb', false);
|
||||
art.emit('fullscreenWeb:exit');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import mountUrlMix from './mountUrlMix';
|
||||
import attrInit from './attrInit';
|
||||
import eventInit from './eventInit';
|
||||
import playMix from './playMix';
|
||||
@@ -20,6 +21,7 @@ export default class Player {
|
||||
this.reconnectTime = 0;
|
||||
this.maxReconnectTime = 5;
|
||||
|
||||
mountUrlMix(art, this);
|
||||
attrInit(art, this);
|
||||
eventInit(art, this);
|
||||
playMix(art, this);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export default function mountUrlMix(art, player) {
|
||||
Object.defineProperty(player, 'mountUrl', {
|
||||
writable: true,
|
||||
value: url => url
|
||||
});
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import Draggabilly from 'draggabilly';
|
||||
import { setStyle, append } from '../utils';
|
||||
|
||||
let draggie = null;
|
||||
export default function pipMix(art, player) {
|
||||
const { option, i18n, refs: { $player, $pipClose, $pipTitle }, events: { destroyEvents, proxy } } = art;
|
||||
let cachePos = null;
|
||||
let draggie = null;
|
||||
|
||||
Object.defineProperty(player, 'pipState', {
|
||||
get: () => $player.classList.contains('artplayer-pip')
|
||||
@@ -29,6 +30,9 @@ export default function pipMix(art, player) {
|
||||
destroyEvents.push(() => {
|
||||
draggie.destroy();
|
||||
});
|
||||
} else {
|
||||
setStyle($player, 'left', `${cachePos.x}px`);
|
||||
setStyle($player, 'top', `${cachePos.y}px`);
|
||||
}
|
||||
$player.classList.add('artplayer-pip');
|
||||
player.fullscreenExit();
|
||||
@@ -42,6 +46,7 @@ export default function pipMix(art, player) {
|
||||
Object.defineProperty(player, 'pipExit', {
|
||||
value: () => {
|
||||
$player.classList.remove('artplayer-pip');
|
||||
cachePos = draggie.position;
|
||||
setStyle($player, 'left', null);
|
||||
setStyle($player, 'top', null);
|
||||
player.fullscreenExit();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { setStyle, secondToTime } from '../utils';
|
||||
|
||||
export default function screenshotMix(art, player) {
|
||||
const { notice } = art;
|
||||
const { option, notice } = art;
|
||||
|
||||
function captureFrame() {
|
||||
const { $video } = art.refs;
|
||||
@@ -13,7 +13,7 @@ export default function screenshotMix(art, player) {
|
||||
const elink = document.createElement('a');
|
||||
setStyle(elink, 'display', 'none');
|
||||
elink.href = dataUri;
|
||||
elink.download = `ArtPlayer_${secondToTime($video.currentTime)}.png`;
|
||||
elink.download = `${option.title || 'artplayer'}_${secondToTime($video.currentTime)}.png`;
|
||||
document.body.appendChild(elink);
|
||||
elink.click();
|
||||
document.body.removeChild(elink);
|
||||
|
||||
@@ -2,9 +2,10 @@ export default function switchMix(art, player) {
|
||||
const { refs: { $video }, i18n, notice, isPlaying, option } = art;
|
||||
|
||||
Object.defineProperty(player, 'switch', {
|
||||
value: (url, name = 'Unknown') => {
|
||||
value: (url, name = 'unknown') => {
|
||||
const currentTime = player.currentTime();
|
||||
$video.src = url;
|
||||
art.emit('beforeMountUrl', url);
|
||||
$video.src = player.mountUrl(url);
|
||||
option.url = url;
|
||||
player.playbackRateRemove();
|
||||
player.aspectRatioRemove();
|
||||
|
||||
+11
-3
@@ -5,7 +5,7 @@ let id = 0;
|
||||
export default class Setting {
|
||||
constructor(art) {
|
||||
this.art = art;
|
||||
this.isOpen = false;
|
||||
this.state = false;
|
||||
this.$settings = [];
|
||||
if (art.option.setting) {
|
||||
this.init();
|
||||
@@ -59,7 +59,7 @@ export default class Setting {
|
||||
show() {
|
||||
const { refs: { $setting }, i18n, notice } = this.art;
|
||||
setStyle($setting, 'display', 'flex');
|
||||
this.isOpen = true;
|
||||
this.state = true;
|
||||
notice.show(i18n.get('Show setting'));
|
||||
this.art.emit('setting:show', $setting);
|
||||
}
|
||||
@@ -67,8 +67,16 @@ export default class Setting {
|
||||
hide() {
|
||||
const { refs: { $setting }, i18n, notice } = this.art;
|
||||
setStyle($setting, 'display', 'none');
|
||||
this.isOpen = false;
|
||||
this.state = false;
|
||||
notice.show(i18n.get('Hide setting'));
|
||||
this.art.emit('setting:hide', $setting);
|
||||
}
|
||||
|
||||
toggle() {
|
||||
if (this.state) {
|
||||
this.hide();
|
||||
} else {
|
||||
this.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,16 @@
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
transition: all .2s ease-in-out;
|
||||
.artplayer-pip-title {
|
||||
flex: 1;
|
||||
padding: 0 10px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
cursor: move;
|
||||
}
|
||||
.artplayer-pip-close {
|
||||
width: 35px;
|
||||
@@ -43,6 +47,13 @@
|
||||
display: flex;
|
||||
}
|
||||
|
||||
&.artplayer-hover {
|
||||
.artplayer-pip-header {
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
}
|
||||
|
||||
.artplayer-mask {
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
+15
-10
@@ -3,7 +3,7 @@ import { errorHandle, getExt, setStyles, setStyle } from './utils';
|
||||
export default class Subtitle {
|
||||
constructor(art) {
|
||||
this.art = art;
|
||||
this.isOpen = true;
|
||||
this.state = true;
|
||||
this.vttText = '';
|
||||
const { url } = this.art.option.subtitle;
|
||||
if (url) {
|
||||
@@ -95,7 +95,7 @@ export default class Subtitle {
|
||||
show() {
|
||||
const { refs: { $subtitle }, i18n, notice } = this.art;
|
||||
setStyle($subtitle, 'display', 'block');
|
||||
this.isOpen = true;
|
||||
this.state = true;
|
||||
notice.show(i18n.get('Show subtitle'));
|
||||
this.art.emit('subtitle:show', $subtitle);
|
||||
}
|
||||
@@ -103,21 +103,26 @@ export default class Subtitle {
|
||||
hide() {
|
||||
const { refs: { $subtitle }, i18n, notice } = this.art;
|
||||
setStyle($subtitle, 'display', 'none');
|
||||
this.isOpen = false;
|
||||
this.state = false;
|
||||
notice.show(i18n.get('Hide subtitle'));
|
||||
this.art.emit('subtitle:hide', $subtitle);
|
||||
}
|
||||
|
||||
switch(url, name = 'unknown') {
|
||||
const { $track, i18n, notice } = this.art.refs;
|
||||
toggle() {
|
||||
if (this.state) {
|
||||
this.hide();
|
||||
} else {
|
||||
this.show();
|
||||
}
|
||||
}
|
||||
|
||||
switch(url) {
|
||||
const { $track } = this.art.refs;
|
||||
this.checkExt(url);
|
||||
errorHandle($track, 'You need to initialize the subtitle option first.');
|
||||
this.load(url).then(data => {
|
||||
if (url !== data) {
|
||||
$track.src = data;
|
||||
notice.show(`${i18n.get('Switch subtitle')}: ${name}`);
|
||||
this.art.emit('subtitle:switch', url);
|
||||
}
|
||||
$track.src = data;
|
||||
this.art.emit('subtitle:switch', url);
|
||||
});
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -120,7 +120,7 @@ export function sublings(target) {
|
||||
|
||||
export function debounce(func, wait, context) {
|
||||
let timeout;
|
||||
return function fn() {
|
||||
function fn() {
|
||||
const args = arguments;
|
||||
const later = function later() {
|
||||
timeout = null;
|
||||
@@ -128,5 +128,11 @@ export function debounce(func, wait, context) {
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
}
|
||||
|
||||
fn.clearTimeout = function ct() {
|
||||
clearTimeout(timeout);
|
||||
};
|
||||
|
||||
return fn;
|
||||
}
|
||||
Reference in new issue
Block a user