@wordpress/block-editor 12.22.0 → 12.23.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 (273) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +4 -0
  3. package/build/components/block-list/block.js +11 -3
  4. package/build/components/block-list/block.js.map +1 -1
  5. package/build/components/block-mover/button.js +4 -1
  6. package/build/components/block-mover/button.js.map +1 -1
  7. package/build/components/block-mover/index.js +5 -1
  8. package/build/components/block-mover/index.js.map +1 -1
  9. package/build/components/block-patterns-list/index.js +4 -1
  10. package/build/components/block-patterns-list/index.js.map +1 -1
  11. package/build/components/block-settings-menu/block-settings-dropdown.js +7 -3
  12. package/build/components/block-settings-menu/block-settings-dropdown.js.map +1 -1
  13. package/build/components/block-switcher/index.js +4 -3
  14. package/build/components/block-switcher/index.js.map +1 -1
  15. package/build/components/block-toolbar/index.js +5 -4
  16. package/build/components/block-toolbar/index.js.map +1 -1
  17. package/build/components/block-toolbar/shuffle.js +18 -9
  18. package/build/components/block-toolbar/shuffle.js.map +1 -1
  19. package/build/components/block-tools/block-selection-button.js +48 -8
  20. package/build/components/block-tools/block-selection-button.js.map +1 -1
  21. package/build/components/block-tools/index.js +14 -2
  22. package/build/components/block-tools/index.js.map +1 -1
  23. package/build/components/global-styles/advanced-panel.js +9 -2
  24. package/build/components/global-styles/advanced-panel.js.map +1 -1
  25. package/build/components/global-styles/background-panel.js +444 -0
  26. package/build/components/global-styles/background-panel.js.map +1 -0
  27. package/build/components/global-styles/color-panel.js +2 -1
  28. package/build/components/global-styles/color-panel.js.map +1 -1
  29. package/build/components/global-styles/get-global-styles-changes.js +3 -0
  30. package/build/components/global-styles/get-global-styles-changes.js.map +1 -1
  31. package/build/components/global-styles/hooks.js +1 -1
  32. package/build/components/global-styles/hooks.js.map +1 -1
  33. package/build/components/global-styles/index.js +13 -0
  34. package/build/components/global-styles/index.js.map +1 -1
  35. package/build/components/global-styles/use-global-styles-output.js +15 -14
  36. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  37. package/build/components/global-styles/utils.js +2 -1
  38. package/build/components/global-styles/utils.js.map +1 -1
  39. package/build/components/iframe/index.js +9 -4
  40. package/build/components/iframe/index.js.map +1 -1
  41. package/build/components/inserter/block-patterns-tab/index.js.map +1 -1
  42. package/build/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +5 -0
  43. package/build/components/inserter/block-patterns-tab/pattern-category-preview-panel.js.map +1 -1
  44. package/build/components/inserter/library.js +2 -0
  45. package/build/components/inserter/library.js.map +1 -1
  46. package/build/components/inserter/menu.js +8 -2
  47. package/build/components/inserter/menu.js.map +1 -1
  48. package/build/components/inserter/search-items.js +36 -15
  49. package/build/components/inserter/search-items.js.map +1 -1
  50. package/build/components/keyboard-shortcuts/index.js +11 -0
  51. package/build/components/keyboard-shortcuts/index.js.map +1 -1
  52. package/build/components/list-view/block-select-button.js +16 -0
  53. package/build/components/list-view/block-select-button.js.map +1 -1
  54. package/build/components/list-view/block.js +1 -1
  55. package/build/components/list-view/block.js.map +1 -1
  56. package/build/components/list-view/index.js +17 -2
  57. package/build/components/list-view/index.js.map +1 -1
  58. package/build/components/list-view/use-list-view-collapse-items.js +47 -0
  59. package/build/components/list-view/use-list-view-collapse-items.js.map +1 -0
  60. package/build/components/rich-text/index.js +14 -11
  61. package/build/components/rich-text/index.js.map +1 -1
  62. package/build/components/rich-text/index.native.js +17 -11
  63. package/build/components/rich-text/index.native.js.map +1 -1
  64. package/build/components/rich-text/native/get-format-colors.native.js +1 -1
  65. package/build/components/rich-text/native/get-format-colors.native.js.map +1 -1
  66. package/build/components/rich-text/native/index.native.js +2 -2
  67. package/build/components/rich-text/native/index.native.js.map +1 -1
  68. package/build/components/rich-text/with-deprecations.js +0 -3
  69. package/build/components/rich-text/with-deprecations.js.map +1 -1
  70. package/build/components/url-popover/image-url-input-ui.js +50 -36
  71. package/build/components/url-popover/image-url-input-ui.js.map +1 -1
  72. package/build/components/use-block-display-information/index.js +4 -6
  73. package/build/components/use-block-display-information/index.js.map +1 -1
  74. package/build/hooks/anchor.js +2 -2
  75. package/build/hooks/anchor.js.map +1 -1
  76. package/build/hooks/background.js +70 -424
  77. package/build/hooks/background.js.map +1 -1
  78. package/build/hooks/index.js +7 -0
  79. package/build/hooks/index.js.map +1 -1
  80. package/build/hooks/use-zoom-out.js +47 -0
  81. package/build/hooks/use-zoom-out.js.map +1 -0
  82. package/build/index.js +7 -0
  83. package/build/index.js.map +1 -1
  84. package/build/private-apis.js +6 -1
  85. package/build/private-apis.js.map +1 -1
  86. package/build/private-apis.native.js +3 -1
  87. package/build/private-apis.native.js.map +1 -1
  88. package/build/store/private-actions.js +13 -0
  89. package/build/store/private-actions.js.map +1 -1
  90. package/build/store/private-keys.js +2 -1
  91. package/build/store/private-keys.js.map +1 -1
  92. package/build/store/private-selectors.js +24 -3
  93. package/build/store/private-selectors.js.map +1 -1
  94. package/build/store/reducer.js +22 -0
  95. package/build/store/reducer.js.map +1 -1
  96. package/build/store/selectors.js +34 -32
  97. package/build/store/selectors.js.map +1 -1
  98. package/build/store/utils.js +7 -1
  99. package/build/store/utils.js.map +1 -1
  100. package/build/utils/transform-styles/index.js +2 -1
  101. package/build/utils/transform-styles/index.js.map +1 -1
  102. package/build-module/components/block-list/block.js +11 -3
  103. package/build-module/components/block-list/block.js.map +1 -1
  104. package/build-module/components/block-mover/button.js +4 -1
  105. package/build-module/components/block-mover/button.js.map +1 -1
  106. package/build-module/components/block-mover/index.js +5 -1
  107. package/build-module/components/block-mover/index.js.map +1 -1
  108. package/build-module/components/block-patterns-list/index.js +4 -1
  109. package/build-module/components/block-patterns-list/index.js.map +1 -1
  110. package/build-module/components/block-settings-menu/block-settings-dropdown.js +7 -3
  111. package/build-module/components/block-settings-menu/block-settings-dropdown.js.map +1 -1
  112. package/build-module/components/block-switcher/index.js +4 -3
  113. package/build-module/components/block-switcher/index.js.map +1 -1
  114. package/build-module/components/block-toolbar/index.js +5 -4
  115. package/build-module/components/block-toolbar/index.js.map +1 -1
  116. package/build-module/components/block-toolbar/shuffle.js +18 -9
  117. package/build-module/components/block-toolbar/shuffle.js.map +1 -1
  118. package/build-module/components/block-tools/block-selection-button.js +50 -10
  119. package/build-module/components/block-tools/block-selection-button.js.map +1 -1
  120. package/build-module/components/block-tools/index.js +14 -2
  121. package/build-module/components/block-tools/index.js.map +1 -1
  122. package/build-module/components/global-styles/advanced-panel.js +9 -2
  123. package/build-module/components/global-styles/advanced-panel.js.map +1 -1
  124. package/build-module/components/global-styles/background-panel.js +430 -0
  125. package/build-module/components/global-styles/background-panel.js.map +1 -0
  126. package/build-module/components/global-styles/color-panel.js +2 -1
  127. package/build-module/components/global-styles/color-panel.js.map +1 -1
  128. package/build-module/components/global-styles/get-global-styles-changes.js +3 -0
  129. package/build-module/components/global-styles/get-global-styles-changes.js.map +1 -1
  130. package/build-module/components/global-styles/hooks.js +1 -1
  131. package/build-module/components/global-styles/hooks.js.map +1 -1
  132. package/build-module/components/global-styles/index.js +1 -0
  133. package/build-module/components/global-styles/index.js.map +1 -1
  134. package/build-module/components/global-styles/use-global-styles-output.js +16 -15
  135. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  136. package/build-module/components/global-styles/utils.js +1 -0
  137. package/build-module/components/global-styles/utils.js.map +1 -1
  138. package/build-module/components/iframe/index.js +9 -4
  139. package/build-module/components/iframe/index.js.map +1 -1
  140. package/build-module/components/inserter/block-patterns-tab/index.js.map +1 -1
  141. package/build-module/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +5 -0
  142. package/build-module/components/inserter/block-patterns-tab/pattern-category-preview-panel.js.map +1 -1
  143. package/build-module/components/inserter/library.js +2 -0
  144. package/build-module/components/inserter/library.js.map +1 -1
  145. package/build-module/components/inserter/menu.js +8 -2
  146. package/build-module/components/inserter/menu.js.map +1 -1
  147. package/build-module/components/inserter/search-items.js +33 -15
  148. package/build-module/components/inserter/search-items.js.map +1 -1
  149. package/build-module/components/keyboard-shortcuts/index.js +11 -0
  150. package/build-module/components/keyboard-shortcuts/index.js.map +1 -1
  151. package/build-module/components/list-view/block-select-button.js +16 -0
  152. package/build-module/components/list-view/block-select-button.js.map +1 -1
  153. package/build-module/components/list-view/block.js +1 -1
  154. package/build-module/components/list-view/block.js.map +1 -1
  155. package/build-module/components/list-view/index.js +17 -2
  156. package/build-module/components/list-view/index.js.map +1 -1
  157. package/build-module/components/list-view/use-list-view-collapse-items.js +40 -0
  158. package/build-module/components/list-view/use-list-view-collapse-items.js.map +1 -0
  159. package/build-module/components/rich-text/index.js +15 -12
  160. package/build-module/components/rich-text/index.js.map +1 -1
  161. package/build-module/components/rich-text/index.native.js +16 -11
  162. package/build-module/components/rich-text/index.native.js.map +1 -1
  163. package/build-module/components/rich-text/native/get-format-colors.native.js +1 -1
  164. package/build-module/components/rich-text/native/get-format-colors.native.js.map +1 -1
  165. package/build-module/components/rich-text/native/index.native.js +2 -2
  166. package/build-module/components/rich-text/native/index.native.js.map +1 -1
  167. package/build-module/components/rich-text/with-deprecations.js +0 -3
  168. package/build-module/components/rich-text/with-deprecations.js.map +1 -1
  169. package/build-module/components/url-popover/image-url-input-ui.js +50 -36
  170. package/build-module/components/url-popover/image-url-input-ui.js.map +1 -1
  171. package/build-module/components/use-block-display-information/index.js +5 -7
  172. package/build-module/components/use-block-display-information/index.js.map +1 -1
  173. package/build-module/hooks/anchor.js +2 -2
  174. package/build-module/hooks/anchor.js.map +1 -1
  175. package/build-module/hooks/background.js +67 -419
  176. package/build-module/hooks/background.js.map +1 -1
  177. package/build-module/hooks/index.js +1 -0
  178. package/build-module/hooks/index.js.map +1 -1
  179. package/build-module/hooks/use-zoom-out.js +41 -0
  180. package/build-module/hooks/use-zoom-out.js.map +1 -0
  181. package/build-module/index.js +1 -1
  182. package/build-module/index.js.map +1 -1
  183. package/build-module/private-apis.js +7 -2
  184. package/build-module/private-apis.js.map +1 -1
  185. package/build-module/private-apis.native.js +3 -1
  186. package/build-module/private-apis.native.js.map +1 -1
  187. package/build-module/store/private-actions.js +12 -0
  188. package/build-module/store/private-actions.js.map +1 -1
  189. package/build-module/store/private-keys.js +1 -0
  190. package/build-module/store/private-keys.js.map +1 -1
  191. package/build-module/store/private-selectors.js +22 -4
  192. package/build-module/store/private-selectors.js.map +1 -1
  193. package/build-module/store/reducer.js +21 -0
  194. package/build-module/store/reducer.js.map +1 -1
  195. package/build-module/store/selectors.js +35 -33
  196. package/build-module/store/selectors.js.map +1 -1
  197. package/build-module/store/utils.js +6 -1
  198. package/build-module/store/utils.js.map +1 -1
  199. package/build-module/utils/transform-styles/index.js +2 -1
  200. package/build-module/utils/transform-styles/index.js.map +1 -1
  201. package/build-style/content-rtl.css +4 -1
  202. package/build-style/content.css +4 -1
  203. package/build-style/style-rtl.css +84 -79
  204. package/build-style/style.css +84 -79
  205. package/package.json +31 -31
  206. package/src/components/block-list/block.js +19 -3
  207. package/src/components/block-mover/button.js +4 -1
  208. package/src/components/block-mover/index.js +8 -1
  209. package/src/components/block-patterns-list/index.js +22 -17
  210. package/src/components/block-preview/style.scss +28 -0
  211. package/src/components/block-settings-menu/block-settings-dropdown.js +8 -2
  212. package/src/components/block-switcher/index.js +5 -3
  213. package/src/components/block-switcher/style.scss +1 -1
  214. package/src/components/block-toolbar/index.js +22 -19
  215. package/src/components/block-toolbar/shuffle.js +19 -13
  216. package/src/components/block-toolbar/style.scss +1 -1
  217. package/src/components/block-tools/block-selection-button.js +66 -9
  218. package/src/components/block-tools/index.js +18 -1
  219. package/src/components/button-block-appender/content.scss +5 -1
  220. package/src/components/default-block-appender/content.scss +2 -2
  221. package/src/components/global-styles/advanced-panel.js +8 -2
  222. package/src/components/global-styles/background-panel.js +591 -0
  223. package/src/components/global-styles/color-panel.js +2 -1
  224. package/src/components/global-styles/get-global-styles-changes.js +3 -0
  225. package/src/components/global-styles/hooks.js +1 -0
  226. package/src/components/global-styles/index.js +4 -0
  227. package/src/components/global-styles/style.scss +78 -1
  228. package/src/{hooks/test/background.js → components/global-styles/test/background-panel.js} +36 -1
  229. package/src/components/global-styles/test/get-global-styles-changes.js +22 -3
  230. package/src/components/global-styles/test/use-global-styles-output.js +9 -9
  231. package/src/components/global-styles/use-global-styles-output.js +27 -16
  232. package/src/components/global-styles/utils.js +1 -0
  233. package/src/components/iframe/index.js +19 -9
  234. package/src/components/inserter/block-patterns-tab/index.js +1 -0
  235. package/src/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +5 -0
  236. package/src/components/inserter/library.js +4 -0
  237. package/src/components/inserter/menu.js +8 -1
  238. package/src/components/inserter/search-items.js +37 -15
  239. package/src/components/inserter/style.scss +6 -12
  240. package/src/components/keyboard-shortcuts/index.js +11 -0
  241. package/src/components/list-view/block-select-button.js +13 -1
  242. package/src/components/list-view/block.js +1 -1
  243. package/src/components/list-view/index.js +18 -1
  244. package/src/components/list-view/style.scss +4 -4
  245. package/src/components/list-view/use-list-view-collapse-items.js +33 -0
  246. package/src/components/rich-text/index.js +30 -13
  247. package/src/components/rich-text/index.native.js +14 -11
  248. package/src/components/rich-text/native/get-format-colors.native.js +1 -1
  249. package/src/components/rich-text/native/index.native.js +2 -2
  250. package/src/components/rich-text/with-deprecations.js +0 -3
  251. package/src/components/url-popover/image-url-input-ui.js +68 -51
  252. package/src/components/use-block-display-information/index.js +8 -10
  253. package/src/hooks/anchor.js +11 -9
  254. package/src/hooks/background.js +77 -538
  255. package/src/hooks/index.js +1 -0
  256. package/src/hooks/use-zoom-out.js +36 -0
  257. package/src/index.js +1 -0
  258. package/src/private-apis.js +13 -1
  259. package/src/private-apis.native.js +2 -0
  260. package/src/store/private-actions.js +12 -0
  261. package/src/store/private-keys.js +1 -0
  262. package/src/store/private-selectors.js +54 -27
  263. package/src/store/reducer.js +22 -0
  264. package/src/store/selectors.js +195 -180
  265. package/src/store/test/private-actions.js +10 -0
  266. package/src/store/test/private-selectors.js +13 -0
  267. package/src/store/test/reducer.js +26 -0
  268. package/src/store/test/selectors.js +90 -199
  269. package/src/store/utils.js +13 -0
  270. package/src/style.scss +0 -2
  271. package/src/utils/transform-styles/index.js +2 -1
  272. package/src/hooks/anchor.scss +0 -4
  273. package/src/hooks/background.scss +0 -75
