Skip to content

Commit d927ede

Browse files
Merge pull request #23 from ChristianEdwardPadilla/development
Features related to components and rendering
2 parents ae9a7ca + 6731eff commit d927ede

File tree

8 files changed

+132
-101
lines changed

8 files changed

+132
-101
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { Component, Fragment } from 'react';
2+
import { Rect } from 'react-konva';
3+
// import PropTypes from 'prop-types';
4+
5+
class GrandchildRectangle extends Component {
6+
render() {
7+
return <Rect />;
8+
}
9+
}
10+
11+
export default GrandchildRectangle;

src/components/KonvaStage.jsx

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { Component, createRef } from 'react';
1+
import React, { Component, createRef, Fragment } from 'react';
22
// import PropTypes from 'prop-types';
33
import {
4-
Stage, Layer, Image, Group,
4+
Stage, Layer, Group, Label, Text,
55
} from 'react-konva';
66
import TransformerComponent from './TransformerComponent.jsx';
77
import Rectangle from './Rectangle.jsx';
@@ -64,38 +64,26 @@ class KonvaStage extends Component {
6464
height={window.innerHeight}
6565
>
6666
<Layer>
67-
<Group
68-
scaleX={scaleX}
69-
scaleY={scaleY}
70-
ref={(node) => {
71-
this.group = node;
72-
}}
73-
draggable={draggable}
74-
>
75-
{components
76-
.find(comp => comp.title === focusComponent.title)
77-
.childrenArray.map((child, i) => (
78-
<Rectangle
79-
draggable={child.draggable}
80-
selectedShapeName={selectedShapeName}
81-
key={`${i}${child.componentName}`}
82-
childId={child.childId}
83-
componentId={focusComponent.id}
84-
x={child.position.x}
85-
y={child.position.y}
86-
width={child.position.width}
87-
height={child.position.height}
88-
title={child.childId + child.componentName}
89-
color={child.color}
90-
handleTransform={handleTransform}
91-
/>
92-
))}
93-
<TransformerComponent
94-
focusComponent={focusComponent}
95-
focusChild={focusChild}
96-
selectedShapeName={selectedShapeName}
97-
/>
98-
</Group>
67+
{components
68+
.find(comp => comp.title === focusComponent.title)
69+
.childrenArray.map((child, i) => (
70+
<Rectangle
71+
key={`${i}${child.componentName}`}
72+
draggable={false}
73+
selectedShapeName={selectedShapeName}
74+
childId={child.childId}
75+
focusChild={focusChild}
76+
focusComponent={focusComponent}
77+
componentId={focusComponent.id}
78+
x={child.position.x}
79+
y={child.position.y}
80+
width={child.position.width}
81+
height={child.position.height}
82+
title={child.componentName + child.childId}
83+
color={child.color}
84+
handleTransform={handleTransform}
85+
/>
86+
))}
9987
</Layer>
10088
</Stage>
10189
);

src/components/LeftColExpansionPanel.jsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const LeftColExpansionPanel = (props) => {
3333
return focusComponent.title === title ? 'focused' : '';
3434
}
3535

