@wordpress/media-utils 5.31.1-next.f56bd8138.0 → 5.32.1-next.47f435fc9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/components/index.js +36 -11
  3. package/build/components/index.js.map +7 -1
  4. package/build/components/media-upload/index.js +156 -221
  5. package/build/components/media-upload/index.js.map +7 -1
  6. package/build/index.js +44 -65
  7. package/build/index.js.map +7 -1
  8. package/build/lock-unlock.js +31 -14
  9. package/build/lock-unlock.js.map +7 -1
  10. package/build/private-apis.js +31 -17
  11. package/build/private-apis.js.map +7 -1
  12. package/build/utils/flatten-form-data.js +28 -20
  13. package/build/utils/flatten-form-data.js.map +7 -1
  14. package/build/utils/get-mime-types-array.js +38 -20
  15. package/build/utils/get-mime-types-array.js.map +7 -1
  16. package/build/utils/sideload-media.js +50 -38
  17. package/build/utils/sideload-media.js.map +7 -1
  18. package/build/utils/sideload-to-server.js +53 -36
  19. package/build/utils/sideload-to-server.js.map +7 -1
  20. package/build/utils/transform-attachment.js +29 -23
  21. package/build/utils/transform-attachment.js.map +7 -1
  22. package/build/utils/types.js +16 -5
  23. package/build/utils/types.js.map +7 -1
  24. package/build/utils/upload-error.js +30 -21
  25. package/build/utils/upload-error.js.map +7 -1
  26. package/build/utils/upload-media.js +62 -71
  27. package/build/utils/upload-media.js.map +7 -1
  28. package/build/utils/upload-to-server.js +54 -27
  29. package/build/utils/upload-to-server.js.map +7 -1
  30. package/build/utils/validate-file-size.js +44 -32
  31. package/build/utils/validate-file-size.js.map +7 -1
  32. package/build/utils/validate-mime-type-for-user.js +42 -30
  33. package/build/utils/validate-mime-type-for-user.js.map +7 -1
  34. package/build/utils/validate-mime-type.js +37 -32
  35. package/build/utils/validate-mime-type.js.map +7 -1
  36. package/build-module/components/index.js +5 -2
  37. package/build-module/components/index.js.map +7 -1
  38. package/build-module/components/media-upload/index.js +137 -215
  39. package/build-module/components/media-upload/index.js.map +7 -1
  40. package/build-module/index.js +16 -8
  41. package/build-module/index.js.map +7 -1
  42. package/build-module/lock-unlock.js +8 -7
  43. package/build-module/lock-unlock.js.map +7 -1
  44. package/build-module/private-apis.js +7 -11
  45. package/build-module/private-apis.js.map +7 -1
  46. package/build-module/utils/flatten-form-data.js +7 -17
  47. package/build-module/utils/flatten-form-data.js.map +7 -1
  48. package/build-module/utils/get-mime-types-array.js +17 -17
  49. package/build-module/utils/get-mime-types-array.js.map +7 -1
  50. package/build-module/utils/sideload-media.js +28 -34
  51. package/build-module/utils/sideload-media.js.map +7 -1
  52. package/build-module/utils/sideload-to-server.js +22 -33
  53. package/build-module/utils/sideload-to-server.js.map +7 -1
  54. package/build-module/utils/transform-attachment.js +8 -20
  55. package/build-module/utils/transform-attachment.js.map +7 -1
  56. package/build-module/utils/types.js +1 -2
  57. package/build-module/utils/types.js.map +7 -1
  58. package/build-module/utils/upload-error.js +9 -17
  59. package/build-module/utils/upload-error.js.map +7 -1
  60. package/build-module/utils/upload-media.js +36 -63
  61. package/build-module/utils/upload-media.js.map +7 -1
  62. package/build-module/utils/upload-to-server.js +23 -22
  63. package/build-module/utils/upload-to-server.js.map +7 -1
  64. package/build-module/utils/validate-file-size.js +19 -25
  65. package/build-module/utils/validate-file-size.js.map +7 -1
  66. package/build-module/utils/validate-mime-type-for-user.js +18 -24
  67. package/build-module/utils/validate-mime-type-for-user.js.map +7 -1
  68. package/build-module/utils/validate-mime-type.js +14 -27
  69. package/build-module/utils/validate-mime-type.js.map +7 -1
  70. package/package.json +15 -8
