fix(viewport): emit paneReady when dimensions are ready

* Wait for viewport & nodes to have their dimensions established before emitting paneReady
* Avoids getting 0 values for dimensions on paneReady
This commit is contained in:
Braks
2022-04-11 11:30:10 +02:00
parent 4fbb16f6ad
commit e14cf124ff
4 changed files with 37 additions and 20 deletions
+3 -7
View File
@@ -1,6 +1,5 @@
import { zoomIdentity } from 'd3-zoom'
import useVueFlow from './useVueFlow'
import useWindow from './useWindow'
import { getRectOfNodes, pointToRendererPoint, getTransformForBounds } from '~/utils'
import { GraphNode, Store, ViewportFuncs, D3Selection } from '~/types'
@@ -22,18 +21,13 @@ export default (store: Store = useVueFlow().store): ViewportFuncs => ({
y: store.viewport.y,
zoom: store.viewport.zoom,
}),
fitView: async (
fitView: (
options = {
padding: DEFAULT_PADDING,
includeHiddenNodes: false,
duration: 0,
},
) => {
// if ssr we can't wait for dimensions, they'll never really exist
const window = useWindow()
if ('screen' in window)
await until(store.dimensions).toMatch(({ height, width }) => !isNaN(width) && width > 0 && !isNaN(height) && height > 0)
if (!store.getNodes.length) return
let nodes: GraphNode[] = []
@@ -43,6 +37,7 @@ export default (store: Store = useVueFlow().store): ViewportFuncs => ({
if (!nodes || !nodes.length) {
nodes = options.includeHiddenNodes ? store.nodes : store.getNodes
}
const bounds = getRectOfNodes(nodes)
const { x, y, zoom } = getTransformForBounds(
bounds,
@@ -53,6 +48,7 @@ export default (store: Store = useVueFlow().store): ViewportFuncs => ({
options.padding ?? DEFAULT_PADDING,
options.offset,
)
const transform = zoomIdentity.translate(x, y).scale(zoom)
store.d3Selection && store.d3Zoom?.transform(transition(store.d3Selection, options?.duration), transform)