Skip to content

Commit 3178b20

Browse files
Merge pull request #19 from ChristianEdwardPadilla/development
can move and resize single child rect
2 parents a7733c5 + 3091e52 commit 3178b20

File tree

7 files changed

+116
-99
lines changed

7 files changed

+116
-99
lines changed

src/actions/components.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,31 @@ import { loadState } from '../localStorage';
3232
// import createApplicationUtil from '../utils/createApplication.util';
3333

3434
export const loadInitData = () => (dispatch) => {
35-
loadState()
36-
.then(data => dispatch({
37-
type: LOAD_INIT_DATA,
38-
payload: {
39-
data: data ? data.workspace : {},
40-
},
41-
}));
35+
loadState().then(data => dispatch({
36+
type: LOAD_INIT_DATA,
37+
payload: {
38+
data: data ? data.workspace : {},
39+
},
40+
}));
4241
};
4342

44-
export const updateChildren = (({
45-
parentIds, childIndex, childId,
46-
}) => ({
43+
export const updateChildren = ({ parentIds, childIndex, childId }) => ({
4744
type: UPDATE_CHILDREN,
4845
payload: {
49-
parentIds, childIndex, childId,
46+
parentIds,
47+
childIndex,
48+
childId,
5049
},
51-
}));
50+
});
5251

53-
export const parentReassignment = (({ index, id, parentIds }) => ({
52+
export const parentReassignment = ({ index, id, parentIds }) => ({
5453
type: REASSIGN_PARENT,
5554
payload: {
5655
index,
5756
id,
5857
parentIds,
5958
},
60-
}));
59+
});
6160

6261
export const addComponent = ({ title }) => (dispatch) => {
6362
dispatch({ type: ADD_COMPONENT, payload: { title } });
@@ -82,12 +81,20 @@ export const deleteComponent = ({ index, id, parentIds = [] }) => (dispatch) =>
8281
};
8382

