1
1
import { until } from '@vueuse/core'
2
2
import { zoomIdentity } from 'd3-zoom'
3
3
import { computed , ref } from 'vue'
4
- import type { ComputedGetters , D3Selection , GraphNode , State , ViewportFunctions } from '~/types'
5
- import { clampPosition , getRectOfNodes , getTransformForBounds , pointToRendererPoint } from '~/utils'
4
+ import type { ComputedGetters , D3Selection , GraphNode , Project , State , ViewportFunctions } from '~/types'
5
+ import { clampPosition , getRectOfNodes , getTransformForBounds , pointToRendererPoint , rendererPointToPoint } from '~/utils'
6
6
7
7
interface ExtendedViewport extends ViewportFunctions {
8
8
initialized : boolean
9
+ screenToFlowCoordinate : Project
10
+ flowToScreenCoordinate : Project
9
11
}
10
12
11
13
const DEFAULT_PADDING = 0.1
@@ -22,6 +24,8 @@ const initialViewportHelper: ExtendedViewport = {
22
24
setCenter : noop ,
23
25
fitBounds : noop ,
24
26
project : ( position ) => position ,
27
+ screenToFlowCoordinate : ( position ) => position ,
28
+ flowToScreenCoordinate : ( position ) => position ,
25
29
setViewport : noop ,
26
30
setTransform : noop ,
27
31
getViewport : ( ) => ( { x : 0 , y : 0 , zoom : 1 } ) ,
@@ -30,7 +34,19 @@ const initialViewportHelper: ExtendedViewport = {
30
34
}
31
35
32
36
export function useViewport ( state : State , getters : ComputedGetters ) {
33
- const { nodes, d3Zoom, d3Selection, dimensions, translateExtent, minZoom, maxZoom, viewport, snapToGrid, snapGrid } = $ ( state )
37
+ const {
38
+ vueFlowRef : domNode ,
39
+ nodes,
40
+ d3Zoom,
41
+ d3Selection,
42
+ dimensions,
43
+ translateExtent,
44
+ minZoom,
45
+ maxZoom,
46
+ viewport,
47
+ snapToGrid,
48
+ snapGrid,
49
+ } = $ ( state )
34
50
35
51
const { getNodes } = getters
36
52
@@ -176,6 +192,34 @@ export function useViewport(state: State, getters: ComputedGetters) {
176
192
return transformViewport ( x , y , zoom , options ?. duration )
177
193
} ,
178
194
project : ( position ) => pointToRendererPoint ( position , viewport , snapToGrid , snapGrid ) ,
195
+ screenToFlowCoordinate : ( position ) => {
196
+ if ( domNode ) {
197
+ const { x : domX , y : domY } = domNode . getBoundingClientRect ( )
198
+
199
+ const correctedPosition = {
200
+ x : position . x - domX ,
201
+ y : position . y - domY ,
202
+ }
203
+
204
+ return pointToRendererPoint ( correctedPosition , viewport , snapToGrid , snapGrid )
205
+ }
206
+
207
+ return { x : 0 , y : 0 }
208
+ } ,
209
+ flowToScreenCoordinate : ( position ) => {
210
+ if ( domNode ) {
211
+ const { x : domX , y : domY } = domNode . getBoundingClientRect ( )
212
+
213
+ const correctedPosition = {
214
+ x : position . x + domX ,
215
+ y : position . y + domY ,
216
+ }
217
+
218
+ return rendererPointToPoint ( correctedPosition , viewport )
219
+ }
220
+
221
+ return { x : 0 , y : 0 }
222
+ } ,
179
223
}
180
224
}
181
225
0 commit comments