Skip to content

Commit

Permalink
feat: add DragNodeWithForce Behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Mar 8, 2021
1 parent cf4c9b9 commit 89f7696
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/graphin/src/Graphin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface GraphinState {
graph: IGraph;
apis: ApisType;
theme: ThemeData;
layout: LayoutController;
};
}

Expand Down Expand Up @@ -141,18 +142,18 @@ class Graphin extends React.PureComponent<GraphinProps, GraphinState> {

this.theme = {} as ThemeData;
this.apis = {} as ApisType;
this.layout = {} as LayoutController;
this.options = { ...otherOptions } as GraphOptions;

this.state = {
isReady: false,
context: {
graph: this.graph,
apis: this.apis,
theme: this.theme,
layout: this.layout,
},
};

this.options = { ...otherOptions } as GraphOptions;
this.layout = {} as LayoutController;
}

initData = (data: GraphinProps['data']) => {
Expand Down Expand Up @@ -275,6 +276,7 @@ class Graphin extends React.PureComponent<GraphinProps, GraphinState> {
graph: this.graph,
apis: this.apis,
theme: this.theme,
layout: this.layout,
},
});
};
Expand Down Expand Up @@ -362,6 +364,7 @@ class Graphin extends React.PureComponent<GraphinProps, GraphinState> {
graph: this.graph,
apis: this.apis,
theme: this.theme,
layout: this.layout,
},
};
},
Expand Down
4 changes: 4 additions & 0 deletions packages/graphin/src/GraphinContext.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from 'react';
import { Graph as IGraph } from '@antv/g6';
import { ApisType } from './apis/types';
import { ThemeType } from './theme/index';
import LayoutController from './layout';

const defaultContext = {
graph: {} as IGraph,
apis: {} as ApisType,
theme: {} as ThemeType,
layout: {} as LayoutController,
};
export interface GraphinContextType {
graph: IGraph;
apis: ApisType;
theme: ThemeType;
layout: LayoutController;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
Expand Down
59 changes: 59 additions & 0 deletions packages/graphin/src/behaviors/DragNodeWithForce.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useEffect } from 'react';
import GraphinContext from '../GraphinContext';

import { IG6GraphEvent } from '@antv/g6';

export interface DragNodeWithForceProps {
/**
* @description 琚嫋鎷界殑鑺傜偣锛屾槸鍚﹁嚜鍔ㄥ浐瀹氫綇
* @description.en-US Whether the dragged node is automatically fixed
* @default false
*/
autoPin?: boolean;
}
const DragNodeWithForce = (props: DragNodeWithForceProps) => {
const { graph, layout } = React.useContext(GraphinContext);
const { autoPin } = props;

useEffect(() => {
const { instance } = layout;
const { simulation, type } = instance;
console.log(instance, layout);
const handleNodeDragStart = () => {
if (simulation) {
simulation.stop();
}
};
const handleNodeDragEnd = (e: IG6GraphEvent) => {
if (type !== 'graphin-force') {
return;
}

if (e.item) {
console.log('e.item', instance);
const nodeModel = e.item.get('model');
nodeModel.x = e.x;
nodeModel.y = e.y;
nodeModel.layout = {
...nodeModel.layout,
force: {
mass: autoPin ? 1000000 : null,
},
};
const drageNodes = [nodeModel];
simulation.restart(drageNodes, graph);
graph.refreshPositions();
}
};

graph.on('node:dragstart', handleNodeDragStart);
graph.on('node:dragend', handleNodeDragEnd);
return () => {
graph.off('node:dragstart', handleNodeDragStart);
graph.off('node:dragend', handleNodeDragEnd);
};
}, [graph, autoPin]);
return null;
};

export default DragNodeWithForce;
2 changes: 2 additions & 0 deletions packages/graphin/src/behaviors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ActivateRelations from './ActivateRelations';
import Hoverable from './Hoverable';
import FitView from './FitView';
import FontPaint from './FontPaint';
import DragNodeWithForce from './DragNodeWithForce';

export default {
/** 鍐呯疆 */
Expand All @@ -29,4 +30,5 @@ export default {
TreeCollapse,
FitView,
FontPaint,
DragNodeWithForce,
};

0 comments on commit 89f7696

Please sign in to comment.