|
1 | 1 | import * as React from 'react'; |
2 | | -import classNames from 'classnames'; |
3 | | -import RegionCreator from './RegionCreator'; |
4 | 2 | import RegionList from './RegionList'; |
5 | | -import RegionRect, { RegionRectRef } from './RegionRect'; |
6 | | -import { AnnotationRegion, Rect } from '../@types'; |
7 | | -import { CreatorItemRegion, CreatorStatus } from '../store/creator'; |
| 3 | +import { AnnotationRegion } from '../@types'; |
8 | 4 | import './RegionAnnotations.scss'; |
9 | 5 |
|
10 | 6 | type Props = { |
11 | 7 | activeAnnotationId: string | null; |
12 | 8 | annotations: AnnotationRegion[]; |
13 | | - isCreating: boolean; |
14 | | - isDiscoverabilityEnabled: boolean; |
15 | | - isRotated: boolean; |
16 | | - location: number; |
17 | | - resetCreator: () => void; |
18 | 9 | setActiveAnnotationId: (annotationId: string | null) => void; |
19 | | - setReferenceShape: (rect: DOMRect) => void; |
20 | | - setStaged: (staged: CreatorItemRegion | null) => void; |
21 | | - setStatus: (status: CreatorStatus) => void; |
22 | | - staged?: CreatorItemRegion | null; |
23 | 10 | }; |
24 | 11 |
|
25 | | -type State = { |
26 | | - rectRef?: RegionRectRef; |
27 | | -}; |
28 | | - |
29 | | -export default class RegionAnnotations extends React.PureComponent<Props, State> { |
30 | | - static defaultProps = { |
31 | | - annotations: [], |
32 | | - isCreating: false, |
33 | | - isDiscoverabilityEnabled: false, |
34 | | - isRotated: false, |
35 | | - }; |
36 | | - |
37 | | - state: State = {}; |
38 | | - |
39 | | - componentDidUpdate(_prevProps: Props, prevState: State): void { |
40 | | - const { setReferenceShape } = this.props; |
41 | | - const { rectRef } = this.state; |
42 | | - const { rectRef: prevRectRef } = prevState; |
43 | | - |
44 | | - if (prevRectRef !== rectRef && rectRef) { |
45 | | - setReferenceShape(rectRef.getBoundingClientRect()); |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - handleAbort = (): void => { |
50 | | - const { resetCreator } = this.props; |
51 | | - resetCreator(); |
52 | | - }; |
53 | | - |
54 | | - handleAnnotationActive = (annotationId: string | null): void => { |
55 | | - const { setActiveAnnotationId } = this.props; |
| 12 | +const RegionAnnotations = (props: Props): JSX.Element => { |
| 13 | + const { activeAnnotationId, annotations, setActiveAnnotationId } = props; |
56 | 14 |
|
| 15 | + const handleAnnotationActive = (annotationId: string | null): void => { |
57 | 16 | setActiveAnnotationId(annotationId); |
58 | 17 | }; |
59 | 18 |
|
60 | | - handleStart = (): void => { |
61 | | - const { setStaged, setStatus } = this.props; |
62 | | - setStaged(null); |
63 | | - setStatus(CreatorStatus.started); |
64 | | - }; |
65 | | - |
66 | | - handleStop = (shape: Rect): void => { |
67 | | - const { location, setStaged, setStatus } = this.props; |
68 | | - setStaged({ location, shape }); |
69 | | - setStatus(CreatorStatus.staged); |
70 | | - }; |
71 | | - |
72 | | - renderCreator = (): JSX.Element => { |
73 | | - const { isCreating, isDiscoverabilityEnabled, isRotated } = this.props; |
74 | | - const canCreate = isCreating && !isRotated; |
75 | | - |
76 | | - return ( |
77 | | - <> |
78 | | - {canCreate && ( |
79 | | - <RegionCreator |
80 | | - className={classNames('ba-RegionAnnotations-creator', { |
81 | | - 'is-discoverability-enabled': isDiscoverabilityEnabled, |
82 | | - })} |
83 | | - onAbort={this.handleAbort} |
84 | | - onStart={this.handleStart} |
85 | | - onStop={this.handleStop} |
86 | | - /> |
87 | | - )} |
88 | | - </> |
89 | | - ); |
90 | | - }; |
91 | | - |
92 | | - renderList = (): JSX.Element => { |
93 | | - const { activeAnnotationId, annotations } = this.props; |
94 | | - |
95 | | - return ( |
96 | | - <RegionList |
97 | | - activeId={activeAnnotationId} |
98 | | - annotations={annotations} |
99 | | - className="ba-RegionAnnotations-list" |
100 | | - onSelect={this.handleAnnotationActive} |
101 | | - /> |
102 | | - ); |
103 | | - }; |
104 | | - |
105 | | - setRectRef = (rectRef: RegionRectRef): void => { |
106 | | - this.setState({ rectRef }); |
107 | | - }; |
108 | | - |
109 | | - render(): JSX.Element { |
110 | | - const { isCreating, isDiscoverabilityEnabled, isRotated, staged } = this.props; |
111 | | - const canCreate = isCreating && !isRotated; |
112 | | - |
113 | | - return ( |
114 | | - <> |
115 | | - {isDiscoverabilityEnabled ? ( |
116 | | - <> |
117 | | - {/* With discoverability, put the RegionCreator below the RegionList so that existing regions can be clicked */} |
118 | | - {this.renderCreator()} |
119 | | - {this.renderList()} |
120 | | - </> |
121 | | - ) : ( |
122 | | - <> |
123 | | - {this.renderList()} |
124 | | - {this.renderCreator()} |
125 | | - </> |
126 | | - )} |
| 19 | + return ( |
| 20 | + <RegionList |
| 21 | + activeId={activeAnnotationId} |
| 22 | + annotations={annotations} |
| 23 | + className="ba-RegionAnnotations-list" |
| 24 | + onSelect={handleAnnotationActive} |
| 25 | + /> |
| 26 | + ); |
| 27 | +}; |
127 | 28 |
|
128 | | - {/* Layer 3a: Staged (unsaved) annotation target, if any */} |
129 | | - {canCreate && staged && ( |
130 | | - <div className="ba-RegionAnnotations-target"> |
131 | | - <RegionRect ref={this.setRectRef} isActive shape={staged.shape} /> |
132 | | - </div> |
133 | | - )} |
134 | | - </> |
135 | | - ); |
136 | | - } |
137 | | -} |
| 29 | +export default RegionAnnotations; |
0 commit comments