logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

upload.js (3882B)


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