Files
esengine/packages/behavior-tree-editor/src/application/use-cases/DeleteNodeUseCase.ts

77 lines
2.1 KiB
TypeScript
Raw Normal View History

import { CommandManager, ICommand } from '@esengine/editor-runtime';
import { DeleteNodeCommand } from '../commands/tree/DeleteNodeCommand';
import { RemoveConnectionCommand } from '../commands/tree/RemoveConnectionCommand';
import { ITreeState } from '../commands/ITreeState';
/**
*
*
*/
export class DeleteNodeUseCase {
constructor(
private readonly commandManager: CommandManager,
private readonly treeState: ITreeState
) {}
/**
*
*/
execute(nodeId: string): void {
const tree = this.treeState.getTree();
const relatedConnections = tree.connections.filter(
(conn) => conn.from === nodeId || conn.to === nodeId
);
const commands: ICommand[] = [];
relatedConnections.forEach((conn) => {
commands.push(
new RemoveConnectionCommand(
this.treeState,
conn.from,
conn.to,
conn.fromProperty,
conn.toProperty
)
);
});
commands.push(new DeleteNodeCommand(this.treeState, nodeId));
this.commandManager.executeBatch(commands);
}
/**
*
*/
executeBatch(nodeIds: string[]): void {
const tree = this.treeState.getTree();
const commands: ICommand[] = [];
const nodeIdSet = new Set(nodeIds);
const relatedConnections = tree.connections.filter(
(conn) => nodeIdSet.has(conn.from) || nodeIdSet.has(conn.to)
);
relatedConnections.forEach((conn) => {
commands.push(
new RemoveConnectionCommand(
this.treeState,
conn.from,
conn.to,
conn.fromProperty,
conn.toProperty
)
);
});
nodeIds.forEach((nodeId) => {
commands.push(new DeleteNodeCommand(this.treeState, nodeId));
});
this.commandManager.executeBatch(commands);
}
}