2025-11-03 21:22:16 +08:00
|
|
|
import { useCallback, useMemo } from 'react';
|
2025-11-18 14:46:51 +08:00
|
|
|
import { CommandManager } from '@esengine/editor-core';
|
|
|
|
|
import { ConnectionType } from '../domain/models/Connection';
|
|
|
|
|
import { IValidator } from '../domain/interfaces/IValidator';
|
|
|
|
|
import { TreeStateAdapter } from '../application/state/BehaviorTreeDataStore';
|
|
|
|
|
import { AddConnectionUseCase } from '../application/use-cases/AddConnectionUseCase';
|
|
|
|
|
import { RemoveConnectionUseCase } from '../application/use-cases/RemoveConnectionUseCase';
|
|
|
|
|
import { createLogger } from '@esengine/ecs-framework';
|
|
|
|
|
|
|
|
|
|
const logger = createLogger('useConnectionOperations');
|
2025-11-03 21:22:16 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接操作 Hook
|
|
|
|
|
*/
|
|
|
|
|
export function useConnectionOperations(
|
|
|
|
|
validator: IValidator,
|
|
|
|
|
commandManager: CommandManager
|
|
|
|
|
) {
|
2025-11-18 14:46:51 +08:00
|
|
|
const treeState = useMemo(() => TreeStateAdapter.getInstance(), []);
|
2025-11-03 21:22:16 +08:00
|
|
|
|
|
|
|
|
const addConnectionUseCase = useMemo(
|
|
|
|
|
() => new AddConnectionUseCase(commandManager, treeState, validator),
|
|
|
|
|
[commandManager, treeState, validator]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const removeConnectionUseCase = useMemo(
|
|
|
|
|
() => new RemoveConnectionUseCase(commandManager, treeState),
|
|
|
|
|
[commandManager, treeState]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const addConnection = useCallback((
|
|
|
|
|
from: string,
|
|
|
|
|
to: string,
|
|
|
|
|
connectionType: ConnectionType = 'node',
|
|
|
|
|
fromProperty?: string,
|
|
|
|
|
toProperty?: string
|
|
|
|
|
) => {
|
|
|
|
|
try {
|
|
|
|
|
return addConnectionUseCase.execute(from, to, connectionType, fromProperty, toProperty);
|
|
|
|
|
} catch (error) {
|
2025-11-18 14:46:51 +08:00
|
|
|
logger.error('添加连接失败:', error);
|
2025-11-03 21:22:16 +08:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}, [addConnectionUseCase]);
|
|
|
|
|
|
|
|
|
|
const removeConnection = useCallback((
|
|
|
|
|
from: string,
|
|
|
|
|
to: string,
|
|
|
|
|
fromProperty?: string,
|
|
|
|
|
toProperty?: string
|
|
|
|
|
) => {
|
|
|
|
|
try {
|
|
|
|
|
removeConnectionUseCase.execute(from, to, fromProperty, toProperty);
|
|
|
|
|
} catch (error) {
|
2025-11-18 14:46:51 +08:00
|
|
|
logger.error('移除连接失败:', error);
|
2025-11-03 21:22:16 +08:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}, [removeConnectionUseCase]);
|
|
|
|
|
|
|
|
|
|
return useMemo(() => ({
|
|
|
|
|
addConnection,
|
|
|
|
|
removeConnection
|
|
|
|
|
}), [addConnection, removeConnection]);
|
|
|
|
|
}
|