@wordpress/core-data 7.3.0 → 7.4.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 (110) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +53 -5
  3. package/build/entity-context.js +13 -0
  4. package/build/entity-context.js.map +1 -0
  5. package/build/entity-provider.js +4 -189
  6. package/build/entity-provider.js.map +1 -1
  7. package/build/hooks/index.js +22 -0
  8. package/build/hooks/index.js.map +1 -1
  9. package/build/hooks/use-entity-block-editor.js +140 -0
  10. package/build/hooks/use-entity-block-editor.js.map +1 -0
  11. package/build/hooks/use-entity-id.js +28 -0
  12. package/build/hooks/use-entity-id.js.map +1 -0
  13. package/build/hooks/use-entity-prop.js +65 -0
  14. package/build/hooks/use-entity-prop.js.map +1 -0
  15. package/build/hooks/use-resource-permissions.js +25 -8
  16. package/build/hooks/use-resource-permissions.js.map +1 -1
  17. package/build/resolvers.js +64 -60
  18. package/build/resolvers.js.map +1 -1
  19. package/build/selectors.js +16 -8
  20. package/build/selectors.js.map +1 -1
  21. package/build/utils/index.js +19 -0
  22. package/build/utils/index.js.map +1 -1
  23. package/build/utils/user-permissions.js +32 -0
  24. package/build/utils/user-permissions.js.map +1 -0
  25. package/build-module/entity-context.js +6 -0
  26. package/build-module/entity-context.js.map +1 -0
  27. package/build-module/entity-provider.js +3 -183
  28. package/build-module/entity-provider.js.map +1 -1
  29. package/build-module/hooks/index.js +3 -0
  30. package/build-module/hooks/index.js.map +1 -1
  31. package/build-module/hooks/use-entity-block-editor.js +132 -0
  32. package/build-module/hooks/use-entity-block-editor.js.map +1 -0
  33. package/build-module/hooks/use-entity-id.js +22 -0
  34. package/build-module/hooks/use-entity-id.js.map +1 -0
  35. package/build-module/hooks/use-entity-prop.js +58 -0
  36. package/build-module/hooks/use-entity-prop.js.map +1 -0
  37. package/build-module/hooks/use-resource-permissions.js +25 -8
  38. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  39. package/build-module/resolvers.js +65 -61
  40. package/build-module/resolvers.js.map +1 -1
  41. package/build-module/selectors.js +17 -9
  42. package/build-module/selectors.js.map +1 -1
  43. package/build-module/utils/index.js +1 -0
  44. package/build-module/utils/index.js.map +1 -1
  45. package/build-module/utils/user-permissions.js +24 -0
  46. package/build-module/utils/user-permissions.js.map +1 -0
  47. package/build-types/actions.d.ts +2 -2
  48. package/build-types/actions.d.ts.map +1 -1
  49. package/build-types/batch/create-batch.d.ts.map +1 -1
  50. package/build-types/entities.d.ts.map +1 -1
  51. package/build-types/entity-context.d.ts +2 -0
  52. package/build-types/entity-context.d.ts.map +1 -0
  53. package/build-types/entity-provider.d.ts +0 -47
  54. package/build-types/entity-provider.d.ts.map +1 -1
  55. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
  56. package/build-types/hooks/index.d.ts +3 -0
  57. package/build-types/hooks/index.d.ts.map +1 -1
  58. package/build-types/hooks/use-entity-block-editor.d.ts +22 -0
  59. package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -0
  60. package/build-types/hooks/use-entity-id.d.ts +9 -0
  61. package/build-types/hooks/use-entity-id.d.ts.map +1 -0
  62. package/build-types/hooks/use-entity-prop.d.ts +19 -0
  63. package/build-types/hooks/use-entity-prop.d.ts.map +1 -0
  64. package/build-types/hooks/use-resource-permissions.d.ts +8 -70
  65. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
  66. package/build-types/index.d.ts +35 -32
  67. package/build-types/index.d.ts.map +1 -1
  68. package/build-types/locks/reducer.d.ts +1 -1
  69. package/build-types/locks/reducer.d.ts.map +1 -1
  70. package/build-types/queried-data/actions.d.ts +1 -1
  71. package/build-types/queried-data/actions.d.ts.map +1 -1
  72. package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
  73. package/build-types/queried-data/reducer.d.ts +1 -1
  74. package/build-types/queried-data/reducer.d.ts.map +1 -1
  75. package/build-types/queried-data/selectors.d.ts +0 -1
  76. package/build-types/queried-data/selectors.d.ts.map +1 -1
  77. package/build-types/reducer.d.ts +13 -13
  78. package/build-types/reducer.d.ts.map +1 -1
  79. package/build-types/resolvers.d.ts +3 -2
  80. package/build-types/resolvers.d.ts.map +1 -1
  81. package/build-types/selectors.d.ts +11 -6
  82. package/build-types/selectors.d.ts.map +1 -1
  83. package/build-types/utils/get-nested-value.d.ts.map +1 -1
  84. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
  85. package/build-types/utils/if-matching-action.d.ts +1 -1
  86. package/build-types/utils/index.d.ts +1 -0
  87. package/build-types/utils/on-sub-key.d.ts +1 -1
  88. package/build-types/utils/replace-action.d.ts +1 -1
  89. package/build-types/utils/set-nested-value.d.ts.map +1 -1
  90. package/build-types/utils/user-permissions.d.ts +4 -0
  91. package/build-types/utils/user-permissions.d.ts.map +1 -0
  92. package/package.json +18 -17
  93. package/src/entity-context.js +6 -0
  94. package/src/entity-provider.js +2 -209
  95. package/src/hooks/index.ts +3 -0
  96. package/src/hooks/test/use-entity-record.js +5 -3
  97. package/src/hooks/test/use-resource-permissions.js +96 -5
  98. package/src/hooks/use-entity-block-editor.js +148 -0
  99. package/src/hooks/use-entity-id.js +21 -0
  100. package/src/hooks/use-entity-prop.js +60 -0
  101. package/src/hooks/use-resource-permissions.ts +46 -9
  102. package/src/resolvers.js +85 -67
  103. package/src/selectors.ts +18 -9
  104. package/src/test/entity-provider.js +6 -2
  105. package/src/test/resolvers.js +217 -50
  106. package/src/test/selectors.js +18 -55
  107. package/src/utils/index.js +5 -0
  108. package/src/utils/user-permissions.js +39 -0
  109. package/tsconfig.json +2 -1
  110. package/tsconfig.tsbuildinfo +1 -1
