@wordpress/core-data 6.16.0 → 6.17.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 (236) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/actions.js +69 -134
  3. package/build/actions.js.map +1 -1
  4. package/build/batch/create-batch.js +0 -22
  5. package/build/batch/create-batch.js.map +1 -1
  6. package/build/batch/default-processor.js +3 -15
  7. package/build/batch/default-processor.js.map +1 -1
  8. package/build/batch/index.js +0 -3
  9. package/build/batch/index.js.map +1 -1
  10. package/build/entities.js +109 -36
  11. package/build/entities.js.map +1 -1
  12. package/build/entity-provider.js +34 -53
  13. package/build/entity-provider.js.map +1 -1
  14. package/build/entity-types/attachment.js.map +1 -1
  15. package/build/entity-types/base-entity-records.js +0 -3
  16. package/build/entity-types/base-entity-records.js.map +1 -1
  17. package/build/entity-types/comment.js.map +1 -1
  18. package/build/entity-types/helpers.js.map +1 -1
  19. package/build/entity-types/index.js.map +1 -1
  20. package/build/entity-types/menu-location.js.map +1 -1
  21. package/build/entity-types/nav-menu-item.js.map +1 -1
  22. package/build/entity-types/nav-menu.js.map +1 -1
  23. package/build/entity-types/page.js.map +1 -1
  24. package/build/entity-types/plugin.js.map +1 -1
  25. package/build/entity-types/post.js.map +1 -1
  26. package/build/entity-types/settings.js.map +1 -1
  27. package/build/entity-types/sidebar.js.map +1 -1
  28. package/build/entity-types/taxonomy.js.map +1 -1
  29. package/build/entity-types/theme.js.map +1 -1
  30. package/build/entity-types/type.js.map +1 -1
  31. package/build/entity-types/user.js.map +1 -1
  32. package/build/entity-types/widget-type.js.map +1 -1
  33. package/build/entity-types/widget.js.map +1 -1
  34. package/build/entity-types/wp-template-part.js.map +1 -1
  35. package/build/entity-types/wp-template.js.map +1 -1
  36. package/build/fetch/__experimental-fetch-link-suggestions.js +14 -20
  37. package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
  38. package/build/fetch/__experimental-fetch-url-data.js +4 -13
  39. package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
  40. package/build/fetch/index.js +0 -3
  41. package/build/fetch/index.js.map +1 -1
  42. package/build/hooks/constants.js +4 -5
  43. package/build/hooks/constants.js.map +1 -1
  44. package/build/hooks/index.js +0 -5
  45. package/build/hooks/index.js.map +1 -1
  46. package/build/hooks/memoize.js +0 -3
  47. package/build/hooks/memoize.js.map +1 -1
  48. package/build/hooks/use-entity-record.js +0 -9
  49. package/build/hooks/use-entity-record.js.map +1 -1
  50. package/build/hooks/use-entity-records.js +2 -9
  51. package/build/hooks/use-entity-records.js.map +1 -1
  52. package/build/hooks/use-query-select.js +1 -13
  53. package/build/hooks/use-query-select.js.map +1 -1
  54. package/build/hooks/use-resource-permissions.js +0 -15
  55. package/build/hooks/use-resource-permissions.js.map +1 -1
  56. package/build/index.js +9 -41
  57. package/build/index.js.map +1 -1
  58. package/build/locks/actions.js +1 -6
  59. package/build/locks/actions.js.map +1 -1
  60. package/build/locks/engine.js +1 -9
  61. package/build/locks/engine.js.map +1 -1
  62. package/build/locks/reducer.js +8 -9
  63. package/build/locks/reducer.js.map +1 -1
  64. package/build/locks/selectors.js +8 -11
  65. package/build/locks/selectors.js.map +1 -1
  66. package/build/locks/utils.js +4 -19
  67. package/build/locks/utils.js.map +1 -1
  68. package/build/name.js +0 -1
  69. package/build/name.js.map +1 -1
  70. package/build/private-apis.js +1 -2
  71. package/build/private-apis.js.map +1 -1
  72. package/build/private-selectors.js +2 -5
  73. package/build/private-selectors.js.map +1 -1
  74. package/build/queried-data/actions.js +4 -6
  75. package/build/queried-data/actions.js.map +1 -1
  76. package/build/queried-data/get-query-parts.js +11 -24
  77. package/build/queried-data/get-query-parts.js.map +1 -1
  78. package/build/queried-data/index.js +0 -6
  79. package/build/queried-data/index.js.map +1 -1
  80. package/build/queried-data/reducer.js +33 -51
  81. package/build/queried-data/reducer.js.map +1 -1
  82. package/build/queried-data/selectors.js +4 -25
  83. package/build/queried-data/selectors.js.map +1 -1
  84. package/build/reducer.js +76 -128
  85. package/build/reducer.js.map +1 -1
  86. package/build/resolvers.js +91 -133
  87. package/build/resolvers.js.map +1 -1
  88. package/build/selectors.js +51 -145
  89. package/build/selectors.js.map +1 -1
  90. package/build/sync.js +19 -0
  91. package/build/sync.js.map +1 -0
  92. package/build/types.js.map +1 -1
  93. package/build/utils/conservative-map-item.js +3 -10
  94. package/build/utils/conservative-map-item.js.map +1 -1
  95. package/build/utils/forward-resolver.js +0 -2
  96. package/build/utils/forward-resolver.js.map +1 -1
  97. package/build/utils/get-normalized-comma-separable.js +0 -3
  98. package/build/utils/get-normalized-comma-separable.js.map +1 -1
  99. package/build/utils/if-matching-action.js +0 -3
  100. package/build/utils/if-matching-action.js.map +1 -1
  101. package/build/utils/index.js +0 -10
  102. package/build/utils/index.js.map +1 -1
  103. package/build/utils/is-raw-attribute.js +0 -1
  104. package/build/utils/is-raw-attribute.js.map +1 -1
  105. package/build/utils/on-sub-key.js +5 -9
  106. package/build/utils/on-sub-key.js.map +1 -1
  107. package/build/utils/replace-action.js +0 -2
  108. package/build/utils/replace-action.js.map +1 -1
  109. package/build/utils/set-nested-value.js +0 -4
  110. package/build/utils/set-nested-value.js.map +1 -1
  111. package/build/utils/with-weak-map-cache.js +3 -6
  112. package/build/utils/with-weak-map-cache.js.map +1 -1
  113. package/build-module/actions.js +72 -95
  114. package/build-module/actions.js.map +1 -1
  115. package/build-module/batch/create-batch.js +1 -20
  116. package/build-module/batch/create-batch.js.map +1 -1
  117. package/build-module/batch/default-processor.js +4 -13
  118. package/build-module/batch/default-processor.js.map +1 -1
  119. package/build-module/batch/index.js.map +1 -1
  120. package/build-module/entities.js +109 -25
  121. package/build-module/entities.js.map +1 -1
  122. package/build-module/entity-provider.js +35 -45
  123. package/build-module/entity-provider.js.map +1 -1
  124. package/build-module/entity-types/attachment.js.map +1 -1
  125. package/build-module/entity-types/base-entity-records.js +0 -2
  126. package/build-module/entity-types/base-entity-records.js.map +1 -1
  127. package/build-module/entity-types/comment.js.map +1 -1
  128. package/build-module/entity-types/helpers.js.map +1 -1
  129. package/build-module/entity-types/index.js.map +1 -1
  130. package/build-module/entity-types/menu-location.js.map +1 -1
  131. package/build-module/entity-types/nav-menu-item.js.map +1 -1
  132. package/build-module/entity-types/nav-menu.js.map +1 -1
  133. package/build-module/entity-types/page.js.map +1 -1
  134. package/build-module/entity-types/plugin.js.map +1 -1
  135. package/build-module/entity-types/post.js.map +1 -1
  136. package/build-module/entity-types/settings.js.map +1 -1
  137. package/build-module/entity-types/sidebar.js.map +1 -1
  138. package/build-module/entity-types/taxonomy.js.map +1 -1
  139. package/build-module/entity-types/theme.js.map +1 -1
  140. package/build-module/entity-types/type.js.map +1 -1
  141. package/build-module/entity-types/user.js.map +1 -1
  142. package/build-module/entity-types/widget-type.js.map +1 -1
  143. package/build-module/entity-types/widget.js.map +1 -1
  144. package/build-module/entity-types/wp-template-part.js.map +1 -1
  145. package/build-module/entity-types/wp-template.js.map +1 -1
  146. package/build-module/fetch/__experimental-fetch-link-suggestions.js +15 -15
  147. package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
  148. package/build-module/fetch/__experimental-fetch-url-data.js +5 -10
  149. package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
  150. package/build-module/fetch/index.js.map +1 -1
  151. package/build-module/hooks/constants.js +3 -4
  152. package/build-module/hooks/constants.js.map +1 -1
  153. package/build-module/hooks/index.js.map +1 -1
  154. package/build-module/hooks/memoize.js +2 -1
  155. package/build-module/hooks/memoize.js.map +1 -1
  156. package/build-module/hooks/use-entity-record.js +1 -3
  157. package/build-module/hooks/use-entity-record.js.map +1 -1
  158. package/build-module/hooks/use-entity-records.js +2 -3
  159. package/build-module/hooks/use-entity-records.js.map +1 -1
  160. package/build-module/hooks/use-query-select.js +1 -9
  161. package/build-module/hooks/use-query-select.js.map +1 -1
  162. package/build-module/hooks/use-resource-permissions.js +1 -10
  163. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  164. package/build-module/index.js +11 -19
  165. package/build-module/index.js.map +1 -1
  166. package/build-module/locks/actions.js +0 -3
  167. package/build-module/locks/actions.js.map +1 -1
  168. package/build-module/locks/engine.js +0 -5
  169. package/build-module/locks/engine.js.map +1 -1
  170. package/build-module/locks/reducer.js +7 -7
  171. package/build-module/locks/reducer.js.map +1 -1
  172. package/build-module/locks/selectors.js +7 -8
  173. package/build-module/locks/selectors.js.map +1 -1
  174. package/build-module/locks/utils.js +4 -14
  175. package/build-module/locks/utils.js.map +1 -1
  176. package/build-module/name.js.map +1 -1
  177. package/build-module/private-apis.js.map +1 -1
  178. package/build-module/private-selectors.js +2 -2
  179. package/build-module/private-selectors.js.map +1 -1
  180. package/build-module/queried-data/actions.js +4 -3
  181. package/build-module/queried-data/actions.js.map +1 -1
  182. package/build-module/queried-data/get-query-parts.js +13 -21
  183. package/build-module/queried-data/get-query-parts.js.map +1 -1
  184. package/build-module/queried-data/index.js.map +1 -1
  185. package/build-module/queried-data/reducer.js +33 -42
  186. package/build-module/queried-data/reducer.js.map +1 -1
  187. package/build-module/queried-data/selectors.js +6 -21
  188. package/build-module/queried-data/selectors.js.map +1 -1
  189. package/build-module/reducer.js +79 -104
  190. package/build-module/reducer.js.map +1 -1
  191. package/build-module/resolvers.js +94 -96
  192. package/build-module/resolvers.js.map +1 -1
  193. package/build-module/selectors.js +56 -92
  194. package/build-module/selectors.js.map +1 -1
  195. package/build-module/sync.js +12 -0
  196. package/build-module/sync.js.map +1 -0
  197. package/build-module/types.js.map +1 -1
  198. package/build-module/utils/conservative-map-item.js +4 -8
  199. package/build-module/utils/conservative-map-item.js.map +1 -1
  200. package/build-module/utils/forward-resolver.js +0 -1
  201. package/build-module/utils/forward-resolver.js.map +1 -1
  202. package/build-module/utils/get-normalized-comma-separable.js +0 -2
  203. package/build-module/utils/get-normalized-comma-separable.js.map +1 -1
  204. package/build-module/utils/if-matching-action.js +0 -2
  205. package/build-module/utils/if-matching-action.js.map +1 -1
  206. package/build-module/utils/index.js.map +1 -1
  207. package/build-module/utils/is-raw-attribute.js.map +1 -1
  208. package/build-module/utils/on-sub-key.js +5 -7
  209. package/build-module/utils/on-sub-key.js.map +1 -1
  210. package/build-module/utils/replace-action.js +0 -1
  211. package/build-module/utils/replace-action.js.map +1 -1
  212. package/build-module/utils/set-nested-value.js +0 -3
  213. package/build-module/utils/set-nested-value.js.map +1 -1
  214. package/build-module/utils/with-weak-map-cache.js +3 -5
  215. package/build-module/utils/with-weak-map-cache.js.map +1 -1
  216. package/build-types/actions.d.ts.map +1 -1
  217. package/build-types/entities.d.ts +53 -0
  218. package/build-types/entities.d.ts.map +1 -1
  219. package/build-types/entity-provider.d.ts +1 -1
  220. package/build-types/entity-provider.d.ts.map +1 -1
  221. package/build-types/hooks/memoize.d.ts +1 -1
  222. package/build-types/hooks/memoize.d.ts.map +1 -1
  223. package/build-types/index.d.ts +3 -3
  224. package/build-types/index.d.ts.map +1 -1
  225. package/build-types/resolvers.d.ts.map +1 -1
  226. package/build-types/sync.d.ts +2 -0
  227. package/build-types/sync.d.ts.map +1 -0
  228. package/package.json +17 -15
  229. package/src/actions.js +24 -14
  230. package/src/entities.js +91 -0
  231. package/src/entity-provider.js +11 -2
  232. package/src/resolvers.js +89 -41
  233. package/src/sync.js +18 -0
  234. package/src/test/entity-provider.js +272 -0
  235. package/tsconfig.json +1 -0
  236. package/tsconfig.tsbuildinfo +1 -1
