/
sidenotes.js
1152 lines (966 loc) · 42.3 KB
/
sidenotes.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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sidenotes.js: standalone JS library for parsing HTML documents with
Pandoc-style footnotes and dynamically repositioning them into the
left/right margins, when browser windows are wide enough.
Sidenotes (see https://gwern.net/Sidenotes ) are superior to footnotes where
possible because they enable the reader to immediately look at them without
requiring user action to “go to” or “pop up” the footnotes; even floating
footnotes require effort by the reader.
sidenotes.js is inspired by the Tufte-CSS sidenotes
(https://edwardtufte.github.io/tufte-css/#sidenotes), but where Tufte-CSS
uses static footnotes inlined into the body of the page (requiring
modifications to Pandoc’s compilation), which doesn’t always work well for
particularly long or frequent sidenotes, sidenotes.js will rearrange
sidenotes to fit as best as possible, and will respond to window changes.
Particularly long sidenotes are also partially “collapsed”. Styling
(especially for oversized-sidenotes which must scroll) is done in
/static/css/default.css “SIDENOTES” section.
Author: Said Achmiz
2019-03-11
license: MIT (derivative of footnotes.js, which is PD)
*/
/*****************/
/* Configuration.
*/
Sidenotes = {
/* The `sidenoteSpacing` constant defines the minimum vertical space that
is permitted between adjacent sidenotes; any less, and they are
considered to be overlapping.
*/
sidenoteSpacing: 60.0,
/* This includes the border width.
*/
sidenotePadding: 13.0,
/* Elements which occupy (partially or fully) the sidenote columns, and
which can thus collide with sidenotes.
*/
potentiallyOverlappingElementsSelectors: [
".width-full img",
".width-full video",
".width-full .caption-wrapper",
".width-full table",
".width-full pre",
".marginnote"
],
constrainMarginNotesWithinSelectors: [
".backlink-context",
".margin-notes-block"
],
/* The smallest width (in CSS dimensions) at which sidenotes will be shown.
If the viewport is narrower than this, then sidenotes are disabled.
*/
minimumViewportWidthForSidenotes: "1761px",
useLeftColumn: () => false,
useRightColumn: () => true
};
/******************/
/* Implementation.
*/
Sidenotes = { ...Sidenotes,
/* Media query objects (for checking and attaching listeners).
*/
mediaQueries: {
viewportWidthBreakpoint: matchMedia(`(min-width: ${Sidenotes.minimumViewportWidthForSidenotes})`)
},
/*****************/
/* Infrastructure.
*/
sidenotes: null,
citations: null,
sidenoteColumnLeft: null,
sidenoteColumnRight: null,
hiddenSidenoteStorage: null,
positionUpdateQueued: false,
sidenoteOfNumber: (number) => {
return (Sidenotes.sidenotes.find(sidenote => Notes.noteNumberFromHash(sidenote.id) == number) ?? null);
},
citationOfNumber: (number) => {
return (Sidenotes.citations.find(citation => Notes.noteNumberFromHash(citation.id) == number) ?? null);
},
/* The sidenote of the same number as the given citation;
or, the citation of the same number as the given sidenote.
*/
counterpart: (element) => {
let number = Notes.noteNumberFromHash(element.id);
let counterpart = (element.classList.contains("sidenote")
? Sidenotes.citationOfNumber(number)
: Sidenotes.sidenoteOfNumber(number));
if (counterpart == null)
GWLog(`Counterpart of ${element.tagName}#${element.id}.${(Array.from(element.classList).join("."))} not found!`, "sidenotes.js", 0);
return counterpart;
},
/* The “target counterpart” is the element associated with the target, i.e.:
if the URL hash targets a footnote reference, its counterpart is the
sidenote for that citation; and vice-versa, if the hash targets a sidenote,
its counterpart is the in-text citation. We want a target counterpart to be
highlighted along with the target itself; therefore we apply a special
‘targeted’ class to the target counterpart.
*/
updateTargetCounterpart: () => {
GWLog("Sidenotes.updateTargetCounterpart", "sidenotes.js", 1);
if (Sidenotes.mediaQueries.viewportWidthBreakpoint.matches == false)
return;
// Clear existing targeting.
let targetedElementSelector = [
".footnote-ref",
".footnote",
".sidenote"
].map(x => x + ".targeted").join(", ");
document.querySelectorAll(targetedElementSelector).forEach(element => {
element.classList.remove("targeted");
});
// Identify target and counterpart, if any.
let target = location.hash.match(/^#(sn|fnref)[0-9]+$/)
? getHashTargetedElement()
: null;
if (target == null)
return;
let counterpart = Sidenotes.counterpart(target);
// Mark the target and the counterpart, if any.
if (target)
target.classList.add("targeted");
if (counterpart)
counterpart.classList.add("targeted");
},
/* Set margin notes to ‘inline’ or ‘sidenote’ style, depending on what mode
the page is in (based on viewport width), whether each margin note is
in a constrained block, and whether it’s on the main page or in
something like a pop-frame.
(This function should be called from a load or inject event handler,
and the event info passed to it as argument.)
*/
setMarginNoteStyle: (eventInfo) => {
GWLog("Sidenotes.setMarginNoteStyle", "sidenotes.js", 1);
eventInfo.container.querySelectorAll(".marginnote").forEach(marginNote => {
let inline = ( marginNote.closest(Sidenotes.constrainMarginNotesWithinSelectors.join(", "))
|| Sidenotes.mediaQueries.viewportWidthBreakpoint.matches == false
|| eventInfo.document != document);
marginNote.swapClasses([ "inline", "sidenote" ], (inline ? 0 : 1));
});
},
/* Hide sidenotes within currently-collapsed collapse blocks. Show
sidenotes not within currently-collapsed collapse blocks.
*/
updateSidenotesInCollapseBlocks: () => {
GWLog("Sidenotes.updateSidenotesInCollapseBlocks", "sidenotes.js", 1);
Sidenotes.sidenotes.forEach(sidenote => {
let citation = Sidenotes.counterpart(sidenote);
sidenote.classList.toggle("hidden", isWithinCollapsedBlock(citation));
});
},
/* Queues a sidenote position update on the next available animation frame,
if an update is not already queued.
*/
updateSidenotePositionsIfNeeded: () => {
if (Sidenotes.positionUpdateQueued)
return;
Sidenotes.positionUpdateQueued = true;
requestAnimationFrame(() => {
Sidenotes.positionUpdateQueued = false;
Sidenotes.updateSidenotePositions();
});
},
/* This function actually calculates and sets the positions of all sidenotes.
*/
updateSidenotePositions: () => {
GWLog("Sidenotes.updateSidenotePositions", "sidenotes.js", 1);
/* If we’re in footnotes mode (ie. the viewport is too narrow), then
don’t do anything.
*/
if (Sidenotes.mediaQueries.viewportWidthBreakpoint.matches == false)
return;
// Update the disposition of sidenotes within collapse blocks.
Sidenotes.updateSidenotesInCollapseBlocks();
// Check for cut-off sidenotes.
Sidenotes.sidenotes.forEach(sidenote => {
/* Check whether the sidenote is currently hidden (i.e., within a
currently-collapsed collapse block or similar). If so, skip it.
*/
if (sidenote.classList.contains("hidden")) {
Sidenotes.hiddenSidenoteStorage.append(sidenote);
return;
}
// On which side should the sidenote go?
let sidenoteNumber = Notes.noteNumberFromHash(sidenote.id);
let side = null;
if ( Sidenotes.useLeftColumn() == true
&& Sidenotes.useRightColumn() == false) {
// Left.
side = Sidenotes.sidenoteColumnLeft;
} else if ( Sidenotes.useLeftColumn() == false
&& Sidenotes.useRightColumn() == true) {
// Right.
side = Sidenotes.sidenoteColumnRight;
} else if ( Sidenotes.useLeftColumn() == true
&& Sidenotes.useRightColumn() == true) {
// Odd - right; even - left.
side = (sidenoteNumber % 2
? Sidenotes.sidenoteColumnLeft
: Sidenotes.sidenoteColumnRight);
}
// Inject the sidenote into the column (provisionally).
side.append(sidenote);
/* Mark sidenotes which are cut off vertically.
*/
let sidenoteOuterWrapper = sidenote.firstElementChild;
sidenote.classList.toggle("cut-off", (sidenoteOuterWrapper.scrollHeight > sidenoteOuterWrapper.offsetHeight + 2));
});
/* Determine proscribed vertical ranges (ie. bands of the page from which
sidenotes are excluded, by the presence of, eg. a full-width table).
*/
let leftColumnBoundingRect = Sidenotes.sidenoteColumnLeft.getBoundingClientRect();
let rightColumnBoundingRect = Sidenotes.sidenoteColumnRight.getBoundingClientRect();
/* Examine all potentially overlapping elements (ie. non-sidenote
elements that may appear in, or extend into, the side columns).
*/
let proscribedVerticalRangesLeft = [ ];
let proscribedVerticalRangesRight = [ ];
document.querySelectorAll(Sidenotes.potentiallyOverlappingElementsSelectors.join(", ")).forEach(potentiallyOverlappingElement => {
if (isWithinCollapsedBlock(potentiallyOverlappingElement))
return;
let elementBoundingRect = potentiallyOverlappingElement.getBoundingClientRect();
if (!( elementBoundingRect.left > leftColumnBoundingRect.right
|| elementBoundingRect.right < leftColumnBoundingRect.left))
proscribedVerticalRangesLeft.push({ top: (elementBoundingRect.top - Sidenotes.sidenoteSpacing) - leftColumnBoundingRect.top,
bottom: (elementBoundingRect.bottom + Sidenotes.sidenoteSpacing) - leftColumnBoundingRect.top,
element: potentiallyOverlappingElement });
if (!( elementBoundingRect.left > rightColumnBoundingRect.right
|| elementBoundingRect.right < rightColumnBoundingRect.left))
proscribedVerticalRangesRight.push({ top: (elementBoundingRect.top - Sidenotes.sidenoteSpacing) - rightColumnBoundingRect.top,
bottom: (elementBoundingRect.bottom + Sidenotes.sidenoteSpacing) - rightColumnBoundingRect.top,
element: potentiallyOverlappingElement });
});
// The bottom edges of each column are also “proscribed vertical ranges”.
proscribedVerticalRangesLeft.push({
top: Sidenotes.sidenoteColumnLeft.clientHeight,
bottom: Sidenotes.sidenoteColumnLeft.clientHeight
});
proscribedVerticalRangesRight.push({
top: Sidenotes.sidenoteColumnRight.clientHeight,
bottom: Sidenotes.sidenoteColumnRight.clientHeight
});
// Sort and merge.
[ proscribedVerticalRangesLeft, proscribedVerticalRangesRight ].forEach(ranges => {
ranges.sort((rangeA, rangeB) => {
return (rangeA.top - rangeB.top);
});
for (let i = 0; i < ranges.length - 1; i++) {
let thisRange = ranges[i];
let nextRange = ranges[i + 1];
if (nextRange.top <= thisRange.bottom) {
thisRange.bottom = nextRange.bottom;
ranges.splice(i + 1, 1);
i++;
}
}
});
/* Remove sidenotes from page, so that we can set their positions
without causing reflow. Store their layout heights (which cannot
be retrieved in the normal way while the sidenotes aren’t part of
the DOM).
*/
Sidenotes.sidenotes.forEach(sidenote => {
sidenote.lastKnownHeight = sidenote.offsetHeight;
sidenote.remove();
});
// Clean up old layout cells, if any.
[ Sidenotes.sidenoteColumnLeft, Sidenotes.sidenoteColumnRight ].forEach(column => {
column.querySelectorAll(".sidenote-layout-cell").forEach(cell => cell.remove());
});
// Construct new layout cells.
let layoutCells = [ ];
let sides = [ ];
if (Sidenotes.useLeftColumn())
sides.push([ Sidenotes.sidenoteColumnLeft, leftColumnBoundingRect, proscribedVerticalRangesLeft ]);
if (Sidenotes.useRightColumn())
sides.push([ Sidenotes.sidenoteColumnRight, rightColumnBoundingRect, proscribedVerticalRangesRight ]);
sides.forEach(side => {
let [ column, rect, ranges ] = side;
let prevRangeBottom = 0;
ranges.forEach(range => {
let cell = newElement("DIV", {
"class": "sidenote-layout-cell"
}, {
"sidenotes": [ ],
"container": column,
"room": (range.top - prevRangeBottom),
"style": `top: ${prevRangeBottom + "px"}; height: ${(range.top - prevRangeBottom) + "px"}`
});
column.append(cell);
cell.rect = cell.getBoundingClientRect();
layoutCells.push(cell);
prevRangeBottom = range.bottom;
});
});
/* Default position for a sidenote within a layout cell is vertically
aligned with the footnote reference, or else at the top of the
cell, whichever is lower.
*/
let defaultNotePosInCellForCitation = (cell, citation) => {
return Math.max(0, Math.round((citation.getBoundingClientRect().top - cell.rect.top) + 4));
};
// Assign sidenotes to layout cells.
for (citation of Sidenotes.citations) {
let citationBoundingRect = citation.getBoundingClientRect();
let sidenote = Sidenotes.counterpart(citation);
/* Is this sidenote even displayed? Or is it hidden (i.e., its
citation is within a currently-collapsed collapse block)? If so,
skip it.
*/
if (sidenote.classList.contains("hidden")) {
Sidenotes.hiddenSidenoteStorage.append(sidenote);
continue;
}
// Get all the cells that the sidenote can fit into.
let fittingLayoutCells = layoutCells.filter(cell => cell.room >= sidenote.lastKnownHeight);
if (fittingLayoutCells.length == 0) {
GWLog("TOO MUCH SIDENOTES. GIVING UP. :(", "sidenotes.js");
Sidenotes.sidenotes.forEach(sidenote => {
sidenote.remove();
});
return;
}
/* These functions are used to sort layout cells by best fit for
placing the current sidenote.
*/
let vDistanceToCell = (cell) => {
if ( citationBoundingRect.top > cell.rect.top
&& citationBoundingRect.top < cell.rect.bottom)
return 0;
return (citationBoundingRect.top < cell.rect.top
? Math.abs(citationBoundingRect.top - cell.rect.top)
: Math.abs(citationBoundingRect.top - cell.rect.bottom));
};
let hDistanceToCell = (cell) => {
return Math.abs(citationBoundingRect.left - (cell.left + (cell.width / 2)));
};
let overlapWithNote = (cell, note) => {
let notePosInCell = defaultNotePosInCellForCitation(cell, citation);
let otherNoteCitation = Sidenotes.counterpart(note);
let otherNotePosInCell = defaultNotePosInCellForCitation(cell, otherNoteCitation);
return ( otherNotePosInCell > notePosInCell + sidenote.lastKnownHeight + Sidenotes.sidenoteSpacing
|| notePosInCell > otherNotePosInCell + note.lastKnownHeight + Sidenotes.sidenoteSpacing)
? 0
: Math.max(notePosInCell + sidenote.lastKnownHeight + Sidenotes.sidenoteSpacing - otherNotePosInCell,
otherNotePosInCell + note.lastKnownHeight + Sidenotes.sidenoteSpacing - notePosInCell);
};
let cellCrowdedness = (cell) => {
return cell.sidenotes.reduce((totalOverlap, note) => { return (totalOverlap + overlapWithNote(cell, note)); }, 0);
};
/* We sort the fitting cells by vertical distance from the sidenote
and crowdedness at the sidenote’s default location within the
cell, and secondarily by horizontal distance from the sidenote.
*/
fittingLayoutCells.sort((cellA, cellB) => {
return ( ( (vDistanceToCell(cellA) + cellCrowdedness(cellA))
- (vDistanceToCell(cellB) + cellCrowdedness(cellB)))
|| (hDistanceToCell(cellA) - hDistanceToCell(cellB)));
});
let closestFittingLayoutCell = fittingLayoutCells[0];
// Add the sidenote to the selected cell.
closestFittingLayoutCell.room -= (sidenote.lastKnownHeight + Sidenotes.sidenoteSpacing);
closestFittingLayoutCell.sidenotes.push(sidenote);
};
// Function to compute distance between two successive sidenotes.
let getDistance = (noteA, noteB) => {
return (noteB.posInCell - (noteA.posInCell + noteA.lastKnownHeight + Sidenotes.sidenoteSpacing));
};
// Position sidenotes within layout cells.
layoutCells.forEach(cell => {
if (cell.sidenotes.length == 0)
return;
// Set all of the cell’s sidenotes to default positions.
cell.sidenotes.forEach(sidenote => {
let citation = Sidenotes.counterpart(sidenote);
sidenote.posInCell = defaultNotePosInCellForCitation(cell, citation);
});
// Sort the cell’s sidenotes vertically (secondarily by number).
cell.sidenotes.sort((noteA, noteB) => {
return ( (noteA.posInCell - noteB.posInCell)
|| (parseInt(noteA.id.substr(2)) - parseInt(noteB.id.substr(2))));
});
// Called in pushNotesUp().
let shiftNotesUp = (noteIndexes, shiftUpDistance) => {
noteIndexes.forEach(idx => {
cell.sidenotes[idx].posInCell -= shiftUpDistance;
});
};
// Called immediately below.
let pushNotesUp = (pushUpWhich, pushUpForce, bruteStrength = false) => {
let roomToPush = pushUpWhich.first == 0
? cell.sidenotes[pushUpWhich.first].posInCell
: Math.max(0, getDistance(cell.sidenotes[pushUpWhich.first - 1], cell.sidenotes[pushUpWhich.first]));
let pushUpDistance = bruteStrength
? pushUpForce
: Math.floor(pushUpForce / pushUpWhich.length);
if (pushUpDistance <= roomToPush) {
shiftNotesUp(pushUpWhich, pushUpDistance);
return (pushUpForce - pushUpDistance);
} else {
shiftNotesUp(pushUpWhich, roomToPush);
if (pushUpWhich.first == 0)
return (pushUpForce - roomToPush);
pushUpWhich.splice(0, 0, pushUpWhich.first - 1);
return pushNotesUp(pushUpWhich, (pushUpForce - roomToPush), bruteStrength);
}
};
/* Check each sidenote after the first for overlap with the one
above it; if it overlaps, try pushing the sidenote(s) above it
upward, and also shift the note itself downward.
*/
for (let i = 1; i < cell.sidenotes.length; i++) {
let prevNote = cell.sidenotes[i - 1];
let thisNote = cell.sidenotes[i];
let nextNote = (i == cell.sidenotes.length - 1)
? null
: cell.sidenotes[i + 1];
let overlapAbove = Math.max(0, (-1 * getDistance(prevNote, thisNote)));
if (overlapAbove == 0)
continue;
let pushUpForce = Math.round(overlapAbove / 2);
thisNote.posInCell += ((overlapAbove - pushUpForce) + pushNotesUp([ (i - 1) ], pushUpForce));
}
/* Check whether the lowest sidenote overlaps the cell’s bottom;
if so, push it (and any sidenotes above it that it bumps into)
upward.
*/
let overlapOfBottom = Math.max(0, (cell.sidenotes.last.posInCell + cell.sidenotes.last.lastKnownHeight) - parseInt(cell.style.height));
if (overlapOfBottom > 0)
pushNotesUp([ (cell.sidenotes.length - 1) ], overlapOfBottom, true);
// Set the sidenote positions via inline styles.
cell.sidenotes.forEach(sidenote => {
sidenote.style.top = Math.round(sidenote.posInCell) + "px";
});
// Re-inject the sidenotes into the page.
cell.append(...cell.sidenotes);
});
// Un-hide the sidenote columns.
Sidenotes.sidenoteColumnLeft.style.visibility = "";
Sidenotes.sidenoteColumnRight.style.visibility = "";
// Fire event.
GW.notificationCenter.fireEvent("Sidenotes.sidenotePositionsDidUpdate");
},
/* Destroys the HTML structure of the sidenotes.
*/
deconstructSidenotes: () => {
GWLog("Sidenotes.deconstructSidenotes", "sidenotes.js", 1);
Sidenotes.sidenotes = null;
Sidenotes.citations = null;
if (Sidenotes.sidenoteColumnLeft)
Sidenotes.sidenoteColumnLeft.remove();
Sidenotes.sidenoteColumnLeft = null;
if (Sidenotes.sidenoteColumnRight)
Sidenotes.sidenoteColumnRight.remove();
Sidenotes.sidenoteColumnRight = null;
if (Sidenotes.hiddenSidenoteStorage)
Sidenotes.hiddenSidenoteStorage.remove();
Sidenotes.hiddenSidenoteStorage = null;
},
/* Constructs the HTML structure, and associated listeners and auxiliaries,
of the sidenotes.
*/
constructSidenotes: () => {
GWLog("Sidenotes.constructSidenotes", "sidenotes.js", 1);
/* Do nothing if constructSidenotes() somehow gets run extremely early
in the page load process.
*/
let markdownBody = document.querySelector("#markdownBody");
if (markdownBody == null)
return;
// Destroy before creating.
Sidenotes.deconstructSidenotes();
// Add the sidenote columns.
Sidenotes.sidenoteColumnLeft = newElement("DIV", { "id": "sidenote-column-left" });
Sidenotes.sidenoteColumnRight = newElement("DIV", { "id": "sidenote-column-right" });
[ Sidenotes.sidenoteColumnLeft, Sidenotes.sidenoteColumnRight ].forEach(column => {
column.classList.add("footnotes");
column.style.visibility = "hidden";
markdownBody.append(column);
});
// Add the hidden sidenote storage.
markdownBody.append(Sidenotes.hiddenSidenoteStorage = newElement("DIV", {
"id": "hidden-sidenote-storage",
"class": "footnotes",
"style": "display:none"
}));
/* Create and inject the sidenotes.
*/
Sidenotes.sidenotes = [ ];
// The footnote references (citations).
Sidenotes.citations = Array.from(document.querySelectorAll("a.footnote-ref"));
// If there are no footnotes, we’re done.
if (Sidenotes.citations.length == 0)
return;
Sidenotes.citations.forEach(citation => {
let noteNumber = Notes.noteNumberFromHash(citation.hash);
// Create the sidenote outer containing block...
let sidenote = newElement("DIV", { class: "sidenote", id: `sn${noteNumber}` });
/* Fill the sidenote either by copying from an existing footnote
in the current page, or else by transcluding the footnote to
which the citation refers.
*/
let referencedFootnote = document.querySelector(`#fn${noteNumber}`);
let sidenoteContents = newDocument(referencedFootnote
? referencedFootnote.childNodes
: synthesizeIncludeLink(citation, {
"class": "include-strict include-unwrap",
"data-include-selector-not": ".footnote-self-link"
}));
/* If the sidenote contents were copied from a footnote that exists
in the page, then we should regenerate placeholders, otherwise
the back-to-citation links (among possibly other things) may
not work right.
*/
if (referencedFootnote)
regeneratePlaceholderIds(sidenoteContents);
// Wrap the contents of the footnote in two wrapper divs...
sidenote.appendChild(sidenote.outerWrapper = newElement("DIV", {
class: "sidenote-outer-wrapper"
})).appendChild(sidenote.innerWrapper = newElement("DIV", {
class: "sidenote-inner-wrapper"
})).append(sidenoteContents);
/* Create & inject the sidenote self-links (ie. boxed sidenote
numbers).
*/
sidenote.append(newElement("A", {
"class": "sidenote-self-link",
"href": `#sn${noteNumber}`
}, {
"textContent": noteNumber
}));
// Remove footnote self-link.
sidenote.querySelector(".footnote-self-link")?.remove();
// Add listener to update sidenote positions when media loads.
sidenote.querySelectorAll("figure img, figure video").forEach(mediaElement => {
mediaElement.addEventListener("load", (event) => {
Sidenotes.updateSidenotePositionsIfNeeded();
}, { once: true });
});
// Add the sidenote to the sidenotes array...
Sidenotes.sidenotes.push(sidenote);
// Inject the sidenote into the page.
Sidenotes.hiddenSidenoteStorage.append(sidenote);
});
/* Bind sidenote mouse-hover events.
*/
Sidenotes.citations.forEach(citation => {
let sidenote = Sidenotes.counterpart(citation);
// Unbind existing events, if any.
if (sidenote.onSidenoteMouseEnterHighlightCitation)
sidenote.removeEventListener("mouseenter", sidenote.onSidenoteMouseEnterHighlightCitation);
if (sidenote.onSidenoteMouseLeaveUnhighlightCitation)
sidenote.removeEventListener("mouseleave", sidenote.onSidenoteMouseLeaveUnhighlightCitation);
if (citation.onCitationMouseEnterSlideSidenote)
citation.removeEventListener("mouseenter", citation.onCitationMouseEnterSlideSidenote);
if (sidenote.onSidenoteMouseEnterSlideSidenote)
sidenote.removeEventListener("mouseenter", sidenote.onSidenoteMouseEnterSlideSidenote);
if (sidenote.onSidenoteMouseLeaveUnslideSidenote)
sidenote.removeEventListener("mouseleave", sidenote.onSidenoteMouseLeaveUnslideSidenote);
if (sidenote.scrollListener)
sidenote.outerWrapper.removeEventListener("scroll", sidenote.scrollListener);
// Bind new events.
sidenote.addEventListener("mouseenter", sidenote.onSidenoteMouseEnterHighlightCitation = (event) => {
citation.classList.toggle("highlighted", true);
});
sidenote.addEventListener("mouseleave", sidenote.onSidenoteMouseLeaveUnhighlightCitation = (event) => {
citation.classList.toggle("highlighted", false);
});
citation.addEventListener("mouseenter", citation.onCitationMouseEnterSlideSidenote = (event) => {
Sidenotes.putAllSidenotesBack(sidenote);
requestAnimationFrame(() => {
Sidenotes.slideSidenoteIntoView(sidenote, true);
});
});
sidenote.addEventListener("mouseenter", sidenote.onSidenoteMouseEnterSlideSidenote = (event) => {
Sidenotes.putAllSidenotesBack(sidenote);
requestAnimationFrame(() => {
Sidenotes.slideSidenoteIntoView(sidenote, false);
});
});
sidenote.addEventListener("mouseleave", sidenote.onSidenoteMouseLeaveUnslideSidenote = (event) => {
Sidenotes.putSidenoteBack(sidenote);
});
sidenote.scrollListener = addScrollListener((event) => {
sidenote.classList.toggle("hide-more-indicator", sidenote.outerWrapper.scrollTop + sidenote.outerWrapper.clientHeight == sidenote.outerWrapper.scrollHeight);
}, {
name: "Sidenotes.updateSidenoteHideMoreIndicatorVisibilityOnScrollListener",
target: sidenote.outerWrapper
});
});
GW.notificationCenter.fireEvent("Sidenotes.sidenotesDidConstruct");
// Fire event.
GW.notificationCenter.fireEvent("GW.contentDidInject", {
source: "Sidenotes.constructSidenotes",
container: Sidenotes.hiddenSidenoteStorage,
document: document,
loadLocation: location,
flags: GW.contentDidInjectEventFlags.fullWidthPossible.localize
});
},
cleanup: () => {
GWLog("Sidenotes.cleanup", "sidenotes.js", 1);
/* Deactivate active media queries.
*/
cancelDoWhenMatchMedia("Sidenotes.rewriteHashForCurrentMode");
cancelDoWhenMatchMedia("Sidenotes.rewriteCitationTargetsForCurrentMode");
cancelDoWhenMatchMedia("Sidenotes.addOrRemoveEventHandlersForCurrentMode");
/* Remove sidenotes & auxiliaries from HTML.
*/
Sidenotes.deconstructSidenotes();
GW.notificationCenter.fireEvent("Sidenotes.cleanupDidComplete");
},
/* Q: Why is this setup function so long and complex?
A: In order to properly handle all of the following:
1. The two different modes (footnote popups vs. sidenotes)
2. The interactions between sidenotes and collapse blocks
3. Linking to footnotes/sidenotes
4. Loading a URL that links to a footnote/sidenote
5. Changes in the viewport width dynamically altering all of the above
… and, of course, correct layout of the sidenotes, even in tricky cases
where the citations are densely packed and the sidenotes are long.
*/
setup: () => {
GWLog("Sidenotes.setup", "sidenotes.js", 1);
/* If the page was loaded with a hash that points to a footnote, but
sidenotes are enabled (or vice-versa), rewrite the hash in
accordance with the current mode (this will also cause the page to
end up scrolled to the appropriate element - footnote or sidenote).
Add an active media query to rewrite the hash whenever the viewport
width media query changes.
*/
doWhenMatchMedia(Sidenotes.mediaQueries.viewportWidthBreakpoint, "Sidenotes.rewriteHashForCurrentMode", (mediaQuery) => {
let regex = new RegExp(mediaQuery.matches ? "^#fn[0-9]+$" : "^#sn[0-9]+$");
let prefix = (mediaQuery.matches ? "#sn" : "#fn");
if (location.hash.match(regex)) {
relocate(prefix + Notes.noteNumberFromHash());
// Update targeting.
if (mediaQuery.matches)
Sidenotes.updateTargetCounterpart();
else
updateFootnoteTargeting();
}
}, null, (mediaQuery) => {
if (location.hash.match(/^#sn[0-9]/)) {
relocate("#fn" + Notes.noteNumberFromHash());
// Update targeting.
updateFootnoteTargeting();
}
});
/* We do not bother to construct sidenotes on mobile clients, and so
the rest of this is also irrelevant.
*/
if (GW.isMobile())
return;
/* Update the margin note style, and add event listener to re-update it
when the viewport width changes. Also add event handler to update
margin note style in transcluded content and pop-frames.
*/
addContentLoadHandler((info) => {
doWhenMatchMedia(Sidenotes.mediaQueries.viewportWidthBreakpoint, "Sidenotes.updateMarginNoteStyleForCurrentMode", (mediaQuery) => {
Sidenotes.setMarginNoteStyle(info);
});
}, "rewrite", (info) => info.container == document.body, true);
addContentInjectHandler(Sidenotes.setMarginNoteStyle, ">rewrite");
/* When an anchor link is clicked that sets the hash to its existing
value, weird things happen. In particular, weird things happen with
citations and sidenotes. We must prevent that, by updating state
properly when that happens. (No ‘hashchange’ event is fired in this
case, so we cannot depend on the ‘GW.hashDidChange’ event handler.)
*/
addContentInjectHandler(Sidenotes.addFauxHashChangeEventsToNoteMetaLinks = (eventInfo) => {
let selector = [
"a.footnote-ref",
"a.sidenote-self-link",
".sidenote a.footnote-back"
].join(", ");
eventInfo.container.querySelectorAll(selector).forEach(link => {
link.addActivateEvent((event) => {
if (link.hash == location.hash)
Sidenotes.updateStateAfterHashChange();
});
});
}, "eventListeners", (info) => info.document == document);
/* In footnote mode (ie. on viewports too narrow to support sidenotes),
footnote reference links (i.e., citations) should point down to
footnotes (this is the default state). But in sidenote mode,
footnote reference links should point to sidenotes.
We therefore rewrite all footnote reference links appropriately to
the current mode (based on viewport width).
We also add an active media query to rewrite the links if a change
in viewport width results in switching modes, as well as an event
handler to rewrite footnote reference links in transcluded content.
*/
doWhenMatchMedia(Sidenotes.mediaQueries.viewportWidthBreakpoint, "Sidenotes.rewriteCitationTargetsForCurrentMode", (mediaQuery) => {
document.querySelectorAll("a.footnote-ref").forEach(citation => {
citation.href = (mediaQuery.matches ? "#sn" : "#fn") + Notes.noteNumberFromHash(citation.hash);
});
}, null, (mediaQuery) => {
document.querySelectorAll("a.footnote-ref").forEach(citation => {
citation.href = "#fn" + Notes.noteNumberFromHash(citation.hash);
});
});
addContentLoadHandler(Sidenotes.rewriteCitationTargetsInLoadedContent = (eventInfo) => {
document.querySelectorAll("a.footnote-ref").forEach(citation => {
if (citation.pathname == location.pathname)
citation.href = (Sidenotes.mediaQueries.viewportWidthBreakpoint.matches ? "#sn" : "#fn")
+ Notes.noteNumberFromHash(citation.hash);
});
}, "rewrite", (info) => info.document == document);
/* What happens if the page loads with a URL hash that points to a
sidenote or footnote or citation? We need to scroll appropriately,
and do other adjustments, just as we do when the hash updates.
*/
GW.notificationCenter.addHandlerForEvent("Sidenotes.sidenotePositionsDidUpdate", Sidenotes.updateStateAfterHashChange = (info) => {
GWLog("Sidenotes.updateStateAfterHashChange", "sidenotes.js", 1);
// Update highlighted state of sidenote and citation, if need be.
Sidenotes.updateTargetCounterpart();
/* If hash targets a sidenote, reveal corresponding citation; and
vice-versa. Scroll everything into view properly.
*/
if (location.hash.match(/#sn[0-9]/)) {
let citation = document.querySelector("#fnref" + Notes.noteNumberFromHash());
if (citation == null)
return;
let sidenote = Sidenotes.counterpart(citation);
revealElement(citation, {
scrollIntoView: false
});
Sidenotes.slideLockSidenote(sidenote);
requestAnimationFrame(() => {
scrollElementIntoView(sidenote, {
offset: (-1 * (Sidenotes.sidenotePadding + 1))
});
Sidenotes.unSlideLockSidenote(sidenote);
});
} else if (location.hash.match(/#fnref[0-9]/)) {
let citation = getHashTargetedElement();
let sidenote = Sidenotes.counterpart(citation);
Sidenotes.slideLockSidenote(sidenote);
requestAnimationFrame(() => {
let sidenoteRect = sidenote.getBoundingClientRect();
let citationRect = citation.getBoundingClientRect();
if ( sidenoteRect.top < Sidenotes.sidenotePadding + 1
&& citationRect.bottom + (-1 * (sidenoteRect.top - Sidenotes.sidenotePadding)) < window.innerHeight)
scrollElementIntoView(sidenote, {
offset: (-1 * (Sidenotes.sidenotePadding + 1))
});
Sidenotes.unSlideLockSidenote(sidenote);
});
}
/* Hide mode selectors, as they would otherwise overlap a
sidenote that’s on the top-right.
*/
if (Notes.noteNumberFromHash() > "")
Sidenotes.hideInterferingUIElements();
}, { once: true });
// Add event listeners, and the switch between modes.
doWhenMatchMedia(Sidenotes.mediaQueries.viewportWidthBreakpoint, "Sidenotes.addOrRemoveEventHandlersForCurrentMode", (mediaQuery) => {
doWhenPageLayoutComplete(Sidenotes.updateSidenotePositionsIfNeeded);
/* After the hash updates, properly highlight everything, if needed.
Also, if the hash points to a sidenote whose citation is in a
collapse block, expand it and all collapse blocks enclosing it.
*/
GW.notificationCenter.addHandlerForEvent("GW.hashDidChange", Sidenotes.updateStateAfterHashChange);
/* Add event handler to (asynchronously) recompute sidenote positioning
when full-width media lazy-loads.
*/
GW.notificationCenter.addHandlerForEvent("Rewrite.fullWidthMediaDidLoad", Sidenotes.updateSidenotePositionsAfterFullWidthMediaDidLoad = (info) => {
if (isWithinCollapsedBlock(info.mediaElement))
return;
doWhenPageLayoutComplete(Sidenotes.updateSidenotePositionsIfNeeded);
});
/* Add event handler to (asynchronously) recompute sidenote positioning
when collapse blocks are expanded/collapsed.
*/
GW.notificationCenter.addHandlerForEvent("Collapse.collapseStateDidChange", Sidenotes.updateSidenotePositionsAfterCollapseStateDidChange = (info) => {
doWhenPageLayoutComplete(Sidenotes.updateSidenotePositionsIfNeeded);
}, { condition: (info) => (info.collapseBlock.closest("#markdownBody") != null) });
/* Add event handler to (asynchronously) recompute sidenote positioning
when new content is loaded (e.g. via transclusion).
*/
GW.notificationCenter.addHandlerForEvent("Rewrite.contentDidChange", Sidenotes.updateSidenotePositionsAfterContentDidChange = (info) => {
doWhenPageLayoutComplete(Sidenotes.updateSidenotePositionsIfNeeded);
}, { condition: (info) => (info.document == document) });
/* Add a resize listener so that sidenote positions are recalculated when
the window is resized.
*/
addWindowResizeListener(Sidenotes.windowResized = (event) => {
GWLog("Sidenotes.windowResized", "sidenotes.js", 2);
doWhenPageLayoutComplete(Sidenotes.updateSidenotePositionsIfNeeded);
}, {
name: "Sidenotes.updateSidenotePositionsOnWindowResizeListener"
});
/* Add handler to bind more sidenote-slide events if more
citations are injected (e.g., in a popup).
*/
addContentInjectHandler(Sidenotes.bindAdditionalSidenoteSlideEvents = (eventInfo) => {
eventInfo.container.querySelectorAll("a.footnote-ref").forEach(citation => {
if (citation.pathname != location.pathname)
return;
let sidenote = Sidenotes.counterpart(citation);
citation.addEventListener("mouseenter", citation.onCitationMouseEnterSlideSidenote = (event) => {
Sidenotes.putAllSidenotesBack(sidenote);
requestAnimationFrame(() => {
Sidenotes.slideSidenoteIntoView(sidenote, true);
});
});
});
}, "eventListeners", (info) => info.document != document);
/* Add a scroll listener to un-slide all sidenotes on scroll.
*/
addScrollListener((event) => {
Sidenotes.putAllSidenotesBack();
}, {
name: "Sidenotes.unSlideSidenotesOnScrollListener",
defer: true
});
}, (mediaQuery) => {
/* Deactivate event handlers.
*/
GW.notificationCenter.removeHandlerForEvent("GW.hashDidChange", Sidenotes.updateStateAfterHashChange);
GW.notificationCenter.removeHandlerForEvent("Rewrite.contentDidChange", Sidenotes.updateSidenotePositionsAfterContentDidChange);
GW.notificationCenter.removeHandlerForEvent("Rewrite.fullWidthMediaDidLoad", Sidenotes.updateSidenotePositionsAfterFullWidthMediaDidLoad);
GW.notificationCenter.removeHandlerForEvent("Collapse.collapseStateDidChange", Sidenotes.updateSidenotePositionsAfterCollapseStateDidChange);
GW.notificationCenter.removeHandlerForEvent("GW.contentDidInject", Sidenotes.bindAdditionalSidenoteSlideEvents);
removeScrollListener("Sidenotes.unSlideSidenotesOnScroll");
removeWindowResizeListener("Sidenotes.recalculateSidenotePositionsOnWindowResize");
}, (mediaQuery) => {
/* Deactivate event handlers.
*/
GW.notificationCenter.removeHandlerForEvent("GW.hashDidChange", Sidenotes.updateStateAfterHashChange);
GW.notificationCenter.removeHandlerForEvent("Rewrite.contentDidChange", Sidenotes.updateSidenotePositionsAfterContentDidChange);
GW.notificationCenter.removeHandlerForEvent("Rewrite.fullWidthMediaDidLoad", Sidenotes.updateSidenotePositionsAfterFullWidthMediaDidLoad);
GW.notificationCenter.removeHandlerForEvent("Collapse.collapseStateDidChange", Sidenotes.updateSidenotePositionsAfterCollapseStateDidChange);
GW.notificationCenter.removeHandlerForEvent("GW.contentDidInject", Sidenotes.bindAdditionalSidenoteSlideEvents);
removeScrollListener("Sidenotes.unSlideSidenotesOnScroll");
removeWindowResizeListener("Sidenotes.recalculateSidenotePositionsOnWindowResize");
});
// Once the sidenotes are constructed, lay them out.
GW.notificationCenter.addHandlerForEvent("Sidenotes.sidenotesDidConstruct", (info) => {
// Lay out sidenotes once page layout is complete.
doWhenPageLayoutComplete(() => {
Sidenotes.updateSidenotePositionsIfNeeded();
// Add listener to lay out sidenotes when they are re-constructed.
GW.notificationCenter.addHandlerForEvent("Sidenotes.sidenotesDidConstruct", (info) => {
// Update highlighted state of sidenote and citation, if need be.
Sidenotes.updateTargetCounterpart();