@@ -1,5 +1,5 @@
1
1
  export default replaceAction;
2
- export type AnyFunction = import('../types').AnyFunction;
2
+ export type AnyFunction = import("../types").AnyFunction;
3
3
  /** @typedef {import('../types').AnyFunction} AnyFunction */
4
4
  /**
5
5
  * Higher-order reducer creator which substitutes the action object before
@@ -1 +1 @@
1
- {"version":3,"file":"set-nested-value.d.ts","sourceRoot":"","sources":["../../src/utils/set-nested-value.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,0DAHW,QAAM,MAAM,mBAyBtB"}
1
+ {"version":3,"file":"set-nested-value.d.ts","sourceRoot":"","sources":["../../src/utils/set-nested-value.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,0DAHW,QAAM,MAAM,SACZ,GAAC,OAwBX"}
@@ -0,0 +1,4 @@
1
+ export function getUserPermissionsFromResponse(response: any): {};
2
+ export function getUserPermissionCacheKey(action: any, resource: any, id: any): string;
3
+ export const ALLOWED_RESOURCE_ACTIONS: string[];
4
+ //# sourceMappingURL=user-permissions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-permissions.d.ts","sourceRoot":"","sources":["../../src/utils/user-permissions.js"],"names":[],"mappings":"AAOA,kEAmBC;AAED,uFAUC;AAtCD,gDAKE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/core-data",
3
- "version": "7.3.0",
3
+ "version": "7.4.0",
4
4
  "description": "Access to and manipulation of core WordPress entities.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -32,21 +32,22 @@
32
32
  ],
33
33
  "dependencies": {
34
34
  "@babel/runtime": "^7.16.0",
35
- "@wordpress/api-fetch": "^7.3.0",
36
- "@wordpress/block-editor": "^13.3.0",
37
- "@wordpress/blocks": "^13.3.0",
38
- "@wordpress/compose": "^7.3.0",
39
- "@wordpress/data": "^10.3.0",
40
- "@wordpress/deprecated": "^4.3.0",
41
- "@wordpress/element": "^6.3.0",
42
- "@wordpress/html-entities": "^4.3.0",
43
- "@wordpress/i18n": "^5.3.0",
44
- "@wordpress/is-shallow-equal": "^5.3.0",
45
- "@wordpress/private-apis": "^1.3.0",
46
- "@wordpress/rich-text": "^7.3.0",
47
- "@wordpress/sync": "^1.3.0",
48
- "@wordpress/undo-manager": "^1.3.0",
49
- "@wordpress/url": "^4.3.0",
35
+ "@wordpress/api-fetch": "^7.4.0",
36
+ "@wordpress/block-editor": "^13.4.0",
37
+ "@wordpress/blocks": "^13.4.0",
38
+ "@wordpress/compose": "^7.4.0",
39
+ "@wordpress/data": "^10.4.0",
40
+ "@wordpress/deprecated": "^4.4.0",
41
+ "@wordpress/element": "^6.4.0",
42
+ "@wordpress/html-entities": "^4.4.0",
43
+ "@wordpress/i18n": "^5.4.0",
44
+ "@wordpress/is-shallow-equal": "^5.4.0",
45
+ "@wordpress/private-apis": "^1.4.0",
46
+ "@wordpress/rich-text": "^7.4.0",
47
+ "@wordpress/sync": "^1.4.0",
48
+ "@wordpress/undo-manager": "^1.4.0",
49
+ "@wordpress/url": "^4.4.0",
50
+ "@wordpress/warning": "^3.4.0",
50
51
  "change-case": "^4.1.2",
51
52
  "equivalent-key-map": "^0.2.2",
52
53
  "fast-deep-equal": "^3.1.3",
@@ -60,5 +61,5 @@
60
61
  "publishConfig": {
61
62
  "access": "public"
62
63
  },
63
- "gitHead": "122867d355ca4edc63d3a3bbd9411d3a2e1458df"
64
+ "gitHead": "363edb39b8dda8727f652e42cbb8497732693ed2"
64
65
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { createContext } from '@wordpress/element';
5
+
6
+ export const EntityContext = createContext( {} );
@@ -1,24 +1,12 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import {
5
- createContext,
6
- useContext,
7
- useCallback,
8
- useMemo,
9
- } from '@wordpress/element';
10
- import { useSelect, useDispatch } from '@wordpress/data';
11
- import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
4
+ import { useContext, useMemo } from '@wordpress/element';
12
5
 
13
6
  /**
14
7
  * Internal dependencies
15
8
  */
