Skip to content

Commit e3aa529

Browse files
committed
fixed toggle state functionality
2 parents 2b87b85 + 1a40a57 commit e3aa529

15 files changed

+630
-340
lines changed

src/actionTypes/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const ADD_CHILD = 'ADD_CHILD';
44
export const DELETE_CHILD = 'DELETE_CHILD';
55
export const UPDATE_COMPONENT = 'UPDATE_COMPONENT';
66
export const DELETE_COMPONENT = 'DELETE_COMPONENT';
7+
export const TOGGLE_STATE = 'TOGGLE_STATE';
78
export const CHANGE_FOCUS_COMPONENT = 'CHANGE_FOCUS_COMPONENT';
89
export const CHANGE_COMPONENT_FOCUS_CHILD = 'CHANGE_COMPONENT_FOCUS_CHILD';
910
export const CHANGE_FOCUS_CHILD = 'CHANGE_FOCUS_CHILD';
@@ -26,4 +27,7 @@ export const ADD_PROP = 'ADD_PROP';
2627
export const DELETE_ALL_DATA = 'DELETE_ALL_DATA';
2728
export const CHANGE_IMAGE_PATH = 'CHANGE_IMAGE_PATH';
2829
export const UPDATE_HTML_ATTR = 'UPDATE_HTML_ATTR';
29-
export const UPDATE_CHILDREN_SORT = 'UPDATE_CHILDREN_SORT'
30+
export const UPDATE_CHILDREN_SORT = 'UPDATE_CHILDREN_SORT';
31+
export const CHANGE_IMAGE_SOURCE = 'CHANGE_IMAGE_SOURCE';
32+
export const DELETE_IMAGE = 'DELETE_IMAGE';
33+

src/actions/components.ts

Lines changed: 87 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import {
2-
ComponentInt, ComponentsInt, PropInt, ChildInt,
3-
} from '../utils/Interfaces.ts';
1+
import { ComponentInt, ComponentsInt, PropInt, ChildInt } from '../utils/Interfaces.ts';
42

