-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fixed issue with threshold translation #882 and add NodeContext…
…Menu (#906) ### What problem does this PR solve? feat: fixed issue with threshold translation #882 feat: add NodeContextMenu ### Type of change - [ ] New Feature (non-breaking change which adds functionality)
- Loading branch information
Showing
9 changed files
with
263 additions
and
57 deletions.
There are no files selected for viewing
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,18 @@ | ||
.contextMenu { | ||
background: white; | ||
border-style: solid; | ||
box-shadow: 10px 19px 20px rgba(0, 0, 0, 10%); | ||
position: absolute; | ||
z-index: 10; | ||
button { | ||
border: none; | ||
display: block; | ||
padding: 0.5em; | ||
text-align: left; | ||
width: 100%; | ||
} | ||
|
||
button:hover { | ||
background: white; | ||
} | ||
} |
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,108 @@ | ||
import { useCallback, useRef, useState } from 'react'; | ||
import { NodeMouseHandler, useReactFlow } from 'reactflow'; | ||
|
||
import styles from './index.less'; | ||
|
||
export interface INodeContextMenu { | ||
id: string; | ||
top: number; | ||
left: number; | ||
right?: number; | ||
bottom?: number; | ||
[key: string]: unknown; | ||
} | ||
|
||
export function NodeContextMenu({ | ||
id, | ||
top, | ||
left, | ||
right, | ||
bottom, | ||
...props | ||
}: INodeContextMenu) { | ||
const { getNode, setNodes, addNodes, setEdges } = useReactFlow(); | ||
|
||
const duplicateNode = useCallback(() => { | ||
const node = getNode(id); | ||
const position = { | ||
x: node?.position?.x || 0 + 50, | ||
y: node?.position?.y || 0 + 50, | ||
}; | ||
|
||
addNodes({ | ||
...(node || {}), | ||
data: node?.data, | ||
selected: false, | ||
dragging: false, | ||
id: `${node?.id}-copy`, | ||
position, | ||
}); | ||
}, [id, getNode, addNodes]); | ||
|
||
const deleteNode = useCallback(() => { | ||
setNodes((nodes) => nodes.filter((node) => node.id !== id)); | ||
setEdges((edges) => edges.filter((edge) => edge.source !== id)); | ||
}, [id, setNodes, setEdges]); | ||
|
||
return ( | ||
<div | ||
style={{ top, left, right, bottom }} | ||
className={styles.contextMenu} | ||
{...props} | ||
> | ||
<p style={{ margin: '0.5em' }}> | ||
<small>node: {id}</small> | ||
</p> | ||
<button onClick={duplicateNode} type={'button'}> | ||
duplicate | ||
</button> | ||
<button onClick={deleteNode} type={'button'}> | ||
delete | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export const useHandleNodeContextMenu = (sideWidth: number) => { | ||
const [menu, setMenu] = useState<INodeContextMenu>({} as INodeContextMenu); | ||
const ref = useRef<any>(null); | ||
|
||
const onNodeContextMenu: NodeMouseHandler = useCallback( | ||
(event, node) => { | ||
// Prevent native context menu from showing | ||
event.preventDefault(); | ||
|
||
// Calculate position of the context menu. We want to make sure it | ||
// doesn't get positioned off-screen. | ||
const pane = ref.current?.getBoundingClientRect(); | ||
// setMenu({ | ||
// id: node.id, | ||
// top: event.clientY < pane.height - 200 ? event.clientY : 0, | ||
// left: event.clientX < pane.width - 200 ? event.clientX : 0, | ||
// right: event.clientX >= pane.width - 200 ? pane.width - event.clientX : 0, | ||
// bottom: | ||
// event.clientY >= pane.height - 200 ? pane.height - event.clientY : 0, | ||
// }); | ||
|
||
console.info('clientX:', event.clientX); | ||
console.info('clientY:', event.clientY); | ||
|
||
setMenu({ | ||
id: node.id, | ||
top: event.clientY - 72, | ||
left: event.clientX - sideWidth, | ||
// top: event.clientY < pane.height - 200 ? event.clientY - 72 : 0, | ||
// left: event.clientX < pane.width - 200 ? event.clientX : 0, | ||
}); | ||
}, | ||
[sideWidth], | ||
); | ||
|
||
// Close the context menu if it's open whenever the window is clicked. | ||
const onPaneClick = useCallback( | ||
() => setMenu({} as INodeContextMenu), | ||
[setMenu], | ||
); | ||
|
||
return { onNodeContextMenu, menu, onPaneClick, ref }; | ||
}; |
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,20 @@ | ||
import { IModalProps } from '@/interfaces/common'; | ||
import { Drawer } from 'antd'; | ||
|
||
const FlowDrawer = ({ visible, hideModal }: IModalProps<any>) => { | ||
return ( | ||
<Drawer | ||
title="Basic Drawer" | ||
placement="right" | ||
// closable={false} | ||
onClose={hideModal} | ||
open={visible} | ||
getContainer={false} | ||
mask={false} | ||
> | ||
<p>Some contents...</p> | ||
</Drawer> | ||
); | ||
}; | ||
|
||
export default FlowDrawer; |
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.