The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

97 lines
3.1 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import IconButton from '../../../components/icon_button';
  5. import Motion from 'react-motion/lib/Motion';
  6. import spring from 'react-motion/lib/spring';
  7. import ImmutablePureComponent from 'react-immutable-pure-component';
  8. import { defineMessages, injectIntl } from 'react-intl';
  9. import classNames from 'classnames';
  10. const messages = defineMessages({
  11. undo: { id: 'upload_form.undo', defaultMessage: 'Undo' },
  12. description: { id: 'upload_form.description', defaultMessage: 'Describe for the visually impaired' },
  13. });
  14. @injectIntl
  15. export default class Upload extends ImmutablePureComponent {
  16. static propTypes = {
  17. media: ImmutablePropTypes.map.isRequired,
  18. intl: PropTypes.object.isRequired,
  19. onUndo: PropTypes.func.isRequired,
  20. onDescriptionChange: PropTypes.func.isRequired,
  21. };
  22. state = {
  23. hovered: false,
  24. focused: false,
  25. dirtyDescription: null,
  26. };
  27. handleUndoClick = () => {
  28. this.props.onUndo(this.props.media.get('id'));
  29. }
  30. handleInputChange = e => {
  31. this.setState({ dirtyDescription: e.target.value });
  32. }
  33. handleMouseEnter = () => {
  34. this.setState({ hovered: true });
  35. }
  36. handleMouseLeave = () => {
  37. this.setState({ hovered: false });
  38. }
  39. handleInputFocus = () => {
  40. this.setState({ focused: true });
  41. }
  42. handleInputBlur = () => {
  43. const { dirtyDescription } = this.state;
  44. this.setState({ focused: false, dirtyDescription: null });
  45. if (dirtyDescription !== null) {
  46. this.props.onDescriptionChange(this.props.media.get('id'), dirtyDescription);
  47. }
  48. }
  49. render () {
  50. const { intl, media } = this.props;
  51. const active = this.state.hovered || this.state.focused;
  52. const description = this.state.dirtyDescription || media.get('description') || '';
  53. return (
  54. <div className='compose-form__upload' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  55. <Motion defaultStyle={{ scale: 0.8 }} style={{ scale: spring(1, { stiffness: 180, damping: 12 }) }}>
  56. {({ scale }) => (
  57. <div className='compose-form__upload-thumbnail' style={{ transform: `translateZ(0) scale(${scale})`, backgroundImage: `url(${media.get('preview_url')})` }}>
  58. <IconButton icon='times' title={intl.formatMessage(messages.undo)} size={36} onClick={this.handleUndoClick} />
  59. <div className={classNames('compose-form__upload-description', { active })}>
  60. <label>
  61. <span style={{ display: 'none' }}>{intl.formatMessage(messages.description)}</span>
  62. <input
  63. placeholder={intl.formatMessage(messages.description)}
  64. type='text'
  65. value={description}
  66. maxLength={140}
  67. onFocus={this.handleInputFocus}
  68. onChange={this.handleInputChange}
  69. onBlur={this.handleInputBlur}
  70. />
  71. </label>
  72. </div>
  73. </div>
  74. )}
  75. </Motion>
  76. </div>
  77. );
  78. }
  79. }