@wordpress/block-editor 12.13.0 → 12.15.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 (528) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +1 -1
  3. package/build/components/block-heading-level-dropdown/index.js +1 -1
  4. package/build/components/block-heading-level-dropdown/index.js.map +1 -1
  5. package/build/components/block-list/block.native.js +2 -0
  6. package/build/components/block-list/block.native.js.map +1 -1
  7. package/build/components/block-list/index.js +2 -9
  8. package/build/components/block-list/index.js.map +1 -1
  9. package/build/components/block-list-appender/index.js +16 -3
  10. package/build/components/block-list-appender/index.js.map +1 -1
  11. package/build/components/block-parent-selector/index.js +1 -1
  12. package/build/components/block-parent-selector/index.js.map +1 -1
  13. package/build/components/block-pattern-setup/index.js +25 -16
  14. package/build/components/block-pattern-setup/index.js.map +1 -1
  15. package/build/components/block-pattern-setup/setup-toolbar.js +4 -2
  16. package/build/components/block-pattern-setup/setup-toolbar.js.map +1 -1
  17. package/build/components/block-patterns-list/index.js +36 -19
  18. package/build/components/block-patterns-list/index.js.map +1 -1
  19. package/build/components/block-quick-navigation/index.js +6 -4
  20. package/build/components/block-quick-navigation/index.js.map +1 -1
  21. package/build/components/block-rename/index.js +28 -0
  22. package/build/components/block-rename/index.js.map +1 -0
  23. package/build/components/block-rename/is-empty-string.js +10 -0
  24. package/build/components/block-rename/is-empty-string.js.map +1 -0
  25. package/build/components/block-rename/modal.js +87 -0
  26. package/build/components/block-rename/modal.js.map +1 -0
  27. package/build/components/block-rename/rename-control.js +74 -0
  28. package/build/components/block-rename/rename-control.js.map +1 -0
  29. package/build/components/block-rename/use-block-rename.js +17 -0
  30. package/build/components/block-rename/use-block-rename.js.map +1 -0
  31. package/build/components/block-settings-menu/block-settings-dropdown.js +33 -20
  32. package/build/components/block-settings-menu/block-settings-dropdown.js.map +1 -1
  33. package/build/components/block-settings-menu-controls/index.js +7 -0
  34. package/build/components/block-settings-menu-controls/index.js.map +1 -1
  35. package/build/components/block-styles/index.js +0 -8
  36. package/build/components/block-styles/index.js.map +1 -1
  37. package/build/components/block-switcher/pattern-transformations-menu.js +18 -14
  38. package/build/components/block-switcher/pattern-transformations-menu.js.map +1 -1
  39. package/build/components/block-toolbar/block-toolbar-menu.native.js +1 -1
  40. package/build/components/block-toolbar/block-toolbar-menu.native.js.map +1 -1
  41. package/build/components/block-tools/back-compat.js +2 -2
  42. package/build/components/block-tools/back-compat.js.map +1 -1
  43. package/build/components/block-tools/block-contextual-toolbar.js +11 -81
  44. package/build/components/block-tools/block-contextual-toolbar.js.map +1 -1
  45. package/build/components/block-tools/empty-block-inserter.js +56 -0
  46. package/build/components/block-tools/empty-block-inserter.js.map +1 -0
  47. package/build/components/block-tools/index.js +48 -9
  48. package/build/components/block-tools/index.js.map +1 -1
  49. package/build/components/block-tools/selected-block-tools.js +113 -0
  50. package/build/components/block-tools/selected-block-tools.js.map +1 -0
  51. package/build/components/block-tools/use-selected-block-tool-props.js +56 -0
  52. package/build/components/block-tools/use-selected-block-tool-props.js.map +1 -0
  53. package/build/components/block-types-list/index.js +6 -2
  54. package/build/components/block-types-list/index.js.map +1 -1
  55. package/build/components/date-format-picker/index.js +8 -1
  56. package/build/components/date-format-picker/index.js.map +1 -1
  57. package/build/components/editable-text/index.js +1 -2
  58. package/build/components/editable-text/index.js.map +1 -1
  59. package/build/components/global-styles/advanced-panel.js +1 -1
  60. package/build/components/global-styles/advanced-panel.js.map +1 -1
  61. package/build/components/iframe/index.js +4 -3
  62. package/build/components/iframe/index.js.map +1 -1
  63. package/build/components/image-size-control/index.js +0 -5
  64. package/build/components/image-size-control/index.js.map +1 -1
  65. package/build/components/inner-blocks/index.js +3 -1
  66. package/build/components/inner-blocks/index.js.map +1 -1
  67. package/build/components/inner-blocks/warning-max-depth-exceeded.native.js +29 -31
  68. package/build/components/inner-blocks/warning-max-depth-exceeded.native.js.map +1 -1
  69. package/build/components/inserter/block-patterns-explorer/{explorer.js → index.js} +7 -7
  70. package/build/components/inserter/block-patterns-explorer/index.js.map +1 -0
  71. package/build/components/inserter/block-patterns-explorer/{sidebar.js → pattern-explorer-sidebar.js} +1 -1
  72. package/build/components/inserter/block-patterns-explorer/pattern-explorer-sidebar.js.map +1 -0
  73. package/build/components/inserter/block-patterns-explorer/{patterns-list.js → pattern-list.js} +4 -4
  74. package/build/components/inserter/block-patterns-explorer/pattern-list.js.map +1 -0
  75. package/build/components/inserter/block-patterns-tab/index.js +75 -0
  76. package/build/components/inserter/block-patterns-tab/index.js.map +1 -0
  77. package/build/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +48 -0
  78. package/build/components/inserter/block-patterns-tab/pattern-category-preview-panel.js.map +1 -0
  79. package/build/components/inserter/block-patterns-tab/pattern-category-previews.js +108 -0
  80. package/build/components/inserter/block-patterns-tab/pattern-category-previews.js.map +1 -0
  81. package/build/components/inserter/{block-patterns-filter.js → block-patterns-tab/patterns-filter.js} +18 -34
  82. package/build/components/inserter/block-patterns-tab/patterns-filter.js.map +1 -0
  83. package/build/components/inserter/block-patterns-tab/use-pattern-categories.js +55 -0
  84. package/build/components/inserter/block-patterns-tab/use-pattern-categories.js.map +1 -0
  85. package/build/components/inserter/block-patterns-tab/utils.js +69 -0
  86. package/build/components/inserter/block-patterns-tab/utils.js.map +1 -0
  87. package/build/components/inserter/hooks/use-patterns-state.js +2 -1
  88. package/build/components/inserter/hooks/use-patterns-state.js.map +1 -1
  89. package/build/components/inserter/media-tab/media-list.js +9 -5
  90. package/build/components/inserter/media-tab/media-list.js.map +1 -1
  91. package/build/components/inserter/media-tab/media-preview.js +15 -12
  92. package/build/components/inserter/media-tab/media-preview.js.map +1 -1
  93. package/build/components/inserter/menu.js +3 -4
  94. package/build/components/inserter/menu.js.map +1 -1
  95. package/build/components/inserter-listbox/index.js +11 -6
  96. package/build/components/inserter-listbox/index.js.map +1 -1
  97. package/build/components/inserter-listbox/item.js +24 -23
  98. package/build/components/inserter-listbox/item.js.map +1 -1
  99. package/build/components/inserter-listbox/row.js +5 -5
  100. package/build/components/inserter-listbox/row.js.map +1 -1
  101. package/build/components/link-control/index.js +6 -5
  102. package/build/components/link-control/index.js.map +1 -1
  103. package/build/components/list-view/block-select-button.js +39 -0
  104. package/build/components/list-view/block-select-button.js.map +1 -1
  105. package/build/components/list-view/block.js +16 -3
  106. package/build/components/list-view/block.js.map +1 -1
  107. package/build/components/list-view/index.js +3 -2
  108. package/build/components/list-view/index.js.map +1 -1
  109. package/build/components/list-view/use-list-view-expand-selected-item.js +2 -3
  110. package/build/components/list-view/use-list-view-expand-selected-item.js.map +1 -1
  111. package/build/components/navigable-toolbar/index.js +69 -18
  112. package/build/components/navigable-toolbar/index.js.map +1 -1
  113. package/build/components/preview-options/index.js +4 -2
  114. package/build/components/preview-options/index.js.map +1 -1
  115. package/build/components/provider/use-block-sync.js +1 -14
  116. package/build/components/provider/use-block-sync.js.map +1 -1
  117. package/build/components/rich-text/index.js +0 -1
  118. package/build/components/rich-text/index.js.map +1 -1
  119. package/build/components/rich-text/index.native.js +4 -2
  120. package/build/components/rich-text/index.native.js.map +1 -1
  121. package/build/components/rich-text/native/format-edit.js +45 -0
  122. package/build/components/rich-text/native/format-edit.js.map +1 -0
  123. package/build/components/rich-text/native/get-format-colors.native.js +41 -0
  124. package/build/components/rich-text/native/get-format-colors.native.js.map +1 -0
  125. package/build/components/rich-text/native/index.js +9 -0
  126. package/build/components/rich-text/native/index.js.map +1 -0
  127. package/build/components/rich-text/native/index.native.js +1189 -0
  128. package/build/components/rich-text/native/index.native.js.map +1 -0
  129. package/build/components/rich-text/native/toolbar-button-with-options.native.js +58 -0
  130. package/build/components/rich-text/native/toolbar-button-with-options.native.js.map +1 -0
  131. package/build/components/rich-text/native/use-format-types.js +111 -0
  132. package/build/components/rich-text/native/use-format-types.js.map +1 -0
  133. package/build/components/rich-text/use-input-rules.js +30 -1
  134. package/build/components/rich-text/use-input-rules.js.map +1 -1
  135. package/build/components/rich-text/use-paste-handler.js +2 -5
  136. package/build/components/rich-text/use-paste-handler.js.map +1 -1
  137. package/build/components/spacing-sizes-control/input-controls/spacing-input-control.js +1 -1
  138. package/build/components/spacing-sizes-control/input-controls/spacing-input-control.js.map +1 -1
  139. package/build/components/url-popover/image-url-input-ui.js +2 -1
  140. package/build/components/url-popover/image-url-input-ui.js.map +1 -1
  141. package/build/components/use-block-drop-zone/index.js +4 -1
  142. package/build/components/use-block-drop-zone/index.js.map +1 -1
  143. package/build/components/use-moving-animation/index.js +2 -1
  144. package/build/components/use-moving-animation/index.js.map +1 -1
  145. package/build/components/use-settings/index.js +2 -2
  146. package/build/components/use-settings/index.js.map +1 -1
  147. package/build/components/writing-flow/use-tab-nav.js +7 -3
  148. package/build/components/writing-flow/use-tab-nav.js.map +1 -1
  149. package/build/hooks/align.js +10 -10
  150. package/build/hooks/align.js.map +1 -1
  151. package/build/hooks/align.native.js +2 -2
  152. package/build/hooks/align.native.js.map +1 -1
  153. package/build/hooks/anchor.js +7 -6
  154. package/build/hooks/anchor.js.map +1 -1
  155. package/build/hooks/background.js +16 -4
  156. package/build/hooks/background.js.map +1 -1
  157. package/build/hooks/block-hooks.js +7 -8
  158. package/build/hooks/block-hooks.js.map +1 -1
  159. package/build/hooks/block-renaming.js +42 -0
  160. package/build/hooks/block-renaming.js.map +1 -1
  161. package/build/hooks/content-lock-ui.js +5 -5
  162. package/build/hooks/content-lock-ui.js.map +1 -1
  163. package/build/hooks/custom-class-name.js +8 -7
  164. package/build/hooks/custom-class-name.js.map +1 -1
  165. package/build/hooks/custom-fields.js +57 -52
  166. package/build/hooks/custom-fields.js.map +1 -1
  167. package/build/hooks/duotone.js +9 -20
  168. package/build/hooks/duotone.js.map +1 -1
  169. package/build/hooks/index.js +0 -1
  170. package/build/hooks/index.js.map +1 -1
  171. package/build/hooks/layout.js +103 -93
  172. package/build/hooks/layout.js.map +1 -1
  173. package/build/hooks/position.js +10 -9
  174. package/build/hooks/position.js.map +1 -1
  175. package/build/hooks/style.js +10 -13
  176. package/build/hooks/style.js.map +1 -1
  177. package/build/hooks/utils.js +30 -0
  178. package/build/hooks/utils.js.map +1 -1
  179. package/build/layouts/constrained.js +4 -3
  180. package/build/layouts/constrained.js.map +1 -1
  181. package/build/private-apis.js +3 -0
  182. package/build/private-apis.js.map +1 -1
  183. package/build/store/actions.js +30 -37
  184. package/build/store/actions.js.map +1 -1
  185. package/build/store/reducer.js +18 -0
  186. package/build/store/reducer.js.map +1 -1
  187. package/build/store/selectors.js +18 -13
  188. package/build/store/selectors.js.map +1 -1
  189. package/build/utils/transform-styles/index.js +26 -6
  190. package/build/utils/transform-styles/index.js.map +1 -1
  191. package/build-module/components/block-heading-level-dropdown/index.js +1 -1
  192. package/build-module/components/block-heading-level-dropdown/index.js.map +1 -1
  193. package/build-module/components/block-list/block.native.js +2 -0
  194. package/build-module/components/block-list/block.native.js.map +1 -1
  195. package/build-module/components/block-list/index.js +3 -10
  196. package/build-module/components/block-list/index.js.map +1 -1
  197. package/build-module/components/block-list-appender/index.js +16 -3
  198. package/build-module/components/block-list-appender/index.js.map +1 -1
  199. package/build-module/components/block-parent-selector/index.js +1 -1
  200. package/build-module/components/block-parent-selector/index.js.map +1 -1
  201. package/build-module/components/block-pattern-setup/index.js +24 -15
  202. package/build-module/components/block-pattern-setup/index.js.map +1 -1
  203. package/build-module/components/block-pattern-setup/setup-toolbar.js +4 -2
  204. package/build-module/components/block-pattern-setup/setup-toolbar.js.map +1 -1
  205. package/build-module/components/block-patterns-list/index.js +36 -19
  206. package/build-module/components/block-patterns-list/index.js.map +1 -1
  207. package/build-module/components/block-quick-navigation/index.js +7 -5
  208. package/build-module/components/block-quick-navigation/index.js.map +1 -1
  209. package/build-module/components/block-rename/index.js +4 -0
  210. package/build-module/components/block-rename/index.js.map +1 -0
  211. package/build-module/components/block-rename/is-empty-string.js +4 -0
  212. package/build-module/components/block-rename/is-empty-string.js.map +1 -0
  213. package/build-module/components/block-rename/modal.js +79 -0
  214. package/build-module/components/block-rename/modal.js.map +1 -0
  215. package/build-module/components/block-rename/rename-control.js +66 -0
  216. package/build-module/components/block-rename/rename-control.js.map +1 -0
  217. package/build-module/components/block-rename/use-block-rename.js +10 -0
  218. package/build-module/components/block-rename/use-block-rename.js.map +1 -0
  219. package/build-module/components/block-settings-menu/block-settings-dropdown.js +34 -21
  220. package/build-module/components/block-settings-menu/block-settings-dropdown.js.map +1 -1
  221. package/build-module/components/block-settings-menu-controls/index.js +7 -0
  222. package/build-module/components/block-settings-menu-controls/index.js.map +1 -1
  223. package/build-module/components/block-styles/index.js +0 -8
  224. package/build-module/components/block-styles/index.js.map +1 -1
  225. package/build-module/components/block-switcher/pattern-transformations-menu.js +17 -13
  226. package/build-module/components/block-switcher/pattern-transformations-menu.js.map +1 -1
  227. package/build-module/components/block-toolbar/block-toolbar-menu.native.js +1 -1
  228. package/build-module/components/block-toolbar/block-toolbar-menu.native.js.map +1 -1
  229. package/build-module/components/block-tools/back-compat.js +1 -1
  230. package/build-module/components/block-tools/back-compat.js.map +1 -1
  231. package/build-module/components/block-tools/block-contextual-toolbar.js +11 -80
  232. package/build-module/components/block-tools/block-contextual-toolbar.js.map +1 -1
  233. package/build-module/components/block-tools/empty-block-inserter.js +48 -0
  234. package/build-module/components/block-tools/empty-block-inserter.js.map +1 -0
  235. package/build-module/components/block-tools/index.js +48 -9
  236. package/build-module/components/block-tools/index.js.map +1 -1
  237. package/build-module/components/block-tools/selected-block-tools.js +105 -0
  238. package/build-module/components/block-tools/selected-block-tools.js.map +1 -0
  239. package/build-module/components/block-tools/use-selected-block-tool-props.js +50 -0
  240. package/build-module/components/block-tools/use-selected-block-tool-props.js.map +1 -0
  241. package/build-module/components/block-types-list/index.js +6 -2
  242. package/build-module/components/block-types-list/index.js.map +1 -1
  243. package/build-module/components/date-format-picker/index.js +8 -1
  244. package/build-module/components/date-format-picker/index.js.map +1 -1
  245. package/build-module/components/editable-text/index.js +1 -2
  246. package/build-module/components/editable-text/index.js.map +1 -1
  247. package/build-module/components/global-styles/advanced-panel.js +1 -1
  248. package/build-module/components/global-styles/advanced-panel.js.map +1 -1
  249. package/build-module/components/iframe/index.js +4 -3
  250. package/build-module/components/iframe/index.js.map +1 -1
  251. package/build-module/components/image-size-control/index.js +0 -5
  252. package/build-module/components/image-size-control/index.js.map +1 -1
  253. package/build-module/components/inner-blocks/index.js +3 -1
  254. package/build-module/components/inner-blocks/index.js.map +1 -1
  255. package/build-module/components/inner-blocks/warning-max-depth-exceeded.native.js +30 -32
  256. package/build-module/components/inner-blocks/warning-max-depth-exceeded.native.js.map +1 -1
  257. package/build-module/components/inserter/block-patterns-explorer/{explorer.js → index.js} +5 -5
  258. package/build-module/components/inserter/block-patterns-explorer/index.js.map +1 -0
  259. package/build-module/components/inserter/block-patterns-explorer/{sidebar.js → pattern-explorer-sidebar.js} +1 -1
  260. package/build-module/components/inserter/block-patterns-explorer/pattern-explorer-sidebar.js.map +1 -0
  261. package/build-module/components/inserter/block-patterns-explorer/{patterns-list.js → pattern-list.js} +3 -3
  262. package/build-module/components/inserter/block-patterns-explorer/pattern-list.js.map +1 -0
  263. package/build-module/components/inserter/block-patterns-tab/index.js +66 -0
  264. package/build-module/components/inserter/block-patterns-tab/index.js.map +1 -0
  265. package/build-module/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +42 -0
  266. package/build-module/components/inserter/block-patterns-tab/pattern-category-preview-panel.js.map +1 -0
  267. package/build-module/components/inserter/block-patterns-tab/pattern-category-previews.js +100 -0
  268. package/build-module/components/inserter/block-patterns-tab/pattern-category-previews.js.map +1 -0
  269. package/build-module/components/inserter/{block-patterns-filter.js → block-patterns-tab/patterns-filter.js} +7 -20
  270. package/build-module/components/inserter/block-patterns-tab/patterns-filter.js.map +1 -0
  271. package/build-module/components/inserter/block-patterns-tab/use-pattern-categories.js +47 -0
  272. package/build-module/components/inserter/block-patterns-tab/use-pattern-categories.js.map +1 -0
  273. package/build-module/components/inserter/block-patterns-tab/utils.js +57 -0
  274. package/build-module/components/inserter/block-patterns-tab/utils.js.map +1 -0
  275. package/build-module/components/inserter/hooks/use-patterns-state.js +2 -1
  276. package/build-module/components/inserter/hooks/use-patterns-state.js.map +1 -1
  277. package/build-module/components/inserter/media-tab/media-list.js +9 -5
  278. package/build-module/components/inserter/media-tab/media-list.js.map +1 -1
  279. package/build-module/components/inserter/media-tab/media-preview.js +15 -12
  280. package/build-module/components/inserter/media-tab/media-preview.js.map +1 -1
  281. package/build-module/components/inserter/menu.js +4 -3
  282. package/build-module/components/inserter/menu.js.map +1 -1
  283. package/build-module/components/inserter-listbox/index.js +13 -8
  284. package/build-module/components/inserter-listbox/index.js.map +1 -1
  285. package/build-module/components/inserter-listbox/item.js +25 -23
  286. package/build-module/components/inserter-listbox/item.js.map +1 -1
  287. package/build-module/components/inserter-listbox/row.js +6 -5
  288. package/build-module/components/inserter-listbox/row.js.map +1 -1
  289. package/build-module/components/link-control/index.js +7 -6
  290. package/build-module/components/link-control/index.js.map +1 -1
  291. package/build-module/components/list-view/block-select-button.js +39 -0
  292. package/build-module/components/list-view/block-select-button.js.map +1 -1
  293. package/build-module/components/list-view/block.js +16 -3
  294. package/build-module/components/list-view/block.js.map +1 -1
  295. package/build-module/components/list-view/index.js +3 -2
  296. package/build-module/components/list-view/index.js.map +1 -1
  297. package/build-module/components/list-view/use-list-view-expand-selected-item.js +2 -3
  298. package/build-module/components/list-view/use-list-view-expand-selected-item.js.map +1 -1
  299. package/build-module/components/navigable-toolbar/index.js +69 -17
  300. package/build-module/components/navigable-toolbar/index.js.map +1 -1
  301. package/build-module/components/preview-options/index.js +4 -2
  302. package/build-module/components/preview-options/index.js.map +1 -1
  303. package/build-module/components/provider/use-block-sync.js +1 -14
  304. package/build-module/components/provider/use-block-sync.js.map +1 -1
  305. package/build-module/components/rich-text/index.js +0 -1
  306. package/build-module/components/rich-text/index.js.map +1 -1
  307. package/build-module/components/rich-text/index.native.js +4 -2
  308. package/build-module/components/rich-text/index.native.js.map +1 -1
  309. package/build-module/components/rich-text/native/format-edit.js +38 -0
  310. package/build-module/components/rich-text/native/format-edit.js.map +1 -0
  311. package/build-module/components/rich-text/native/get-format-colors.native.js +34 -0
  312. package/build-module/components/rich-text/native/get-format-colors.native.js.map +1 -0
  313. package/build-module/components/rich-text/native/index.js +2 -0
  314. package/build-module/components/rich-text/native/index.js.map +1 -0
  315. package/build-module/components/rich-text/native/index.native.js +1179 -0
  316. package/build-module/components/rich-text/native/index.native.js.map +1 -0
  317. package/build-module/components/rich-text/native/toolbar-button-with-options.native.js +51 -0
  318. package/build-module/components/rich-text/native/toolbar-button-with-options.native.js.map +1 -0
  319. package/build-module/components/rich-text/native/use-format-types.js +104 -0
  320. package/build-module/components/rich-text/native/use-format-types.js.map +1 -0
  321. package/build-module/components/rich-text/use-input-rules.js +31 -2
  322. package/build-module/components/rich-text/use-input-rules.js.map +1 -1
  323. package/build-module/components/rich-text/use-paste-handler.js +2 -5
  324. package/build-module/components/rich-text/use-paste-handler.js.map +1 -1
  325. package/build-module/components/spacing-sizes-control/input-controls/spacing-input-control.js +1 -1
  326. package/build-module/components/spacing-sizes-control/input-controls/spacing-input-control.js.map +1 -1
  327. package/build-module/components/url-popover/image-url-input-ui.js +2 -1
  328. package/build-module/components/url-popover/image-url-input-ui.js.map +1 -1
  329. package/build-module/components/use-block-drop-zone/index.js +4 -1
  330. package/build-module/components/use-block-drop-zone/index.js.map +1 -1
  331. package/build-module/components/use-moving-animation/index.js +2 -1
  332. package/build-module/components/use-moving-animation/index.js.map +1 -1
  333. package/build-module/components/use-settings/index.js +2 -2
  334. package/build-module/components/use-settings/index.js.map +1 -1
  335. package/build-module/components/writing-flow/use-tab-nav.js +7 -3
  336. package/build-module/components/writing-flow/use-tab-nav.js.map +1 -1
  337. package/build-module/hooks/align.js +8 -8
  338. package/build-module/hooks/align.js.map +1 -1
  339. package/build-module/hooks/align.native.js +2 -2
  340. package/build-module/hooks/align.native.js.map +1 -1
  341. package/build-module/hooks/anchor.js +5 -4
  342. package/build-module/hooks/anchor.js.map +1 -1
  343. package/build-module/hooks/background.js +17 -5
  344. package/build-module/hooks/background.js.map +1 -1
  345. package/build-module/hooks/block-hooks.js +5 -6
  346. package/build-module/hooks/block-hooks.js.map +1 -1
  347. package/build-module/hooks/block-renaming.js +40 -0
  348. package/build-module/hooks/block-renaming.js.map +1 -1
  349. package/build-module/hooks/content-lock-ui.js +3 -3
  350. package/build-module/hooks/content-lock-ui.js.map +1 -1
  351. package/build-module/hooks/custom-class-name.js +6 -5
  352. package/build-module/hooks/custom-class-name.js.map +1 -1
  353. package/build-module/hooks/custom-fields.js +57 -52
  354. package/build-module/hooks/custom-fields.js.map +1 -1
  355. package/build-module/hooks/duotone.js +10 -21
  356. package/build-module/hooks/duotone.js.map +1 -1
  357. package/build-module/hooks/index.js +0 -1
  358. package/build-module/hooks/index.js.map +1 -1
  359. package/build-module/hooks/layout.js +103 -93
  360. package/build-module/hooks/layout.js.map +1 -1
  361. package/build-module/hooks/position.js +11 -10
  362. package/build-module/hooks/position.js.map +1 -1
  363. package/build-module/hooks/style.js +10 -13
  364. package/build-module/hooks/style.js.map +1 -1
  365. package/build-module/hooks/utils.js +30 -1
  366. package/build-module/hooks/utils.js.map +1 -1
  367. package/build-module/layouts/constrained.js +4 -3
  368. package/build-module/layouts/constrained.js.map +1 -1
  369. package/build-module/private-apis.js +4 -1
  370. package/build-module/private-apis.js.map +1 -1
  371. package/build-module/store/actions.js +29 -37
  372. package/build-module/store/actions.js.map +1 -1
  373. package/build-module/store/reducer.js +17 -0
  374. package/build-module/store/reducer.js.map +1 -1
  375. package/build-module/store/selectors.js +17 -13
  376. package/build-module/store/selectors.js.map +1 -1
  377. package/build-module/utils/transform-styles/index.js +24 -7
  378. package/build-module/utils/transform-styles/index.js.map +1 -1
  379. package/build-style/content-rtl.css +6 -6
  380. package/build-style/content.css +6 -6
  381. package/build-style/style-rtl.css +26 -20
  382. package/build-style/style.css +26 -20
  383. package/package.json +31 -31
  384. package/src/components/block-heading-level-dropdown/index.js +1 -1
  385. package/src/components/block-list/block.native.js +2 -0
  386. package/src/components/block-list/index.js +4 -18
  387. package/src/components/block-list-appender/index.js +20 -4
  388. package/src/components/block-parent-selector/index.js +1 -1
  389. package/src/components/block-pattern-setup/index.js +38 -22
  390. package/src/components/block-pattern-setup/setup-toolbar.js +2 -0
  391. package/src/components/block-pattern-setup/style.scss +4 -1
  392. package/src/components/block-patterns-list/README.md +4 -4
  393. package/src/components/block-patterns-list/index.js +60 -34
  394. package/src/components/block-patterns-list/style.scss +7 -0
  395. package/src/components/block-quick-navigation/index.js +11 -5
  396. package/src/components/block-rename/index.js +3 -0
  397. package/src/components/block-rename/is-empty-string.js +3 -0
  398. package/src/components/block-rename/modal.js +115 -0
  399. package/src/components/block-rename/rename-control.js +80 -0
  400. package/src/components/block-rename/use-block-rename.js +10 -0
  401. package/src/components/block-settings-menu/block-settings-dropdown.js +38 -34
  402. package/src/components/block-settings-menu-controls/index.js +9 -0
  403. package/src/components/block-styles/index.js +0 -10
  404. package/src/components/block-switcher/pattern-transformations-menu.js +20 -14
  405. package/src/components/block-toolbar/block-toolbar-menu.native.js +1 -4
  406. package/src/components/block-toolbar/style.scss +8 -0
  407. package/src/components/block-tools/back-compat.js +1 -1
  408. package/src/components/block-tools/block-contextual-toolbar.js +11 -134
  409. package/src/components/block-tools/empty-block-inserter.js +56 -0
  410. package/src/components/block-tools/index.js +72 -16
  411. package/src/components/block-tools/selected-block-tools.js +127 -0
  412. package/src/components/block-tools/style.scss +0 -10
  413. package/src/components/block-tools/use-selected-block-tool-props.js +66 -0
  414. package/src/components/block-types-list/index.js +5 -4
  415. package/src/components/button-block-appender/content.scss +2 -2
  416. package/src/components/date-format-picker/index.js +7 -0
  417. package/src/components/editable-text/README.md +0 -36
  418. package/src/components/editable-text/index.js +1 -8
  419. package/src/components/global-styles/advanced-panel.js +1 -1
  420. package/src/components/iframe/index.js +4 -3
  421. package/src/components/image-size-control/index.js +0 -6
  422. package/src/components/inner-blocks/index.js +6 -2
  423. package/src/components/inner-blocks/warning-max-depth-exceeded.native.js +57 -33
  424. package/src/components/inserter/block-patterns-explorer/{explorer.js → index.js} +4 -4
  425. package/src/components/inserter/block-patterns-explorer/{patterns-list.js → pattern-list.js} +9 -2
  426. package/src/components/inserter/block-patterns-tab/index.js +118 -0
  427. package/src/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +48 -0
  428. package/src/components/inserter/block-patterns-tab/pattern-category-previews.js +179 -0
  429. package/src/components/inserter/{block-patterns-filter.js → block-patterns-tab/patterns-filter.js} +15 -21
  430. package/src/components/inserter/block-patterns-tab/use-pattern-categories.js +101 -0
  431. package/src/components/inserter/block-patterns-tab/utils.js +78 -0
  432. package/src/components/inserter/hooks/use-patterns-state.js +3 -1
  433. package/src/components/inserter/media-tab/media-list.js +7 -7
  434. package/src/components/inserter/media-tab/media-preview.js +27 -22
  435. package/src/components/inserter/menu.js +4 -5
  436. package/src/components/inserter-listbox/index.js +11 -7
  437. package/src/components/inserter-listbox/item.js +11 -12
  438. package/src/components/inserter-listbox/row.js +6 -12
  439. package/src/components/link-control/README.md +2 -2
  440. package/src/components/link-control/index.js +15 -6
  441. package/src/components/link-control/style.scss +8 -5
  442. package/src/components/list-view/block-select-button.js +44 -1
  443. package/src/components/list-view/block.js +11 -11
  444. package/src/components/list-view/index.js +2 -0
  445. package/src/components/list-view/use-list-view-expand-selected-item.js +7 -8
  446. package/src/components/media-replace-flow/style.scss +2 -2
  447. package/src/components/navigable-toolbar/index.js +71 -25
  448. package/src/components/plain-text/README.md +2 -30
  449. package/src/components/preview-options/index.js +2 -0
  450. package/src/components/provider/use-block-sync.js +2 -21
  451. package/src/components/rich-text/README.md +6 -63
  452. package/src/components/rich-text/index.js +0 -1
  453. package/src/components/rich-text/index.native.js +4 -2
  454. package/src/components/rich-text/native/format-edit.js +44 -0
  455. package/src/components/rich-text/native/get-format-colors.native.js +54 -0
  456. package/src/components/rich-text/native/index.js +1 -0
  457. package/src/components/rich-text/native/index.native.js +1406 -0
  458. package/src/components/rich-text/native/style.native.scss +28 -0
  459. package/src/components/rich-text/native/test/__snapshots__/index.native.js.snap +79 -0
  460. package/src/components/rich-text/native/test/index.native.js +278 -0
  461. package/src/components/rich-text/native/test/performance/rich-text.native.js +44 -0
  462. package/src/components/rich-text/native/toolbar-button-with-options.native.js +61 -0
  463. package/src/components/rich-text/native/use-format-types.js +146 -0
  464. package/src/components/rich-text/use-input-rules.js +30 -2
  465. package/src/components/rich-text/use-paste-handler.js +1 -6
  466. package/src/components/spacing-sizes-control/input-controls/spacing-input-control.js +1 -1
  467. package/src/components/url-input/README.md +1 -74
  468. package/src/components/url-popover/image-url-input-ui.js +1 -0
  469. package/src/components/use-block-drop-zone/index.js +4 -1
  470. package/src/components/use-moving-animation/index.js +1 -1
  471. package/src/components/use-settings/index.js +2 -2
  472. package/src/components/use-settings/test/index.js +1 -1
  473. package/src/components/writing-flow/use-tab-nav.js +8 -3
  474. package/src/hooks/align.js +8 -8
  475. package/src/hooks/align.native.js +2 -2
  476. package/src/hooks/anchor.js +21 -23
  477. package/src/hooks/background.js +28 -6
  478. package/src/hooks/block-hooks.js +20 -16
  479. package/src/hooks/block-renaming.js +47 -0
  480. package/src/hooks/content-lock-ui.js +3 -3
  481. package/src/hooks/custom-class-name.js +7 -6
  482. package/src/hooks/custom-fields.js +73 -70
  483. package/src/hooks/duotone.js +23 -33
  484. package/src/hooks/index.js +0 -1
  485. package/src/hooks/layout.js +126 -113
  486. package/src/hooks/position.js +8 -21
  487. package/src/hooks/style.js +17 -31
  488. package/src/hooks/test/__snapshots__/align.native.js.snap +5 -5
  489. package/src/hooks/test/align.js +4 -4
  490. package/src/hooks/utils.js +33 -1
  491. package/src/layouts/constrained.js +57 -50
  492. package/src/private-apis.js +4 -1
  493. package/src/store/actions.js +26 -72
  494. package/src/store/reducer.js +19 -0
  495. package/src/store/selectors.js +15 -19
  496. package/src/style.scss +1 -1
  497. package/src/utils/test/transform-styles.js +49 -0
  498. package/src/utils/transform-styles/index.js +39 -13
  499. package/build/components/block-tools/selected-block-popover.js +0 -221
  500. package/build/components/block-tools/selected-block-popover.js.map +0 -1
  501. package/build/components/inserter/block-patterns-explorer/explorer.js.map +0 -1
  502. package/build/components/inserter/block-patterns-explorer/patterns-list.js.map +0 -1
  503. package/build/components/inserter/block-patterns-explorer/sidebar.js.map +0 -1
  504. package/build/components/inserter/block-patterns-filter.js.map +0 -1
  505. package/build/components/inserter/block-patterns-tab.js +0 -270
  506. package/build/components/inserter/block-patterns-tab.js.map +0 -1
  507. package/build/hooks/block-rename-ui.js +0 -165
  508. package/build/hooks/block-rename-ui.js.map +0 -1
  509. package/build/store/utils.js +0 -22
  510. package/build/store/utils.js.map +0 -1
  511. package/build-module/components/block-tools/selected-block-popover.js +0 -213
  512. package/build-module/components/block-tools/selected-block-popover.js.map +0 -1
  513. package/build-module/components/inserter/block-patterns-explorer/explorer.js.map +0 -1
  514. package/build-module/components/inserter/block-patterns-explorer/patterns-list.js.map +0 -1
  515. package/build-module/components/inserter/block-patterns-explorer/sidebar.js.map +0 -1
  516. package/build-module/components/inserter/block-patterns-filter.js.map +0 -1
  517. package/build-module/components/inserter/block-patterns-tab.js +0 -254
  518. package/build-module/components/inserter/block-patterns-tab.js.map +0 -1
  519. package/build-module/hooks/block-rename-ui.js +0 -157
  520. package/build-module/hooks/block-rename-ui.js.map +0 -1
  521. package/build-module/store/utils.js +0 -16
  522. package/build-module/store/utils.js.map +0 -1
  523. package/src/components/block-tools/selected-block-popover.js +0 -265
  524. package/src/components/inserter/block-patterns-tab.js +0 -448
  525. package/src/hooks/block-rename-ui.js +0 -226
  526. package/src/store/utils.js +0 -12
  527. /package/src/{hooks/block-rename-ui.scss → components/block-rename/style.scss} +0 -0
  528. /package/src/components/inserter/block-patterns-explorer/{sidebar.js → pattern-explorer-sidebar.js} +0 -0
