Skip to content

Commit

Permalink
refactor: CopyButton, DeleteButton, SelectFeaturesButton use ho…
Browse files Browse the repository at this point in the history
…oks now
  • Loading branch information
simonseyock authored and dnlkoch committed Feb 20, 2024
1 parent d46f8fe commit a0f50af
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 180 deletions.
72 changes: 39 additions & 33 deletions src/Button/CopyButton/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import AnimateUtil from '@terrestris/ol-util/dist/AnimateUtil/AnimateUtil';
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import { usePropOrDefault } from '@terrestris/react-util/dist/Hooks/usePropOrDefault/usePropOrDefault';
import {
useSelectFeatures,
UseSelectFeaturesProps
} from '@terrestris/react-util/dist/Hooks/useSelectFeatures/useSelectFeatures';
import { DigitizeUtil } from '@terrestris/react-util/dist/Util/DigitizeUtil';
import OlFeature from 'ol/Feature';
import { SelectEvent as OlSelectEvent } from 'ol/interaction/Select';
import OlVectorLayer from 'ol/layer/Vector';
import OlVectorSource from 'ol/source/Vector';
import OlStyle from 'ol/style/Style';
import React, {
useEffect,
useState
useCallback,
useMemo,
} from 'react';

import { CSS_PREFIX } from '../../constants';
import SelectFeaturesButton, { SelectFeaturesButtonProps } from '../SelectFeaturesButton/SelectFeaturesButton';
import ToggleButton, {ToggleButtonProps} from '../ToggleButton/ToggleButton';

