Comments (1)
What's working for me right now is this:
const onConnectStart: OnConnectStart = useCallback((evt, node) => {
const { nodeId } = node
const pNode = nodeId ? getNode(nodeId) : {}
// @ts-ignore
parentNode.current = pNode
connectingNodeId.current = nodeId
}, [])
const onConnectEnd: OnConnectEnd = useCallback(
(event) => {
if (!connectingNodeId.current) return
const targetIsPane = (event.target as Element).classList.contains(
'react-flow__pane'
)
if (targetIsPane) {
// Get mouse position
// @ts-ignore
const clientX = (event?.clientX as number) || 0
// @ts-ignore
const clientY = (event?.clientY as number) || 0
// Convert mouse coordinates to relative position
const screenPos = screenToFlowPosition({
x: clientX,
y: clientY,
})
// Get the absolute position of the parent node (if any)
const parentX = parentNode.current?.positionAbsolute?.x || 0
const parentY = parentNode.current?.positionAbsolute?.y || 0
// Subtract parent coords from relative screenPos
const posX = screenPos.x - parentX
const posY = screenPos.y - parentY
const newId = getId()
const newNode = NewNode({
id: newId,
position: { x: posX, y: posY },
parentId: connectingNodeId.current,
})
setNodes((nds) => nds.concat(newNode))
setEdges((eds) =>
// @ts-ignore
eds.concat({
id: newId,
source: connectingNodeId.current,
target: newId,
})
)
}
},
[screenToFlowPosition]
)
This won't work anymore when I upgrade to v12 though...
from xyflow.
Related Issues (20)
- [React Flow]: Handle: No node id found. Make sure to only use a Handle inside a custom Node.
- [React Flow]: Handle: No node id found. Make sure to only use a Handle inside a custom Node. HOT 1
- Zoom In, Zoom Out, fitview, and toggle interactivity HOT 4
- Mini Map preview disappeared in 12.0.0-next.18 HOT 1
- Allow drag selection inside nodes HOT 11
- Revert to window level event listeners
- [SvelteFlow] panOnScroll shift+scroll horizontal does not work after clicking canvas HOT 4
- `Edge` should accept a generic `type` argument like `Node` HOT 2
- Node Position Changes Cause All Nodes To Disappear HOT 3
- Provide helper to create an edge from edge params or `Connection` HOT 9
- Large Data React-Window Virtualization HOT 7
- prevent outcoming edge from target handle HOT 3
- Edge marker is not dynamically updated HOT 9
- accept array of classes in noPanClassName HOT 5
- snap guidelines HOT 2
- Extra Objects needed HOT 2
- Node position not matching the nodes list and rendered node HOT 5
- defining a fixed markerEnd (or Start) in custom Edge doesn't work? HOT 2
- I face a bug to align center expand and collapse flow HOT 1
- Export type definitions for individual methods and variables of `useSvelteFlow` functionality HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xyflow.