@@ -6,32 +6,20 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.BACKGROUND_SUPPORT_KEY = void 0;
8
8
  exports.BackgroundImagePanel = BackgroundImagePanel;
9
- exports.default = exports.coordsToBackgroundPosition = exports.backgroundPositionToCoords = exports.IMAGE_BACKGROUND_TYPE = void 0;
9
+ exports.default = void 0;
10
10
  exports.getBackgroundImageClasses = getBackgroundImageClasses;
11
- exports.hasBackgroundImageValue = hasBackgroundImageValue;
12
- exports.hasBackgroundSizeValue = hasBackgroundSizeValue;
13
11
  exports.hasBackgroundSupport = hasBackgroundSupport;
14
- exports.resetBackgroundImage = resetBackgroundImage;
12
+ exports.setBackgroundStyleDefaults = setBackgroundStyleDefaults;
15
13
  var _react = require("react");
16
- var _classnames = _interopRequireDefault(require("classnames"));
17
- var _blob = require("@wordpress/blob");
18
14
  var _blocks = require("@wordpress/blocks");
19
- var _dom = require("@wordpress/dom");
20
- var _components = require("@wordpress/components");
21
15
  var _data = require("@wordpress/data");
22
16
  var _element = require("@wordpress/element");
23
- var _i18n = require("@wordpress/i18n");
24
- var _notices = require("@wordpress/notices");
25
- var _url = require("@wordpress/url");
26
17
  var _inspectorControls = _interopRequireDefault(require("../components/inspector-controls"));
