Skip to content

Commit 83d04b3

Browse files
committed
feat: adds useGetFitPadding hook
1 parent 6922911 commit 83d04b3

File tree

5 files changed

+63
-16
lines changed

5 files changed

+63
-16
lines changed

Diff for: src/components/EditFeatureDrawer/EditFeatureGeometryToolbar/index.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343

4444
import useAppDispatch from '../../../hooks/useAppDispatch';
4545
import useAppSelector from '../../../hooks/useAppSelector';
46+
import useGetFitPadding from '../../../hooks/useGetFitPadding';
4647

4748
import {
4849
setFormDirty
@@ -74,6 +75,7 @@ export const EditFeatureGeometryToolbar: React.FC<EditFeatureGeometryToolbarProp
7475

7576
const map = useMap();
7677
const dispatch = useAppDispatch();
78+
const getFitPadding = useGetFitPadding();
7779

7880
const [editLayer, setEditLayer] = useState<OlLayerVector<OlSourceVector>>();
7981
const [, setRevision] = useState<number>(0);
@@ -124,11 +126,11 @@ export const EditFeatureGeometryToolbar: React.FC<EditFeatureGeometryToolbarProp
124126

125127
if (!isEmptyOlExtent(source.getExtent())) {
126128
map?.getView().fit(source.getExtent(), {
127-
padding: [50, 50, 50, 50]
129+
padding: getFitPadding(true)
128130
});
129131
}
130132
}
131-
}, [feature, editLayer, gjFormat, map]);
133+
}, [feature, editLayer, gjFormat, map, getFitPadding]);
132134