53
import {
64
LOAD_INIT_DATA,
75
ADD_COMPONENT,
86
ADD_CHILD,
97
DELETE_CHILD,
108
DELETE_COMPONENT,
9+
TOGGLE_STATE,
1110
CHANGE_FOCUS_COMPONENT,
1211
CHANGE_FOCUS_CHILD,
1312
CHANGE_COMPONENT_FOCUS_CHILD,
@@ -25,19 +24,28 @@ import {
2524
DELETE_ALL_DATA,
2625
UPDATE_HTML_ATTR,
2726
UPDATE_CHILDREN_SORT,
27+
CHANGE_IMAGE_SOURCE,
28+
DELETE_IMAGE
2829
} from '../actionTypes/index.js';
2930

3031
import { loadState } from '../localStorage';
3132
import createFiles from '../utils/createFiles.util.ts';
3233
import createApplicationUtil from '../utils/createApplication.util.ts';
3334

35+
export const changeImagePath = (imageSource: string) => ({
36+
type: CHANGE_IMAGE_SOURCE,
37+
payload: imageSource
38+
});
39+
3440
export const loadInitData = () => (dispatch: any) => {
35-
loadState().then((data: any) => dispatch({
36-
type: LOAD_INIT_DATA,
37-
payload: {
38-
data: data ? data.workspace : {},
39-
},
40-
}));
41+
loadState().then((data: any) =>
42+
dispatch({
43+
type: LOAD_INIT_DATA,
44+
payload: {
45+
data: data ? data.workspace : {}
46+
}
47+
})
48+
);
4149
};
4250

4351
export const addComponent = ({ title }: { title: string }) => (dispatch: any) => {
@@ -47,11 +55,11 @@ export const addComponent = ({ title }: { title: string }) => (dispatch: any) =>
4755
export const addChild = ({
4856
title,
4957
childType,
50-
HTMLInfo,
58+
HTMLInfo
5159
}: {
52-
title: string;
53-
childType: string;
54-
HTMLInfo: object;
60+
title: string;
61+
childType: string;
62+
HTMLInfo: object;
5563
}) => (dispatch: any) => {
5664
dispatch({ type: ADD_CHILD, payload: { title, childType, HTMLInfo } });
5765
};
@@ -63,10 +71,10 @@ export const deleteChild = ({}) => (dispatch: any) => {
6371

6472
export const deleteComponent = ({
6573
componentId,
66-
stateComponents,
74+
stateComponents
6775
}: {
68-
componentId: number;
69-
stateComponents: ComponentsInt;
76+
componentId: number;
77+
stateComponents: ComponentsInt;
7078
}) => (dispatch: any) => {
7179
// find all places where the "to be deleted" is a child and do what u gotta do
7280
stateComponents.forEach((parent: ComponentInt) => {
@@ -78,8 +86,8 @@ stateComponents: ComponentsInt;
7886
payload: {
7987
parentId: parent.id,
8088
childId: child.childId,
81-
calledFromDeleteComponent: true,
82-
},
89+
calledFromDeleteComponent: true
90+
}
8391
});
8492
});
8593
});
@@ -101,55 +109,62 @@ export const changeFocusChild = ({ childId }: { childId: number }) => (dispatch:
101109

102110
export const changeComponentFocusChild = ({
103111
componentId,
104-
childId,
112+
childId
105113
}: {
106-
componentId: number;
107-
childId: number;
114+
componentId: number;
115+
childId: number;
108116
}) => (dispatch: any) => {
109117
dispatch({
110118
type: CHANGE_COMPONENT_FOCUS_CHILD,
111-
payload: { componentId, childId },
119+
payload: { componentId, childId }
112120
});
113121
};
114122

123+
export const deleteImage = () => ({
124+
type: DELETE_IMAGE,
125+
payload: ''
126+
});
127+
115128
export const exportFiles = ({
116129
components,
117130
path,
118131
appName,
119-
exportAppBool,
132+
exportAppBool
120133
}: {
121-
components: ComponentsInt;
122-
path: string;
123-
appName: string;
124-
exportAppBool: boolean;
134+
components: ComponentsInt;
135+
path: string;
136+
appName: string;
137+
exportAppBool: boolean;
125138
}) => (dispatch: any) => {
126139
// this dispatch sets the global state property 'loading' to true until the createFiles call resolves below
127140
dispatch({
128-
type: EXPORT_FILES,
141+
type: EXPORT_FILES
129142
});
130143

131144
createFiles(components, path, appName, exportAppBool)
132-
.then(dir => dispatch({
133-
type: EXPORT_FILES_SUCCESS,
134-
payload: { status: true, dir: dir[0] },
135-
}))
136-
.catch(err => dispatch({
137-
type: EXPORT_FILES_ERROR,
138-
payload: { status: true, err },
139-
}));
145+
.then(dir =>
146+
dispatch({
147+
type: EXPORT_FILES_SUCCESS,
148+
payload: { status: true, dir: dir[0] }
149+
})
150+
)
151+
.catch(err =>
152+
dispatch({
153+
type: EXPORT_FILES_ERROR,
154+
payload: { status: true, err }
155+
})
156+
);
140157
};
141158

142159
export const handleClose = () => ({
143160
type: HANDLE_CLOSE,
144-
payload: false,
161+
payload: false
145162
});
146163

147164
export const handleTransform = (
148165
componentId: number,
149166
childId: number,
150-
{
151-
x, y, width, height,
152-
}: { x: number; y: number; width: number; height: number },
167+
{ x, y, width, height }: { x: number; y: number; width: number; height: number }
153168
) => ({
154169
type: HANDLE_TRANSFORM,
155170
payload: {
@@ -158,22 +173,22 @@ export const handleTransform = (
158173
x,
159174
y,
160175
width,
161-
height,
162-
},
176+
height
177+
}
163178
});
164179

165180
export const createApplication = ({
166181
path,
167182
components = [],
168183
genOption,
169184
appName = 'reactype_app',
170-
exportAppBool,
185+
exportAppBool
171186
}: {
172-
path: string;
173-
components: ComponentsInt;
174-
genOption: number;
175-
appName: string;
176-
exportAppBool: boolean;
187+
path: string;
188+
components: ComponentsInt;
189+
genOption: number;
190+
appName: string;
191+
exportAppBool: boolean;
177192
}) => (dispatch: any) => {
178193
if (genOption === 0) {
179194
exportAppBool = false;
@@ -182,72 +197,78 @@ exportAppBool: boolean;
182197
appName,
183198
path,
184199
components,
185-
exportAppBool,
186-
}),
200+
exportAppBool
201+
})
187202
);
188203
} else if (genOption) {
189204
exportAppBool = true;
190205
dispatch({
191-
type: CREATE_APPLICATION,
206+
type: CREATE_APPLICATION
192207
});
193208
createApplicationUtil({
194209
path,
195210
appName,
196-
genOption,
211+
genOption
197212
// exportAppBool
198213
})
199214
.then(() => {
200215
dispatch({
201-
type: CREATE_APPLICATION_SUCCESS,
216+
type: CREATE_APPLICATION_SUCCESS
202217
});
203218
dispatch(
204219
exportFiles({
205220
appName,
206221
path,
207222
components,
208-
exportAppBool,
209-
}),
223+
exportAppBool
224+
})
210225
);
211226
})
212-
.catch(err => dispatch({
213-
type: CREATE_APPLICATION_ERROR,
214-
payload: { status: true, err },
215-
}));
227+
.catch(err =>
228+
dispatch({
229+
type: CREATE_APPLICATION_ERROR,
230+
payload: { status: true, err }
231+
})
232+
);
216233
}
217234
};
218235

