File size: 1,641 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable eslint-comments/no-unlimited-disable */
/* eslint-disable */
// @ts-nocheck (Unused file)

import { defineComponent } from 'vue'
import debounce from 'lodash/debounce'

export default function ({
  indexOffset = 0,
} = {}) {
  // @vue/component
  return defineComponent({
    watch: {
      inspectedIndex: 'refreshScrollToInspected',
    },

    mounted() {
      this.refreshScrollToInspected()
    },

    activated() {
      this.refreshScrollToInspected()
    },

    methods: {
      refreshScrollToInspected() {
        if (this.inspectedIndex) { this.scrollIntoInspected(this.inspectedIndex as number) }
      },

      scrollIntoInspected: debounce(async function (index) {
        index += indexOffset
        // Wait for defer frames (time-slicing)
        for (let f = 0; f < 2; f++) {
          await waitForFrame()
        }
        const scroller = this.$globalRefs.leftRecycleList || this.$globalRefs.leftScroll
        if (!scroller) {
          this.scrollIntoInspected(index)
          return
        }
        const parentHeight = scroller.offsetHeight
        const height = this.highDensity ? 22 : 34
        const top = index * height
        const scrollTop = scroller.scrollTop
        if (top < scrollTop) {
          scroller.scrollTop = top
        }
        else if (top + height > scrollTop + parentHeight) {
          scroller.scrollTop = top + height - parentHeight
        }
      } as (this: any, index: number) => Promise<void>, 200, {
        leading: true,
      }),
    },
  })
}

function waitForFrame() {
  return new Promise((resolve) => {
    requestAnimationFrame(resolve)
  })
}