-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ziqiya/master
feat: 新增大屏自适应缩放 hook
- Loading branch information
Showing
14 changed files
with
13,768 additions
and
14,541 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* @文件描述: 大屏缩放自适应屏幕 hook | ||
* @公司: thundersdata | ||
* @作者: 阮旭松 | ||
* @Date: 2023-11-07 11:00:14 | ||
* @LastEditors: 阮旭松 | ||
* @LastEditTime: 2024-12-24 17:53:33 | ||
*/ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
// 初始缩放比例 | ||
const INITIAL_SCALE = 1; | ||
|
||
export function useScreenScale(screenWidth: number, screenHeight: number) { | ||
const [scale, setScale] = useState<number>(INITIAL_SCALE); | ||
const [isReady, setIsReady] = useState(false); | ||
const screenDomRef = useRef<HTMLDivElement | null>(null); | ||
|
||
/** 对 echarts 图表进行反缩放,修复 zoom 以后 tooltip 偏移问题 */ | ||
useEffect(() => { | ||
const canvasDoms = document.getElementsByTagName('canvas'); | ||
Array.from(canvasDoms).forEach((item: any) => { | ||
if (!item || item.id) return; | ||
item.style.zoom = `calc(1/${scale})`; | ||
item.style.transformOrigin = 'left top'; | ||
item.style.transform = `scale(${scale})`; | ||
}); | ||
}, [scale]); | ||
|
||
/** 对 dom 进行缩放 */ | ||
const handelDomScale = (dom: HTMLElement | null, scale: number) => { | ||
if (!dom) return; | ||
dom.style.transform = 'translate(-50%)'; | ||
dom.style.zoom = `${scale}`; | ||
}; | ||
|
||
/** 设置缩放比例 */ | ||
const setScreenScale = () => { | ||
const newScale = | ||
document.documentElement.clientWidth / document.documentElement.clientHeight < screenWidth / screenHeight | ||
? document.documentElement.clientWidth / screenWidth | ||
: document.documentElement.clientHeight / screenHeight; | ||
setScale(newScale); | ||
if (screenDomRef.current) { | ||
handelDomScale(screenDomRef.current, newScale); | ||
} | ||
}; | ||
|
||
/** 设置缩放比例 */ | ||
useEffect(() => { | ||
setScreenScale(); // 初始化时调用一次 | ||
|
||
// 监听屏幕宽度改变事件 | ||
const resizeObserver = new ResizeObserver(setScreenScale); | ||
resizeObserver.observe(document.documentElement); // 观察 documentElement 的尺寸变化 | ||
|
||
return () => { | ||
resizeObserver.disconnect(); // 清理 ResizeObserver | ||
}; | ||
}, [screenWidth, screenHeight]); | ||
|
||
useEffect(() => { | ||
if (scale) { | ||
// 当 scale 计算完成后再渲染 Outlet | ||
setIsReady(true); | ||
} | ||
}, [scale]); | ||
|
||
return { isReady, scale, screenDomRef }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { Outlet } from '@umijs/max'; | ||
|
||
import { SCREEN_HEIGHT, SCREEN_WIDTH } from '@/constant'; | ||
import { useScreenScale } from '@/hooks/useScreenScale'; | ||
|
||
import styles from './index.module.less'; | ||
|
||
export default function BasicLayout() { | ||
const { isReady, screenDomRef } = useScreenScale(SCREEN_WIDTH, SCREEN_HEIGHT); | ||
return ( | ||
<div className={styles.screenWrapper}> | ||
<div className={styles.background} /> | ||
<Outlet /> | ||
<div className={styles.screen} ref={screenDomRef} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }}> | ||
<div className={styles.background} /> | ||
{isReady && <Outlet />} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.container { | ||
height: 100%; | ||
margin-top: 100px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { BarLine } from '@td-design/lego'; | ||
|
||
import { ScreenContext } from '@/context'; | ||
|
||
export default () => { | ||
const echartsHeight = useContext(ScreenContext); | ||
return ( | ||
<BarLine | ||
xAxisData={['03月', '04月']} | ||
yAxis={[{ name: '万元' }, { name: '%' }]} | ||
lineData={{ name: '同比增长率', data: [12, 11] }} | ||
barData={{ name: '运费', data: [500, 584] }} | ||
style={{ width: '100%', height: echartsHeight }} | ||
style={{ width: '100%', height: 300 }} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { BarLine } from '@td-design/lego'; | ||
|
||
import { ScreenContext } from '@/context'; | ||
|
||
export default () => { | ||
const echartsHeight = useContext(ScreenContext); | ||
|
||
return ( | ||
<BarLine | ||
xAxisData={['03月', '04月']} | ||
yAxis={[{ name: '万元' }, { name: '%' }]} | ||
lineData={{ name: '同比增长率', data: [12, 11] }} | ||
barData={{ name: '运费', data: [500, 584] }} | ||
style={{ width: '100%', height: echartsHeight }} | ||
style={{ width: '100%', height: 300 }} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters