-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathDrawButton.tsx
159 lines (138 loc) · 4.23 KB
/
DrawButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as React from 'react';
import {ReactNode, useCallback, useState} from 'react';
import OlFeature from 'ol/Feature';
import OlGeometry from 'ol/geom/Geometry';
import {
DrawEvent
} from 'ol/interaction/Draw';
import { useDraw, UseDrawProps } from '@terrestris/react-util/dist/Hooks/useDraw/useDraw';
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import {usePropOrDefault} from '@terrestris/react-util/dist/Hooks/usePropOrDefault/usePropOrDefault';
import {DigitizeUtil} from '@terrestris/react-util/dist/Util/DigitizeUtil';
import { CSS_PREFIX } from '../../constants';
import { FeatureLabelModal } from '../../FeatureLabelModal/FeatureLabelModal';
import ToggleButton, { ToggleButtonProps } from '../ToggleButton/ToggleButton';
type ButtonDrawType = 'Point' | 'LineString' | 'Polygon' | 'Circle' | 'Rectangle' | 'Text';
interface OwnProps {
drawType: ButtonDrawType;
/**
* Callback function that will be called when the ok-button of the modal was clicked
*/
onModalLabelOk?: (feature: OlFeature<OlGeometry>) => void;
/**
* Callback function that will be called
* when the cancel-button of the modal was clicked
*/
onModalLabelCancel?: () => void;
/**
* Maximal length of feature label.
* If exceeded label will be divided into multiple lines. Optional.
*/
maxLabelLineLength?: number;
/**
* Title for modal used for input of labels for digitize features.
*/
modalPromptTitle?: string;
/**
* Text string for `OK` button of the modal.
*/
modalPromptOkButtonText?: string;
/**
* Text string for `Cancel` button of the modal.
*/
modalPromptCancelButtonText?: string;
}
export type DrawButtonProps = OwnProps & Omit<UseDrawProps, 'drawType'|'active'> & Partial<ToggleButtonProps>;
/**
* The className added to this component.
*/
const defaultClassName = `${CSS_PREFIX}drawbutton`;
/**
* The DrawButton.
*/
const DrawButton: React.FC<DrawButtonProps> = ({
className,
digitizeLayer,
drawInteractionConfig,
drawStyle,
drawType,
maxLabelLineLength,
modalPromptCancelButtonText = 'Cancel',
modalPromptOkButtonText = 'Ok',
modalPromptTitle = 'Label',
onDrawEnd,
onDrawStart,
onModalLabelCancel,
onModalLabelOk,
pressed,
...passThroughProps
}) => {
const map = useMap();
const layer = usePropOrDefault(
digitizeLayer,
() => map ? DigitizeUtil.getDigitizeLayer(map) : undefined,
[map]
);
/**
* Currently drawn feature which should be represented as label or post-it.
*/
const [digitizeTextFeature, setDigitizeTextFeature] = useState<OlFeature<OlGeometry> | null>(null);
const onDrawEndInternal = useCallback((evt: DrawEvent) => {
if (drawType === 'Text') {
evt.feature.set('isLabel', true);
setDigitizeTextFeature(evt.feature);
}
onDrawEnd?.(evt);
}, [drawType, onDrawEnd]);
useDraw({
onDrawEnd: onDrawEndInternal,
digitizeLayer: layer,
drawInteractionConfig,
drawStyle,
drawType: drawType === 'Text' ? 'Point' : drawType,
onDrawStart,
active: !!pressed
});
const finalClassName = className
? `${defaultClassName} ${className}`
: defaultClassName;
const btnWrapperClass = `${CSS_PREFIX}digitize-button-wrapper`;
let modal: ReactNode = null;
if (digitizeTextFeature) {
const onModalLabelOkInternal = () => {
onModalLabelOk?.(digitizeTextFeature);
setDigitizeTextFeature(null);
};
const onModalLabelCancelInternal = () => {
layer?.getSource()?.removeFeature(digitizeTextFeature);
setDigitizeTextFeature(null);
onModalLabelCancel?.();
digitizeLayer?.getSource()?.removeFeature(digitizeTextFeature);
setDigitizeTextFeature(null);
};
modal = (
<FeatureLabelModal
feature={digitizeTextFeature}
onOk={onModalLabelOkInternal}
onCancel={onModalLabelCancelInternal}
title={modalPromptTitle}
okText={modalPromptOkButtonText}
cancelText={modalPromptCancelButtonText}
maxLabelLineLength={maxLabelLineLength}
/>
);
}
return (
<span className={btnWrapperClass}>
<ToggleButton
className={finalClassName}
pressed={pressed}
{...passThroughProps}
/>
{
modal
}
</span>
);
};
export default DrawButton;