16
- import { STORE_NAME } from './name';
17
- import { updateFootnotesFromMeta } from './footnotes';
18
-
19
- const EMPTY_ARRAY = [];
20
-
21
- const EntityContext = createContext( {} );
9
+ import { EntityContext } from './entity-context';
22
10
 
23
11
  /**
24
12
  * Context provider component for providing
@@ -51,198 +39,3 @@ export default function EntityProvider( { kind, type: name, id, children } ) {
51
39
  </EntityContext.Provider>
52
40
  );
53
41
  }
54
-
55
- /**
56
- * Hook that returns the ID for the nearest
57
- * provided entity of the specified type.
58
- *
59
- * @param {string} kind The entity kind.
60
- * @param {string} name The entity name.
61
- */
62
- export function useEntityId( kind, name ) {
63
- const context = useContext( EntityContext );
64
- return context?.[ kind ]?.[ name ];
65
- }
66
-
67
- /**
68
- * Hook that returns the value and a setter for the
69
- * specified property of the nearest provided
70
- * entity of the specified type.
71
- *
72
- * @param {string} kind The entity kind.
73
- * @param {string} name The entity name.
74
- * @param {string} prop The property name.
75
- * @param {string} [_id] An entity ID to use instead of the context-provided one.
76
- *
77
- * @return {[*, Function, *]} An array where the first item is the
78
- * property value, the second is the
79
- * setter and the third is the full value
80
- * object from REST API containing more
81
- * information like `raw`, `rendered` and
82
- * `protected` props.
83
- */
84
- export function useEntityProp( kind, name, prop, _id ) {
85
- const providerId = useEntityId( kind, name );
86
- const id = _id ?? providerId;
87
-
88
- const { value, fullValue } = useSelect(
89
- ( select ) => {
90
- const { getEntityRecord, getEditedEntityRecord } =
91
- select( STORE_NAME );
92
- const record = getEntityRecord( kind, name, id ); // Trigger resolver.
93
- const editedRecord = getEditedEntityRecord( kind, name, id );
94
- return record && editedRecord
95
- ? {
96
- value: editedRecord[ prop ],
97
- fullValue: record[ prop ],
98
- }
99
- : {};
100
- },
101
- [ kind, name, id, prop ]
102
- );
103
- const { editEntityRecord } = useDispatch( STORE_NAME );
104
- const setValue = useCallback(
105
- ( newValue ) => {
106
- editEntityRecord( kind, name, id, {
107
- [ prop ]: newValue,
108
- } );
109
- },
110
- [ editEntityRecord, kind, name, id, prop ]
111
- );
112
-
113
- return [ value, setValue, fullValue ];
114
- }
115
-
116
- const parsedBlocksCache = new WeakMap();
117
-
118
- /**
119
- * Hook that returns block content getters and setters for
120
- * the nearest provided entity of the specified type.
121
- *
122
- * The return value has the shape `[ blocks, onInput, onChange ]`.
123
- * `onInput` is for block changes that don't create undo levels
124
- * or dirty the post, non-persistent changes, and `onChange` is for
125
- * persistent changes. They map directly to the props of a
126
- * `BlockEditorProvider` and are intended to be used with it,
127
- * or similar components or hooks.
128
- *
129
- * @param {string} kind The entity kind.
130
- * @param {string} name The entity name.
131
- * @param {Object} options
132
- * @param {string} [options.id] An entity ID to use instead of the context-provided one.
133
- *
134
- * @return {[unknown[], Function, Function]} The block array and setters.
135
- */
136
- export function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
137
- const providerId = useEntityId( kind, name );
138
- const id = _id ?? providerId;
139
- const { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );
140
- const { content, editedBlocks, meta } = useSelect(
141
- ( select ) => {
142
- if ( ! id ) {
143
- return {};
144
- }
145
- const { getEditedEntityRecord } = select( STORE_NAME );
146
- const editedRecord = getEditedEntityRecord( kind, name, id );
147
- return {
148
- editedBlocks: editedRecord.blocks,
149
- content: editedRecord.content,
150
- meta: editedRecord.meta,
151
- };
152
- },
153
- [ kind, name, id ]
154
- );
155
- const { __unstableCreateUndoLevel, editEntityRecord } =
156
- useDispatch( STORE_NAME );
157
-
158
- const blocks = useMemo( () => {
159
- if ( ! id ) {
160
- return undefined;
161
- }
162
-
163
- if ( editedBlocks ) {
164
- return editedBlocks;
165
- }
166
-
167
- if ( ! content || typeof content !== 'string' ) {
168
- return EMPTY_ARRAY;
169
- }
170
-
171
- // If there's an edit, cache the parsed blocks by the edit.
172
- // If not, cache by the original enity record.
173
- const edits = getEntityRecordEdits( kind, name, id );
174
- const isUnedited = ! edits || ! Object.keys( edits ).length;
175
- const cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;
176
- let _blocks = parsedBlocksCache.get( cackeKey );
177
-
178
- if ( ! _blocks ) {
179
- _blocks = parse( content );
180
- parsedBlocksCache.set( cackeKey, _blocks );
181
- }
182
-
183
- return _blocks;
184
- }, [
185
- kind,
186
- name,
187
- id,
188
- editedBlocks,
189
- content,
190
- getEntityRecord,
191
- getEntityRecordEdits,
192
- ] );
193
-
194
- const updateFootnotes = useCallback(
195
- ( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),
196
- [ meta ]
197
- );
198
-
199
- const onChange = useCallback(
200
- ( newBlocks, options ) => {
201
- const noChange = blocks === newBlocks;
202
- if ( noChange ) {
203
- return __unstableCreateUndoLevel( kind, name, id );
204
- }
205
- const { selection, ...rest } = options;
206
-
207
- // We create a new function here on every persistent edit
208
- // to make sure the edit makes the post dirty and creates
209
- // a new undo level.
210
- const edits = {
211
- selection,
212
- content: ( { blocks: blocksForSerialization = [] } ) =>
213
- __unstableSerializeAndClean( blocksForSerialization ),
214
- ...updateFootnotes( newBlocks ),
215
- };
216
-
217
- editEntityRecord( kind, name, id, edits, {
218
- isCached: false,
219
- ...rest,
220
- } );
221
- },
222
- [
223
- kind,
224
- name,
225
- id,
226
- blocks,
227
- updateFootnotes,
228
- __unstableCreateUndoLevel,
229
- editEntityRecord,
230
- ]
231
- );
232
-
233
- const onInput = useCallback(
234
- ( newBlocks, options ) => {
235
- const { selection, ...rest } = options;
236
- const footnotesChanges = updateFootnotes( newBlocks );
237
- const edits = { selection, ...footnotesChanges };
238
-
239
- editEntityRecord( kind, name, id, edits, {
240
- isCached: true,
241
- ...rest,
242
- } );
243
- },
244
- [ kind, name, id, updateFootnotes, editEntityRecord ]
245
- );
246
-
247
- return [ blocks, onInput, onChange ];
248
- }
@@ -10,3 +10,6 @@ export {
10
10
  default as useResourcePermissions,
11
11
  __experimentalUseResourcePermissions,
12
12
  } from './use-resource-permissions';
