• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

terrestris / react-geo / 16338835141

17 Jul 2025 07:22AM CUT coverage: 57.562%. Remained the same
16338835141

push

github

web-flow
Merge pull request #4344 from terrestris/dependabot/npm_and_yarn/typescript-eslint-8.37.0

build(deps-dev): bump typescript-eslint from 8.35.0 to 8.37.0

532 of 1032 branches covered (51.55%)

Branch coverage included in aggregate %.

1036 of 1692 relevant lines covered (61.23%)

10.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/Button/DrawButton/DrawButton.tsx
1
import * as React from 'react';
2

3
import {ReactNode, useCallback, useState} from 'react';
4

5
import OlFeature from 'ol/Feature';
6

7
import OlGeometry from 'ol/geom/Geometry';
8

9
import {
10
  DrawEvent
11
} from 'ol/interaction/Draw';
12

13
import { useDraw, UseDrawProps } from '@terrestris/react-util/dist/Hooks/useDraw/useDraw';
14
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
15
import {usePropOrDefault} from '@terrestris/react-util/dist/Hooks/usePropOrDefault/usePropOrDefault';
16
import {DigitizeUtil} from '@terrestris/react-util/dist/Util/DigitizeUtil';
17

18

19
import { CSS_PREFIX } from '../../constants';
20
import { FeatureLabelModal } from '../../FeatureLabelModal/FeatureLabelModal';
21
import ToggleButton, { ToggleButtonProps } from '../ToggleButton/ToggleButton';
22

23
type ButtonDrawType = 'Point' | 'LineString' | 'Polygon' | 'Circle' | 'Rectangle' | 'Text';
24

25
interface OwnProps {
26
  drawType: ButtonDrawType;
27
  /**
28
   * Callback function that will be called when the ok-button of the modal was clicked
29
   */
30
  onModalLabelOk?: (feature: OlFeature<OlGeometry>) => void;
31
  /**
32
   * Callback function that will be called
33
   * when the cancel-button of the modal was clicked
34
   */
35
  onModalLabelCancel?: () => void;
36
  /**
37
   * Maximal length of feature label.
38
   * If exceeded label will be divided into multiple lines. Optional.
39
   */
40
  maxLabelLineLength?: number;
41
  /**
42
   * Title for modal used for input of labels for digitize features.
43
   */
44
  modalPromptTitle?: string;
45
  /**
46
   * Text string for `OK` button of the modal.
47
   */
48
  modalPromptOkButtonText?: string;
49
  /**
50
   * Text string for `Cancel` button of the modal.
51
   */
52
  modalPromptCancelButtonText?: string;
53
}
54

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

57
/**
58
 * The className added to this component.
59
 */
60
const defaultClassName = `${CSS_PREFIX}drawbutton`;
2✔
61

62
/**
63
 * The DrawButton.
64
 */
65
const DrawButton: React.FC<DrawButtonProps> = ({
2✔
66
  className,
67
  digitizeLayer,
68
  drawInteractionConfig,
69
  drawStyle,
70
  drawType,
71
  maxLabelLineLength,
72
  modalPromptCancelButtonText = 'Cancel',
3✔
73
  modalPromptOkButtonText = 'Ok',
3✔
74
  modalPromptTitle = 'Label',
3✔
75
  onDrawEnd,
76
  onDrawStart,
77
  onModalLabelCancel,
78
  onModalLabelOk,
79
  pressed,
80
  ...passThroughProps
81
}) => {
82
  const map = useMap();
3✔
83
  const layer = usePropOrDefault(
3✔
84
    digitizeLayer,
85
    () => map ? DigitizeUtil.getDigitizeLayer(map) : undefined,
1!
86
    [map]
87
  );
88
  /**
89
   * Currently drawn feature which should be represented as label or post-it.
90
   */
91
  const [digitizeTextFeature, setDigitizeTextFeature] = useState<OlFeature<OlGeometry> | null>(null);
3✔
92

93
  const onDrawEndInternal = useCallback((evt: DrawEvent) => {
3✔
94
    if (drawType === 'Text') {
×
95
      evt.feature.set('isLabel', true);
×
96
      setDigitizeTextFeature(evt.feature);
×
97
    }
98
    onDrawEnd?.(evt);
×
99
  }, [drawType, onDrawEnd]);
100

101
  useDraw({
3✔
102
    onDrawEnd: onDrawEndInternal,
103
    digitizeLayer: layer,
104
    drawInteractionConfig,
105
    drawStyle,
106
    drawType: drawType === 'Text' ? 'Point' : drawType,
3!
107
    onDrawStart,
108
    active: !!pressed
109
  });
110

111
  const finalClassName = className
3!
112
    ? `${defaultClassName} ${className}`
113
    : defaultClassName;
114

115
  const btnWrapperClass = `${CSS_PREFIX}digitize-button-wrapper`;
3✔
116

117
  let modal: ReactNode = null;
3✔
118
  if (digitizeTextFeature) {
3!
119
    const onModalLabelOkInternal = () => {
×
120
      onModalLabelOk?.(digitizeTextFeature);
×
121
      setDigitizeTextFeature(null);
×
122
    };
123

124
    const onModalLabelCancelInternal = () => {
×
125
      layer?.getSource()?.removeFeature(digitizeTextFeature);
×
126
      setDigitizeTextFeature(null);
×
127
      onModalLabelCancel?.();
×
128
      digitizeLayer?.getSource()?.removeFeature(digitizeTextFeature);
×
129
      setDigitizeTextFeature(null);
×
130
    };
131

132
    modal = (
×
133
      <FeatureLabelModal
134
        feature={digitizeTextFeature}
135
        onOk={onModalLabelOkInternal}
136
        onCancel={onModalLabelCancelInternal}
137
        title={modalPromptTitle}
138
        okText={modalPromptOkButtonText}
139
        cancelText={modalPromptCancelButtonText}
140
        maxLabelLineLength={maxLabelLineLength}
141
      />
142
    );
143
  }
144

145
  return (
3✔
146
    <span className={btnWrapperClass}>
147
      <ToggleButton
148
        className={finalClassName}
149
        pressed={pressed}
150
        {...passThroughProps}
151
      />
152
      {
153
        modal
154
      }
155
    </span>
156
  );
157
};
158

159
export default DrawButton;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc