@wordpress/block-library 9.19.2 → 9.21.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 (124) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/archives/edit.js +2 -2
  3. package/build/archives/edit.js.map +1 -1
  4. package/build/audio/edit.js +66 -33
  5. package/build/audio/edit.js.map +1 -1
  6. package/build/avatar/index.js +8 -3
  7. package/build/avatar/index.js.map +1 -1
  8. package/build/button/edit.js +43 -16
  9. package/build/button/edit.js.map +1 -1
  10. package/build/categories/edit.js +3 -3
  11. package/build/categories/edit.js.map +1 -1
  12. package/build/comment-template/hooks.js +6 -0
  13. package/build/comment-template/hooks.js.map +1 -1
  14. package/build/cover/index.js +8 -1
  15. package/build/cover/index.js.map +1 -1
  16. package/build/embed/edit.js +4 -1
  17. package/build/embed/edit.js.map +1 -1
  18. package/build/image/constants.js +2 -1
  19. package/build/image/constants.js.map +1 -1
  20. package/build/image/edit.js +3 -2
  21. package/build/image/edit.js.map +1 -1
  22. package/build/image/image.js +98 -80
  23. package/build/image/image.js.map +1 -1
  24. package/build/navigation/edit/index.js +8 -4
  25. package/build/navigation/edit/index.js.map +1 -1
  26. package/build/navigation-link/edit.js +27 -8
  27. package/build/navigation-link/edit.js.map +1 -1
  28. package/build/post-author/index.js +8 -1
  29. package/build/post-author/index.js.map +1 -1
  30. package/build/post-featured-image/edit.js +2 -1
  31. package/build/post-featured-image/edit.js.map +1 -1
  32. package/build/rss/edit.js +21 -1
  33. package/build/rss/edit.js.map +1 -1
  34. package/build/rss/index.js +7 -0
  35. package/build/rss/index.js.map +1 -1
  36. package/build/site-logo/index.js +8 -1
  37. package/build/site-logo/index.js.map +1 -1
  38. package/build/site-title/edit.js +1 -1
  39. package/build/site-title/edit.js.map +1 -1
  40. package/build/social-links/index.js +1 -0
  41. package/build/social-links/index.js.map +1 -1
  42. package/build/table-of-contents/edit.js +50 -8
  43. package/build/table-of-contents/edit.js.map +1 -1
  44. package/build/table-of-contents/hooks.js +13 -4
  45. package/build/table-of-contents/hooks.js.map +1 -1
  46. package/build/table-of-contents/index.js +3 -0
  47. package/build/table-of-contents/index.js.map +1 -1
  48. package/build-module/archives/edit.js +2 -2
  49. package/build-module/archives/edit.js.map +1 -1
  50. package/build-module/audio/edit.js +68 -35
  51. package/build-module/audio/edit.js.map +1 -1
  52. package/build-module/avatar/index.js +8 -3
  53. package/build-module/avatar/index.js.map +1 -1
  54. package/build-module/button/edit.js +44 -17
  55. package/build-module/button/edit.js.map +1 -1
  56. package/build-module/categories/edit.js +3 -3
  57. package/build-module/categories/edit.js.map +1 -1
  58. package/build-module/comment-template/hooks.js +6 -0
  59. package/build-module/comment-template/hooks.js.map +1 -1
  60. package/build-module/cover/index.js +8 -1
  61. package/build-module/cover/index.js.map +1 -1
  62. package/build-module/embed/edit.js +4 -1
  63. package/build-module/embed/edit.js.map +1 -1
  64. package/build-module/image/constants.js +1 -0
  65. package/build-module/image/constants.js.map +1 -1
  66. package/build-module/image/edit.js +3 -2
  67. package/build-module/image/edit.js.map +1 -1
  68. package/build-module/image/image.js +102 -84
  69. package/build-module/image/image.js.map +1 -1
  70. package/build-module/navigation/edit/index.js +8 -4
  71. package/build-module/navigation/edit/index.js.map +1 -1
  72. package/build-module/navigation-link/edit.js +28 -9
  73. package/build-module/navigation-link/edit.js.map +1 -1
  74. package/build-module/post-author/index.js +8 -1
  75. package/build-module/post-author/index.js.map +1 -1
  76. package/build-module/post-featured-image/edit.js +2 -1
  77. package/build-module/post-featured-image/edit.js.map +1 -1
  78. package/build-module/rss/edit.js +22 -2
  79. package/build-module/rss/edit.js.map +1 -1
  80. package/build-module/rss/index.js +7 -0
  81. package/build-module/rss/index.js.map +1 -1
  82. package/build-module/site-logo/index.js +8 -1
  83. package/build-module/site-logo/index.js.map +1 -1
  84. package/build-module/site-title/edit.js +1 -1
  85. package/build-module/site-title/edit.js.map +1 -1
  86. package/build-module/social-links/index.js +1 -0
  87. package/build-module/social-links/index.js.map +1 -1
  88. package/build-module/table-of-contents/edit.js +52 -10
  89. package/build-module/table-of-contents/edit.js.map +1 -1
  90. package/build-module/table-of-contents/hooks.js +13 -4
  91. package/build-module/table-of-contents/hooks.js.map +1 -1
  92. package/build-module/table-of-contents/index.js +3 -0
  93. package/build-module/table-of-contents/index.js.map +1 -1
  94. package/build-style/editor-rtl.css +0 -9
  95. package/build-style/editor.css +0 -9
  96. package/build-style/image/editor-rtl.css +0 -9
  97. package/build-style/image/editor.css +0 -9
  98. package/package.json +35 -35
  99. package/src/archives/edit.js +2 -2
  100. package/src/audio/edit.js +84 -33
  101. package/src/avatar/block.json +8 -3
  102. package/src/button/edit.js +69 -24
  103. package/src/categories/edit.js +3 -3
  104. package/src/comment-template/hooks.js +14 -6
  105. package/src/cover/block.json +8 -1
  106. package/src/embed/edit.js +7 -1
  107. package/src/image/constants.js +1 -0
  108. package/src/image/edit.js +3 -3
  109. package/src/image/editor.scss +0 -13
  110. package/src/image/image.js +124 -134
  111. package/src/navigation/edit/index.js +4 -0
  112. package/src/navigation-link/edit.js +45 -11
  113. package/src/post-author/block.json +8 -1
  114. package/src/post-featured-image/edit.js +2 -1
  115. package/src/rss/block.json +7 -0
  116. package/src/rss/edit.js +21 -0
  117. package/src/rss/index.php +27 -9
  118. package/src/site-logo/block.json +8 -1
  119. package/src/site-title/edit.js +1 -1
  120. package/src/site-title/index.php +1 -1
  121. package/src/social-links/block.json +1 -0
  122. package/src/table-of-contents/block.json +3 -0
  123. package/src/table-of-contents/edit.js +45 -4
  124. package/src/table-of-contents/hooks.js +12 -3