13
+ export { default as useEntityBlockEditor } from './use-entity-block-editor';
14
+ export { default as useEntityId } from './use-entity-id';
15
+ export { default as useEntityProp } from './use-entity-prop';
@@ -27,10 +27,11 @@ describe( 'useEntityRecord', () => {
27
27
  } );
28
28
 
29
29
  const TEST_RECORD = { id: 1, hello: 'world' };
30
+ const TEST_RECORD_RESPONSE = { json: () => Promise.resolve( TEST_RECORD ) };
30
31
 
31
32
  it( 'resolves the entity record when missing from the state', async () => {
32
33
  // Provide response
33
- triggerFetch.mockImplementation( () => TEST_RECORD );
34
+ triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );
34
35
 
35
36
  let data;
36
37
  const TestComponent = () => {
@@ -60,6 +61,7 @@ describe( 'useEntityRecord', () => {
60
61
  await waitFor( () =>
61
62
  expect( triggerFetch ).toHaveBeenCalledWith( {
62
63
  path: '/wp/v2/widgets/1?context=edit',
64
+ parse: false,
63
65
  } )
64
66
  );
65
67
 
@@ -79,7 +81,7 @@ describe( 'useEntityRecord', () => {
79
81
 
80
82
  it( 'applies edits to the entity record', async () => {
81
83
  // Provide response
82
- triggerFetch.mockImplementation( () => TEST_RECORD );
84
+ triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );
83
85
 
84
86
  let widget;
85
87
  const TestComponent = () => {
@@ -119,7 +121,7 @@ describe( 'useEntityRecord', () => {
119
121
  } );
120
122
 
121
123
  it( 'does not resolve entity record when disabled via options', async () => {
122
- triggerFetch.mockImplementation( () => TEST_RECORD );
124
+ triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );
123
125
 
124
126
  let data;
125
127
  const TestComponent = ( { enabled } ) => {
@@ -24,11 +24,9 @@ describe( 'useResourcePermissions', () => {
24
24
  registry.register( coreDataStore );
25
25
 
26
26
  triggerFetch.mockImplementation( () => ( {
27
- headers: {
28
- get: () => ( {
29
- allow: 'POST',
30
- } ),
31
- },
27
+ headers: new Headers( {
28
+ allow: 'POST',
29
+ } ),
32
30
  } ) );
33
31
  } );
34
32
 
@@ -95,4 +93,97 @@ describe( 'useResourcePermissions', () => {
95
93
  } )
96
94
  );
97
95
  } );
96
+
97
+ it( 'retrieves the relevant permissions for a id-less entity', async () => {
98
+ let data;
99
+ const TestComponent = () => {
100
+ data = useResourcePermissions( {
101
+ kind: 'root',
102
+ name: 'media',
103
+ } );
104
+ return <div />;
105
+ };
106
+ render(
107
+ <RegistryProvider value={ registry }>
108
+ <TestComponent />
109
+ </RegistryProvider>
110
+ );
111
+ expect( data ).toEqual( {
112
+ status: 'IDLE',
113
+ isResolving: false,
114
+ hasResolved: false,
115
+ canCreate: false,
116
+ canRead: false,
117
+ } );
118
+
119
+ await waitFor( () =>
120
+ expect( data ).toEqual( {
121
+ status: 'SUCCESS',
122
+ isResolving: false,
123
+ hasResolved: true,
124
+ canCreate: true,
125
+ canRead: false,
126
+ } )
127
+ );
128
+ } );
129
+
130
+ it( 'retrieves the relevant permissions for an entity', async () => {
131
+ let data;
132
+ const TestComponent = () => {
133
+ data = useResourcePermissions( {
134
+ kind: 'root',
135
+ name: 'media',
136
+ id: 1,
137
+ } );
138
+ return <div />;
139
+ };
140
+ render(
141
+ <RegistryProvider value={ registry }>
142
+ <TestComponent />
143
+ </RegistryProvider>
144
+ );
145
+ expect( data ).toEqual( {
146
+ status: 'IDLE',
147
+ isResolving: false,
148
+ hasResolved: false,
149
+ canCreate: false,
150
+ canRead: false,
151
+ canUpdate: false,
152
+ canDelete: false,
153
+ } );
154
+
155
+ await waitFor( () =>
156
+ expect( data ).toEqual( {
157
+ status: 'SUCCESS',
158
+ isResolving: false,
159
+ hasResolved: true,
160
+ canCreate: true,
161
+ canRead: false,
162
+ canUpdate: false,
163
+ canDelete: false,
164
+ } )
165
+ );
166
+ } );
167
+
168
+ it( 'should warn when called with incorrect arguments signature', () => {
169
+ const TestComponent = () => {
170
+ useResourcePermissions(
171
+ {
172
+ kind: 'root',
173
+ name: 'media',
174
+ },
175
+ 1
176
+ );
177
+ return null;
178
+ };
179
+ render(
180
+ <RegistryProvider value={ registry }>
181
+ <TestComponent />
182
+ </RegistryProvider>
183
+ );
184
+
185
+ expect( console ).toHaveWarnedWith(
186
+ `When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`
187
+ );
188
+ } );
98
189
  } );
@@ -0,0 +1,148 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback, useMemo } from '@wordpress/element';
5
+ import { useDispatch, useSelect } from '@wordpress/data';
6
+ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
7
+
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ import { STORE_NAME } from '../name';
12
+ import useEntityId from './use-entity-id';
13
+ import { updateFootnotesFromMeta } from '../footnotes';
14
+
15
+ const EMPTY_ARRAY = [];
16
+ const parsedBlocksCache = new WeakMap();
17
+
18
+ /**
19
+ * Hook that returns block content getters and setters for
20
+ * the nearest provided entity of the specified type.
21
+ *
22
+ * The return value has the shape `[ blocks, onInput, onChange ]`.
23
+ * `onInput` is for block changes that don't create undo levels
24
+ * or dirty the post, non-persistent changes, and `onChange` is for
25
+ * persistent changes. They map directly to the props of a
26
+ * `BlockEditorProvider` and are intended to be used with it,
27
+ * or similar components or hooks.
28
+ *
29
+ * @param {string} kind The entity kind.
30
+ * @param {string} name The entity name.
31
+ * @param {Object} options
32
+ * @param {string} [options.id] An entity ID to use instead of the context-provided one.
33
+ *
34
+ * @return {[unknown[], Function, Function]} The block array and setters.
35
+ */
36
+ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
37
+ const providerId = useEntityId( kind, name );
38
+ const id = _id ?? providerId;
39
+ const { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );
40
+ const { content, editedBlocks, meta } = useSelect(
41
+ ( select ) => {
42
+ if ( ! id ) {
43
+ return {};
44
+ }
45
+ const { getEditedEntityRecord } = select( STORE_NAME );
46
+ const editedRecord = getEditedEntityRecord( kind, name, id );
47
+ return {
48
+ editedBlocks: editedRecord.blocks,
49
+ content: editedRecord.content,
50
+ meta: editedRecord.meta,
51
+ };
52
+ },
53
+ [ kind, name, id ]
54
+ );
55
+ const { __unstableCreateUndoLevel, editEntityRecord } =
56
+ useDispatch( STORE_NAME );
57
+
58
+ const blocks = useMemo( () => {
59
+ if ( ! id ) {
60
+ return undefined;
61
+ }
62
+
63
+ if ( editedBlocks ) {
64
+ return editedBlocks;
65
+ }
66
+
67
+ if ( ! content || typeof content !== 'string' ) {
68
+ return EMPTY_ARRAY;
69
+ }
70
+
71
+ // If there's an edit, cache the parsed blocks by the edit.
72
+ // If not, cache by the original enity record.
73
+ const edits = getEntityRecordEdits( kind, name, id );
74
+ const isUnedited = ! edits || ! Object.keys( edits ).length;
75
+ const cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;
76
+ let _blocks = parsedBlocksCache.get( cackeKey );
77
+
78
+ if ( ! _blocks ) {
79
+ _blocks = parse( content );
80
+ parsedBlocksCache.set( cackeKey, _blocks );
81
+ }
82
+
83
+ return _blocks;
84
+ }, [
85
+ kind,
86
+ name,
87
+ id,
88
+ editedBlocks,
89
+ content,
90
+ getEntityRecord,
91
+ getEntityRecordEdits,
92
+ ] );
93
+
94
+ const updateFootnotes = useCallback(
95
+ ( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),
96
+ [ meta ]
97
+ );
98
+
99
+ const onChange = useCallback(
100
+ ( newBlocks, options ) => {
101
+ const noChange = blocks === newBlocks;
102
+ if ( noChange ) {
103
+ return __unstableCreateUndoLevel( kind, name, id );
104
+ }
105
+ const { selection, ...rest } = options;
106
+
107
+ // We create a new function here on every persistent edit
108
+ // to make sure the edit makes the post dirty and creates
109
+ // a new undo level.
110
+ const edits = {
111
+ selection,
112
+ content: ( { blocks: blocksForSerialization = [] } ) =>
113
+ __unstableSerializeAndClean( blocksForSerialization ),
114
+ ...updateFootnotes( newBlocks ),
115
+ };
116
+
117
+ editEntityRecord( kind, name, id, edits, {
118
+ isCached: false,
119
+ ...rest,
120
+ } );
121
+ },
122
+ [
123
+ kind,
124
+ name,
125
+ id,
126
+ blocks,
127
+ updateFootnotes,
128
+ __unstableCreateUndoLevel,
129
+ editEntityRecord,
130
+ ]
131
+ );
132
+
133
+ const onInput = useCallback(
134
+ ( newBlocks, options ) => {
135
+ const { selection, ...rest } = options;
136
+ const footnotesChanges = updateFootnotes( newBlocks );
137
+ const edits = { selection, ...footnotesChanges };
138
+
139
+ editEntityRecord( kind, name, id, edits, {
140
+ isCached: true,
141
+ ...rest,
142
+ } );
143
+ },
144
+ [ kind, name, id, updateFootnotes, editEntityRecord ]
145
+ );
146
+
147
+ return [ blocks, onInput, onChange ];
148
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useContext } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import { EntityContext } from '../entity-context';
10
+
11
+ /**
12
+ * Hook that returns the ID for the nearest
13
+ * provided entity of the specified type.
14
+ *
15
+ * @param {string} kind The entity kind.
16
+ * @param {string} name The entity name.
17
+ */
18
+ export default function useEntityId( kind, name ) {
19
+ const context = useContext( EntityContext );
20
+ return context?.[ kind ]?.[ name ];
21
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback } from '@wordpress/element';
5
+ import { useDispatch, useSelect } from '@wordpress/data';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import { STORE_NAME } from '../name';
11
+ import useEntityId from './use-entity-id';
12
+
13
+ /**
14
+ * Hook that returns the value and a setter for the
15
+ * specified property of the nearest provided
16
+ * entity of the specified type.
17
+ *
18
+ * @param {string} kind The entity kind.
19
+ * @param {string} name The entity name.
20
+ * @param {string} prop The property name.
21
+ * @param {string} [_id] An entity ID to use instead of the context-provided one.
22
+ *
23
+ * @return {[*, Function, *]} An array where the first item is the
24
+ * property value, the second is the
25
+ * setter and the third is the full value
26
+ * object from REST API containing more
27
+ * information like `raw`, `rendered` and
28
+ * `protected` props.
29
+ */
30
+ export default function useEntityProp( kind, name, prop, _id ) {
31
+ const providerId = useEntityId( kind, name );
32
+ const id = _id ?? providerId;
33
+
34
+ const { value, fullValue } = useSelect(
35
+ ( select ) => {
36
+ const { getEntityRecord, getEditedEntityRecord } =
37
+ select( STORE_NAME );
38
+ const record = getEntityRecord( kind, name, id ); // Trigger resolver.
39
+ const editedRecord = getEditedEntityRecord( kind, name, id );
40
+ return record && editedRecord
41
+ ? {
42
+ value: editedRecord[ prop ],
43
+ fullValue: record[ prop ],
44
+ }
45
+ : {};
46
+ },
47
+ [ kind, name, id, prop ]
48
+ );
49
+ const { editEntityRecord } = useDispatch( STORE_NAME );
50
+ const setValue = useCallback(
51
+ ( newValue ) => {
52
+ editEntityRecord( kind, name, id, {
53
+ [ prop ]: newValue,
54
+ } );
55
+ },
56
+ [ editEntityRecord, kind, name, id, prop ]
57
+ );
58
+
59
+ return [ value, setValue, fullValue ];
60
+ }