refactor(core): use useOnInitHandler to emit paneReady event

This commit is contained in:
braks
2024-02-05 07:51:12 +01:00
committed by Braks
parent 9e72e725e8
commit 5798cc5578
6 changed files with 136 additions and 133 deletions
@@ -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)
}
})
}
+9 -7
View File
@@ -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
}) })
} }
+1 -1
View File
@@ -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)
+2 -20
View File
@@ -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
} }
+3
View File
@@ -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,
+2
View File
@@ -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 */