点击修改z-index

This commit is contained in:
zqm
2026-01-05 15:40:40 +08:00
parent 6c30ab376b
commit 3a3fe14153
4 changed files with 107 additions and 24 deletions

View File

@@ -139,6 +139,11 @@ const props = defineProps({
children: { children: {
type: [Array, Object], type: [Array, Object],
default: () => [] default: () => []
},
// 外部样式
style: {
type: Object,
default: () => ({})
} }
}) })
@@ -259,11 +264,32 @@ watch(() => props.windowState, (newState) => {
} }
}, { immediate: true }) }, { immediate: true })
// 用于跟踪当前z-index的响应式ref确保点击时能触发style更新
const currentZIndex = ref(zIndexManager.getFloatingAreaZIndex(props.id));
// 监听props.id变化重新初始化z-index
watch(() => props.id, (newId) => {
currentZIndex.value = zIndexManager.getFloatingAreaZIndex(newId);
}, { immediate: true });
// 监听Z_INDEX_UPDATE事件当其他Area的z-index变化时更新当前Area的z-index
// 因为zIndexManager重新排序了所有Area的z-index所以当前Area的z-index也可能变化
const unsubscribeZIndexUpdate = onEvent(EVENT_TYPES.Z_INDEX_UPDATE, () => {
currentZIndex.value = zIndexManager.getFloatingAreaZIndex(props.id);
});
// 组件卸载时取消订阅
onUnmounted(() => {
unsubscribeZIndexUpdate();
});
// 根据状态计算尺寸和位置样式 // 根据状态计算尺寸和位置样式
const areaStyle = computed(() => { const areaStyle = computed(() => {
let internalStyle;
if (isMaximized.value) { if (isMaximized.value) {
// 最大化时填充满父容器使用更高的z-index确保在最顶层 // 最大化时填充满父容器使用更高的z-index确保在最顶层
return { internalStyle = {
width: '100%', width: '100%',
height: '100%', height: '100%',
position: 'absolute', position: 'absolute',
@@ -273,18 +299,18 @@ const areaStyle = computed(() => {
margin: 0, margin: 0,
padding: 0 padding: 0
} }
} } else {
// 非最大化状态优先使用originalPosition的值实时响应拖拽变化 // 非最大化状态优先使用originalPosition的值实时响应拖拽变化
const style = { internalStyle = {
width: `${originalPosition.value.width}px`, width: `${originalPosition.value.width}px`,
height: `${originalPosition.value.height}px`, height: `${originalPosition.value.height}px`,
left: `${originalPosition.value.left}px`, left: `${originalPosition.value.left}px`,
top: `${originalPosition.value.top}px`, top: `${originalPosition.value.top}px`,
zIndex: zIndexManager.getFloatingAreaZIndex(props.id) // 使用动态的z-index zIndex: currentZIndex.value // 使用响应式的z-index
}
} }
return style return internalStyle;
}) })
// 使用事件总线替代直接emit // 使用事件总线替代直接emit
@@ -704,12 +730,21 @@ const onAreaClick = () => {
zIndexManager.activateFloatingArea(props.id) zIndexManager.activateFloatingArea(props.id)
const newZIndex = zIndexManager.getFloatingAreaZIndex(props.id); const newZIndex = zIndexManager.getFloatingAreaZIndex(props.id);
console.log(`[Area:${props.id}] New z-index: ${newZIndex}`); console.log(`[Area:${props.id}] New z-index: ${newZIndex}`);
// 更新响应式z-index触发areaStyle重新计算
currentZIndex.value = newZIndex;
emitEvent(EVENT_TYPES.Z_INDEX_UPDATE, { emitEvent(EVENT_TYPES.Z_INDEX_UPDATE, {
areaId: props.id, areaId: props.id,
zIndex: newZIndex zIndex: newZIndex
}, { }, {
source: { component: 'Area', areaId: props.id } source: { component: 'Area', areaId: props.id }
}) })
// 直接更新DOM元素的z-index确保界面上能看到变化
if (areaRef.value) {
areaRef.value.style.zIndex = newZIndex;
}
} }
// 组件挂载后获取父容器引用并初始化位置 // 组件挂载后获取父容器引用并初始化位置

View File

@@ -40,7 +40,6 @@
:key="area.id" :key="area.id"
:type="'Area'" :type="'Area'"
:config="area" :config="area"
:style="{ zIndex: zIndexManager.getFloatingAreaZIndex(area.id) }"
/> />
</div> </div>
</template> </template>
@@ -700,14 +699,17 @@ const addFloatingPanel = (panel) => {
} }
}; };
// 生成唯一的areaId
const areaId = `area-${Date.now()}`;
const newArea = { const newArea = {
id: `area-${Date.now()}`, id: areaId,
type: 'floating', // 添加浮动类型标识 type: 'floating', // 添加浮动类型标识
left: 100 + Math.random() * 200, left: 100 + Math.random() * 200,
top: 100 + Math.random() * 200, top: 100 + Math.random() * 200,
width: 300, width: 300,
height: 200, height: 200,
zIndex: zIndexManager.getFloatingAreaZIndex(`area-${Date.now()}`), zIndex: zIndexManager.getFloatingAreaZIndex(areaId),
// 使用children结构以兼容Render组件的渲染逻辑 // 使用children结构以兼容Render组件的渲染逻辑
children: { children: {
type: 'TabPage', type: 'TabPage',

View File

@@ -3,6 +3,7 @@
<component <component
:is="componentType" :is="componentType"
v-bind="componentProps" v-bind="componentProps"
:style="combinedStyle"
> >
<!-- 统一处理children属性不区分组件类型只要有children就渲染 --> <!-- 统一处理children属性不区分组件类型只要有children就渲染 -->
<template v-if="config.children"> <template v-if="config.children">
@@ -12,6 +13,7 @@
:type="child.type" :type="child.type"
:config="child" :config="child"
:debug="debug" :debug="debug"
:style="child.style"
/> />
</div> </div>
</template> </template>
@@ -42,6 +44,11 @@ const props = defineProps({
debug: { debug: {
type: Boolean, type: Boolean,
default: false default: false
},
// 外部样式
style: {
type: Object,
default: () => ({})
} }
}) })
@@ -105,6 +112,14 @@ const componentProps = computed(() => {
} }
}) })
// 合并内部样式和外部样式
const combinedStyle = computed(() => {
return {
...componentProps.value.style,
...props.style
}
})
// 暴露组件实例方法 // 暴露组件实例方法

