@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
@@ -1,208 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.walkToNodeInValue = void 0;
3
4
  exports.useDataController = useDataController;
4
- exports.walkToNodeInValue = walkToNodeInValue;
5
5
  const element_1 = require("@wordpress/element");
6
- const hooks_1 = require("@wordpress/hooks");
7
6
  const toolkit_1 = require("@reduxjs/toolkit");
8
- const immer_1 = require("immer");
9
7
  const conditions_1 = require("./conditions");
10
- const MoveError_1 = require("./MoveError");
11
8
  const PathError_1 = require("./PathError");
12
- const simple_native_property_internal_shared_1 = require("./simple-native-property-internal-shared");
9
+ const actions_1 = require("./data-controller/actions");
10
+ const reducer_1 = require("./data-controller/reducer");
11
+ const utils_1 = require("./data-controller/utils");
13
12
  function useDataController(blockClientId) {
14
13
  return (0, element_1.useMemo)(() => {
15
- const initialState = {
16
- treeRoot: { children: {}, rendered: true }, dataStores: { byProperty: {}, list: [] }, blockClientId, changedByManagedControl: false, rerenderTrigger: 0,
17
- batchAddedPropertiesThatNeedWriteThrough: []
18
- };
19
- const store = (0, toolkit_1.configureStore)({
20
- reducer(state = initialState, action) {
21
- switch (action.type) {
22
- case "addProperties":
23
- return (0, immer_1.produce)(state, (draft) => {
24
- draft.changedByManagedControl = true;
25
- if (!draft.dataStores.list.includes(action.dataStore)) {
26
- draft.dataStores.list.push(action.dataStore);
27
- }
28
- for (const property of action.properties) {
29
- if (Array.isArray(property)) {
30
- for (const prop of property) {
31
- if (action.dataStore.getValue(prop.name) === undefined && action.inBatch) {
32
- draft.batchAddedPropertiesThatNeedWriteThrough.push(prop);
33
- }
34
- action.dataStore.addProperty(prop, !action.inBatch);
35
- draft.dataStores.byProperty[prop.name] = action.dataStore;
36
- draft.treeRoot.children[prop.name] = buildNodeFromDataAndDefinition(action.dataStore.getValue(prop.name), prop);
37
- }
38
- }
39
- else {
40
- if (action.dataStore.getValue(property.name) === undefined && action.inBatch) {
41
- draft.batchAddedPropertiesThatNeedWriteThrough.push(property);
42
- }
43
- action.dataStore.addProperty(property, !action.inBatch);
44
- draft.dataStores.byProperty[property.name] = action.dataStore;
45
- draft.treeRoot.children[property.name] = buildNodeFromDataAndDefinition(action.dataStore.getValue(property.name), property);
46
- }
47
- }
48
- if (!action.inBatch) {
49
- performConditionChecks(draft, state, blockClientId);
50
- }
51
- });
52
- case "checkConditions":
53
- return (0, immer_1.produce)(state, (draft) => performConditionChecks(draft, state, blockClientId));
54
- case "validateNodes":
55
- return (0, immer_1.produce)(state, (draft) => {
56
- let hasChanged = false;
57
- applyToTree(draft.treeRoot, (node, path) => {
58
- if (!node.rendered) {
59
- return TreeMutatorResult.SKIP_DESCENDANTS;
60
- }
61
- if (validateFoundNode(node, path, draft)) {
62
- hasChanged = true;
63
- }
64
- return TreeMutatorResult.CONTINUE;
65
- });
66
- if (hasChanged) {
67
- draft.rerenderTrigger = state.rerenderTrigger + 1;
68
- }
69
- });
70
- case "markManagedControlChangeHandled":
71
- return (0, immer_1.produce)(state, (draft) => {
72
- draft.changedByManagedControl = false;
73
- });
74
- case "commitBatchAddedProperties":
75
- return (0, immer_1.produce)(state, (draft) => {
76
- draft.batchAddedPropertiesThatNeedWriteThrough = [];
77
- for (const batchAddedProperty of state.batchAddedPropertiesThatNeedWriteThrough) {
78
- const batchAddedPropertyState = state.treeRoot.children[batchAddedProperty.name];
79
- if (batchAddedPropertyState?.rendered) {
80
- writeValueToBackingDataStoreWithCorrections([batchAddedProperty.name], state, (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(batchAddedProperty));
81
- }
82
- }
83
- });
84
- }
85
- if (!('path' in action)) {
86
- return state; // This accounts for WordPress' meta actions
87
- }
88
- const { path, type } = action;
89
- switch (type) {
90
- case "addNode":
91
- const parentDefinition = resolveParentNodeDefinition(state.treeRoot, path);
92
- if (!parentDefinition) {
93
- throw new Error("Cannot add children to a node without a definition");
94
- }
95
- const nodeNameOrIndex = path[path.length - 1];
96
- let parentDefinitionChildren;
97
- if (Array.isArray(parentDefinition.children) || parentDefinition.children === undefined) {
98
- parentDefinitionChildren = parentDefinition.children;
99
- }
100
- else {
101
- const subtype = action.value?.type ?? action.subtype;
102
- if (!subtype || !parentDefinition.children[subtype]) {
103
- throw new PathError_1.PathError("Unable to locate the definition of the named node because its type could not be resolved.", path);
104
- }
105
- parentDefinitionChildren = parentDefinition.children[subtype];
106
- }
107
- if (typeof nodeNameOrIndex === 'string') {
108
- const definition = parentDefinitionChildren?.find(definition => definition.name === nodeNameOrIndex);
109
- if (definition === undefined) {
110
- throw new PathError_1.PathError("Unable to locate the definition of the named node.", path);
111
- }
112
- const data = populateValueBasedOnDefinition(action.value, parentDefinition);
113
- const builtNode = buildNodeFromDataAndDefinition(data, definition);
114
- return (0, immer_1.produce)(state, (draft) => {
115
- draft.changedByManagedControl = true;
116
- const parentNode = walkToNode(draft.treeRoot, path.slice(0, path.length - 1));
117
- if (parentNode.children === undefined) {
118
- parentNode.children = { [nodeNameOrIndex]: builtNode };
119
- }
120
- else {
121
- if (Array.isArray(parentNode.children)) {
122
- throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path);
123
- }
124
- parentNode.children[nodeNameOrIndex] = builtNode;
125
- }
126
- writeValueToBackingDataStoreWithCorrections(path, state, data);
127
- if (!action.inBatch) {
128
- performConditionChecks(draft, state, blockClientId);
129
- }
130
- });
131
- }
132
- else if (nodeNameOrIndex === undefined) {
133
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
134
- }
135
- else {
136
- const data = populateValueBasedOnDefinition(action.value, parentDefinition);
137
- const builtNode = buildNodeFromDataAndDefinition(data, parentDefinition);
138
- return (0, immer_1.produce)(state, (draft) => {
139
- draft.changedByManagedControl = true;
140
- const parentNode = walkToNode(draft.treeRoot, path.slice(0, path.length - 1));
141
- if (parentNode.children === undefined) {
142
- parentNode.children = [];
143
- parentNode.children[nodeNameOrIndex] = builtNode;
144
- }
145
- else {
146
- if (!Array.isArray(parentNode.children)) {
147
- throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path);
148
- }
149
- parentNode.children[nodeNameOrIndex] = builtNode;
150
- }
151
- writeValueToBackingDataStoreWithCorrections(path, state, data);
152
- if (!action.inBatch) {
153
- performConditionChecks(draft, state, blockClientId);
154
- }
155
- });
156
- }
157
- case "removeNode":
158
- return (0, immer_1.produce)(state, (draft) => {
159
- draft.changedByManagedControl = true;
160
- const parentNode = walkToNode(draft.treeRoot, path.slice(0, path.length - 1));
161
- removeFromParentNode(parentNode.children, path);
162
- removeValueFromBackingDataStore(getDataStore(path[0], state), path);
163
- if (!action.inBatch) {
164
- performConditionChecks(draft, state, blockClientId);
165
- }
166
- });
167
- case "moveNode":
168
- // If from and to are the same, then this is a no-op, so we can just skip it
169
- if (action.from === action.to) {
170
- return state;
171
- }
172
- return (0, immer_1.produce)(state, (draft) => {
173
- draft.changedByManagedControl = true;
174
- moveNodeWithinParent(walkToNode(draft.treeRoot, path).children, path, action);
175
- const dataStore = getDataStore(path[0], state);
176
- dataStore.setValue(path[0], (0, immer_1.produce)(dataStore.getValue(path[0]), (dsDraft) => {
177
- moveNodeWithinParent(walkToNodeInValue(dsDraft, path), path, action);
178
- }));
179
- if (!action.inBatch) {
180
- performConditionChecks(draft, state, blockClientId);
181
- }
182
- });
183
- case "validateNode":
184
- return (0, immer_1.produce)(state, (draft) => validateNode(path, draft));
185
- case "writeValue":
186
- writeValueToBackingDataStoreWithCorrections(path, state, action.value);
187
- if (!action.inBatch) {
188
- return (0, immer_1.produce)(state, (draft) => {
189
- draft.changedByManagedControl = true;
190
- validateNode(path, draft);
191
- performConditionChecks(draft, state, blockClientId);
192
- });
193
- }
194
- return state;
195
- }
196
- return state; // This accounts for WordPress' meta actions
197
- }
198
- });
14
+ const reducer = (0, reducer_1.buildReducer)(blockClientId);
15
+ const store = (0, toolkit_1.configureStore)({ reducer });
199
16
  function getDataStoreImpl(property, required) {
200
- return required ? getDataStore(property, store.getState(), true) : getDataStore(property, store.getState(), false);
17
+ return required ? (0, utils_1.getDataStore)(property, store.getState(), true) : (0, utils_1.getDataStore)(property, store.getState(), false);
201
18
  }
202
19
  const dataController = {
203
- dispatch(action) {
204
- return store.dispatch(action);
205
- },
206
20
  getDataStore: getDataStoreImpl,
207
21
  getAllDataStores() {
208
22
  return store.getState().dataStores.list;
@@ -211,33 +25,36 @@ function useDataController(blockClientId) {
211
25
  return propertyPath => (0, conditions_1.resolveValueForCondition)(propertyPath, contextPath, blockClientId, getDataStoreImpl);
212
26
  },
213
27
  getValue(path) {
214
- return getOptionalValue(path, store.getState());
28
+ return (0, utils_1.getOptionalValue)(path, store.getState());
215
29
  },
216
30
  setValue(path, value) {
217
- store.dispatch({ type: "writeValue", path, value });
31
+ store.dispatch(actions_1.actions.node.write({ path, value }));
218
32
  },
219
33
  addProperties(properties, dataStore, inBatch) {
220
- store.dispatch({ type: "addProperties", properties, dataStore, inBatch });
34
+ store.dispatch(actions_1.actions.addProperties({ properties, dataStore }, inBatch));
221
35
  },
222
36
  addNode(path, value) {
223
37
  try {
224
- store.dispatch({ type: "addNode", path, value });
38
+ store.dispatch(actions_1.actions.node.add({ path, value }));
225
39
  }
226
40
  catch (e) {
227
41
  if (e instanceof PathError_1.PathError && e.errorType === PathError_1.PathErrorType.UNEXPECTED_LEAF) {
228
42
  for (let i = e.errorIndex; i < path.length - 1; i++) {
229
- store.dispatch({ type: "addNode", path: path.slice(0, i), inBatch: true });
43
+ store.dispatch(actions_1.actions.node.add({ path: path.slice(0, i), value }, true));
230
44
  }
231
- store.dispatch({ type: "addNode", path, value });
45
+ store.dispatch(actions_1.actions.node.add({ path, value }));
232
46
  }
233
47
  throw e;
234
48
  }
235
49
  },
236
50
  removeNode(path) {
237
- store.dispatch({ type: "removeNode", path });
51
+ store.dispatch(actions_1.actions.node.remove({ path }));
238
52
  },
239
53
  moveNode(path, indices) {
240
- store.dispatch({ type: "moveNode", path, ...indices });
54
+ store.dispatch(actions_1.actions.node.move({ path, ...indices }));
55
+ },
56
+ validateNode(path) {
57
+ store.dispatch(actions_1.actions.node.validate({ path }));
241
58
  },
242
59
  hasRenderedNode(properties) {
243
60
  const rootNodes = store.getState().treeRoot.children;
@@ -275,36 +92,34 @@ function useDataController(blockClientId) {
275
92
  return false;
276
93
  },
277
94
  getIsRendered(path) {
278
- return walkToNode(store.getState().treeRoot, path).rendered ?? true;
95
+ return (0, utils_1.walkToNode)(store.getState().treeRoot, path).rendered ?? true;
279
96
  },
280
97
  getNode(path) {
281
- return walkToNode(store.getState().treeRoot, path);
98
+ return (0, utils_1.walkToNode)(store.getState().treeRoot, path);
282
99
  },
283
100
  getValidationError(path) {
284
- return walkToNode(store.getState().treeRoot, path).validationError;
101
+ return (0, utils_1.walkToNode)(store.getState().treeRoot, path).validationError;
285
102
  },
286
103
  getWasChangedByManagedControl() {
287
104
  return store.getState().changedByManagedControl;
288
105
  },
289
106
  markManagedControlChangeHandled() {
290
- store.dispatch({ type: "markManagedControlChangeHandled" });
107
+ store.dispatch(actions_1.actions.markManagedControlChangeHandled({}));
108
+ },
109
+ checkConditions() {
110
+ store.dispatch(actions_1.actions.checkConditions({}));
111
+ },
112
+ validateNodes() {
113
+ store.dispatch(actions_1.actions.validateNodes({}));
114
+ },
115
+ commitBatchAddedProperties() {
116
+ store.dispatch(actions_1.actions.commitBatchAddedProperties({}));
291
117
  },
292
118
  blockClientId
293
119
  };
294
120
  return dataController;
295
121
  }, [blockClientId]);
296
122
  }
297
- function getOptionalValue(path, state) {
298
- try {
299
- return walkToNodeInValue(getDataStore(path[0], state).getValue(path[0]), path);
300
- }
301
- catch (e) {
302
- if (e instanceof PathError_1.PathError) {
303
- return undefined;
304
- }
305
- throw e;
306
- }
307
- }
308
123
  function testForValidationErrors(node) {
309
124
  if (!node || !node.rendered) {
310
125
  return false;
@@ -328,392 +143,6 @@ function testForValidationErrors(node) {
328
143
  }
329
144
  return false;
330
145
  }
331
- function validateNode(path, draft) {
332
- if (validateFoundNode(walkToNode(draft.treeRoot, path), path, draft)) {
333
- draft.rerenderTrigger = draft.rerenderTrigger + 1;
334
- }
335
- }
336
- function removeFromParentNode(parentNode, path) {
337
- if (typeof parentNode !== 'object' || parentNode === undefined || parentNode === null) {
338
- throw new PathError_1.PathError("Encountered a non-existent node while expecting one.", path);
339
- }
340
- const nodeName = path[path.length - 1];
341
- if (typeof nodeName === 'string') {
342
- if (Array.isArray(parentNode)) {
343
- throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, path.length - 1);
344
- }
345
- delete parentNode[nodeName];
346
- }
347
- else if (nodeName === undefined) {
348
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
349
- }
350
- else {
351
- if (!Array.isArray(parentNode)) {
352
- throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, path.length - 1);
353
- }
354
- parentNode.splice(nodeName, 1);
355
- }
356
- }
357
- function moveNodeWithinParent(parentNode, path, move) {
358
- if (typeof parentNode !== 'object' || parentNode === undefined || parentNode === null) {
359
- throw new PathError_1.PathError("Encountered a non-existent node while expecting one.", path);
360
- }
361
- if (isNumericMove(move)) {
362
- if (!Array.isArray(parentNode)) {
363
- throw new MoveError_1.MoveError("Attempted to move a child of a node with string-keyed descendants by index", path, move);
364
- }
365
- if (move.from > move.to) {
366
- parentNode.splice(move.to, 0, ...parentNode.splice(move.from, 1));
367
- }
368
- else { //We know that
369
- parentNode.splice(move.to - 1, 0, ...parentNode.splice(move.from, 1));
370
- }
371
- }
372
- else {
373
- if (Array.isArray(parentNode)) {
374
- throw new MoveError_1.MoveError("Attempted to move a child of a node with indexed descendants by string-key", path, move);
375
- }
376
- if (parentNode[move.to] !== undefined) {
377
- throw new MoveError_1.MoveError("Attempted to move a string-keyed child to the same key as another", path, move);
378
- }
379
- }
380
- }
381
- function isNumericMove(move) {
382
- return typeof move.from === 'number' && typeof move.to === 'number';
383
- }
384
- function resolveParentNodeDefinition(root, path) {
385
- if (path.length < 2) {
386
- throw new PathError_1.PathError("Unable to get the parent node of a path with less than two nodes", path, path.length - 1, PathError_1.PathErrorType.INSUFFICIENT_LENGTH);
387
- }
388
- return walkToNode(root, path.slice(0, path.length - (typeof path[path.length - 2] === 'number' ? 2 : 1))).definition;
389
- }
390
- function populateValueBasedOnDefinition(value, definition) {
391
- const typedChildren = Array.isArray(definition?.children) ? definition.children : definition?.children?.[value?.type];
392
- if (!typedChildren?.length) {
393
- return value ?? (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(definition);
394
- }
395
- else if (value === null || value === undefined) {
396
- return (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(definition);
397
- }
398
- else if (typeof value !== 'object') {
399
- return value;
400
- }
401
- else if (Array.isArray(value)) {
402
- return value;
403
- }
404
- return { ...value, ...Object.fromEntries(typedChildren.map(def => [def.name, populateValueBasedOnDefinition(value[def.name], def)])) };
405
- }
406
- function walkToNode(root, path) {
407
- let current = root;
408
- for (let i = 0; i < path.length; i++) {
409
- current = descendIntoDCNodeChild(current, path, i);
410
- }
411
- return current;
412
- }
413
- function descendIntoDCNodeChild(current, path, i) {
414
- if (current?.children === undefined) {
415
- throw new PathError_1.PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
416
- }
417
- const pathNode = path[i];
418
- if (typeof pathNode === 'string') {
419
- if (Array.isArray(current.children)) {
420
- throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, i);
421
- }
422
- current = current.children[pathNode];
423
- }
424
- else if (pathNode === undefined) {
425
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path, i);
426
- }
427
- else {
428
- if (!Array.isArray(current.children)) {
429
- throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, i);
430
- }
431
- current = current.children[pathNode];
432
- }
433
- if (current === undefined) {
434
- throw new PathError_1.PathError("Encountered a non-existent node while expecting one.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
435
- }
436
- return current;
437
- }
438
- function performConditionChecks(draft, state, blockClientId) {
439
- const propertyValueResolverBuilder = path => propertyPath => (0, conditions_1.resolveValueForCondition)(propertyPath, path, blockClientId, property => getDataStore(property, draft, false));
440
- applyToTree(draft.treeRoot, (node, path) => {
441
- const condition = node.condition;
442
- if (condition === undefined) {
443
- node.rendered = true;
444
- return TreeMutatorResult.CONTINUE;
445
- }
446
- else {
447
- const newRendered = (0, conditions_1.testCondition)(condition, propertyValueResolverBuilder(path));
448
- if (newRendered !== node.rendered) {
449
- const dataStore = getDataStore(path[0], state);
450
- let currentValue;
451
- try {
452
- currentValue = walkToNodeInValue(dataStore.getValue(path[0]), path);
453
- }
454
- catch (e) {
455
- if (!(e instanceof PathError_1.PathError)) {
456
- throw e;
457
- }
458
- currentValue = undefined;
459
- }
460
- if (!newRendered) {
461
- // Create a backup if we are transitioning from rendered -> unrendered
462
- node.backup = currentValue;
463
- removeValueFromBackingDataStore(dataStore, path);
464
- }
465
- else if (currentValue === undefined) {
466
- // Restore from backup if we are transitioning from unrendered -> rendered
467
- const stateNode = walkToNode(state.treeRoot, path);
468
- writeValueToBackingDataStore(dataStore, path, stateNode.backup ?? (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(stateNode.definition));
469
- }
470
- node.rendered = newRendered;
471
- }
472
- return node.rendered ? TreeMutatorResult.CONTINUE : TreeMutatorResult.SKIP_DESCENDANTS;
473
- }
474
- });
475
- }
476
- /**
477
- * @return true iff the validation state changed
478
- */
479
- function validateFoundNode(node, path, draft) {
480
- const definition = node.definition;
481
- if (definition) {
482
- let validationError;
483
- const value = getOptionalValue(path, draft);
484
- if (definition.required && !value) {
485
- validationError = `"${definition.label || definition.name}" is required.`;
486
- }
487
- else {
488
- validationError = definition.validator?.(value) ?? "";
489
- }
490
- if (node.validationError !== validationError) {
491
- node.validationError = validationError;
492
- return true;
493
- }
494
- }
495
- return false;
496
- }
497
- function getDataStore(property, state, throwOnError = true) {
498
- let dataStore = state.dataStores.byProperty[property];
499
- if (dataStore) {
500
- return dataStore;
501
- }
502
- dataStore = state.dataStores.list.find(dataStore => dataStore.handlesProperty(property));
503
- if (dataStore) {
504
- 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.`);
505
- }
506
- else if (throwOnError) {
507
- throw new Error(`Unable to resolve the dataStore for ${property} on block ${state.blockClientId}`);
508
- }
509
- return dataStore;
510
- }
511
- function removeValueFromBackingDataStore(dataStore, path) {
512
- const dsValue = dataStore.getValue(path[0]);
513
- if (path.length === 1) {
514
- dataStore.setValue(path[0], undefined);
515
- }
516
- else {
517
- dataStore.setValue(path[0], (0, immer_1.produce)(dsValue, (dsValueDraft) => {
518
- const nodeParent = walkToNodeInValue(dsValueDraft, path.slice(0, path.length - 1));
519
- const nodeName = path[path.length - 1];
520
- if (typeof nodeName === 'string') {
521
- delete nodeParent[nodeName];
522
- }
523
- else if (nodeName === undefined) {
524
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
525
- }
526
- else {
527
- nodeParent.splice(nodeName, 1);
528
- }
529
- }));
530
- }
531
- }
532
- function writeValueToBackingDataStoreWithCorrections(path, state, value) {
533
- if (typeof value === 'number' && Number.isNaN(value)) {
534
- value = undefined; // This ensures that NaN's don't get stored
535
- }
536
- const rootDCNode = state.treeRoot.children[path[0]];
537
- if (rootDCNode === undefined) {
538
- throw new PathError_1.PathError("Encountered a path pointing to a non-existent root node.", path, 0);
539
- }
540
- const dataStore = getDataStore(path[0], state);
541
- // This path both handles scalar values and ensures that the root "value" of Groups and Lists are initialized before writing the actual attribute
542
- // 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
543
- if (path.length === 1 || (value === undefined && dataStore.getValue(path[0]) === undefined)) {
544
- const writtenValue = value ?? (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(rootDCNode.definition, path.length === 1);
545
- dataStore.setValue(path[0], writtenValue);
546
- if (rootDCNode.definition) {
547
- (0, hooks_1.doAction)('plaudit.gutenbergApiExtensions.simpleNativeProperties.afterWrite', writtenValue, rootDCNode.definition, dataStore);
548
- }
549
- if (path.length === 1) {
550
- return;
551
- }
552
- }
553
- let closestUsableDefinition = rootDCNode.definition;
554
- let lastWrittenValue;
555
- dataStore.setValue(path[0], (0, immer_1.produce)(dataStore.getValue(path[0]), (dsValueDraft) => {
556
- let currentDSValue = dsValueDraft;
557
- let currentDCNode = rootDCNode;
558
- for (let i = 1; i < path.length - 1; i++) {
559
- const nodeName = path[i];
560
- currentDCNode = descendIntoDCNodeChild(currentDCNode, path, i);
561
- if (typeof nodeName === 'string') {
562
- //We only update the definition being used when we descend into a new node proper
563
- closestUsableDefinition = currentDCNode.definition;
564
- }
565
- else if (nodeName === undefined) {
566
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path, i);
567
- }
568
- let nextDSValue = descendIntoDSValueNodeChild(currentDSValue, path, i);
569
- if (nextDSValue === undefined) {
570
- lastWrittenValue = currentDSValue[nodeName] = (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(closestUsableDefinition);
571
- currentDSValue = descendIntoDSValueNodeChild(currentDSValue, path, i);
572
- if (currentDSValue === undefined) {
573
- throw new PathError_1.PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
574
- }
575
- }
576
- else {
577
- currentDSValue = nextDSValue;
578
- }
579
- }
580
- const nodeName = path[path.length - 1];
581
- if (nodeName === undefined) {
582
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
583
- }
584
- lastWrittenValue = currentDSValue[nodeName] = value;
585
- }));
586
- if (closestUsableDefinition) {
587
- (0, hooks_1.doAction)('plaudit.gutenbergApiExtensions.simpleNativeProperties.afterWrite', lastWrittenValue, closestUsableDefinition, dataStore);
588
- }
589
- }
590
- function writeValueToBackingDataStore(dataStore, path, value) {
591
- if (typeof value === 'number' && Number.isNaN(value)) {
592
- value = undefined; // This ensures that NaN's don't get stored
593
- }
594
- const dsValue = dataStore.getValue(path[0]);
595
- if (path.length === 1) {
596
- dataStore.setValue(path[0], value);
597
- }
598
- else {
599
- dataStore.setValue(path[0], (0, immer_1.produce)(dsValue, (dsValueDraft) => {
600
- const nodeParent = walkToNodeInValue(dsValueDraft, path.slice(0, path.length - 1));
601
- const nodeName = path[path.length - 1];
602
- if (nodeName === undefined) {
603
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
604
- }
605
- nodeParent[nodeName] = value;
606
- }));
607
- }
608
- }
609
- /**
610
- * @param dsValue a value from a {@link DataStore}
611
- * @param path this is expected to be the entire path (including the node that was used to get {@link dsValue}
612
- */
613
- function walkToNodeInValue(dsValue, path) {
614
- let current = dsValue;
615
- for (let i = 1; i < path.length; i++) {
616
- if (current === undefined) {
617
- throw new PathError_1.PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
618
- }
619
- current = descendIntoDSValueNodeChild(current, path, i);
620
- }
621
- return current;
622
- }
623
- function descendIntoDSValueNodeChild(current, path, i) {
624
- const pathNode = path[i];
625
- if (typeof pathNode === 'string') {
626
- if (Array.isArray(current)) {
627
- throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, i);
628
- }
629
- current = current[pathNode];
630
- }
631
- else if (pathNode === undefined) {
632
- throw new PathError_1.PathError("Encountered an undefined name in a path.", path, i);
633
- }
634
- else {
635
- if (!Array.isArray(current)) {
636
- throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, i);
637
- }
638
- current = current[pathNode];
639
- }
640
- return current;
641
- }
642
- var TreeMutatorResult;
643
- (function (TreeMutatorResult) {
644
- TreeMutatorResult[TreeMutatorResult["STOP"] = 0] = "STOP";
645
- TreeMutatorResult[TreeMutatorResult["CONTINUE"] = 1] = "CONTINUE";
646
- TreeMutatorResult[TreeMutatorResult["SKIP_DESCENDANTS"] = 2] = "SKIP_DESCENDANTS";
647
- })(TreeMutatorResult || (TreeMutatorResult = {}));
648
- function applyToTree(tree, mutator, path = []) {
649
- if (tree.children === undefined) {
650
- return TreeMutatorResult.CONTINUE;
651
- }
652
- const steps = Array.isArray(tree.children)
653
- ? tree.children.map((node, index) => [path.toSpliced(path.length, 0, index), node])
654
- : Object.entries(tree.children).map(([name, node]) => [path.toSpliced(path.length, 0, name), node]);
655
- for (const [nodePath, node] of steps) {
656
- const mutatorRes = mutator(node, nodePath);
657
- if (!mutatorRes) {
658
- return TreeMutatorResult.STOP;
659
- }
660
- else if (mutatorRes === TreeMutatorResult.CONTINUE) {
661
- if (!applyToTree(node, mutator, nodePath)) {
662
- return TreeMutatorResult.STOP;
663
- }
664
- }
665
- }
666
- return TreeMutatorResult.CONTINUE;
667
- }
668
- function buildNodeFromDataAndDefinition(data, definition) {
669
- const children = definition.children;
670
- if (children) {
671
- if (data === undefined || data === null) {
672
- if (definition.type === 'array') {
673
- return { rendered: true, condition: definition.condition, definition, validationError: "", children: [] };
674
- }
675
- else if (definition.type === 'object') {
676
- if (Array.isArray(children)) {
677
- return {
678
- rendered: true, condition: definition.condition, definition, validationError: "",
679
- children: Object.fromEntries(children.map(def => [def.name, buildNodeFromDataAndDefinition(undefined, def)]))
680
- };
681
- }
682
- else {
683
- //TODO: We might need to throw an error here
684
- return { rendered: true, condition: definition.condition, definition, validationError: "", children: {} };
685
- }
686
- }
687
- }
688
- else if (typeof data === 'object') {
689
- if (Array.isArray(data)) {
690
- return {
691
- rendered: true, condition: definition.condition, definition, validationError: "", children: data.map(item => {
692
- const typedChildren = Array.isArray(children) ? children : children[item.type];
693
- if (typedChildren === undefined) {
694
- //TODO: We might need to throw an error here
695
- return { children: [], rendered: true, validationError: "" };
696
- }
697
- return {
698
- children: Object.fromEntries(typedChildren.map(def => [def.name, buildNodeFromDataAndDefinition(item[def.name], def)])),
699
- rendered: true, validationError: ""
700
- };
701
- })
702
- };
703
- }
704
- else {
705
- const typedChildren = Array.isArray(children) ? children : children[data.type];
706
- if (typedChildren === undefined) {
707
- //TODO: We might need to throw an error here
708
- return { rendered: true, condition: definition.condition, definition, validationError: "", children: [] };
709
- }
710
- return {
711
- rendered: true, condition: definition.condition, definition, validationError: "",
712
- children: Object.fromEntries(typedChildren.map(def => [def.name, buildNodeFromDataAndDefinition(data[def.name], def)]))
713
- };
714
- }
715
- }
716
- }
717
- return { rendered: true, condition: definition.condition, definition, validationError: "" };
718
- }
146
+ var utils_2 = require("./data-controller/utils");
147
+ Object.defineProperty(exports, "walkToNodeInValue", { enumerable: true, get: function () { return utils_2.walkToNodeInValue; } });
719
148
  //# sourceMappingURL=data-controller.js.map