@@ -1,19 +1,8 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { Component } from '@wordpress/element';
5
- import { __ } from '@wordpress/i18n';
1
+ import { Component } from "@wordpress/element";
2
+ import { __ } from "@wordpress/i18n";
6
3
  const DEFAULT_EMPTY_GALLERY = [];
7
-
8
- /**
9
- * Prepares the Featured Image toolbars and frames.
10
- *
11
- * @return {window.wp.media.view.MediaFrame.Select} The default media workflow.
12
- */
13
4
  const getFeaturedImageMediaFrame = () => {
14
- const {
15
- wp
16
- } = window;
5
+ const { wp } = window;
17
6
  return wp.media.view.MediaFrame.Select.extend({
18
7
  /**
19
8
  * Enables the Set Featured Image Button.
@@ -33,16 +22,12 @@ const getFeaturedImageMediaFrame = () => {
33
22
  * @return {void}
34
23
  */
35
24
  editState() {
36
- const selection = this.state('featured-image').get('selection');
25
+ const selection = this.state("featured-image").get("selection");
37
26
  const view = new wp.media.view.EditImage({
38
27
  model: selection.single(),
39
28
  controller: this
40
29
  }).render();
41
-
42
- // Set the view to the EditImage frame using the selected image.
43
30
  this.content.set(view);
44
-
45
- // After bringing in the frame, load the actual editor via an ajax call.
46
31
  view.loadEditor();
47
32
  },
48
33
  /**
@@ -51,28 +36,23 @@ const getFeaturedImageMediaFrame = () => {
51
36
  * @return {void}
52
37
  */
53
38
  createStates: function createStates() {
54
- this.on('toolbar:create:featured-image', this.featuredImageToolbar, this);
55
- this.on('content:render:edit-image', this.editState, this);
56
- this.states.add([new wp.media.controller.FeaturedImage(), new wp.media.controller.EditImage({
57
- model: this.options.editImage
58
- })]);
39
+ this.on(
40
+ "toolbar:create:featured-image",
41
+ this.featuredImageToolbar,
42
+ this
43
+ );
44
+ this.on("content:render:edit-image", this.editState, this);
45
+ this.states.add([
46
+ new wp.media.controller.FeaturedImage(),
47
+ new wp.media.controller.EditImage({
48
+ model: this.options.editImage
49
+ })
50
+ ]);
59
51
  }
60
52
  });
61
53
  };
