96 lines
2.0 KiB
TypeScript
96 lines
2.0 KiB
TypeScript
/**
|
||
* 视角跟随状态管理 Store
|
||
* 使用 Pinia 管理视角跟随的全局状态
|
||
*/
|
||
|
||
import { defineStore } from 'pinia';
|
||
import { computed,ref } from 'vue';
|
||
|
||
export interface FollowViewState {
|
||
isFollowing: boolean;
|
||
robotId: string;
|
||
robotName: string;
|
||
timer: NodeJS.Timeout | null;
|
||
}
|
||
|
||
export const useFollowViewStore = defineStore('followView', () => {
|
||
// 状态
|
||
const state = ref<FollowViewState>({
|
||
isFollowing: false,
|
||
robotId: '',
|
||
robotName: '',
|
||
timer: null,
|
||
});
|
||
|
||
// 计算属性
|
||
const isFollowing = computed(() => state.value.isFollowing);
|
||
const robotId = computed(() => state.value.robotId);
|
||
const robotName = computed(() => state.value.robotName);
|
||
|
||
/**
|
||
* 开始视角跟随
|
||
* @param robotId 机器人ID
|
||
* @param robotName 机器人名称
|
||
* @param editor 编辑器实例
|
||
*/
|
||
const startFollow = (robotId: string, robotName: string, editor: any) => {
|
||
// 如果已经在跟随其他机器人,先停止
|
||
if (state.value.isFollowing && state.value.robotId !== robotId) {
|
||
stopFollow();
|
||
}
|
||
|
||
// 更新状态
|
||
state.value.isFollowing = true;
|
||
state.value.robotId = robotId;
|
||
state.value.robotName = robotName;
|
||
|
||
// 立即执行一次聚焦
|
||
editor.gotoById(robotId);
|
||
|
||
// 设置定时器,每10毫秒执行一次
|
||
state.value.timer = setInterval(() => {
|
||
if (state.value.isFollowing) {
|
||
editor.gotoById(robotId);
|
||
}
|
||
}, 10);
|
||
|
||
console.log('开始视角跟随:', robotId, robotName);
|
||
};
|
||
|
||
/**
|
||
* 停止视角跟随
|
||
*/
|
||
const stopFollow = () => {
|
||
state.value.isFollowing = false;
|
||
state.value.robotId = '';
|
||
state.value.robotName = '';
|
||
|
||
if (state.value.timer) {
|
||
clearInterval(state.value.timer);
|
||
state.value.timer = null;
|
||
}
|
||
|
||
console.log('停止视角跟随');
|
||
};
|
||
|
||
/**
|
||
* 重置状态
|
||
*/
|
||
const reset = () => {
|
||
stopFollow();
|
||
};
|
||
|
||
return {
|
||
// 状态
|
||
state,
|
||
// 计算属性
|
||
isFollowing,
|
||
robotId,
|
||
robotName,
|
||
// 方法
|
||
startFollow,
|
||
stopFollow,
|
||
reset,
|
||
};
|
||
});
|