-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.tsx
141 lines (136 loc) · 3.84 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
Button,
Grid,
WithStyles,
withStyles,
createStyles,
Theme,
Tooltip,
Typography,
} from '@material-ui/core';
import React, { Fragment } from 'react';
import { inject, observer } from 'mobx-react';
import AttachmentIcon from '@material-ui/icons/Attachment';
import { EDITOR_STORE, EditorStoreProp } from '../../stores';
const styles = (theme: Theme) =>
createStyles({
previewImgRoot: {
position: 'relative',
'&:hover div': {
visibility: 'visible',
},
},
removeImage: {
height: theme.spacing.unit * 3,
width: theme.spacing.unit * 3,
top: -theme.spacing.unit,
right: -theme.spacing.unit,
fontFamily: theme.typography.fontFamily,
background: '#d20000',
position: 'absolute',
cursor: 'pointer',
color: '#fff',
borderRadius: '50%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
visibility: 'hidden',
},
input: {
display: 'none',
},
preview: {
display: 'flex',
justifyContent: 'center',
flexDirection: 'row',
},
previewImg: {
height: theme.spacing.unit * 12,
boxSizing: 'border-box',
border: `1px dotted ${theme.palette.background.default}`,
},
button: {
margin: theme.spacing.unit,
marginLeft: 0,
},
rightIcon: {
marginLeft: theme.spacing.unit,
},
});
@inject(EDITOR_STORE)
@observer
class ImportBackground extends React.Component<
EditorStoreProp & WithStyles<typeof styles>
> {
handleChange = (event: any) => {
const { setBackground, setOpenBackgroundDialog } = this.props.editorStore!;
const background = event.target.files[0];
if (background) {
setOpenBackgroundDialog(false);
setBackground(background);
}
};
render() {
const { editorStore, classes } = this.props;
const { background, resetBackground } = editorStore!;
const imageData: any = background.data;
return (
<Fragment>
<Grid item xs={12}>
<Grid container spacing={8}>
<Grid item xs={3} />
<Grid item xs={6}>
<input
accept="image/*"
className={classes.input}
id="button-file"
type="file"
onChange={this.handleChange}
/>
<label htmlFor="button-file">
<Tooltip
disableFocusListener
title="*.png | *.jpeg | *.jpg"
placement="bottom"
>
<Button
variant="contained"
color="default"
className={classes.button}
fullWidth
component="span"
>
{'Choose background ...'}
<AttachmentIcon className={classes.rightIcon} />
</Button>
</Tooltip>
</label>
</Grid>
<Grid item xs={3} />
{imageData ? (
<Grid container spacing={8} justify="center">
<Grid item xs={12} className={classes.preview}>
<div className={classes.previewImgRoot}>
<img src={imageData} className={classes.previewImg} />
<div
className={classes.removeImage}
onClick={resetBackground}
>
x
</div>
</div>
</Grid>
<Grid item>
<Typography>
{background.size.width}x{background.size.height}
</Typography>
</Grid>
</Grid>
) : null}
</Grid>
</Grid>
</Fragment>
);
}
}
export default withStyles(styles)(ImportBackground);