refactor(core): replace array fns
This commit is contained in:
@@ -133,9 +133,11 @@ export function applyChanges<
|
||||
const elementIds = elements.map((el) => el.id)
|
||||
|
||||
for (const element of elements) {
|
||||
const currentChanges = changes.filter((c) => (<any>c).id === element.id)
|
||||
for (const currentChange of changes) {
|
||||
if ((<any>currentChange).id !== element.id) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (const currentChange of currentChanges) {
|
||||
switch (currentChange.type) {
|
||||
case 'select':
|
||||
element.selected = currentChange.selected
|
||||
|
||||
@@ -34,28 +34,32 @@ export function getDragItems(
|
||||
findNode: Actions['findNode'],
|
||||
nodeId?: string,
|
||||
): NodeDragItem[] {
|
||||
return nodes
|
||||
.filter(
|
||||
(n) =>
|
||||
(n.selected || n.id === nodeId) &&
|
||||
(!n.parentNode || !isParentSelected(n, findNode)) &&
|
||||
(n.draggable || (nodesDraggable && typeof n.draggable === 'undefined')),
|
||||
)
|
||||
.map((n) =>
|
||||
markRaw({
|
||||
id: n.id,
|
||||
position: n.position || { x: 0, y: 0 },
|
||||
distance: {
|
||||
x: mousePos.x - n.computedPosition?.x || 0,
|
||||
y: mousePos.y - n.computedPosition?.y || 0,
|
||||
},
|
||||
from: n.computedPosition,
|
||||
extent: n.extent,
|
||||
parentNode: n.parentNode,
|
||||
dimensions: n.dimensions,
|
||||
expandParent: n.expandParent,
|
||||
}),
|
||||
)
|
||||
const dragItems: NodeDragItem[] = []
|
||||
for (const node of nodes) {
|
||||
if (
|
||||
(node.selected || node.id === nodeId) &&
|
||||
(!node.parentNode || !isParentSelected(node, findNode)) &&
|
||||
(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined'))
|
||||
) {
|
||||
dragItems.push(
|
||||
markRaw({
|
||||
id: node.id,
|
||||
position: node.position || { x: 0, y: 0 },
|
||||
distance: {
|
||||
x: mousePos.x - node.computedPosition?.x || 0,
|
||||
y: mousePos.y - node.computedPosition?.y || 0,
|
||||
},
|
||||
from: node.computedPosition,
|
||||
extent: node.extent,
|
||||
parentNode: node.parentNode,
|
||||
dimensions: node.dimensions,
|
||||
expandParent: node.expandParent,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return dragItems
|
||||
}
|
||||
|
||||
export function getEventHandlerParams({
|
||||
@@ -67,15 +71,14 @@ export function getEventHandlerParams({
|
||||
dragItems: NodeDragItem[]
|
||||
findNode: Actions['findNode']
|
||||
}): [GraphNode, GraphNode[]] {
|
||||
const extendedDragItems = dragItems.reduce((acc, dragItem) => {
|
||||
const extendedDragItems: GraphNode[] = []
|
||||
for (const dragItem of dragItems) {
|
||||
const node = findNode(dragItem.id)
|
||||
|
||||
if (node) {
|
||||
acc.push(node)
|
||||
extendedDragItems.push(node)
|
||||
}
|
||||
|
||||
return acc
|
||||
}, [] as GraphNode[])
|
||||
}
|
||||
|
||||
return [id ? extendedDragItems.find((n) => n.id === id)! : extendedDragItems[0], extendedDragItems]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
import { markRaw, unref } from 'vue'
|
||||
import type { Actions, Connection, ConnectionLookup, DefaultEdgeOptions, Edge, GraphEdge, GraphNode, Node, State } from '../types'
|
||||
import type {
|
||||
Actions,
|
||||
Connection,
|
||||
ConnectionLookup,
|
||||
DefaultEdgeOptions,
|
||||
Edge,
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
Node,
|
||||
State,
|
||||
ValidConnectionFunc,
|
||||
VueFlowStore,
|
||||
} from '../types'
|
||||
import { ErrorCode, VueFlowError, connectionExists, getEdgeId, isEdge, isNode, parseEdge, parseNode } from '.'
|
||||
|
||||
type NonUndefined<T> = T extends undefined ? never : T
|
||||
@@ -195,3 +207,71 @@ export function areConnectionMapsEqual(a?: Map<string, Connection>, b?: Map<stri
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export function createGraphEdges(
|
||||
nextEdges: (Edge | Connection)[],
|
||||
isValidConnection: ValidConnectionFunc | null,
|
||||
findNode: Actions['findNode'],
|
||||
findEdge: Actions['findEdge'],
|
||||
onError: VueFlowStore['emits']['error'],
|
||||
defaultEdgeOptions: DefaultEdgeOptions | undefined,
|
||||
nodes: GraphNode[],
|
||||
edges: GraphEdge[],
|
||||
) {
|
||||
const validEdges: GraphEdge[] = []
|
||||
|
||||
for (const edgeOrConnection of nextEdges) {
|
||||
const edge = isEdge(edgeOrConnection)
|
||||
? edgeOrConnection
|
||||
: addEdgeToStore(edgeOrConnection, edges, onError, defaultEdgeOptions)
|
||||
|
||||
if (!edge) {
|
||||
continue
|
||||
}
|
||||
|
||||
const sourceNode = findNode(edge.source)
|
||||
const targetNode = findNode(edge.target)
|
||||
|
||||
if (!sourceNode || !targetNode) {
|
||||
onError(new VueFlowError(ErrorCode.EDGE_SOURCE_TARGET_MISSING, edge.id, edge.source, edge.target))
|
||||
continue
|
||||
}
|
||||
|
||||
if (!sourceNode) {
|
||||
onError(new VueFlowError(ErrorCode.EDGE_SOURCE_MISSING, edge.id, edge.source))
|
||||
continue
|
||||
}
|
||||
|
||||
if (!targetNode) {
|
||||
onError(new VueFlowError(ErrorCode.EDGE_TARGET_MISSING, edge.id, edge.target))
|
||||
continue
|
||||
}
|
||||
|
||||
if (isValidConnection) {
|
||||
const isValid = isValidConnection(edge, {
|
||||
edges,
|
||||
nodes,
|
||||
sourceNode,
|
||||
targetNode,
|
||||
})
|
||||
|
||||
if (!isValid) {
|
||||
onError(new VueFlowError(ErrorCode.EDGE_INVALID, edge.id))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
const existingEdge = findEdge(edge.id)
|
||||
|
||||
validEdges.push({
|
||||
...parseEdge(edge, existingEdge, defaultEdgeOptions),
|
||||
sourceNode,
|
||||
targetNode,
|
||||
})
|
||||
}
|
||||
|
||||
return validEdges
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user