@@ -0,0 +1,1406 @@
1
+ /* eslint no-console: ["error", { allow: ["warn"] }] */
2
+
3
+ /**
4
+ * External dependencies
5
+ */
6
+ import { View, Platform, Dimensions } from 'react-native';
7
+ import memize from 'memize';
8
+ import { colord } from 'colord';
9
+
10
+ /**
11
+ * WordPress dependencies
12
+ */
13
+ import RCTAztecView from '@wordpress/react-native-aztec';
14
+ import {
15
+ showUserSuggestions,
16
+ showXpostSuggestions,
17
+ } from '@wordpress/react-native-bridge';
18
+ import { BlockFormatControls, getPxFromCssUnit } from '@wordpress/block-editor';
19
+ import { Component } from '@wordpress/element';
20
+ import {
21
+ compose,
22
+ debounce,
23
+ withPreferredColorScheme,
24
+ } from '@wordpress/compose';
25
+ import { withSelect } from '@wordpress/data';
26
+ import { childrenBlock } from '@wordpress/blocks';
27
+ import { decodeEntities } from '@wordpress/html-entities';
28
+ import { BACKSPACE, DELETE, ENTER } from '@wordpress/keycodes';
29
+ import { isURL } from '@wordpress/url';
30
+ import { atSymbol, plus } from '@wordpress/icons';
31
+ import { __ } from '@wordpress/i18n';
32
+ import {
33
+ applyFormat,
34
+ getActiveFormat,
35
+ getActiveFormats,
36
+ insert,
37
+ getTextContent,
38
+ isEmpty,
39
+ create,
40
+ toHTMLString,
41
+ isCollapsed,
42
+ remove,
43
+ } from '@wordpress/rich-text';
44
+
45
+ /**
46
+ * Internal dependencies
47
+ */
48
+ import { useFormatTypes } from './use-format-types';
49
+ import FormatEdit from './format-edit';
50
+ import { getFormatColors } from './get-format-colors';
51
+ import styles from './style.scss';
52
+ import ToolbarButtonWithOptions from './toolbar-button-with-options';
53
+
54
+ const unescapeSpaces = ( text ) => {
55
+ return text.replace( / | /gi, ' ' );
56
+ };
57
+
58
+ // The flattened color palettes array is memoized to ensure that the same array instance is
59
+ // returned for the colors palettes. This value might be used as a prop, so having the same
60
+ // instance will prevent unnecessary re-renders of the RichText component.
61
+ const flatColorPalettes = memize( ( colorsPalettes ) => [
62
+ ...( colorsPalettes?.theme || [] ),
63
+ ...( colorsPalettes?.custom || [] ),
64
+ ...( colorsPalettes?.default || [] ),
65
+ ] );
66
+
67
+ const getSelectionColor = memize(
68
+ (
69
+ currentSelectionColor,
70
+ defaultSelectionColor,
71
+ baseGlobalStyles,
72
+ isBlockBasedTheme
73
+ ) => {
74
+ let selectionColor = defaultSelectionColor;
75
+ if ( currentSelectionColor ) {
76
+ selectionColor = currentSelectionColor;
77
+ }
78
+
79
+ if ( isBlockBasedTheme ) {
80
+ const colordTextColor = colord( selectionColor );
81
+ const colordBackgroundColor = colord(
82
+ baseGlobalStyles?.color?.background
83
+ );
84
+ const isColordTextReadable = colordTextColor.isReadable(
85
+ colordBackgroundColor
86
+ );
87
+ if ( ! isColordTextReadable ) {
88
+ selectionColor = baseGlobalStyles?.color?.text;
89
+ }
90
+ }
91
+
92
+ return selectionColor;
93
+ }
94
+ );
95
+
96
+ const gutenbergFormatNamesToAztec = {
97
+ 'core/bold': 'bold',
98
+ 'core/italic': 'italic',
99
+ 'core/strikethrough': 'strikethrough',
100
+ 'core/text-color': 'mark',
101
+ };
102
+
103
+ const EMPTY_PARAGRAPH_TAGS = '<p></p>';
104
+ const DEFAULT_FONT_SIZE = 16;
105
+ const MIN_LINE_HEIGHT = 1;
106
+
107
+ export class RichText extends Component {
108
+ constructor( {
109
+ value,
110
+ selectionStart,
111
+ selectionEnd,
112
+ __unstableMultilineTag: multiline,
113
+ } ) {
114
+ super( ...arguments );
115
+
116
+ this.isMultiline = false;
117
+ if ( multiline === true || multiline === 'p' || multiline === 'li' ) {
118
+ this.multilineTag = multiline === true ? 'p' : multiline;
119
+ this.isMultiline = true;
120
+ }
121
+
122
+ if ( this.multilineTag === 'li' ) {
123
+ this.multilineWrapperTags = [ 'ul', 'ol' ];
124
+ }
125
+
126
+ this.isIOS = Platform.OS === 'ios';
127
+ this.createRecord = this.createRecord.bind( this );
128
+ this.restoreParagraphTags = this.restoreParagraphTags.bind( this );
129
+ this.onChangeFromAztec = this.onChangeFromAztec.bind( this );
130
+ this.onKeyDown = this.onKeyDown.bind( this );
131
+ this.handleEnter = this.handleEnter.bind( this );
132
+ this.handleDelete = this.handleDelete.bind( this );
133
+ this.onPaste = this.onPaste.bind( this );
134
+ this.onFocus = this.onFocus.bind( this );
135
+ this.onBlur = this.onBlur.bind( this );
136
+ this.onTextUpdate = this.onTextUpdate.bind( this );
137
+ this.onContentSizeChange = this.onContentSizeChange.bind( this );
138
+ this.onFormatChange = this.onFormatChange.bind( this );
139
+ this.formatToValue = memize( this.formatToValue.bind( this ), {
140
+ maxSize: 1,
141
+ } );
142
+ this.debounceCreateUndoLevel = debounce( this.onCreateUndoLevel, 1000 );
143
+ // This prevents a bug in Aztec which triggers onSelectionChange twice on format change.
144
+ this.onSelectionChange = this.onSelectionChange.bind( this );
145
+ this.onSelectionChangeFromAztec =
146
+ this.onSelectionChangeFromAztec.bind( this );
147
+ this.valueToFormat = this.valueToFormat.bind( this );
148
+ this.getHtmlToRender = this.getHtmlToRender.bind( this );
149
+ this.handleSuggestionFunc = this.handleSuggestionFunc.bind( this );
150
+ this.handleUserSuggestion = this.handleSuggestionFunc(
151
+ showUserSuggestions,
152
+ '@'
153
+ ).bind( this );
154
+ this.handleXpostSuggestion = this.handleSuggestionFunc(
155
+ showXpostSuggestions,
156
+ '+'
157
+ ).bind( this );
158
+ this.suggestionOptions = this.suggestionOptions.bind( this );
159
+ this.insertString = this.insertString.bind( this );
160
+ this.manipulateEventCounterToForceNativeToRefresh =
161
+ this.manipulateEventCounterToForceNativeToRefresh.bind( this );
162
+ this.shouldDropEventFromAztec =
163
+ this.shouldDropEventFromAztec.bind( this );
164
+ this.state = {
165
+ activeFormats: [],
166
+ selectedFormat: null,
167
+ height: 0,
168
+ currentFontSize: this.getFontSize( arguments[ 0 ] ),
169
+ };
170
+ this.needsSelectionUpdate = false;
171
+ this.savedContent = '';
172
+ this.isTouched = false;
173
+ this.lastAztecEventType = null;
174
+
175
+ this.lastHistoryValue = value;
176
+
177
+ // Internal values that are update synchronously, unlike props.
178
+ this.value = value;
179
+ this.selectionStart = selectionStart;
180
+ this.selectionEnd = selectionEnd;
181
+ }
182
+
183
+ /**
184
+ * Get the current record (value and selection) from props and state.
185
+ *
186
+ * @return {Object} The current record (value and selection).
187
+ */
188
+ getRecord() {
189
+ const {
190
+ selectionStart: start,
191
+ selectionEnd: end,
192
+ colorPalette,
193
+ } = this.props;
194
+ const { value } = this.props;
195
+ const currentValue = this.formatToValue( value );
196
+
197
+ const { formats, replacements, text } = currentValue;
198
+ const { activeFormats } = this.state;
199
+ const newFormats = getFormatColors( value, formats, colorPalette );
200
+
201
+ return {
202
+ formats: newFormats,
203
+ replacements,
204
+ text,
205
+ start,
206
+ end,
207
+ activeFormats,
208
+ };
209
+ }
210
+
211
+ /**
212
+ * Creates a RichText value "record" from the current content and selection
213
+ * information
214
+ *
215
+ *
216
+ * @return {Object} A RichText value with formats and selection.
217
+ */
218
+ createRecord() {
219
+ const { preserveWhiteSpace } = this.props;
220
+ const value = {
221
+ start: this.selectionStart,
222
+ end: this.selectionEnd,
223
+ ...create( {
224
+ html: this.value,
225
+ range: null,
226
+ multilineTag: this.multilineTag,
227
+ multilineWrapperTags: this.multilineWrapperTags,
228
+ preserveWhiteSpace,
229
+ } ),
230
+ };
231
+ const start = Math.min( this.selectionStart, value.text.length );
232
+ const end = Math.min( this.selectionEnd, value.text.length );
233
+ return { ...value, start, end };
234
+ }
235
+
236
+ valueToFormat( value ) {
237
+ // Remove the outer root tags.
238
+ return this.removeRootTagsProducedByAztec(
239
+ toHTMLString( {
240
+ value,
241
+ multilineTag: this.multilineTag,
242
+ } )
243
+ );
244
+ }
245
+
246
+ getActiveFormatNames( record ) {
247
+ const { formatTypes } = this.props;
248
+
249
+ return formatTypes
250
+ .map( ( { name } ) => name )
251
+ .filter( ( name ) => {
252
+ return getActiveFormat( record, name ) !== undefined;
253
+ } )
254
+ .map( ( name ) => gutenbergFormatNamesToAztec[ name ] )
255
+ .filter( Boolean );
256
+ }
257
+
258
+ onFormatChange( record ) {
259
+ const { start = 0, end = 0, activeFormats = [] } = record;
260
+ const changeHandlers = Object.fromEntries(
261
+ Object.entries( this.props ).filter( ( [ key ] ) =>
262
+ key.startsWith( 'format_on_change_functions_' )
263
+ )
264
+ );
265
+
266
+ Object.values( changeHandlers ).forEach( ( changeHandler ) => {
267
+ changeHandler( record.formats, record.text );
268
+ } );
269
+
270
+ this.value = this.valueToFormat( record );
271
+ this.props.onChange( this.value );
272
+ this.setState( { activeFormats } );
273
+ this.props.onSelectionChange( start, end );
274
+ this.selectionStart = start;
275
+ this.selectionEnd = end;
276
+
277
+ this.onCreateUndoLevel();
278
+
279
+ this.lastAztecEventType = 'format change';
280
+ }
281
+
282
+ insertString( record, string ) {
283
+ if ( record && string ) {
284
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
285
+ const toInsert = insert( record, string );
286
+ this.onFormatChange( toInsert );
287
+ }
288
+ }
289
+
290
+ onCreateUndoLevel() {
291
+ const { __unstableOnCreateUndoLevel: onCreateUndoLevel } = this.props;
292
+ // If the content is the same, no level needs to be created.
293
+ if ( this.lastHistoryValue === this.value ) {
294
+ return;
295
+ }
296
+
297
+ onCreateUndoLevel();
298
+ this.lastHistoryValue = this.value;
299
+ }
300
+
301
+ /*
302
+ * Cleans up any root tags produced by aztec.
303
+ * TODO: This should be removed on a later version when aztec doesn't return the top tag of the text being edited
304
+ */
305
+ removeRootTagsProducedByAztec( html ) {
306
+ let result = this.removeRootTag( this.props.tagName, html );
307
+
308
+ if ( this.props.tagsToEliminate ) {
309
+ this.props.tagsToEliminate.forEach( ( element ) => {
310
+ result = this.removeTag( element, result );
311
+ } );
312
+ }
313
+
314
+ return result;
315
+ }
316
+
317
+ removeRootTag( tag, html ) {
318
+ const openingTagRegexp = RegExp( '^<' + tag + '[^>]*>', 'gim' );
319
+ const closingTagRegexp = RegExp( '</' + tag + '>$', 'gim' );
320
+
321
+ return html
322
+ .replace( openingTagRegexp, '' )
323
+ .replace( closingTagRegexp, '' );
324
+ }
325
+
326
+ removeTag( tag, html ) {
327
+ const openingTagRegexp = RegExp( '<' + tag + '>', 'gim' );
328
+ const closingTagRegexp = RegExp( '</' + tag + '>', 'gim' );
329
+ return html
330
+ .replace( openingTagRegexp, '' )
331
+ .replace( closingTagRegexp, '' );
332
+ }
333
+
334
+ /*
335
+ * Handles any case where the content of the AztecRN instance has changed
336
+ */
337
+ onChangeFromAztec( event ) {
338
+ if ( this.shouldDropEventFromAztec( event, 'onChange' ) ) {
339
+ return;
340
+ }
341
+
342
+ const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
343
+ unescapeSpaces( event.nativeEvent.text )
344
+ );
345
+ // On iOS, onChange can be triggered after selection changes, even though there are no content changes.
346
+ if ( contentWithoutRootTag === this.value ) {
347
+ return;
348
+ }
349
+ this.lastEventCount = event.nativeEvent.eventCount;
350
+ this.comesFromAztec = true;
351
+ this.firedAfterTextChanged = true; // The onChange event always fires after the fact.
352
+ this.onTextUpdate( event );
353
+ this.lastAztecEventType = 'input';
354
+ }
355
+
356
+ onTextUpdate( event ) {
357
+ const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
358
+ unescapeSpaces( event.nativeEvent.text )
359
+ );
360
+ let formattedContent = contentWithoutRootTag;
361
+ if ( ! this.isIOS ) {
362
+ formattedContent = this.restoreParagraphTags(
363
+ contentWithoutRootTag,
364
+ this.multilineTag
365
+ );
366
+ }
367
+
368
+ this.debounceCreateUndoLevel();
369
+ const refresh = this.value !== formattedContent;
370
+ this.value = formattedContent;
371
+
372
+ // We don't want to refresh if our goal is just to create a record.
373
+ if ( refresh ) {
374
+ this.props.onChange( formattedContent );
375
+ }
376
+ }
377
+
378
+ restoreParagraphTags( value, tag ) {
379
+ if ( tag === 'p' && ( ! value || ! value.startsWith( '<p>' ) ) ) {
380
+ return '<p>' + value + '</p>';
381
+ }
382
+ return value;
383
+ }
384
+
385
+ /*
386
+ * Handles any case where the content of the AztecRN instance has changed in size
387
+ */
388
+ onContentSizeChange( contentSize ) {
389
+ this.setState( contentSize );
390
+ this.lastAztecEventType = 'content size change';
391
+ }
392
+
393
+ onKeyDown( event ) {
394
+ if ( event.defaultPrevented ) {
395
+ return;
396
+ }
397
+
398
+ // Add stubs for conformance in downstream autocompleters logic.
399
+ this.customEditableOnKeyDown?.( {
400
+ preventDefault: () => undefined,
401
+ ...event,
402
+ key: RCTAztecView.KeyCodes[ event?.keyCode ],
403
+ } );
404
+
405
+ this.handleDelete( event );
406
+ this.handleEnter( event );
407
+ this.handleTriggerKeyCodes( event );
408
+ }
409
+
410
+ handleEnter( event ) {
411
+ if ( event.keyCode !== ENTER ) {
412
+ return;
413
+ }
414
+ const { onEnter } = this.props;
415
+
416
+ if ( ! onEnter ) {
417
+ return;
418
+ }
419
+
420
+ onEnter( {
421
+ value: this.createRecord(),
422
+ onChange: this.onFormatChange,
423
+ shiftKey: event.shiftKey,
424
+ } );
425
+ this.lastAztecEventType = 'input';
426
+ }
427
+
428
+ handleDelete( event ) {
429
+ if ( this.shouldDropEventFromAztec( event, 'handleDelete' ) ) {
430
+ return;
431
+ }
432
+
433
+ const { keyCode } = event;
434
+
435
+ if ( keyCode !== DELETE && keyCode !== BACKSPACE ) {
436
+ return;
437
+ }
438
+ const isReverse = keyCode === BACKSPACE;
439
+
440
+ const { onDelete } = this.props;
441
+ this.lastEventCount = event.nativeEvent.eventCount;
442
+ this.comesFromAztec = true;
443
+ this.firedAfterTextChanged = event.nativeEvent.firedAfterTextChanged;
444
+ const value = this.createRecord();
445
+ const { start, end, text } = value;
446
+ let newValue;
447
+
448
+ // Always handle full content deletion ourselves.
449
+ if ( start === 0 && end !== 0 && end >= text.length ) {
450
+ newValue = remove( value );
451
+ this.onFormatChange( newValue );
452
+ event.preventDefault();
453
+ return;
454
+ }
455
+
456
+ // Only process delete if the key press occurs at an uncollapsed edge.
457
+ if (
458
+ ! onDelete ||
459
+ ! isCollapsed( value ) ||
460
+ ( isReverse && start !== 0 ) ||
461
+ ( ! isReverse && end !== text.length )
462
+ ) {
463
+ return;
464
+ }
465
+
466
+ onDelete( { isReverse, value } );
467
+
468
+ event.preventDefault();
469
+ this.lastAztecEventType = 'input';
470
+ }
471
+
472
+ handleTriggerKeyCodes( event ) {
473
+ const { keyCode } = event;
474
+ const triggeredOption = this.suggestionOptions().find( ( option ) => {
475
+ const triggeredKeyCode = option.triggerChar.charCodeAt( 0 );
476
+ return triggeredKeyCode === keyCode;
477
+ } );
478
+
479
+ if ( triggeredOption ) {
480
+ const record = this.getRecord();
481
+ const text = getTextContent( record );
482
+ // Only respond to the trigger if the selection is on the start of text or line
483
+ // or if the character before is a space.
484
+ const useTrigger =
485
+ text.length === 0 ||
486
+ record.start === 0 ||
487
+ text.charAt( record.start - 1 ) === '\n' ||
488
+ text.charAt( record.start - 1 ) === ' ';
489
+
490
+ if ( useTrigger && triggeredOption.onClick ) {
491
+ triggeredOption.onClick();
492
+ } else {
493
+ this.insertString( record, triggeredOption.triggerChar );
494
+ }
495
+ }
496
+ }
497
+
498
+ suggestionOptions() {
499
+ const { areMentionsSupported, areXPostsSupported } = this.props;
500
+ const allOptions = [
501
+ {
502
+ supported: areMentionsSupported,
503
+ title: __( 'Insert mention' ),
504
+ onClick: this.handleUserSuggestion,
505
+ triggerChar: '@',
506
+ value: 'mention',
507
+ label: __( 'Mention' ),
508
+ icon: atSymbol,
509
+ },
510
+ {
511
+ supported: areXPostsSupported,
512
+ title: __( 'Insert crosspost' ),
513
+ onClick: this.handleXpostSuggestion,
514
+ triggerChar: '+',
515
+ value: 'crosspost',
516
+ label: __( 'Crosspost' ),
517
+ icon: plus,
518
+ },
519
+ ];
520
+ return allOptions.filter( ( op ) => op.supported );
521
+ }
522
+
523
+ handleSuggestionFunc( suggestionFunction, prefix ) {
524
+ return () => {
525
+ const record = this.getRecord();
526
+ suggestionFunction()
527
+ .then( ( suggestion ) => {
528
+ this.insertString( record, `${ prefix }${ suggestion } ` );
529
+ } )
530
+ .catch( () => {} );
531
+ };
532
+ }
533
+
534
+ /**
535
+ * Handles a paste event from the native Aztec Wrapper.
536
+ *
537
+ * @param {Object} event The paste event which wraps `nativeEvent`.
538
+ */
539
+ onPaste( event ) {
540
+ const { onPaste, onChange } = this.props;
541
+ const { activeFormats = [] } = this.state;
542
+
543
+ const { pastedText, pastedHtml, files } = event.nativeEvent;
544
+ const currentRecord = this.createRecord();
545
+
546
+ event.preventDefault();
547
+
548
+ // There is a selection, check if a URL is pasted.
549
+ if ( ! isCollapsed( currentRecord ) ) {
550
+ const trimmedText = ( pastedHtml || pastedText )
551
+ .replace( /<[^>]+>/g, '' )
552
+ .trim();
553
+
554
+ // A URL was pasted, turn the selection into a link.
555
+ if ( isURL( trimmedText ) ) {
556
+ const linkedRecord = applyFormat( currentRecord, {
557
+ type: 'a',
558
+ attributes: {
559
+ href: decodeEntities( trimmedText ),
560
+ },
561
+ } );
562
+ this.value = this.valueToFormat( linkedRecord );
563
+ onChange( this.value );
564
+
565
+ // Allows us to ask for this information when we get a report.
566
+ window.console.log( 'Created link:\n\n', trimmedText );
567
+
568
+ return;
569
+ }
570
+ }
571
+
572
+ if ( onPaste ) {
573
+ onPaste( {
574
+ value: currentRecord,
575
+ onChange: this.onFormatChange,
576
+ html: pastedHtml,
577
+ plainText: pastedText,
578
+ files,
579
+ activeFormats,
580
+ } );
581
+ }
582
+ }
583
+
584
+ onFocus() {
585
+ this.isTouched = true;
586
+
587
+ const { unstableOnFocus, onSelectionChange } = this.props;
588
+
589
+ if ( unstableOnFocus ) {
590
+ unstableOnFocus();
591
+ }
592
+
593
+ // We know for certain that on focus, the old selection is invalid. It
594
+ // will be recalculated on `selectionchange`.
595
+
596
+ onSelectionChange( this.selectionStart, this.selectionEnd );
597
+
598
+ this.lastAztecEventType = 'focus';
599
+ }
600
+
601
+ onBlur( event ) {
602
+ this.isTouched = false;
603
+
604
+ // Check if value is up to date with latest state of native AztecView.
605
+ if (
606
+ event.nativeEvent.text &&
607
+ event.nativeEvent.text !== this.props.value
608
+ ) {
609
+ this.onTextUpdate( event );
610
+ }
611
+
612
+ if ( this.props.onBlur ) {
613
+ this.props.onBlur( event );
614
+ }
615
+
616
+ this.lastAztecEventType = 'blur';
617
+ }
618
+
619
+ onSelectionChange( start, end ) {
620
+ const hasChanged =
621
+ this.selectionStart !== start || this.selectionEnd !== end;
622
+
623
+ this.selectionStart = start;
624
+ this.selectionEnd = end;
625
+
626
+ // This is a manual selection change event if onChange was not triggered just before
627
+ // and we did not just trigger a text update
628
+ // `onChange` could be the last event and could have been triggered a long time ago so
629
+ // this approach is not perfectly reliable.
630
+ const isManual =
631
+ this.lastAztecEventType !== 'input' &&
632
+ this.props.value === this.value;
633
+ if ( hasChanged && isManual ) {
634
+ const value = this.createRecord();
635
+ const activeFormats = getActiveFormats( value );
636
+ this.setState( { activeFormats } );
637
+ }
638
+
639
+ this.props.onSelectionChange( start, end );
640
+ }
641
+
642
+ shouldDropEventFromAztec( event, logText ) {
643
+ const shouldDrop =
644
+ ! this.isIOS && event.nativeEvent.eventCount <= this.lastEventCount;
645
+ if ( shouldDrop ) {
646
+ window.console.log(
647
+ `Dropping ${ logText } from Aztec as its event counter is older than latest sent to the native side. Got ${ event.nativeEvent.eventCount } but lastEventCount is ${ this.lastEventCount }.`
648
+ );
649
+ }
650
+ return shouldDrop;
651
+ }
652
+
653
+ /**
654
+ * Determines whether the text input should receive focus after an update.
655
+ * For cases where a RichText with a value is merged with an empty one.
656
+ *
657
+ * @param {Object} prevProps - The previous props of the component.
658
+ * @return {boolean} True if the text input should receive focus, false otherwise.
659
+ */
660
+ shouldFocusTextInputAfterMerge( prevProps ) {
661
+ const {
662
+ __unstableIsSelected: isSelected,
663
+ blockIsSelected,
664
+ selectionStart,
665
+ selectionEnd,
666
+ __unstableMobileNoFocusOnMount,
667
+ } = this.props;
668
+
669
+ const {
670
+ __unstableIsSelected: prevIsSelected,
671
+ blockIsSelected: prevBlockIsSelected,
672
+ } = prevProps;
673
+
674
+ const noSelectionValues =
675
+ selectionStart === undefined && selectionEnd === undefined;
676
+ const textInputWasNotFocused = ! prevIsSelected && ! isSelected;
677
+
678
+ return (
679
+ ! __unstableMobileNoFocusOnMount &&
680
+ noSelectionValues &&
681
+ textInputWasNotFocused &&
682
+ ! prevBlockIsSelected &&
683
+ blockIsSelected
684
+ );
685
+ }
686
+
687
+ onSelectionChangeFromAztec( start, end, text, event ) {
688
+ if ( this.shouldDropEventFromAztec( event, 'onSelectionChange' ) ) {
689
+ return;
690
+ }
691
+
692
+ // `end` can be less than `start` on iOS
693
+ // Let's fix that here so `rich-text/slice` can work properly.
694
+ const realStart = Math.min( start, end );
695
+ const realEnd = Math.max( start, end );
696
+
697
+ // Check and dicsard stray event, where the text and selection is equal to the ones already cached.
698
+ const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
699
+ unescapeSpaces( event.nativeEvent.text )
700
+ );
701
+ if (
702
+ contentWithoutRootTag === this.value &&
703
+ realStart === this.selectionStart &&
704
+ realEnd === this.selectionEnd
705
+ ) {
706
+ return;
707
+ }
708
+
709
+ this.comesFromAztec = true;
710
+ this.firedAfterTextChanged = true; // Selection change event always fires after the fact.
711
+
712
+ // Update text before updating selection
713
+ // Make sure there are changes made to the content before upgrading it upward.
714
+ this.onTextUpdate( event );
715
+
716
+ // Aztec can send us selection change events after it has lost focus.
717
+ // For instance the autocorrect feature will complete a partially written
718
+ // word when resigning focus, causing a selection change event.
719
+ // Forwarding this selection change could cause this RichText to regain
720
+ // focus and start a focus loop.
721
+ //
722
+ // See https://github.com/wordpress-mobile/gutenberg-mobile/issues/1696
723
+ if ( this.props.__unstableIsSelected ) {
724
+ this.onSelectionChange( realStart, realEnd );
725
+ }
726
+ // Update lastEventCount to prevent Aztec from re-rendering the content it just sent.
727
+ this.lastEventCount = event.nativeEvent.eventCount;
728
+
729
+ this.lastAztecEventType = 'selection change';
730
+ }
731
+
732
+ isEmpty() {
733
+ return isEmpty( this.formatToValue( this.props.value ) );
734
+ }
735
+
736
+ formatToValue( value ) {
737
+ const { preserveWhiteSpace } = this.props;
738
+ // Handle deprecated `children` and `node` sources.
739
+ if ( Array.isArray( value ) ) {
740
+ return create( {
741
+ html: childrenBlock.toHTML( value ),
742
+ multilineTag: this.multilineTag,
743
+ multilineWrapperTags: this.multilineWrapperTags,
744
+ preserveWhiteSpace,
745
+ } );
746
+ }
747
+
748
+ if ( this.props.format === 'string' ) {
749
+ return create( {
750
+ html: value,
751
+ multilineTag: this.multilineTag,
752
+ multilineWrapperTags: this.multilineWrapperTags,
753
+ preserveWhiteSpace,
754
+ } );
755
+ }
756
+
757
+ // Guard for blocks passing `null` in onSplit callbacks. May be removed
758
+ // if onSplit is revised to not pass a `null` value.
759
+ if ( value === null ) {
760
+ return create();
761
+ }
762
+
763
+ return value;
764
+ }
765
+
766
+ manipulateEventCounterToForceNativeToRefresh() {
767
+ if ( this.isIOS ) {
768
+ this.lastEventCount = undefined;
769
+ return;
770
+ }
771
+
772
+ if ( typeof this.lastEventCount !== 'undefined' ) {
773
+ this.lastEventCount += 100; // bump by a hundred, hopefully native hasn't bombarded the JS side in the meantime.
774
+ } // no need to bump when 'undefined' as native side won't receive the key when the value is undefined, and that will cause force updating anyway,
775
+ // see https://github.com/WordPress/gutenberg/blob/82e578dcc75e67891c750a41a04c1e31994192fc/packages/react-native-aztec/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java#L213-L215
776
+ }
777
+
778
+ shouldComponentUpdate( nextProps, nextState ) {
779
+ if (
780
+ nextProps.tagName !== this.props.tagName ||
781
+ nextProps.reversed !== this.props.reversed ||
782
+ nextProps.start !== this.props.start
783
+ ) {
784
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
785
+ this.value = undefined;
786
+ return true;
787
+ }
788
+
789
+ // TODO: Please re-introduce the check to avoid updating the content right after an `onChange` call.
790
+ // It was removed in https://github.com/WordPress/gutenberg/pull/12417 to fix undo/redo problem.
791
+
792
+ // If the component is changed React side (undo/redo/merging/splitting/custom text actions)
793
+ // we need to make sure the native is updated as well.
794
+
795
+ // Also, don't trust the "this.lastContent" as on Android, incomplete text events arrive
796
+ // with only some of the text, while the virtual keyboard's suggestion system does its magic.
797
+ // ** compare with this.lastContent for optimizing performance by not forcing Aztec with text it already has
798
+ // , but compare with props.value to not lose "half word" text because of Android virtual keyb autosuggestion behavior
799
+ if (
800
+ typeof nextProps.value !== 'undefined' &&
801
+ typeof this.props.value !== 'undefined' &&
802
+ ( ! this.comesFromAztec || ! this.firedAfterTextChanged ) &&
803
+ nextProps.value !== this.props.value
804
+ ) {
805
+ // Gutenberg seems to try to mirror the caret state even on events that only change the content so,
806
+ // let's force caret update if state has selection set.
807
+ if (
808
+ typeof nextProps.selectionStart !== 'undefined' &&
809
+ typeof nextProps.selectionEnd !== 'undefined'
810
+ ) {
811
+ this.needsSelectionUpdate = true;
812
+ }
813
+
814
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
815
+ }
816
+
817
+ if ( ! this.comesFromAztec ) {
818
+ if (
819
+ typeof nextProps.selectionStart !== 'undefined' &&
820
+ typeof nextProps.selectionEnd !== 'undefined' &&
821
+ nextProps.selectionStart !== this.props.selectionStart &&
822
+ nextProps.selectionStart !== this.selectionStart &&
823
+ nextProps.__unstableIsSelected
824
+ ) {
825
+ this.needsSelectionUpdate = true;
826
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
827
+ }
828
+
829
+ // For font size changes from a prop value a force refresh
830
+ // is needed without the selection update.
831
+ if ( nextProps?.fontSize !== this.props?.fontSize ) {
832
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
833
+ }
834
+
835
+ if (
836
+ ( nextProps?.style?.fontSize !== this.props?.style?.fontSize &&
837
+ nextState.currentFontSize !==
838
+ this.state.currentFontSize ) ||
839
+ nextState.currentFontSize !== this.state.currentFontSize ||
840
+ nextProps?.style?.lineHeight !== this.props?.style?.lineHeight
841
+ ) {
842
+ this.needsSelectionUpdate = true;
843
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
844
+ }
845
+ }
846
+
847
+ return true;
848
+ }
849
+
850
+ componentDidMount() {
851
+ // Request focus if wrapping block is selected and parent hasn't inhibited the focus request. This method of focusing
852
+ // is trying to implement the web-side counterpart of BlockList's `focusTabbable` where the BlockList is focusing an
853
+ // inputbox by searching the DOM. We don't have the DOM in RN so, using the combination of blockIsSelected and __unstableMobileNoFocusOnMount
854
+ // to determine if we should focus the RichText.
855
+ if (
856
+ this.props.blockIsSelected &&
857
+ ! this.props.__unstableMobileNoFocusOnMount
858
+ ) {
859
+ this._editor.focus();
860
+ this.onSelectionChange(
861
+ this.props.selectionStart || 0,
862
+ this.props.selectionEnd || 0
863
+ );
864
+ }
865
+ }
866
+
867
+ componentWillUnmount() {
868
+ if ( this._editor.isFocused() ) {
869
+ this._editor.blur();
870
+ }
871
+ }
872
+
873
+ componentDidUpdate( prevProps ) {
874
+ const { style, tagName } = this.props;
875
+ const { currentFontSize } = this.state;
876
+
877
+ if ( this.props.value !== this.value ) {
878
+ this.value = this.props.value;
879
+ }
880
+ const { __unstableIsSelected: prevIsSelected } = prevProps;
881
+ const { __unstableIsSelected: isSelected } = this.props;
882
+
883
+ if ( isSelected && ! prevIsSelected ) {
884
+ this._editor.focus();
885
+ // Update selection props explicitly when component is selected as Aztec won't call onSelectionChange
886
+ // if its internal value hasn't change. When created, default value is 0, 0.
887
+ this.onSelectionChange(
888
+ this.props.selectionStart || 0,
889
+ this.props.selectionEnd || 0
890
+ );
891
+ } else if ( this.shouldFocusTextInputAfterMerge( prevProps ) ) {
892
+ // Since this is happening when merging blocks, the selection should be at the last character position.
893
+ // As a fallback the internal selectionEnd value is used.
894
+ const lastCharacterPosition =
895
+ this.value?.length ?? this.selectionEnd;
896
+ this._editor.focus();
897
+ this.props.onSelectionChange(
898
+ lastCharacterPosition,
899
+ lastCharacterPosition
900
+ );
901
+ } else if ( ! isSelected && prevIsSelected ) {
902
+ this._editor.blur();
903
+ }
904
+
905
+ // For font size values changes from the font size picker
906
+ // we compare previous values to refresh the selected font size,
907
+ // this is also used when the tag name changes
908
+ // e.g Heading block and a level change like h1->h2.
909
+ const currentFontSizeStyle = this.getParsedFontSize( style?.fontSize );
910
+ const prevFontSizeStyle = this.getParsedFontSize(
911
+ prevProps?.style?.fontSize
912
+ );
913
+ const isDifferentTag = prevProps.tagName !== tagName;
914
+ if (
915
+ ( currentFontSize &&
916
+ ( currentFontSizeStyle || prevFontSizeStyle ) &&
917
+ currentFontSizeStyle !== currentFontSize ) ||
918
+ isDifferentTag
919
+ ) {
920
+ this.setState( {
921
+ currentFontSize: this.getFontSize( this.props ),
922
+ } );
923
+ }
924
+ }
925
+
926
+ getHtmlToRender( record, tagName ) {
927
+ // Save back to HTML from React tree.
928
+ let value = this.valueToFormat( record );
929
+
930
+ if ( value === undefined ) {
931
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
932
+ value = '';
933
+ }
934
+ // On android if content is empty we need to send no content or else the placeholder will not show.
935
+ if (
936
+ ! this.isIOS &&
937
+ ( value === '' || value === EMPTY_PARAGRAPH_TAGS )
938
+ ) {
939
+ return '';
940
+ }
941
+
942
+ if ( tagName ) {
943
+ let extraAttributes = ``;
944
+ if ( tagName === `ol` ) {
945
+ if ( this.props.reversed ) {
946
+ extraAttributes += ` reversed`;
947
+ }
948
+ if ( this.props.start ) {
949
+ extraAttributes += ` start=${ this.props.start }`;
950
+ }
951
+ }
952
+ value = `<${ tagName }${ extraAttributes }>${ value }</${ tagName }>`;
953
+ }
954
+ return value;
955
+ }
956
+
957
+ getEditableProps() {
958
+ return {
959
+ // Overridable props.
960
+ style: {},
961
+ className: 'rich-text',
962
+ onKeyDown: () => null,
963
+ };
964
+ }
965
+
966
+ getParsedFontSize( fontSize ) {
967
+ const { height, width } = Dimensions.get( 'window' );
968
+ const cssUnitOptions = { height, width, fontSize: DEFAULT_FONT_SIZE };
969
+
970
+ if ( ! fontSize ) {
971
+ return fontSize;
972
+ }
973
+
974
+ const selectedPxValue =
975
+ getPxFromCssUnit( fontSize, cssUnitOptions ) ?? DEFAULT_FONT_SIZE;
976
+
977
+ return parseFloat( selectedPxValue );
978
+ }
979
+
980
+ getFontSize( props ) {
981
+ const { baseGlobalStyles, tagName, fontSize, style } = props;
982
+ const tagNameFontSize =
983
+ baseGlobalStyles?.elements?.[ tagName ]?.typography?.fontSize;
984
+
985
+ let newFontSize = DEFAULT_FONT_SIZE;
986
+
987
+ // Disables line-height rendering for pre elements until we fix some issues with AztecAndroid.
988
+ if ( tagName === 'pre' && ! this.isIOS ) {
989
+ return undefined;
990
+ }
991
+
992
+ // For block-based themes, get the default editor font size.
993
+ if ( baseGlobalStyles?.typography?.fontSize && tagName === 'p' ) {
994
+ newFontSize = baseGlobalStyles?.typography?.fontSize;
995
+ }
996
+
997
+ // For block-based themes, get the default element font size
998
+ // e.g h1, h2.
999
+ if ( tagNameFontSize ) {
1000
+ newFontSize = tagNameFontSize;
1001
+ }
1002
+
1003
+ // For font size values provided from the styles,
1004
+ // usually from values set from the font size picker.
1005
+ if ( style?.fontSize ) {
1006
+ newFontSize = style.fontSize;
1007
+ }
1008
+
1009
+ // Fall-back to a font size provided from its props (if there's any)
1010
+ // and there are no other default values to use.
1011
+ if ( fontSize && ! tagNameFontSize && ! style?.fontSize ) {
1012
+ newFontSize = fontSize;
1013
+ }
1014
+
1015
+ // We need to always convert to px units because the selected value
1016
+ // could be coming from the web where it could be stored as a different unit.
1017
+ const selectedPxValue = this.getParsedFontSize( newFontSize );
1018
+
1019
+ return selectedPxValue;
1020
+ }
1021
+
1022
+ getLineHeight() {
1023
+ const { baseGlobalStyles, tagName, lineHeight, style } = this.props;
1024
+ const tagNameLineHeight =
1025
+ baseGlobalStyles?.elements?.[ tagName ]?.typography?.lineHeight;
1026
+ let newLineHeight;
1027
+
1028
+ // Disables line-height rendering for pre elements until we fix some issues with AztecAndroid.
1029
+ if ( tagName === 'pre' && ! this.isIOS ) {
1030
+ return undefined;
1031
+ }
1032
+
1033
+ if ( ! this.getIsBlockBasedTheme() ) {
1034
+ return;
1035
+ }
1036
+
1037
+ // For block-based themes, get the default editor line height.
1038
+ if ( baseGlobalStyles?.typography?.lineHeight && tagName === 'p' ) {
1039
+ newLineHeight = parseFloat(
1040
+ baseGlobalStyles?.typography?.lineHeight
1041
+ );
1042
+ }
1043
+
1044
+ // For block-based themes, get the default element line height
1045
+ // e.g h1, h2.
1046
+ if ( tagNameLineHeight ) {
1047
+ newLineHeight = parseFloat( tagNameLineHeight );
1048
+ }
1049
+
1050
+ // For line height values provided from the styles,
1051
+ // usually from values set from the line height picker.
1052
+ if ( style?.lineHeight ) {
1053
+ newLineHeight = parseFloat( style.lineHeight );
1054
+ }
1055
+
1056
+ // Fall-back to a line height provided from its props (if there's any)
1057
+ // and there are no other default values to use.
1058
+ if ( lineHeight && ! tagNameLineHeight && ! style?.lineHeight ) {
1059
+ newLineHeight = lineHeight;
1060
+ }
1061
+
1062
+ // Check the final value is not over the minimum supported value.
1063
+ if ( newLineHeight && newLineHeight < MIN_LINE_HEIGHT ) {
1064
+ newLineHeight = MIN_LINE_HEIGHT;
1065
+ }
1066
+
1067
+ // Until we parse CSS values correctly, avoid passing NaN values to Aztec
1068
+ if ( isNaN( newLineHeight ) ) {
1069
+ return undefined;
1070
+ }
1071
+
1072
+ return newLineHeight;
1073
+ }
1074
+
1075
+ getIsBlockBasedTheme() {
1076
+ const { baseGlobalStyles } = this.props;
1077
+
1078
+ return (
1079
+ baseGlobalStyles && Object.entries( baseGlobalStyles ).length !== 0
1080
+ );
1081
+ }
1082
+
1083
+ getBlockUseDefaultFont() {
1084
+ // For block-based themes it enables using the defaultFont
1085
+ // in Aztec for iOS so it allows customizing the font size
1086
+ // for the Preformatted/Code and Heading blocks.
1087
+ if ( ! this.isIOS ) {
1088
+ return;
1089
+ }
1090
+
1091
+ const { tagName } = this.props;
1092
+ const isBlockBasedTheme = this.getIsBlockBasedTheme();
1093
+ const tagsToMatch = /pre|h([1-6])$/gm;
1094
+
1095
+ return isBlockBasedTheme && tagsToMatch.test( tagName );
1096
+ }
1097
+
1098
+ getLinkTextColor( defaultColor ) {
1099
+ const { style } = this.props;
1100
+ const customColor = style?.linkColor && colord( style.linkColor );
1101
+
1102
+ return customColor && customColor.isValid()
1103
+ ? customColor.toHex()
1104
+ : defaultColor;
1105
+ }
1106
+
1107
+ getPlaceholderTextColor() {
1108
+ const {
1109
+ baseGlobalStyles,
1110
+ getStylesFromColorScheme,
1111
+ placeholderTextColor,
1112
+ style,
1113
+ } = this.props;
1114
+
1115
+ // Default placeholder text color.
1116
+ const placeholderStyle = getStylesFromColorScheme(
1117
+ styles.richTextPlaceholder,
1118
+ styles.richTextPlaceholderDark
1119
+ );
1120
+ const { color: defaultPlaceholderTextColor } = placeholderStyle;
1121
+ // Custom 63% opacity for theme and inherited colors.
1122
+ const placeholderOpacity = 'A1';
1123
+
1124
+ // Determine inherited placeholder color if available.
1125
+ const inheritPlaceholderColor = style?.placeholderColor
1126
+ ? `${ style.placeholderColor }${ placeholderOpacity }`
1127
+ : undefined;
1128
+
1129
+ // If using block-based themes, derive the placeholder color from global styles.
1130
+ const globalStylesPlaceholderColor = baseGlobalStyles?.color?.text
1131
+ ? `${ baseGlobalStyles.color.text }${ placeholderOpacity }`
1132
+ : undefined;
1133
+
1134
+ return (
1135
+ inheritPlaceholderColor ??
1136
+ placeholderTextColor ??
1137
+ globalStylesPlaceholderColor ??
1138
+ defaultPlaceholderTextColor
1139
+ );
1140
+ }
1141
+
1142
+ render() {
1143
+ const {
1144
+ tagName,
1145
+ style,
1146
+ __unstableIsSelected: isSelected,
1147
+ children,
1148
+ getStylesFromColorScheme,
1149
+ minWidth,
1150
+ maxWidth,
1151
+ formatTypes,
1152
+ parentBlockStyles,
1153
+ accessibilityLabel,
1154
+ disableEditingMenu = false,
1155
+ baseGlobalStyles,
1156
+ selectionStart,
1157
+ selectionEnd,
1158
+ disableSuggestions,
1159
+ containerWidth,
1160
+ } = this.props;
1161
+ const { currentFontSize } = this.state;
1162
+
1163
+ const record = this.getRecord();
1164
+ const html = this.getHtmlToRender( record, tagName );
1165
+ const editableProps = this.getEditableProps();
1166
+ const blockUseDefaultFont = this.getBlockUseDefaultFont();
1167
+
1168
+ const fontSize = currentFontSize;
1169
+ const lineHeight = this.getLineHeight();
1170
+
1171
+ const {
1172
+ color: defaultColor,
1173
+ textDecorationColor: defaultTextDecorationColor,
1174
+ fontFamily: defaultFontFamily,
1175
+ } = getStylesFromColorScheme( styles.richText, styles.richTextDark );
1176
+ const linkTextColor = this.getLinkTextColor(
1177
+ defaultTextDecorationColor
1178
+ );
1179
+
1180
+ const currentSelectionStart = selectionStart ?? 0;
1181
+ const currentSelectionEnd = selectionEnd ?? 0;
1182
+ let selection = null;
1183
+ if ( this.needsSelectionUpdate ) {
1184
+ this.needsSelectionUpdate = false;
1185
+ selection = {
1186
+ start: currentSelectionStart,
1187
+ end: currentSelectionEnd,
1188
+ };
1189
+
1190
+ // On AztecAndroid, setting the caret to an out-of-bounds position will crash the editor so, let's check for some cases.
1191
+ if ( ! this.isIOS ) {
1192
+ // The following regular expression is used in Aztec here:
1193
+ // https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
1194
+ const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
1195
+ if ( brBeforeParaMatches ) {
1196
+ console.warn(
1197
+ 'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...'
1198
+ );
1199
+ const count = (
1200
+ brBeforeParaMatches[ 0 ].match( /br/g ) || []
1201
+ ).length;
1202
+ if ( count > 0 ) {
1203
+ let newSelectionStart = currentSelectionStart - count;
1204
+ if ( newSelectionStart < 0 ) {
1205
+ newSelectionStart = 0;
1206
+ }
1207
+ let newSelectionEnd = currentSelectionEnd - count;
1208
+ if ( newSelectionEnd < 0 ) {
1209
+ newSelectionEnd = 0;
1210
+ }
1211
+ selection = {
1212
+ start: newSelectionStart,
1213
+ end: newSelectionEnd,
1214
+ };
1215
+ }
1216
+ }
1217
+ }
1218
+ }
1219
+
1220
+ if ( this.comesFromAztec ) {
1221
+ this.comesFromAztec = false;
1222
+ this.firedAfterTextChanged = false;
1223
+ }
1224
+
1225
+ // Logic below assures that `RichText` width will always have equal value when container is almost fully filled.
1226
+ const width =
1227
+ maxWidth && this.state.width && maxWidth - this.state.width < 10
1228
+ ? maxWidth
1229
+ : this.state.width;
1230
+ const containerStyles = [
1231
+ style?.padding &&
1232
+ style?.backgroundColor && {
1233
+ padding: style.padding,
1234
+ backgroundColor: style.backgroundColor,
1235
+ },
1236
+ containerWidth && {
1237
+ width: containerWidth,
1238
+ },
1239
+ ];
1240
+
1241
+ const defaultSelectionColor = getStylesFromColorScheme(
1242
+ styles[ 'rich-text-selection' ],
1243
+ styles[ 'rich-text-selection--dark' ]
1244
+ ).color;
1245
+ const selectionColor = getSelectionColor(
1246
+ this.props.selectionColor,
1247
+ defaultSelectionColor,
1248
+ baseGlobalStyles,
1249
+ this.getIsBlockBasedTheme()
1250
+ );
1251
+
1252
+ const EditableView = ( props ) => {
1253
+ this.customEditableOnKeyDown = props?.onKeyDown;
1254
+
1255
+ return <></>;
1256
+ };
1257
+
1258
+ return (
1259
+ <View style={ containerStyles }>
1260
+ { children &&
1261
+ children( {
1262
+ isSelected,
1263
+ value: record,
1264
+ onChange: this.onFormatChange,
1265
+ onFocus: () => {},
1266
+ editableProps,
1267
+ editableTagName: EditableView,
1268
+ } ) }
1269
+ <RCTAztecView
1270
+ accessibilityLabel={ accessibilityLabel }
1271
+ ref={ ( ref ) => {
1272
+ this._editor = ref;
1273
+
1274
+ if ( this.props.setRef ) {
1275
+ this.props.setRef( ref );
1276
+ }
1277
+ } }
1278
+ style={ {
1279
+ backgroundColor: styles.richText.backgroundColor,
1280
+ ...style,
1281
+ ...( this.isIOS && minWidth && maxWidth
1282
+ ? { width }
1283
+ : { maxWidth } ),
1284
+ minHeight: this.state.height,
1285
+ } }
1286
+ blockUseDefaultFont={ blockUseDefaultFont }
1287
+ text={ {
1288
+ text: html,
1289
+ eventCount: this.lastEventCount,
1290
+ selection,
1291
+ linkTextColor,
1292
+ tag: tagName,
1293
+ } }
1294
+ placeholder={ this.props.placeholder }
1295
+ placeholderTextColor={ this.getPlaceholderTextColor() }
1296
+ deleteEnter={ this.props.deleteEnter }
1297
+ onChange={ this.onChangeFromAztec }
1298
+ onFocus={ this.onFocus }
1299
+ onBlur={ this.onBlur }
1300
+ onKeyDown={ this.onKeyDown }
1301
+ triggerKeyCodes={
1302
+ disableEditingMenu
1303
+ ? []
1304
+ : this.suggestionOptions().map(
1305
+ ( op ) => op.triggerChar
1306
+ )
1307
+ }
1308
+ onPaste={ this.onPaste }
1309
+ activeFormats={ this.getActiveFormatNames( record ) }
1310
+ onContentSizeChange={ this.onContentSizeChange }
1311
+ onSelectionChange={ this.onSelectionChangeFromAztec }
1312
+ blockType={ { tag: tagName } }
1313
+ color={
1314
+ ( style && style.color ) ||
1315
+ ( parentBlockStyles && parentBlockStyles.color ) ||
1316
+ ( baseGlobalStyles && baseGlobalStyles?.color?.text ) ||
1317
+ defaultColor
1318
+ }
1319
+ maxImagesWidth={ 200 }
1320
+ fontFamily={ this.props.fontFamily || defaultFontFamily }
1321
+ fontSize={ fontSize }
1322
+ lineHeight={ lineHeight }
1323
+ fontWeight={ this.props.fontWeight }
1324
+ fontStyle={ this.props.fontStyle }
1325
+ disableEditingMenu={ disableEditingMenu }
1326
+ isMultiline={ this.isMultiline }
1327
+ textAlign={ this.props.textAlign }
1328
+ { ...( this.isIOS ? { maxWidth } : {} ) }
1329
+ minWidth={ minWidth }
1330
+ id={ this.props.id }
1331
+ selectionColor={ selectionColor }
1332
+ disableAutocorrection={ this.props.disableAutocorrection }
1333
+ />
1334
+ { isSelected && (
1335
+ <>
1336
+ <FormatEdit
1337
+ forwardedRef={ this._editor }
1338
+ formatTypes={ formatTypes }
1339
+ value={ record }
1340
+ onChange={ this.onFormatChange }
1341
+ onFocus={ () => {} }
1342
+ />
1343
+ { ! disableSuggestions && (
1344
+ <BlockFormatControls>
1345
+ <ToolbarButtonWithOptions
1346
+ options={ this.suggestionOptions() }
1347
+ />
1348
+ </BlockFormatControls>
1349
+ ) }
1350
+ </>
1351
+ ) }
1352
+ </View>
1353
+ );
1354
+ }
1355
+ }
1356
+
1357
+ RichText.defaultProps = {
1358
+ format: 'string',
1359
+ value: '',
1360
+ tagName: 'div',
1361
+ };
1362
+
1363
+ const withFormatTypes = ( WrappedComponent ) => ( props ) => {
1364
+ const {
1365
+ clientId,
1366
+ identifier,
1367
+ withoutInteractiveFormatting,
1368
+ allowedFormats,
1369
+ } = props;
1370
+ const { formatTypes } = useFormatTypes( {
1371
+ clientId,
1372
+ identifier,
1373
+ withoutInteractiveFormatting,
1374
+ allowedFormats,
1375
+ } );
1376
+
1377
+ return <WrappedComponent { ...props } formatTypes={ formatTypes } />;
1378
+ };
1379
+
1380
+ export default compose( [
1381
+ withSelect( ( select, { clientId } ) => {
1382
+ const { getBlockParents, getBlock, getSettings } =
1383
+ select( 'core/block-editor' );
1384
+ const parents = getBlockParents( clientId, true );
1385
+ const parentBlock = parents ? getBlock( parents[ 0 ] ) : undefined;
1386
+ const parentBlockStyles = parentBlock?.attributes?.childrenStyles;
1387
+
1388
+ const settings = getSettings();
1389
+ const baseGlobalStyles = settings?.__experimentalGlobalStylesBaseStyles;
1390
+
1391
+ const colorPalettes = settings?.__experimentalFeatures?.color?.palette;
1392
+ const colorPalette = colorPalettes
1393
+ ? flatColorPalettes( colorPalettes )
1394
+ : settings?.colors;
1395
+
1396
+ return {
1397
+ areMentionsSupported: settings?.capabilities?.mentions === true,
1398
+ areXPostsSupported: settings?.capabilities?.xposts === true,
1399
+ parentBlockStyles,
1400
+ baseGlobalStyles,
1401
+ colorPalette,
1402
+ };
1403
+ } ),
1404
+ withPreferredColorScheme,
1405
+ withFormatTypes,
1406
+ ] )( RichText );