Files
vue-ts-example/src/components/h-cesium-viewer/useHCesiumViewerCls.站点.ts

92 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// src/utils/useHCesiumViewerClsGroundStation.ts
import type { MaybeRefOrGetter } from 'vue';
import type { HCesiumManager } from './managers/HCesiumManager'; // 导入新的 Viewer Manager
import type { I站点 } from './managers/HCesiumManager.types'; // 类型定义保持不变
import { GroundStationManager } from './managers/HCesiumManager.站点'; // 导入 GroundStation Manager
/**
* 管理 Cesium Viewer 中的地面站实体,根据选中的 ID 列表进行同步。
* @param hCesiumViewerManager - HCesiumViewerManager 实例或其 getter。
* @param groundStationList - 包含所有可用地面站选项的数组或 getter。
* @param selectedStationIds - 包含当前选中地面站 ID 的 Set 或 getter。
*/
export function useHCesiumViewerClsGroundStation(
hCesiumViewerManager: MaybeRefOrGetter<HCesiumManager | null>, // 更新参数类型和名称
groundStationList: MaybeRefOrGetter<Array<I站点> | undefined>,
selectedStationIds: MaybeRefOrGetter<Set<string> | undefined>,
) {
// GroundStationManager 实例引用
const groundStationManager = ref<GroundStationManager | null>(null);
// 创建一个从 ID 到站点选项的映射,方便查找
const stationMap = computed(() => {
const map = new Map<string, I站点>();
const list = toValue(groundStationList) ?? [];
for (const station of list) {
map.set(station.id, station);
}
return map;
});
// 使用 watch 显式监听依赖项
watch(
[hCesiumViewerManager, groundStationList, selectedStationIds], // 监听这些源的变化
() => {
// 回调函数
const viewerManagerInstance = toValue(hCesiumViewerManager);
// 检查 Viewer Manager 和内部 Viewer 实例是否存在
if (!viewerManagerInstance || !viewerManagerInstance.getViewer()) {
// 如果 viewer manager 或 viewer 实例尚未初始化或已销毁,则清理旧 manager 并返回
groundStationManager.value?.destroy();
groundStationManager.value = null;
return;
}
// 确保 GroundStationManager 实例存在且与当前的 ViewerManager 关联
if (!groundStationManager.value) {
groundStationManager.value = new GroundStationManager(viewerManagerInstance);
}
const manager = groundStationManager.value; // 使用 manager 实例
const selectedIdsSet = toValue(selectedStationIds) ?? new Set<string>(); // 直接获取 Set如果为 undefined 则创建空 Set
const currentEntityIds = new Set(manager.getCurrentStationEntities().keys()); // 从 manager 获取当前实体 ID
// 1. 移除不再选中的站点
for (const entityId of currentEntityIds) {
if (!selectedIdsSet.has(entityId)) {
manager.removeStation(entityId); // 使用 manager 的移除方法
}
}
// 2. 添加新增的选中站点
for (const selectedId of selectedIdsSet) {
if (!currentEntityIds.has(selectedId)) {
const stationToAdd = stationMap.value.get(selectedId);
if (stationToAdd) {
manager.addOrUpdateStation(stationToAdd); // 使用 manager 的添加/更新方法
} else {
// 如果在 groundStationList 中找不到对应的站点信息,发出警告
console.warn(`无法找到 ID 为 "${selectedId}" 的站点信息,无法添加到地图。`);
}
}
}
},
{ immediate: true, deep: false }, // 立即执行一次,非深度监听
); // watch 结束
// 组件卸载时确保最终清理
onBeforeUnmount(() => {
groundStationManager.value?.destroy();
groundStationManager.value = null; // 明确置空
});
// 返回 stationMap 可能在某些场景下有用,例如调试或扩展
return {
stationMap,
};
}