File size: 2,495 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { get } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import { getCurrentUser } from 'calypso/state/current-user/selectors';
import { AspectRatios } from 'calypso/state/editor/image-editor/constants';
import ImageEditor from '../';

class ImageEditorExample extends Component {
	constructor() {
		super();

		this.state = {
			media: {
				URL: 'https://cldup.com/mA_hqNVj0w.jpg',
			},
		};
	}

	getTestingImage = () => document.querySelector( '#devdocs-example-image-editor-result' );

	onImageEditorDone = ( error, blob ) => {
		if ( error ) {
			return;
		}

		const imageUrl = window.URL.createObjectURL( blob );

		this.getTestingImage().src = imageUrl;
	};

	onImageEditorReset = () => {
		this.getTestingImage().src = this.state.media.URL || this.state.media.src;
	};

	componentDidMount() {
		const fileInput = document.querySelector( '#devdocs-example-image-editor-file-input' );

		fileInput.addEventListener( 'change', this.onImageUpload );
	}

	onImageUpload = ( e ) => {
		const imageFile = e.target.files[ 0 ];

		const imageObjectUrl = URL.createObjectURL( imageFile );

		this.setState( {
			media: {
				src: imageObjectUrl,
			},
		} );

		this.getTestingImage().src = imageObjectUrl;
	};

	render() {
		const { primarySiteId } = this.props;

		return (
			<div>
				<div
					style={ {
						marginBottom: '20px',
					} }
				>
					<h4>Upload an image</h4>
					<input type="file" id="devdocs-example-image-editor-file-input" />
				</div>

				<div style={ { height: '80vh' } }>
					<ImageEditor
						siteId={ primarySiteId }
						media={ this.state.media }
						onDone={ this.onImageEditorDone }
						onReset={ this.onImageEditorReset }
						allowedAspectRatios={ [
							AspectRatios.ASPECT_1X1,
							AspectRatios.ASPECT_4X3,
							AspectRatios.ASPECT_16X9,
						] }
						defaultAspectRatio={ AspectRatios.ASPECT_1X1 }
					/>
				</div>

				<div
					style={ {
						textAlign: 'center',
						marginTop: '15px',
					} }
				>
					<h4>Changes to the image above are shown below</h4>
					<img alt="" id="devdocs-example-image-editor-result" src={ this.state.media.URL } />
				</div>
			</div>
		);
	}
}

const ConnectedImageEditorExample = connect( ( state ) => {
	const primarySiteId = get( getCurrentUser( state ), 'primary_blog', null );

	return {
		primarySiteId,
	};
} )( ImageEditorExample );

ConnectedImageEditorExample.displayName = 'ImageEditor';

export default ConnectedImageEditorExample;