27
- var _mediaReplaceFlow = _interopRequireDefault(require("../components/media-replace-flow"));
28
- var _useSettings = require("../components/use-settings");
29
18
  var _utils = require("./utils");
30
19
  var _store = require("../store");
31
- /**
32
- * External dependencies
33
- */
34
-
20
+ var _backgroundPanel = _interopRequireWildcard(require("../components/global-styles/background-panel"));
21
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
22
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
35
23
  /**
36
24
  * WordPress dependencies
37
25
  */
@@ -41,31 +29,11 @@ var _store = require("../store");
41
29
  */
42
30
 
43
31
  const BACKGROUND_SUPPORT_KEY = exports.BACKGROUND_SUPPORT_KEY = 'background';
44
- const IMAGE_BACKGROUND_TYPE = exports.IMAGE_BACKGROUND_TYPE = 'image';
45
32
 
46
- /**
47
- * Checks if there is a current value in the background image block support
48
- * attributes.
49
- *
50
- * @param {Object} style Style attribute.
51
- * @return {boolean} Whether or not the block has a background image value set.
52
- */
53
- function hasBackgroundImageValue(style) {
54
- const hasValue = !!style?.background?.backgroundImage?.id || !!style?.background?.backgroundImage?.url;
55
- return hasValue;
56
- }
57
-
58
- /**
59
- * Checks if there is a current value in the background size block support
60
- * attributes. Background size values include background size as well
61
- * as background position.
62
- *
63
- * @param {Object} style Style attribute.
64
- * @return {boolean} Whether or not the block has a background size value set.
65
- */
66
- function hasBackgroundSizeValue(style) {
67
- return style?.background?.backgroundPosition !== undefined || style?.background?.backgroundSize !== undefined;
68
- }
33
+ // Initial control values where no block style is set.
34
+ const BACKGROUND_BLOCK_DEFAULT_VALUES = {
35
+ backgroundSize: 'cover'
36
+ };
69
37
 
