@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.
- package/CHANGELOG.md +2 -0
- package/README.md +53 -5
- package/build/entity-context.js +13 -0
- package/build/entity-context.js.map +1 -0
- package/build/entity-provider.js +4 -189
- package/build/entity-provider.js.map +1 -1
- package/build/hooks/index.js +22 -0
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/use-entity-block-editor.js +140 -0
- package/build/hooks/use-entity-block-editor.js.map +1 -0
- package/build/hooks/use-entity-id.js +28 -0
- package/build/hooks/use-entity-id.js.map +1 -0
- package/build/hooks/use-entity-prop.js +65 -0
- package/build/hooks/use-entity-prop.js.map +1 -0
- package/build/hooks/use-resource-permissions.js +25 -8
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/resolvers.js +64 -60
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +16 -8
- package/build/selectors.js.map +1 -1
- package/build/utils/index.js +19 -0
- package/build/utils/index.js.map +1 -1
- package/build/utils/user-permissions.js +32 -0
- package/build/utils/user-permissions.js.map +1 -0
- package/build-module/entity-context.js +6 -0
- package/build-module/entity-context.js.map +1 -0
- package/build-module/entity-provider.js +3 -183
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/hooks/index.js +3 -0
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/use-entity-block-editor.js +132 -0
- package/build-module/hooks/use-entity-block-editor.js.map +1 -0
- package/build-module/hooks/use-entity-id.js +22 -0
- package/build-module/hooks/use-entity-id.js.map +1 -0
- package/build-module/hooks/use-entity-prop.js +58 -0
- package/build-module/hooks/use-entity-prop.js.map +1 -0
- package/build-module/hooks/use-resource-permissions.js +25 -8
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/resolvers.js +65 -61
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +17 -9
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/index.js +1 -0
- package/build-module/utils/index.js.map +1 -1
- package/build-module/utils/user-permissions.js +24 -0
- package/build-module/utils/user-permissions.js.map +1 -0
- package/build-types/actions.d.ts +2 -2
- package/build-types/actions.d.ts.map +1 -1
- package/build-types/batch/create-batch.d.ts.map +1 -1
- package/build-types/entities.d.ts.map +1 -1
- package/build-types/entity-context.d.ts +2 -0
- package/build-types/entity-context.d.ts.map +1 -0
- package/build-types/entity-provider.d.ts +0 -47
- package/build-types/entity-provider.d.ts.map +1 -1
- package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
- package/build-types/hooks/index.d.ts +3 -0
- package/build-types/hooks/index.d.ts.map +1 -1
- package/build-types/hooks/use-entity-block-editor.d.ts +22 -0
- package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -0
- package/build-types/hooks/use-entity-id.d.ts +9 -0
- package/build-types/hooks/use-entity-id.d.ts.map +1 -0
- package/build-types/hooks/use-entity-prop.d.ts +19 -0
- package/build-types/hooks/use-entity-prop.d.ts.map +1 -0
- package/build-types/hooks/use-resource-permissions.d.ts +8 -70
- package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
- package/build-types/index.d.ts +35 -32
- package/build-types/index.d.ts.map +1 -1
- package/build-types/locks/reducer.d.ts +1 -1
- package/build-types/locks/reducer.d.ts.map +1 -1
- package/build-types/queried-data/actions.d.ts +1 -1
- package/build-types/queried-data/actions.d.ts.map +1 -1
- package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
- package/build-types/queried-data/reducer.d.ts +1 -1
- package/build-types/queried-data/reducer.d.ts.map +1 -1
- package/build-types/queried-data/selectors.d.ts +0 -1
- package/build-types/queried-data/selectors.d.ts.map +1 -1
- package/build-types/reducer.d.ts +13 -13
- package/build-types/reducer.d.ts.map +1 -1
- package/build-types/resolvers.d.ts +3 -2
- package/build-types/resolvers.d.ts.map +1 -1
- package/build-types/selectors.d.ts +11 -6
- package/build-types/selectors.d.ts.map +1 -1
- package/build-types/utils/get-nested-value.d.ts.map +1 -1
- package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
- package/build-types/utils/if-matching-action.d.ts +1 -1
- package/build-types/utils/index.d.ts +1 -0
- package/build-types/utils/on-sub-key.d.ts +1 -1
- package/build-types/utils/replace-action.d.ts +1 -1
- package/build-types/utils/set-nested-value.d.ts.map +1 -1
- package/build-types/utils/user-permissions.d.ts +4 -0
- package/build-types/utils/user-permissions.d.ts.map +1 -0
- package/package.json +18 -17
- package/src/entity-context.js +6 -0
- package/src/entity-provider.js +2 -209
- package/src/hooks/index.ts +3 -0
- package/src/hooks/test/use-entity-record.js +5 -3
- package/src/hooks/test/use-resource-permissions.js +96 -5
- package/src/hooks/use-entity-block-editor.js +148 -0
- package/src/hooks/use-entity-id.js +21 -0
- package/src/hooks/use-entity-prop.js +60 -0
- package/src/hooks/use-resource-permissions.ts +46 -9
- package/src/resolvers.js +85 -67
- package/src/selectors.ts +18 -9
- package/src/test/entity-provider.js +6 -2
- package/src/test/resolvers.js +217 -50
- package/src/test/selectors.js +18 -55
- package/src/utils/index.js +5 -0
- package/src/utils/user-permissions.js +39 -0
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
import { useSelect, useDispatch } from '@wordpress/data';
|
|
6
|
-
import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
|
|
4
|
+
import { useContext, useMemo } from '@wordpress/element';
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
* Internal dependencies
|
|
10
8
|
*/
|
|
11
|
-
import {
|
|
12
|
-
import { updateFootnotesFromMeta } from './footnotes';
|
|
13
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
-
const EMPTY_ARRAY = [];
|
|
15
|
-
const EntityContext = createContext({});
|
|
9
|
+
import { EntityContext } from './entity-context';
|
|
16
10
|
|
|
17
11
|
/**
|
|
18
12
|
* Context provider component for providing
|
|
@@ -27,6 +21,7 @@ const EntityContext = createContext({});
|
|
|
27
21
|
* @return {Object} The provided children, wrapped with
|
|
28
22
|
* the entity's context provider.
|
|
29
23
|
*/
|
|
24
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
30
25
|
export default function EntityProvider({
|
|
31
26
|
kind,
|
|
32
27
|
type: name,
|
|
@@ -46,179 +41,4 @@ export default function EntityProvider({
|
|
|
46
41
|
children: children
|
|
47
42
|
});
|
|
48
43
|
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Hook that returns the ID for the nearest
|
|
52
|
-
* provided entity of the specified type.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} kind The entity kind.
|
|
55
|
-
* @param {string} name The entity name.
|
|
56
|
-
*/
|
|
57
|
-
export function useEntityId(kind, name) {
|
|
58
|
-
const context = useContext(EntityContext);
|
|
59
|
-
return context?.[kind]?.[name];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Hook that returns the value and a setter for the
|
|
64
|
-
* specified property of the nearest provided
|
|
65
|
-
* entity of the specified type.
|
|
66
|
-
*
|
|
67
|
-
* @param {string} kind The entity kind.
|
|
68
|
-
* @param {string} name The entity name.
|
|
69
|
-
* @param {string} prop The property name.
|
|
70
|
-
* @param {string} [_id] An entity ID to use instead of the context-provided one.
|
|
71
|
-
*
|
|
72
|
-
* @return {[*, Function, *]} An array where the first item is the
|
|
73
|
-
* property value, the second is the
|
|
74
|
-
* setter and the third is the full value
|
|
75
|
-
* object from REST API containing more
|
|
76
|
-
* information like `raw`, `rendered` and
|
|
77
|
-
* `protected` props.
|
|
78
|
-
*/
|
|
79
|
-
export function useEntityProp(kind, name, prop, _id) {
|
|
80
|
-
const providerId = useEntityId(kind, name);
|
|
81
|
-
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
82
|
-
const {
|
|
83
|
-
value,
|
|
84
|
-
fullValue
|
|
85
|
-
} = useSelect(select => {
|
|
86
|
-
const {
|
|
87
|
-
getEntityRecord,
|
|
88
|
-
getEditedEntityRecord
|
|
89
|
-
} = select(STORE_NAME);
|
|
90
|
-
const record = getEntityRecord(kind, name, id); // Trigger resolver.
|
|
91
|
-
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
92
|
-
return record && editedRecord ? {
|
|
93
|
-
value: editedRecord[prop],
|
|
94
|
-
fullValue: record[prop]
|
|
95
|
-
} : {};
|
|
96
|
-
}, [kind, name, id, prop]);
|
|
97
|
-
const {
|
|
98
|
-
editEntityRecord
|
|
99
|
-
} = useDispatch(STORE_NAME);
|
|
100
|
-
const setValue = useCallback(newValue => {
|
|
101
|
-
editEntityRecord(kind, name, id, {
|
|
102
|
-
[prop]: newValue
|
|
103
|
-
});
|
|
104
|
-
}, [editEntityRecord, kind, name, id, prop]);
|
|
105
|
-
return [value, setValue, fullValue];
|
|
106
|
-
}
|
|
107
|
-
const parsedBlocksCache = new WeakMap();
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Hook that returns block content getters and setters for
|
|
111
|
-
* the nearest provided entity of the specified type.
|
|
112
|
-
*
|
|
113
|
-
* The return value has the shape `[ blocks, onInput, onChange ]`.
|
|
114
|
-
* `onInput` is for block changes that don't create undo levels
|
|
115
|
-
* or dirty the post, non-persistent changes, and `onChange` is for
|
|
116
|
-
* persistent changes. They map directly to the props of a
|
|
117
|
-
* `BlockEditorProvider` and are intended to be used with it,
|
|
118
|
-
* or similar components or hooks.
|
|
119
|
-
*
|
|
120
|
-
* @param {string} kind The entity kind.
|
|
121
|
-
* @param {string} name The entity name.
|
|
122
|
-
* @param {Object} options
|
|
123
|
-
* @param {string} [options.id] An entity ID to use instead of the context-provided one.
|
|
124
|
-
*
|
|
125
|
-
* @return {[unknown[], Function, Function]} The block array and setters.
|
|
126
|
-
*/
|
|
127
|
-
export function useEntityBlockEditor(kind, name, {
|
|
128
|
-
id: _id
|
|
129
|
-
} = {}) {
|
|
130
|
-
const providerId = useEntityId(kind, name);
|
|
131
|
-
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
132
|
-
const {
|
|
133
|
-
getEntityRecord,
|
|
134
|
-
getEntityRecordEdits
|
|
135
|
-
} = useSelect(STORE_NAME);
|
|
136
|
-
const {
|
|
137
|
-
content,
|
|
138
|
-
editedBlocks,
|
|
139
|
-
meta
|
|
140
|
-
} = useSelect(select => {
|
|
141
|
-
if (!id) {
|
|
142
|
-
return {};
|
|
143
|
-
}
|
|
144
|
-
const {
|
|
145
|
-
getEditedEntityRecord
|
|
146
|
-
} = select(STORE_NAME);
|
|
147
|
-
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
148
|
-
return {
|
|
149
|
-
editedBlocks: editedRecord.blocks,
|
|
150
|
-
content: editedRecord.content,
|
|
151
|
-
meta: editedRecord.meta
|
|
152
|
-
};
|
|
153
|
-
}, [kind, name, id]);
|
|
154
|
-
const {
|
|
155
|
-
__unstableCreateUndoLevel,
|
|
156
|
-
editEntityRecord
|
|
157
|
-
} = useDispatch(STORE_NAME);
|
|
158
|
-
const blocks = useMemo(() => {
|
|
159
|
-
if (!id) {
|
|
160
|
-
return undefined;
|
|
161
|
-
}
|
|
162
|
-
if (editedBlocks) {
|
|
163
|
-
return editedBlocks;
|
|
164
|
-
}
|
|
165
|
-
if (!content || typeof content !== 'string') {
|
|
166
|
-
return EMPTY_ARRAY;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// If there's an edit, cache the parsed blocks by the edit.
|
|
170
|
-
// If not, cache by the original enity record.
|
|
171
|
-
const edits = getEntityRecordEdits(kind, name, id);
|
|
172
|
-
const isUnedited = !edits || !Object.keys(edits).length;
|
|
173
|
-
const cackeKey = isUnedited ? getEntityRecord(kind, name, id) : edits;
|
|
174
|
-
let _blocks = parsedBlocksCache.get(cackeKey);
|
|
175
|
-
if (!_blocks) {
|
|
176
|
-
_blocks = parse(content);
|
|
177
|
-
parsedBlocksCache.set(cackeKey, _blocks);
|
|
178
|
-
}
|
|
179
|
-
return _blocks;
|
|
180
|
-
}, [kind, name, id, editedBlocks, content, getEntityRecord, getEntityRecordEdits]);
|
|
181
|
-
const updateFootnotes = useCallback(_blocks => updateFootnotesFromMeta(_blocks, meta), [meta]);
|
|
182
|
-
const onChange = useCallback((newBlocks, options) => {
|
|
183
|
-
const noChange = blocks === newBlocks;
|
|
184
|
-
if (noChange) {
|
|
185
|
-
return __unstableCreateUndoLevel(kind, name, id);
|
|
186
|
-
}
|
|
187
|
-
const {
|
|
188
|
-
selection,
|
|
189
|
-
...rest
|
|
190
|
-
} = options;
|
|
191
|
-
|
|
192
|
-
// We create a new function here on every persistent edit
|
|
193
|
-
// to make sure the edit makes the post dirty and creates
|
|
194
|
-
// a new undo level.
|
|
195
|
-
const edits = {
|
|
196
|
-
selection,
|
|
197
|
-
content: ({
|
|
198
|
-
blocks: blocksForSerialization = []
|
|
199
|
-
}) => __unstableSerializeAndClean(blocksForSerialization),
|
|
200
|
-
...updateFootnotes(newBlocks)
|
|
201
|
-
};
|
|
202
|
-
editEntityRecord(kind, name, id, edits, {
|
|
203
|
-
isCached: false,
|
|
204
|
-
...rest
|
|
205
|
-
});
|
|
206
|
-
}, [kind, name, id, blocks, updateFootnotes, __unstableCreateUndoLevel, editEntityRecord]);
|
|
207
|
-
const onInput = useCallback((newBlocks, options) => {
|
|
208
|
-
const {
|
|
209
|
-
selection,
|
|
210
|
-
...rest
|
|
211
|
-
} = options;
|
|
212
|
-
const footnotesChanges = updateFootnotes(newBlocks);
|
|
213
|
-
const edits = {
|
|
214
|
-
selection,
|
|
215
|
-
...footnotesChanges
|
|
216
|
-
};
|
|
217
|
-
editEntityRecord(kind, name, id, edits, {
|
|
218
|
-
isCached: true,
|
|
219
|
-
...rest
|
|
220
|
-
});
|
|
221
|
-
}, [kind, name, id, updateFootnotes, editEntityRecord]);
|
|
222
|
-
return [blocks, onInput, onChange];
|
|
223
|
-
}
|
|
224
44
|
//# sourceMappingURL=entity-provider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createContext","useContext","useCallback","useMemo","useSelect","useDispatch","parse","__unstableSerializeAndClean","STORE_NAME","updateFootnotesFromMeta","jsx","_jsx","EMPTY_ARRAY","EntityContext","EntityProvider","kind","type","name","id","children","parent","childContext","Provider","value","useEntityId","context","useEntityProp","prop","_id","providerId","fullValue","select","getEntityRecord","getEditedEntityRecord","record","editedRecord","editEntityRecord","setValue","newValue","parsedBlocksCache","WeakMap","useEntityBlockEditor","getEntityRecordEdits","content","editedBlocks","meta","blocks","__unstableCreateUndoLevel","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","set","updateFootnotes","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","isCached","onInput","footnotesChanges"],"sources":["@wordpress/core-data/src/entity-provider.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tcreateContext,\n\tuseContext,\n\tuseCallback,\n\tuseMemo,\n} from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from './name';\nimport { updateFootnotesFromMeta } from './footnotes';\n\nconst EMPTY_ARRAY = [];\n\nconst EntityContext = createContext( {} );\n\n/**\n * Context provider component for providing\n * an entity for a specific entity.\n *\n * @param {Object} props The component's props.\n * @param {string} props.kind The entity kind.\n * @param {string} props.type The entity name.\n * @param {number} props.id The entity ID.\n * @param {*} props.children The children to wrap.\n *\n * @return {Object} The provided children, wrapped with\n * the entity's context provider.\n */\nexport default function EntityProvider( { kind, type: name, id, children } ) {\n\tconst parent = useContext( EntityContext );\n\tconst childContext = useMemo(\n\t\t() => ( {\n\t\t\t...parent,\n\t\t\t[ kind ]: {\n\t\t\t\t...parent?.[ kind ],\n\t\t\t\t[ name ]: id,\n\t\t\t},\n\t\t} ),\n\t\t[ parent, kind, name, id ]\n\t);\n\treturn (\n\t\t<EntityContext.Provider value={ childContext }>\n\t\t\t{ children }\n\t\t</EntityContext.Provider>\n\t);\n}\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n */\nexport function useEntityId( kind, name ) {\n\tconst context = useContext( EntityContext );\n\treturn context?.[ kind ]?.[ name ];\n}\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport function useEntityProp( kind, name, prop, _id ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } =\n\t\t\t\tselect( STORE_NAME );\n\t\t\tconst record = getEntityRecord( kind, name, id ); // Trigger resolver.\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn record && editedRecord\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedRecord[ prop ],\n\t\t\t\t\t\tfullValue: record[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, name, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ editEntityRecord, kind, name, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n\nconst parsedBlocksCache = new WeakMap();\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * persistent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[unknown[], Function, Function]} The block array and setters.\n */\nexport function useEntityBlockEditor( kind, name, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\tconst { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );\n\tconst { content, editedBlocks, meta } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! id ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn {\n\t\t\t\teditedBlocks: editedRecord.blocks,\n\t\t\t\tcontent: editedRecord.content,\n\t\t\t\tmeta: editedRecord.meta,\n\t\t\t};\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } =\n\t\tuseDispatch( STORE_NAME );\n\n\tconst blocks = useMemo( () => {\n\t\tif ( ! id ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif ( editedBlocks ) {\n\t\t\treturn editedBlocks;\n\t\t}\n\n\t\tif ( ! content || typeof content !== 'string' ) {\n\t\t\treturn EMPTY_ARRAY;\n\t\t}\n\n\t\t// If there's an edit, cache the parsed blocks by the edit.\n\t\t// If not, cache by the original enity record.\n\t\tconst edits = getEntityRecordEdits( kind, name, id );\n\t\tconst isUnedited = ! edits || ! Object.keys( edits ).length;\n\t\tconst cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;\n\t\tlet _blocks = parsedBlocksCache.get( cackeKey );\n\n\t\tif ( ! _blocks ) {\n\t\t\t_blocks = parse( content );\n\t\t\tparsedBlocksCache.set( cackeKey, _blocks );\n\t\t}\n\n\t\treturn _blocks;\n\t}, [\n\t\tkind,\n\t\tname,\n\t\tid,\n\t\teditedBlocks,\n\t\tcontent,\n\t\tgetEntityRecord,\n\t\tgetEntityRecordEdits,\n\t] );\n\n\tconst updateFootnotes = useCallback(\n\t\t( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),\n\t\t[ meta ]\n\t);\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst noChange = blocks === newBlocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, name, id );\n\t\t\t}\n\t\t\tconst { selection, ...rest } = options;\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tconst edits = {\n\t\t\t\tselection,\n\t\t\t\tcontent: ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t\t__unstableSerializeAndClean( blocksForSerialization ),\n\t\t\t\t...updateFootnotes( newBlocks ),\n\t\t\t};\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: false,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[\n\t\t\tkind,\n\t\t\tname,\n\t\t\tid,\n\t\t\tblocks,\n\t\t\tupdateFootnotes,\n\t\t\t__unstableCreateUndoLevel,\n\t\t\teditEntityRecord,\n\t\t]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection, ...rest } = options;\n\t\t\tconst footnotesChanges = updateFootnotes( newBlocks );\n\t\t\tconst edits = { selection, ...footnotesChanges };\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: true,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, id, updateFootnotes, editEntityRecord ]\n\t);\n\n\treturn [ blocks, onInput, on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AAO,EAAEA,CAAE;QAAEG,MAAM,EAAEmB,sBAAsB,GAAG;MAAG,CAAC,KACjD1D,2BAA2B,CAAE0D,sBAAuB,CAAC;MACtD,GAAGP,eAAe,CAAEE,SAAU;IAC/B,CAAC;IAEDxB,gBAAgB,CAAErB,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAE+B,KAAK,EAAE;MACxCiB,QAAQ,EAAE,KAAK;MACf,GAAGF;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CACCjD,IAAI,EACJE,IAAI,EACJC,EAAE,EACF4B,MAAM,EACNY,eAAe,EACfX,yBAAyB,EACzBX,gBAAgB,CAElB,CAAC;EAED,MAAM+B,OAAO,GAAGjE,WAAW,CAC1B,CAAE0D,SAAS,EAAEC,OAAO,KAAM;IACzB,MAAM;MAAEE,SAAS;MAAE,GAAGC;IAAK,CAAC,GAAGH,OAAO;IACtC,MAAMO,gBAAgB,GAAGV,eAAe,CAAEE,SAAU,CAAC;IACrD,MAAMX,KAAK,GAAG;MAAEc,SAAS;MAAE,GAAGK;IAAiB,CAAC;IAEhDhC,gBAAgB,CAAErB,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAE+B,KAAK,EAAE;MACxCiB,QAAQ,EAAE,IAAI;MACd,GAAGF;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CAAEjD,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAEwC,eAAe,EAAEtB,gBAAgB,CACpD,CAAC;EAED,OAAO,CAAEU,MAAM,EAAEqB,OAAO,EAAER,QAAQ,CAAE;AACrC","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["useContext","useMemo","EntityContext","jsx","_jsx","EntityProvider","kind","type","name","id","children","parent","childContext","Provider","value"],"sources":["@wordpress/core-data/src/entity-provider.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext, useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { EntityContext } from './entity-context';\n\n/**\n * Context provider component for providing\n * an entity for a specific entity.\n *\n * @param {Object} props The component's props.\n * @param {string} props.kind The entity kind.\n * @param {string} props.type The entity name.\n * @param {number} props.id The entity ID.\n * @param {*} props.children The children to wrap.\n *\n * @return {Object} The provided children, wrapped with\n * the entity's context provider.\n */\nexport default function EntityProvider( { kind, type: name, id, children } ) {\n\tconst parent = useContext( EntityContext );\n\tconst childContext = useMemo(\n\t\t() => ( {\n\t\t\t...parent,\n\t\t\t[ kind ]: {\n\t\t\t\t...parent?.[ kind ],\n\t\t\t\t[ name ]: id,\n\t\t\t},\n\t\t} ),\n\t\t[ parent, kind, name, id ]\n\t);\n\treturn (\n\t\t<EntityContext.Provider value={ childContext }>\n\t\t\t{ children }\n\t\t</EntityContext.Provider>\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAU,EAAEC,OAAO,QAAQ,oBAAoB;;AAExD;AACA;AACA;AACA,SAASC,aAAa,QAAQ,kBAAkB;;AAEhD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAZA,SAAAC,GAAA,IAAAC,IAAA;AAaA,eAAe,SAASC,cAAcA,CAAE;EAAEC,IAAI;EAAEC,IAAI,EAAEC,IAAI;EAAEC,EAAE;EAAEC;AAAS,CAAC,EAAG;EAC5E,MAAMC,MAAM,GAAGX,UAAU,CAAEE,aAAc,CAAC;EAC1C,MAAMU,YAAY,GAAGX,OAAO,CAC3B,OAAQ;IACP,GAAGU,MAAM;IACT,CAAEL,IAAI,GAAI;MACT,GAAGK,MAAM,GAAIL,IAAI,CAAE;MACnB,CAAEE,IAAI,GAAIC;IACX;EACD,CAAC,CAAE,EACH,CAAEE,MAAM,EAAEL,IAAI,EAAEE,IAAI,EAAEC,EAAE,CACzB,CAAC;EACD,oBACCL,IAAA,CAACF,aAAa,CAACW,QAAQ;IAACC,KAAK,EAAGF,YAAc;IAAAF,QAAA,EAC3CA;EAAQ,CACa,CAAC;AAE3B","ignoreList":[]}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { default as useEntityRecord, __experimentalUseEntityRecord } from './use-entity-record';
|
|
2
2
|
export { default as useEntityRecords, __experimentalUseEntityRecords } from './use-entity-records';
|
|
3
3
|
export { default as useResourcePermissions, __experimentalUseResourcePermissions } from './use-resource-permissions';
|
|
4
|
+
export { default as useEntityBlockEditor } from './use-entity-block-editor';
|
|
5
|
+
export { default as useEntityId } from './use-entity-id';
|
|
6
|
+
export { default as useEntityProp } from './use-entity-prop';
|
|
4
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["default","useEntityRecord","__experimentalUseEntityRecord","useEntityRecords","__experimentalUseEntityRecords","useResourcePermissions","__experimentalUseResourcePermissions"],"sources":["@wordpress/core-data/src/hooks/index.ts"],"sourcesContent":["export {\n\tdefault as useEntityRecord,\n\t__experimentalUseEntityRecord,\n} from './use-entity-record';\nexport {\n\tdefault as useEntityRecords,\n\t__experimentalUseEntityRecords,\n} from './use-entity-records';\nexport {\n\tdefault as useResourcePermissions,\n\t__experimentalUseResourcePermissions,\n} from './use-resource-permissions';\n"],"mappings":"AAAA,SACCA,OAAO,IAAIC,eAAe,EAC1BC,6BAA6B,QACvB,qBAAqB;AAC5B,SACCF,OAAO,IAAIG,gBAAgB,EAC3BC,8BAA8B,QACxB,sBAAsB;AAC7B,SACCJ,OAAO,IAAIK,sBAAsB,EACjCC,oCAAoC,QAC9B,4BAA4B","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["default","useEntityRecord","__experimentalUseEntityRecord","useEntityRecords","__experimentalUseEntityRecords","useResourcePermissions","__experimentalUseResourcePermissions","useEntityBlockEditor","useEntityId","useEntityProp"],"sources":["@wordpress/core-data/src/hooks/index.ts"],"sourcesContent":["export {\n\tdefault as useEntityRecord,\n\t__experimentalUseEntityRecord,\n} from './use-entity-record';\nexport {\n\tdefault as useEntityRecords,\n\t__experimentalUseEntityRecords,\n} from './use-entity-records';\nexport {\n\tdefault as useResourcePermissions,\n\t__experimentalUseResourcePermissions,\n} from './use-resource-permissions';\nexport { default as useEntityBlockEditor } from './use-entity-block-editor';\nexport { default as useEntityId } from './use-entity-id';\nexport { default as useEntityProp } from './use-entity-prop';\n"],"mappings":"AAAA,SACCA,OAAO,IAAIC,eAAe,EAC1BC,6BAA6B,QACvB,qBAAqB;AAC5B,SACCF,OAAO,IAAIG,gBAAgB,EAC3BC,8BAA8B,QACxB,sBAAsB;AAC7B,SACCJ,OAAO,IAAIK,sBAAsB,EACjCC,oCAAoC,QAC9B,4BAA4B;AACnC,SAASN,OAAO,IAAIO,oBAAoB,QAAQ,2BAA2B;AAC3E,SAASP,OAAO,IAAIQ,WAAW,QAAQ,iBAAiB;AACxD,SAASR,OAAO,IAAIS,aAAa,QAAQ,mBAAmB","ignoreList":[]}
|
|
@@ -0,0 +1,132 @@
|
|
|
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
|
+
const EMPTY_ARRAY = [];
|
|
15
|
+
const parsedBlocksCache = new WeakMap();
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Hook that returns block content getters and setters for
|
|
19
|
+
* the nearest provided entity of the specified type.
|
|
20
|
+
*
|
|
21
|
+
* The return value has the shape `[ blocks, onInput, onChange ]`.
|
|
22
|
+
* `onInput` is for block changes that don't create undo levels
|
|
23
|
+
* or dirty the post, non-persistent changes, and `onChange` is for
|
|
24
|
+
* persistent changes. They map directly to the props of a
|
|
25
|
+
* `BlockEditorProvider` and are intended to be used with it,
|
|
26
|
+
* or similar components or hooks.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} kind The entity kind.
|
|
29
|
+
* @param {string} name The entity name.
|
|
30
|
+
* @param {Object} options
|
|
31
|
+
* @param {string} [options.id] An entity ID to use instead of the context-provided one.
|
|
32
|
+
*
|
|
33
|
+
* @return {[unknown[], Function, Function]} The block array and setters.
|
|
34
|
+
*/
|
|
35
|
+
export default function useEntityBlockEditor(kind, name, {
|
|
36
|
+
id: _id
|
|
37
|
+
} = {}) {
|
|
38
|
+
const providerId = useEntityId(kind, name);
|
|
39
|
+
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
40
|
+
const {
|
|
41
|
+
getEntityRecord,
|
|
42
|
+
getEntityRecordEdits
|
|
43
|
+
} = useSelect(STORE_NAME);
|
|
44
|
+
const {
|
|
45
|
+
content,
|
|
46
|
+
editedBlocks,
|
|
47
|
+
meta
|
|
48
|
+
} = useSelect(select => {
|
|
49
|
+
if (!id) {
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
const {
|
|
53
|
+
getEditedEntityRecord
|
|
54
|
+
} = select(STORE_NAME);
|
|
55
|
+
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
56
|
+
return {
|
|
57
|
+
editedBlocks: editedRecord.blocks,
|
|
58
|
+
content: editedRecord.content,
|
|
59
|
+
meta: editedRecord.meta
|
|
60
|
+
};
|
|
61
|
+
}, [kind, name, id]);
|
|
62
|
+
const {
|
|
63
|
+
__unstableCreateUndoLevel,
|
|
64
|
+
editEntityRecord
|
|
65
|
+
} = useDispatch(STORE_NAME);
|
|
66
|
+
const blocks = useMemo(() => {
|
|
67
|
+
if (!id) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
if (editedBlocks) {
|
|
71
|
+
return editedBlocks;
|
|
72
|
+
}
|
|
73
|
+
if (!content || typeof content !== 'string') {
|
|
74
|
+
return EMPTY_ARRAY;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// If there's an edit, cache the parsed blocks by the edit.
|
|
78
|
+
// If not, cache by the original enity record.
|
|
79
|
+
const edits = getEntityRecordEdits(kind, name, id);
|
|
80
|
+
const isUnedited = !edits || !Object.keys(edits).length;
|
|
81
|
+
const cackeKey = isUnedited ? getEntityRecord(kind, name, id) : edits;
|
|
82
|
+
let _blocks = parsedBlocksCache.get(cackeKey);
|
|
83
|
+
if (!_blocks) {
|
|
84
|
+
_blocks = parse(content);
|
|
85
|
+
parsedBlocksCache.set(cackeKey, _blocks);
|
|
86
|
+
}
|
|
87
|
+
return _blocks;
|
|
88
|
+
}, [kind, name, id, editedBlocks, content, getEntityRecord, getEntityRecordEdits]);
|
|
89
|
+
const updateFootnotes = useCallback(_blocks => updateFootnotesFromMeta(_blocks, meta), [meta]);
|
|
90
|
+
const onChange = useCallback((newBlocks, options) => {
|
|
91
|
+
const noChange = blocks === newBlocks;
|
|
92
|
+
if (noChange) {
|
|
93
|
+
return __unstableCreateUndoLevel(kind, name, id);
|
|
94
|
+
}
|
|
95
|
+
const {
|
|
96
|
+
selection,
|
|
97
|
+
...rest
|
|
98
|
+
} = options;
|
|
99
|
+
|
|
100
|
+
// We create a new function here on every persistent edit
|
|
101
|
+
// to make sure the edit makes the post dirty and creates
|
|
102
|
+
// a new undo level.
|
|
103
|
+
const edits = {
|
|
104
|
+
selection,
|
|
105
|
+
content: ({
|
|
106
|
+
blocks: blocksForSerialization = []
|
|
107
|
+
}) => __unstableSerializeAndClean(blocksForSerialization),
|
|
108
|
+
...updateFootnotes(newBlocks)
|
|
109
|
+
};
|
|
110
|
+
editEntityRecord(kind, name, id, edits, {
|
|
111
|
+
isCached: false,
|
|
112
|
+
...rest
|
|
113
|
+
});
|
|
114
|
+
}, [kind, name, id, blocks, updateFootnotes, __unstableCreateUndoLevel, editEntityRecord]);
|
|
115
|
+
const onInput = useCallback((newBlocks, options) => {
|
|
116
|
+
const {
|
|
117
|
+
selection,
|
|
118
|
+
...rest
|
|
119
|
+
} = options;
|
|
120
|
+
const footnotesChanges = updateFootnotes(newBlocks);
|
|
121
|
+
const edits = {
|
|
122
|
+
selection,
|
|
123
|
+
...footnotesChanges
|
|
124
|
+
};
|
|
125
|
+
editEntityRecord(kind, name, id, edits, {
|
|
126
|
+
isCached: true,
|
|
127
|
+
...rest
|
|
128
|
+
});
|
|
129
|
+
}, [kind, name, id, updateFootnotes, editEntityRecord]);
|
|
130
|
+
return [blocks, onInput, onChange];
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=use-entity-block-editor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useMemo","useDispatch","useSelect","parse","__unstableSerializeAndClean","STORE_NAME","useEntityId","updateFootnotesFromMeta","EMPTY_ARRAY","parsedBlocksCache","WeakMap","useEntityBlockEditor","kind","name","id","_id","providerId","getEntityRecord","getEntityRecordEdits","content","editedBlocks","meta","select","getEditedEntityRecord","editedRecord","blocks","__unstableCreateUndoLevel","editEntityRecord","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","set","updateFootnotes","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","isCached","onInput","footnotesChanges"],"sources":["@wordpress/core-data/src/hooks/use-entity-block-editor.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from '../name';\nimport useEntityId from './use-entity-id';\nimport { updateFootnotesFromMeta } from '../footnotes';\n\nconst EMPTY_ARRAY = [];\nconst parsedBlocksCache = new WeakMap();\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * persistent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[unknown[], Function, Function]} The block array and setters.\n */\nexport default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\tconst { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );\n\tconst { content, editedBlocks, meta } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! id ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn {\n\t\t\t\teditedBlocks: editedRecord.blocks,\n\t\t\t\tcontent: editedRecord.content,\n\t\t\t\tmeta: editedRecord.meta,\n\t\t\t};\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } =\n\t\tuseDispatch( STORE_NAME );\n\n\tconst blocks = useMemo( () => {\n\t\tif ( ! id ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif ( editedBlocks ) {\n\t\t\treturn editedBlocks;\n\t\t}\n\n\t\tif ( ! content || typeof content !== 'string' ) {\n\t\t\treturn EMPTY_ARRAY;\n\t\t}\n\n\t\t// If there's an edit, cache the parsed blocks by the edit.\n\t\t// If not, cache by the original enity record.\n\t\tconst edits = getEntityRecordEdits( kind, name, id );\n\t\tconst isUnedited = ! edits || ! Object.keys( edits ).length;\n\t\tconst cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;\n\t\tlet _blocks = parsedBlocksCache.get( cackeKey );\n\n\t\tif ( ! _blocks ) {\n\t\t\t_blocks = parse( content );\n\t\t\tparsedBlocksCache.set( cackeKey, _blocks );\n\t\t}\n\n\t\treturn _blocks;\n\t}, [\n\t\tkind,\n\t\tname,\n\t\tid,\n\t\teditedBlocks,\n\t\tcontent,\n\t\tgetEntityRecord,\n\t\tgetEntityRecordEdits,\n\t] );\n\n\tconst updateFootnotes = useCallback(\n\t\t( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),\n\t\t[ meta ]\n\t);\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst noChange = blocks === newBlocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, name, id );\n\t\t\t}\n\t\t\tconst { selection, ...rest } = options;\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tconst edits = {\n\t\t\t\tselection,\n\t\t\t\tcontent: ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t\t__unstableSerializeAndClean( blocksForSerialization ),\n\t\t\t\t...updateFootnotes( newBlocks ),\n\t\t\t};\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: false,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[\n\t\t\tkind,\n\t\t\tname,\n\t\t\tid,\n\t\t\tblocks,\n\t\t\tupdateFootnotes,\n\t\t\t__unstableCreateUndoLevel,\n\t\t\teditEntityRecord,\n\t\t]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection, ...rest } = options;\n\t\t\tconst footnotesChanges = updateFootnotes( newBlocks );\n\t\t\tconst edits = { selection, ...footnotesChanges };\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: true,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, id, updateFootnotes, editEntityRecord ]\n\t);\n\n\treturn [ blocks, onInput, onChange ];\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAW,EAAEC,OAAO,QAAQ,oBAAoB;AACzD,SAASC,WAAW,EAAEC,SAAS,QAAQ,iBAAiB;A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
|
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
|
+
}
|
|
22
|
+
//# sourceMappingURL=use-entity-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useContext","EntityContext","useEntityId","kind","name","context"],"sources":["@wordpress/core-data/src/hooks/use-entity-id.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { EntityContext } from '../entity-context';\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n */\nexport default function useEntityId( kind, name ) {\n\tconst context = useContext( EntityContext );\n\treturn context?.[ kind ]?.[ name ];\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAU,QAAQ,oBAAoB;;AAE/C;AACA;AACA;AACA,SAASC,aAAa,QAAQ,mBAAmB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,WAAWA,CAAEC,IAAI,EAAEC,IAAI,EAAG;EACjD,MAAMC,OAAO,GAAGL,UAAU,CAAEC,aAAc,CAAC;EAC3C,OAAOI,OAAO,GAAIF,IAAI,CAAE,GAAIC,IAAI,CAAE;AACnC","ignoreList":[]}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 !== null && _id !== void 0 ? _id : providerId;
|
|
33
|
+
const {
|
|
34
|
+
value,
|
|
35
|
+
fullValue
|
|
36
|
+
} = useSelect(select => {
|
|
37
|
+
const {
|
|
38
|
+
getEntityRecord,
|
|
39
|
+
getEditedEntityRecord
|
|
40
|
+
} = select(STORE_NAME);
|
|
41
|
+
const record = getEntityRecord(kind, name, id); // Trigger resolver.
|
|
42
|
+
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
43
|
+
return record && editedRecord ? {
|
|
44
|
+
value: editedRecord[prop],
|
|
45
|
+
fullValue: record[prop]
|
|
46
|
+
} : {};
|
|
47
|
+
}, [kind, name, id, prop]);
|
|
48
|
+
const {
|
|
49
|
+
editEntityRecord
|
|
50
|
+
} = useDispatch(STORE_NAME);
|
|
51
|
+
const setValue = useCallback(newValue => {
|
|
52
|
+
editEntityRecord(kind, name, id, {
|
|
53
|
+
[prop]: newValue
|
|
54
|
+
});
|
|
55
|
+
}, [editEntityRecord, kind, name, id, prop]);
|
|
56
|
+
return [value, setValue, fullValue];
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=use-entity-prop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useDispatch","useSelect","STORE_NAME","useEntityId","useEntityProp","kind","name","prop","_id","providerId","id","value","fullValue","select","getEntityRecord","getEditedEntityRecord","record","editedRecord","editEntityRecord","setValue","newValue"],"sources":["@wordpress/core-data/src/hooks/use-entity-prop.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from '../name';\nimport useEntityId from './use-entity-id';\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport default function useEntityProp( kind, name, prop, _id ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } =\n\t\t\t\tselect( STORE_NAME );\n\t\t\tconst record = getEntityRecord( kind, name, id ); // Trigger resolver.\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn record && editedRecord\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedRecord[ prop ],\n\t\t\t\t\t\tfullValue: record[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, name, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ editEntityRecord, kind, name, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAW,QAAQ,oBAAoB;AAChD,SAASC,WAAW,EAAEC,SAAS,QAAQ,iBAAiB;;AAExD;AACA;AACA;AACA,SAASC,UAAU,QAAQ,SAAS;AACpC,OAAOC,WAAW,MAAM,iBAAiB;;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,aAAaA,CAAEC,IAAI,EAAEC,IAAI,EAAEC,IAAI,EAAEC,GAAG,EAAG;EAC9D,MAAMC,UAAU,GAAGN,WAAW,CAAEE,IAAI,EAAEC,IAAK,CAAC;EAC5C,MAAMI,EAAE,GAAGF,GAAG,aAAHA,GAAG,cAAHA,GAAG,GAAIC,UAAU;EAE5B,MAAM;IAAEE,KAAK;IAAEC;EAAU,CAAC,GAAGX,SAAS,CACnCY,MAAM,IAAM;IACb,MAAM;MAAEC,eAAe;MAAEC;IAAsB,CAAC,GAC/CF,MAAM,CAAEX,UAAW,CAAC;IACrB,MAAMc,MAAM,GAAGF,eAAe,CAAET,IAAI,EAAEC,IAAI,EAAEI,EAAG,CAAC,CAAC,CAAC;IAClD,MAAMO,YAAY,GAAGF,qBAAqB,CAAEV,IAAI,EAAEC,IAAI,EAAEI,EAAG,CAAC;IAC5D,OAAOM,MAAM,IAAIC,YAAY,GAC1B;MACAN,KAAK,EAAEM,YAAY,CAAEV,IAAI,CAAE;MAC3BK,SAAS,EAAEI,MAAM,CAAET,IAAI;IACvB,CAAC,GACD,CAAC,CAAC;EACN,CAAC,EACD,CAAEF,IAAI,EAAEC,IAAI,EAAEI,EAAE,EAAEH,IAAI,CACvB,CAAC;EACD,MAAM;IAAEW;EAAiB,CAAC,GAAGlB,WAAW,CAAEE,UAAW,CAAC;EACtD,MAAMiB,QAAQ,GAAGpB,WAAW,CACzBqB,QAAQ,IAAM;IACfF,gBAAgB,CAAEb,IAAI,EAAEC,IAAI,EAAEI,EAAE,EAAE;MACjC,CAAEH,IAAI,GAAIa;IACX,CAAE,CAAC;EACJ,CAAC,EACD,CAAEF,gBAAgB,EAAEb,IAAI,EAAEC,IAAI,EAAEI,EAAE,EAAEH,IAAI,CACzC,CAAC;EAED,OAAO,CAAEI,KAAK,EAAEQ,QAAQ,EAAEP,SAAS,CAAE;AACtC","ignoreList":[]}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
4
|
import deprecated from '@wordpress/deprecated';
|
|
5
|
+
import warning from '@wordpress/warning';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Internal dependencies
|
|
@@ -19,15 +20,17 @@ import useQuerySelect from './use-query-select';
|
|
|
19
20
|
*
|
|
20
21
|
* @since 6.1.0 Introduced in WordPress core.
|
|
21
22
|
*
|
|
22
|
-
* @param resource
|
|
23
|
-
*
|
|
23
|
+
* @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
|
|
24
|
+
* or REST base as a string - `media`.
|
|
25
|
+
* @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged
|
|
26
|
+
* when using an entity object as a resource to check permissions and will be ignored.
|
|
24
27
|
*
|
|
25
28
|
* @example
|
|
26
29
|
* ```js
|
|
27
30
|
* import { useResourcePermissions } from '@wordpress/core-data';
|
|
28
31
|
*
|
|
29
32
|
* function PagesList() {
|
|
30
|
-
* const { canCreate, isResolving } = useResourcePermissions( '
|
|
33
|
+
* const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );
|
|
31
34
|
*
|
|
32
35
|
* if ( isResolving ) {
|
|
33
36
|
* return 'Loading ...';
|
|
@@ -55,7 +58,7 @@ import useQuerySelect from './use-query-select';
|
|
|
55
58
|
* canUpdate,
|
|
56
59
|
* canDelete,
|
|
57
60
|
* isResolving
|
|
58
|
-
* } = useResourcePermissions( '
|
|
61
|
+
* } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );
|
|
59
62
|
*
|
|
60
63
|
* if ( isResolving ) {
|
|
61
64
|
* return 'Loading ...';
|
|
@@ -82,13 +85,26 @@ import useQuerySelect from './use-query-select';
|
|
|
82
85
|
* @return Entity records data.
|
|
83
86
|
* @template IdType
|
|
84
87
|
*/
|
|
85
|
-
|
|
88
|
+
function useResourcePermissions(resource, id) {
|
|
89
|
+
// Serialize `resource` to a string that can be safely used as a React dep.
|
|
90
|
+
// We can't just pass `resource` as one of the deps, because if it is passed
|
|
91
|
+
// as an object literal, then it will be a different object on each call even
|
|
92
|
+
// if the values remain the same.
|
|
93
|
+
const isEntity = typeof resource === 'object';
|
|
94
|
+
const resourceAsString = isEntity ? JSON.stringify(resource) : resource;
|
|
95
|
+
if (isEntity && typeof id !== 'undefined') {
|
|
96
|
+
globalThis.SCRIPT_DEBUG === true ? warning(`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`) : void 0;
|
|
97
|
+
}
|
|
86
98
|
return useQuerySelect(resolve => {
|
|
99
|
+
const hasId = isEntity ? !!resource.id : !!id;
|
|
87
100
|
const {
|
|
88
101
|
canUser
|
|
89
102
|
} = resolve(coreStore);
|
|
90
|
-
const create = canUser('create',
|
|
91
|
-
|
|
103
|
+
const create = canUser('create', isEntity ? {
|
|
104
|
+
kind: resource.kind,
|
|
105
|
+
name: resource.name
|
|
106
|
+
} : resource);
|
|
107
|
+
if (!hasId) {
|
|
92
108
|
const read = canUser('read', resource);
|
|
93
109
|
const isResolving = create.isResolving || read.isResolving;
|
|
94
110
|
const hasResolved = create.hasResolved && read.hasResolved;
|
|
@@ -126,8 +142,9 @@ export default function useResourcePermissions(resource, id) {
|
|
|
126
142
|
canUpdate: hasResolved && update.data,
|
|
127
143
|
canDelete: hasResolved && _delete.data
|
|
128
144
|
};
|
|
129
|
-
}, [
|
|
145
|
+
}, [resourceAsString, id]);
|
|
130
146
|
}
|
|
147
|
+
export default useResourcePermissions;
|
|
131
148
|
export function __experimentalUseResourcePermissions(resource, id) {
|
|
132
149
|
deprecated(`wp.data.__experimentalUseResourcePermissions`, {
|
|
133
150
|
alternative: 'wp.data.useResourcePermissions',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["deprecated","store","coreStore","Status","useQuerySelect","useResourcePermissions","resource","id","resolve","canUser","create","read","isResolving","hasResolved","status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} ),\n];\n\n/**\n * Resolves resource permissions.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param resource
|
|
1
|
+
{"version":3,"names":["deprecated","warning","store","coreStore","Status","useQuerySelect","useResourcePermissions","resource","id","isEntity","resourceAsString","JSON","stringify","globalThis","SCRIPT_DEBUG","resolve","hasId","canUser","create","kind","name","read","isResolving","hasResolved","status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\nimport warning from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} ),\n];\n\ntype EntityResource = { kind: string; name: string; id?: string | number };\n\nfunction useResourcePermissions< IdType = void >(\n\tresource: string,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType >;\n\nfunction useResourcePermissions< IdType = void >(\n\tresource: EntityResource,\n\tid?: never\n): ResourcePermissionsResolution< IdType >;\n\n/**\n * Resolves resource permissions.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`\n * or REST base as a string - `media`.\n * @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged\n * when using an entity object as a resource to check permissions and will be ignored.\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function PagesList() {\n * const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PagesList />\n * ```\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function Page({ pageId }) {\n * const {\n * canCreate,\n * canUpdate,\n * canDelete,\n * isResolving\n * } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * {canUpdate ? (<button>Edit page</button>) : false}\n * {canDelete ? (<button>Delete page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <Page pageId={ 15 } />\n * ```\n *\n * In the above example, when `PagesList` is rendered into an\n * application, the appropriate permissions and the resolution details will be retrieved from\n * the store state using `canUser()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template IdType\n */\nfunction useResourcePermissions< IdType = void >(\n\tresource: string | EntityResource,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType > {\n\t// Serialize `resource` to a string that can be safely used as a React dep.\n\t// We can't just pass `resource` as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst isEntity = typeof resource === 'object';\n\tconst resourceAsString = isEntity ? JSON.stringify( resource ) : resource;\n\n\tif ( isEntity && typeof id !== 'undefined' ) {\n\t\twarning(\n\t\t\t`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`\n\t\t);\n\t}\n\n\treturn useQuerySelect(\n\t\t( resolve ) => {\n\t\t\tconst hasId = isEntity ? !! resource.id : !! id;\n\t\t\tconst { canUser } = resolve( coreStore );\n\t\t\tconst create = canUser(\n\t\t\t\t'create',\n\t\t\t\tisEntity\n\t\t\t\t\t? { kind: resource.kind, name: resource.name }\n\t\t\t\t\t: resource\n\t\t\t);\n\n\t\t\tif ( ! hasId ) {\n\t\t\t\tconst read = canUser( 'read', resource );\n\n\t\t\t\tconst isResolving = create.isResolving || read.isResolving;\n\t\t\t\tconst hasResolved = create.hasResolved && read.hasResolved;\n\t\t\t\tlet status = Status.Idle;\n\t\t\t\tif ( isResolving ) {\n\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tstatus,\n\t\t\t\t\tisResolving,\n\t\t\t\t\thasResolved,\n\t\t\t\t\tcanCreate: create.hasResolved && create.data,\n\t\t\t\t\tcanRead: read.hasResolved && read.data,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst read = canUser( 'read', resource, id );\n\t\t\tconst update = canUser( 'update', resource, id );\n\t\t\tconst _delete = canUser( 'delete', resource, id );\n\t\t\tconst isResolving =\n\t\t\t\tread.isResolving ||\n\t\t\t\tcreate.isResolving ||\n\t\t\t\tupdate.isResolving ||\n\t\t\t\t_delete.isResolving;\n\t\t\tconst hasResolved =\n\t\t\t\tread.hasResolved &&\n\t\t\t\tcreate.hasResolved &&\n\t\t\t\tupdate.hasResolved &&\n\t\t\t\t_delete.hasResolved;\n\n\t\t\tlet status = Status.Idle;\n\t\t\tif ( isResolving ) {\n\t\t\t\tstatus = Status.Resolving;\n\t\t\t} else if ( hasResolved ) {\n\t\t\t\tstatus = Status.Success;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus,\n\t\t\t\tisResolving,\n\t\t\t\thasResolved,\n\t\t\t\tcanRead: hasResolved && read.data,\n\t\t\t\tcanCreate: hasResolved && create.data,\n\t\t\t\tcanUpdate: hasResolved && update.data,\n\t\t\t\tcanDelete: hasResolved && _delete.data,\n\t\t\t};\n\t\t},\n\t\t[ resourceAsString, id ]\n\t);\n}\n\nexport default useResourcePermissions;\n\nexport function __experimentalUseResourcePermissions(\n\tresource: string,\n\tid?: unknown\n) {\n\tdeprecated( `wp.data.__experimentalUseResourcePermissions`, {\n\t\talternative: 'wp.data.useResourcePermissions',\n\t\tsince: '6.1',\n\t} );\n\treturn useResourcePermissions( resource, id );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,UAAU,MAAM,uBAAuB;AAC9C,OAAOC,OAAO,MAAM,oBAAoB;;AAExC;AACA;AACA;AACA,SAASC,KAAK,IAAIC,SAAS,QAAQ,KAAK;AACxC,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,cAAc,MAAM,oBAAoB;;AAqB/C;AACA;AACA;;AAsBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,sBAAsBA,CAC9BC,QAAiC,EACjCC,EAAW,EAC+B;EAC1C;EACA;EACA;EACA;EACA,MAAMC,QAAQ,GAAG,OAAOF,QAAQ,KAAK,QAAQ;EAC7C,MAAMG,gBAAgB,GAAGD,QAAQ,GAAGE,IAAI,CAACC,SAAS,CAAEL,QAAS,CAAC,GAAGA,QAAQ;EAEzE,IAAKE,QAAQ,IAAI,OAAOD,EAAE,KAAK,WAAW,EAAG;IAC5CK,UAAA,CAAAC,YAAA,YAAAb,OAAO,CACL,2FACF,CAAC;EAC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