Skip to content

Commit 1d24d3d

Browse files
small stage change
1 parent 99f64d3 commit 1d24d3d

File tree

1 file changed

+48
-71
lines changed

1 file changed

+48
-71
lines changed

src/components/KonvaStage.jsx

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
import React, { Component, createRef, Fragment } from "react";
2-
import Button from "@material-ui/core/Button";
1+
import React, { Component, createRef, Fragment } from 'react';
2+
import Button from '@material-ui/core/Button';
33

44
// import PropTypes from 'prop-types';
55
import {
6-
Stage,
7-
Layer,
8-
Line,
9-
Group,
10-
Label,
11-
Text,
12-
Rect,
13-
Transformer
14-
} from "react-konva";
15-
import Rectangle from "./Rectangle.jsx";
6+
Stage, Layer, Line, Group, Label, Text, Rect, Transformer,
7+
} from 'react-konva';
8+
import Rectangle from './Rectangle.jsx';
169

1710
class KonvaStage extends Component {
1811
constructor(props) {
@@ -22,7 +15,7 @@ class KonvaStage extends Component {
2215
stageHeight: 1000,
2316
blockSnapSize: 7,
2417
grid: [],
25-
gridStroke: 1
18+
gridStroke: 1,
2619
};
2720
}
2821

@@ -31,36 +24,31 @@ class KonvaStage extends Component {
3124
// here we should add listener for "container" resize
3225
// take a look here https://developers.google.com/web/updates/2016/10/resizeobserver
3326
// for simplicity I will just listen window resize
34-
window.addEventListener("resize", this.checkSize);
27+
window.addEventListener('resize', this.checkSize);
3528
this.createGrid();
3629
}
3730

3831
getDirectChildrenCopy(focusComponent) {
39-
const component = this.props.components.find(
40-
comp => comp.id === focusComponent.id
41-
);
32+
const component = this.props.components.find(comp => comp.id === focusComponent.id);
4233

43-
const childrenArr = component.childrenArray.filter(
44-
child => child.childId !== "-1"
45-
);
34+
const childrenArr = component.childrenArray.filter(child => child.childId !== '-1');
4635

4736
let childrenArrCopy = this.cloneDeep(childrenArr);
4837

4938
const pseudoChild = {
50-
childId: "-1",
39+
childId: '-1',
5140
childComponentId: component.id,
5241
componentName: component.title,
5342
position: {
5443
x: component.position.x,
5544
y: component.position.y,
5645
width: component.position.width,
57-
height: component.position.height
46+
height: component.position.height,
5847
},
5948
draggable: true,
60-
color: component.color
49+
color: component.color,
6150
};
62-
// console.log('getDirectChildrenCopy, pseudoChild.position: ', pseudoChild.position);
63-
childrenArrCopy = childrenArrCopy.concat(pseudoChild);
51+
childrenArrCopy = childrenArrCopy.concat(pseudoChild); // could just use push here, concat needlessly generate new array
6452
return childrenArrCopy;
6553
}
6654

@@ -69,19 +57,19 @@ class KonvaStage extends Component {
6957

7058
if (Array.isArray(value)) {
7159
result = [];
72-
value.forEach(elm => {
73-
if (typeof elm === "object") {
60+
value.forEach((elm) => {
61+
if (typeof elm === 'object') {
7462
result.push(this.cloneDeep(elm));
7563
} else {
7664
result.push(elm);
7765
}
7866
});
7967
return result;
8068
}
81-
if (typeof value === "object" && value !== null) {
69+
if (typeof value === 'object' && value !== null) {
8270
result = {};
83-
Object.keys(value).forEach(key => {
84-
if (typeof value[key] === "object") {
71+
Object.keys(value).forEach((key) => {
72+
if (typeof value[key] === 'object') {
8573
result[key] = this.cloneDeep(value[key]);
8674
} else {
8775
result[key] = value[key];
@@ -93,40 +81,39 @@ class KonvaStage extends Component {
9381
}
9482

9583
componentWillUnmount() {
96-
window.removeEventListener("resize", this.checkSize);
84+
window.removeEventListener('resize', this.checkSize);
9785
}
9886

9987
checkSize = () => {
10088
const width = this.container.offsetWidth;
10189
const height = this.container.offsetHeight;
10290
this.setState({
10391
stageWidth: width,
104-
stageHeight: height
92+
stageHeight: height,
10593
});
10694
};
10795

108-
handleStageMouseDown = e => {
96+
handleStageMouseDown = (e) => {
10997
// // clicked on stage - clear selection
11098
if (e.target === e.target.getStage()) {
11199
// add functionality for allowing no focusChild
112-
console.log("user clicked on canvas:");
100+
console.log('user clicked on canvas:');
113101
return;
114102
}
115103
// // clicked on transformer - do nothing
116-
const clickedOnTransformer =
117-
e.target.getParent().className === "Transformer";
104+
const clickedOnTransformer = e.target.getParent().className === 'Transformer';
118105
if (clickedOnTransformer) {
119-
console.log("user clicked on transformer");
106+
console.log('user clicked on transformer');
120107
return;
121108
}
122109

123110
// find clicked rect by its name
124111
const rectChildId = e.target.attrs.childId;
125-
console.log("user clicked on child rectangle with childId: ", rectChildId);
112+
console.log('user clicked on child rectangle with childId: ', rectChildId);
126113
this.props.changeFocusChild({ childId: rectChildId });
127114
this.props.changeComponentFocusChild({
128115
componentId: this.props.focusComponent.id,
129-
childId: rectChildId
116+
childId: rectChildId,
130117
});
131118
};
132119

@@ -139,81 +126,73 @@ class KonvaStage extends Component {
139126
Math.round(i * this.state.blockSnapSize) + 0.5,
140127
0,
141128
Math.round(i * this.state.blockSnapSize) + 0.5,
142-
this.state.stageHeight
129+
this.state.stageHeight,
143130
]}
144-
stroke={"#ddd"}
131+
stroke={'#ddd'}
145132
strokeWidth={this.state.gridStroke}
146133
key={`${i}vertical`}
147-
/>
134+
/>,
148135
);
149136
}
150-
for (
151-
let j = 0;
152-
j < this.state.stageHeight / this.state.blockSnapSize;
153-
j++
154-
) {
137+
for (let j = 0; j < this.state.stageHeight / this.state.blockSnapSize; j++) {
155138
output.push(
156139
<Line
157140
points={[
158141
0,
159142
Math.round(j * this.state.blockSnapSize),
160143
this.state.stageWidth,
161-
Math.round(j * this.state.blockSnapSize)
144+
Math.round(j * this.state.blockSnapSize),
162145
]}
163-
stroke={"#ddd"}
146+
stroke={'#ddd'}
164147
strokeWidth={this.state.gridStroke}
165148
key={`${j}horizontal`}
166-
/>
149+
/>,
167150
);
168151
}
169-
console.log("calling function to render grid");
152+
console.log('calling function to render grid');
170153
this.setState({
171-
grid: output
154+
grid: output,
172155
});
173156
};
174157

175158
render() {
176159
const {
177-
components,
178-
handleTransform,
179-
focusComponent,
180-
focusChild,
181-
deleteChild
160+
components, handleTransform, focusComponent, focusChild, deleteChild,
182161
} = this.props;
183162

184163
return (
185164
<div
186165
style={{
187-
width: "100%",
188-
height: "100%"
166+
width: '100%',
167+
height: '100%',
189168
}}
190-
ref={node => {
169+
ref={(node) => {
191170
this.container = node;
192171
}}
193172
>
194173
<Button
195174
onClick={deleteChild}
196175
style={{
197-
width: "150px",
198-
position: "relative",
199-
float: "right",
200-
background: "#dbdbdb",
201-
zIndex: 2
176+
width: '150px',
177+
position: 'relative',
178+
float: 'right',
179+
background: '#dbdbdb',
180+
zIndex: 2,
202181
}}
203182
>
204183
delete child
205184
</Button>
206185
<Stage
207-
className={"canvasStage"}
208-
ref={node => {
186+
className={'canvasStage'}
187+
ref={(node) => {
209188
this.stage = node;
210189
}}
211190
onMouseDown={this.handleStageMouseDown}
212191
width={this.state.stageWidth}
213192
height={this.state.stageHeight}
214193
>
215194
<Layer
216-
ref={node => {
195+
ref={(node) => {
217196
this.layer = node;
218197
}}
219198
>
@@ -242,9 +221,7 @@ class KonvaStage extends Component {
242221
/>
243222
))
244223
.sort(
245-
(rectA, rectB) =>
246-
rectA.props.width * rectA.props.height <
247-
rectB.props.width * rectB.props.height
224+
(rectA, rectB) => rectA.props.width * rectA.props.height < rectB.props.width * rectB.props.height,
248225
) // shouldnt this be subtraction instead of < ? see MDN
249226
// reasoning for the sort:
250227
// Konva determines zIndex (which rect is clicked on if rects overlap) based on rendering order

0 commit comments

Comments
 (0)