position-items.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { debounce } from 'lodash'
  2. const COLLAPSED_HEADER_HEIGHT = 75
  3. const OFFSET_FOR_ENTRIES_ABOVE = 70
  4. const GAP_BETWEEN_ENTRIES = 4
  5. export const positionItems = debounce(
  6. (
  7. element: HTMLDivElement,
  8. previousFocusedItemIndex: number,
  9. docId: string
  10. ) => {
  11. const items = Array.from(
  12. element.querySelectorAll<HTMLDivElement>('.review-panel-entry')
  13. )
  14. items.sort((a, b) => Number(a.dataset.pos) - Number(b.dataset.pos))
  15. if (!items.length) {
  16. return
  17. }
  18. let activeItemIndex = items.findIndex(item =>
  19. item.classList.contains('review-panel-entry-action')
  20. )
  21. if (activeItemIndex === -1) {
  22. // if there is no action available
  23. // check if there is manually selected entry
  24. activeItemIndex = items.findIndex(item =>
  25. item.classList.contains('review-panel-entry-selected')
  26. )
  27. }
  28. if (activeItemIndex === -1) {
  29. // if entry was not selected manually
  30. // check if there is an entry in selection and use that as the focused item
  31. activeItemIndex = items.findIndex(item =>
  32. item.classList.contains('review-panel-entry-highlighted')
  33. )
  34. }
  35. if (activeItemIndex === -1) {
  36. activeItemIndex = previousFocusedItemIndex
  37. }
  38. const activeItem = items[activeItemIndex]
  39. if (!activeItem) {
  40. return
  41. }
  42. const activeItemTop = getTopPosition(activeItem, activeItemIndex === 0)
  43. activeItem.style.top = `${activeItemTop}px`
  44. activeItem.style.visibility = 'visible'
  45. const focusedItemRect = activeItem.getBoundingClientRect()
  46. // above the active item
  47. let topLimit = activeItemTop
  48. for (let i = activeItemIndex - 1; i >= 0; i--) {
  49. const item = items[i]
  50. const rect = item.getBoundingClientRect()
  51. let top = getTopPosition(item, i === 0)
  52. const bottom = top + rect.height
  53. if (bottom > topLimit) {
  54. top = topLimit - rect.height - GAP_BETWEEN_ENTRIES
  55. }
  56. item.style.top = `${top}px`
  57. item.style.visibility = 'visible'
  58. topLimit = top
  59. }
  60. // below the active item
  61. let bottomLimit = activeItemTop + focusedItemRect.height
  62. for (let i = activeItemIndex + 1; i < items.length; i++) {
  63. const item = items[i]
  64. const rect = item.getBoundingClientRect()
  65. let top = getTopPosition(item, false)
  66. if (top < bottomLimit) {
  67. top = bottomLimit + GAP_BETWEEN_ENTRIES
  68. }
  69. item.style.top = `${top}px`
  70. item.style.visibility = 'visible'
  71. bottomLimit = top + rect.height
  72. }
  73. return {
  74. docId,
  75. activeItemIndex,
  76. }
  77. },
  78. 100,
  79. { leading: false, trailing: true, maxWait: 1000 }
  80. )
  81. function getTopPosition(item: HTMLDivElement, isFirstEntry: boolean) {
  82. const offset = isFirstEntry ? 0 : OFFSET_FOR_ENTRIES_ABOVE
  83. return Math.max(COLLAPSED_HEADER_HEIGHT + offset, Number(item.dataset.top))
  84. }