-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslides.js
455 lines (386 loc) · 13.5 KB
/
slides.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
document.addEventListener("DOMContentLoaded", function () {
const article = document.querySelector("article");
markdownToSlides(article);
const navList = document.querySelector("nav ul");
const sections = document.querySelectorAll("section");
createMinis(navList, sections);
let activeSection = sections[0];
let manualActivation = false; // Manual activation flag to prevent conflicts during scrolling
function keepInView() {
manualActivation = true;
let times = [];
for (let i = 0; i < 500; i += 20) {
times.push(i);
}
function activeWithScroll() {
activateSection(activeSection, true);
}
for (let ms of times) {
setTimeout(activeWithScroll, ms);
}
let lastMs = times[times.length - 1];
setTimeout(function () {
manualActivation = false;
}, lastMs);
}
function toggleNav() {
manualActivation = true;
const nav = document.querySelector("nav");
const aside = document.querySelector("aside");
nav.classList.toggle("collapsed");
aside.classList.toggle("collapsed");
keepInView();
}
// Initialize the page
let [sectionIndex, segmentNumber] = parseHash();
initSections(sections, sectionIndex, segmentNumber);
adjustBottomPadding(); // Ensure bottom padding is correct on load
setupScrollActivation(); // Set up scroll-based section activation
handleHashChange(); // Activate based on the initial hash
window.addEventListener("hashchange", handleHashChange); // Handle hash changes
document.addEventListener("keydown", function (ev) {
console.log("Global event:", ev.key, ev.code);
if (ev.key === "Escape") {
toggleNav();
return;
}
let isModifierPressed =
ev.shiftKey || ev.ctrlKey || ev.altKey || ev.metaKey;
if (isModifierPressed) {
return;
}
if (ev.key === "n") {
toggleNav();
return;
}
if (ev.key === "f") {
manualActivation = true;
if (document.fullscreenElement) {
document.exitFullscreen();
} else {
document.documentElement.requestFullscreen();
}
keepInView();
toggleNav();
return;
}
});
// ======================== COMPONENTS ========================== //
// Component 0: Markdown to Slides
function markdownToSlides(article) {
const slides = [];
console.log("First Slide");
let currentSlide = null;
let currentCount = 0;
let nodeList = Array.from(article.childNodes);
for (let node of nodeList) {
if (!currentSlide) {
console.log("Next Slide");
currentCount = 0;
currentSlide = document.createElement("section");
slides.push(currentSlide);
}
if (node.tagName === "HR") {
// When an <hr> is encountered, finalize the current slide
if (currentCount === 0) {
currentSlide.textContent = "(this slide unintentionally left blank)";
}
currentSlide = null;
continue;
}
// move the node to the current slide
console.log(" section child", node);
currentSlide.appendChild(node);
let text = node.textContent.trim();
if (text.length > 0) {
currentCount += 1;
}
}
if (currentSlide) {
if (currentCount === 0) {
currentSlide.textContent = "(this slide unintentionally left blank)";
}
}
article.textContent = "";
slides.forEach((slide) => {
article.appendChild(slide);
});
}
function createMinis(navList, sections) {
sections.forEach(function (section, index) {
let sectionHeader = `Untitled (${index})`;
for (let child of section.childNodes) {
let text = child.textContent.trim();
if (text) {
sectionHeader = text;
break;
}
}
// Create an <ol> element for navigation
const navItem = document.createElement("ol");
navItem.textContent = sectionHeader;
navItem.onclick = function () {
activateSectionManual(sections[index]);
};
// Clone the section for the miniature and adjust for miniature display
const miniatureSection = document.createElement("div");
miniatureSection.classList.add("miniature");
const miniatureContent = section.cloneNode(true);
miniatureContent.classList.add("miniature-content");
miniatureSection.appendChild(miniatureContent);
// Add the miniature section to the nav item
navItem.appendChild(miniatureSection);
// Add the nav item to the list
navList.appendChild(navItem);
});
}
// Component 1: Section Management
function initSections(sections, sectionIndex, segmentNumber) {
sections.forEach(function (section, i) {
let currentHighlight = 0;
if (sectionIndex === i) {
currentHighlight = segmentNumber;
}
setupCarousel(section, currentHighlight);
});
setupGlobalKeyListeners(); // Handle up/down/space key navigation
setupResizeListener(); // Add listener for window resizing
}
function activateSectionManual(section) {
manualActivation = true;
activateSection(section, true); // Scroll into view when clicked
updateHash(section); // Update hash based on selection
}
function activateSection(section, shouldScroll) {
if (activeSection) {
activeSection.classList.remove("active");
}
activeSection = section;
activeSection.classList.add("active");
// Scroll into view if required (but only for manual activation, not scroll activation)
if (shouldScroll) {
activeSection.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
}
// Component 2: Carousel Navigation (dimming/focusing lines, arrow key navigation)
function setupCarousel(section, sectionNumber = 0) {
const carousel = section.querySelector("carousel");
if (!carousel) {
return;
}
const $pre = section.querySelector("pre");
const $code = $pre.querySelector("code");
const startCountAt = Number(carousel.getAttribute("data-line-start")) || 1;
const carouselSegemnts = carousel.getAttribute("data-slides").split(/[|;]/);
Prism.highlightAll();
wrapLinesWithNumbers($pre, $code, startCountAt);
let currentHighlight = sectionNumber;
// Function to navigate to a specific carousel segment (1-indexed)
section.gotoSegment = function (segmentNumber) {
if (!segmentNumber) {
segmentNumber = 0;
}
console.log("segmentNumber 1:", segmentNumber);
let segmentIndex = segmentNumber - 1;
if (!segmentIndex) {
segmentIndex = 0;
}
console.log("segmentIndex 2:", carouselSegemnts.length, segmentIndex);
let segmentGroup = carouselSegemnts[segmentIndex];
let segmentRanges = parseSlideRanges(segmentGroup);
applyLineFocus($pre, startCountAt, segmentRanges);
};
section.gotoSegment(currentHighlight);
document.addEventListener("keydown", function (e) {
if (section !== activeSection) {
return;
}
if (e.key === "ArrowRight") {
currentHighlight += 1;
} else if (e.key === "ArrowLeft") {
currentHighlight -= 1;
} else {
return;
}
currentHighlight = Math.max(0, currentHighlight);
currentHighlight = Math.min(
carouselSegemnts.length + 1,
currentHighlight,
);
console.log("new segment index", currentHighlight);
if (currentHighlight > 0) {
console.log("arrow right hash");
updateHash(section, currentHighlight);
} else {
console.log("arrow left hash");
updateHash(section);
}
section.gotoSegment(currentHighlight);
});
}
function wrapLinesWithNumbers($pre, $code, startLine) {
startLine -= 1;
let html = $code.innerHTML.trim();
let lines = html.split("\n");
let $ol = document.createElement("ol");
$ol.style.counterReset = `line-number ${startLine}`;
for (let line of lines) {
let $li = document.createElement("li");
$li.innerHTML = line + ` `;
$ol.appendChild($li);
}
$pre.innerHTML = "";
$pre.appendChild($ol);
}
function applyLineFocus(pre, startCountAt, selectedLineNumbers) {
let wild = selectedLineNumbers.length === 0;
const lines = pre.querySelectorAll("ol li");
lines.forEach((line, index) => {
let lineNumber = index + startCountAt;
let selected = wild || selectedLineNumbers.includes(lineNumber);
if (selected) {
line.classList.add("focused");
line.classList.remove("dimmed");
} else {
line.classList.add("dimmed");
line.classList.remove("focused");
}
});
}
function parseSlideRanges(slideRange) {
if (!slideRange) {
return [];
}
slideRange = slideRange.trim();
const ranges = slideRange.split(/[,\s]+/);
console.log("ranges:", `'${slideRange}'`, ranges);
let selectedLines = [];
for (let range of ranges) {
if (range.includes("-")) {
let [start, end] = range.split("-").map(Number);
for (let i = start; i <= end; i += 1) {
selectedLines.push(i);
}
} else {
selectedLines.push(Number(range));
}
}
return selectedLines;
}
// Component 3: Scroll-based Section Activation (activate section on scroll)
function setupScrollActivation() {
window.addEventListener("scroll", function () {
if (manualActivation) {
// Skip scroll-based activation during manual actions
return;
}
const scrollY = window.scrollY;
let newActiveSection = null;
sections.forEach(function (section) {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
// Check if the section is sufficiently in view (using 50% of its height)
if (
scrollY >= sectionTop - sectionHeight * 0.5 &&
scrollY < sectionTop + sectionHeight * 0.5
) {
newActiveSection = section;
}
});
// Activate the new section, but don't scroll to it (shouldScroll = false)
if (newActiveSection && newActiveSection !== activeSection) {
activateSection(newActiveSection, false); // No scrolling when activating via scroll
updateHash(newActiveSection);
}
});
}
// Component 4: Global Key Listeners for Section Navigation (up, down, spacebar)
function setupGlobalKeyListeners() {
let nextKeys = [/*"ArrowDown",*/ " "];
let prevKeys = [/*"ArrowUp",*/ "Backspace", "Delete"];
let specialKeys = [].concat(nextKeys, prevKeys);
window.addEventListener("keydown", function (e) {
let isSpecialKey = specialKeys.includes(e.key);
if (isSpecialKey) {
e.preventDefault(); // Disable default scroll behavior
}
let currentIndex = Array.from(sections).indexOf(activeSection);
let isNextKey = nextKeys.includes(e.key);
if (isNextKey) {
currentIndex += 1;
if (currentIndex >= sections.length) {
currentIndex = sections.length - 1;
}
activateSectionManual(sections[currentIndex]);
return;
}
let isPrevKey = prevKeys.includes(e.key);
if (isPrevKey) {
currentIndex -= 1;
if (currentIndex < 0) {
currentIndex = 0;
}
activateSectionManual(sections[currentIndex]);
return;
}
});
}
// Component 5: Dynamic Padding for Final Section
function setupResizeListener() {
window.addEventListener("resize", adjustBottomPadding);
}
function adjustBottomPadding() {
let dynamicPaddingElement = document.querySelector(".dynamic-padding");
if (!dynamicPaddingElement) {
dynamicPaddingElement = document.createElement("div");
dynamicPaddingElement.classList.add("dynamic-padding");
dynamicPaddingElement.style.height = 0;
article.appendChild(dynamicPaddingElement);
}
const lastSection = sections[sections.length - 1];
const viewportHeight = window.innerHeight;
const sectionHeight = lastSection.offsetHeight;
// Calculate the required padding to align the last section's title at the top
const neededPadding = Math.max(0, viewportHeight - sectionHeight);
dynamicPaddingElement.style.height = `${neededPadding}px`;
}
// Component 6: Hash-Based Routing (section and carousel navigation)
function handleHashChange() {
let [sectionIndex, segmentNumber] = parseHash();
hastToState(sectionIndex, segmentNumber);
}
function parseHash() {
let hash = window.location.hash.substring(1); // Get hash without '#'
console.log("###### the hash change", hash);
let [sectionNumber, segmentNumber] = hash.split(".").map(Number);
// Handle section selection (clamp to valid range)
let lastOrCurrentSection = Math.min(
sections.length - 1,
(sectionNumber || 1) - 1,
);
let sectionIndex = Math.max(0, lastOrCurrentSection);
console.log("hashChange", sectionNumber, segmentNumber);
return [sectionIndex, segmentNumber];
}
function hastToState(sectionIndex, segmentNumber) {
let targetSection = sections[sectionIndex];
activateSection(targetSection, true);
// updateHash(newActiveSection);
if (targetSection.gotoSegment) {
targetSection.gotoSegment(segmentNumber);
}
}
// Component 7: Update Hash without Triggering Hashchange
function updateHash(section, slideNumber = 0) {
let _sections = Array.from(sections);
let sectionIndex = 1 + _sections.indexOf(section); // 1-indexed
let hash = `#${sectionIndex}`;
if (slideNumber) {
hash = `${hash}.${slideNumber}`;
}
history.replaceState(null, null, hash); // Update hash without triggering hashchange
}
});