109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
|
|
/*
|
||
|
|
* This file is part of the kernelstudio package.
|
||
|
|
*
|
||
|
|
* (c) 2014-2026 zlin <admin@kernelstudio.com>
|
||
|
|
*
|
||
|
|
* For the full copyright and license information, please view the LICENSE file
|
||
|
|
* that was distributed with this source code.
|
||
|
|
*/
|
||
|
|
import type { EdgeNodeElement, NodeGraph, TaskNodeElement } from './element';
|
||
|
|
import { Edge, Graph, Node } from '@antv/x6';
|
||
|
|
|
||
|
|
export const defaultHeight: Record<string, number> = {
|
||
|
|
component: 110,
|
||
|
|
};
|
||
|
|
|
||
|
|
export const createModelNode = (element: TaskNodeElement, width: number = 250, height: number = 120): any => {
|
||
|
|
let realHeight = defaultHeight[element.category as string];
|
||
|
|
if (!realHeight) {
|
||
|
|
realHeight = 120;
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
shape: 'node',
|
||
|
|
id: element.key,
|
||
|
|
position: {
|
||
|
|
x: element.position?.x || 0,
|
||
|
|
y: element.position?.y || 0,
|
||
|
|
},
|
||
|
|
size: {
|
||
|
|
width: width,
|
||
|
|
height: realHeight,
|
||
|
|
},
|
||
|
|
attrs: {
|
||
|
|
label: {
|
||
|
|
text: element.name,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
data: element,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
export const resolveNodeTaskElements = (graph: Graph): TaskNodeElement[] => {
|
||
|
|
const taskElements: TaskNodeElement[] = [];
|
||
|
|
if (graph) {
|
||
|
|
const nodes = graph?.getNodes() as Node[];
|
||
|
|
if (nodes) {
|
||
|
|
nodes.forEach(node => {
|
||
|
|
const nodeData = node.getData() as TaskNodeElement;
|
||
|
|
const newElement = {
|
||
|
|
...nodeData,
|
||
|
|
key: node.id,
|
||
|
|
position: node.getPosition(),
|
||
|
|
width: node.getSize().width,
|
||
|
|
height: node.getSize().height,
|
||
|
|
};
|
||
|
|
taskElements.push(newElement);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return taskElements;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const resolveNodeEdgeElements = (graph: Graph): EdgeNodeElement[] => {
|
||
|
|
const edgeElements: EdgeNodeElement[] = [];
|
||
|
|
if (graph) {
|
||
|
|
const graphEdges = graph?.getEdges() ?? [] as Edge[];
|
||
|
|
if (graphEdges) {
|
||
|
|
graphEdges.forEach(edge => {
|
||
|
|
const nodeData = edge.getData() as TaskNodeElement;
|
||
|
|
edgeElements.push({
|
||
|
|
id: nodeData?.id ?? 0,
|
||
|
|
key: edge.id,
|
||
|
|
type: 'edge',
|
||
|
|
status: nodeData?.status,
|
||
|
|
source: edge.getSource() ? edge.getSource() as unknown as string : null,
|
||
|
|
target: edge.getSource() ? edge.getTarget() as unknown as string : null,
|
||
|
|
attrs: edge.getAttrs() ?? {},
|
||
|
|
router: edge.getRouter() ?? {},
|
||
|
|
connector: edge.getConnector() ?? null,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return edgeElements;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const resolveNodeGraph = (graph: Graph): NodeGraph => {
|
||
|
|
const nodes: TaskNodeElement[] = resolveNodeTaskElements(graph);
|
||
|
|
const edges: EdgeNodeElement[] = resolveNodeEdgeElements(graph);
|
||
|
|
return {
|
||
|
|
nodes,
|
||
|
|
edges,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
export const hasElements = (graph: Graph): boolean => {
|
||
|
|
if (graph) {
|
||
|
|
const taskElements: TaskNodeElement[] = resolveNodeTaskElements(graph);
|
||
|
|
return taskElements.length > 0;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const hasRootElementNode = (graph: Graph): boolean => {
|
||
|
|
if (graph) {
|
||
|
|
const taskElements: TaskNodeElement[] = resolveNodeTaskElements(graph);
|
||
|
|
return taskElements.filter(e => e.type === 'root').length === 1;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
};
|