fix(handles): correctly calculate handle pos by using viewport zoom

This commit is contained in:
Braks
2022-07-16 14:37:08 +02:00
parent c0c72f1f6d
commit b7428e9512
4 changed files with 21 additions and 28 deletions
@@ -120,7 +120,10 @@ onMounted(() => {
updatePosition(xyzPos, parentX && parentY ? { x: parentX, y: parentY, z: parentZ! } : undefined) updatePosition(xyzPos, parentX && parentY ? { x: parentX, y: parentY, z: parentZ! } : undefined)
node.handleBounds = getHandleBounds(nodeElement.value, viewport.zoom) node.handleBounds = {
source: getHandleBounds('.source', nodeElement.value, viewport.zoom),
target: getHandleBounds('.target', nodeElement.value, viewport.zoom),
}
}, },
{ immediate: true, flush: 'post' }, { immediate: true, flush: 'post' },
) )
+5 -1
View File
@@ -80,7 +80,11 @@ export default (state: State, getters: ComputedGetters): Actions => {
dimensions.height && dimensions.height &&
(node.dimensions.width !== dimensions.width || node.dimensions.height !== dimensions.height || update.forceUpdate) (node.dimensions.width !== dimensions.width || node.dimensions.height !== dimensions.height || update.forceUpdate)
) )
node.handleBounds = getHandleBounds(update.nodeElement, state.viewport.zoom)
node.handleBounds = {
source: getHandleBounds('.source', update.nodeElement, state.viewport.zoom),
target: getHandleBounds('.target', update.nodeElement, state.viewport.zoom),
}
if (doUpdate) { if (doUpdate) {
node.dimensions = dimensions node.dimensions = dimensions
+1 -1
View File
@@ -113,7 +113,7 @@ export const parseEdge = (edge: Edge, defaults?: Partial<GraphEdge>): GraphEdge
const getConnectedElements = (node: GraphNode, elements: Elements, dir: 'source' | 'target') => { const getConnectedElements = (node: GraphNode, elements: Elements, dir: 'source' | 'target') => {
if (!isNode(node)) return [] if (!isNode(node)) return []
const origin = (dir === 'source' ? 'target' : 'source') const origin = dir === 'source' ? 'target' : 'source'
const ids = elements.filter((e) => isEdge(e) && e[origin] === node.id).map((e) => isEdge(e) && e[dir]) const ids = elements.filter((e) => isEdge(e) && e[origin] === node.id).map((e) => isEdge(e) && e[dir])
return elements.filter((e) => ids.includes(e.id)) return elements.filter((e) => ids.includes(e.id))
} }
+11 -25
View File
@@ -2,43 +2,29 @@ import type { Ref } from 'vue'
import { getDimensions } from './graph' import { getDimensions } from './graph'
import type { Actions, GraphNode, HandleElement, Position } from '~/types' import type { Actions, GraphNode, HandleElement, Position } from '~/types'
export const getHandleBoundsByHandleType = ( export const getHandleBounds = (selector: string, nodeElement: HTMLDivElement, zoom: number): HandleElement[] | undefined => {
selector: string,
nodeElement: HTMLDivElement,
parentBounds: ClientRect | DOMRect,
k: number,
): HandleElement[] | null => {
const handles = nodeElement.querySelectorAll(selector) const handles = nodeElement.querySelectorAll(selector)
if (!handles || !handles.length) return null if (!handles || !handles.length) {
return undefined
}
const handlesArray = Array.from(handles) as HTMLDivElement[] const handlesArray = Array.from(handles) as HTMLDivElement[]
const nodeBounds = nodeElement.getBoundingClientRect()
return handlesArray.map((handle): HandleElement => { return handlesArray.map((handle): HandleElement => {
const bounds = handle.getBoundingClientRect() const handleBounds = handle.getBoundingClientRect()
const dimensions = getDimensions(handle)
const handleId = handle.getAttribute('data-handleid') ?? undefined
const handlePosition = handle.getAttribute('data-handlepos') as Position
return { return {
id: handleId, id: handle.getAttribute('data-handleid'),
position: handlePosition, position: handle.getAttribute('data-handlepos') as unknown as Position,
x: (bounds.left - parentBounds.left) / k, x: (handleBounds.left - nodeBounds.left) / zoom,
y: (bounds.top - parentBounds.top) / k, y: (handleBounds.top - nodeBounds.top) / zoom,
...dimensions, ...getDimensions(handle),
} }
}) })
} }
export const getHandleBounds = (nodeElement: HTMLDivElement, scale: number, id?: string) => {
const bounds = nodeElement.getBoundingClientRect()
return {
source: getHandleBoundsByHandleType(`.source${id ? `.vue-flow__handle-${id}` : ''}`, nodeElement, bounds, scale) ?? undefined,
target: getHandleBoundsByHandleType(`.target${id ? `.vue-flow__handle-${id}` : ''}`, nodeElement, bounds, scale) ?? undefined,
}
}
export const handleNodeClick = ( export const handleNodeClick = (
node: GraphNode, node: GraphNode,
multiSelectionActive: boolean, multiSelectionActive: boolean,