File size: 3,203 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import percentageFactory from 'percentage-regex';

const percentageRegex = percentageFactory( { exact: true } );
const isPercentage = ( val ) => percentageRegex.test( val );

const embedsConfig = {
	default: {
		sizingFunction: function defaultEmbedSizingFunction( embed, availableWidth ) {
			const { aspectRatio } = embed;
			let { width, height } = embed;

			if ( ! isNaN( aspectRatio ) ) {
				// width and height were numbers, so grab the aspect ratio
				// and scale to the `availableWidth`
				width = availableWidth;
				height = availableWidth / aspectRatio;
			}
			if ( isPercentage( width ) ) {
				// if `width` is a percentage, then scale based on `availableWidth`
				width = availableWidth * ( parseInt( width, 10 ) / 100 );
			}
			if ( isPercentage( height ) ) {
				// if `height` is a percentage, then scale based on the calculated `width`
				height = width * ( parseInt( height, 10 ) / 100 );
			}
			return {
				width: `${ width | 0 }px`,
				height: `${ height | 0 }px`,
				paddingRight: '1px', // this exists to solve a bug in safari that we found here: https://github.com/Automattic/wp-calypso/issues/8987
			};
		},
	},
	spotify: {
		sizingFunction: function spotifyEmbedSizingFunction( embed, availableWidth ) {
			let height;

			// Spotify can handle maximum height of : width + 80, if our resulted height
			// from aspectRatio calculation will be larger, we'll use availableWidth + 80
			if ( embed.aspectRatio ) {
				height = Math.min( availableWidth / embed.aspectRatio, availableWidth + 80 );
			} else {
				height = availableWidth + 80;
			}

			return {
				width: availableWidth + 'px',
				height: height + 'px',
			};
		},
		urlRegex: /\/\/embed.spotify.com/,
	},
	soundcloud: {
		sizingFunction: function soundcloudEmbedSizingFunction( embed, availableWidth ) {
			const aspectRatio = embed.aspectRatio || 1;
			let height = '100%';

			if ( embed.iframe.indexOf( 'visual=true' ) > -1 ) {
				height = Math.floor( availableWidth / aspectRatio ) + 'px';
			}

			return {
				width: availableWidth + 'px',
				height: height,
			};
		},
		urlRegex: /\/\/w\.soundcloud\.com\/player/,
	},
};

function extractUrlFromIframe( iframeHtml ) {
	const urlRegex = new RegExp( 'src="([^"]+)"' );
	const res = urlRegex.exec( iframeHtml );

	return res.length > 1 ? res[ 1 ] : null;
}

function resolveEmbedConfig( embed ) {
	let embedType;

	// if there's type, easiest way just to use it
	if ( embedsConfig.hasOwnProperty( embed.type ) ) {
		return embedsConfig[ embed.type ];
	}

	// if no type, check everyone by their url regex
	const url = extractUrlFromIframe( embed.iframe );

	if ( url ) {
		for ( embedType in embedsConfig ) {
			if ( embedsConfig.hasOwnProperty( embedType ) && embedsConfig[ embedType ].urlRegex ) {
				if ( url.match( embedsConfig[ embedType ].urlRegex ) ) {
					return embedsConfig[ embedType ];
				}
			}
		}
	}

	return embedsConfig.default;
}

const exported = {
	getEmbedSizingFunction: function getEmbedSizingFunction( embed ) {
		const embedConfig = resolveEmbedConfig( embed );

		return embedConfig.sizingFunction.bind( embedConfig, embed );
	},
};

export default exported;

export const { getEmbedSizingFunction } = exported;