-
Notifications
You must be signed in to change notification settings - Fork 29
Wrap antd tree to enable scrolling while dragging #8162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelBuessemeyer
merged 9 commits into
master
from
allow-dragscroll-in-tree-compoenents
Nov 11, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cfd46cc
Wrap antd tree to enable scrolling while dragging
471c99a
add explanation to biome ignore comment
46c6501
add changelog entry
335ddde
fix typing
da374ad
extract magic numbers into constants
fbdf34d
Merge branch 'master' into allow-dragscroll-in-tree-compoenents
MichaelBuessemeyer 6525225
apply feedback
f29de65
Merge branch 'master' of github.com:scalableminds/webknossos into all…
db3f595
Merge branch 'master' into allow-dragscroll-in-tree-compoenents
MichaelBuessemeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
frontend/javascripts/oxalis/view/right-border-tabs/scrollable_virtualized_tree.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Tree as AntdTree, type TreeProps } from "antd"; | ||
import type { BasicDataNode } from "antd/es/tree"; | ||
import { throttle } from "lodash"; | ||
import { useCallback, useRef } from "react"; | ||
import type RcTree from "rc-tree"; | ||
|
||
const MIN_SCROLL_SPEED = 30; | ||
const MAX_SCROLL_SPEED = 200; | ||
const MIN_SCROLL_AREA_HEIGHT = 60; | ||
const SCROLL_AREA_RATIO = 10; // 1/10th of the container height | ||
const THROTTLE_TIME = 25; | ||
|
||
function ScrollableVirtualizedTree<T extends BasicDataNode>( | ||
props: TreeProps<T> & { ref: React.RefObject<RcTree> }, | ||
) { | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
// biome-ignore lint/correctness/useExhaustiveDependencies: biome is not smart enough to notice that the function needs to be re-created when wrapperRef changes. | ||
const onDragOver = useCallback( | ||
throttle((info: { event: React.DragEvent<HTMLDivElement> }) => { | ||
const target = info.event.target as HTMLElement; | ||
if (!target || !wrapperRef.current) { | ||
return; | ||
} | ||
const { bottom: currentBottom, top: currentTop } = target.getBoundingClientRect(); | ||
const { bottom: boxBottom, top: boxTop } = wrapperRef.current.getBoundingClientRect(); | ||
const scrollableList = wrapperRef.current.getElementsByClassName("ant-tree-list-holder")[0]; | ||
if (!scrollableList) { | ||
return; | ||
} | ||
const scrollAreaHeight = Math.max( | ||
MIN_SCROLL_AREA_HEIGHT, | ||
Math.round((boxBottom - boxTop) / SCROLL_AREA_RATIO), | ||
); | ||
|
||
if (currentTop > boxBottom - scrollAreaHeight && scrollableList) { | ||
const ratioWithinScrollingArea = | ||
(currentTop - (boxBottom - scrollAreaHeight)) / scrollAreaHeight; | ||
const scrollingValue = Math.max( | ||
Math.round(ratioWithinScrollingArea * MAX_SCROLL_SPEED), | ||
MIN_SCROLL_SPEED, | ||
); | ||
scrollableList.scrollTop += scrollingValue; | ||
} | ||
if (boxTop + scrollAreaHeight > currentBottom && scrollableList) { | ||
const ratioWithinScrollingArea = | ||
(boxTop + scrollAreaHeight - currentBottom) / scrollAreaHeight; | ||
const scrollingValue = Math.max( | ||
Math.round(ratioWithinScrollingArea * MAX_SCROLL_SPEED), | ||
MIN_SCROLL_SPEED, | ||
); | ||
scrollableList.scrollTop -= scrollingValue; | ||
} | ||
}, THROTTLE_TIME), | ||
[wrapperRef], | ||
); | ||
|
||
return ( | ||
<div ref={wrapperRef}> | ||
<AntdTree {...props} onDragOver={onDragOver} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default ScrollableVirtualizedTree; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ import { | |
Modal, | ||
Popover, | ||
Select, | ||
Tree, | ||
type MenuProps, | ||
} from "antd"; | ||
import type { DataNode } from "antd/lib/tree"; | ||
|
@@ -136,6 +135,7 @@ import { MetadataEntryTableRows } from "../metadata_table"; | |
import { SegmentStatisticsModal } from "./segment_statistics_modal"; | ||
import type { ItemType } from "antd/lib/menu/interface"; | ||
import { InputWithUpdateOnBlur } from "oxalis/view/components/input_with_update_on_blur"; | ||
import ScrollableVirtualizedTree from "../scrollable_virtualized_tree"; | ||
|
||
const SCROLL_DELAY_MS = 50; | ||
|
||
|
@@ -1904,7 +1904,7 @@ class SegmentsView extends React.Component<Props, State> { | |
overflow: "hidden", | ||
}} | ||
> | ||
<Tree | ||
<ScrollableVirtualizedTree<SegmentHierarchyNode> | ||
allowDrop={this.allowDrop} | ||
Comment on lines
+1907
to
1908
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider performance optimizations for the tree rendering The current implementation could benefit from the following performance optimizations:
Example optimization for tree items: const TreeItem = React.memo(({ node, ...props }) => {
// Tree item rendering logic
});
const titleRender = React.useCallback((node: SegmentHierarchyNode) => {
return <TreeItem node={node} {...props} />;
}, [props]); |
||
onDrop={this.onDrop} | ||
onSelect={this.onSelectTreeItem} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.