219236
export const openExpansionPanel = (component: ComponentInt) => ({
220237
type: OPEN_EXPANSION_PANEL,
221-
payload: { component },
238+
payload: { component }
222239
});
223240

224241
export const deleteAllData = () => ({
225-
type: DELETE_ALL_DATA,
242+
type: DELETE_ALL_DATA
226243
});
227244

228245
export const deleteProp = (propId: number) => (dispatch: any) => {
229246
dispatch({ type: DELETE_PROP, payload: propId });
230247
};
231248

249+
export const toggleComponentState = (index: string) => (dispatch: any) => {
250+
dispatch({ type: TOGGLE_STATE, payload: index });
251+
};
252+
232253
export const addProp = (prop: PropInt) => ({
233254
type: ADD_PROP,
234-
payload: { ...prop },
255+
payload: { ...prop }
235256
});
236257

237258
export const updateHtmlAttr = ({ attr, value }: { attr: string; value: string }) => (
238-
dispatch: any,
259+
dispatch: any
239260
) => {
240261
dispatch({
241262
type: UPDATE_HTML_ATTR,
242-
payload: { attr, value },
263+
payload: { attr, value }
243264
});
244265
};
245266

246267
export const updateChildrenSort = ({ newSortValues }: { newSortValues: any }) => (
247-
dispatch: any,
268+
dispatch: any
248269
) => {
249270
dispatch({
250271
type: UPDATE_CHILDREN_SORT,
251-
payload: { newSortValues },
272+
payload: { newSortValues }
252273
});
253274
};

src/components/CodePreview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import { format } from 'prettier';
3-
import componentRender from '../utils/componentRender.util.ts';
4-
import { ComponentInt, ComponentsInt } from '../utils/Interfaces.ts';
3+
import componentRender from '../utils/componentRender.util';
4+
import { ComponentInt, ComponentsInt } from '../utils/Interfaces';
55
/** ** SortCHildren will be fixed , dont XXX the file *** */
66
// import SortChildren from './SortChildren.jsx';
77
import SyntaxHighlighter from 'react-syntax-highlighter';

src/components/KonvaStage.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
//This component includes a component for the background image to be uploaded as reference for drawing components
2+
//and also the parent rectangle components.
3+
14
import React, { Component } from "react";
2-
import { Stage, Layer, Line } from "react-konva";
5+
import { Stage, Layer, Line, Image } from "react-konva";
36
import Rectangle from "./Rectangle.tsx";
47
import cloneDeep from "../utils/cloneDeep.ts";
58
import { ComponentInt, ComponentsInt, ChildInt } from "../utils/Interfaces.ts";
9+
import isEmpty from '../utils/isEmpty';
610

711
interface PropsInt {
12+
image: HTMLImageElement;
813
components: ComponentsInt;
914
focusComponent: ComponentInt;
1015
selectableChildren: Array<number>;
@@ -102,6 +107,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
102107
}
103108
};
104109

110+
//event handler to handle mouse click
105111
handleStageMouseDown = (e: any) => {
106112
// clicked on stage - clear selection
107113
if (e.target === e.target.getStage()) {
@@ -116,7 +122,6 @@ class KonvaStage extends Component<PropsInt, StateInt> {
116122

117123
// find clicked rect by its name
118124
const rectChildId = e.target.attrs.childId;
119-
// console.log("user clicked on child rectangle with childId: ", rectChildId);
120125
this.props.changeFocusChild({ childId: rectChildId });
121126
this.props.changeComponentFocusChild({
122127
componentId: this.props.focusComponent.id,
@@ -167,6 +172,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
167172

168173
render() {
169174
const {
175+
image,
170176
components,
171177
handleTransform,
172178
focusComponent,
@@ -202,7 +208,10 @@ class KonvaStage extends Component<PropsInt, StateInt> {
202208
}}
203209
>
204210
{this.state.grid}
205-
{this.getDirectChildrenCopy(focusComponent)
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+
/>
214+
{!isEmpty(focusComponent) && this.getDirectChildrenCopy(focusComponent)
206215
.map((child: ChildInt, i: number) => (
207216
<Rectangle
208217
childType={child.childType}

0 commit comments

Comments
 (0)