Blame view

Yi.Vben5.Vue3/packages/@core/composables/src/use-layout-style.ts 2.43 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  import type { VisibleDomRect } from '@vben-core/shared/utils';
  import type { CSSProperties } from 'vue';
  
  import {
    CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT,
    CSS_VARIABLE_LAYOUT_CONTENT_WIDTH,
    CSS_VARIABLE_LAYOUT_FOOTER_HEIGHT,
    CSS_VARIABLE_LAYOUT_HEADER_HEIGHT,
  } from '@vben-core/shared/constants';
  import { getElementVisibleRect } from '@vben-core/shared/utils';
  import { useCssVar, useDebounceFn } from '@vueuse/core';
  import { computed, onMounted, onUnmounted, ref } from 'vue';
  
  /**
   * @zh_CN content style
   */
  export function useLayoutContentStyle() {
    let resizeObserver: null | ResizeObserver = null;
    const contentElement = ref<HTMLDivElement | null>(null);
    const visibleDomRect = ref<null | VisibleDomRect>(null);
    const contentHeight = useCssVar(CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT);
    const contentWidth = useCssVar(CSS_VARIABLE_LAYOUT_CONTENT_WIDTH);
  
    const overlayStyle = computed((): CSSProperties => {
      const { height, left, top, width } = visibleDomRect.value ?? {};
      return {
        height: `${height}px`,
        left: `${left}px`,
        position: 'fixed',
        top: `${top}px`,
        width: `${width}px`,
        zIndex: 150,
      };
    });
  
    const debouncedCalcHeight = useDebounceFn(
      (_entries: ResizeObserverEntry[]) => {
        visibleDomRect.value = getElementVisibleRect(contentElement.value);
        contentHeight.value = `${visibleDomRect.value.height}px`;
        contentWidth.value = `${visibleDomRect.value.width}px`;
      },
      16,
    );
  
    onMounted(() => {
      if (contentElement.value && !resizeObserver) {
        resizeObserver = new ResizeObserver(debouncedCalcHeight);
        resizeObserver.observe(contentElement.value);
      }
    });
  
    onUnmounted(() => {
      resizeObserver?.disconnect();
      resizeObserver = null;
    });
  
    return { contentElement, overlayStyle, visibleDomRect };
  }
  
  export function useLayoutHeaderStyle() {
    const headerHeight = useCssVar(CSS_VARIABLE_LAYOUT_HEADER_HEIGHT);
  
    return {
      getLayoutHeaderHeight: () => {
        return Number.parseInt(`${headerHeight.value}`, 10);
      },
      setLayoutHeaderHeight: (height: number) => {
        headerHeight.value = `${height}px`;
      },
    };
  }
  
  export function useLayoutFooterStyle() {
    const footerHeight = useCssVar(CSS_VARIABLE_LAYOUT_FOOTER_HEIGHT);
  
    return {
      getLayoutFooterHeight: () => {
        return Number.parseInt(`${footerHeight.value}`, 10);
      },
      setLayoutFooterHeight: (height: number) => {
        footerHeight.value = `${height}px`;
      },
    };
  }