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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/**
* External dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { filter, pick } from 'lodash';
import { isBlobURL } from '@wordpress/blob';
import { withDispatch } from '@wordpress/data';
import {
BlockControls,
BlockIcon,
InspectorControls,
MediaPlaceholder,
MediaUpload,
mediaUpload,
} from '@wordpress/editor';
import {
DropZone,
FormFileUpload,
IconButton,
PanelBody,
RangeControl,
SelectControl,
ToggleControl,
Toolbar,
withNotices,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import { icon } from '.';
import Slideshow from './slideshow';
import './editor.scss';
const ALLOWED_MEDIA_TYPES = [ 'image' ];
const effectOptions = [
{ label: _x( 'Slide', 'Slideshow transition effect', 'jetpack' ), value: 'slide' },
{ label: _x( 'Fade', 'Slideshow transition effect', 'jetpack' ), value: 'fade' },
];
export const pickRelevantMediaFiles = image =>
pick( image, [ 'alt', 'id', 'link', 'url', 'caption' ] );
class SlideshowEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
selectedImage: null,
};
}
setAttributes( attributes ) {
if ( attributes.ids ) {
throw new Error(
'The "ids" attribute should not be changed directly. It is managed automatically when "images" attribute changes'
);
}
if ( attributes.images ) {
attributes = {
...attributes,
ids: attributes.images.map( ( { id } ) => parseInt( id, 10 ) ),
};
}
this.props.setAttributes( attributes );
}
onSelectImages = images => {
const mapped = images.map( image => pickRelevantMediaFiles( image ) );
this.setAttributes( {
images: mapped,
} );
};
onRemoveImage = index => {
return () => {
const images = filter( this.props.attributes.images, ( img, i ) => index !== i );
this.setState( { selectedImage: null } );
this.setAttributes( { images } );
};
};
addFiles = files => {
const currentImages = this.props.attributes.images || [];
const { lockPostSaving, unlockPostSaving, noticeOperations } = this.props;
const lockName = 'slideshowBlockLock';
lockPostSaving( lockName );
mediaUpload( {
allowedTypes: ALLOWED_MEDIA_TYPES,
filesList: files,
onFileChange: images => {
const imagesNormalized = images.map( image => pickRelevantMediaFiles( image ) );
this.setAttributes( {
images: [ ...currentImages, ...imagesNormalized ],
} );
if ( ! imagesNormalized.every( image => isBlobURL( image.url ) ) ) {
unlockPostSaving( lockName );
}
},
onError: noticeOperations.createErrorNotice,
} );
};
uploadFromFiles = event => this.addFiles( event.target.files );
render() {
const {
attributes,
className,
isSelected,
noticeOperations,
noticeUI,
setAttributes,
} = this.props;
const { align, autoplay, delay, effect, images } = attributes;
const prefersReducedMotion =
typeof window !== 'undefined' &&
window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches;
const controls = (
<Fragment>
<InspectorControls>
<PanelBody title={ __( 'Autoplay', 'jetpack' ) }>
<ToggleControl
label={ __( 'Autoplay', 'jetpack' ) }
help={ __( 'Autoplay between slides', 'jetpack' ) }
checked={ autoplay }
onChange={ value => {
setAttributes( { autoplay: value } );
} }
/>
{ autoplay && (
<RangeControl
label={ __( 'Delay between transitions (in seconds)', 'jetpack' ) }
value={ delay }
onChange={ value => {
setAttributes( { delay: value } );
} }
min={ 1 }
max={ 5 }
/>
) }
{ autoplay && prefersReducedMotion && (
<span>
{ __(
'The Reduce Motion accessibility option is selected, therefore autoplay will be disabled in this browser.',
'jetpack'
) }
</span>
) }
</PanelBody>
<PanelBody title={ __( 'Effects', 'jetpack' ) }>
<SelectControl
label={ __( 'Transition effect', 'jetpack' ) }
value={ effect }
onChange={ value => {
setAttributes( { effect: value } );
} }
options={ effectOptions }
/>
</PanelBody>
</InspectorControls>
<BlockControls>
{ !! images.length && (
<Toolbar>
<MediaUpload
onSelect={ this.onSelectImages }
allowedTypes={ ALLOWED_MEDIA_TYPES }
multiple
gallery
value={ images.map( img => img.id ) }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit Slideshow', 'jetpack' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
) }
</BlockControls>
</Fragment>
);
if ( images.length === 0 ) {
return (
<Fragment>
{ controls }
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
className={ className }
labels={ {
title: __( 'Slideshow', 'jetpack' ),
instructions: __(
'Drag images, upload new ones or select files from your library.',
'jetpack'
),
} }
onSelect={ this.onSelectImages }
accept="image/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
multiple
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
</Fragment>
);
}
return (
<Fragment>
{ controls }
{ noticeUI }
<Slideshow
align={ align }
autoplay={ autoplay }
className={ className }
delay={ delay }
effect={ effect }
images={ images }
onError={ noticeOperations.createErrorNotice }
/>
<DropZone onFilesDrop={ this.addFiles } />
{ isSelected && (
<div className="wp-block-jetpack-slideshow__add-item">
<FormFileUpload
multiple
isLarge
className="wp-block-jetpack-slideshow__add-item-button"
onChange={ this.uploadFromFiles }
accept="image/*"
icon="insert"
>
{ __( 'Upload an image', 'jetpack' ) }
</FormFileUpload>
</div>
) }
</Fragment>
);
}
}
export default compose(
withDispatch( dispatch => {
const { lockPostSaving, unlockPostSaving } = dispatch( 'core/editor' );
return {
lockPostSaving,
unlockPostSaving,
};
} ),
withNotices
)( SlideshowEdit );
|