Composer: refactor filename truncation

better-thread-display
Alex Gleason 2020-09-06 16:44:19 -05:00
rodzic 8088ca2748
commit 5aef50b89e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 14 dodań i 12 usunięć

Wyświetl plik

@ -6,6 +6,7 @@ import { is } from 'immutable';
import IconButton from './icon_button';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { isIOS } from '../is_mobile';
import { truncateFilename } from 'soapbox/utils/media';
import classNames from 'classnames';
import { decode } from 'blurhash';
import { isPanoramic, isPortrait, isNonConformingRatio, minimumAspectRatio, maximumAspectRatio } from '../utils/media_aspect_ratio';
@ -14,6 +15,8 @@ import { getSettings } from 'soapbox/actions/settings';
import Icon from 'soapbox/components/icon';
import StillImage from 'soapbox/components/still_image';
const MAX_FILENAME_LENGTH = 45;
const messages = defineMessages({
toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' },
});
@ -142,19 +145,8 @@ class Item extends React.PureComponent {
let thumbnail = '';
const MAX_FILENAME_LENGTH = 45;
const getCroppedFilename = () => {
const remoteURL = attachment.get('remote_url');
const filenameLastIndex = remoteURL.lastIndexOf('/');
const filename = remoteURL.substr(filenameLastIndex + 1);
if (filename.length <= MAX_FILENAME_LENGTH)
return filename;
else
return filename.substr(0, MAX_FILENAME_LENGTH/2) + '...' + filename.substr(filename.length - MAX_FILENAME_LENGTH/2);
};
if (attachment.get('type') === 'unknown') {
const filename = getCroppedFilename();
const filename = truncateFilename(attachment.get('remote_url'), MAX_FILENAME_LENGTH);
return (
<div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ position, float, left, top, right, bottom, height, width: `${width}%` }}>
<a className='media-gallery__item-thumbnail' href={attachment.get('remote_url')} target='_blank' style={{ cursor: 'pointer' }}>

Wyświetl plik

@ -0,0 +1,10 @@
export const truncateFilename = (url, maxLength) => {
const filename = url.split('/').pop();
if (filename.length <= maxLength) return filename;
return [
filename.substr(0, maxLength/2),
filename.substr(filename.length - maxLength/2),
].join('…');
};