useCustomHeight.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. export function useCustomHeight(heightObj: Record<string, number | Ref<HTMLElement | null>>) {
  2. const tableHeight = ref<number>(0);
  3. const calculateHeight = () => {
  4. let totalHeight = 0;
  5. const entries = Object.entries(heightObj);
  6. entries.forEach(([key, height], index) => {
  7. // console.log(`Processing ${key} at index ${index}:`);
  8. // 判断 height 是否为 ref,如果是则解引用,否则直接使用数值
  9. const resolvedHeight = isRef(height) ? unref(height) : height;
  10. let currentHeight = 0;
  11. if (typeof resolvedHeight === 'number') {
  12. currentHeight = resolvedHeight;
  13. } else if (resolvedHeight instanceof HTMLElement) {
  14. currentHeight = resolvedHeight.offsetHeight;
  15. }
  16. totalHeight += currentHeight;
  17. // console.log(`Key: ${key}, Current height: ${currentHeight}, Total so far: ${totalHeight}`);
  18. });
  19. tableHeight.value = window.innerHeight - totalHeight;
  20. };
  21. onMounted(() => {
  22. calculateHeight();
  23. window.addEventListener('resize', calculateHeight);
  24. });
  25. onBeforeUnmount(() => {
  26. window.removeEventListener('resize', calculateHeight);
  27. });
  28. return { tableHeight };
  29. }