Skip to content

Commit 931dc28

Browse files
authored
Merge pull request #16 from tonyito/mvp
Image resize functionality on Konva and typing updates
2 parents 81ff060 + b65dc00 commit 931dc28

File tree

9 files changed

+32
-102
lines changed

9 files changed

+32
-102
lines changed

src/actionTypes/index.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/actions/components.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export const exportFiles = ({
146146
dispatch({
147147
type: EXPORT_FILES
148148
});
149-
150149
createFiles(components, path, appName, exportAppBool)
151150
.then((dir: string) =>
152151
dispatch({

src/components/CodePreview.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ class CodePreview extends Component<Props> {
2626
}}
2727
>
2828
<SyntaxHighlighter style={hybrid}>
29-
{format(componentRender(focusComponent, components))}
29+
{format(componentRender(focusComponent, components), {
30+
singleQuote: true,
31+
trailingComma: 'es5',
32+
bracketSpacing: true,
33+
jsxBracketSameLine: true,
34+
parser: 'typescript'
35+
})}
3036
</SyntaxHighlighter>
3137
</div>
3238
);

src/components/KonvaStage.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//and also the parent rectangle components.
33

44
import React, { Component } from "react";
5-
import { Stage, Layer, Line, Image } from "react-konva";
6-
import Rectangle from "./Rectangle.tsx";
7-
import cloneDeep from "../utils/cloneDeep.ts";
8-
import { ComponentInt, ComponentsInt, ChildInt } from "../utils/Interfaces.ts";
5+
import { Stage, Layer, Line } from "react-konva";
6+
import Rectangle from "./Rectangle";
7+
import cloneDeep from "../utils/cloneDeep";
8+
import { ComponentInt, ComponentsInt, ChildInt } from "../utils/Interfaces";
99
import isEmpty from '../utils/isEmpty';
1010

1111
interface PropsInt {
@@ -25,6 +25,9 @@ interface PropsInt {
2525
focusChild: any;
2626
changeComponentFocusChild: any;
2727
deleteChild: any;
28+
scaleX: number;
29+
scaleY: number;
30+
draggable: boolean;
2831
}
2932

3033
interface StateInt {
@@ -208,9 +211,6 @@ class KonvaStage extends Component<PropsInt, StateInt> {
208211
}}
209212
>
210213
{this.state.grid}
211-
<Image image={this.props.focusComponent.id === 1 ? image : null //only display background image if the focused component is <App>
212-
} draggable width={this.state.stageWidth*0.8} height={this.state.stageHeight*0.9 } //for background image uploaded, fix to fit screen
213-
/>
214214
{!isEmpty(focusComponent) && this.getDirectChildrenCopy(focusComponent)
215215
.map((child: ChildInt, i: number) => (
216216
<Rectangle
@@ -232,9 +232,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
232232
handleTransform={handleTransform}
233233
draggable={true}
234234
blockSnapSize={this.state.blockSnapSize}
235-
imageSource={
236-
child.htmlElement === "Image" && child.HTMLInfo.Src
237-
}
235+
image={this.props.focusComponent.id === 1 ? image : null}
238236
/>
239237
))
240238
.sort((rectA, rectB) => {

src/components/Rectangle.tsx

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, { Component } from 'react';
2+
import Konva from 'konva';
23
import { Rect, Group, Label, Text } from 'react-konva';
3-
import TransformerComponent from './TransformerComponent.tsx';
4-
import GrandchildRectangle from './GrandchildRectangle.tsx';
5-
import { ComponentsInt, ChildInt } from '../utils/Interfaces.ts';
6-
import { ComponentInt } from '../utils/Interfaces.ts';
4+
import TransformerComponent from './TransformerComponent';
5+
import GrandchildRectangle from './GrandchildRectangle';
6+
import { ComponentsInt, ChildInt } from '../utils/Interfaces';
7+
import { ComponentInt } from '../utils/Interfaces';
78

89
interface PropsInt {
910
x: number;
@@ -22,18 +23,12 @@ interface PropsInt {
2223
draggable: boolean;
2324
blockSnapSize: number;
2425
childType: string;
25-
imageSource: string;
2626
handleTransform: any;
27+
image: HTMLImageElement;
2728
}
2829

29-
interface StateInt {
30-
image: any;
31-
}
3230

33-
class Rectangle extends Component<PropsInt, StateInt> {
34-
state = {
35-
image: null
36-
};
31+
class Rectangle extends Component<PropsInt> {
3732

3833
getComponentColor(componentId: number) {
3934
const color = this.props.components.find(
@@ -48,13 +43,8 @@ class Rectangle extends Component<PropsInt, StateInt> {
4843
);
4944
}
5045

51-
handleResize(
52-
componentId: number,
53-
childId: number,
54-
target: any,
55-
blockSnapSize: number
56-
) {
57-
let focChild: ChildInt = this.props.components
46+
handleResize(componentId: number, childId: number, target: Konva.Group, blockSnapSize: number) {
47+
let focChild: ChildInt | ComponentInt = this.props.components
5848
.find((comp: ComponentInt) => comp.id === this.props.componentId)
5949
.childrenArray.find((child: ChildInt) => child.childId === childId);
6050

@@ -76,27 +66,14 @@ class Rectangle extends Component<PropsInt, StateInt> {
7666
this.props.handleTransform(componentId, childId, transformation);
7767
}
7868

79-
handleDrag(
80-
componentId: number,
81-
childId: number,
82-
target: any,
83-
blockSnapSize: any
84-
) {
69+
handleDrag(componentId: number, childId: number, target: Konva.Group, blockSnapSize: number) {
8570
const transformation = {
8671
x: Math.round(target.x() / blockSnapSize) * blockSnapSize,
8772
y: Math.round(target.y() / blockSnapSize) * blockSnapSize
8873
};
8974
this.props.handleTransform(componentId, childId, transformation);
9075
}
9176

92-
setImage = (imageSource: string): void => {
93-
if (!imageSource) return;
94-
const image = new window.Image();
95-
image.src = imageSource;
96-
if (!image.height) return null;
97-
this.setState({ image });
98-
};
99-
10077
render() {
10178
const {
10279
x,
@@ -114,8 +91,7 @@ class Rectangle extends Component<PropsInt, StateInt> {
11491
components,
11592
draggable,
11693
blockSnapSize,
117-
childType,
118-
imageSource
94+
childType
11995
} = this.props;
12096

12197
// the Group is responsible for dragging of all children
@@ -167,13 +143,9 @@ class Rectangle extends Component<PropsInt, StateInt> {
167143
draggable={false}
168144
fill={null}
169145
shadowBlur={childId === -1 ? 6 : null}
170-
fillPatternImage={this.state.image ? this.state.image : null}
171-
fillPatternScaleX={
172-
this.state.image ? width / this.state.image.width : 1
173-
}
174-
fillPatternScaleY={
175-
this.state.image ? height / this.state.image.height : 1
176-
}
146+
fillPatternImage={childId === -1 ? this.props.image : null} //spooky addition, image if uploaded will only be background of App component
147+
fillPatternScaleX={this.props.image ? width / this.props.image.width : 1}
148+
fillPatternScaleY={this.props.image? height / this.props.image.height : 1}
177149
_useStrictMode
178150
/>
179151
<Label>
@@ -205,9 +177,6 @@ class Rectangle extends Component<PropsInt, StateInt> {
205177
componentId={componentId}
206178
directParentName={childComponentName}
207179
childType={grandchild.childType}
208-
imageSource={
209-
grandchild.htmlElement === 'Image' && grandchild.HTMLInfo.Src
210-
}
211180
childComponentName={grandchild.componentName}
212181
childComponentId={grandchild.childComponentId}
213182
focusChild={focusChild}
@@ -231,7 +200,7 @@ class Rectangle extends Component<PropsInt, StateInt> {
231200
/>
232201
))}
233202
{focusChild && focusChild.childId === childId && draggable && (
234-
<TransformerComponent
203+
<TransformerComponent //This is the component that binds the Rect nodes to the Transformer node so they can be resized.
235204
focusChild={focusChild}
236205
rectClass={'childRect'}
237206
anchorSize={8}

src/components/SimpleModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const SimpleModal = (props: any) => {
7070
>
7171
<CloseIcon />
7272
</IconButton>
73-
<Typography variant="title" id="modal-title">
73+
<Typography variant="h6" id="modal-title">
7474
{message}
7575
</Typography>
7676
<div>{children}</div>

src/containers/MainContainer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import KonvaStage from '../components/KonvaStage';
1616
import { ComponentInt, ComponentsInt } from '../utils/Interfaces';
1717
import * as actions from '../actions/components';
18+
import { changeImageSource } from "../utils/componentReducer.util";
1819

1920
interface PropsInt {
2021
image: HTMLImageElement | null;

src/reducers/index.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/utils/createFiles.util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const createFiles = (
4646

4747
promises.push(newPromise);
4848
});
49-
5049
return Promise.all(promises);
5150
};
5251

0 commit comments

Comments
 (0)