2017-09-28 23:31:31 +10:00
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-10-16 18:36:15 +11:00
|
|
|
import Motion from '../../ui/util/optional_motion';
|
2017-09-28 23:31:31 +10:00
|
|
|
import spring from 'react-motion/lib/spring';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2018-02-22 10:35:46 +11:00
|
|
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
2017-09-28 23:31:31 +10:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
description: { id: 'upload_form.description', defaultMessage: 'Describe for the visually impaired' },
|
|
|
|
});
|
|
|
|
|
2018-09-15 01:59:48 +10:00
|
|
|
export default @injectIntl
|
|
|
|
class Upload extends ImmutablePureComponent {
|
2017-09-28 23:31:31 +10:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
media: ImmutablePropTypes.map.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
onUndo: PropTypes.func.isRequired,
|
|
|
|
onDescriptionChange: PropTypes.func.isRequired,
|
2018-02-22 10:35:46 +11:00
|
|
|
onOpenFocalPoint: PropTypes.func.isRequired,
|
2018-08-19 03:40:35 +10:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-09-28 23:31:31 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
hovered: false,
|
|
|
|
focused: false,
|
|
|
|
dirtyDescription: null,
|
|
|
|
};
|
|
|
|
|
2018-08-19 03:40:35 +10:00
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
|
|
|
this.handleSubmit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
|
|
|
this.handleInputBlur();
|
|
|
|
this.props.onSubmit();
|
|
|
|
}
|
|
|
|
|
2017-09-28 23:31:31 +10:00
|
|
|
handleUndoClick = () => {
|
|
|
|
this.props.onUndo(this.props.media.get('id'));
|
|
|
|
}
|
|
|
|
|
2018-02-22 10:35:46 +11:00
|
|
|
handleFocalPointClick = () => {
|
|
|
|
this.props.onOpenFocalPoint(this.props.media.get('id'));
|
|
|
|
}
|
|
|
|
|
2017-09-28 23:31:31 +10:00
|
|
|
handleInputChange = e => {
|
|
|
|
this.setState({ dirtyDescription: e.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseEnter = () => {
|
|
|
|
this.setState({ hovered: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseLeave = () => {
|
|
|
|
this.setState({ hovered: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputFocus = () => {
|
|
|
|
this.setState({ focused: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputBlur = () => {
|
|
|
|
const { dirtyDescription } = this.state;
|
|
|
|
|
|
|
|
this.setState({ focused: false, dirtyDescription: null });
|
|
|
|
|
|
|
|
if (dirtyDescription !== null) {
|
|
|
|
this.props.onDescriptionChange(this.props.media.get('id'), dirtyDescription);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { intl, media } = this.props;
|
|
|
|
const active = this.state.hovered || this.state.focused;
|
2017-12-13 05:57:22 +11:00
|
|
|
const description = this.state.dirtyDescription || (this.state.dirtyDescription !== '' && media.get('description')) || '';
|
2018-02-22 10:35:46 +11:00
|
|
|
const focusX = media.getIn(['meta', 'focus', 'x']);
|
|
|
|
const focusY = media.getIn(['meta', 'focus', 'y']);
|
|
|
|
const x = ((focusX / 2) + .5) * 100;
|
|
|
|
const y = ((focusY / -2) + .5) * 100;
|
2017-09-28 23:31:31 +10:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='compose-form__upload' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
|
|
|
|
<Motion defaultStyle={{ scale: 0.8 }} style={{ scale: spring(1, { stiffness: 180, damping: 12 }) }}>
|
|
|
|
{({ scale }) => (
|
2018-02-22 10:35:46 +11:00
|
|
|
<div className='compose-form__upload-thumbnail' style={{ transform: `scale(${scale})`, backgroundImage: `url(${media.get('preview_url')})`, backgroundPosition: `${x}% ${y}%` }}>
|
|
|
|
<div className={classNames('compose-form__upload__actions', { active })}>
|
2018-09-28 10:11:14 +10:00
|
|
|
<button className='icon-button' onClick={this.handleUndoClick}><i className='fa fa-times' /> <FormattedMessage id='upload_form.undo' defaultMessage='Delete' /></button>
|
|
|
|
{media.get('type') === 'image' && <button className='icon-button' onClick={this.handleFocalPointClick}><i className='fa fa-crosshairs' /> <FormattedMessage id='upload_form.focus' defaultMessage='Crop' /></button>}
|
2018-02-22 10:35:46 +11:00
|
|
|
</div>
|
2017-09-28 23:31:31 +10:00
|
|
|
|
|
|
|
<div className={classNames('compose-form__upload-description', { active })}>
|
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.description)}</span>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder={intl.formatMessage(messages.description)}
|
|
|
|
type='text'
|
|
|
|
value={description}
|
2017-09-29 10:30:00 +10:00
|
|
|
maxLength={420}
|
2017-09-28 23:31:31 +10:00
|
|
|
onFocus={this.handleInputFocus}
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
onBlur={this.handleInputBlur}
|
2018-08-19 03:40:35 +10:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2017-09-28 23:31:31 +10:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Motion>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|