Skip to content

Commit fb825f7

Browse files
authoredSep 23, 2024··
feat(ui): move graphs by dragging mouse in app resource tree view (#18025) (#20009)
Signed-off-by: linghaoSu <linghao.su@daocloud.io>
1 parent eba559a commit fb825f7

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
 

‎ui/src/app/applications/components/application-resource-tree/application-resource-tree.tsx

+57
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,58 @@ export const ApplicationResourceTree = (props: ApplicationResourceTreeProps) =>
11911191
});
11921192
const graphNodes = graph.nodes();
11931193
const size = getGraphSize(graphNodes.map(id => graph.node(id)));
1194+
1195+
const resourceTreeRef = React.useRef<HTMLDivElement>();
1196+
1197+
const graphMoving = React.useRef({
1198+
enable: false,
1199+
x: 0,
1200+
y: 0
1201+
});
1202+
1203+
const onGraphDragStart: React.PointerEventHandler<HTMLDivElement> = e => {
1204+
if (e.target !== resourceTreeRef.current) {
1205+
return;
1206+
}
1207+
1208+
if (!resourceTreeRef.current?.parentElement) {
1209+
return;
1210+
}
1211+
1212+
graphMoving.current.enable = true;
1213+
graphMoving.current.x = e.clientX;
1214+
graphMoving.current.y = e.clientY;
1215+
};
1216+
1217+
const onGraphDragMoving: React.PointerEventHandler<HTMLDivElement> = e => {
1218+
if (!graphMoving.current.enable) {
1219+
return;
1220+
}
1221+
1222+
if (!resourceTreeRef.current?.parentElement) {
1223+
return;
1224+
}
1225+
1226+
const graphContainer = resourceTreeRef.current?.parentElement;
1227+
1228+
const currentPositionX = graphContainer.scrollLeft;
1229+
const currentPositionY = graphContainer.scrollTop;
1230+
1231+
const scrollLeft = currentPositionX + graphMoving.current.x - e.clientX;
1232+
const scrollTop = currentPositionY + graphMoving.current.y - e.clientY;
1233+
1234+
graphContainer.scrollTo(scrollLeft, scrollTop);
1235+
1236+
graphMoving.current.x = e.clientX;
1237+
graphMoving.current.y = e.clientY;
1238+
};
1239+
1240+
const onGraphDragEnd: React.PointerEventHandler<HTMLDivElement> = e => {
1241+
if (graphMoving.current.enable) {
1242+
graphMoving.current.enable = false;
1243+
e.preventDefault();
1244+
}
1245+
};
11941246
return (
11951247
(graphNodes.length === 0 && (
11961248
<EmptyState icon=' fa fa-network-wired'>
@@ -1199,6 +1251,11 @@ export const ApplicationResourceTree = (props: ApplicationResourceTreeProps) =>
11991251
</EmptyState>
12001252
)) || (
12011253
<div
1254+
ref={resourceTreeRef}
1255+
onPointerDown={onGraphDragStart}
1256+
onPointerMove={onGraphDragMoving}
1257+
onPointerUp={onGraphDragEnd}
1258+
onPointerLeave={onGraphDragEnd}
12021259
className={classNames('application-resource-tree', {'application-resource-tree--network': props.useNetworkingHierarchy})}
12031260
style={{width: size.width + 150, height: size.height + 250, transformOrigin: '0% 0%', transform: `scale(${props.zoom})`}}>
12041261
{graphNodes.map(key => {

0 commit comments

Comments
 (0)
Please sign in to comment.