Skip to content

Commit c855dfe

Browse files
committed
feat(core): add viewport helper to state
1 parent fbadf94 commit c855dfe

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

packages/core/src/composables/useOnInitHandler.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,33 @@ import { useVueFlow } from './useVueFlow'
44
export function useOnInitHandler() {
55
const vfInstance = useVueFlow()
66

7-
watch(vfInstance.viewportInitialized, (isInitialized) => {
8-
if (isInitialized) {
9-
setTimeout(() => {
10-
vfInstance.emits.init(vfInstance)
11-
vfInstance.emits.paneReady(vfInstance)
12-
}, 1)
13-
}
14-
})
7+
watch(
8+
() => vfInstance.viewportHelper.value.viewportInitialized,
9+
(isInitialized) => {
10+
if (isInitialized) {
11+
setTimeout(() => {
12+
// todo: call these when *nodes* are initialized instead of the viewport
13+
// currently doesn't work quite right because the viewport dimensions are not yet available when the nodes are initialized
14+
if (!vfInstance.fitViewOnInitDone.value && vfInstance.fitViewOnInit.value) {
15+
vfInstance.fitView()
16+
}
17+
18+
vfInstance.fitViewOnInitDone.value = true
19+
20+
// Here, we are making all nodes visible once we have the dimensions.
21+
if (!document.querySelector('#vue-flow__initialized-styles')) {
22+
const style = document.createElement('style')
23+
style.id = 'vue-flow__initialized-styles'
24+
document.head.appendChild(style)
25+
26+
const css = `.vue-flow__node { visibility: visible !important; }`
27+
style.appendChild(document.createTextNode(css))
28+
}
29+
30+
vfInstance.emits.init(vfInstance)
31+
vfInstance.emits.paneReady(vfInstance)
32+
}, 1)
33+
}
34+
},
35+
)
1536
}

packages/core/src/composables/useViewport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { computed } from 'vue'
33
import type { ComputedGetters, D3Selection, GraphNode, Project, State, ViewportFunctions } from '../types'
44
import { clampPosition, getRectOfNodes, getTransformForBounds, pointToRendererPoint, rendererPointToPoint, warn } from '../utils'
55

6-
interface ExtendedViewport extends ViewportFunctions {
6+
export interface ViewportHelper extends ViewportFunctions {
77
viewportInitialized: boolean
88
screenToFlowCoordinate: Project
99
flowToScreenCoordinate: Project
@@ -17,7 +17,7 @@ function noop() {
1717
return Promise.resolve(false)
1818
}
1919

20-
const initialViewportHelper: ExtendedViewport = {
20+
const initialViewportHelper: ViewportHelper = {
2121
zoomIn: noop,
2222
zoomOut: noop,
2323
zoomTo: noop,
@@ -72,7 +72,7 @@ export function useViewport(state: State, getters: ComputedGetters) {
7272
})
7373
}
7474

75-
return computed<ExtendedViewport>(() => {
75+
return computed<ViewportHelper>(() => {
7676
const isInitialized = state.d3Zoom && state.d3Selection && state.dimensions.width && state.dimensions.height
7777

7878
if (!isInitialized) {

packages/core/src/store/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ export function useActions(
939939
toObject,
940940
fromObject,
941941
updateNodeInternals,
942+
viewportHelper,
942943
$reset,
943944
$destroy: () => {},
944945
}

packages/core/src/store/state.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ function defaultState(): State {
5151
d3Selection: null,
5252
d3ZoomHandler: null,
5353

54-
viewportInitialized: false,
55-
5654
minZoom: 0.5,
5755
maxZoom: 2,
5856

packages/core/src/types/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CSSProperties, ComputedRef, ToRefs } from 'vue'
22
import type { KeyFilter } from '@vueuse/core'
3+
import type { ViewportHelper } from '../composables'
34
import type {
45
Dimensions,
56
ElementData,
@@ -57,8 +58,6 @@ export interface State extends Omit<FlowOptions, 'id' | 'modelValue'> {
5758
readonly d3Selection: D3Selection | null
5859
readonly d3ZoomHandler: D3ZoomHandler | null
5960

60-
viewportInitialized: boolean
61-
6261
/** use setMinZoom action to change minZoom */
6362
minZoom: number
6463
/** use setMaxZoom action to change maxZoom */
@@ -291,6 +290,8 @@ export interface Actions extends ViewportFunctions {
291290
getConnectedEdges: (nodesOrId: Node[] | string) => GraphEdge[]
292291
/** pan the viewport; return indicates if a transform has happened or not */
293292
panBy: (delta: XYPosition) => boolean
293+
/** viewport helper instance */
294+
viewportHelper: ComputedRef<ViewportHelper>
294295

295296
/** reset state to defaults */
296297
$reset: () => void

tests/cypress/support/component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ import { VueFlow } from '@vue-flow/core'
88
import type { FlowProps } from '@vue-flow/core'
99

1010
function mountVueFlow(props?: FlowProps, attrs?: Record<string, any>, slots?: Record<string, any>) {
11-
cy.mount(VueFlow as any, {
11+
cy.mount(VueFlow, {
1212
props: {
1313
id: 'test',
1414
fitViewOnInit: true,
1515
...props,
1616
} as FlowProps,
1717
attrs: {
18-
key: 'flowy',
1918
style: {
2019
height: '100vh',
2120
width: '100vw',
22-
},
21+
} as CSSStyleDeclaration,
2322
...attrs,
24-
} as Record<string, any>,
23+
},
2524
slots,
2625
})
2726
}

0 commit comments

Comments
 (0)