-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrv.ts
400 lines (327 loc) · 10.4 KB
/
brv.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// these declared variables are inserted in main.go
declare const lastRead: LastRead | null
declare const nextHref: string
declare const prevHref: string
interface LastRead {
href: string | undefined;
position: number | undefined;
"padding-left": string;
"padding-right": string;
"font-family": string;
"font-size": string;
"line-height": string;
}
// a toc navigation point, for those whose target elem is on current page
interface TocPoint {
anchor: HTMLAnchorElement; // <a> in toc, constant
pos: number; // y-position of target of <a>
}
// customisation option
interface CustomiseOpt {
input: HTMLInputElement; // <input>, constant
cssKey: string; // <body> css property name it controls
originalValue: string; // value set in the epub
setter: (value: string) => string; // how to compute css value from input
}
const customiseOpts: CustomiseOpt[] = [
initCustomiseOpt("brv-left-margin", v => v+"%"),
initCustomiseOpt("brv-right-margin", v => v+"%"),
initCustomiseOpt("brv-font-family", v => v),
initCustomiseOpt("brv-font-size", v => v+"%"),
initCustomiseOpt("brv-line-height", v => v),
]
const boxElem = document.getElementById("brv-box")!
const tocElem = document.getElementById("brv-toc")!
const ciElem = document.getElementById("brv-ci")!
//
// setup
//
// the app box is the toc/customisation/info/about modal
// that floats on top of book pages
// 0: hidden
// 1: toc
// 2: customisation + info + about
let appBoxState = 0
// set document language
if (!document.documentElement.lang) {
setLang()
}
// toc points for this page
// the .pos will change whenever the page resizes or the typography changes
let tocPoints = makeTocPoints()
// use the customisation saved from last read
applyLastRead()
// highlight current point in toc
highlightToc()
// make it respond to keydown
document.addEventListener("keydown", onKeyDown)
// make it respond to scrolling
document.addEventListener("scroll", debounce(onPageShift))
// make it respond to page resize
window.addEventListener("resize", debounce(onPageReformat))
// going to a target on the same page should hide toc
tocPoints.forEach(({anchor}) => {
anchor.addEventListener("click", hideAppBox)
})
// clicking the background should hide box
boxElem.addEventListener("click", event => {
if (event.target == boxElem) {
hideAppBox()
}
})
setupCustomiseControl()
//
// functions
//
function onKeyDown(event: KeyboardEvent) {
if (event.target instanceof Element &&
event.target.tagName.toLowerCase() == "input") {
return
}
switch (event.key) {
case "Escape":
event.preventDefault()
if (appBoxState > 0) {
hideAppBox()
}
break
case " ":
event.preventDefault()
const displayValues = [
["none", "", ""],
["block", "block", "none"],
["block", "none", "block"],
]
// show/hide the box according to appBoxState
appBoxState += event.shiftKey ? 2 : 1
appBoxState %= 3;
[
boxElem.style.display,
tocElem.style.display,
ciElem.style.display,
] = displayValues[appBoxState]
break
case "<":
event.preventDefault()
if (prevHref) {
window.location.pathname = "/" + prevHref
} else {
window.alert("No page before this.")
}
break
case ">":
event.preventDefault()
if (nextHref) {
window.location.pathname = "/" + nextHref
} else {
window.alert("No page behind this.")
}
break
}
}
function hideAppBox() {
boxElem.style.display = "none"
appBoxState = 0
}
// do what's necessary when the page shifts:
// update and highlight current toc point, save position for later
// should be called when page is shifted due to scrolling and more
function onPageShift() {
// re-highlight
const curr = highlightToc()
// update location as page shifts
if (tocPoints.length > 1 && curr) {
const href = curr.href
const hashIdx = href.indexOf("#")
if (hashIdx >= 0) {
// location
const id = href.substring(hashIdx+1)
const elem = document.getElementById(id)
if (elem != null) {
elem.removeAttribute("id")
window.location.hash = "#" + id
elem.id = id
}
// title
document.title = curr.textContent!
}
} else if (tocPoints.length == 1) {
document.title = tocPoints[0].anchor.textContent!
}
// save last read
saveLastRead()
}
function onPageReformat() {
// re-calculate toc target positions
tocPoints = makeTocPoints()
// re-highlight
onPageShift()
}
function setLang() {
const infoElem = document.getElementById("brv-info")!
for (const tr of infoElem.querySelectorAll("tr")) {
const th = tr.children[0]
const td = tr.children[1]
if (th.textContent == "Language") {
document.documentElement.lang = td.textContent!
break
}
}
}
// return the <a> point which is considered currently being read
function currentTocPoint(): HTMLAnchorElement {
// section is being read if its top y-position is past mid
const mid = window.scrollY + window.innerHeight/2
let curr: number
for (curr = 0; curr < tocPoints.length; curr++) {
const tPos = tocPoints[curr].pos
if (tPos > mid) {
break
}
}
curr--
return tocPoints[curr < 0 ? 0 : curr].anchor
}
// highlight (by updating elem class) current point.
// return it if exists.
function highlightToc() : HTMLAnchorElement | null {
if (tocPoints.length == 0) {
return null
}
const a = tocPoints.length == 1 ? tocPoints[0].anchor : currentTocPoint()
const className = "current"
tocPoints.forEach(({anchor}) => {
anchor.parentElement!.classList.remove(className)
})
a.parentElement!.classList.add(className)
return a
}
// set .anchor (always the same on every call).
// set .pos, calculate the position of each toc target on the current page.
function makeTocPoints(): TocPoint[] {
const pageHref = window.location.pathname
const anchors = tocElem.querySelectorAll<HTMLAnchorElement>(`a[href^="${pageHref}"]`)
let points: TocPoint[] = []
anchors.forEach((elem: HTMLAnchorElement) => {
let pos: number
const href = elem.href
const hashIdx = href.indexOf("#")
if (hashIdx < 0) { // base
pos = 0
} else {
const id = href.substring(hashIdx + 1)
const target = document.getElementById(id)
pos = target == null ? 0 : target.offsetTop
}
points.push({anchor: elem, pos})
})
return points
}
function applyConfig() {
customiseOpts.forEach(({input, cssKey, originalValue, setter}) => {
// clean
let inValue = input.value.trim()
if (input.type == "number" && inValue != "") {
const num: number = +inValue
inValue = num.toString()
}
input.value = inValue
// apply
document.body.style[cssKey] = inValue ? setter(inValue) : originalValue
})
onPageReformat()
}
function applyLastRead() {
// scroll to last read
if (lastRead) {
customiseOpts.forEach(({input, cssKey}) => {
input.value = lastRead[cssKey]
})
}
// otherwise, default values in config.html are used
// apply to page
applyConfig()
if (lastRead && lastRead.position) {
window.scrollTo(0, scrollPositionFromLastRead(lastRead.position))
}
}
function setupCustomiseControl() {
const applyBtn = document.getElementById("brv-apply-config")!
const okBtn = document.getElementById("brv-ok-config")!
// setup inputs respond to enter
customiseOpts.forEach(({input}) => {
input.addEventListener("keydown", event => {
if (event.key == "Enter") {
event.preventDefault();
(event.shiftKey ? applyBtn : okBtn).click()
}
})
})
// right margin changes with left if they are the same
const mlElem = customiseOpts[0].input
const mrElem = customiseOpts[1].input
const updateMaster = () => {
mlElem.dataset.master = mlElem.value == mrElem.value ? "1" : "0"
}
updateMaster()
mlElem.addEventListener("input", () => {
if (mlElem.dataset.master == "1") {
mrElem.value = mlElem.value
}
});
[mlElem, mrElem].forEach(elem => {
elem.addEventListener("change", updateMaster)
})
// setup buttons respond to click
applyBtn.addEventListener("click", function() {
applyConfig()
saveLastRead()
})
okBtn.addEventListener("click", function() {
applyConfig()
hideAppBox()
saveLastRead()
})
}
function initCustomiseOpt(id: string, setter: (value: string) => string): CustomiseOpt {
const input = document.getElementById(id) as HTMLInputElement
const cssKey = input.name
const originalValue = document.body.style[cssKey]
return {input, cssKey, originalValue, setter}
}
function saveLastRead() {
let lastRead = {
href: window.location.pathname,
position: readingPosition(),
}
customiseOpts.forEach(({input, cssKey}) => {
lastRead[cssKey] = input.value
})
// send to server
fetch("/save_brv", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(lastRead),
})
}
function readingPosition(): number {
const pos = window.scrollY + window.innerHeight/4
return pos / document.body.clientHeight
}
// the inverse of readingPosition()
function scrollPositionFromLastRead(lastReadPosition: number): number {
const pos = document.body.clientHeight * lastReadPosition
return pos - window.innerHeight/4
}
// return a version of fn that won't be called too often
function debounce(fn: () => void, wait: number = 200): () => void {
let timeout: number;
return function() {
const later = () => {
clearTimeout(timeout)
fn()
}
clearTimeout(timeout)
timeout = setTimeout(later, wait)
}
}