Skip to content

Commit eb2dbe9

Browse files
committed
add image background working
1 parent 32f1bb7 commit eb2dbe9

File tree

6 files changed

+258
-116
lines changed

6 files changed

+258
-116
lines changed

src/components/GrandchildRectangle.jsx

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import React, { Component } from 'react';
2-
import { Rect, Group } from 'react-konva';
1+
import React, { Component } from "react";
2+
import { Rect, Group } from "react-konva";
33
// import findComponentById from '../utils/findComponentById.ts';
44

55
class GrandchildRectangle extends Component {
66
getComponentColor(componentId) {
77
// const color = findComponentById(componentId, this.props.components).color;
8-
const color = this.props.components.find(comp => comp.id === componentId).color;
8+
const color = this.props.components.find(comp => comp.id === componentId)
9+
.color;
910
return color;
1011
}
1112

1213
getPseudoChild() {
13-
return this.props.components.find(comp => comp.id === this.props.childComponentId);
14+
return this.props.components.find(
15+
comp => comp.id === this.props.childComponentId
16+
);
1417
}
1518

19+
setImage = imageSource => {
20+
//console.log("IMAGE SOURCE", imageSource);
21+
if (!imageSource) return;
22+
const image = new window.Image();
23+
image.src = imageSource;
24+
if (!image.height) return null;
25+
return image;
26+
};
27+
1628
render() {
1729
const {
1830
x,
@@ -28,6 +40,7 @@ class GrandchildRectangle extends Component {
2840
height,
2941
focusChild,
3042
components,
43+
imageSource
3144
} = this.props;
3245

3346
// the Group is responsible for dragging of all children
@@ -53,15 +66,20 @@ class GrandchildRectangle extends Component {
5366
scaleY={1}
5467
width={width}
5568
height={height}
56-
stroke={childType === 'COMP' ? this.getComponentColor(childComponentId) : '#000000'}
69+
stroke={
70+
childType === "COMP"
71+
? this.getComponentColor(childComponentId)
72+
: "#000000"
73+
}
74+
fillPatternImage={imageSource ? this.setImage(imageSource) : null}
5775
// fill={color}
5876
// opacity={0.8}
5977
strokeWidth={4}
6078
strokeScaleEnabled={false}
6179
draggable={false}
6280
/>
63-
{childType === 'COMP'
64-
&& components
81+
{childType === "COMP" &&
82+
components
6583
.find(comp => comp.title === childComponentName)
6684
.childrenArray.filter(child => child.childId !== -1)
6785
.map((grandchild, i) => (
@@ -70,21 +88,35 @@ class GrandchildRectangle extends Component {
7088
components={components}
7189
componentId={componentId}
7290
childType={grandchild.childType}
91+
imageSource={
92+
grandchild.htmlElement == "Image" && grandchild.HTMLInfo.Src
93+
? grandchild.HTMLInfo.Src
94+
: null
95+
}
7396
childComponentName={grandchild.componentName}
7497
childComponentId={grandchild.childComponentId}
7598
focusChild={focusChild}
7699
childId={childId}
77-
width={grandchild.position.width * (width / this.getPseudoChild().position.width)}
100+
// fillPatternImage={
101+
// grandchild.HTMLInfo.Src
102+
// ? this.setImage(grandchild.HTMLInfo.Src)
103+
// : null
104+
// }
105+
width={
106+
grandchild.position.width *
107+
(width / this.getPseudoChild().position.width)
108+
}
78109
height={
79-
grandchild.position.height * (height / this.getPseudoChild().position.height)
110+
grandchild.position.height *
111+
(height / this.getPseudoChild().position.height)
80112
}
81113
x={
82-
(grandchild.position.x - this.getPseudoChild().position.x)
83-
* (width / this.getPseudoChild().position.width)
114+
(grandchild.position.x - this.getPseudoChild().position.x) *
115+
(width / this.getPseudoChild().position.width)
84116
}
85117
y={
86-
(grandchild.position.y - this.getPseudoChild().position.y)
87-
* (height / this.getPseudoChild().position.height)
118+
(grandchild.position.y - this.getPseudoChild().position.y) *
119+
(height / this.getPseudoChild().position.height)
88120
}
89121
/>
90122
))}

src/components/HtmlAttr.jsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ class HtmlAttr extends Component {
7575
{}
7676
);
7777

78+
handleSave = attr => {
79+
console.log(attr, this.state[attr]);
80+
this.props.updateHtmlAttr({ attr, value: this.state[attr] });
81+
this.setState({
82+
[attr]: ""
83+
});
84+
};
85+
7886
handleChange = event => {
7987
this.setState({
8088
[event.target.id]: event.target.value.trim()
@@ -169,9 +177,10 @@ class HtmlAttr extends Component {
169177
marginBottom: "10px"
170178
}}
171179
// style={{ maxWidth: "20px" }}
172-
onClick={() => {
173-
updateHtmlAttr({ attr, value: this.state[attr] });
174-
}}
180+
// onClick={() => {
181+
// updateHtmlAttr({ attr, value: this.state[attr] });
182+
// }}
183+
onClick={() => this.handleSave(attr)}
175184
>
176185
<SaveIcon className={classes.extendedIcon} />
177186
Save

src/components/KonvaStage.jsx

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
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
import {
4-
Stage, Layer, Line, Group, Label, Text, Rect, Transformer,
5-
} from 'react-konva';
6-
import Rectangle from './Rectangle.jsx';
7-
import cloneDeep from '../utils/cloneDeep.ts';
4+
Stage,
5+
Layer,
6+
Line,
7+
Group,
8+
Label,
9+
Text,
10+
Rect,
11+
Transformer
12+
} from "react-konva";
13+
import Rectangle from "./Rectangle.jsx";
14+
import cloneDeep from "../utils/cloneDeep.ts";
815

916
class KonvaStage extends Component {
1017
constructor(props) {
@@ -14,14 +21,18 @@ class KonvaStage extends Component {
1421
stageHeight: 1000,
1522
blockSnapSize: 10,
1623
grid: [],
17-
gridStroke: 1,
24+
gridStroke: 1
1825
};
1926
}
2027

2128
getDirectChildrenCopy(focusComponent) {
22-
const component = this.props.components.find(comp => comp.id === focusComponent.id);
29+
const component = this.props.components.find(
30+
comp => comp.id === focusComponent.id
31+
);
2332

24-
const childrenArr = component.childrenArray.filter(child => child.childId !== -1);
33+
const childrenArr = component.childrenArray.filter(
34+
child => child.childId !== -1
35+
);
2536

2637
let childrenArrCopy = cloneDeep(childrenArr);
2738

@@ -33,10 +44,10 @@ class KonvaStage extends Component {
3344
x: component.position.x,
3445
y: component.position.y,
3546
width: component.position.width,
36-
height: component.position.height,
47+
height: component.position.height
3748
},
3849
draggable: true,
39-
color: component.color,
50+
color: component.color
4051
};
4152
childrenArrCopy = childrenArrCopy.concat(pseudoChild); // could just use push here, concat needlessly generate new array
4253
return childrenArrCopy;
@@ -47,52 +58,53 @@ class KonvaStage extends Component {
4758
// here we should add listener for "container" resize
4859
// take a look here https://developers.google.com/web/updates/2016/10/resizeobserver
4960
// for simplicity I will just listen window resize
50-
window.addEventListener('resize', this.checkSize);
51-
this.container.addEventListener('keydown', this.handleKeyDown);
61+
window.addEventListener("resize", this.checkSize);
62+
this.container.addEventListener("keydown", this.handleKeyDown);
5263
this.createGrid();
5364
}
5465

5566
componentWillUnmount() {
56-
window.removeEventListener('resize', this.checkSize);
57-
this.container.removeEventListener('keydown', this.handleKeyDown);
67+
window.removeEventListener("resize", this.checkSize);
68+
this.container.removeEventListener("keydown", this.handleKeyDown);
5869
}
5970

6071
checkSize = () => {
6172
const width = this.container.offsetWidth;
6273
const height = this.container.offsetHeight;
6374
this.setState({
6475
stageWidth: width,
65-
stageHeight: height,
76+
stageHeight: height
6677
});
6778
};
6879

69-
handleKeyDown = (e) => {
70-
if (e.key === 'Delete' || e.key === 'Backspace') {
80+
handleKeyDown = e => {
81+
if (e.key === "Delete" || e.key === "Backspace") {
7182
this.props.deleteChild({});
7283
}
7384
};
7485

75-
handleStageMouseDown = (e) => {
86+
handleStageMouseDown = e => {
7687
// // clicked on stage - clear selection
7788
if (e.target === e.target.getStage()) {
7889
// add functionality for allowing no focusChild
79-
console.log('user clicked on canvas:');
90+
console.log("user clicked on canvas:");
8091
return;
8192
}
8293
// // clicked on transformer - do nothing
83-
const clickedOnTransformer = e.target.getParent().className === 'Transformer';
94+
const clickedOnTransformer =
95+
e.target.getParent().className === "Transformer";
8496
if (clickedOnTransformer) {
85-
console.log('user clicked on transformer');
97+
console.log("user clicked on transformer");
8698
return;
8799
}
88100

89101
// find clicked rect by its name
90102
const rectChildId = e.target.attrs.childId;
91-
console.log('user clicked on child rectangle with childId: ', rectChildId);
103+
// console.log("user clicked on child rectangle with childId: ", rectChildId);
92104
this.props.changeFocusChild({ childId: rectChildId });
93105
this.props.changeComponentFocusChild({
94106
componentId: this.props.focusComponent.id,
95-
childId: rectChildId,
107+
childId: rectChildId
96108
});
97109
};
98110

@@ -105,73 +117,81 @@ class KonvaStage extends Component {
105117
Math.round(i * this.state.blockSnapSize) + 0.5,
106118
0,
107119
Math.round(i * this.state.blockSnapSize) + 0.5,
108-
this.state.stageHeight,
120+
this.state.stageHeight
109121
]}
110-
stroke={'#ddd'}
122+
stroke={"#ddd"}
111123
strokeWidth={this.state.gridStroke}
112124
key={`${i}vertical`}
113-
/>,
125+
/>
114126
);
115127
}
116-
for (let j = 0; j < this.state.stageHeight / this.state.blockSnapSize; j++) {
128+
for (
129+
let j = 0;
130+
j < this.state.stageHeight / this.state.blockSnapSize;
131+
j++
132+
) {
117133
output.push(
118134
<Line
119135
points={[
120136
0,
121137
Math.round(j * this.state.blockSnapSize),
122138
this.state.stageWidth,
123-
Math.round(j * this.state.blockSnapSize),
139+
Math.round(j * this.state.blockSnapSize)
124140
]}
125-
stroke={'#ddd'}
141+
stroke={"#ddd"}
126142
strokeWidth={this.state.gridStroke}
127143
key={`${j}horizontal`}
128-
/>,
144+
/>
129145
);
130146
}
131147
this.setState({
132-
grid: output,
148+
grid: output
133149
});
134150
};
135151

136152
render() {
137153
const {
138-
components, handleTransform, focusComponent, focusChild, deleteChild,
154+
components,
155+
handleTransform,
156+
focusComponent,
157+
focusChild,
158+
deleteChild
139159
} = this.props;
140160

141161
return (
142162
<div
143163
style={{
144-
width: '95%',
145-
height: '95%',
164+
width: "95%",
165+
height: "95%"
146166
}}
147-
ref={(node) => {
167+
ref={node => {
148168
this.container = node;
149169
}}
150170
tabIndex="0" // required for keydown event to be heard by this.container
151171
>
152172
<Button
153173
onClick={deleteChild}
154174
style={{
155-
width: '150px',
156-
position: 'relative',
157-
float: 'right',
158-
background: '#dbdbdb',
159-
zIndex: 2,
175+
width: "150px",
176+
position: "relative",
177+
float: "right",
178+
background: "#dbdbdb",
179+
zIndex: 2
160180
}}
161181
>
162182
delete child
163183
</Button>
164184
<Stage
165-
className={'canvasStage'}
166-
ref={(node) => {
185+
className={"canvasStage"}
186+
ref={node => {
167187
this.stage = node;
168188
}}
169189
onMouseDown={this.handleStageMouseDown}
170190
width={this.state.stageWidth}
171191
height={this.state.stageHeight}
172192
>
173193
<Layer
174-
ref={(node) => {
194+
ref={node => {
175195
this.layer = node;
176196
}}
177197
>
@@ -197,10 +217,17 @@ class KonvaStage extends Component {
197217
handleTransform={handleTransform}
198218
draggable={true}
199219
blockSnapSize={this.state.blockSnapSize}
220+
imageSource={
221+
child.htmlElement == "Image" && child.HTMLInfo.Src
222+
? child.HTMLInfo.Src
223+
: null
224+
}
200225
/>
201226
))
202227
.sort(
203-
(rectA, rectB) => rectA.props.width * rectA.props.height < rectB.props.width * rectB.props.height,
228+
(rectA, rectB) =>
229+
rectA.props.width * rectA.props.height <
230+
rectB.props.width * rectB.props.height
204231
) // shouldnt this be subtraction instead of < ? see MDN
205232
// reasoning for the sort:
206233
// Konva determines zIndex (which rect is clicked on if rects overlap) based on rendering order

0 commit comments

Comments
 (0)