@wordpress/block-editor 12.13.0 → 12.14.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 (344) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/components/block-heading-level-dropdown/index.js +1 -1
  3. package/build/components/block-heading-level-dropdown/index.js.map +1 -1
  4. package/build/components/block-list/index.js +2 -9
  5. package/build/components/block-list/index.js.map +1 -1
  6. package/build/components/block-list-appender/index.js +16 -3
  7. package/build/components/block-list-appender/index.js.map +1 -1
  8. package/build/components/block-quick-navigation/index.js +6 -4
  9. package/build/components/block-quick-navigation/index.js.map +1 -1
  10. package/build/components/block-styles/index.js +0 -8
  11. package/build/components/block-styles/index.js.map +1 -1
  12. package/build/components/block-switcher/pattern-transformations-menu.js +18 -14
  13. package/build/components/block-switcher/pattern-transformations-menu.js.map +1 -1
  14. package/build/components/block-tools/back-compat.js +2 -2
  15. package/build/components/block-tools/back-compat.js.map +1 -1
  16. package/build/components/block-tools/block-contextual-toolbar.js +11 -81
  17. package/build/components/block-tools/block-contextual-toolbar.js.map +1 -1
  18. package/build/components/block-tools/empty-block-inserter.js +56 -0
  19. package/build/components/block-tools/empty-block-inserter.js.map +1 -0
  20. package/build/components/block-tools/index.js +48 -9
  21. package/build/components/block-tools/index.js.map +1 -1
  22. package/build/components/block-tools/selected-block-tools.js +113 -0
  23. package/build/components/block-tools/selected-block-tools.js.map +1 -0
  24. package/build/components/block-tools/use-selected-block-tool-props.js +56 -0
  25. package/build/components/block-tools/use-selected-block-tool-props.js.map +1 -0
  26. package/build/components/editable-text/index.js +1 -2
  27. package/build/components/editable-text/index.js.map +1 -1
  28. package/build/components/iframe/index.js +4 -3
  29. package/build/components/iframe/index.js.map +1 -1
  30. package/build/components/inserter/block-patterns-explorer/{explorer.js → index.js} +7 -7
  31. package/build/components/inserter/block-patterns-explorer/index.js.map +1 -0
  32. package/build/components/inserter/block-patterns-explorer/{sidebar.js → pattern-explorer-sidebar.js} +1 -1
  33. package/build/components/inserter/block-patterns-explorer/pattern-explorer-sidebar.js.map +1 -0
  34. package/build/components/inserter/block-patterns-explorer/{patterns-list.js → pattern-list.js} +4 -4
  35. package/build/components/inserter/block-patterns-explorer/pattern-list.js.map +1 -0
  36. package/build/components/inserter/block-patterns-tab/index.js +75 -0
  37. package/build/components/inserter/block-patterns-tab/index.js.map +1 -0
  38. package/build/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +48 -0
  39. package/build/components/inserter/block-patterns-tab/pattern-category-preview-panel.js.map +1 -0
  40. package/build/components/inserter/block-patterns-tab/pattern-category-previews.js +108 -0
  41. package/build/components/inserter/block-patterns-tab/pattern-category-previews.js.map +1 -0
  42. package/build/components/inserter/{block-patterns-filter.js → block-patterns-tab/patterns-filter.js} +18 -34
  43. package/build/components/inserter/block-patterns-tab/patterns-filter.js.map +1 -0
  44. package/build/components/inserter/block-patterns-tab/use-pattern-categories.js +55 -0
  45. package/build/components/inserter/block-patterns-tab/use-pattern-categories.js.map +1 -0
  46. package/build/components/inserter/block-patterns-tab/utils.js +70 -0
  47. package/build/components/inserter/block-patterns-tab/utils.js.map +1 -0
  48. package/build/components/inserter/media-tab/media-list.js +9 -5
  49. package/build/components/inserter/media-tab/media-list.js.map +1 -1
  50. package/build/components/inserter/media-tab/media-preview.js +15 -12
  51. package/build/components/inserter/media-tab/media-preview.js.map +1 -1
  52. package/build/components/inserter/menu.js +3 -4
  53. package/build/components/inserter/menu.js.map +1 -1
  54. package/build/components/link-control/index.js +6 -5
  55. package/build/components/link-control/index.js.map +1 -1
  56. package/build/components/list-view/block-select-button.js +39 -0
  57. package/build/components/list-view/block-select-button.js.map +1 -1
  58. package/build/components/list-view/block.js +16 -3
  59. package/build/components/list-view/block.js.map +1 -1
  60. package/build/components/list-view/index.js +3 -2
  61. package/build/components/list-view/index.js.map +1 -1
  62. package/build/components/navigable-toolbar/index.js +69 -18
  63. package/build/components/navigable-toolbar/index.js.map +1 -1
  64. package/build/components/provider/use-block-sync.js +1 -14
  65. package/build/components/provider/use-block-sync.js.map +1 -1
  66. package/build/components/rich-text/index.js +0 -1
  67. package/build/components/rich-text/index.js.map +1 -1
  68. package/build/components/rich-text/index.native.js +3 -1
  69. package/build/components/rich-text/index.native.js.map +1 -1
  70. package/build/components/rich-text/native/format-edit.js +45 -0
  71. package/build/components/rich-text/native/format-edit.js.map +1 -0
  72. package/build/components/rich-text/native/get-format-colors.native.js +41 -0
  73. package/build/components/rich-text/native/get-format-colors.native.js.map +1 -0
  74. package/build/components/rich-text/native/index.js +9 -0
  75. package/build/components/rich-text/native/index.js.map +1 -0
  76. package/build/components/rich-text/native/index.native.js +1158 -0
  77. package/build/components/rich-text/native/index.native.js.map +1 -0
  78. package/build/components/rich-text/native/toolbar-button-with-options.native.js +58 -0
  79. package/build/components/rich-text/native/toolbar-button-with-options.native.js.map +1 -0
  80. package/build/components/rich-text/native/use-format-types.js +111 -0
  81. package/build/components/rich-text/native/use-format-types.js.map +1 -0
  82. package/build/components/rich-text/use-paste-handler.js +2 -5
  83. package/build/components/rich-text/use-paste-handler.js.map +1 -1
  84. package/build/components/url-popover/image-url-input-ui.js +2 -1
  85. package/build/components/url-popover/image-url-input-ui.js.map +1 -1
  86. package/build/components/writing-flow/use-tab-nav.js +7 -3
  87. package/build/components/writing-flow/use-tab-nav.js.map +1 -1
  88. package/build/hooks/align.js +10 -10
  89. package/build/hooks/align.js.map +1 -1
  90. package/build/hooks/align.native.js +2 -2
  91. package/build/hooks/align.native.js.map +1 -1
  92. package/build/hooks/anchor.js +7 -6
  93. package/build/hooks/anchor.js.map +1 -1
  94. package/build/hooks/background.js +16 -4
  95. package/build/hooks/background.js.map +1 -1
  96. package/build/hooks/block-hooks.js +7 -8
  97. package/build/hooks/block-hooks.js.map +1 -1
  98. package/build/hooks/block-rename-ui.js +7 -5
  99. package/build/hooks/block-rename-ui.js.map +1 -1
  100. package/build/hooks/content-lock-ui.js +5 -5
  101. package/build/hooks/content-lock-ui.js.map +1 -1
  102. package/build/hooks/custom-class-name.js +8 -7
  103. package/build/hooks/custom-class-name.js.map +1 -1
  104. package/build/hooks/custom-fields.js +4 -4
  105. package/build/hooks/custom-fields.js.map +1 -1
  106. package/build/hooks/duotone.js +9 -20
  107. package/build/hooks/duotone.js.map +1 -1
  108. package/build/hooks/layout.js +90 -86
  109. package/build/hooks/layout.js.map +1 -1
  110. package/build/hooks/position.js +10 -9
  111. package/build/hooks/position.js.map +1 -1
  112. package/build/hooks/style.js +10 -13
  113. package/build/hooks/style.js.map +1 -1
  114. package/build/hooks/utils.js +30 -0
  115. package/build/hooks/utils.js.map +1 -1
  116. package/build/private-apis.js +3 -0
  117. package/build/private-apis.js.map +1 -1
  118. package/build/store/actions.js +30 -37
  119. package/build/store/actions.js.map +1 -1
  120. package/build/store/reducer.js +18 -0
  121. package/build/store/reducer.js.map +1 -1
  122. package/build/store/selectors.js +16 -13
  123. package/build/store/selectors.js.map +1 -1
  124. package/build-module/components/block-heading-level-dropdown/index.js +1 -1
  125. package/build-module/components/block-heading-level-dropdown/index.js.map +1 -1
  126. package/build-module/components/block-list/index.js +3 -10
  127. package/build-module/components/block-list/index.js.map +1 -1
  128. package/build-module/components/block-list-appender/index.js +16 -3
  129. package/build-module/components/block-list-appender/index.js.map +1 -1
  130. package/build-module/components/block-quick-navigation/index.js +7 -5
  131. package/build-module/components/block-quick-navigation/index.js.map +1 -1
  132. package/build-module/components/block-styles/index.js +0 -8
  133. package/build-module/components/block-styles/index.js.map +1 -1
  134. package/build-module/components/block-switcher/pattern-transformations-menu.js +17 -13
  135. package/build-module/components/block-switcher/pattern-transformations-menu.js.map +1 -1
  136. package/build-module/components/block-tools/back-compat.js +1 -1
  137. package/build-module/components/block-tools/back-compat.js.map +1 -1
  138. package/build-module/components/block-tools/block-contextual-toolbar.js +11 -80
  139. package/build-module/components/block-tools/block-contextual-toolbar.js.map +1 -1
  140. package/build-module/components/block-tools/empty-block-inserter.js +48 -0
  141. package/build-module/components/block-tools/empty-block-inserter.js.map +1 -0
  142. package/build-module/components/block-tools/index.js +48 -9
  143. package/build-module/components/block-tools/index.js.map +1 -1
  144. package/build-module/components/block-tools/selected-block-tools.js +105 -0
  145. package/build-module/components/block-tools/selected-block-tools.js.map +1 -0
  146. package/build-module/components/block-tools/use-selected-block-tool-props.js +50 -0
  147. package/build-module/components/block-tools/use-selected-block-tool-props.js.map +1 -0
  148. package/build-module/components/editable-text/index.js +1 -2
  149. package/build-module/components/editable-text/index.js.map +1 -1
  150. package/build-module/components/iframe/index.js +4 -3
  151. package/build-module/components/iframe/index.js.map +1 -1
  152. package/build-module/components/inserter/block-patterns-explorer/{explorer.js → index.js} +5 -5
  153. package/build-module/components/inserter/block-patterns-explorer/index.js.map +1 -0
  154. package/build-module/components/inserter/block-patterns-explorer/{sidebar.js → pattern-explorer-sidebar.js} +1 -1
  155. package/build-module/components/inserter/block-patterns-explorer/pattern-explorer-sidebar.js.map +1 -0
  156. package/build-module/components/inserter/block-patterns-explorer/{patterns-list.js → pattern-list.js} +2 -2
  157. package/build-module/components/inserter/block-patterns-explorer/pattern-list.js.map +1 -0
  158. package/build-module/components/inserter/block-patterns-tab/index.js +66 -0
  159. package/build-module/components/inserter/block-patterns-tab/index.js.map +1 -0
  160. package/build-module/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +42 -0
  161. package/build-module/components/inserter/block-patterns-tab/pattern-category-preview-panel.js.map +1 -0
  162. package/build-module/components/inserter/block-patterns-tab/pattern-category-previews.js +100 -0
  163. package/build-module/components/inserter/block-patterns-tab/pattern-category-previews.js.map +1 -0
  164. package/build-module/components/inserter/{block-patterns-filter.js → block-patterns-tab/patterns-filter.js} +7 -20
  165. package/build-module/components/inserter/block-patterns-tab/patterns-filter.js.map +1 -0
  166. package/build-module/components/inserter/block-patterns-tab/use-pattern-categories.js +47 -0
  167. package/build-module/components/inserter/block-patterns-tab/use-pattern-categories.js.map +1 -0
  168. package/build-module/components/inserter/block-patterns-tab/utils.js +58 -0
  169. package/build-module/components/inserter/block-patterns-tab/utils.js.map +1 -0
  170. package/build-module/components/inserter/media-tab/media-list.js +9 -5
  171. package/build-module/components/inserter/media-tab/media-list.js.map +1 -1
  172. package/build-module/components/inserter/media-tab/media-preview.js +15 -12
  173. package/build-module/components/inserter/media-tab/media-preview.js.map +1 -1
  174. package/build-module/components/inserter/menu.js +4 -3
  175. package/build-module/components/inserter/menu.js.map +1 -1
  176. package/build-module/components/link-control/index.js +7 -6
  177. package/build-module/components/link-control/index.js.map +1 -1
  178. package/build-module/components/list-view/block-select-button.js +39 -0
  179. package/build-module/components/list-view/block-select-button.js.map +1 -1
  180. package/build-module/components/list-view/block.js +16 -3
  181. package/build-module/components/list-view/block.js.map +1 -1
  182. package/build-module/components/list-view/index.js +3 -2
  183. package/build-module/components/list-view/index.js.map +1 -1
  184. package/build-module/components/navigable-toolbar/index.js +69 -17
  185. package/build-module/components/navigable-toolbar/index.js.map +1 -1
  186. package/build-module/components/provider/use-block-sync.js +1 -14
  187. package/build-module/components/provider/use-block-sync.js.map +1 -1
  188. package/build-module/components/rich-text/index.js +0 -1
  189. package/build-module/components/rich-text/index.js.map +1 -1
  190. package/build-module/components/rich-text/index.native.js +3 -1
  191. package/build-module/components/rich-text/index.native.js.map +1 -1
  192. package/build-module/components/rich-text/native/format-edit.js +38 -0
  193. package/build-module/components/rich-text/native/format-edit.js.map +1 -0
  194. package/build-module/components/rich-text/native/get-format-colors.native.js +34 -0
  195. package/build-module/components/rich-text/native/get-format-colors.native.js.map +1 -0
  196. package/build-module/components/rich-text/native/index.js +2 -0
  197. package/build-module/components/rich-text/native/index.js.map +1 -0
  198. package/build-module/components/rich-text/native/index.native.js +1148 -0
  199. package/build-module/components/rich-text/native/index.native.js.map +1 -0
  200. package/build-module/components/rich-text/native/toolbar-button-with-options.native.js +51 -0
  201. package/build-module/components/rich-text/native/toolbar-button-with-options.native.js.map +1 -0
  202. package/build-module/components/rich-text/native/use-format-types.js +104 -0
  203. package/build-module/components/rich-text/native/use-format-types.js.map +1 -0
  204. package/build-module/components/rich-text/use-paste-handler.js +2 -5
  205. package/build-module/components/rich-text/use-paste-handler.js.map +1 -1
  206. package/build-module/components/url-popover/image-url-input-ui.js +2 -1
  207. package/build-module/components/url-popover/image-url-input-ui.js.map +1 -1
  208. package/build-module/components/writing-flow/use-tab-nav.js +7 -3
  209. package/build-module/components/writing-flow/use-tab-nav.js.map +1 -1
  210. package/build-module/hooks/align.js +8 -8
  211. package/build-module/hooks/align.js.map +1 -1
  212. package/build-module/hooks/align.native.js +2 -2
  213. package/build-module/hooks/align.native.js.map +1 -1
  214. package/build-module/hooks/anchor.js +5 -4
  215. package/build-module/hooks/anchor.js.map +1 -1
  216. package/build-module/hooks/background.js +17 -5
  217. package/build-module/hooks/background.js.map +1 -1
  218. package/build-module/hooks/block-hooks.js +5 -6
  219. package/build-module/hooks/block-hooks.js.map +1 -1
  220. package/build-module/hooks/block-rename-ui.js +5 -3
  221. package/build-module/hooks/block-rename-ui.js.map +1 -1
  222. package/build-module/hooks/content-lock-ui.js +3 -3
  223. package/build-module/hooks/content-lock-ui.js.map +1 -1
  224. package/build-module/hooks/custom-class-name.js +6 -5
  225. package/build-module/hooks/custom-class-name.js.map +1 -1
  226. package/build-module/hooks/custom-fields.js +4 -4
  227. package/build-module/hooks/custom-fields.js.map +1 -1
  228. package/build-module/hooks/duotone.js +10 -21
  229. package/build-module/hooks/duotone.js.map +1 -1
  230. package/build-module/hooks/layout.js +90 -86
  231. package/build-module/hooks/layout.js.map +1 -1
  232. package/build-module/hooks/position.js +11 -10
  233. package/build-module/hooks/position.js.map +1 -1
  234. package/build-module/hooks/style.js +10 -13
  235. package/build-module/hooks/style.js.map +1 -1
  236. package/build-module/hooks/utils.js +30 -1
  237. package/build-module/hooks/utils.js.map +1 -1
  238. package/build-module/private-apis.js +4 -1
  239. package/build-module/private-apis.js.map +1 -1
  240. package/build-module/store/actions.js +29 -37
  241. package/build-module/store/actions.js.map +1 -1
  242. package/build-module/store/reducer.js +17 -0
  243. package/build-module/store/reducer.js.map +1 -1
  244. package/build-module/store/selectors.js +15 -13
  245. package/build-module/store/selectors.js.map +1 -1
  246. package/build-style/style-rtl.css +16 -15
  247. package/build-style/style.css +16 -15
  248. package/package.json +31 -31
  249. package/src/components/block-heading-level-dropdown/index.js +1 -1
  250. package/src/components/block-list/index.js +4 -18
  251. package/src/components/block-list-appender/index.js +20 -4
  252. package/src/components/block-quick-navigation/index.js +11 -5
  253. package/src/components/block-styles/index.js +0 -10
  254. package/src/components/block-switcher/pattern-transformations-menu.js +20 -14
  255. package/src/components/block-toolbar/style.scss +8 -0
  256. package/src/components/block-tools/back-compat.js +1 -1
  257. package/src/components/block-tools/block-contextual-toolbar.js +11 -134
  258. package/src/components/block-tools/empty-block-inserter.js +56 -0
  259. package/src/components/block-tools/index.js +72 -16
  260. package/src/components/block-tools/selected-block-tools.js +127 -0
  261. package/src/components/block-tools/style.scss +0 -10
  262. package/src/components/block-tools/use-selected-block-tool-props.js +66 -0
  263. package/src/components/editable-text/index.js +1 -8
  264. package/src/components/iframe/index.js +4 -3
  265. package/src/components/inserter/block-patterns-explorer/{explorer.js → index.js} +4 -4
  266. package/src/components/inserter/block-patterns-explorer/{patterns-list.js → pattern-list.js} +4 -1
  267. package/src/components/inserter/block-patterns-tab/index.js +118 -0
  268. package/src/components/inserter/block-patterns-tab/pattern-category-preview-panel.js +48 -0
  269. package/src/components/inserter/block-patterns-tab/pattern-category-previews.js +175 -0
  270. package/src/components/inserter/{block-patterns-filter.js → block-patterns-tab/patterns-filter.js} +15 -21
  271. package/src/components/inserter/block-patterns-tab/use-pattern-categories.js +96 -0
  272. package/src/components/inserter/block-patterns-tab/utils.js +76 -0
  273. package/src/components/inserter/media-tab/media-list.js +7 -7
  274. package/src/components/inserter/media-tab/media-preview.js +27 -22
  275. package/src/components/inserter/menu.js +4 -5
  276. package/src/components/link-control/README.md +2 -2
  277. package/src/components/link-control/index.js +15 -6
  278. package/src/components/link-control/style.scss +8 -5
  279. package/src/components/list-view/block-select-button.js +44 -1
  280. package/src/components/list-view/block.js +11 -11
  281. package/src/components/list-view/index.js +2 -0
  282. package/src/components/media-replace-flow/style.scss +2 -2
  283. package/src/components/navigable-toolbar/index.js +71 -25
  284. package/src/components/plain-text/README.md +3 -3
  285. package/src/components/provider/use-block-sync.js +2 -21
  286. package/src/components/rich-text/README.md +9 -8
  287. package/src/components/rich-text/index.js +0 -1
  288. package/src/components/rich-text/index.native.js +3 -1
  289. package/src/components/rich-text/native/format-edit.js +44 -0
  290. package/src/components/rich-text/native/get-format-colors.native.js +54 -0
  291. package/src/components/rich-text/native/index.js +1 -0
  292. package/src/components/rich-text/native/index.native.js +1363 -0
  293. package/src/components/rich-text/native/style.native.scss +28 -0
  294. package/src/components/rich-text/native/test/__snapshots__/index.native.js.snap +79 -0
  295. package/src/components/rich-text/native/test/index.native.js +278 -0
  296. package/src/components/rich-text/native/test/performance/rich-text.native.js +44 -0
  297. package/src/components/rich-text/native/toolbar-button-with-options.native.js +61 -0
  298. package/src/components/rich-text/native/use-format-types.js +146 -0
  299. package/src/components/rich-text/use-paste-handler.js +1 -6
  300. package/src/components/url-popover/image-url-input-ui.js +1 -0
  301. package/src/components/writing-flow/use-tab-nav.js +8 -3
  302. package/src/hooks/align.js +8 -8
  303. package/src/hooks/align.native.js +2 -2
  304. package/src/hooks/anchor.js +21 -23
  305. package/src/hooks/background.js +28 -6
  306. package/src/hooks/block-hooks.js +20 -16
  307. package/src/hooks/block-rename-ui.js +6 -4
  308. package/src/hooks/content-lock-ui.js +3 -3
  309. package/src/hooks/custom-class-name.js +7 -6
  310. package/src/hooks/custom-fields.js +5 -5
  311. package/src/hooks/duotone.js +23 -33
  312. package/src/hooks/layout.js +114 -105
  313. package/src/hooks/position.js +8 -21
  314. package/src/hooks/style.js +17 -31
  315. package/src/hooks/test/align.js +4 -4
  316. package/src/hooks/utils.js +33 -1
  317. package/src/private-apis.js +4 -1
  318. package/src/store/actions.js +26 -72
  319. package/src/store/reducer.js +19 -0
  320. package/src/store/selectors.js +13 -19
  321. package/build/components/block-tools/selected-block-popover.js +0 -221
  322. package/build/components/block-tools/selected-block-popover.js.map +0 -1
  323. package/build/components/inserter/block-patterns-explorer/explorer.js.map +0 -1
  324. package/build/components/inserter/block-patterns-explorer/patterns-list.js.map +0 -1
  325. package/build/components/inserter/block-patterns-explorer/sidebar.js.map +0 -1
  326. package/build/components/inserter/block-patterns-filter.js.map +0 -1
  327. package/build/components/inserter/block-patterns-tab.js +0 -270
  328. package/build/components/inserter/block-patterns-tab.js.map +0 -1
  329. package/build/store/utils.js +0 -22
  330. package/build/store/utils.js.map +0 -1
  331. package/build-module/components/block-tools/selected-block-popover.js +0 -213
  332. package/build-module/components/block-tools/selected-block-popover.js.map +0 -1
  333. package/build-module/components/inserter/block-patterns-explorer/explorer.js.map +0 -1
  334. package/build-module/components/inserter/block-patterns-explorer/patterns-list.js.map +0 -1
  335. package/build-module/components/inserter/block-patterns-explorer/sidebar.js.map +0 -1
  336. package/build-module/components/inserter/block-patterns-filter.js.map +0 -1
  337. package/build-module/components/inserter/block-patterns-tab.js +0 -254
  338. package/build-module/components/inserter/block-patterns-tab.js.map +0 -1
  339. package/build-module/store/utils.js +0 -16
  340. package/build-module/store/utils.js.map +0 -1
  341. package/src/components/block-tools/selected-block-popover.js +0 -265
  342. package/src/components/inserter/block-patterns-tab.js +0 -448
  343. package/src/store/utils.js +0 -12
  344. /package/src/components/inserter/block-patterns-explorer/{sidebar.js → pattern-explorer-sidebar.js} +0 -0
