@wordpress/block-editor 12.0.0 → 12.1.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 (311) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +1 -1
  3. package/build/components/block-controls/slot.js +12 -4
  4. package/build/components/block-controls/slot.js.map +1 -1
  5. package/build/components/block-controls/slot.native.js +10 -1
  6. package/build/components/block-controls/slot.native.js.map +1 -1
  7. package/build/components/block-draggable/index.js +11 -7
  8. package/build/components/block-draggable/index.js.map +1 -1
  9. package/build/components/block-draggable/use-scroll-when-dragging.js +2 -2
  10. package/build/components/block-draggable/use-scroll-when-dragging.js.map +1 -1
  11. package/build/components/block-list/block-invalid-warning.native.js +15 -7
  12. package/build/components/block-list/block-invalid-warning.native.js.map +1 -1
  13. package/build/components/block-list/block-list-item-cell.native.js +15 -2
  14. package/build/components/block-list/block-list-item-cell.native.js.map +1 -1
  15. package/build/components/block-list/block-list-item.native.js +158 -195
  16. package/build/components/block-list/block-list-item.native.js.map +1 -1
  17. package/build/components/block-list/block-outline.native.js +57 -0
  18. package/build/components/block-list/block-outline.native.js.map +1 -0
  19. package/build/components/block-list/block.native.js +343 -299
  20. package/build/components/block-list/block.native.js.map +1 -1
  21. package/build/components/block-list/index.native.js +202 -298
  22. package/build/components/block-list/index.native.js.map +1 -1
  23. package/build/components/block-list/insertion-point.native.js +4 -2
  24. package/build/components/block-list/insertion-point.native.js.map +1 -1
  25. package/build/components/block-mobile-toolbar/block-actions-menu.native.js +1 -1
  26. package/build/components/block-mobile-toolbar/block-actions-menu.native.js.map +1 -1
  27. package/build/components/block-settings-menu/block-settings-dropdown.js +8 -10
  28. package/build/components/block-settings-menu/block-settings-dropdown.js.map +1 -1
  29. package/build/components/block-settings-menu-controls/index.js +15 -4
  30. package/build/components/block-settings-menu-controls/index.js.map +1 -1
  31. package/build/components/block-tools/block-contextual-toolbar.js +17 -62
  32. package/build/components/block-tools/block-contextual-toolbar.js.map +1 -1
  33. package/build/components/block-tools/selected-block-popover.js +3 -8
  34. package/build/components/block-tools/selected-block-popover.js.map +1 -1
  35. package/build/components/colors-gradients/use-multiple-origin-colors-and-gradients.js +3 -2
  36. package/build/components/colors-gradients/use-multiple-origin-colors-and-gradients.js.map +1 -1
  37. package/build/components/global-styles/advanced-panel.js +86 -0
  38. package/build/components/global-styles/advanced-panel.js.map +1 -0
  39. package/build/components/global-styles/color-panel.js +4 -1
  40. package/build/components/global-styles/color-panel.js.map +1 -1
  41. package/build/components/global-styles/dimensions-panel.js +6 -6
  42. package/build/components/global-styles/dimensions-panel.js.map +1 -1
  43. package/build/components/global-styles/hooks.js +1 -2
  44. package/build/components/global-styles/hooks.js.map +1 -1
  45. package/build/components/global-styles/index.js +24 -0
  46. package/build/components/global-styles/index.js.map +1 -1
  47. package/build/components/global-styles/typography-panel.js +1 -1
  48. package/build/components/global-styles/typography-panel.js.map +1 -1
  49. package/build/components/global-styles/use-global-styles-output.js +27 -4
  50. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  51. package/build/components/global-styles/utils.js +30 -0
  52. package/build/components/global-styles/utils.js.map +1 -1
  53. package/build/components/image-editor/use-save-image.js +24 -8
  54. package/build/components/image-editor/use-save-image.js.map +1 -1
  55. package/build/components/inserter-draggable-blocks/index.js +5 -0
  56. package/build/components/inserter-draggable-blocks/index.js.map +1 -1
  57. package/build/components/inspector-controls/fill.js +1 -1
  58. package/build/components/inspector-controls/fill.js.map +1 -1
  59. package/build/components/inspector-controls/fill.native.js +1 -1
  60. package/build/components/inspector-controls/fill.native.js.map +1 -1
  61. package/build/components/inspector-controls/slot.js +3 -6
  62. package/build/components/inspector-controls/slot.js.map +1 -1
  63. package/build/components/inspector-controls/slot.native.js +1 -1
  64. package/build/components/inspector-controls/slot.native.js.map +1 -1
  65. package/build/components/line-height-control/index.js +7 -2
  66. package/build/components/line-height-control/index.js.map +1 -1
  67. package/build/components/link-control/use-internal-input-value.js +9 -8
  68. package/build/components/link-control/use-internal-input-value.js.map +1 -1
  69. package/build/components/list-view/block-contents.js +7 -2
  70. package/build/components/list-view/block-contents.js.map +1 -1
  71. package/build/components/list-view/block-select-button.js +2 -1
  72. package/build/components/list-view/block-select-button.js.map +1 -1
  73. package/build/components/list-view/block.js +4 -4
  74. package/build/components/list-view/block.js.map +1 -1
  75. package/build/components/list-view/index.js +32 -18
  76. package/build/components/list-view/index.js.map +1 -1
  77. package/build/components/list-view/use-list-view-drop-zone.js +163 -11
  78. package/build/components/list-view/use-list-view-drop-zone.js.map +1 -1
  79. package/build/components/media-placeholder/index.js +68 -7
  80. package/build/components/media-placeholder/index.js.map +1 -1
  81. package/build/components/multi-selection-inspector/index.js +2 -2
  82. package/build/components/multi-selection-inspector/index.js.map +1 -1
  83. package/build/components/off-canvas-editor/leaf-more-menu.js +3 -1
  84. package/build/components/off-canvas-editor/leaf-more-menu.js.map +1 -1
  85. package/build/components/preview-options/index.js +6 -1
  86. package/build/components/preview-options/index.js.map +1 -1
  87. package/build/components/spacing-sizes-control/spacing-input-control.js +1 -1
  88. package/build/components/spacing-sizes-control/spacing-input-control.js.map +1 -1
  89. package/build/components/url-input/index.js +1 -2
  90. package/build/components/url-input/index.js.map +1 -1
  91. package/build/hooks/align.js +1 -1
  92. package/build/hooks/align.js.map +1 -1
  93. package/build/hooks/border.js +1 -1
  94. package/build/hooks/border.js.map +1 -1
  95. package/build/hooks/color.js +1 -1
  96. package/build/hooks/color.js.map +1 -1
  97. package/build/hooks/content-lock-ui.js +8 -12
  98. package/build/hooks/content-lock-ui.js.map +1 -1
  99. package/build/hooks/duotone.js +1 -1
  100. package/build/hooks/duotone.js.map +1 -1
  101. package/build/hooks/index.native.js +8 -0
  102. package/build/hooks/index.native.js.map +1 -1
  103. package/build/hooks/layout.js +2 -2
  104. package/build/hooks/layout.js.map +1 -1
  105. package/build/hooks/position.js +1 -1
  106. package/build/hooks/position.js.map +1 -1
  107. package/build/hooks/style.js +1 -1
  108. package/build/hooks/style.js.map +1 -1
  109. package/build/hooks/use-editor-wrapper-styles.native.js +255 -0
  110. package/build/hooks/use-editor-wrapper-styles.native.js.map +1 -0
  111. package/build/hooks/use-typography-props.js +14 -10
  112. package/build/hooks/use-typography-props.js.map +1 -1
  113. package/build/index.native.js +31 -0
  114. package/build/index.native.js.map +1 -0
  115. package/build/utils/use-should-contextual-toolbar-show.js +16 -12
  116. package/build/utils/use-should-contextual-toolbar-show.js.map +1 -1
  117. package/build-module/components/block-controls/slot.js +11 -4
  118. package/build-module/components/block-controls/slot.js.map +1 -1
  119. package/build-module/components/block-controls/slot.native.js +9 -1
  120. package/build-module/components/block-controls/slot.native.js.map +1 -1
  121. package/build-module/components/block-draggable/index.js +10 -6
  122. package/build-module/components/block-draggable/index.js.map +1 -1
  123. package/build-module/components/block-draggable/use-scroll-when-dragging.js +2 -2
  124. package/build-module/components/block-draggable/use-scroll-when-dragging.js.map +1 -1
  125. package/build-module/components/block-list/block-invalid-warning.native.js +16 -8
  126. package/build-module/components/block-list/block-invalid-warning.native.js.map +1 -1
  127. package/build-module/components/block-list/block-list-item-cell.native.js +13 -2
  128. package/build-module/components/block-list/block-list-item-cell.native.js.map +1 -1
  129. package/build-module/components/block-list/block-list-item.native.js +160 -190
  130. package/build-module/components/block-list/block-list-item.native.js.map +1 -1
  131. package/build-module/components/block-list/block-outline.native.js +44 -0
  132. package/build-module/components/block-list/block-outline.native.js.map +1 -0
  133. package/build-module/components/block-list/block.native.js +341 -298
  134. package/build-module/components/block-list/block.native.js.map +1 -1
  135. package/build-module/components/block-list/index.native.js +203 -293
  136. package/build-module/components/block-list/index.native.js.map +1 -1
  137. package/build-module/components/block-list/insertion-point.native.js +4 -2
  138. package/build-module/components/block-list/insertion-point.native.js.map +1 -1
  139. package/build-module/components/block-mobile-toolbar/block-actions-menu.native.js +1 -1
  140. package/build-module/components/block-mobile-toolbar/block-actions-menu.native.js.map +1 -1
  141. package/build-module/components/block-settings-menu/block-settings-dropdown.js +8 -9
  142. package/build-module/components/block-settings-menu/block-settings-dropdown.js.map +1 -1
  143. package/build-module/components/block-settings-menu-controls/index.js +13 -5
  144. package/build-module/components/block-settings-menu-controls/index.js.map +1 -1
  145. package/build-module/components/block-tools/block-contextual-toolbar.js +18 -62
  146. package/build-module/components/block-tools/block-contextual-toolbar.js.map +1 -1
  147. package/build-module/components/block-tools/selected-block-popover.js +3 -7
  148. package/build-module/components/block-tools/selected-block-popover.js.map +1 -1
  149. package/build-module/components/colors-gradients/use-multiple-origin-colors-and-gradients.js +3 -2
  150. package/build-module/components/colors-gradients/use-multiple-origin-colors-and-gradients.js.map +1 -1
  151. package/build-module/components/global-styles/advanced-panel.js +74 -0
  152. package/build-module/components/global-styles/advanced-panel.js.map +1 -0
  153. package/build-module/components/global-styles/color-panel.js +5 -2
  154. package/build-module/components/global-styles/color-panel.js.map +1 -1
  155. package/build-module/components/global-styles/dimensions-panel.js +6 -6
  156. package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
  157. package/build-module/components/global-styles/hooks.js +1 -2
  158. package/build-module/components/global-styles/hooks.js.map +1 -1
  159. package/build-module/components/global-styles/index.js +3 -1
  160. package/build-module/components/global-styles/index.js.map +1 -1
  161. package/build-module/components/global-styles/typography-panel.js +1 -1
  162. package/build-module/components/global-styles/typography-panel.js.map +1 -1
  163. package/build-module/components/global-styles/use-global-styles-output.js +25 -4
  164. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  165. package/build-module/components/global-styles/utils.js +25 -0
  166. package/build-module/components/global-styles/utils.js.map +1 -1
  167. package/build-module/components/image-editor/use-save-image.js +24 -8
  168. package/build-module/components/image-editor/use-save-image.js.map +1 -1
  169. package/build-module/components/inserter-draggable-blocks/index.js +4 -0
  170. package/build-module/components/inserter-draggable-blocks/index.js.map +1 -1
  171. package/build-module/components/inspector-controls/fill.js +1 -1
  172. package/build-module/components/inspector-controls/fill.js.map +1 -1
  173. package/build-module/components/inspector-controls/fill.native.js +1 -1
  174. package/build-module/components/inspector-controls/fill.native.js.map +1 -1
  175. package/build-module/components/inspector-controls/slot.js +4 -7
  176. package/build-module/components/inspector-controls/slot.js.map +1 -1
  177. package/build-module/components/inspector-controls/slot.native.js +1 -1
  178. package/build-module/components/inspector-controls/slot.native.js.map +1 -1
  179. package/build-module/components/line-height-control/index.js +7 -2
  180. package/build-module/components/line-height-control/index.js.map +1 -1
  181. package/build-module/components/link-control/use-internal-input-value.js +9 -8
  182. package/build-module/components/link-control/use-internal-input-value.js.map +1 -1
  183. package/build-module/components/list-view/block-contents.js +7 -3
  184. package/build-module/components/list-view/block-contents.js.map +1 -1
  185. package/build-module/components/list-view/block-select-button.js +2 -1
  186. package/build-module/components/list-view/block-select-button.js.map +1 -1
  187. package/build-module/components/list-view/block.js +4 -4
  188. package/build-module/components/list-view/block.js.map +1 -1
  189. package/build-module/components/list-view/index.js +32 -18
  190. package/build-module/components/list-view/index.js.map +1 -1
  191. package/build-module/components/list-view/use-list-view-drop-zone.js +160 -11
  192. package/build-module/components/list-view/use-list-view-drop-zone.js.map +1 -1
  193. package/build-module/components/media-placeholder/index.js +66 -7
  194. package/build-module/components/media-placeholder/index.js.map +1 -1
  195. package/build-module/components/multi-selection-inspector/index.js +2 -2
  196. package/build-module/components/multi-selection-inspector/index.js.map +1 -1
  197. package/build-module/components/off-canvas-editor/leaf-more-menu.js +3 -1
  198. package/build-module/components/off-canvas-editor/leaf-more-menu.js.map +1 -1
  199. package/build-module/components/preview-options/index.js +7 -2
  200. package/build-module/components/preview-options/index.js.map +1 -1
  201. package/build-module/components/spacing-sizes-control/spacing-input-control.js +1 -1
  202. package/build-module/components/spacing-sizes-control/spacing-input-control.js.map +1 -1
  203. package/build-module/components/url-input/index.js +1 -2
  204. package/build-module/components/url-input/index.js.map +1 -1
  205. package/build-module/hooks/align.js +1 -1
  206. package/build-module/hooks/align.js.map +1 -1
  207. package/build-module/hooks/border.js +1 -1
  208. package/build-module/hooks/border.js.map +1 -1
  209. package/build-module/hooks/color.js +1 -1
  210. package/build-module/hooks/color.js.map +1 -1
  211. package/build-module/hooks/content-lock-ui.js +8 -11
  212. package/build-module/hooks/content-lock-ui.js.map +1 -1
  213. package/build-module/hooks/duotone.js +1 -1
  214. package/build-module/hooks/duotone.js.map +1 -1
  215. package/build-module/hooks/index.native.js +1 -0
  216. package/build-module/hooks/index.native.js.map +1 -1
  217. package/build-module/hooks/layout.js +2 -2
  218. package/build-module/hooks/layout.js.map +1 -1
  219. package/build-module/hooks/position.js +1 -1
  220. package/build-module/hooks/position.js.map +1 -1
  221. package/build-module/hooks/style.js +1 -1
  222. package/build-module/hooks/style.js.map +1 -1
  223. package/build-module/hooks/use-editor-wrapper-styles.native.js +242 -0
  224. package/build-module/hooks/use-editor-wrapper-styles.native.js.map +1 -0
  225. package/build-module/hooks/use-typography-props.js +14 -10
  226. package/build-module/hooks/use-typography-props.js.map +1 -1
  227. package/build-module/index.native.js +6 -0
  228. package/build-module/index.native.js.map +1 -0
  229. package/build-module/utils/use-should-contextual-toolbar-show.js +16 -12
  230. package/build-module/utils/use-should-contextual-toolbar-show.js.map +1 -1
  231. package/build-style/content-rtl.css +0 -1
  232. package/build-style/content.css +0 -1
  233. package/build-style/style-rtl.css +37 -13
  234. package/build-style/style.css +37 -13
  235. package/package.json +31 -31
  236. package/src/components/block-breadcrumb/style.scss +2 -1
  237. package/src/components/block-controls/slot.js +8 -4
  238. package/src/components/block-controls/slot.native.js +6 -1
  239. package/src/components/block-draggable/index.js +10 -6
  240. package/src/components/block-draggable/use-scroll-when-dragging.js +8 -2
  241. package/src/components/block-list/block-invalid-warning.native.js +17 -9
  242. package/src/components/block-list/block-list-item-cell.native.js +10 -1
  243. package/src/components/block-list/block-list-item.native.js +180 -208
  244. package/src/components/block-list/block-outline.native.js +58 -0
  245. package/src/components/block-list/block.native.js +564 -523
  246. package/src/components/block-list/content.scss +0 -1
  247. package/src/components/block-list/index.native.js +229 -298
  248. package/src/components/block-list/insertion-point.native.js +2 -2
  249. package/src/components/block-list/test/block-invalid-warning.native.js +48 -0
  250. package/src/components/block-list/test/index.native.js +205 -0
  251. package/src/components/block-mobile-toolbar/block-actions-menu.native.js +3 -1
  252. package/src/components/block-pattern-setup/style.scss +1 -4
  253. package/src/components/block-patterns-list/style.scss +1 -4
  254. package/src/components/block-settings-menu/block-settings-dropdown.js +10 -17
  255. package/src/components/block-settings-menu-controls/index.js +24 -4
  256. package/src/components/block-styles/style.scss +4 -4
  257. package/src/components/block-tools/block-contextual-toolbar.js +28 -62
  258. package/src/components/block-tools/selected-block-popover.js +3 -5
  259. package/src/components/block-tools/style.scss +8 -0
  260. package/src/components/colors-gradients/use-multiple-origin-colors-and-gradients.js +16 -2
  261. package/src/components/global-styles/advanced-panel.js +82 -0
  262. package/src/components/global-styles/color-panel.js +6 -1
  263. package/src/components/global-styles/dimensions-panel.js +6 -6
  264. package/src/components/global-styles/hooks.js +1 -5
  265. package/src/components/global-styles/index.js +6 -1
  266. package/src/components/global-styles/style.scss +14 -0
  267. package/src/components/global-styles/test/utils.js +57 -1
  268. package/src/components/global-styles/typography-panel.js +1 -1
  269. package/src/components/global-styles/use-global-styles-output.js +21 -3
  270. package/src/components/global-styles/utils.js +27 -0
  271. package/src/components/image-editor/use-save-image.js +20 -9
  272. package/src/components/inserter-draggable-blocks/index.js +4 -0
  273. package/src/components/inspector-controls/fill.js +1 -1
  274. package/src/components/inspector-controls/fill.native.js +1 -1
  275. package/src/components/inspector-controls/slot.js +4 -9
  276. package/src/components/inspector-controls/slot.native.js +1 -1
  277. package/src/components/line-height-control/index.js +7 -2
  278. package/src/components/line-height-control/stories/index.js +1 -1
  279. package/src/components/link-control/test/index.js +42 -0
  280. package/src/components/link-control/use-internal-input-value.js +8 -7
  281. package/src/components/list-view/block-contents.js +26 -20
  282. package/src/components/list-view/block-select-button.js +5 -1
  283. package/src/components/list-view/block.js +5 -2
  284. package/src/components/list-view/index.js +26 -14
  285. package/src/components/list-view/style.scss +5 -2
  286. package/src/components/list-view/test/use-list-view-drop-zone.js +88 -12
  287. package/src/components/list-view/use-list-view-drop-zone.js +194 -11
  288. package/src/components/media-placeholder/index.js +74 -1
  289. package/src/components/multi-selection-inspector/index.js +2 -2
  290. package/src/components/off-canvas-editor/leaf-more-menu.js +2 -1
  291. package/src/components/preview-options/index.js +9 -2
  292. package/src/components/spacing-sizes-control/spacing-input-control.js +1 -0
  293. package/src/components/url-input/index.js +1 -2
  294. package/src/hooks/align.js +2 -1
  295. package/src/hooks/border.js +2 -1
  296. package/src/hooks/color.js +2 -1
  297. package/src/hooks/content-lock-ui.js +3 -15
  298. package/src/hooks/duotone.js +1 -0
  299. package/src/hooks/index.native.js +1 -0
  300. package/src/hooks/layout.js +4 -2
  301. package/src/hooks/position.js +2 -1
  302. package/src/hooks/style.js +2 -1
  303. package/src/hooks/test/use-editor-wrapper-styles.native.js +282 -0
  304. package/src/hooks/test/use-typography-props.js +47 -2
  305. package/src/hooks/use-editor-wrapper-styles.native.js +250 -0
  306. package/src/hooks/use-editor-wrapper-styles.native.scss +11 -0
  307. package/src/hooks/use-typography-props.js +10 -11
  308. package/src/index.native.js +6 -0
  309. package/src/utils/use-should-contextual-toolbar-show.js +19 -9
  310. package/tsconfig.json +1 -0
  311. package/tsconfig.tsbuildinfo +1 -1
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { View, Text, TouchableWithoutFeedback, Dimensions } from 'react-native';
4
+ import { Pressable, useWindowDimensions, View } from 'react-native';
5
5
 
