refactor(core): use useOnInitHandler to emit paneReady event
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
import { watch } from 'vue'
|
||||||
|
import { useVueFlow } from './useVueFlow'
|
||||||
|
|
||||||
|
export function useOnInitHandler() {
|
||||||
|
const vfInstance = useVueFlow()
|
||||||
|
|
||||||
|
watch(vfInstance.viewportInitialized, (isInitialized) => {
|
||||||
|
if (isInitialized) {
|
||||||
|
setTimeout(() => {
|
||||||
|
vfInstance.emits.paneReady(vfInstance)
|
||||||
|
}, 1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import type { ComputedGetters, D3Selection, GraphNode, Project, State, ViewportF
|
|||||||
import { clampPosition, getRectOfNodes, getTransformForBounds, pointToRendererPoint, rendererPointToPoint, warn } from '../utils'
|
import { clampPosition, getRectOfNodes, getTransformForBounds, pointToRendererPoint, rendererPointToPoint, warn } from '../utils'
|
||||||
|
|
||||||
interface ExtendedViewport extends ViewportFunctions {
|
interface ExtendedViewport extends ViewportFunctions {
|
||||||
initialized: boolean
|
viewportInitialized: boolean
|
||||||
screenToFlowCoordinate: Project
|
screenToFlowCoordinate: Project
|
||||||
flowToScreenCoordinate: Project
|
flowToScreenCoordinate: Project
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ const initialViewportHelper: ExtendedViewport = {
|
|||||||
setTransform: noop,
|
setTransform: noop,
|
||||||
getViewport: () => ({ x: 0, y: 0, zoom: 1 }),
|
getViewport: () => ({ x: 0, y: 0, zoom: 1 }),
|
||||||
getTransform: () => ({ x: 0, y: 0, zoom: 1 }),
|
getTransform: () => ({ x: 0, y: 0, zoom: 1 }),
|
||||||
initialized: false,
|
viewportInitialized: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useViewport(state: State, getters: ComputedGetters) {
|
export function useViewport(state: State, getters: ComputedGetters) {
|
||||||
@@ -73,9 +73,14 @@ export function useViewport(state: State, getters: ComputedGetters) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return computed<ExtendedViewport>(() => {
|
return computed<ExtendedViewport>(() => {
|
||||||
if (state.d3Zoom && state.d3Selection && state.dimensions.width && state.dimensions.height) {
|
const isInitialized = state.d3Zoom && state.d3Selection && state.dimensions.width && state.dimensions.height
|
||||||
|
|
||||||
|
if (!isInitialized) {
|
||||||
|
return initialViewportHelper
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
initialized: true,
|
viewportInitialized: true,
|
||||||
// todo: allow passing scale as option
|
// todo: allow passing scale as option
|
||||||
zoomIn: (options) => {
|
zoomIn: (options) => {
|
||||||
return zoom(1.2, options?.duration)
|
return zoom(1.2, options?.duration)
|
||||||
@@ -200,9 +205,6 @@ export function useViewport(state: State, getters: ComputedGetters) {
|
|||||||
return { x: 0, y: 0 }
|
return { x: 0, y: 0 }
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return initialViewportHelper
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ export class Storage {
|
|||||||
|
|
||||||
const getters = useGetters(reactiveState, nodeIds, edgeIds)
|
const getters = useGetters(reactiveState, nodeIds, edgeIds)
|
||||||
|
|
||||||
const actions = useActions(id, emits, hooksOn, reactiveState, getters, nodeIds, edgeIds)
|
const actions = useActions(id, reactiveState, getters, nodeIds, edgeIds)
|
||||||
|
|
||||||
actions.setState(reactiveState)
|
actions.setState(reactiveState)
|
||||||
|
|
||||||
|
|||||||
@@ -54,8 +54,6 @@ import { useState } from './state'
|
|||||||
|
|
||||||
export function useActions(
|
export function useActions(
|
||||||
id: string,
|
id: string,
|
||||||
emits: any,
|
|
||||||
hooksOn: any,
|
|
||||||
state: State,
|
state: State,
|
||||||
getters: ComputedGetters,
|
getters: ComputedGetters,
|
||||||
// todo: change to a Set
|
// todo: change to a Set
|
||||||
@@ -846,7 +844,7 @@ export function useActions(
|
|||||||
const y = viewport?.y || position[1]
|
const y = viewport?.y || position[1]
|
||||||
const nextZoom = viewport?.zoom || zoom || state.viewport.zoom
|
const nextZoom = viewport?.zoom || zoom || state.viewport.zoom
|
||||||
|
|
||||||
return until(() => viewportHelper.value.initialized)
|
return until(() => viewportHelper.value.viewportInitialized)
|
||||||
.toBe(true)
|
.toBe(true)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
viewportHelper.value
|
viewportHelper.value
|
||||||
@@ -891,7 +889,7 @@ export function useActions(
|
|||||||
setState(resetState)
|
setState(resetState)
|
||||||
}
|
}
|
||||||
|
|
||||||
const actions: Actions = {
|
return {
|
||||||
updateNodePositions,
|
updateNodePositions,
|
||||||
updateNodeDimensions,
|
updateNodeDimensions,
|
||||||
setElements,
|
setElements,
|
||||||
@@ -944,20 +942,4 @@ export function useActions(
|
|||||||
$reset,
|
$reset,
|
||||||
$destroy: () => {},
|
$destroy: () => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
until(() => viewportHelper.value.initialized)
|
|
||||||
.toBe(true)
|
|
||||||
.then(() => {
|
|
||||||
state.hooks.paneReady.trigger({
|
|
||||||
id,
|
|
||||||
emits,
|
|
||||||
vueFlowVersion: typeof __VUE_FLOW_VERSION__ !== 'undefined' ? __VUE_FLOW_VERSION__ : 'UNKNOWN',
|
|
||||||
...hooksOn,
|
|
||||||
...state,
|
|
||||||
...getters,
|
|
||||||
...actions,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return actions
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ function defaultState(): State {
|
|||||||
d3Zoom: null,
|
d3Zoom: null,
|
||||||
d3Selection: null,
|
d3Selection: null,
|
||||||
d3ZoomHandler: null,
|
d3ZoomHandler: null,
|
||||||
|
|
||||||
|
viewportInitialized: false,
|
||||||
|
|
||||||
minZoom: 0.5,
|
minZoom: 0.5,
|
||||||
maxZoom: 2,
|
maxZoom: 2,
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ export interface State extends Omit<FlowOptions, 'id' | 'modelValue'> {
|
|||||||
readonly d3Selection: D3Selection | null
|
readonly d3Selection: D3Selection | null
|
||||||
readonly d3ZoomHandler: D3ZoomHandler | null
|
readonly d3ZoomHandler: D3ZoomHandler | null
|
||||||
|
|
||||||
|
viewportInitialized: boolean
|
||||||
|
|
||||||
/** use setMinZoom action to change minZoom */
|
/** use setMinZoom action to change minZoom */
|
||||||
minZoom: number
|
minZoom: number
|
||||||
/** use setMaxZoom action to change maxZoom */
|
/** use setMaxZoom action to change maxZoom */
|
||||||
|
|||||||
Reference in New Issue
Block a user