@@ -2,20 +2,21 @@
2
2
  * External dependencies
3
3
  */
4
4
  import fastDeepEqual from 'fast-deep-equal/es6';
5
+
5
6
  /**
6
7
  * WordPress dependencies
7
8
  */
8
-
9
9
  import { compose } from '@wordpress/compose';
10
10
  import { combineReducers } from '@wordpress/data';
11
11
  import isShallowEqual from '@wordpress/is-shallow-equal';
12
+
12
13
  /**
13
14
  * Internal dependencies
14
15
  */
15
-
16
16
  import { ifMatchingAction, replaceAction } from './utils';
17
17
  import { reducer as queriedDataReducer } from './queried-data';
18
18
  import { rootEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
19
+
19
20
  /** @typedef {import('./types').AnyFunction} AnyFunction */
20
21
 
21
22
  /**
@@ -29,17 +30,17 @@ import { rootEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
29
30
  *
30
31
  * @return {Object} Updated state.
31
32
  */
32
-
33
33
  export function terms(state = {}, action) {
34
34
  switch (action.type) {
35
35
  case 'RECEIVE_TERMS':
36
- return { ...state,
36
+ return {
37
+ ...state,
37
38
  [action.taxonomy]: action.terms
38
39
  };
39
40
  }
40
-
41
41
  return state;
42
42
  }
43
+
43
44
  /**
44
45
  * Reducer managing authors state. Keyed by id.
45
46
  *
@@ -48,7 +49,6 @@ export function terms(state = {}, action) {
48
49
  *
49
50
  * @return {Object} Updated state.
50
51
  */
51
-
52
52
  export function users(state = {
53
53
  byId: {},
54
54
  queries: {}
@@ -56,20 +56,23 @@ export function users(state = {
56
56
  switch (action.type) {
57
57
  case 'RECEIVE_USER_QUERY':
58
58
  return {
59
- byId: { ...state.byId,
59
+ byId: {
60
+ ...state.byId,
60
61
  // Key users by their ID.
61
- ...action.users.reduce((newUsers, user) => ({ ...newUsers,
62
+ ...action.users.reduce((newUsers, user) => ({
63
+ ...newUsers,
62
64
  [user.id]: user
63
65
  }), {})
64
66
  },
65
- queries: { ...state.queries,
67
+ queries: {
68
+ ...state.queries,
66
69
  [action.queryID]: action.users.map(user => user.id)
67
70
  }
68
71
  };
69
72
  }
70
-
71
73
  return state;
72
74
  }
75
+
73
76
  /**
74
77
  * Reducer managing current user state.
75
78
  *
@@ -78,15 +81,14 @@ export function users(state = {
78
81
  *
79
82
  * @return {Object} Updated state.
80
83
  */
81
-
82
84
  export function currentUser(state = {}, action) {
83
85
  switch (action.type) {
84
86
  case 'RECEIVE_CURRENT_USER':
85
87
  return action.currentUser;
86
88
  }
87
-
88
89
  return state;
89
90
  }
91
+
90
92
  /**
91
93
  * Reducer managing taxonomies.
92
94
  *
@@ -95,15 +97,14 @@ export function currentUser(state = {}, action) {
95
97
  *
96
98
  * @return {Object} Updated state.
97
99
  */
98
-
99
100
  export function taxonomies(state = [], action) {
100
101
  switch (action.type) {
101
102
  case 'RECEIVE_TAXONOMIES':
102
103
  return action.taxonomies;
103
104
  }
104
-
105
105
  return state;
106
106
  }
107
+
107
108
  /**
108
109
  * Reducer managing the current theme.
109
110
  *
@@ -112,15 +113,14 @@ export function taxonomies(state = [], action) {
112
113
  *
113
114
  * @return {string|undefined} Updated state.
114
115
  */
115
-
116
116
  export function currentTheme(state = undefined, action) {
117
117
  switch (action.type) {
118
118
  case 'RECEIVE_CURRENT_THEME':
119
119
  return action.currentTheme.stylesheet;
120
120
  }
121
-
122
121
  return state;
123
122
  }
123
+
124
124
  /**
125
125
  * Reducer managing the current global styles id.
126
126
  *
@@ -129,15 +129,14 @@ export function currentTheme(state = undefined, action) {
129
129
  *
130
130
  * @return {string|undefined} Updated state.
131
131
  */
132
-
133
132
  export function currentGlobalStylesId(state = undefined, action) {
134
133
  switch (action.type) {
135
134
  case 'RECEIVE_CURRENT_GLOBAL_STYLES_ID':
136
135
  return action.id;
137
136
  }
138
-
139
137
  return state;
140
138
  }
139
+
141
140
  /**
142
141
  * Reducer managing the theme base global styles.
143
142
  *
@@ -146,17 +145,17 @@ export function currentGlobalStylesId(state = undefined, action) {
146
145
  *
147
146
  * @return {Record<string, object>} Updated state.
148
147
  */
149
-
150
148
  export function themeBaseGlobalStyles(state = {}, action) {
151
149
  switch (action.type) {
152
150
  case 'RECEIVE_THEME_GLOBAL_STYLES':
153
- return { ...state,
151
+ return {
152
+ ...state,
154
153
  [action.stylesheet]: action.globalStyles
155
154
  };
156
155
  }
157
-
158
156
  return state;
159
157
  }
158
+
160
159
  /**
161
160
  * Reducer managing the theme global styles variations.
162
161
  *
@@ -165,18 +164,16 @@ export function themeBaseGlobalStyles(state = {}, action) {
165
164
  *
166
165
  * @return {Record<string, object>} Updated state.
167
166
  */
168
-
169
167
  export function themeGlobalStyleVariations(state = {}, action) {
170
168
  switch (action.type) {
171
169
  case 'RECEIVE_THEME_GLOBAL_STYLE_VARIATIONS':
172
- return { ...state,
170
+ return {
171
+ ...state,
173
172
  [action.stylesheet]: action.variations
174
173
  };
175
174
  }
176
-
177
175
  return state;
178
176
  }
179
-
180
177
  const withMultiEntityRecordEdits = reducer => (state, action) => {
181
178
  if (action.type === 'UNDO' || action.type === 'REDO') {
182
179
  const {
@@ -203,9 +200,9 @@ const withMultiEntityRecordEdits = reducer => (state, action) => {
203
200
  });
204
201
  return newState;
205
202
  }
206
-
207
203
  return reducer(state, action);
208
204
  };
205
+
209
206
  /**
210
207
  * Higher Order Reducer for a given entity config. It supports:
211
208
  *
@@ -217,68 +214,62 @@ const withMultiEntityRecordEdits = reducer => (state, action) => {
217
214
  *
218
215
  * @return {AnyFunction} Reducer.
219
216
  */
220
-
221
-
222
217
  function entity(entityConfig) {
223
- return compose([withMultiEntityRecordEdits, // Limit to matching action type so we don't attempt to replace action on
218
+ return compose([withMultiEntityRecordEdits,
219
+ // Limit to matching action type so we don't attempt to replace action on
224
220
  // an unhandled action.
225
- ifMatchingAction(action => action.name && action.kind && action.name === entityConfig.name && action.kind === entityConfig.kind), // Inject the entity config into the action.
221
+ ifMatchingAction(action => action.name && action.kind && action.name === entityConfig.name && action.kind === entityConfig.kind),
222
+ // Inject the entity config into the action.
226
223
  replaceAction(action => {
227
- return { ...action,
224
+ return {
225
+ ...action,
228
226
  key: entityConfig.key || DEFAULT_ENTITY_KEY
229
227
  };
230
228
  })])(combineReducers({
231
229
  queriedData: queriedDataReducer,
232
230
  edits: (state = {}, action) => {
233
231
  var _action$query$context;
234
-
235
232
  switch (action.type) {
236
233
  case 'RECEIVE_ITEMS':
237
234
  const context = (_action$query$context = action?.query?.context) !== null && _action$query$context !== void 0 ? _action$query$context : 'default';
238
-
239
235
  if (context !== 'default') {
240
236
  return state;
241
237
  }
242
-
243
- const nextState = { ...state
238
+ const nextState = {
239
+ ...state
244
240
  };
245
-
246
241
  for (const record of action.items) {
247
242
  const recordId = record[action.key];
248
243
  const edits = nextState[recordId];
249
-
250
244
  if (!edits) {
251
245
  continue;
252
246
  }
253
-
254
247
  const nextEdits = Object.keys(edits).reduce((acc, key) => {
255
248
  var _record$key$raw;
256
-
257
249
  // If the edited value is still different to the persisted value,
258
250
  // keep the edited value in edits.
259
- if ( // Edits are the "raw" attribute values, but records may have
251
+ if (
252
+ // Edits are the "raw" attribute values, but records may have
260
253
  // objects with more properties, so we use `get` here for the
261
254
  // comparison.
262
- !fastDeepEqual(edits[key], (_record$key$raw = record[key]?.raw) !== null && _record$key$raw !== void 0 ? _record$key$raw : record[key]) && ( // Sometimes the server alters the sent value which means
255
+ !fastDeepEqual(edits[key], (_record$key$raw = record[key]?.raw) !== null && _record$key$raw !== void 0 ? _record$key$raw : record[key]) && (
256
+ // Sometimes the server alters the sent value which means
263
257
  // we need to also remove the edits before the api request.
264
258
  !action.persistedEdits || !fastDeepEqual(edits[key], action.persistedEdits[key]))) {
265
259
  acc[key] = edits[key];
266
260
  }
267
-
268
261
  return acc;
269
262
  }, {});
270
-
271
263
  if (Object.keys(nextEdits).length) {
272
264
  nextState[recordId] = nextEdits;
273
265
  } else {
274
266
  delete nextState[recordId];
275
267
  }
276
268
  }
277
-
278
269
  return nextState;
279
-
280
270
  case 'EDIT_ENTITY_RECORD':
281
- const nextEdits = { ...state[action.recordId],
271
+ const nextEdits = {
272
+ ...state[action.recordId],
282
273
  ...action.edits
283
274
  };
284
275
  Object.keys(nextEdits).forEach(key => {
@@ -288,18 +279,19 @@ function entity(entityConfig) {
288
279
  delete nextEdits[key];
289
280
  }
290
281
  });
291
- return { ...state,
282
+ return {
283
+ ...state,
292
284
  [action.recordId]: nextEdits
293
285
  };
294
286
  }
295
-
296
287
  return state;
297
288
  },
298
289
  saving: (state = {}, action) => {
299
290
  switch (action.type) {
300
291
  case 'SAVE_ENTITY_RECORD_START':
301
292
  case 'SAVE_ENTITY_RECORD_FINISH':
302
- return { ...state,
293
+ return {
294
+ ...state,
303
295
  [action.recordId]: {
304
296
  pending: action.type === 'SAVE_ENTITY_RECORD_START',
305
297
  error: action.error,
@@ -307,25 +299,25 @@ function entity(entityConfig) {
307
299
  }
308
300
  };
309
301
  }
310
-
311
302
  return state;
312
303
  },
313
304
  deleting: (state = {}, action) => {
314
305
  switch (action.type) {
315
306
  case 'DELETE_ENTITY_RECORD_START':
316
307
  case 'DELETE_ENTITY_RECORD_FINISH':
317
- return { ...state,
308
+ return {
309
+ ...state,
318
310
  [action.recordId]: {
319
311
  pending: action.type === 'DELETE_ENTITY_RECORD_START',
320
312
  error: action.error
321
313
  }
322
314
  };
323
315
  }
324
-
325
316
  return state;
326
317
  }
327
318
  }));
328
319
  }
320
+
329
321
  /**
330
322
  * Reducer keeping track of the registered entities.
331
323
  *
@@ -334,16 +326,14 @@ function entity(entityConfig) {
334
326
  *
335
327
  * @return {Object} Updated state.
336
328
  */
337
-
338
-
339
329
  export function entitiesConfig(state = rootEntitiesConfig, action) {
340
330
  switch (action.type) {
341
331
  case 'ADD_ENTITIES':
342
332
  return [...state, ...action.entities];
343
333
  }
344
-
345
334
  return state;
346
335
  }
336
+
347
337
  /**
348
338
  * Reducer keeping track of the registered entities config and data.
349
339
  *
@@ -352,46 +342,42 @@ export function entitiesConfig(state = rootEntitiesConfig, action) {
352
342
  *
353
343
  * @return {Object} Updated state.
354
344
  */
355
-
356
345
  export const entities = (state = {}, action) => {
357
- const newConfig = entitiesConfig(state.config, action); // Generates a dynamic reducer for the entities.
346
+ const newConfig = entitiesConfig(state.config, action);
358
347
 
348
+ // Generates a dynamic reducer for the entities.
359
349
  let entitiesDataReducer = state.reducer;
360
-
361
350
  if (!entitiesDataReducer || newConfig !== state.config) {
362
351
  const entitiesByKind = newConfig.reduce((acc, record) => {
363
352
  const {
364
353
  kind
365
354
  } = record;
366
-
367
355
  if (!acc[kind]) {
368
356
  acc[kind] = [];
369
357
  }
370
-
371
358
  acc[kind].push(record);
372
359
  return acc;
373
360
  }, {});
374
361
  entitiesDataReducer = combineReducers(Object.entries(entitiesByKind).reduce((memo, [kind, subEntities]) => {
375
- const kindReducer = combineReducers(subEntities.reduce((kindMemo, entityConfig) => ({ ...kindMemo,
362
+ const kindReducer = combineReducers(subEntities.reduce((kindMemo, entityConfig) => ({
363
+ ...kindMemo,
376
364
  [entityConfig.name]: entity(entityConfig)
377
365
  }), {}));
378
366
  memo[kind] = kindReducer;
379
367
  return memo;
380
368
  }, {}));
381
369
  }
382
-
383
370
  const newData = entitiesDataReducer(state.records, action);
384
-
385
371
  if (newData === state.records && newConfig === state.config && entitiesDataReducer === state.reducer) {
386
372
  return state;
387
373
  }
388
-
389
374
  return {
390
375
  reducer: entitiesDataReducer,
391
376
  records: newData,
392
377
  config: newConfig
393
378
  };
394
379
  };
380
+
395
381
  /**
396
382
  * @typedef {Object} UndoStateMeta
397
383
  *
@@ -407,11 +393,11 @@ export const entities = (state = {}, action) => {
407
393
  *
408
394
  * @todo Given how we use this we might want to make a custom class for it.
409
395
  */
410
-
411
396
  const UNDO_INITIAL_STATE = {
412
397
  list: [],
413
398
  offset: 0
414
399
  };
400
+
415
401
  /**
416
402
  * Reducer keeping track of entity edit undo history.
417
403
  *
@@ -420,32 +406,31 @@ const UNDO_INITIAL_STATE = {
420
406
  *
421
407
  * @return {UndoState} Updated state.
422
408
  */
423
-
424
409
  export function undo(state = UNDO_INITIAL_STATE, action) {
425
410
  const omitPendingRedos = currentState => {
426
- return { ...currentState,
411
+ return {
412
+ ...currentState,
427
413
  list: currentState.list.slice(0, currentState.offset || undefined),
428
414
  offset: 0
429
415
  };
430
416
  };
431
-
432
417
  const appendCachedEditsToLastUndo = currentState => {
433
418
  if (!currentState.cache) {
434
419
  return currentState;
435
420
  }
436
-
437
- let nextState = { ...currentState,
421
+ let nextState = {
422
+ ...currentState,
438
423
  list: [...currentState.list]
439
424
  };
440
425
  nextState = omitPendingRedos(nextState);
441
426
  const previousUndoState = nextState.list.pop();
442
427
  const updatedUndoState = currentState.cache.reduce(appendEditToStack, previousUndoState);
443
428
  nextState.list.push(updatedUndoState);
444
- return { ...nextState,
429
+ return {
430
+ ...nextState,
445
431
  cache: undefined
446
432
  };
447
433
  };
448
-
449
434
  const appendEditToStack = (stack = [], {
450
435
  kind,
451
436
  name,
@@ -463,10 +448,10 @@ export function undo(state = UNDO_INITIAL_STATE, action) {
463
448
  return k === kind && n === name && r === recordId && p === property;
464
449
  });
465
450
  const nextStack = [...stack];
466
-
467
451
  if (existingEditIndex !== -1) {
468
452
  // If the edit is already in the stack leave the initial "from" value.
469
- nextStack[existingEditIndex] = { ...nextStack[existingEditIndex],
453
+ nextStack[existingEditIndex] = {
454
+ ...nextStack[existingEditIndex],
470
455
  to
471
456
  };
472
457
  } else {
@@ -479,29 +464,25 @@ export function undo(state = UNDO_INITIAL_STATE, action) {
479
464
  to
480
465
  });
481
466
  }
482
-
483
467
  return nextStack;
484
468
  };
485
-
486
469
  switch (action.type) {
487
470
  case 'CREATE_UNDO_LEVEL':
488
471
  return appendCachedEditsToLastUndo(state);
489
-
490
472
  case 'UNDO':
491
473
  case 'REDO':
492
474
  {
493
475
  const nextState = appendCachedEditsToLastUndo(state);
494
- return { ...nextState,
476
+ return {
477
+ ...nextState,
495
478
  offset: state.offset + (action.type === 'UNDO' ? -1 : 1)
496
479
  };
497
480
  }
498
-
499
481
  case 'EDIT_ENTITY_RECORD':
500
482
  {
501
483
  if (!action.meta.undo) {
502
484
  return state;
503
485
  }
504
-
505
486
  const edits = Object.keys(action.edits).map(key => {
506
487
  return {
507
488
  kind: action.kind,
@@ -512,33 +493,31 @@ export function undo(state = UNDO_INITIAL_STATE, action) {
512
493
  to: action.edits[key]
513
494
  };
514
495
  });
515
-
516
496
  if (action.meta.undo.isCached) {
517
- return { ...state,
497
+ return {
498
+ ...state,
518
499
  cache: edits.reduce(appendEditToStack, state.cache)
519
500
  };
520
501
  }
521
-
522
502
  let nextState = omitPendingRedos(state);
523
503
  nextState = appendCachedEditsToLastUndo(nextState);
524
- nextState = { ...nextState,
504
+ nextState = {
505
+ ...nextState,
525
506
  list: [...nextState.list]
526
- }; // When an edit is a function it's an optimization to avoid running some expensive operation.
507
+ };
508
+ // When an edit is a function it's an optimization to avoid running some expensive operation.
527
509
  // We can't rely on the function references being the same so we opt out of comparing them here.
528
-
529
510
  const comparisonUndoEdits = Object.values(action.meta.undo.edits).filter(edit => typeof edit !== 'function');
530
511
  const comparisonEdits = Object.values(action.edits).filter(edit => typeof edit !== 'function');
531
-
532
512
  if (!isShallowEqual(comparisonUndoEdits, comparisonEdits)) {
533
513
  nextState.list.push(edits);
534
514
  }
535
-
536
515
  return nextState;
537
516
  }
538
517
  }
539
-
540
518
  return state;
541
519
  }
520
+
542
521
  /**
543
522
  * Reducer managing embed preview data.
544
523
  *
@@ -547,7 +526,6 @@ export function undo(state = UNDO_INITIAL_STATE, action) {
547
526
  *
548
527
  * @return {Object} Updated state.
549
528
  */
550
-
551
529
  export function embedPreviews(state = {}, action) {
552
530
  switch (action.type) {
553
531
  case 'RECEIVE_EMBED_PREVIEW':
@@ -555,13 +533,14 @@ export function embedPreviews(state = {}, action) {
555
533
  url,
556
534
  preview
557
535
  } = action;
558
- return { ...state,
536
+ return {
537
+ ...state,
559
538
  [url]: preview
560
539
  };
561
540
  }
562
-
563
541
  return state;
564
542
  }
543
+
565
544
  /**
566
545
  * State which tracks whether the user can perform an action on a REST
567
546
  * resource.
@@ -571,17 +550,17 @@ export function embedPreviews(state = {}, action) {
571
550
  *
572
551
  * @return {Object} Updated state.
573
552
  */
574
-
575
553
  export function userPermissions(state = {}, action) {
576
554
  switch (action.type) {
577
555
  case 'RECEIVE_USER_PERMISSION':
578
- return { ...state,
556
+ return {
557
+ ...state,
579
558
  [action.key]: action.isAllowed
580
559
  };
581
560
  }
582
-
583
561
  return state;
584
562
  }
563
+
585
564
  /**
586
565
  * Reducer returning autosaves keyed by their parent's post id.
587
566
  *
@@ -590,7 +569,6 @@ export function userPermissions(state = {}, action) {
590
569
  *
591
570
  * @return {Object} Updated state.
592
571
  */
593
-
594
572
  export function autosaves(state = {}, action) {
595
573
  switch (action.type) {
596
574
  case 'RECEIVE_AUTOSAVES':
@@ -598,11 +576,11 @@ export function autosaves(state = {}, action) {
598
576
  postId,
599
577
  autosaves: autosavesData
600
578
  } = action;
601
- return { ...state,
579
+ return {
580
+ ...state,
602
581
  [postId]: autosavesData
603
582
  };
604
583
  }
605
-
606
584
  return state;
607
585
  }
608
586
  export function blockPatterns(state = [], action) {
@@ -610,7 +588,6 @@ export function blockPatterns(state = [], action) {
610
588
  case 'RECEIVE_BLOCK_PATTERNS':
611
589
  return action.patterns;
612
590
  }
613
-
614
591
  return state;
615
592
  }
616
593
  export function blockPatternCategories(state = [], action) {
@@ -618,7 +595,6 @@ export function blockPatternCategories(state = [], action) {
618
595
  case 'RECEIVE_BLOCK_PATTERN_CATEGORIES':
619
596
  return action.categories;
620
597
  }
621
-
622
598
  return state;
623
599
  }
624
600
  export function navigationFallbackId(state = null, action) {
@@ -626,9 +602,9 @@ export function navigationFallbackId(state = null, action) {
626
602
  case 'RECEIVE_NAVIGATION_FALLBACK_ID':
627
603
  return action.fallbackId;
628
604
  }
629
-
630
605
  return state;
631
606
  }
607
+
632
608
  /**
633
609
  * Reducer managing the theme global styles revisions.
634
610
  *
@@ -637,15 +613,14 @@ export function navigationFallbackId(state = null, action) {
637
613
  *
638
614
  * @return {Record<string, object>} Updated state.
639
615
  */
640
-
641
616
  export function themeGlobalStyleRevisions(state = {}, action) {
642
617
  switch (action.type) {
643
618
  case 'RECEIVE_THEME_GLOBAL_STYLE_REVISIONS':
644
- return { ...state,
619
+ return {
620
+ ...state,
645
621
  [action.currentId]: action.revisions
646
622
  };
647
623
  }
648
-
649
624
  return state;
650
625
  }
651
626
  export default combineReducers({