]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/components/media_gallery.js
Add preference to always display sensitive media (#6448)
[mastodon.git] / app / javascript / mastodon / components / media_gallery.js
1 import React from 'react';
2 import ImmutablePropTypes from 'react-immutable-proptypes';
3 import PropTypes from 'prop-types';
4 import { is } from 'immutable';
5 import IconButton from './icon_button';
6 import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
7 import { isIOS } from '../is_mobile';
8 import classNames from 'classnames';
9 import { autoPlayGif, displaySensitiveMedia } from '../initial_state';
10
11 const messages = defineMessages({
12 toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' },
13 });
14
15 class Item extends React.PureComponent {
16
17 static contextTypes = {
18 router: PropTypes.object,
19 };
20
21 static propTypes = {
22 attachment: ImmutablePropTypes.map.isRequired,
23 standalone: PropTypes.bool,
24 index: PropTypes.number.isRequired,
25 size: PropTypes.number.isRequired,
26 onClick: PropTypes.func.isRequired,
27 };
28
29 static defaultProps = {
30 standalone: false,
31 index: 0,
32 size: 1,
33 };
34
35 handleMouseEnter = (e) => {
36 if (this.hoverToPlay()) {
37 e.target.play();
38 }
39 }
40
41 handleMouseLeave = (e) => {
42 if (this.hoverToPlay()) {
43 e.target.pause();
44 e.target.currentTime = 0;
45 }
46 }
47
48 hoverToPlay () {
49 const { attachment } = this.props;
50 return !autoPlayGif && attachment.get('type') === 'gifv';
51 }
52
53 handleClick = (e) => {
54 const { index, onClick } = this.props;
55
56 if (this.context.router && e.button === 0) {
57 e.preventDefault();
58 onClick(index);
59 }
60
61 e.stopPropagation();
62 }
63
64 render () {
65 const { attachment, index, size, standalone } = this.props;
66
67 let width = 50;
68 let height = 100;
69 let top = 'auto';
70 let left = 'auto';
71 let bottom = 'auto';
72 let right = 'auto';
73
74 if (size === 1) {
75 width = 100;
76 }
77
78 if (size === 4 || (size === 3 && index > 0)) {
79 height = 50;
80 }
81
82 if (size === 2) {
83 if (index === 0) {
84 right = '2px';
85 } else {
86 left = '2px';
87 }
88 } else if (size === 3) {
89 if (index === 0) {
90 right = '2px';
91 } else if (index > 0) {
92 left = '2px';
93 }
94
95 if (index === 1) {
96 bottom = '2px';
97 } else if (index > 1) {
98 top = '2px';
99 }
100 } else if (size === 4) {
101 if (index === 0 || index === 2) {
102 right = '2px';
103 }
104
105 if (index === 1 || index === 3) {
106 left = '2px';
107 }
108
109 if (index < 2) {
110 bottom = '2px';
111 } else {
112 top = '2px';
113 }
114 }
115
116 let thumbnail = '';
117
118 if (attachment.get('type') === 'image') {
119 const previewUrl = attachment.get('preview_url');
120 const previewWidth = attachment.getIn(['meta', 'small', 'width']);
121
122 const originalUrl = attachment.get('url');
123 const originalWidth = attachment.getIn(['meta', 'original', 'width']);
124
125 const hasSize = typeof originalWidth === 'number' && typeof previewWidth === 'number';
126
127 const srcSet = hasSize ? `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w` : null;
128 const sizes = hasSize ? `(min-width: 1025px) ${320 * (width / 100)}px, ${width}vw` : null;
129
130 thumbnail = (
131 <a
132 className='media-gallery__item-thumbnail'
133 href={attachment.get('remote_url') || originalUrl}
134 onClick={this.handleClick}
135 target='_blank'
136 >
137 <img src={previewUrl} srcSet={srcSet} sizes={sizes} alt={attachment.get('description')} title={attachment.get('description')} />
138 </a>
139 );
140 } else if (attachment.get('type') === 'gifv') {
141 const autoPlay = !isIOS() && autoPlayGif;
142
143 thumbnail = (
144 <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
145 <video
146 className='media-gallery__item-gifv-thumbnail'
147 aria-label={attachment.get('description')}
148 role='application'
149 src={attachment.get('url')}
150 onClick={this.handleClick}
151 onMouseEnter={this.handleMouseEnter}
152 onMouseLeave={this.handleMouseLeave}
153 autoPlay={autoPlay}
154 loop
155 muted
156 />
157
158 <span className='media-gallery__gifv__label'>GIF</span>
159 </div>
160 );
161 }
162
163 return (
164 <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
165 {thumbnail}
166 </div>
167 );
168 }
169
170 }
171
172 @injectIntl
173 export default class MediaGallery extends React.PureComponent {
174
175 static propTypes = {
176 sensitive: PropTypes.bool,
177 standalone: PropTypes.bool,
178 media: ImmutablePropTypes.list.isRequired,
179 size: PropTypes.object,
180 height: PropTypes.number.isRequired,
181 onOpenMedia: PropTypes.func.isRequired,
182 intl: PropTypes.object.isRequired,
183 };
184
185 static defaultProps = {
186 standalone: false,
187 };
188
189 state = {
190 visible: !this.props.sensitive || displaySensitiveMedia,
191 };
192
193 componentWillReceiveProps (nextProps) {
194 if (!is(nextProps.media, this.props.media)) {
195 this.setState({ visible: !nextProps.sensitive });
196 }
197 }
198
199 handleOpen = () => {
200 this.setState({ visible: !this.state.visible });
201 }
202
203 handleClick = (index) => {
204 this.props.onOpenMedia(this.props.media, index);
205 }
206
207 handleRef = (node) => {
208 if (node && this.isStandaloneEligible()) {
209 // offsetWidth triggers a layout, so only calculate when we need to
210 this.setState({
211 width: node.offsetWidth,
212 });
213 }
214 }
215
216 isStandaloneEligible() {
217 const { media, standalone } = this.props;
218 return standalone && media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
219 }
220
221 render () {
222 const { media, intl, sensitive, height } = this.props;
223 const { width, visible } = this.state;
224
225 let children;
226
227 const style = {};
228
229 if (this.isStandaloneEligible()) {
230 if (!visible && width) {
231 // only need to forcibly set the height in "sensitive" mode
232 style.height = width / this.props.media.getIn([0, 'meta', 'small', 'aspect']);
233 } else {
234 // layout automatically, using image's natural aspect ratio
235 style.height = '';
236 }
237 } else {
238 // crop the image
239 style.height = height;
240 }
241
242 if (!visible) {
243 let warning;
244
245 if (sensitive) {
246 warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
247 } else {
248 warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
249 }
250
251 children = (
252 <button className='media-spoiler' onClick={this.handleOpen} style={style} ref={this.handleRef}>
253 <span className='media-spoiler__warning'>{warning}</span>
254 <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
255 </button>
256 );
257 } else {
258 const size = media.take(4).size;
259
260 if (this.isStandaloneEligible()) {
261 children = <Item standalone onClick={this.handleClick} attachment={media.get(0)} />;
262 } else {
263 children = media.take(4).map((attachment, i) => <Item key={attachment.get('id')} onClick={this.handleClick} attachment={attachment} index={i} size={size} />);
264 }
265 }
266
267 return (
268 <div className='media-gallery' style={style}>
269 <div className={classNames('spoiler-button', { 'spoiler-button--visible': visible })}>
270 <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={visible ? 'eye' : 'eye-slash'} overlay onClick={this.handleOpen} />
271 </div>
272
273 {children}
274 </div>
275 );
276 }
277
278 }
This page took 0.20688 seconds and 4 git commands to generate.