1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/plugins/bookPlayer/plugin.js

328 lines
9.9 KiB
JavaScript
Raw Normal View History

2020-11-21 03:34:16 -05:00
import { Events } from 'jellyfin-apiclient';
import 'material-design-icons-iconfont';
2020-08-14 08:46:34 +02:00
import loading from '../../components/loading/loading';
2020-08-16 20:24:45 +02:00
import keyboardnavigation from '../../scripts/keyboardNavigation';
import dialogHelper from '../../components/dialogHelper/dialogHelper';
import ServerConnections from '../../components/ServerConnections';
2021-04-25 18:11:52 -04:00
import * as Screenfull from 'screenfull';
2020-06-06 15:53:47 +09:00
import TableOfContents from './tableOfContents';
2020-12-16 01:19:10 -05:00
import dom from '../../scripts/dom';
2020-11-21 03:34:16 -05:00
import { translateHtml } from '../../scripts/globalize';
import '../../scripts/dom';
import '../../elements/emby-button/paper-icon-button-light';
import html from './template.html';
import './style.scss';
export class BookPlayer {
constructor() {
this.name = 'Book Player';
this.type = 'mediaplayer';
this.id = 'bookplayer';
this.priority = 1;
this.onDialogClosed = this.onDialogClosed.bind(this);
this.openTableOfContents = this.openTableOfContents.bind(this);
2020-12-16 01:19:10 -05:00
this.previous = this.previous.bind(this);
this.next = this.next.bind(this);
this.onWindowKeyUp = this.onWindowKeyUp.bind(this);
2020-12-16 01:19:10 -05:00
this.onTouchStart = this.onTouchStart.bind(this);
}
2020-05-16 22:02:52 +10:00
play(options) {
2020-09-01 22:55:08 +09:00
this.progress = 0;
this.cancellationToken = false;
this.loaded = false;
loading.show();
const elem = this.createMediaElement();
return this.setCurrentSrc(elem, options);
}
2020-05-16 22:02:52 +10:00
stop() {
this.unbindEvents();
2020-09-01 22:55:08 +09:00
const elem = this.mediaElement;
const tocElement = this.tocElement;
const rendition = this.rendition;
2020-05-16 22:02:52 +10:00
if (elem) {
dialogHelper.close(elem);
2020-09-01 22:55:08 +09:00
this.mediaElement = null;
}
if (tocElement) {
tocElement.destroy();
2020-09-01 22:55:08 +09:00
this.tocElement = null;
}
if (rendition) {
rendition.destroy();
}
2020-09-01 22:55:08 +09:00
// hide loader in case player was not fully loaded yet
loading.hide();
2020-09-01 22:55:08 +09:00
this.cancellationToken = true;
}
currentItem() {
2020-09-01 22:55:08 +09:00
return this.item;
}
currentTime() {
2020-09-01 22:55:08 +09:00
return this.progress * 1000;
}
duration() {
return 1000;
}
getBufferedRanges() {
return [{
start: 0,
end: 10000000
}];
}
volume() {
return 100;
}
isMuted() {
return false;
}
paused() {
return false;
}
seekable() {
return true;
}
2020-05-16 22:02:52 +10:00
onWindowKeyUp(e) {
const key = keyboardnavigation.getKeyName(e);
2020-09-01 22:55:08 +09:00
if (!this.loaded) return;
switch (key) {
case 'l':
case 'ArrowRight':
case 'Right':
2020-12-16 01:19:10 -05:00
this.next();
break;
case 'j':
case 'ArrowLeft':
case 'Left':
2020-12-16 01:19:10 -05:00
this.previous();
break;
case 'Escape':
2020-09-01 22:55:08 +09:00
if (this.tocElement) {
// Close table of contents on ESC if it is open
2020-09-01 22:55:08 +09:00
this.tocElement.destroy();
} else {
// Otherwise stop the entire book player
this.stop();
}
break;
2020-05-16 22:02:52 +10:00
}
}
2020-05-16 22:02:52 +10:00
2020-12-16 01:19:10 -05:00
onTouchStart(e) {
if (!this.loaded || !e.touches || e.touches.length === 0) return;
// epubjs stores pages off the screen or something for preloading
// get the modulus of the touch event to account for the increased width
const touchX = e.touches[0].clientX % dom.getWindowSize().innerWidth;
if (touchX < dom.getWindowSize().innerWidth / 2) {
this.previous();
} else {
this.next();
}
}
onDialogClosed() {
this.stop();
}
2020-05-16 22:02:52 +10:00
bindMediaElementEvents() {
2020-09-01 22:55:08 +09:00
const elem = this.mediaElement;
2020-05-19 16:05:44 +10:00
elem.addEventListener('close', this.onDialogClosed, {once: true});
elem.querySelector('#btnBookplayerExit').addEventListener('click', this.onDialogClosed, {once: true});
elem.querySelector('#btnBookplayerToc').addEventListener('click', this.openTableOfContents);
2021-04-25 18:11:52 -04:00
elem.querySelector('#btnBookplayerFullscreen').addEventListener('click', this.toggleFullscreen);
2020-12-16 01:19:10 -05:00
elem.querySelector('#btnBookplayerPrev')?.addEventListener('click', this.previous);
elem.querySelector('#btnBookplayerNext')?.addEventListener('click', this.next);
}
2020-05-19 16:05:44 +10:00
bindEvents() {
this.bindMediaElementEvents();
document.addEventListener('keyup', this.onWindowKeyUp);
2020-12-16 01:19:10 -05:00
this.rendition.on('touchstart', this.onTouchStart);
2020-09-01 22:55:08 +09:00
this.rendition.on('keyup', this.onWindowKeyUp);
}
unbindMediaElementEvents() {
2020-09-01 22:55:08 +09:00
const elem = this.mediaElement;
elem.removeEventListener('close', this.onDialogClosed);
elem.querySelector('#btnBookplayerExit').removeEventListener('click', this.onDialogClosed);
elem.querySelector('#btnBookplayerToc').removeEventListener('click', this.openTableOfContents);
2021-04-25 18:11:52 -04:00
elem.querySelector('#btnBookplayerFullscreen').removeEventListener('click', this.toggleFullscreen)
2020-12-16 01:19:10 -05:00
elem.querySelector('#btnBookplayerPrev')?.removeEventListener('click', this.previous);
elem.querySelector('#btnBookplayerNext')?.removeEventListener('click', this.next);
}
unbindEvents() {
2020-09-01 22:55:08 +09:00
if (this.mediaElement) {
this.unbindMediaElementEvents();
}
document.removeEventListener('keyup', this.onWindowKeyUp);
2020-12-16 01:19:10 -05:00
this.rendition?.off('touchstart', this.onTouchStart);
this.rendition?.off('keyup', this.onWindowKeyUp);
}
openTableOfContents() {
2020-09-01 22:55:08 +09:00
if (this.loaded) {
this.tocElement = new TableOfContents(this);
}
}
2021-04-25 18:11:52 -04:00
toggleFullscreen() {
if (Screenfull.isEnabled) {
const icon = document.querySelector('#btnBookplayerFullscreen .material-icons');
icon.classList.remove(Screenfull.isFullscreen ? 'fullscreen_exit' : 'fullscreen');
icon.classList.add(Screenfull.isFullscreen ? 'fullscreen' : 'fullscreen_exit');
Screenfull.toggle();
}
}
2020-12-16 01:19:10 -05:00
previous(e) {
e?.preventDefault();
if (this.rendition) {
this.rendition.book.package.metadata.direction === 'rtl' ? this.rendition.next() : this.rendition.prev();
}
}
2020-12-16 01:19:10 -05:00
next(e) {
e?.preventDefault();
if (this.rendition) {
this.rendition.book.package.metadata.direction === 'rtl' ? this.rendition.prev() : this.rendition.next();
}
}
createMediaElement() {
2020-09-01 22:55:08 +09:00
let elem = this.mediaElement;
if (elem) {
return elem;
2020-05-19 16:05:44 +10:00
}
elem = document.getElementById('bookPlayer');
if (!elem) {
elem = dialogHelper.createDialog({
exitAnimationDuration: 400,
size: 'fullscreen',
autoFocus: false,
scrollY: false,
exitAnimation: 'fadeout',
removeOnClose: true
});
2020-09-01 22:55:08 +09:00
elem.id = 'bookPlayer';
2020-11-21 03:34:16 -05:00
elem.innerHTML = translateHtml(html);
2020-05-16 22:02:52 +10:00
dialogHelper.open(elem);
2020-05-16 22:02:52 +10:00
}
2020-09-01 22:55:08 +09:00
this.mediaElement = elem;
return elem;
}
2020-05-16 22:02:52 +10:00
setCurrentSrc(elem, options) {
const item = options.items[0];
2020-09-01 22:55:08 +09:00
this.item = item;
this.streamInfo = {
started: true,
ended: false,
mediaSource: {
Id: item.Id
}
};
const serverId = item.ServerId;
const apiClient = ServerConnections.getApiClient(serverId);
2020-05-16 22:02:52 +10:00
2021-04-25 18:11:52 -04:00
if (!Screenfull.isEnabled) {
document.getElementById("btnBookplayerFullscreen").display = 'none';
}
return new Promise((resolve, reject) => {
2020-07-21 13:25:50 +01:00
import('epubjs').then(({default: epubjs}) => {
const downloadHref = apiClient.getItemDownloadUrl(item.Id);
const book = epubjs(downloadHref, {openAs: 'epub'});
2021-01-28 14:03:26 -05:00
const rendition = book.renderTo('bookPlayerContainer', {
width: '100%',
// Calculate the height of the window because using 100% is not accurate when the dialog is opening
height: document.body.clientHeight,
// TODO: Add option for scrolled-doc
flow: 'paginated'
});
2020-09-01 22:55:08 +09:00
this.currentSrc = downloadHref;
this.rendition = rendition;
return rendition.display().then(() => {
const epubElem = document.querySelector('.epub-container');
epubElem.style.display = 'none';
this.bindEvents();
2020-05-16 22:02:52 +10:00
2020-09-01 22:55:08 +09:00
return this.rendition.book.locations.generate(1024).then(async () => {
if (this.cancellationToken) reject();
const percentageTicks = options.startPositionTicks / 10000000;
if (percentageTicks !== 0.0) {
const resumeLocation = book.locations.cfiFromPercentage(percentageTicks);
await rendition.display(resumeLocation);
}
2020-09-01 22:55:08 +09:00
this.loaded = true;
epubElem.style.display = 'block';
rendition.on('relocated', (locations) => {
2020-09-01 22:55:08 +09:00
this.progress = book.locations.percentageFromCfi(locations.start.cfi);
2020-09-08 02:05:02 -04:00
Events.trigger(this, 'timeupdate');
});
loading.hide();
return resolve();
});
}, () => {
console.error('failed to display epub');
return reject();
2020-05-16 22:02:52 +10:00
});
});
});
}
2020-05-16 22:02:52 +10:00
canPlayMediaType(mediaType) {
2020-05-16 22:02:52 +10:00
return (mediaType || '').toLowerCase() === 'book';
}
2020-05-30 13:00:51 +02:00
canPlayItem(item) {
2020-09-01 22:55:08 +09:00
if (item.Path && item.Path.endsWith('epub')) {
2020-05-30 13:00:51 +02:00
return true;
}
2020-06-06 15:53:47 +09:00
2020-05-30 13:00:51 +02:00
return false;
}
}
2020-05-16 22:02:52 +10:00
export default BookPlayer;