@wordpress/core-data 7.32.0 → 7.32.1-next.ff1cebbba.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 (383) hide show
  1. package/build/actions.js +375 -632
  2. package/build/actions.js.map +7 -1
  3. package/build/batch/create-batch.js +49 -62
  4. package/build/batch/create-batch.js.map +7 -1
  5. package/build/batch/default-processor.js +43 -39
  6. package/build/batch/default-processor.js.map +7 -1
  7. package/build/batch/index.js +38 -17
  8. package/build/batch/index.js.map +7 -1
  9. package/build/dynamic-entities.js +30 -32
  10. package/build/dynamic-entities.js.map +7 -1
  11. package/build/entities.js +298 -410
  12. package/build/entities.js.map +7 -1
  13. package/build/entity-context.js +29 -12
  14. package/build/entity-context.js.map +7 -1
  15. package/build/entity-provider.js +38 -46
  16. package/build/entity-provider.js.map +7 -1
  17. package/build/entity-types/attachment.js +16 -5
  18. package/build/entity-types/attachment.js.map +7 -1
  19. package/build/entity-types/base-entity-records.js +16 -42
  20. package/build/entity-types/base-entity-records.js.map +7 -1
  21. package/build/entity-types/base.js +16 -5
  22. package/build/entity-types/base.js.map +7 -1
  23. package/build/entity-types/comment.js +16 -5
  24. package/build/entity-types/comment.js.map +7 -1
  25. package/build/entity-types/global-styles-revision.js +16 -5
  26. package/build/entity-types/global-styles-revision.js.map +7 -1
  27. package/build/entity-types/helpers.js +16 -5
  28. package/build/entity-types/helpers.js.map +7 -1
  29. package/build/entity-types/index.js +16 -5
  30. package/build/entity-types/index.js.map +7 -1
  31. package/build/entity-types/menu-location.js +16 -5
  32. package/build/entity-types/menu-location.js.map +7 -1
  33. package/build/entity-types/nav-menu-item.js +16 -5
  34. package/build/entity-types/nav-menu-item.js.map +7 -1
  35. package/build/entity-types/nav-menu.js +16 -5
  36. package/build/entity-types/nav-menu.js.map +7 -1
  37. package/build/entity-types/page.js +16 -5
  38. package/build/entity-types/page.js.map +7 -1
  39. package/build/entity-types/plugin.js +16 -5
  40. package/build/entity-types/plugin.js.map +7 -1
  41. package/build/entity-types/post-revision.js +16 -5
  42. package/build/entity-types/post-revision.js.map +7 -1
  43. package/build/entity-types/post-status.js +16 -5
  44. package/build/entity-types/post-status.js.map +7 -1
  45. package/build/entity-types/post.js +16 -5
  46. package/build/entity-types/post.js.map +7 -1
  47. package/build/entity-types/settings.js +16 -5
  48. package/build/entity-types/settings.js.map +7 -1
  49. package/build/entity-types/sidebar.js +16 -5
  50. package/build/entity-types/sidebar.js.map +7 -1
  51. package/build/entity-types/taxonomy.js +16 -5
  52. package/build/entity-types/taxonomy.js.map +7 -1
  53. package/build/entity-types/term.js +16 -5
  54. package/build/entity-types/term.js.map +7 -1
  55. package/build/entity-types/theme.js +16 -5
  56. package/build/entity-types/theme.js.map +7 -1
  57. package/build/entity-types/type.js +16 -5
  58. package/build/entity-types/type.js.map +7 -1
  59. package/build/entity-types/user.js +16 -5
  60. package/build/entity-types/user.js.map +7 -1
  61. package/build/entity-types/widget-type.js +16 -5
  62. package/build/entity-types/widget-type.js.map +7 -1
  63. package/build/entity-types/widget.js +16 -5
  64. package/build/entity-types/widget.js.map +7 -1
  65. package/build/entity-types/wp-template-part.js +16 -5
  66. package/build/entity-types/wp-template-part.js.map +7 -1
  67. package/build/entity-types/wp-template.js +16 -5
  68. package/build/entity-types/wp-template.js.map +7 -1
  69. package/build/fetch/__experimental-fetch-link-suggestions.js +141 -154
  70. package/build/fetch/__experimental-fetch-link-suggestions.js.map +7 -1
  71. package/build/fetch/__experimental-fetch-url-data.js +47 -59
  72. package/build/fetch/__experimental-fetch-url-data.js.map +7 -1
  73. package/build/fetch/index.js +53 -32
  74. package/build/fetch/index.js.map +7 -1
  75. package/build/footnotes/get-footnotes-order.js +38 -25
  76. package/build/footnotes/get-footnotes-order.js.map +7 -1
  77. package/build/footnotes/get-rich-text-values-cached.js +26 -27
  78. package/build/footnotes/get-rich-text-values-cached.js.map +7 -1
  79. package/build/footnotes/index.js +68 -55
  80. package/build/footnotes/index.js.map +7 -1
  81. package/build/hooks/constants.js +33 -12
  82. package/build/hooks/constants.js.map +7 -1
  83. package/build/hooks/index.js +59 -68
  84. package/build/hooks/index.js.map +7 -1
  85. package/build/hooks/memoize.js +34 -12
  86. package/build/hooks/memoize.js.map +7 -1
  87. package/build/hooks/use-entity-block-editor.js +111 -119
  88. package/build/hooks/use-entity-block-editor.js.map +7 -1
  89. package/build/hooks/use-entity-id.js +25 -23
  90. package/build/hooks/use-entity-id.js.map +7 -1
  91. package/build/hooks/use-entity-prop.js +59 -60
  92. package/build/hooks/use-entity-prop.js.map +7 -1
  93. package/build/hooks/use-entity-record.js +95 -155
  94. package/build/hooks/use-entity-record.js.map +7 -1
  95. package/build/hooks/use-entity-records.js +131 -139
  96. package/build/hooks/use-entity-records.js.map +7 -1
  97. package/build/hooks/use-query-select.js +65 -84
  98. package/build/hooks/use-query-select.js.map +7 -1
  99. package/build/hooks/use-resource-permissions.js +92 -145
  100. package/build/hooks/use-resource-permissions.js.map +7 -1
  101. package/build/index.js +96 -153
  102. package/build/index.js.map +7 -1
  103. package/build/lock-unlock.js +31 -14
  104. package/build/lock-unlock.js.map +7 -1
  105. package/build/locks/actions.js +36 -19
  106. package/build/locks/actions.js.map +7 -1
  107. package/build/locks/engine.js +48 -47
  108. package/build/locks/engine.js.map +7 -1
  109. package/build/locks/reducer.js +54 -63
  110. package/build/locks/reducer.js.map +7 -1
  111. package/build/locks/selectors.js +35 -30
  112. package/build/locks/selectors.js.map +7 -1
  113. package/build/locks/utils.js +37 -16
  114. package/build/locks/utils.js.map +7 -1
  115. package/build/name.js +27 -12
  116. package/build/name.js.map +7 -1
  117. package/build/private-actions.js +67 -75
  118. package/build/private-actions.js.map +7 -1
  119. package/build/private-apis.js +33 -16
  120. package/build/private-apis.js.map +7 -1
  121. package/build/private-selectors.js +204 -184
  122. package/build/private-selectors.js.map +7 -1
  123. package/build/queried-data/actions.js +32 -41
  124. package/build/queried-data/actions.js.map +7 -1
  125. package/build/queried-data/get-query-parts.js +41 -79
  126. package/build/queried-data/get-query-parts.js.map +7 -1
  127. package/build/queried-data/index.js +39 -36
  128. package/build/queried-data/index.js.map +7 -1
  129. package/build/queried-data/reducer.js +162 -193
  130. package/build/queried-data/reducer.js.map +7 -1
  131. package/build/queried-data/selectors.js +57 -85
  132. package/build/queried-data/selectors.js.map +7 -1
  133. package/build/reducer.js +279 -404
  134. package/build/reducer.js.map +7 -1
  135. package/build/resolvers.js +553 -600
  136. package/build/resolvers.js.map +7 -1
  137. package/build/selectors.js +456 -981
  138. package/build/selectors.js.map +7 -1
  139. package/build/sync.js +34 -22
  140. package/build/sync.js.map +7 -1
  141. package/build/types.js +16 -5
  142. package/build/types.js.map +7 -1
  143. package/build/utils/conservative-map-item.js +34 -27
  144. package/build/utils/conservative-map-item.js.map +7 -1
  145. package/build/utils/crdt-blocks.js +289 -0
  146. package/build/utils/crdt-blocks.js.map +7 -0
  147. package/build/utils/crdt.js +202 -0
  148. package/build/utils/crdt.js.map +7 -0
  149. package/build/utils/forward-resolver.js +24 -16
  150. package/build/utils/forward-resolver.js.map +7 -1
  151. package/build/utils/get-nested-value.js +26 -21
  152. package/build/utils/get-nested-value.js.map +7 -1
  153. package/build/utils/get-normalized-comma-separable.js +25 -17
  154. package/build/utils/get-normalized-comma-separable.js.map +7 -1
  155. package/build/utils/if-matching-action.js +25 -19
  156. package/build/utils/if-matching-action.js.map +7 -1
  157. package/build/utils/index.js +77 -108
  158. package/build/utils/index.js.map +7 -1
  159. package/build/utils/is-numeric-id.js +22 -12
  160. package/build/utils/is-numeric-id.js.map +7 -1
  161. package/build/utils/is-raw-attribute.js +22 -13
  162. package/build/utils/is-raw-attribute.js.map +7 -1
  163. package/build/utils/log-entity-deprecation.js +37 -38
  164. package/build/utils/log-entity-deprecation.js.map +7 -1
  165. package/build/utils/on-sub-key.js +30 -24
  166. package/build/utils/on-sub-key.js.map +7 -1
  167. package/build/utils/receive-intermediate-results.js +29 -6
  168. package/build/utils/receive-intermediate-results.js.map +7 -1
  169. package/build/utils/replace-action.js +24 -17
  170. package/build/utils/replace-action.js.map +7 -1
  171. package/build/utils/set-nested-value.js +25 -30
  172. package/build/utils/set-nested-value.js.map +7 -1
  173. package/build/utils/user-permissions.js +41 -13
  174. package/build/utils/user-permissions.js.map +7 -1
  175. package/build/utils/with-weak-map-cache.js +26 -22
  176. package/build/utils/with-weak-map-cache.js.map +7 -1
  177. package/build-module/actions.js +322 -601
  178. package/build-module/actions.js.map +7 -1
  179. package/build-module/batch/create-batch.js +21 -57
  180. package/build-module/batch/create-batch.js.map +7 -1
  181. package/build-module/batch/default-processor.js +14 -33
  182. package/build-module/batch/default-processor.js.map +7 -1
  183. package/build-module/batch/index.js +7 -3
  184. package/build-module/batch/index.js.map +7 -1
  185. package/build-module/dynamic-entities.js +7 -28
  186. package/build-module/dynamic-entities.js.map +7 -1
  187. package/build-module/entities.js +263 -399
  188. package/build-module/entities.js.map +7 -1
  189. package/build-module/entity-context.js +7 -7
  190. package/build-module/entity-context.js.map +7 -1
  191. package/build-module/entity-provider.js +19 -42
  192. package/build-module/entity-provider.js.map +7 -1
  193. package/build-module/entity-types/attachment.js +1 -2
  194. package/build-module/entity-types/attachment.js.map +7 -1
  195. package/build-module/entity-types/base-entity-records.js +1 -37
  196. package/build-module/entity-types/base-entity-records.js.map +7 -1
  197. package/build-module/entity-types/base.js +1 -2
  198. package/build-module/entity-types/base.js.map +7 -1
  199. package/build-module/entity-types/comment.js +1 -2
  200. package/build-module/entity-types/comment.js.map +7 -1
  201. package/build-module/entity-types/global-styles-revision.js +1 -2
  202. package/build-module/entity-types/global-styles-revision.js.map +7 -1
  203. package/build-module/entity-types/helpers.js +1 -2
  204. package/build-module/entity-types/helpers.js.map +7 -1
  205. package/build-module/entity-types/index.js +1 -2
  206. package/build-module/entity-types/index.js.map +7 -1
  207. package/build-module/entity-types/menu-location.js +1 -2
  208. package/build-module/entity-types/menu-location.js.map +7 -1
  209. package/build-module/entity-types/nav-menu-item.js +1 -2
  210. package/build-module/entity-types/nav-menu-item.js.map +7 -1
  211. package/build-module/entity-types/nav-menu.js +1 -2
  212. package/build-module/entity-types/nav-menu.js.map +7 -1
  213. package/build-module/entity-types/page.js +1 -2
  214. package/build-module/entity-types/page.js.map +7 -1
  215. package/build-module/entity-types/plugin.js +1 -2
  216. package/build-module/entity-types/plugin.js.map +7 -1
  217. package/build-module/entity-types/post-revision.js +1 -2
  218. package/build-module/entity-types/post-revision.js.map +7 -1
  219. package/build-module/entity-types/post-status.js +1 -2
  220. package/build-module/entity-types/post-status.js.map +7 -1
  221. package/build-module/entity-types/post.js +1 -2
  222. package/build-module/entity-types/post.js.map +7 -1
  223. package/build-module/entity-types/settings.js +1 -2
  224. package/build-module/entity-types/settings.js.map +7 -1
  225. package/build-module/entity-types/sidebar.js +1 -2
  226. package/build-module/entity-types/sidebar.js.map +7 -1
  227. package/build-module/entity-types/taxonomy.js +1 -2
  228. package/build-module/entity-types/taxonomy.js.map +7 -1
  229. package/build-module/entity-types/term.js +1 -2
  230. package/build-module/entity-types/term.js.map +7 -1
  231. package/build-module/entity-types/theme.js +1 -2
  232. package/build-module/entity-types/theme.js.map +7 -1
  233. package/build-module/entity-types/type.js +1 -2
  234. package/build-module/entity-types/type.js.map +7 -1
  235. package/build-module/entity-types/user.js +1 -2
  236. package/build-module/entity-types/user.js.map +7 -1
  237. package/build-module/entity-types/widget-type.js +1 -2
  238. package/build-module/entity-types/widget-type.js.map +7 -1
  239. package/build-module/entity-types/widget.js +1 -2
  240. package/build-module/entity-types/widget.js.map +7 -1
  241. package/build-module/entity-types/wp-template-part.js +1 -2
  242. package/build-module/entity-types/wp-template-part.js.map +7 -1
  243. package/build-module/entity-types/wp-template.js +1 -2
  244. package/build-module/entity-types/wp-template.js.map +7 -1
  245. package/build-module/fetch/__experimental-fetch-link-suggestions.js +111 -149
  246. package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +7 -1
  247. package/build-module/fetch/__experimental-fetch-url-data.js +20 -49
  248. package/build-module/fetch/__experimental-fetch-url-data.js.map +7 -1
  249. package/build-module/fetch/index.js +20 -15
  250. package/build-module/fetch/index.js.map +7 -1
  251. package/build-module/footnotes/get-footnotes-order.js +10 -19
  252. package/build-module/footnotes/get-footnotes-order.js.map +7 -1
  253. package/build-module/footnotes/get-rich-text-values-cached.js +8 -23
  254. package/build-module/footnotes/get-rich-text-values-cached.js.map +7 -1
  255. package/build-module/footnotes/index.js +34 -47
  256. package/build-module/footnotes/index.js.map +7 -1
  257. package/build-module/hooks/constants.js +11 -8
  258. package/build-module/hooks/constants.js.map +7 -1
  259. package/build-module/hooks/index.js +27 -15
  260. package/build-module/hooks/index.js.map +7 -1
  261. package/build-module/hooks/memoize.js +6 -8
  262. package/build-module/hooks/memoize.js.map +7 -1
  263. package/build-module/hooks/use-entity-block-editor.js +80 -110
  264. package/build-module/hooks/use-entity-block-editor.js.map +7 -1
  265. package/build-module/hooks/use-entity-id.js +7 -19
  266. package/build-module/hooks/use-entity-id.js.map +7 -1
  267. package/build-module/hooks/use-entity-prop.js +31 -55
  268. package/build-module/hooks/use-entity-prop.js.map +7 -1
  269. package/build-module/hooks/use-entity-record.js +63 -148
  270. package/build-module/hooks/use-entity-record.js.map +7 -1
  271. package/build-module/hooks/use-entity-records.js +98 -131
  272. package/build-module/hooks/use-entity-records.js.map +7 -1
  273. package/build-module/hooks/use-query-select.js +27 -71
  274. package/build-module/hooks/use-query-select.js.map +7 -1
  275. package/build-module/hooks/use-resource-permissions.js +57 -136
  276. package/build-module/hooks/use-resource-permissions.js.map +7 -1
  277. package/build-module/index.js +49 -71
  278. package/build-module/index.js.map +7 -1
  279. package/build-module/lock-unlock.js +8 -7
  280. package/build-module/lock-unlock.js.map +7 -1
  281. package/build-module/locks/actions.js +8 -13
  282. package/build-module/locks/actions.js.map +7 -1
  283. package/build-module/locks/engine.js +17 -38
  284. package/build-module/locks/engine.js.map +7 -1
  285. package/build-module/locks/reducer.js +37 -59
  286. package/build-module/locks/reducer.js.map +7 -1
  287. package/build-module/locks/selectors.js +16 -23
  288. package/build-module/locks/selectors.js.map +7 -1
  289. package/build-module/locks/utils.js +15 -12
  290. package/build-module/locks/utils.js.map +7 -1
  291. package/build-module/name.js +5 -8
  292. package/build-module/name.js.map +7 -1
  293. package/build-module/private-actions.js +35 -69
  294. package/build-module/private-actions.js.map +7 -1
  295. package/build-module/private-apis.js +8 -8
  296. package/build-module/private-apis.js.map +7 -1
  297. package/build-module/private-selectors.js +167 -174
  298. package/build-module/private-selectors.js.map +7 -1
  299. package/build-module/queried-data/actions.js +11 -38
  300. package/build-module/queried-data/actions.js.map +7 -1
  301. package/build-module/queried-data/get-query-parts.js +20 -75
  302. package/build-module/queried-data/get-query-parts.js.map +7 -1
  303. package/build-module/queried-data/index.js +7 -4
  304. package/build-module/queried-data/index.js.map +7 -1
  305. package/build-module/queried-data/reducer.js +134 -185
  306. package/build-module/queried-data/reducer.js.map +7 -1
  307. package/build-module/queried-data/selectors.js +23 -78
  308. package/build-module/queried-data/selectors.js.map +7 -1
  309. package/build-module/reducer.js +243 -393
  310. package/build-module/reducer.js.map +7 -1
  311. package/build-module/resolvers.js +478 -549
  312. package/build-module/resolvers.js.map +7 -1
  313. package/build-module/selectors.js +410 -953
  314. package/build-module/selectors.js.map +7 -1
  315. package/build-module/sync.js +14 -17
  316. package/build-module/sync.js.map +7 -1
  317. package/build-module/types.js +1 -2
  318. package/build-module/types.js.map +7 -1
  319. package/build-module/utils/conservative-map-item.js +6 -22
  320. package/build-module/utils/conservative-map-item.js.map +7 -1
  321. package/build-module/utils/crdt-blocks.js +255 -0
  322. package/build-module/utils/crdt-blocks.js.map +7 -0
  323. package/build-module/utils/crdt.js +167 -0
  324. package/build-module/utils/crdt.js.map +7 -0
  325. package/build-module/utils/forward-resolver.js +6 -12
  326. package/build-module/utils/forward-resolver.js.map +7 -1
  327. package/build-module/utils/get-nested-value.js +9 -18
  328. package/build-module/utils/get-nested-value.js.map +7 -1
  329. package/build-module/utils/get-normalized-comma-separable.js +7 -13
  330. package/build-module/utils/get-normalized-comma-separable.js.map +7 -1
  331. package/build-module/utils/if-matching-action.js +7 -15
  332. package/build-module/utils/if-matching-action.js.map +7 -1
  333. package/build-module/utils/index.js +35 -14
  334. package/build-module/utils/index.js.map +7 -1
  335. package/build-module/utils/is-numeric-id.js +5 -9
  336. package/build-module/utils/is-numeric-id.js.map +7 -1
  337. package/build-module/utils/is-raw-attribute.js +5 -10
  338. package/build-module/utils/is-raw-attribute.js.map +7 -1
  339. package/build-module/utils/log-entity-deprecation.js +8 -31
  340. package/build-module/utils/log-entity-deprecation.js.map +7 -1
  341. package/build-module/utils/on-sub-key.js +8 -19
  342. package/build-module/utils/on-sub-key.js.map +7 -1
  343. package/build-module/utils/receive-intermediate-results.js +7 -2
  344. package/build-module/utils/receive-intermediate-results.js.map +7 -1
  345. package/build-module/utils/replace-action.js +6 -13
  346. package/build-module/utils/replace-action.js.map +7 -1
  347. package/build-module/utils/set-nested-value.js +8 -27
  348. package/build-module/utils/set-nested-value.js.map +7 -1
  349. package/build-module/utils/user-permissions.js +19 -9
  350. package/build-module/utils/user-permissions.js.map +7 -1
  351. package/build-module/utils/with-weak-map-cache.js +8 -18
  352. package/build-module/utils/with-weak-map-cache.js.map +7 -1
  353. package/build-types/actions.d.ts.map +1 -1
  354. package/build-types/entities.d.ts +0 -56
  355. package/build-types/entities.d.ts.map +1 -1
  356. package/build-types/index.d.ts.map +1 -1
  357. package/build-types/private-selectors.d.ts.map +1 -1
  358. package/build-types/resolvers.d.ts +3 -0
  359. package/build-types/resolvers.d.ts.map +1 -1
  360. package/build-types/selectors.d.ts.map +1 -1
  361. package/build-types/sync.d.ts +6 -1
  362. package/build-types/sync.d.ts.map +1 -1
  363. package/build-types/types.d.ts +9 -0
  364. package/build-types/types.d.ts.map +1 -1
  365. package/build-types/utils/crdt-blocks.d.ts +30 -0
  366. package/build-types/utils/crdt-blocks.d.ts.map +1 -0
  367. package/build-types/utils/crdt.d.ts +49 -0
  368. package/build-types/utils/crdt.d.ts.map +1 -0
  369. package/package.json +26 -19
  370. package/src/actions.js +56 -74
  371. package/src/entities.js +59 -113
  372. package/src/private-selectors.ts +32 -7
  373. package/src/resolvers.js +173 -120
  374. package/src/selectors.ts +0 -13
  375. package/src/sync.ts +12 -0
  376. package/src/test/resolvers.js +183 -0
  377. package/src/types.ts +12 -0
  378. package/src/utils/crdt-blocks.ts +503 -0
  379. package/src/utils/crdt.ts +310 -0
  380. package/src/utils/test/crdt-blocks.ts +375 -0
  381. package/src/utils/test/crdt.ts +254 -0
  382. package/tsconfig.tsbuildinfo +1 -1
  383. package/src/sync.js +0 -27
