@plaudit/gutenberg-api-extensions 2.82.0 → 2.84.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/blocks/SNPGroupComponent.js.map +1 -1
  3. package/dist/blocks/common-native-property-constructors.js +3 -2
  4. package/dist/blocks/common-native-property-constructors.js.map +1 -1
  5. package/dist/blocks/data-controller/actions.d.ts +135 -0
  6. package/dist/blocks/data-controller/actions.js +19 -0
  7. package/dist/blocks/data-controller/actions.js.map +1 -0
  8. package/dist/blocks/data-controller/reducer.d.ts +4 -0
  9. package/dist/blocks/data-controller/reducer.js +143 -0
  10. package/dist/blocks/data-controller/reducer.js.map +1 -0
  11. package/dist/blocks/data-controller/trigger-handlers.d.ts +140 -0
  12. package/dist/blocks/data-controller/trigger-handlers.js +117 -0
  13. package/dist/blocks/data-controller/trigger-handlers.js.map +1 -0
  14. package/dist/blocks/data-controller/utils.d.ts +43 -0
  15. package/dist/blocks/data-controller/utils.js +359 -0
  16. package/dist/blocks/data-controller/utils.js.map +1 -0
  17. package/dist/blocks/data-controller.d.ts +4 -37
  18. package/dist/blocks/data-controller.js +33 -604
  19. package/dist/blocks/data-controller.js.map +1 -1
  20. package/dist/blocks/layered-styles-impl.js +9 -8
  21. package/dist/blocks/layered-styles-impl.js.map +1 -1
  22. package/dist/blocks/layout/NodeContext.js +3 -3
  23. package/dist/blocks/layout/NodeContext.js.map +1 -1
  24. package/dist/blocks/simple-native-property-api.d.ts +10 -7
  25. package/dist/blocks/simple-native-property-api.js.map +1 -1
  26. package/dist/blocks/simple-native-property-impl.js +29 -24
  27. package/dist/blocks/simple-native-property-impl.js.map +1 -1
  28. package/dist/blocks/snp-data-store.d.ts +9 -6
  29. package/dist/blocks/snp-data-store.js +17 -12
  30. package/dist/blocks/snp-data-store.js.map +1 -1
  31. package/dist/controls/FullSizeToggleControl.d.ts +1 -1
  32. package/dist/controls/FullSizeToggleControl.js.map +1 -1
  33. package/dist/lib/gutenberg-api-extensions-state/layered-block-styles-logic.js +12 -12
  34. package/dist/lib/gutenberg-api-extensions-state/layered-block-styles-logic.js.map +1 -1
  35. package/dist/lib/gutenberg-api-extensions-state/snp-logic.js +2 -2
  36. package/dist/lib/gutenberg-api-extensions-state/snp-logic.js.map +1 -1
  37. package/package.json +7 -7
  38. package/simple-native-properties.md +1 -0
  39. package/src/blocks/SNPGroupComponent.tsx +1 -1
  40. package/src/blocks/common-native-property-constructors.tsx +3 -2
  41. package/src/blocks/data-controller/actions.ts +20 -0
  42. package/src/blocks/data-controller/reducer.ts +149 -0
  43. package/src/blocks/data-controller/trigger-handlers.ts +138 -0
  44. package/src/blocks/data-controller/utils.ts +339 -0
  45. package/src/blocks/data-controller.ts +38 -605
  46. package/src/blocks/layered-styles-impl.ts +10 -9
  47. package/src/blocks/layout/NodeContext.tsx +3 -3
  48. package/src/blocks/simple-native-property-api.ts +12 -10
  49. package/src/blocks/simple-native-property-impl.tsx +31 -24
  50. package/src/blocks/snp-data-store.ts +22 -13
  51. package/src/controls/FullSizeToggleControl.tsx +3 -3
  52. package/src/lib/gutenberg-api-extensions-state/layered-block-styles-logic.ts +12 -11
  53. package/src/lib/gutenberg-api-extensions-state/snp-logic.ts +2 -2
