refactor(core): first check element below
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -113,6 +113,7 @@ export default function useHandle({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { connection, handleDomNode, isValid } = isValidHandle(
|
const { connection, handleDomNode, isValid } = isValidHandle(
|
||||||
|
event,
|
||||||
prevClosestHandle,
|
prevClosestHandle,
|
||||||
connectionMode.value,
|
connectionMode.value,
|
||||||
nodeId,
|
nodeId,
|
||||||
@@ -133,6 +134,7 @@ export default function useHandle({
|
|||||||
function onPointerUp(event: MouseEvent | TouchEvent) {
|
function onPointerUp(event: MouseEvent | TouchEvent) {
|
||||||
if (prevClosestHandle) {
|
if (prevClosestHandle) {
|
||||||
const { connection, isValid } = isValidHandle(
|
const { connection, isValid } = isValidHandle(
|
||||||
|
event,
|
||||||
prevClosestHandle,
|
prevClosestHandle,
|
||||||
connectionMode.value,
|
connectionMode.value,
|
||||||
nodeId,
|
nodeId,
|
||||||
@@ -186,6 +188,7 @@ export default function useHandle({
|
|||||||
const doc = getHostForElement(event.target as HTMLElement)
|
const doc = getHostForElement(event.target as HTMLElement)
|
||||||
|
|
||||||
const { connection, isValid } = isValidHandle(
|
const { connection, isValid } = isValidHandle(
|
||||||
|
event,
|
||||||
{
|
{
|
||||||
nodeId,
|
nodeId,
|
||||||
id: handleId,
|
id: handleId,
|
||||||
|
|||||||
@@ -9,6 +9,24 @@ export interface ConnectionHandle {
|
|||||||
y: number
|
y: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resetRecentHandle(handleDomNode: Element): void {
|
||||||
|
handleDomNode?.classList.remove('vue-flow__handle-valid')
|
||||||
|
handleDomNode?.classList.remove('vue-flow__handle-connecting')
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isMouseEvent = (event: MouseEvent | TouchEvent): event is MouseEvent => 'clientX' in event
|
||||||
|
|
||||||
|
export const getEventPosition = (event: MouseEvent | TouchEvent, bounds?: DOMRect) => {
|
||||||
|
const isMouseTriggered = isMouseEvent(event)
|
||||||
|
const evtX = isMouseTriggered ? event.clientX : event.touches?.[0].clientX
|
||||||
|
const evtY = isMouseTriggered ? event.clientY : event.touches?.[0].clientY
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: evtX - (bounds?.left ?? 0),
|
||||||
|
y: evtY - (bounds?.top ?? 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// this functions collects all handles and adds an absolute position
|
// this functions collects all handles and adds an absolute position
|
||||||
// so that we can later find the closest handle to the mouse position
|
// so that we can later find the closest handle to the mouse position
|
||||||
export function getHandles(
|
export function getHandles(
|
||||||
@@ -58,6 +76,7 @@ interface Result {
|
|||||||
|
|
||||||
// checks if and returns connection in fom of an object { source: 123, target: 312 }
|
// checks if and returns connection in fom of an object { source: 123, target: 312 }
|
||||||
export function isValidHandle(
|
export function isValidHandle(
|
||||||
|
event: MouseEvent | TouchEvent,
|
||||||
handle: Pick<ConnectionHandle, 'nodeId' | 'id' | 'type'>,
|
handle: Pick<ConnectionHandle, 'nodeId' | 'id' | 'type'>,
|
||||||
connectionMode: ConnectionMode,
|
connectionMode: ConnectionMode,
|
||||||
fromNodeId: string,
|
fromNodeId: string,
|
||||||
@@ -69,24 +88,27 @@ export function isValidHandle(
|
|||||||
const { edges, findNode } = useVueFlow()
|
const { edges, findNode } = useVueFlow()
|
||||||
|
|
||||||
const isTarget = fromType === 'target'
|
const isTarget = fromType === 'target'
|
||||||
|
|
||||||
const handleDomNode = doc.querySelector(`.vue-flow__handle[data-id="${handle?.nodeId}-${handle?.id}-${handle?.type}"]`)
|
const handleDomNode = doc.querySelector(`.vue-flow__handle[data-id="${handle?.nodeId}-${handle?.id}-${handle?.type}"]`)
|
||||||
|
const { x, y } = getEventPosition(event)
|
||||||
|
const handleBelow = doc.elementFromPoint(x, y)
|
||||||
|
const handleToCheck = handleBelow?.classList.contains('vue-flow__handle') ? handleBelow : handleDomNode
|
||||||
|
|
||||||
const result: Result = {
|
const result: Result = {
|
||||||
handleDomNode,
|
handleDomNode: handleToCheck,
|
||||||
isValid: false,
|
isValid: false,
|
||||||
connection: { source: '', target: '', sourceHandle: null, targetHandle: null },
|
connection: { source: '', target: '', sourceHandle: null, targetHandle: null },
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handleDomNode) {
|
if (handleDomNode) {
|
||||||
const handleIsTarget = handle.type === 'target'
|
|
||||||
const handleIsSource = handle.type === 'source'
|
|
||||||
const handleNodeId = handleDomNode.getAttribute('data-nodeid')
|
const handleNodeId = handleDomNode.getAttribute('data-nodeid')
|
||||||
const handleId = handleDomNode.getAttribute('data-handleid')
|
const handleId = handleDomNode.getAttribute('data-handleid')
|
||||||
|
|
||||||
const connection: Connection = {
|
const connection: Connection = {
|
||||||
source: isTarget ? handle.nodeId : fromNodeId,
|
source: isTarget ? handle.nodeId : fromNodeId,
|
||||||
sourceHandle: isTarget ? handle.id : fromHandleId,
|
sourceHandle: isTarget ? handleId : fromHandleId,
|
||||||
target: isTarget ? fromNodeId : handle.nodeId,
|
target: isTarget ? fromNodeId : handle.nodeId,
|
||||||
targetHandle: isTarget ? fromHandleId : handle.id,
|
targetHandle: isTarget ? fromHandleId : handleId,
|
||||||
}
|
}
|
||||||
|
|
||||||
result.connection = connection
|
result.connection = connection
|
||||||
@@ -94,7 +116,7 @@ export function isValidHandle(
|
|||||||
// in strict mode we don't allow target to target or source to source connections
|
// in strict mode we don't allow target to target or source to source connections
|
||||||
const isValid =
|
const isValid =
|
||||||
connectionMode === ConnectionMode.Strict
|
connectionMode === ConnectionMode.Strict
|
||||||
? (isTarget && handleIsSource) || (!isTarget && handleIsTarget)
|
? (isTarget && handle.type === 'source') || (!isTarget && handle.type === 'target')
|
||||||
: handleNodeId !== fromNodeId || handleId !== fromHandleId
|
: handleNodeId !== fromNodeId || handleId !== fromHandleId
|
||||||
|
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
@@ -143,21 +165,3 @@ export function getHandleType(edgeUpdaterType: HandleType | undefined, handleDom
|
|||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resetRecentHandle(handleDomNode: Element): void {
|
|
||||||
handleDomNode?.classList.remove('vue-flow__handle-valid')
|
|
||||||
handleDomNode?.classList.remove('vue-flow__handle-connecting')
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isMouseEvent = (event: MouseEvent | TouchEvent): event is MouseEvent => 'clientX' in event
|
|
||||||
|
|
||||||
export const getEventPosition = (event: MouseEvent | TouchEvent, bounds?: DOMRect) => {
|
|
||||||
const isMouseTriggered = isMouseEvent(event)
|
|
||||||
const evtX = isMouseTriggered ? event.clientX : event.touches?.[0].clientX
|
|
||||||
const evtY = isMouseTriggered ? event.clientY : event.touches?.[0].clientY
|
|
||||||
|
|
||||||
return {
|
|
||||||
x: evtX - (bounds?.left ?? 0),
|
|
||||||
y: evtY - (bounds?.top ?? 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user