70
38
  /**
71
39
  * Determine whether there is block support for background.
@@ -76,9 +44,6 @@ function hasBackgroundSizeValue(style) {
76
44
  * @return {boolean} Whether there is support.
77
45
  */
78
46
  function hasBackgroundSupport(blockName, feature = 'any') {
79
- if (_element.Platform.OS !== 'web') {
80
- return false;
81
- }
82
47
  const support = (0, _blocks.getBlockSupport)(blockName, BACKGROUND_SUPPORT_KEY);
83
48
  if (support === true) {
84
49
  return true;
@@ -88,77 +53,44 @@ function hasBackgroundSupport(blockName, feature = 'any') {
88
53
  }
89
54
  return !!support?.[feature];
90
55
  }
91
- function useBlockProps({
92
- name,
93
- style
94
- }) {
95
- if (!hasBackgroundSupport(name) || !style?.background?.backgroundImage) {
56
+ function setBackgroundStyleDefaults(backgroundStyle) {
57
+ if (!backgroundStyle) {
96
58
  return;
97
59
  }
98
- const backgroundImage = style?.background?.backgroundImage;
99
- let props;
60
+ const backgroundImage = backgroundStyle?.backgroundImage;
61
+ let backgroundStylesWithDefaults;
100
62
 
101
63
  // Set block background defaults.
102
64
  if (backgroundImage?.source === 'file' && !!backgroundImage?.url) {
103
- if (!style?.background?.backgroundSize) {
104
- props = {
105
- style: {
106
- backgroundSize: 'cover'
107
- }
65
+ if (!backgroundStyle?.backgroundSize) {
66
+ backgroundStylesWithDefaults = {
67
+ backgroundSize: 'cover'
108
68
  };
109
69
  }
110
- if ('contain' === style?.background?.backgroundSize && !style?.background?.backgroundPosition) {
111
- props = {
112
- style: {
113
- backgroundPosition: 'center'
114
- }
70
+ if ('contain' === backgroundStyle?.backgroundSize && !backgroundStyle?.backgroundPosition) {
71
+ backgroundStylesWithDefaults = {
72
+ backgroundPosition: 'center'
115
73
  };
116
74
  }
117
75
  }
118
- if (!props) {
76
+ return backgroundStylesWithDefaults;
77
+ }
78
+ function useBlockProps({
79
+ name,
80
+ style
81
+ }) {
82
+ if (!hasBackgroundSupport(name) || !style?.background?.backgroundImage) {
119
83
  return;
120
84
  }
121
- return props;
122
- }
123
-
124
- /**
125
- * Resets the background image block support attributes. This can be used when disabling
126
- * the background image controls for a block via a `ToolsPanel`.
127
- *
128
- * @param {Object} style Style attribute.
129
- * @param {Function} setAttributes Function to set block's attributes.
130
- */
131
- function resetBackgroundImage(style = {}, setAttributes) {
132
- setAttributes({
133
- style: (0, _utils.cleanEmptyObject)({
134
- ...style,
135
- background: {
136
- ...style?.background,
137
- backgroundImage: undefined
138
- }
139
- })
140
- });
141
- }
142
-
143
- /**
144
- * Resets the background size block support attributes. This can be used when disabling
145
- * the background size controls for a block via a `ToolsPanel`.
146
- *
147
- * @param {Object} style Style attribute.
148
- * @param {Function} setAttributes Function to set block's attributes.
149
- */
150
- function resetBackgroundSize(style = {}, setAttributes) {
151
- setAttributes({
152
- style: (0, _utils.cleanEmptyObject)({
153
- ...style,
154
- background: {
155
- ...style?.background,
156
- backgroundPosition: undefined,
157
- backgroundRepeat: undefined,
158
- backgroundSize: undefined
159
- }
160
- })
161
- });
85
+ const backgroundStyles = setBackgroundStyleDefaults(style?.background);
86
+ if (!backgroundStyles) {
87
+ return;
88
+ }
89
+ return {
90
+ style: {
91
+ ...backgroundStyles
92
+ }
93
+ };
162
94
  }
163
95
 
164
96
  /**
@@ -169,343 +101,57 @@ function resetBackgroundSize(style = {}, setAttributes) {
169
101
  * @return {string} CSS class name.
170
102
  */
171
103
  function getBackgroundImageClasses(style) {
172
- return hasBackgroundImageValue(style) ? 'has-background' : '';
104
+ return (0, _backgroundPanel.hasBackgroundImageValue)(style) ? 'has-background' : '';
173
105
  }
174
- function InspectorImagePreview({
175
- label,
176
- filename,
177
- url: imgUrl
106
+ function BackgroundInspectorControl({
107
+ children
178
108
  }) {
179
- const imgLabel = label || (0, _url.getFilename)(imgUrl);
180
- return (0, _react.createElement)(_components.__experimentalItemGroup, {
181
- as: "span"
182
- }, (0, _react.createElement)(_components.__experimentalHStack, {
183
- justify: "flex-start",
184
- as: "span"
185
- }, (0, _react.createElement)("span", {
186
- className: (0, _classnames.default)('block-editor-hooks__background__inspector-image-indicator-wrapper', {
187
- 'has-image': imgUrl
188
- }),
189
- "aria-hidden": true
190
- }, imgUrl && (0, _react.createElement)("span", {
191
- className: "block-editor-hooks__background__inspector-image-indicator",
192
- style: {
193
- backgroundImage: `url(${imgUrl})`
194
- }
195
- })), (0, _react.createElement)(_components.FlexItem, {
196
- as: "span"
197
- }, (0, _react.createElement)(_components.__experimentalTruncate, {
198
- numberOfLines: 1,
199
- className: "block-editor-hooks__background__inspector-media-replace-title"
200
- }, imgLabel), (0, _react.createElement)(_components.VisuallyHidden, {
201
- as: "span"
202
- }, filename ? (0, _i18n.sprintf)( /* translators: %s: file name */
203
- (0, _i18n.__)('Selected image: %s'), filename) : (0, _i18n.__)('No image selected')))));
204
- }
205
- function BackgroundImagePanelItem({
206
- clientId,
207
- isShownByDefault,
208
- setAttributes
209
- }) {
210
- const {
211
- style,
212
- mediaUpload
213
- } = (0, _data.useSelect)(select => {
214
- const {
215
- getBlockAttributes,
216
- getSettings
217
- } = select(_store.store);
218
- return {
219
- style: getBlockAttributes(clientId)?.style,
220
- mediaUpload: getSettings().mediaUpload
221
- };
222
- }, [clientId]);
223
- const {
224
- id,
225
- title,
226
- url
227
- } = style?.background?.backgroundImage || {};
228
- const replaceContainerRef = (0, _element.useRef)();
229
- const {
230
- createErrorNotice
231
- } = (0, _data.useDispatch)(_notices.store);
232
- const onUploadError = message => {
233
- createErrorNotice(message, {
234
- type: 'snackbar'
235
- });
236
- };
237
- const onSelectMedia = media => {
238
- if (!media || !media.url) {
239
- const newStyle = {
240
- ...style,
241
- background: {
242
- ...style?.background,
243
- backgroundImage: undefined
244
- }
245
- };
246
- const newAttributes = {
247
- style: (0, _utils.cleanEmptyObject)(newStyle)
248
- };
249
- setAttributes(newAttributes);
250
- return;
251
- }
252
- if ((0, _blob.isBlobURL)(media.url)) {
253
- return;
254
- }
255
-
256
- // For media selections originated from a file upload.
257
- if (media.media_type && media.media_type !== IMAGE_BACKGROUND_TYPE || !media.media_type && media.type && media.type !== IMAGE_BACKGROUND_TYPE) {
258
- onUploadError((0, _i18n.__)('Only images can be used as a background image.'));
259
- return;
260
- }
261
- const newStyle = {
262
- ...style,
263
- background: {
264
- ...style?.background,
265
- backgroundImage: {
266
- url: media.url,
267
- id: media.id,
268
- source: 'file',
269
- title: media.title || undefined
270
- }
271
- }
272
- };
273
- const newAttributes = {
274
- style: (0, _utils.cleanEmptyObject)(newStyle)
275
- };
276
- setAttributes(newAttributes);
277
- };
278
- const onFilesDrop = filesList => {
279
- mediaUpload({
280
- allowedTypes: ['image'],
281
- filesList,
282
- onFileChange([image]) {
283
- if ((0, _blob.isBlobURL)(image?.url)) {
284
- return;
285
- }
286
- onSelectMedia(image);
287
- },
288
- onError: onUploadError
289
- });
290
- };
291
- const resetAllFilter = (0, _element.useCallback)(previousValue => {
109
+ const resetAllFilter = (0, _element.useCallback)(attributes => {
292
110
  return {
293
- ...previousValue,
111
+ ...attributes,
294
112
  style: {
295
- ...previousValue.style,
113
+ ...attributes.style,
296
114
  background: undefined
297
115
  }
298
116
  };
299
117
  }, []);
300
- const hasValue = hasBackgroundImageValue(style);
301
- return (0, _react.createElement)(_components.__experimentalToolsPanelItem, {
302
- className: "single-column",
303
- hasValue: () => hasValue,
304
- label: (0, _i18n.__)('Background image'),
305
- onDeselect: () => resetBackgroundImage(style, setAttributes),
306
- isShownByDefault: isShownByDefault,
307
- resetAllFilter: resetAllFilter,
308
- panelId: clientId
309
- }, (0, _react.createElement)("div", {
310
- className: "block-editor-hooks__background__inspector-media-replace-container",
311
- ref: replaceContainerRef
312
- }, (0, _react.createElement)(_mediaReplaceFlow.default, {
313
- mediaId: id,
314
- mediaURL: url,
315
- allowedTypes: [IMAGE_BACKGROUND_TYPE],
316
- accept: "image/*",
317
- onSelect: onSelectMedia,
318
- name: (0, _react.createElement)(InspectorImagePreview, {
319
- label: (0, _i18n.__)('Background image'),
320
- filename: title,
321
- url: url
322
- }),
323
- variant: "secondary"
324
- }, hasValue && (0, _react.createElement)(_components.MenuItem, {
325
- onClick: () => {
326
- const [toggleButton] = _dom.focus.tabbable.find(replaceContainerRef.current);
327
- // Focus the toggle button and close the dropdown menu.
328
- // This ensures similar behaviour as to selecting an image, where the dropdown is
329
- // closed and focus is redirected to the dropdown toggle button.
330
- toggleButton?.focus();
331
- toggleButton?.click();
332
- resetBackgroundImage(style, setAttributes);
333
- }
334
- }, (0, _i18n.__)('Reset '))), (0, _react.createElement)(_components.DropZone, {
335
- onFilesDrop: onFilesDrop,
336
- label: (0, _i18n.__)('Drop to upload')
337
- })));
338
- }
339
- function backgroundSizeHelpText(value) {
340
- if (value === 'cover' || value === undefined) {
341
- return (0, _i18n.__)('Image covers the space evenly.');
342
- }
343
- if (value === 'contain') {
344
- return (0, _i18n.__)('Image is contained without distortion.');
345
- }
346
- return (0, _i18n.__)('Specify a fixed width.');
118
+ return (0, _react.createElement)(_inspectorControls.default, {
119
+ group: "background",
120
+ resetAllFilter: resetAllFilter
121
+ }, children);
347
122
  }
348
- const coordsToBackgroundPosition = value => {
349
- if (!value || isNaN(value.x) && isNaN(value.y)) {
350
- return undefined;
351
- }
352
- const x = isNaN(value.x) ? 0.5 : value.x;
353
- const y = isNaN(value.y) ? 0.5 : value.y;
354
- return `${x * 100}% ${y * 100}%`;
355
- };
356
- exports.coordsToBackgroundPosition = coordsToBackgroundPosition;
357
- const backgroundPositionToCoords = value => {
358
- if (!value) {
359
- return {
360
- x: undefined,
361
- y: undefined
362
- };
363
- }
364
- let [x, y] = value.split(' ').map(v => parseFloat(v) / 100);
365
- x = isNaN(x) ? undefined : x;
366
- y = isNaN(y) ? x : y;
367
- return {
368
- x,
369
- y
370
- };
371
- };
372
- exports.backgroundPositionToCoords = backgroundPositionToCoords;
373
- function BackgroundSizePanelItem({
123
+ function BackgroundImagePanel({
374
124
  clientId,
375
- isShownByDefault,
376
- setAttributes
125
+ name,
126
+ setAttributes,
127
+ settings
377
128
  }) {
378
129
  const style = (0, _data.useSelect)(select => select(_store.store).getBlockAttributes(clientId)?.style, [clientId]);
379
- const sizeValue = style?.background?.backgroundSize;
380
- const repeatValue = style?.background?.backgroundRepeat;
381
-
382
- // An `undefined` value is treated as `cover` by the toggle group control.
383
- // An empty string is treated as `auto` by the toggle group control. This
384
- // allows a user to select "Size" and then enter a custom value, with an
385
- // empty value being treated as `auto`.
386
- const currentValueForToggle = sizeValue !== undefined && sizeValue !== 'cover' && sizeValue !== 'contain' || sizeValue === '' ? 'auto' : sizeValue || 'cover';
387
-
388
- // If the current value is `cover` and the repeat value is `undefined`, then
389
- // the toggle should be unchecked as the default state. Otherwise, the toggle
390
- // should reflect the current repeat value.
391
- const repeatCheckedValue = !(repeatValue === 'no-repeat' || currentValueForToggle === 'cover' && repeatValue === undefined);
392
- const hasValue = hasBackgroundSizeValue(style);
393
- const resetAllFilter = (0, _element.useCallback)(previousValue => {
394
- return {
395
- ...previousValue,
396
- style: {
397
- ...previousValue.style,
398
- background: {
399
- ...previousValue.style?.background,
400
- backgroundRepeat: undefined,
401
- backgroundSize: undefined
402
- }
403
- }
404
- };
405
- }, []);
406
- const updateBackgroundSize = next => {
407
- // When switching to 'contain' toggle the repeat off.
408
- let nextRepeat = repeatValue;
409
- if (next === 'contain') {
410
- nextRepeat = 'no-repeat';
411
- }
412
- if ((currentValueForToggle === 'cover' || currentValueForToggle === 'contain') && next === 'auto') {
413
- nextRepeat = undefined;
414
- }
415
- setAttributes({
416
- style: (0, _utils.cleanEmptyObject)({
417
- ...style,
418
- background: {
419
- ...style?.background,
420
- backgroundRepeat: nextRepeat,
421
- backgroundSize: next
422
- }
423
- })
424
- });
425
- };
426
- const updateBackgroundPosition = next => {
130
+ if (!(0, _backgroundPanel.useHasBackgroundPanel)(settings) || !hasBackgroundSupport(name, 'backgroundImage')) {
131
+ return null;
132
+ }
133
+ const defaultControls = (0, _blocks.getBlockSupport)(name, [BACKGROUND_SUPPORT_KEY, '__experimentalDefaultControls']);
134
+ const onChange = newStyle => {
427
135
  setAttributes({
428
- style: (0, _utils.cleanEmptyObject)({
429
- ...style,
430
- background: {
431
- ...style?.background,
432
- backgroundPosition: coordsToBackgroundPosition(next)
433
- }
434
- })
136
+ style: (0, _utils.cleanEmptyObject)(newStyle)
435
137
  });
436
138
  };
437
- const toggleIsRepeated = () => {
438
- setAttributes({
439
- style: (0, _utils.cleanEmptyObject)({
440
- ...style,
441
- background: {
442
- ...style?.background,
443
- backgroundRepeat: repeatCheckedValue === true ? 'no-repeat' : undefined
444
- }
445
- })
446
- });
139
+ const updatedSettings = {
140
+ ...settings,
141
+ background: {
142
+ ...settings.background,
143
+ backgroundSize: settings?.background?.backgroundSize && hasBackgroundSupport(name, 'backgroundSize')
144
+ }
447
145
  };
448
- return (0, _react.createElement)(_components.__experimentalVStack, {
449
- as: _components.__experimentalToolsPanelItem,
450
- spacing: 2,
451
- className: "single-column",
452
- hasValue: () => hasValue,
453
- label: (0, _i18n.__)('Size'),
454
- onDeselect: () => resetBackgroundSize(style, setAttributes),
455
- isShownByDefault: isShownByDefault,
456
- resetAllFilter: resetAllFilter,
457
- panelId: clientId
458
- }, (0, _react.createElement)(_components.FocalPointPicker, {
459
- __next40pxDefaultSize: true,
460
- label: (0, _i18n.__)('Position'),
461
- url: style?.background?.backgroundImage?.url,
462
- value: backgroundPositionToCoords(style?.background?.backgroundPosition),
463
- onChange: updateBackgroundPosition
464
- }), (0, _react.createElement)(_components.__experimentalToggleGroupControl, {
465
- size: '__unstable-large',
466
- label: (0, _i18n.__)('Size'),
467
- value: currentValueForToggle,
468
- onChange: updateBackgroundSize,
469
- isBlock: true,
470
- help: backgroundSizeHelpText(sizeValue)
471
- }, (0, _react.createElement)(_components.__experimentalToggleGroupControlOption, {
472
- key: 'cover',
473
- value: 'cover',
474
- label: (0, _i18n.__)('Cover')
475
- }), (0, _react.createElement)(_components.__experimentalToggleGroupControlOption, {
476
- key: 'contain',
477
- value: 'contain',
478
- label: (0, _i18n.__)('Contain')
479
- }), (0, _react.createElement)(_components.__experimentalToggleGroupControlOption, {
480
- key: 'fixed',
481
- value: 'auto',
482
- label: (0, _i18n.__)('Fixed')
483
- })), sizeValue !== undefined && sizeValue !== 'cover' && sizeValue !== 'contain' ? (0, _react.createElement)(_components.__experimentalUnitControl, {
484
- size: '__unstable-large',
485
- onChange: updateBackgroundSize,
486
- value: sizeValue
487
- }) : null, currentValueForToggle !== 'cover' && (0, _react.createElement)(_components.ToggleControl, {
488
- label: (0, _i18n.__)('Repeat'),
489
- checked: repeatCheckedValue,
490
- onChange: toggleIsRepeated
491
- }));
492
- }
493
- function BackgroundImagePanel(props) {
494
- const [backgroundImage, backgroundSize] = (0, _useSettings.useSettings)('background.backgroundImage', 'background.backgroundSize');
495
- if (!backgroundImage || !hasBackgroundSupport(props.name, 'backgroundImage')) {
496
- return null;
497
- }
498
- const showBackgroundSize = !!(backgroundSize && hasBackgroundSupport(props.name, 'backgroundSize'));
499
- const defaultControls = (0, _blocks.getBlockSupport)(props.name, [BACKGROUND_SUPPORT_KEY, '__experimentalDefaultControls']);
500
- return (0, _react.createElement)(_inspectorControls.default, {
501
- group: "background"
502
- }, (0, _react.createElement)(BackgroundImagePanelItem, {
503
- isShownByDefault: defaultControls?.backgroundImage,
504
- ...props
505
- }), showBackgroundSize && (0, _react.createElement)(BackgroundSizePanelItem, {
506
- isShownByDefault: defaultControls?.backgroundSize,
507
- ...props
508
- }));
146
+ return (0, _react.createElement)(_backgroundPanel.default, {
147
+ as: BackgroundInspectorControl,
148
+ panelId: clientId,
149
+ defaultControls: defaultControls,
150
+ defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES,
151
+ settings: updatedSettings,
152
+ onChange: onChange,
153
+ value: style
154
+ });
509
155
  }
510
156
  var _default = exports.default = {
511
157
  useBlockProps,