@@ -0,0 +1,339 @@
1
+ import {doAction} from "@wordpress/hooks";
2
+
3
+ import {Draft, produce} from "immer";
4
+
5
+ import type {DCNode, DCStoreState} from "../data-controller";
6
+ import {MoveError} from "../MoveError";
7
+ import {PathError, PathErrorType} from "../PathError";
8
+ import {buildDefaultValueFromDefinition} from "../simple-native-property-internal-shared";
9
+ import type {DataStore, HydratedSimpleNativeProperty, NodePath, RawPath} from "../simple-native-property-api";
10
+
11
+ export function removeFromParentNode(parentNode: any|undefined, path: RawPath) {
12
+ if (typeof parentNode !== 'object' || parentNode === undefined || parentNode === null) {
13
+ throw new PathError("Encountered a non-existent node while expecting one.", path);
14
+ }
15
+ const nodeName = path[path.length - 1];
16
+ if (typeof nodeName === 'string') {
17
+ if (Array.isArray(parentNode)) {
18
+ throw new PathError(
19
+ "Encountered a node with indexed descendants while expecting one with string-keyed descendants.",
20
+ path, path.length - 1);
21
+ }
22
+ delete parentNode[nodeName];
23
+ } else if (nodeName === undefined) {
24
+ throw new PathError("Encountered an undefined name in a path.", path);
25
+ } else {
26
+ if (!Array.isArray(parentNode)) {
27
+ throw new PathError(
28
+ "Encountered a node with string-keyed descendants while expecting one with indexed descendants.",
29
+ path, path.length - 1);
30
+ }
31
+ parentNode.splice(nodeName, 1);
32
+ }
33
+ }
34
+
35
+ export function moveNodeWithinParent(parentNode: any|undefined|null, path: RawPath, move: {from: number, to: number}|{from: string, to: string}) {
36
+ if (typeof parentNode !== 'object' || parentNode === undefined || parentNode === null) {
37
+ throw new PathError("Encountered a non-existent node while expecting one.", path);
38
+ }
39
+ if (isNumericMove(move)) {
40
+ if (!Array.isArray(parentNode)) {
41
+ throw new MoveError("Attempted to move a child of a node with string-keyed descendants by index", path, move);
42
+ }
43
+ if (move.from > move.to) {
44
+ parentNode.splice(move.to, 0, ...parentNode.splice(move.from, 1));
45
+ } else { //We know that
46
+ parentNode.splice(move.to - 1, 0, ...parentNode.splice(move.from, 1));
47
+ }
48
+ } else {
49
+ if (Array.isArray(parentNode)) {
50
+ throw new MoveError("Attempted to move a child of a node with indexed descendants by string-key", path, move);
51
+ }
52
+ if (parentNode[move.to] !== undefined) {
53
+ throw new MoveError("Attempted to move a string-keyed child to the same key as another", path, move);
54
+ }
55
+ }
56
+ }
57
+ export function isNumericMove(move: {from: number, to: number}|{from: string, to: string}): move is {from: number, to: number} {
58
+ return typeof move.from === 'number' && typeof move.to === 'number';
59
+ }
60
+
61
+ export function resolveParentNodeDefinition(root: DCNode, path: RawPath): HydratedSimpleNativeProperty|undefined {
62
+ if (path.length < 2) {
63
+ throw new PathError("Unable to get the parent node of a path with less than two nodes", path, path.length - 1, PathErrorType.INSUFFICIENT_LENGTH);
64
+ }
65
+ return walkToNode(root, path.slice(0, path.length - (typeof path[path.length - 2] === 'number' ? 2 : 1))).definition;
66
+ }
67
+
68
+ export function populateValueBasedOnDefinition(value: any, definition: HydratedSimpleNativeProperty|undefined): any {
69
+ const typedChildren = Array.isArray(definition?.children) ? definition.children : definition?.children?.[value?.type];
70
+ if (!typedChildren?.length) {
71
+ return value ?? buildDefaultValueFromDefinition(definition);
72
+ } else if (value === null || value === undefined) {
73
+ return buildDefaultValueFromDefinition(definition);
74
+ } else if (typeof value !== 'object') {
75
+ return value;
76
+ } else if (Array.isArray(value)) {
77
+ return value;
78
+ }
79
+ return {...value, ...Object.fromEntries(typedChildren.map(def => [def.name, populateValueBasedOnDefinition(value[def.name], def)]))};
80
+ }
81
+
82
+ export function walkToNode(root: DCNode, path: RawPath): DCNode {
83
+ let current = root;
84
+ for (let i = 0; i < path.length; i++) {
85
+ current = descendIntoDCNodeChild(current, path, i);
86
+ }
87
+ return current;
88
+ }
89
+ export function descendIntoDCNodeChild(current: DCNode|undefined, path: RawPath, i: number): DCNode {
90
+ if (current?.children === undefined) {
91
+ throw new PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathErrorType.UNEXPECTED_LEAF);
92
+ }
93
+ const pathNode = path[i];
94
+ if (typeof pathNode === 'string') {
95
+ if (Array.isArray(current.children)) {
96
+ throw new PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, i);
97
+ }
98
+ current = current.children[pathNode];
99
+ } else if (pathNode === undefined) {
100
+ throw new PathError("Encountered an undefined name in a path.", path, i);
101
+ } else {
102
+ if (!Array.isArray(current.children)) {
103
+ throw new PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, i);
104
+ }
105
+ current = current.children[pathNode];
106
+ }
107
+ if (current === undefined) {
108
+ throw new PathError("Encountered a non-existent node while expecting one.", path, i, PathErrorType.UNEXPECTED_LEAF);
109
+ }
110
+ return current;
111
+ }
112
+
113
+ export function getDataStore(property: string, state: Draft<DCStoreState>, throwOnError: false): DataStore|undefined;
114
+ export function getDataStore(property: string, state: Draft<DCStoreState>, throwOnError?: undefined|true): DataStore;
115
+ export function getDataStore(property: string, state: Draft<DCStoreState>, throwOnError = true): DataStore|undefined {
116
+ let dataStore = state.dataStores.byProperty[property];
117
+ if (dataStore) {
118
+ return dataStore;
119
+ }
120
+ dataStore = state.dataStores.list.find(dataStore => dataStore.handlesProperty(property));
121
+ if (dataStore) {
122
+ console.warn(`Unable to locate the DataStore for ${property} via the byProperty map, but did find a DataStore that handles that property. This should not be possible.`);
123
+ } else if (throwOnError) {
124
+ throw new Error(`Unable to resolve the dataStore for ${property} on block ${state.blockClientId}`);
125
+ }
126
+ return dataStore;
127
+ }
128
+
129
+ export function getOptionalValue(path: NodePath, state: Draft<DCStoreState>) {
130
+ try {
131
+ return walkToNodeInValue(getDataStore(path[0], state).getValue(path[0]), path);
132
+ } catch (e) {
133
+ if (e instanceof PathError) {
134
+ return undefined;
135
+ }
136
+ throw e;
137
+ }
138
+ }
139
+
140
+ export function removeValueFromBackingDataStore(dataStore: DataStore, path: NodePath) {
141
+ const dsValue = dataStore.getValue(path[0]);
142
+ if (path.length === 1) {
143
+ dataStore.setValue(path[0], undefined);
144
+ } else {
145
+ dataStore.setValue(path[0], produce(dsValue, (dsValueDraft: any) => {
146
+ const nodeParent = walkToNodeInValue(dsValueDraft, path.slice(0, path.length - 1));
147
+ const nodeName = path[path.length - 1];
148
+ if (typeof nodeName === 'string') {
149
+ delete nodeParent[nodeName];
150
+ } else if (nodeName === undefined) {
151
+ throw new PathError("Encountered an undefined name in a path.", path);
152
+ } else {
153
+ (nodeParent as any[]).splice(nodeName, 1);
154
+ }
155
+ }));
156
+ }
157
+ }
158
+ export function writeValueToBackingDataStoreWithCorrections(path: NodePath, state: DCStoreState, value: any) {
159
+ if (typeof value === 'number' && Number.isNaN(value)) {
160
+ value = undefined; // This ensures that NaN's don't get stored
161
+ }
162
+ const rootDCNode = state.treeRoot.children[path[0]];
163
+ if (rootDCNode === undefined) {
164
+ throw new PathError("Encountered a path pointing to a non-existent root node.", path, 0);
165
+ }
166
+ const dataStore = getDataStore(path[0], state);
167
+
168
+ // This path both handles scalar values and ensures that the root "value" of Groups and Lists are initialized before writing the actual attribute
169
+ // In order to avoid accidentally overwriting group data when lazily adding items (i.e. via a ToolsPanel instance), we skip this code path if the group's root value has already been initialized
170
+ if (path.length === 1 || (value === undefined && dataStore.getValue(path[0]) === undefined)) {
171
+ const writtenValue = value ?? buildDefaultValueFromDefinition(rootDCNode.definition, path.length === 1);
172
+ dataStore.setValue(path[0], writtenValue);
173
+ if (rootDCNode.definition) {
174
+ doAction('plaudit.gutenbergApiExtensions.simpleNativeProperties.afterWrite', writtenValue, rootDCNode.definition, dataStore);
175
+ }
176
+ if (path.length === 1) {
177
+ return;
178
+ }
179
+ }
180
+
181
+ let closestUsableDefinition = rootDCNode.definition;
182
+ let lastWrittenValue: unknown;
183
+ dataStore.setValue(path[0], produce(dataStore.getValue(path[0]), (dsValueDraft: any) => {
184
+ let currentDSValue = dsValueDraft;
185
+ let currentDCNode = rootDCNode;
186
+ for (let i = 1; i < path.length - 1; i++) {
187
+ const nodeName = path[i];
188
+ currentDCNode = descendIntoDCNodeChild(currentDCNode, path, i);
189
+ if (typeof nodeName === 'string') {
190
+ //We only update the definition being used when we descend into a new node proper
191
+ closestUsableDefinition = currentDCNode.definition;
192
+ } else if (nodeName === undefined) {
193
+ throw new PathError("Encountered an undefined name in a path.", path, i);
194
+ }
195
+
196
+ let nextDSValue = descendIntoDSValueNodeChild(currentDSValue, path, i);
197
+ if (nextDSValue === undefined) {
198
+ lastWrittenValue = currentDSValue[nodeName] = buildDefaultValueFromDefinition(closestUsableDefinition);
199
+ currentDSValue = descendIntoDSValueNodeChild(currentDSValue, path, i);
200
+ if (currentDSValue === undefined) {
201
+ throw new PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathErrorType.UNEXPECTED_LEAF);
202
+ }
203
+ } else {
204
+ currentDSValue = nextDSValue;
205
+ }
206
+ }
207
+ const nodeName = path[path.length - 1];
208
+ if (nodeName === undefined) {
209
+ throw new PathError("Encountered an undefined name in a path.", path);
210
+ }
211
+ lastWrittenValue = currentDSValue[nodeName] = value;
212
+ }));
213
+ if (closestUsableDefinition) {
214
+ doAction('plaudit.gutenbergApiExtensions.simpleNativeProperties.afterWrite', lastWrittenValue, closestUsableDefinition, dataStore);
215
+ }
216
+ }
217
+ export function writeValueToBackingDataStore(dataStore: DataStore, path: NodePath, value: any) {
218
+ if (typeof value === 'number' && Number.isNaN(value)) {
219
+ value = undefined; // This ensures that NaN's don't get stored
220
+ }
221
+ const dsValue = dataStore.getValue(path[0]);
222
+ if (path.length === 1) {
223
+ dataStore.setValue(path[0], value);
224
+ } else {
225
+ dataStore.setValue(path[0], produce(dsValue, (dsValueDraft: any) => {
226
+ const nodeParent = walkToNodeInValue(dsValueDraft, path.slice(0, path.length - 1));
227
+ const nodeName = path[path.length - 1];
228
+ if (nodeName === undefined) {
229
+ throw new PathError("Encountered an undefined name in a path.", path);
230
+ }
231
+ nodeParent[nodeName] = value;
232
+ }));
233
+ }
234
+ }
235
+
236
+ /**
237
+ * @param dsValue a value from a {@link DataStore}
238
+ * @param path this is expected to be the entire path (including the node that was used to get {@link dsValue}
239
+ */
240
+ export function walkToNodeInValue(dsValue: any, path: RawPath) {
241
+ let current = dsValue;
242
+ for (let i = 1; i < path.length; i++) {
243
+ if (current === undefined) {
244
+ throw new PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathErrorType.UNEXPECTED_LEAF);
245
+ }
246
+ current = descendIntoDSValueNodeChild(current, path, i);
247
+ }
248
+ return current;
249
+ }
250
+ function descendIntoDSValueNodeChild(current: any, path: RawPath, i: number) {
251
+ const pathNode = path[i];
252
+ if (typeof pathNode === 'string') {
253
+ if (Array.isArray(current)) {
254
+ throw new PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, i);
255
+ }
256
+ current = current[pathNode];
257
+ } else if (pathNode === undefined) {
258
+ throw new PathError("Encountered an undefined name in a path.", path, i);
259
+ } else {
260
+ if (!Array.isArray(current)) {
261
+ throw new PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, i);
262
+ }
263
+ current = current[pathNode];
264
+ }
265
+ return current;
266
+ }
267
+
268
+ export enum TreeMutatorResult {
269
+ STOP, CONTINUE, SKIP_DESCENDANTS
270
+ }
271
+ export function applyToTree(tree: DCNode, mutator: (node: DCNode, path: NodePath) => TreeMutatorResult, path: RawPath = []) {
272
+ if (tree.children === undefined) {
273
+ return TreeMutatorResult.CONTINUE;
274
+ }
275
+
276
+ const steps: Array<[NodePath, DCNode]> = Array.isArray(tree.children)
277
+ ? tree.children.map((node, index) => [path.toSpliced(path.length, 0, index) as NodePath, node])
278
+ : Object.entries(tree.children).map(([name, node]) => [path.toSpliced(path.length, 0, name) as NodePath, node]);
279
+
280
+ for (const [nodePath, node] of steps) {
281
+ const mutatorRes = mutator(node, nodePath);
282
+ if (!mutatorRes) {
283
+ return TreeMutatorResult.STOP;
284
+ } else if (mutatorRes === TreeMutatorResult.CONTINUE) {
285
+ if (!applyToTree(node, mutator, nodePath)) {
286
+ return TreeMutatorResult.STOP;
287
+ }
288
+ }
289
+ }
290
+ return TreeMutatorResult.CONTINUE;
291
+ }
292
+
293
+ export function buildNodeFromDataAndDefinition(data: any, definition: HydratedSimpleNativeProperty): DCNode {
294
+ const children = definition.children;
295
+ if (children) {
296
+ if (data === undefined || data === null) {
297
+ if (definition.type === 'array') {
298
+ return {rendered: true, condition: definition.condition, definition, validationError: "", children: []};
299
+ } else if (definition.type === 'object') {
300
+ if (Array.isArray(children)) {
301
+ return {
302
+ rendered: true, condition: definition.condition, definition, validationError: "",
303
+ children: Object.fromEntries(children.map(def => [def.name, buildNodeFromDataAndDefinition(undefined, def)]))
304
+ };
305
+ } else {
306
+ //TODO: We might need to throw an error here
307
+ return {rendered: true, condition: definition.condition, definition, validationError: "", children: {}};
308
+ }
309
+ }
310
+ } else if (typeof data === 'object') {
311
+ if (Array.isArray(data)) {
312
+ return {
313
+ rendered: true, condition: definition.condition, definition, validationError: "", children: data.map(item => {
314
+ const typedChildren = Array.isArray(children) ? children : children[item.type];
315
+ if (typedChildren === undefined) {
316
+ //TODO: We might need to throw an error here
317
+ return {children: [], rendered: true, validationError: ""};
318
+ }
319
+ return {
320
+ children: Object.fromEntries(typedChildren.map(def => [def.name, buildNodeFromDataAndDefinition(item[def.name], def)])),
321
+ rendered: true, validationError: ""
322
+ };
323
+ })
324
+ };
325
+ } else {
326
+ const typedChildren = Array.isArray(children) ? children : children[data.type];
327
+ if (typedChildren === undefined) {
328
+ //TODO: We might need to throw an error here
329
+ return {rendered: true, condition: definition.condition, definition, validationError: "", children: []};
330
+ }
331
+ return {
332
+ rendered: true, condition: definition.condition, definition, validationError: "",
333
+ children: Object.fromEntries(typedChildren.map(def => [def.name, buildNodeFromDataAndDefinition(data[def.name], def)]))
334
+ };
335
+ }
336
+ }
337
+ }
338
+ return {rendered: true, condition: definition.condition, definition, validationError: ""};
339
+ }