@@ -4,7 +4,7 @@ import type { ComputedGetters, D3Selection, GraphNode, Project, State, ViewportF
4
4
import { clampPosition , getRectOfNodes , getTransformForBounds , pointToRendererPoint , rendererPointToPoint , warn } from '../utils'
5
5
6
6
interface ExtendedViewport extends ViewportFunctions {
7
- initialized : boolean
7
+ viewportInitialized : boolean
8
8
screenToFlowCoordinate : Project
9
9
flowToScreenCoordinate : Project
10
10
}
@@ -31,7 +31,7 @@ const initialViewportHelper: ExtendedViewport = {
31
31
setTransform : noop ,
32
32
getViewport : ( ) => ( { x : 0 , y : 0 , zoom : 1 } ) ,
33
33
getTransform : ( ) => ( { x : 0 , y : 0 , zoom : 1 } ) ,
34
- initialized : false ,
34
+ viewportInitialized : false ,
35
35
}
36
36
37
37
export function useViewport ( state : State , getters : ComputedGetters ) {
@@ -73,136 +73,138 @@ export function useViewport(state: State, getters: ComputedGetters) {
73
73
}
74
74
75
75
return computed < ExtendedViewport > ( ( ) => {
76
- if ( state . d3Zoom && state . d3Selection && state . dimensions . width && state . dimensions . height ) {
77
- return {
78
- initialized : true ,
79
- // todo: allow passing scale as option
80
- zoomIn : ( options ) => {
81
- return zoom ( 1.2 , options ?. duration )
82
- } ,
83
- zoomOut : ( options ) => {
84
- return zoom ( 1 / 1.2 , options ?. duration )
85
- } ,
86
- zoomTo : ( zoomLevel , options ) => {
87
- return new Promise < boolean > ( ( resolve ) => {
88
- if ( state . d3Selection && state . d3Zoom ) {
89
- state . d3Zoom . scaleTo (
90
- transition ( state . d3Selection , options ?. duration , ( ) => {
91
- resolve ( true )
92
- } ) ,
93
- zoomLevel ,
94
- )
95
- } else {
96
- resolve ( false )
97
- }
98
- } )
99
- } ,
100
- setViewport : ( transform , options ) => {
101
- return transformViewport ( transform . x , transform . y , transform . zoom , options ?. duration )
102
- } ,
103
- setTransform : ( transform , options ) => {
104
- return transformViewport ( transform . x , transform . y , transform . zoom , options ?. duration )
105
- } ,
106
- getViewport : ( ) => ( {
76
+ const isInitialized = state . d3Zoom && state . d3Selection && state . dimensions . width && state . dimensions . height
77
+
78
+ if ( ! isInitialized ) {
79
+ return initialViewportHelper
80
+ }
81
+
82
+ return {
83
+ viewportInitialized : true ,
84
+ // todo: allow passing scale as option
85
+ zoomIn : ( options ) => {
86
+ return zoom ( 1.2 , options ?. duration )
87
+ } ,
88
+ zoomOut : ( options ) => {
89
+ return zoom ( 1 / 1.2 , options ?. duration )
90
+ } ,
91
+ zoomTo : ( zoomLevel , options ) => {
92
+ return new Promise < boolean > ( ( resolve ) => {
93
+ if ( state . d3Selection && state . d3Zoom ) {
94
+ state . d3Zoom . scaleTo (
95
+ transition ( state . d3Selection , options ?. duration , ( ) => {
96
+ resolve ( true )
97
+ } ) ,
98
+ zoomLevel ,
99
+ )
100
+ } else {
101
+ resolve ( false )
102
+ }
103
+ } )
104
+ } ,
105
+ setViewport : ( transform , options ) => {
106
+ return transformViewport ( transform . x , transform . y , transform . zoom , options ?. duration )
107
+ } ,
108
+ setTransform : ( transform , options ) => {
109
+ return transformViewport ( transform . x , transform . y , transform . zoom , options ?. duration )
110
+ } ,
111
+ getViewport : ( ) => ( {
112
+ x : state . viewport . x ,
113
+ y : state . viewport . y ,
114
+ zoom : state . viewport . zoom ,
115
+ } ) ,
116
+ getTransform : ( ) => {
117
+ return {
107
118
x : state . viewport . x ,
108
119
y : state . viewport . y ,
109
120
zoom : state . viewport . zoom ,
110
- } ) ,
111
- getTransform : ( ) => {
112
- return {
113
- x : state . viewport . x ,
114
- y : state . viewport . y ,
115
- zoom : state . viewport . zoom ,
116
- }
121
+ }
122
+ } ,
123
+ fitView : (
124
+ options = {
125
+ padding : DEFAULT_PADDING ,
126
+ includeHiddenNodes : false ,
127
+ duration : 0 ,
117
128
} ,
118
- fitView : (
119
- options = {
120
- padding : DEFAULT_PADDING ,
121
- includeHiddenNodes : false ,
122
- duration : 0 ,
123
- } ,
124
- ) => {
125
- const nodesToFit : GraphNode [ ] = ( options . includeHiddenNodes ? state . nodes : getNodes . value ) . filter ( ( node ) => {
126
- const initialized = node . dimensions . width && node . dimensions . height
127
- let shouldInclude = true
128
-
129
- if ( options . nodes ?. length ) {
130
- shouldInclude = options . nodes . includes ( node . id )
131
- }
132
-
133
- return initialized && shouldInclude
134
- } )
135
-
136
- if ( ! nodesToFit . length ) {
137
- return Promise . resolve ( false )
129
+ ) => {
130
+ const nodesToFit : GraphNode [ ] = ( options . includeHiddenNodes ? state . nodes : getNodes . value ) . filter ( ( node ) => {
131
+ const initialized = node . dimensions . width && node . dimensions . height
132
+ let shouldInclude = true
133
+
134
+ if ( options . nodes ?. length ) {
135
+ shouldInclude = options . nodes . includes ( node . id )
138
136
}
139
137
140
- const bounds = getRectOfNodes ( nodesToFit )
138
+ return initialized && shouldInclude
139
+ } )
141
140
142
- const { x, y, zoom } = getTransformForBounds (
143
- bounds ,
144
- state . dimensions . width ,
145
- state . dimensions . height ,
146
- options . minZoom ?? state . minZoom ,
147
- options . maxZoom ?? state . maxZoom ,
148
- options . padding ?? DEFAULT_PADDING ,
149
- options . offset ,
150
- )
141
+ if ( ! nodesToFit . length ) {
142
+ return Promise . resolve ( false )
143
+ }
151
144
152
- return transformViewport ( x , y , zoom , options ?. duration )
153
- } ,
154
- setCenter : ( x , y , options ) => {
155
- const nextZoom = typeof options ?. zoom !== 'undefined' ? options . zoom : state . maxZoom
156
- const centerX = state . dimensions . width / 2 - x * nextZoom
157
- const centerY = state . dimensions . height / 2 - y * nextZoom
145
+ const bounds = getRectOfNodes ( nodesToFit )
158
146
159
- return transformViewport ( centerX , centerY , nextZoom , options ?. duration )
160
- } ,
161
- fitBounds : ( bounds , options = { padding : DEFAULT_PADDING } ) => {
162
- const { x, y, zoom } = getTransformForBounds (
163
- bounds ,
164
- state . dimensions . width ,
165
- state . dimensions . height ,
166
- state . minZoom ,
167
- state . maxZoom ,
168
- options . padding ,
169
- )
170
-
171
- return transformViewport ( x , y , zoom , options ?. duration )
172
- } ,
173
- project : ( position ) => pointToRendererPoint ( position , state . viewport , state . snapToGrid , state . snapGrid ) ,
174
- screenToFlowCoordinate : ( position ) => {
175
- if ( state . vueFlowRef ) {
176
- const { x : domX , y : domY } = state . vueFlowRef . getBoundingClientRect ( )
147
+ const { x, y, zoom } = getTransformForBounds (
148
+ bounds ,
149
+ state . dimensions . width ,
150
+ state . dimensions . height ,
151
+ options . minZoom ?? state . minZoom ,
152
+ options . maxZoom ?? state . maxZoom ,
153
+ options . padding ?? DEFAULT_PADDING ,
154
+ options . offset ,
155
+ )
156
+
157
+ return transformViewport ( x , y , zoom , options ?. duration )
158
+ } ,
159
+ setCenter : ( x , y , options ) => {
160
+ const nextZoom = typeof options ?. zoom !== 'undefined' ? options . zoom : state . maxZoom
161
+ const centerX = state . dimensions . width / 2 - x * nextZoom
162
+ const centerY = state . dimensions . height / 2 - y * nextZoom
163
+
164
+ return transformViewport ( centerX , centerY , nextZoom , options ?. duration )
165
+ } ,
166
+ fitBounds : ( bounds , options = { padding : DEFAULT_PADDING } ) => {
167
+ const { x, y, zoom } = getTransformForBounds (
168
+ bounds ,
169
+ state . dimensions . width ,
170
+ state . dimensions . height ,
171
+ state . minZoom ,
172
+ state . maxZoom ,
173
+ options . padding ,
174
+ )
177
175
178
- const correctedPosition = {
179
- x : position . x - domX ,
180
- y : position . y - domY ,
181
- }
176
+ return transformViewport ( x , y , zoom , options ?. duration )
177
+ } ,
178
+ project : ( position ) => pointToRendererPoint ( position , state . viewport , state . snapToGrid , state . snapGrid ) ,
179
+ screenToFlowCoordinate : ( position ) => {
180
+ if ( state . vueFlowRef ) {
181
+ const { x : domX , y : domY } = state . vueFlowRef . getBoundingClientRect ( )
182
182
183
- return pointToRendererPoint ( correctedPosition , state . viewport , state . snapToGrid , state . snapGrid )
183
+ const correctedPosition = {
184
+ x : position . x - domX ,
185
+ y : position . y - domY ,
184
186
}
185
187
186
- return { x : 0 , y : 0 }
187
- } ,
188
- flowToScreenCoordinate : ( position ) => {
189
- if ( state . vueFlowRef ) {
190
- const { x : domX , y : domY } = state . vueFlowRef . getBoundingClientRect ( )
188
+ return pointToRendererPoint ( correctedPosition , state . viewport , state . snapToGrid , state . snapGrid )
189
+ }
191
190
192
- const correctedPosition = {
193
- x : position . x + domX ,
194
- y : position . y + domY ,
195
- }
191
+ return { x : 0 , y : 0 }
192
+ } ,
193
+ flowToScreenCoordinate : ( position ) => {
194
+ if ( state . vueFlowRef ) {
195
+ const { x : domX , y : domY } = state . vueFlowRef . getBoundingClientRect ( )
196
196
197
- return rendererPointToPoint ( correctedPosition , state . viewport )
197
+ const correctedPosition = {
198
+ x : position . x + domX ,
199
+ y : position . y + domY ,
198
200
}
199
201
200
- return { x : 0 , y : 0 }
201
- } ,
202
- }
203
- }
202
+ return rendererPointToPoint ( correctedPosition , state . viewport )
203
+ }
204
204
205
- return initialViewportHelper
205
+ return { x : 0 , y : 0 }
206
+ } ,
207
+ }
206
208
} )
207
209
}
208
210
0 commit comments