refactor: directly bind instance functions
This commit is contained in:
@@ -2,8 +2,8 @@
|
|||||||
import NodeRenderer from '../NodeRenderer/NodeRenderer.vue'
|
import NodeRenderer from '../NodeRenderer/NodeRenderer.vue'
|
||||||
import EdgeRenderer from '../EdgeRenderer/EdgeRenderer.vue'
|
import EdgeRenderer from '../EdgeRenderer/EdgeRenderer.vue'
|
||||||
import { useVueFlow, useZoomPanHelper, useWindow } from '../../composables'
|
import { useVueFlow, useZoomPanHelper, useWindow } from '../../composables'
|
||||||
import { FlowInstance, Store } from '../../types'
|
import { FlowExportObject, FlowInstance, Store, XYPosition } from '../../types'
|
||||||
import { onLoadGetEdges, onLoadGetElements, onLoadGetNodes, onLoadProject, onLoadToObject } from '../../utils'
|
import { pointToRendererPoint } from '../../utils'
|
||||||
|
|
||||||
const { id, store } = useVueFlow()
|
const { id, store } = useVueFlow()
|
||||||
|
|
||||||
@@ -20,23 +20,38 @@ const untilDimensions = async (store: Store) => {
|
|||||||
|
|
||||||
const ready = ref(false)
|
const ready = ref(false)
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
const { zoomIn, zoomOut, zoomTo, setTransform, getTransform, fitView, fitBounds, setCenter } = useZoomPanHelper(store)
|
const { fitView, ...rest } = useZoomPanHelper(store)
|
||||||
const instance: FlowInstance = {
|
let instance: FlowInstance | null = {
|
||||||
fitView: (params = { padding: 0.1 }) => fitView(params),
|
fitView: (params = { padding: 0.1 }) => fitView(params),
|
||||||
zoomIn,
|
...rest,
|
||||||
zoomOut,
|
|
||||||
zoomTo,
|
project(position: XYPosition) {
|
||||||
setTransform,
|
return pointToRendererPoint(position, store.viewport, store.snapToGrid, store.snapGrid)
|
||||||
getTransform,
|
},
|
||||||
setCenter,
|
getElements() {
|
||||||
fitBounds,
|
return [...store.nodes, ...store.edges]
|
||||||
project: onLoadProject(store),
|
},
|
||||||
getElements: onLoadGetElements(store),
|
getNodes() {
|
||||||
getNodes: onLoadGetNodes(store),
|
return store.nodes
|
||||||
getEdges: onLoadGetEdges(store),
|
},
|
||||||
toObject: onLoadToObject(store),
|
getEdges() {
|
||||||
|
return store.edges
|
||||||
|
},
|
||||||
|
toObject() {
|
||||||
|
// we have to stringify/parse so objects containing refs (like nodes and edges) can potentially be saved in a storage
|
||||||
|
return JSON.parse(
|
||||||
|
JSON.stringify({
|
||||||
|
nodes: store.nodes,
|
||||||
|
edges: store.edges,
|
||||||
|
position: [store.viewport.x, store.viewport.y],
|
||||||
|
zoom: store.viewport.zoom,
|
||||||
|
} as FlowExportObject),
|
||||||
|
)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onScopeDispose(() => (instance = null))
|
||||||
|
|
||||||
await untilDimensions(store)
|
await untilDimensions(store)
|
||||||
|
|
||||||
ready.value = true
|
ready.value = true
|
||||||
|
|||||||
@@ -8,15 +8,11 @@ import {
|
|||||||
EdgeMarkerType,
|
EdgeMarkerType,
|
||||||
Elements,
|
Elements,
|
||||||
FlowElement,
|
FlowElement,
|
||||||
FlowElements,
|
|
||||||
FlowExportObject,
|
|
||||||
Getters,
|
Getters,
|
||||||
GraphEdge,
|
GraphEdge,
|
||||||
GraphNode,
|
GraphNode,
|
||||||
Node,
|
Node,
|
||||||
Rect,
|
Rect,
|
||||||
State,
|
|
||||||
Store,
|
|
||||||
Viewport,
|
Viewport,
|
||||||
XYPosition,
|
XYPosition,
|
||||||
XYZPosition,
|
XYZPosition,
|
||||||
@@ -219,9 +215,6 @@ export const pointToRendererPoint = (
|
|||||||
return position
|
return position
|
||||||
}
|
}
|
||||||
|
|
||||||
export const onLoadProject = (currentStore: Store) => (position: XYPosition) =>
|
|
||||||
pointToRendererPoint(position, currentStore.viewport, currentStore.snapToGrid, currentStore.snapGrid)
|
|
||||||
|
|
||||||
const getBoundsOfBoxes = (box1: Box, box2: Box): Box => ({
|
const getBoundsOfBoxes = (box1: Box, box2: Box): Box => ({
|
||||||
x: Math.min(box1.x, box2.x),
|
x: Math.min(box1.x, box2.x),
|
||||||
y: Math.min(box1.y, box2.y),
|
y: Math.min(box1.y, box2.y),
|
||||||
@@ -304,22 +297,6 @@ export const getConnectedEdges = (nodes: GraphNode[], edges: GraphEdge[]) => {
|
|||||||
return edges.filter((edge) => nodeIds.includes(edge.source) || nodeIds.includes(edge.target))
|
return edges.filter((edge) => nodeIds.includes(edge.source) || nodeIds.includes(edge.target))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const onLoadGetNodes = (store: Store) => (): GraphNode[] => store.nodes
|
|
||||||
export const onLoadGetEdges = (store: Store) => (): GraphEdge[] => store.edges
|
|
||||||
export const onLoadGetElements = (store: Store) => (): FlowElements => [...store.nodes, ...store.edges]
|
|
||||||
|
|
||||||
export const onLoadToObject = (store: State) => (): FlowExportObject => {
|
|
||||||
// we have to stringify/parse so objects containing refs (like nodes and edges) can potentially be saved in a storage
|
|
||||||
return JSON.parse(
|
|
||||||
JSON.stringify({
|
|
||||||
nodes: store.nodes,
|
|
||||||
edges: store.edges,
|
|
||||||
position: [store.viewport.x, store.viewport.y],
|
|
||||||
zoom: store.viewport.zoom,
|
|
||||||
} as FlowExportObject),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getTransformForBounds = (
|
export const getTransformForBounds = (
|
||||||
bounds: Rect,
|
bounds: Rect,
|
||||||
width: number,
|
width: number,
|
||||||
|
|||||||
Reference in New Issue
Block a user