11// @flow weak
22
3- import React , { Fragment } from "react"
3+ import React , { Fragment , memo } from "react"
44import HighlightBox from "../HighlightBox"
55import { styled } from "@material-ui/core/styles"
66import PreventScrollToParents from "../PreventScrollToParents"
7+ import { shallowEqualObjects } from "shallow-equal"
78
89const TransformGrabber = styled ( "div" ) ( {
910 width : 8 ,
@@ -19,148 +20,160 @@ const boxCursorMap = [
1920 [ "sw-resize" , "s-resize" , "se-resize" ] ,
2021]
2122
22- export const RegionSelectAndTransformBoxes = ( {
23- regions,
24- mouseEvents,
25- projectRegionBox,
26- dragWithPrimary,
27- createWithPrimary,
28- zoomWithPrimary,
29- onBeginMovePoint,
30- onSelectRegion,
31- layoutParams,
32- mat,
33- onBeginBoxTransform,
34- onBeginMovePolygonPoint,
35- onAddPolygonPoint,
36- showHighlightBox,
37- } ) => {
38- return regions
39- . filter ( ( r ) => r . visible || r . visible === undefined )
40- . filter ( ( r ) => ! r . locked )
41- . map ( ( r , i ) => {
42- const pbox = projectRegionBox ( r )
43- const { iw, ih } = layoutParams . current
44- return (
45- < Fragment key = { r . id } >
46- < PreventScrollToParents >
47- { showHighlightBox && (
48- < HighlightBox
49- region = { r }
50- mouseEvents = { mouseEvents }
51- dragWithPrimary = { dragWithPrimary }
52- createWithPrimary = { createWithPrimary }
53- zoomWithPrimary = { zoomWithPrimary }
54- onBeginMovePoint = { onBeginMovePoint }
55- onSelectRegion = { onSelectRegion }
56- pbox = { pbox }
23+ const arePropsEqual = ( prev , next ) => {
24+ return (
25+ shallowEqualObjects ( prev . region , next . region ) &&
26+ prev . dragWithPrimary === next . dragWithPrimary &&
27+ prev . createWithPrimary === next . createWithPrimary &&
28+ prev . zoomWithPrimary === next . zoomWithPrimary &&
29+ prev . layoutParams . current . iw === next . layoutParams . current . iw &&
30+ prev . layoutParams . current . ih === next . layoutParams . current . ih &&
31+ prev . mat === next . mat
32+ )
33+ }
34+
35+ export const RegionSelectAndTransformBox = memo (
36+ ( {
37+ region : r ,
38+ mouseEvents,
39+ projectRegionBox,
40+ dragWithPrimary,
41+ createWithPrimary,
42+ zoomWithPrimary,
43+ onBeginMovePoint,
44+ onSelectRegion,
45+ layoutParams,
46+ mat,
47+ onBeginBoxTransform,
48+ onBeginMovePolygonPoint,
49+ onAddPolygonPoint,
50+ showHighlightBox,
51+ } ) => {
52+ const pbox = projectRegionBox ( r )
53+ const { iw, ih } = layoutParams . current
54+ return (
55+ < Fragment >
56+ < PreventScrollToParents >
57+ { showHighlightBox && (
58+ < HighlightBox
59+ region = { r }
60+ mouseEvents = { mouseEvents }
61+ dragWithPrimary = { dragWithPrimary }
62+ createWithPrimary = { createWithPrimary }
63+ zoomWithPrimary = { zoomWithPrimary }
64+ onBeginMovePoint = { onBeginMovePoint }
65+ onSelectRegion = { onSelectRegion }
66+ pbox = { pbox }
67+ />
68+ ) }
69+ { r . type === "box" &&
70+ ! dragWithPrimary &&
71+ ! zoomWithPrimary &&
72+ ! r . locked &&
73+ r . highlighted &&
74+ mat . a < 1.2 &&
75+ [
76+ [ 0 , 0 ] ,
77+ [ 0.5 , 0 ] ,
78+ [ 1 , 0 ] ,
79+ [ 1 , 0.5 ] ,
80+ [ 1 , 1 ] ,
81+ [ 0.5 , 1 ] ,
82+ [ 0 , 1 ] ,
83+ [ 0 , 0.5 ] ,
84+ [ 0.5 , 0.5 ] ,
85+ ] . map ( ( [ px , py ] , i ) => (
86+ < TransformGrabber
87+ key = { i }
88+ { ...mouseEvents }
89+ onMouseDown = { ( e ) => {
90+ if ( e . button === 0 )
91+ return onBeginBoxTransform ( r , [ px * 2 - 1 , py * 2 - 1 ] )
92+ mouseEvents . onMouseDown ( e )
93+ } }
94+ style = { {
95+ left : pbox . x - 4 - 2 + pbox . w * px ,
96+ top : pbox . y - 4 - 2 + pbox . h * py ,
97+ cursor : boxCursorMap [ py * 2 ] [ px * 2 ] ,
98+ borderRadius : px === 0.5 && py === 0.5 ? 4 : undefined ,
99+ } }
57100 />
58- ) }
59- { r . type === "box" &&
60- ! dragWithPrimary &&
61- ! zoomWithPrimary &&
62- ! r . locked &&
63- r . highlighted &&
64- mat . a < 1.2 &&
65- [
66- [ 0 , 0 ] ,
67- [ 0.5 , 0 ] ,
68- [ 1 , 0 ] ,
69- [ 1 , 0.5 ] ,
70- [ 1 , 1 ] ,
71- [ 0.5 , 1 ] ,
72- [ 0 , 1 ] ,
73- [ 0 , 0.5 ] ,
74- [ 0.5 , 0.5 ] ,
75- ] . map ( ( [ px , py ] , i ) => (
101+ ) ) }
102+ { r . type === "polygon" &&
103+ ! dragWithPrimary &&
104+ ! zoomWithPrimary &&
105+ ! r . locked &&
106+ r . highlighted &&
107+ r . points . map ( ( [ px , py ] , i ) => {
108+ const proj = mat
109+ . clone ( )
110+ . inverse ( )
111+ . applyToPoint ( px * iw , py * ih )
112+ return (
76113 < TransformGrabber
77114 key = { i }
78115 { ...mouseEvents }
79116 onMouseDown = { ( e ) => {
80- if ( e . button === 0 )
81- return onBeginBoxTransform ( r , [ px * 2 - 1 , py * 2 - 1 ] )
117+ if ( e . button === 0 && ( ! r . open || i === 0 ) )
118+ return onBeginMovePolygonPoint ( r , i )
82119 mouseEvents . onMouseDown ( e )
83120 } }
84121 style = { {
85- left : pbox . x - 4 - 2 + pbox . w * px ,
86- top : pbox . y - 4 - 2 + pbox . h * py ,
87- cursor : boxCursorMap [ py * 2 ] [ px * 2 ] ,
88- borderRadius : px === 0.5 && py === 0.5 ? 4 : undefined ,
122+ cursor : ! r . open ? "move" : i === 0 ? "pointer" : undefined ,
123+ pointerEvents :
124+ r . open && i === r . points . length - 1 ? "none" : undefined ,
125+ left : proj . x - 4 ,
126+ top : proj . y - 4 ,
89127 } }
90128 />
91- ) ) }
92- { r . type === "polygon" &&
93- ! dragWithPrimary &&
94- ! zoomWithPrimary &&
95- ! r . locked &&
96- r . highlighted &&
97- r . points . map ( ( [ px , py ] , i ) => {
129+ )
130+ } ) }
131+ { r . type === "polygon" &&
132+ r . highlighted &&
133+ ! dragWithPrimary &&
134+ ! zoomWithPrimary &&
135+ ! r . locked &&
136+ ! r . open &&
137+ r . points . length > 1 &&
138+ r . points
139+ . map ( ( p1 , i ) => [ p1 , r . points [ ( i + 1 ) % r . points . length ] ] )
140+ . map ( ( [ p1 , p2 ] ) => [ ( p1 [ 0 ] + p2 [ 0 ] ) / 2 , ( p1 [ 1 ] + p2 [ 1 ] ) / 2 ] )
141+ . map ( ( pa , i ) => {
98142 const proj = mat
99143 . clone ( )
100144 . inverse ( )
101- . applyToPoint ( px * iw , py * ih )
145+ . applyToPoint ( pa [ 0 ] * iw , pa [ 1 ] * ih )
102146 return (
103147 < TransformGrabber
104148 key = { i }
105149 { ...mouseEvents }
106150 onMouseDown = { ( e ) => {
107- if ( e . button === 0 && ( ! r . open || i === 0 ) )
108- return onBeginMovePolygonPoint ( r , i )
151+ if ( e . button === 0 ) return onAddPolygonPoint ( r , pa , i + 1 )
109152 mouseEvents . onMouseDown ( e )
110153 } }
111154 style = { {
112- cursor : ! r . open
113- ? "move"
114- : i === 0
115- ? "pointer"
116- : undefined ,
117- pointerEvents :
118- r . open && i === r . points . length - 1
119- ? "none"
120- : undefined ,
155+ cursor : "copy" ,
121156 left : proj . x - 4 ,
122157 top : proj . y - 4 ,
158+ border : "2px dotted #fff" ,
159+ opacity : 0.5 ,
123160 } }
124161 />
125162 )
126163 } ) }
127- { r . type === "polygon" &&
128- r . highlighted &&
129- ! dragWithPrimary &&
130- ! zoomWithPrimary &&
131- ! r . locked &&
132- ! r . open &&
133- r . points . length > 1 &&
134- r . points
135- . map ( ( p1 , i ) => [ p1 , r . points [ ( i + 1 ) % r . points . length ] ] )
136- . map ( ( [ p1 , p2 ] ) => [ ( p1 [ 0 ] + p2 [ 0 ] ) / 2 , ( p1 [ 1 ] + p2 [ 1 ] ) / 2 ] )
137- . map ( ( pa , i ) => {
138- const proj = mat
139- . clone ( )
140- . inverse ( )
141- . applyToPoint ( pa [ 0 ] * iw , pa [ 1 ] * ih )
142- return (
143- < TransformGrabber
144- key = { i }
145- { ...mouseEvents }
146- onMouseDown = { ( e ) => {
147- if ( e . button === 0 )
148- return onAddPolygonPoint ( r , pa , i + 1 )
149- mouseEvents . onMouseDown ( e )
150- } }
151- style = { {
152- cursor : "copy" ,
153- left : proj . x - 4 ,
154- top : proj . y - 4 ,
155- border : "2px dotted #fff" ,
156- opacity : 0.5 ,
157- } }
158- />
159- )
160- } ) }
161- </ PreventScrollToParents >
162- </ Fragment >
163- )
164+ </ PreventScrollToParents >
165+ </ Fragment >
166+ )
167+ } ,
168+ arePropsEqual
169+ )
170+
171+ export const RegionSelectAndTransformBoxes = ( props ) => {
172+ return props . regions
173+ . filter ( ( r ) => r . visible || r . visible === undefined )
174+ . filter ( ( r ) => ! r . locked )
175+ . map ( ( r , i ) => {
176+ return < RegionSelectAndTransformBox key = { r . id } { ...props } region = { r } />
164177 } )
165178}
166179
0 commit comments