|
1 | 1 | /**
|
2 | 2 | * External dependencies
|
3 | 3 | */
|
4 |
| -import { get, has, now, reduce, template } from 'lodash'; |
| 4 | +import { get, now, template } from 'lodash'; |
5 | 5 |
|
6 | 6 | /**
|
7 | 7 | * WordPress dependencies
|
8 | 8 | */
|
9 | 9 | import { __, sprintf } from '@wordpress/i18n';
|
10 |
| -import { getColorObjectByAttributeValues, getColorObjectByColorValue } from '@wordpress/block-editor'; |
11 | 10 |
|
12 | 11 | /**
|
13 | 12 | * Internal dependencies
|
14 | 13 | */
|
15 | 14 | import {
|
16 |
| - FILE_SIZE_ERROR_VIEW, |
17 | 15 | FILE_TYPE_ERROR_VIEW,
|
18 |
| - MEGABYTE_IN_BYTES, |
19 | 16 | MINIMUM_FEATURED_IMAGE_WIDTH,
|
20 |
| - VIDEO_ALLOWED_MEGABYTES_PER_SECOND, |
21 | 17 | } from '../constants';
|
22 | 18 |
|
23 |
| -const allowedVideoMimeTypes = [ 'video/mp4' ]; |
24 |
| - |
25 | 19 | /**
|
26 | 20 | * Determines whether whether the image has the minimum required dimensions.
|
27 | 21 | *
|
@@ -111,71 +105,6 @@ export const validateFeaturedImage = ( media, dimensions, required ) => {
|
111 | 105 | return 0 === errors.length ? null : errors;
|
112 | 106 | };
|
113 | 107 |
|
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 |
| - |
179 | 108 | /**
|
180 | 109 | * Gets the compiled template for a given notice message.
|
181 | 110 | *
|
@@ -216,11 +145,7 @@ export const isFileTypeAllowed = ( attachment, allowedTypes ) => {
|
216 | 145 | return false;
|
217 | 146 | }
|
218 | 147 |
|
219 |
| - if ( 'video' === fileType && ! allowedVideoMimeTypes.includes( mimeType ) ) { |
220 |
| - return false; |
221 |
| - } |
222 |
| - |
223 |
| - return true; |
| 148 | + return 'video' !== fileType; |
224 | 149 | };
|
225 | 150 |
|
226 | 151 | /**
|
@@ -256,94 +181,6 @@ export const enforceFileType = function( attachment, SelectionError ) {
|
256 | 181 | }
|
257 | 182 | };
|
258 | 183 |
|
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 |
| - |
347 | 184 | /**
|
348 | 185 | * Sets the featured image, on selecting it in the Media Library.
|
349 | 186 | *
|
|
0 commit comments