1
- import React , { Component } 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' ;
1
+ import React , { Component } from "react" ;
2
+ import { Rect , Group , Label , Text } from "react-konva" ;
3
+ import TransformerComponent from "./TransformerComponent.jsx" ;
4
+ import GrandchildRectangle from "./GrandchildRectangle.jsx" ;
7
5
8
6
class Rectangle extends Component {
9
7
getComponentColor ( componentId ) {
10
- const color = this . props . components . find ( comp => comp . id == componentId ) . color ;
8
+ const color = this . props . components . find ( comp => comp . id == componentId )
9
+ . color ;
11
10
return color ;
12
11
}
13
12
14
- handleResize ( componentId , childId , target ) {
13
+ handleResize ( componentId , childId , target , blockSnapSize ) {
15
14
const focChild = this . props . components
16
15
. find ( comp => comp . id === componentId )
17
16
. childrenArray . find ( child => child . childId === childId ) ;
18
17
19
18
const transformation = {
20
- width : target . width ( ) * target . scaleX ( ) ,
21
- height : target . height ( ) * target . scaleY ( ) ,
19
+ // width:
20
+ // Math.round((target.width() * target.scaleX()) / blockSnapSize) *
21
+ // blockSnapSize,
22
+ // height:
23
+ // Math.round((target.height() * target.scaleY()) / blockSnapSize) *
24
+ // blockSnapSize,
25
+ // x: target.x() + focChild.position.x,
26
+ // y: target.y() + focChild.position.y
27
+
28
+ width :
29
+ Math . round ( ( target . width ( ) * target . scaleX ( ) ) / blockSnapSize ) *
30
+ blockSnapSize ,
31
+ height :
32
+ Math . round ( ( target . height ( ) * target . scaleY ( ) ) / blockSnapSize ) *
33
+ blockSnapSize ,
22
34
x : target . x ( ) + focChild . position . x ,
23
- y : target . y ( ) + focChild . position . y ,
35
+ y : target . y ( ) + focChild . position . y
24
36
} ;
25
37
26
38
this . props . handleTransform ( componentId , childId , transformation ) ;
27
39
}
28
40
29
- handleDrag ( componentId , childId , target ) {
41
+ handleDrag ( componentId , childId , target , blockSnapSize ) {
30
42
console . log ( target ) ;
43
+ console . log ( "blockSnapSize" , blockSnapSize ) ;
31
44
32
45
const transformation = {
33
- x : target . x ( ) ,
34
- y : target . y ( ) ,
46
+ // x: target.x(),
47
+ // y: target.y()
48
+ x : Math . round ( target . x ( ) / blockSnapSize ) * blockSnapSize ,
49
+ y : Math . round ( target . y ( ) / blockSnapSize ) * blockSnapSize
35
50
} ;
51
+ console . log ( transformation ) ;
36
52
this . props . handleTransform ( componentId , childId , transformation ) ;
37
53
}
38
54
@@ -52,6 +68,7 @@ class Rectangle extends Component {
52
68
focusChild,
53
69
components,
54
70
draggable,
71
+ blockSnapSize
55
72
} = this . props ;
56
73
57
74
// the Group is responsible for dragging of all children
@@ -65,11 +82,13 @@ class Rectangle extends Component {
65
82
scaleY = { scaleY }
66
83
width = { width }
67
84
height = { height }
68
- onDragEnd = { event => this . handleDrag ( componentId , childId , event . target ) }
85
+ onDragEnd = { event =>
86
+ this . handleDrag ( componentId , childId , event . target , blockSnapSize )
87
+ }
69
88
>
70
89
< Rect
71
90
name = { `${ childId } ` }
72
- className = { ' childRect' }
91
+ className = { " childRect" }
73
92
x = { 0 }
74
93
y = { 0 }
75
94
childId = { childId }
@@ -82,39 +101,47 @@ class Rectangle extends Component {
82
101
stroke = { this . getComponentColor ( childComponentId ) }
83
102
// fill={color}
84
103
// opacity={0.8}
85
- onTransformEnd = { event => this . handleResize ( componentId , childId , event . target ) }
104
+ onTransformEnd = { event =>
105
+ this . handleResize ( componentId , childId , event . target , blockSnapSize )
106
+ }
86
107
strokeWidth = { 4 }
87
108
strokeScaleEnabled = { false }
88
109
draggable = { false }
89
- dashEnabled = { childId === '-1' } // dash line only enabled for pseudochild
90
- dash = { [ 10 , 3 ] } // 10px dashes with 3px gaps
110
+ fill = { childId === "-1" ? "white" : null }
111
+ shadowBlur = { childId === "-1" ? 6 : null }
112
+ // dashEnabled={childId === "-1"} // dash line only enabled for pseudochild
113
+ // dash={[10, 3]} // 10px dashes with 3px gaps
91
114
/>
92
115
< Label >
93
116
< Text
94
- fontStyle = { ' bold' }
95
- fontVariant = { ' small-caps' }
117
+ fontStyle = { " bold" }
118
+ fontVariant = { " small-caps" }
96
119
// pseudochild's label should look different than normal children:
97
- text = { childId === '-1' ? title . slice ( 0 , title . length - 2 ) : title }
98
- fill = { childId === '-1' ? this . getComponentColor ( childComponentId ) : 'white' }
99
- fontSize = { childId === '-1' ? 15 : 10 }
120
+ text = { childId === "-1" ? title . slice ( 0 , title . length - 2 ) : title }
121
+ fill = {
122
+ childId === "-1"
123
+ ? this . getComponentColor ( childComponentId )
124
+ : "black"
125
+ }
126
+ fontSize = { childId === "-1" ? 15 : 10 }
100
127
x = { 4 }
101
- y = { childId === '-1' ? - 15 : 5 }
128
+ y = { childId === "-1" ? - 15 : 5 }
102
129
/>
103
130
</ Label >
104
- { focusChild
105
- && focusChild . childId === childId
106
- && draggable && (
131
+ { focusChild &&
132
+ focusChild . childId === childId &&
133
+ draggable && (
107
134
< TransformerComponent
108
135
focusChild = { focusChild }
109
- rectClass = { ' childRect' }
136
+ rectClass = { " childRect" }
110
137
anchorSize = { 8 }
111
- color = { ' grey' }
138
+ color = { " grey" }
112
139
/>
113
- ) }
114
- { childId !== '-1'
115
- && components
140
+ ) }
141
+ { childId !== "-1" &&
142
+ components
116
143
. find ( comp => comp . title === childComponentName )
117
- . childrenArray . filter ( child => child . childId !== '-1' )
144
+ . childrenArray . filter ( child => child . childId !== "-1" )
118
145
. map ( ( grandchild , i ) => (
119
146
< GrandchildRectangle
120
147
key = { i }
@@ -128,8 +155,12 @@ class Rectangle extends Component {
128
155
y = { grandchild . position . y * ( height / window . innerHeight ) }
129
156
scaleX = { 1 }
130
157
scaleY = { 1 }
131
- width = { grandchild . position . width * ( width / ( window . innerWidth / 2 ) ) }
132
- height = { grandchild . position . height * ( height / window . innerHeight ) }
158
+ width = {
159
+ grandchild . position . width * ( width / ( window . innerWidth / 2 ) )
160
+ }
161
+ height = {
162
+ grandchild . position . height * ( height / window . innerHeight )
163
+ }
133
164
// title={child.componentName + child.childId}
134
165
/>
135
166
) ) }
0 commit comments