refactor(fitview-nodes): cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import ReactFlow, {
|
import ReactFlow, {
|
||||||
Controls,
|
Controls,
|
||||||
ReactFlowProvider,
|
ReactFlowProvider,
|
||||||
@@ -10,6 +10,8 @@ import ReactFlow, {
|
|||||||
useEdgesState,
|
useEdgesState,
|
||||||
MarkerType,
|
MarkerType,
|
||||||
EdgeMarker,
|
EdgeMarker,
|
||||||
|
Panel,
|
||||||
|
useReactFlow,
|
||||||
} from 'reactflow';
|
} from 'reactflow';
|
||||||
|
|
||||||
import dagre from 'dagre';
|
import dagre from 'dagre';
|
||||||
@@ -29,6 +31,7 @@ const nodeExtent: CoordinateExtent = [
|
|||||||
const LayoutFlow = () => {
|
const LayoutFlow = () => {
|
||||||
const [nodes, setNodes, onNodesChange] = useNodesState(initialItems.nodes);
|
const [nodes, setNodes, onNodesChange] = useNodesState(initialItems.nodes);
|
||||||
const [edges, setEdges, onEdgesChange] = useEdgesState(initialItems.edges);
|
const [edges, setEdges, onEdgesChange] = useEdgesState(initialItems.edges);
|
||||||
|
const { fitView } = useReactFlow();
|
||||||
|
|
||||||
const onConnect = useCallback(
|
const onConnect = useCallback(
|
||||||
(connection: Connection) => {
|
(connection: Connection) => {
|
||||||
@@ -85,31 +88,31 @@ const LayoutFlow = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.layoutflow}>
|
<div className={styles.layoutflow}>
|
||||||
<ReactFlowProvider>
|
<ReactFlow
|
||||||
<ReactFlow
|
nodes={nodes}
|
||||||
nodes={nodes}
|
edges={edges}
|
||||||
edges={edges}
|
onConnect={onConnect}
|
||||||
onConnect={onConnect}
|
nodeExtent={nodeExtent}
|
||||||
nodeExtent={nodeExtent}
|
onInit={() => onLayout('TB')}
|
||||||
onInit={() => onLayout('TB')}
|
onNodesChange={onNodesChange}
|
||||||
onNodesChange={onNodesChange}
|
onEdgesChange={onEdgesChange}
|
||||||
onEdgesChange={onEdgesChange}
|
>
|
||||||
>
|
<Controls />
|
||||||
<Controls />
|
</ReactFlow>
|
||||||
</ReactFlow>
|
<Panel position="top-right">
|
||||||
<div className={styles.controls}>
|
<button onClick={() => onLayout('TB')}>vertical layout</button>
|
||||||
<button onClick={() => onLayout('TB')} style={{ marginRight: 10 }}>
|
<button onClick={() => onLayout('LR')}>horizontal layout</button>
|
||||||
vertical layout
|
<button onClick={() => unselect()}>unselect nodes</button>
|
||||||
</button>
|
<button onClick={() => changeMarker()}>change marker</button>
|
||||||
<button onClick={() => onLayout('LR')} style={{ marginRight: 10 }}>
|
<button onClick={() => fitView()}>fitView</button>
|
||||||
horizontal layout
|
<button onClick={() => fitView({ nodes: nodes.slice(0, 2) })}>fitView partially</button>
|
||||||
</button>
|
</Panel>
|
||||||
<button onClick={() => unselect()}>unselect nodes</button>
|
|
||||||
<button onClick={() => changeMarker()}>change marker</button>
|
|
||||||
</div>
|
|
||||||
</ReactFlowProvider>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default LayoutFlow;
|
export default () => (
|
||||||
|
<ReactFlowProvider>
|
||||||
|
<LayoutFlow />
|
||||||
|
</ReactFlowProvider>
|
||||||
|
);
|
||||||
|
|||||||
@@ -2,10 +2,3 @@
|
|||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
.controls {
|
|
||||||
position: absolute;
|
|
||||||
right: 10px;
|
|
||||||
top: 10px;
|
|
||||||
z-index: 10;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -143,14 +143,13 @@ export function fitView(get: StoreApi<ReactFlowState>['getState'], options: Inte
|
|||||||
|
|
||||||
if (d3initialized && (isInitialFitView || !options.initial)) {
|
if (d3initialized && (isInitialFitView || !options.initial)) {
|
||||||
const nodes = getNodes().filter((n) => {
|
const nodes = getNodes().filter((n) => {
|
||||||
const isVisible = (options.includeHiddenNodes ? n.width && n.height : !n.hidden);
|
const isVisible = options.includeHiddenNodes ? n.width && n.height : !n.hidden;
|
||||||
let shouldInclude = true;
|
|
||||||
|
|
||||||
if (options.nodes?.length) {
|
if (options.nodes?.length) {
|
||||||
shouldInclude = options.nodes.includes(n.id);
|
return isVisible && options.nodes.some((optionNode) => optionNode.id === n.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isVisible && shouldInclude;
|
return isVisible;
|
||||||
});
|
});
|
||||||
|
|
||||||
const nodesInitialized = nodes.every((n) => n.width && n.height);
|
const nodesInitialized = nodes.every((n) => n.width && n.height);
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ export type FitViewOptions = {
|
|||||||
minZoom?: number;
|
minZoom?: number;
|
||||||
maxZoom?: number;
|
maxZoom?: number;
|
||||||
duration?: number;
|
duration?: number;
|
||||||
nodes?: string[];
|
nodes?: (Partial<Node> & { id: Node['id'] })[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OnConnectStartParams = {
|
export type OnConnectStartParams = {
|
||||||
|
|||||||
Reference in New Issue
Block a user