mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 04:29:20 +08:00
fix: ColPage 回写侧栏宽度与收起状态以恢复跨页布局记忆
This commit is contained in:
@@ -56,11 +56,23 @@ export const useColSidebarLayoutStore = defineStore(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setWidth(kind: ColSidebarKind, width: number) {
|
function setWidth(kind: ColSidebarKind, width: number) {
|
||||||
layoutOf(kind).width = clampWidth(width);
|
const next = clampWidth(width);
|
||||||
|
const prev = layoutOf(kind);
|
||||||
|
if (prev.width === next) return;
|
||||||
|
// 整对象替换,确保 pinia persist 能稳定序列化
|
||||||
|
layouts.value = {
|
||||||
|
...layouts.value,
|
||||||
|
[kind]: { ...prev, width: next },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCollapsed(kind: ColSidebarKind, collapsed: boolean) {
|
function setCollapsed(kind: ColSidebarKind, collapsed: boolean) {
|
||||||
layoutOf(kind).collapsed = collapsed;
|
const prev = layoutOf(kind);
|
||||||
|
if (prev.collapsed === collapsed) return;
|
||||||
|
layouts.value = {
|
||||||
|
...layouts.value,
|
||||||
|
[kind]: { ...prev, collapsed },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function $reset() {
|
function $reset() {
|
||||||
|
|||||||
@@ -51,11 +51,18 @@ const delegatedSlots = computed(() => {
|
|||||||
return resultSlots;
|
return resultSlots;
|
||||||
});
|
});
|
||||||
|
|
||||||
const leftPanelRef = ref<{
|
type LeftPanelExpose = {
|
||||||
collapse: () => void;
|
collapse: () => void;
|
||||||
expand: () => void;
|
expand: () => void;
|
||||||
|
getSize?: () => number;
|
||||||
|
resize?: (size: number) => void;
|
||||||
isCollapsed?: boolean | { value: boolean };
|
isCollapsed?: boolean | { value: boolean };
|
||||||
} | null>(null);
|
};
|
||||||
|
|
||||||
|
const leftPanelRef = ref<LeftPanelExpose | null>(null);
|
||||||
|
|
||||||
|
/** 拖拽写回时跳过,避免 resize↔emit 回环 */
|
||||||
|
let syncingFromProp = false;
|
||||||
|
|
||||||
function isLeftPanelCollapsed() {
|
function isLeftPanelCollapsed() {
|
||||||
const flag = leftPanelRef.value?.isCollapsed;
|
const flag = leftPanelRef.value?.isCollapsed;
|
||||||
@@ -74,15 +81,26 @@ function collapseLeft() {
|
|||||||
leftPanelRef.value?.collapse();
|
leftPanelRef.value?.collapse();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 收起态的 size 不要写回宽度记忆 */
|
function isCollapsedSize(size: number) {
|
||||||
function onLeftResize(size: number) {
|
|
||||||
const collapsedSize = props.leftCollapsedWidth ?? 0;
|
const collapsedSize = props.leftCollapsedWidth ?? 0;
|
||||||
if (props.leftCollapsible && size <= collapsedSize + 0.5) {
|
return props.leftCollapsible && size <= collapsedSize + 0.5;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
/** 把展开宽度写回父级(忽略收起态的小宽度) */
|
||||||
|
function commitLeftWidth(size: number) {
|
||||||
|
if (syncingFromProp) return;
|
||||||
|
if (!Number.isFinite(size) || isCollapsedSize(size)) return;
|
||||||
|
if (Math.abs(size - props.leftWidth) < 0.05) return;
|
||||||
emit('update:leftWidth', size);
|
emit('update:leftWidth', size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Group layout 比单 Panel resize 更稳(拖拽过程/结束都会来) */
|
||||||
|
function onLayout(sizes: number[]) {
|
||||||
|
const left = sizes[0];
|
||||||
|
if (left == null) return;
|
||||||
|
commitLeftWidth(left);
|
||||||
|
}
|
||||||
|
|
||||||
function onLeftCollapse() {
|
function onLeftCollapse() {
|
||||||
emit('update:leftCollapsed', true);
|
emit('update:leftCollapsed', true);
|
||||||
}
|
}
|
||||||
@@ -101,10 +119,33 @@ function syncCollapsedFromProp(collapsed: boolean) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
/** 按持久化宽度强制套到 Panel(default-size 仅注册时生效一次) */
|
||||||
if (props.leftCollapsed) {
|
function applyWidthFromProp() {
|
||||||
void nextTick(() => syncCollapsedFromProp(true));
|
const panel = leftPanelRef.value;
|
||||||
|
if (!panel?.resize) return;
|
||||||
|
if (props.leftCollapsed || isLeftPanelCollapsed()) return;
|
||||||
|
const current = panel.getSize?.();
|
||||||
|
if (typeof current === 'number' && Math.abs(current - props.leftWidth) < 0.05) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
syncingFromProp = true;
|
||||||
|
try {
|
||||||
|
panel.resize(props.leftWidth);
|
||||||
|
} finally {
|
||||||
|
void nextTick(() => {
|
||||||
|
syncingFromProp = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
void nextTick(() => {
|
||||||
|
if (props.leftCollapsed) {
|
||||||
|
syncCollapsedFromProp(true);
|
||||||
|
} else {
|
||||||
|
applyWidthFromProp();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
@@ -114,6 +155,14 @@ watch(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/** 其它页面改了 store 宽度时,keep-alive 缓存页也跟着套 */
|
||||||
|
watch(
|
||||||
|
() => props.leftWidth,
|
||||||
|
() => {
|
||||||
|
void nextTick(applyWidthFromProp);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
expandLeft,
|
expandLeft,
|
||||||
collapseLeft,
|
collapseLeft,
|
||||||
@@ -130,7 +179,11 @@ defineExpose({
|
|||||||
<slot :name="slotName" v-bind="slotProps"></slot>
|
<slot :name="slotName" v-bind="slotProps"></slot>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<ResizablePanelGroup class="w-full" direction="horizontal">
|
<ResizablePanelGroup
|
||||||
|
class="w-full"
|
||||||
|
direction="horizontal"
|
||||||
|
@layout="onLayout"
|
||||||
|
>
|
||||||
<ResizablePanel
|
<ResizablePanel
|
||||||
ref="leftPanelRef"
|
ref="leftPanelRef"
|
||||||
:collapsed-size="leftCollapsedWidth"
|
:collapsed-size="leftCollapsedWidth"
|
||||||
@@ -140,7 +193,6 @@ defineExpose({
|
|||||||
:min-size="leftMinWidth"
|
:min-size="leftMinWidth"
|
||||||
@collapse="onLeftCollapse"
|
@collapse="onLeftCollapse"
|
||||||
@expand="onLeftExpand"
|
@expand="onLeftExpand"
|
||||||
@resize="onLeftResize"
|
|
||||||
>
|
>
|
||||||
<template #default="slotProps">
|
<template #default="slotProps">
|
||||||
<slot
|
<slot
|
||||||
|
|||||||
Reference in New Issue
Block a user