refactor: 更新上下文菜单和机器人菜单组件,改为通过机器人ID传递信息,添加editor token,优化机器人信息获取逻辑
This commit is contained in:
parent
c32dd105ef
commit
96cbdf8b51
@ -34,8 +34,9 @@
|
||||
|
||||
<!-- 机器人菜单 -->
|
||||
<RobotMenu
|
||||
v-else-if="menuType === 'robot' && robotInfo"
|
||||
:robot-info="robotInfo"
|
||||
v-else-if="menuType === 'robot' && robotId"
|
||||
:robot-id="robotId"
|
||||
:token="token"
|
||||
@action-complete="handleActionComplete"
|
||||
@custom-image="handleCustomImage"
|
||||
/>
|
||||
@ -52,10 +53,10 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { message } from 'ant-design-vue';
|
||||
import { computed, defineAsyncComponent, ref } from 'vue';
|
||||
import { computed, defineAsyncComponent, type InjectionKey, ref, type ShallowRef } from 'vue';
|
||||
|
||||
import type { RobotInfo } from '../../apis/robot';
|
||||
import type { StorageLocationInfo } from '../../services/context-menu';
|
||||
import type { EditorService } from '../../services/editor.service';
|
||||
|
||||
// 使用动态导入避免 TypeScript 错误
|
||||
const DefaultMenu = defineAsyncComponent(() => import('./default-menu.vue'));
|
||||
@ -68,14 +69,15 @@ interface Props {
|
||||
y?: number;
|
||||
menuType?: 'default' | 'storage' | 'storage-background' | 'robot' | 'point' | 'area';
|
||||
storageLocations?: StorageLocationInfo[];
|
||||
robotInfo?: RobotInfo;
|
||||
robotId?: string; // 改为传递机器人ID
|
||||
token?: InjectionKey<ShallowRef<EditorService>>; // 添加 editor token
|
||||
apiBaseUrl?: string;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'close'): void;
|
||||
(e: 'actionComplete', data: any): void;
|
||||
(e: 'customImage', data: { robotInfo: RobotInfo }): void;
|
||||
(e: 'customImage', data: { robotInfo: any }): void; // 使用 any 类型,因为 RobotInfo 不再直接导入
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
@ -190,7 +192,7 @@ const getActionDisplayName = (action: string): string => {
|
||||
};
|
||||
|
||||
// 处理自定义图片事件
|
||||
const handleCustomImage = (data: { robotInfo: RobotInfo }) => {
|
||||
const handleCustomImage = (data: { robotInfo: any }) => {
|
||||
console.log('打开自定义图片设置:', data.robotInfo);
|
||||
emit('customImage', data);
|
||||
// 不关闭菜单,只有关闭按钮才能关闭
|
||||
|
@ -126,7 +126,7 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { message } from 'ant-design-vue';
|
||||
import { ref } from 'vue';
|
||||
import { computed, inject, type InjectionKey, ref, type ShallowRef } from 'vue';
|
||||
|
||||
import type { RobotInfo } from '../../apis/robot';
|
||||
import type { RobotAction } from '../../services/context-menu/robot-menu.service';
|
||||
@ -135,10 +135,12 @@ import {
|
||||
getRobotStatusColor,
|
||||
getRobotStatusText
|
||||
} from '../../services/context-menu/robot-menu.service';
|
||||
import type { EditorService } from '../../services/editor.service';
|
||||
import RobotImageSettingsModal from '../modal/robot-image-settings-modal.vue';
|
||||
|
||||
interface Props {
|
||||
robotInfo?: RobotInfo;
|
||||
robotId?: string; // 改为传递机器人ID而不是完整的机器人信息
|
||||
token?: InjectionKey<ShallowRef<EditorService>>; // 添加 editor token
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
@ -150,6 +152,15 @@ const props = defineProps<Props>();
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 注入 editor service
|
||||
const editor = props.token ? inject(props.token) : null;
|
||||
|
||||
// 根据 robotId 获取机器人信息
|
||||
const robotInfo = computed<RobotInfo | null>(() => {
|
||||
if (!props.robotId || !editor?.value) return null;
|
||||
return editor.value.getRobotById(props.robotId) || null;
|
||||
});
|
||||
|
||||
// 图片设置模态框状态
|
||||
const imageSettingsVisible = ref(false);
|
||||
const selectedRobotName = ref('');
|
||||
@ -171,40 +182,40 @@ const getBatteryClass = (batteryLevel: number) => {
|
||||
|
||||
// 处理机器人操作
|
||||
const handleRobotAction = async (action: RobotAction | 'custom_image', actionName: string) => {
|
||||
if (!props.robotInfo) return;
|
||||
if (!robotInfo.value) return;
|
||||
|
||||
// 处理自定义图片操作
|
||||
if (action === 'custom_image') {
|
||||
if (!props.robotInfo?.label) {
|
||||
if (!robotInfo.value?.label) {
|
||||
message.error('未找到机器人信息');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('打开机器人图片设置:', props.robotInfo);
|
||||
console.log('打开机器人图片设置:', robotInfo.value);
|
||||
|
||||
// 打开模态框,不关闭右键菜单
|
||||
selectedRobotName.value = props.robotInfo.label;
|
||||
selectedRobotName.value = robotInfo.value.label;
|
||||
imageSettingsVisible.value = true;
|
||||
|
||||
// 只触发自定义图片事件,不触发操作完成事件
|
||||
emit('customImage', {
|
||||
robotInfo: props.robotInfo
|
||||
robotInfo: robotInfo.value
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`执行机器人操作: ${action} (${actionName})`, props.robotInfo);
|
||||
console.log(`执行机器人操作: ${action} (${actionName})`, robotInfo.value);
|
||||
|
||||
// 使用服务函数执行操作
|
||||
const result = await executeRobotAction(action, props.robotInfo);
|
||||
const result = await executeRobotAction(action, robotInfo.value);
|
||||
|
||||
console.log('机器人操作结果:', result);
|
||||
|
||||
// 发送操作完成事件
|
||||
emit('actionComplete', {
|
||||
action,
|
||||
robot: props.robotInfo,
|
||||
robot: robotInfo.value,
|
||||
success: result.success
|
||||
});
|
||||
} catch (error) {
|
||||
@ -212,7 +223,7 @@ const handleRobotAction = async (action: RobotAction | 'custom_image', actionNam
|
||||
|
||||
emit('actionComplete', {
|
||||
action,
|
||||
robot: props.robotInfo,
|
||||
robot: robotInfo.value,
|
||||
success: false
|
||||
});
|
||||
}
|
||||
|
@ -160,7 +160,8 @@ const toRemoveRobots = () =>
|
||||
:x="contextMenuState.x"
|
||||
:y="contextMenuState.y"
|
||||
:menu-type="contextMenuState.menuType"
|
||||
:robot-info="contextMenuState.robotInfo"
|
||||
:robot-id="contextMenuState.robotInfo?.id"
|
||||
:token="token"
|
||||
@close="handleContextMenuClose"
|
||||
/>
|
||||
|
||||
|
@ -465,8 +465,8 @@ const handleGlobalKeydown = (event: KeyboardEvent) => {
|
||||
:y="contextMenuState.y"
|
||||
:menu-type="contextMenuState.menuType"
|
||||
:storage-locations="contextMenuState.storageLocations"
|
||||
:robot-info="contextMenuState.robotInfo"
|
||||
:editor="editor"
|
||||
:robot-id="contextMenuState.robotInfo?.id"
|
||||
:token="EDITOR_KEY"
|
||||
@close="handleCloseContextMenu"
|
||||
@action-complete="handleActionComplete"
|
||||
/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user