1
1
import { Tree as AntdTree , type TreeProps } from "antd" ;
2
2
import type { BasicDataNode } from "antd/es/tree" ;
3
3
import { throttle } from "lodash" ;
4
- import Constants from "oxalis/constants" ;
5
4
import { useCallback , useRef } from "react" ;
6
5
import type RcTree from "rc-tree" ;
7
6
8
- const SCROLL_SPEED_PX = 32 ;
9
- const MIN_SCROLL_AREA_HEIGHT = 48 ;
7
+ const MIN_SCROLL_SPEED = 30 ;
8
+ const MAX_SCROLL_SPEED = 200 ;
9
+ const MIN_SCROLL_AREA_HEIGHT = 60 ;
10
10
const SCROLL_AREA_RATIO = 10 ; // 1/10th of the container height
11
+ const THROTTLE_TIME = 25 ;
11
12
12
13
function ScrollableVirtualizedTree < T extends BasicDataNode > (
13
14
props : TreeProps < T > & { ref : React . RefObject < RcTree > } ,
@@ -23,18 +24,33 @@ function ScrollableVirtualizedTree<T extends BasicDataNode>(
23
24
const { bottom : currentBottom , top : currentTop } = target . getBoundingClientRect ( ) ;
24
25
const { bottom : boxBottom , top : boxTop } = wrapperRef . current . getBoundingClientRect ( ) ;
25
26
const scrollableList = wrapperRef . current . getElementsByClassName ( "ant-tree-list-holder" ) [ 0 ] ;
27
+ if ( ! scrollableList ) {
28
+ return ;
29
+ }
26
30
const scrollAreaHeight = Math . max (
27
31
MIN_SCROLL_AREA_HEIGHT ,
28
32
Math . round ( ( boxBottom - boxTop ) / SCROLL_AREA_RATIO ) ,
29
33
) ;
30
34
31
35
if ( currentTop > boxBottom - scrollAreaHeight && scrollableList ) {
32
- scrollableList . scrollTop += SCROLL_SPEED_PX ;
36
+ const ratioWithinScrollingArea =
37
+ ( currentTop - ( boxBottom - scrollAreaHeight ) ) / scrollAreaHeight ;
38
+ const scrollingValue = Math . max (
39
+ Math . round ( ratioWithinScrollingArea * MAX_SCROLL_SPEED ) ,
40
+ MIN_SCROLL_SPEED ,
41
+ ) ;
42
+ scrollableList . scrollTop += scrollingValue ;
33
43
}
34
44
if ( boxTop + scrollAreaHeight > currentBottom && scrollableList ) {
35
- scrollableList . scrollTop -= SCROLL_SPEED_PX ;
45
+ const ratioWithinScrollingArea =
46
+ ( boxTop + scrollAreaHeight - currentBottom ) / scrollAreaHeight ;
47
+ const scrollingValue = Math . max (
48
+ Math . round ( ratioWithinScrollingArea * MAX_SCROLL_SPEED ) ,
49
+ MIN_SCROLL_SPEED ,
50
+ ) ;
51
+ scrollableList . scrollTop -= scrollingValue ;
36
52
}
37
- } , Constants . RESIZE_THROTTLE_TIME ) ,
53
+ } , THROTTLE_TIME ) ,
38
54
[ wrapperRef ] ,
39
55
) ;
40
56
0 commit comments