gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit956069b8956069b8Records: R1 result, mission 007mre956069b8/components/virtual-list/virtual-list.hl

9.2 KB

  1. // virtual-list.hl — THE VIRTUAL LIST: a very long list (tens of thousands of rows) that scrolls smoothly on a phone because only the rows
  2. // near the viewport exist in the page. Every row has the same fixed height, so the list knows where row N is without measuring: a spacer
  3. // as tall as the whole list (`itemCount * rowHeight`) makes the scroll bar honest, and the few rows that are near the viewport are placed in it.
  4. // The component does not fetch anything. The HOST owns the data: it hands in the rows it has loaded (`loaded`, the first of them is row number
  5. // `loadedFrom`), and the component calls the host's `onRange(first, count)` when the rows near the viewport are not all there yet; the host loads
  6. // them (usually page by page from its server) and hands in the new `loaded` / `loadedFrom`. Until they arrive the missing rows are blank rows of the same
  7. // height, so the list never jumps. A row is `{ id, title, sub, aside, img }`: a thumbnail (`img`, loaded lazily; a plain grey square when it is empty),
  8. // the title with a second line under it, and a short text at the right (a play count, a length). A tap calls `onPick(id, index)`.
  9. //
  10. // VirtualList { itemCount = 13449 rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId }
  11. //
  12. // Anything written inside the tag (`VirtualList { … h1 { "Title" } }`) stands above the rows and scrolls away with them.
  13. // The host gives the list a height (it scrolls inside its own box): `virtual-list { height: 70dvh }` or a flex child. `emit vlTop()` scrolls back to the
  14. // first row (after the host changed the sort order or the filter). USE ONE PER PAGE: a composed component's members are the page's state, so the
  15. // public names below are chosen to stay clear of a host's own (no `rows`, `total`, `current`, `offset`).
  16. import Icon from '../icons/icons.hl'
  17. Number itemCount = 0
  18. Number rowHeight = 56
  19. Number overscan = 10
  20. Hybrid loaded = []
  21. Number loadedFrom = 0
  22. String selectedId = ''
  23. String listLabel = 'List'
  24. String emptyText = ''
  25. String thumbs = 'true'
  26. onRange = null
  27. onPick = null
  28. slot = null
  29. // what the scroll position says: the first row that exists in the page and how many exist
  30. Number vlFirst = 0
  31. Number vlCount = 30
  32. String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'
  33. String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'
  34. String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'
  35. Boolean isEmpty = itemCount == 0 && emptyText != ''
  36. Hybrid shown = shownOf(loaded, loadedFrom, vlFirst, vlCount, itemCount, selectedId)
  37. // the rows that exist in the page: from the host's loaded rows, a blank row where the host has not delivered one yet
  38. shownOf(rows, from0, first0, count0, n, sel) {
  39. out = []
  40. let i = first0
  41. let end = first0 + count0
  42. if (end > n) { end = n }
  43. while (i < end) {
  44. let k = i - from0
  45. if (k >= 0 && k < rows.length) {
  46. r = rows[k]
  47. out.push({ idx = '' + i id = r.id title = r.title sub = r.sub aside = r.aside img = r.img hasImg = r.img != '' cls = r.id == sel ? 'vl-row current' : 'vl-row' pos = i + 1 })
  48. } else {
  49. out.push({ idx = '' + i id = '' title = '' sub = '' aside = '' img = '' hasImg = false cls = 'vl-row blank' pos = i + 1 })
  50. }
  51. i = i + 1
  52. }
  53. return out
  54. }
  55. Style {
  56. 'virtual-list' { display = 'block' position = 'relative' height = '100%' minHeight = '0' color = 'var(--color-text, #000)' }
  57. // the box that scrolls (the host gives virtual-list its height); the mount tick (the code editor's trick) reads the real size once the page is live
  58. '@keyframes virtual-list-mount' { 'from' { opacity = '.999' } to { opacity = '1' } }
  59. 'virtual-list .vl-scroller' { position = 'absolute' inset = '0' overflowY = 'auto' overflowX = 'hidden' overscrollBehavior = 'contain' animation = 'virtual-list-mount 100ms linear infinite' }
  60. 'virtual-list .vl-scroller:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }
  61. 'virtual-list .vl-head' { display = 'block' }
  62. 'virtual-list .vl-head:empty' { display = 'none' }
  63. 'virtual-list .vl-spacer' { position = 'relative' width = '100%' }
  64. 'virtual-list .vl-window' { position = 'absolute' left = '0' right = '0' top = '0' willChange = 'transform' }
  65. // a row: exactly rowHeight tall (the tap area is the row), thumbnail · title over sub · aside; no frames, no gaps between rows
  66. 'virtual-list .vl-row' { display = 'flex' alignItems = 'center' gap = '.7rem' boxSizing = 'border-box' width = '100%' height = 'var(--vl-row)' margin = '0' padding = '0 .6rem' font = 'inherit' fontWeight = '400' textAlign = 'left' color = 'var(--color-text, #000)' background = 'transparent' border = '0' borderRadius = 'var(--radius-small, 5px)' cursor = 'pointer' overflow = 'hidden' }
  67. 'virtual-list .vl-row:hover' { background = 'var(--color-surface, #f2f2f2)' }
  68. 'virtual-list .vl-row:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }
  69. 'virtual-list .vl-row.current' { color = 'var(--color-accent, #000)' background = 'var(--color-surface, #f2f2f2)' }
  70. 'virtual-list .vl-row.blank' { cursor = 'default' pointerEvents = 'none' }
  71. 'virtual-list .vl-thumb' { display = 'block' flex = 'none' width = 'calc(var(--vl-row) - 1rem)' height = 'calc(var(--vl-row) - 1rem)' borderRadius = 'var(--radius-small, 5px)' background = 'var(--color-surface, #ddd)' overflow = 'hidden' }
  72. // on the marked row the grey of the empty thumbnail would be the row's own surface: it takes the darker sunken colour
  73. 'virtual-list .vl-row.current .vl-thumb' { background = 'var(--color-background-sunken, #ccc)' }
  74. 'virtual-list .vl-row.blank .vl-thumb' { background = 'transparent' }
  75. 'virtual-list .vl-thumb img' { display = 'block' width = '100%' height = '100%' objectFit = 'cover' }
  76. 'virtual-list.vl-plain .vl-thumb' { display = 'none' }
  77. 'virtual-list .vl-text' { display = 'flex' flexDirection = 'column' justifyContent = 'center' flex = '1 1 auto' minWidth = '0' }
  78. 'virtual-list .vl-title' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' }
  79. 'virtual-list .vl-sub' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' fontSize = '.85rem' color = 'var(--color-text-muted, #555)' }
  80. 'virtual-list .vl-row.current .vl-sub' { color = 'inherit' }
  81. 'virtual-list .vl-aside' { display = 'block' flex = 'none' fontSize = '.85rem' fontVariantNumeric = 'tabular-nums' color = 'var(--color-text-muted, #555)' }
  82. 'virtual-list .vl-row.current .vl-aside' { color = 'inherit' }
  83. 'virtual-list .vl-empty' { margin = '0' padding = '1rem .6rem' color = 'var(--color-text-muted, #555)' }
  84. }
  85. View {
  86. virtualList { class = rootClass
  87. div { class = "vl-scroller" role = "list" tabindex = "0" 'aria-label' = listLabel 'aria-rowcount' = itemCount
  88. on scroll(e) { emit vlScroll(e) } on animationiteration(e) { emit vlMount(e) }
  89. div { class = "vl-head" slot }
  90. div { class = "vl-spacer" style = spacerStyle
  91. div { class = "vl-window" style = windowStyle
  92. for (r of shown) {
  93. button { type = "button" class = r.cls role = "listitem" 'data-index' = r.idx 'data-id' = r.id 'aria-posinset' = r.pos 'aria-setsize' = itemCount title = r.title
  94. span { class = "vl-thumb" if (r.hasImg) { img { src = r.img alt = "" loading = "lazy" decoding = "async" } } }
  95. span { class = "vl-text"
  96. span { class = "vl-title" r.title }
  97. span { class = "vl-sub" r.sub }
  98. }
  99. span { class = "vl-aside" r.aside }
  100. on click(e) { emit vlPick(e) }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. if (isEmpty) { p { class = "vl-empty" emptyText } }
  107. }
  108. }
  109. // ---- the scroll position -> the rows that exist ------------------------------------------------------------------------------
  110. // look at the scroll box: which rows should exist, and ask the host for them when it has not delivered all
  111. vlSync(box) {
  112. // what the host put in front of the rows (a title, a picture) scrolls away with them: the rows start below it
  113. head = box.querySelector('.vl-head')
  114. let top = head == null ? box.scrollTop : box.scrollTop - head.offsetHeight
  115. if (top < 0) { top = 0 }
  116. idx = (top - top % rowHeight) / rowHeight
  117. let first = idx - overscan
  118. if (first < 0) { first = 0 }
  119. fit = box.clientHeight > 0 ? box.clientHeight : window.innerHeight
  120. count = (fit - fit % rowHeight) / rowHeight + 2 + 2 * overscan
  121. if (first != vlFirst) { vlFirst = first }
  122. if (count != vlCount) { vlCount = count }
  123. let last = first + count
  124. if (last > itemCount) { last = itemCount }
  125. if (onRange != null && itemCount > 0 && (first < loadedFrom || last > loadedFrom + loaded.length)) { onRange(first, last - first) }
  126. }
  127. on vlScroll(e) {
  128. vlSync(e.target)
  129. return null
  130. }
  131. // the first tick of the mount animation: the page is live, read the real size and position, then stop the animation
  132. on vlMount(e) {
  133. e.target.style.animationName = 'none'
  134. vlSync(e.target)
  135. return null
  136. }
  137. // the host changed the order or the filter: back to the first row
  138. on vlTop() {
  139. box = document.querySelector('virtual-list .vl-scroller')
  140. if (box != null) { box.scrollTop = 0 vlSync(box) }
  141. return null
  142. }
  143. on vlPick(e) {
  144. b = e.target.closest('button')
  145. if (b == null) { return null }
  146. id = b.getAttribute('data-id')
  147. if (id == '' || onPick == null) { return null }
  148. onPick(id, toNumber(b.getAttribute('data-index')))
  149. return null
  150. }

Branches

Latest commits

  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre
  • deb8cef5Records: mission 006 startedmre
  • 4f0ad42fMission 006: jukebox#20mre
  • 800cae06Records: #21 planmre
  • 64bc7823Records: #21 causemre
  • 76639655Records: B1 questionmre
  • d7906b36Records: mission 005 startedmre
  • d3df773cMission 005: doc linemre
  • 6a5d840aMission 005: jukebox#22mre
  • 13e5e748Records: mission 004 startedmre
  • a8098271Mission 004: jukebox#21mre
  • df8f5205Records: jukebox#19-21, #11 closedmre
  • b7a4e692Records: hybriel#298mre
  • 9bcbb017Records: day38b waits for day39 fixmre
  • f80504e1Records: day38b repro sentmre
  • 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
  • 68aace7fRecords: mission 003 day38bmre
  • d105d8a9Records: mpackdb integrity check cleanmre
  • 6e980c53Records: mpackdb rule refinementmre