修复运行异常

This commit is contained in:
zqm
2025-12-29 09:05:37 +08:00
parent 09e4076635
commit 9aad6ebc21
10 changed files with 448 additions and 281 deletions

View File

@@ -168,11 +168,9 @@ const props = defineProps({
// 事件订阅管理 - 使用Set避免key冲突并添加唯一标识符
const subscriptions = new Set();
const subscriptionRegistry = new Map(); // 用于追踪订阅详细信息
const subscriptionRegistry = new Map();
// 动态获取当前面板所在的Area ID
const getCurrentAreaId = () => {
// 通过DOM向上查找最近的Area容器
const panelElement = document.querySelector(`[data-panel-id="${props.id}"]`);
if (panelElement) {
const areaElement = panelElement.closest('[data-area-id]');
@@ -181,8 +179,6 @@ const getCurrentAreaId = () => {
}
}
// 备用方法:通过父组件查找
// 向上查找vs-area容器
const parentElement = document.querySelector(`[data-panel-id="${props.id}"]`)?.parentElement;
if (parentElement) {
const areaElement = parentElement.closest('.vs-area');
@@ -399,7 +395,6 @@ const onDragStart = (e) => {
const areaId = getCurrentAreaId();
// 2. 使用事件总线触发面板拖拽开始事件,包含统一的 dragId 和标准化数据格式
emitEvent(EVENT_TYPES.PANEL_DRAG_START, {
dragId: currentDragId,
panelId: props.id,
@@ -410,27 +405,9 @@ const onDragStart = (e) => {
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 3. 同时触发Area拖拽开始事件实现通过Panel标题栏拖拽Area
emitEvent(EVENT_TYPES.AREA_DRAG_START, {
dragId: currentDragId,
areaId: areaId,
event: e,
element: null,
position: { x: e.clientX, y: e.clientY },
clientX: e.clientX,
clientY: e.clientY,
startLeft: 0,
startTop: 0,
timestamp: Date.now()
}, {
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 4. 防止文本选择和默认行为
e.preventDefault()
e.stopPropagation()
// 5. 添加Document事件监听器使用一次性变量避免内存泄漏
addDocumentDragListeners()
}
};
@@ -444,7 +421,6 @@ const onDragMove = (e) => {
const areaId = getCurrentAreaId();
// 1. 使用事件总线触发面板拖拽移动事件,包含 dragId
emitEvent(EVENT_TYPES.PANEL_DRAG_MOVE, {
dragId: currentDragId,
panelId: props.id,
@@ -454,23 +430,9 @@ const onDragMove = (e) => {
}, {
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 2. 同时触发Area拖拽移动事件实现通过Panel标题栏拖拽Area
emitEvent(EVENT_TYPES.AREA_DRAG_MOVE, {
dragId: currentDragId,
areaId: areaId,
event: e,
position: { x: e.clientX, y: e.clientY },
clientX: e.clientX,
clientY: e.clientY,
timestamp: Date.now()
}, {
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
}
};
// 拖拽结束
const onDragEnd = () => {
if (isDragging && currentDragId) {
isDragging = false;
@@ -479,7 +441,6 @@ const onDragEnd = () => {
const areaId = getCurrentAreaId();
// 1. 使用事件总线触发面板拖拽结束事件,包含 dragId
emitEvent(EVENT_TYPES.PANEL_DRAG_END, {
dragId: currentDragId,
panelId: props.id,
@@ -489,22 +450,8 @@ const onDragEnd = () => {
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 2. 同时触发Area拖拽结束事件实现通过Panel标题栏拖拽Area
emitEvent(EVENT_TYPES.AREA_DRAG_END, {
dragId: currentDragId,
areaId: areaId,
event: null,
position: null,
timestamp: Date.now()
}, {
source: { component: 'Panel', panelId: props.id, dragId: currentDragId }
})
// 3. 使用统一的清理方法,确保一致性和完整性
cleanupDragEventListeners()
// 4. 重置 dragId
currentDragId = null
currentDragId = null;
cleanupDragEventListeners();
}
};
@@ -652,7 +599,7 @@ onUnmounted(() => {
try {
// 1. 立即设置标志位,防止新的异步操作
isDragging = false
isDragging.value = false
// 2. 同步清理所有可以直接清理的资源
const cleanupResult = cleanupEventListeners()
@@ -662,24 +609,12 @@ onUnmounted(() => {
console.log(`[Panel:${props.id}] 组件清理结果:`, cleanupResult)
}
// 4. 清理超时保护 - 简化版本,防止无限等待
const cleanupTimeout = setTimeout(() => {
console.warn(`[Panel:${props.id}] 清理超时,但继续卸载`)
}, 200) // 缩短超时时间
// 5. 清理超时定时器,确保不会泄露
setTimeout(() => {
clearTimeout(cleanupTimeout)
}, 250)
} catch (error) {
console.error(`[Panel:${props.id}] 清理过程中出现异常:`, error)
// 即使出现异常,也要尝试强制清理
try {
isDragging = false
// 强制清理事件监听器
cleanupEventListeners()
isDragging.value = false
} catch (forceError) {
console.error(`[Panel:${props.id}] 强制清理也失败:`, forceError)
}