Skip to content

Commit cfad4f6

Browse files
committed
Clear image functionality implemented
1 parent a14728d commit cfad4f6

File tree

7 files changed

+56
-11
lines changed

7 files changed

+56
-11
lines changed

src/actionTypes/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ export const DELETE_ALL_DATA = 'DELETE_ALL_DATA';
2727
export const CHANGE_IMAGE_PATH = 'CHANGE_IMAGE_PATH';
2828
export const UPDATE_HTML_ATTR = 'UPDATE_HTML_ATTR';
2929
export const UPDATE_CHILDREN_SORT = 'UPDATE_CHILDREN_SORT';
30-
export const CHANGE_IMAGE_SOURCE = 'CHANGE_IMAGE_SOURCE';
30+
export const CHANGE_IMAGE_SOURCE = 'CHANGE_IMAGE_SOURCE';
31+
export const DELETE_IMAGE = 'DELETE_IMAGE';

src/actions/components.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import {
2525
DELETE_ALL_DATA,
2626
UPDATE_HTML_ATTR,
2727
UPDATE_CHILDREN_SORT,
28-
CHANGE_IMAGE_SOURCE
28+
CHANGE_IMAGE_SOURCE,
29+
DELETE_IMAGE
2930
} from '../actionTypes/index.js';
3031

3132
import { loadState } from '../localStorage';
@@ -118,6 +119,12 @@ childId: number;
118119
});
119120
};
120121

