-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
167 lines (151 loc) · 4.88 KB
/
index.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* WordPress dependencies
*/
import { __ } from 'i18n';
import { Toolbar, Placeholder } from 'components';
import { pick } from 'lodash';
/**
* Internal dependencies
*/
import './style.scss';
import './block.scss';
import { registerBlockType } from '../../api';
import MediaUploadButton from '../../media-upload-button';
import InspectorControls from '../../inspector-controls';
import RangeControl from '../../inspector-controls/range-control';
import ToggleControl from '../../inspector-controls/toggle-control';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import GalleryImage from './gallery-image';
import BlockDescription from '../../block-description';
const MAX_COLUMNS = 8;
const editMediaLibrary = ( attributes, setAttributes ) => {
const frameConfig = {
frame: 'post',
title: __( 'Update Gallery media' ),
button: {
text: __( 'Select' ),
},
multiple: true,
state: 'gallery-edit',
selection: new wp.media.model.Selection( attributes.images, { multiple: true } ),
};
const editFrame = wp.media( frameConfig );
function updateFn() {
setAttributes( {
images: this.frame.state().attributes.library.models.map( ( a ) => {
return a.attributes;
} ),
} );
}
editFrame.on( 'insert', updateFn );
editFrame.state( 'gallery-edit' ).on( 'update', updateFn );
editFrame.open( 'gutenberg-gallery' );
};
// the media library image object contains numerous attributes
// we only need this set to display the image in the library
const slimImageObjects = ( imgs ) => {
const attrSet = [ 'sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt' ];
return imgs.map( ( img ) => pick( img, attrSet ) );
};
function defaultColumnsNumber( attributes ) {
attributes.images = attributes.images || [];
return Math.min( 3, attributes.images.length );
}
registerBlockType( 'core/gallery', {
title: __( 'Gallery' ),
icon: 'format-gallery',
category: 'common',
getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align };
}
},
edit( { attributes, setAttributes, focus, className } ) {
const { images = [], columns = defaultColumnsNumber( attributes ), align = 'none' } = attributes;
const setColumnsNumber = ( event ) => setAttributes( { columns: event.target.value } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const { imageCrop = true } = attributes;
const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } );
const controls = (
focus && (
<BlockControls key="controls">
<BlockAlignmentToolbar
value={ align }
onChange={ updateAlignment }
controls={ [ 'left', 'center', 'right', 'wide', 'full' ] }
/>
{ !! images.length && (
<Toolbar controls={ [ {
icon: 'edit',
title: __( 'Edit Gallery' ),
onClick: () => editMediaLibrary( attributes, setAttributes ),
} ] } />
) }
</BlockControls>
)
);
if ( images.length === 0 ) {
const setMediaUrl = ( imgs ) => setAttributes( { images: slimImageObjects( imgs ) } );
const uploadButtonProps = { isLarge: true };
return [
controls,
<Placeholder
key="placeholder"
instructions={ __( 'Drag images here or insert from media library' ) }
icon="format-gallery"
label={ __( 'Gallery' ) }
className={ className }>
<MediaUploadButton
buttonProps={ uploadButtonProps }
onSelect={ setMediaUrl }
type="image"
autoOpen
multiple="true"
>
{ __( 'Insert from Media Library' ) }
</MediaUploadButton>
</Placeholder>,
];
}
return [
controls,
focus && images.length > 1 && (
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( 'Image galleries are a great way to share groups of pictures on your site.' ) }</p>
</BlockDescription>
<h3>{ __( 'Gallery Settings' ) }</h3>
<RangeControl
label={ __( 'Columns' ) }
value={ columns }
onChange={ setColumnsNumber }
min="1"
max={ Math.min( MAX_COLUMNS, images.length ) }
/>
<ToggleControl
label={ __( 'Crop Images' ) }
checked={ !! imageCrop }
onChange={ toggleImageCrop }
/>
</InspectorControls>
),
<div key="gallery" className={ `${ className } align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` }>
{ images.map( ( img ) => (
<GalleryImage key={ img.url } img={ img } />
) ) }
</div>,
];
},
save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), align = 'none', imageCrop = true } = attributes;
return (
<div className={ `align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( img ) => (
<GalleryImage key={ img.url } img={ img } />
) ) }
</div>
);
},
} );