summaryrefslogtreecommitdiff
blob: bcff28e5fcd8652af21f7b153047ce5de0ddb53d (plain)
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
/**
 * External dependencies
 */
import { createBlock } from '@wordpress/blocks';
import { filter } from 'lodash';

/**
 * Filter valid images
 *
 * @param {array} images Array of image objects
 * @return {array} Array of image objects which have id and url
 */
function getValidImages( images ) {
	return filter( images, ( { id, url } ) => id && url );
}

const transforms = {
	from: [
		{
			type: 'block',
			isMultiBlock: true,
			blocks: [ 'core/image' ],
			isMatch: images => getValidImages( images ).length > 0,
			transform: images => {
				const validImages = getValidImages( images );
				return createBlock( 'jetpack/slideshow', {
					images: validImages.map( ( { alt, caption, id, url } ) => ( {
						alt,
						caption,
						id,
						url,
					} ) ),
					ids: validImages.map( ( { id } ) => id ),
				} );
			},
		},
		{
			type: 'block',
			blocks: [ 'core/gallery', 'jetpack/tiled-gallery' ],
			transform: ( { images } ) => {
				const validImages = getValidImages( images );
				if ( validImages.length > 0 ) {
					return createBlock( 'jetpack/slideshow', {
						images: validImages.map( ( { alt, caption, id, url } ) => ( {
							alt,
							caption,
							id,
							url,
						} ) ),
						ids: validImages.map( ( { id } ) => id ),
					} );
				}
				return createBlock( 'jetpack/slideshow' );
			},
		},
	],
	to: [
		{
			type: 'block',
			blocks: [ 'core/gallery' ],
			transform: ( { images, ids } ) => createBlock( 'core/gallery', { images, ids } ),
		},
		{
			type: 'block',
			blocks: [ 'core/image' ],
			transform: ( { images } ) => {
				if ( images.length > 0 ) {
					return images.map( ( { id, url, alt, caption } ) =>
						createBlock( 'core/image', { id, url, alt, caption } )
					);
				}
				return createBlock( 'core/image' );
			},
		},
	],
};

export default transforms;