refacor: 重构目录架构

This commit is contained in:
2026-09-16 22:43:21 +08:00
parent b37cdb0d54
commit 2754ed1e45
2762 changed files with 0 additions and 1529 deletions
@@ -0,0 +1,210 @@
<script lang="ts" setup>
import type { ColPageProps } from './types';
import { computed, nextTick, onMounted, ref, unref, useSlots, watch } from 'vue';
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from '@vben-core/shadcn-ui';
import Page from '../page/page.vue';
defineOptions({
name: 'ColPage',
inheritAttrs: false,
});
const props = withDefaults(defineProps<ColPageProps>(), {
leftWidth: 30,
leftCollapsed: false,
rightWidth: 70,
resizable: true,
});
const emit = defineEmits<{
'update:leftWidth': [width: number];
'update:leftCollapsed': [collapsed: boolean];
}>();
const delegatedProps = computed(() => {
const {
leftWidth: _leftWidth,
leftMinWidth: _leftMinWidth,
leftMaxWidth: _leftMaxWidth,
leftCollapsedWidth: _leftCollapsedWidth,
leftCollapsible: _leftCollapsible,
leftCollapsed: _leftCollapsed,
rightWidth: _rightWidth,
rightMinWidth: _rightMinWidth,
rightMaxWidth: _rightMaxWidth,
rightCollapsedWidth: _rightCollapsedWidth,
rightCollapsible: _rightCollapsible,
resizable: _resizable,
splitLine: _splitLine,
splitHandle: _splitHandle,
...delegated
} = props;
return delegated;
});
const slots = useSlots();
const delegatedSlots = computed(() => {
const resultSlots: string[] = [];
for (const key of Object.keys(slots)) {
if (!['default', 'left'].includes(key)) {
resultSlots.push(key);
}
}
return resultSlots;
});
const leftPanelRef = ref<InstanceType<typeof ResizablePanel>>();
/** 自身操作引起的回写,避免与外部 v-model 互相触发 */
const suppressExternalSync = ref(false);
const leftDefaultSize = computed(() =>
props.leftCollapsed
? (props.leftCollapsedWidth ?? 0)
: props.leftWidth,
);
const rightDefaultSize = computed(() =>
props.leftCollapsed
? 100 - (props.leftCollapsedWidth ?? 0)
: props.rightWidth,
);
function isLeftCollapsed() {
return !!unref(leftPanelRef.value?.isCollapsed);
}
function runAsSelfApply(apply: () => void) {
suppressExternalSync.value = true;
apply();
void nextTick(() => {
suppressExternalSync.value = false;
});
}
function expandLeft() {
leftPanelRef.value?.expand();
}
function collapseLeft() {
leftPanelRef.value?.collapse();
}
function applyCollapsed(collapsed: boolean) {
const panel = leftPanelRef.value;
if (!panel) return;
if (collapsed) {
panel.collapse();
return;
}
panel.expand();
void nextTick(() => panel.resize(props.leftWidth));
}
function onLeftCollapse() {
runAsSelfApply(() => emit('update:leftCollapsed', true));
}
function onLeftExpand() {
runAsSelfApply(() => emit('update:leftCollapsed', false));
}
/** 收起过程中的尺寸不写入展开宽度 */
function onLeftResize(size: number) {
const collapsedSize = props.leftCollapsedWidth ?? 0;
if (isLeftCollapsed() || size <= collapsedSize + 0.51) return;
if (Math.abs(size - props.leftWidth) < 0.15) return;
runAsSelfApply(() => emit('update:leftWidth', size));
}
watch(
() => props.leftCollapsed,
(collapsed) => {
if (suppressExternalSync.value) return;
applyCollapsed(collapsed);
},
);
watch(
() => props.leftWidth,
(width) => {
if (suppressExternalSync.value || isLeftCollapsed()) return;
const current = leftPanelRef.value?.getSize();
if (current == null || Math.abs(current - width) < 0.3) return;
leftPanelRef.value?.resize(width);
},
);
onMounted(() => {
if (!props.leftCollapsed) return;
void nextTick(() => applyCollapsed(true));
});
defineExpose({
expandLeft,
collapseLeft,
});
</script>
<template>
<Page v-bind="delegatedProps">
<!-- 继承默认的slot -->
<template
v-for="slotName in delegatedSlots"
:key="slotName"
#[slotName]="slotProps"
>
<slot :name="slotName" v-bind="slotProps"></slot>
</template>
<ResizablePanelGroup class="w-full" direction="horizontal">
<ResizablePanel
id="col-page-left"
ref="leftPanelRef"
:collapsed-size="leftCollapsedWidth"
:collapsible="leftCollapsible"
:default-size="leftDefaultSize"
:max-size="leftMaxWidth"
:min-size="leftMinWidth"
@collapse="onLeftCollapse"
@expand="onLeftExpand"
@resize="onLeftResize"
>
<template #default="slotProps">
<slot
name="left"
v-bind="{
...slotProps,
expand: expandLeft,
collapse: collapseLeft,
}"
></slot>
</template>
</ResizablePanel>
<ResizableHandle
v-if="resizable"
:style="{ backgroundColor: splitLine ? undefined : 'transparent' }"
:with-handle="splitHandle"
/>
<ResizablePanel
id="col-page-right"
:collapsed-size="rightCollapsedWidth"
:collapsible="rightCollapsible"
:default-size="rightDefaultSize"
:max-size="rightMaxWidth"
:min-size="rightMinWidth"
>
<template #default>
<slot></slot>
</template>
</ResizablePanel>
</ResizablePanelGroup>
</Page>
</template>
@@ -0,0 +1,2 @@
export { default as ColPage } from './col-page.vue';
export * from './types';
@@ -0,0 +1,30 @@
import type { PageProps } from '../page/types';
export interface ColPageProps extends PageProps {
/**
* 左侧宽度(百分比 1-100)。配合 `update:leftWidth` 可做受控/持久化。
* @default 30
*/
leftWidth?: number;
leftMinWidth?: number;
leftMaxWidth?: number;
leftCollapsedWidth?: number;
leftCollapsible?: boolean;
/**
* 左侧是否收起。配合 `update:leftCollapsed` 可做受控/持久化。
*/
leftCollapsed?: boolean;
/**
* 右侧宽度
* @default 70
*/
rightWidth?: number;
rightMinWidth?: number;
rightCollapsedWidth?: number;
rightMaxWidth?: number;
rightCollapsible?: boolean;
resizable?: boolean;
splitLine?: boolean;
splitHandle?: boolean;
}