62
-
63
- /**
64
- * Prepares the default frame for selecting a single media item.
65
- *
66
- * @return {window.wp.media.view.MediaFrame.Select} The default media workflow.
67
- */
68
54
  const getSingleMediaFrame = () => {
69
- const {
70
- wp
71
- } = window;
72
-
73
- // Extend the default Select frame, and use the same `createStates` method as in core,
74
- // but with the addition of `filterable: 'uploaded'` to the Library state, so that
75
- // the user can filter the media library by uploaded media.
55
+ const { wp } = window;
76
56
  return wp.media.view.MediaFrame.Select.extend({
77
57
  /**
78
58
  * Create the default states on the frame.
@@ -82,39 +62,25 @@ const getSingleMediaFrame = () => {
82
62
  if (this.options.states) {
83
63
  return;
84
64
  }
85
-
86
- // Add the default states.
87
65
  this.states.add([
88
- // Main states.
89
- new wp.media.controller.Library({
90
- library: wp.media.query(options.library),
91
- multiple: options.multiple,
92
- title: options.title,
93
- priority: 20,
94
- filterable: 'uploaded' // Allow filtering by uploaded images.
95
- }), new wp.media.controller.EditImage({
96
- model: options.editImage
97
- })]);
66
+ // Main states.
67
+ new wp.media.controller.Library({
68
+ library: wp.media.query(options.library),
69
+ multiple: options.multiple,
70
+ title: options.title,
71
+ priority: 20,
72
+ filterable: "uploaded"
73
+ // Allow filtering by uploaded images.
74
+ }),
75
+ new wp.media.controller.EditImage({
76
+ model: options.editImage
77
+ })
78
+ ]);
98
79
  }
99
80
  });
100
81
  };
101
-
102
- /**
103
- * Prepares the Gallery toolbars and frames.
104
- *
105
- * @return {window.wp.media.view.MediaFrame.Post} The default media workflow.
106
- */
107
82
  const getGalleryDetailsMediaFrame = () => {
108
- const {
109
- wp
110
- } = window;
111
- /**
112
- * Custom gallery details frame.
113
- *
114
- * @see https://github.com/xwp/wp-core-media-widgets/blob/905edbccfc2a623b73a93dac803c5335519d7837/wp-admin/js/widgets/media-gallery-widget.js
115
- * @class GalleryDetailsMediaFrame
116
- * @class
117
- */
83
+ const { wp } = window;
118
84
  return wp.media.view.MediaFrame.Post.extend({
119
85
  /**
120
86
  * Set up gallery toolbar.
@@ -122,33 +88,33 @@ const getGalleryDetailsMediaFrame = () => {
122
88
  * @return {void}
123
89
  */
124
90
  galleryToolbar() {
125
- const editing = this.state().get('editing');
126
- this.toolbar.set(new wp.media.view.Toolbar({
127
- controller: this,
128
- items: {
129
- insert: {
130
- style: 'primary',
131
- text: editing ? wp.media.view.l10n.updateGallery : wp.media.view.l10n.insertGallery,
132
- priority: 80,
133
- requires: {
134
- library: true
135
- },
136
- /**
137
- * @fires wp.media.controller.State#update
138
- */
139
- click() {
140
- const controller = this.controller,
141
- state = controller.state();
142
- controller.close();
143
- state.trigger('update', state.get('library'));
144
-
145
- // Restore and reset the default state.
146
- controller.setState(controller.options.state);
147
- controller.reset();
91
+ const editing = this.state().get("editing");
92
+ this.toolbar.set(
93
+ new wp.media.view.Toolbar({
94
+ controller: this,
95
+ items: {
96
+ insert: {
97
+ style: "primary",
98
+ text: editing ? wp.media.view.l10n.updateGallery : wp.media.view.l10n.insertGallery,
99
+ priority: 80,
100
+ requires: { library: true },
101
+ /**
102
+ * @fires wp.media.controller.State#update
103
+ */
104
+ click() {
105
+ const controller = this.controller, state = controller.state();
106
+ controller.close();
107
+ state.trigger(
108
+ "update",
109
+ state.get("library")
110
+ );
111
+ controller.setState(controller.options.state);
112
+ controller.reset();
113
+ }
148
114
  }
149
115
  }
150
- }
151
- }));
116
+ })
117
+ );
152
118
  },
153
119
  /**
154
120
  * Handle the edit state requirements of selected media item.
@@ -156,16 +122,12 @@ const getGalleryDetailsMediaFrame = () => {
156
122
  * @return {void}
157
123
  */
158
124
  editState() {
159
- const selection = this.state('gallery').get('selection');
125
+ const selection = this.state("gallery").get("selection");
160
126
  const view = new wp.media.view.EditImage({
161
127
  model: selection.single(),
162
128
  controller: this
163
129
  }).render();
164
-
165
- // Set the view to the EditImage frame using the selected image.
166
130
  this.content.set(view);
167
-
168
- // After bringing in the frame, load the actual editor via an ajax call.
169
131
  view.loadEditor();
170
132
  },
171
133
  /**
@@ -174,37 +136,49 @@ const getGalleryDetailsMediaFrame = () => {
174
136
  * @return {void}
175
137
  */
176
138
  createStates: function createStates() {
177
- this.on('toolbar:create:main-gallery', this.galleryToolbar, this);
178
- this.on('content:render:edit-image', this.editState, this);
179
- this.states.add([new wp.media.controller.Library({
180
- id: 'gallery',
181
- title: wp.media.view.l10n.createGalleryTitle,
182
- priority: 40,
183
- toolbar: 'main-gallery',
184
- filterable: 'uploaded',
185
- multiple: 'add',
186
- editable: false,
187
- library: wp.media.query({
188
- type: 'image',
189
- ...this.options.library
190
- })
191
- }), new wp.media.controller.EditImage({
192
- model: this.options.editImage
193
- }), new wp.media.controller.GalleryEdit({
194
- library: this.options.selection,
195
- editing: this.options.editing,
196
- menu: 'gallery',
197
- displaySettings: false,
198
- multiple: true
199
- }), new wp.media.controller.GalleryAdd()]);
139
+ this.on("toolbar:create:main-gallery", this.galleryToolbar, this);
140
+ this.on("content:render:edit-image", this.editState, this);
141
+ this.states.add([
142
+ new wp.media.controller.Library({
143
+ id: "gallery",
144
+ title: wp.media.view.l10n.createGalleryTitle,
145
+ priority: 40,
146
+ toolbar: "main-gallery",
147
+ filterable: "uploaded",
148
+ multiple: "add",
149
+ editable: false,
150
+ library: wp.media.query({
151
+ type: "image",
152
+ ...this.options.library
153
+ })
154
+ }),
155
+ new wp.media.controller.EditImage({
156
+ model: this.options.editImage
157
+ }),
158
+ new wp.media.controller.GalleryEdit({
159
+ library: this.options.selection,
160
+ editing: this.options.editing,
161
+ menu: "gallery",
162
+ displaySettings: false,
163
+ multiple: true
164
+ }),
165
+ new wp.media.controller.GalleryAdd()
166
+ ]);
200
167
  }
201
168
  });
202
169
  };
203
-
204
- // The media library image object contains numerous attributes
205
- // we only need this set to display the image in the library.
206
- const slimImageObject = img => {
207
- const attrSet = ['sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt', 'link', 'caption'];
170
+ const slimImageObject = (img) => {
171
+ const attrSet = [
172
+ "sizes",
173
+ "mime",
174
+ "type",
175
+ "subtype",
176
+ "id",
177
+ "url",
178
+ "alt",
179
+ "link",
180
+ "caption"
181
+ ];
208
182
  return attrSet.reduce((result, key) => {
209
183
  if (img?.hasOwnProperty(key)) {
210
184
  result[key] = img[key];
@@ -212,17 +186,15 @@ const slimImageObject = img => {
212
186
  return result;
213
187
  }, {});
214
188
  };
215
- const getAttachmentsCollection = ids => {
216
- const {
217
- wp
218
- } = window;
189
+ const getAttachmentsCollection = (ids) => {
190
+ const { wp } = window;
219
191
  return wp.media.query({
220
- order: 'ASC',
221
- orderby: 'post__in',
192
+ order: "ASC",
193
+ orderby: "post__in",
222
194
  post__in: ids,
223
195
  posts_per_page: -1,
224
196
  query: true,
225
- type: 'image'
197
+ type: "image"
226
198
  });
227
199
  };
228
200
  class MediaUpload extends Component {
@@ -235,13 +207,11 @@ class MediaUpload extends Component {
235
207
  this.onClose = this.onClose.bind(this);
236
208
  }
237
209
  initializeListeners() {
238
- // When an image is selected in the media frame...
239
- this.frame.on('select', this.onSelect);
240
- this.frame.on('update', this.onUpdate);
241
- this.frame.on('open', this.onOpen);
242
- this.frame.on('close', this.onClose);
210
+ this.frame.on("select", this.onSelect);
211
+ this.frame.on("update", this.onUpdate);
212
+ this.frame.on("open", this.onOpen);
213
+ this.frame.on("close", this.onClose);
243
214
  }
244
-
245
215
  /**
246
216
  * Sets the Gallery frame and initializes listeners.
247
217
  *
@@ -254,26 +224,19 @@ class MediaUpload extends Component {
254
224
  multiple = false,
255
225
  value = DEFAULT_EMPTY_GALLERY
256
226
  } = this.props;
257
-
258
- // If the value did not changed there is no need to rebuild the frame,
259
- // we can continue to use the existing one.
260
227
  if (value === this.lastGalleryValue) {
261
228
  return;
262
229
  }
263
- const {
264
- wp
265
- } = window;
230
+ const { wp } = window;
266
231
  this.lastGalleryValue = value;
267
-
268
- // If a frame already existed remove it.
269
232
  if (this.frame) {
270
233
  this.frame.remove();
271
234
  }
272
235
  let currentState;
273
236
  if (addToGallery) {
274
- currentState = 'gallery-library';
237
+ currentState = "gallery-library";
275
238
  } else {
276
- currentState = value && value.length ? 'gallery-edit' : 'gallery';
239
+ currentState = value && value.length ? "gallery-edit" : "gallery";
277
240
  }
278
241
  if (!this.GalleryDetailsMediaFrame) {
279
242
  this.GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
@@ -293,21 +256,14 @@ class MediaUpload extends Component {
293
256
  wp.media.frame = this.frame;
294
257
  this.initializeListeners();
295
258
  }
296
-
297
259
  /**
298
260
  * Initializes the Media Library requirements for the featured image flow.
299
261
  *
300
262
  * @return {void}
301
263
  */
302
264
  buildAndSetFeatureImageFrame() {
303
- const {
304
- wp
305
- } = window;
306
- const {
307
- value: featuredImageId,
308
- multiple,
309
- allowedTypes
310
- } = this.props;
265
+ const { wp } = window;
266
+ const { value: featuredImageId, multiple, allowedTypes } = this.props;
311
267
  const featuredImageFrame = getFeaturedImageMediaFrame();
312
268
  const attachments = getAttachmentsCollection(featuredImageId);
313
269
  const selection = new wp.media.model.Selection(attachments.models, {
@@ -315,35 +271,28 @@ class MediaUpload extends Component {
315
271
  });
316
272
  this.frame = new featuredImageFrame({
317
273
  mimeType: allowedTypes,
318
- state: 'featured-image',
274
+ state: "featured-image",
319
275
  multiple,
320
276
  selection,
321
277
  editing: featuredImageId
322
278
  });
323
279
  wp.media.frame = this.frame;
324
- // In order to select the current featured image when opening
325
- // the media library we have to set the appropriate settings.
326
- // Currently they are set in php for the post editor, but
327
- // not for site editor.
328
280
  wp.media.view.settings.post = {
329
281
  ...wp.media.view.settings.post,
330
282
  featuredImageId: featuredImageId || -1
331
283
  };
332
284
  }
333
-
334
285
  /**
335
286
  * Initializes the Media Library requirements for the single image flow.
336
287
  *
337
288
  * @return {void}
338
289
  */
339
290
  buildAndSetSingleMediaFrame() {
340
- const {
341
- wp
342
- } = window;
291
+ const { wp } = window;
343
292
  const {
344
293
  allowedTypes,
345
294
  multiple = false,
346
- title = __('Select or Upload Media'),
295
+ title = __("Select or Upload Media"),
347
296
  value
348
297
  } = this.props;
349
298
  const frameConfig = {
@@ -351,12 +300,8 @@ class MediaUpload extends Component {
351
300
  multiple
352
301
  };
353
302
  if (!!allowedTypes) {
354
- frameConfig.library = {
355
- type: allowedTypes
356
- };
303
+ frameConfig.library = { type: allowedTypes };
357
304
  }
358
-
359
- // If a frame already exists, remove it.
360
305
  if (this.frame) {
361
306
  this.frame.remove();
362
307
  }
@@ -377,73 +322,55 @@ class MediaUpload extends Component {
377
322
  this.frame?.remove();
378
323
  }
379
324
  onUpdate(selections) {
380
- const {
381
- onSelect,
382
- multiple = false
383
- } = this.props;
325
+ const { onSelect, multiple = false } = this.props;
384
326
  const state = this.frame.state();
385
- const selectedImages = selections || state.get('selection');
327
+ const selectedImages = selections || state.get("selection");
386
328
  if (!selectedImages || !selectedImages.models.length) {
387
329
  return;
388
330
  }
389
331
  if (multiple) {
390
- onSelect(selectedImages.models.map(model => slimImageObject(model.toJSON())));
332
+ onSelect(
333
+ selectedImages.models.map(
334
+ (model) => slimImageObject(model.toJSON())
335
+ )
336
+ );
391
337
  } else {
392
338
  onSelect(slimImageObject(selectedImages.models[0].toJSON()));
393
339
  }
394
340
  }
395
341
  onSelect() {
396
- const {
397
- onSelect,
398
- multiple = false
399
- } = this.props;
400
- // Get media attachment details from the frame state.
401
- const attachment = this.frame.state().get('selection').toJSON();
342
+ const { onSelect, multiple = false } = this.props;
343
+ const attachment = this.frame.state().get("selection").toJSON();
402
344
  onSelect(multiple ? attachment : attachment[0]);
403
345
  }
404
346
  onOpen() {
405
- const {
406
- wp
407
- } = window;
408
- const {
409
- value
410
- } = this.props;
347
+ const { wp } = window;
348
+ const { value } = this.props;
411
349
  this.updateCollection();
412
-
413
- //Handle active tab in media model on model open.
414
350
  if (this.props.mode) {
415
351
  this.frame.content.mode(this.props.mode);
416
352
  }
417
-
418
- // Handle both this.props.value being either (number[]) multiple ids
419
- // (for galleries) or a (number) singular id (e.g. image block).
420
353
  const hasMedia = Array.isArray(value) ? !!value?.length : !!value;
421
354
  if (!hasMedia) {
422
355
  return;
423
356
  }
424
357
  const isGallery = this.props.gallery;
425
- const selection = this.frame.state().get('selection');
358
+ const selection = this.frame.state().get("selection");
426
359
  const valueArray = Array.isArray(value) ? value : [value];
427
360
  if (!isGallery) {
428
- valueArray.forEach(id => {
361
+ valueArray.forEach((id) => {
429
362
  selection.add(wp.media.attachment(id));
430
363
  });
431
364
  }
432
-
433
- // Load the images so they are available in the media modal.
434
365
  const attachments = getAttachmentsCollection(valueArray);
435
-
436
- // Once attachments are loaded, set the current selection.
437
- attachments.more().done(function () {
366
+ attachments.more().done(function() {
438
367
  if (isGallery && attachments?.models?.length) {
439
368
  selection.add(attachments.models);
440
369
  }
441
370
  });
442
371
  }
443
372
  onClose() {
444
- const {
445
- onClose
446
- } = this.props;
373
+ const { onClose } = this.props;
447
374
  if (onClose) {
448
375
  onClose();
449
376
  }
@@ -453,14 +380,8 @@ class MediaUpload extends Component {
453
380
  const frameContent = this.frame.content.get();
454
381
  if (frameContent && frameContent.collection) {
455
382
  const collection = frameContent.collection;
456
-
457
- // Clean all attachments we have in memory.
458
- collection.toArray().forEach(model => model.trigger('destroy', model));
459
-
460
- // Reset has more flag, if library had small amount of items all items may have been loaded before.
383
+ collection.toArray().forEach((model) => model.trigger("destroy", model));
461
384
  collection.mirroring._hasMore = true;
462
-
463
- // Request items.
464
385
  collection.more();
465
386
  }
466
387
  }
@@ -485,10 +406,11 @@ class MediaUpload extends Component {
485
406
  this.frame.open();
486
407
  }
487
408
  render() {
488
- return this.props.render({
489
- open: this.openModal
490
- });
409
+ return this.props.render({ open: this.openModal });
491
410
  }
492
411
  }
493
- export default MediaUpload;
494
- //# sourceMappingURL=index.js.map
412
+ var media_upload_default = MediaUpload;
413
+ export {
414
+ media_upload_default as default
415
+ };
416
+ //# sourceMappingURL=index.js.map