View File

@@ -76,15 +76,26 @@ class ZIndexManager {
*/ */
activateFloatingArea(areaId) { activateFloatingArea(areaId) {
console.log(`[zIndexManager] activateFloatingArea called for areaId: ${areaId}`); console.log(`[zIndexManager] activateFloatingArea called for areaId: ${areaId}`);
// 获取当前最大z-index // 获取当前所有浮动区域
const maxZIndex = Math.max(...Array.from(this.floatingAreas.values()), Z_INDEX_LAYERS.FLOATING_AREA); const areas = Array.from(this.floatingAreas.entries());
const newZIndex = maxZIndex + 1;
this.floatingAreas.set(areaId, newZIndex);
console.log(`[zIndexManager] Set z-index for ${areaId} to ${newZIndex}`);
// 触发重排以确保层级生效 // 将当前激活的区域移到最后
this.reorderFloatingAreas(); const activeArea = areas.find(a => a[0] === areaId);
console.log(`[zIndexManager] Floating areas after activation:`, this.getStatus()); if (activeArea) {
// 从数组中移除激活区域
const updatedAreas = areas.filter(a => a[0] !== areaId);
// 将激活区域添加到数组末尾
updatedAreas.push(activeArea);
// 重新分配z-index确保激活区域在最顶层
updatedAreas.forEach(([id], index) => {
const zIndex = Z_INDEX_LAYERS.FLOATING_AREA + index + 1;
this.floatingAreas.set(id, zIndex);
console.log(`[zIndexManager] Reordered ${id} to z-index: ${zIndex}`);
});
}
console.log(`[zIndexManager] activateFloatingArea completed, current state:`, this.getStatus());
} }
/** /**
@@ -92,14 +103,34 @@ class ZIndexManager {
*/ */
reorderFloatingAreas() { reorderFloatingAreas() {
console.log(`[zIndexManager] reorderFloatingAreas called`); console.log(`[zIndexManager] reorderFloatingAreas called`);
const areas = Array.from(this.floatingAreas.entries());
areas.sort((a, b) => a[1] - b[1]); // 按z-index排序
// 获取当前所有浮动区域
const areas = Array.from(this.floatingAreas.entries());
// 检查是否需要重排z-index是否连续
let needsReorder = false;
areas.sort((a, b) => a[1] - b[1]);
for (let i = 0; i < areas.length; i++) {
const expectedZIndex = Z_INDEX_LAYERS.FLOATING_AREA + i + 1;
if (areas[i][1] !== expectedZIndex) {
needsReorder = true;
break;
}
}
if (!needsReorder) {
console.log(`[zIndexManager] z-index already continuous, skipping reorder`);
return;
}
// 只有在需要时才重排
areas.forEach(([areaId], index) => { areas.forEach(([areaId], index) => {
const newZIndex = Z_INDEX_LAYERS.FLOATING_AREA + index + 1; const newZIndex = Z_INDEX_LAYERS.FLOATING_AREA + index + 1;
this.floatingAreas.set(areaId, newZIndex); this.floatingAreas.set(areaId, newZIndex);
console.log(`[zIndexManager] Reordered ${areaId} to z-index: ${newZIndex}`); console.log(`[zIndexManager] Reordered ${areaId} to z-index: ${newZIndex}`);
}); });
console.log(`[zIndexManager] reorderFloatingAreas completed, current state:`, this.getStatus()); console.log(`[zIndexManager] reorderFloatingAreas completed, current state:`, this.getStatus());
} }