1- import React , { Component , Fragment } from "react" ;
2- import { Rect , Group , Label , Text } from "react-konva" ;
3- import TransformerComponent from "./TransformerComponent.jsx" ;
4- import GrandchildRectangle from "./GrandchildRectangle.jsx" ;
1+ import React , { Component , Fragment } from 'react' ;
2+ import {
3+ Rect , Group , Label , Text ,
4+ } from 'react-konva' ;
5+ import TransformerComponent from './TransformerComponent.jsx' ;
6+ import GrandchildRectangle from './GrandchildRectangle.jsx' ;
57
68class Rectangle extends Component {
79 state = {
8- rectImage : null
10+ image : null ,
11+ imageHeight : 0 ,
12+ imageWidth : 0 ,
913 } ;
1014
1115 getComponentColor ( componentId ) {
12- if ( componentId === "888" ) {
13- return "#000000" ;
14- }
15- const color = this . props . components . find ( comp => comp . id === componentId )
16- . color ;
16+ const color = this . props . components . find ( comp => comp . id === componentId ) . color ;
1717 return color ;
1818 }
1919
2020 getPseudoChild ( ) {
21- return this . props . components . find (
22- comp => comp . id === this . props . childComponentId
23- ) ;
21+ return this . props . components . find ( comp => comp . id === this . props . childComponentId ) ;
2422 }
2523
2624 handleResize ( componentId , childId , target , blockSnapSize ) {
@@ -31,19 +29,13 @@ class Rectangle extends Component {
3129 . childrenArray . find ( child => child . childId === childId ) ;
3230
3331 if ( childId === - 1 ) {
34- focChild = this . props . components . find (
35- comp => comp . id === this . props . componentId
36- ) ;
32+ focChild = this . props . components . find ( comp => comp . id === this . props . componentId ) ;
3733 }
3834 const transformation = {
39- width :
40- Math . round ( ( target . width ( ) * target . scaleX ( ) ) / blockSnapSize ) *
41- blockSnapSize ,
42- height :
43- Math . round ( ( target . height ( ) * target . scaleY ( ) ) / blockSnapSize ) *
44- blockSnapSize ,
35+ width : Math . round ( ( target . width ( ) * target . scaleX ( ) ) / blockSnapSize ) * blockSnapSize ,
36+ height : Math . round ( ( target . height ( ) * target . scaleY ( ) ) / blockSnapSize ) * blockSnapSize ,
4537 x : target . x ( ) + focChild . position . x ,
46- y : target . y ( ) + focChild . position . y
38+ y : target . y ( ) + focChild . position . y ,
4739 } ;
4840
4941 this . props . handleTransform ( componentId , childId , transformation ) ;
@@ -52,19 +44,17 @@ class Rectangle extends Component {
5244 handleDrag ( componentId , childId , target , blockSnapSize ) {
5345 const transformation = {
5446 x : Math . round ( target . x ( ) / blockSnapSize ) * blockSnapSize ,
55- y : Math . round ( target . y ( ) / blockSnapSize ) * blockSnapSize
47+ y : Math . round ( target . y ( ) / blockSnapSize ) * blockSnapSize ,
5648 } ;
5749 this . props . handleTransform ( componentId , childId , transformation ) ;
5850 }
5951
60- setImage = imageSource => {
52+ setImage = ( imageSource ) => {
6153 if ( ! imageSource ) return ;
6254 const image = new window . Image ( ) ;
63-
6455 image . src = imageSource ;
65- // if there was an error grtting img; heigth should b Zero
6656 if ( ! image . height ) return null ;
67- return image ;
57+ this . setState ( { image } ) ;
6858 } ;
6959
7060 render ( ) {
@@ -85,7 +75,7 @@ class Rectangle extends Component {
8575 draggable,
8676 blockSnapSize,
8777 childType,
88- imageSource
78+ imageSource,
8979 } = this . props ;
9080
9181 // the Group is responsible for dragging of all children
@@ -99,22 +89,20 @@ class Rectangle extends Component {
9989 scaleY = { scaleY }
10090 width = { width }
10191 height = { height }
102- onDragEnd = { event =>
103- this . handleDrag ( componentId , childId , event . target , blockSnapSize )
104- }
105- ref = { node => {
92+ onDragEnd = { event => this . handleDrag ( componentId , childId , event . target , blockSnapSize ) }
93+ ref = { ( node ) => {
10694 this . group = node ;
10795 } }
10896 tabIndex = "0" // required for keypress event to be heard by this.group
10997 >
11098 < Rect
11199 // a Konva Rect is generated for each child of the focusComponent (including the pseudochild, representing the focusComponent itself)
112- ref = { node => {
100+ ref = { ( node ) => {
113101 this . rect = node ;
114102 } }
115103 tabIndex = "0" // required for keypress event to be heard by this.group
116104 name = { `${ childId } ` }
117- className = { " childRect" }
105+ className = { ' childRect' }
118106 x = { 0 }
119107 y = { 0 }
120108 childId = { childId }
@@ -124,47 +112,34 @@ class Rectangle extends Component {
124112 scaleY = { 1 }
125113 width = { width }
126114 height = { height }
127- stroke = {
128- childType === "COMP"
129- ? this . getComponentColor ( childComponentId )
130- : "#000000"
131- }
132- // fill={color}
133- // opacity={0.8}
134- onTransformEnd = { event =>
135- this . handleResize ( componentId , childId , event . target , blockSnapSize )
115+ stroke = { childType === 'COMP' ? this . getComponentColor ( childComponentId ) : '#000000' }
116+ onTransformEnd = { event => this . handleResize ( componentId , childId , event . target , blockSnapSize )
136117 }
137- strokeWidth = { childType === " COMP" ? 4 : 1 }
118+ strokeWidth = { childType === ' COMP' ? 4 : 2 }
138119 strokeScaleEnabled = { false }
139120 draggable = { false }
140- fill = { childId === - 1 ? " white" : null }
121+ fill = { childId === - 1 ? ' white' : null }
141122 shadowBlur = { childId === - 1 ? 6 : null }
142- fillPatternImage = { imageSource ? this . setImage ( imageSource ) : null }
143-
144- // fillPatternImage={null}
145- // dashEnabled={childId === "-1"} // dash line only enabled for pseudochild
146- // dash={[10, 3]} // 10px dashes with 3px gaps
123+ fillPatternImage = { this . state . image ? this . state . image : this . setImage ( imageSource ) }
124+ fillPatternScaleX = { this . state . image ? width / this . state . image . width : 1 }
125+ fillPatternScaleY = { this . state . image ? height / this . state . image . height : 1 }
147126 />
148127 < Label >
149128 < Text
150- fontStyle = { " bold" }
151- fontVariant = { " small-caps" }
129+ fontStyle = { ' bold' }
130+ fontVariant = { ' small-caps' }
152131 // pseudochild's label should look different than normal children:
153132 text = { childId === - 1 ? title . slice ( 0 , title . length - 2 ) : title }
154- fill = {
155- childId === - 1
156- ? this . getComponentColor ( childComponentId )
157- : "#000000"
158- }
133+ fill = { childId === - 1 ? this . getComponentColor ( childComponentId ) : '#000000' }
159134 fontSize = { childId === - 1 ? 15 : 10 }
160135 x = { 4 }
161136 y = { childId === - 1 ? - 20 : - 12 }
162137 />
163138 </ Label >
164139 { // for all children other than the pseudoChild, find their component's children array and recursively render the children found there
165- childId !== - 1 &&
166- childType === " COMP" &&
167- components
140+ childId !== - 1
141+ && childType === ' COMP'
142+ && components
168143 . find ( comp => comp . title === childComponentName )
169144 . childrenArray . filter ( child => child . childId !== - 1 )
170145 . map ( ( grandchild , i ) => (
@@ -174,43 +149,35 @@ class Rectangle extends Component {
174149 componentId = { componentId }
175150 directParentName = { childComponentName }
176151 childType = { grandchild . childType }
177- imageSource = {
178- grandchild . htmlElement == "Image" && grandchild . HTMLInfo . Src
179- ? grandchild . HTMLInfo . Src
180- : null
181- }
152+ imageSource = { grandchild . htmlElement === 'Image' && grandchild . HTMLInfo . Src }
182153 childComponentName = { grandchild . componentName }
183154 childComponentId = { grandchild . childComponentId }
184155 focusChild = { focusChild }
185156 childId = { childId } // scary addition, grandchildren rects default to childId of "direct" children
186- width = {
187- grandchild . position . width *
188- ( width / this . getPseudoChild ( ) . position . width )
189- }
157+ width = { grandchild . position . width * ( width / this . getPseudoChild ( ) . position . width ) }
190158 height = {
191- grandchild . position . height *
192- ( height / this . getPseudoChild ( ) . position . height )
159+ grandchild . position . height * ( height / this . getPseudoChild ( ) . position . height )
193160 }
194161 x = {
195- ( grandchild . position . x - this . getPseudoChild ( ) . position . x ) *
196- ( width / this . getPseudoChild ( ) . position . width )
162+ ( grandchild . position . x - this . getPseudoChild ( ) . position . x )
163+ * ( width / this . getPseudoChild ( ) . position . width )
197164 }
198165 y = {
199- ( grandchild . position . y - this . getPseudoChild ( ) . position . y ) *
200- ( height / this . getPseudoChild ( ) . position . height )
166+ ( grandchild . position . y - this . getPseudoChild ( ) . position . y )
167+ * ( height / this . getPseudoChild ( ) . position . height )
201168 }
202169 />
203170 ) ) }
204- { focusChild &&
205- focusChild . childId === childId &&
206- draggable && (
171+ { focusChild
172+ && focusChild . childId === childId
173+ && draggable && (
207174 < TransformerComponent
208175 focusChild = { focusChild }
209- rectClass = { " childRect" }
176+ rectClass = { ' childRect' }
210177 anchorSize = { 8 }
211- color = { " grey" }
178+ color = { ' grey' }
212179 />
213- ) }
180+ ) }
214181 </ Group >
215182 ) ;
216183 }
0 commit comments