Skip to content

Commit 2a30bf3

Browse files
authored
Merge pull request #976 from xieliuduo/dev-board
refactor: autoBoard lazyLoad
2 parents 0674956 + b26c1fa commit 2a30bf3

File tree

14 files changed

+209
-271
lines changed

14 files changed

+209
-271
lines changed

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@
217217
"path": "cz-conventional-changelog"
218218
}
219219
}
220-
}
220+
}

frontend/src/app/hooks/useVisibleHidden.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import { useEffect, useState } from 'react';
2020

21-
export const useVisibleHidden = (timeNum: number = 500) => {
21+
export const useVisibleHidden = (timeNum: number = 0) => {
2222
const [visible, setVisible] = useState<'visible' | 'hidden'>('hidden');
2323
useEffect(() => {
2424
setTimeout(() => {

frontend/src/app/pages/DashBoardPage/components/FreeBoardBackground.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,5 @@ export default SlideBackground;
7575
const Warp = styled(StyledBackground)<{ editing: boolean }>`
7676
position: relative;
7777
box-shadow: ${p => (p.editing ? '0px 1px 8px 2px #8cb4be;' : '')};
78-
transition: all ease-in 200ms;
7978
transform-origin: 0 0;
8079
`;

frontend/src/app/pages/DashBoardPage/components/WidgetCore/DataChartWidget/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const DataChartWidget: React.FC<{}> = memo(() => {
4646
const { id: widgetId } = widget;
4747
const { widgetChartClick } = useContext(WidgetMethodContext);
4848
const { ref, cacheW, cacheH } = useCacheWidthHeight();
49+
4950
const widgetRef = useRef<Widget>(widget);
5051
useEffect(() => {
5152
widgetRef.current = widget;
@@ -62,6 +63,7 @@ export const DataChartWidget: React.FC<{}> = memo(() => {
6263
);
6364

6465
const chart = useMemo(() => {
66+
// console.log('_dataChartID', dataChart?.id);
6567
if (!dataChart) {
6668
return null;
6769
}
@@ -115,6 +117,7 @@ export const DataChartWidget: React.FC<{}> = memo(() => {
115117
}),
116118
[data],
117119
);
120+
118121
const chartFrame = useMemo(() => {
119122
if (!dataChart) {
120123
return `not found dataChart`;
@@ -166,8 +169,8 @@ export const DataChartWidget: React.FC<{}> = memo(() => {
166169
});
167170
const ChartFrameBox = styled.div`
168171
position: absolute;
169-
height: 100%;
170172
width: 100%;
173+
height: 100%;
171174
overflow: hidden;
172175
`;
173176
const Wrap = styled.div`

frontend/src/app/pages/DashBoardPage/components/WidgetToolBar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ const WidgetToolBar: FC = () => {
109109
return (
110110
<StyleWrap onClick={ssp} className="widget-tool-bar">
111111
<Space size={0}>
112-
{renderErrorInfo(errInfo)}
113-
{renderWaiting()}
114112
{renderLoading()}
113+
{renderWaiting()}
114+
{renderErrorInfo(errInfo)}
115115
{renderLocking()}
116116
{renderLinkage()}
117117
{renderWidgetAction()}

frontend/src/app/pages/DashBoardPage/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,9 @@ export const WIDGET_TITLE_ALIGN_OPTIONS = [
279279
{ name: '左', value: TEXT_ALIGN_ENUM.left },
280280
{ name: '中', value: TEXT_ALIGN_ENUM.center },
281281
];
282+
283+
export const DefaultWidgetData = {
284+
id: '',
285+
columns: [],
286+
rows: [],
287+
};
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Datart
3+
*
4+
* Copyright 2021
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
import { useWidgetRowHeight } from 'app/hooks/useWidgetRowHeight';
19+
import { throttle } from 'echarts';
20+
import {
21+
RefObject,
22+
useCallback,
23+
useContext,
24+
useEffect,
25+
useMemo,
26+
useRef,
27+
} from 'react';
28+
import { Layout } from 'react-grid-layout';
29+
import { BoardContext } from '../components/BoardProvider/BoardProvider';
30+
import { WidgetInfo } from '../pages/Board/slice/types';
31+
export default function useAutoBoardRenderItem(
32+
layoutWidgetInfoMap: Record<string, WidgetInfo>,
33+
margin: [number, number],
34+
) {
35+
const { ref, widgetRowHeight } = useWidgetRowHeight();
36+
37+
const { renderedWidgetById } = useContext(BoardContext);
38+
39+
const currentLayout = useRef<Layout[]>([]);
40+
41+
let waitItemInfos = useRef<{ id: string; rendered: boolean }[]>([]);
42+
43+
const gridWrapRef: RefObject<HTMLDivElement> = useRef(null);
44+
45+
useEffect(() => {
46+
const layoutWaitWidgetInfos = Object.values(layoutWidgetInfoMap).filter(
47+
widgetInfo => {
48+
return !widgetInfo.rendered;
49+
},
50+
);
51+
52+
waitItemInfos.current = layoutWaitWidgetInfos.map(widgetInfo => ({
53+
id: widgetInfo.id,
54+
rendered: widgetInfo.rendered,
55+
}));
56+
}, [layoutWidgetInfoMap]);
57+
58+
const calcItemTop = useCallback(
59+
(id: string) => {
60+
const curItem = currentLayout.current.find(ele => ele.i === id);
61+
if (!curItem) return Infinity;
62+
return Math.round((widgetRowHeight + margin[0]) * curItem.y);
63+
},
64+
[margin, widgetRowHeight],
65+
);
66+
67+
const lazyRender = useCallback(() => {
68+
if (!gridWrapRef.current) return;
69+
if (!waitItemInfos.current.length) return;
70+
const waitingItems = waitItemInfos.current;
71+
const { offsetHeight, scrollTop } = gridWrapRef.current! || {};
72+
waitingItems.forEach(item => {
73+
const itemTop = calcItemTop(item.id);
74+
if (itemTop - scrollTop < offsetHeight) {
75+
renderedWidgetById(item.id);
76+
}
77+
});
78+
}, [calcItemTop, renderedWidgetById]);
79+
80+
const ttRender = useMemo(() => throttle(lazyRender, 50), [lazyRender]);
81+
82+
useEffect(() => {
83+
if (gridWrapRef.current) {
84+
setImmediate(() => lazyRender());
85+
gridWrapRef.current.removeEventListener('scroll', ttRender, false);
86+
gridWrapRef.current.addEventListener('scroll', ttRender, false);
87+
// issues#339
88+
window.addEventListener('resize', ttRender, false);
89+
}
90+
return () => {
91+
gridWrapRef?.current?.removeEventListener('scroll', ttRender, false);
92+
window.removeEventListener('resize', ttRender, false);
93+
};
94+
}, [ttRender, lazyRender]);
95+
96+
return {
97+
ref,
98+
gridWrapRef,
99+
currentLayout,
100+
widgetRowHeight,
101+
};
102+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Datart
3+
*
4+
* Copyright 2021
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
import { useEffect, useState } from 'react';
19+
import { Layouts } from 'react-grid-layout';
20+
import { Widget } from '../pages/Board/slice/types';
21+
export default function useGridLayoutMap(
22+
layoutWidgetMap: Record<string, Widget>,
23+
) {
24+
const [layoutMap, setLayoutMap] = useState<Layouts>({});
25+
useEffect(() => {
26+
const layoutMap: Layouts = {
27+
lg: [],
28+
xs: [],
29+
};
30+
Object.values(layoutWidgetMap).forEach(widget => {
31+
const lg = widget.config.rect || widget.config.mobileRect || {};
32+
const xs = widget.config.mobileRect || widget.config.rect || {};
33+
const lock = widget.config.lock;
34+
layoutMap.lg.push({
35+
i: widget.id,
36+
x: lg.x,
37+
y: lg.y,
38+
w: lg.width,
39+
h: lg.height,
40+
static: lock,
41+
});
42+
layoutMap.xs.push({
43+
i: widget.id,
44+
x: xs.x,
45+
y: xs.y,
46+
w: xs.width,
47+
h: xs.height,
48+
static: lock,
49+
});
50+
});
51+
setLayoutMap(layoutMap);
52+
}, [layoutWidgetMap]);
53+
return layoutMap;
54+
}

0 commit comments

Comments
 (0)