position-items.ts 2.6 KB

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