interface OwnProps {
/**
Expand All @@ -29,76 +34,77 @@ interface OwnProps {
onFeatureCopy?: (event: OlSelectEvent) => void;
}

export type CopyButtonProps = OwnProps & Omit<SelectFeaturesButtonProps,
'layers'|'onFeatureSelect'|'featuresCollection'>;
export type CopyButtonProps = OwnProps & Omit<UseSelectFeaturesProps,
'layers'|'onFeatureSelect'|'featuresCollection'|'clearAfterSelect'> & Partial<ToggleButtonProps>;

// The class name for the component.
const defaultClassName = `${CSS_PREFIX}copybutton`;

const CopyButton: React.FC<CopyButtonProps> = ({
className,
onFeatureCopy,
digitizeLayer,
onFeatureCopy,
selectStyle,
selectInteractionConfig,
hitTolerance,
pressed,
...passThroughProps
}) => {

const [layers, setLayers] = useState<[OlVectorLayer<OlVectorSource<OlFeature>>]|null>(null);

const map = useMap();

useEffect(() => {
if (!map) {
return;
}
const layer = usePropOrDefault(
digitizeLayer,
() => map ? DigitizeUtil.getDigitizeLayer(map) : undefined,
[map]
);

if (digitizeLayer) {
setLayers([digitizeLayer]);
} else {
setLayers([DigitizeUtil.getDigitizeLayer(map)]);
}
}, [map, digitizeLayer]);
const layers = useMemo(() => layer ? [layer] : [], [layer]);

const onFeatureSelect = (event: OlSelectEvent) => {
const onFeatureSelect = useCallback((event: OlSelectEvent) => {
onFeatureCopy?.(event);

const feat = event.selected[0];

if (!feat || !layers || !map) {
if (!feat || !layer || !map) {
return;
}

const copy = feat.clone();

layers[0].getSource()?.addFeature(copy);
layer.getSource()?.addFeature(copy);

AnimateUtil.moveFeature(
map,
layers[0],
layer,
copy,
500,
50,
layers[0].getStyle() as OlStyle
layer.getStyle() as OlStyle
);
};
}, [layer, onFeatureCopy, map]);

useSelectFeatures({
selectStyle,
selectInteractionConfig,
layers,
active: !!pressed,
hitTolerance,
onFeatureSelect,
clearAfterSelect: true
});

const finalClassName = className
? `${defaultClassName} ${className}`
: defaultClassName;

if (!layers) {
return null;
}

return (
<SelectFeaturesButton
layers={layers}
onFeatureSelect={onFeatureSelect}
<ToggleButton
pressed={pressed}
className={finalClassName}
clearAfterSelect={true}
{...passThroughProps}
/>
);

};

export default CopyButton;
71 changes: 42 additions & 29 deletions src/Button/DeleteButton/DeleteButton.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import { usePropOrDefault } from '@terrestris/react-util/dist/Hooks/usePropOrDefault/usePropOrDefault';
import {
useSelectFeatures,
UseSelectFeaturesProps
} from '@terrestris/react-util/dist/Hooks/useSelectFeatures/useSelectFeatures';
import { DigitizeUtil } from '@terrestris/react-util/dist/Util/DigitizeUtil';
import OlFeature from 'ol/Feature';
import { SelectEvent as OlSelectEvent } from 'ol/interaction/Select';
import OlVectorLayer from 'ol/layer/Vector';
import OlVectorSource from 'ol/source/Vector';
import * as React from 'react';
import { useEffect, useState } from 'react';
import {useCallback, useMemo} from 'react';

import { CSS_PREFIX } from '../../constants';
import SelectFeaturesButton, { SelectFeaturesButtonProps } from '../SelectFeaturesButton/SelectFeaturesButton';
import ToggleButton, {ToggleButtonProps} from '../ToggleButton/ToggleButton';

interface OwnProps {
/**
Expand All @@ -25,8 +30,8 @@ interface OwnProps {
onFeatureRemove?: (event: OlSelectEvent) => void;
}

export type DeleteButtonProps = OwnProps & Omit<SelectFeaturesButtonProps,
'layers'|'onFeatureSelect'|'featuresCollection'>;
export type DeleteButtonProps = OwnProps & Omit<UseSelectFeaturesProps,
'layers'|'onFeatureSelect'|'featuresCollection'|'clearAfterSelect'> & Partial<ToggleButtonProps>;

/**
* The className added to this component.
Expand All @@ -37,45 +42,53 @@ export const DeleteButton: React.FC<DeleteButtonProps> = ({
className,
digitizeLayer,
onFeatureRemove,
selectStyle,
selectInteractionConfig,
hitTolerance,
pressed,
...passThroughProps
}) => {
const [layers, setLayers] = useState<[OlVectorLayer<OlVectorSource<OlFeature>>]|null>(null);

const map = useMap();

useEffect(() => {
if (!map) {
return;
}

if (digitizeLayer) {
setLayers([digitizeLayer]);
} else {
setLayers([DigitizeUtil.getDigitizeLayer(map)]);
}
}, [map, digitizeLayer]);
const layer = usePropOrDefault(
digitizeLayer,
() => map ? DigitizeUtil.getDigitizeLayer(map) : undefined,
[map]
);

if (!layers) {
return null;
}
const layers = useMemo(() => layer ? [layer] : [], [layer]);

const onFeatureSelect = (event: OlSelectEvent) => {
const onFeatureSelect = useCallback((event: OlSelectEvent) => {
if (!layer) {
return;
}
onFeatureRemove?.(event);
const feat = event.selected[0];
layers[0].getSource()?.removeFeature(feat);
};
layer.getSource()?.removeFeature(feat);
}, [layer, onFeatureRemove]);

useSelectFeatures({
selectStyle,
selectInteractionConfig,
layers,
active: !!pressed,
hitTolerance,
onFeatureSelect,
clearAfterSelect: true
});

const finalClassName = className
? `${defaultClassName} ${className}`
: defaultClassName;

return <SelectFeaturesButton
layers={layers}
onFeatureSelect={onFeatureSelect}
className={finalClassName}
clearAfterSelect={true}
{...passThroughProps}
/>;
return (
<ToggleButton
pressed={pressed}
className={finalClassName}
{...passThroughProps}
/>
);
};

export default DeleteButton;
2 changes: 1 addition & 1 deletion src/Button/DrawButton/DrawButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface OwnProps {
modalPromptCancelButtonText?: string;
}

export type DrawButtonProps = OwnProps & Omit<UseDrawProps, 'drawType'> & Partial<ToggleButtonProps>;
export type DrawButtonProps = OwnProps & Omit<UseDrawProps, 'drawType'|'active'> & Partial<ToggleButtonProps>;

/**
* The className added to this component.
Expand Down
Loading

0 comments on commit a0f50af

Please sign in to comment.