Commit 9abf1763c78ead7de21ece6d328337a6a1da5f05

Authored by vben
1 parent 84c9d78f

perf(modal): optimize table embedding height calculation problem

CHANGELOG.zh_CN.md
@@ -12,6 +12,11 @@ @@ -12,6 +12,11 @@
12 12
13 - 表单代码优化重构 13 - 表单代码优化重构
14 14
  15 +### ⚡ Performance Improvements
  16 +
  17 +- Modal slot 可以覆盖
  18 +- 优化表格嵌入高度计算问题
  19 +
15 ### 🎫 Chores 20 ### 🎫 Chores
16 21
17 - 添加部分注释 22 - 添加部分注释
src/components/Modal/src/BasicModal.tsx
@@ -225,11 +225,11 @@ export default defineComponent({ @@ -225,11 +225,11 @@ export default defineComponent({
225 {...{ ...attrs, ...props, ...unref(getProps) }} 225 {...{ ...attrs, ...props, ...unref(getProps) }}
226 > 226 >
227 {{ 227 {{
228 - ...extendSlots(slots, ['default']),  
229 - default: () => renderContent(),  
230 - closeIcon: () => renderClose(),  
231 footer: () => renderFooter(), 228 footer: () => renderFooter(),
  229 + closeIcon: () => renderClose(),
232 title: () => renderTitle(), 230 title: () => renderTitle(),
  231 + ...extendSlots(slots, ['default']),
  232 + default: () => renderContent(),
233 }} 233 }}
234 </Modal> 234 </Modal>
235 ); 235 );
src/components/Modal/src/ModalWrapper.tsx
@@ -15,10 +15,12 @@ import { @@ -15,10 +15,12 @@ import {
15 import { Spin } from 'ant-design-vue'; 15 import { Spin } from 'ant-design-vue';
16 16
17 import { useWindowSizeFn } from '/@/hooks/event/useWindowSize'; 17 import { useWindowSizeFn } from '/@/hooks/event/useWindowSize';
18 -import { useTimeout } from '/@/hooks/core/useTimeout'; 18 +// import { useTimeout } from '/@/hooks/core/useTimeout';
19 19
20 import { getSlot } from '/@/utils/helper/tsxHelper'; 20 import { getSlot } from '/@/utils/helper/tsxHelper';
21 import { useElResize } from '/@/hooks/event/useElResize'; 21 import { useElResize } from '/@/hooks/event/useElResize';
  22 +import { provideModal } from './provideModal';
  23 +
22 export default defineComponent({ 24 export default defineComponent({
23 name: 'ModalWrapper', 25 name: 'ModalWrapper',
24 props: { 26 props: {
@@ -56,6 +58,11 @@ export default defineComponent({ @@ -56,6 +58,11 @@ export default defineComponent({
56 const wrapperRef = ref<HTMLElement | null>(null); 58 const wrapperRef = ref<HTMLElement | null>(null);
57 const spinRef = ref<any>(null); 59 const spinRef = ref<any>(null);
58 const realHeightRef = ref(0); 60 const realHeightRef = ref(0);
  61 + // 重试次数
  62 + // let tryCount = 0;
  63 + let stopElResizeFn: Fn = () => {};
  64 +
  65 + provideModal(setModalHeight);
59 66
60 const wrapStyle = computed(() => { 67 const wrapStyle = computed(() => {
61 return { 68 return {
@@ -65,10 +72,6 @@ export default defineComponent({ @@ -65,10 +72,6 @@ export default defineComponent({
65 }; 72 };
66 }); 73 });
67 74
68 - // 重试次数  
69 - let tryCount = 0;  
70 - let stopElResizeFn: Fn = () => {};  
71 -  
72 watchEffect(() => { 75 watchEffect(() => {
73 setModalHeight(); 76 setModalHeight();
74 }); 77 });
@@ -123,17 +126,17 @@ export default defineComponent({ @@ -123,17 +126,17 @@ export default defineComponent({
123 } 126 }
124 await nextTick(); 127 await nextTick();
125 const spinEl = unref(spinRef); 128 const spinEl = unref(spinRef);
126 - if (!spinEl) {  
127 - useTimeout(() => {  
128 - // retry  
129 - if (tryCount < 3) {  
130 - setModalHeight();  
131 - }  
132 - tryCount++;  
133 - }, 10);  
134 - return;  
135 - }  
136 - tryCount = 0; 129 + // if (!spinEl) {
  130 + // useTimeout(() => {
  131 + // // retry
  132 + // if (tryCount < 3) {
  133 + // setModalHeight();
  134 + // }
  135 + // tryCount++;
  136 + // }, 10);
  137 + // return;
  138 + // }
  139 + // tryCount = 0;
137 140
138 const spinContainerEl = spinEl.$el.querySelector('.ant-spin-container') as HTMLElement; 141 const spinContainerEl = spinEl.$el.querySelector('.ant-spin-container') as HTMLElement;
139 if (!spinContainerEl) return; 142 if (!spinContainerEl) return;
src/components/Modal/src/provideModal.ts 0 → 100644
  1 +import { provide, inject } from 'vue';
  2 +
  3 +const key = Symbol('basic-modal');
  4 +
  5 +export function provideModal(redoHeight: Fn) {
  6 + provide(key, redoHeight);
  7 +}
  8 +
  9 +export function injectModal(): Fn {
  10 + return inject(key) as Fn;
  11 +}
src/components/Table/src/hooks/useTableScroll.ts
@@ -6,47 +6,44 @@ import { isBoolean } from &#39;/@/utils/is&#39;; @@ -6,47 +6,44 @@ import { isBoolean } from &#39;/@/utils/is&#39;;
6 import { useTimeout } from '/@/hooks/core/useTimeout'; 6 import { useTimeout } from '/@/hooks/core/useTimeout';
7 import { useWindowSizeFn } from '/@/hooks/event/useWindowSize'; 7 import { useWindowSizeFn } from '/@/hooks/event/useWindowSize';
8 import { useProps } from './useProps'; 8 import { useProps } from './useProps';
  9 +import { injectModal } from '/@/components/Modal/src/provideModal';
9 10
10 export function useTableScroll(refProps: ComputedRef<BasicTableProps>, tableElRef: Ref<any>) { 11 export function useTableScroll(refProps: ComputedRef<BasicTableProps>, tableElRef: Ref<any>) {
11 const { propsRef } = useProps(refProps); 12 const { propsRef } = useProps(refProps);
12 13
13 const tableHeightRef: Ref<number | null> = ref(null); 14 const tableHeightRef: Ref<number | null> = ref(null);
14 15
  16 + const redoModalHeight = injectModal();
  17 +
15 watch( 18 watch(
16 () => unref(propsRef).canResize, 19 () => unref(propsRef).canResize,
17 () => { 20 () => {
18 redoHeight(); 21 redoHeight();
19 } 22 }
20 ); 23 );
  24 +
21 function redoHeight() { 25 function redoHeight() {
22 const { canResize } = unref(propsRef); 26 const { canResize } = unref(propsRef);
23 27
24 - if (!canResize) {  
25 - return;  
26 - } 28 + if (!canResize) return;
27 calcTableHeight(); 29 calcTableHeight();
28 } 30 }
29 31
30 async function calcTableHeight(cb?: () => void) { 32 async function calcTableHeight(cb?: () => void) {
31 const { canResize, resizeHeightOffset, pagination, maxHeight } = unref(propsRef); 33 const { canResize, resizeHeightOffset, pagination, maxHeight } = unref(propsRef);
32 - if (!canResize) {  
33 - return;  
34 - } 34 + if (!canResize) return;
  35 +
35 await nextTick(); 36 await nextTick();
36 const table = unref(tableElRef) as any; 37 const table = unref(tableElRef) as any;
  38 + if (!table) return;
37 39
38 - if (!table) {  
39 - return;  
40 - }  
41 const tableEl: Element = table.$el; 40 const tableEl: Element = table.$el;
42 - if (!tableEl) {  
43 - return;  
44 - } 41 + if (!tableEl) return;
  42 +
45 const el: HTMLElement | null = tableEl.querySelector('.ant-table-thead '); 43 const el: HTMLElement | null = tableEl.querySelector('.ant-table-thead ');
46 // const layoutMain: Element | null = document.querySelector('.default-layout__main '); 44 // const layoutMain: Element | null = document.querySelector('.default-layout__main ');
47 - if (!el) {  
48 - return;  
49 - } 45 + if (!el) return;
  46 +
50 // 表格距离底部高度 47 // 表格距离底部高度
51 const { bottomIncludeBody } = getViewportOffset(el); 48 const { bottomIncludeBody } = getViewportOffset(el);
52 // 表格高度+距离底部高度-自定义偏移量 49 // 表格高度+距离底部高度-自定义偏移量
@@ -89,6 +86,8 @@ export function useTableScroll(refProps: ComputedRef&lt;BasicTableProps&gt;, tableElRe @@ -89,6 +86,8 @@ export function useTableScroll(refProps: ComputedRef&lt;BasicTableProps&gt;, tableElRe
89 tableHeightRef.value = 86 tableHeightRef.value =
90 tableHeightRef.value! > maxHeight! ? (maxHeight as number) : tableHeightRef.value; 87 tableHeightRef.value! > maxHeight! ? (maxHeight as number) : tableHeightRef.value;
91 cb && cb(); 88 cb && cb();
  89 + // 解决表格放modal内的时候,modal自适应高度计算问题
  90 + redoModalHeight && redoModalHeight();
92 }, 0); 91 }, 0);
93 } 92 }
94 93