-
Notifications
You must be signed in to change notification settings - Fork 4k
/
background.js
321 lines (288 loc) · 7.7 KB
/
background.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';
import { getBlockSupport } from '@wordpress/blocks';
import { focus } from '@wordpress/dom';
import {
__experimentalToolsPanelItem as ToolsPanelItem,
DropZone,
FlexItem,
MenuItem,
VisuallyHidden,
__experimentalItemGroup as ItemGroup,
__experimentalHStack as HStack,
__experimentalTruncate as Truncate,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { Platform, useCallback, useRef } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { getFilename } from '@wordpress/url';
/**
* Internal dependencies
*/
import InspectorControls from '../components/inspector-controls';
import MediaReplaceFlow from '../components/media-replace-flow';
import useSetting from '../components/use-setting';
import { cleanEmptyObject } from './utils';
import { store as blockEditorStore } from '../store';
export const BACKGROUND_SUPPORT_KEY = 'background';
export const IMAGE_BACKGROUND_TYPE = 'image';
/**
* Checks if there is a current value in the background image block support
* attributes.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a background image value set.
*/
export function hasBackgroundImageValue( props ) {
const hasValue =
!! props.attributes.style?.background?.backgroundImage?.id ||
!! props.attributes.style?.background?.backgroundImage?.url;
return hasValue;
}
/**
* Determine whether there is block support for background.
*
* @param {string} blockName Block name.
* @param {string} feature Background image feature to check for.
*
* @return {boolean} Whether there is support.
*/
export function hasBackgroundSupport( blockName, feature = 'any' ) {
if ( Platform.OS !== 'web' ) {
return false;
}
const support = getBlockSupport( blockName, BACKGROUND_SUPPORT_KEY );
if ( support === true ) {
return true;
}
if ( feature === 'any' ) {
return !! support?.backgroundImage;
}
return !! support?.[ feature ];
}
/**
* Resets the background image block support attributes. This can be used when disabling
* the background image controls for a block via a `ToolsPanel`.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetBackgroundImage( { attributes = {}, setAttributes } ) {
const { style = {} } = attributes;
setAttributes( {
style: cleanEmptyObject( {
...style,
background: {
...style?.background,
backgroundImage: undefined,
},
} ),
} );
}
function InspectorImagePreview( { label, filename, url: imgUrl } ) {
const imgLabel = label || getFilename( imgUrl );
return (
<ItemGroup as="span">
<HStack justify="flex-start" as="span">
<span
className={ classnames(
'block-editor-hooks__background__inspector-image-indicator-wrapper',
{
'has-image': imgUrl,
}
) }
aria-hidden
>
{ imgUrl && (
<span
className="block-editor-hooks__background__inspector-image-indicator"
style={ {
backgroundImage: `url(${ imgUrl })`,
} }
/>
) }
</span>
<FlexItem as="span">
<Truncate
numberOfLines={ 1 }
className="block-editor-hooks__background__inspector-media-replace-title"
>
{ imgLabel }
</Truncate>
<VisuallyHidden as="span">
{ filename
? sprintf(
/* translators: %s: file name */
__( 'Selected image: %s' ),
filename
)
: __( 'No image selected' ) }
</VisuallyHidden>
</FlexItem>
</HStack>
</ItemGroup>
);
}
function BackgroundImagePanelItem( props ) {
const { attributes, clientId, setAttributes } = props;
const { id, title, url } =
attributes.style?.background?.backgroundImage || {};
const replaceContainerRef = useRef();
const { mediaUpload } = useSelect( ( select ) => {
return {
mediaUpload: select( blockEditorStore ).getSettings().mediaUpload,
};
} );
const { createErrorNotice } = useDispatch( noticesStore );
const onUploadError = ( message ) => {
createErrorNotice( message, { type: 'snackbar' } );
};
const onSelectMedia = ( media ) => {
if ( ! media || ! media.url ) {
const newStyle = {
...attributes.style,
background: {
...attributes.style?.background,
backgroundImage: undefined,
},
};
const newAttributes = {
style: cleanEmptyObject( newStyle ),
};
setAttributes( newAttributes );
return;
}
if ( isBlobURL( media.url ) ) {
return;
}
// For media selections originated from a file upload.
if (
( media.media_type &&
media.media_type !== IMAGE_BACKGROUND_TYPE ) ||
( ! media.media_type &&
media.type &&
media.type !== IMAGE_BACKGROUND_TYPE )
) {
onUploadError(
__( 'Only images can be used as a background image.' )
);
return;
}
const newStyle = {
...attributes.style,
background: {
...attributes.style?.background,
backgroundImage: {
url: media.url,
id: media.id,
source: 'file',
title: media.title || undefined,
},
},
};
const newAttributes = {
style: cleanEmptyObject( newStyle ),
};
setAttributes( newAttributes );
};
const onFilesDrop = ( filesList ) => {
mediaUpload( {
allowedTypes: [ 'image' ],
filesList,
onFileChange( [ image ] ) {
if ( isBlobURL( image?.url ) ) {
return;
}
onSelectMedia( image );
},
onError: onUploadError,
} );
};
const resetAllFilter = useCallback( ( previousValue ) => {
return {
...previousValue,
style: {
...previousValue.style,
background: undefined,
},
};
}, [] );
const hasValue = hasBackgroundImageValue( props );
return (
<ToolsPanelItem
className="single-column"
hasValue={ () => hasValue }
label={ __( 'Background image' ) }
onDeselect={ () => resetBackgroundImage( props ) }
isShownByDefault={ true }
resetAllFilter={ resetAllFilter }
panelId={ clientId }
>
<div
className="block-editor-hooks__background__inspector-media-replace-container"
ref={ replaceContainerRef }
>
<MediaReplaceFlow
mediaId={ id }
mediaURL={ url }
allowedTypes={ [ IMAGE_BACKGROUND_TYPE ] }
accept="image/*"
onSelect={ onSelectMedia }
name={
<InspectorImagePreview
label={ __( 'Background image' ) }
filename={ title }
url={ url }
/>
}
variant="secondary"
>
{ hasValue && (
<MenuItem
onClick={ () => {
const [ toggleButton ] = focus.tabbable.find(
replaceContainerRef.current
);
// Focus the toggle button and close the dropdown menu.
// This ensures similar behaviour as to selecting an image, where the dropdown is
// closed and focus is redirected to the dropdown toggle button.
toggleButton?.focus();
toggleButton?.click();
resetBackgroundImage( props );
} }
>
{ __( 'Reset ' ) }
</MenuItem>
) }
</MediaReplaceFlow>
<DropZone
onFilesDrop={ onFilesDrop }
label={ __( 'Drop to upload' ) }
/>
</div>
</ToolsPanelItem>
);
}
export function BackgroundImagePanel( props ) {
const isBackgroundImageSupported =
useSetting( 'background.backgroundImage' ) &&
hasBackgroundSupport( props.name, 'backgroundImage' );
if ( ! isBackgroundImageSupported ) {
return null;
}
return (
<InspectorControls group="background">
{ isBackgroundImageSupported && (
<BackgroundImagePanelItem { ...props } />
) }
</InspectorControls>
);
}