feat(store): flatten child map of nodes
Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -9,7 +9,6 @@ import {
|
|||||||
Elements,
|
Elements,
|
||||||
FlowInstance,
|
FlowInstance,
|
||||||
addEdge,
|
addEdge,
|
||||||
isNode,
|
|
||||||
removeElements,
|
removeElements,
|
||||||
} from '~/index'
|
} from '~/index'
|
||||||
|
|
||||||
@@ -21,7 +20,10 @@ const nodes = ref<Elements>([
|
|||||||
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 } },
|
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 } },
|
||||||
{ id: '4', label: 'Node 4', position: { x: 400, y: 200 } },
|
{ id: '4', label: 'Node 4', position: { x: 400, y: 200 } },
|
||||||
])
|
])
|
||||||
const edges = ref<Elements>([])
|
const edges = ref<Elements>([
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
|
])
|
||||||
const vfInstance = ref<FlowInstance>()
|
const vfInstance = ref<FlowInstance>()
|
||||||
const onLoad = (flowInstance: FlowInstance) => {
|
const onLoad = (flowInstance: FlowInstance) => {
|
||||||
flowInstance.fitView({ padding: 0.1 })
|
flowInstance.fitView({ padding: 0.1 })
|
||||||
|
|||||||
@@ -3,27 +3,27 @@ import GroupNode from './GroupNode.vue'
|
|||||||
import { VueFlow, Elements, Node, Edge, Connection, addEdge } from '~/index'
|
import { VueFlow, Elements, Node, Edge, Connection, addEdge } from '~/index'
|
||||||
|
|
||||||
const elements = ref<Elements<{ label: string; group?: string }>>([
|
const elements = ref<Elements<{ label: string; group?: string }>>([
|
||||||
{
|
{ id: 'node-2', label: 'node-2', position: { x: 50, y: 5 } },
|
||||||
id: 'node-1',
|
|
||||||
data: { label: 'node-1', group: undefined },
|
|
||||||
position: { x: 250, y: 5 },
|
|
||||||
parentNode: 'group-a',
|
|
||||||
extent: 'parent',
|
|
||||||
},
|
|
||||||
{ id: 'node-2', data: { label: 'node-2', group: undefined }, position: { x: 50, y: 5 } },
|
|
||||||
{
|
{
|
||||||
id: 'group-a',
|
id: 'group-a',
|
||||||
style: { zIndex: 2, width: '300px', height: '300px' },
|
style: { zIndex: 2, width: '300px', height: '300px' },
|
||||||
selectable: false,
|
selectable: false,
|
||||||
data: { label: 'A' },
|
label: 'A',
|
||||||
position: { x: 50, y: 100 },
|
position: { x: 50, y: 100 },
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
id: 'node-1',
|
||||||
|
label: 'node-1',
|
||||||
|
position: { x: 250, y: 5 },
|
||||||
|
extent: 'parent',
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'group-b',
|
id: 'group-b',
|
||||||
type: 'group-b',
|
|
||||||
style: { zIndex: 2 },
|
style: { zIndex: 2 },
|
||||||
selectable: false,
|
selectable: false,
|
||||||
data: { label: 'B' },
|
label: 'B',
|
||||||
position: { x: 500, y: 100 },
|
position: { x: 500, y: 100 },
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|||||||
+26
-10
@@ -95,22 +95,37 @@ export default (state: FlowState, getters: FlowGetters): FlowActions => {
|
|||||||
state.nodesConnectable = isInteractive
|
state.nodesConnectable = isInteractive
|
||||||
state.elementsSelectable = isInteractive
|
state.elementsSelectable = isInteractive
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getParent(root: Node[], id: string): GraphNode | undefined {
|
||||||
|
let node
|
||||||
|
|
||||||
|
root.some((n) => {
|
||||||
|
if (n.id === id) {
|
||||||
|
return (node = n)
|
||||||
|
}
|
||||||
|
if (n.children) {
|
||||||
|
return (node = getParent(n.children, id))
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
const setNodes: FlowActions['setNodes'] = (nodes, extent: CoordinateExtent) => {
|
const setNodes: FlowActions['setNodes'] = (nodes, extent: CoordinateExtent) => {
|
||||||
const parseChildren = (n: Node, arr: GraphNode[] = []) => {
|
const parseChildren = (n: Node, p?: GraphNode, arr: GraphNode[] = []) => {
|
||||||
arr.concat(parseNode(n, extent))
|
const parent = typeof p === 'undefined' || typeof p !== 'object' ? getParent(arr, n.id) : p
|
||||||
|
const parsed = parseNode(n, extent, {
|
||||||
|
parentNode: parent,
|
||||||
|
})
|
||||||
|
arr.push(parsed)
|
||||||
if (n.children && n.children.length) {
|
if (n.children && n.children.length) {
|
||||||
n.children.forEach((c) => parseChildren(c))
|
n.children.forEach((c) => parseChildren(c, parsed, arr))
|
||||||
} else {
|
|
||||||
return arr
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nodes = nodes.flatMap((node) => {
|
nodes = nodes.flatMap((node) => {
|
||||||
const parsed = parseNode(node, extent)
|
|
||||||
const children: GraphNode[] = []
|
const children: GraphNode[] = []
|
||||||
if (node.children && node.children.length) {
|
parseChildren(node, undefined, children)
|
||||||
node.children.forEach((c) => parseChildren(c, children))
|
return children
|
||||||
}
|
|
||||||
return [parsed, ...children]
|
|
||||||
})
|
})
|
||||||
state.nodes = <GraphNode[]>nodes
|
state.nodes = <GraphNode[]>nodes
|
||||||
}
|
}
|
||||||
@@ -136,6 +151,7 @@ export default (state: FlowState, getters: FlowGetters): FlowActions => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const setState: FlowActions['setState'] = (opts) => {
|
const setState: FlowActions['setState'] = (opts) => {
|
||||||
|
if (typeof opts.modelValue !== 'undefined') setElements(opts.modelValue, opts.nodeExtent ?? state.nodeExtent)
|
||||||
if (typeof opts.elements !== 'undefined') setElements(opts.elements, opts.nodeExtent ?? state.nodeExtent)
|
if (typeof opts.elements !== 'undefined') setElements(opts.elements, opts.nodeExtent ?? state.nodeExtent)
|
||||||
if (typeof opts.nodes !== 'undefined') setNodes(opts.nodes, opts.nodeExtent ?? state.nodeExtent)
|
if (typeof opts.nodes !== 'undefined') setNodes(opts.nodes, opts.nodeExtent ?? state.nodeExtent)
|
||||||
if (typeof opts.edges !== 'undefined') setEdges(opts.edges)
|
if (typeof opts.edges !== 'undefined') setEdges(opts.edges)
|
||||||
|
|||||||
+2
-1
@@ -113,7 +113,8 @@ export type FlowInstance<DataNode = any, DataEdge = DataNode> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface FlowProps<DataNode = any, DataEdge = DataNode> {
|
export interface FlowProps<DataNode = any, DataEdge = DataNode> {
|
||||||
nodes: Node<DataNode>[]
|
modelValue?: any
|
||||||
|
nodes?: Node<DataNode>[]
|
||||||
edges?: Edge<DataEdge>[]
|
edges?: Edge<DataEdge>[]
|
||||||
elements?: Elements
|
elements?: Elements
|
||||||
id?: string
|
id?: string
|
||||||
|
|||||||
+3
-4
@@ -163,7 +163,8 @@ export const pointToRendererPoint = (
|
|||||||
export const onLoadProject = (currentStore: FlowStore) => (position: XYPosition) =>
|
export const onLoadProject = (currentStore: FlowStore) => (position: XYPosition) =>
|
||||||
pointToRendererPoint(position, currentStore.transform, currentStore.snapToGrid, currentStore.snapGrid)
|
pointToRendererPoint(position, currentStore.transform, currentStore.snapToGrid, currentStore.snapGrid)
|
||||||
|
|
||||||
export const parseNode = (node: Node, nodeExtent: CoordinateExtent): GraphNode => ({
|
export const parseNode = (node: Node, nodeExtent: CoordinateExtent, defaults?: Partial<GraphNode>): GraphNode => ({
|
||||||
|
...defaults,
|
||||||
...node,
|
...node,
|
||||||
id: node.id.toString(),
|
id: node.id.toString(),
|
||||||
type: node.type ?? 'default',
|
type: node.type ?? 'default',
|
||||||
@@ -322,9 +323,7 @@ export function calculateXYZPosition(node: GraphNode, result: XYZPosition): XYZP
|
|||||||
x: result.x + node.parentNode.position.x,
|
x: result.x + node.parentNode.position.x,
|
||||||
y: result.y + node.parentNode.position.y,
|
y: result.y + node.parentNode.position.y,
|
||||||
z:
|
z:
|
||||||
node.parentNode.computedPosition.z > node.computedPosition.z
|
node.parentNode.computedPosition.z > node.computedPosition.z ? node.parentNode.computedPosition.z : node.computedPosition.z,
|
||||||
? node.parentNode.computedPosition.z + 1
|
|
||||||
: node.computedPosition.z,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user