-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
110 lines (87 loc) · 3.21 KB
/
renderer.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
class Book {
constructor() {
this.maxPages = 20
this.currentPage = 1
this.usedPages = 1
this.pageContents = Array(this.maxPages).fill("")
this.pageIndicator = document.getElementById('header')
this.textBox = document.getElementById('textbox')
this.leftArrow = document.getElementById('left')
this.rightArrow = document.getElementById('right')
}
update() {
// Checking to ensure that page counts have not gone out of bounds.
if (this.currentPage < 1)
this.currentPage = 1
else if (this.currentPage > this.maxPages)
this.currentPage = this.maxPages
else if (this.usedPages < this.currentPage)
this.usedPages = this.currentPage
// Hides arrows if they are expected to allow the user to go out of the range of the page counts.
this.leftArrow.style.visibility = (this.currentPage <= 1) ? "hidden" : "visible"
this.rightArrow.style.visibility = (this.currentPage >= this.maxPages) ? "hidden" : "visible"
// Sets the page indicator text to the right numbers
this.pageIndicator.innerText = `Page ${this.currentPage} of ${this.usedPages}`
// Sets the textbox to the content of the page.
this.textBox.innerText = this.pageContents[this.currentPage-1]
}
// Inserts the content of the textbox into the array
savePageContents() {
this.pageContents[this.currentPage-1] = this.textBox.innerText
}
nextPage() {
this.savePageContents()
this.currentPage++
this.update()
}
previousPage() {
this.savePageContents()
this.currentPage--
this.update()
}
save() {
this.savePageContents()
let json = JSON
json.currentPage = this.currentPage
json.usedPages = this.usedPages
json.pageContents = this.pageContents
tools.saveDialog(JSON.stringify(json, null, 2))
}
load() {
let result = tools.loadDialog()
if (Object.keys(result).length != 0) {
this.currentPage = result.currentPage
this.usedPages = result.usedPages
this.pageContents = result.pageContents
this.update()
}
}
}
window.onkeydown = function(evt) {
// Thanks to https://github.com/electron/electron/issues/8793#issuecomment-648307765 - Disables zooming with CTRL+ and CTRL-
if ((evt.code == "Minus" || evt.code == "Equal") && (evt.ctrlKey || evt.metaKey)) {evt.preventDefault()}
}
// Save button
document.getElementById('save').addEventListener('click', function() {
book.save()
}, false)
// Load button
document.getElementById('load').addEventListener('click', function() {
book.load()
}, false)
// Close button
document.getElementById('close').addEventListener('click', function() {
window.close()
}, false)
// Minimize button
document.getElementById('minimize').addEventListener('click', function() {
tools.minimize()
}, false)
const book = new Book()
book.update()
document.getElementById('left').addEventListener('click', function() {
book.previousPage()
}, false)
document.getElementById('right').addEventListener('click', function() {
book.nextPage()
}, false)