123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import { ComputedRef, isRef, nextTick, Ref, ref, unref, watch } from 'vue';
- import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated';
- import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
- import { useLayoutHeight } from '/@/layouts/default/content/useContentViewHeight';
- import { getViewportOffset } from '/@/utils/domUtils';
- import { isNumber, isString } from '/@/utils/is';
- export interface CompensationHeight {
-
- useLayoutFooter: boolean;
-
- elements?: Ref[];
- }
- type Upward = number | string | null | undefined;
- export function useContentHeight(
- flag: ComputedRef<Boolean>,
- anchorRef: Ref,
- subtractHeightRefs: Ref[],
- substractSpaceRefs: Ref[],
- upwardSpace: Ref<Upward> | ComputedRef<Upward> | Upward = 0,
- offsetHeightRef: Ref<number> = ref(0)
- ) {
- const contentHeight: Ref<Nullable<number>> = ref(null);
- const { footerHeightRef: layoutFooterHeightRef } = useLayoutHeight();
- let compensationHeight: CompensationHeight = {
- useLayoutFooter: true,
- };
- const setCompensation = (params: CompensationHeight) => {
- compensationHeight = params;
- };
- function redoHeight() {
- nextTick(() => {
- calcContentHeight();
- });
- }
- function calcSubtractSpace(element: Element | null | undefined, direction: 'all' | 'top' | 'bottom' = 'all'): number {
- function numberPx(px: string) {
- return Number(px.replace(/[^\d]/g, ''));
- }
- let subtractHeight = 0;
- const ZERO_PX = '0px';
- if (element) {
- const cssStyle = getComputedStyle(element);
- const marginTop = numberPx(cssStyle?.marginTop ?? ZERO_PX);
- const marginBottom = numberPx(cssStyle?.marginBottom ?? ZERO_PX);
- const paddingTop = numberPx(cssStyle?.paddingTop ?? ZERO_PX);
- const paddingBottom = numberPx(cssStyle?.paddingBottom ?? ZERO_PX);
- if (direction === 'all') {
- subtractHeight += marginTop;
- subtractHeight += marginBottom;
- subtractHeight += paddingTop;
- subtractHeight += paddingBottom;
- } else if (direction === 'top') {
- subtractHeight += marginTop;
- subtractHeight += paddingTop;
- } else {
- subtractHeight += marginBottom;
- subtractHeight += paddingBottom;
- }
- }
- return subtractHeight;
- }
- function getEl(element: any): Nullable<HTMLDivElement> {
- if (element == null) {
- return null;
- }
- return (element instanceof HTMLDivElement ? element : element.$el) as HTMLDivElement;
- }
- async function calcContentHeight() {
- if (!flag.value) {
- return;
- }
-
- await nextTick();
- const anchorEl = getEl(unref(anchorRef));
- if (!anchorEl) {
- return;
- }
- const { bottomIncludeBody } = getViewportOffset(anchorEl);
-
- let substractHeight = 0;
- subtractHeightRefs.forEach((item) => {
- substractHeight += getEl(unref(item))?.offsetHeight ?? 0;
- });
-
- let substractSpaceHeight = calcSubtractSpace(anchorEl) ?? 0;
- substractSpaceRefs.forEach((item) => {
- substractSpaceHeight += calcSubtractSpace(getEl(unref(item)));
- });
-
- let upwardSpaceHeight = 0;
- function upward(element: Element | null, upwardLvlOrClass: number | string | null | undefined) {
- if (element && upwardLvlOrClass) {
- const parent = element.parentElement;
- if (parent) {
- if (isString(upwardLvlOrClass)) {
- if (!parent.classList.contains(upwardLvlOrClass)) {
- upwardSpaceHeight += calcSubtractSpace(parent, 'bottom');
- upward(parent, upwardLvlOrClass);
- } else {
- upwardSpaceHeight += calcSubtractSpace(parent, 'bottom');
- }
- } else if (isNumber(upwardLvlOrClass)) {
- if (upwardLvlOrClass > 0) {
- upwardSpaceHeight += calcSubtractSpace(parent, 'bottom');
- upward(parent, --upwardLvlOrClass);
- }
- }
- }
- }
- }
- if (isRef(upwardSpace)) {
- upward(anchorEl, unref(upwardSpace));
- } else {
- upward(anchorEl, upwardSpace);
- }
- let height =
- bottomIncludeBody - unref(layoutFooterHeightRef) - unref(offsetHeightRef) - substractHeight - substractSpaceHeight - upwardSpaceHeight;
-
- const calcCompensationHeight = () => {
- compensationHeight.elements?.forEach((item) => {
- height += getEl(unref(item))?.offsetHeight ?? 0;
- });
- };
- if (compensationHeight.useLayoutFooter && unref(layoutFooterHeightRef) > 0) {
- calcCompensationHeight();
- } else {
- calcCompensationHeight();
- }
- contentHeight.value = height;
- }
- onMountedOrActivated(() => {
- nextTick(() => {
- calcContentHeight();
- });
- });
- useWindowSizeFn(
- () => {
- calcContentHeight();
- },
- 50,
- { immediate: true }
- );
- watch(
- () => [layoutFooterHeightRef.value],
- () => {
- calcContentHeight();
- },
- {
- flush: 'post',
- immediate: true,
- }
- );
- return { redoHeight, setCompensation, contentHeight };
- }
|