@@ -217,18 +217,29 @@ function Image({
217
217
  lightbox,
218
218
  metadata
219
219
  } = attributes;
220
-
221
- // The only supported unit is px, so we can parseInt to strip the px here.
222
- const numericWidth = width ? parseInt(width, 10) : undefined;
223
- const numericHeight = height ? parseInt(height, 10) : undefined;
224
- const imageRef = (0, _element.useRef)();
220
+ const [imageElement, setImageElement] = (0, _element.useState)();
221
+ const [resizeDelta, setResizeDelta] = (0, _element.useState)(null);
222
+ const [pixelSize, setPixelSize] = (0, _element.useState)({});
223
+ const [offsetTop, setOffsetTop] = (0, _element.useState)(0);
224
+ const setResizeObserved = (0, _compose.useResizeObserver)(([entry]) => {
225
+ if (!resizeDelta) {
226
+ const [box] = entry.borderBoxSize;
227
+ setPixelSize({
228
+ width: box.inlineSize,
229
+ height: box.blockSize
230
+ });
231
+ }
232
+ // This is usually 0 unless the image height is less than the line-height.
233
+ setOffsetTop(entry.target.offsetTop);
234
+ });
235
+ const effectResizeableBoxPlacement = (0, _element.useCallback)(() => {
236
+ var _imageElement$offsetT;
237
+ setOffsetTop((_imageElement$offsetT = imageElement?.offsetTop) !== null && _imageElement$offsetT !== void 0 ? _imageElement$offsetT : 0);
238
+ }, [imageElement]);
239
+ const setRefs = (0, _compose.useMergeRefs)([setImageElement, setResizeObserved]);
225
240
  const {
226
241
  allowResize = true
227
242
  } = context;
228
- const {
229
- getBlock,
230
- getSettings
231
- } = (0, _data.useSelect)(_blockEditor.store);
232
243
  const image = (0, _data.useSelect)(select => id && isSingleSelected ? select(_coreData.store).getMedia(id, {
233
244
  context: 'view'
234
245
  }) : null, [id, isSingleSelected]);
@@ -240,7 +251,8 @@ function Image({
240
251
  } = (0, _data.useSelect)(select => {
241
252
  const {
242
253
  getBlockRootClientId,
243
- canInsertBlockType
254
+ canInsertBlockType,
255
+ getSettings
244
256
  } = select(_blockEditor.store);
245
257
  const rootClientId = getBlockRootClientId(clientId);
246
258
  const settings = getSettings();
@@ -251,6 +263,10 @@ function Image({
251
263
  canInsertCover: canInsertBlockType('core/cover', rootClientId)
252
264
  };
253
265
  }, [clientId]);
266
+ const {
267
+ getBlock,
268
+ getSettings
269
+ } = (0, _data.useSelect)(_blockEditor.store);
254
270
  const {
255
271
  replaceBlocks,
256
272
  toggleSelection
@@ -259,6 +275,9 @@ function Image({
259
275
  createErrorNotice,
260
276
  createSuccessNotice
261
277
  } = (0, _data.useDispatch)(_notices.store);
278
+ const {
279
+ editEntityRecord
280
+ } = (0, _data.useDispatch)(_coreData.store);
262
281
  const isLargeViewport = (0, _compose.useViewportMatch)('medium');
263
282
  const isWideAligned = ['wide', 'full'].includes(align);
264
283
  const [{
@@ -297,9 +316,9 @@ function Image({
297
316
  .fetch(url.includes('?') ? url : url + '?').then(response => response.blob()).then(blob => setExternalBlob(blob))
298
317
  // Do nothing, cannot upload.
299
318
  .catch(() => {});
300
- }, [id, url, isSingleSelected, externalBlob]);
319
+ }, [id, url, isSingleSelected, externalBlob, getSettings]);
301
320
 
302
- // Get naturalWidth and naturalHeight from image ref, and fall back to loaded natural
321
+ // Get naturalWidth and naturalHeight from image, and fall back to loaded natural
303
322
  // width and height. This resolves an issue in Safari where the loaded natural
304
323
  // width and height is otherwise lost when switching between alignments.
305
324
  // See: https://github.com/WordPress/gutenberg/pull/37210.
@@ -308,16 +327,10 @@ function Image({
308
327
  naturalHeight
309
328
  } = (0, _element.useMemo)(() => {
310
329
  return {
311
- naturalWidth: imageRef.current?.naturalWidth || loadedNaturalWidth || undefined,
312
- naturalHeight: imageRef.current?.naturalHeight || loadedNaturalHeight || undefined
330
+ naturalWidth: imageElement?.naturalWidth || loadedNaturalWidth || undefined,
331
+ naturalHeight: imageElement?.naturalHeight || loadedNaturalHeight || undefined
313
332
  };
314
- }, [loadedNaturalWidth, loadedNaturalHeight, imageRef.current?.complete]);
315
- function onResizeStart() {
316
- toggleSelection(false);
317
- }
318
- function onResizeStop() {
319
- toggleSelection(true);
320
- }
333
+ }, [loadedNaturalWidth, loadedNaturalHeight, imageElement?.complete]);
321
334
  function onImageError() {
322
335
  setHasImageErrored(true);
323
336
 
@@ -451,7 +464,21 @@ function Image({
451
464
  !!lightbox && lightbox?.enabled !== lightboxSetting?.enabled || lightboxSetting?.allowEditing;
452
465
  const lightboxChecked = !!lightbox?.enabled || !lightbox && !!lightboxSetting?.enabled;
453
466
  const dropdownMenuProps = (0, _hooks.useToolsPanelDropdownMenuProps)();
454
- const dimensionsControl = /*#__PURE__*/(0, _jsxRuntime.jsx)(DimensionsTool, {
467
+ const dimensionsControl = isResizable && (_constants.SIZED_LAYOUTS.includes(parentLayoutType) ? /*#__PURE__*/(0, _jsxRuntime.jsx)(DimensionsTool, {
468
+ value: {
469
+ aspectRatio
470
+ },
471
+ onChange: ({
472
+ aspectRatio: newAspectRatio
473
+ }) => {
474
+ setAttributes({
475
+ aspectRatio: newAspectRatio,
476
+ scale: 'cover'
477
+ });
478
+ },
479
+ defaultAspectRatio: "auto",
480
+ tools: ['aspectRatio']
481
+ }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(DimensionsTool, {
455
482
  value: {
456
483
  width,
457
484
  height,
@@ -482,22 +509,7 @@ function Image({
482
509
  defaultAspectRatio: "auto",
483
510
  scaleOptions: scaleOptions,
484
511
  unitsOptions: dimensionsUnitsOptions
485
- });
486
- const aspectRatioControl = /*#__PURE__*/(0, _jsxRuntime.jsx)(DimensionsTool, {
487
- value: {
488
- aspectRatio
489
- },
490
- onChange: ({
491
- aspectRatio: newAspectRatio
492
- }) => {
493
- setAttributes({
494
- aspectRatio: newAspectRatio,
495
- scale: 'cover'
496
- });
497
- },
498
- defaultAspectRatio: "auto",
499
- tools: ['aspectRatio']
500
- });
512
+ }));
501
513
  const resetAll = () => {
502
514
  setAttributes({
503
515
  alt: undefined,
@@ -513,7 +525,7 @@ function Image({
513
525
  label: (0, _i18n.__)('Settings'),
514
526
  resetAll: resetAll,
515
527
  dropdownMenuProps: dropdownMenuProps,
516
- children: isResizable && (parentLayoutType === 'grid' ? aspectRatioControl : dimensionsControl)
528
+ children: dimensionsControl
517
529
  })
518
530
  });
519
531
  const arePatternOverridesEnabled = metadata?.bindings?.__default?.source === 'core/pattern-overrides';
@@ -665,7 +677,7 @@ function Image({
665
677
  }),
666
678
  __nextHasNoMarginBottom: true
667
679
  })
668
- }), isResizable && (parentLayoutType === 'grid' ? aspectRatioControl : dimensionsControl), !!imageSizeOptions.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(ResolutionTool, {
680
+ }), dimensionsControl, !!imageSizeOptions.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(ResolutionTool, {
669
681
  value: sizeSlug,
670
682
  onChange: updateImage,
671
683
  options: imageSizeOptions
@@ -710,7 +722,6 @@ function Image({
710
722
  queryId
711
723
  } = context;
712
724
  const isDescendentOfQueryLoop = Number.isFinite(queryId);
713
- const [, setFeaturedImage] = (0, _coreData.useEntityProp)('postType', postType, 'featured_media', postId);
714
725
  let img = temporaryURL && hasImageErrored ?
715
726
  /*#__PURE__*/
716
727
  // Show a placeholder during upload when the blob URL can't be loaded. This can
@@ -730,11 +741,19 @@ function Image({
730
741
  alt: defaultedAlt,
731
742
  onError: onImageError,
732
743
  onLoad: onImageLoad,
733
- ref: imageRef,
744
+ ref: setRefs,
734
745
  className: borderProps.className,
746
+ width: naturalWidth,
747
+ height: naturalHeight,
735
748
  style: {
736
- width: width && height || aspectRatio ? '100%' : undefined,
737
- height: width && height || aspectRatio ? '100%' : undefined,
749
+ aspectRatio,
750
+ ...(resizeDelta ? {
751
+ width: pixelSize.width + resizeDelta.width,
752
+ height: pixelSize.height + resizeDelta.height
753
+ } : {
754
+ width,
755
+ height
756
+ }),
738
757
  objectFit: scale,
739
758
  ...borderProps.style,
740
759
  ...shadowProps.style
@@ -748,8 +767,7 @@ function Image({
748
767
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__experimentalImageEditor, {
749
768
  id: id,
750
769
  url: url,
751
- width: numericWidth,
752
- height: numericHeight,
770
+ ...pixelSize,
753
771
  naturalHeight: naturalHeight,
754
772
  naturalWidth: naturalWidth,
755
773
  onSaveImage: imageAttributes => setAttributes(imageAttributes),
@@ -759,25 +777,18 @@ function Image({
759
777
  borderProps: isRounded ? undefined : borderProps
760
778
  })
761
779
  });
762
- } else if (!isResizable || parentLayoutType === 'grid') {
763
- img = /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
764
- style: {
765
- width,
766
- height,
767
- aspectRatio
768
- },
769
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ImageWrapper, {
770
- href: href,
771
- children: img
772
- })
773
- });
774
780
  } else {
781
+ img = /*#__PURE__*/(0, _jsxRuntime.jsx)(ImageWrapper, {
782
+ href: href,
783
+ children: img
784
+ });
785
+ }
786
+ let resizableBox;
787
+ if (isResizable && isSingleSelected && !isEditingImage && !_constants.SIZED_LAYOUTS.includes(parentLayoutType)) {
775
788
  const numericRatio = aspectRatio && (0, _utils.evalAspectRatio)(aspectRatio);
776
- const customRatio = numericWidth / numericHeight;
789
+ const customRatio = pixelSize.width / pixelSize.height;
777
790
  const naturalRatio = naturalWidth / naturalHeight;
778
791
  const ratio = numericRatio || customRatio || naturalRatio || 1;
779
- const currentWidth = !numericWidth && numericHeight ? numericHeight * ratio : numericWidth;
780
- const currentHeight = !numericHeight && numericWidth ? numericWidth / ratio : numericHeight;
781
792
  const minWidth = naturalWidth < naturalHeight ? _constants.MIN_SIZE : _constants.MIN_SIZE * ratio;
782
793
  const minHeight = naturalHeight < naturalWidth ? _constants.MIN_SIZE : _constants.MIN_SIZE / ratio;
783
794
 
@@ -820,17 +831,16 @@ function Image({
820
831
  }
821
832
  }
822
833
  /* eslint-enable no-lonely-if */
823
- img = /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ResizableBox, {
834
+ resizableBox = /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ResizableBox, {
835
+ ref: effectResizeableBoxPlacement,
824
836
  style: {
825
- display: 'block',
826
- objectFit: scale,
827
- aspectRatio: !width && !height && aspectRatio ? aspectRatio : undefined
837
+ position: 'absolute',
838
+ // To match the vertical-align: bottom of the img (from style.scss)
839
+ // syncs the top with the img. This matters when the img height is
840
+ // less than the line-height.
841
+ inset: `${offsetTop}px 0 0 0`
828
842
  },
829
- size: {
830
- width: currentWidth !== null && currentWidth !== void 0 ? currentWidth : 'auto',
831
- height: currentHeight !== null && currentHeight !== void 0 ? currentHeight : 'auto'
832
- },
833
- showHandle: isSingleSelected,
843
+ size: pixelSize,
834
844
  minWidth: minWidth,
835
845
  maxWidth: maxResizeWidth,
836
846
  minHeight: minHeight,
@@ -842,9 +852,19 @@ function Image({
842
852
  bottom: true,
843
853
  left: showLeftHandle
844
854
  },
845
- onResizeStart: onResizeStart,
846
- onResizeStop: (event, direction, elt) => {
847
- onResizeStop();
855
+ onResizeStart: () => {
856
+ toggleSelection(false);
857
+ },
858
+ onResize: (event, direction, elt, delta) => {
859
+ setResizeDelta(delta);
860
+ },
861
+ onResizeStop: (event, direction, elt, delta) => {
862
+ toggleSelection(true);
863
+ setResizeDelta(null);
864
+ setPixelSize(current => ({
865
+ width: current.width + delta.width,
866
+ height: current.height + delta.height
867
+ }));
848
868
 
849
869
  // Clear hardcoded width if the resized width is close to the max-content width.
850
870
  if (maxContentWidth &&
@@ -868,11 +888,7 @@ function Image({
868
888
  aspectRatio: ratio === naturalRatio ? undefined : String(ratio)
869
889
  });
870
890
  },
871
- resizeRatio: align === 'center' ? 2 : 1,
872
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ImageWrapper, {
873
- href: href,
874
- children: img
875
- })
891
+ resizeRatio: align === 'center' ? 2 : 1
876
892
  });
877
893
  }
878
894
  if (!url && !temporaryURL) {
@@ -885,7 +901,9 @@ function Image({
885
901
  * Set the post's featured image with the current image.
886
902
  */
887
903
  const setPostFeatureImage = () => {
888
- setFeaturedImage(id);
904
+ editEntityRecord('postType', postType, postId, {
905
+ featured_media: id
906
+ });
889
907
  createSuccessNotice((0, _i18n.__)('Post featured image updated.'), {
890
908
  type: 'snackbar'
891
909
  });
@@ -899,7 +917,7 @@ function Image({
899
917
  })
900
918
  });
901
919
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
902
- children: [mediaReplaceFlow, controls, featuredImageControl, img, /*#__PURE__*/(0, _jsxRuntime.jsx)(_caption.Caption, {
920
+ children: [mediaReplaceFlow, controls, featuredImageControl, img, resizableBox, /*#__PURE__*/(0, _jsxRuntime.jsx)(_caption.Caption, {
903
921
  attributes: attributes,
904
922
  setAttributes: setAttributes,
905
923
  isSelected: isSingleSelected,