-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
context.js
155 lines (138 loc) · 3.74 KB
/
context.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
import React, { Component } from 'react'
// State
const defaultState = {
pages: [],
ready: false,
error: null,
totalPages: 0,
canZoomIn: false,
canZoomOut: false,
fullscreen: false,
isLastPage: false,
isFirstPage: false,
currentPage: null,
currentZoom: null,
renderError: false,
}
// Settings
const defaultSettings = {
theme: 'Light',
language: 'en',
maxPages: 500,
bookMode: false,
mangaMode: false,
forceSort: true,
allowFullScreen: true,
autoHideControls: false,
allowGlobalShortcuts: false,
}
// Default values
const defaultContext = { ...defaultState, ...defaultSettings }
// Create main context
export const ReaderContext = React.createContext()
// Create provider for context
export class ReaderProvider extends Component {
constructor(props) {
super(props)
this.updateState = (data, callback) => {
this.setState(data, callback)
}
this.clear = () => {
this.setState({ ...defaultState })
}
this.toggleSetting = setting => {
this.setState(prevState => ({ [setting]: !prevState[setting] }))
}
this.createPage = page => {
const pages = this.state.pages.concat(page)
if (page.index === 0) {
this.setState({
ready: true,
error: null,
currentPage: 0,
pages,
})
} else {
this.setState({ pages })
}
}
this.trigger = (eventName, data) => {
if (eventName === 'error' && data) {
console.error(data)
this.setState({ ready: false, error: data })
}
}
this.togglePin = () => {
this.setState(prevState => ({
autoHideControls: !prevState.autoHideControls,
}))
}
this.navigateToPage = pageIndex => {
this.setState(prevState => {
const { totalPages } = prevState
const lastIndex = totalPages - 1
// Validate page index
if (pageIndex < 0 || pageIndex > lastIndex) return {}
// Update state
const isLastPage = pageIndex === lastIndex
const isFirstPage = pageIndex === 0
return { isLastPage, isFirstPage, currentPage: pageIndex }
})
}
this.navigateForward = () => {
const { isLastPage, currentPage } = this.state
if (!isLastPage) {
this.navigateToPage(currentPage + 1)
}
}
this.navigateBackward = () => {
const { isFirstPage, currentPage } = this.state
if (!isFirstPage) {
this.navigateToPage(currentPage - 1)
}
}
this.getPage = index => {
const { pages, bookMode } = this.state
const page = pages && pages.length && pages[index]
// Page source exists
if (page) {
const nextIndex = index + 1
const nextPageExists = nextIndex < pages.length
const shouldRenderBookMode = bookMode && nextPageExists && index > 0
// Return two pages:
// Cover (first page) will always render as a single page
if (shouldRenderBookMode) {
const nextPage = pages[nextIndex]
return [page, nextPage]
}
// Return single page
return page
}
// No pages found
return null
}
this.state = {
// Actions
clear: this.clear,
trigger: this.trigger,
getPage: this.getPage,
updateState: this.updateState,
createPage: this.createPage,
toggleSetting: this.toggleSetting,
togglePin: this.togglePin,
navigateToPage: this.navigateToPage,
navigateForward: this.navigateForward,
navigateBackward: this.navigateBackward,
// Options
...defaultContext,
...this.props.externalOptions,
}
}
render() {
return (
<ReaderContext.Provider value={this.state}>
{this.props.children}
</ReaderContext.Provider>
)
}
}