6
6
  /**
7
7
  * WordPress dependencies
8
8
  */
9
- import { Component, createRef, useMemo } from '@wordpress/element';
9
+ import { useCallback, useMemo, useRef, useState } from '@wordpress/element';
10
10
  import {
11
11
  GlobalStylesContext,
12
12
  getMergedGlobalStyles,
@@ -14,51 +14,265 @@ import {
14
14
  alignmentHelpers,
15
15
  useGlobalStyles,
16
16
  } from '@wordpress/components';
17
- import { withDispatch, withSelect } from '@wordpress/data';
18
- import { compose, withPreferredColorScheme } from '@wordpress/compose';
19
17
  import {
20
- getBlockType,
21
18
  __experimentalGetAccessibleBlockLabel as getAccessibleBlockLabel,
22
- switchToBlockType,
19
+ getBlockType,
23
20
  getDefaultBlockName,
24
21
  isUnmodifiedBlock,
22
+ isUnmodifiedDefaultBlock,
23
+ switchToBlockType,
25
24
  } from '@wordpress/blocks';
26
- import { useSetting } from '@wordpress/block-editor';
25
+ import {
26
+ useDispatch,
27
+ useSelect,
28
+ withDispatch,
29
+ withSelect,
30
+ } from '@wordpress/data';
31
+ import { compose, ifCondition, pure } from '@wordpress/compose';
27
32
 
28
33
  /**
29
34
  * Internal dependencies
30
35
  */
31
- import styles from './block.scss';
32
36
  import BlockEdit from '../block-edit';
37
+ import BlockDraggable from '../block-draggable';
33
38
  import BlockInvalidWarning from './block-invalid-warning';
34
39
  import BlockMobileToolbar from '../block-mobile-toolbar';
40
+ import BlockOutline from './block-outline';
41
+ import styles from './block.scss';
35
42
  import { store as blockEditorStore } from '../../store';
36
- import BlockDraggable from '../block-draggable';
37
43
  import { useLayout } from './layout';
44
+ import useSetting from '../use-setting';
38
45
 
39
46
  const emptyArray = [];
40
- function BlockForType( {
47
+
48
+ // Helper function to memoize the wrapperProps since getEditWrapperProps always returns a new reference.
49
+ const wrapperPropsCache = new WeakMap();
50
+ const emptyObj = {};
51
+ function getWrapperProps( value, getWrapperPropsFunction ) {
52
+ if ( ! getWrapperPropsFunction ) {
53
+ return emptyObj;
54
+ }
55
+ const cachedValue = wrapperPropsCache.get( value );
56
+ if ( ! cachedValue ) {
57
+ const wrapperProps = getWrapperPropsFunction( value );
58
+ wrapperPropsCache.set( value, wrapperProps );
59
+ return wrapperProps;
60
+ }
61
+ return cachedValue;
62
+ }
63
+
64
+ function BlockWrapper( {
65
+ accessibilityLabel,
66
+ align,
67
+ blockWidth,
68
+ children,
69
+ clientId,
70
+ draggingClientId,
71
+ draggingEnabled,
72
+ isDescendentBlockSelected,
73
+ isParentSelected,
74
+ isSelected,
75
+ isStackedHorizontally,
76
+ isTouchable,
77
+ marginHorizontal,
78
+ marginVertical,
79
+ onDeleteBlock,
80
+ onFocus,
81
+ } ) {
82
+ const { width: screenWidth } = useWindowDimensions();
83
+ const anchorNodeRef = useRef();
84
+ const { isFullWidth } = alignmentHelpers;
85
+ const isScreenWidthEqual = blockWidth === screenWidth;
86
+ const isFullWidthToolbar = isFullWidth( align ) || isScreenWidthEqual;
87
+ const blockWrapperStyles = { flex: 1 };
88
+ const blockWrapperStyle = [
89
+ blockWrapperStyles,
90
+ {
91
+ marginVertical,
92
+ marginHorizontal,
93
+ },
94
+ ];
95
+ const accessible = ! ( isSelected || isDescendentBlockSelected );
96
+
97
+ return (
98
+ <Pressable
99
+ accessibilityLabel={ accessibilityLabel }
100
+ accessibilityRole="button"
101
+ accessible={ accessible }
102
+ disabled={ ! isTouchable }
103
+ onPress={ onFocus }
104
+ style={ blockWrapperStyle }
105
+ >
106
+ <BlockOutline
107
+ isSelected={ isSelected }
108
+ isParentSelected={ isParentSelected }
109
+ screenWidth={ screenWidth }
110
+ />
111
+ <BlockDraggable
112
+ clientId={ clientId }
113
+ draggingClientId={ draggingClientId }
114
+ enabled={ draggingEnabled }
115
+ testID="draggable-trigger-content"
116
+ >
117
+ { children }
118
+ </BlockDraggable>
119
+ <View style={ styles.neutralToolbar } ref={ anchorNodeRef }>
120
+ { isSelected && (
121
+ <BlockMobileToolbar
122
+ anchorNodeRef={ anchorNodeRef.current }
123
+ blockWidth={ blockWidth }
124
+ clientId={ clientId }
125
+ draggingClientId={ draggingClientId }
126
+ isFullWidth={ isFullWidthToolbar }
127
+ isStackedHorizontally={ isStackedHorizontally }
128
+ onDelete={ onDeleteBlock }
129
+ />
130
+ ) }
131
+ </View>
132
+ </Pressable>
133
+ );
134
+ }
135
+
136
+ function BlockListBlock( {
41
137
  attributes,
138
+ blockWidth: blockWrapperWidth,
139
+ canRemove,
42
140
  clientId,
43
141
  contentStyle,
44
- getBlockWidth,
45
- insertBlocksAfter,
142
+ isLocked,
46
143
  isSelected,
47
- onMerge,
144
+ isSelectionEnabled,
145
+ isStackedHorizontally,
146
+ isValid,
147
+ marginHorizontal,
148
+ marginVertical,
48
149
  name,
49
- onBlockFocus,
50
- onChange,
51
150
  onDeleteBlock,
151
+ onInsertBlocksAfter,
152
+ onMerge,
52
153
  onReplace,
53
- parentWidth,
54
154
  parentBlockAlignment,
55
- wrapperProps,
56
- blockWidth,
57
- baseGlobalStyles,
155
+ parentWidth,
156
+ rootClientId,
157
+ setAttributes,
158
+ toggleSelection,
58
159
  } ) {
160
+ const {
161
+ baseGlobalStyles,
162
+ blockType,
163
+ draggingClientId,
164
+ draggingEnabled,
165
+ firstToSelectId,
166
+ isDescendantOfParentSelected,
167
+ isDescendentBlockSelected,
168
+ isParentSelected,
169
+ order,
170
+ } = useSelect(
171
+ ( select ) => {
172
+ const {
173
+ getBlockCount,
174
+ getBlockHierarchyRootClientId,
175
+ getBlockIndex,
176
+ getBlockParents,
177
+ getLowestCommonAncestorWithSelectedBlock,
178
+ getSelectedBlockClientId,
179
+ getSettings,
180
+ hasSelectedInnerBlock,
181
+ } = select( blockEditorStore );
182
+ const currentBlockType = getBlockType( name || 'core/missing' );
183
+ const blockOrder = getBlockIndex( clientId );
184
+ const descendentBlockSelected = hasSelectedInnerBlock(
185
+ clientId,
186
+ true
187
+ );
188
+ const selectedBlockClientId = getSelectedBlockClientId();
189
+
190
+ const parents = getBlockParents( clientId, true );
191
+ const parentSelected =
192
+ // Set false as a default value to prevent re-render when it's changed from null to false.
193
+ ( selectedBlockClientId || false ) &&
194
+ selectedBlockClientId === rootClientId;
195
+
196
+ const selectedParents = clientId ? parents : [];
197
+ const descendantOfParentSelected =
198
+ selectedParents.includes( rootClientId );
199
+ const hasInnerBlocks = getBlockCount( clientId ) > 0;
200
+
201
+ const commonAncestor =
202
+ getLowestCommonAncestorWithSelectedBlock( clientId );
203
+ const commonAncestorIndex = parents.indexOf( commonAncestor ) - 1;
204
+ const firstBlockToSelectId = commonAncestor
205
+ ? parents[ commonAncestorIndex ]
206
+ : parents[ parents.length - 1 ];
207
+
208
+ // For blocks with inner blocks, we only enable the dragging in the nested
209
+ // blocks if any of them are selected. This way we prevent the long-press
210
+ // gesture from being disabled for elements within the block UI.
211
+ const isDraggingEnabled =
212
+ ! hasInnerBlocks || isSelected || ! descendentBlockSelected;
213
+ // Dragging nested blocks is not supported yet. For this reason, the block to be dragged
214
+ // will be the top in the hierarchy.
215
+ const currentDraggingClientId =
216
+ getBlockHierarchyRootClientId( clientId );
217
+
218
+ const globalStylesBaseStyles =
219
+ getSettings()?.__experimentalGlobalStylesBaseStyles;
220
+
221
+ return {
222
+ baseGlobalStyles: globalStylesBaseStyles,
223
+ blockType: currentBlockType,
224
+ draggingClientId: currentDraggingClientId,
225
+ draggingEnabled: isDraggingEnabled,
226
+ firstToSelectId: firstBlockToSelectId,
227
+ isDescendantOfParentSelected: descendantOfParentSelected,
228
+ isDescendentBlockSelected: descendentBlockSelected,
229
+ isParentSelected: parentSelected,
230
+ order: blockOrder,
231
+ };
232
+ },
233
+ [ clientId, isSelected, name, rootClientId ]
234
+ );
235
+ const { removeBlock, selectBlock } = useDispatch( blockEditorStore );
236
+ const initialBlockWidth = blockWrapperWidth - 2 * marginHorizontal;
237
+ const [ blockWidth, setBlockWidth ] = useState( initialBlockWidth );
238
+ const parentLayout = useLayout() || {};
59
239
  const defaultColors = useMobileGlobalStylesColors();
60
- const fontSizes = useSetting( 'typography.fontSizes' ) || emptyArray;
61
240
  const globalStyle = useGlobalStyles();
241
+ const fontSizes = useSetting( 'typography.fontSizes' ) || emptyArray;
242
+
243
+ const onRemove = useCallback(
244
+ () => removeBlock( clientId ),
245
+ [ clientId, removeBlock ]
246
+ );
247
+ const onFocus = useCallback( () => {
248
+ const blockId = firstToSelectId ?? clientId;
249
+ if ( ! isSelected ) {
250
+ selectBlock( blockId );
251
+ }
252
+ }, [ selectBlock, clientId, firstToSelectId, isSelected ] );
253
+
254
+ const onLayout = useCallback(
255
+ ( { nativeEvent } ) => {
256
+ const layoutWidth = Math.floor( nativeEvent.layout.width );
257
+
258
+ if ( ! blockWidth || ! layoutWidth ) {
259
+ return;
260
+ }
261
+
262
+ if ( blockWidth !== layoutWidth ) {
263
+ setBlockWidth( layoutWidth );
264
+ }
265
+ },
266
+ [ blockWidth, setBlockWidth ]
267
+ );
268
+
269
+ // Block level styles.
270
+ const wrapperProps = getWrapperProps(
271
+ attributes,
272
+ blockType.getEditWrapperProps
273
+ );
274
+
275
+ // Inherited styles merged with block level styles.
62
276
  const mergedStyle = useMemo( () => {
63
277
  return getMergedGlobalStyles(
64
278
  baseGlobalStyles,
@@ -69,12 +283,20 @@ function BlockForType( {
69
283
  name,
70
284
  fontSizes
71
285
  );
286
+ // eslint-disable-next-line react-hooks/exhaustive-deps
72
287
  }, [
73
- defaultColors,
74
- globalStyle,
75
- // I couldn't simply use attributes and wrapperProps.styles as a dependency because they are almost always a new reference.
76
- // Thanks to the JSON.stringify we check if the value is the same instead of reference.
288
+ // It is crucial to keep the dependencies array minimal to prevent unnecessary calls that could negatively impact performance.
289
+ // JSON.stringify is used for the following purposes:
290
+ // 1. To create a single, comparable value from the globalStyle, wrapperProps.style, and attributes objects. This allows useMemo to
291
+ // efficiently determine if a change has occurred in any of these objects.
292
+ // 2. To filter the attributes object, ensuring that only the relevant attributes (included in
293
+ // GlobalStylesContext.BLOCK_STYLE_ATTRIBUTES) are considered as dependencies. This reduces the likelihood of
294
+ // unnecessary useMemo calls when other, unrelated attributes change.
295
+ // eslint-disable-next-line react-hooks/exhaustive-deps
296
+ JSON.stringify( globalStyle ),
297
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
298
  JSON.stringify( wrapperProps.style ),
299
+ // eslint-disable-next-line react-hooks/exhaustive-deps
78
300
  JSON.stringify(
79
301
  Object.fromEntries(
80
302
  Object.entries( attributes ?? {} ).filter( ( [ key ] ) =>
@@ -84,502 +306,251 @@ function BlockForType( {
84
306
  ),
85
307
  ] );
86
308
 
87
- const parentLayout = useLayout();
88
-
89
- return (
90
- <GlobalStylesContext.Provider value={ mergedStyle }>
91
- <BlockEdit
92
- name={ name }
93
- isSelected={ isSelected }
94
- attributes={ attributes }
95
- setAttributes={ onChange }
96
- onFocus={ onBlockFocus }
97
- onReplace={ onReplace }
98
- insertBlocksAfter={ insertBlocksAfter }
99
- mergeBlocks={ onMerge }
100
- // Block level styles.
101
- wrapperProps={ wrapperProps }
102
- // Inherited styles merged with block level styles.
103
- style={ mergedStyle }
104
- clientId={ clientId }
105
- parentWidth={ parentWidth }
106
- contentStyle={ contentStyle }
107
- onDeleteBlock={ onDeleteBlock }
108
- blockWidth={ blockWidth }
109
- parentBlockAlignment={ parentBlockAlignment }
110
- __unstableParentLayout={ parentLayout }
111
- />
112
- <View onLayout={ getBlockWidth } />
113
- </GlobalStylesContext.Provider>
309
+ const { align } = attributes;
310
+ const isFocused = isSelected || isDescendentBlockSelected;
311
+ const isTouchable =
312
+ isSelected ||
313
+ isDescendantOfParentSelected ||
314
+ isParentSelected ||
315
+ ! rootClientId;
316
+
317
+ const accessibilityLabel = getAccessibleBlockLabel(
318
+ blockType,
319
+ attributes,
320
+ order + 1
114
321
  );
115
- }
116
-
117
- class BlockListBlock extends Component {
118
- constructor() {
119
- super( ...arguments );
120
-
121
- this.insertBlocksAfter = this.insertBlocksAfter.bind( this );
122
- this.onFocus = this.onFocus.bind( this );
123
- this.getBlockWidth = this.getBlockWidth.bind( this );
124
-
125
- this.state = {
126
- blockWidth: this.props.blockWidth - 2 * this.props.marginHorizontal,
127
- };
128
-
129
- this.anchorNodeRef = createRef();
130
- }
131
-
132
- onFocus() {
133
- const { firstToSelectId, isSelected, onSelect } = this.props;
134
- if ( ! isSelected ) {
135
- onSelect( firstToSelectId );
136
- }
137
- }
138
-
139
- insertBlocksAfter( blocks ) {
140
- this.props.onInsertBlocks( blocks, this.props.order + 1 );
141
-
142
- if ( blocks[ 0 ] ) {
143
- // Focus on the first block inserted.
144
- this.props.onSelect( blocks[ 0 ].clientId );
145
- }
146
- }
147
-
148
- getBlockWidth( { nativeEvent } ) {
149
- const { layout } = nativeEvent;
150
- const { blockWidth } = this.state;
151
- const layoutWidth = Math.floor( layout.width );
152
-
153
- if ( ! blockWidth || ! layoutWidth ) {
154
- return;
155
- }
156
-
157
- if ( blockWidth !== layoutWidth ) {
158
- this.setState( { blockWidth: layoutWidth } );
159
- }
160
- }
161
-
162
- getBlockForType() {
163
- const { blockWidth } = this.state;
164
- return (
165
- <BlockForType
166
- { ...this.props }
167
- onBlockFocus={ this.onFocus }
168
- insertBlocksAfter={ this.insertBlocksAfter }
169
- getBlockWidth={ this.getBlockWidth }
170
- blockWidth={ blockWidth }
171
- />
172
- );
173
- }
174
-
175
- renderBlockTitle() {
176
- return (
177
- <View style={ styles.blockTitle }>
178
- <Text>BlockType: { this.props.name }</Text>
179
- </View>
180
- );
181
- }
182
322
 
183
- render() {
184
- const {
185
- attributes,
186
- blockType,
187
- clientId,
188
- icon,
189
- isSelected,
190
- isValid,
191
- order,
192
- title,
193
- isDimmed,
194
- isTouchable,
195
- onDeleteBlock,
196
- isStackedHorizontally,
197
- isParentSelected,
198
- getStylesFromColorScheme,
199
- marginVertical,
200
- marginHorizontal,
201
- isInnerBlockSelected,
202
- name,
203
- draggingEnabled,
204
- draggingClientId,
205
- } = this.props;
206
-
207
- if ( ! attributes || ! blockType ) {
208
- return null;
209
- }
210
- const { blockWidth } = this.state;
211
- const { align } = attributes;
212
- const accessibilityLabel = getAccessibleBlockLabel(
213
- blockType,
214
- attributes,
215
- order + 1
216
- );
217
- const { isFullWidth, isContainerRelated } = alignmentHelpers;
218
- const accessible = ! ( isSelected || isInnerBlockSelected );
219
- const screenWidth = Math.floor( Dimensions.get( 'window' ).width );
220
- const isScreenWidthEqual = blockWidth === screenWidth;
221
- const isScreenWidthWider = blockWidth < screenWidth;
222
- const isFullWidthToolbar = isFullWidth( align ) || isScreenWidthEqual;
223
-
224
- return (
225
- <TouchableWithoutFeedback
226
- onPress={ this.onFocus }
227
- accessible={ accessible }
228
- accessibilityRole={ 'button' }
229
- >
230
- <View
231
- style={ { flex: 1 } }
232
- accessibilityLabel={ accessibilityLabel }
233
- >
234
- <View
235
- pointerEvents={ isTouchable ? 'auto' : 'box-only' }
236
- accessibilityLabel={ accessibilityLabel }
237
- style={ [
238
- { marginVertical, marginHorizontal, flex: 1 },
239
- isDimmed && styles.dimmed,
240
- ] }
241
- >
242
- { isSelected && (
243
- <View
244
- pointerEvents="box-none"
245
- style={ [
246
- styles.solidBorder,
247
- isFullWidth( align ) &&
248
- isScreenWidthWider &&
249
- styles.borderFullWidth,
250
- isFullWidth( align ) &&
251
- isContainerRelated( name ) &&
252
- isScreenWidthWider &&
253
- styles.containerBorderFullWidth,
254
- getStylesFromColorScheme(
255
- styles.solidBorderColor,
256
- styles.solidBorderColorDark
257
- ),
258
- ] }
259
- />
260
- ) }
261
- { isParentSelected && (
262
- <View
263
- style={ [
264
- styles.dashedBorder,
265
- getStylesFromColorScheme(
266
- styles.dashedBorderColor,
267
- styles.dashedBorderColorDark
268
- ),
269
- ] }
270
- />
271
- ) }
272
- <BlockDraggable
323
+ return (
324
+ <BlockWrapper
325
+ accessibilityLabel={ accessibilityLabel }
326
+ align={ align }
327
+ blockWidth={ blockWidth }
328
+ clientId={ clientId }
329
+ draggingClientId={ draggingClientId }
330
+ draggingEnabled={ draggingEnabled }
331
+ isFocused={ isFocused }
332
+ isDescendentBlockSelected={ isDescendentBlockSelected }
333
+ isParentSelected={ isParentSelected }
334
+ isSelected={ isSelected }
335
+ isStackedHorizontally={ isStackedHorizontally }
336
+ isTouchable={ isTouchable }
337
+ marginHorizontal={ marginHorizontal }
338
+ marginVertical={ marginVertical }
339
+ onDeleteBlock={ onDeleteBlock }
340
+ onFocus={ onFocus }
341
+ >
342
+ { () =>
343
+ ! isValid ? (
344
+ <BlockInvalidWarning clientId={ clientId } />
345
+ ) : (
346
+ <GlobalStylesContext.Provider value={ mergedStyle }>
347
+ <BlockEdit
348
+ attributes={ attributes }
349
+ blockWidth={ blockWidth }
273
350
  clientId={ clientId }
274
- draggingClientId={ draggingClientId }
275
- enabled={ draggingEnabled }
276
- testID="draggable-trigger-content"
277
- >
278
- { () =>
279
- isValid ? (
280
- this.getBlockForType()
281
- ) : (
282
- <BlockInvalidWarning
283
- blockTitle={ title }
284
- icon={ icon }
285
- clientId={ clientId }
286
- />
287
- )
351
+ contentStyle={ contentStyle }
352
+ insertBlocksAfter={
353
+ isLocked ? undefined : onInsertBlocksAfter
288
354
  }
289
- </BlockDraggable>
290
- <View
291
- style={ styles.neutralToolbar }
292
- ref={ this.anchorNodeRef }
293
- >
294
- { isSelected && (
295
- <BlockMobileToolbar
296
- clientId={ clientId }
297
- onDelete={ onDeleteBlock }
298
- isStackedHorizontally={
299
- isStackedHorizontally
300
- }
301
- blockWidth={ blockWidth }
302
- anchorNodeRef={ this.anchorNodeRef.current }
303
- isFullWidth={ isFullWidthToolbar }
304
- draggingClientId={ draggingClientId }
305
- />
306
- ) }
307
- </View>
308
- </View>
309
- </View>
310
- </TouchableWithoutFeedback>
311
- );
312
- }
313
- }
314
-
315
- // Helper function to memoize the wrapperProps since getEditWrapperProps always returns a new reference.
316
- const wrapperPropsCache = new WeakMap();
317
- const emptyObj = {};
318
- function getWrapperProps( value, getWrapperPropsFunction ) {
319
- if ( ! getWrapperPropsFunction ) {
320
- return emptyObj;
321
- }
322
- const cachedValue = wrapperPropsCache.get( value );
323
- if ( ! cachedValue ) {
324
- const wrapperProps = getWrapperPropsFunction( value );
325
- wrapperPropsCache.set( value, wrapperProps );
326
- return wrapperProps;
327
- }
328
- return cachedValue;
355
+ isSelected={ isSelected }
356
+ isSelectionEnabled={ isSelectionEnabled }
357
+ mergeBlocks={ canRemove ? onMerge : undefined }
358
+ name={ name }
359
+ onFocus={ onFocus }
360
+ onRemove={ canRemove ? onRemove : undefined }
361
+ onReplace={ canRemove ? onReplace : undefined }
362
+ parentBlockAlignment={ parentBlockAlignment }
363
+ parentWidth={ parentWidth }
364
+ setAttributes={ setAttributes }
365
+ style={ mergedStyle }
366
+ toggleSelection={ toggleSelection }
367
+ __unstableParentLayout={
368
+ Object.keys( parentLayout ).length
369
+ ? parentLayout
370
+ : undefined
371
+ }
372
+ wrapperProps={ wrapperProps }
373
+ />
374
+ <View onLayout={ onLayout } />
375
+ </GlobalStylesContext.Provider>
376
+ )
377
+ }
378
+ </BlockWrapper>
379
+ );
329
380
  }
330
381
 
331
- export default compose( [
332
- withSelect( ( select, { clientId } ) => {
333
- const {
334
- getBlockIndex,
335
- getBlockCount,
336
- getSettings,
337
- isBlockSelected,
338
- getBlock,
339
- getSelectedBlockClientId,
340
- getLowestCommonAncestorWithSelectedBlock,
341
- getBlockParents,
342
- hasSelectedInnerBlock,
343
- getBlockHierarchyRootClientId,
344
- } = select( blockEditorStore );
345
-
346
- const order = getBlockIndex( clientId );
347
- const isSelected = isBlockSelected( clientId );
348
- const isInnerBlockSelected = hasSelectedInnerBlock( clientId );
349
- const block = getBlock( clientId );
350
- const { name, attributes, isValid } = block || {};
351
-
352
- const blockType = getBlockType( name || 'core/missing' );
353
- const title = blockType?.title;
354
- const icon = blockType?.icon;
355
-
356
- const parents = getBlockParents( clientId, true );
357
- const parentId = parents[ 0 ] || '';
358
-
359
- const selectedBlockClientId = getSelectedBlockClientId();
360
-
361
- const commonAncestor =
362
- getLowestCommonAncestorWithSelectedBlock( clientId );
363
- const commonAncestorIndex = parents.indexOf( commonAncestor ) - 1;
364
- const firstToSelectId = commonAncestor
365
- ? parents[ commonAncestorIndex ]
366
- : parents[ parents.length - 1 ];
367
-
368
- const isParentSelected =
369
- // Set false as a default value to prevent re-render when it's changed from null to false.
370
- ( selectedBlockClientId || false ) &&
371
- selectedBlockClientId === parentId;
372
-
373
- const selectedParents = selectedBlockClientId
374
- ? getBlockParents( selectedBlockClientId )
375
- : [];
376
- const isDescendantOfParentSelected =
377
- selectedParents.includes( parentId );
378
- const isTouchable =
379
- isSelected ||
380
- isDescendantOfParentSelected ||
381
- isParentSelected ||
382
- parentId === '';
383
- const baseGlobalStyles =
384
- getSettings()?.__experimentalGlobalStylesBaseStyles;
385
-
386
- const hasInnerBlocks = getBlockCount( clientId ) > 0;
387
- // For blocks with inner blocks, we only enable the dragging in the nested
388
- // blocks if any of them are selected. This way we prevent the long-press
389
- // gesture from being disabled for elements within the block UI.
390
- const draggingEnabled =
391
- ! hasInnerBlocks ||
392
- isSelected ||
393
- ! hasSelectedInnerBlock( clientId, true );
394
- // Dragging nested blocks is not supported yet. For this reason, the block to be dragged
395
- // will be the top in the hierarchy.
396
- const draggingClientId = getBlockHierarchyRootClientId( clientId );
397
-
398
- return {
399
- icon,
400
- name: name || 'core/missing',
401
- order,
402
- title,
403
- attributes,
404
- blockType,
405
- draggingClientId,
406
- draggingEnabled,
407
- isSelected,
408
- isInnerBlockSelected,
409
- isValid,
410
- isParentSelected,
411
- firstToSelectId,
412
- isTouchable,
413
- baseGlobalStyles,
414
- wrapperProps: getWrapperProps(
415
- attributes,
416
- blockType.getEditWrapperProps
417
- ),
418
- };
419
- } ),
420
- withDispatch( ( dispatch, ownProps, registry ) => {
421
- const {
422
- insertBlocks,
423
- mergeBlocks,
424
- replaceBlocks,
425
- selectBlock,
426
- updateBlockAttributes,
427
- moveBlocksToPosition,
428
- removeBlock,
429
- } = dispatch( blockEditorStore );
430
-
431
- return {
432
- onMerge( forward ) {
433
- const { clientId, rootClientId } = ownProps;
434
- const {
435
- getPreviousBlockClientId,
436
- getNextBlockClientId,
437
- getBlock,
438
- getBlockAttributes,
439
- getBlockName,
440
- getBlockOrder,
441
- getBlockIndex,
442
- getBlockRootClientId,
443
- canInsertBlockType,
444
- } = registry.select( blockEditorStore );
445
-
446
- /**
447
- * Moves the block with clientId up one level. If the block type
448
- * cannot be inserted at the new location, it will be attempted to
449
- * convert to the default block type.
450
- *
451
- * @param {string} _clientId The block to move.
452
- * @param {boolean} changeSelection Whether to change the selection
453
- * to the moved block.
454
- */
455
- function moveFirstItemUp( _clientId, changeSelection = true ) {
456
- const targetRootClientId =
457
- getBlockRootClientId( _clientId );
458
- const blockOrder = getBlockOrder( _clientId );
459
- const [ firstClientId ] = blockOrder;
460
-
382
+ const applyWithSelect = withSelect( ( select, { clientId, rootClientId } ) => {
383
+ const {
384
+ isBlockSelected,
385
+ getBlockMode,
386
+ isSelectionEnabled,
387
+ getTemplateLock,
388
+ __unstableGetBlockWithoutInnerBlocks,
389
+ canRemoveBlock,
390
+ canMoveBlock,
391
+ } = select( blockEditorStore );
392
+ const block = __unstableGetBlockWithoutInnerBlocks( clientId );
393
+ const isSelected = isBlockSelected( clientId );
394
+ const templateLock = getTemplateLock( rootClientId );
395
+ const canRemove = canRemoveBlock( clientId, rootClientId );
396
+ const canMove = canMoveBlock( clientId, rootClientId );
397
+
398
+ // The fallback to `{}` is a temporary fix.
399
+ // This function should never be called when a block is not present in
400
+ // the state. It happens now because the order in withSelect rendering
401
+ // is not correct.
402
+ const { name, attributes, isValid } = block || {};
403
+
404
+ // Do not add new properties here, use `useSelect` instead to avoid
405
+ // leaking new props to the public API (editor.BlockListBlock filter).
406
+ return {
407
+ mode: getBlockMode( clientId ),
408
+ isSelectionEnabled: isSelectionEnabled(),
409
+ isLocked: !! templateLock,
410
+ canRemove,
411
+ canMove,
412
+ // Users of the editor.BlockListBlock filter used to be able to
413
+ // access the block prop.
414
+ // Ideally these blocks would rely on the clientId prop only.
415
+ // This is kept for backward compatibility reasons.
416
+ block,
417
+ name,
418
+ attributes,
419
+ isValid,
420
+ isSelected,
421
+ };
422
+ } );
423
+
424
+ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
425
+ const {
426
+ updateBlockAttributes,
427
+ insertBlocks,
428
+ mergeBlocks,
429
+ replaceBlocks,
430
+ toggleSelection,
431
+ __unstableMarkLastChangeAsPersistent,
432
+ moveBlocksToPosition,
433
+ removeBlock,
434
+ } = dispatch( blockEditorStore );
435
+
436
+ // Do not add new properties here, use `useDispatch` instead to avoid
437
+ // leaking new props to the public API (editor.BlockListBlock filter).
438
+ return {
439
+ setAttributes( newAttributes ) {
440
+ const { getMultiSelectedBlockClientIds } =
441
+ registry.select( blockEditorStore );
442
+ const multiSelectedBlockClientIds =
443
+ getMultiSelectedBlockClientIds();
444
+ const { clientId } = ownProps;
445
+ const clientIds = multiSelectedBlockClientIds.length
446
+ ? multiSelectedBlockClientIds
447
+ : [ clientId ];
448
+
449
+ updateBlockAttributes( clientIds, newAttributes );
450
+ },
451
+ onInsertBlocks( blocks, index ) {
452
+ const { rootClientId } = ownProps;
453
+ insertBlocks( blocks, index, rootClientId );
454
+ },
455
+ onInsertBlocksAfter( blocks ) {
456
+ const { clientId, rootClientId } = ownProps;
457
+ const { getBlockIndex } = registry.select( blockEditorStore );
458
+ const index = getBlockIndex( clientId );
459
+ insertBlocks( blocks, index + 1, rootClientId );
460
+ },
461
+ onMerge( forward ) {
462
+ const { clientId, rootClientId } = ownProps;
463
+ const {
464
+ getPreviousBlockClientId,
465
+ getNextBlockClientId,
466
+ getBlock,
467
+ getBlockAttributes,
468
+ getBlockName,
469
+ getBlockOrder,
470
+ getBlockIndex,
471
+ getBlockRootClientId,
472
+ canInsertBlockType,
473
+ } = registry.select( blockEditorStore );
474
+
475
+ /**
476
+ * Moves the block with clientId up one level. If the block type
477
+ * cannot be inserted at the new location, it will be attempted to
478
+ * convert to the default block type.
479
+ *
480
+ * @param {string} _clientId The block to move.
481
+ * @param {boolean} changeSelection Whether to change the selection
482
+ * to the moved block.
483
+ */
484
+ function moveFirstItemUp( _clientId, changeSelection = true ) {
485
+ const targetRootClientId = getBlockRootClientId( _clientId );
486
+ const blockOrder = getBlockOrder( _clientId );
487
+ const [ firstClientId ] = blockOrder;
488
+
489
+ if (
490
+ blockOrder.length === 1 &&
491
+ isUnmodifiedBlock( getBlock( firstClientId ) )
492
+ ) {
493
+ removeBlock( _clientId );
494
+ } else {
461
495
  if (
462
- blockOrder.length === 1 &&
463
- isUnmodifiedBlock( getBlock( firstClientId ) )
496
+ canInsertBlockType(
497
+ getBlockName( firstClientId ),
498
+ targetRootClientId
499
+ )
464
500
  ) {
465
- removeBlock( _clientId );
501
+ moveBlocksToPosition(
502
+ [ firstClientId ],
503
+ _clientId,
504
+ targetRootClientId,
505
+ getBlockIndex( _clientId )
506
+ );
466
507
  } else {
467
- if (
468
- canInsertBlockType(
469
- getBlockName( firstClientId ),
470
- targetRootClientId
471
- )
472
- ) {
473
- moveBlocksToPosition(
474
- [ firstClientId ],
475
- _clientId,
476
- targetRootClientId,
477
- getBlockIndex( _clientId )
478
- );
479
- } else {
480
- const replacement = switchToBlockType(
481
- getBlock( firstClientId ),
482
- getDefaultBlockName()
483
- );
484
-
485
- if ( replacement && replacement.length ) {
486
- registry.batch( () => {
487
- insertBlocks(
488
- replacement,
489
- getBlockIndex( _clientId ),
490
- targetRootClientId,
491
- changeSelection
492
- );
493
- removeBlock( firstClientId, false );
494
- } );
495
- }
496
- }
497
-
498
- if (
499
- ! getBlockOrder( _clientId ).length &&
500
- isUnmodifiedBlock( getBlock( _clientId ) )
501
- ) {
502
- removeBlock( _clientId, false );
503
- }
504
- }
505
- }
506
-
507
- // For `Delete` or forward merge, we should do the exact same thing
508
- // as `Backspace`, but from the other block.
509
- if ( forward ) {
510
- if ( rootClientId ) {
511
- const nextRootClientId =
512
- getNextBlockClientId( rootClientId );
513
-
514
- if ( nextRootClientId ) {
515
- // If there is a block that follows with the same parent
516
- // block name and the same attributes, merge the inner
517
- // blocks.
518
- if (
519
- getBlockName( rootClientId ) ===
520
- getBlockName( nextRootClientId )
521
- ) {
522
- const rootAttributes =
523
- getBlockAttributes( rootClientId );
524
- const previousRootAttributes =
525
- getBlockAttributes( nextRootClientId );
526
-
527
- if (
528
- Object.keys( rootAttributes ).every(
529
- ( key ) =>
530
- rootAttributes[ key ] ===
531
- previousRootAttributes[ key ]
532
- )
533
- ) {
534
- registry.batch( () => {
535
- moveBlocksToPosition(
536
- getBlockOrder( nextRootClientId ),
537
- nextRootClientId,
538
- rootClientId
539
- );
540
- removeBlock( nextRootClientId, false );
541
- } );
542
- return;
543
- }
544
- } else {
545
- mergeBlocks( rootClientId, nextRootClientId );
546
- return;
547
- }
508
+ const replacement = switchToBlockType(
509
+ getBlock( firstClientId ),
510
+ getDefaultBlockName()
511
+ );
512
+
513
+ if ( replacement && replacement.length ) {
514
+ registry.batch( () => {
515
+ insertBlocks(
516
+ replacement,
517
+ getBlockIndex( _clientId ),
518
+ targetRootClientId,
519
+ changeSelection
520
+ );
521
+ removeBlock( firstClientId, false );
522
+ } );
548
523
  }
549
524
  }
550
525
 
551
- const nextBlockClientId = getNextBlockClientId( clientId );
552
-
553
- if ( ! nextBlockClientId ) {
554
- return;
555
- }
556
-
557
- if ( getBlockOrder( nextBlockClientId ).length ) {
558
- moveFirstItemUp( nextBlockClientId, false );
559
- } else {
560
- mergeBlocks( clientId, nextBlockClientId );
526
+ if (
527
+ ! getBlockOrder( _clientId ).length &&
528
+ isUnmodifiedBlock( getBlock( _clientId ) )
529
+ ) {
530
+ removeBlock( _clientId, false );
561
531
  }
562
- } else {
563
- const previousBlockClientId =
564
- getPreviousBlockClientId( clientId );
565
-
566
- if ( previousBlockClientId ) {
567
- mergeBlocks( previousBlockClientId, clientId );
568
- } else if ( rootClientId ) {
569
- const previousRootClientId =
570
- getPreviousBlockClientId( rootClientId );
571
-
572
- // If there is a preceding block with the same parent block
573
- // name and the same attributes, merge the inner blocks.
532
+ }
533
+ }
534
+
535
+ // For `Delete` or forward merge, we should do the exact same thing
536
+ // as `Backspace`, but from the other block.
537
+ if ( forward ) {
538
+ if ( rootClientId ) {
539
+ const nextRootClientId =
540
+ getNextBlockClientId( rootClientId );
541
+
542
+ if ( nextRootClientId ) {
543
+ // If there is a block that follows with the same parent
544
+ // block name and the same attributes, merge the inner
545
+ // blocks.
574
546
  if (
575
- previousRootClientId &&
576
547
  getBlockName( rootClientId ) ===
577
- getBlockName( previousRootClientId )
548
+ getBlockName( nextRootClientId )
578
549
  ) {
579
550
  const rootAttributes =
580
551
  getBlockAttributes( rootClientId );
581
552
  const previousRootAttributes =
582
- getBlockAttributes( previousRootClientId );
553
+ getBlockAttributes( nextRootClientId );
583
554
 
584
555
  if (
585
556
  Object.keys( rootAttributes ).every(
@@ -590,33 +561,103 @@ export default compose( [
590
561
  ) {
591
562
  registry.batch( () => {
592
563
  moveBlocksToPosition(
593
- getBlockOrder( rootClientId ),
594
- rootClientId,
595
- previousRootClientId
564
+ getBlockOrder( nextRootClientId ),
565
+ nextRootClientId,
566
+ rootClientId
596
567
  );
597
- removeBlock( rootClientId, false );
568
+ removeBlock( nextRootClientId, false );
598
569
  } );
599
570
  return;
600
571
  }
572
+ } else {
573
+ mergeBlocks( rootClientId, nextRootClientId );
574
+ return;
601
575
  }
576
+ }
577
+ }
578
+
579
+ const nextBlockClientId = getNextBlockClientId( clientId );
580
+
581
+ if ( ! nextBlockClientId ) {
582
+ return;
583
+ }
584
+
585
+ if ( getBlockOrder( nextBlockClientId ).length ) {
586
+ moveFirstItemUp( nextBlockClientId, false );
587
+ } else {
588
+ mergeBlocks( clientId, nextBlockClientId );
589
+ }
590
+ } else {
591
+ const previousBlockClientId =
592
+ getPreviousBlockClientId( clientId );
593
+
594
+ if ( previousBlockClientId ) {
595
+ mergeBlocks( previousBlockClientId, clientId );
596
+ } else if ( rootClientId ) {
597
+ const previousRootClientId =
598
+ getPreviousBlockClientId( rootClientId );
599
+
600
+ // If there is a preceding block with the same parent block
601
+ // name and the same attributes, merge the inner blocks.
602
+ if (
603
+ previousRootClientId &&
604
+ getBlockName( rootClientId ) ===
605
+ getBlockName( previousRootClientId )
606
+ ) {
607
+ const rootAttributes =
608
+ getBlockAttributes( rootClientId );
609
+ const previousRootAttributes =
610
+ getBlockAttributes( previousRootClientId );
602
611
 
603
- moveFirstItemUp( rootClientId );
612
+ if (
613
+ Object.keys( rootAttributes ).every(
614
+ ( key ) =>
615
+ rootAttributes[ key ] ===
616
+ previousRootAttributes[ key ]
617
+ )
618
+ ) {
619
+ registry.batch( () => {
620
+ moveBlocksToPosition(
621
+ getBlockOrder( rootClientId ),
622
+ rootClientId,
623
+ previousRootClientId
624
+ );
625
+ removeBlock( rootClientId, false );
626
+ } );
627
+ return;
628
+ }
604
629
  }
630
+
631
+ moveFirstItemUp( rootClientId );
605
632
  }
606
- },
607
- onInsertBlocks( blocks, index ) {
608
- insertBlocks( blocks, index, ownProps.rootClientId );
609
- },
610
- onSelect( clientId = ownProps.clientId, initialPosition ) {
611
- selectBlock( clientId, initialPosition );
612
- },
613
- onChange: ( attributes ) => {
614
- updateBlockAttributes( ownProps.clientId, attributes );
615
- },
616
- onReplace( blocks, indexToSelect ) {
617
- replaceBlocks( [ ownProps.clientId ], blocks, indexToSelect );
618
- },
619
- };
620
- } ),
621
- withPreferredColorScheme,
622
- ] )( BlockListBlock );
633
+ }
634
+ },
635
+ onReplace( blocks, indexToSelect, initialPosition ) {
636
+ if (
637
+ blocks.length &&
638
+ ! isUnmodifiedDefaultBlock( blocks[ blocks.length - 1 ] )
639
+ ) {
640
+ __unstableMarkLastChangeAsPersistent();
641
+ }
642
+ replaceBlocks(
643
+ [ ownProps.clientId ],
644
+ blocks,
645
+ indexToSelect,
646
+ initialPosition
647
+ );
648
+ },
649
+ toggleSelection( selectionEnabled ) {
650
+ toggleSelection( selectionEnabled );
651
+ },
652
+ };
653
+ } );
654
+
655
+ export default compose(
656
+ pure,
657
+ applyWithSelect,
658
+ applyWithDispatch,
659
+ // Block is sometimes not mounted at the right time, causing it be undefined
660
+ // see issue for more info
661
+ // https://github.com/WordPress/gutenberg/issues/17013
662
+ ifCondition( ( { block } ) => !! block )
663
+ )( BlockListBlock );