@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
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -339,7 +339,7 @@ _Parameters_
|
|
|
339
339
|
|
|
340
340
|
- _state_ `State`: Data state.
|
|
341
341
|
- _action_ `string`: Action to check. One of: 'create', 'read', 'update', 'delete'.
|
|
342
|
-
- _resource_ `string`:
|
|
342
|
+
- _resource_ `string | EntityResource`: Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }` or REST base as a string - `media`.
|
|
343
343
|
- _id_ `EntityRecordKey`: Optional ID of the rest resource to check.
|
|
344
344
|
|
|
345
345
|
_Returns_
|
|
@@ -976,6 +976,47 @@ The following set of react hooks available to import from the `@wordpress/core-d
|
|
|
976
976
|
|
|
977
977
|
<!-- START TOKEN(Autogenerated hooks|src/hooks/index.ts) -->
|
|
978
978
|
|
|
979
|
+
### useEntityBlockEditor
|
|
980
|
+
|
|
981
|
+
Hook that returns block content getters and setters for the nearest provided entity of the specified type.
|
|
982
|
+
|
|
983
|
+
The return value has the shape `[ blocks, onInput, onChange ]`. `onInput` is for block changes that don't create undo levels or dirty the post, non-persistent changes, and `onChange` is for persistent changes. They map directly to the props of a `BlockEditorProvider` and are intended to be used with it, or similar components or hooks.
|
|
984
|
+
|
|
985
|
+
_Parameters_
|
|
986
|
+
|
|
987
|
+
- _kind_ `string`: The entity kind.
|
|
988
|
+
- _name_ `string`: The entity name.
|
|
989
|
+
- _options_ `Object`:
|
|
990
|
+
- _options.id_ `[string]`: An entity ID to use instead of the context-provided one.
|
|
991
|
+
|
|
992
|
+
_Returns_
|
|
993
|
+
|
|
994
|
+
- `[unknown[], Function, Function]`: The block array and setters.
|
|
995
|
+
|
|
996
|
+
### useEntityId
|
|
997
|
+
|
|
998
|
+
Hook that returns the ID for the nearest provided entity of the specified type.
|
|
999
|
+
|
|
1000
|
+
_Parameters_
|
|
1001
|
+
|
|
1002
|
+
- _kind_ `string`: The entity kind.
|
|
1003
|
+
- _name_ `string`: The entity name.
|
|
1004
|
+
|
|
1005
|
+
### useEntityProp
|
|
1006
|
+
|
|
1007
|
+
Hook that returns the value and a setter for the specified property of the nearest provided entity of the specified type.
|
|
1008
|
+
|
|
1009
|
+
_Parameters_
|
|
1010
|
+
|
|
1011
|
+
- _kind_ `string`: The entity kind.
|
|
1012
|
+
- _name_ `string`: The entity name.
|
|
1013
|
+
- _prop_ `string`: The property name.
|
|
1014
|
+
- _\_id_ `[string]`: An entity ID to use instead of the context-provided one.
|
|
1015
|
+
|
|
1016
|
+
_Returns_
|
|
1017
|
+
|
|
1018
|
+
- `[*, Function, *]`: An array where the first item is the property value, the second is the setter and the third is the full value object from REST API containing more information like `raw`, `rendered` and `protected` props.
|
|
1019
|
+
|
|
979
1020
|
### useEntityRecord
|
|
980
1021
|
|
|
981
1022
|
Resolves the specified entity record.
|
|
@@ -1132,7 +1173,10 @@ _Usage_
|
|
|
1132
1173
|
import { useResourcePermissions } from '@wordpress/core-data';
|
|
1133
1174
|
|
|
1134
1175
|
function PagesList() {
|
|
1135
|
-
const { canCreate, isResolving } = useResourcePermissions(
|
|
1176
|
+
const { canCreate, isResolving } = useResourcePermissions( {
|
|
1177
|
+
kind: 'postType',
|
|
1178
|
+
name: 'page',
|
|
1179
|
+
} );
|
|
1136
1180
|
|
|
1137
1181
|
if ( isResolving ) {
|
|
1138
1182
|
return 'Loading ...';
|
|
@@ -1155,7 +1199,11 @@ import { useResourcePermissions } from '@wordpress/core-data';
|
|
|
1155
1199
|
|
|
1156
1200
|
function Page( { pageId } ) {
|
|
1157
1201
|
const { canCreate, canUpdate, canDelete, isResolving } =
|
|
1158
|
-
useResourcePermissions(
|
|
1202
|
+
useResourcePermissions( {
|
|
1203
|
+
kind: 'postType',
|
|
1204
|
+
name: 'page',
|
|
1205
|
+
id: pageId,
|
|
1206
|
+
} );
|
|
1159
1207
|
|
|
1160
1208
|
if ( isResolving ) {
|
|
1161
1209
|
return 'Loading ...';
|
|
@@ -1181,8 +1229,8 @@ the store state using `canUser()`, or resolved if missing.
|
|
|
1181
1229
|
|
|
1182
1230
|
_Parameters_
|
|
1183
1231
|
|
|
1184
|
-
- _resource_ `string`:
|
|
1185
|
-
- _id_ `IdType`: ID of
|
|
1232
|
+
- _resource_ `string | EntityResource`: Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }` or REST base as a string - `media`.
|
|
1233
|
+
- _id_ `IdType`: Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged when using an entity object as a resource to check permissions and will be ignored.
|
|
1186
1234
|
|
|
1187
1235
|
_Returns_
|
|
1188
1236
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.EntityContext = void 0;
|
|
7
|
+
var _element = require("@wordpress/element");
|
|
8
|
+
/**
|
|
9
|
+
* WordPress dependencies
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const EntityContext = exports.EntityContext = (0, _element.createContext)({});
|
|
13
|
+
//# sourceMappingURL=entity-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_element","require","EntityContext","exports","createContext"],"sources":["@wordpress/core-data/src/entity-context.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createContext } from '@wordpress/element';\n\nexport const EntityContext = createContext( {} );\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGO,MAAMC,aAAa,GAAAC,OAAA,CAAAD,aAAA,GAAG,IAAAE,sBAAa,EAAE,CAAC,CAAE,CAAC","ignoreList":[]}
|
package/build/entity-provider.js
CHANGED
|
@@ -4,14 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = EntityProvider;
|
|
7
|
-
exports.useEntityBlockEditor = useEntityBlockEditor;
|
|
8
|
-
exports.useEntityId = useEntityId;
|
|
9
|
-
exports.useEntityProp = useEntityProp;
|
|
10
7
|
var _element = require("@wordpress/element");
|
|
11
|
-
var
|
|
12
|
-
var _blocks2 = require("@wordpress/blocks");
|
|
13
|
-
var _name = require("./name");
|
|
14
|
-
var _footnotes = require("./footnotes");
|
|
8
|
+
var _entityContext = require("./entity-context");
|
|
15
9
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
16
10
|
/**
|
|
17
11
|
* WordPress dependencies
|
|
@@ -21,9 +15,6 @@ var _jsxRuntime = require("react/jsx-runtime");
|
|
|
21
15
|
* Internal dependencies
|
|
22
16
|
*/
|
|
23
17
|
|
|
24
|
-
const EMPTY_ARRAY = [];
|
|
25
|
-
const EntityContext = (0, _element.createContext)({});
|
|
26
|
-
|
|
27
18
|
/**
|
|
28
19
|
* Context provider component for providing
|
|
29
20
|
* an entity for a specific entity.
|
|
@@ -36,14 +27,13 @@ const EntityContext = (0, _element.createContext)({});
|
|
|
36
27
|
*
|
|
37
28
|
* @return {Object} The provided children, wrapped with
|
|
38
29
|
* the entity's context provider.
|
|
39
|
-
*/
|
|
40
|
-
function EntityProvider({
|
|
30
|
+
*/function EntityProvider({
|
|
41
31
|
kind,
|
|
42
32
|
type: name,
|
|
43
33
|
id,
|
|
44
34
|
children
|
|
45
35
|
}) {
|
|
46
|
-
const parent = (0, _element.useContext)(EntityContext);
|
|
36
|
+
const parent = (0, _element.useContext)(_entityContext.EntityContext);
|
|
47
37
|
const childContext = (0, _element.useMemo)(() => ({
|
|
48
38
|
...parent,
|
|
49
39
|
[kind]: {
|
|
@@ -51,184 +41,9 @@ function EntityProvider({
|
|
|
51
41
|
[name]: id
|
|
52
42
|
}
|
|
53
43
|
}), [parent, kind, name, id]);
|
|
54
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(EntityContext.Provider, {
|
|
44
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_entityContext.EntityContext.Provider, {
|
|
55
45
|
value: childContext,
|
|
56
46
|
children: children
|
|
57
47
|
});
|
|
58
48
|
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Hook that returns the ID for the nearest
|
|
62
|
-
* provided entity of the specified type.
|
|
63
|
-
*
|
|
64
|
-
* @param {string} kind The entity kind.
|
|
65
|
-
* @param {string} name The entity name.
|
|
66
|
-
*/
|
|
67
|
-
function useEntityId(kind, name) {
|
|
68
|
-
const context = (0, _element.useContext)(EntityContext);
|
|
69
|
-
return context?.[kind]?.[name];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Hook that returns the value and a setter for the
|
|
74
|
-
* specified property of the nearest provided
|
|
75
|
-
* entity of the specified type.
|
|
76
|
-
*
|
|
77
|
-
* @param {string} kind The entity kind.
|
|
78
|
-
* @param {string} name The entity name.
|
|
79
|
-
* @param {string} prop The property name.
|
|
80
|
-
* @param {string} [_id] An entity ID to use instead of the context-provided one.
|
|
81
|
-
*
|
|
82
|
-
* @return {[*, Function, *]} An array where the first item is the
|
|
83
|
-
* property value, the second is the
|
|
84
|
-
* setter and the third is the full value
|
|
85
|
-
* object from REST API containing more
|
|
86
|
-
* information like `raw`, `rendered` and
|
|
87
|
-
* `protected` props.
|
|
88
|
-
*/
|
|
89
|
-
function useEntityProp(kind, name, prop, _id) {
|
|
90
|
-
const providerId = useEntityId(kind, name);
|
|
91
|
-
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
92
|
-
const {
|
|
93
|
-
value,
|
|
94
|
-
fullValue
|
|
95
|
-
} = (0, _data.useSelect)(select => {
|
|
96
|
-
const {
|
|
97
|
-
getEntityRecord,
|
|
98
|
-
getEditedEntityRecord
|
|
99
|
-
} = select(_name.STORE_NAME);
|
|
100
|
-
const record = getEntityRecord(kind, name, id); // Trigger resolver.
|
|
101
|
-
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
102
|
-
return record && editedRecord ? {
|
|
103
|
-
value: editedRecord[prop],
|
|
104
|
-
fullValue: record[prop]
|
|
105
|
-
} : {};
|
|
106
|
-
}, [kind, name, id, prop]);
|
|
107
|
-
const {
|
|
108
|
-
editEntityRecord
|
|
109
|
-
} = (0, _data.useDispatch)(_name.STORE_NAME);
|
|
110
|
-
const setValue = (0, _element.useCallback)(newValue => {
|
|
111
|
-
editEntityRecord(kind, name, id, {
|
|
112
|
-
[prop]: newValue
|
|
113
|
-
});
|
|
114
|
-
}, [editEntityRecord, kind, name, id, prop]);
|
|
115
|
-
return [value, setValue, fullValue];
|
|
116
|
-
}
|
|
117
|
-
const parsedBlocksCache = new WeakMap();
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Hook that returns block content getters and setters for
|
|
121
|
-
* the nearest provided entity of the specified type.
|
|
122
|
-
*
|
|
123
|
-
* The return value has the shape `[ blocks, onInput, onChange ]`.
|
|
124
|
-
* `onInput` is for block changes that don't create undo levels
|
|
125
|
-
* or dirty the post, non-persistent changes, and `onChange` is for
|
|
126
|
-
* persistent changes. They map directly to the props of a
|
|
127
|
-
* `BlockEditorProvider` and are intended to be used with it,
|
|
128
|
-
* or similar components or hooks.
|
|
129
|
-
*
|
|
130
|
-
* @param {string} kind The entity kind.
|
|
131
|
-
* @param {string} name The entity name.
|
|
132
|
-
* @param {Object} options
|
|
133
|
-
* @param {string} [options.id] An entity ID to use instead of the context-provided one.
|
|
134
|
-
*
|
|
135
|
-
* @return {[unknown[], Function, Function]} The block array and setters.
|
|
136
|
-
*/
|
|
137
|
-
function useEntityBlockEditor(kind, name, {
|
|
138
|
-
id: _id
|
|
139
|
-
} = {}) {
|
|
140
|
-
const providerId = useEntityId(kind, name);
|
|
141
|
-
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
142
|
-
const {
|
|
143
|
-
getEntityRecord,
|
|
144
|
-
getEntityRecordEdits
|
|
145
|
-
} = (0, _data.useSelect)(_name.STORE_NAME);
|
|
146
|
-
const {
|
|
147
|
-
content,
|
|
148
|
-
editedBlocks,
|
|
149
|
-
meta
|
|
150
|
-
} = (0, _data.useSelect)(select => {
|
|
151
|
-
if (!id) {
|
|
152
|
-
return {};
|
|
153
|
-
}
|
|
154
|
-
const {
|
|
155
|
-
getEditedEntityRecord
|
|
156
|
-
} = select(_name.STORE_NAME);
|
|
157
|
-
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
158
|
-
return {
|
|
159
|
-
editedBlocks: editedRecord.blocks,
|
|
160
|
-
content: editedRecord.content,
|
|
161
|
-
meta: editedRecord.meta
|
|
162
|
-
};
|
|
163
|
-
}, [kind, name, id]);
|
|
164
|
-
const {
|
|
165
|
-
__unstableCreateUndoLevel,
|
|
166
|
-
editEntityRecord
|
|
167
|
-
} = (0, _data.useDispatch)(_name.STORE_NAME);
|
|
168
|
-
const blocks = (0, _element.useMemo)(() => {
|
|
169
|
-
if (!id) {
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
|
-
if (editedBlocks) {
|
|
173
|
-
return editedBlocks;
|
|
174
|
-
}
|
|
175
|
-
if (!content || typeof content !== 'string') {
|
|
176
|
-
return EMPTY_ARRAY;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// If there's an edit, cache the parsed blocks by the edit.
|
|
180
|
-
// If not, cache by the original enity record.
|
|
181
|
-
const edits = getEntityRecordEdits(kind, name, id);
|
|
182
|
-
const isUnedited = !edits || !Object.keys(edits).length;
|
|
183
|
-
const cackeKey = isUnedited ? getEntityRecord(kind, name, id) : edits;
|
|
184
|
-
let _blocks = parsedBlocksCache.get(cackeKey);
|
|
185
|
-
if (!_blocks) {
|
|
186
|
-
_blocks = (0, _blocks2.parse)(content);
|
|
187
|
-
parsedBlocksCache.set(cackeKey, _blocks);
|
|
188
|
-
}
|
|
189
|
-
return _blocks;
|
|
190
|
-
}, [kind, name, id, editedBlocks, content, getEntityRecord, getEntityRecordEdits]);
|
|
191
|
-
const updateFootnotes = (0, _element.useCallback)(_blocks => (0, _footnotes.updateFootnotesFromMeta)(_blocks, meta), [meta]);
|
|
192
|
-
const onChange = (0, _element.useCallback)((newBlocks, options) => {
|
|
193
|
-
const noChange = blocks === newBlocks;
|
|
194
|
-
if (noChange) {
|
|
195
|
-
return __unstableCreateUndoLevel(kind, name, id);
|
|
196
|
-
}
|
|
197
|
-
const {
|
|
198
|
-
selection,
|
|
199
|
-
...rest
|
|
200
|
-
} = options;
|
|
201
|
-
|
|
202
|
-
// We create a new function here on every persistent edit
|
|
203
|
-
// to make sure the edit makes the post dirty and creates
|
|
204
|
-
// a new undo level.
|
|
205
|
-
const edits = {
|
|
206
|
-
selection,
|
|
207
|
-
content: ({
|
|
208
|
-
blocks: blocksForSerialization = []
|
|
209
|
-
}) => (0, _blocks2.__unstableSerializeAndClean)(blocksForSerialization),
|
|
210
|
-
...updateFootnotes(newBlocks)
|
|
211
|
-
};
|
|
212
|
-
editEntityRecord(kind, name, id, edits, {
|
|
213
|
-
isCached: false,
|
|
214
|
-
...rest
|
|
215
|
-
});
|
|
216
|
-
}, [kind, name, id, blocks, updateFootnotes, __unstableCreateUndoLevel, editEntityRecord]);
|
|
217
|
-
const onInput = (0, _element.useCallback)((newBlocks, options) => {
|
|
218
|
-
const {
|
|
219
|
-
selection,
|
|
220
|
-
...rest
|
|
221
|
-
} = options;
|
|
222
|
-
const footnotesChanges = updateFootnotes(newBlocks);
|
|
223
|
-
const edits = {
|
|
224
|
-
selection,
|
|
225
|
-
...footnotesChanges
|
|
226
|
-
};
|
|
227
|
-
editEntityRecord(kind, name, id, edits, {
|
|
228
|
-
isCached: true,
|
|
229
|
-
...rest
|
|
230
|
-
});
|
|
231
|
-
}, [kind, name, id, updateFootnotes, editEntityRecord]);
|
|
232
|
-
return [blocks, onInput, onChange];
|
|
233
|
-
}
|
|
234
49
|
//# sourceMappingURL=entity-provider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_element","require","_data","_blocks2","_name","_footnotes","_jsxRuntime","EMPTY_ARRAY","EntityContext","createContext","EntityProvider","kind","type","name","id","children","parent","useContext","childContext","useMemo","jsx","Provider","value","useEntityId","context","useEntityProp","prop","_id","providerId","fullValue","useSelect","select","getEntityRecord","getEditedEntityRecord","STORE_NAME","record","editedRecord","editEntityRecord","useDispatch","setValue","useCallback","newValue","parsedBlocksCache","WeakMap","useEntityBlockEditor","getEntityRecordEdits","content","editedBlocks","meta","blocks","__unstableCreateUndoLevel","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","parse","set","updateFootnotes","updateFootnotesFromMeta","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","__unstableSerializeAndClean","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, onChange ];\n}\n"],"mappings":";;;;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAMA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AAKA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,UAAA,GAAAJ,OAAA;AAAsD,IAAAK,WAAA,GAAAL,OAAA;AAhBtD;AACA;AACA;;AAUA;AACA;AACA;;AAIA,MAAMM,WAAW,GAAG,EAAE;AAEtB,MAAMC,aAAa,GAAG,IAAAC,sBAAa,EAAE,CAAC,CAAE,CAAC;;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASC,cAAcA,CAAE;EAAEC,IAAI;EAAEC,IAAI,EAAEC,IAAI;EAAEC,EAAE;EAAEC;AAAS,CAAC,EAAG;EAC5E,MAAMC,MAAM,GAAG,IAAAC,mBAAU,EAAET,aAAc,CAAC;EAC1C,MAAMU,YAAY,GAAG,IAAAC,gBAAO,EAC3B,OAAQ;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AAEG,SAAU,CAAC;IACrD,MAAMb,KAAK,GAAG;MAAEgB,SAAS;MAAE,GAAGM;IAAiB,CAAC;IAEhDrC,gBAAgB,CAAE1B,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAEsC,KAAK,EAAE;MACxCoB,QAAQ,EAAE,IAAI;MACd,GAAGH;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CAAE1D,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAEgD,eAAe,EAAEzB,gBAAgB,CACpD,CAAC;EAED,OAAO,CAAEY,MAAM,EAAEwB,OAAO,EAAET,QAAQ,CAAE;AACrC","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_element","require","_entityContext","_jsxRuntime","EntityProvider","kind","type","name","id","children","parent","useContext","EntityContext","childContext","useMemo","jsx","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":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAKA,IAAAC,cAAA,GAAAD,OAAA;AAAiD,IAAAE,WAAA,GAAAF,OAAA;AARjD;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GACe,SAASG,cAAcA,CAAE;EAAEC,IAAI;EAAEC,IAAI,EAAEC,IAAI;EAAEC,EAAE;EAAEC;AAAS,CAAC,EAAG;EAC5E,MAAMC,MAAM,GAAG,IAAAC,mBAAU,EAAEC,4BAAc,CAAC;EAC1C,MAAMC,YAAY,GAAG,IAAAC,gBAAO,EAC3B,OAAQ;IACP,GAAGJ,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,oBACC,IAAAL,WAAA,CAAAY,GAAA,EAACb,cAAA,CAAAU,aAAa,CAACI,QAAQ;IAACC,KAAK,EAAGJ,YAAc;IAAAJ,QAAA,EAC3CA;EAAQ,CACa,CAAC;AAE3B","ignoreList":[]}
|
package/build/hooks/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
4
|
Object.defineProperty(exports, "__esModule", {
|
|
4
5
|
value: true
|
|
5
6
|
});
|
|
@@ -21,6 +22,24 @@ Object.defineProperty(exports, "__experimentalUseResourcePermissions", {
|
|
|
21
22
|
return _useResourcePermissions.__experimentalUseResourcePermissions;
|
|
22
23
|
}
|
|
23
24
|
});
|
|
25
|
+
Object.defineProperty(exports, "useEntityBlockEditor", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _useEntityBlockEditor.default;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "useEntityId", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return _useEntityId.default;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, "useEntityProp", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _useEntityProp.default;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
24
43
|
Object.defineProperty(exports, "useEntityRecord", {
|
|
25
44
|
enumerable: true,
|
|
26
45
|
get: function () {
|
|
@@ -42,6 +61,9 @@ Object.defineProperty(exports, "useResourcePermissions", {
|
|
|
42
61
|
var _useEntityRecord = _interopRequireWildcard(require("./use-entity-record"));
|
|
43
62
|
var _useEntityRecords = _interopRequireWildcard(require("./use-entity-records"));
|
|
44
63
|
var _useResourcePermissions = _interopRequireWildcard(require("./use-resource-permissions"));
|
|
64
|
+
var _useEntityBlockEditor = _interopRequireDefault(require("./use-entity-block-editor"));
|
|
65
|
+
var _useEntityId = _interopRequireDefault(require("./use-entity-id"));
|
|
66
|
+
var _useEntityProp = _interopRequireDefault(require("./use-entity-prop"));
|
|
45
67
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
46
68
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
47
69
|
//# sourceMappingURL=index.js.map
|
package/build/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_useEntityRecord","_interopRequireWildcard","require","_useEntityRecords","_useResourcePermissions","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set"],"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":"
|
|
1
|
+
{"version":3,"names":["_useEntityRecord","_interopRequireWildcard","require","_useEntityRecords","_useResourcePermissions","_useEntityBlockEditor","_interopRequireDefault","_useEntityId","_useEntityProp","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set"],"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,IAAAA,gBAAA,GAAAC,uBAAA,CAAAC,OAAA;AAIA,IAAAC,iBAAA,GAAAF,uBAAA,CAAAC,OAAA;AAIA,IAAAE,uBAAA,GAAAH,uBAAA,CAAAC,OAAA;AAIA,IAAAG,qBAAA,GAAAC,sBAAA,CAAAJ,OAAA;AACA,IAAAK,YAAA,GAAAD,sBAAA,CAAAJ,OA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/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = useEntityBlockEditor;
|
|
8
|
+
var _element = require("@wordpress/element");
|
|
9
|
+
var _data = require("@wordpress/data");
|
|
10
|
+
var _blocks2 = require("@wordpress/blocks");
|
|
11
|
+
var _name = require("../name");
|
|
12
|
+
var _useEntityId = _interopRequireDefault(require("./use-entity-id"));
|
|
13
|
+
var _footnotes = require("../footnotes");
|
|
14
|
+
/**
|
|
15
|
+
* WordPress dependencies
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Internal dependencies
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const EMPTY_ARRAY = [];
|
|
23
|
+
const parsedBlocksCache = new WeakMap();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Hook that returns block content getters and setters for
|
|
27
|
+
* the nearest provided entity of the specified type.
|
|
28
|
+
*
|
|
29
|
+
* The return value has the shape `[ blocks, onInput, onChange ]`.
|
|
30
|
+
* `onInput` is for block changes that don't create undo levels
|
|
31
|
+
* or dirty the post, non-persistent changes, and `onChange` is for
|
|
32
|
+
* persistent changes. They map directly to the props of a
|
|
33
|
+
* `BlockEditorProvider` and are intended to be used with it,
|
|
34
|
+
* or similar components or hooks.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} kind The entity kind.
|
|
37
|
+
* @param {string} name The entity name.
|
|
38
|
+
* @param {Object} options
|
|
39
|
+
* @param {string} [options.id] An entity ID to use instead of the context-provided one.
|
|
40
|
+
*
|
|
41
|
+
* @return {[unknown[], Function, Function]} The block array and setters.
|
|
42
|
+
*/
|
|
43
|
+
function useEntityBlockEditor(kind, name, {
|
|
44
|
+
id: _id
|
|
45
|
+
} = {}) {
|
|
46
|
+
const providerId = (0, _useEntityId.default)(kind, name);
|
|
47
|
+
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
48
|
+
const {
|
|
49
|
+
getEntityRecord,
|
|
50
|
+
getEntityRecordEdits
|
|
51
|
+
} = (0, _data.useSelect)(_name.STORE_NAME);
|
|
52
|
+
const {
|
|
53
|
+
content,
|
|
54
|
+
editedBlocks,
|
|
55
|
+
meta
|
|
56
|
+
} = (0, _data.useSelect)(select => {
|
|
57
|
+
if (!id) {
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
const {
|
|
61
|
+
getEditedEntityRecord
|
|
62
|
+
} = select(_name.STORE_NAME);
|
|
63
|
+
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
64
|
+
return {
|
|
65
|
+
editedBlocks: editedRecord.blocks,
|
|
66
|
+
content: editedRecord.content,
|
|
67
|
+
meta: editedRecord.meta
|
|
68
|
+
};
|
|
69
|
+
}, [kind, name, id]);
|
|
70
|
+
const {
|
|
71
|
+
__unstableCreateUndoLevel,
|
|
72
|
+
editEntityRecord
|
|
73
|
+
} = (0, _data.useDispatch)(_name.STORE_NAME);
|
|
74
|
+
const blocks = (0, _element.useMemo)(() => {
|
|
75
|
+
if (!id) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
if (editedBlocks) {
|
|
79
|
+
return editedBlocks;
|
|
80
|
+
}
|
|
81
|
+
if (!content || typeof content !== 'string') {
|
|
82
|
+
return EMPTY_ARRAY;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// If there's an edit, cache the parsed blocks by the edit.
|
|
86
|
+
// If not, cache by the original enity record.
|
|
87
|
+
const edits = getEntityRecordEdits(kind, name, id);
|
|
88
|
+
const isUnedited = !edits || !Object.keys(edits).length;
|
|
89
|
+
const cackeKey = isUnedited ? getEntityRecord(kind, name, id) : edits;
|
|
90
|
+
let _blocks = parsedBlocksCache.get(cackeKey);
|
|
91
|
+
if (!_blocks) {
|
|
92
|
+
_blocks = (0, _blocks2.parse)(content);
|
|
93
|
+
parsedBlocksCache.set(cackeKey, _blocks);
|
|
94
|
+
}
|
|
95
|
+
return _blocks;
|
|
96
|
+
}, [kind, name, id, editedBlocks, content, getEntityRecord, getEntityRecordEdits]);
|
|
97
|
+
const updateFootnotes = (0, _element.useCallback)(_blocks => (0, _footnotes.updateFootnotesFromMeta)(_blocks, meta), [meta]);
|
|
98
|
+
const onChange = (0, _element.useCallback)((newBlocks, options) => {
|
|
99
|
+
const noChange = blocks === newBlocks;
|
|
100
|
+
if (noChange) {
|
|
101
|
+
return __unstableCreateUndoLevel(kind, name, id);
|
|
102
|
+
}
|
|
103
|
+
const {
|
|
104
|
+
selection,
|
|
105
|
+
...rest
|
|
106
|
+
} = options;
|
|
107
|
+
|
|
108
|
+
// We create a new function here on every persistent edit
|
|
109
|
+
// to make sure the edit makes the post dirty and creates
|
|
110
|
+
// a new undo level.
|
|
111
|
+
const edits = {
|
|
112
|
+
selection,
|
|
113
|
+
content: ({
|
|
114
|
+
blocks: blocksForSerialization = []
|
|
115
|
+
}) => (0, _blocks2.__unstableSerializeAndClean)(blocksForSerialization),
|
|
116
|
+
...updateFootnotes(newBlocks)
|
|
117
|
+
};
|
|
118
|
+
editEntityRecord(kind, name, id, edits, {
|
|
119
|
+
isCached: false,
|
|
120
|
+
...rest
|
|
121
|
+
});
|
|
122
|
+
}, [kind, name, id, blocks, updateFootnotes, __unstableCreateUndoLevel, editEntityRecord]);
|
|
123
|
+
const onInput = (0, _element.useCallback)((newBlocks, options) => {
|
|
124
|
+
const {
|
|
125
|
+
selection,
|
|
126
|
+
...rest
|
|
127
|
+
} = options;
|
|
128
|
+
const footnotesChanges = updateFootnotes(newBlocks);
|
|
129
|
+
const edits = {
|
|
130
|
+
selection,
|
|
131
|
+
...footnotesChanges
|
|
132
|
+
};
|
|
133
|
+
editEntityRecord(kind, name, id, edits, {
|
|
134
|
+
isCached: true,
|
|
135
|
+
...rest
|
|
136
|
+
});
|
|
137
|
+
}, [kind, name, id, updateFootnotes, editEntityRecord]);
|
|
138
|
+
return [blocks, onInput, onChange];
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=use-entity-block-editor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_element","require","_data","_blocks2","_name","_useEntityId","_interopRequireDefault","_footnotes","EMPTY_ARRAY","parsedBlocksCache","WeakMap","useEntityBlockEditor","kind","name","id","_id","providerId","useEntityId","getEntityRecord","getEntityRecordEdits","useSelect","STORE_NAME","content","editedBlocks","meta","select","getEditedEntityRecord","editedRecord","blocks","__unstableCreateUndoLevel","editEntityRecord","useDispatch","useMemo","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","parse","set","updateFootnotes","useCallback","updateFootnotesFromMeta","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","__unstableSerializeAndClean","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":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AAKA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,YAAA,GAAAC,sBAAA,CAAAL,OAAA;AACA,IAAAM,UAAA,GAAAN,OAAA;AAZA;AACA;AACA;;AAKA;AACA;AACA;;AAKA,MAAMO,WAAW,GAAG,EAAE;AACtB,MAAMC,iBAAiB,GAAG,IAAIC,OAAO,CAAC,CAAC;;AAEvC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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D,IAAAC,oCAA2B,EAAED,sBAAuB,CAAC;MACtD,GAAGT,eAAe,CAAEI,SAAU;IAC/B,CAAC;IAEDlB,gBAAgB,CAAElB,IAAI,EAAEC,IAAI,EAAEC,EAAE,EAAEoB,KAAK,EAAE;MACxCqB,QAAQ,EAAE,KAAK;MACf,GAAGH;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CACCxC,IAAI,EACJC,IAAI,EACJC,EAAE,EACFc,MAAM,EACNgB,eAAe,EACff,yBAAyB,EACzBC,gBAAgB,CAElB,CAAC;EAED,MAAM0B,OAAO,GAAG,IAAAX,oBAAW,EAC1B,CAAEG,SAAS,EAAEC,OAAO,KAAM;IACzB,MAAM;MAAEE,SAAS;MAAE,GAAGC;IAAK,CAAC,GAAGH,OAAO;IACtC,MAAMQ,gBAAgB,GAAGb,eAAe,CAAEI,SAAU,CAAC;IACrD,MAAMd,KAAK,GAAG;MAAEiB,SAAS;MAAE,GAAGM;IAAiB,CAAC;IAEhD3B,gBAAgB,CAAElB,IAAI,EAAEC,IAAI,EAAEC,EAAE,EAAEoB,KAAK,EAAE;MACxCqB,QAAQ,EAAE,IAAI;MACd,GAAGH;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CAAExC,IAAI,EAAEC,IAAI,EAAEC,EAAE,EAAE8B,eAAe,EAAEd,gBAAgB,CACpD,CAAC;EAED,OAAO,CAAEF,MAAM,EAAE4B,OAAO,EAAET,QAAQ,CAAE;AACrC","ignoreList":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useEntityId;
|
|
7
|
+
var _element = require("@wordpress/element");
|
|
8
|
+
var _entityContext = require("../entity-context");
|
|
9
|
+
/**
|
|
10
|
+
* WordPress dependencies
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Internal dependencies
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Hook that returns the ID for the nearest
|
|
19
|
+
* provided entity of the specified type.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} kind The entity kind.
|
|
22
|
+
* @param {string} name The entity name.
|
|
23
|
+
*/
|
|
24
|
+
function useEntityId(kind, name) {
|
|
25
|
+
const context = (0, _element.useContext)(_entityContext.EntityContext);
|
|
26
|
+
return context?.[kind]?.[name];
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=use-entity-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_element","require","_entityContext","useEntityId","kind","name","context","useContext","EntityContext"],"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":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAKA,IAAAC,cAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASE,WAAWA,CAAEC,IAAI,EAAEC,IAAI,EAAG;EACjD,MAAMC,OAAO,GAAG,IAAAC,mBAAU,EAAEC,4BAAc,CAAC;EAC3C,OAAOF,OAAO,GAAIF,IAAI,CAAE,GAAIC,IAAI,CAAE;AACnC","ignoreList":[]}
|