Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
272 lines (208 loc) · 6.71 KB
/
Copy pathapp.js
File metadata and controls
272 lines (208 loc) · 6.71 KB
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
const categoriesView = document.getElementById("categories-view");
const flashcardView = document.getElementById("flashcard-view");
const categoryList = document.getElementById("category-list");
const categoryTitle = document.getElementById("categoryTitle");
const card = document.getElementById("card");
const cardContent = document.getElementById("cardContent");
const prevBtn = document.getElementById("prevBtn");
const nextBtn = document.getElementById("nextBtn");
const backBtn = document.getElementById("backBtn");
const counter = document.getElementById("counter");
let currentCategory = null;
let currentIndex = 0;
let currentCategoryLength = 0;
let showingFront = true;
let FLASHCARDS = {};
/**
* Parses markdown text for the flash-cards, finds the text and code to be displayed on the
* front-side and back-side of the flash-cards, and puts them into a per-catagory dictionary.
*
* @param text Markdown text to be parsed for flash-cards.
* @returns Dictionary indexed by the category containing lists of dictionaries containing the
* text for the front-side stored in the key 'front', code for the front-side stored in
* the key 'frontCode', and code for the back-side stored in the key 'back'.
*/
function parseCards(text) {
const cards = {};
let currentCategory = null;
let currentQuestionCode = [];
let currentQuestion = null;
let currentAnswer = [];
const lines = text.split("\n");
function saveCard() {
if (
currentCategory &&
currentQuestion &&
currentAnswer.length
) {
cards[currentCategory].push({
front: currentQuestion,
frontCode: currentQuestionCode.join("\n"),
back: currentAnswer.join("\n")
});
}
}
for (const line of lines) {
if (line.startsWith("# ")) {
saveCard();
currentCategory = line.substring(2).trim();
cards[currentCategory] = [];
currentQuestionCode = [];
currentQuestion = null;
currentAnswer = [];
continue;
} else if (line.startsWith("## ")) {
saveCard();
currentQuestion = line.substring(3).trim();
currentQuestionCode = [];
currentAnswer = [];
continue;
}
if (currentQuestion) {
if (line.startsWith(">")) {
currentQuestionCode.push(line.substring(1));
currentAnswer.push(line.substring(1));
} else {
currentAnswer.push(line);
}
}
}
saveCard();
return cards;
}
/**
* Builds the category list on the webpage according to the keys of the flashcards found from the
* markdown file. Creates onclick events for each element to open the respective category.
*/
function buildCategoryList() {
categoryList.innerHTML = "";
Object.keys(FLASHCARDS).forEach(cat => {
const li = document.createElement("li");
li.textContent = cat;
li.onclick = () => openCategory(cat);
categoryList.appendChild(li);
});
}
/**
* Generate the flash cards and make them visible on the site.
*/
async function loadCards() {
const response = await fetch("cards.md");
const text = await response.text();
FLASHCARDS = parseCards(text);
buildCategoryList();
}
/**
* Opens the category according to the button which was clicked.
*
* @param cat Category to be opened.
*/
function openCategory(cat) {
currentCategory = cat;
currentIndex = 0;
currentCategoryLength = FLASHCARDS[currentCategory].length;
showingFront = true;
categoryTitle.textContent = cat;
categoriesView.classList.add("hidden");
flashcardView.classList.remove("hidden");
renderCard();
}
/**
* Render the current flash card from the current category and index using the previously parsed
* data in FLASHCARDS.
*/
function renderCard() {
counter.innerText = currentIndex+1 + "/" + currentCategoryLength
const data = FLASHCARDS[currentCategory][currentIndex];
cardContent.innerHTML = "";
if (showingFront) {
if (data.frontCode.length > 0) {
const pre_front = document.createElement("pre");
const code_front = document.createElement("code");
code_front.className = "language-python";
code_front.textContent = data.frontCode;
pre_front.appendChild(code_front);
cardContent.appendChild(pre_front);
hljs.highlightElement(code_front);
}
const h3 = document.createElement("h3");
h3.textContent = data.front;
cardContent.appendChild(h3);
return;
}
const pre = document.createElement("pre");
const code = document.createElement("code");
code.className = "language-python";
code.textContent = data.back;
pre.appendChild(code);
cardContent.appendChild(pre);
hljs.highlightElement(code);
}
/**
* Flips the current flashcard by rendering the other side.
*/
function flipCard() {
showingFront = !showingFront;
renderCard();
}
/**
* Go to the next card by increasing the index and rendering its front-side.
*/
function nextCard() {
currentIndex = (currentIndex + 1) % currentCategoryLength;
showingFront = true;
renderCard();
}
/**
* Go to the previous card by increasing the index and rendering its back-side.
*/
function previousCard() {
currentIndex = (currentIndex - 1 + currentCategoryLength) % currentCategoryLength;
showingFront = true;
renderCard();
}
/**
* Go back to the categories list.
*/
function exitCategory() {
flashcardView.classList.add("hidden");
categoriesView.classList.remove("hidden");
}
/**
* Find which key was pressed in keydown event and change the flashcard accordingly.
*
* @param e keydown event.
*/
function keyDownHandler(e) {
switch (e.key) {
case "ArrowLeft":
previousCard();
break;
case "ArrowRight":
nextCard();
break;
case "ArrowUp":
flipCard();
break;
case "ArrowDown":
flipCard();
break;
case " ":
flipCard();
break;
case "Escape":
exitCategory();
break;
default:
break;
}
}
// add click events to buttons and cards to control the flash cards
card.onclick = () => {flipCard()};
nextBtn.onclick = () => {nextCard()};
prevBtn.onclick = () => {previousCard()};
backBtn.onclick = () => {exitCategory()};
// add event listener to be able to control the flash cards using the arrow keys
document.addEventListener("keydown", keyDownHandler);
// load cards into webpage
loadCards();