166 lines
4.8 KiB
TypeScript
166 lines
4.8 KiB
TypeScript
import { createId } from '../../tools/tools'
|
||
import { NODE_DATATYPE, NODE_COMPONENT, NODE_DATATIER } from '../tools/index.d'
|
||
interface NodeData {
|
||
type?: string
|
||
component?: any// 节点组件
|
||
data?: object// 节点数据
|
||
tier?: string// 节点层级
|
||
isHeader?: boolean// 是否显示头
|
||
superiorID?: string// 上级节点ID
|
||
superiorNodeType?: string// 上级节点类型
|
||
disableDelete?: boolean// 是否禁用删除
|
||
disableCopy?: boolean// 是否禁用复制
|
||
createIndexPosition?: number// 创建索引位置
|
||
isActive?: boolean// 是否激活
|
||
}
|
||
interface NodeOptions {
|
||
id?: string
|
||
position?: { x: number, y: number }
|
||
positionX?: number
|
||
positionY?: number
|
||
component?: any
|
||
data?: NodeData
|
||
}// 不可传入type class (内部使用)
|
||
|
||
export class NodeManager {
|
||
stateManager: any
|
||
vueFlow: any
|
||
nodesep = 100 // 节点间距
|
||
ranksep = 100 // 层级间距
|
||
constructor(options) {
|
||
this.stateManager = options.stateManager;
|
||
this.vueFlow = options.vueFlow
|
||
}
|
||
|
||
/** 删除节点 */
|
||
deleteNode(id: string) {
|
||
this.stateManager.getSubordinateAllNodes(id, { isElMessageBox: true })
|
||
}
|
||
/** 添加节点 */
|
||
addNode(node: any) {
|
||
this.stateManager.addNode(node)
|
||
this.stateManager.setActiveNodeID(node.id)
|
||
}
|
||
|
||
/** 创建节点 */
|
||
createNode(options: NodeOptions) {
|
||
const superiorID = options?.data?.superiorID
|
||
//获取上级节点所生成的最后一个node,设置位置为最后一个节点的xy 加上 节点间距
|
||
const superiorGenerateNodes = this.stateManager.getSubordNodes(superiorID)
|
||
const currentNode = superiorGenerateNodes.find((node) => {
|
||
return (node.data.createIndexPosition === options?.data?.createIndexPosition && options?.data?.createIndexPosition)
|
||
})
|
||
const endGenerateNode = superiorGenerateNodes.reduce((max, current) => {
|
||
return current.data.createIndexPosition > max.data.createIndexPosition ? current : max
|
||
}, superiorGenerateNodes[0])
|
||
const snode = superiorID ? this.stateManager.flowManager.getNodeById(superiorID) : this.stateManager.flowManager.getLastNode();
|
||
const id = options.id || createId()
|
||
const positionX = options.positionX || 0
|
||
const positionY = options.positionY || 0
|
||
const position = options.position ||
|
||
(
|
||
currentNode ?
|
||
currentNode.position :
|
||
endGenerateNode ?
|
||
{
|
||
x: endGenerateNode.position.x + positionX,
|
||
y: endGenerateNode.position.y + positionY + this.ranksep + 200
|
||
} : snode ?
|
||
{
|
||
x: snode.position.x + snode.dimensions.width + this.nodesep + positionX,
|
||
y: snode.position.y + positionY
|
||
} :
|
||
{
|
||
x: positionX,
|
||
y: positionY
|
||
}
|
||
)
|
||
const data = options?.data || {}
|
||
data['component'] = options.component
|
||
const options_ = {
|
||
id,
|
||
position,
|
||
data
|
||
}
|
||
if(currentNode)this.stateManager.deleteNode(currentNode.id)
|
||
this.addNode(options_)
|
||
return options_;
|
||
}
|
||
/** 创建结果节点 */
|
||
createResultNode(options?: NodeOptions) {
|
||
const options_ = {
|
||
...(options ? options : {}),
|
||
component: NODE_COMPONENT.RESULT_IMAGE,
|
||
positionY: options?.positionY || 0,
|
||
data: {
|
||
tier: NODE_DATATIER.RESULT_IMAGE,
|
||
type: NODE_DATATYPE.RESULT_IMAGE,
|
||
createIndexPosition: options?.data?.createIndexPosition || 1,
|
||
isHeader: true,
|
||
...(options?.data || {}),
|
||
},
|
||
}
|
||
return this.createNode(options_)
|
||
}
|
||
/** 创建卡片选择节点 */
|
||
createCardsSelect(options?: NodeOptions) {
|
||
const options_ = {
|
||
...(options ? options : {}),
|
||
component: NODE_COMPONENT.CARD,
|
||
positionY: 50,
|
||
data: {
|
||
tier: NODE_DATATIER.CARDS_SELECT,
|
||
type: NODE_DATATYPE.CARDS_SELECT,
|
||
createIndexPosition: options?.data?.createIndexPosition || 1,
|
||
...(options?.data || {}),
|
||
},
|
||
}
|
||
return this.createNode(options_)
|
||
}
|
||
/** 创建卡片节点 */
|
||
createCardNode(options?: NodeOptions) {
|
||
const options_ = {
|
||
...(options ? options : {}),
|
||
component: NODE_COMPONENT.CARD,
|
||
data: {
|
||
...(options?.data || {}),
|
||
}
|
||
}
|
||
return this.createNode(options_)
|
||
}
|
||
/** 创建文本节点 */
|
||
createTextNode(options?: NodeOptions) {
|
||
const options_ = {
|
||
...(options ? options : {}),
|
||
component: NODE_COMPONENT.TEXT,
|
||
data: {
|
||
...(options?.data || {}),
|
||
}
|
||
}
|
||
return this.createNode(options_)
|
||
}
|
||
|
||
copyNodeById(id: string) {
|
||
const node = this.stateManager.getNodeById(id)
|
||
let copyNode = JSON.parse(JSON.stringify(node))
|
||
const flowNode = this.stateManager.flowManager.getNodeById(id)
|
||
if (!node) return console.warn(`${id}找不到对应节点`)
|
||
if (node.data?.disableCopy) return console.warn(`${id}节点已禁用复制`)
|
||
const node_ = {
|
||
...copyNode,
|
||
id: createId(),
|
||
position: {
|
||
x: node.position.x,
|
||
y: node.position.y + (flowNode?.dimensions?.height || 0) + this.ranksep,
|
||
}
|
||
}
|
||
delete node_.data?.superiorID
|
||
delete node_.data?.disableDelete
|
||
node_.data.data.imageProcessTasks.forEach((item) => {
|
||
item.canvasId = null
|
||
})
|
||
this.stateManager.addNode(node_)
|
||
}
|
||
dispose() {}
|
||
}
|