File size: 3,088 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
import { addQueryArgs } from '@wordpress/url';
import { ASSEMBLER_V2_DESIGN, DEFAULT_VIEWPORT_HEIGHT } from '../constants';
import type { Design, DesignPreviewOptions } from '../types';

function encodeParenthesesInText( text: string ) {
	return encodeURIComponent( text ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' );
}

export const getDesignPreviewUrl = (
	design: Design,
	options: DesignPreviewOptions = {}
): string => {
	const { recipe, slug, preview_data } = design;

	//Anchor.fm themes get previews from their starter sites, ${slug}starter.wordpress.com
	if ( [ 'hannah', 'riley', 'gilbert' ].indexOf( slug ) >= 0 ) {
		return `https://${ slug }starter.wordpress.com`;
	}

	let url = addQueryArgs( 'https://public-api.wordpress.com/wpcom/v2/block-previews/site', {
		stylesheet: recipe?.stylesheet,
		pattern_ids: recipe?.pattern_ids?.join( ',' ),
		header_pattern_ids: recipe?.header_pattern_ids
			? recipe?.header_pattern_ids.join( ',' )
			: undefined,
		footer_pattern_ids: recipe?.footer_pattern_ids
			? recipe?.footer_pattern_ids.join( ',' )
			: undefined,
		language: options.language,
		viewport_height: ! options.disable_viewport_height
			? options.viewport_height || DEFAULT_VIEWPORT_HEIGHT
			: undefined,
		...( options.use_screenshot_overrides && {
			use_screenshot_overrides: options.use_screenshot_overrides,
		} ),
		remove_assets: options.remove_assets,
		...( options.style_variation &&
			options.style_variation.slug !== 'default' && {
				style_variation: options.style_variation.title,
			} ),
		...( options.viewport_unit_to_px && {
			viewport_unit_to_px: options.viewport_unit_to_px,
		} ),
	} );

	// The preview url is sometimes used in a `background-image: url()` CSS rule and unescaped
	// parentheses in the URL break it. `addQueryArgs` and `encodeURIComponent` don't escape
	// parentheses so we've got to do it ourselves.
	const siteTitle = options.site_title || preview_data?.site_title || design.title;
	if ( siteTitle ) {
		url += `&site_title=${ encodeParenthesesInText( siteTitle ) }`;
	}

	const siteTagline = options.site_tagline || preview_data?.site_tagline;
	if ( siteTagline ) {
		url += `&site_tagline=${ encodeParenthesesInText( siteTagline ) }`;
	}

	if ( preview_data?.site_logo_url ) {
		url += `&site_logo_url=${ encodeParenthesesInText( preview_data?.site_logo_url ) }`;
	}

	return url;
};

export const getAssemblerDesign = () => {
	return ASSEMBLER_V2_DESIGN;
};

export const getThemeIdFromDesign = ( design: Design ) => {
	const stylesheet = design?.recipe?.stylesheet;
	if ( stylesheet ) {
		// Transform stylesheet "premium/skivers" into themeId "skivers"
		const slashIndex = stylesheet.lastIndexOf( '/' );
		const themeId = stylesheet.substring( slashIndex + 1 );
		return themeId;
	}
	return null;
};

export const shuffleDesigns = ( designs: Design[], seed: number ) => {
	const shuffled = [ ...designs ];
	for ( let i = shuffled.length - 1; i > 0; i-- ) {
		const j = ( i + seed + shuffled.length ) % ( i + 1 );
		[ shuffled[ i ], shuffled[ j ] ] = [ shuffled[ j ], shuffled[ i ] ];
	}

	return shuffled;
};