133135
const undoEdit = () => {
134136

Diff for: src/components/MultiSearch/index.tsx

+3-12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656

5757
import './index.less';
5858
import useAppDispatch from '../../hooks/useAppDispatch';
59+
import useGetFitPadding from '../../hooks/useGetFitPadding';
5960
import { setSearchResultState } from '../../store/searchResult';
6061

6162
export type SearchEngineFunction = (value: string, viewBox?: OlExtent) => Promise<ResultCategory[] | undefined>;
@@ -99,6 +100,7 @@ export const MultiSearch: React.FC<MultiSearchProps> = ({
99100
const [resultsVisible, setResultsVisible] = useState<boolean>(false);
100101
const [settingsVisible, setSettingsVisible] = useState<boolean>(false);
101102
const [searchResults, setSearchResults] = useState<ResultCategory[]>([]);
103+
const getFitPadding = useGetFitPadding();
102104

103105
useEffect(() => {
104106
window.addEventListener('mousedown', handleClickAway);
@@ -334,22 +336,11 @@ export const MultiSearch: React.FC<MultiSearchProps> = ({
334336

335337
const zoomOffsetOnClick = (item: Item) => {
336338
const extent = item.feature.getGeometry()?.getExtent();
337-
const toolMenuElement = document.querySelector('.tool-menu');
338-
const toolMenuWidth = toolMenuElement?.clientWidth ?? 0;
339-
340-
let drawerWidth = 0;
341-
if (ClientConfiguration.search?.searchResultDrawer) {
342-
drawerWidth = Number(
343-
getComputedStyle(document.body).getPropertyValue('--drawerWidth').replace('px', '')
344-
) || 450;
345-
}
346-
347-
const padding = [0, drawerWidth, 0, toolMenuWidth];
348339

349340
if (extent) {
350341
map?.getView().fit(extent, {
351342
size: map.getSize(),
352-
padding
343+
padding: getFitPadding(ClientConfiguration.search?.searchResultDrawer)
353344
});
354345
}
355346
};

Diff for: src/components/ToolMenu/FeatureInfo/PaginationToolbar/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { DigitizeUtil } from '@terrestris/react-util/dist/Util/DigitizeUtil';
4545

4646
import useAppDispatch from '../../../../hooks/useAppDispatch';
4747
import useAppSelector from '../../../../hooks/useAppSelector';
48+
import useGetFitPadding from '../../../../hooks/useGetFitPadding';
4849
import {
4950
setLayerId,
5051
setFeature
@@ -76,6 +77,7 @@ export const PaginationToolbar: React.FC<PaginationToolbarProps> = ({
7677
} = useTranslation();
7778
const dispatch = useAppDispatch();
7879
const map = useMap();
80+
const getFitPadding = useGetFitPadding();
7981

8082
const activeCopyTools = useAppSelector(state => state.featureInfo.activeCopyTools);
8183

@@ -152,7 +154,7 @@ export const PaginationToolbar: React.FC<PaginationToolbarProps> = ({
152154
}
153155

154156
map.getView().fit(source.getExtent(), {
155-
padding: [150, 150, 150, 150]
157+
padding: getFitPadding()
156158
});
157159
dispatch(setLayerId(getUid(layer)));
158160
dispatch(setFeature(geojsonFeature));

Diff for: src/components/ToolMenu/LayerTree/LayerTreeContextMenu/index.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262

6363
import useAppDispatch from '../../../../hooks/useAppDispatch';
6464
import useAppSelector from '../../../../hooks/useAppSelector';
65+
import useGetFitPadding from '../../../../hooks/useGetFitPadding';
6566
import useSHOGunAPIClient from '../../../../hooks/useSHOGunAPIClient';
6667

6768
import {
@@ -96,6 +97,7 @@ export const LayerTreeContextMenu: React.FC<LayerTreeContextMenuProps> = ({
9697
const dispatch = useAppDispatch();
9798
const client = useSHOGunAPIClient();
9899
const map = useMap();
100+
const getFitPadding = useGetFitPadding();
99101
const {
100102
t
101103
} = useTranslation();
@@ -158,7 +160,9 @@ export const LayerTreeContextMenu: React.FC<LayerTreeContextMenuProps> = ({
158160
} : {}
159161
});
160162
extent = transformExtent(extent, 'EPSG:4326', map.getView().getProjection());
161-
map.getView().fit(extent);
163+
map.getView().fit(extent, {
164+
padding: getFitPadding()
165+
});
162166
} catch (error) {
163167
Logger.error(error);
164168
notification.error({

Diff for: src/hooks/useGetFitPadding.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* This hook returns a function to calculate padding for the fit method
3+
* of an ol.View. It considers the menu width, header height, footer height
4+
* and drawer width if drawerOpen is set to true.
5+
*
6+
* It takes a boolean to determine if the drawer is open.
7+
*
8+
* The returned function (getFitPadding) computes an array consisting of:
9+
* - header height (using the '--headerHeight' CSS property, defaults to 50px if undefined)
10+
* - drawer width (using the '--drawerWidth' CSS property, defaults to 450px if undefined and if the drawer is open)
11+
* - footer height (using the '--footerHeight' CSS property, defaults to 40px if undefined)
12+
* - tool menu width (derived from the width of the element with the 'tool-menu' class)
13+
*
14+
* @returns (drawerOpen: boolean) => [number, number, number, number]
15+
*/
16+
export const useGetFitPadding = () => {
17+
18+
const getCssPropertyValue = (value: string, fallback: number) => {
19+
return Number(
20+
getComputedStyle(document.body).getPropertyValue(value).replace('px', '')
21+
) || fallback;
22+
};
23+
24+
/**
25+
* This function calculates the padding for the fit method of an ol.View.
26+
*
27+
* @param drawerOpen Include the drawer width in the calculation
28+
* @returns [topPadding, rightPadding, bottomPadding, leftPadding]
29+
*/
30+
function getFitPadding(drawerOpen = false) {
31+
const toolMenuElement = document.querySelector('.tool-menu');
32+
const toolMenuWidth = toolMenuElement?.clientWidth ?? 0;
33+
34+
const headerHeight = getCssPropertyValue('--headerHeight', 50);
35+
const footerHeight = getCssPropertyValue('--footerHeight', 40);
36+
37+
let drawerWidth = 0;
38+
if (drawerOpen) {
39+
drawerWidth = getCssPropertyValue('--drawerWidth', 450);
40+
}
41+
42+
return [headerHeight, drawerWidth, footerHeight, toolMenuWidth];
43+
};
44+
45+
return getFitPadding;
46+
};
47+
48+
export default useGetFitPadding;

0 commit comments

Comments
 (0)