|
1 | 1 | <script lang="ts" setup> |
2 | | -import { until } from '@vueuse/core' |
3 | | -import { computed, onUnmounted, ref, toRef } from 'vue' |
| 2 | +import { computed, onMounted, onUnmounted, ref, toRef } from 'vue' |
4 | 3 | import type { HandleProps } from '../../types' |
5 | 4 | import { Position } from '../../types' |
6 | 5 | import { useHandle, useNode, useVueFlow } from '../../composables' |
@@ -97,38 +96,42 @@ const isConnectable = computed(() => { |
97 | 96 |
|
98 | 97 | // todo: remove this and have users handle this themselves using `updateNodeInternals` |
99 | 98 | // set up handle bounds if they don't exist yet and the node has been initialized (i.e. the handle was added after the node has already been mounted) |
100 | | -until(() => !!node.dimensions.width && !!node.dimensions.height) |
101 | | - .toBe(true, { flush: 'post' }) |
102 | | - .then(() => { |
103 | | - const existingBounds = node.handleBounds[type.value]?.find((b) => b.id === handleId) |
| 99 | +onMounted(() => { |
| 100 | + // if the node isn't initialized yet, we can't set up the handle bounds |
| 101 | + // the handle bounds will be automatically set up when the node is initialized (`updateNodeDimensions`) |
| 102 | + if (!node.dimensions.width || !node.dimensions.height) { |
| 103 | + return |
| 104 | + } |
104 | 105 |
|
105 | | - if (!vueFlowRef.value || existingBounds) { |
106 | | - return |
107 | | - } |
| 106 | + const existingBounds = node.handleBounds[type.value]?.find((b) => b.id === handleId) |
108 | 107 |
|
109 | | - const viewportNode = vueFlowRef.value.querySelector('.vue-flow__transformationpane') |
| 108 | + if (!vueFlowRef.value || existingBounds) { |
| 109 | + return |
| 110 | + } |
110 | 111 |
|
111 | | - if (!nodeEl.value || !handle.value || !viewportNode || !handleId) { |
112 | | - return |
113 | | - } |
| 112 | + const viewportNode = vueFlowRef.value.querySelector('.vue-flow__transformationpane') |
114 | 113 |
|
115 | | - const nodeBounds = nodeEl.value.getBoundingClientRect() |
| 114 | + if (!nodeEl.value || !handle.value || !viewportNode || !handleId) { |
| 115 | + return |
| 116 | + } |
| 117 | +
|
| 118 | + const nodeBounds = nodeEl.value.getBoundingClientRect() |
116 | 119 |
|
117 | | - const handleBounds = handle.value.getBoundingClientRect() |
| 120 | + const handleBounds = handle.value.getBoundingClientRect() |
118 | 121 |
|
119 | | - const style = window.getComputedStyle(viewportNode) |
120 | | - const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform) |
| 122 | + const style = window.getComputedStyle(viewportNode) |
| 123 | + const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform) |
121 | 124 |
|
122 | | - const nextBounds = { |
123 | | - id: handleId, |
124 | | - position, |
125 | | - x: (handleBounds.left - nodeBounds.left) / zoom, |
126 | | - y: (handleBounds.top - nodeBounds.top) / zoom, |
127 | | - ...getDimensions(handle.value), |
128 | | - } |
| 125 | + const nextBounds = { |
| 126 | + id: handleId, |
| 127 | + position, |
| 128 | + x: (handleBounds.left - nodeBounds.left) / zoom, |
| 129 | + y: (handleBounds.top - nodeBounds.top) / zoom, |
| 130 | + ...getDimensions(handle.value), |
| 131 | + } |
129 | 132 |
|
130 | | - node.handleBounds[type.value] = [...(node.handleBounds[type.value] ?? []), nextBounds] |
131 | | - }) |
| 133 | + node.handleBounds[type.value] = [...(node.handleBounds[type.value] ?? []), nextBounds] |
| 134 | +}) |
132 | 135 |
|
133 | 136 | onUnmounted(() => { |
134 | 137 | // clean up node internals |
|
0 commit comments