-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathModifyButton.tsx
155 lines (138 loc) · 3.96 KB
/
ModifyButton.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
import * as React from 'react';
import {ReactNode, useCallback, useState} from 'react';
import OlFeature from 'ol/Feature';
import OlGeometry from 'ol/geom/Geometry';
import { SelectEvent as OlSelectEvent } from 'ol/interaction/Select';
import { useModify, UseModifyProps } from '@terrestris/react-util/dist/Hooks/useModify/useModify';
import { CSS_PREFIX } from '../../constants';
import { FeatureLabelModal } from '../../FeatureLabelModal/FeatureLabelModal';
import ToggleButton, {ToggleButtonProps} from '../ToggleButton/ToggleButton';
interface OwnProps {
/**
* The className which should be added.
*/
className?: string;
/**
* 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;
/**
* Enable label editing via modal. Will open before being able to modify/translate feature. Default: `true`.
*/
editLabel?: boolean;
}
export type ModifyButtonProps = OwnProps & Omit<UseModifyProps, 'active'> &
Partial<ToggleButtonProps>;
/**
* The className added to this component.
*/
const defaultClassName = `${CSS_PREFIX}modifybutton`;
export const ModifyButton: React.FC<ModifyButtonProps> = ({
className,
hitTolerance = 5,
onFeatureSelect,
onModifyStart,
onModifyEnd,
onTranslateStart,
onTranslateEnd,
onTranslating,
digitizeLayer,
selectStyle,
selectInteractionConfig,
modifyInteractionConfig,
translateInteractionConfig,
onModalLabelOk,
onModalLabelCancel,
maxLabelLineLength,
modalPromptTitle,
modalPromptOkButtonText,
modalPromptCancelButtonText,
editLabel = true,
pressed,
...passThroughProps
}) => {
const [editLabelFeature, setEditLabelFeature] = useState<OlFeature<OlGeometry>|null>(null);
const onFeatureSelectInternal = useCallback((event: OlSelectEvent) => {
if (editLabel) {
const labeled = event.selected.find(f => f.get('isLabel'));
setEditLabelFeature(labeled || null);
}
onFeatureSelect?.(event);
}, [editLabel, onFeatureSelect]);
useModify({
selectStyle,
selectInteractionConfig,
digitizeLayer,
onModifyStart,
onModifyEnd,
onTranslateStart,
onTranslateEnd,
onTranslating,
active: !!pressed,
modifyInteractionConfig,
translateInteractionConfig,
onFeatureSelect: onFeatureSelectInternal,
hitTolerance
});
const finalClassName = className
? `${defaultClassName} ${className}`
: defaultClassName;
const btnWrapperClass = `${CSS_PREFIX}digitize-button-wrapper`;
let modal: ReactNode = null;
if (editLabelFeature) {
const onModalLabelOkInternal = () => {
onModalLabelOk?.(editLabelFeature);
setEditLabelFeature(null);
};
const onModalLabelCancelInternal = () => {
onModalLabelCancel?.();
setEditLabelFeature(null);
};
modal = (
<FeatureLabelModal
feature={editLabelFeature}
onOk={onModalLabelOkInternal}
onCancel={onModalLabelCancelInternal}
title={modalPromptTitle}
okText={modalPromptOkButtonText}
cancelText={modalPromptCancelButtonText}
maxLabelLineLength={maxLabelLineLength}
/>
);
}
return (
<span className={btnWrapperClass}>
<ToggleButton
pressed={pressed}
className={finalClassName}
{...passThroughProps}
/>
{
modal
}
</span>
);
};
export default ModifyButton;