-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
paginator.js
1103 lines (1056 loc) · 42 KB
/
paginator.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
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
const debounce = (f, wait, immediate) => {
let timeout
return (...args) => {
const later = () => {
timeout = null
if (!immediate) f(...args)
}
const callNow = immediate && !timeout
if (timeout) clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) f(...args)
}
}
const lerp = (min, max, x) => x * (max - min) + min
const easeOutQuad = x => 1 - (1 - x) * (1 - x)
const animate = (a, b, duration, ease, render) => new Promise(resolve => {
let start
const step = now => {
start ??= now
const fraction = Math.min(1, (now - start) / duration)
render(lerp(a, b, ease(fraction)))
if (fraction < 1) requestAnimationFrame(step)
else resolve()
}
requestAnimationFrame(step)
})
// collapsed range doesn't return client rects sometimes (or always?)
// try make get a non-collapsed range or element
const uncollapse = range => {
if (!range?.collapsed) return range
const { endOffset, endContainer } = range
if (endContainer.nodeType === 1) {
const node = endContainer.childNodes[endOffset]
if (node?.nodeType === 1) return node
return endContainer
}
if (endOffset + 1 < endContainer.length) range.setEnd(endContainer, endOffset + 1)
else if (endOffset > 1) range.setStart(endContainer, endOffset - 1)
else return endContainer.parentNode
return range
}
const makeRange = (doc, node, start, end = start) => {
const range = doc.createRange()
range.setStart(node, start)
range.setEnd(node, end)
return range
}
// use binary search to find an offset value in a text node
const bisectNode = (doc, node, cb, start = 0, end = node.nodeValue.length) => {
if (end - start === 1) {
const result = cb(makeRange(doc, node, start), makeRange(doc, node, end))
return result < 0 ? start : end
}
const mid = Math.floor(start + (end - start) / 2)
const result = cb(makeRange(doc, node, start, mid), makeRange(doc, node, mid, end))
return result < 0 ? bisectNode(doc, node, cb, start, mid)
: result > 0 ? bisectNode(doc, node, cb, mid, end) : mid
}
const { SHOW_ELEMENT, SHOW_TEXT, SHOW_CDATA_SECTION,
FILTER_ACCEPT, FILTER_REJECT, FILTER_SKIP } = NodeFilter
const filter = SHOW_ELEMENT | SHOW_TEXT | SHOW_CDATA_SECTION
// needed cause there seems to be a bug in `getBoundingClientRect()` in Firefox
// where it fails to include rects that have zero width and non-zero height
// (CSSOM spec says "rectangles [...] of which the height or width is not zero")
// which makes the visible range include an extra space at column boundaries
const getBoundingClientRect = target => {
let top = Infinity, right = -Infinity, left = Infinity, bottom = -Infinity
for (const rect of target.getClientRects()) {
left = Math.min(left, rect.left)
top = Math.min(top, rect.top)
right = Math.max(right, rect.right)
bottom = Math.max(bottom, rect.bottom)
}
return new DOMRect(left, top, right - left, bottom - top)
}
const getVisibleRange = (doc, start, end, mapRect) => {
// first get all visible nodes
const acceptNode = node => {
const name = node.localName?.toLowerCase()
// ignore all scripts, styles, and their children
if (name === 'script' || name === 'style') return FILTER_REJECT
if (node.nodeType === 1) {
const { left, right } = mapRect(node.getBoundingClientRect())
// no need to check child nodes if it's completely out of view
if (right < start || left > end) return FILTER_REJECT
// elements must be completely in view to be considered visible
// because you can't specify offsets for elements
if (left >= start && right <= end) return FILTER_ACCEPT
// TODO: it should probably allow elements that do not contain text
// because they can exceed the whole viewport in both directions
// especially in scrolled mode
} else {
// ignore empty text nodes
if (!node.nodeValue?.trim()) return FILTER_SKIP
// create range to get rect
const range = doc.createRange()
range.selectNodeContents(node)
const { left, right } = mapRect(range.getBoundingClientRect())
// it's visible if any part of it is in view
if (right >= start && left <= end) return FILTER_ACCEPT
}
return FILTER_SKIP
}
const walker = doc.createTreeWalker(doc.body, filter, { acceptNode })
const nodes = []
for (let node = walker.nextNode(); node; node = walker.nextNode())
nodes.push(node)
// we're only interested in the first and last visible nodes
const from = nodes[0] ?? doc.body
const to = nodes[nodes.length - 1] ?? from
// find the offset at which visibility changes
const startOffset = from.nodeType === 1 ? 0
: bisectNode(doc, from, (a, b) => {
const p = mapRect(getBoundingClientRect(a))
const q = mapRect(getBoundingClientRect(b))
if (p.right < start && q.left > start) return 0
return q.left > start ? -1 : 1
})
const endOffset = to.nodeType === 1 ? 0
: bisectNode(doc, to, (a, b) => {
const p = mapRect(getBoundingClientRect(a))
const q = mapRect(getBoundingClientRect(b))
if (p.right < end && q.left > end) return 0
return q.left > end ? -1 : 1
})
const range = doc.createRange()
range.setStart(from, startOffset)
range.setEnd(to, endOffset)
return range
}
const selectionIsBackward = sel => {
const range = document.createRange()
range.setStart(sel.anchorNode, sel.anchorOffset)
range.setEnd(sel.focusNode, sel.focusOffset)
return range.collapsed
}
const setSelectionTo = (target, collapse) => {
let range
if (target.startContainer) range = target.cloneRange()
else if (target.nodeType) {
range = document.createRange()
range.selectNode(target)
}
if (range) {
const sel = range.startContainer.ownerDocument.defaultView.getSelection()
sel.removeAllRanges()
if (collapse === -1) range.collapse(true)
else if (collapse === 1) range.collapse()
sel.addRange(range)
}
}
const getDirection = doc => {
const { defaultView } = doc
const { writingMode, direction } = defaultView.getComputedStyle(doc.body)
const vertical = writingMode === 'vertical-rl'
|| writingMode === 'vertical-lr'
const rtl = doc.body.dir === 'rtl'
|| direction === 'rtl'
|| doc.documentElement.dir === 'rtl'
return { vertical, rtl }
}
const getBackground = doc => {
const bodyStyle = doc.defaultView.getComputedStyle(doc.body)
return bodyStyle.backgroundColor === 'rgba(0, 0, 0, 0)'
&& bodyStyle.backgroundImage === 'none'
? doc.defaultView.getComputedStyle(doc.documentElement).background
: bodyStyle.background
}
const makeMarginals = (length, part) => Array.from({ length }, () => {
const div = document.createElement('div')
const child = document.createElement('div')
div.append(child)
child.setAttribute('part', part)
return div
})
const setStylesImportant = (el, styles) => {
const { style } = el
for (const [k, v] of Object.entries(styles)) style.setProperty(k, v, 'important')
}
class View {
#observer = new ResizeObserver(() => this.expand())
#element = document.createElement('div')
#iframe = document.createElement('iframe')
#contentRange = document.createRange()
#overlayer
#vertical = false
#rtl = false
#column = true
#size
#layout = {}
constructor({ container, onExpand }) {
this.container = container
this.onExpand = onExpand
this.#iframe.setAttribute('part', 'filter')
this.#element.append(this.#iframe)
Object.assign(this.#element.style, {
boxSizing: 'content-box',
position: 'relative',
overflow: 'hidden',
flex: '0 0 auto',
width: '100%', height: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
})
Object.assign(this.#iframe.style, {
overflow: 'hidden',
border: '0',
display: 'none',
width: '100%', height: '100%',
})
// `allow-scripts` is needed for events because of WebKit bug
// https://bugs.webkit.org/show_bug.cgi?id=218086
this.#iframe.setAttribute('sandbox', 'allow-same-origin allow-scripts')
this.#iframe.setAttribute('scrolling', 'no')
}
get element() {
return this.#element
}
get document() {
return this.#iframe.contentDocument
}
async load(src, afterLoad, beforeRender) {
if (typeof src !== 'string') throw new Error(`${src} is not string`)
return new Promise(resolve => {
this.#iframe.addEventListener('load', () => {
const doc = this.document
afterLoad?.(doc)
// it needs to be visible for Firefox to get computed style
this.#iframe.style.display = 'block'
const { vertical, rtl } = getDirection(doc)
const background = getBackground(doc)
this.#iframe.style.display = 'none'
this.#vertical = vertical
this.#rtl = rtl
this.#contentRange.selectNodeContents(doc.body)
const layout = beforeRender?.({ vertical, rtl, background })
this.#iframe.style.display = 'block'
this.render(layout)
this.#observer.observe(doc.body)
// the resize observer above doesn't work in Firefox
// (see https://bugzilla.mozilla.org/show_bug.cgi?id=1832939)
// until the bug is fixed we can at least account for font load
doc.fonts.ready.then(() => this.expand())
resolve()
}, { once: true })
this.#iframe.src = src
})
}
render(layout) {
if (!layout) return
this.#column = layout.flow !== 'scrolled'
this.#layout = layout
if (this.#column) this.columnize(layout)
else this.scrolled(layout)
}
scrolled({ gap, columnWidth }) {
const vertical = this.#vertical
const doc = this.document
setStylesImportant(doc.documentElement, {
'box-sizing': 'border-box',
'padding': vertical ? `${gap}px 0` : `0 ${gap}px`,
'column-width': 'auto',
'height': 'auto',
'width': 'auto',
})
setStylesImportant(doc.body, {
[vertical ? 'max-height' : 'max-width']: `${columnWidth}px`,
'margin': 'auto',
})
this.setImageSize()
this.expand()
}
columnize({ width, height, gap, columnWidth }) {
const vertical = this.#vertical
this.#size = vertical ? height : width
const doc = this.document
setStylesImportant(doc.documentElement, {
'box-sizing': 'border-box',
'column-width': `${Math.trunc(columnWidth)}px`,
'column-gap': `${gap}px`,
'column-fill': 'auto',
...(vertical
? { 'width': `${width}px` }
: { 'height': `${height}px` }),
'padding': vertical ? `${gap / 2}px 0` : `0 ${gap / 2}px`,
'overflow': 'hidden',
// force wrap long words
'overflow-wrap': 'break-word',
// reset some potentially problematic props
'position': 'static', 'border': '0', 'margin': '0',
'max-height': 'none', 'max-width': 'none',
'min-height': 'none', 'min-width': 'none',
// fix glyph clipping in WebKit
'-webkit-line-box-contain': 'block glyphs replaced',
})
setStylesImportant(doc.body, {
'max-height': 'none',
'max-width': 'none',
'margin': '0',
})
this.setImageSize()
this.expand()
}
setImageSize() {
const { width, height, margin } = this.#layout
const vertical = this.#vertical
const doc = this.document
for (const el of doc.body.querySelectorAll('img, svg, video')) {
// preserve max size if they are already set
const { maxHeight, maxWidth } = doc.defaultView.getComputedStyle(el)
setStylesImportant(el, {
'max-height': vertical
? (maxHeight !== 'none' && maxHeight !== '0px' ? maxHeight : '100%')
: `${height - margin * 2}px`,
'max-width': vertical
? `${width - margin * 2}px`
: (maxWidth !== 'none' && maxWidth !== '0px' ? maxWidth : '100%'),
'object-fit': 'contain',
'page-break-inside': 'avoid',
'break-inside': 'avoid',
'box-sizing': 'border-box',
})
}
}
expand() {
const { documentElement } = this.document
if (this.#column) {
const side = this.#vertical ? 'height' : 'width'
const otherSide = this.#vertical ? 'width' : 'height'
const contentRect = this.#contentRange.getBoundingClientRect()
const rootRect = documentElement.getBoundingClientRect()
// offset caused by column break at the start of the page
// which seem to be supported only by WebKit and only for horizontal writing
const contentStart = this.#vertical ? 0
: this.#rtl ? rootRect.right - contentRect.right : contentRect.left - rootRect.left
const contentSize = contentStart + contentRect[side]
const pageCount = Math.ceil(contentSize / this.#size)
const expandedSize = pageCount * this.#size
this.#element.style.padding = '0'
this.#iframe.style[side] = `${expandedSize}px`
this.#element.style[side] = `${expandedSize + this.#size * 2}px`
this.#iframe.style[otherSide] = '100%'
this.#element.style[otherSide] = '100%'
documentElement.style[side] = `${this.#size}px`
if (this.#overlayer) {
this.#overlayer.element.style.margin = '0'
this.#overlayer.element.style.left = this.#vertical ? '0' : `${this.#size}px`
this.#overlayer.element.style.top = this.#vertical ? `${this.#size}px` : '0'
this.#overlayer.element.style[side] = `${expandedSize}px`
this.#overlayer.redraw()
}
} else {
const side = this.#vertical ? 'width' : 'height'
const otherSide = this.#vertical ? 'height' : 'width'
const contentSize = documentElement.getBoundingClientRect()[side]
const expandedSize = contentSize
const { margin } = this.#layout
const padding = this.#vertical ? `0 ${margin}px` : `${margin}px 0`
this.#element.style.padding = padding
this.#iframe.style[side] = `${expandedSize}px`
this.#element.style[side] = `${expandedSize}px`
this.#iframe.style[otherSide] = '100%'
this.#element.style[otherSide] = '100%'
if (this.#overlayer) {
this.#overlayer.element.style.margin = padding
this.#overlayer.element.style.left = '0'
this.#overlayer.element.style.top = '0'
this.#overlayer.element.style[side] = `${expandedSize}px`
this.#overlayer.redraw()
}
}
this.onExpand()
}
set overlayer(overlayer) {
this.#overlayer = overlayer
this.#element.append(overlayer.element)
}
get overlayer() {
return this.#overlayer
}
destroy() {
if (this.document) this.#observer.unobserve(this.document.body)
}
}
// NOTE: everything here assumes the so-called "negative scroll type" for RTL
export class Paginator extends HTMLElement {
static observedAttributes = [
'flow', 'gap', 'margin',
'max-inline-size', 'max-block-size', 'max-column-count',
]
#root = this.attachShadow({ mode: 'closed' })
#observer = new ResizeObserver(() => this.render())
#top
#background
#container
#header
#footer
#view
#vertical = false
#rtl = false
#margin = 0
#index = -1
#anchor = 0 // anchor view to a fraction (0-1), Range, or Element
#justAnchored = false
#locked = false // while true, prevent any further navigation
#styles
#styleMap = new WeakMap()
#mediaQuery = matchMedia('(prefers-color-scheme: dark)')
#mediaQueryListener
#scrollBounds
#touchState
#touchScrolled
#lastVisibleRange
constructor() {
super()
this.#root.innerHTML = `<style>
:host {
display: block;
container-type: size;
}
:host, #top {
box-sizing: border-box;
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
#top {
--_gap: 7%;
--_margin: 48px;
--_max-inline-size: 720px;
--_max-block-size: 1440px;
--_max-column-count: 2;
--_max-column-count-portrait: 1;
--_max-column-count-spread: var(--_max-column-count);
--_half-gap: calc(var(--_gap) / 2);
--_max-width: calc(var(--_max-inline-size) * var(--_max-column-count-spread));
--_max-height: var(--_max-block-size);
display: grid;
grid-template-columns:
minmax(var(--_half-gap), 1fr)
var(--_half-gap)
minmax(0, calc(var(--_max-width) - var(--_gap)))
var(--_half-gap)
minmax(var(--_half-gap), 1fr);
grid-template-rows:
minmax(var(--_margin), 1fr)
minmax(0, var(--_max-height))
minmax(var(--_margin), 1fr);
&.vertical {
--_max-column-count-spread: var(--_max-column-count-portrait);
--_max-width: var(--_max-block-size);
--_max-height: calc(var(--_max-inline-size) * var(--_max-column-count-spread));
}
@container (orientation: portrait) {
& {
--_max-column-count-spread: var(--_max-column-count-portrait);
}
&.vertical {
--_max-column-count-spread: var(--_max-column-count);
}
}
}
#background {
grid-column: 1 / -1;
grid-row: 1 / -1;
}
#container {
grid-column: 2 / 5;
grid-row: 2;
overflow: hidden;
}
:host([flow="scrolled"]) #container {
grid-column: 1 / -1;
grid-row: 1 / -1;
overflow: auto;
}
#header {
grid-column: 3 / 4;
grid-row: 1;
}
#footer {
grid-column: 3 / 4;
grid-row: 3;
align-self: end;
}
#header, #footer {
display: grid;
height: var(--_margin);
}
:is(#header, #footer) > * {
display: flex;
align-items: center;
min-width: 0;
}
:is(#header, #footer) > * > * {
width: 100%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
text-align: center;
font-size: .75em;
opacity: .6;
}
</style>
<div id="top">
<div id="background" part="filter"></div>
<div id="header"></div>
<div id="container"></div>
<div id="footer"></div>
</div>
`
this.#top = this.#root.getElementById('top')
this.#background = this.#root.getElementById('background')
this.#container = this.#root.getElementById('container')
this.#header = this.#root.getElementById('header')
this.#footer = this.#root.getElementById('footer')
this.#observer.observe(this.#container)
this.#container.addEventListener('scroll', () => this.dispatchEvent(new Event('scroll')))
this.#container.addEventListener('scroll', debounce(() => {
if (this.scrolled) {
if (this.#justAnchored) this.#justAnchored = false
else this.#afterScroll('scroll')
}
}, 250))
const opts = { passive: false }
this.addEventListener('touchstart', this.#onTouchStart.bind(this), opts)
this.addEventListener('touchmove', this.#onTouchMove.bind(this), opts)
this.addEventListener('touchend', this.#onTouchEnd.bind(this))
this.addEventListener('load', ({ detail: { doc } }) => {
doc.addEventListener('touchstart', this.#onTouchStart.bind(this), opts)
doc.addEventListener('touchmove', this.#onTouchMove.bind(this), opts)
doc.addEventListener('touchend', this.#onTouchEnd.bind(this))
})
this.addEventListener('relocate', ({ detail }) => {
if (detail.reason === 'selection') setSelectionTo(this.#anchor, 0)
else if (detail.reason === 'navigation') {
if (this.#anchor === 1) setSelectionTo(detail.range, 1)
else if (typeof this.#anchor === 'number')
setSelectionTo(detail.range, -1)
else setSelectionTo(this.#anchor, -1)
}
})
const checkPointerSelection = debounce((range, sel) => {
const selRange = sel.getRangeAt(0)
const backward = selectionIsBackward(sel)
if (backward && selRange.compareBoundaryPoints(Range.START_TO_START, range) < 0)
this.prev()
else if (!backward && selRange.compareBoundaryPoints(Range.END_TO_END, range) > 0)
this.next()
}, 700)
this.addEventListener('load', ({ detail: { doc } }) => {
let isPointerSelecting = false
doc.addEventListener('pointerdown', () => isPointerSelecting = true)
doc.addEventListener('pointerup', () => isPointerSelecting = false)
let isKeyboardSelecting = false
doc.addEventListener('keydown', () => isKeyboardSelecting = true)
doc.addEventListener('keyup', () => isKeyboardSelecting = false)
doc.addEventListener('selectionchange', () => {
if (this.scrolled) return
const range = this.#lastVisibleRange
if (!range) return
const sel = doc.getSelection()
if (!sel.rangeCount) return
if (isPointerSelecting && sel.type === 'Range')
checkPointerSelection(range, sel)
else if (isKeyboardSelecting || sel.type === 'Caret') {
const selRange = sel.getRangeAt(0).cloneRange()
const backward = selectionIsBackward(sel)
if (!backward) selRange.collapse()
this.#scrollToAnchor(selRange)
}
})
doc.addEventListener('focusin', e => this.scrolled ? null :
// NOTE: `requestAnimationFrame` is needed in WebKit
requestAnimationFrame(() => this.#scrollToAnchor(e.target)))
})
this.#mediaQueryListener = () => {
if (!this.#view) return
this.#background.style.background = getBackground(this.#view.document)
}
this.#mediaQuery.addEventListener('change', this.#mediaQueryListener)
}
attributeChangedCallback(name, _, value) {
switch (name) {
case 'flow':
this.render()
break
case 'gap':
case 'margin':
case 'max-block-size':
case 'max-column-count':
this.#top.style.setProperty('--_' + name, value)
break
case 'max-inline-size':
// needs explicit `render()` as it doesn't necessarily resize
this.#top.style.setProperty('--_' + name, value)
this.render()
break
}
}
open(book) {
this.bookDir = book.dir
this.sections = book.sections
}
#createView() {
if (this.#view) {
this.#view.destroy()
this.#container.removeChild(this.#view.element)
}
this.#view = new View({
container: this,
onExpand: () => this.#scrollToAnchor(this.#anchor),
})
this.#container.append(this.#view.element)
return this.#view
}
#beforeRender({ vertical, rtl, background }) {
this.#vertical = vertical
this.#rtl = rtl
this.#top.classList.toggle('vertical', vertical)
// set background to `doc` background
// this is needed because the iframe does not fill the whole element
this.#background.style.background = background
const { width, height } = this.#container.getBoundingClientRect()
const size = vertical ? height : width
const style = getComputedStyle(this.#top)
const maxInlineSize = parseFloat(style.getPropertyValue('--_max-inline-size'))
const maxColumnCount = parseInt(style.getPropertyValue('--_max-column-count-spread'))
const margin = parseFloat(style.getPropertyValue('--_margin'))
this.#margin = margin
const g = parseFloat(style.getPropertyValue('--_gap')) / 100
// The gap will be a percentage of the #container, not the whole view.
// This means the outer padding will be bigger than the column gap. Let
// `a` be the gap percentage. The actual percentage for the column gap
// will be (1 - a) * a. Let us call this `b`.
//
// To make them the same, we start by shrinking the outer padding
// setting to `b`, but keep the column gap setting the same at `a`. Then
// the actual size for the column gap will be (1 - b) * a. Repeating the
// process again and again, we get the sequence
// x₁ = (1 - b) * a
// x₂ = (1 - x₁) * a
// ...
// which converges to x = (1 - x) * a. Solving for x, x = a / (1 + a).
// So to make the spacing even, we must shrink the outer padding with
// f(x) = x / (1 + x).
// But we want to keep the outer padding, and make the inner gap bigger.
// So we apply the inverse, f⁻¹ = -x / (x - 1) to the column gap.
const gap = -g / (g - 1) * size
const flow = this.getAttribute('flow')
if (flow === 'scrolled') {
// FIXME: vertical-rl only, not -lr
this.setAttribute('dir', vertical ? 'rtl' : 'ltr')
this.#top.style.padding = '0'
const columnWidth = maxInlineSize
this.heads = null
this.feet = null
this.#header.replaceChildren()
this.#footer.replaceChildren()
return { flow, margin, gap, columnWidth }
}
const divisor = Math.min(maxColumnCount, Math.ceil(size / maxInlineSize))
const columnWidth = (size / divisor) - gap
this.setAttribute('dir', rtl ? 'rtl' : 'ltr')
const marginalDivisor = vertical
? Math.min(2, Math.ceil(width / maxInlineSize))
: divisor
const marginalStyle = {
gridTemplateColumns: `repeat(${marginalDivisor}, 1fr)`,
gap: `${gap}px`,
direction: this.bookDir === 'rtl' ? 'rtl' : 'ltr',
}
Object.assign(this.#header.style, marginalStyle)
Object.assign(this.#footer.style, marginalStyle)
const heads = makeMarginals(marginalDivisor, 'head')
const feet = makeMarginals(marginalDivisor, 'foot')
this.heads = heads.map(el => el.children[0])
this.feet = feet.map(el => el.children[0])
this.#header.replaceChildren(...heads)
this.#footer.replaceChildren(...feet)
return { height, width, margin, gap, columnWidth }
}
render() {
if (!this.#view) return
this.#view.render(this.#beforeRender({
vertical: this.#vertical,
rtl: this.#rtl,
}))
this.#scrollToAnchor(this.#anchor)
}
get scrolled() {
return this.getAttribute('flow') === 'scrolled'
}
get scrollProp() {
const { scrolled } = this
return this.#vertical ? (scrolled ? 'scrollLeft' : 'scrollTop')
: scrolled ? 'scrollTop' : 'scrollLeft'
}
get sideProp() {
const { scrolled } = this
return this.#vertical ? (scrolled ? 'width' : 'height')
: scrolled ? 'height' : 'width'
}
get size() {
return this.#container.getBoundingClientRect()[this.sideProp]
}
get viewSize() {
return this.#view.element.getBoundingClientRect()[this.sideProp]
}
get start() {
return Math.abs(this.#container[this.scrollProp])
}
get end() {
return this.start + this.size
}
get page() {
return Math.floor(((this.start + this.end) / 2) / this.size)
}
get pages() {
return Math.round(this.viewSize / this.size)
}
scrollBy(dx, dy) {
const delta = this.#vertical ? dy : dx
const element = this.#container
const { scrollProp } = this
const [offset, a, b] = this.#scrollBounds
const rtl = this.#rtl
const min = rtl ? offset - b : offset - a
const max = rtl ? offset + a : offset + b
element[scrollProp] = Math.max(min, Math.min(max,
element[scrollProp] + delta))
}
snap(vx, vy) {
const velocity = this.#vertical ? vy : vx
const [offset, a, b] = this.#scrollBounds
const { start, end, pages, size } = this
const min = Math.abs(offset) - a
const max = Math.abs(offset) + b
const d = velocity * (this.#rtl ? -size : size)
const page = Math.floor(
Math.max(min, Math.min(max, (start + end) / 2
+ (isNaN(d) ? 0 : d))) / size)
this.#scrollToPage(page, 'snap').then(() => {
const dir = page <= 0 ? -1 : page >= pages - 1 ? 1 : null
if (dir) return this.#goTo({
index: this.#adjacentIndex(dir),
anchor: dir < 0 ? () => 1 : () => 0,
})
})
}
#onTouchStart(e) {
const touch = e.changedTouches[0]
this.#touchState = {
x: touch?.screenX, y: touch?.screenY,
t: e.timeStamp,
vx: 0, xy: 0,
}
}
#onTouchMove(e) {
const state = this.#touchState
if (state.pinched) return
state.pinched = globalThis.visualViewport.scale > 1
if (this.scrolled || state.pinched) return
if (e.touches.length > 1) {
if (this.#touchScrolled) e.preventDefault()
return
}
e.preventDefault()
const touch = e.changedTouches[0]
const x = touch.screenX, y = touch.screenY
const dx = state.x - x, dy = state.y - y
const dt = e.timeStamp - state.t
state.x = x
state.y = y
state.t = e.timeStamp
state.vx = dx / dt
state.vy = dy / dt
this.#touchScrolled = true
this.scrollBy(dx, dy)
}
#onTouchEnd() {
this.#touchScrolled = false
if (this.scrolled) return
// XXX: Firefox seems to report scale as 1... sometimes...?
// at this point I'm basically throwing `requestAnimationFrame` at
// anything that doesn't work
requestAnimationFrame(() => {
if (globalThis.visualViewport.scale === 1)
this.snap(this.#touchState.vx, this.#touchState.vy)
})
}
// allows one to process rects as if they were LTR and horizontal
#getRectMapper() {
if (this.scrolled) {
const size = this.viewSize
const margin = this.#margin
return this.#vertical
? ({ left, right }) =>
({ left: size - right - margin, right: size - left - margin })
: ({ top, bottom }) => ({ left: top + margin, right: bottom + margin })
}
const pxSize = this.pages * this.size
return this.#rtl
? ({ left, right }) =>
({ left: pxSize - right, right: pxSize - left })
: this.#vertical
? ({ top, bottom }) => ({ left: top, right: bottom })
: f => f
}
async #scrollToRect(rect, reason) {
if (this.scrolled) {
const offset = this.#getRectMapper()(rect).left - this.#margin
return this.#scrollTo(offset, reason)
}
const offset = this.#getRectMapper()(rect).left
return this.#scrollToPage(Math.floor(offset / this.size) + (this.#rtl ? -1 : 1), reason)
}
async #scrollTo(offset, reason, smooth) {
const element = this.#container
const { scrollProp, size } = this
if (element[scrollProp] === offset) {
this.#scrollBounds = [offset, this.atStart ? 0 : size, this.atEnd ? 0 : size]
this.#afterScroll(reason)
return
}
// FIXME: vertical-rl only, not -lr
if (this.scrolled && this.#vertical) offset = -offset
if ((reason === 'snap' || smooth) && this.hasAttribute('animated')) return animate(
element[scrollProp], offset, 300, easeOutQuad,
x => element[scrollProp] = x,
).then(() => {
this.#scrollBounds = [offset, this.atStart ? 0 : size, this.atEnd ? 0 : size]
this.#afterScroll(reason)
})
else {
element[scrollProp] = offset
this.#scrollBounds = [offset, this.atStart ? 0 : size, this.atEnd ? 0 : size]
this.#afterScroll(reason)
}
}
async #scrollToPage(page, reason, smooth) {
const offset = this.size * (this.#rtl ? -page : page)
return this.#scrollTo(offset, reason, smooth)
}
async scrollToAnchor(anchor, select) {
return this.#scrollToAnchor(anchor, select ? 'selection' : 'navigation')
}
async #scrollToAnchor(anchor, reason = 'anchor') {
this.#anchor = anchor
const rects = uncollapse(anchor)?.getClientRects?.()
// if anchor is an element or a range
if (rects) {
// when the start of the range is immediately after a hyphen in the
// previous column, there is an extra zero width rect in that column
const rect = Array.from(rects)
.find(r => r.width > 0 && r.height > 0) || rects[0]
if (!rect) return
await this.#scrollToRect(rect, reason)
return
}
// if anchor is a fraction
if (this.scrolled) {
await this.#scrollTo(anchor * this.viewSize, reason)
return
}
const { pages } = this
if (!pages) return
const textPages = pages - 2
const newPage = Math.round(anchor * (textPages - 1))
await this.#scrollToPage(newPage + 1, reason)
}
#getVisibleRange() {
if (this.scrolled) return getVisibleRange(this.#view.document,
this.start + this.#margin, this.end - this.#margin, this.#getRectMapper())
const size = this.#rtl ? -this.size : this.size
return getVisibleRange(this.#view.document,
this.start - size, this.end - size, this.#getRectMapper())
}
#afterScroll(reason) {
const range = this.#getVisibleRange()
this.#lastVisibleRange = range
// don't set new anchor if relocation was to scroll to anchor
if (reason !== 'selection' && reason !== 'navigation' && reason !== 'anchor')
this.#anchor = range
else this.#justAnchored = true
const index = this.#index
const detail = { reason, range, index }
if (this.scrolled) detail.fraction = this.start / this.viewSize
else if (this.pages > 0) {
const { page, pages } = this
this.#header.style.visibility = page > 1 ? 'visible' : 'hidden'
detail.fraction = (page - 1) / (pages - 2)
detail.size = 1 / (pages - 2)
}
this.dispatchEvent(new CustomEvent('relocate', { detail }))
}
async #display(promise) {
const { index, src, anchor, onLoad, select } = await promise
this.#index = index
const hasFocus = this.#view?.document?.hasFocus()
if (src) {
const view = this.#createView()
const afterLoad = doc => {
if (doc.head) {
const $styleBefore = doc.createElement('style')
doc.head.prepend($styleBefore)
const $style = doc.createElement('style')
doc.head.append($style)
this.#styleMap.set(doc, [$styleBefore, $style])
}
onLoad?.({ doc, index })
}
const beforeRender = this.#beforeRender.bind(this)
await view.load(src, afterLoad, beforeRender)
this.dispatchEvent(new CustomEvent('create-overlayer', {
detail: {
doc: view.document, index,
attach: overlayer => view.overlayer = overlayer,
},
}))
this.#view = view
}
await this.scrollToAnchor((typeof anchor === 'function'
? anchor(this.#view.document) : anchor) ?? 0, select)
if (hasFocus) this.focusView()
}
#canGoToIndex(index) {
return index >= 0 && index <= this.sections.length - 1
}
async #goTo({ index, anchor, select}) {
if (index === this.#index) await this.#display({ index, anchor, select })
else {
const oldIndex = this.#index
const onLoad = detail => {
this.sections[oldIndex]?.unload?.()
this.setStyles(this.#styles)
this.dispatchEvent(new CustomEvent('load', { detail }))
}
await this.#display(Promise.resolve(this.sections[index].load())
.then(src => ({ index, src, anchor, onLoad, select }))
.catch(e => {
console.warn(e)
console.warn(new Error(`Failed to load section ${index}`))
return {}
}))
}
}
async goTo(target) {
if (this.#locked) return
const resolved = await target
if (this.#canGoToIndex(resolved.index)) return this.#goTo(resolved)
}
#scrollPrev(distance) {