36-
function isAncestor() {
36+
function isAncestorOfFocused() {
3737
// add logic for determining if given component is an ancestor of focusedComponent
3838
return false;
3939
}
@@ -47,7 +47,6 @@ const LeftColExpansionPanel = (props) => {
4747
component="a"
4848
style={{ color: 'red' }}
4949
onClick={() => {
50-
console.log({ title });
5150
changeFocusComponent({ title });
5251
}}
5352
>
@@ -63,17 +62,14 @@ const LeftColExpansionPanel = (props) => {
6362
style={{ color }}
6463
/>
6564
<ListItemSecondaryAction>
66-
{isFocused() || isAncestor() ? (
65+
{isFocused() || isAncestorOfFocused() ? (
6766
<div />
6867
) : (
6968
<IconButton aria-label="Add">
7069
<AddIcon
7170
style={{ color, float: 'right' }}
7271
onClick={() => {
73-
console.log(title);
7472
addChild({ title });
75-
// get childId somewhere, or call changeFocusChild within addChild (better idea)
76-
// changeFocusChild({ title });
7773
}}
7874
/>
7975
</IconButton>

src/components/Rectangle.jsx

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,95 @@
11
import React, { Component } from 'react';
2-
import { Rect } from 'react-konva';
2+
import {
3+
Rect, Group, Label, Text,
4+
} from 'react-konva';
5+
import TransformerComponent from './TransformerComponent.jsx';
36
// import PropTypes from 'prop-types';
47

58
class Rectangle extends Component {
6-
extractPositionInfo(componentId, childId, target) {
7-
console.log('extracting position info, Rectanlge.jsx');
9+
handleResize(componentId, childId, target) {
810
const transformation = {
9-
x: target.x(),
10-
y: target.y(),
1111
width: target.width() * target.scaleX(),
1212
height: target.height() * target.scaleY(),
1313
};
14+
this.props.handleTransform(componentId, childId, transformation);
15+
}
1416

17+
handleDrag(componentId, childId, target) {
18+
const transformation = {
19+
x: target.x(),
20+
y: target.y(),
21+
};
1522
this.props.handleTransform(componentId, childId, transformation);
1623
}
1724

1825
render() {
1926
const {
20-
color, x, y, childId, componentId, draggable, width, height, title,
27+
color,
28+
x,
29+
y,
30+
childId,
31+
componentId,
32+
draggable,
33+
width,
34+
height,
35+
title,
36+
focusChild,
37+
focusComponent,
2138
} = this.props;
22-
console.log('childId in rectangle:', childId);
39+
40+
// the Group is responsible for dragging of all children
41+
// the Rect emits changes to child width and height with help from Transformer
2342
return (
24-
<Rect
25-
name={`${childId}`}
43+
<Group
44+
draggable={true}
2645
x={x}
2746
y={y}
28-
childId={childId}
29-
componentId={componentId}
30-
title={title}
3147
scaleX={1}
3248
scaleY={1}
3349
width={width}
3450
height={height}
35-
stroke={color}
36-
strokeWidth={6}
37-
strokeScaleEnabled={false}
38-
onTransformEnd={event => this.extractPositionInfo(componentId, childId, event.target)}
39-
onDragEnd={event => this.extractPositionInfo(componentId, childId, event.target)}
40-
draggable={draggable}
41-
color={color}
42-
// use dragBoundFunc to bind children within parents
43-
// (but if only children are rendered/movable, and parent is size of canvas, won't need this anyways )
44-
// dragBoundFunc={function dragBoundFunc(pos) {
45-
// console.log('binding');
46-
// const newY = pos.y < 200 ? 200 : pos.y;
47-
// const newX = pos.x < 200 ? 200 : pos.x;
48-
// return {
49-
// x: newX,
50-
// y: newY,
51-
// };
52-
// }}
53-
/>
51+
onDragEnd={event => this.handleDrag(componentId, childId, event.target)}
52+
>
53+
<Rect
54+
name={`${childId}`}
55+
// x={0}
56+
// y={0}
57+
childId={childId}
58+
componentId={componentId}
59+
title={title}
60+
scaleX={1}
61+
scaleY={1}
62+
width={width}
63+
height={height}
64+
stroke={color}
65+
color={color}
66+
// fill={color}
67+
// opacity={0.8}
68+
onTransformEnd={event => this.handleResize(componentId, childId, event.target)}
69+
strokeWidth={4}
70+
strokeScaleEnabled={false}
71+
draggable={false}
72+
/>
73+
<Label>
74+
<Text text={title} fill={'white'} />
75+
</Label>
76+
<Rect
77+
// replace with grandchildren rectangles
78+
scaleX={0.2}
79+
scaleY={0.2}
80+
width={width}
81+
height={height}
82+
stroke={color}
83+
color={color}
84+
strokeWidth={4}
85+
draggable={false}
86+
/>
87+
{focusChild.childId === childId ? (
88+
<TransformerComponent focusComponent={focusComponent} focusChild={focusChild} />
89+
) : (
90+
<Label />
91+
)}
92+
</Group>
5493
);
5594
}
5695
}

src/components/TransformerComponent.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ export default class TransformerComponent extends Component {
1111
this.checkNode();
1212
}
1313

14+
// this function makes sure the transformer follows along with the focusChild
1415
checkNode() {
1516
const stage = this.transformer.getStage();
1617
const { focusChild } = this.props;
1718
const selectedNode = stage.findOne(`.${focusChild.childId}`);
18-
// console.log('selected node from within TransformerComponent: ', selectedNode);
19-
// console.log('stage: ', stage);
20-
// console.log('focusChild: ', focusChild, focusChild.childId, typeof focusChild.childId);
19+
2120
if (selectedNode === this.transformer.node()) {
2221
return;
2322
}

src/containers/MainContainer.jsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,18 @@ class MainContainer extends Component {
111111
<MuiThemeProvider theme={theme}>
112112
<div className="main-container" style={{ cursor }}>
113113
<div className="main" ref={main}>
114-
{components.length > 0 ? (
115-
<KonvaStage
116-
scaleX={scaleX}
117-
scaleY={scaleY}
118-
image={image}
119-
draggable={draggable}
120-
components={components}
121-
handleTransform={handleTransformation}
122-
openExpansionPanel={openPanel}
123-
focusComponent={focusComponent}
124-
focusChild={focusChild}
125-
changeFocusChild={changeFocusChild}
126-
/>
127-
) : (
128-
<p>Add some components</p>
129-
)}
114+
<KonvaStage
115+
scaleX={scaleX}
116+
scaleY={scaleY}
117+
image={image}
118+
draggable={draggable}
119+
components={components}
120+
handleTransform={handleTransformation}
121+
openExpansionPanel={openPanel}
122+
focusComponent={focusComponent}
123+
focusChild={focusChild}
124+
changeFocusChild={changeFocusChild}
125+
/>
130126
</div>
131127
</div>
132128
</MuiThemeProvider>

src/store.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ let composer;
1010

1111
if (process.env.NODE_ENV === 'development') {
1212
composer = compose(
13-
applyMiddleware(logger, thunk),
13+
applyMiddleware(thunk), // re-add logger if you want redux log messages
1414
composeWithDevTools(),
1515
);
1616
} else {
1717
composer = compose(applyMiddleware(thunk));
1818
}
1919

20-
const store = createStore(
21-
reducers,
22-
composer,
23-
);
20+
const store = createStore(reducers, composer);
2421

2522
store.subscribe(throttle(() => saveState(store.getState()), 1000));
2623

src/utils/componentReducer.util.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ export const addComponent = (state, { title }) => {
4141
const strippedTitle = title
4242
.replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
4343
.replace(/[-_\s0-9\W]+/gi, '');
44+
45+
if (state.components.find(comp => comp.title === strippedTitle)) {
46+
// alert the user that duplicate component names are not allowed
47+
return {
48+
...state,
49+
};
50+
}
4451
const newComponent = {
4552
...initialComponentState,
4653
title: strippedTitle,
@@ -126,10 +133,10 @@ export const handleTransform = (state, {
126133
const transformedChild = {
127134
...child,
128135
position: {
129-
x,
130-
y,
131-
width,
132-
height,
136+
x: x || child.position.x,
137+
y: y || child.position.y,
138+
width: width || child.position.width,
139+
height: height || child.position.height,
133140
},
134141
};
135142

@@ -222,9 +229,7 @@ export const changeFocusChild = (state, { title, childId }) => {
222229
// just finds first child with given title, need to pass in specific childId somehow
223230
// maybe default to title if childId is unknown
224231
const focComp = state.components.find(comp => comp.title === state.focusComponent.title);
225-
console.log('erusbdfd', focComp, childId);
226232
const newFocusChild = focComp.childrenArray.find(child => child.childId === childId) || state.focusChild;
227-
console.log('trying to changeFocusChild', state.focusComponent.childrenArray);
228233
return {
229234
...state,
230235
focusChild: newFocusChild,

0 commit comments

Comments
 (0)