Skip to content

Commit 1dba2e5

Browse files
Merge pull request #36 from ChristianEdwardPadilla/development
transformer
2 parents 8ad8eed + 0fe1f5c commit 1dba2e5

File tree

5 files changed

+82
-43
lines changed

5 files changed

+82
-43
lines changed

src/components/GrandchildRectangle.jsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import { Rect, Group } from 'react-konva';
44

55
class GrandchildRectangle extends Component {
66
getComponentColor(componentId) {
7-
console.log('siodfbsldfk', componentId);
87
const color = this.props.components.find(comp => comp.id == componentId).color;
9-
console.log(color);
10-
118
return color;
129
}
1310

1411
render() {
1512
const {
16-
color,
1713
x,
1814
y,
1915
scaleX,
@@ -51,8 +47,7 @@ class GrandchildRectangle extends Component {
5147
scaleY={1}
5248
width={width}
5349
height={height}
54-
stroke={color}
55-
color={this.getComponentColor(childComponentId)}
50+
stroke={this.getComponentColor(childComponentId)}
5651
// fill={color}
5752
// opacity={0.8}
5853
strokeWidth={4}
@@ -76,7 +71,6 @@ class GrandchildRectangle extends Component {
7671
scaleY={1}
7772
width={grandchild.position.width * (width / (window.innerWidth / 2))}
7873
height={grandchild.position.height * (height / window.innerHeight)}
79-
color={color}
8074
/>
8175
))}
8276
</Group>

src/components/KonvaStage.jsx

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { Component, createRef, Fragment } from 'react';
22
// import PropTypes from 'prop-types';
33
import {
4-
Stage, Layer, Group, Label, Text,
4+
Stage, Layer, Group, Label, Text, Rect, Transformer,
55
} from 'react-konva';
6-
import TransformerComponent from './TransformerComponent.jsx';
76
import Rectangle from './Rectangle.jsx';
7+
import TransformerComponent from './TransformerComponent.jsx';
88

99
class KonvaStage extends Component {
1010
constructor(props) {
@@ -34,8 +34,6 @@ class KonvaStage extends Component {
3434
checkSize = () => {
3535
const width = this.container.offsetWidth;
3636
const height = this.container.offsetHeight;
37-
console.log('setting width: ', this.state.stageWidth, width);
38-
console.log('setting height: ', this.state.heightWidth, height);
3937
this.setState({
4038
stageWidth: width,
4139
stageHeight: height,
@@ -46,24 +44,28 @@ class KonvaStage extends Component {
4644
// // clicked on stage - clear selection
4745
if (e.target === e.target.getStage()) {
4846
// add functionality for allowing no focusChild
49-
console.log(e.target.getStage());
47+
console.log('user clicked on canvas:');
5048
return;
5149
}
5250
// // clicked on transformer - do nothing
5351
const clickedOnTransformer = e.target.getParent().className === 'Transformer';
5452
if (clickedOnTransformer) {
53+
console.log('user clicked on transformer');
5554
return;
5655
}
5756

57+
if (e.target.attrs.className === 'componentRect') {
58+
console.log('user clicked on componentRect');
59+
}
60+
5861
// find clicked rect by its name
5962
const rectChildId = e.target.attrs.childId;
60-
console.log('e.target : ', rectChildId);
63+
console.log('user clicked on child rectangle with Id: ', rectChildId);
6164
this.props.changeFocusChild({ childId: rectChildId });
6265
this.props.changeComponentFocusChild({
6366
componentId: this.props.focusComponent.id,
6467
childId: rectChildId,
6568
});
66-
// put individual component's focus child logic here
6769
};
6870

6971
render() {
@@ -101,6 +103,38 @@ class KonvaStage extends Component {
101103
height={this.state.stageHeight - 10}
102104
>
103105
<Layer>
106+
{focusComponent.title !== 'App' && (
107+
<Group
108+
draggable={true}
109+
x={focusComponent.position.x}
110+
y={focusComponent.position.y}
111+
width={focusComponent.position.width}
112+
height={focusComponent.position.height}
113+
>
114+
<Rect
115+
className={'componentRect'}
116+
stroke={focusComponent.color}
117+
x={0}
118+
y={0}
119+
name={'-1'}
120+
width={focusComponent.position.width}
121+
height={focusComponent.position.height}
122+
strokeWidth={2}
123+
strokeScaleEnabled={false}
124+
/>
125+
<Label>
126+
<Text
127+
text={focusComponent.title}
128+
fill={focusComponent.color}
129+
fontStyle={'bold'}
130+
fontVariant={'small-caps'}
131+
fontSize={14}
132+
y={-15}
133+
/>
134+
</Label>
135+
<TransformerComponent rectClass={'componentRect'} />
136+
</Group>
137+
)}
104138
{components
105139
.find(comp => comp.id === focusComponent.id)
106140
.childrenArray.map((child, i) => (
@@ -119,7 +153,6 @@ class KonvaStage extends Component {
119153
width={child.position.width}
120154
height={child.position.height}
121155
title={child.componentName + child.childId}
122-
color={child.color}
123156
handleTransform={handleTransform}
124157
draggable={true}
125158
/>

src/components/Rectangle.jsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import GrandchildRectangle from './GrandchildRectangle.jsx';
88

99
class Rectangle extends Component {
1010
getComponentColor(componentId) {
11-
console.log('siodfbsldfk', componentId);
1211
const color = this.props.components.find(comp => comp.id == componentId).color;
13-
console.log(color);
14-
1512
return color;
1613
}
1714

@@ -42,7 +39,6 @@ class Rectangle extends Component {
4239

4340
render() {
4441
const {
45-
color,
4642
x,
4743
y,
4844
scaleX,
@@ -74,6 +70,7 @@ class Rectangle extends Component {
7470
>
7571
<Rect
7672
name={`${childId}`}
73+
className={'childRect'}
7774
x={0}
7875
y={0}
7976
// absolutePosition={{ x, y }}
@@ -84,8 +81,7 @@ class Rectangle extends Component {
8481
scaleY={1}
8582
width={width}
8683
height={height}
87-
stroke={color}
88-
color={this.getComponentColor(childComponentId)}
84+
stroke={this.getComponentColor(childComponentId)}
8985
// fill={color}
9086
// opacity={0.8}
9187
onTransformEnd={event => this.handleResize(componentId, childId, event.target)}
@@ -94,7 +90,15 @@ class Rectangle extends Component {
9490
draggable={false}
9591
/>
9692
<Label>
97-
<Text text={title} fill={'white'} />
93+
<Text
94+
text={title}
95+
fill={'white'}
96+
fontStyle={'bold'}
97+
fontVariant={'small-caps'}
98+
fontSize={10}
99+
x={4}
100+
y={4}
101+
/>
98102
</Label>
99103
{components
100104
.find(comp => comp.title === childComponentName)
@@ -114,14 +118,11 @@ class Rectangle extends Component {
114118
width={grandchild.position.width * (width / (window.innerWidth / 2))}
115119
height={grandchild.position.height * (height / window.innerHeight)}
116120
// title={child.componentName + child.childId}
117-
color={grandchild.color}
118121
/>
119122
))}
120-
{focusChild && focusChild.childId === childId && draggable ? (
121-
<TransformerComponent focusChild={focusChild} />
122-
) : (
123-
<Label />
124-
)}
123+
{focusChild
124+
&& focusChild.childId === childId
125+
&& draggable && <TransformerComponent focusChild={focusChild} rectClass={'childRect'} />}
125126
</Group>
126127
);
127128
}

src/components/TransformerComponent.jsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ import { Transformer } from 'react-konva';
44

55
export default class TransformerComponent extends Component {
66
componentDidMount() {
7-
this.checkNode();
7+
this.checkNode(this.props.rectClass);
88
}
99

1010
componentDidUpdate() {
11-
this.checkNode();
11+
this.checkNode(this.props.rectClass);
1212
}
1313

1414
// this function makes sure the transformer follows along with the focusChild
15-
checkNode() {
15+
checkNode(rectClass) {
1616
const stage = this.transformer.getStage();
1717
const { focusChild } = this.props;
18-
const selectedNode = stage.findOne(`.${focusChild.childId}`);
18+
19+
// depending on the rectClass prop, this transformer is either attached to
20+
// a childRect or the focused component's componentRect
21+
let selectedNode;
22+
if (rectClass === 'componentRect') {
23+
selectedNode = stage.findOne(`.${-1}`);
24+
} else if (rectClass === 'childRect') {
25+
selectedNode = stage.findOne(`.${focusChild.childId}`);
26+
} else {
27+
return;
28+
}
1929

2030
if (selectedNode === this.transformer.node()) {
2131
return;
@@ -32,10 +42,14 @@ export default class TransformerComponent extends Component {
3242
return (
3343
<Transformer
3444
rotateEnabled={false}
35-
onMouseUp={this.handleMouseUp}
3645
ref={(node) => {
3746
this.transformer = node;
3847
}}
48+
borderEnabled={false}
49+
anchorFill={'grey'}
50+
anchorStroke={'grey'}
51+
anchorSize={8}
52+
keepRatio={false}
3953
/>
4054
);
4155
}

src/utils/componentReducer.util.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const initialComponentState = {
1515
props: [],
1616
nextPropId: 0,
1717
position: {
18-
x: 110,
19-
y: 120,
20-
width: 50,
21-
height: 50,
18+
x: 50,
19+
y: 50,
20+
width: 600,
21+
height: 600,
2222
},
2323
childrenArray: [],
2424
nextChildId: 1,
@@ -170,9 +170,6 @@ export const deleteChild = (
170170
export const handleTransform = (state, {
171171
componentId, childId, x, y, width, height,
172172
}) => {
173-
// console.log('componentId and childId: ', componentId, childId);
174-
// console.log('state.focuscomponent: ', state.focusComponent);
175-
console.log('incoming x and y: ', x, y);
176173
const child = state.components
177174
.find(comp => comp.id === componentId)
178175
.childrenArray.find(child => child.childId === childId);
@@ -285,10 +282,10 @@ export const deleteComponent = (state, { componentId }) => {
285282

286283
export const changeFocusComponent = (state, { title }) => {
287284
const newFocusComp = state.components.find(comp => comp.title === title);
288-
// set the "focus child" to the focus child of this particular component .
289-
const newFocusChildId = newFocusComp.focusChildId ;
285+
// set the "focus child" to the focus child of this particular component .
286+
const newFocusChildId = newFocusComp.focusChildId;
290287

291-
const newFocusChild = newFocusComp.childrenArray.find( child => child.childId == newFocusChildId) ;
288+
const newFocusChild = newFocusComp.childrenArray.find(child => child.childId == newFocusChildId);
292289

293290
const result = getSelectable(newFocusComp, state.components);
294291

0 commit comments

Comments
 (0)