@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,140 @@
1
+ import { ActionReducerMapBuilder } from "@reduxjs/toolkit";
2
+ import type { Draft } from "immer";
3
+ import type { NodePath } from "../simple-native-property-api";
4
+ import type { DCNode, DCStoreState } from "../data-controller";
5
+ export declare const triggerHandlers: {
6
+ readonly validation: {
7
+ addAction: (action: import("@reduxjs/toolkit").ActionCreatorWithPreparedPayload<any[], {
8
+ path: NodePath;
9
+ }, string, never, {
10
+ inBatch?: boolean;
11
+ }>) => number;
12
+ isInstance: (action: any) => action is {
13
+ payload: {
14
+ path: NodePath;
15
+ };
16
+ type: string;
17
+ } & {
18
+ meta: {
19
+ inBatch?: boolean;
20
+ };
21
+ } & {
22
+ meta: {
23
+ inBatch?: boolean;
24
+ };
25
+ };
26
+ shouldTrigger: (action: any) => action is {
27
+ payload: {
28
+ path: NodePath;
29
+ };
30
+ type: string;
31
+ } & {
32
+ meta: {
33
+ inBatch?: boolean;
34
+ };
35
+ } & {
36
+ meta: {
37
+ inBatch?: boolean;
38
+ };
39
+ };
40
+ attachTo: (builder: ActionReducerMapBuilder<DCStoreState>) => Omit<ActionReducerMapBuilder<DCStoreState>, "addCase" | "addAsyncThunk">;
41
+ };
42
+ readonly conditionChecks: {
43
+ addAction: (action: import("@reduxjs/toolkit").ActionCreatorWithPreparedPayload<any[], {}, string, never, {
44
+ inBatch?: boolean;
45
+ }>) => number;
46
+ isInstance: (action: any) => action is {
47
+ payload: {};
48
+ type: string;
49
+ } & {
50
+ meta: {
51
+ inBatch?: boolean;
52
+ };
53
+ } & {
54
+ meta: {
55
+ inBatch?: boolean;
56
+ };
57
+ };
58
+ shouldTrigger: (action: any) => action is {
59
+ payload: {};
60
+ type: string;
61
+ } & {
62
+ meta: {
63
+ inBatch?: boolean;
64
+ };
65
+ } & {
66
+ meta: {
67
+ inBatch?: boolean;
68
+ };
69
+ };
70
+ attachTo: (builder: ActionReducerMapBuilder<DCStoreState>) => Omit<ActionReducerMapBuilder<DCStoreState>, "addCase" | "addAsyncThunk">;
71
+ };
72
+ readonly changedByManagedControl: {
73
+ addAction: (action: import("@reduxjs/toolkit").ActionCreatorWithPreparedPayload<any[], {}, string, never, {
74
+ inBatch?: boolean;
75
+ }>) => number;
76
+ isInstance: (action: any) => action is {
77
+ payload: {};
78
+ type: string;
79
+ } & {
80
+ meta: {
81
+ inBatch?: boolean;
82
+ };
83
+ } & {
84
+ meta: {
85
+ inBatch?: boolean;
86
+ };
87
+ };
88
+ shouldTrigger: (action: any) => action is {
89
+ payload: {};
90
+ type: string;
91
+ } & {
92
+ meta: {
93
+ inBatch?: boolean;
94
+ };
95
+ } & {
96
+ meta: {
97
+ inBatch?: boolean;
98
+ };
99
+ };
100
+ attachTo: (builder: ActionReducerMapBuilder<DCStoreState>) => Omit<ActionReducerMapBuilder<DCStoreState>, "addCase" | "addAsyncThunk">;
101
+ };
102
+ };
103
+ type CommonTriggers = {
104
+ changedByManagedControl?: boolean;
105
+ conditionChecks?: boolean;
106
+ };
107
+ type BaseActionMeta<ADDITIONAL_TRIGGERS extends {
108
+ [K in Exclude<keyof typeof triggerHandlers, keyof CommonTriggers>]?: boolean;
109
+ }> = {
110
+ inBatch?: boolean;
111
+ triggers: CommonTriggers & ADDITIONAL_TRIGGERS;
112
+ };
113
+ export type GlobalActionMeta = BaseActionMeta<{}>;
114
+ export declare function createGlobalAction<P extends {
115
+ [key: Exclude<string, 'type' | 'inBatch'>]: any;
116
+ } = {}, T extends string = string>(type: T, triggers?: GlobalActionMeta['triggers']): import("@reduxjs/toolkit").ActionCreatorWithPreparedPayload<[payload: P, inBatch?: boolean | undefined], P, T, never, {
117
+ inBatch: boolean;
118
+ triggers: CommonTriggers;
119
+ }>;
120
+ export type PathedActionMeta = BaseActionMeta<{
121
+ validation?: boolean;
122
+ }>;
123
+ export declare function createPathedAction<P extends {
124
+ [key: Exclude<string, 'type' | 'path' | 'inBatch'>]: any;
125
+ } = {}, T extends string = string>(type: T, triggers?: PathedActionMeta['triggers']): import("@reduxjs/toolkit").ActionCreatorWithPreparedPayload<[payload: P & {
126
+ path: NodePath;
127
+ }, inBatch?: boolean | undefined], P & {
128
+ path: NodePath;
129
+ }, T, never, {
130
+ inBatch: boolean;
131
+ triggers: CommonTriggers & {
132
+ validation?: boolean;
133
+ };
134
+ }>;
135
+ export declare function validateNode(path: NodePath, draft: Draft<DCStoreState>): void;
136
+ /**
137
+ * @return true iff the validation state changed
138
+ */
139
+ export declare function validateFoundNode(node: DCNode, path: NodePath, draft: Draft<DCStoreState>): boolean;
140
+ export {};
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.triggerHandlers = void 0;
4
+ exports.createGlobalAction = createGlobalAction;
5
+ exports.createPathedAction = createPathedAction;
6
+ exports.validateNode = validateNode;
7
+ exports.validateFoundNode = validateFoundNode;
8
+ const toolkit_1 = require("@reduxjs/toolkit");
9
+ const PathError_1 = require("../PathError");
10
+ const simple_native_property_internal_shared_1 = require("../simple-native-property-internal-shared");
11
+ const conditions_1 = require("../conditions");
12
+ const utils_1 = require("./utils");
13
+ function makeTriggerHandler(reducer) {
14
+ const triggeringActionCreators = [];
15
+ const isInstance = (action) => {
16
+ return triggeringActionCreators.some(actionCreator => actionCreator.match(action));
17
+ };
18
+ const shouldTrigger = (action) => isInstance(action) && (action.meta.inBatch !== true);
19
+ return {
20
+ addAction: (action) => triggeringActionCreators.push(action), isInstance, shouldTrigger,
21
+ attachTo: (builder) => builder.addMatcher(shouldTrigger, reducer)
22
+ };
23
+ }
24
+ exports.triggerHandlers = {
25
+ validation: makeTriggerHandler((state, { payload: { path } }) => {
26
+ validateNode(path, state);
27
+ return state;
28
+ }),
29
+ conditionChecks: makeTriggerHandler(performConditionChecks),
30
+ changedByManagedControl: makeTriggerHandler(state => {
31
+ state.changedByManagedControl = true;
32
+ }),
33
+ };
34
+ function createGlobalAction(type, triggers = {}) {
35
+ const res = (0, toolkit_1.createAction)(type, (payload, inBatch = false) => ({ payload, meta: { inBatch, triggers } }));
36
+ for (const [trigger, enabled] of Object.entries(triggers)) {
37
+ if (enabled) {
38
+ exports.triggerHandlers[trigger].addAction(res);
39
+ }
40
+ }
41
+ return res;
42
+ }
43
+ function createPathedAction(type, triggers = {}) {
44
+ const res = (0, toolkit_1.createAction)(type, (payload, inBatch = false) => ({ payload, meta: { inBatch, triggers } }));
45
+ for (const [trigger, enabled] of Object.entries(triggers)) {
46
+ if (enabled) {
47
+ exports.triggerHandlers[trigger].addAction(res);
48
+ }
49
+ }
50
+ return res;
51
+ }
52
+ function validateNode(path, draft) {
53
+ if (validateFoundNode((0, utils_1.walkToNode)(draft.treeRoot, path), path, draft)) {
54
+ draft.rerenderTrigger = draft.rerenderTrigger + 1;
55
+ }
56
+ }
57
+ /**
58
+ * @return true iff the validation state changed
59
+ */
60
+ function validateFoundNode(node, path, draft) {
61
+ const definition = node.definition;
62
+ if (definition) {
63
+ let validationError;
64
+ const value = (0, utils_1.getOptionalValue)(path, draft);
65
+ if (definition.required && !value) {
66
+ validationError = `"${definition.label || definition.name}" is required.`;
67
+ }
68
+ else {
69
+ validationError = definition.validator?.(value) ?? "";
70
+ }
71
+ if (node.validationError !== validationError) {
72
+ node.validationError = validationError;
73
+ return true;
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+ function performConditionChecks(state) {
79
+ const blockClientId = state.blockClientId;
80
+ const propertyValueResolverBuilder = path => propertyPath => (0, conditions_1.resolveValueForCondition)(propertyPath, path, blockClientId, property => (0, utils_1.getDataStore)(property, state, false));
81
+ (0, utils_1.applyToTree)(state.treeRoot, (node, path) => {
82
+ const condition = node.condition;
83
+ if (condition === undefined) {
84
+ node.rendered = true;
85
+ return utils_1.TreeMutatorResult.CONTINUE;
86
+ }
87
+ else {
88
+ const newRendered = (0, conditions_1.testCondition)(condition, propertyValueResolverBuilder(path));
89
+ if (newRendered !== node.rendered) {
90
+ const dataStore = (0, utils_1.getDataStore)(path[0], state);
91
+ let currentValue;
92
+ try {
93
+ currentValue = (0, utils_1.walkToNodeInValue)(dataStore.getValue(path[0]), path);
94
+ }
95
+ catch (e) {
96
+ if (!(e instanceof PathError_1.PathError)) {
97
+ throw e;
98
+ }
99
+ currentValue = undefined;
100
+ }
101
+ if (!newRendered) {
102
+ // Create a backup if we are transitioning from rendered -> unrendered
103
+ node.backup = currentValue;
104
+ (0, utils_1.removeValueFromBackingDataStore)(dataStore, path);
105
+ }
106
+ else if (currentValue === undefined) {
107
+ // Restore from backup if we are transitioning from unrendered -> rendered
108
+ const stateNode = (0, utils_1.walkToNode)(state.treeRoot, path);
109
+ (0, utils_1.writeValueToBackingDataStore)(dataStore, path, stateNode.backup ?? (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(stateNode.definition));
110
+ }
111
+ node.rendered = newRendered;
112
+ }
113
+ return node.rendered ? utils_1.TreeMutatorResult.CONTINUE : utils_1.TreeMutatorResult.SKIP_DESCENDANTS;
114
+ }
115
+ });
116
+ }
117
+ //# sourceMappingURL=trigger-handlers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trigger-handlers.js","sourceRoot":"","sources":["../../../src/blocks/data-controller/trigger-handlers.ts"],"names":[],"mappings":";;;AAmDA,gDAQC;AAGD,gDAQC;AAED,oCAIC;AAKD,8CAgBC;AAjGD,8CAAyH;AAIzH,4CAAuC;AAEvC,sGAA0F;AAC1F,8CAAsE;AACtE,mCASiB;AAKjB,SAAS,kBAAkB,CAAiE,OAAiC;IAG5H,MAAM,wBAAwB,GAA8B,EAAE,CAAC;IAC/D,MAAM,UAAU,GAAG,CAAC,MAAW,EAAwB,EAAE;QACxD,OAAO,wBAAwB,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACpF,CAAC,CAAC;IACF,MAAM,aAAa,GAAG,CAAC,MAAW,EAAwB,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC;IAClH,OAAO;QACN,SAAS,EAAE,CAAC,MAA+B,EAAE,EAAE,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,aAAa;QAChH,QAAQ,EAAG,CAAC,OAAuC,EAAE,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,aAAa,EAAE,OAAO,CAAC;KAClG,CAAA;AACF,CAAC;AAEY,QAAA,eAAe,GAAG;IAC9B,UAAU,EAAE,kBAAkB,CAAiC,CAAC,KAAK,EAAE,EAAC,OAAO,EAAE,EAAC,IAAI,EAAC,EAAC,EAAE,EAAE;QAC3F,YAAY,CAAC,IAAI,EAAE,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AC;oBACnD,IAAA,oCAA4B,EAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,MAAM,IAAI,IAAA,wEAA+B,EAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;gBAC1H,CAAC;gBACD,IAAI,CAAC,QAAQ,GAAG,WAAW,CAAC;YAC7B,CAAC;YACD,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,yBAAiB,CAAC,QAAQ,CAAC,CAAC,CAAC,yBAAiB,CAAC,gBAAgB,CAAC;QACxF,CAAC;IACF,CAAC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { Draft } from "immer";
2
+ import type { DCNode, DCStoreState } from "../data-controller";
3
+ import type { DataStore, HydratedSimpleNativeProperty, NodePath, RawPath } from "../simple-native-property-api";
4
+ export declare function removeFromParentNode(parentNode: any | undefined, path: RawPath): void;
5
+ export declare function moveNodeWithinParent(parentNode: any | undefined | null, path: RawPath, move: {
6
+ from: number;
7
+ to: number;
8
+ } | {
9
+ from: string;
10
+ to: string;
11
+ }): void;
12
+ export declare function isNumericMove(move: {
13
+ from: number;
14
+ to: number;
15
+ } | {
16
+ from: string;
17
+ to: string;
18
+ }): move is {
19
+ from: number;
20
+ to: number;
21
+ };
22
+ export declare function resolveParentNodeDefinition(root: DCNode, path: RawPath): HydratedSimpleNativeProperty | undefined;
23
+ export declare function populateValueBasedOnDefinition(value: any, definition: HydratedSimpleNativeProperty | undefined): any;
24
+ export declare function walkToNode(root: DCNode, path: RawPath): DCNode;
25
+ export declare function descendIntoDCNodeChild(current: DCNode | undefined, path: RawPath, i: number): DCNode;
26
+ export declare function getDataStore(property: string, state: Draft<DCStoreState>, throwOnError: false): DataStore | undefined;
27
+ export declare function getDataStore(property: string, state: Draft<DCStoreState>, throwOnError?: undefined | true): DataStore;
28
+ export declare function getOptionalValue(path: NodePath, state: Draft<DCStoreState>): any;
29
+ export declare function removeValueFromBackingDataStore(dataStore: DataStore, path: NodePath): void;
30
+ export declare function writeValueToBackingDataStoreWithCorrections(path: NodePath, state: DCStoreState, value: any): void;
31
+ export declare function writeValueToBackingDataStore(dataStore: DataStore, path: NodePath, value: any): void;
32
+ /**
33
+ * @param dsValue a value from a {@link DataStore}
34
+ * @param path this is expected to be the entire path (including the node that was used to get {@link dsValue}
35
+ */
36
+ export declare function walkToNodeInValue(dsValue: any, path: RawPath): any;
37
+ export declare enum TreeMutatorResult {
38
+ STOP = 0,
39
+ CONTINUE = 1,
40
+ SKIP_DESCENDANTS = 2
41
+ }
42
+ export declare function applyToTree(tree: DCNode, mutator: (node: DCNode, path: NodePath) => TreeMutatorResult, path?: RawPath): TreeMutatorResult.STOP | TreeMutatorResult.CONTINUE;
43
+ export declare function buildNodeFromDataAndDefinition(data: any, definition: HydratedSimpleNativeProperty): DCNode;
@@ -0,0 +1,359 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TreeMutatorResult = void 0;
4
+ exports.removeFromParentNode = removeFromParentNode;
5
+ exports.moveNodeWithinParent = moveNodeWithinParent;
6
+ exports.isNumericMove = isNumericMove;
7
+ exports.resolveParentNodeDefinition = resolveParentNodeDefinition;
8
+ exports.populateValueBasedOnDefinition = populateValueBasedOnDefinition;
9
+ exports.walkToNode = walkToNode;
10
+ exports.descendIntoDCNodeChild = descendIntoDCNodeChild;
11
+ exports.getDataStore = getDataStore;
12
+ exports.getOptionalValue = getOptionalValue;
13
+ exports.removeValueFromBackingDataStore = removeValueFromBackingDataStore;
14
+ exports.writeValueToBackingDataStoreWithCorrections = writeValueToBackingDataStoreWithCorrections;
15
+ exports.writeValueToBackingDataStore = writeValueToBackingDataStore;
16
+ exports.walkToNodeInValue = walkToNodeInValue;
17
+ exports.applyToTree = applyToTree;
18
+ exports.buildNodeFromDataAndDefinition = buildNodeFromDataAndDefinition;
19
+ const hooks_1 = require("@wordpress/hooks");
20
+ const immer_1 = require("immer");
21
+ const MoveError_1 = require("../MoveError");
22
+ const PathError_1 = require("../PathError");
23
+ const simple_native_property_internal_shared_1 = require("../simple-native-property-internal-shared");
24
+ function removeFromParentNode(parentNode, path) {
25
+ if (typeof parentNode !== 'object' || parentNode === undefined || parentNode === null) {
26
+ throw new PathError_1.PathError("Encountered a non-existent node while expecting one.", path);
27
+ }
28
+ const nodeName = path[path.length - 1];
29
+ if (typeof nodeName === 'string') {
30
+ if (Array.isArray(parentNode)) {
31
+ throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, path.length - 1);
32
+ }
33
+ delete parentNode[nodeName];
34
+ }
35
+ else if (nodeName === undefined) {
36
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
37
+ }
38
+ else {
39
+ if (!Array.isArray(parentNode)) {
40
+ throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, path.length - 1);
41
+ }
42
+ parentNode.splice(nodeName, 1);
43
+ }
44
+ }
45
+ function moveNodeWithinParent(parentNode, path, move) {
46
+ if (typeof parentNode !== 'object' || parentNode === undefined || parentNode === null) {
47
+ throw new PathError_1.PathError("Encountered a non-existent node while expecting one.", path);
48
+ }
49
+ if (isNumericMove(move)) {
50
+ if (!Array.isArray(parentNode)) {
51
+ throw new MoveError_1.MoveError("Attempted to move a child of a node with string-keyed descendants by index", path, move);
52
+ }
53
+ if (move.from > move.to) {
54
+ parentNode.splice(move.to, 0, ...parentNode.splice(move.from, 1));
55
+ }
56
+ else { //We know that
57
+ parentNode.splice(move.to - 1, 0, ...parentNode.splice(move.from, 1));
58
+ }
59
+ }
60
+ else {
61
+ if (Array.isArray(parentNode)) {
62
+ throw new MoveError_1.MoveError("Attempted to move a child of a node with indexed descendants by string-key", path, move);
63
+ }
64
+ if (parentNode[move.to] !== undefined) {
65
+ throw new MoveError_1.MoveError("Attempted to move a string-keyed child to the same key as another", path, move);
66
+ }
67
+ }
68
+ }
69
+ function isNumericMove(move) {
70
+ return typeof move.from === 'number' && typeof move.to === 'number';
71
+ }
72
+ function resolveParentNodeDefinition(root, path) {
73
+ if (path.length < 2) {
74
+ 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);
75
+ }
76
+ return walkToNode(root, path.slice(0, path.length - (typeof path[path.length - 2] === 'number' ? 2 : 1))).definition;
77
+ }
78
+ function populateValueBasedOnDefinition(value, definition) {
79
+ const typedChildren = Array.isArray(definition?.children) ? definition.children : definition?.children?.[value?.type];
80
+ if (!typedChildren?.length) {
81
+ return value ?? (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(definition);
82
+ }
83
+ else if (value === null || value === undefined) {
84
+ return (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(definition);
85
+ }
86
+ else if (typeof value !== 'object') {
87
+ return value;
88
+ }
89
+ else if (Array.isArray(value)) {
90
+ return value;
91
+ }
92
+ return { ...value, ...Object.fromEntries(typedChildren.map(def => [def.name, populateValueBasedOnDefinition(value[def.name], def)])) };
93
+ }
94
+ function walkToNode(root, path) {
95
+ let current = root;
96
+ for (let i = 0; i < path.length; i++) {
97
+ current = descendIntoDCNodeChild(current, path, i);
98
+ }
99
+ return current;
100
+ }
101
+ function descendIntoDCNodeChild(current, path, i) {
102
+ if (current?.children === undefined) {
103
+ throw new PathError_1.PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
104
+ }
105
+ const pathNode = path[i];
106
+ if (typeof pathNode === 'string') {
107
+ if (Array.isArray(current.children)) {
108
+ throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, i);
109
+ }
110
+ current = current.children[pathNode];
111
+ }
112
+ else if (pathNode === undefined) {
113
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path, i);
114
+ }
115
+ else {
116
+ if (!Array.isArray(current.children)) {
117
+ throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, i);
118
+ }
119
+ current = current.children[pathNode];
120
+ }
121
+ if (current === undefined) {
122
+ throw new PathError_1.PathError("Encountered a non-existent node while expecting one.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
123
+ }
124
+ return current;
125
+ }
126
+ function getDataStore(property, state, throwOnError = true) {
127
+ let dataStore = state.dataStores.byProperty[property];
128
+ if (dataStore) {
129
+ return dataStore;
130
+ }
131
+ dataStore = state.dataStores.list.find(dataStore => dataStore.handlesProperty(property));
132
+ if (dataStore) {
133
+ 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.`);
134
+ }
135
+ else if (throwOnError) {
136
+ throw new Error(`Unable to resolve the dataStore for ${property} on block ${state.blockClientId}`);
137
+ }
138
+ return dataStore;
139
+ }
140
+ function getOptionalValue(path, state) {
141
+ try {
142
+ return walkToNodeInValue(getDataStore(path[0], state).getValue(path[0]), path);
143
+ }
144
+ catch (e) {
145
+ if (e instanceof PathError_1.PathError) {
146
+ return undefined;
147
+ }
148
+ throw e;
149
+ }
150
+ }
151
+ function removeValueFromBackingDataStore(dataStore, path) {
152
+ const dsValue = dataStore.getValue(path[0]);
153
+ if (path.length === 1) {
154
+ dataStore.setValue(path[0], undefined);
155
+ }
156
+ else {
157
+ dataStore.setValue(path[0], (0, immer_1.produce)(dsValue, (dsValueDraft) => {
158
+ const nodeParent = walkToNodeInValue(dsValueDraft, path.slice(0, path.length - 1));
159
+ const nodeName = path[path.length - 1];
160
+ if (typeof nodeName === 'string') {
161
+ delete nodeParent[nodeName];
162
+ }
163
+ else if (nodeName === undefined) {
164
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
165
+ }
166
+ else {
167
+ nodeParent.splice(nodeName, 1);
168
+ }
169
+ }));
170
+ }
171
+ }
172
+ function writeValueToBackingDataStoreWithCorrections(path, state, value) {
173
+ if (typeof value === 'number' && Number.isNaN(value)) {
174
+ value = undefined; // This ensures that NaN's don't get stored
175
+ }
176
+ const rootDCNode = state.treeRoot.children[path[0]];
177
+ if (rootDCNode === undefined) {
178
+ throw new PathError_1.PathError("Encountered a path pointing to a non-existent root node.", path, 0);
179
+ }
180
+ const dataStore = getDataStore(path[0], state);
181
+ // This path both handles scalar values and ensures that the root "value" of Groups and Lists are initialized before writing the actual attribute
182
+ // 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
183
+ if (path.length === 1 || (value === undefined && dataStore.getValue(path[0]) === undefined)) {
184
+ const writtenValue = value ?? (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(rootDCNode.definition, path.length === 1);
185
+ dataStore.setValue(path[0], writtenValue);
186
+ if (rootDCNode.definition) {
187
+ (0, hooks_1.doAction)('plaudit.gutenbergApiExtensions.simpleNativeProperties.afterWrite', writtenValue, rootDCNode.definition, dataStore);
188
+ }
189
+ if (path.length === 1) {
190
+ return;
191
+ }
192
+ }
193
+ let closestUsableDefinition = rootDCNode.definition;
194
+ let lastWrittenValue;
195
+ dataStore.setValue(path[0], (0, immer_1.produce)(dataStore.getValue(path[0]), (dsValueDraft) => {
196
+ let currentDSValue = dsValueDraft;
197
+ let currentDCNode = rootDCNode;
198
+ for (let i = 1; i < path.length - 1; i++) {
199
+ const nodeName = path[i];
200
+ currentDCNode = descendIntoDCNodeChild(currentDCNode, path, i);
201
+ if (typeof nodeName === 'string') {
202
+ //We only update the definition being used when we descend into a new node proper
203
+ closestUsableDefinition = currentDCNode.definition;
204
+ }
205
+ else if (nodeName === undefined) {
206
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path, i);
207
+ }
208
+ let nextDSValue = descendIntoDSValueNodeChild(currentDSValue, path, i);
209
+ if (nextDSValue === undefined) {
210
+ lastWrittenValue = currentDSValue[nodeName] = (0, simple_native_property_internal_shared_1.buildDefaultValueFromDefinition)(closestUsableDefinition);
211
+ currentDSValue = descendIntoDSValueNodeChild(currentDSValue, path, i);
212
+ if (currentDSValue === undefined) {
213
+ throw new PathError_1.PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
214
+ }
215
+ }
216
+ else {
217
+ currentDSValue = nextDSValue;
218
+ }
219
+ }
220
+ const nodeName = path[path.length - 1];
221
+ if (nodeName === undefined) {
222
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
223
+ }
224
+ lastWrittenValue = currentDSValue[nodeName] = value;
225
+ }));
226
+ if (closestUsableDefinition) {
227
+ (0, hooks_1.doAction)('plaudit.gutenbergApiExtensions.simpleNativeProperties.afterWrite', lastWrittenValue, closestUsableDefinition, dataStore);
228
+ }
229
+ }
230
+ function writeValueToBackingDataStore(dataStore, path, value) {
231
+ if (typeof value === 'number' && Number.isNaN(value)) {
232
+ value = undefined; // This ensures that NaN's don't get stored
233
+ }
234
+ const dsValue = dataStore.getValue(path[0]);
235
+ if (path.length === 1) {
236
+ dataStore.setValue(path[0], value);
237
+ }
238
+ else {
239
+ dataStore.setValue(path[0], (0, immer_1.produce)(dsValue, (dsValueDraft) => {
240
+ const nodeParent = walkToNodeInValue(dsValueDraft, path.slice(0, path.length - 1));
241
+ const nodeName = path[path.length - 1];
242
+ if (nodeName === undefined) {
243
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path);
244
+ }
245
+ nodeParent[nodeName] = value;
246
+ }));
247
+ }
248
+ }
249
+ /**
250
+ * @param dsValue a value from a {@link DataStore}
251
+ * @param path this is expected to be the entire path (including the node that was used to get {@link dsValue}
252
+ */
253
+ function walkToNodeInValue(dsValue, path) {
254
+ let current = dsValue;
255
+ for (let i = 1; i < path.length; i++) {
256
+ if (current === undefined) {
257
+ throw new PathError_1.PathError("Encountered a leaf node while expecting one with descendants.", path, i, PathError_1.PathErrorType.UNEXPECTED_LEAF);
258
+ }
259
+ current = descendIntoDSValueNodeChild(current, path, i);
260
+ }
261
+ return current;
262
+ }
263
+ function descendIntoDSValueNodeChild(current, path, i) {
264
+ const pathNode = path[i];
265
+ if (typeof pathNode === 'string') {
266
+ if (Array.isArray(current)) {
267
+ throw new PathError_1.PathError("Encountered a node with indexed descendants while expecting one with string-keyed descendants.", path, i);
268
+ }
269
+ current = current[pathNode];
270
+ }
271
+ else if (pathNode === undefined) {
272
+ throw new PathError_1.PathError("Encountered an undefined name in a path.", path, i);
273
+ }
274
+ else {
275
+ if (!Array.isArray(current)) {
276
+ throw new PathError_1.PathError("Encountered a node with string-keyed descendants while expecting one with indexed descendants.", path, i);
277
+ }
278
+ current = current[pathNode];
279
+ }
280
+ return current;
281
+ }
282
+ var TreeMutatorResult;
283
+ (function (TreeMutatorResult) {
284
+ TreeMutatorResult[TreeMutatorResult["STOP"] = 0] = "STOP";
285
+ TreeMutatorResult[TreeMutatorResult["CONTINUE"] = 1] = "CONTINUE";
286
+ TreeMutatorResult[TreeMutatorResult["SKIP_DESCENDANTS"] = 2] = "SKIP_DESCENDANTS";
287
+ })(TreeMutatorResult || (exports.TreeMutatorResult = TreeMutatorResult = {}));
288
+ function applyToTree(tree, mutator, path = []) {
289
+ if (tree.children === undefined) {
290
+ return TreeMutatorResult.CONTINUE;
291
+ }
292
+ const steps = Array.isArray(tree.children)
293
+ ? tree.children.map((node, index) => [path.toSpliced(path.length, 0, index), node])
294
+ : Object.entries(tree.children).map(([name, node]) => [path.toSpliced(path.length, 0, name), node]);
295
+ for (const [nodePath, node] of steps) {
296
+ const mutatorRes = mutator(node, nodePath);
297
+ if (!mutatorRes) {
298
+ return TreeMutatorResult.STOP;
299
+ }
300
+ else if (mutatorRes === TreeMutatorResult.CONTINUE) {
301
+ if (!applyToTree(node, mutator, nodePath)) {
302
+ return TreeMutatorResult.STOP;
303
+ }
304
+ }
305
+ }
306
+ return TreeMutatorResult.CONTINUE;
307
+ }
308
+ function buildNodeFromDataAndDefinition(data, definition) {
309
+ const children = definition.children;
310
+ if (children) {
311
+ if (data === undefined || data === null) {
312
+ if (definition.type === 'array') {
313
+ return { rendered: true, condition: definition.condition, definition, validationError: "", children: [] };
314
+ }
315
+ else if (definition.type === 'object') {
316
+ if (Array.isArray(children)) {
317
+ return {
318
+ rendered: true, condition: definition.condition, definition, validationError: "",
319
+ children: Object.fromEntries(children.map(def => [def.name, buildNodeFromDataAndDefinition(undefined, def)]))
320
+ };
321
+ }
322
+ else {
323
+ //TODO: We might need to throw an error here
324
+ return { rendered: true, condition: definition.condition, definition, validationError: "", children: {} };
325
+ }
326
+ }
327
+ }
328
+ else if (typeof data === 'object') {
329
+ if (Array.isArray(data)) {
330
+ return {
331
+ rendered: true, condition: definition.condition, definition, validationError: "", children: data.map(item => {
332
+ const typedChildren = Array.isArray(children) ? children : children[item.type];
333
+ if (typedChildren === undefined) {
334
+ //TODO: We might need to throw an error here
335
+ return { children: [], rendered: true, validationError: "" };
336
+ }
337
+ return {
338
+ children: Object.fromEntries(typedChildren.map(def => [def.name, buildNodeFromDataAndDefinition(item[def.name], def)])),
339
+ rendered: true, validationError: ""
340
+ };
341
+ })
342
+ };
343
+ }
344
+ else {
345
+ const typedChildren = Array.isArray(children) ? children : children[data.type];
346
+ if (typedChildren === undefined) {
347
+ //TODO: We might need to throw an error here
348
+ return { rendered: true, condition: definition.condition, definition, validationError: "", children: [] };
349
+ }
350
+ return {
351
+ rendered: true, condition: definition.condition, definition, validationError: "",
352
+ children: Object.fromEntries(typedChildren.map(def => [def.name, buildNodeFromDataAndDefinition(data[def.name], def)]))
353
+ };
354
+ }
355
+ }
356
+ }
357
+ return { rendered: true, condition: definition.condition, definition, validationError: "" };
358
+ }
359
+ //# sourceMappingURL=utils.js.map