Composer: refactor filename truncation
This commit is contained in:
parent
8088ca2748
commit
5aef50b89e
2 changed files with 14 additions and 12 deletions
|
@ -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' }}>
|
||||
|
|
10
app/soapbox/utils/media.js
Normal file
10
app/soapbox/utils/media.js
Normal file
|
@ -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('…');
|
||||
};
|
Loading…
Reference in a new issue