8483
export const updateComponent = ({
85-
id, index, newParentId = null, color = null, stateful = null,
84+
id,
85+
index,
86+
newParentId = null,
87+
color = null,
88+
stateful = null,
8689
}) => (dispatch) => {
8790
dispatch({
8891
type: UPDATE_COMPONENT,
8992
payload: {
90-
id, index, newParentId, color, stateful,
93+
id,
94+
index,
95+
newParentId,
96+
color,
97+
stateful,
9198
},
9299
});
93100

@@ -123,12 +130,17 @@ export const handleClose = () => ({
123130
payload: false,
124131
});
125132

126-
export const handleTransform = (id, {
133+
export const handleTransform = (componentId, id, {
127134
x, y, width, height,
128135
}) => ({
129136
type: HANDLE_TRANSFORM,
130137
payload: {
131-
id, x, y, width, height,
138+
componentId,
139+
id,
140+
x,
141+
y,
142+
width,
143+
height,
132144
},
133145
});
134146

src/components/KonvaStage.jsx

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class KonvaStage extends Component {
1818
this.group = createRef();
1919
}
2020

21+
// Christian - this function causes the expansionPanel of the clicked rect to open
22+
// (and focusedComponent to change, which we don't want)
23+
// could reuse this logic for affecting state of children array
24+
// ADD FOCUS CHILD FUNCTIONALITY HERE
2125
handleStageMouseDown = (e) => {
2226
// // clicked on stage - cler selection
2327
// if (e.target === e.target.getStage()) {
@@ -32,13 +36,14 @@ class KonvaStage extends Component {
3236

3337
// find clicked rect by its name
3438
const id = e.target.name();
39+
console.log(e.target);
3540
const rect = this.props.components.find(r => r.id === id);
3641

37-
if (rect) {
38-
this.props.openExpansionPanel(rect);
39-
} else {
40-
this.props.openExpansionPanel({});
41-
}
42+
// if (rect) {
43+
// this.props.openExpansionPanel(rect || this.props.focusComponent);
44+
// } else {
45+
// this.props.openExpansionPanel(this.props.focusComponent);
46+
// }
4247
};
4348

4449
// WAS ALREADY COMMENTED OUT
@@ -88,36 +93,24 @@ class KonvaStage extends Component {
8893
}}
8994
draggable={draggable}
9095
>
91-
{/* {components.map((comp, i) => (
92-
<Rectangle
93-
draggable={comp.draggable}
94-
selectedShapeName={selectedShapeName}
95-
key={i}
96-
componentId={comp.id}
97-
x={comp.position.x}
98-
y={comp.position.y}
99-
width={comp.position.width}
100-
height={comp.position.height}
101-
title={comp.title}
102-
color={comp.color}
103-
handleTransform={handleTransform}
104-
/>
105-
))} */}
106-
{components.find((comp) => comp.title === focusComponent.title)
107-
.childrenArray.map((child, i) => <Rectangle
108-
draggable={child.draggable}
109-
selectedShapeName={selectedShapeName}
110-
key={i + child.componentName}
111-
componentId={child.id}
112-
x={child.position.x}
113-
y={child.position.y}
114-
width={child.position.width}
115-
height={child.position.height}
116-
title={child.childId + child.componentName}
117-
color={'red'}
118-
handleTransform={handleTransform}
119-
/>)}
120-
{/* )} */}
96+
{components
97+
.find(comp => comp.title === focusComponent.title)
98+
.childrenArray.map((child, i) => (
99+
<Rectangle
100+
draggable={child.draggable}
101+
selectedShapeName={selectedShapeName}
102+
key={i + child.componentName}
103+
childId={child.childId}
104+
componentId={focusComponent.id}
105+
x={child.position.x}
106+
y={child.position.y}
107+
width={child.position.width}
108+
height={child.position.height}
109+
title={child.childId + child.componentName}
110+
color={child.color}
111+
handleTransform={handleTransform}
112+
/>
113+
))}
121114
<TransformerComponent
122115
focusComponent={focusComponent}
123116
focusChild={focusChild}

src/components/LeftColExpansionPanel.jsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ const LeftColExpansionPanel = (props) => {
3939
<ListItem
4040
button
4141
component="a"
42-
// style={
43-
// if (components.find(comp => comp.title === focusComponent.title))
44-
// }
4542
style={{ color: 'red' }}
4643
onClick={() => {
4744
console.log({ title });

src/components/Rectangle.jsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,53 @@ import { Rect } from 'react-konva';
33
// import PropTypes from 'prop-types';
44

55
class Rectangle extends Component {
6-
extractPositionInfo(componentId, target) {
6+
extractPositionInfo(componentId, childId, target) {
7+
console.log('extracting position info, Rectanlge.jsx');
78
const transformation = {
89
x: target.x(),
910
y: target.y(),
1011
width: target.width() * target.scaleX(),
1112
height: target.height() * target.scaleY(),
1213
};
1314

14-
this.props.handleTransform(componentId, transformation);
15+
this.props.handleTransform(componentId, childId, transformation);
1516
}
1617

1718
render() {
1819
const {
19-
color, x, y, componentId, draggable, width, height, title,
20+
color, x, y, childId, componentId, draggable, width, height, title,
2021
} = this.props;
21-
22+
console.log('childId in rectangle:', childId);
2223
return (
2324
<Rect
24-
name={componentId}
25+
name={`${childId}`}
2526
x={x}
2627
y={y}
27-
componentid={componentId}
28+
childId={childId}
29+
componentId={componentId}
30+
title={title}
2831
scaleX={1}
2932
scaleY={1}
3033
width={width}
3134
height={height}
3235
stroke={color}
3336
strokeWidth={6}
3437
strokeScaleEnabled={false}
35-
onTransformEnd={event => this.extractPositionInfo(componentId, event.target)}
36-
onDragEnd={event => this.extractPositionInfo(componentId, event.target)}
38+
onTransformEnd={event => this.extractPositionInfo(componentId, childId, event.target)}
39+
onDragEnd={event => this.extractPositionInfo(componentId, childId, event.target)}
3740
draggable={draggable}
38-
dragBoundFunc={function dragBoundFunc(pos) {
39-
console.log('binding');
40-
const newY = pos.y < 200 ? 200 : pos.y;
41-
const newX = pos.x < 200 ? 200 : pos.x;
42-
return {
43-
x: newX,
44-
y: newY,
45-
};
46-
}}
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+
// }}
4753
/>
4854
);
4955
}

src/components/TransformerComponent.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export default class TransformerComponent extends Component {
1515
const stage = this.transformer.getStage();
1616
const { focusChild } = this.props;
1717
const selectedNode = stage.findOne(`.${focusChild.childId}`);
18-
18+
// console.log('selected node from within TransformerComponent: ', selectedNode);
19+
// console.log('stage: ', stage);
20+
// console.log('focusChild: ', focusChild, focusChild.childId, typeof focusChild.childId);
1921
if (selectedNode === this.transformer.node()) {
2022
return;
2123
}

src/containers/MainContainer.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import KonvaStage from '../components/KonvaStage.jsx';
1616
const IPC = require('electron').ipcRenderer;
1717

1818
const mapDispatchToProps = dispatch => ({
19-
handleTransformation: (id, {
19+
handleTransformation: (componentId, childId, {
2020
x, y, width, height,
2121
}) => dispatch(
22-
handleTransform(id, {
22+
handleTransform(componentId, childId, {
2323
x,
2424
y,
2525
width,

src/utils/componentReducer.util.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ export const addChild = (state, { title }) => {
7070
.replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
7171
.replace(/[-_\s0-9\W]+/gi, '');
7272

73-
const view = state.components.filter((comp) => {
74-
if (comp.title === state.focusComponent.title) return comp;
75-
})[0];
73+
// view represents the component that this child will live (and be rendered) in
74+
const view = state.components.find(comp => comp.title === state.focusComponent.title);
75+
76+
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
77+
const parentComponent = state.components.find(comp => comp.title === title);
7678

77-
console.log(view);
79+
console.log('view from addChild: ', view);
7880

7981
const newChild = {
8082
childId: view.nextChildId.toString(),
@@ -85,6 +87,8 @@ export const addChild = (state, { title }) => {
8587
width: 50,
8688
height: 50,
8789
},
90+
draggable: true,
91+
color: parentComponent.color,
8892
};
8993

9094
const compsChildrenArr = [...view.childrenArray, newChild];
@@ -162,8 +166,9 @@ export const deleteComponent = (state, { index, id }) => {
162166
};
163167

164168
export const changeFocusComponent = (state, { title }) => {
165-
166-
let focusComp = state.components.filter((comp) => {if (comp.title === title) return comp})[0]
169+
const focusComp = state.components.filter((comp) => {
170+
if (comp.title === title) return comp;
171+
})[0];
167172

168173
return {
169174
...state,
@@ -312,25 +317,27 @@ export const updatePosition = (state, { id, x, y }) => {
312317
*/
313318

314319
export const handleTransform = (state, {
315-
id, x, y, width, height,
320+
componentId, childId, x, y, width, height,
316321
}) => {
317-
const components = state.components.map((component) => {
318-
if (component.id === id) {
319-
return {
320-
...component,
321-
position: {
322-
x,
323-
y,
324-
width,
325-
height,
326-
},
327-
};
328-
}
329-
return component;
330-
});
322+
console.log('componentId and childId: ', componentId, childId);
323+
console.log('state.focuscomponent: ', state.focusComponent);
324+
// const components = state.components.map((component) => {
325+
// if (component.id === id) {
326+
// return {
327+
// ...component,
328+
// position: {
329+
// x,
330+
// y,
331+
// width,
332+
// height,
333+
// },
334+
// };
335+
// }
336+
// return component;
337+
// });
331338
return {
332339
...state,
333-
components,
340+
// components,
334341
};
335342
};
336343

@@ -380,7 +387,7 @@ export const moveToBottom = (state, componentId) => {
380387

381388
export const openExpansionPanel = (state, { component }) => ({
382389
...state,
383-
focusComponent: component,
390+
// focusComponent: component,
384391
});
385392

386393
export const addProp = (state, {

0 commit comments

Comments
 (0)