Skip to content

Commit 97ca07a

Browse files
committedFeb 21, 2020
Remove unused JS methods & npm dependencies
1 parent cd77370 commit 97ca07a

16 files changed

+4
-517
lines changed
 

‎assets/src/common/components/select-media-frame.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { __, sprintf } from '@wordpress/i18n';
1111
/**
1212
* Internal dependencies
1313
*/
14-
import { enforceFileSize, enforceFileType, getNoticeTemplate, mediaLibraryHasTwoNotices } from '../helpers';
14+
import { enforceFileType, getNoticeTemplate } from '../helpers';
1515

1616
const { wp } = window;
1717

@@ -159,13 +159,6 @@ export const EnforcedFileToolbarSelect = wp.media.view.Toolbar.Select.extend( {
159159
const attachment = selection.models[ 0 ];
160160

161161
enforceFileType.call( this, attachment, SelectionFileTypeError );
162-
enforceFileSize.call( this, attachment, SelectionFileSizeError );
163-
164-
// If there are two notices, like for wrong size and type, prevent the notices from covering the media.
165-
const mediaFrame = this.$el.parents( '.media-frame' );
166-
if ( mediaFrame ) {
167-
mediaFrame.toggleClass( 'has-two-notices', mediaLibraryHasTwoNotices.call( this ) );
168-
}
169162
},
170163
} );
171164

‎assets/src/common/constants.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
export const MIN_FONT_SIZE = 6;
33
export const MAX_FONT_SIZE = 72;
44
export const MINIMUM_FEATURED_IMAGE_WIDTH = 1200;
5-
export const MEGABYTE_IN_BYTES = 1000000;
6-
export const VIDEO_ALLOWED_MEGABYTES_PER_SECOND = 1;
75
export const FILE_TYPE_ERROR_VIEW = 'select-file-type-error';
8-
export const FILE_SIZE_ERROR_VIEW = 'select-file-size-error';

‎assets/src/common/helpers/index.js

+2-165
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
/**
22
* External dependencies
33
*/
4-
import { get, has, now, reduce, template } from 'lodash';
4+
import { get, now, template } from 'lodash';
55

66
/**
77
* WordPress dependencies
88
*/
99
import { __, sprintf } from '@wordpress/i18n';
10-
import { getColorObjectByAttributeValues, getColorObjectByColorValue } from '@wordpress/block-editor';
1110

1211
/**
1312
* Internal dependencies
1413
*/
1514
import {
16-
FILE_SIZE_ERROR_VIEW,
1715
FILE_TYPE_ERROR_VIEW,
18-
MEGABYTE_IN_BYTES,
1916
MINIMUM_FEATURED_IMAGE_WIDTH,
20-
VIDEO_ALLOWED_MEGABYTES_PER_SECOND,
2117
} from '../constants';
2218

