vben
2020-10-30 9abf1763c78ead7de21ece6d328337a6a1da5f05
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import type { PropType } from 'vue';
import type { ModalWrapperProps } from './types';
 
import {
  defineComponent,
  computed,
  ref,
  watchEffect,
  unref,
  watch,
  onMounted,
  nextTick,
  onUnmounted,
} from 'vue';
import { Spin } from 'ant-design-vue';
 
import { useWindowSizeFn } from '/@/hooks/event/useWindowSize';
// import { useTimeout } from '/@/hooks/core/useTimeout';
 
import { getSlot } from '/@/utils/helper/tsxHelper';
import { useElResize } from '/@/hooks/event/useElResize';
import { provideModal } from './provideModal';
 
export default defineComponent({
  name: 'ModalWrapper',
  props: {
    loading: {
      type: Boolean as PropType<boolean>,
      default: false,
    },
    modalHeaderHeight: {
      type: Number as PropType<number>,
      default: 50,
    },
    modalFooterHeight: {
      type: Number as PropType<number>,
      default: 70,
    },
    minHeight: {
      type: Number as PropType<number>,
      default: 200,
    },
    footerOffset: {
      type: Number as PropType<number>,
      default: 0,
    },
    visible: {
      type: Boolean as PropType<boolean>,
      default: false,
    },
    fullScreen: {
      type: Boolean as PropType<boolean>,
      default: false,
    },
  },
  emits: ['heightChange', 'getExtHeight'],
  setup(props: ModalWrapperProps, { slots, emit }) {
    const wrapperRef = ref<HTMLElement | null>(null);
    const spinRef = ref<any>(null);
    const realHeightRef = ref(0);
    // 重试次数
    // let tryCount = 0;
    let stopElResizeFn: Fn = () => {};
 
    provideModal(setModalHeight);
 
    const wrapStyle = computed(() => {
      return {
        minHeight: `${props.minHeight}px`,
        height: `${unref(realHeightRef)}px`,
        overflow: 'auto',
      };
    });
 
    watchEffect(() => {
      setModalHeight();
    });
 
    watch(
      () => props.fullScreen,
      (v) => {
        !v && setModalHeight();
      }
    );
 
    onMounted(() => {
      const { modalHeaderHeight, modalFooterHeight } = props;
      emit('getExtHeight', modalHeaderHeight + modalFooterHeight);
      listenElResize();
    });
 
    onUnmounted(() => {
      stopElResizeFn && stopElResizeFn();
    });
 
    useWindowSizeFn(setModalHeight);
 
    async function setModalHeight() {
      // 解决在弹窗关闭的时候监听还存在,导致再次打开弹窗没有高度
      // 加上这个,就必须在使用的时候传递父级的visible
      if (!props.visible) return;
      const wrapperRefDom = unref(wrapperRef);
      if (!wrapperRefDom) return;
      const bodyDom = wrapperRefDom.parentElement;
      if (!bodyDom) return;
      bodyDom.style.padding = '0';
      await nextTick();
 
      try {
        const modalDom = bodyDom.parentElement && bodyDom.parentElement.parentElement;
        if (!modalDom) {
          return;
        }
        const modalRect = getComputedStyle(modalDom).top;
        const modalTop = Number.parseInt(modalRect);
        let maxHeight =
          window.innerHeight -
          modalTop * 2 +
          (props.footerOffset! || 0) -
          props.modalFooterHeight -
          props.modalHeaderHeight;
 
        // 距离顶部过进会出现滚动条
        if (modalTop < 40) {
          maxHeight -= 26;
        }
        await nextTick();
        const spinEl = unref(spinRef);
        // if (!spinEl) {
        //   useTimeout(() => {
        //     // retry
        //     if (tryCount < 3) {
        //       setModalHeight();
        //     }
        //     tryCount++;
        //   }, 10);
        //   return;
        // }
        // tryCount = 0;
 
        const spinContainerEl = spinEl.$el.querySelector('.ant-spin-container') as HTMLElement;
        if (!spinContainerEl) return;
 
        const realHeight = spinContainerEl.scrollHeight;
 
        if (props.fullScreen) {
          realHeightRef.value =
            window.innerHeight - props.modalFooterHeight - props.modalHeaderHeight - 6;
        } else {
          realHeightRef.value = realHeight > maxHeight ? maxHeight : realHeight + 16 + 30;
        }
        emit('heightChange', unref(realHeightRef));
        nextTick(() => {
          const el = spinEl.$el;
          if (el) {
            el.style.height = `${unref(realHeightRef)}px`;
          }
        });
      } catch (error) {
        console.log(error);
      }
    }
 
    function listenElResize() {
      const wrapper = unref(wrapperRef);
      if (!wrapper) return;
      const container = wrapper.querySelector('.ant-spin-container');
      if (!container) return;
      const [start, stop] = useElResize(container, () => {
        setModalHeight();
      });
      stopElResizeFn = stop;
      start();
    }
 
    return () => {
      return (
        <div ref={wrapperRef} style={unref(wrapStyle)}>
          <Spin ref={spinRef} spinning={props.loading}>
            {() => getSlot(slots)}
          </Spin>
        </div>
      );
    };
  },
});