@fragmentsx/render-react 1.5.2 → 1.6.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 (27) hide show
  1. package/dist/hooks/fragment/useFragmentManager.d.ts.map +1 -1
  2. package/dist/hooks/layer/useLayerValue.d.ts.map +1 -1
  3. package/dist/hooks/layer/useLayerVariableValue.d.ts.map +1 -1
  4. package/dist/hooks/layer/useLayersValue.d.ts.map +1 -1
  5. package/dist/hooks/layer/useReadVariable.d.ts +8 -11
  6. package/dist/hooks/layer/useReadVariable.d.ts.map +1 -1
  7. package/dist/hooks/layer/useReadVariables.d.ts +3 -3
  8. package/dist/hooks/layer/useReadVariables.d.ts.map +1 -1
  9. package/dist/hooks/layer/useResolvedLayerVariables.d.ts +1 -1
  10. package/dist/hooks/layer/useResolvedLayerVariables.d.ts.map +1 -1
  11. package/dist/hooks/layer-styles/useLayerStyles/calcImagePaint.test.d.ts +2 -0
  12. package/dist/hooks/layer-styles/useLayerStyles/calcImagePaint.test.d.ts.map +1 -0
  13. package/dist/hooks/utils/useExtractProps.d.ts.map +1 -1
  14. package/dist/hooks/utils/useLayerInteractions.d.ts.map +1 -1
  15. package/dist/index.cjs.js +402 -438
  16. package/dist/index.es.js +403 -439
  17. package/dist/nodes/Area/hooks/useAsyncLoadArea.d.ts.map +1 -1
  18. package/dist/nodes/Instance/Instance.d.ts.map +1 -1
  19. package/dist/nodes/Instance/hooks/useInstanceProps.d.ts.map +1 -1
  20. package/dist/nodes/Instance/test/project-props.browser.test.d.ts +2 -0
  21. package/dist/nodes/Instance/test/project-props.browser.test.d.ts.map +1 -0
  22. package/dist/nodes/Text/hooks/useTextContent.d.ts.map +1 -1
  23. package/dist/providers/InstanceContext.d.ts +1 -0
  24. package/dist/providers/InstanceContext.d.ts.map +1 -1
  25. package/dist/utils/common/transformCssValue.test.d.ts +2 -0
  26. package/dist/utils/common/transformCssValue.test.d.ts.map +1 -0
  27. package/package.json +4 -2
package/dist/index.es.js CHANGED
@@ -1,9 +1,9 @@
1
- import { createContext, useContext, useMemo, useReducer, useRef, useEffect, useCallback, useState, memo, Suspense } from "react";
1
+ import { createContext, useContext, useMemo, useCallback, useEffect, useState, useRef, memo, Suspense } from "react";
2
2
  import { definition, getNormalizeLayer as getNormalizeLayer$1, isVariableLink as isVariableLink$1, parseLayerField, isLink as isLink$1 } from "@fragmentsx/definition";
3
3
  import { jsx, jsxs } from "react/jsx-runtime";
4
+ import { useGraph, useGraphStack } from "@graph-state/react";
4
5
  import { cleanGraph, generateId, getKey as getKey$1, isValue, isObject, pick, toPx, setKey as setKey$1, toKebabCase, set, isPrimitive, get, omit, noop, isFiniteNumber, hashGenerator as hashGenerator$1, isBrowser as isBrowser$1 } from "@fragmentsx/utils";
5
6
  import { keyOfEntity, isPartialKey, entityOfKey, isLinkKey } from "@graph-state/core";
6
- import { useGraph, useGraphStack } from "@graph-state/react";
7
7
  import { createAreaManager } from "@fragmentsx/client-core";
8
8
  const GlobalManagerContext = createContext(null);
9
9
  const GlobalManagerProvider = GlobalManagerContext.Provider;
@@ -12,12 +12,13 @@ const FragmentContext = createContext({
12
12
  });
13
13
  const defaultCustomRender = (layerKey, node) => node;
14
14
  const CustomRender = createContext(defaultCustomRender);