23-
const allowedVideoMimeTypes = [ 'video/mp4' ];
24-
2519
/**
2620
* Determines whether whether the image has the minimum required dimensions.
2721
*
@@ -111,71 +105,6 @@ export const validateFeaturedImage = ( media, dimensions, required ) => {
111105
return 0 === errors.length ? null : errors;
112106
};
113107

114-
/**
115-
* Converts a hexadecimal color to its RGBA form.
116-
*
117-
* @param {string} hex Hex value.
118-
* @param {number} opacity Opacity.
119-
*
120-
* @return {Object} Rgba value.
121-
*/
122-
export const getRgbaFromHex = ( hex, opacity = 100 ) => {
123-
if ( ! hex ) {
124-
return [];
125-
}
126-
127-
hex = hex.replace( '#', '' );
128-
129-
if ( hex.length === 3 ) {
130-
// If this is a 3 digit color, e.g. #f00, make it 6 digits by duplicating each one.
131-
hex = `${ hex.charAt( 0 ) }${ hex.charAt( 0 ) }${ hex.charAt( 1 ) }${ hex.charAt( 1 ) }${ hex.charAt( 2 ) }${ hex.charAt( 2 ) }`;
132-
}
133-
134-
const r = parseInt( hex.substring( 0, 2 ), 16 );
135-
const g = parseInt( hex.substring( 2, 4 ), 16 );
136-
const b = parseInt( hex.substring( 4, 6 ), 16 );
137-
138-
// Opacity needs to be in the range of 0-100.
139-
opacity = Math.min( 100, Math.max( 0, opacity ) );
140-
141-
return [
142-
r,
143-
g,
144-
b,
145-
opacity / 100,
146-
];
147-
};
148-
149-
/**
150-
* Returns a CSS background-color property based on passed color and available colors.
151-
*
152-
* Either backgroundColor or customBackgroundColor should be passed, not both.
153-
*
154-
* @param {Object[]} colors Array of color objects as set by the theme or by the editor defaults.
155-
* @param {?Object} backgroundColor Color object.
156-
* @param {?string} backgroundColor.name Color name.
157-
* @param {?string} backgroundColor.slug Color slug.
158-
* @param {?string} backgroundColor.color Color value.
159-
* @param {?string} customBackgroundColor A string containing the custom color value.
160-
* @param {?number} opacity Opacity.
161-
*
162-
* @return {?string} Background color string or undefined if no color has been set / found.
163-
*/
164-
export const getBackgroundColorWithOpacity = ( colors, backgroundColor, customBackgroundColor, opacity = undefined ) => {
165-
// Order: 1. Existing colors as set by the theme. 2. Custom color objects. 3. Custom background color.
166-
const colorObject = backgroundColor ?
167-
( getColorObjectByColorValue( colors, backgroundColor.color ) || getColorObjectByAttributeValues( colors, backgroundColor.slug, backgroundColor.color || customBackgroundColor ) ) :
168-
{ color: customBackgroundColor };
169-
170-
if ( colorObject && colorObject.color ) {
171-
const [ r, g, b, a ] = getRgbaFromHex( colorObject.color, opacity );
172-
173-
return `rgba(${ r }, ${ g }, ${ b }, ${ a })`;
174-
}
175-
176-
return undefined;
177-
};
178-
179108
/**
180109
* Gets the compiled template for a given notice message.
181110
*
@@ -216,11 +145,7 @@ export const isFileTypeAllowed = ( attachment, allowedTypes ) => {
216145
return false;
217146
}
218147

219-
if ( 'video' === fileType && ! allowedVideoMimeTypes.includes( mimeType ) ) {
220-
return false;
221-
}
222-
223-
return true;
148+
return 'video' !== fileType;
224149
};
225150

226151
/**
@@ -256,94 +181,6 @@ export const enforceFileType = function( attachment, SelectionError ) {
256181
}
257182
};
258183

259-
/**
260-
* If the attachment has the wrong file size, this displays a notice in the Media Library and disables the 'Select' button.
261-
*
262-
* This is not an arrow function so that it can be called with enforceFileSize.call( this, foo, bar ).
263-
*
264-
* @param {Object} attachment The selected attachment.
265-
* @param {Object} SelectionError The error to display.
266-
*/
267-
export const enforceFileSize = function( attachment, SelectionError ) {
268-
if ( ! attachment ) {
269-
return;
270-
}
271-
272-
const isVideo = 'video' === get( attachment, [ 'media_type' ], null ) || 'video' === get( attachment, [ 'attributes', 'type' ], null );
273-
274-
// If the file type is 'video' and its size is over the limit, display a notice in the Media Library.
275-
if ( isVideo && isVideoSizeExcessive( getVideoBytesPerSecond( attachment ) ) ) {
276-
this.secondary.set(
277-
FILE_SIZE_ERROR_VIEW,
278-
new SelectionError( {
279-
actualVideoMegabytesPerSecond: Math.round( getVideoBytesPerSecond( attachment ) / MEGABYTE_IN_BYTES ),
280-
maxVideoMegabytesPerSecond: VIDEO_ALLOWED_MEGABYTES_PER_SECOND,
281-
} ),
282-
);
283-
} else {
284-
this.secondary.unset( FILE_SIZE_ERROR_VIEW );
285-
}
286-
};
287-
288-
/**
289-
* Gets whether the Media Library has two notices.
290-
*
291-
* It's possible to have a notice that the file type and size are wrong.
292-
* In that case, this will need different styling, so the notices don't overlap the media.
293-
*
294-
* @return {boolean} Whether the Media Library has two notices.
295-
*/
296-
export const mediaLibraryHasTwoNotices = function() {
297-
return Boolean( this.secondary.get( FILE_TYPE_ERROR_VIEW ) ) && Boolean( this.secondary.get( FILE_SIZE_ERROR_VIEW ) );
298-
};
299-
300-
/**
301-
* Gets whether the video file size is over a certain amount of bytes per second.
302-
*
303-
* @param {number} videoSize Video size per second, in bytes.
304-
* @return {boolean} Whether the file size is more than a certain amount of MB per second, or null of the data isn't available.
305-
*/
306-
export const isVideoSizeExcessive = ( videoSize ) => {
307-
return videoSize > VIDEO_ALLOWED_MEGABYTES_PER_SECOND * MEGABYTE_IN_BYTES;
308-
};
309-
310-
/**
311-
* Gets the number of seconds in a colon-separated time string, like '01:10'.
312-
*
313-
* @param {string} time A colon-separated time, like '0:12'.
314-
* @return {number} seconds The number of seconds in the time, like 12.
315-
*/
316-
export const getSecondsFromTime = ( time ) => {
317-
const minuteInSeconds = 60;
318-
const splitTime = time.split( ':' );
319-
320-
return reduce(
321-
splitTime,
322-
( totalSeconds, timeSection, index ) => {
323-
const parsedTimeSection = isNaN( parseInt( timeSection ) ) ? 0 : parseInt( timeSection );
324-
const distanceFromRight = splitTime.length - 1 - index;
325-
const multiple = Math.pow( minuteInSeconds, distanceFromRight ); // This should be 1 for seconds, 60 for minutes, 360 for hours...
326-
return totalSeconds + ( multiple * parsedTimeSection );
327-
},
328-
0,
329-
);
330-
};
331-
332-
/**
333-
* Given a URL, returns file size in bytes.
334-
*
335-
* @param {string} url URL to a file.
336-
* @return {Promise<number>} File size in bytes.
337-
*/
338-
export const getContentLengthFromUrl = async ( url ) => {
339-
const { fetch } = window;
340-
341-
const response = await fetch( url, {
342-
method: 'head',
343-
} );
344-
return Number( response.headers.get( 'content-length' ) );
345-
};
346-
347184
/**
348185
* Sets the featured image, on selecting it in the Media Library.
349186
*

‎assets/src/common/helpers/test/enforceFileSize.js

-79
This file was deleted.

‎assets/src/common/helpers/test/getBackgroundColorWithOpacity.js

-71
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.