File size: 1,530 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
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import FormTextInput from 'calypso/components/forms/form-text-input';
import { withUpdateMedia } from 'calypso/data/media/with-update-media';

class EditorMediaModalGalleryCaption extends Component {
	static displayName = 'EditorMediaModalGalleryCaption';

	static propTypes = {
		siteId: PropTypes.number,
		item: PropTypes.object,
	};

	state = {
		caption: null,
	};

	getValue = () => {
		if ( null !== this.state.caption ) {
			return this.state.caption;
		}

		if ( this.props.item ) {
			return this.props.item.caption;
		}
	};

	setCaption = ( event ) => {
		this.setState( {
			caption: event.target.value,
		} );
	};

	saveCaption = () => {
		const { siteId, item } = this.props;
		if ( ! siteId || ! item ) {
			return;
		}

		// Avoid saving if caption value hasn't changed
		const caption = this.getValue();
		if ( item && caption === item.caption ) {
			return;
		}

		this.props.updateMedia( siteId, item.ID, { caption } );
	};

	render() {
		return (
			<FormTextInput
				value={ this.getValue() }
				placeholder={ this.props.translate( 'Caption this image…' ) }
				onChange={ this.setCaption }
				onBlur={ this.saveCaption }
				onMouseDown={ ( event ) => event.stopPropagation() }
				// eslint-disable-next-line wpcalypso/jsx-classname-namespace
				className="editor-media-modal-gallery__caption"
			/>
		);
	}
}

export default localize( withUpdateMedia( EditorMediaModalGalleryCaption ) );