-
Notifications
You must be signed in to change notification settings - Fork 1
/
handbook.js
82 lines (70 loc) · 2.91 KB
/
handbook.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
const {ipcRenderer} = require('electron');
const {v4: uuidv4} = require('uuid');
const fs = require('fs');
const path = require('path');
const titleBar = document.getElementById('title-bar');
/* This is a different file from renderer.js and it has no note list or note content */
// Make the titlebar buttons work
const minimizeButton = document.getElementById('min-button');
const maximizeButton = document.getElementById('max-button');
const closeButton = document.getElementById('close-button');
var isMaximized = false;
minimizeButton.addEventListener('click', () => {
ipcRenderer.invoke('minimize-window');
});
maximizeButton.addEventListener('click', () => {
if (isMaximized) {
ipcRenderer.invoke('unmaximize-window');
isMaximized = false;
} else {
ipcRenderer.invoke('maximize-window');
isMaximized = true;
}
});
closeButton.addEventListener('click', () => {
ipcRenderer.invoke('close-window');
});
const backButton = document.getElementById('back-button');
backButton.addEventListener('click', () => {
ipcRenderer.invoke('open-main');
});
const handbook = document.getElementById('handbook');
// When the user clicks on any section, open the SECTIONNAME.md file and render its contents
const contents = document.getElementById('contents');
for (const a of contents.children) {
a.addEventListener('click', () => {
// Use the href attribute to get the file name (also remove the #)
let normalizedName = a.href.split('#')[1];
let file = path.join(__dirname, 'handbook', normalizedName + '.md');
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
console.log(err);
} else {
// markdown-it
let md = window.markdownit();
let result = md.render(data);
handbook.innerHTML = result;
// Replace all [ ] with unchecked checkboxes
handbook.innerHTML = handbook.innerHTML.replaceAll('[ ]', '<input type="checkbox" disabled>');
// Replace all [x] with checked checkboxes
handbook.innerHTML = handbook.innerHTML.replaceAll('[x]', '<input type="checkbox" checked disabled>');
var br = document.createElement('br');
for (i = 0; i < 5; i++) {
handbook.appendChild(br.cloneNode());
}
// Make all links open in the user's default browser
for (const a of handbook.getElementsByTagName('a')) {
if (a.href.substring(0, 4) === 'http') {
a.addEventListener('click', (event) => {
event.preventDefault();
ipcRenderer.invoke('open-link', a.href);
});
}
}
}
});
});
if (a.href.split('#')[1] === 'introduction') {
a.click();
}
}