Skip to content

Commit e8e32b5

Browse files
Merge pull request #98 from ChristianEdwardPadilla/development
image src scaling correctly, html attr set snapback bug fix
2 parents 8df3c17 + 1c90225 commit e8e32b5

File tree

6 files changed

+215
-308
lines changed

6 files changed

+215
-308
lines changed

src/components/GrandchildRectangle.jsx

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { Rect, Group } from 'react-konva';
44

55
class GrandchildRectangle extends Component {
66
state = {
7+
image: null,
78
imageHeight: 0,
89
imageWidth: 0,
910
};
1011

1112
getComponentColor(componentId) {
12-
// const color = findComponentById(componentId, this.props.components).color;
1313
const color = this.props.components.find(comp => comp.id === componentId).color;
1414
return color;
1515
}
@@ -19,13 +19,11 @@ class GrandchildRectangle extends Component {
1919
}
2020

2121
setImage = (imageSource) => {
22-
// console.log("IMAGE SOURCE", imageSource);
2322
if (!imageSource) return;
2423
const image = new window.Image();
2524
image.src = imageSource;
2625
if (!image.height) return null;
27-
this.setState({ imageHeight: image.height, imageWidth: image.width });
28-
return image;
26+
this.setState({ image });
2927
};
3028

3129
render() {
@@ -70,16 +68,12 @@ class GrandchildRectangle extends Component {
7068
width={width}
7169
height={height}
7270
stroke={childType === 'COMP' ? this.getComponentColor(childComponentId) : '#000000'}
73-
fillPatternImage={imageSource ? this.setImage(imageSource) : null}
74-
fillPatternScaleX={0.1}
75-
fillPatternScaleY={0.1}
76-
// fill={color}
77-
// opacity={0.8}
78-
strokeWidth={10}
71+
fillPatternImage={this.state.image ? this.state.image : this.setImage(imageSource)}
72+
fillPatternScaleX={this.state.image ? width / this.state.image.width : 1}
73+
fillPatternScaleY={this.state.image ? height / this.state.image.height : 1}
74+
strokeWidth={2}
7975
strokeScaleEnabled={false}
8076
draggable={false}
81-
// centerScaling={true}
82-
// enabledAnchors={['top-left', 'top-right', 'bottom-left', 'bottom-right']}
8377
/>
8478
{childType === 'COMP'
8579
&& components
@@ -91,21 +85,11 @@ class GrandchildRectangle extends Component {
9185
components={components}
9286
componentId={componentId}
9387
childType={grandchild.childType}
94-
imageSource={
95-
grandchild.htmlElement == 'Image' && grandchild.HTMLInfo.Src
96-
? grandchild.HTMLInfo.Src
97-
: null
98-
}
88+
imageSource={grandchild.htmlElement === 'Image' && grandchild.HTMLInfo.Src}
9989
childComponentName={grandchild.componentName}
10090
childComponentId={grandchild.childComponentId}
10191
focusChild={focusChild}
10292
childId={childId}
103-
// fillPatternImage={
104-
// grandchild.HTMLInfo.Src
105-
// ? this.setImage(grandchild.HTMLInfo.Src)
106-
// : null
107-
// }
108-
// test test
10993
width={grandchild.position.width * (width / this.getPseudoChild().position.width)}
11094
height={
11195
grandchild.position.height * (height / this.getPseudoChild().position.height)

src/components/KonvaStage.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class KonvaStage extends Component {
4949
// here we should add listener for "container" resize
5050
// take a look here https://developers.google.com/web/updates/2016/10/resizeobserver
5151
// for simplicity I will just listen window resize
52-
// window.addEventListener('resize', this.checkSize);
52+
window.addEventListener('resize', this.checkSize);
5353
this.container.addEventListener('keydown', this.handleKeyDown);
5454
this.createGrid();
5555
}
5656

5757
componentWillUnmount() {
58-
// window.removeEventListener('resize', this.checkSize);
58+
window.removeEventListener('resize', this.checkSize);
5959
this.container.removeEventListener('keydown', this.handleKeyDown);
6060
}
6161

@@ -216,9 +216,7 @@ class KonvaStage extends Component {
216216
handleTransform={handleTransform}
217217
draggable={true}
218218
blockSnapSize={this.state.blockSnapSize}
219-
imageSource={
220-
child.htmlElement === 'Image' && child.HTMLInfo.Src ? child.HTMLInfo.Src : null
221-
}
219+
imageSource={child.htmlElement === 'Image' && child.HTMLInfo.Src}
222220
/>
223221
))
224222
.sort((rectA, rectB) => {

src/components/Rectangle.jsx

Lines changed: 49 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import React, { Component, Fragment } from "react";
2-
import { Rect, Group, Label, Text } from "react-konva";
3-
import TransformerComponent from "./TransformerComponent.jsx";
4-
import GrandchildRectangle from "./GrandchildRectangle.jsx";
1+
import React, { Component, Fragment } from 'react';
2+
import {
3+
Rect, Group, Label, Text,
4+
} from 'react-konva';
5+
import TransformerComponent from './TransformerComponent.jsx';
6+
import GrandchildRectangle from './GrandchildRectangle.jsx';
57

68
class Rectangle extends Component {
79
state = {
8-
rectImage: null
10+
image: null,
11+
imageHeight: 0,
12+
imageWidth: 0,
913
};
1014

1115
getComponentColor(componentId) {
12-
if (componentId === "888") {
13-
return "#000000";
14-
}
15-
const color = this.props.components.find(comp => comp.id === componentId)
16-
.color;
16+
const color = this.props.components.find(comp => comp.id === componentId).color;
1717
return color;
1818
}
1919

2020
getPseudoChild() {
21-
return this.props.components.find(
22-
comp => comp.id === this.props.childComponentId
23-
);
21+
return this.props.components.find(comp => comp.id === this.props.childComponentId);
2422
}
2523

2624
handleResize(componentId, childId, target, blockSnapSize) {
@@ -31,19 +29,13 @@ class Rectangle extends Component {
3129
.childrenArray.find(child => child.childId === childId);
3230

3331
if (childId === -1) {
34-
focChild = this.props.components.find(
35-
comp => comp.id === this.props.componentId
36-
);
32+
focChild = this.props.components.find(comp => comp.id === this.props.componentId);
3733
}
3834
const transformation = {
39-
width:
40-
Math.round((target.width() * target.scaleX()) / blockSnapSize) *
41-
blockSnapSize,
42-
height:
43-
Math.round((target.height() * target.scaleY()) / blockSnapSize) *
44-
blockSnapSize,
35+
width: Math.round((target.width() * target.scaleX()) / blockSnapSize) * blockSnapSize,
36+
height: Math.round((target.height() * target.scaleY()) / blockSnapSize) * blockSnapSize,
4537
x: target.x() + focChild.position.x,
46-
y: target.y() + focChild.position.y
38+
y: target.y() + focChild.position.y,
4739
};
4840

4941
this.props.handleTransform(componentId, childId, transformation);
@@ -52,19 +44,17 @@ class Rectangle extends Component {
5244
handleDrag(componentId, childId, target, blockSnapSize) {
5345
const transformation = {
5446
x: Math.round(target.x() / blockSnapSize) * blockSnapSize,
55-
y: Math.round(target.y() / blockSnapSize) * blockSnapSize
47+
y: Math.round(target.y() / blockSnapSize) * blockSnapSize,
5648
};
5749
this.props.handleTransform(componentId, childId, transformation);
5850
}
5951

60-
setImage = imageSource => {
52+
setImage = (imageSource) => {
6153
if (!imageSource) return;
6254
const image = new window.Image();
63-
6455
image.src = imageSource;
65-
// if there was an error grtting img; heigth should b Zero
6656
if (!image.height) return null;
67-
return image;
57+
this.setState({ image });
6858
};
6959

7060
render() {
@@ -85,7 +75,7 @@ class Rectangle extends Component {
8575
draggable,
8676
blockSnapSize,
8777
childType,
88-
imageSource
78+
imageSource,
8979
} = this.props;
9080

9181
// the Group is responsible for dragging of all children
@@ -99,22 +89,20 @@ class Rectangle extends Component {
9989
scaleY={scaleY}
10090
width={width}
10191
height={height}
102-
onDragEnd={event =>
103-
this.handleDrag(componentId, childId, event.target, blockSnapSize)
104-
}
105-
ref={node => {
92+
onDragEnd={event => this.handleDrag(componentId, childId, event.target, blockSnapSize)}
93+
ref={(node) => {
10694
this.group = node;
10795
}}
10896
tabIndex="0" // required for keypress event to be heard by this.group
10997
>
11098
<Rect
11199
// a Konva Rect is generated for each child of the focusComponent (including the pseudochild, representing the focusComponent itself)
112-
ref={node => {
100+
ref={(node) => {
113101
this.rect = node;
114102
}}
115103
tabIndex="0" // required for keypress event to be heard by this.group
116104
name={`${childId}`}
117-
className={"childRect"}
105+
className={'childRect'}
118106
x={0}
119107
y={0}
120108
childId={childId}
@@ -124,47 +112,34 @@ class Rectangle extends Component {
124112
scaleY={1}
125113
width={width}
126114
height={height}
127-
stroke={
128-
childType === "COMP"
129-
? this.getComponentColor(childComponentId)
130-
: "#000000"
131-
}
132-
// fill={color}
133-
// opacity={0.8}
134-
onTransformEnd={event =>
135-
this.handleResize(componentId, childId, event.target, blockSnapSize)
115+
stroke={childType === 'COMP' ? this.getComponentColor(childComponentId) : '#000000'}
116+
onTransformEnd={event => this.handleResize(componentId, childId, event.target, blockSnapSize)
136117
}
137-
strokeWidth={childType === "COMP" ? 4 : 1}
118+
strokeWidth={childType === 'COMP' ? 4 : 2}
138119
strokeScaleEnabled={false}
139120
draggable={false}
140-
fill={childId === -1 ? "white" : null}
121+
fill={childId === -1 ? 'white' : null}
141122
shadowBlur={childId === -1 ? 6 : null}
142-
fillPatternImage={imageSource ? this.setImage(imageSource) : null}
143-
144-
// fillPatternImage={null}
145-
// dashEnabled={childId === "-1"} // dash line only enabled for pseudochild
146-
// dash={[10, 3]} // 10px dashes with 3px gaps
123+
fillPatternImage={this.state.image ? this.state.image : this.setImage(imageSource)}
124+
fillPatternScaleX={this.state.image ? width / this.state.image.width : 1}
125+
fillPatternScaleY={this.state.image ? height / this.state.image.height : 1}
147126
/>
148127
<Label>
149128
<Text
150-
fontStyle={"bold"}
151-
fontVariant={"small-caps"}
129+
fontStyle={'bold'}
130+
fontVariant={'small-caps'}
152131
// pseudochild's label should look different than normal children:
153132
text={childId === -1 ? title.slice(0, title.length - 2) : title}
154-
fill={
155-
childId === -1
156-
? this.getComponentColor(childComponentId)
157-
: "#000000"
158-
}
133+
fill={childId === -1 ? this.getComponentColor(childComponentId) : '#000000'}
159134
fontSize={childId === -1 ? 15 : 10}
160135
x={4}
161136
y={childId === -1 ? -20 : -12}
162137
/>
163138
</Label>
164139
{// for all children other than the pseudoChild, find their component's children array and recursively render the children found there
165-
childId !== -1 &&
166-
childType === "COMP" &&
167-
components
140+
childId !== -1
141+
&& childType === 'COMP'
142+
&& components
168143
.find(comp => comp.title === childComponentName)
169144
.childrenArray.filter(child => child.childId !== -1)
170145
.map((grandchild, i) => (
@@ -174,43 +149,35 @@ class Rectangle extends Component {
174149
componentId={componentId}
175150
directParentName={childComponentName}
176151
childType={grandchild.childType}
177-
imageSource={
178-
grandchild.htmlElement == "Image" && grandchild.HTMLInfo.Src
179-
? grandchild.HTMLInfo.Src
180-
: null
181-
}
152+
imageSource={grandchild.htmlElement === 'Image' && grandchild.HTMLInfo.Src}
182153
childComponentName={grandchild.componentName}
183154
childComponentId={grandchild.childComponentId}
184155
focusChild={focusChild}
185156
childId={childId} // scary addition, grandchildren rects default to childId of "direct" children
186-
width={
187-
grandchild.position.width *
188-
(width / this.getPseudoChild().position.width)
189-
}
157+
width={grandchild.position.width * (width / this.getPseudoChild().position.width)}
190158
height={
191-
grandchild.position.height *
192-
(height / this.getPseudoChild().position.height)
159+
grandchild.position.height * (height / this.getPseudoChild().position.height)
193160
}
194161
x={
195-
(grandchild.position.x - this.getPseudoChild().position.x) *
196-
(width / this.getPseudoChild().position.width)
162+
(grandchild.position.x - this.getPseudoChild().position.x)
163+
* (width / this.getPseudoChild().position.width)
197164
}
198165
y={
199-
(grandchild.position.y - this.getPseudoChild().position.y) *
200-
(height / this.getPseudoChild().position.height)
166+
(grandchild.position.y - this.getPseudoChild().position.y)
167+
* (height / this.getPseudoChild().position.height)
201168
}
202169
/>
203170
))}
204-
{focusChild &&
205-
focusChild.childId === childId &&
206-
draggable && (
171+
{focusChild
172+
&& focusChild.childId === childId
173+
&& draggable && (
207174
<TransformerComponent
208175
focusChild={focusChild}
209-
rectClass={"childRect"}
176+
rectClass={'childRect'}
210177
anchorSize={8}
211-
color={"grey"}
178+
color={'grey'}
212179
/>
213-
)}
180+
)}
214181
</Group>
215182
);
216183
}

src/utils/Interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ChildInt {
2222
position: PositionInt;
2323
color: string | null; // maybe optional instead, look up null vs undefined
2424
htmlElement: string | null; // maybe should be optional instead
25-
HTMLInfo: object; // replace with HTMLinfo specifics
25+
HTMLInfo: { [index: string]: string }; // replace with HTMLinfo specifics
2626
}
2727

2828
export interface ChildrenInt extends Array<ChildInt> {}
@@ -48,8 +48,8 @@ export interface ApplicationStateInt {
4848
successOpen: boolean;
4949
errorOpen: boolean;
5050
focusComponent: ComponentInt;
51-
selectableChildren: Array<number>;
52-
ancestors: Array<number>;
51+
selectableChildren: number[];
52+
ancestors: number[];
5353
initialApplicationFocusChild: ChildInt;
5454
focusChild: ChildInt;
5555
components: ComponentsInt;

0 commit comments

Comments
 (0)