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