-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
541 lines (462 loc) Β· 14 KB
/
index.html
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<html>
<style>
div,
span,
form,
input {
padding: 0;
margin: 0;
}
body {
font-family: Helvetica, sans-serif;
font-size: 30px;
padding: 0;
margin: 0;
}
#slide-content {
max-width: 100vw;
margin: 0 30px;
margin-top: 30px;
}
#slide-content img:first-of-type,
#menu-controls {
margin-top: -30px; /* undo the margin-top of #slide-content */
}
#menu {
color: #ccc;
padding: none;
padding: 5px 30px 5px 30px;
font-size: 12px;
border-bottom: 1px solid #efefef;
z-index: 99;
position: absolute;
background: #ffffff;
width: 100%;
}
#menu-controls {
display: flex;
flex-direction: row;
align-items: center;
}
/* flex spacing */
#help-icon,
#url-input,
input[type='submit'] {
margin-right: 10px;
}
#menu-controls form {
flex-grow: 1;
/* everything else is 0 */
}
#help-icon {
height: 12px;
width: 12px;
font-size: 12px;
line-height: 12px;
border-style: 1px solid #ccc;
border-radius: 50%;
padding: 1px;
text-align: center;
border-radius: 50%;
display: inline-block;
color: #ccc;
font-weight: 100;
border: 1px solid #ccc;
display: inline-block;
cursor: pointer;
}
#help-info {
font-size: medium;
}
img {
/* constrain by either height or width, whatever hits the edge first */
max-width: 100%;
max-height: 100vh;
width: auto;
height: auto;
}
p > em {
/* hide the image labels */
display: none;
}
form {
border: none;
margin: 0;
padding: 0;
display: flex;
}
input {
border: 1px solid #ccc;
background-color: #fff;
padding: none;
margin: none;
font-size: 12px;
color: #ccc;
padding: 2px 4px;
display: inline-block;
}
#url-input {
color: #bbb;
flex-grow: 1;
}
#slide-index {
font-size: 12px;
}
</style>
<script src="https://unpkg.com/commonmark@0.29.3/dist/commonmark.js "></script>
<script>
/* globals commonmark */
function loadMarkdown(url) {}
class SimpleSlider {
static responseCache = {};
static DEFAULT_URL =
'https://gist.githubusercontent.com/kortina/66d1263fe538b30f4a489d8c97faa140/raw/c56f514a0c1eabe546537b59ba02e6fba8e399d1/very-simple-slide-example.md';
static USE_TEST_MD = false;
static TEST_MD = `
# TEST
![hendrix](https://kortina.s3.amazonaws.com/_/git/sofia/eternal-apocalypse/hendrix-1.jpg)
The speaker notes for slide numero uno. Images render as slides, text as the speaker notes.
*Machine Gun.*
![winehouse](https://kortina.s3.amazonaws.com/_/git/sofia/eternal-apocalypse/winehouse-1.jpg)
I like to write this way, even for essays these days, with an image on screen for each section.
![cobain](https://kortina.s3.amazonaws.com/_/git/sofia/eternal-apocalypse/cobain-1.jpg)
Better to burn out than fade away, said this man.
Sometimes when I'm editing, there will be several paragraphs where I don't want the image to scroll off the top of the screen.
# An h1 heading will also render as a slide.
## Another h1 or h2 or h3 creates a new slide.
#### h4, h5, and h6 will render as part of the same slide.
##### h5, and h6 will render as part of the same slide.
###### h6 will render as part of the same slide.
---
###### A horizontal rule (\`---\`) will force a new slide.
# Want to use an iframe embed? Just put it in an h1...
# <iframe width="560" height="315" src="https://www.youtube.com/embed/UcU04NGMQb8" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
# That's about it.
`;
constructor(markdownRaw) {
this.markdownRaw = markdownRaw;
this.slides = [];
this._currentSlide = null;
}
static qsURL() {
var p = new URLSearchParams(document.location.search);
return p.get('url');
}
static initURL() {
var u = this.qsURL() || this.DEFAULT_URL;
var el = document.getElementById('url-input');
if (u && !el.value) {
el.value = u;
}
}
static formSubmit(event) {
console.log(`formSubmit: ${this.formValue()}`);
var url = this.formValue();
if (`${url}` != '') {
var qsURL = this.qsURL();
if (qsURL == url) {
console.warning(`url already loaded`);
} else {
var l = document.location;
var e = encodeURIComponent(url);
var n = `${l.protocol}//${l.host}${l.pathname}?url=${e}`;
console.log(n);
document.location = n;
}
}
return false;
}
static formValue() {
var el = document.getElementById('url-input');
if (el) {
return el.value;
}
}
spawnSlide() {
if (this._currentSlide) {
this.slides.push(this._currentSlide);
}
this._currentSlide = {
slide: new commonmark.Node('document'),
speakerNotes: new commonmark.Node('document'),
};
}
static fetchMarkdown() {
this.fetchMarkdownFromURL(this.formValue());
}
static fetchMarkdownFromURL(url) {
var markdown = this.USE_TEST_MD ? this.TEST_MD : this.responseCache[url];
if (markdown) {
this.onFetch(markdown);
} else {
fetch(url)
.then((response) => response.text())
.then((result) => {
this.responseCache[url] = result;
this.onFetch(result);
});
}
}
static onFetch(result) {
window.VSS = new SimpleSlider(result);
window.VSS.parseAndRenderMarkdown(result);
}
static removeFrontmatter(markdownRaw) {
window.markdownRaw = markdownRaw;
console.log(markdownRaw);
if (markdownRaw.startsWith('---')) {
var lines = markdownRaw.split('\n');
var i = 1;
while (lines[i] != '---') {
i++;
}
return lines.slice(i + 1).join('\n');
}
return markdownRaw;
}
parseMarkdown(markdownRaw) {
var reader = new commonmark.Parser();
markdownRaw = SimpleSlider.removeFrontmatter(markdownRaw);
var parsed = reader.parse(markdownRaw);
window.PARSED = parsed;
var walker = parsed.walker();
var event, node;
var slide = new commonmark.Node('document');
var speakerNotes = new commonmark.Node('document');
var spawns = false;
while ((event = walker.next())) {
node = event.node;
this.prefetchImage(node, event);
// we only care about direct descendants of root `document`
if (!childOfRoot(node)) {
continue;
}
spawns = spawns || spawnsNewSlide(node, event);
// Make sure we only visit nodes once.
// NB: this will break if we flip / do NO handle nodes on-entering:
if (event.entering) {
// code_block doesn't seem to have the non-entering event
if (node.type != 'code_block') {
continue;
}
}
// console.log(node.type, {
// type: node.type,
// childOfRoot: childOfRoot(node),
// firstChildType: firstChildType(node),
// spawns: spawns,
// entering: event.entering,
// iH: node.innerHTML,
// });
var contentNode = this.cloneByTakingChildren(node);
node.unlink();
if (spawns) {
this.spawnSlide();
// reset spawns:
spawns = false;
}
this.addContentToCurrentSlide(contentNode);
}
this.spawnSlide();
// this.slides.push({slide: slide, speakerNotes: speakerNotes});
}
parseAndRenderMarkdown(markdownRaw) {
// TODO: display slide numbers
// TODO: speaker notes window: https://stackoverflow.com/questions/5277482/can-i-control-two-browser-windows-with-one-html5-app
this.parseMarkdown(markdownRaw);
window.SLIDES = this.slides;
moveTo(currentIndex());
}
addContentToCurrentSlide(contentNode) {
if (!this._currentSlide) {
this.spawnSlide();
}
if (rendersOnSlide(contentNode)) {
this._currentSlide.slide.appendChild(contentNode);
} else {
this._currentSlide.speakerNotes.appendChild(contentNode);
}
}
prefetchImage(node, event) {
if (event.entering && node.type == 'image' && node.destination) {
var img = new Image();
img.src = node.destination;
}
}
cloneByTakingChildren(node) {
var nn = new commonmark.Node(node.type);
nn.level = node.level;
while (node.firstChild) {
nn.appendChild(node.firstChild);
}
nn.literal = node.literal;
return nn;
}
}
function firstChildType(node) {
return node.firstChild && node.firstChild.type;
}
function rendersOnSlide(node) {
var fcType = firstChildType(node);
if (fcType == 'image') {
return true;
}
if (node.type == 'heading') {
return true;
}
return false;
}
function spawnsNewSlide(node, event) {
// Only spawn new slides when not entering,
// except for thematic_break, which has no non-entering event.
if (event.entering) {
return node.type == 'thematic_break';
}
var fcType = firstChildType(node);
if (fcType == 'image') {
return true;
}
if (node.type == 'heading' && node.level <= 3) {
return true;
}
return false;
}
function childOfRoot(node) {
return node.parent && node.parent.parent === null ? true : false;
}
function renderSlideIndex() {
if (!window.SLIDES) {
return;
}
var el = document.getElementById('slide-index');
el.innerHTML = `${this.currentIndex()} / ${window.SLIDES.length - 1}`;
}
function renderIndex(i) {
// TODO: update window.SLIDES to none if we load invalid URL?
if (!window.SLIDES || window.SLIDES.length < i) {
return;
}
this.renderSlideIndex();
var writer = new commonmark.HtmlRenderer();
var s = window.SLIDES[i];
document.getElementById('slide-content').innerHTML = writer.render(s.slide);
renderSpeakerNotes(i);
}
function onDocReady(fn) {
// see if DOM is already available
if (document.readyState === 'complete' || document.readyState === 'interactive') {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
function moveTo(i) {
document.location.hash = `#${i}`;
renderIndex(i);
}
function currentIndex() {
var currentIndexStr = document.location.hash || '#0';
return parseInt(currentIndexStr.replace('#', ''), 10);
}
function moveLeft() {
moveTo(Math.max(currentIndex() - 1, 0));
}
function moveRight() {
var max = window.SLIDES ? window.SLIDES.length - 1 : 0;
moveTo(Math.min(currentIndex() + 1, max));
}
function onKey(e) {
if (e.target.nodeName == 'INPUT') {
return;
}
switch (e.code) {
case 'ArrowLeft':
captureEvent(e);
return moveLeft();
case 'ArrowRight':
captureEvent(e);
return moveRight();
case 'KeyN':
captureEvent(e);
return openSpeakerNotes();
case 'KeyM':
captureEvent(e);
return toggleMenu();
case 'Slash':
if (e.key == '?') {
captureEvent(e);
return toggleHelp();
}
default:
break;
// console.log(`code: ${e.code}`);
}
}
function captureEvent(e) {
if (e.target.nodeName == 'BODY') {
e.preventDefault();
}
}
function openSpeakerNotes() {
window.SN_WINDOW = window.open('slider-notes.html', 'secondary', 'width=600,height=1000');
window.SN_WINDOW.onload = function () {
renderSpeakerNotes(currentIndex());
};
}
function renderSpeakerNotes(i) {
if (window.SN_WINDOW) {
if (!window.SLIDES || window.SLIDES.length < i) {
return;
}
var writer = new commonmark.HtmlRenderer();
var s = window.SLIDES[i];
var html = writer.render(s.speakerNotes);
window.SN_WINDOW.render(html);
} else {
console.log('Speaker notes window is not open.');
}
}
function toggleMenu() {
toggleElementWithId('menu');
}
function toggleHelp() {
toggleElementWithId('help-info');
}
function toggleElementWithId(id) {
var el = document.getElementById(id);
var d = ['', 'none', null, undefined].includes(el.style.display) ? 'block' : 'none';
el.style.display = d;
}
onDocReady(function () {
SimpleSlider.initURL();
SimpleSlider.fetchMarkdown();
document.addEventListener('keydown', onKey); // keyup resulted in arrows still working
document.getElementById('help-icon').addEventListener('click', toggleHelp);
});
</script>
<body>
<div id="menu" style="display: block">
<div id="menu-controls">
<div id="help-icon" title="help">?</div>
<form onsubmit="return SimpleSlider.formSubmit();">
<input id="url-input" type="text" />
<input type="submit" value="Load" />
</form>
<div id="slide-index"></div>
</div>
<div id="help-info" style="display: none">
<pre>
Keyboard Shortcuts
n open speaker notes
β next slide
β prev slide
m show / hide menu
? show / hide help
</pre
>
</div>
</div>
<div id="slide-content"></div>
</body>
</html>