refactor(nodes): calculate bounds on mount

This commit is contained in:
braks
2022-10-07 22:55:38 +02:00
committed by Braks
parent 44a65b3bbb
commit 1d1447ed10
2 changed files with 36 additions and 9 deletions
@@ -1,9 +1,9 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useHandle, useVueFlow } from '../../composables' import { useHandle, useNode, useVueFlow } from '../../composables'
import type { Position } from '../../types' import type { Position } from '../../types'
import { ConnectionMode } from '../../types' import { ConnectionMode } from '../../types'
import { NodeId } from '../../context'
import type { HandleProps } from '../../types/handle' import type { HandleProps } from '../../types/handle'
import { getDimensions } from '../../utils'
const { const {
type = 'source', type = 'source',
@@ -15,9 +15,11 @@ const {
}, },
} = defineProps<HandleProps>() } = defineProps<HandleProps>()
const { connectionStartHandle, connectionMode } = $(useVueFlow()) const { connectionStartHandle, connectionMode, vueFlowRef } = $(useVueFlow())
const nodeId = inject(NodeId, '') const { id: nodeId, node, nodeEl } = useNode()
const handle = ref<HTMLDivElement>()
const handleId = $computed(() => id ?? (connectionMode === ConnectionMode.Strict ? null : `${nodeId}__handle-${position}`)) const handleId = $computed(() => id ?? (connectionMode === ConnectionMode.Strict ? null : `${nodeId}__handle-${position}`))
@@ -49,6 +51,32 @@ const getClasses = computed(() => {
}, },
] ]
}) })
onMounted(() => {
const existingBounds = node.handleBounds[type]?.find((b) => b.id === handleId)
if (!vueFlowRef || existingBounds) return
const viewportNode = vueFlowRef.querySelector('.vue-flow__transformationpane')
if (!nodeEl || !handle.value || !viewportNode) return
const nodeBounds = nodeEl.value.getBoundingClientRect()
const handleBounds = handle.value.getBoundingClientRect()
const style = window.getComputedStyle(viewportNode)
const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform)
const nextBounds = {
id: handleId,
position,
x: (handleBounds.left - nodeBounds.left) / zoom,
y: (handleBounds.top - nodeBounds.top) / zoom,
...getDimensions(handle.value),
}
node.handleBounds[type] = [...(node.handleBounds[type] ?? []), nextBounds]
})
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -59,6 +87,7 @@ export default {
<template> <template>
<div <div
ref="handle"
:data-handleid="handleId" :data-handleid="handleId"
:data-nodeid="nodeId" :data-nodeid="nodeId"
:data-handlepos="position" :data-handlepos="position"
+3 -5
View File
@@ -115,12 +115,10 @@ export default (state: State, getters: ComputedGetters): Actions => {
(node.dimensions.width !== dimensions.width || node.dimensions.height !== dimensions.height) (node.dimensions.width !== dimensions.width || node.dimensions.height !== dimensions.height)
) || update.forceUpdate ) || update.forceUpdate
node.handleBounds = {
source: getHandleBounds('.source', update.nodeElement, zoom),
target: getHandleBounds('.target', update.nodeElement, zoom),
}
if (doUpdate) { if (doUpdate) {
node.handleBounds.source = getHandleBounds('.source', update.nodeElement, zoom)
node.handleBounds.target = getHandleBounds('.target', update.nodeElement, zoom)
node.dimensions = dimensions node.dimensions = dimensions
res.push({ res.push({