@fragmentsx/render-react 1.6.0 → 1.6.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"useLayerValue.d.ts","sourceRoot":"","sources":["../../../src/hooks/layer/useLayerValue.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,UAAU,EAGV,OAAO,EAER,MAAM,mBAAmB,CAAC;AAQ3B,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAI/D,MAAM,WAAW,oBAAqB,SAAQ,eAAe;IAC3D,OAAO,CAAC,EAAE,UAAU,CAAC;IACrB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;CACpB;AAED,eAAO,MAAM,aAAa,aACd,OAAO,YACP,MAAM,YACN,oBAAoB,UAkF/B,CAAC"}
1
+ {"version":3,"file":"useLayerValue.d.ts","sourceRoot":"","sources":["../../../src/hooks/layer/useLayerValue.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,UAAU,EAGV,OAAO,EAER,MAAM,mBAAmB,CAAC;AAQ3B,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAI/D,MAAM,WAAW,oBAAqB,SAAQ,eAAe;IAC3D,OAAO,CAAC,EAAE,UAAU,CAAC;IACrB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;CACpB;AAED,eAAO,MAAM,aAAa,aACd,OAAO,YACP,MAAM,YACN,oBAAoB,UAmF/B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useReadVariables.d.ts","sourceRoot":"","sources":["../../../src/hooks/layer/useReadVariables.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,UAAU,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAuBrE,eAAO,MAAM,gBAAgB,iBACb,OAAO,EAAE,kBACP,UAAU,GAAG,IAAI;;;GA2GlC,CAAC"}
1
+ {"version":3,"file":"useReadVariables.d.ts","sourceRoot":"","sources":["../../../src/hooks/layer/useReadVariables.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,UAAU,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAwBrE,eAAO,MAAM,gBAAgB,iBACb,OAAO,EAAE,kBACP,UAAU,GAAG,IAAI;;;GAoHlC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useExtractProps.d.ts","sourceRoot":"","sources":["../../../src/hooks/utils/useExtractProps.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAe,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAoMrE;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe,UACnB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,YACpB,UAAU,kBACJ,UAAU,gBACZ,OAAO,EAAE,KACtB,MAAM,CAAC,MAAM,EAAE,OAAO,CA8NxB,CAAC"}
1
+ {"version":3,"file":"useExtractProps.d.ts","sourceRoot":"","sources":["../../../src/hooks/utils/useExtractProps.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAe,OAAO,EAAE,MAAM,mBAAmB,CAAC;AA0OrE;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe,UACnB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,YACpB,UAAU,kBACJ,UAAU,gBACZ,OAAO,EAAE,KACtB,MAAM,CAAC,MAAM,EAAE,OAAO,CAwQxB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useLayerInteractions.d.ts","sourceRoot":"","sources":["../../../src/hooks/utils/useLayerInteractions.ts"],"names":[],"mappings":"AAIA,OAAO,EAAe,OAAO,EAAE,MAAM,mBAAmB,CAAC;AASzD,MAAM,WAAW,2BAA2B;IAC1C,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,eAAO,MAAM,oBAAoB,aACrB,OAAO,YACP,2BAA2B;;;;CAwFtC,CAAC"}
1
+ {"version":3,"file":"useLayerInteractions.d.ts","sourceRoot":"","sources":["../../../src/hooks/utils/useLayerInteractions.ts"],"names":[],"mappings":"AAIA,OAAO,EAAe,OAAO,EAAE,MAAM,mBAAmB,CAAC;AASzD,MAAM,WAAW,2BAA2B;IAC1C,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,eAAO,MAAM,oBAAoB,aACrB,OAAO,YACP,2BAA2B;;;;CAiGtC,CAAC"}
package/dist/index.cjs.js CHANGED
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
2
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
6
  const react = require("react");
4
7
  const definition = require("@fragmentsx/definition");
@@ -885,10 +888,14 @@ const useReadVariables = (variableKeys, customManager) => {
885
888
  const areaManager = react.useContext(AreaManagerContext);
886
889
  const scopes = react.useContext(ScopeContext);
887
890
  const resultManager = customManager ?? ((_a = react.useContext(InstanceContext)) == null ? void 0 : _a.innerManager) ?? fragmentManager;
888
- const variableLayers = react$1.useGraphStack(resultManager, filterKeys, {
889
- pause: !filterKeys.length || !resultManager,
890
- selector: (graph) => graph ? utils.pick(graph, "_id", "defaultValue", "required") : graph
891
- });
891
+ const safeFilterKeys = resultManager ? filterKeys : [];
892
+ const variableLayers = react$1.useGraphStack(
893
+ safeFilterKeys.length ? resultManager : null,
894
+ safeFilterKeys,
895
+ {
896
+ selector: (graph) => graph ? utils.pick(graph, "_id", "defaultValue", "required") : graph
897
+ }
898
+ );
892
899
  react$1.useGraph(globalManager, (_b = globalManager == null ? void 0 : globalManager.$properties) == null ? void 0 : _b.key);
893
900
  react$1.useGraph(areaManager, (_c = areaManager == null ? void 0 : areaManager.$properties) == null ? void 0 : _c.key);
894
901
  const globalPropertyKeys = react.useMemo(() => {
@@ -909,12 +916,13 @@ const useReadVariables = (variableKeys, customManager) => {
909
916
  (scope) => (scope == null ? void 0 : scope.type) === definition.definition.scopeTypes.CollectionItemScope
910
917
  );
911
918
  return filterKeys.map((variableKey, index) => {
912
- var _a2;
913
- const layer = variableLayers[index] ?? null;
919
+ var _a2, _b2;
920
+ variableLayers[index];
921
+ const layer = ((_a2 = getNormalizeLayer(variableKey, resultManager)) == null ? void 0 : _a2.layer) ?? null;
914
922
  const { _id: propertyId } = layer ?? {};
915
923
  let collectionItemProp = void 0;
916
924
  if (lastCollectionItem && propertyId) {
917
- collectionItemProp = utils.isPrimitive(lastCollectionItem == null ? void 0 : lastCollectionItem.value) && ((_a2 = core.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);
925
+ collectionItemProp = utils.isPrimitive(lastCollectionItem == null ? void 0 : lastCollectionItem.value) && ((_b2 = core.entityOfKey(lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition)) == null ? void 0 : _b2._id) === propertyId ? lastCollectionItem == null ? void 0 : lastCollectionItem.value : extractVariableValue(lastCollectionItem == null ? void 0 : lastCollectionItem.value, propertyId);
918
926
  }
919
927
  const instanceProp = (instanceProps == null ? void 0 : instanceProps[propertyId]) ?? null;
920
928
  const areaLayer = areaManager == null ? void 0 : areaManager.resolve(variableKey);
@@ -1902,6 +1910,7 @@ const useLayerStyles = (layerKey) => {
1902
1910
  return {};
1903
1911
  }
1904
1912
  };
1913
+ const MAX_NESTING_DEPTH = 5;
1905
1914
  function extractVariablesFromHtml(html) {
1906
1915
  if (!html) return [];
1907
1916
  const regex = /<span(?=\s)(?=(?:[^>]*?\s)?class="[^"]*variable[^"]*")(?=(?:[^>]*?\s)?data-type="mention")(?=(?:[^>]*?\s)?data-id="([^"]+)")[^>]*>.*?<\/span>/gi;
@@ -1920,24 +1929,75 @@ function extractVariablesFromHtml(html) {
1920
1929
  }
1921
1930
  return mentions;
1922
1931
  }
1932
+ function collectNestedVariableKeys(content, instanceProps, fragmentManager, maxDepth = MAX_NESTING_DEPTH) {
1933
+ var _a, _b;
1934
+ const allKeys = [];
1935
+ const visited = /* @__PURE__ */ new Set();
1936
+ let currentContents = [content];
1937
+ for (let depth = 0; depth < maxDepth; depth++) {
1938
+ const nextContents = [];
1939
+ for (const html of currentContents) {
1940
+ const variables = extractVariablesFromHtml(html);
1941
+ for (const variable of variables) {
1942
+ const keyStr = variable.variableKey;
1943
+ if (visited.has(keyStr)) continue;
1944
+ visited.add(keyStr);
1945
+ allKeys.push(variable.variableKey);
1946
+ const propertyId = (_a = core.entityOfKey(variable.variableKey)) == null ? void 0 : _a._id;
1947
+ const value = (instanceProps == null ? void 0 : instanceProps[propertyId]) ?? ((_b = fragmentManager == null ? void 0 : fragmentManager.resolve(variable.variableKey)) == null ? void 0 : _b.defaultValue) ?? null;
1948
+ if (typeof value === "string") {
1949
+ nextContents.push(value);
1950
+ }
1951
+ }
1952
+ }
1953
+ if (nextContents.length === 0) break;
1954
+ currentContents = nextContents;
1955
+ }
1956
+ return allKeys;
1957
+ }
1958
+ function resolveNestedContent(content, resolvedMap, maxDepth = MAX_NESTING_DEPTH) {
1959
+ let result = content;
1960
+ const visited = /* @__PURE__ */ new Set();
1961
+ for (let depth = 0; depth < maxDepth; depth++) {
1962
+ const variables = extractVariablesFromHtml(result);
1963
+ if (variables.length === 0) break;
1964
+ let hasCycle = false;
1965
+ const depthKeys = /* @__PURE__ */ new Set();
1966
+ for (const variable of variables) {
1967
+ const key = variable.variableKey;
1968
+ if (visited.has(key)) {
1969
+ console.warn(`Circular variable reference detected: ${key}`);
1970
+ hasCycle = true;
1971
+ continue;
1972
+ }
1973
+ depthKeys.add(key);
1974
+ result = result.replace(
1975
+ variable.fullMatch,
1976
+ String(resolvedMap.get(key) ?? "")
1977
+ );
1978
+ }
1979
+ for (const key of depthKeys) {
1980
+ visited.add(key);
1981
+ }
1982
+ if (hasCycle) break;
1983
+ }
1984
+ return result;
1985
+ }
1923
1986
  const useTextContent = (layerKey, manager) => {
1924
1987
  const { manager: fragmentManager } = react.useContext(FragmentContext);
1925
- const [content, , contentInfo] = useLayerValue(
1926
- layerKey,
1927
- "content",
1928
- fragmentManager
1929
- );
1930
- const variables = extractVariablesFromHtml(content);
1931
- const variableKeys = variables.map((variable) => variable.variableKey);
1932
- const resolvedVariables = useReadVariables(variableKeys);
1933
- let nextContent = content;
1934
- variables.forEach((variable, index) => {
1988
+ const { props: instanceProps } = react.useContext(InstanceContext);
1989
+ const [content] = useLayerValue(layerKey, "content", fragmentManager);
1990
+ const allVariableKeys = react.useMemo(
1991
+ () => collectNestedVariableKeys(content, instanceProps, fragmentManager),
1992
+ [content, instanceProps, fragmentManager]
1993
+ );
1994
+ const allResolved = useReadVariables(allVariableKeys);
1995
+ const resolvedMap = /* @__PURE__ */ new Map();
1996
+ allVariableKeys.forEach((key, index) => {
1935
1997
  var _a;
1936
- nextContent = nextContent.replace(
1937
- variable.fullMatch,
1938
- ((_a = resolvedVariables[index]) == null ? void 0 : _a.value) ?? ""
1939
- );
1998
+ resolvedMap.set(key, ((_a = allResolved[index]) == null ? void 0 : _a.value) ?? "");
1940
1999
  });
2000
+ let nextContent = resolveNestedContent(content, resolvedMap);
1941
2001
  if (typeof nextContent === "string") {
1942
2002
  nextContent = nextContent.replace(
1943
2003
  /\{\{(.*?)}}/,
@@ -2046,14 +2106,21 @@ const useLayerInteractions = (layerKey, options) => {
2046
2106
  });
2047
2107
  const fireEvent = react.useCallback(
2048
2108
  (eventLink) => {
2049
- var _a2, _b, _c, _d, _e, _f, _g;
2050
- const eventType = (_a2 = core.entityOfKey(eventLink)) == null ? void 0 : _a2._type;
2051
- const eventValue = (_b = getNormalizeLayer(eventLink, fragmentManager)) == null ? void 0 : _b.layer;
2052
- console.log(scopes, eventLink, eventType, eventValue);
2109
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
2110
+ const isVariableGoal = typeof eventLink === "number";
2111
+ if (isVariableGoal) {
2112
+ if (!areaScope) return null;
2113
+ (_b = (_a2 = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _a2.reachGoal) == null ? void 0 : _b.call(_a2, {
2114
+ goalId: eventLink,
2115
+ ...utils.pick(areaScope, "variantId", "campaignId", "areaId")
2116
+ });
2117
+ return;
2118
+ }
2119
+ const eventType = (_c = core.entityOfKey(eventLink)) == null ? void 0 : _c._type;
2120
+ const eventValue = (_d = getNormalizeLayer(eventLink, fragmentManager)) == null ? void 0 : _d.layer;
2053
2121
  if (eventType === definition.definition.nodes.GoalEvent && (eventValue == null ? void 0 : eventValue.goalId)) {
2054
2122
  if (!areaScope) return null;
2055
- console.log((_c = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _c.reachGoal);
2056
- (_e = (_d = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _d.reachGoal) == null ? void 0 : _e.call(_d, {
2123
+ (_f = (_e = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _e.reachGoal) == null ? void 0 : _f.call(_e, {
2057
2124
  goalId: eventValue == null ? void 0 : eventValue.goalId,
2058
2125
  ...utils.pick(areaScope, "variantId", "campaignId", "areaId")
2059
2126
  });
@@ -2061,9 +2128,9 @@ const useLayerInteractions = (layerKey, options) => {
2061
2128
  if (eventType === definition.definition.nodes.MutateEvent) {
2062
2129
  const target = eventValue == null ? void 0 : eventValue.target;
2063
2130
  const targetLayer = getNormalizeLayer(target, fragmentManager);
2064
- const value = (eventValue == null ? void 0 : eventValue.value) ?? ((_f = targetLayer == null ? void 0 : targetLayer.layer) == null ? void 0 : _f.defaultValue);
2131
+ const value = (eventValue == null ? void 0 : eventValue.value) ?? ((_g = targetLayer == null ? void 0 : targetLayer.layer) == null ? void 0 : _g.defaultValue);
2065
2132
  if (definition.isVariableLink(target) && utils.isValue(value)) {
2066
- const targetId = (_g = core.entityOfKey(target)) == null ? void 0 : _g._id;
2133
+ const targetId = (_h = core.entityOfKey(target)) == null ? void 0 : _h._id;
2067
2134
  onChangeProps == null ? void 0 : onChangeProps(targetId, value);
2068
2135
  }
2069
2136
  }
@@ -2260,7 +2327,12 @@ const useCollection = (layerKey, options) => {
2260
2327
  });
2261
2328
  const sourceValues = react$1.useGraphStack(fragmentManager, sourceValue ?? []);
2262
2329
  const resultValues = (sourceValue ?? []).map(
2263
- (initValue, index) => (sourceValues == null ? void 0 : sourceValues[index]) ?? initValue
2330
+ (initValue, index) => (
2331
+ // Если initValue — объект (пришёл из пропсов через useReadVariables),
2332
+ // он уже содержит актуальные значения и имеет приоритет.
2333
+ // Если initValue — ссылка (строка), используем graph-версию для реактивности.
2334
+ utils.isObject(initValue) ? initValue : (sourceValues == null ? void 0 : sourceValues[index]) ?? initValue
2335
+ )
2264
2336
  );
2265
2337
  return {
2266
2338
  source,
@@ -2348,6 +2420,7 @@ const buildFieldIdMapping = (childDef, parentDef, childManager, parentManager) =
2348
2420
  continue;
2349
2421
  }
2350
2422
  mapping[parentFieldVar._id] = childFieldVar._id;
2423
+ mapping[fieldName] = childFieldVar._id;
2351
2424
  }
2352
2425
  return mapping;
2353
2426
  };
@@ -2378,7 +2451,28 @@ const remapArrayItems = (items, mapping) => {
2378
2451
  return remapped;
2379
2452
  });
2380
2453
  };
2454
+ const fillArrayItemDefaults = (items, parentDef, manager) => {
2455
+ if (!(parentDef == null ? void 0 : parentDef.fields)) return items;
2456
+ const fields = utils.cleanGraph(parentDef.fields);
2457
+ return items.map((item) => {
2458
+ const filled = { ...item };
2459
+ let changed = false;
2460
+ for (const [fieldName, fieldRef] of Object.entries(fields)) {
2461
+ const fieldVar = resolveFieldRef(fieldRef, manager);
2462
+ if (!fieldVar) continue;
2463
+ if (filled[fieldName] !== void 0) continue;
2464
+ if (fieldVar._id && filled[fieldVar._id] !== void 0) continue;
2465
+ if (fieldVar.defaultValue !== void 0) {
2466
+ filled[fieldName] = fieldVar.defaultValue;
2467
+ changed = true;
2468
+ }
2469
+ }
2470
+ return changed ? filled : item;
2471
+ });
2472
+ };
2381
2473
  const useExtractProps = (props, manager, parentManager, definitions) => {
2474
+ const areaManager = react.useContext(AreaManagerContext);
2475
+ const globalManager = react.useContext(GlobalManagerContext);
2382
2476
  const arrayVariableKeys = react.useMemo(() => {
2383
2477
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
2384
2478
  const keys = [];
@@ -2397,8 +2491,9 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2397
2491
  const arrayResolvedResults = useReadVariables(arrayVariableKeys);
2398
2492
  const arrayResolvedByKey = {};
2399
2493
  arrayVariableKeys.forEach((key, i) => {
2400
- var _a;
2494
+ var _a, _b;
2401
2495
  arrayResolvedByKey[key] = ((_a = arrayResolvedResults[i]) == null ? void 0 : _a.value) ?? null;
2496
+ ((_b = arrayResolvedResults[i]) == null ? void 0 : _b.layer) ?? null;
2402
2497
  });
2403
2498
  const leafKeys = react.useMemo(() => {
2404
2499
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
@@ -2418,12 +2513,12 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2418
2513
  return keys;
2419
2514
  }, [definitions, manager]);
2420
2515
  const leafLayers = react$1.useGraphStack(leafKeys.length ? manager : null, leafKeys);
2421
- const arrayItemKeys = react.useMemo(() => {
2516
+ const { arrayItemKeys, arrayItemManager } = react.useMemo(() => {
2422
2517
  var _a;
2423
- if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
2424
- const resolveManager = parentManager ?? manager;
2425
- if (!resolveManager) return [];
2518
+ if (!manager || !(definitions == null ? void 0 : definitions.length))
2519
+ return { arrayItemKeys: [], arrayItemManager: null };
2426
2520
  const keys = [];
2521
+ let itemManager = null;
2427
2522
  for (const defKey of definitions) {
2428
2523
  const defVar = manager.resolve(defKey);
2429
2524
  if (!(defVar == null ? void 0 : defVar._id)) continue;
@@ -2431,14 +2526,17 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2431
2526
  continue;
2432
2527
  const propValue = props[defVar._id];
2433
2528
  if (!definition.isVariableLink(propValue)) continue;
2434
- const parentVar = resolveManager.resolve(propValue);
2529
+ const resolveManager = parentManager ?? manager;
2530
+ const parentVar = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ?? (areaManager == null ? void 0 : areaManager.resolve(propValue)) ?? (globalManager == null ? void 0 : globalManager.resolve(propValue));
2531
+ const ownerManager = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ? resolveManager : (areaManager == null ? void 0 : areaManager.resolve(propValue)) ? areaManager : (globalManager == null ? void 0 : globalManager.resolve(propValue)) ? globalManager : null;
2435
2532
  if ((parentVar == null ? void 0 : parentVar.type) !== definition.definition.variableType.Array) continue;
2436
2533
  const items = parentVar.defaultValue;
2437
2534
  if (!Array.isArray(items)) continue;
2535
+ if (ownerManager) itemManager = ownerManager;
2438
2536
  for (const item of items) {
2439
2537
  if (definition.isLink(item)) {
2440
2538
  keys.push(item);
2441
- const resolved = resolveManager.resolve(item);
2539
+ const resolved = ownerManager == null ? void 0 : ownerManager.resolve(item);
2442
2540
  if ((resolved == null ? void 0 : resolved.value) && utils.isObject(resolved.value) && ((_a = resolved.value) == null ? void 0 : _a._type)) {
2443
2541
  const valueKey = core.keyOfEntity(resolved.value);
2444
2542
  if (valueKey) keys.push(valueKey);
@@ -2446,13 +2544,14 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2446
2544
  }
2447
2545
  }
2448
2546
  }
2449
- return keys;
2450
- }, [definitions, manager, parentManager, props]);
2547
+ return { arrayItemKeys: keys, arrayItemManager: itemManager };
2548
+ }, [definitions, manager, parentManager, areaManager, globalManager, props]);
2451
2549
  const arrayItemLayers = react$1.useGraphStack(
2452
- arrayItemKeys.length ? parentManager ?? manager : null,
2550
+ arrayItemKeys.length ? arrayItemManager ?? parentManager ?? manager : null,
2453
2551
  arrayItemKeys
2454
2552
  );
2455
2553
  return react.useMemo(() => {
2554
+ var _a;
2456
2555
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return props;
2457
2556
  const resolveManager = parentManager ?? manager;
2458
2557
  const base = {};
@@ -2483,28 +2582,29 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2483
2582
  continue;
2484
2583
  }
2485
2584
  if (defVar.type === definition.definition.variableType.Array && defVar.definition) {
2486
- if (definition.isVariableLink(propValue) && resolveManager) {
2487
- const parentVar = resolveManager.resolve(propValue);
2585
+ if (definition.isVariableLink(propValue)) {
2586
+ const parentVar = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ?? (areaManager == null ? void 0 : areaManager.resolve(propValue)) ?? (globalManager == null ? void 0 : globalManager.resolve(propValue));
2587
+ const parentVarManager = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ? resolveManager : (areaManager == null ? void 0 : areaManager.resolve(propValue)) ? areaManager : (globalManager == null ? void 0 : globalManager.resolve(propValue)) ? globalManager : resolveManager;
2488
2588
  if ((parentVar == null ? void 0 : parentVar.type) === definition.definition.variableType.Array && (parentVar == null ? void 0 : parentVar.definition)) {
2489
2589
  const resolvedArray = arrayResolvedByKey[propValue];
2490
2590
  if (Array.isArray(resolvedArray)) {
2491
- const concreteItems = resolveArrayItems(
2492
- resolvedArray,
2493
- resolveManager
2494
- );
2591
+ const itemResolver = parentVarManager ?? resolveManager;
2592
+ const concreteItems = itemResolver ? resolveArrayItems(resolvedArray, itemResolver) : [];
2495
2593
  const childDef = resolveFieldRef(defVar.definition, manager);
2496
- const parentDef = resolveFieldRef(
2497
- parentVar.definition,
2498
- resolveManager
2499
- );
2594
+ const parentDef = parentVarManager ? resolveFieldRef(parentVar.definition, parentVarManager) : utils.isObject(parentVar.definition) && ((_a = parentVar.definition) == null ? void 0 : _a._id) ? parentVar.definition : null;
2500
2595
  if ((childDef == null ? void 0 : childDef.type) === definition.definition.variableType.Object && (childDef == null ? void 0 : childDef.fields) && (parentDef == null ? void 0 : parentDef.type) === definition.definition.variableType.Object && (parentDef == null ? void 0 : parentDef.fields)) {
2596
+ const filledItems = fillArrayItemDefaults(
2597
+ concreteItems,
2598
+ parentDef,
2599
+ parentVarManager ?? resolveManager
2600
+ );
2501
2601
  const mapping = buildFieldIdMapping(
2502
2602
  childDef,
2503
2603
  parentDef,
2504
2604
  manager,
2505
- resolveManager
2605
+ parentVarManager ?? resolveManager
2506
2606
  );
2507
- expanded[defVar._id] = remapArrayItems(concreteItems, mapping);
2607
+ expanded[defVar._id] = remapArrayItems(filledItems, mapping);
2508
2608
  } else {
2509
2609
  expanded[defVar._id] = concreteItems;
2510
2610
  }
@@ -2532,6 +2632,8 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2532
2632
  props,
2533
2633
  manager,
2534
2634
  parentManager,
2635
+ areaManager,
2636
+ globalManager,
2535
2637
  definitions
2536
2638
  ]);
2537
2639
  };
@@ -3377,6 +3479,24 @@ const useArea = (areaCode, options) => {
3377
3479
  }
3378
3480
  return asyncState;
3379
3481
  };
3482
+ class AreaErrorBoundary extends react.Component {
3483
+ constructor() {
3484
+ super(...arguments);
3485
+ __publicField(this, "state", { hasError: false });
3486
+ }
3487
+ static getDerivedStateFromError() {
3488
+ return { hasError: true };
3489
+ }
3490
+ componentDidCatch(error, info) {
3491
+ if (process.env.NODE_ENV !== "production") {
3492
+ console.error("[fragments] Area error:", error, info.componentStack);
3493
+ }
3494
+ }
3495
+ render() {
3496
+ if (this.state.hasError) return null;
3497
+ return this.props.children;
3498
+ }
3499
+ }
3380
3500
  const AreaInitial = (areaProps) => {
3381
3501
  var _a;
3382
3502
  const { data: areaData } = useArea(areaProps.areaCode, {
@@ -3391,20 +3511,23 @@ const AreaInitial = (areaProps) => {
3391
3511
  areaData == null ? void 0 : areaData.fragmentId,
3392
3512
  globalManager
3393
3513
  );
3394
- const resultProps = react.useMemo(
3395
- () => ({ ...(areaData == null ? void 0 : areaData.props) ?? {}, ...(areaProps == null ? void 0 : areaProps.props) ?? {} }),
3396
- [areaData, areaProps]
3397
- );
3398
- if (!areaData) return null;
3514
+ const resultProps = react.useMemo(() => {
3515
+ const baseProps = (areaData == null ? void 0 : areaData.props) ?? {};
3516
+ if (typeof (areaProps == null ? void 0 : areaProps.props) === "function") {
3517
+ return { ...baseProps, ...areaProps.props(baseProps) };
3518
+ }
3519
+ return { ...baseProps, ...(areaProps == null ? void 0 : areaProps.props) ?? {} };
3520
+ }, [areaData, areaProps]);
3399
3521
  const areaManager = react.useMemo(
3400
3522
  () => clientCore.createAreaManager({
3401
- areaId: areaData.areaId,
3402
- campaignId: areaData.campaignId,
3403
- variantId: areaData.variantId,
3404
- areaProperties: areaData.areaProperties
3523
+ areaId: areaData == null ? void 0 : areaData.areaId,
3524
+ campaignId: areaData == null ? void 0 : areaData.campaignId,
3525
+ variantId: areaData == null ? void 0 : areaData.variantId,
3526
+ areaProperties: areaData == null ? void 0 : areaData.areaProperties
3405
3527
  }),
3406
- [areaData.areaId]
3528
+ [areaData == null ? void 0 : areaData.areaId]
3407
3529
  );
3530
+ if (!areaData) return null;
3408
3531
  return /* @__PURE__ */ jsxRuntime.jsx(AreaManagerProvider, { value: areaManager, children: /* @__PURE__ */ jsxRuntime.jsx(
3409
3532
  Scope,
3410
3533
  {
@@ -3420,7 +3543,7 @@ const AreaInitial = (areaProps) => {
3420
3543
  ) });
3421
3544
  };
3422
3545
  const Area = (props) => {
3423
- return "globalManager" in props ? /* @__PURE__ */ jsxRuntime.jsx(react.Suspense, { fallback: null, children: /* @__PURE__ */ jsxRuntime.jsx(GlobalManagerProvider, { value: props.globalManager, children: /* @__PURE__ */ jsxRuntime.jsx(AreaInitial, { ...props }) }) }) : /* @__PURE__ */ jsxRuntime.jsx(react.Suspense, { fallback: null, children: /* @__PURE__ */ jsxRuntime.jsx(AreaInitial, { ...props }) });
3546
+ return "globalManager" in props ? /* @__PURE__ */ jsxRuntime.jsx(react.Suspense, { fallback: null, children: /* @__PURE__ */ jsxRuntime.jsx(GlobalManagerProvider, { value: props.globalManager, children: /* @__PURE__ */ jsxRuntime.jsx(AreaErrorBoundary, { children: /* @__PURE__ */ jsxRuntime.jsx(AreaInitial, { ...props }) }) }) }) : /* @__PURE__ */ jsxRuntime.jsx(react.Suspense, { fallback: null, children: /* @__PURE__ */ jsxRuntime.jsx(AreaErrorBoundary, { children: /* @__PURE__ */ jsxRuntime.jsx(AreaInitial, { ...props }) }) });
3424
3547
  };
3425
3548
  function cssToJsStyle(cssKey) {
3426
3549
  return cssKey.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
package/dist/index.es.js CHANGED
@@ -1,4 +1,7 @@
1
- import { createContext, useContext, useMemo, useCallback, useEffect, useState, useRef, memo, Suspense } from "react";
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { createContext, useContext, useMemo, useCallback, useEffect, useState, useRef, memo, Suspense, Component } from "react";
2
5
  import { definition, getNormalizeLayer as getNormalizeLayer$1, isVariableLink as isVariableLink$1, parseLayerField, isLink as isLink$1 } from "@fragmentsx/definition";
3
6
  import { jsx, jsxs } from "react/jsx-runtime";
4
7
  import { useGraph, useGraphStack } from "@graph-state/react";
@@ -883,10 +886,14 @@ const useReadVariables = (variableKeys, customManager) => {
883
886
  const areaManager = useContext(AreaManagerContext);
884
887
  const scopes = useContext(ScopeContext);
885
888
  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
- });
889
+ const safeFilterKeys = resultManager ? filterKeys : [];
890
+ const variableLayers = useGraphStack(
891
+ safeFilterKeys.length ? resultManager : null,
892
+ safeFilterKeys,
893
+ {
894
+ selector: (graph) => graph ? pick(graph, "_id", "defaultValue", "required") : graph
895
+ }
896
+ );
890
897
  useGraph(globalManager, (_b = globalManager == null ? void 0 : globalManager.$properties) == null ? void 0 : _b.key);
891
898
  useGraph(areaManager, (_c = areaManager == null ? void 0 : areaManager.$properties) == null ? void 0 : _c.key);
892
899
  const globalPropertyKeys = useMemo(() => {
@@ -907,12 +914,13 @@ const useReadVariables = (variableKeys, customManager) => {
907
914
  (scope) => (scope == null ? void 0 : scope.type) === definition.scopeTypes.CollectionItemScope
908
915
  );
909
916
  return filterKeys.map((variableKey, index) => {
910
- var _a2;
911
- const layer = variableLayers[index] ?? null;
917
+ var _a2, _b2;
918
+ variableLayers[index];
919
+ const layer = ((_a2 = getNormalizeLayer(variableKey, resultManager)) == null ? void 0 : _a2.layer) ?? null;
912
920
  const { _id: propertyId } = layer ?? {};
913
921
  let collectionItemProp = void 0;
914
922
  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);
923
+ collectionItemProp = isPrimitive(lastCollectionItem == null ? void 0 : lastCollectionItem.value) && ((_b2 = entityOfKey(lastCollectionItem == null ? void 0 : lastCollectionItem.sourceDefinition)) == null ? void 0 : _b2._id) === propertyId ? lastCollectionItem == null ? void 0 : lastCollectionItem.value : extractVariableValue(lastCollectionItem == null ? void 0 : lastCollectionItem.value, propertyId);
916
924
  }
917
925
  const instanceProp = (instanceProps == null ? void 0 : instanceProps[propertyId]) ?? null;
918
926
  const areaLayer = areaManager == null ? void 0 : areaManager.resolve(variableKey);
@@ -1900,6 +1908,7 @@ const useLayerStyles = (layerKey) => {
1900
1908
  return {};
1901
1909
  }
1902
1910
  };
1911
+ const MAX_NESTING_DEPTH = 5;
1903
1912
  function extractVariablesFromHtml(html) {
1904
1913
  if (!html) return [];
1905
1914
  const regex = /<span(?=\s)(?=(?:[^>]*?\s)?class="[^"]*variable[^"]*")(?=(?:[^>]*?\s)?data-type="mention")(?=(?:[^>]*?\s)?data-id="([^"]+)")[^>]*>.*?<\/span>/gi;
@@ -1918,24 +1927,75 @@ function extractVariablesFromHtml(html) {
1918
1927
  }
1919
1928
  return mentions;
1920
1929
  }
1930
+ function collectNestedVariableKeys(content, instanceProps, fragmentManager, maxDepth = MAX_NESTING_DEPTH) {
1931
+ var _a, _b;
1932
+ const allKeys = [];
1933
+ const visited = /* @__PURE__ */ new Set();
1934
+ let currentContents = [content];
1935
+ for (let depth = 0; depth < maxDepth; depth++) {
1936
+ const nextContents = [];
1937
+ for (const html of currentContents) {
1938
+ const variables = extractVariablesFromHtml(html);
1939
+ for (const variable of variables) {
1940
+ const keyStr = variable.variableKey;
1941
+ if (visited.has(keyStr)) continue;
1942
+ visited.add(keyStr);
1943
+ allKeys.push(variable.variableKey);
1944
+ const propertyId = (_a = entityOfKey(variable.variableKey)) == null ? void 0 : _a._id;
1945
+ const value = (instanceProps == null ? void 0 : instanceProps[propertyId]) ?? ((_b = fragmentManager == null ? void 0 : fragmentManager.resolve(variable.variableKey)) == null ? void 0 : _b.defaultValue) ?? null;
1946
+ if (typeof value === "string") {
1947
+ nextContents.push(value);
1948
+ }
1949
+ }
1950
+ }
1951
+ if (nextContents.length === 0) break;
1952
+ currentContents = nextContents;
1953
+ }
1954
+ return allKeys;
1955
+ }
1956
+ function resolveNestedContent(content, resolvedMap, maxDepth = MAX_NESTING_DEPTH) {
1957
+ let result = content;
1958
+ const visited = /* @__PURE__ */ new Set();
1959
+ for (let depth = 0; depth < maxDepth; depth++) {
1960
+ const variables = extractVariablesFromHtml(result);
1961
+ if (variables.length === 0) break;
1962
+ let hasCycle = false;
1963
+ const depthKeys = /* @__PURE__ */ new Set();
1964
+ for (const variable of variables) {
1965
+ const key = variable.variableKey;
1966
+ if (visited.has(key)) {
1967
+ console.warn(`Circular variable reference detected: ${key}`);
1968
+ hasCycle = true;
1969
+ continue;
1970
+ }
1971
+ depthKeys.add(key);
1972
+ result = result.replace(
1973
+ variable.fullMatch,
1974
+ String(resolvedMap.get(key) ?? "")
1975
+ );
1976
+ }
1977
+ for (const key of depthKeys) {
1978
+ visited.add(key);
1979
+ }
1980
+ if (hasCycle) break;
1981
+ }
1982
+ return result;
1983
+ }
1921
1984
  const useTextContent = (layerKey, manager) => {
1922
1985
  const { manager: fragmentManager } = useContext(FragmentContext);
1923
- const [content, , contentInfo] = useLayerValue(
1924
- layerKey,
1925
- "content",
1926
- fragmentManager
1927
- );
1928
- const variables = extractVariablesFromHtml(content);
1929
- const variableKeys = variables.map((variable) => variable.variableKey);
1930
- const resolvedVariables = useReadVariables(variableKeys);
1931
- let nextContent = content;
1932
- variables.forEach((variable, index) => {
1986
+ const { props: instanceProps } = useContext(InstanceContext);
1987
+ const [content] = useLayerValue(layerKey, "content", fragmentManager);
1988
+ const allVariableKeys = useMemo(
1989
+ () => collectNestedVariableKeys(content, instanceProps, fragmentManager),
1990
+ [content, instanceProps, fragmentManager]
1991
+ );
1992
+ const allResolved = useReadVariables(allVariableKeys);
1993
+ const resolvedMap = /* @__PURE__ */ new Map();
1994
+ allVariableKeys.forEach((key, index) => {
1933
1995
  var _a;
1934
- nextContent = nextContent.replace(
1935
- variable.fullMatch,
1936
- ((_a = resolvedVariables[index]) == null ? void 0 : _a.value) ?? ""
1937
- );
1996
+ resolvedMap.set(key, ((_a = allResolved[index]) == null ? void 0 : _a.value) ?? "");
1938
1997
  });
1998
+ let nextContent = resolveNestedContent(content, resolvedMap);
1939
1999
  if (typeof nextContent === "string") {
1940
2000
  nextContent = nextContent.replace(
1941
2001
  /\{\{(.*?)}}/,
@@ -2044,14 +2104,21 @@ const useLayerInteractions = (layerKey, options) => {
2044
2104
  });
2045
2105
  const fireEvent = useCallback(
2046
2106
  (eventLink) => {
2047
- var _a2, _b, _c, _d, _e, _f, _g;
2048
- const eventType = (_a2 = entityOfKey(eventLink)) == null ? void 0 : _a2._type;
2049
- const eventValue = (_b = getNormalizeLayer(eventLink, fragmentManager)) == null ? void 0 : _b.layer;
2050
- console.log(scopes, eventLink, eventType, eventValue);
2107
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
2108
+ const isVariableGoal = typeof eventLink === "number";
2109
+ if (isVariableGoal) {
2110
+ if (!areaScope) return null;
2111
+ (_b = (_a2 = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _a2.reachGoal) == null ? void 0 : _b.call(_a2, {
2112
+ goalId: eventLink,
2113
+ ...pick(areaScope, "variantId", "campaignId", "areaId")
2114
+ });
2115
+ return;
2116
+ }
2117
+ const eventType = (_c = entityOfKey(eventLink)) == null ? void 0 : _c._type;
2118
+ const eventValue = (_d = getNormalizeLayer(eventLink, fragmentManager)) == null ? void 0 : _d.layer;
2051
2119
  if (eventType === definition.nodes.GoalEvent && (eventValue == null ? void 0 : eventValue.goalId)) {
2052
2120
  if (!areaScope) return null;
2053
- console.log((_c = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _c.reachGoal);
2054
- (_e = (_d = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _d.reachGoal) == null ? void 0 : _e.call(_d, {
2121
+ (_f = (_e = globalManager == null ? void 0 : globalManager.$metrics) == null ? void 0 : _e.reachGoal) == null ? void 0 : _f.call(_e, {
2055
2122
  goalId: eventValue == null ? void 0 : eventValue.goalId,
2056
2123
  ...pick(areaScope, "variantId", "campaignId", "areaId")
2057
2124
  });
@@ -2059,9 +2126,9 @@ const useLayerInteractions = (layerKey, options) => {
2059
2126
  if (eventType === definition.nodes.MutateEvent) {
2060
2127
  const target = eventValue == null ? void 0 : eventValue.target;
2061
2128
  const targetLayer = getNormalizeLayer(target, fragmentManager);
2062
- const value = (eventValue == null ? void 0 : eventValue.value) ?? ((_f = targetLayer == null ? void 0 : targetLayer.layer) == null ? void 0 : _f.defaultValue);
2129
+ const value = (eventValue == null ? void 0 : eventValue.value) ?? ((_g = targetLayer == null ? void 0 : targetLayer.layer) == null ? void 0 : _g.defaultValue);
2063
2130
  if (isVariableLink$1(target) && isValue(value)) {
2064
- const targetId = (_g = entityOfKey(target)) == null ? void 0 : _g._id;
2131
+ const targetId = (_h = entityOfKey(target)) == null ? void 0 : _h._id;
2065
2132
  onChangeProps == null ? void 0 : onChangeProps(targetId, value);
2066
2133
  }
2067
2134
  }
@@ -2258,7 +2325,12 @@ const useCollection = (layerKey, options) => {
2258
2325
  });
2259
2326
  const sourceValues = useGraphStack(fragmentManager, sourceValue ?? []);
2260
2327
  const resultValues = (sourceValue ?? []).map(
2261
- (initValue, index) => (sourceValues == null ? void 0 : sourceValues[index]) ?? initValue
2328
+ (initValue, index) => (
2329
+ // Если initValue — объект (пришёл из пропсов через useReadVariables),
2330
+ // он уже содержит актуальные значения и имеет приоритет.
2331
+ // Если initValue — ссылка (строка), используем graph-версию для реактивности.
2332
+ isObject(initValue) ? initValue : (sourceValues == null ? void 0 : sourceValues[index]) ?? initValue
2333
+ )
2262
2334
  );
2263
2335
  return {
2264
2336
  source,
@@ -2346,6 +2418,7 @@ const buildFieldIdMapping = (childDef, parentDef, childManager, parentManager) =
2346
2418
  continue;
2347
2419
  }
2348
2420
  mapping[parentFieldVar._id] = childFieldVar._id;
2421
+ mapping[fieldName] = childFieldVar._id;
2349
2422
  }
2350
2423
  return mapping;
2351
2424
  };
@@ -2376,7 +2449,28 @@ const remapArrayItems = (items, mapping) => {
2376
2449
  return remapped;
2377
2450
  });
2378
2451
  };
2452
+ const fillArrayItemDefaults = (items, parentDef, manager) => {
2453
+ if (!(parentDef == null ? void 0 : parentDef.fields)) return items;
2454
+ const fields = cleanGraph(parentDef.fields);
2455
+ return items.map((item) => {
2456
+ const filled = { ...item };
2457
+ let changed = false;
2458
+ for (const [fieldName, fieldRef] of Object.entries(fields)) {
2459
+ const fieldVar = resolveFieldRef(fieldRef, manager);
2460
+ if (!fieldVar) continue;
2461
+ if (filled[fieldName] !== void 0) continue;
2462
+ if (fieldVar._id && filled[fieldVar._id] !== void 0) continue;
2463
+ if (fieldVar.defaultValue !== void 0) {
2464
+ filled[fieldName] = fieldVar.defaultValue;
2465
+ changed = true;
2466
+ }
2467
+ }
2468
+ return changed ? filled : item;
2469
+ });
2470
+ };
2379
2471
  const useExtractProps = (props, manager, parentManager, definitions) => {
2472
+ const areaManager = useContext(AreaManagerContext);
2473
+ const globalManager = useContext(GlobalManagerContext);
2380
2474
  const arrayVariableKeys = useMemo(() => {
2381
2475
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
2382
2476
  const keys = [];
@@ -2395,8 +2489,9 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2395
2489
  const arrayResolvedResults = useReadVariables(arrayVariableKeys);
2396
2490
  const arrayResolvedByKey = {};
2397
2491
  arrayVariableKeys.forEach((key, i) => {
2398
- var _a;
2492
+ var _a, _b;
2399
2493
  arrayResolvedByKey[key] = ((_a = arrayResolvedResults[i]) == null ? void 0 : _a.value) ?? null;
2494
+ ((_b = arrayResolvedResults[i]) == null ? void 0 : _b.layer) ?? null;
2400
2495
  });
2401
2496
  const leafKeys = useMemo(() => {
2402
2497
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
@@ -2416,12 +2511,12 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2416
2511
  return keys;
2417
2512
  }, [definitions, manager]);
2418
2513
  const leafLayers = useGraphStack(leafKeys.length ? manager : null, leafKeys);
2419
- const arrayItemKeys = useMemo(() => {
2514
+ const { arrayItemKeys, arrayItemManager } = useMemo(() => {
2420
2515
  var _a;
2421
- if (!manager || !(definitions == null ? void 0 : definitions.length)) return [];
2422
- const resolveManager = parentManager ?? manager;
2423
- if (!resolveManager) return [];
2516
+ if (!manager || !(definitions == null ? void 0 : definitions.length))
2517
+ return { arrayItemKeys: [], arrayItemManager: null };
2424
2518
  const keys = [];
2519
+ let itemManager = null;
2425
2520
  for (const defKey of definitions) {
2426
2521
  const defVar = manager.resolve(defKey);
2427
2522
  if (!(defVar == null ? void 0 : defVar._id)) continue;
@@ -2429,14 +2524,17 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2429
2524
  continue;
2430
2525
  const propValue = props[defVar._id];
2431
2526
  if (!isVariableLink$1(propValue)) continue;
2432
- const parentVar = resolveManager.resolve(propValue);
2527
+ const resolveManager = parentManager ?? manager;
2528
+ const parentVar = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ?? (areaManager == null ? void 0 : areaManager.resolve(propValue)) ?? (globalManager == null ? void 0 : globalManager.resolve(propValue));
2529
+ const ownerManager = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ? resolveManager : (areaManager == null ? void 0 : areaManager.resolve(propValue)) ? areaManager : (globalManager == null ? void 0 : globalManager.resolve(propValue)) ? globalManager : null;
2433
2530
  if ((parentVar == null ? void 0 : parentVar.type) !== definition.variableType.Array) continue;
2434
2531
  const items = parentVar.defaultValue;
2435
2532
  if (!Array.isArray(items)) continue;
2533
+ if (ownerManager) itemManager = ownerManager;
2436
2534
  for (const item of items) {
2437
2535
  if (isLink$1(item)) {
2438
2536
  keys.push(item);
2439
- const resolved = resolveManager.resolve(item);
2537
+ const resolved = ownerManager == null ? void 0 : ownerManager.resolve(item);
2440
2538
  if ((resolved == null ? void 0 : resolved.value) && isObject(resolved.value) && ((_a = resolved.value) == null ? void 0 : _a._type)) {
2441
2539
  const valueKey = keyOfEntity(resolved.value);
2442
2540
  if (valueKey) keys.push(valueKey);
@@ -2444,13 +2542,14 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2444
2542
  }
2445
2543
  }
2446
2544
  }
2447
- return keys;
2448
- }, [definitions, manager, parentManager, props]);
2545
+ return { arrayItemKeys: keys, arrayItemManager: itemManager };
2546
+ }, [definitions, manager, parentManager, areaManager, globalManager, props]);
2449
2547
  const arrayItemLayers = useGraphStack(
2450
- arrayItemKeys.length ? parentManager ?? manager : null,
2548
+ arrayItemKeys.length ? arrayItemManager ?? parentManager ?? manager : null,
2451
2549
  arrayItemKeys
2452
2550
  );
2453
2551
  return useMemo(() => {
2552
+ var _a;
2454
2553
  if (!manager || !(definitions == null ? void 0 : definitions.length)) return props;
2455
2554
  const resolveManager = parentManager ?? manager;
2456
2555
  const base = {};
@@ -2481,28 +2580,29 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2481
2580
  continue;
2482
2581
  }
2483
2582
  if (defVar.type === definition.variableType.Array && defVar.definition) {
2484
- if (isVariableLink$1(propValue) && resolveManager) {
2485
- const parentVar = resolveManager.resolve(propValue);
2583
+ if (isVariableLink$1(propValue)) {
2584
+ const parentVar = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ?? (areaManager == null ? void 0 : areaManager.resolve(propValue)) ?? (globalManager == null ? void 0 : globalManager.resolve(propValue));
2585
+ const parentVarManager = (resolveManager == null ? void 0 : resolveManager.resolve(propValue)) ? resolveManager : (areaManager == null ? void 0 : areaManager.resolve(propValue)) ? areaManager : (globalManager == null ? void 0 : globalManager.resolve(propValue)) ? globalManager : resolveManager;
2486
2586
  if ((parentVar == null ? void 0 : parentVar.type) === definition.variableType.Array && (parentVar == null ? void 0 : parentVar.definition)) {
2487
2587
  const resolvedArray = arrayResolvedByKey[propValue];
2488
2588
  if (Array.isArray(resolvedArray)) {
2489
- const concreteItems = resolveArrayItems(
2490
- resolvedArray,
2491
- resolveManager
2492
- );
2589
+ const itemResolver = parentVarManager ?? resolveManager;
2590
+ const concreteItems = itemResolver ? resolveArrayItems(resolvedArray, itemResolver) : [];
2493
2591
  const childDef = resolveFieldRef(defVar.definition, manager);
2494
- const parentDef = resolveFieldRef(
2495
- parentVar.definition,
2496
- resolveManager
2497
- );
2592
+ const parentDef = parentVarManager ? resolveFieldRef(parentVar.definition, parentVarManager) : isObject(parentVar.definition) && ((_a = parentVar.definition) == null ? void 0 : _a._id) ? parentVar.definition : null;
2498
2593
  if ((childDef == null ? void 0 : childDef.type) === definition.variableType.Object && (childDef == null ? void 0 : childDef.fields) && (parentDef == null ? void 0 : parentDef.type) === definition.variableType.Object && (parentDef == null ? void 0 : parentDef.fields)) {
2594
+ const filledItems = fillArrayItemDefaults(
2595
+ concreteItems,
2596
+ parentDef,
2597
+ parentVarManager ?? resolveManager
2598
+ );
2499
2599
  const mapping = buildFieldIdMapping(
2500
2600
  childDef,
2501
2601
  parentDef,
2502
2602
  manager,
2503
- resolveManager
2603
+ parentVarManager ?? resolveManager
2504
2604
  );
2505
- expanded[defVar._id] = remapArrayItems(concreteItems, mapping);
2605
+ expanded[defVar._id] = remapArrayItems(filledItems, mapping);
2506
2606
  } else {
2507
2607
  expanded[defVar._id] = concreteItems;
2508
2608
  }
@@ -2530,6 +2630,8 @@ const useExtractProps = (props, manager, parentManager, definitions) => {
2530
2630
  props,
2531
2631
  manager,
2532
2632
  parentManager,
2633
+ areaManager,
2634
+ globalManager,
2533
2635
  definitions
2534
2636
  ]);
2535
2637
  };
@@ -3375,6 +3477,24 @@ const useArea = (areaCode, options) => {
3375
3477
  }
3376
3478
  return asyncState;
3377
3479
  };
3480
+ class AreaErrorBoundary extends Component {
3481
+ constructor() {
3482
+ super(...arguments);
3483
+ __publicField(this, "state", { hasError: false });
3484
+ }
3485
+ static getDerivedStateFromError() {
3486
+ return { hasError: true };
3487
+ }
3488
+ componentDidCatch(error, info) {
3489
+ if (process.env.NODE_ENV !== "production") {
3490
+ console.error("[fragments] Area error:", error, info.componentStack);
3491
+ }
3492
+ }
3493
+ render() {
3494
+ if (this.state.hasError) return null;
3495
+ return this.props.children;
3496
+ }
3497
+ }
3378
3498
  const AreaInitial = (areaProps) => {
3379
3499
  var _a;
3380
3500
  const { data: areaData } = useArea(areaProps.areaCode, {
@@ -3389,20 +3509,23 @@ const AreaInitial = (areaProps) => {
3389
3509
  areaData == null ? void 0 : areaData.fragmentId,
3390
3510
  globalManager
3391
3511
  );
3392
- const resultProps = useMemo(
3393
- () => ({ ...(areaData == null ? void 0 : areaData.props) ?? {}, ...(areaProps == null ? void 0 : areaProps.props) ?? {} }),
3394
- [areaData, areaProps]
3395
- );
3396
- if (!areaData) return null;
3512
+ const resultProps = useMemo(() => {
3513
+ const baseProps = (areaData == null ? void 0 : areaData.props) ?? {};
3514
+ if (typeof (areaProps == null ? void 0 : areaProps.props) === "function") {
3515
+ return { ...baseProps, ...areaProps.props(baseProps) };
3516
+ }
3517
+ return { ...baseProps, ...(areaProps == null ? void 0 : areaProps.props) ?? {} };
3518
+ }, [areaData, areaProps]);
3397
3519
  const areaManager = useMemo(
3398
3520
  () => createAreaManager({
3399
- areaId: areaData.areaId,
3400
- campaignId: areaData.campaignId,
3401
- variantId: areaData.variantId,
3402
- areaProperties: areaData.areaProperties
3521
+ areaId: areaData == null ? void 0 : areaData.areaId,
3522
+ campaignId: areaData == null ? void 0 : areaData.campaignId,
3523
+ variantId: areaData == null ? void 0 : areaData.variantId,
3524
+ areaProperties: areaData == null ? void 0 : areaData.areaProperties
3403
3525
  }),
3404
- [areaData.areaId]
3526
+ [areaData == null ? void 0 : areaData.areaId]
3405
3527
  );
3528
+ if (!areaData) return null;
3406
3529
  return /* @__PURE__ */ jsx(AreaManagerProvider, { value: areaManager, children: /* @__PURE__ */ jsx(
3407
3530
  Scope,
3408
3531
  {
@@ -3418,7 +3541,7 @@ const AreaInitial = (areaProps) => {
3418
3541
  ) });
3419
3542
  };
3420
3543
  const Area = (props) => {
3421
- return "globalManager" in props ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(GlobalManagerProvider, { value: props.globalManager, children: /* @__PURE__ */ jsx(AreaInitial, { ...props }) }) }) : /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(AreaInitial, { ...props }) });
3544
+ return "globalManager" in props ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(GlobalManagerProvider, { value: props.globalManager, children: /* @__PURE__ */ jsx(AreaErrorBoundary, { children: /* @__PURE__ */ jsx(AreaInitial, { ...props }) }) }) }) : /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(AreaErrorBoundary, { children: /* @__PURE__ */ jsx(AreaInitial, { ...props }) }) });
3422
3545
  };
3423
3546
  function cssToJsStyle(cssKey) {
3424
3547
  return cssKey.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
@@ -1,7 +1,8 @@
1
+ export type AreaPropsFn = (variantProps: Record<string, unknown>) => Record<string, unknown>;
1
2
  export interface AreaProps {
2
3
  areaCode: string;
3
4
  globalManager?: unknown;
4
- props?: Record<string, unknown>;
5
+ props?: Record<string, unknown> | AreaPropsFn;
5
6
  options?: {
6
7
  ssr?: boolean;
7
8
  };
@@ -1 +1 @@
1
- {"version":3,"file":"Area.d.ts","sourceRoot":"","sources":["../../../src/nodes/Area/Area.tsx"],"names":[],"mappings":"AAUA,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE;QACR,GAAG,CAAC,EAAE,OAAO,CAAC;KACf,CAAC;CACH;AAqDD,eAAO,MAAM,IAAI,UAAW,SAAS,4CAYpC,CAAC"}
1
+ {"version":3,"file":"Area.d.ts","sourceRoot":"","sources":["../../../src/nodes/Area/Area.tsx"],"names":[],"mappings":"AAuCA,MAAM,MAAM,WAAW,GAAG,CACxB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAClC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE7B,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,CAAC;IAC9C,OAAO,CAAC,EAAE;QACR,GAAG,CAAC,EAAE,OAAO,CAAC;KACf,CAAC;CACH;AAwDD,eAAO,MAAM,IAAI,UAAW,SAAS,4CAgBpC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Area.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Area.test.d.ts","sourceRoot":"","sources":["../../../src/nodes/Area/Area.test.tsx"],"names":[],"mappings":""}
@@ -1,5 +1,5 @@
1
1
  export { Area } from './Area';
2
- export type { AreaProps } from './Area';
2
+ export type { AreaProps, AreaPropsFn } from './Area';
3
3
  export { useArea } from './hooks/useArea';
4
4
  export type { UseAreaOptions, UseAreaResult, AreaData, } from './hooks/useArea.types';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/nodes/Area/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,YAAY,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,YAAY,EACV,cAAc,EACd,aAAa,EACb,QAAQ,GACT,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/nodes/Area/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,YAAY,EACV,cAAc,EACd,aAAa,EACb,QAAQ,GACT,MAAM,uBAAuB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useCollection.d.ts","sourceRoot":"","sources":["../../../../src/nodes/Collection/hooks/useCollection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C,OAAO,EAAY,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAKzE,eAAO,MAAM,aAAa,aAAc,OAAO,YAAY,eAAe;;;;;;;;;;;;;;;;;;;;;;CA2BzE,CAAC"}
1
+ {"version":3,"file":"useCollection.d.ts","sourceRoot":"","sources":["../../../../src/nodes/Collection/hooks/useCollection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C,OAAO,EAAY,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAKzE,eAAO,MAAM,aAAa,aAAc,OAAO,YAAY,eAAe;;;;;;;;;;;;;;;;;;;;;;CA8BzE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"Instance.d.ts","sourceRoot":"","sources":["../../../src/nodes/Instance/Instance.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,WAAW,EAKZ,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,UAAU,EAAkB,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAc,OAAO,EAAe,MAAM,mBAAmB,CAAC;AAuErE,UAAU,eAAe;IACvB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,aAAc,SAAQ,wBAAwB,CAAC,KAAK,CAAC;IACpE,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,OAAO,UAAU,CAAC,YAAY,CAAC;IACpD,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,GAAG,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,aAAa,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAC9D;AAiGD,eAAO,MAAM,QAAQ,yDAoBpB,CAAC"}
1
+ {"version":3,"file":"Instance.d.ts","sourceRoot":"","sources":["../../../src/nodes/Instance/Instance.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,WAAW,EAKZ,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,UAAU,EAAkB,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAc,OAAO,EAAe,MAAM,mBAAmB,CAAC;AAuErE,UAAU,eAAe;IACvB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,aAAc,SAAQ,wBAAwB,CAAC,KAAK,CAAC;IACpE,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,OAAO,UAAU,CAAC,YAAY,CAAC;IACpD,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,GAAG,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,aAAa,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAC9D;AAkGD,eAAO,MAAM,QAAQ,yDAoBpB,CAAC"}
@@ -11,7 +11,7 @@ export declare const useTextAttributes: (layerKey: LinkKey, options?: UseTextAtt
11
11
  };
12
12
  styles: any;
13
13
  classnames: string | null;
14
- content: any;
14
+ content: string;
15
15
  highlight: {
16
16
  stylesString: string;
17
17
  };
@@ -1,3 +1,5 @@
1
1
  import { GraphState, LinkKey } from '@graph-state/core';
2
- export declare const useTextContent: (layerKey: LinkKey, manager: GraphState) => any;
2
+ export declare function collectNestedVariableKeys(content: string, instanceProps: Record<string, any>, fragmentManager: GraphState | null, maxDepth?: number): LinkKey[];
3
+ export declare function resolveNestedContent(content: string, resolvedMap: Map<string, any>, maxDepth?: number): string;
4
+ export declare const useTextContent: (layerKey: LinkKey, manager: GraphState) => string;
3
5
  //# sourceMappingURL=useTextContent.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useTextContent.d.ts","sourceRoot":"","sources":["../../../../src/nodes/Text/hooks/useTextContent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAe,OAAO,EAAE,MAAM,mBAAmB,CAAC;AA+BrE,eAAO,MAAM,cAAc,aAAc,OAAO,WAAW,UAAU,QA4CpE,CAAC"}
1
+ {"version":3,"file":"useTextContent.d.ts","sourceRoot":"","sources":["../../../../src/nodes/Text/hooks/useTextContent.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,UAAU,EAEV,OAAO,EACR,MAAM,mBAAmB,CAAC;AAgC3B,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAClC,eAAe,EAAE,UAAU,GAAG,IAAI,EAClC,QAAQ,GAAE,MAA0B,GACnC,OAAO,EAAE,CAkCX;AAED,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,QAAQ,GAAE,MAA0B,GACnC,MAAM,CAmCR;AAED,eAAO,MAAM,cAAc,aAAc,OAAO,WAAW,UAAU,WA0BpE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=useTextContent.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTextContent.test.d.ts","sourceRoot":"","sources":["../../../../src/nodes/Text/hooks/useTextContent.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fragmentsx/render-react",
3
3
  "private": false,
4
- "version": "1.6.0",
4
+ "version": "1.6.2",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
@@ -27,9 +27,9 @@
27
27
  "@graph-state/core": "^0.13.2",
28
28
  "@graph-state/react": "^0.8.3",
29
29
  "react-use-measure": "^2.1.7",
30
- "@fragmentsx/client-core": "0.4.1",
31
- "@fragmentsx/definition": "0.2.2",
32
- "@fragmentsx/utils": "0.2.0"
30
+ "@fragmentsx/client-core": "0.4.2",
31
+ "@fragmentsx/utils": "0.2.0",
32
+ "@fragmentsx/definition": "0.2.2"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@testing-library/jest-dom": "^6.6.3",