web-map/src/pages/scene-editor.vue

338 lines
10 KiB
Vue
Raw Normal View History

2025-04-27 00:05:18 +08:00
<script setup lang="ts">
2025-06-16 00:24:11 +08:00
import { message, Modal } from 'ant-design-vue';
import { computed, onMounted, provide, ref, type ShallowRef, shallowRef, watch } from 'vue';
2025-05-17 22:53:30 +08:00
import { useI18n } from 'vue-i18n';
import { getSceneById, importBinTaskExcel, pushSceneById, saveSceneById } from '../apis/scene';
import BatchEditToolbar from '../components/batch-edit-toolbar.vue';
import AutoCreateStorageModal from '../components/modal/auto-create-storage-modal.vue';
import { EditorService } from '../services/editor.service';
import { useViewState } from '../services/useViewState';
import { decodeTextFile, downloadFile, selectFile, textToBlob } from '../services/utils';
import { editorStore } from '../stores/editor.store';
2025-04-27 00:05:18 +08:00
const EDITOR_KEY = Symbol('editor-key');
type Props = {
id: string;
};
const props = defineProps<Props>();
2025-05-17 22:53:30 +08:00
const { t } = useI18n();
2025-05-05 01:06:09 +08:00
//#region 接口
const readScene = async () => {
const res = await getSceneById(props.id);
title.value = res?.label ?? '';
2025-05-06 23:48:21 +08:00
editor.value?.load(res?.json, editable.value);
2025-05-05 01:06:09 +08:00
};
2025-05-17 22:53:30 +08:00
const saveScene = async () => {
const json = editor.value?.save();
if (!json) return Promise.reject('无法获取场景数据');
const res = await saveSceneById(props.id, json);
if (!res) return Promise.reject('保存失败');
// 保存成功后重置历史记录状态,表示当前场景已保存
if (editor.value?.store) {
editor.value.store.historyIndex = undefined;
editor.value.store.histories = [];
}
message.success(t('场景保存成功'));
return Promise.resolve();
};
2025-05-17 22:53:30 +08:00
const pushScene = async () => {
2025-06-16 00:24:11 +08:00
const res = await pushSceneById(props.id);
if (!res) return Promise.reject();
message.success(t('场景推送成功'));
return Promise.resolve();
2025-05-17 22:53:30 +08:00
};
2025-05-05 01:06:09 +08:00
//#endregion
2025-04-30 00:17:09 +08:00
const title = ref<string>('');
// 监听标题变化,动态更新页面标题
onMounted(() => {
document.title = '场景编辑器';
});
2025-05-05 01:06:09 +08:00
watch(
() => props.id,
async () => {
await readScene();
// 在场景加载完成后自动保存和恢复视图状态
await handleAutoSaveAndRestoreViewState();
},
2025-05-05 01:06:09 +08:00
{ immediate: true, flush: 'post' },
);
2025-04-30 00:17:09 +08:00
2025-04-27 00:05:18 +08:00
const container = shallowRef<HTMLDivElement>();
const editor = shallowRef<EditorService>();
provide(EDITOR_KEY, editor);
// 自动生成库位对话框相关状态
const autoCreateStorageVisible = ref(false);
const autoCreateStorageData = ref<{
areaName: string;
actionPoints: any[];
}>({
areaName: '',
actionPoints: [],
});
2025-04-27 00:05:18 +08:00
onMounted(() => {
editor.value = new EditorService(container.value!);
// 将 editor 存储到 store 中
editorStore.setEditor(editor as ShallowRef<EditorService>);
// 监听自动生成库位对话框事件
editor.value?.on('autoCreateStorageDialog', (data: any) => {
autoCreateStorageData.value = data;
autoCreateStorageVisible.value = true;
});
2025-04-27 00:05:18 +08:00
});
2025-04-30 00:17:09 +08:00
const editable = ref<boolean>(false);
2025-05-05 01:06:09 +08:00
watch(editable, (v) => editor.value?.setState(v));
const toPush = () => {
2025-05-17 22:53:30 +08:00
Modal.confirm({
class: 'confirm',
title: t('是否保存并推送该场景文件?'),
content: t('推送前会先保存当前场景的所有修改。'),
2025-05-17 22:53:30 +08:00
centered: true,
cancelText: t('取消'),
okText: t('保存并推送'),
onOk: async () => {
try {
await saveScene();
await pushScene();
} catch (error) {
console.error('保存或推送失败:', error);
}
},
2025-05-17 22:53:30 +08:00
});
};
2025-05-17 22:53:30 +08:00
2025-05-07 20:12:15 +08:00
const importScene = async () => {
const file = await selectFile('.scene');
if (!file || !file.size) return;
2025-05-07 20:12:15 +08:00
const json = await decodeTextFile(file);
editor.value?.load(json, editable.value, undefined, true); // 第四个参数isImport=true
2025-05-07 20:12:15 +08:00
};
const exportScene = () => {
const json = editor.value?.save();
if (!json) return;
const blob = textToBlob(json);
if (!blob?.size) return;
const url = URL.createObjectURL(blob);
downloadFile(url, `${title.value || 'unknown'}.scene`);
URL.revokeObjectURL(url);
};
// 导入Bintask Excel文件
const importBinTask = async () => {
try {
const file = await selectFile('.xlsx,.xls');
if (!file || !file.size) return;
const success = await importBinTaskExcel(props.id, file);
if (success) {
message.success('Bintask导入成功');
} else {
message.error('Bintask导入失败');
}
} catch (error: any) {
message.error(error.message || 'Bintask导入失败');
}
};
2025-05-05 23:21:31 +08:00
const show = ref<boolean>(true);
const current = ref<{ type: 'robot' | 'point' | 'line' | 'area'; id: string }>();
watch(
() => editor.value?.selected.value[0],
(v) => {
2025-05-06 23:48:21 +08:00
const pen = editor.value?.getPenById(v);
if (pen?.id) {
current.value = { type: <'point' | 'line' | 'area'>pen.name, id: pen.id };
return;
2025-05-05 23:21:31 +08:00
}
if (current.value?.type === 'robot') return;
current.value = undefined;
},
);
const isRobot = computed(() => current.value?.type === 'robot');
const isPoint = computed(() => current.value?.type === 'point');
const isRoute = computed(() => current.value?.type === 'line');
const isArea = computed(() => current.value?.type === 'area');
const selectRobot = (id: string) => {
current.value = { type: 'robot', id };
editor.value?.inactive();
};
// 视图状态管理
const { saveViewState, autoSaveAndRestoreViewState, isSaving } = useViewState();
// 保存当前视图状态
const handleSaveViewState = async () => {
if (!editor.value) return;
try {
await saveViewState(editor.value, props.id);
message.success('视图状态保存成功');
} catch {
message.error('视图状态保存失败');
}
};
// 自动保存和恢复视图状态
const handleAutoSaveAndRestoreViewState = async () => {
if (!editor.value) return;
await autoSaveAndRestoreViewState(editor.value, props.id);
};
// 返回到父级 iframe 的场景卡片
const backToCards = () => {
window.parent?.postMessage({ type: 'scene_return_to_cards' }, '*');
};
// 处理自动生成库位确认
const handleAutoCreateStorageConfirm = (actionPoints: any[]) => {
if (!editor.value) return;
editor.value.autoCreateStorageLocations(autoCreateStorageData.value.areaName, actionPoints);
message.success(`已为 ${actionPoints.length} 个动作点自动生成库位`);
};
// 处理自动生成库位取消
const handleAutoCreateStorageCancel = () => {
autoCreateStorageVisible.value = false;
};
2025-04-27 00:05:18 +08:00
</script>
<template>
<a-layout class="full">
2025-05-01 01:07:16 +08:00
<a-layout-header class="p-16" style="height: 64px">
<a-flex justify="space-between" align="center">
<a-typography-text class="title">{{ title }} --场景编辑</a-typography-text>
2025-05-01 01:07:16 +08:00
<a-space align="center">
<a-button @click="backToCards"> 返回 </a-button>
<a-button type="primary" :loading="isSaving" @click="handleSaveViewState"> 保存比例 </a-button>
2025-05-02 00:35:53 +08:00
<a-button v-if="editable" class="warning" @click="editable = false">
2025-05-03 00:28:22 +08:00
<i class="icon exit size-18 mr-8" />
2025-05-02 00:35:53 +08:00
<span>{{ $t('退出编辑器') }}</span>
</a-button>
<a-button v-else type="primary" @click="editable = true">
2025-05-03 00:28:22 +08:00
<i class="icon edit size-18 mr-8" />
2025-05-01 01:07:16 +08:00
<span>{{ $t('启用编辑器') }}</span>
</a-button>
2025-06-01 22:39:00 +08:00
<a-button @click="toPush">{{ $t('推送') }}</a-button>
2025-05-07 20:12:15 +08:00
<a-button @click="importScene">{{ $t('导入') }}</a-button>
<a-button @click="importBinTask">导入Bintask</a-button>
2025-05-07 20:12:15 +08:00
<a-button @click="exportScene">{{ $t('导出') }}</a-button>
2025-05-01 01:07:16 +08:00
</a-space>
2025-04-30 00:17:09 +08:00
</a-flex>
</a-layout-header>
2025-04-27 00:05:18 +08:00
2025-05-01 01:07:16 +08:00
<a-layout class="p-16">
<a-layout-sider :width="320">
2025-05-05 23:21:31 +08:00
<a-tabs type="card">
2025-05-01 01:07:16 +08:00
<a-tab-pane key="1" :tab="$t('机器人')">
2025-05-05 01:06:09 +08:00
<RobotGroups
v-if="editor"
:token="EDITOR_KEY"
2025-05-10 15:41:21 +08:00
:sid="id"
2025-05-05 01:06:09 +08:00
:editable="editable"
2025-05-05 23:21:31 +08:00
:current="current?.id"
@change="selectRobot"
2025-05-25 00:07:22 +08:00
show-group-edit
2025-05-05 01:06:09 +08:00
/>
2025-05-01 01:07:16 +08:00
</a-tab-pane>
<a-tab-pane key="2" :tab="$t('库位')">
<PenGroups v-if="editor" :token="EDITOR_KEY" :current="current?.id" only-storage />
</a-tab-pane>
2025-05-05 23:21:31 +08:00
<a-tab-pane key="3" :tab="$t('高级组')">
<PenGroups v-if="editor" :token="EDITOR_KEY" :current="current?.id" />
</a-tab-pane>
2025-05-01 01:07:16 +08:00
</a-tabs>
2025-04-27 00:05:18 +08:00
</a-layout-sider>
<a-layout-content>
2025-05-01 01:07:16 +08:00
<div ref="container" class="editor-container full"></div>
2025-04-27 00:05:18 +08:00
</a-layout-content>
</a-layout>
</a-layout>
2025-05-05 23:21:31 +08:00
<div v-if="editable" class="toolbar-container">
2025-05-07 20:12:15 +08:00
<EditorToolbar v-if="editor" :token="EDITOR_KEY" :id="id" />
2025-05-05 23:21:31 +08:00
</div>
<!-- 批量编辑工具栏 - 只在编辑模式下显示 -->
<BatchEditToolbar v-if="editable && editor" :token="EDITOR_KEY" />
<!-- 自动生成库位对话框 -->
<AutoCreateStorageModal
v-model:visible="autoCreateStorageVisible"
:area-name="autoCreateStorageData.areaName"
:action-points="autoCreateStorageData.actionPoints"
@confirm="handleAutoCreateStorageConfirm"
@cancel="handleAutoCreateStorageCancel"
/>
2025-05-05 23:21:31 +08:00
<template v-if="current?.id">
<a-float-button style="top: 80px; right: 16px" shape="square" @click="show = !show">
<template #icon><i class="icon detail" /></template>
</a-float-button>
<div v-if="show" class="card-container">
<RobotDetailCard v-if="isRobot" :token="EDITOR_KEY" :current="current.id" />
2025-05-06 23:48:21 +08:00
<template v-if="isPoint">
2025-05-08 00:42:08 +08:00
<PointEditCard v-if="editable" :token="EDITOR_KEY" :id="current.id" />
2025-05-06 23:48:21 +08:00
<PointDetailCard v-else :token="EDITOR_KEY" :current="current.id" />
</template>
<template v-if="isRoute">
2025-05-09 20:15:04 +08:00
<RouteEditCard v-if="editable" :token="EDITOR_KEY" :id="current.id" />
<RouteDetailCard v-else :token="EDITOR_KEY" :current="current.id" />
2025-05-06 23:48:21 +08:00
</template>
<template v-if="isArea">
2025-05-08 19:42:45 +08:00
<AreaEditCard v-if="editable" :token="EDITOR_KEY" :id="current.id" />
2025-05-06 23:48:21 +08:00
<AreaDetailCard v-else :token="EDITOR_KEY" :current="current.id" />
</template>
2025-05-05 23:21:31 +08:00
</div>
</template>
2025-04-27 00:05:18 +08:00
</template>
2025-05-01 01:07:16 +08:00
<style scoped lang="scss">
.editor-container {
background-color: transparent !important;
}
2025-05-05 23:21:31 +08:00
.toolbar-container {
position: fixed;
bottom: 40px;
left: 50%;
2025-05-07 20:12:15 +08:00
z-index: 100;
2025-05-08 19:42:45 +08:00
transform: translateX(-50%);
2025-05-05 23:21:31 +08:00
}
.card-container {
position: fixed;
top: 80px;
right: 64px;
2025-05-07 20:12:15 +08:00
z-index: 100;
2025-05-05 23:21:31 +08:00
width: 320px;
2025-05-08 19:42:45 +08:00
height: calc(100% - 96px);
overflow: visible;
overflow-y: auto;
2025-05-25 16:45:45 +08:00
pointer-events: none;
& > * {
pointer-events: all;
}
2025-05-05 23:21:31 +08:00
}
2025-05-01 01:07:16 +08:00
</style>