@@ -3,7 +3,17 @@
3
3
  */
4
4
  import triggerFetch from '@wordpress/api-fetch';
5
5
 
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import { syncManager } from '../sync';
10
+
6
11
  jest.mock( '@wordpress/api-fetch' );
12
+ jest.mock( '../sync', () => ( {
13
+ syncManager: {
14
+ load: jest.fn(),
15
+ },
16
+ } ) );
7
17
 
8
18
  /**
9
19
  * Internal dependencies
@@ -41,6 +51,11 @@ describe( 'getEntityRecord', () => {
41
51
  finishResolutions: jest.fn(),
42
52
  } );
43
53
  triggerFetch.mockReset();
54
+ syncManager.load.mockClear();
55
+ } );
56
+
57
+ afterEach( () => {
58
+ delete window.__experimentalEnableSync;
44
59
  } );
45
60
 
46
61
  it( 'yields with requested post type', async () => {
@@ -111,6 +126,174 @@ describe( 'getEntityRecord', () => {
111
126
  1
112
127
  );
113
128
  } );
129
+
130
+ it( 'loads entity with sync manager when __experimentalEnableSync is true', async () => {
131
+ const POST_RECORD = { id: 1, title: 'Test Post' };
132
+ const POST_RESPONSE = {
133
+ json: () => Promise.resolve( POST_RECORD ),
134
+ };
135
+ const ENTITIES_WITH_SYNC = [
136
+ {
137
+ name: 'post',
138
+ kind: 'postType',
139
+ baseURL: '/wp/v2/posts',
140
+ baseURLParams: { context: 'edit' },
141
+ syncConfig: {},
142
+ },
143
+ ];
144
+
145
+ window.__experimentalEnableSync = true;
146
+
147
+ const resolveSelectWithSync = {
148
+ getEntitiesConfig: jest.fn( () => ENTITIES_WITH_SYNC ),
149
+ getEditedEntityRecord: jest.fn(),
150
+ };
151
+
152
+ triggerFetch.mockImplementation( () => POST_RESPONSE );
153
+
154
+ await getEntityRecord(
155
+ 'postType',
156
+ 'post',
157
+ 1
158
+ )( {
159
+ dispatch,
160
+ registry,
161
+ resolveSelect: resolveSelectWithSync,
162
+ } );
163
+
164
+ // Verify load was called with correct arguments.
165
+ expect( syncManager.load ).toHaveBeenCalledTimes( 1 );
166
+ expect( syncManager.load ).toHaveBeenCalledWith(
167
+ {},
168
+ 'postType/post',
169
+ 1,
170
+ POST_RECORD,
171
+ {
172
+ editRecord: expect.any( Function ),
173
+ getEditedRecord: expect.any( Function ),
174
+ }
175
+ );
176
+ } );
177
+
178
+ it( 'provides transient properties when read/write config is supplied', async () => {
179
+ const POST_RECORD = { id: 1, title: 'Test Post' };
180
+ const POST_RESPONSE = {
181
+ json: () => Promise.resolve( POST_RECORD ),
182
+ };
183
+ const ENTITIES_WITH_SYNC = [
184
+ {
185
+ name: 'post',
186
+ kind: 'postType',
187
+ baseURL: '/wp/v2/posts',
188
+ baseURLParams: { context: 'edit' },
189
+ syncConfig: {},
190
+ transientEdits: {
191
+ foo: {
192
+ read: () => 'bar',
193
+ },
194
+ },
195
+ },
196
+ ];
197
+
198
+ window.__experimentalEnableSync = true;
199
+
200
+ const resolveSelectWithSync = {
201
+ getEntitiesConfig: jest.fn( () => ENTITIES_WITH_SYNC ),
202
+ getEditedEntityRecord: jest.fn(),
203
+ };
204
+
205
+ triggerFetch.mockImplementation( () => POST_RESPONSE );
206
+
207
+ await getEntityRecord(
208
+ 'postType',
209
+ 'post',
210
+ 1
211
+ )( {
212
+ dispatch,
213
+ registry,
214
+ resolveSelect: resolveSelectWithSync,
215
+ } );
216
+
217
+ // Verify load was called with correct arguments.
218
+ expect( syncManager.load ).toHaveBeenCalledTimes( 1 );
219
+ expect( syncManager.load ).toHaveBeenCalledWith(
220
+ {},
221
+ 'postType/post',
222
+ 1,
223
+ { ...POST_RECORD, foo: 'bar' },
224
+ {
225
+ editRecord: expect.any( Function ),
226
+ getEditedRecord: expect.any( Function ),
227
+ }
228
+ );
229
+ } );
230
+
231
+ it( 'does not load entity when query is present', async () => {
232
+ const POST_RECORD = { id: 1, title: 'Test Post' };
233
+ const POST_RESPONSE = {
234
+ json: () => Promise.resolve( POST_RECORD ),
235
+ };
236
+ const ENTITIES_WITH_SYNC = [
237
+ {
238
+ name: 'post',
239
+ kind: 'postType',
240
+ baseURL: '/wp/v2/posts',
241
+ baseURLParams: { context: 'edit' },
242
+ syncConfig: {},
243
+ },
244
+ ];
245
+
246
+ window.__experimentalEnableSync = true;
247
+
248
+ const resolveSelectWithSync = {
249
+ getEntitiesConfig: jest.fn( () => ENTITIES_WITH_SYNC ),
250
+ };
251
+
252
+ triggerFetch.mockImplementation( () => POST_RESPONSE );
253
+
254
+ // Call with a query parameter
255
+ await getEntityRecord( 'postType', 'post', 1, { foo: 'bar' } )( {
256
+ dispatch,
257
+ registry,
258
+ resolveSelect: resolveSelectWithSync,
259
+ } );
260
+
261
+ expect( syncManager.load ).not.toHaveBeenCalled();
262
+ } );
263
+
264
+ it( 'does not load entity when __experimentalEnableSync is undefined', async () => {
265
+ const POST_RECORD = { id: 1, title: 'Test Post' };
266
+ const POST_RESPONSE = {
267
+ json: () => Promise.resolve( POST_RECORD ),
268
+ };
269
+ const ENTITIES_WITH_SYNC = [
270
+ {
271
+ name: 'post',
272
+ kind: 'postType',
273
+ baseURL: '/wp/v2/posts',
274
+ baseURLParams: { context: 'edit' },
275
+ syncConfig: {},
276
+ },
277
+ ];
278
+
279
+ const resolveSelectWithSync = {
280
+ getEntitiesConfig: jest.fn( () => ENTITIES_WITH_SYNC ),
281
+ };
282
+
283
+ triggerFetch.mockImplementation( () => POST_RESPONSE );
284
+
285
+ await getEntityRecord(
286
+ 'postType',
287
+ 'post',
288
+ 1
289
+ )( {
290
+ dispatch,
291
+ registry,
292
+ resolveSelect: resolveSelectWithSync,
293
+ } );
294
+
295
+ expect( syncManager.load ).not.toHaveBeenCalled();
296
+ } );
114
297
  } );
115
298
 
116
299
  describe( 'getEntityRecords', () => {
package/src/types.ts CHANGED
@@ -1,3 +1,15 @@
1
1
  export interface AnyFunction {
2
2
  ( ...args: any[] ): any;
3
3
  }
4
+
5
+ // Avoid a circular dependency with @wordpress/editor
6
+ export interface WPBlockSelection {
7
+ clientId: string;
8
+ attributeKey: string;
9
+ offset: number;
10
+ }
11
+
12
+ export interface WPSelection {
13
+ selectionEnd: WPBlockSelection;
14
+ selectionStart: WPBlockSelection;
15
+ }
@@ -0,0 +1,503 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { v4 as uuidv4 } from 'uuid';
5
+ import fastDeepEqual from 'fast-deep-equal/es6';
6
+
7
+ /**
8
+ * WordPress dependencies
9
+ */
10
+ import { RichTextData } from '@wordpress/rich-text';
11
+ import { Y } from '@wordpress/sync';
12
+
13
+ // @ts-expect-error No exported types.
14
+ import { getBlockTypes } from '@wordpress/blocks';
15
+
16
+ /**
17
+ * Internal dependencies
18
+ */
19
+ import type { WPBlockSelection } from '../types';
20
+
21
+ interface BlockAttributes {
22
+ [ key: string ]: unknown;
23
+ }
24
+
25
+ interface BlockType {
26
+ name: string;
27
+ attributes?: Record< string, { type?: string } >;
28
+ }
29
+
30
+ export interface Block {
31
+ attributes: BlockAttributes;
32
+ clientId?: string;
33
+ innerBlocks: Block[];
34
+ originalContent?: string; // unserializable
35
+ validationIssues?: string[]; // unserializable
36
+ name: string;
37
+ }
38
+
39
+ export type YBlock = Y.Map<
40
+ /* name, clientId, and originalContent are strings. */
41
+ | string
42
+ /* validationIssues? is an array of strings. */
43
+ | string[]
44
+ /* attributes is a Y.Map< unknown >. */
45
+ | YBlockAttributes
46
+ /* innerBlocks is a Y.Array< YBlock >. */
47
+ | YBlocks
48
+ >;
49
+
50
+ export type YBlocks = Y.Array< YBlock >;
51
+ export type YBlockAttributes = Y.Map< Y.Text | unknown >;
52
+
53
+ // The Y.Map type is not easy to work with. The generic type it accepts represents
54
+ // the possible values of the map, which are varied in our case. This type is
55
+ // accurate, but will require aggressive type narrowing when the map values are
56
+ // accessed -- or type casting with `as`.
57
+ // export type YBlock = Y.Map< Block[ keyof Block ] >;
58
+
59
+ const serializableBlocksCache = new WeakMap< WeakKey, Block[] >();
60
+
61
+ function makeBlockAttributesSerializable(
62
+ attributes: BlockAttributes
63
+ ): BlockAttributes {
64
+ const newAttributes = { ...attributes };
65
+ for ( const [ key, value ] of Object.entries( attributes ) ) {
66
+ if ( value instanceof RichTextData ) {
67
+ newAttributes[ key ] = value.valueOf();
68
+ }
69
+ }
70
+ return newAttributes;
71
+ }
72
+
73
+ function makeBlocksSerializable( blocks: Block[] | YBlocks ): Block[] {
74
+ return blocks.map( ( block: Block | YBlock ) => {
75
+ const blockAsJson = block instanceof Y.Map ? block.toJSON() : block;
76
+ const { name, innerBlocks, attributes, ...rest } = blockAsJson;
77
+ delete rest.validationIssues;
78
+ delete rest.originalContent;
79
+ // delete rest.isValid
80
+ return {
81
+ ...rest,
82
+ name,
83
+ attributes: makeBlockAttributesSerializable( attributes ),
84
+ innerBlocks: makeBlocksSerializable( innerBlocks ),
85
+ };
86
+ } );
87
+ }
88
+
89
+ /**
90
+ * @param {any} gblock
91
+ * @param {Y.Map} yblock
92
+ */
93
+ function areBlocksEqual( gblock: Block, yblock: YBlock ): boolean {
94
+ const yblockAsJson = yblock.toJSON();
95
+
96
+ // we must not sync clientId, as this can't be generated consistently and
97
+ // hence will lead to merge conflicts.
98
+ const overwrites = {
99
+ innerBlocks: null,
100
+ clientId: null,
101
+ };
102
+ const res = fastDeepEqual(
103
+ Object.assign( {}, gblock, overwrites ),
104
+ Object.assign( {}, yblockAsJson, overwrites )
105
+ );
106
+ const inners = gblock.innerBlocks || [];
107
+ const yinners = yblock.get( 'innerBlocks' ) as YBlocks;
108
+ return (
109
+ res &&
110
+ inners.length === yinners.length &&
111
+ inners.every( ( block: Block, i: number ) =>
112
+ areBlocksEqual( block, yinners.get( i ) )
113
+ )
114
+ );
115
+ }
116
+
117
+ function createNewYAttributeMap(
118
+ blockName: string,
119
+ attributes: BlockAttributes
120
+ ): YBlockAttributes {
121
+ return new Y.Map(
122
+ Object.entries( attributes ).map(
123
+ ( [ attributeName, attributeValue ] ) => {
124
+ return [
125
+ attributeName,
126
+ createNewYAttributeValue(
127
+ blockName,
128
+ attributeName,
129
+ attributeValue
130
+ ),
131
+ ];
132
+ }
133
+ )
134
+ );
135
+ }
136
+
137
+ function createNewYAttributeValue(
138
+ blockName: string,
139
+ attributeName: string,
140
+ attributeValue: unknown
141
+ ): Y.Text | unknown {
142
+ const isRichText = isRichTextAttribute( blockName, attributeName );
143
+
144
+ if ( isRichText ) {
145
+ return new Y.Text( attributeValue?.toString() ?? '' );
146
+ }
147
+
148
+ return attributeValue;
149
+ }
150
+
151
+ function createNewYBlock( block: Block ): YBlock {
152
+ return new Y.Map(
153
+ Object.entries( block ).map( ( [ key, value ] ) => {
154
+ switch ( key ) {
155
+ case 'attributes': {
156
+ return [ key, createNewYAttributeMap( block.name, value ) ];
157
+ }
158
+
159
+ case 'innerBlocks': {
160
+ const innerBlocks = new Y.Array();
161
+
162
+ // If not an array, set to empty Y.Array.
163
+ if ( ! Array.isArray( value ) ) {
164
+ return [ key, innerBlocks ];
165
+ }
166
+
167
+ innerBlocks.insert(
168
+ 0,
169
+ value.map( ( innerBlock: Block ) =>
170
+ createNewYBlock( innerBlock )
171
+ )
172
+ );
173
+
174
+ return [ key, innerBlocks ];
175
+ }
176
+
177
+ default:
178
+ return [ key, value ];
179
+ }
180
+ } )
181
+ );
182
+ }
183
+
184
+ /**
185
+ * Merge incoming block data into the local Y.Doc.
186
+ * This function is called to sync local block changes to a shared Y.Doc.
187
+ *
188
+ * @param yblocks The blocks in the local Y.Doc.
189
+ * @param incomingBlocks Gutenberg blocks being synced, either from a peer or from the local editor.
190
+ * @param lastSelection The last cursor position, used for hinting the diff algorithm.
191
+ */
192
+ export function mergeCrdtBlocks(
193
+ yblocks: YBlocks,
194
+ incomingBlocks: Block[],
195
+ lastSelection: WPBlockSelection | null
196
+ ): void {
197
+ // Ensure we are working with serializable block data.
198
+ if ( ! serializableBlocksCache.has( incomingBlocks ) ) {
199
+ serializableBlocksCache.set(
200
+ incomingBlocks,
201
+ makeBlocksSerializable( incomingBlocks )
202
+ );
203
+ }
204
+ const allBlocks = serializableBlocksCache.get( incomingBlocks ) ?? [];
205
+
206
+ // Ensure we skip blocks that we don't want to sync at the moment
207
+ const blocksToSync = allBlocks.filter( ( block ) =>
208
+ shouldBlockBeSynced( block )
209
+ );
210
+
211
+ // This is a rudimentary diff implementation similar to the y-prosemirror diffing
212
+ // approach.
213
+ // A better implementation would also diff the textual content and represent it
214
+ // using a Y.Text type.
215
+ // However, at this time it makes more sense to keep this algorithm generic to
216
+ // support all kinds of block types.
217
+ // Ideally, we ensure that block data structure have a consistent data format.
218
+ // E.g.:
219
+ // - textual content (using rich-text formatting?) may always be stored under `block.text`
220
+ // - local information that shouldn't be shared (e.g. clientId or isDragging) is stored under `block.private`
221
+ //
222
+ // @credit Kevin Jahns (dmonad)
223
+ // @link https://github.com/WordPress/gutenberg/pull/68483
224
+ const numOfCommonEntries = Math.min(
225
+ blocksToSync.length ?? 0,
226
+ yblocks.length
227
+ );
228
+
229
+ let left = 0;
230
+ let right = 0;
231
+
232
+ // skip equal blocks from left
233
+ for (
234
+ ;
235
+ left < numOfCommonEntries &&
236
+ areBlocksEqual( blocksToSync[ left ], yblocks.get( left ) );
237
+ left++
238
+ ) {
239
+ /* nop */
240
+ }
241
+
242
+ // skip equal blocks from right
243
+ for (
244
+ ;
245
+ right < numOfCommonEntries - left &&
246
+ areBlocksEqual(
247
+ blocksToSync[ blocksToSync.length - right - 1 ],
248
+ yblocks.get( yblocks.length - right - 1 )
249
+ );
250
+ right++
251
+ ) {
252
+ /* nop */
253
+ }
254
+
255
+ const numOfUpdatesNeeded = numOfCommonEntries - left - right;
256
+ const numOfInsertionsNeeded = Math.max(
257
+ 0,
258
+ blocksToSync.length - yblocks.length
259
+ );
260
+ const numOfDeletionsNeeded = Math.max(
261
+ 0,
262
+ yblocks.length - blocksToSync.length
263
+ );
264
+
265
+ // updates
266
+ for ( let i = 0; i < numOfUpdatesNeeded; i++, left++ ) {
267
+ const block = blocksToSync[ left ];
268
+ const yblock = yblocks.get( left );
269
+ Object.entries( block ).forEach( ( [ key, value ] ) => {
270
+ switch ( key ) {
271
+ case 'attributes': {
272
+ const currentAttributes = yblock.get(
273
+ key
274
+ ) as YBlockAttributes;
275
+
276
+ // If attributes are not set on the yblock, use the new values.
277
+ if ( ! currentAttributes ) {
278
+ yblock.set(
279
+ key,
280
+ createNewYAttributeMap( block.name, value )
281
+ );
282
+ break;
283
+ }
284
+
285
+ Object.entries( value ).forEach(
286
+ ( [ attributeName, attributeValue ] ) => {
287
+ if (
288
+ fastDeepEqual(
289
+ currentAttributes?.get( attributeName ),
290
+ attributeValue
291
+ )
292
+ ) {
293
+ return;
294
+ }
295
+
296
+ const isRichText = isRichTextAttribute(
297
+ block.name,
298
+ attributeName
299
+ );
300
+
301
+ if (
302
+ isRichText &&
303
+ 'string' === typeof attributeValue
304
+ ) {
305
+ // Rich text values are stored as persistent Y.Text instances.
306
+ // Update the value with a delta in place.
307
+ const blockYText = currentAttributes.get(
308
+ attributeName
309
+ ) as Y.Text;
310
+
311
+ mergeRichTextUpdate(
312
+ blockYText,
313
+ attributeValue,
314
+ lastSelection
315
+ );
316
+ } else {
317
+ currentAttributes.set(
318
+ attributeName,
319
+ createNewYAttributeValue(
320
+ block.name,
321
+ attributeName,
322
+ attributeValue
323
+ )
324
+ );
325
+ }
326
+ }
327
+ );
328
+
329
+ // Delete any attributes that are no longer present.
330
+ currentAttributes.forEach(
331
+ ( _attrValue: unknown, attrName: string ) => {
332
+ if ( ! value.hasOwnProperty( attrName ) ) {
333
+ currentAttributes.delete( attrName );
334
+ }
335
+ }
336
+ );
337
+
338
+ break;
339
+ }
340
+
341
+ case 'innerBlocks': {
342
+ // Recursively merge innerBlocks
343
+ const yInnerBlocks = yblock.get( key ) as Y.Array< YBlock >;
344
+ mergeCrdtBlocks( yInnerBlocks, value ?? [], lastSelection );
345
+ break;
346
+ }
347
+
348
+ default:
349
+ if ( ! fastDeepEqual( block[ key ], yblock.get( key ) ) ) {
350
+ yblock.set( key, value );
351
+ }
352
+ }
353
+ } );
354
+ yblock.forEach( ( _v, k ) => {
355
+ if ( ! block.hasOwnProperty( k ) ) {
356
+ yblock.delete( k );
357
+ }
358
+ } );
359
+ }
360
+
361
+ // deletes
362
+ yblocks.delete( left, numOfDeletionsNeeded );
363
+
364
+ // inserts
365
+ for ( let i = 0; i < numOfInsertionsNeeded; i++, left++ ) {
366
+ const newBlock = [ createNewYBlock( blocksToSync[ left ] ) ];
367
+
368
+ yblocks.insert( left, newBlock );
369
+ }
370
+
371
+ // remove duplicate clientids
372
+ const knownClientIds = new Set< string >();
373
+ for ( let j = 0; j < yblocks.length; j++ ) {
374
+ const yblock: YBlock = yblocks.get( j );
375
+
376
+ let clientId: string = yblock.get( 'clientId' ) as string;
377
+
378
+ if ( knownClientIds.has( clientId ) ) {
379
+ clientId = uuidv4();
380
+ yblock.set( 'clientId', clientId );
381
+ }
382
+ knownClientIds.add( clientId );
383
+ }
384
+ }
385
+
386
+ /**
387
+ * Determine if a block should be synced.
388
+ *
389
+ * Ex: A gallery block should not be synced until the images have been
390
+ * uploaded to WordPress, and their url is available. Before that,
391
+ * it's not possible to access the blobs on a client as those are
392
+ * local.
393
+ *
394
+ * @param block The block to check.
395
+ * @return True if the block should be synced, false otherwise.
396
+ */
397
+ function shouldBlockBeSynced( block: Block ): boolean {
398
+ // Verify that the gallery block is ready to be synced.
399
+ // This means that, all images have had their blobs converted to full URLs.
400
+ // Checking for only the blobs ensures that blocks that have just been inserted work as well.
401
+ if ( 'core/gallery' === block.name ) {
402
+ return ! block.innerBlocks.some(
403
+ ( innerBlock ) =>
404
+ innerBlock.attributes && innerBlock.attributes.blob
405
+ );
406
+ }
407
+
408
+ // Allow all other blocks to be synced.
409
+ return true;
410
+ }
411
+
412
+ // Cache rich-text attributes for all block types.
413
+ let cachedRichTextAttributes: Map< string, Map< string, true > >;
414
+
415
+ /**
416
+ * Given a block name and attribute key, return true if the attribute is rich-text typed.
417
+ *
418
+ * @param blockName The name of the block, e.g. 'core/paragraph'.
419
+ * @param attributeName The name of the attribute to check, e.g. 'content'.
420
+ * @return True if the attribute is rich-text typed, false otherwise.
421
+ */
422
+ function isRichTextAttribute(
423
+ blockName: string,
424
+ attributeName: string
425
+ ): boolean {
426
+ if ( ! cachedRichTextAttributes ) {
427
+ // Parse the attributes for all blocks once.
428
+ cachedRichTextAttributes = new Map< string, Map< string, true > >();
429
+
430
+ for ( const blockType of getBlockTypes() as BlockType[] ) {
431
+ const richTextAttributeMap = new Map< string, true >();
432
+
433
+ for ( const [ name, definition ] of Object.entries(
434
+ blockType.attributes ?? {}
435
+ ) ) {
436
+ if ( 'rich-text' === definition.type ) {
437
+ richTextAttributeMap.set( name, true );
438
+ }
439
+ }
440
+
441
+ cachedRichTextAttributes.set(
442
+ blockType.name,
443
+ richTextAttributeMap
444
+ );
445
+ }
446
+ }
447
+
448
+ return (
449
+ cachedRichTextAttributes.get( blockName )?.has( attributeName ) ?? false
450
+ );
451
+ }
452
+
453
+ /**
454
+ * Given a Y.Text object and an updated string value, diff the new value and
455
+ * apply the delta to the Y.Text.
456
+ *
457
+ * @param blockYText The Y.Text to update.
458
+ * @param updatedValue The updated value.
459
+ * @param lastSelection The last cursor position before this update, used to hint the diff algorithm.
460
+ */
461
+ function mergeRichTextUpdate(
462
+ blockYText: Y.Text,
463
+ updatedValue: string,
464
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
465
+ lastSelection: WPBlockSelection | null
466
+ ): void {
467
+ // TODO
468
+ // ====
469
+ // Gutenberg does not use Yjs shared types natively, so we can only subscribe
470
+ // to changes from store and apply them to Yjs types that we create and
471
+ // manage. Crucially, for rich-text attributes, we do not receive granular
472
+ // string updates; we get the new full string value on each change, even when
473
+ // only a single character changed.
474
+ //
475
+ // The code below allows us to compute a delta between the current and new
476
+ // value, then apply it to the Y.Text. However, it relies on a library
477
+ // (quill-delta) with a licensing issue that we are working to resolve.
478
+ //
479
+ // For now, we simply replace the full text content on each change.
480
+ //
481
+ // if ( ! localDoc ) {
482
+ // // Y.Text must be attached to a Y.Doc to be able to do operations on it.
483
+ // // Create a temporary Y.Text attached to a local Y.Doc for delta computation.
484
+ // localDoc = new Y.Doc();
485
+ // }
486
+
487
+ // const localYText = localDoc.getText( 'temporary-text' );
488
+ // localYText.delete( 0, localYText.length );
489
+ // localYText.insert( 0, updatedValue );
490
+
491
+ // const currentValueAsDelta = new Delta( blockYText.toDelta() );
492
+ // const updatedValueAsDelta = new Delta( localYText.toDelta() );
493
+
494
+ // const deltaDiff = currentValueAsDelta.diff(
495
+ // updatedValueAsDelta,
496
+ // lastSelection?.offset
497
+ // );
498
+
499
+ // blockYText.applyDelta( deltaDiff.ops );
500
+
501
+ blockYText.delete( 0, blockYText.toString().length );
502
+ blockYText.insert( 0, updatedValue );
503
+ }