Skip to content

Commit a0be4e0

Browse files
committed
Merge pull request #1 from netlify/imageWidget
Support for image preview & drag and drop
2 parents 75231be + 3e80bab commit a0be4e0

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

example/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
window.repoFiles = {
1010
_posts: {
1111
"2015-02-14-this-is-a-post.md": {
12-
content: "---\ntitle: This is a post\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n"
12+
content: "---\ntitle: This is a post\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n"
1313
}
1414
},
1515
_faqs: {

example/nf-logo.png

13.9 KB
Loading

src/components/Widgets/ImageControl.js

+100-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,112 @@ import React from 'react';
33
export default class ImageControl extends React.Component {
44
constructor(props) {
55
super(props);
6+
7+
this.state = {
8+
currentImage: props.value
9+
};
10+
11+
this.revokeCurrentImage = this.revokeCurrentImage.bind(this);
612
this.handleChange = this.handleChange.bind(this);
13+
this.handleFileInputRef = this.handleFileInputRef.bind(this);
14+
this.handleClick = this.handleClick.bind(this);
15+
this.handleDragEnter = this.handleDragEnter.bind(this);
16+
this.handleDragOver = this.handleDragOver.bind(this);
17+
this.renderImageName = this.renderImageName.bind(this);
18+
}
19+
20+
componentWillUnmount() {
21+
this.revokeCurrentImage();
22+
}
23+
24+
revokeCurrentImage() {
25+
if (this.state.currentImage instanceof File) {
26+
window.URL.revokeObjectURL(this.state.currentImage);
27+
}
28+
}
29+
30+
handleFileInputRef(el) {
31+
this._fileInput = el;
32+
}
33+
34+
handleClick(e) {
35+
this._fileInput.click();
36+
}
37+
38+
handleDragEnter(e) {
39+
e.stopPropagation();
40+
e.preventDefault();
41+
}
42+
43+
handleDragOver(e) {
44+
e.stopPropagation();
45+
e.preventDefault();
746
}
847

948
handleChange(e) {
10-
this.props.onChange(e.target.value);
49+
e.stopPropagation();
50+
e.preventDefault();
51+
this.revokeCurrentImage();
52+
const fileList = e.dataTransfer ? e.dataTransfer.files : e.target.files;
53+
const files = [...fileList];
54+
const imageType = /^image\//;
55+
56+
// Iterate through the list of files and return the first image on the list
57+
const file = files.find((currentFile) => {
58+
if (imageType.test(currentFile.type)) {
59+
return currentFile;
60+
}
61+
});
62+
63+
if (file) {
64+
// Custom toString function on file, so it can be used on regular image fields
65+
file.toString = function() {
66+
return window.URL.createObjectURL(file);
67+
};
68+
}
69+
70+
this.props.onChange(file);
71+
this.setState({currentImage: file});
72+
}
73+
74+
renderImageName() {
75+
if (!this.state.currentImage) return null;
76+
77+
if (this.state.currentImage instanceof File) {
78+
return this.state.currentImage.name;
79+
} else if (typeof this.state.currentImage === 'string') {
80+
const fileName = this.state.currentImage;
81+
return fileName.substring(fileName.lastIndexOf('/') + 1);
82+
}
83+
84+
return null;
1185
}
1286

1387
render() {
14-
return <input type="file" onChange={this.handleChange}/>;
88+
const imageName = this.renderImageName();
89+
return (
90+
<div
91+
onDragEnter={this.handleDragEnter}
92+
onDragOver={this.handleDragOver}
93+
onDrop={this.handleChange}
94+
>
95+
<span onClick={this.handleClick}>
96+
{imageName ? imageName : 'Click or drop imag here.'}
97+
</span>
98+
<input
99+
type="file"
100+
accept="image/*"
101+
onChange={this.handleChange}
102+
style={styles.input}
103+
ref={this.handleFileInputRef}
104+
/>
105+
</div>
106+
);
15107
}
16108
}
109+
110+
const styles = {
111+
input: {
112+
display: 'none'
113+
}
114+
};

src/components/Widgets/ImagePreview.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default class ImagePreview extends React.Component {
77

88
render() {
99
const { value } = this.props;
10-
1110
return value ? <img src={value}/> : null;
1211
}
1312
}

0 commit comments

Comments
 (0)