122+
export const deleteImage = () => ({
123+
type: DELETE_IMAGE,
124+
payload: ''
125+
})
126+
127+
121128
export const exportFiles = ({
122129
components,
123130
path,

src/containers/AppContainer.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ type Props = {
2121
selectableChildren: Array<number>;
2222
loadInitData: any;
2323
changeImagePath: any;
24+
changed: boolean;
2425
};
2526

2627
type State = {
2728
image: HTMLImageElement | null;
2829
width: number;
30+
changed: boolean;
2931
}
3032

3133
const mapStateToProps = (store: any) => ({
@@ -50,6 +52,7 @@ class AppContainer extends Component<Props, State> {
5052
this.state = {
5153
image: null,
5254
width: 25,
55+
changed: false
5356
};
5457

5558
IPC.on('new-file', (event, file: string) => {
@@ -69,25 +72,30 @@ class AppContainer extends Component<Props, State> {
6972
// };
7073
componentDidUpdate(prevProps: Props) {
7174
const { imageSource } = this.props;
72-
if (imageSource !== prevProps.imageSource) {
75+
const {changed} = this.state;
76+
if (imageSource == '' && changed) {
77+
this.setState({...this.state, image:null, changed:false});
78+
79+
}
80+
else if (imageSource !== prevProps.imageSource) {
7381
this.setImage(imageSource);
7482
}
7583
}
7684

7785
setImage = (imageSource: string) => {
86+
if (imageSource) {
7887
let image: HTMLImageElement;
7988
image = new window.Image();
8089
image.src = imageSource;
8190
image.onload = () => {
8291
// setState will redraw layer
8392
// because "image" property is changed
84-
this.setState({ image });
93+
this.setState({ image, changed: true });
8594
};
95+
}
8696
};
8797

8898
clearImage = () => {
89-
const { changeImagePath } = this.props;
90-
changeImagePath('');
9199
this.setState({
92100
image: null
93101
})
@@ -117,6 +125,7 @@ class AppContainer extends Component<Props, State> {
117125
focusComponent={focusComponent}
118126
selectableChildren={selectableChildren}
119127
setImage={this.setImage}
128+
clearImage={this.clearImage}
120129
/>
121130
<MainContainer components={components} image={this.state.image}
122131
imageSource={this.props.imageSource}/>

src/containers/LeftContainer.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface PropsInt {
3333
deleteComponent: any;
3434
createApp: any;
3535
deleteAllData: any;
36+
deleteImage: any;
3637
}
3738

3839
interface StateInt {
@@ -42,6 +43,10 @@ interface StateInt {
4243
genOption: number;
4344
}
4445

46+
const mapStateToProps = (store: any) => ({
47+
imageSource: store.workspace.imageSource
48+
});
49+
4550
const mapDispatchToProps = (dispatch: any) => ({
4651
addComponent: ({ title }: { title: string }) =>
4752
dispatch(actions.addComponent({ title })),
@@ -66,6 +71,7 @@ const mapDispatchToProps = (dispatch: any) => ({
6671
stateComponents: ComponentsInt;
6772
}) => dispatch(actions.deleteComponent({ componentId, stateComponents })),
6873
deleteAllData: () => dispatch(actions.deleteAllData()),
74+
deleteImage: () => dispatch(actions.deleteImage()),
6975
createApp: ({
7076
path,
7177
components,
@@ -99,7 +105,8 @@ class LeftContainer extends Component<PropsInt, StateInt> {
99105
"Export components",
100106
"Export components with application files"
101107
],
102-
genOption: 0
108+
genOption: 0.,
109+
imageSource: this.props.imageSource
103110
};
104111

105112
IPC.on("app_dir_selected", (event: any, path: string) => {
@@ -204,7 +211,8 @@ class LeftContainer extends Component<PropsInt, StateInt> {
204211
addChild,
205212
changeFocusComponent,
206213
changeFocusChild,
207-
selectableChildren
214+
selectableChildren,
215+
deleteImage
208216
} = this.props;
209217
const { componentName, modal } = this.state;
210218

@@ -225,7 +233,7 @@ class LeftContainer extends Component<PropsInt, StateInt> {
225233
components={components}
226234
/>
227235
));
228-
const { addImage } = this;
236+
const { addImage, clearImage } = this;
229237

230238
return (
231239
<div className="column left">
@@ -302,7 +310,8 @@ class LeftContainer extends Component<PropsInt, StateInt> {
302310
aria-label="Remove Image"
303311
variant="contained"
304312
fullWidth
305-
onClick={() => {}}
313+
onClick={deleteImage
314+
}
306315
className={classes.clearButton}
307316
style={{ borderRadius: 0, top: 0, backgroundColor: '#dc004e', color: '#fff' }}
308317
>
@@ -406,7 +415,7 @@ function styles(): any {
406415
export default compose(
407416
withStyles(styles),
408417
connect(
409-
null,
418+
mapStateToProps,
410419
mapDispatchToProps
411420
)
412421
)(LeftContainer);

src/reducers/componentReducer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CHANGE_FOCUS_CHILD,
1515
CHANGE_COMPONENT_FOCUS_CHILD,
1616
CHANGE_IMAGE_SOURCE,
17+
DELETE_IMAGE,
1718
EXPORT_FILES,
1819
CREATE_APPLICATION,
1920
EXPORT_FILES_SUCCESS,
@@ -35,6 +36,7 @@ import {
3536
addChild,
3637
deleteChild,
3738
deleteComponent,
39+
deleteImage,
3840
changeFocusComponent,
3941
changeComponentFocusChild,
4042
changeFocusChild,
@@ -131,6 +133,8 @@ const componentReducer = (state = initialApplicationState, action: any) => {
131133
return changeImageSource(state, action.payload);
132134
case EXPORT_FILES:
133135
return { ...state, loading: true };
136+
case DELETE_IMAGE:
137+
return deleteImage(state, action.payload);
134138
case EXPORT_FILES_SUCCESS:
135139
return exportFilesSuccess(state, action.payload);
136140
case CREATE_APPLICATION_ERROR:

src/utils/componentReducer.util.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ export const deleteChild = (
223223
};
224224
};
225225

226+
export const deleteImage = (state: ApplicationStateInt) => {
227+
return {
228+
...state,
229+
imageSource: ''
230+
}
231+
}
232+
226233
export const handleTransform = (
227234
state: ApplicationStateInt,
228235
{

src/utils/isEmpty.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const isEmpty = (obj: object) => {
2+
for (let key in obj) {
3+
if (obj.hasOwnProperty(key)) return false;
4+
}
5+
return true;
6+
}
7+
8+
export default isEmpty;

0 commit comments

Comments
 (0)