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