1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-03 10:25:08 +01:00

Use js-utility-belt's truncateText

This commit is contained in:
Brett Sun 2016-06-13 16:37:19 +02:00
parent ec78ecb8ab
commit de571c7fab
5 changed files with 10 additions and 27 deletions

View File

@ -20,8 +20,8 @@ import withContext from '../context/with_context';
import { currentUserShape, whitelabelShape } from '../prop_types'; import { currentUserShape, whitelabelShape } from '../prop_types';
import { setDocumentTitle } from '../../utils/dom'; import { setDocumentTitle } from '../../utils/dom';
import { truncateTextAtCharIndex } from '../../utils/general';
import { getLangText } from '../../utils/lang'; import { getLangText } from '../../utils/lang';
import { truncateText } from '../../utils/text';
let ContractSettings = React.createClass({ let ContractSettings = React.createClass({
@ -105,7 +105,7 @@ let ContractSettings = React.createClass({
<ActionPanel <ActionPanel
key={contract.id} key={contract.id}
title={contract.name} title={contract.name}
content={truncateTextAtCharIndex(contract.name, 120, '(...).pdf')} content={truncateText(contract.name, 120, '(...).pdf')}
buttons={ buttons={
<div className="pull-right"> <div className="pull-right">
<AclProxy <AclProxy
@ -147,7 +147,7 @@ let ContractSettings = React.createClass({
<ActionPanel <ActionPanel
key={contract.id} key={contract.id}
title={contract.name} title={contract.name}
content={truncateTextAtCharIndex(contract.name, 120, '(...).pdf')} content={truncateText(contract.name, 120, '(...).pdf')}
buttons={ buttons={
<div className="pull-right"> <div className="pull-right">
<AclProxy <AclProxy

View File

@ -6,9 +6,9 @@ import FileDragAndDropPreviewImage from './file_drag_and_drop_preview_image';
import FileDragAndDropPreviewOther from './file_drag_and_drop_preview_other'; import FileDragAndDropPreviewOther from './file_drag_and_drop_preview_other';
import { FileStatus } from '../react_s3_fine_uploader_utils'; import { FileStatus } from '../react_s3_fine_uploader_utils';
import { getLangText } from '../../../utils/lang';
import { truncateTextAtCharIndex } from '../../../utils/general';
import { extractFileExtensionFromString } from '../../../utils/file'; import { extractFileExtensionFromString } from '../../../utils/file';
import { getLangText } from '../../../utils/lang';
import { truncateText } from '../../../utils/text';
const { shape, string, number, func, bool } = React.PropTypes; const { shape, string, number, func, bool } = React.PropTypes;
@ -76,7 +76,7 @@ const FileDragAndDropPreview = React.createClass({
if (numberOfDisplayedFiles === 1) { if (numberOfDisplayedFiles === 1) {
return ( return (
<span className="file-name"> <span className="file-name">
{truncateTextAtCharIndex(file.name, 30, '(...).' + extractFileExtensionFromString(file.name))} {truncateText(file.name, 30, '(...).' + extractFileExtensionFromString(file.name))}
</span> </span>
); );
} else { } else {

View File

@ -4,8 +4,8 @@ import React from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import { displayValidProgressFilesFilter, FileStatus } from '../react_s3_fine_uploader_utils'; import { displayValidProgressFilesFilter, FileStatus } from '../react_s3_fine_uploader_utils';
import { truncateTextAtCharIndex } from '../../../utils/general';
import { getLangText } from '../../../utils/lang'; import { getLangText } from '../../../utils/lang';
import { truncateText } from '../../../utils/text';
const { func, array, bool, shape, string } = React.PropTypes; const { func, array, bool, shape, string } = React.PropTypes;
@ -127,7 +127,7 @@ export default function UploadButton({ className = 'btn btn-default btn-sm', sho
if (uploadingFiles.length) { if (uploadingFiles.length) {
return ( return (
<span> <span>
{' ' + truncateTextAtCharIndex(uploadingFiles[0].name, 40) + ' '} {' ' + truncateText(uploadingFiles[0].name, 40) + ' '}
[<a onClick={this.onClickRemove}>{getLangText('cancel upload')}</a>] [<a onClick={this.onClickRemove}>{getLangText('cancel upload')}</a>]
</span> </span>
); );
@ -135,7 +135,7 @@ export default function UploadButton({ className = 'btn btn-default btn-sm', sho
return ( return (
<span> <span>
<span className='ascribe-icon icon-ascribe-ok'/> <span className='ascribe-icon icon-ascribe-ok'/>
{' ' + truncateTextAtCharIndex(uploadedFile.name, 40) + ' '} {' ' + truncateText(uploadedFile.name, 40) + ' '}
[<a onClick={this.onClickRemove}>{getLangText('remove')}</a>] [<a onClick={this.onClickRemove}>{getLangText('remove')}</a>]
</span> </span>
); );

View File

@ -27,23 +27,6 @@ export function escapeHTML(s) {
return document.createElement('div').appendChild(document.createTextNode(s)).parentNode.innerHTML; return document.createElement('div').appendChild(document.createTextNode(s)).parentNode.innerHTML;
} }
/**
* Takes a string and breaks it at the supplied index and replaces it
* with a (potentially) short string that also has been provided
* @param {string} text The string to truncate
* @param {number} charIndex The char number at which the text should be truncated
* @param {String} replacement All text after charIndex will be replaced with this string
* @return {string} The truncated text
*/
export function truncateTextAtCharIndex(text, charIndex, replacement = '...') {
let truncatedText = '';
truncatedText = text.slice(0, charIndex);
truncatedText += text.length > charIndex ? replacement : '';
return truncatedText;
}
/** /**
* @param index, int, the starting index of the substring to be replaced * @param index, int, the starting index of the substring to be replaced
* @param character, substring to be replaced * @param character, substring to be replaced

View File

@ -1,2 +1,2 @@
// Re-export related utilities from js-utility-belt for easier access // Re-export related utilities from js-utility-belt for easier access
export { formatText, sprintf } from 'js-utility-belt/es6/text'; export { formatText, sprintf, truncateText } from 'js-utility-belt/es6/text';