mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-23 22:01:20 -08:00
格式化
This commit is contained in:
1 parent
0454f785be
commit
203ad0d3a0
75 files changed
+3120
-2904
No files matched your search
@@ -1,32 +1,32 @@
|
||||
import { inverseClass } from '../utils';
|
||||
|
||||
export default function aspectRatio(art) {
|
||||
const { option, i18n, player } = art;
|
||||
return {
|
||||
disable: !option.aspectRatio,
|
||||
name: 'aspectRatio',
|
||||
index: 20,
|
||||
html: `
|
||||
${i18n.get('Aspect ratio')}:
|
||||
<span data-ratio="default" class="default current">${i18n.get('Default')}</span>
|
||||
<span data-ratio="4:3">4:3</span>
|
||||
<span data-ratio="16:9">16:9</span>
|
||||
`,
|
||||
click: event => {
|
||||
const { target } = event;
|
||||
const { ratio } = target.dataset;
|
||||
if (ratio) {
|
||||
player.aspectRatio(ratio.split(':'));
|
||||
art.contextmenu.hide();
|
||||
}
|
||||
},
|
||||
callback: $menu => {
|
||||
art.on('aspectRatioChange', ratio => {
|
||||
const $current = Array.from($menu.querySelectorAll('span')).find(
|
||||
item => item.dataset.ratio === ratio.join(':')
|
||||
);
|
||||
inverseClass($current, 'current');
|
||||
});
|
||||
}
|
||||
};
|
||||
const { option, i18n, player } = art;
|
||||
return {
|
||||
disable: !option.aspectRatio,
|
||||
name: 'aspectRatio',
|
||||
index: 20,
|
||||
html: `
|
||||
${i18n.get('Aspect ratio')}:
|
||||
<span data-ratio="default" class="default current">${i18n.get('Default')}</span>
|
||||
<span data-ratio="4:3">4:3</span>
|
||||
<span data-ratio="16:9">16:9</span>
|
||||
`,
|
||||
click: event => {
|
||||
const { target } = event;
|
||||
const { ratio } = target.dataset;
|
||||
if (ratio) {
|
||||
player.aspectRatio(ratio.split(':'));
|
||||
art.contextmenu.hide();
|
||||
}
|
||||
},
|
||||
callback: $menu => {
|
||||
art.on('aspectRatioChange', ratio => {
|
||||
const $current = Array.from($menu.querySelectorAll('span')).find(
|
||||
item => item.dataset.ratio === ratio.join(':'),
|
||||
);
|
||||
inverseClass($current, 'current');
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
export default function close(art) {
|
||||
return {
|
||||
disable: false,
|
||||
name: 'close',
|
||||
index: 50,
|
||||
html: art.i18n.get('Close'),
|
||||
click: () => {
|
||||
art.contextmenu.hide();
|
||||
}
|
||||
};
|
||||
return {
|
||||
disable: false,
|
||||
name: 'close',
|
||||
index: 50,
|
||||
html: art.i18n.get('Close'),
|
||||
click: () => {
|
||||
art.contextmenu.hide();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -7,97 +7,108 @@ import close from './close';
|
||||
|
||||
let id = 0;
|
||||
export default class Contextmenu {
|
||||
constructor(art) {
|
||||
id = 0;
|
||||
this.art = art;
|
||||
this.art.on('firstCanplay', () => {
|
||||
this.init();
|
||||
});
|
||||
}
|
||||
|
||||
init() {
|
||||
const { option, refs: { $player, $contextmenu }, events: { proxy } } = this.art;
|
||||
|
||||
option.contextmenu.push(
|
||||
playbackRate,
|
||||
aspectRatio,
|
||||
info,
|
||||
version,
|
||||
close
|
||||
);
|
||||
|
||||
option.contextmenu.forEach(item => {
|
||||
this.add(item);
|
||||
});
|
||||
|
||||
proxy($player, 'contextmenu', event => {
|
||||
event.preventDefault();
|
||||
this.show();
|
||||
this.setPos(event);
|
||||
});
|
||||
|
||||
proxy($player, 'click', event => {
|
||||
if (!event.composedPath().includes($contextmenu)) {
|
||||
this.hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
add(item, callback) {
|
||||
const menu = typeof item === 'function' ? item(this.art) : item;
|
||||
if (!menu.disable) {
|
||||
id++;
|
||||
const { refs: { $contextmenu }, events: { proxy } } = this.art;
|
||||
const name = menu.name || `contextmenu${id}`;
|
||||
const $menu = document.createElement('div');
|
||||
$menu.classList.value = `art-contextmenu art-contextmenu-${name}`;
|
||||
setStyles($menu, menu.style || {});
|
||||
append($menu, menu.html);
|
||||
if (menu.click) {
|
||||
proxy($menu, 'click', event => {
|
||||
event.preventDefault();
|
||||
menu.click.call(this, event);
|
||||
this.art.emit('contextmenu:click', $menu);
|
||||
constructor(art) {
|
||||
id = 0;
|
||||
this.art = art;
|
||||
this.art.on('firstCanplay', () => {
|
||||
this.init();
|
||||
});
|
||||
}
|
||||
insertByIndex($contextmenu, $menu, menu.index || id);
|
||||
this[name] = $menu;
|
||||
menu.callback && menu.callback($menu);
|
||||
callback && callback($menu);
|
||||
this.art.emit('contextmenu:add', $menu);
|
||||
}
|
||||
}
|
||||
|
||||
setPos(event) {
|
||||
const { refs: { $player, $contextmenu } } = this.art;
|
||||
const mouseX = event.clientX;
|
||||
const mouseY = event.clientY;
|
||||
const { height: cHeight, width: cWidth, left: cLeft, top: cTop } = $player.getBoundingClientRect();
|
||||
const { height: mHeight, width: mWidth } = $contextmenu.getBoundingClientRect();
|
||||
let menuLeft = mouseX - cLeft;
|
||||
let menuTop = mouseY - cTop;
|
||||
|
||||
if (mouseX + mWidth > cLeft + cWidth) {
|
||||
menuLeft = cWidth - mWidth;
|
||||
}
|
||||
|
||||
if (mouseY + mHeight > cTop + cHeight) {
|
||||
menuTop = cHeight - mHeight;
|
||||
init() {
|
||||
const {
|
||||
option,
|
||||
refs: { $player, $contextmenu },
|
||||
events: { proxy },
|
||||
} = this.art;
|
||||
|
||||
option.contextmenu.push(playbackRate, aspectRatio, info, version, close);
|
||||
|
||||
option.contextmenu.forEach(item => {
|
||||
this.add(item);
|
||||
});
|
||||
|
||||
proxy($player, 'contextmenu', event => {
|
||||
event.preventDefault();
|
||||
this.show();
|
||||
this.setPos(event);
|
||||
});
|
||||
|
||||
proxy($player, 'click', event => {
|
||||
if (!event.composedPath().includes($contextmenu)) {
|
||||
this.hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setStyle($contextmenu, 'left', `${menuLeft}px`);
|
||||
setStyle($contextmenu, 'top', `${menuTop}px`);
|
||||
}
|
||||
add(item, callback) {
|
||||
const menu = typeof item === 'function' ? item(this.art) : item;
|
||||
if (!menu.disable) {
|
||||
id += 1;
|
||||
const {
|
||||
refs: { $contextmenu },
|
||||
events: { proxy },
|
||||
} = this.art;
|
||||
const name = menu.name || `contextmenu${id}`;
|
||||
const $menu = document.createElement('div');
|
||||
$menu.classList.value = `art-contextmenu art-contextmenu-${name}`;
|
||||
setStyles($menu, menu.style || {});
|
||||
append($menu, menu.html);
|
||||
if (menu.click) {
|
||||
proxy($menu, 'click', event => {
|
||||
event.preventDefault();
|
||||
menu.click.call(this, event);
|
||||
this.art.emit('contextmenu:click', $menu);
|
||||
});
|
||||
}
|
||||
insertByIndex($contextmenu, $menu, menu.index || id);
|
||||
this[name] = $menu;
|
||||
if (menu.callback) {
|
||||
menu.callback($menu);
|
||||
}
|
||||
if (callback) {
|
||||
callback($menu);
|
||||
}
|
||||
this.art.emit('contextmenu:add', $menu);
|
||||
}
|
||||
}
|
||||
|
||||
hide() {
|
||||
const { refs: { $contextmenu } } = this.art;
|
||||
setStyle($contextmenu, 'display', 'none');
|
||||
this.art.emit('contextmenu:hide', $contextmenu);
|
||||
}
|
||||
setPos(event) {
|
||||
const {
|
||||
refs: { $player, $contextmenu },
|
||||
} = this.art;
|
||||
const mouseX = event.clientX;
|
||||
const mouseY = event.clientY;
|
||||
const { height: cHeight, width: cWidth, left: cLeft, top: cTop } = $player.getBoundingClientRect();
|
||||
const { height: mHeight, width: mWidth } = $contextmenu.getBoundingClientRect();
|
||||
let menuLeft = mouseX - cLeft;
|
||||
let menuTop = mouseY - cTop;
|
||||
|
||||
show() {
|
||||
const { refs: { $contextmenu } } = this.art;
|
||||
setStyle($contextmenu, 'display', 'block');
|
||||
this.art.emit('contextmenu:show', $contextmenu);
|
||||
}
|
||||
if (mouseX + mWidth > cLeft + cWidth) {
|
||||
menuLeft = cWidth - mWidth;
|
||||
}
|
||||
|
||||
if (mouseY + mHeight > cTop + cHeight) {
|
||||
menuTop = cHeight - mHeight;
|
||||
}
|
||||
|
||||
setStyle($contextmenu, 'left', `${menuLeft}px`);
|
||||
setStyle($contextmenu, 'top', `${menuTop}px`);
|
||||
}
|
||||
|
||||
hide() {
|
||||
const {
|
||||
refs: { $contextmenu },
|
||||
} = this.art;
|
||||
setStyle($contextmenu, 'display', 'none');
|
||||
this.art.emit('contextmenu:hide', $contextmenu);
|
||||
}
|
||||
|
||||
show() {
|
||||
const {
|
||||
refs: { $contextmenu },
|
||||
} = this.art;
|
||||
setStyle($contextmenu, 'display', 'block');
|
||||
this.art.emit('contextmenu:show', $contextmenu);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
export default function info(art) {
|
||||
return {
|
||||
disable: false,
|
||||
name: 'info',
|
||||
index: 30,
|
||||
html: art.i18n.get('Video info'),
|
||||
click: () => {
|
||||
art.info.show();
|
||||
art.contextmenu.hide();
|
||||
}
|
||||
};
|
||||
return {
|
||||
disable: false,
|
||||
name: 'info',
|
||||
index: 30,
|
||||
html: art.i18n.get('Video info'),
|
||||
click: () => {
|
||||
art.info.show();
|
||||
art.contextmenu.hide();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,35 +1,35 @@
|
||||
import { inverseClass } from '../utils';
|
||||
|
||||
export default function playbackRate(art) {
|
||||
const { option, i18n, player } = art;
|
||||
return {
|
||||
disable: !option.playbackRate,
|
||||
name: 'playbackRate',
|
||||
index: 10,
|
||||
html: `
|
||||
${i18n.get('Play speed')}:
|
||||
<span data-rate="0.5">0.5</span>
|
||||
<span data-rate="0.75">0.75</span>
|
||||
<span data-rate="1" class="normal current">${i18n.get('Normal')}</span>
|
||||
<span data-rate="1.25">1.25</span>
|
||||
<span data-rate="1.5">1.5</span>
|
||||
<span data-rate="2.0">2.0</span>
|
||||
`,
|
||||
click: event => {
|
||||
const { target } = event;
|
||||
const { rate } = target.dataset;
|
||||
if (rate) {
|
||||
player.playbackRate(Number(rate));
|
||||
art.contextmenu.hide();
|
||||
}
|
||||
},
|
||||
callback: $menu => {
|
||||
art.on('playbackRateChange', rate => {
|
||||
const $current = Array.from($menu.querySelectorAll('span')).find(
|
||||
item => Number(item.dataset.rate) === rate
|
||||
);
|
||||
inverseClass($current, 'current');
|
||||
});
|
||||
}
|
||||
};
|
||||
const { option, i18n, player } = art;
|
||||
return {
|
||||
disable: !option.playbackRate,
|
||||
name: 'playbackRate',
|
||||
index: 10,
|
||||
html: `
|
||||
${i18n.get('Play speed')}:
|
||||
<span data-rate="0.5">0.5</span>
|
||||
<span data-rate="0.75">0.75</span>
|
||||
<span data-rate="1" class="normal current">${i18n.get('Normal')}</span>
|
||||
<span data-rate="1.25">1.25</span>
|
||||
<span data-rate="1.5">1.5</span>
|
||||
<span data-rate="2.0">2.0</span>
|
||||
`,
|
||||
click: event => {
|
||||
const { target } = event;
|
||||
const { rate } = target.dataset;
|
||||
if (rate) {
|
||||
player.playbackRate(Number(rate));
|
||||
art.contextmenu.hide();
|
||||
}
|
||||
},
|
||||
callback: $menu => {
|
||||
art.on('playbackRateChange', rate => {
|
||||
const $current = Array.from($menu.querySelectorAll('span')).find(
|
||||
item => Number(item.dataset.rate) === rate,
|
||||
);
|
||||
inverseClass($current, 'current');
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
export default {
|
||||
disable: false,
|
||||
name: 'version',
|
||||
index: 40,
|
||||
html: '<a href="https://github.com/zhw2590582/artplayer" target="_blank">ArtPlayer __VERSION__</a>'
|
||||
disable: false,
|
||||
name: 'version',
|
||||
index: 40,
|
||||
html: '<a href="https://github.com/zhw2590582/artplayer" target="_blank">ArtPlayer __VERSION__</a>',
|
||||
};
|
||||
Reference in new issue
Block a user