15
- const InstanceContext$1 = createContext({
15
+ const InstanceContext = createContext({
16
16
  layerKey: null,
17
17
  parentManager: null,
18
18
  innerManager: null,
19
19
  props: {},
20
- definitions: []
20
+ definitions: [],
21
+ onChangeProps: null
21
22
  });
22
23
  const RenderTargetContext = createContext(
23
24
  definition.renderTarget.document
@@ -660,8 +661,8 @@ function hashGenerator(layerKey) {
660
661
  return /^[0-9]/.test(raw) ? `h${raw}` : raw;
661
662
  }
662
663
  const transformCssValue = (value) => {
663
- var _a;
664
- if ((_a = value == null ? void 0 : value.startsWith) == null ? void 0 : _a.call(value, "http")) {
664
+ var _a, _b;
665
+ if (((_a = value == null ? void 0 : value.startsWith) == null ? void 0 : _a.call(value, "http")) || ((_b = value == null ? void 0 : value.startsWith) == null ? void 0 : _b.call(value, "data:"))) {
665
666
  value = `url("${value}")`;
666
667
  }
667
668
  return value;
@@ -861,178 +862,135 @@ const useResolvedLayer = (layerKey, options) => {
861
862
  }
862
863
  return { layerKey: key, rawLayer, layer, isOverride };
863
864
  };
864
- function useForceUpdate() {
865
- return useReducer(() => ({}), {});
866
- }
867
- const EMPTY_RESOLVED = {
868
- values: {},
869
- cssVariableValues: {},
870
- rawValues: {}
871
- };
872
- const extractVariableValue$1 = (input, variableId) => {
873
- if (isObject(input)) {
865
+ const extractVariableValue = (input, variableId) => {
866
+ if (input && typeof input === "object") {
874
867
  if (variableId in input) return input[variableId];
875
868
  for (const key in input) {
876
869
  if (input.hasOwnProperty(key)) {
877
- const result = extractVariableValue$1(input[key], variableId);
870
+ const result = extractVariableValue(input[key], variableId);
878
871
  if (result !== void 0) return result;
879
872
  }
880
873
  }
881
874
  }
882
875
  return void 0;
883
876
  };
884
- function deepMerge$1(a, b) {
885
- if (!isObject(a) || !isObject(b)) return a;
886
- const result = { ...b };
887
- for (const [key, value] of Object.entries(a)) {
888
- if (key in result && typeof result[key] === "object" && typeof value === "object") {
889
- result[key] = deepMerge$1(value, result[key]);
890
- } else {
891
- result[key] = value;
877
+ const useReadVariables = (variableKeys, customManager) => {
878
+ var _a, _b, _c, _d;
879
+ const filterKeys = variableKeys.filter(isVariableLink$1);
880
+ const { manager: fragmentManager } = useContext(FragmentContext);
881
+ const { props: instanceProps } = useContext(InstanceContext);
882
+ const globalManager = useContext(GlobalManagerContext);
883
+ const areaManager = useContext(AreaManagerContext);
884
+ const scopes = useContext(ScopeContext);
885
+ const resultManager = customManager ?? ((_a = useContext(InstanceContext)) == null ? void 0 : _a.innerManager) ?? fragmentManager;
886
+ const variableLayers = useGraphStack(resultManager, filterKeys, {
887
+ pause: !filterKeys.length || !resultManager,
888
+ selector: (graph) => graph ? pick(graph, "_id", "defaultValue", "required") : graph
889
+ });
890
+ useGraph(globalManager, (_b = globalManager == null ? void 0 : globalManager.$properties) == null ? void 0 : _b.key);
891
+ useGraph(areaManager, (_c = areaManager == null ? void 0 : areaManager.$properties) == null ? void 0 : _c.key);
892
+ const globalPropertyKeys = useMemo(() => {
893
+ if (!(globalManager == null ? void 0 : globalManager.$properties)) return [];
894
+ return filterKeys.filter((key) => globalManager.resolve(key));
895
+ }, [filterKeys, globalManager]);
896
+ const areaPropertyKeys = useMemo(() => {
897
+ if (!(areaManager == null ? void 0 : areaManager.$properties)) return [];
898
+ return filterKeys.filter((key) => areaManager.resolve(key));
899
+ }, [filterKeys, areaManager]);
900
+ useGraphStack(
901
+ globalPropertyKeys.length ? globalManager : null,
902
+ globalPropertyKeys
903
+ );
904
+ useGraphStack(areaPropertyKeys.length ? areaManager : null, areaPropertyKeys);
905
+ const lastCollectionItem = (_d = scopes == null ? void 0 : scopes.findLast) == null ? void 0 : _d.call(
906
+ scopes,
907
+ (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.CollectionItemScope
908
+ );
909
+ return filterKeys.map((variableKey, index) => {
910
+ var _a2;
911
+ const layer = variableLayers[index] ?? null;
912
+ const { _id: propertyId } = layer ?? {};
913
+ let collectionItemProp = void 0;
914
+ if (lastCollectionItem && propertyId) {
915
+ collectionItemProp = isPrimitive(lastCollectionItem == null ? void 0 : lastCollectionItem.value) && ((_a2 = entityOfKey(lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition)) == null ? void 0 : _a2._id) === propertyId ? lastCollectionItem == null ? void 0 : lastCollectionItem.value : extractVariableValue(lastCollectionItem == null ? void 0 : lastCollectionItem.value, propertyId);
916
+ }
917
+ const instanceProp = (instanceProps == null ? void 0 : instanceProps[propertyId]) ?? null;
918
+ const areaLayer = areaManager == null ? void 0 : areaManager.resolve(variableKey);
919
+ const areaProp = (areaLayer == null ? void 0 : areaLayer.defaultValue) ?? null;
920
+ const globalLayer = globalManager == null ? void 0 : globalManager.resolve(variableKey);
921
+ const globalProp = (globalLayer == null ? void 0 : globalLayer.defaultValue) ?? null;
922
+ const required = (layer == null ? void 0 : layer.required) ?? false;
923
+ const defaultValue = (layer == null ? void 0 : layer.defaultValue) ?? null;
924
+ const currentValue = collectionItemProp ?? (variableKey === instanceProp ? null : instanceProp) ?? areaProp ?? globalProp ?? null;
925
+ const resultValue = required ? currentValue : currentValue ?? defaultValue;
926
+ if (isVariableLink$1(resultValue)) {
927
+ const refKey = resultValue;
928
+ const refAreaLayer = areaManager == null ? void 0 : areaManager.resolve(refKey);
929
+ const refGlobalLayer = globalManager == null ? void 0 : globalManager.resolve(refKey);
930
+ const refFragmentLayer = resultManager == null ? void 0 : resultManager.resolve(refKey);
931
+ const refValue = (refAreaLayer == null ? void 0 : refAreaLayer.defaultValue) ?? (refGlobalLayer == null ? void 0 : refGlobalLayer.defaultValue) ?? (refFragmentLayer == null ? void 0 : refFragmentLayer.defaultValue) ?? resultValue;
932
+ return {
933
+ value: refValue,
934
+ layer: refAreaLayer ?? refGlobalLayer ?? refFragmentLayer ?? null
935
+ };
892
936
  }
893
- }
894
- return result;
895
- }
896
- const useResolvedLayerVariables = (layer, rawLayer, customScopes) => {
897
- var _a;
898
- const contextScopes = useContext(ScopeContext);
899
- const scopes = customScopes ?? contextScopes;
900
- const subscriptions = useRef([]);
901
- const [dep, forceUpdate] = useForceUpdate();
902
- useEffect(() => {
903
- const controller = new AbortController();
904
- let ready = false;
905
- const update = () => {
906
- if (!ready) return;
907
- forceUpdate();
937
+ return {
938
+ value: resultValue,
939
+ layer: layer ?? areaLayer ?? globalLayer ?? null
908
940
  };
909
- for (const { manager, key } of subscriptions.current) {
910
- manager.subscribe(key, update, { signal: controller.signal });
911
- }
912
- ready = true;
913
- return () => controller.abort();
914
941
  });
915
- if (!layer) {
916
- subscriptions.current = [];
917
- return EMPTY_RESOLVED;
942
+ };
943
+ const EMPTY_RESOLVED = {
944
+ values: {},
945
+ cssVariableValues: {},
946
+ rawValues: {}
947
+ };
948
+ const collectVariableKeys = (layer) => {
949
+ const keys = [];
950
+ if (!layer) return keys;
951
+ for (const [, value] of Object.entries(layer)) {
952
+ if (isVariableLink$1(value)) {
953
+ keys.push(value);
954
+ } else if (isObject(value) && !isVariableLink$1(value)) {
955
+ for (const [, nestedValue] of Object.entries(
956
+ value
957
+ )) {
958
+ if (isVariableLink$1(nestedValue)) {
959
+ keys.push(nestedValue);
960
+ }
961
+ }
962
+ }
918
963
  }
964
+ return keys;
965
+ };
966
+ const useResolvedLayerVariables = (layer, rawLayer, _customScopes) => {
967
+ var _a;
968
+ const variableKeys = collectVariableKeys(layer);
969
+ const resolved = useReadVariables(variableKeys);
970
+ if (!layer) return EMPTY_RESOLVED;
971
+ const resolvedByKey = {};
972
+ variableKeys.forEach((key, i) => {
973
+ var _a2;
974
+ resolvedByKey[key] = ((_a2 = resolved[i]) == null ? void 0 : _a2.value) ?? null;
975
+ });
919
976
  const values = {};
920
977
  const cssVariableValues = {};
921
978
  const rawValues = {};
922
- const readVariable = (variableKey) => {
923
- var _a2, _b, _c;
924
- const variableId = (_a2 = entityOfKey(variableKey)) == null ? void 0 : _a2._id;
925
- if (!isVariableLink$1(variableKey)) {
926
- return { value: null, layer: null };
927
- }
928
- let instanceScope = scopes.findLast(
929
- (scope) => {
930
- var _a3;
931
- return (scope == null ? void 0 : scope.type) === definition.scopeTypes.InstanceScope && !!((_a3 = scope.documentManager) == null ? void 0 : _a3.resolve(variableKey));
932
- }
933
- );
934
- if (!instanceScope) {
935
- instanceScope = scopes.find(
936
- (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.InstanceScope
937
- );
938
- }
939
- const resolveVariableLayer = (manager, variableLink, customProps) => {
940
- var _a3, _b2;
941
- const variableLayer2 = (_a3 = getNormalizeLayer(variableLink, manager)) == null ? void 0 : _a3.layer;
942
- if (!variableLayer2) return null;
943
- if ((variableLayer2 == null ? void 0 : variableLayer2.type) === definition.variableType.Array) {
944
- const definitionLayer = manager.resolve(variableLayer2 == null ? void 0 : variableLayer2.definition);
945
- if (!((_b2 = variableLayer2 == null ? void 0 : variableLayer2.defaultValue) == null ? void 0 : _b2.length) || (definitionLayer == null ? void 0 : definitionLayer.type) !== definition.variableType.Object)
946
- return variableLayer2;
947
- const extractVariableDefaultValue = (mgr, varEntity) => {
948
- const varLayer = mgr.resolve(varEntity);
949
- if ((varLayer == null ? void 0 : varLayer.type) !== definition.variableType.Object)
950
- return varLayer == null ? void 0 : varLayer.defaultValue;
951
- return Object.values((varLayer == null ? void 0 : varLayer.fields) ?? {}).reduce(
952
- (acc, fieldLink) => {
953
- var _a4;
954
- if (!!fieldLink && isVariableLink$1(fieldLink)) {
955
- const _id = (_a4 = entityOfKey(fieldLink)) == null ? void 0 : _a4._id;
956
- if (_id) acc[_id] = extractVariableDefaultValue(mgr, fieldLink);
957
- }
958
- return acc;
959
- },
960
- {}
961
- );
962
- };
963
- const definitionDefaultValue = extractVariableDefaultValue(
964
- manager,
965
- definitionLayer
966
- );
967
- const customValue = customProps == null ? void 0 : customProps[variableLayer2 == null ? void 0 : variableLayer2._id];
968
- return {
969
- ...variableLayer2,
970
- defaultValue: variableLayer2.defaultValue.map(
971
- (item, index) => {
972
- const customItemValue = Array.isArray(customValue) ? customValue.at(index) : null;
973
- return deepMerge$1(
974
- customItemValue ? { ...item, ...customItemValue } : item,
975
- definitionDefaultValue
976
- );
977
- }
978
- )
979
- };
980
- }
981
- return variableLayer2;
982
- };
983
- const variableLayer = resolveVariableLayer(
984
- instanceScope == null ? void 0 : instanceScope.documentManager,
985
- variableKey
986
- );
987
- if ((instanceScope == null ? void 0 : instanceScope.documentManager) && variableKey) {
988
- subscriptions.current.push({
989
- manager: instanceScope.documentManager,
990
- key: variableKey
991
- });
992
- }
993
- let instanceProp = variableId ? (_b = instanceScope == null ? void 0 : instanceScope.props) == null ? void 0 : _b[variableId] : void 0;
994
- if (Array.isArray(instanceProp)) {
995
- instanceProp = instanceProp.map(
996
- (rawProp, index) => {
997
- var _a3;
998
- return deepMerge$1(rawProp, (_a3 = variableLayer == null ? void 0 : variableLayer.defaultValue) == null ? void 0 : _a3.at(index));
999
- }
1000
- );
1001
- }
1002
- const lastCollectionItem = scopes.findLast(
1003
- (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.CollectionItemScope
1004
- );
1005
- resolveVariableLayer(
1006
- lastCollectionItem == null ? void 0 : lastCollectionItem.manager,
1007
- lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition,
1008
- lastCollectionItem == null ? void 0 : lastCollectionItem.value
1009
- );
1010
- const collectionItemProp = isPrimitive(lastCollectionItem == null ? void 0 : lastCollectionItem.value) && ((_c = entityOfKey(lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition)) == null ? void 0 : _c._id) === variableId ? lastCollectionItem == null ? void 0 : lastCollectionItem.value : extractVariableValue$1(lastCollectionItem == null ? void 0 : lastCollectionItem.value, variableId);
1011
- const currentValue = variableKey === instanceProp ? null : collectionItemProp ?? instanceProp ?? null;
1012
- const required = (variableLayer == null ? void 0 : variableLayer.required) ?? false;
1013
- const defaultValue = (variableLayer == null ? void 0 : variableLayer.defaultValue) ?? null;
1014
- const resultValue = required ? currentValue : currentValue ?? collectionItemProp ?? defaultValue;
1015
- if (isVariableLink$1(resultValue)) {
1016
- return readVariable(resultValue);
1017
- }
1018
- return { value: resultValue, layer: variableLayer };
1019
- };
1020
979
  const resolveField = (fieldPath, value, raw) => {
1021
980
  var _a2;
1022
981
  rawValues[fieldPath] = raw;
1023
982
  if (isVariableLink$1(value)) {
1024
- const resolved = readVariable(value);
1025
- values[fieldPath] = resolved.value ?? value;
983
+ const resolvedValue = resolvedByKey[value] ?? value;
984
+ values[fieldPath] = resolvedValue;
1026
985
  const variableId = (_a2 = entityOfKey(value)) == null ? void 0 : _a2._id;
1027
986
  cssVariableValues[fieldPath] = `var(--${variableId}, ${transformCssValue(
1028
- resolved.value ?? value
987
+ resolvedValue
1029
988
  )})`;
1030
989
  } else {
1031
990
  values[fieldPath] = value;
1032
991
  cssVariableValues[fieldPath] = transformCssValue(value);
1033
992
  }
1034
993
  };
1035
- subscriptions.current = [];
1036
994
  for (const [field, value] of Object.entries(layer)) {
1037
995
  resolveField(field, value, rawLayer == null ? void 0 : rawLayer[field]);
1038
996
  if (isObject(value) && !isVariableLink$1(value)) {
@@ -1057,188 +1015,6 @@ const useRenderTarget = () => {
1057
1015
  isDocument: renderTarget === definition.renderTarget.document
1058
1016
  };
1059
1017
  };
1060
- const useReadVariables = (variableKeys) => {
1061
- const filterKeys = variableKeys.filter(isVariableLink$1);
1062
- const { manager: fragmentManager } = useContext(FragmentContext);
1063
- const { props, innerManager, layerKey } = useContext(InstanceContext);
1064
- const resultManager = innerManager ?? fragmentManager;
1065
- const variableLayers = useGraphStack(
1066
- !!filterKeys.length ? resultManager : null,
1067
- filterKeys,
1068
- {
1069
- selector: (graph) => graph ? pick(graph, "defaultValue", "required") : graph
1070
- }
1071
- );
1072
- return variableLayers.map((layer) => {
1073
- const { _id: propertyId } = layer ?? {};
1074
- const currentValue = (props == null ? void 0 : props[propertyId]) ?? null;
1075
- const required = (layer == null ? void 0 : layer.required) ?? false;
1076
- const defaultValue = (layer == null ? void 0 : layer.defaultValue) ?? null;
1077
- const resultValue = required ? currentValue : currentValue ?? defaultValue;
1078
- return {
1079
- value: resultValue,
1080
- layer
1081
- };
1082
- });
1083
- };
1084
- const extractVariableValue = (input, variableId) => {
1085
- if (isObject(input)) {
1086
- if (variableId in input) {
1087
- return input[variableId];
1088
- }
1089
- for (let key in input) {
1090
- if (input.hasOwnProperty(key)) {
1091
- const result = extractVariableValue(input[key], variableId);
1092
- if (result !== void 0) {
1093
- return result;
1094
- }
1095
- }
1096
- }
1097
- }
1098
- return void 0;
1099
- };
1100
- function deepMerge(a, b) {
1101
- if (!isObject(a) || !isObject(b)) return a;
1102
- const result = { ...b };
1103
- for (const [key, value] of Object.entries(a)) {
1104
- if (key in result && typeof result[key] === "object" && typeof value === "object") {
1105
- result[key] = deepMerge(value, result[key]);
1106
- } else {
1107
- result[key] = value;
1108
- }
1109
- }
1110
- return result;
1111
- }
1112
- const useReadVariable = (variableKey, customScopes) => {
1113
- const contextScopes = useContext(ScopeContext);
1114
- const scopes = customScopes ?? contextScopes;
1115
- const subscriptions = useRef([]);
1116
- const [dep, forceUpdate] = useForceUpdate();
1117
- useEffect(() => {
1118
- const controller = new AbortController();
1119
- let ready = false;
1120
- const update = () => {
1121
- if (!ready) return;
1122
- forceUpdate();
1123
- };
1124
- for (const { manager, key } of subscriptions.current) {
1125
- manager.subscribe(key, update, { signal: controller.signal });
1126
- }
1127
- ready = true;
1128
- return () => controller.abort();
1129
- });
1130
- const extractVariableDefaultValue = (manager, variableEntity) => {
1131
- const variableLayer = manager.resolve(variableEntity);
1132
- if ((variableLayer == null ? void 0 : variableLayer.type) !== definition.variableType.Object)
1133
- return variableLayer == null ? void 0 : variableLayer.defaultValue;
1134
- return Object.values((variableLayer == null ? void 0 : variableLayer.fields) ?? {}).reduce(
1135
- (acc, fieldLink) => {
1136
- var _a;
1137
- if (!!fieldLink && isVariableLink$1(fieldLink)) {
1138
- const _id = (_a = entityOfKey(fieldLink)) == null ? void 0 : _a._id;
1139
- if (_id) {
1140
- acc[_id] = extractVariableDefaultValue(manager, fieldLink);
1141
- }
1142
- }
1143
- return acc;
1144
- },
1145
- {}
1146
- );
1147
- };
1148
- const resolveVariableLayer = (manager, variableLink, customProps) => {
1149
- var _a, _b;
1150
- const variableLayer = (_a = getNormalizeLayer(variableLink, manager)) == null ? void 0 : _a.layer;
1151
- if (!variableLayer) return null;
1152
- if ((variableLayer == null ? void 0 : variableLayer.type) === definition.variableType.Array) {
1153
- const definitionLayer = manager.resolve(variableLayer == null ? void 0 : variableLayer.definition);
1154
- if (!((_b = variableLayer == null ? void 0 : variableLayer.defaultValue) == null ? void 0 : _b.length) || (definitionLayer == null ? void 0 : definitionLayer.type) !== definition.variableType.Object)
1155
- return variableLayer;
1156
- const definitionDefaultValue = extractVariableDefaultValue(
1157
- manager,
1158
- definitionLayer
1159
- );
1160
- const customValue = customProps == null ? void 0 : customProps[variableLayer == null ? void 0 : variableLayer._id];
1161
- return {
1162
- ...variableLayer,
1163
- defaultValue: (variableLayer == null ? void 0 : variableLayer.defaultValue).map((item, index) => {
1164
- const customItemValue = Array.isArray(customValue) ? customValue.at(index) : null;
1165
- return deepMerge(
1166
- customItemValue ? { ...item, ...customItemValue } : item,
1167
- definitionDefaultValue
1168
- );
1169
- })
1170
- };
1171
- }
1172
- return variableLayer;
1173
- };
1174
- const readVariable = (variableKey2) => {
1175
- var _a, _b, _c;
1176
- const variableId = (_a = entityOfKey(variableKey2)) == null ? void 0 : _a._id;
1177
- if (!isVariableLink$1(variableKey2)) {
1178
- return {
1179
- value: null,
1180
- layer: null
1181
- };
1182
- }
1183
- let instanceScope = scopes.findLast(
1184
- (scope) => {
1185
- var _a2;
1186
- return (scope == null ? void 0 : scope.type) === definition.scopeTypes.InstanceScope && !!((_a2 = scope.documentManager) == null ? void 0 : _a2.resolve(variableKey2));
1187
- }
1188
- );
1189
- if (!instanceScope) {
1190
- instanceScope = scopes.find(
1191
- (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.InstanceScope
1192
- );
1193
- }
1194
- const variableLayer = resolveVariableLayer(
1195
- instanceScope == null ? void 0 : instanceScope.documentManager,
1196
- variableKey2
1197
- );
1198
- if ((instanceScope == null ? void 0 : instanceScope.documentManager) && variableKey2) {
1199
- subscriptions.current.push({
1200
- manager: instanceScope.documentManager,
1201
- key: variableKey2
1202
- });
1203
- }
1204
- let instanceProp = (_b = instanceScope == null ? void 0 : instanceScope.props) == null ? void 0 : _b[variableId];
1205
- if (Array.isArray(instanceProp)) {
1206
- instanceProp = instanceProp.map(
1207
- (rawProp, index) => {
1208
- var _a2;
1209
- return deepMerge(rawProp, (_a2 = variableLayer == null ? void 0 : variableLayer.defaultValue) == null ? void 0 : _a2.at(index));
1210
- }
1211
- );
1212
- }
1213
- const lastCollectionItem = scopes.findLast(
1214
- (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.CollectionItemScope
1215
- );
1216
- resolveVariableLayer(
1217
- lastCollectionItem == null ? void 0 : lastCollectionItem.manager,
1218
- lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition,
1219
- lastCollectionItem == null ? void 0 : lastCollectionItem.value
1220
- );
1221
- const collectionItemProp = isPrimitive(lastCollectionItem == null ? void 0 : lastCollectionItem.value) && ((_c = entityOfKey(lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition)) == null ? void 0 : _c._id) === variableId ? lastCollectionItem == null ? void 0 : lastCollectionItem.value : extractVariableValue(lastCollectionItem == null ? void 0 : lastCollectionItem.value, variableId);
1222
- const currentValue = variableKey2 === instanceProp ? null : collectionItemProp ?? instanceProp ?? null;
1223
- const required = (variableLayer == null ? void 0 : variableLayer.required) ?? false;
1224
- const defaultValue = (variableLayer == null ? void 0 : variableLayer.defaultValue) ?? null;
1225
- const resultValue = required ? currentValue : currentValue ?? collectionItemProp ?? defaultValue;
1226
- if (isVariableLink$1(resultValue)) {
1227
- return readVariable(resultValue);
1228
- }
1229
- return {
1230
- value: resultValue,
1231
- layer: variableLayer
1232
- };
1233
- };
1234
- subscriptions.current = [];
1235
- const result = readVariable(variableKey);
1236
- return {
1237
- ...result,
1238
- readVariable,
1239
- _dep: dep
1240
- };
1241
- };
1242
1018
  const useLayersValue = (layerKeys, fieldKey, options) => {
1243
1019
  const { manager: fragmentManager } = useContext(FragmentContext);
1244
1020
  const resultManager = (options == null ? void 0 : options.manager) ?? fragmentManager;
@@ -1250,12 +1026,21 @@ const useLayersValue = (layerKeys, fieldKey, options) => {
1250
1026
  // selector: (data) => (data ? pick(data, fieldKey) : data),
1251
1027
  });
1252
1028
  const normalizeLayers = useNormalizeLayers(keys, resultManager, options);
1253
- const { readVariable } = useReadVariable(null, options == null ? void 0 : options.scopes);
1029
+ const variableKeysFromLayers = normalizeLayers.map((nl) => get(nl.layer, fieldKey)).filter(isVariableLink$1);
1030
+ const resolvedVariables = useReadVariables(
1031
+ variableKeysFromLayers,
1032
+ resultManager
1033
+ );
1034
+ const resolvedByKey = {};
1035
+ variableKeysFromLayers.forEach((key, i) => {
1036
+ var _a;
1037
+ resolvedByKey[key] = ((_a = resolvedVariables[i]) == null ? void 0 : _a.value) ?? null;
1038
+ });
1254
1039
  return normalizeLayers.map((normalizedLayer) => {
1255
- var _a, _b;
1040
+ var _a;
1256
1041
  const rawValue = get(normalizedLayer.rawLayer, fieldKey);
1257
1042
  const layerValue = get(normalizedLayer.layer, fieldKey);
1258
- const variableValue = (_a = readVariable(layerValue)) == null ? void 0 : _a.value;
1043
+ const variableValue = isVariableLink$1(layerValue) ? resolvedByKey[layerValue] ?? null : null;
1259
1044
  const currentValue = variableValue ?? layerValue;
1260
1045
  const isInherit = isInheritField(
1261
1046
  resultManager,
@@ -1280,15 +1065,15 @@ const useLayersValue = (layerKeys, fieldKey, options) => {
1280
1065
  );
1281
1066
  };
1282
1067
  const restore = (fallbackValue) => {
1283
- var _a2, _b2, _c;
1284
- const tempValue = ((_c = (_b2 = resultManager.resolve((_a2 = resultManager == null ? void 0 : resultManager.$fragment) == null ? void 0 : _a2.temp)) == null ? void 0 : _b2[normalizedLayer.layerKey]) == null ? void 0 : _c[fieldKey]) ?? fallbackValue;
1068
+ var _a2, _b, _c;
1069
+ const tempValue = ((_c = (_b = resultManager.resolve((_a2 = resultManager == null ? void 0 : resultManager.$fragment) == null ? void 0 : _a2.temp)) == null ? void 0 : _b[normalizedLayer.layerKey]) == null ? void 0 : _c[fieldKey]) ?? fallbackValue;
1285
1070
  return tempValue;
1286
1071
  };
1287
1072
  const updateValue = (value, options2) => {
1288
1073
  setter(value, options2);
1289
1074
  };
1290
1075
  const isVariable = isVariableLink$1(rawValue ?? layerValue);
1291
- const variableId = isVariable ? (_b = entityOfKey(layerValue)) == null ? void 0 : _b._id : null;
1076
+ const variableId = isVariable ? (_a = entityOfKey(layerValue)) == null ? void 0 : _a._id : null;
1292
1077
  return [
1293
1078
  currentValue,
1294
1079
  updateValue,
@@ -1305,7 +1090,7 @@ const useLayersValue = (layerKeys, fieldKey, options) => {
1305
1090
  });
1306
1091
  };
1307
1092
  const useLayerValue = (layerKey, fieldKey, options) => {
1308
- var _a;
1093
+ var _a, _b;
1309
1094
  const { manager: fragmentManager } = useContext(FragmentContext);
1310
1095
  const resultManager = (options == null ? void 0 : options.manager) ?? fragmentManager;
1311
1096
  if (!resultManager) {
@@ -1318,7 +1103,11 @@ const useLayerValue = (layerKey, fieldKey, options) => {
1318
1103
  const { layer, rawLayer } = useNormalizeLayer(key, resultManager, options);
1319
1104
  const rawValue = get(rawLayer, fieldKey);
1320
1105
  const layerValue = get(layer, fieldKey);
1321
- const { value: variableValue } = useReadVariable(layerValue, options == null ? void 0 : options.scopes);
1106
+ const variableResults = useReadVariables(
1107
+ isVariableLink$1(layerValue) ? [layerValue] : [],
1108
+ resultManager
1109
+ );
1110
+ const variableValue = ((_a = variableResults[0]) == null ? void 0 : _a.value) ?? null;
1322
1111
  const currentValue = variableValue ?? layerValue;
1323
1112
  const isInherit = isInheritField(resultManager, key, fieldKey);
1324
1113
  const isOverride = !isInherit && !isPartOfPrimary(resultManager, key);
@@ -1335,8 +1124,8 @@ const useLayerValue = (layerKey, fieldKey, options) => {
1335
1124
  }, [updateLayerData]);
1336
1125
  const restore = useCallback(
1337
1126
  (fallbackValue) => {
1338
- var _a2, _b, _c;
1339
- const tempValue = ((_c = (_b = resultManager.resolve((_a2 = resultManager == null ? void 0 : resultManager.$fragment) == null ? void 0 : _a2.temp)) == null ? void 0 : _b[key]) == null ? void 0 : _c[fieldKey]) ?? fallbackValue;
1127
+ var _a2, _b2, _c;
1128
+ const tempValue = ((_c = (_b2 = resultManager.resolve((_a2 = resultManager == null ? void 0 : resultManager.$fragment) == null ? void 0 : _a2.temp)) == null ? void 0 : _b2[key]) == null ? void 0 : _c[fieldKey]) ?? fallbackValue;
1340
1129
  updateLayerData({ [fieldKey]: tempValue });
1341
1130
  return tempValue;
1342
1131
  },
@@ -1349,7 +1138,7 @@ const useLayerValue = (layerKey, fieldKey, options) => {
1349
1138
  [setter]
1350
1139
  );
1351
1140
  const isVariable = isVariableLink$1(rawValue ?? layerValue);
1352
- const variableId = isVariable ? (_a = entityOfKey(layerValue)) == null ? void 0 : _a._id : null;
1141
+ const variableId = isVariable ? (_b = entityOfKey(layerValue)) == null ? void 0 : _b._id : null;
1353
1142
  return [
1354
1143
  currentValue,
1355
1144
  updateValue,
@@ -1364,6 +1153,20 @@ const useLayerValue = (layerKey, fieldKey, options) => {
1364
1153
  }
1365
1154
  ];
1366
1155
  };
1156
+ const useReadVariable = (variableKey, _customScopes) => {
1157
+ const keys = variableKey && isVariableLink$1(variableKey) ? [variableKey] : [];
1158
+ const results = useReadVariables(keys);
1159
+ const result = results[0] ?? { value: null, layer: null };
1160
+ return {
1161
+ ...result,
1162
+ readVariable: () => {
1163
+ throw new Error(
1164
+ "readVariable() removed — используй useReadVariables"
1165
+ );
1166
+ },
1167
+ _dep: 0
1168
+ };
1169
+ };
1367
1170
  const useLayerChildren = (layerKey, customManager) => {
1368
1171
  const { manager: fragmentManager } = useContext(FragmentContext);
1369
1172
  const [layerData] = useGraph(customManager ?? fragmentManager, layerKey, {
@@ -1413,6 +1216,7 @@ const useLayerLink = (layerKey) => {
1413
1216
  };
1414
1217
  };
1415
1218
  const useLayerVariableValue = (layerKey, fieldKey, manager) => {
1219
+ var _a;
1416
1220
  const { manager: fragmentManager } = useContext(FragmentContext);
1417
1221
  const resultManager = manager ?? fragmentManager;
1418
1222
  const [layerValue, updateValue, layerInfo] = useLayerValue(
@@ -1420,9 +1224,11 @@ const useLayerVariableValue = (layerKey, fieldKey, manager) => {
1420
1224
  fieldKey,
1421
1225
  resultManager
1422
1226
  );
1423
- const { value: instanceValue } = useReadVariable(
1424
- (layerInfo == null ? void 0 : layerInfo.isVariable) ? layerValue : null
1227
+ const variableResults = useReadVariables(
1228
+ (layerInfo == null ? void 0 : layerInfo.isVariable) && isVariableLink$1(layerValue) ? [layerValue] : [],
1229
+ resultManager
1425
1230
  );
1231
+ const instanceValue = ((_a = variableResults[0]) == null ? void 0 : _a.value) ?? null;
1426
1232
  return [
1427
1233
  (layerInfo == null ? void 0 : layerInfo.isVariable) ? instanceValue : layerValue,
1428
1234
  updateValue,
@@ -1523,7 +1329,7 @@ const calcLayerSizeValue = (resolved, vars, sizeType, parentResolved, instanceVa
1523
1329
  };
1524
1330
  const useLayerSizeValue = (layerKey, sizeType) => {
1525
1331
  const { manager: fragmentManager } = useContext(FragmentContext);
1526
- const { layerKey: instanceLayerKey } = useContext(InstanceContext$1);
1332
+ const { layerKey: instanceLayerKey } = useContext(InstanceContext);
1527
1333
  const { isDocument } = useRenderTarget();
1528
1334
  const isTop = isTopLevel(fragmentManager, layerKey);
1529
1335
  const isPartOfInstance = !!instanceLayerKey;
@@ -1635,7 +1441,7 @@ const calcLayerPosition = (resolved, vars, parentResolved, instanceResolved, ins
1635
1441
  };
1636
1442
  };
1637
1443
  const useLayerPosition = (layerKey) => {
1638
- const { layerKey: instanceLayerKey } = useContext(InstanceContext$1);
1444
+ const { layerKey: instanceLayerKey } = useContext(InstanceContext);
1639
1445
  const { manager: fragmentManager } = useContext(FragmentContext);
1640
1446
  const isTop = isTopLevel(fragmentManager, layerKey);
1641
1447
  const { isDocument } = useRenderTarget();
@@ -1979,7 +1785,7 @@ const useLayerStyles = (layerKey) => {
1979
1785
  try {
1980
1786
  if (!layerKey) throw new Error("Empty layer key");
1981
1787
  const { manager: fragmentManager } = useContext(FragmentContext);
1982
- const { layerKey: instanceLayerKey } = useContext(InstanceContext$1);
1788
+ const { layerKey: instanceLayerKey } = useContext(InstanceContext);
1983
1789
  const { isDocument } = useRenderTarget();
1984
1790
  const resolved = useResolvedLayer(layerKey);
1985
1791
  const vars = useResolvedLayerVariables(resolved.layer, resolved.rawLayer);
@@ -2120,16 +1926,14 @@ const useTextContent = (layerKey, manager) => {
2120
1926
  fragmentManager
2121
1927
  );
2122
1928
  const variables = extractVariablesFromHtml(content);
2123
- const { readVariable } = useReadVariable();
2124
- useGraphStack(
2125
- fragmentManager,
2126
- variables.map((variable) => variable.variableKey)
2127
- );
1929
+ const variableKeys = variables.map((variable) => variable.variableKey);
1930
+ const resolvedVariables = useReadVariables(variableKeys);
2128
1931
  let nextContent = content;
2129
- variables.forEach((variable) => {
1932
+ variables.forEach((variable, index) => {
1933
+ var _a;
2130
1934
  nextContent = nextContent.replace(
2131
1935
  variable.fullMatch,
2132
- readVariable(variable.variableKey).value ?? ""
1936
+ ((_a = resolvedVariables[index]) == null ? void 0 : _a.value) ?? ""
2133
1937
  );
2134
1938
  });
2135
1939
  if (typeof nextContent === "string") {
@@ -2217,31 +2021,31 @@ const useGlobalManager = (globalManager) => {
2217
2021
  };
2218
2022
  };
2219
2023
  const useLayerInteractions = (layerKey, options) => {
2024
+ var _a;
2220
2025
  const pause = (options == null ? void 0 : options.pauseInteractions) ?? false;
2221
2026
  const { manager: globalManager } = useGlobalManager();
2222
2027
  const { manager: fragmentManager } = useContext(FragmentContext);
2223
2028
  const scopes = useContext(ScopeContext);
2224
- const areaScope = scopes.find(
2029
+ const areaScope = (_a = scopes == null ? void 0 : scopes.find) == null ? void 0 : _a.call(
2030
+ scopes,
2225
2031
  (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.AreaScope
2226
2032
  );
2227
- const instanceScope = scopes.find(
2228
- (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.InstanceScope
2229
- );
2033
+ const { onChangeProps } = useContext(InstanceContext);
2230
2034
  const [interactionLinks] = useLayerValue(layerKey, "interactions");
2231
2035
  const interactionOns = useLayersValue(interactionLinks, "on");
2232
2036
  const interactionEvents = useLayersValue(interactionLinks, "event");
2233
2037
  const interactions = (interactionLinks ?? []).map((linkKey, index) => {
2234
- var _a, _b;
2038
+ var _a2, _b;
2235
2039
  return {
2236
2040
  linkKey,
2237
- on: (_a = interactionOns == null ? void 0 : interactionOns.at(index)) == null ? void 0 : _a.at(0),
2041
+ on: (_a2 = interactionOns == null ? void 0 : interactionOns.at(index)) == null ? void 0 : _a2.at(0),
2238
2042
  event: (_b = interactionEvents == null ? void 0 : interactionEvents.at(index)) == null ? void 0 : _b.at(0)
2239
2043
  };
2240
2044
  });
2241
2045
  const fireEvent = useCallback(
2242
2046
  (eventLink) => {
2243
- var _a, _b, _c, _d, _e, _f, _g, _h;
2244
- const eventType = (_a = entityOfKey(eventLink)) == null ? void 0 : _a._type;
2047
+ var _a2, _b, _c, _d, _e, _f, _g;
2048
+ const eventType = (_a2 = entityOfKey(eventLink)) == null ? void 0 : _a2._type;
2245
2049
  const eventValue = (_b = getNormalizeLayer(eventLink, fragmentManager)) == null ? void 0 : _b.layer;
2246
2050
  console.log(scopes, eventLink, eventType, eventValue);
2247
2051
  if (eventType === definition.nodes.GoalEvent && (eventValue == null ? void 0 : eventValue.goalId)) {
@@ -2258,7 +2062,7 @@ const useLayerInteractions = (layerKey, options) => {
2258
2062
  const value = (eventValue == null ? void 0 : eventValue.value) ?? ((_f = targetLayer == null ? void 0 : targetLayer.layer) == null ? void 0 : _f.defaultValue);
2259
2063
  if (isVariableLink$1(target) && isValue(value)) {
2260
2064
  const targetId = (_g = entityOfKey(target)) == null ? void 0 : _g._id;
2261
- (_h = instanceScope == null ? void 0 : instanceScope.onChangeProps) == null ? void 0 : _h.call(instanceScope, targetId, value);
2065
+ onChangeProps == null ? void 0 : onChangeProps(targetId, value);
2262
2066
  }
2263
2067
  }
2264
2068
  },
@@ -2573,7 +2377,27 @@ const remapArrayItems = (items, mapping) => {
2573
2377
  });
2574
2378
  };
2575
2379
  const useExtractProps = (props, manager, parentManager, definitions) => {
2576
- const { readVariable, _dep } = useReadVariable();
2380
+ const arrayVariableKeys = useMemo(() => {
2381
+ if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
2382
+ const keys = [];
2383
+ for (const defKey of definitions) {
2384
+ const defVar = manager.resolve(defKey);
2385
+ if (!(defVar == null ? void 0 : defVar._id)) continue;
2386
+ if (defVar.type !== definition.variableType.Array || !defVar.definition)
2387
+ continue;
2388
+ const propValue = props[defVar._id];
2389
+ if (isVariableLink$1(propValue)) {
2390
+ keys.push(propValue);
2391
+ }
2392
+ }
2393
+ return keys;
2394
+ }, [definitions, manager, props]);
2395
+ const arrayResolvedResults = useReadVariables(arrayVariableKeys);
2396
+ const arrayResolvedByKey = {};
2397
+ arrayVariableKeys.forEach((key, i) => {
2398
+ var _a;
2399
+ arrayResolvedByKey[key] = ((_a = arrayResolvedResults[i]) == null ? void 0 : _a.value) ?? null;
2400
+ });
2577
2401
  const leafKeys = useMemo(() => {
2578
2402
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
2579
2403
  const keys = [];
@@ -2621,7 +2445,7 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2621
2445
  }
2622
2446
  }
2623
2447
  return keys;
2624
- }, [definitions, manager, parentManager, props, _dep]);
2448
+ }, [definitions, manager, parentManager, props]);
2625
2449
  const arrayItemLayers = useGraphStack(
2626
2450
  arrayItemKeys.length ? parentManager ?? manager : null,
2627
2451
  arrayItemKeys
@@ -2660,7 +2484,7 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2660
2484
  if (isVariableLink$1(propValue) && resolveManager) {
2661
2485
  const parentVar = resolveManager.resolve(propValue);
2662
2486
  if ((parentVar == null ? void 0 : parentVar.type) === definition.variableType.Array && (parentVar == null ? void 0 : parentVar.definition)) {
2663
- const { value: resolvedArray } = readVariable(propValue);
2487
+ const resolvedArray = arrayResolvedByKey[propValue];
2664
2488
  if (Array.isArray(resolvedArray)) {
2665
2489
  const concreteItems = resolveArrayItems(
2666
2490
  resolvedArray,
@@ -2702,11 +2526,11 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2702
2526
  }, [
2703
2527
  leafLayers,
2704
2528
  arrayItemLayers,
2529
+ arrayResolvedResults,
2705
2530
  props,
2706
2531
  manager,
2707
2532
  parentManager,
2708
- definitions,
2709
- _dep
2533
+ definitions
2710
2534
  ]);
2711
2535
  };
2712
2536
  const CollectionItem = ({
@@ -2893,8 +2717,10 @@ const useFragmentManager = (fragmentId, inputGlobalManager) => {
2893
2717
  } = useGlobalManager(inputGlobalManager);
2894
2718
  useGraph(globalManager, (_a = globalManager == null ? void 0 : globalManager.$fragments) == null ? void 0 : _a.key, {
2895
2719
  selector: (state) => {
2896
- var _a2;
2897
- return ((_a2 = state == null ? void 0 : state.managers) == null ? void 0 : _a2[fragmentId]) ?? null;
2720
+ return {
2721
+ ...state,
2722
+ managers: fragmentId ? pick(state.managers, fragmentId) : state.managers
2723
+ };
2898
2724
  }
2899
2725
  });
2900
2726
  const manager = getFragmentManager(fragmentId);
@@ -2912,7 +2738,7 @@ const useFragmentManager = (fragmentId, inputGlobalManager) => {
2912
2738
  };
2913
2739
  };
2914
2740
  const useFragmentChildren = (fragmentId) => {
2915
- const { layerKey: instanceLayerKey } = useContext(InstanceContext$1);
2741
+ const { layerKey: instanceLayerKey } = useContext(InstanceContext);
2916
2742
  const { manager, fragmentLayerKey } = useFragmentManager(fragmentId);
2917
2743
  const children = useLayerChildren(fragmentLayerKey, manager);
2918
2744
  const [resizeChildren, setResizeChildren] = useState(null);
@@ -2955,7 +2781,7 @@ const useFragmentCssChunks = (fragmentId) => {
2955
2781
  };
2956
2782
  const useFragment = (fragmentId, globalManager) => {
2957
2783
  var _a, _b;
2958
- const instanceContext = useContext(InstanceContext$1);
2784
+ const instanceContext = useContext(InstanceContext);
2959
2785
  const fragmentContext = useFragmentManager(fragmentId);
2960
2786
  const { setRef, children, isResize, primary } = useFragmentChildren(fragmentId);
2961
2787
  const chunkLinks = useFragmentCssChunks(fragmentId);
@@ -3054,7 +2880,64 @@ const Fragment = (props) => {
3054
2880
  }
3055
2881
  return Base;
3056
2882
  };
3057
- const resolveObjectToConcreteValue = (objectVar, manager, readVariable) => {
2883
+ const resolveFromManagers = (key, ...managers) => {
2884
+ for (const mgr of managers) {
2885
+ if (!mgr) continue;
2886
+ const layer = mgr.resolve(key, { deep: true });
2887
+ if (layer == null ? void 0 : layer.type) return { layer, manager: mgr };
2888
+ }
2889
+ return { layer: null, manager: null };
2890
+ };
2891
+ const collectAllVariableKeys = (extractProps, fragmentManager, globalManager, areaManager) => {
2892
+ const keys = [];
2893
+ const collectFromObject = (obj) => {
2894
+ for (const value of Object.values(obj)) {
2895
+ if (isVariableLink$1(value)) {
2896
+ keys.push(value);
2897
+ } else if (isObject(value)) {
2898
+ collectFromObject(value);
2899
+ }
2900
+ }
2901
+ };
2902
+ const collectObjectFields = (objectVar, manager) => {
2903
+ const fields = cleanGraph(objectVar.fields);
2904
+ for (const fieldRef of Object.values(fields)) {
2905
+ let fieldKey;
2906
+ let fieldVar;
2907
+ if (isVariableLink$1(fieldRef)) {
2908
+ fieldKey = fieldRef;
2909
+ fieldVar = manager == null ? void 0 : manager.resolve(fieldKey);
2910
+ } else if (isObject(fieldRef) && (fieldRef == null ? void 0 : fieldRef._id)) {
2911
+ fieldKey = keyOfEntity(fieldRef);
2912
+ fieldVar = fieldRef;
2913
+ }
2914
+ if (!fieldVar || !fieldKey) continue;
2915
+ if (fieldVar.type === definition.variableType.Object && fieldVar.fields) {
2916
+ collectObjectFields(fieldVar, manager);
2917
+ } else {
2918
+ keys.push(fieldKey);
2919
+ }
2920
+ }
2921
+ };
2922
+ for (const value of Object.values(extractProps)) {
2923
+ if (isVariableLink$1(value)) {
2924
+ keys.push(value);
2925
+ const { layer: varLayer, manager: varManager } = resolveFromManagers(
2926
+ value,
2927
+ fragmentManager,
2928
+ globalManager,
2929
+ areaManager
2930
+ );
2931
+ if ((varLayer == null ? void 0 : varLayer.type) === definition.variableType.Object && (varLayer == null ? void 0 : varLayer.fields)) {
2932
+ collectObjectFields(varLayer, varManager);
2933
+ }
2934
+ } else if (isObject(value)) {
2935
+ collectFromObject(value);
2936
+ }
2937
+ }
2938
+ return keys;
2939
+ };
2940
+ const resolveObjectToConcreteValueFromMap = (objectVar, manager, resolvedByKey) => {
3058
2941
  const fields = cleanGraph(objectVar.fields);
3059
2942
  const result = {};
3060
2943
  for (const [fieldName, fieldRef] of Object.entries(fields)) {
@@ -3069,13 +2952,13 @@ const resolveObjectToConcreteValue = (objectVar, manager, readVariable) => {
3069
2952
  }
3070
2953
  if (!fieldVar || !fieldKey) continue;
3071
2954
  if (fieldVar.type === definition.variableType.Object && fieldVar.fields) {
3072
- result[fieldName] = resolveObjectToConcreteValue(
2955
+ result[fieldName] = resolveObjectToConcreteValueFromMap(
3073
2956
  fieldVar,
3074
2957
  manager,
3075
- readVariable
2958
+ resolvedByKey
3076
2959
  );
3077
2960
  } else {
3078
- const { value } = readVariable(fieldKey);
2961
+ const value = resolvedByKey[fieldKey];
3079
2962
  if (value != null) {
3080
2963
  result[fieldName] = value;
3081
2964
  }
@@ -3083,12 +2966,30 @@ const resolveObjectToConcreteValue = (objectVar, manager, readVariable) => {
3083
2966
  }
3084
2967
  return result;
3085
2968
  };
2969
+ const deepResolveObjectValuesFromMap = (obj, resolvedByKey) => {
2970
+ const result = {};
2971
+ for (const [key, value] of Object.entries(obj)) {
2972
+ if (isVariableLink$1(value)) {
2973
+ result[key] = resolvedByKey[value];
2974
+ } else if (isObject(value)) {
2975
+ result[key] = deepResolveObjectValuesFromMap(
2976
+ value,
2977
+ resolvedByKey
2978
+ );
2979
+ } else {
2980
+ result[key] = value;
2981
+ }
2982
+ }
2983
+ return result;
2984
+ };
3086
2985
  const useInstanceProps = (instanceProps) => {
3087
2986
  const isTopInstance = !(instanceProps == null ? void 0 : instanceProps.layerKey);
3088
2987
  const { manager: loadedManager } = useFragmentManager(
3089
2988
  isTopInstance ? instanceProps == null ? void 0 : instanceProps.fragmentId : null
3090
2989
  );
3091
2990
  const { manager: fragmentContextManager } = useContext(FragmentContext);
2991
+ const globalManager = useContext(GlobalManagerContext);
2992
+ const areaManager = useContext(AreaManagerContext);
3092
2993
  const fragmentManager = isTopInstance ? loadedManager : fragmentContextManager;
3093
2994
  const [instanceLayer] = useGraph(fragmentManager, instanceProps.layerKey);
3094
2995
  const instanceLayerProps = (instanceLayer == null ? void 0 : instanceLayer.props) ?? {};
@@ -3101,30 +3002,52 @@ const useInstanceProps = (instanceProps) => {
3101
3002
  };
3102
3003
  }
3103
3004
  const extractProps = cleanGraph(inputProps);
3104
- const { readVariable } = useReadVariable();
3005
+ const variableKeys = !isTopInstance ? collectAllVariableKeys(
3006
+ extractProps,
3007
+ fragmentManager,
3008
+ globalManager,
3009
+ areaManager
3010
+ ) : [];
3011
+ const resolvedVariableResults = useReadVariables(variableKeys);
3012
+ const resolvedByKey = {};
3013
+ variableKeys.forEach((key, i) => {
3014
+ var _a;
3015
+ resolvedByKey[key] = ((_a = resolvedVariableResults[i]) == null ? void 0 : _a.value) ?? null;
3016
+ });
3105
3017
  let resolvedProps = extractProps;
3106
3018
  if (!isTopInstance) {
3107
3019
  resolvedProps = {};
3108
3020
  for (const [key, value] of Object.entries(extractProps)) {
3109
3021
  if (!isVariableLink$1(value)) {
3110
- resolvedProps[key] = value;
3022
+ if (isObject(value)) {
3023
+ resolvedProps[key] = deepResolveObjectValuesFromMap(
3024
+ value,
3025
+ resolvedByKey
3026
+ );
3027
+ } else {
3028
+ resolvedProps[key] = value;
3029
+ }
3111
3030
  continue;
3112
3031
  }
3113
- const varLayer = fragmentManager == null ? void 0 : fragmentManager.resolve(value);
3032
+ const { layer: varLayer, manager: varManager } = resolveFromManagers(
3033
+ value,
3034
+ fragmentManager,
3035
+ globalManager,
3036
+ areaManager
3037
+ );
3114
3038
  if ((varLayer == null ? void 0 : varLayer.type) === definition.variableType.Array) {
3115
3039
  resolvedProps[key] = value;
3116
3040
  continue;
3117
3041
  }
3118
3042
  if ((varLayer == null ? void 0 : varLayer.type) === definition.variableType.Object && (varLayer == null ? void 0 : varLayer.fields)) {
3119
- resolvedProps[key] = resolveObjectToConcreteValue(
3043
+ resolvedProps[key] = resolveObjectToConcreteValueFromMap(
3120
3044
  varLayer,
3121
- fragmentManager,
3122
- readVariable
3045
+ varManager,
3046
+ resolvedByKey
3123
3047
  );
3124
3048
  continue;
3125
3049
  }
3126
- const { value: resolved } = readVariable(value);
3127
- resolvedProps[key] = resolved;
3050
+ resolvedProps[key] = resolvedByKey[value] ?? value;
3128
3051
  }
3129
3052
  }
3130
3053
  useEffect(() => {
@@ -3185,6 +3108,45 @@ const useSuspenseLoadArea = (globalManager, areaCode, skip) => {
3185
3108
  if (valueOrPromise == null) return null;
3186
3109
  return createSuspenseResource(`area:${areaCode}`, valueOrPromise, cache).read();
3187
3110
  };
3111
+ const collectPropertyCssVars = (manager, result) => {
3112
+ if (!(manager == null ? void 0 : manager.$properties)) return;
3113
+ const keys = manager.$properties.getPropertyKeys() ?? [];
3114
+ const processField = (fieldLayer) => {
3115
+ if (!(fieldLayer == null ? void 0 : fieldLayer._id)) return;
3116
+ if (fieldLayer.type === definition.variableType.Object && fieldLayer.fields) {
3117
+ for (const [key, ref] of Object.entries(fieldLayer.fields)) {
3118
+ if (key === "_type" || key === "_id") continue;
3119
+ const child = isObject(ref) ? ref : manager.resolve(ref);
3120
+ if (child) processField(child);
3121
+ }
3122
+ return;
3123
+ }
3124
+ if (fieldLayer.defaultValue != null) {
3125
+ result[`--${fieldLayer._id}`] = String(fieldLayer.defaultValue);
3126
+ }
3127
+ };
3128
+ for (const key of keys) {
3129
+ const layer = manager.resolve(key, { deep: true });
3130
+ if (layer) processField(layer);
3131
+ }
3132
+ };
3133
+ const useManagerCssProperties = () => {
3134
+ var _a, _b, _c, _d;
3135
+ const globalManager = useContext(GlobalManagerContext);
3136
+ const areaManager = useContext(AreaManagerContext);
3137
+ useGraph(globalManager, (_a = globalManager == null ? void 0 : globalManager.$properties) == null ? void 0 : _a.key);
3138
+ useGraph(areaManager, (_b = areaManager == null ? void 0 : areaManager.$properties) == null ? void 0 : _b.key);
3139
+ const globalKeys = ((_c = globalManager == null ? void 0 : globalManager.$properties) == null ? void 0 : _c.getPropertyKeys()) ?? [];
3140
+ const areaKeys = ((_d = areaManager == null ? void 0 : areaManager.$properties) == null ? void 0 : _d.getPropertyKeys()) ?? [];
3141
+ useGraphStack(globalKeys.length ? globalManager : null, globalKeys);
3142
+ useGraphStack(areaKeys.length ? areaManager : null, areaKeys);
3143
+ return useMemo(() => {
3144
+ const vars = {};
3145
+ collectPropertyCssVars(globalManager, vars);
3146
+ collectPropertyCssVars(areaManager, vars);
3147
+ return vars;
3148
+ }, [globalManager, areaManager, globalKeys.length, areaKeys.length]);
3149
+ };
3188
3150
  const InstanceInitial = ({
3189
3151
  Tag: inputTag,
3190
3152
  style = {},
@@ -3216,71 +3178,43 @@ const InstanceInitial = ({
3216
3178
  parentManager,
3217
3179
  definitions
3218
3180
  );
3219
- const cssVars = useMemo(
3181
+ const propsCssVars = useMemo(
3220
3182
  () => toCssCustomProperties(expandedProps),
3221
3183
  [expandedProps]
3222
3184
  );
3223
- useSuspenseLoadFragment(globalManager, fragmentId, !ssr);
3224
- const scopeValue = useMemo(
3225
- () => ({
3226
- type: definition.scopeTypes.InstanceScope,
3227
- props: expandedProps,
3228
- definitions,
3229
- fragmentId,
3230
- documentManager: innerManager,
3231
- layerKey: instanceProps.layerKey,
3232
- onChangeProps
3233
- }),
3234
- [
3235
- expandedProps,
3236
- definitions,
3237
- fragmentId,
3238
- innerManager,
3239
- instanceProps.layerKey,
3240
- onChangeProps
3241
- ]
3185
+ const managerCssVars = useManagerCssProperties();
3186
+ const cssVars = useMemo(
3187
+ () => ({ ...managerCssVars, ...propsCssVars }),
3188
+ [managerCssVars, propsCssVars]
3242
3189
  );
3190
+ useSuspenseLoadFragment(globalManager, fragmentId, !ssr);
3243
3191
  const instanceContextValue = useMemo(
3244
3192
  () => ({
3245
3193
  layerKey: instanceProps.layerKey,
3246
3194
  definitions,
3247
3195
  innerManager,
3248
3196
  parentManager,
3249
- props: expandedProps
3197
+ props: expandedProps,
3198
+ onChangeProps
3250
3199
  }),
3251
3200
  [
3252
3201
  instanceProps.layerKey,
3253
3202
  definitions,
3254
3203
  innerManager,
3255
3204
  parentManager,
3256
- expandedProps
3205
+ expandedProps,
3206
+ onChangeProps
3257
3207
  ]
3258
3208
  );
3259
- return /* @__PURE__ */ jsx(
3260
- Scope,
3209
+ return /* @__PURE__ */ jsx(InstanceContext.Provider, { value: instanceContextValue, children: parentManager ? /* @__PURE__ */ jsx(
3210
+ Tag,
3261
3211
  {
3262
- fragmentManager: innerManager,
3263
- layerKey: instanceProps.layerKey,
3264
- value: scopeValue,
3265
- children: /* @__PURE__ */ jsx(InstanceContext$1.Provider, { value: instanceContextValue, children: parentManager ? /* @__PURE__ */ jsx(
3266
- Tag,
3267
- {
3268
- className: classnames,
3269
- "data-key": instanceProps.layerKey,
3270
- style: { ...style, ...cssVars },
3271
- ...link.linkProps,
3272
- ...events,
3273
- children: /* @__PURE__ */ jsx(
3274
- Fragment,
3275
- {
3276
- fragmentId,
3277
- globalManager,
3278
- FrameElement,
3279
- pauseInteractions
3280
- }
3281
- )
3282
- }
3283
- ) : /* @__PURE__ */ jsx(Tag, { style: { ...style, ...cssVars }, ...link.linkProps, children: /* @__PURE__ */ jsx(
3212
+ className: classnames,
3213
+ "data-key": instanceProps.layerKey,
3214
+ style: { ...style, ...cssVars },
3215
+ ...link.linkProps,
3216
+ ...events,
3217
+ children: /* @__PURE__ */ jsx(
3284
3218
  Fragment,
3285
3219
  {
3286
3220
  fragmentId,
@@ -3288,18 +3222,26 @@ const InstanceInitial = ({
3288
3222
  FrameElement,
3289
3223
  pauseInteractions
3290
3224
  }
3291
- ) }) })
3225
+ )
3292
3226
  }
3293
- );
3227
+ ) : /* @__PURE__ */ jsx(Tag, { style: { ...style, ...cssVars }, ...link.linkProps, children: /* @__PURE__ */ jsx(
3228
+ Fragment,
3229
+ {
3230
+ fragmentId,
3231
+ globalManager,
3232
+ FrameElement,
3233
+ pauseInteractions
3234
+ }
3235
+ ) }) });
3294
3236
  };
3295
3237
  const Instance = (props) => {
3296
- return "globalManager" in props ? /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx("h1", { children: "Test" }), children: /* @__PURE__ */ jsx(GlobalManagerProvider, { value: props.globalManager, children: /* @__PURE__ */ jsx(
3238
+ return "globalManager" in props ? /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(GlobalManagerProvider, { value: props.globalManager, children: /* @__PURE__ */ jsx(
3297
3239
  RenderTargetProvider,
3298
3240
  {
3299
3241
  value: props.renderTarget ?? definition.renderTarget.document,
3300
3242
  children: /* @__PURE__ */ jsx(InstanceInitial, { ...props })
3301
3243
  }
3302
- ) }) }) : /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx("h1", { children: "Test" }), children: /* @__PURE__ */ jsx(
3244
+ ) }) }) : /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(
3303
3245
  RenderTargetProvider,
3304
3246
  {
3305
3247
  value: props.renderTarget ?? definition.renderTarget.document,
@@ -3312,12 +3254,30 @@ const IDLE_STATE = {
3312
3254
  fetching: false,
3313
3255
  error: null
3314
3256
  };
3257
+ const resolveStateFromCache = (globalManager, areaCode, skip) => {
3258
+ if (skip || !globalManager || !areaCode) return IDLE_STATE;
3259
+ const cache = getSuspenseCache(globalManager);
3260
+ const cacheKey = `area:${areaCode}`;
3261
+ if (cache.has(cacheKey)) {
3262
+ try {
3263
+ const cached = cache.get(cacheKey).read();
3264
+ return { data: cached, fetching: false, error: null };
3265
+ } catch (e) {
3266
+ if (!(e instanceof Promise)) {
3267
+ return { data: null, fetching: false, error: e };
3268
+ }
3269
+ }
3270
+ }
3271
+ return { data: null, fetching: true, error: null };
3272
+ };
3315
3273
  const useAsyncLoadArea = (globalManager, areaCode, skip) => {
3316
- const [state, setState] = useState(IDLE_STATE);
3274
+ const [state, setState] = useState(
3275
+ () => resolveStateFromCache(globalManager, areaCode, skip)
3276
+ );
3317
3277
  const areaCodeRef = useRef(areaCode);
3318
3278
  if (areaCodeRef.current !== areaCode) {
3319
3279
  areaCodeRef.current = areaCode;
3320
- setState(IDLE_STATE);
3280
+ setState(resolveStateFromCache(globalManager, areaCode, skip));
3321
3281
  }
3322
3282
  useEffect(() => {
3323
3283
  var _a, _b;
@@ -3339,7 +3299,11 @@ const useAsyncLoadArea = (globalManager, areaCode, skip) => {
3339
3299
  const valueOrPromise = cache.has(cacheKey) ? void 0 : (_b = (_a = globalManager.$load) == null ? void 0 : _a.loadArea) == null ? void 0 : _b.call(_a, areaCode);
3340
3300
  if (valueOrPromise != null && !(valueOrPromise instanceof Promise)) {
3341
3301
  createSuspenseResource(cacheKey, valueOrPromise, cache);
3342
- setState({ data: valueOrPromise, fetching: false, error: null });
3302
+ setState({
3303
+ data: valueOrPromise,
3304
+ fetching: false,
3305
+ error: null
3306
+ });
3343
3307
  return;
3344
3308
  }
3345
3309
  if (valueOrPromise instanceof Promise) {
@@ -3489,7 +3453,7 @@ export {
3489
3453
  GlobalManagerContext,
3490
3454
  GlobalManagerProvider,
3491
3455
  Instance,
3492
- InstanceContext$1 as InstanceContext,
3456
+ InstanceContext,
3493
3457
  RenderTargetContext,
3494
3458
  RenderTargetProvider,
3495
3459
  Scope,