Files
esengine/packages/behavior-tree/src/editor/hooks/useDropHandler.ts
YHH 107439d70c Feature/runtime cdn and plugin loader (#240)
* feat(ui): 完善 UI 布局系统和编辑器可视化工具

* refactor: 移除 ModuleRegistry,统一使用 PluginManager 插件系统

* fix: 修复 CodeQL 警告并提升测试覆盖率

* refactor: 分离运行时入口点,解决 runtime bundle 包含 React 的问题

* fix(ci): 添加 editor-core 和 editor-runtime 到 CI 依赖构建步骤

* docs: 完善 ServiceContainer 文档,新增 Symbol.for 模式和 @InjectProperty 说明

* fix(ci): 修复 type-check 失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): behavior-tree 构建添加 @tauri-apps 外部依赖

* fix(ci): behavior-tree 添加 @tauri-apps/plugin-fs 类型依赖

* fix(ci): platform-web 添加缺失的 behavior-tree 依赖

* fix(lint): 移除正则表达式中不必要的转义字符
2025-11-27 20:42:46 +08:00

132 lines
4.2 KiB
TypeScript

import { useState, type RefObject, React, createLogger } from '@esengine/editor-runtime';
import { NodeTemplate, NodeType } from '../..';
import { Position } from '../domain/value-objects/Position';
import { useNodeOperations } from './useNodeOperations';
const logger = createLogger('useDropHandler');
interface DraggedVariableData {
variableName: string;
}
interface UseDropHandlerParams {
canvasRef: RefObject<HTMLDivElement>;
canvasOffset: { x: number; y: number };
canvasScale: number;
nodeOperations: ReturnType<typeof useNodeOperations>;
onNodeCreate?: (template: NodeTemplate, position: { x: number; y: number }) => void;
}
export function useDropHandler(params: UseDropHandlerParams) {
const {
canvasRef,
canvasOffset,
canvasScale,
nodeOperations,
onNodeCreate
} = params;
const [isDragging, setIsDragging] = useState(false);
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
try {
const rect = canvasRef.current?.getBoundingClientRect();
if (!rect) return;
const position = {
x: (e.clientX - rect.left - canvasOffset.x) / canvasScale,
y: (e.clientY - rect.top - canvasOffset.y) / canvasScale
};
const blackboardVariableData = e.dataTransfer.getData('application/blackboard-variable');
if (blackboardVariableData) {
const variableData = JSON.parse(blackboardVariableData) as DraggedVariableData;
const variableTemplate: NodeTemplate = {
type: NodeType.Action,
displayName: variableData.variableName,
category: 'Blackboard Variable',
icon: 'Database',
description: `Blackboard variable: ${variableData.variableName}`,
color: '#9c27b0',
defaultConfig: {
nodeType: 'blackboard-variable',
variableName: variableData.variableName
},
properties: [
{
name: 'variableName',
label: '变量名',
type: 'variable',
defaultValue: variableData.variableName,
description: '黑板变量的名称',
required: true
}
]
};
nodeOperations.createNode(
variableTemplate,
new Position(position.x, position.y),
{
nodeType: 'blackboard-variable',
variableName: variableData.variableName
}
);
return;
}
let templateData = e.dataTransfer.getData('application/behavior-tree-node');
if (!templateData) {
templateData = e.dataTransfer.getData('text/plain');
}
if (!templateData) {
return;
}
const template = JSON.parse(templateData) as NodeTemplate;
nodeOperations.createNode(
template,
new Position(position.x, position.y),
template.defaultConfig
);
onNodeCreate?.(template, position);
} catch (error) {
logger.error('Failed to create node:', error);
}
};
const handleDragOver = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
e.dataTransfer.dropEffect = 'copy';
if (!isDragging) {
setIsDragging(true);
}
};
const handleDragLeave = (e: React.DragEvent) => {
if (e.currentTarget === e.target) {
setIsDragging(false);
}
};
const handleDragEnter = (e: React.DragEvent) => {
e.preventDefault();
};
return {
isDragging,
handleDrop,
handleDragOver,
handleDragLeave,
handleDragEnter
};
}