Skip to content

Commit c725dbb

Browse files
merge with changeFocusChild functionality and save child position functionality
2 parents c096a37 + 6db9ccd commit c725dbb

File tree

2 files changed

+72
-70
lines changed

2 files changed

+72
-70
lines changed

src/components/KonvaStage.jsx

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ 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 CHANG FOCUS CHILD FUNCTIONALITY HERE
2521
handleStageMouseDown = (e) => {
26-
// // clicked on stage - cler selection
22+
// // clicked on stage - clear selection
2723
if (e.target === e.target.getStage()) {
2824
// add functionality for allowing no focusChild
2925
return;
@@ -38,25 +34,8 @@ class KonvaStage extends Component {
3834
const rectChildId = e.target.attrs.childId;
3935
console.log('e.target : ', rectChildId);
4036
this.props.changeFocusChild({ childId: rectChildId });
41-
// if (rect) {
42-
// this.props.openExpansionPanel(rect || this.props.focusComponent);
43-
// } else {
44-
// this.props.openExpansionPanel(this.props.focusComponent);
45-
// }
4637
};
4738

48-
// WAS ALREADY COMMENTED OUT
49-
// handleStageDrag = () => {
50-
// // const mainWindowHeight = this.main.current.clientHeight;
51-
// // const mainWindowWidth = this.main.current.clientWidth;
52-
// // const groupX = this.refs.group.attrs.x;
53-
// // const groupY = this.refs.group.attrs.y;
54-
55-
// // const componentX = (mainWindowWidth / 2) - groupX;
56-
// // const componentY = (mainWindowHeight / 2) - groupY;
57-
// // console.log(componentX, componentY);
58-
// }
59-
6039
componentDidMount() {
6140
// this.props.setImage();
6241
}
@@ -99,7 +78,7 @@ class KonvaStage extends Component {
9978
<Rectangle
10079
draggable={child.draggable}
10180
selectedShapeName={selectedShapeName}
102-
key={i + child.componentName}
81+
key={`${i}${child.componentName}`}
10382
childId={child.childId}
10483
componentId={focusComponent.id}
10584
x={child.position.x}

src/utils/componentReducer.util.js

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,51 @@ export const addChild = (state, { title }) => {
113113
};
114114
};
115115

116+
export const handleTransform = (state, {
117+
componentId, childId, x, y, width, height,
118+
}) => {
119+
console.log('componentId and childId: ', componentId, childId);
120+
console.log('state.focuscomponent: ', state.focusComponent);
121+
122+
const child = state.components
123+
.find(comp => comp.id === componentId)
124+
.childrenArray.find(child => child.childId === childId);
125+
126+
const transformedChild = {
127+
...child,
128+
position: {
129+
x,
130+
y,
131+
width,
132+
height,
133+
},
134+
};
135+
136+
const children = [
137+
...state.components.find(comp => comp.id === componentId).childrenArray.filter((child) => {
138+
if (child.childId !== childId) return child;
139+
}),
140+
transformedChild,
141+
];
142+
143+
const component = {
144+
...state.components.find(comp => comp.id === componentId),
145+
childrenArray: children,
146+
};
147+
148+
const components = [
149+
...state.components.filter((comp) => {
150+
if (comp.id !== componentId) return comp;
151+
}),
152+
component,
153+
];
154+
155+
return {
156+
...state,
157+
components,
158+
};
159+
};
160+
116161
export const updateComponent = (
117162
state,
118163
{
@@ -255,7 +300,10 @@ export const reassignParent = (state, { index, id, parentIds = [] }) => {
255300
// Give the parent all children of it's to be deleted child
256301
if (parentIds.includes(comp.id)) {
257302
const prevChildrenIds = comp.childrenIds;
258-
return { ...comp, childrenIds: [...new Set(prevChildrenIds.concat(childrenIds))] };
303+
return {
304+
...comp,
305+
childrenIds: [...new Set(prevChildrenIds.concat(childrenIds))],
306+
};
259307
}
260308
return comp;
261309
});
@@ -291,26 +339,26 @@ export const handleClose = (state, status) => ({
291339
successOpen: status,
292340
});
293341

294-
export const updatePosition = (state, { id, x, y }) => {
295-
const components = state.components.map((component) => {
296-
if (component.id === id) {
297-
return {
298-
...component,
299-
position: {
300-
x,
301-
y,
302-
width: component.position.width,
303-
height: component.position.height,
304-
},
305-
};
306-
}
307-
return component;
308-
});
309-
return {
310-
...state,
311-
components,
312-
};
313-
};
342+
// export const updatePosition = (state, { id, x, y }) => {
343+
// const components = state.components.map(component => {
344+
// if (component.id === id) {
345+
// return {
346+
// ...component,
347+
// position: {
348+
// x,
349+
// y,
350+
// width: component.position.width,
351+
// height: component.position.height
352+
// }
353+
// };
354+
// }
355+
// return component;
356+
// });
357+
// return {
358+
// ...state,
359+
// components
360+
// };
361+
// };
314362

315363
/**
316364
* Applies the new x and y coordinates, as well as, the new width
@@ -326,32 +374,7 @@ export const updatePosition = (state, { id, x, y }) => {
326374
* @param {number} height - updated height
327375
*/
328376

329-
export const handleTransform = (state, {
330-
componentId, childId, x, y, width, height,
331-
}) => {
332-
console.log('componentId and childId: ', componentId, childId);
333-
console.log('state.focuscomponent: ', state.focusComponent);
334-
// const components = state.components
335-
// .find(comp => comp.id === componentId)
336-
// .childrenArray.map((child) => {
337-
// if (child.childId === childId) {
338-
// return {
339-
// ...child,
340-
// position: {
341-
// x,
342-
// y,
343-
// width,
344-
// height,
345-
// },
346-
// };
347-
// }
348-
// return child;
349-
// });
350-
return {
351-
...state,
352-
// components,
353-
};
354-
};
377+
// handleTransform used to be here
355378

356379
/**
357380
* Toggles the drag of the group, as well as all components. If the group is draggable the

0 commit comments

Comments
 (0)