@@ -0,0 +1,1363 @@
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
+ onSelectionChangeFromAztec( start, end, text, event ) {
654
+ if ( this.shouldDropEventFromAztec( event, 'onSelectionChange' ) ) {
655
+ return;
656
+ }
657
+
658
+ // `end` can be less than `start` on iOS
659
+ // Let's fix that here so `rich-text/slice` can work properly.
660
+ const realStart = Math.min( start, end );
661
+ const realEnd = Math.max( start, end );
662
+
663
+ // Check and dicsard stray event, where the text and selection is equal to the ones already cached.
664
+ const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
665
+ unescapeSpaces( event.nativeEvent.text )
666
+ );
667
+ if (
668
+ contentWithoutRootTag === this.value &&
669
+ realStart === this.selectionStart &&
670
+ realEnd === this.selectionEnd
671
+ ) {
672
+ return;
673
+ }
674
+
675
+ this.comesFromAztec = true;
676
+ this.firedAfterTextChanged = true; // Selection change event always fires after the fact.
677
+
678
+ // Update text before updating selection
679
+ // Make sure there are changes made to the content before upgrading it upward.
680
+ this.onTextUpdate( event );
681
+
682
+ // Aztec can send us selection change events after it has lost focus.
683
+ // For instance the autocorrect feature will complete a partially written
684
+ // word when resigning focus, causing a selection change event.
685
+ // Forwarding this selection change could cause this RichText to regain
686
+ // focus and start a focus loop.
687
+ //
688
+ // See https://github.com/wordpress-mobile/gutenberg-mobile/issues/1696
689
+ if ( this.props.__unstableIsSelected ) {
690
+ this.onSelectionChange( realStart, realEnd );
691
+ }
692
+ // Update lastEventCount to prevent Aztec from re-rendering the content it just sent.
693
+ this.lastEventCount = event.nativeEvent.eventCount;
694
+
695
+ this.lastAztecEventType = 'selection change';
696
+ }
697
+
698
+ isEmpty() {
699
+ return isEmpty( this.formatToValue( this.props.value ) );
700
+ }
701
+
702
+ formatToValue( value ) {
703
+ const { preserveWhiteSpace } = this.props;
704
+ // Handle deprecated `children` and `node` sources.
705
+ if ( Array.isArray( value ) ) {
706
+ return create( {
707
+ html: childrenBlock.toHTML( value ),
708
+ multilineTag: this.multilineTag,
709
+ multilineWrapperTags: this.multilineWrapperTags,
710
+ preserveWhiteSpace,
711
+ } );
712
+ }
713
+
714
+ if ( this.props.format === 'string' ) {
715
+ return create( {
716
+ html: value,
717
+ multilineTag: this.multilineTag,
718
+ multilineWrapperTags: this.multilineWrapperTags,
719
+ preserveWhiteSpace,
720
+ } );
721
+ }
722
+
723
+ // Guard for blocks passing `null` in onSplit callbacks. May be removed
724
+ // if onSplit is revised to not pass a `null` value.
725
+ if ( value === null ) {
726
+ return create();
727
+ }
728
+
729
+ return value;
730
+ }
731
+
732
+ manipulateEventCounterToForceNativeToRefresh() {
733
+ if ( this.isIOS ) {
734
+ this.lastEventCount = undefined;
735
+ return;
736
+ }
737
+
738
+ if ( typeof this.lastEventCount !== 'undefined' ) {
739
+ this.lastEventCount += 100; // bump by a hundred, hopefully native hasn't bombarded the JS side in the meantime.
740
+ } // 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,
741
+ // see https://github.com/WordPress/gutenberg/blob/82e578dcc75e67891c750a41a04c1e31994192fc/packages/react-native-aztec/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java#L213-L215
742
+ }
743
+
744
+ shouldComponentUpdate( nextProps, nextState ) {
745
+ if (
746
+ nextProps.tagName !== this.props.tagName ||
747
+ nextProps.reversed !== this.props.reversed ||
748
+ nextProps.start !== this.props.start
749
+ ) {
750
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
751
+ this.value = undefined;
752
+ return true;
753
+ }
754
+
755
+ // TODO: Please re-introduce the check to avoid updating the content right after an `onChange` call.
756
+ // It was removed in https://github.com/WordPress/gutenberg/pull/12417 to fix undo/redo problem.
757
+
758
+ // If the component is changed React side (undo/redo/merging/splitting/custom text actions)
759
+ // we need to make sure the native is updated as well.
760
+
761
+ // Also, don't trust the "this.lastContent" as on Android, incomplete text events arrive
762
+ // with only some of the text, while the virtual keyboard's suggestion system does its magic.
763
+ // ** compare with this.lastContent for optimizing performance by not forcing Aztec with text it already has
764
+ // , but compare with props.value to not lose "half word" text because of Android virtual keyb autosuggestion behavior
765
+ if (
766
+ typeof nextProps.value !== 'undefined' &&
767
+ typeof this.props.value !== 'undefined' &&
768
+ ( ! this.comesFromAztec || ! this.firedAfterTextChanged ) &&
769
+ nextProps.value !== this.props.value
770
+ ) {
771
+ // Gutenberg seems to try to mirror the caret state even on events that only change the content so,
772
+ // let's force caret update if state has selection set.
773
+ if (
774
+ typeof nextProps.selectionStart !== 'undefined' &&
775
+ typeof nextProps.selectionEnd !== 'undefined'
776
+ ) {
777
+ this.needsSelectionUpdate = true;
778
+ }
779
+
780
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
781
+ }
782
+
783
+ if ( ! this.comesFromAztec ) {
784
+ if (
785
+ typeof nextProps.selectionStart !== 'undefined' &&
786
+ typeof nextProps.selectionEnd !== 'undefined' &&
787
+ nextProps.selectionStart !== this.props.selectionStart &&
788
+ nextProps.selectionStart !== this.selectionStart &&
789
+ nextProps.__unstableIsSelected
790
+ ) {
791
+ this.needsSelectionUpdate = true;
792
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
793
+ }
794
+
795
+ // For font size changes from a prop value a force refresh
796
+ // is needed without the selection update.
797
+ if ( nextProps?.fontSize !== this.props?.fontSize ) {
798
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
799
+ }
800
+
801
+ if (
802
+ ( nextProps?.style?.fontSize !== this.props?.style?.fontSize &&
803
+ nextState.currentFontSize !==
804
+ this.state.currentFontSize ) ||
805
+ nextState.currentFontSize !== this.state.currentFontSize ||
806
+ nextProps?.style?.lineHeight !== this.props?.style?.lineHeight
807
+ ) {
808
+ this.needsSelectionUpdate = true;
809
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
810
+ }
811
+ }
812
+
813
+ return true;
814
+ }
815
+
816
+ componentDidMount() {
817
+ // Request focus if wrapping block is selected and parent hasn't inhibited the focus request. This method of focusing
818
+ // is trying to implement the web-side counterpart of BlockList's `focusTabbable` where the BlockList is focusing an
819
+ // inputbox by searching the DOM. We don't have the DOM in RN so, using the combination of blockIsSelected and __unstableMobileNoFocusOnMount
820
+ // to determine if we should focus the RichText.
821
+ if (
822
+ this.props.blockIsSelected &&
823
+ ! this.props.__unstableMobileNoFocusOnMount
824
+ ) {
825
+ this._editor.focus();
826
+ this.onSelectionChange(
827
+ this.props.selectionStart || 0,
828
+ this.props.selectionEnd || 0
829
+ );
830
+ }
831
+ }
832
+
833
+ componentWillUnmount() {
834
+ if ( this._editor.isFocused() ) {
835
+ this._editor.blur();
836
+ }
837
+ }
838
+
839
+ componentDidUpdate( prevProps ) {
840
+ const { style, tagName } = this.props;
841
+ const { currentFontSize } = this.state;
842
+
843
+ if ( this.props.value !== this.value ) {
844
+ this.value = this.props.value;
845
+ }
846
+ const { __unstableIsSelected: isSelected } = this.props;
847
+
848
+ const { __unstableIsSelected: prevIsSelected } = prevProps;
849
+
850
+ if ( isSelected && ! prevIsSelected ) {
851
+ this._editor.focus();
852
+ // Update selection props explicitly when component is selected as Aztec won't call onSelectionChange
853
+ // if its internal value hasn't change. When created, default value is 0, 0.
854
+ this.onSelectionChange(
855
+ this.props.selectionStart || 0,
856
+ this.props.selectionEnd || 0
857
+ );
858
+ } else if ( ! isSelected && prevIsSelected ) {
859
+ this._editor.blur();
860
+ }
861
+
862
+ // For font size values changes from the font size picker
863
+ // we compare previous values to refresh the selected font size,
864
+ // this is also used when the tag name changes
865
+ // e.g Heading block and a level change like h1->h2.
866
+ const currentFontSizeStyle = this.getParsedFontSize( style?.fontSize );
867
+ const prevFontSizeStyle = this.getParsedFontSize(
868
+ prevProps?.style?.fontSize
869
+ );
870
+ const isDifferentTag = prevProps.tagName !== tagName;
871
+ if (
872
+ ( currentFontSize &&
873
+ ( currentFontSizeStyle || prevFontSizeStyle ) &&
874
+ currentFontSizeStyle !== currentFontSize ) ||
875
+ isDifferentTag
876
+ ) {
877
+ this.setState( {
878
+ currentFontSize: this.getFontSize( this.props ),
879
+ } );
880
+ }
881
+ }
882
+
883
+ getHtmlToRender( record, tagName ) {
884
+ // Save back to HTML from React tree.
885
+ let value = this.valueToFormat( record );
886
+
887
+ if ( value === undefined ) {
888
+ this.manipulateEventCounterToForceNativeToRefresh(); // force a refresh on the native side
889
+ value = '';
890
+ }
891
+ // On android if content is empty we need to send no content or else the placeholder will not show.
892
+ if (
893
+ ! this.isIOS &&
894
+ ( value === '' || value === EMPTY_PARAGRAPH_TAGS )
895
+ ) {
896
+ return '';
897
+ }
898
+
899
+ if ( tagName ) {
900
+ let extraAttributes = ``;
901
+ if ( tagName === `ol` ) {
902
+ if ( this.props.reversed ) {
903
+ extraAttributes += ` reversed`;
904
+ }
905
+ if ( this.props.start ) {
906
+ extraAttributes += ` start=${ this.props.start }`;
907
+ }
908
+ }
909
+ value = `<${ tagName }${ extraAttributes }>${ value }</${ tagName }>`;
910
+ }
911
+ return value;
912
+ }
913
+
914
+ getEditableProps() {
915
+ return {
916
+ // Overridable props.
917
+ style: {},
918
+ className: 'rich-text',
919
+ onKeyDown: () => null,
920
+ };
921
+ }
922
+
923
+ getParsedFontSize( fontSize ) {
924
+ const { height, width } = Dimensions.get( 'window' );
925
+ const cssUnitOptions = { height, width, fontSize: DEFAULT_FONT_SIZE };
926
+
927
+ if ( ! fontSize ) {
928
+ return fontSize;
929
+ }
930
+
931
+ const selectedPxValue =
932
+ getPxFromCssUnit( fontSize, cssUnitOptions ) ?? DEFAULT_FONT_SIZE;
933
+
934
+ return parseFloat( selectedPxValue );
935
+ }
936
+
937
+ getFontSize( props ) {
938
+ const { baseGlobalStyles, tagName, fontSize, style } = props;
939
+ const tagNameFontSize =
940
+ baseGlobalStyles?.elements?.[ tagName ]?.typography?.fontSize;
941
+
942
+ let newFontSize = DEFAULT_FONT_SIZE;
943
+
944
+ // Disables line-height rendering for pre elements until we fix some issues with AztecAndroid.
945
+ if ( tagName === 'pre' && ! this.isIOS ) {
946
+ return undefined;
947
+ }
948
+
949
+ // For block-based themes, get the default editor font size.
950
+ if ( baseGlobalStyles?.typography?.fontSize && tagName === 'p' ) {
951
+ newFontSize = baseGlobalStyles?.typography?.fontSize;
952
+ }
953
+
954
+ // For block-based themes, get the default element font size
955
+ // e.g h1, h2.
956
+ if ( tagNameFontSize ) {
957
+ newFontSize = tagNameFontSize;
958
+ }
959
+
960
+ // For font size values provided from the styles,
961
+ // usually from values set from the font size picker.
962
+ if ( style?.fontSize ) {
963
+ newFontSize = style.fontSize;
964
+ }
965
+
966
+ // Fall-back to a font size provided from its props (if there's any)
967
+ // and there are no other default values to use.
968
+ if ( fontSize && ! tagNameFontSize && ! style?.fontSize ) {
969
+ newFontSize = fontSize;
970
+ }
971
+
972
+ // We need to always convert to px units because the selected value
973
+ // could be coming from the web where it could be stored as a different unit.
974
+ const selectedPxValue = this.getParsedFontSize( newFontSize );
975
+
976
+ return selectedPxValue;
977
+ }
978
+
979
+ getLineHeight() {
980
+ const { baseGlobalStyles, tagName, lineHeight, style } = this.props;
981
+ const tagNameLineHeight =
982
+ baseGlobalStyles?.elements?.[ tagName ]?.typography?.lineHeight;
983
+ let newLineHeight;
984
+
985
+ // Disables line-height rendering for pre elements until we fix some issues with AztecAndroid.
986
+ if ( tagName === 'pre' && ! this.isIOS ) {
987
+ return undefined;
988
+ }
989
+
990
+ if ( ! this.getIsBlockBasedTheme() ) {
991
+ return;
992
+ }
993
+
994
+ // For block-based themes, get the default editor line height.
995
+ if ( baseGlobalStyles?.typography?.lineHeight && tagName === 'p' ) {
996
+ newLineHeight = parseFloat(
997
+ baseGlobalStyles?.typography?.lineHeight
998
+ );
999
+ }
1000
+
1001
+ // For block-based themes, get the default element line height
1002
+ // e.g h1, h2.
1003
+ if ( tagNameLineHeight ) {
1004
+ newLineHeight = parseFloat( tagNameLineHeight );
1005
+ }
1006
+
1007
+ // For line height values provided from the styles,
1008
+ // usually from values set from the line height picker.
1009
+ if ( style?.lineHeight ) {
1010
+ newLineHeight = parseFloat( style.lineHeight );
1011
+ }
1012
+
1013
+ // Fall-back to a line height provided from its props (if there's any)
1014
+ // and there are no other default values to use.
1015
+ if ( lineHeight && ! tagNameLineHeight && ! style?.lineHeight ) {
1016
+ newLineHeight = lineHeight;
1017
+ }
1018
+
1019
+ // Check the final value is not over the minimum supported value.
1020
+ if ( newLineHeight && newLineHeight < MIN_LINE_HEIGHT ) {
1021
+ newLineHeight = MIN_LINE_HEIGHT;
1022
+ }
1023
+
1024
+ // Until we parse CSS values correctly, avoid passing NaN values to Aztec
1025
+ if ( isNaN( newLineHeight ) ) {
1026
+ return undefined;
1027
+ }
1028
+
1029
+ return newLineHeight;
1030
+ }
1031
+
1032
+ getIsBlockBasedTheme() {
1033
+ const { baseGlobalStyles } = this.props;
1034
+
1035
+ return (
1036
+ baseGlobalStyles && Object.entries( baseGlobalStyles ).length !== 0
1037
+ );
1038
+ }
1039
+
1040
+ getBlockUseDefaultFont() {
1041
+ // For block-based themes it enables using the defaultFont
1042
+ // in Aztec for iOS so it allows customizing the font size
1043
+ // for the Preformatted/Code and Heading blocks.
1044
+ if ( ! this.isIOS ) {
1045
+ return;
1046
+ }
1047
+
1048
+ const { tagName } = this.props;
1049
+ const isBlockBasedTheme = this.getIsBlockBasedTheme();
1050
+ const tagsToMatch = /pre|h([1-6])$/gm;
1051
+
1052
+ return isBlockBasedTheme && tagsToMatch.test( tagName );
1053
+ }
1054
+
1055
+ getLinkTextColor( defaultColor ) {
1056
+ const { style } = this.props;
1057
+ const customColor = style?.linkColor && colord( style.linkColor );
1058
+
1059
+ return customColor && customColor.isValid()
1060
+ ? customColor.toHex()
1061
+ : defaultColor;
1062
+ }
1063
+
1064
+ getPlaceholderTextColor() {
1065
+ const {
1066
+ baseGlobalStyles,
1067
+ getStylesFromColorScheme,
1068
+ placeholderTextColor,
1069
+ style,
1070
+ } = this.props;
1071
+
1072
+ // Default placeholder text color.
1073
+ const placeholderStyle = getStylesFromColorScheme(
1074
+ styles.richTextPlaceholder,
1075
+ styles.richTextPlaceholderDark
1076
+ );
1077
+ const { color: defaultPlaceholderTextColor } = placeholderStyle;
1078
+ // Custom 63% opacity for theme and inherited colors.
1079
+ const placeholderOpacity = 'A1';
1080
+
1081
+ // Determine inherited placeholder color if available.
1082
+ const inheritPlaceholderColor = style?.placeholderColor
1083
+ ? `${ style.placeholderColor }${ placeholderOpacity }`
1084
+ : undefined;
1085
+
1086
+ // If using block-based themes, derive the placeholder color from global styles.
1087
+ const globalStylesPlaceholderColor = baseGlobalStyles?.color?.text
1088
+ ? `${ baseGlobalStyles.color.text }${ placeholderOpacity }`
1089
+ : undefined;
1090
+
1091
+ return (
1092
+ inheritPlaceholderColor ??
1093
+ placeholderTextColor ??
1094
+ globalStylesPlaceholderColor ??
1095
+ defaultPlaceholderTextColor
1096
+ );
1097
+ }
1098
+
1099
+ render() {
1100
+ const {
1101
+ tagName,
1102
+ style,
1103
+ __unstableIsSelected: isSelected,
1104
+ children,
1105
+ getStylesFromColorScheme,
1106
+ minWidth,
1107
+ maxWidth,
1108
+ formatTypes,
1109
+ parentBlockStyles,
1110
+ accessibilityLabel,
1111
+ disableEditingMenu = false,
1112
+ baseGlobalStyles,
1113
+ selectionStart,
1114
+ selectionEnd,
1115
+ disableSuggestions,
1116
+ containerWidth,
1117
+ } = this.props;
1118
+ const { currentFontSize } = this.state;
1119
+
1120
+ const record = this.getRecord();
1121
+ const html = this.getHtmlToRender( record, tagName );
1122
+ const editableProps = this.getEditableProps();
1123
+ const blockUseDefaultFont = this.getBlockUseDefaultFont();
1124
+
1125
+ const fontSize = currentFontSize;
1126
+ const lineHeight = this.getLineHeight();
1127
+
1128
+ const {
1129
+ color: defaultColor,
1130
+ textDecorationColor: defaultTextDecorationColor,
1131
+ fontFamily: defaultFontFamily,
1132
+ } = getStylesFromColorScheme( styles.richText, styles.richTextDark );
1133
+ const linkTextColor = this.getLinkTextColor(
1134
+ defaultTextDecorationColor
1135
+ );
1136
+
1137
+ const currentSelectionStart = selectionStart ?? 0;
1138
+ const currentSelectionEnd = selectionEnd ?? 0;
1139
+ let selection = null;
1140
+ if ( this.needsSelectionUpdate ) {
1141
+ this.needsSelectionUpdate = false;
1142
+ selection = {
1143
+ start: currentSelectionStart,
1144
+ end: currentSelectionEnd,
1145
+ };
1146
+
1147
+ // On AztecAndroid, setting the caret to an out-of-bounds position will crash the editor so, let's check for some cases.
1148
+ if ( ! this.isIOS ) {
1149
+ // The following regular expression is used in Aztec here:
1150
+ // https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
1151
+ const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
1152
+ if ( brBeforeParaMatches ) {
1153
+ console.warn(
1154
+ 'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...'
1155
+ );
1156
+ const count = (
1157
+ brBeforeParaMatches[ 0 ].match( /br/g ) || []
1158
+ ).length;
1159
+ if ( count > 0 ) {
1160
+ let newSelectionStart = currentSelectionStart - count;
1161
+ if ( newSelectionStart < 0 ) {
1162
+ newSelectionStart = 0;
1163
+ }
1164
+ let newSelectionEnd = currentSelectionEnd - count;
1165
+ if ( newSelectionEnd < 0 ) {
1166
+ newSelectionEnd = 0;
1167
+ }
1168
+ selection = {
1169
+ start: newSelectionStart,
1170
+ end: newSelectionEnd,
1171
+ };
1172
+ }
1173
+ }
1174
+ }
1175
+ }
1176
+
1177
+ if ( this.comesFromAztec ) {
1178
+ this.comesFromAztec = false;
1179
+ this.firedAfterTextChanged = false;
1180
+ }
1181
+
1182
+ // Logic below assures that `RichText` width will always have equal value when container is almost fully filled.
1183
+ const width =
1184
+ maxWidth && this.state.width && maxWidth - this.state.width < 10
1185
+ ? maxWidth
1186
+ : this.state.width;
1187
+ const containerStyles = [
1188
+ style?.padding &&
1189
+ style?.backgroundColor && {
1190
+ padding: style.padding,
1191
+ backgroundColor: style.backgroundColor,
1192
+ },
1193
+ containerWidth && {
1194
+ width: containerWidth,
1195
+ },
1196
+ ];
1197
+
1198
+ const defaultSelectionColor = getStylesFromColorScheme(
1199
+ styles[ 'rich-text-selection' ],
1200
+ styles[ 'rich-text-selection--dark' ]
1201
+ ).color;
1202
+ const selectionColor = getSelectionColor(
1203
+ this.props.selectionColor,
1204
+ defaultSelectionColor,
1205
+ baseGlobalStyles,
1206
+ this.getIsBlockBasedTheme()
1207
+ );
1208
+
1209
+ const EditableView = ( props ) => {
1210
+ this.customEditableOnKeyDown = props?.onKeyDown;
1211
+
1212
+ return <></>;
1213
+ };
1214
+
1215
+ return (
1216
+ <View style={ containerStyles }>
1217
+ { children &&
1218
+ children( {
1219
+ isSelected,
1220
+ value: record,
1221
+ onChange: this.onFormatChange,
1222
+ onFocus: () => {},
1223
+ editableProps,
1224
+ editableTagName: EditableView,
1225
+ } ) }
1226
+ <RCTAztecView
1227
+ accessibilityLabel={ accessibilityLabel }
1228
+ ref={ ( ref ) => {
1229
+ this._editor = ref;
1230
+
1231
+ if ( this.props.setRef ) {
1232
+ this.props.setRef( ref );
1233
+ }
1234
+ } }
1235
+ style={ {
1236
+ backgroundColor: styles.richText.backgroundColor,
1237
+ ...style,
1238
+ ...( this.isIOS && minWidth && maxWidth
1239
+ ? { width }
1240
+ : { maxWidth } ),
1241
+ minHeight: this.state.height,
1242
+ } }
1243
+ blockUseDefaultFont={ blockUseDefaultFont }
1244
+ text={ {
1245
+ text: html,
1246
+ eventCount: this.lastEventCount,
1247
+ selection,
1248
+ linkTextColor,
1249
+ tag: tagName,
1250
+ } }
1251
+ placeholder={ this.props.placeholder }
1252
+ placeholderTextColor={ this.getPlaceholderTextColor() }
1253
+ deleteEnter={ this.props.deleteEnter }
1254
+ onChange={ this.onChangeFromAztec }
1255
+ onFocus={ this.onFocus }
1256
+ onBlur={ this.onBlur }
1257
+ onKeyDown={ this.onKeyDown }
1258
+ triggerKeyCodes={
1259
+ disableEditingMenu
1260
+ ? []
1261
+ : this.suggestionOptions().map(
1262
+ ( op ) => op.triggerChar
1263
+ )
1264
+ }
1265
+ onPaste={ this.onPaste }
1266
+ activeFormats={ this.getActiveFormatNames( record ) }
1267
+ onContentSizeChange={ this.onContentSizeChange }
1268
+ onSelectionChange={ this.onSelectionChangeFromAztec }
1269
+ blockType={ { tag: tagName } }
1270
+ color={
1271
+ ( style && style.color ) ||
1272
+ ( parentBlockStyles && parentBlockStyles.color ) ||
1273
+ ( baseGlobalStyles && baseGlobalStyles?.color?.text ) ||
1274
+ defaultColor
1275
+ }
1276
+ maxImagesWidth={ 200 }
1277
+ fontFamily={ this.props.fontFamily || defaultFontFamily }
1278
+ fontSize={ fontSize }
1279
+ lineHeight={ lineHeight }
1280
+ fontWeight={ this.props.fontWeight }
1281
+ fontStyle={ this.props.fontStyle }
1282
+ disableEditingMenu={ disableEditingMenu }
1283
+ isMultiline={ this.isMultiline }
1284
+ textAlign={ this.props.textAlign }
1285
+ { ...( this.isIOS ? { maxWidth } : {} ) }
1286
+ minWidth={ minWidth }
1287
+ id={ this.props.id }
1288
+ selectionColor={ selectionColor }
1289
+ disableAutocorrection={ this.props.disableAutocorrection }
1290
+ />
1291
+ { isSelected && (
1292
+ <>
1293
+ <FormatEdit
1294
+ forwardedRef={ this._editor }
1295
+ formatTypes={ formatTypes }
1296
+ value={ record }
1297
+ onChange={ this.onFormatChange }
1298
+ onFocus={ () => {} }
1299
+ />
1300
+ { ! disableSuggestions && (
1301
+ <BlockFormatControls>
1302
+ <ToolbarButtonWithOptions
1303
+ options={ this.suggestionOptions() }
1304
+ />
1305
+ </BlockFormatControls>
1306
+ ) }
1307
+ </>
1308
+ ) }
1309
+ </View>
1310
+ );
1311
+ }
1312
+ }
1313
+
1314
+ RichText.defaultProps = {
1315
+ format: 'string',
1316
+ value: '',
1317
+ tagName: 'div',
1318
+ };
1319
+
1320
+ const withFormatTypes = ( WrappedComponent ) => ( props ) => {
1321
+ const {
1322
+ clientId,
1323
+ identifier,
1324
+ withoutInteractiveFormatting,
1325
+ allowedFormats,
1326
+ } = props;
1327
+ const { formatTypes } = useFormatTypes( {
1328
+ clientId,
1329
+ identifier,
1330
+ withoutInteractiveFormatting,
1331
+ allowedFormats,
1332
+ } );
1333
+
1334
+ return <WrappedComponent { ...props } formatTypes={ formatTypes } />;
1335
+ };
1336
+
1337
+ export default compose( [
1338
+ withSelect( ( select, { clientId } ) => {
1339
+ const { getBlockParents, getBlock, getSettings } =
1340
+ select( 'core/block-editor' );
1341
+ const parents = getBlockParents( clientId, true );
1342
+ const parentBlock = parents ? getBlock( parents[ 0 ] ) : undefined;
1343
+ const parentBlockStyles = parentBlock?.attributes?.childrenStyles;
1344
+
1345
+ const settings = getSettings();
1346
+ const baseGlobalStyles = settings?.__experimentalGlobalStylesBaseStyles;
1347
+
1348
+ const colorPalettes = settings?.__experimentalFeatures?.color?.palette;
1349
+ const colorPalette = colorPalettes
1350
+ ? flatColorPalettes( colorPalettes )
1351
+ : settings?.colors;
1352
+
1353
+ return {
1354
+ areMentionsSupported: settings?.capabilities?.mentions === true,
1355
+ areXPostsSupported: settings?.capabilities?.xposts === true,
1356
+ parentBlockStyles,
1357
+ baseGlobalStyles,
1358
+ colorPalette,
1359
+ };
1360
+ } ),
1361
+ withPreferredColorScheme,
1362
+ withFormatTypes,
1363
+ ] )( RichText );