-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
137 lines (111 loc) · 3.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { Component } from 'react';
import ePub from 'epubjs';
import getStreamHost from '../../utils/get-stream-host';
import './Epub.scss';
class Epub extends Component {
constructor(props) {
super(props);
this.book = ePub();
this._visibleLocation = 0;
}
componentWillMount() {
// const { id } = this.props.book;
this.book.open(this.props.src)
.then(() => {
console.log('OPEN');
})
.catch((err) => {
console.error(err);
});
}
componentDidMount() {
this._visibleLocation = this.props.location || 0;
this._start();
}
componentWillUpdate(nextProps) {
if (nextProps.location !== this.props.location) {
this._visibleLocation = nextProps.location;
this.rendition.display(this._visibleLocation);
}
if (nextProps.theme !== this.props.theme) {
this.rendition.themes.apply(nextProps.theme);
}
if (nextProps.fontSize !== this.props.fontSize) {
this.rendition.themes.fontSize(nextProps.fontSize);
}
}
componentWillUnmount() {
this._stop();
}
display(what) {
if (this.rendition) {
this.rendition.display(what);
}
}
_start() {
this.rendition = this.book.renderTo('stage', {
flow: this.props.flow || 'paginated',
minSpreadWidth: 550,
width: '100%',
height: '100%',
manager: ePub.ViewManagers.default,
view: ePub.Views.iframe
});
this.rendition.themes.register(`${getStreamHost()}/static/epub.css`);
this.rendition.themes.apply('book-theme');
this.rendition.display(this._visibleLocation);
this.rendition.on('locationChanged', (visibleLocation) => {
this._visibleLocation = visibleLocation.start;
if (this.props.onLocationChange) {
this.props.onLocationChange(visibleLocation);
}
});
this.book.loaded.navigation.then((nav) => {
if (this.props.onNavigationReady) this.props.onNavigationReady(nav.toc);
});
this.keyListener = (e) => {
// Left Key
if ((e.keyCode || e.which) === 37) {
this.rendition.prev();
}
// Right Key
if ((e.keyCode || e.which) === 39) {
this.rendition.next();
}
};
this.book.ready.then(() => {
if (this.props.onReady) this.props.onReady(this.book);
this.rendition.on('keyup', this.keyListener);
document.addEventListener('keyup', this.keyListener, false);
window.addEventListener('resize', this._onResize.bind(this), false);
});
this.book.loaded.navigation.then((nav) => {
if (this.props.onNavigationReady) this.props.onNavigationReady(nav.toc);
});
}
_stop() {
this.rendition.destroy();
this.rendition.off('keyup', this.keyListener);
document.removeEventListener('keyup', this.keyListener, false);
window.removeEventListener('resize', this._onResize, false);
}
_onResize() {
this._stop();
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(this._start.bind(this), 250);
}
render() {
return (
<div className="epub__container">
<button className="epub__container__page-left" onClick={() => this.rendition.prev()} >
<i className="fa fa-chevron-left" />
</button>
<div id="stage" />
<button className="epub__container__page-right" onClick={() => this.rendition.next()} >
<i className="fa fa-chevron-right" />
</button>
</div>
);
}
}
export default Epub;