Unverified 提交 9de6ac11 作者: Lan 提交者: GitHub

refactor(contentHeight):…

refactor(contentHeight): 重构,将PageWrapper的useContentHeight抽象为公共hook,满足自定义扩展组件时的自动高度计算需求。 同时优化高度计算算法。 (#826)
上级 95c16a5d
...@@ -42,9 +42,7 @@ ...@@ -42,9 +42,7 @@
import { propTypes } from '/@/utils/propTypes'; import { propTypes } from '/@/utils/propTypes';
import { omit } from 'lodash-es'; import { omit } from 'lodash-es';
import { PageHeader } from 'ant-design-vue'; import { PageHeader } from 'ant-design-vue';
import { useLayoutHeight } from '/@/layouts/default/content/useContentViewHeight'; import { useContentHeight } from '/@/hooks/web/useContentHeight';
import { useContentHeight } from './useContentHeight';
import { WrapperProps } from './types';
export default defineComponent({ export default defineComponent({
name: 'PageWrapper', name: 'PageWrapper',
...@@ -64,25 +62,23 @@ ...@@ -64,25 +62,23 @@
fixedHeight: propTypes.bool, fixedHeight: propTypes.bool,
}, },
setup(props, { slots }) { setup(props, { slots }) {
const wrapperRef = ref<ElRef>(null); const wrapperRef = ref(null);
const headerRef = ref<ComponentRef>(null); const headerRef = ref(null);
const contentRef = ref<ElRef>(null); const contentRef = ref(null);
const footerRef = ref<ComponentRef>(null); const footerRef = ref(null);
const { prefixCls } = useDesign('page-wrapper'); const { prefixCls } = useDesign('page-wrapper');
const { footerHeightRef } = useLayoutHeight();
const getProps = computed(() => { const getIsContentFullHeight = computed(() => {
return props as WrapperProps; return props.contentFullHeight;
}); });
const { redoHeight, contentHeight } = useContentHeight( const { redoHeight, setCompensation, contentHeight } = useContentHeight(
getProps, getIsContentFullHeight,
wrapperRef, wrapperRef,
headerRef, [headerRef, footerRef],
contentRef, [contentRef]
footerRef,
footerHeightRef
); );
setCompensation({ useLayoutFooter: true, elements: [footerRef] });
const getClass = computed(() => { const getClass = computed(() => {
return [ return [
...@@ -125,7 +121,7 @@ ...@@ -125,7 +121,7 @@
}); });
watch( watch(
() => [getShowFooter.value, footerHeightRef.value], () => [getShowFooter.value],
() => { () => {
redoHeight(); redoHeight();
}, },
......
import { CSSProperties } from 'vue';
export interface WrapperProps {
title?: string;
dense: boolean;
ghost: boolean;
content: string;
contentStyle?: CSSProperties;
contentBackground: boolean;
contentFullHeight: boolean;
contentClass?: string;
fixedHeight: boolean;
}
import { ComputedRef, nextTick, Ref, ref, unref } from 'vue'; import { ComputedRef, nextTick, Ref, ref, unref, watch } from 'vue';
import { WrapperProps } from './types';
import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated'; import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated';
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn'; import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
import { useLayoutHeight } from '/@/layouts/default/content/useContentViewHeight';
import { getViewportOffset } from '/@/utils/domUtils'; import { getViewportOffset } from '/@/utils/domUtils';
export interface CompensationHeight {
// 使用 layout Footer 高度作为判断补偿高度的条件
useLayoutFooter: boolean;
// refs HTMLElement
elements?: Ref[];
}
/**
* 动态计算内容高度,根据锚点dom最下坐标到屏幕最下坐标,根据传入dom的高度、padding、margin等值进行动态计算
* 最终获取合适的内容高度
*
* @param flag 用于开启计算的响应式标识
* @param anchorRef 锚点组件 Ref<ElRef | ComponentRef>
* @param subtractHeightRefs 待减去高度的组件列表 Ref<ElRef | ComponentRef>
* @param substractSpaceRefs 待减去空闲空间(margins/paddings)的组件列表 Ref<ElRef | ComponentRef>
* @param offsetHeightRef 计算偏移的响应式高度,计算高度时将直接减去此值
* @returns 响应式高度
*/
export function useContentHeight( export function useContentHeight(
propsRef: ComputedRef<WrapperProps>, flag: ComputedRef<Boolean>,
wrapperRef: Ref<ElRef>, anchorRef: Ref,
headerRef?: Ref<ComponentRef>, subtractHeightRefs: Ref[],
contentRef?: Ref<ElRef>, substractSpaceRefs: Ref[],
footerRef?: Ref<ComponentRef>,
layoutFooterHeightRef: Ref<number> = ref(0),
offsetHeightRef: Ref<number> = ref(0) offsetHeightRef: Ref<number> = ref(0)
) { ) {
const contentHeight: Ref<Nullable<number>> = ref(null); const contentHeight: Ref<Nullable<number>> = ref(null);
const { footerHeightRef: layoutFooterHeightRef } = useLayoutHeight();
let compensationHeight: CompensationHeight = {
useLayoutFooter: true,
};
const setCompensation = (params: CompensationHeight) => {
compensationHeight = params;
};
const redoHeight = () => { function redoHeight() {
nextTick(() => { nextTick(() => {
calcContentHeight(); calcContentHeight();
}); });
}; }
const subtractMargin = (element: HTMLElement | null | undefined): number => { function calcSubtractSpace(element: HTMLDivElement | null | undefined): number {
let subtractHeight = 0; let subtractHeight = 0;
const ZERO_PX = '0px'; const ZERO_PX = '0px';
let marginBottom = ZERO_PX; let marginBottom = ZERO_PX;
...@@ -40,41 +64,61 @@ export function useContentHeight( ...@@ -40,41 +64,61 @@ export function useContentHeight(
subtractHeight += contentMarginTop; subtractHeight += contentMarginTop;
} }
return subtractHeight; return subtractHeight;
}; }
const calcContentHeight = async () => { function getEl(element: any): Nullable<HTMLDivElement> {
const { contentFullHeight } = unref(propsRef); if (element == null) {
if (!contentFullHeight) { return null;
}
return (element instanceof HTMLDivElement ? element : element.$el) as HTMLDivElement;
}
async function calcContentHeight() {
if (!flag.value) {
return; return;
} }
// Add a delay to get the correct height // Add a delay to get the correct height
await nextTick(); await nextTick();
const wrapperEl = unref(wrapperRef); const wrapperEl = getEl(unref(anchorRef));
if (!wrapperEl) { if (!wrapperEl) {
return; return;
} }
const { bottomIncludeBody } = getViewportOffset(wrapperEl); const { bottomIncludeBody } = getViewportOffset(wrapperEl);
const headerHeight = unref(headerRef)?.$el.offsetHeight ?? 0;
const footerHeight = unref(footerRef)?.$el.offsetHeight ?? 0;
// content's subtract // substract elements height
const substractHeight = subtractMargin(unref(contentRef)); let substractHeight = 0;
subtractHeightRefs.forEach((item) => {
substractHeight += getEl(unref(item))?.offsetHeight ?? 0;
});
// subtract margins / paddings
let substractSpaceHeight = 0;
substractSpaceRefs.forEach((item) => {
substractSpaceHeight += calcSubtractSpace(getEl(unref(item)));
});
let height = let height =
bottomIncludeBody - bottomIncludeBody -
unref(layoutFooterHeightRef) - unref(layoutFooterHeightRef) -
unref(offsetHeightRef) - unref(offsetHeightRef) -
headerHeight - substractHeight -
footerHeight - substractSpaceHeight;
substractHeight;
// fix: compensation height both layout's footer and page's footer was shown // compensation height
if (unref(layoutFooterHeightRef) > 0 && footerHeight > 0) { const calcCompensationHeight = () => {
height += footerHeight; compensationHeight.elements?.forEach((item) => {
height += getEl(unref(item))?.offsetHeight ?? 0;
});
};
if (compensationHeight.useLayoutFooter && unref(layoutFooterHeightRef) > 0) {
calcCompensationHeight();
} else {
calcCompensationHeight();
} }
contentHeight.value = height; contentHeight.value = height;
}; }
onMountedOrActivated(() => { onMountedOrActivated(() => {
nextTick(() => { nextTick(() => {
...@@ -88,6 +132,16 @@ export function useContentHeight( ...@@ -88,6 +132,16 @@ export function useContentHeight(
50, 50,
{ immediate: true } { immediate: true }
); );
watch(
() => [layoutFooterHeightRef.value],
() => {
calcContentHeight();
},
{
flush: 'post',
immediate: true,
}
);
return { redoHeight, contentHeight }; return { redoHeight, setCompensation, contentHeight };
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论