2017-09-28 09:31:31 -04:00
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-10-16 03:36:15 -04:00
|
|
|
import Motion from '../../ui/util/optional_motion';
|
2017-09-28 09:31:31 -04:00
|
|
|
import spring from 'react-motion/lib/spring';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2018-02-21 18:35:46 -05:00
|
|
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
2017-09-28 09:31:31 -04:00
|
|
|
import classNames from 'classnames';
|
2019-01-31 18:14:05 -05:00
|
|
|
import Icon from 'mastodon/components/icon';
|
2017-09-28 09:31:31 -04:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
description: { id: 'upload_form.description', defaultMessage: 'Describe for the visually impaired' },
|
|
|
|
});
|
|
|
|
|
2018-09-14 11:59:48 -04:00
|
|
|
export default @injectIntl
|
|
|
|
class Upload extends ImmutablePureComponent {
|
2017-09-28 09:31:31 -04:00
|
|
|
|
2018-10-10 19:31:03 -04:00
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
2017-09-28 09:31:31 -04:00
|
|
|
static propTypes = {
|
|
|
|
media: ImmutablePropTypes.map.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
onUndo: PropTypes.func.isRequired,
|
|
|
|
onDescriptionChange: PropTypes.func.isRequired,
|
2018-02-21 18:35:46 -05:00
|
|
|
onOpenFocalPoint: PropTypes.func.isRequired,
|
2018-08-18 13:40:35 -04:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-09-28 09:31:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
hovered: false,
|
|
|
|
focused: false,
|
|
|
|
dirtyDescription: null,
|
|
|
|
};
|
|
|
|
|
2018-08-18 13:40:35 -04:00
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
|
|
|
this.handleSubmit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
|
|
|
this.handleInputBlur();
|
2018-10-10 19:31:03 -04:00
|
|
|
this.props.onSubmit(this.context.router.history);
|
2018-08-18 13:40:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 12:17:15 -04:00
|
|
|
handleUndoClick = e => {
|
|
|
|
e.stopPropagation();
|
2017-09-28 09:31:31 -04:00
|
|
|
this.props.onUndo(this.props.media.get('id'));
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:17:15 -04:00
|
|
|
handleFocalPointClick = e => {
|
|
|
|
e.stopPropagation();
|
2018-02-21 18:35:46 -05:00
|
|
|
this.props.onOpenFocalPoint(this.props.media.get('id'));
|
|
|
|
}
|
|
|
|
|
2017-09-28 09:31:31 -04:00
|
|
|
handleInputChange = e => {
|
|
|
|
this.setState({ dirtyDescription: e.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseEnter = () => {
|
|
|
|
this.setState({ hovered: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseLeave = () => {
|
|
|
|
this.setState({ hovered: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputFocus = () => {
|
|
|
|
this.setState({ focused: true });
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:17:15 -04:00
|
|
|
handleClick = () => {
|
|
|
|
this.setState({ focused: true });
|
|
|
|
}
|
|
|
|
|
2017-09-28 09:31:31 -04:00
|
|
|
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-12 13:57:22 -05:00
|
|
|
const description = this.state.dirtyDescription || (this.state.dirtyDescription !== '' && media.get('description')) || '';
|
2018-02-21 18:35:46 -05: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 09:31:31 -04:00
|
|
|
|
|
|
|
return (
|
2018-10-24 12:17:15 -04:00
|
|
|
<div className='compose-form__upload' tabIndex='0' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} onClick={this.handleClick} role='button'>
|
2017-09-28 09:31:31 -04:00
|
|
|
<Motion defaultStyle={{ scale: 0.8 }} style={{ scale: spring(1, { stiffness: 180, damping: 12 }) }}>
|
|
|
|
{({ scale }) => (
|
2018-02-21 18:35:46 -05: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 })}>
|
2019-01-31 18:14:05 -05:00
|
|
|
<button className='icon-button' onClick={this.handleUndoClick}><Icon id='times' /> <FormattedMessage id='upload_form.undo' defaultMessage='Delete' /></button>
|
|
|
|
{media.get('type') === 'image' && <button className='icon-button' onClick={this.handleFocalPointClick}><Icon id='crosshairs' /> <FormattedMessage id='upload_form.focus' defaultMessage='Crop' /></button>}
|
2018-02-21 18:35:46 -05:00
|
|
|
</div>
|
2017-09-28 09:31:31 -04: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-28 20:30:00 -04:00
|
|
|
maxLength={420}
|
2017-09-28 09:31:31 -04:00
|
|
|
onFocus={this.handleInputFocus}
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
onBlur={this.handleInputBlur}
|
2018-08-18 13:40:35 -04:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2017-09-28 09:31:31 -04:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Motion>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|