forked from infiniflow/ragflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FlowHeader and delete edge (infiniflow#959)
### What problem does this PR solve? feat: add FlowHeader and delete edge infiniflow#918 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
- Loading branch information
Showing
13 changed files
with
293 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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,35 @@ | ||
import { useCallback, useLayoutEffect } from 'react'; | ||
import { getLayoutedElements } from './elk-utils'; | ||
|
||
export const elkOptions = { | ||
'elk.algorithm': 'layered', | ||
'elk.layered.spacing.nodeNodeBetweenLayers': '100', | ||
'elk.spacing.nodeNode': '80', | ||
}; | ||
|
||
export const useLayoutGraph = ( | ||
initialNodes, | ||
initialEdges, | ||
setNodes, | ||
setEdges, | ||
) => { | ||
const onLayout = useCallback(({ direction, useInitialNodes = false }) => { | ||
const opts = { 'elk.direction': direction, ...elkOptions }; | ||
const ns = initialNodes; | ||
const es = initialEdges; | ||
|
||
getLayoutedElements(ns, es, opts).then( | ||
({ nodes: layoutedNodes, edges: layoutedEdges }) => { | ||
setNodes(layoutedNodes); | ||
setEdges(layoutedEdges); | ||
|
||
// window.requestAnimationFrame(() => fitView()); | ||
}, | ||
); | ||
}, []); | ||
|
||
// Calculate the initial layout on mount. | ||
useLayoutEffect(() => { | ||
onLayout({ direction: 'RIGHT', useInitialNodes: true }); | ||
}, [onLayout]); | ||
}; |
This file contains 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,42 @@ | ||
import ELK from 'elkjs/lib/elk.bundled.js'; | ||
import { Edge, Node } from 'reactflow'; | ||
|
||
const elk = new ELK(); | ||
|
||
export const getLayoutedElements = ( | ||
nodes: Node[], | ||
edges: Edge[], | ||
options = {}, | ||
) => { | ||
const isHorizontal = options?.['elk.direction'] === 'RIGHT'; | ||
const graph = { | ||
id: 'root', | ||
layoutOptions: options, | ||
children: nodes.map((node) => ({ | ||
...node, | ||
// Adjust the target and source handle positions based on the layout | ||
// direction. | ||
targetPosition: isHorizontal ? 'left' : 'top', | ||
sourcePosition: isHorizontal ? 'right' : 'bottom', | ||
|
||
// Hardcode a width and height for elk to use when layouting. | ||
width: 150, | ||
height: 50, | ||
})), | ||
edges: edges, | ||
}; | ||
|
||
return elk | ||
.layout(graph) | ||
.then((layoutedGraph) => ({ | ||
nodes: layoutedGraph.children.map((node) => ({ | ||
...node, | ||
// React Flow expects a position property on the node instead of `x` | ||
// and `y` fields. | ||
position: { x: node.x, y: node.y }, | ||
})), | ||
|
||
edges: layoutedGraph.edges, | ||
})) | ||
.catch(console.error); | ||
}; |
This file contains 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,3 @@ | ||
.flowHeader { | ||
padding: 20px; | ||
} |
This file contains 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,26 @@ | ||
import { Button, Flex } from 'antd'; | ||
|
||
import { useSaveGraph } from '../hooks'; | ||
import styles from './index.less'; | ||
|
||
const FlowHeader = () => { | ||
const { saveGraph } = useSaveGraph(); | ||
|
||
return ( | ||
<Flex | ||
align="center" | ||
justify="end" | ||
gap={'large'} | ||
className={styles.flowHeader} | ||
> | ||
<Button> | ||
<b>Debug</b> | ||
</Button> | ||
<Button type="primary" onClick={saveGraph}> | ||
<b>Save</b> | ||
</Button> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default FlowHeader; |
This file contains 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 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
Oops, something went wrong.