Skip to content

Commit 227b399

Browse files
committed
committing konva type changes
1 parent 1688c1f commit 227b399

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

src/components/Rectangle.tsx

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import TransformerComponent from './TransformerComponent';
55
import GrandchildRectangle from './GrandchildRectangle';
66
import { ComponentsInt, ChildInt } from '../utils/Interfaces';
77
import { ComponentInt } from '../utils/Interfaces';
8+
import KonvaStage from './KonvaStage';
89

910
interface PropsInt {
1011
x: number;
@@ -27,8 +28,10 @@ interface PropsInt {
2728
image: HTMLImageElement;
2829
}
2930

30-
3131
class Rectangle extends Component<PropsInt> {
32+
rect: Konva.Rect;
33+
group: Konva.Group;
34+
3235
//This assigns the color to the Rect based on componentId's color in the state
3336
getComponentColor(componentId: number) {
3437
const color = this.props.components.find(
@@ -46,7 +49,12 @@ class Rectangle extends Component<PropsInt> {
4649
}
4750

4851
//resize function
49-
handleResize(componentId: number, childId: number, target: Konva.Group, blockSnapSize: number) {
52+
handleResize(
53+
componentId: number,
54+
childId: number,
55+
target: Konva.Group,
56+
blockSnapSize: number
57+
) {
5058
//find the id of the component where the componentID in the state equals the currently focused component
5159
//and then find the numberID for that component
5260
//So, this would be assigning "Container 1", with component ID being whatever the ID for "Container" is
@@ -65,15 +73,15 @@ class Rectangle extends Component<PropsInt> {
6573

6674
//The math here is easier than it looks
6775
//Basically, the height and width is first rounded up to a whole number (behind the whole snapping phenomenon)
68-
//after scaling it by multiplying it by scaleX/Y (height and width are the original height and width, the scale is the CHANGE in w/h),
76+
//after scaling it by multiplying it by scaleX/Y (height and width are the original height and width, the scale is the CHANGE in w/h),
6977
//dividing it by the 'snapsize' or grid unit area (which is 10x10 for this entire application) and re-multiplied by
7078
//that same snapsize. I'm not entirely sure why this had to be divided before the rounding, and re-multiplied,
71-
//since having positions that aren't a whole number doesn't seem to be that big of a deal.
79+
//since having positions that aren't a whole number doesn't seem to be that big of a deal.
7280

7381
//So there's a bit of redundancy in the x and y info. target.x() or y() only log the CHANGE of position of the Rect component.
74-
//Since when you change the width or height of the component you do not change the actual position, the
82+
//Since when you change the width or height of the component you do not change the actual position, the
7583
//value for x an y dispatched to the action creator will always be the same as the current x,y position, unless you can somehow
76-
//resize AND reposition at the same time.
84+
//resize AND reposition at the same time.
7785
const transformation = {
7886
width:
7987
Math.round((target.width() * target.scaleX()) / blockSnapSize) *
@@ -87,9 +95,14 @@ class Rectangle extends Component<PropsInt> {
8795
this.props.handleTransform(componentId, childId, transformation);
8896
}
8997

90-
//mostly the same logic as above, just grabbing the change in position for the focused child and sending it
98+
//mostly the same logic as above, just grabbing the change in position for the focused child and sending it
9199
//to the action creator.
92-
handleDrag(componentId: number, childId: number, target: Konva.Group, blockSnapSize: number) {
100+
handleDrag(
101+
componentId: number,
102+
childId: number,
103+
target: Konva.Group,
104+
blockSnapSize: number
105+
) {
93106
const transformation = {
94107
x: Math.round(target.x() / blockSnapSize) * blockSnapSize,
95108
y: Math.round(target.y() / blockSnapSize) * blockSnapSize
@@ -131,7 +144,8 @@ class Rectangle extends Component<PropsInt> {
131144
onDragEnd={event =>
132145
this.handleDrag(componentId, childId, event.target, blockSnapSize)
133146
}
134-
ref={node => { //this refference actually isn't doing anything since it isn't within the transformer component
147+
ref={node => {
148+
//this refference actually isn't doing anything since it isn't within the transformer component
135149
this.group = node;
136150
}}
137151
tabIndex='0' // required for keypress event to be heard by this.group
@@ -161,18 +175,22 @@ class Rectangle extends Component<PropsInt> {
161175
onTransformEnd={event =>
162176
this.handleResize(componentId, childId, event.target, blockSnapSize)
163177
}
164-
strokeWidth={childType === 'COMP' ? 4 : 2}
178+
strokeWidth={childType === 'COMP' ? 4 : 2}
165179
strokeScaleEnabled={false}
166180
draggable={false}
167181
fill={null}
168182
shadowBlur={childId === -1 ? 6 : null}
169-
fillPatternImage={childId === -1 ? this.props.image : null} //spooky addition, image if uploaded will only be background of App component
170-
fillPatternScaleX={this.props.image ? width / this.props.image.width : 1} //here we are making sure the width of the image will stretch of shrink
171-
fillPatternScaleY={this.props.image? height / this.props.image.height : 1} //based on the width or height of the App component
183+
fillPatternImage={childId === -1 ? this.props.image : null} //spooky addition, image if uploaded will only be background of App component
184+
fillPatternScaleX={
185+
this.props.image ? width / this.props.image.width : 1
186+
} //here we are making sure the width of the image will stretch of shrink
187+
fillPatternScaleY={
188+
this.props.image ? height / this.props.image.height : 1
189+
} //based on the width or height of the App component
172190
_useStrictMode
173191
/>
174192
<Label>
175-
<Text //this is just the text that goes above each Rect,
193+
<Text //this is just the text that goes above each Rect,
176194
fontStyle={'bold'}
177195
fontVariant={'small-caps'}
178196
// pseudochild's label should look different than normal children:
@@ -189,12 +207,12 @@ class Rectangle extends Component<PropsInt> {
189207
</Label>
190208
{// for all children other than the pseudoChild, find their component's children array and recursively render the children found there
191209
childId !== -1 && //inline conditional to check if a child exists
192-
childType === 'COMP' && //inline conditional to see if the child is a component, not an HTML element
210+
childType === 'COMP' && //inline conditional to see if the child is a component, not an HTML element
193211
components //map all 'grandchildren' in the child on display to this new component
194212
.find((comp: ComponentInt) => comp.title === childComponentName)
195213
.childrenArray.filter((child: ChildInt) => child.childId !== -1)
196214
.map((grandchild: ChildInt, i: number) => (
197-
<GrandchildRectangle
215+
<GrandchildRectangle
198216
key={i}
199217
components={components}
200218
componentId={componentId}
@@ -204,15 +222,17 @@ class Rectangle extends Component<PropsInt> {
204222
childComponentId={grandchild.childComponentId}
205223
focusChild={focusChild}
206224
childId={childId} // scary addition, grandchildren rects default to childId of "direct" children
207-
width={ //this is the logic used to display the grandchildren with proper scaling based on the parent (technically child) h/w
225+
width={
226+
//this is the logic used to display the grandchildren with proper scaling based on the parent (technically child) h/w
208227
grandchild.position.width *
209228
(width / this.getPseudoChild().position.width)
210229
}
211230
height={
212231
grandchild.position.height *
213232
(height / this.getPseudoChild().position.height)
214233
}
215-
x={ //similar logic to above
234+
x={
235+
//similar logic to above
216236
(grandchild.position.x - this.getPseudoChild().position.x) *
217237
(width / this.getPseudoChild().position.width)
218238
}
@@ -222,14 +242,16 @@ class Rectangle extends Component<PropsInt> {
222242
}
223243
/>
224244
))}
225-
{focusChild && focusChild.childId === childId && draggable && ( //this conditional logic binds the transformer to the focused child, and Draggable is checked to make sure grandchildren can't be selected
226-
<TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
227-
focusChild={focusChild}
228-
rectClass={'childRect'}
229-
anchorSize={8}
230-
color={'grey'}
231-
/>
232-
)}
245+
{focusChild &&
246+
focusChild.childId === childId &&
247+
draggable && ( //this conditional logic binds the transformer to the focused child, and Draggable is checked to make sure grandchildren can't be selected
248+
<TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
249+
focusChild={focusChild}
250+
rectClass={'childRect'}
251+
anchorSize={8}
252+
color={'grey'}
253+
/>
254+
)}
233255
</Group>
234256
);
235257
}

0 commit comments

Comments
 (0)