forked from gerhardsletten/react-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
161 lines (152 loc) · 3.95 KB
/
App.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, { Component } from "react";
import { createGlobalStyle } from "styled-components";
import FileReaderInput from "react-file-reader-input";
import { ReactReader } from "./modules";
import {
Container,
ReaderContainer,
Bar,
LogoWrapper,
Logo,
GenericButton,
CloseIcon,
FontSizeButton,
ButtonWrapper
} from "./Components";
const storage = global.localStorage || null;
const DEMO_URL =
"/react-reader/files/alice.epub";
const DEMO_NAME = "Alice in wonderland";
const GlobalStyle = createGlobalStyle`
* {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
margin: 0;
padding: 0;
color: inherit;
font-size: inherit;
font-weight: 300;
line-height: 1.4;
word-break: break-word;
}
html {
font-size: 62.5%;
}
body {
margin: 0;
padding: 0;
min-height: 100vh;
font-size: 1.8rem;
background: #333;
position: absolute;
height: 100%;
width: 100%;
color: #fff;
}
`;
class App extends Component {
constructor(props) {
super(props);
this.state = {
fullscreen: false,
location:
storage && storage.getItem("epub-location")
? storage.getItem("epub-location")
: 2,
localFile: null,
localName: null,
largeText: false
};
this.rendition = null;
}
toggleFullscreen = () => {
this.setState(
{
fullscreen: !this.state.fullscreen
},
() => {
setTimeout(() => {
const evt = document.createEvent("UIEvents");
evt.initUIEvent("resize", true, false, global, 0);
}, 1000);
}
);
};
onLocationChanged = location => {
this.setState(
{
location
},
() => {
storage && storage.setItem("epub-location", location);
}
);
};
onToggleFontSize = () => {
const nextState = !this.state.largeText;
this.setState(
{
largeText: nextState
},
() => {
this.rendition.themes.fontSize(nextState ? "140%" : "100%");
}
);
};
getRendition = rendition => {
// Set inital font-size, and add a pointer to rendition for later updates
const { largeText } = this.state;
this.rendition = rendition;
rendition.themes.fontSize(largeText ? "140%" : "100%");
};
handleChangeFile = (event, results) => {
if (results.length > 0) {
const [e, file] = results[0];
if (file.type !== "application/epub+zip") {
return alert("Unsupported type");
}
this.setState({
localFile: e.target.result,
localName: file.name,
location: null
});
}
};
render() {
const { fullscreen, location, localFile, localName } = this.state;
return (
<Container>
<GlobalStyle />
<Bar>
<LogoWrapper href="https://github.com/gerhardsletten/react-reader">
<Logo
src="https://gerhardsletten.github.io/react-reader/files/react-reader.svg"
alt="React-reader - powered by epubjs"
/>
</LogoWrapper>
<ButtonWrapper>
<FileReaderInput as="buffer" onChange={this.handleChangeFile}>
<GenericButton>Upload local epub</GenericButton>
</FileReaderInput>
<GenericButton onClick={this.toggleFullscreen}>
Use full browser window
<CloseIcon />
</GenericButton>
</ButtonWrapper>
</Bar>
<ReaderContainer fullscreen={fullscreen}>
<ReactReader
url={localFile || DEMO_URL}
title={localName || DEMO_NAME}
location={location}
locationChanged={this.onLocationChanged}
getRendition={this.getRendition}
/>
<FontSizeButton onClick={this.onToggleFontSize}>
Toggle font-size
</FontSizeButton>
</ReaderContainer>
</Container>
);
}
}
export default App;