@pixldocs/canvas-renderer 0.5.83 → 0.5.84

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.
package/dist/index.cjs CHANGED
@@ -1696,7 +1696,7 @@ const useEditorStore = zustand.create((set, get) => ({
1696
1696
  const themeConfig = state.canvas.themeConfig ?? createEmptyThemeConfig();
1697
1697
  if (themeConfig.properties.some((p) => p.id === property.id)) return {};
1698
1698
  const updatedProperties = [...themeConfig.properties, property];
1699
- const updatedVariants = themeConfig.variants.map(
1699
+ const updatedVariants = property.linkedTo ? themeConfig.variants : themeConfig.variants.map(
1700
1700
  (v) => v.isDefault ? { ...v, values: { ...v.values, [property.id]: currentValue } } : v
1701
1701
  );
1702
1702
  const nextCanvas = {
@@ -1722,9 +1722,17 @@ const useEditorStore = zustand.create((set, get) => ({
1722
1722
  removeThemeProperty: (propertyId) => set((state) => {
1723
1723
  const themeConfig = state.canvas.themeConfig;
1724
1724
  if (!themeConfig) return {};
1725
- const updatedProperties = themeConfig.properties.filter((p) => p.id !== propertyId);
1725
+ const linkedChildren = themeConfig.properties.filter((p) => p.linkedTo === propertyId);
1726
+ const updatedProperties = themeConfig.properties.filter((p) => p.id !== propertyId).map((p) => p.linkedTo === propertyId ? { ...p, linkedTo: void 0 } : p);
1726
1727
  const updatedVariants = themeConfig.variants.map((v) => {
1727
- const { [propertyId]: _, ...rest } = v.values;
1728
+ const { [propertyId]: removedValue, ...rest } = v.values;
1729
+ if (linkedChildren.length && removedValue !== void 0) {
1730
+ const seeded = { ...rest };
1731
+ for (const child of linkedChildren) {
1732
+ if (seeded[child.id] === void 0) seeded[child.id] = removedValue;
1733
+ }
1734
+ return { ...v, values: seeded };
1735
+ }
1728
1736
  return { ...v, values: rest };
1729
1737
  });
1730
1738
  const nextCanvas = {
@@ -1798,7 +1806,7 @@ const useEditorStore = zustand.create((set, get) => ({
1798
1806
  const PAGE_BG_ID = "__pageBackground__";
1799
1807
  let updatedPages = [...state.canvas.pages];
1800
1808
  for (const prop of themeConfig.properties) {
1801
- const value = variant.values[prop.id];
1809
+ const value = variant.values[prop.linkedTo ?? prop.id];
1802
1810
  if (value === void 0) continue;
1803
1811
  if (prop.elementId === PAGE_BG_ID) {
1804
1812
  if (prop.targetProperty === "backgroundColor") {
@@ -2021,19 +2029,7 @@ const useEditorStore = zustand.create((set, get) => ({
2021
2029
  } else {
2022
2030
  newCollapsedSet.add(groupId);
2023
2031
  }
2024
- const currentPage = getCurrentPageFromCanvas(state.canvas);
2025
- let nextCanvas = state.canvas;
2026
- const group = findNodeById(currentPage.children, groupId);
2027
- if (group && isGroup(group)) {
2028
- nextCanvas = updateCurrentPageChildren(
2029
- nextCanvas,
2030
- (children) => updateNodeInTree(children, groupId, { collapsed: !isCollapsed })
2031
- );
2032
- }
2033
- return {
2034
- collapsedGroups: newCollapsedSet,
2035
- canvas: nextCanvas
2036
- };
2032
+ return { collapsedGroups: newCollapsedSet };
2037
2033
  }),
2038
2034
  // Convenience aliases
2039
2035
  groupElements: (ids, name) => get().groupNodes(ids, name),
@@ -10300,7 +10296,8 @@ function formDefSectionsToInferred(schemaSections, repeatableNodeMap) {
10300
10296
  })),
10301
10297
  // Honor minEntries: 0 — start with no entries, user adds via "+ Add" button.
10302
10298
  initialEntryCount: minEntries,
10303
- parentId
10299
+ parentId,
10300
+ entryNameFieldKey: def.entryNameFieldKey
10304
10301
  };
10305
10302
  if (treeNodeId) section.treeNodeId = treeNodeId;
10306
10303
  sections.push(section);
@@ -10745,6 +10742,26 @@ function baseId(id) {
10745
10742
  const REPEATABLE_KEY_REGEX = /^field_(.+)_(\d+)_(.+)$/;
10746
10743
  const NESTED_REPEATABLE_KEY_REGEX = /^field_(.+)_(\d+)_field_(.+)_(\d+)_(.+)$/;
10747
10744
  const FORMDEF_CANONICAL_KEY_REGEX = /^field_.+_N_.+$/;
10745
+ function parseEntryRange(range, max) {
10746
+ if (!range || !range.trim()) return null;
10747
+ const out = /* @__PURE__ */ new Set();
10748
+ for (const partRaw of range.split(",")) {
10749
+ const part = partRaw.trim();
10750
+ if (!part) continue;
10751
+ if (/^\d+$/.test(part)) {
10752
+ const n = parseInt(part, 10);
10753
+ if (n >= 1 && n <= max) out.add(n);
10754
+ continue;
10755
+ }
10756
+ const m = /^(\d*)\s*-\s*(\d*)$/.exec(part);
10757
+ if (!m) continue;
10758
+ const start = m[1] === "" ? 1 : Math.max(1, parseInt(m[1], 10));
10759
+ const end = m[2] === "" ? max : Math.min(max, parseInt(m[2], 10));
10760
+ for (let i = start; i <= end; i++) out.add(i);
10761
+ }
10762
+ if (out.size === 0) return null;
10763
+ return Array.from(out).sort((a, b) => a - b);
10764
+ }
10748
10765
  function applyTextCase(text, textCase) {
10749
10766
  if (!textCase || textCase === "none") return text;
10750
10767
  switch (textCase) {
@@ -10823,13 +10840,19 @@ function findElementBySourceIdInSubtree(nodes, sourceId) {
10823
10840
  }
10824
10841
  return void 0;
10825
10842
  }
10826
- function findRepeatableElementBySourceId(pages, baseNodeId, oneBasedIndex, sourceElementId) {
10843
+ function findAllRepeatableElementsBySourceId(pages, baseNodeId, oneBasedIndex, sourceElementId, expectedEntriesPerOccurrence) {
10827
10844
  var _a;
10828
10845
  const groups = collectGroupsByBaseId(pages, baseNodeId);
10829
- const group = groups[oneBasedIndex - 1];
10830
- if (!group) return void 0;
10831
- if (!isGroup(group) || !((_a = group.children) == null ? void 0 : _a.length)) return void 0;
10832
- return findElementBySourceIdInSubtree(group.children, sourceElementId) ?? (group.__sourceId === sourceElementId ? group.id : void 0);
10846
+ if (!groups.length) return [];
10847
+ const N = Math.max(1, expectedEntriesPerOccurrence ?? groups.length);
10848
+ const out = [];
10849
+ for (let occStart = 0; occStart < groups.length; occStart += N) {
10850
+ const group = groups[occStart + (oneBasedIndex - 1)];
10851
+ if (!group || !isGroup(group) || !((_a = group.children) == null ? void 0 : _a.length)) continue;
10852
+ const found = findElementBySourceIdInSubtree(group.children, sourceElementId) ?? (group.__sourceId === sourceElementId ? group.id : void 0);
10853
+ if (found) out.push(found);
10854
+ }
10855
+ return out;
10833
10856
  }
10834
10857
  function findNestedRepeatableElementBySourceId(pages, parentBaseNodeId, parentPi, childBaseNodeId, childCi, sourceElementId) {
10835
10858
  var _a, _b;
@@ -10850,6 +10873,24 @@ function findNestedRepeatableElementBySourceId(pages, parentBaseNodeId, parentPi
10850
10873
  if (!childGroup || !isGroup(childGroup) || !((_b = childGroup.children) == null ? void 0 : _b.length)) return void 0;
10851
10874
  return findElementBySourceIdInSubtree(childGroup.children, sourceElementId) ?? (childGroup.__sourceId === sourceElementId ? childGroup.id : void 0);
10852
10875
  }
10876
+ function repeatableLabelKey(label) {
10877
+ return String(label ?? "").trim().toLowerCase();
10878
+ }
10879
+ function repeatableLabelSlug(label) {
10880
+ return repeatableLabelKey(label).replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
10881
+ }
10882
+ function addToSetMap(map, key, value) {
10883
+ if (!key || !value) return;
10884
+ const set = map.get(key);
10885
+ if (set) set.add(value);
10886
+ else map.set(key, /* @__PURE__ */ new Set([value]));
10887
+ }
10888
+ function getFieldKeyAliases(fieldId) {
10889
+ const aliases = /* @__PURE__ */ new Set([fieldId]);
10890
+ const canonicalParts = fieldId.split("_N_");
10891
+ if (canonicalParts.length > 1) aliases.add(canonicalParts[canonicalParts.length - 1]);
10892
+ return Array.from(aliases).filter(Boolean);
10893
+ }
10853
10894
  function idPrefix(node) {
10854
10895
  if (isGroup(node)) return "group";
10855
10896
  const t = node.type;
@@ -10903,7 +10944,7 @@ function findRepeatableByNodeIds(pages, nodeIds) {
10903
10944
  var _a2;
10904
10945
  return !!((_a2 = n.repeatableSection) == null ? void 0 : _a2.label);
10905
10946
  };
10906
- function walk(children, parentRepeatableBaseId, parentInfo) {
10947
+ function walk(children, parentRepeatableBaseId, parentInfo, parentRepeatableEntryIndex) {
10907
10948
  var _a2, _b;
10908
10949
  if (!Array.isArray(children)) return;
10909
10950
  for (let i = 0; i < children.length; i++) {
@@ -10917,7 +10958,7 @@ function findRepeatableByNodeIds(pages, nodeIds) {
10917
10958
  const childBaseFromMeta = child.__baseNodeId;
10918
10959
  const isMatch = schemaBaseIds.has(child.id) || schemaBaseIds.has(childBase) || childBaseFromMeta != null && schemaBaseIds.has(childBaseFromMeta);
10919
10960
  if (!isMatch) {
10920
- if (isGroup(child) && Array.isArray(child.children)) walk(child.children, parentRepeatableBaseId);
10961
+ if (isGroup(child) && Array.isArray(child.children)) walk(child.children, parentRepeatableBaseId, void 0, parentRepeatableEntryIndex);
10921
10962
  j++;
10922
10963
  continue;
10923
10964
  }
@@ -10940,17 +10981,22 @@ function findRepeatableByNodeIds(pages, nodeIds) {
10940
10981
  count,
10941
10982
  node: child,
10942
10983
  baseNodeId: childBaseFromMeta ?? childBase,
10943
- parentBaseNodeId: parentRepeatableBaseId
10984
+ parentBaseNodeId: parentRepeatableBaseId,
10985
+ parentEntryIndex: parentRepeatableEntryIndex
10944
10986
  });
10945
- if (isGroup(child) && Array.isArray(child.children)) walk(child.children, childBase);
10987
+ const childEntryIndex = child.__repeatableEntryIndex;
10988
+ if (isGroup(child) && Array.isArray(child.children)) walk(child.children, effectiveChildBase, void 0, childEntryIndex);
10946
10989
  j += count;
10947
10990
  }
10948
10991
  } else if (isGroup(node) && ((_b = node.children) == null ? void 0 : _b.length)) {
10949
10992
  const nodeBase = baseId(node.id);
10950
10993
  const selfMatch = schemaBaseIds.has(node.id) || schemaBaseIds.has(nodeBase) || node.__baseNodeId != null && schemaBaseIds.has(node.__baseNodeId);
10951
- if (selfMatch && hasRepeatableSection(node)) {
10994
+ const isSelfRepeatable = selfMatch && hasRepeatableSection(node);
10995
+ let matchedSelfBase;
10996
+ if (selfMatch) matchedSelfBase = node.__baseNodeId ?? nodeBase;
10997
+ if (isSelfRepeatable) {
10952
10998
  let count = 1;
10953
- const effectiveBase = node.__baseNodeId ?? nodeBase;
10999
+ const effectiveBase = matchedSelfBase ?? nodeBase;
10954
11000
  if (node.id !== nodeBase && node.id !== effectiveBase) {
10955
11001
  while (i + count < children.length) {
10956
11002
  const next = children[i + count];
@@ -10967,10 +11013,12 @@ function findRepeatableByNodeIds(pages, nodeIds) {
10967
11013
  count,
10968
11014
  node,
10969
11015
  baseNodeId: effectiveBase,
10970
- parentBaseNodeId: parentRepeatableBaseId
11016
+ parentBaseNodeId: parentRepeatableBaseId,
11017
+ parentEntryIndex: parentRepeatableEntryIndex
10971
11018
  });
10972
11019
  }
10973
- walk(node.children, parentRepeatableBaseId);
11020
+ const nodeEntryIndex = node.__repeatableEntryIndex;
11021
+ walk(node.children, matchedSelfBase ?? parentRepeatableBaseId, void 0, nodeEntryIndex ?? parentRepeatableEntryIndex);
10974
11022
  }
10975
11023
  }
10976
11024
  }
@@ -11029,7 +11077,7 @@ function getNestedRepeatableEntryCount(parentId, parentIndex, childId, formValue
11029
11077
  return Math.max(1, maxIndex);
11030
11078
  }
11031
11079
  function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsFromSchema, repeatableEntryCounts, repeatableNestedEntryCounts, displayFormatMap, repeatablePagesFromSchema) {
11032
- var _a, _b, _c, _d, _e, _f;
11080
+ var _a, _b, _c;
11033
11081
  const cloned = JSON.parse(JSON.stringify(config));
11034
11082
  if (!cloned.pages) return cloned;
11035
11083
  const dynamicFields = cloned.dynamicFields;
@@ -11079,17 +11127,56 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11079
11127
  }
11080
11128
  const repeatableList = (repeatableSectionsFromSchema == null ? void 0 : repeatableSectionsFromSchema.length) ? repeatableSectionsFromSchema : getRepeatableFromConfig(pages);
11081
11129
  const nodeIds = repeatableList.map((r) => r.nodeId);
11130
+ const repeatableBasesByLabel = /* @__PURE__ */ new Map();
11131
+ for (const r of repeatableList) {
11132
+ const base = baseId(r.nodeId);
11133
+ addToSetMap(repeatableBasesByLabel, repeatableLabelKey(r.label), base);
11134
+ addToSetMap(repeatableBasesByLabel, repeatableLabelSlug(r.label), base);
11135
+ }
11136
+ const repeatableTokenToBaseIds = /* @__PURE__ */ new Map();
11137
+ for (const r of repeatableList) {
11138
+ const base = baseId(r.nodeId);
11139
+ const labelBases = /* @__PURE__ */ new Set([
11140
+ base,
11141
+ ...repeatableBasesByLabel.get(repeatableLabelKey(r.label)) ?? [],
11142
+ ...repeatableBasesByLabel.get(repeatableLabelSlug(r.label)) ?? []
11143
+ ]);
11144
+ for (const targetBase of labelBases) {
11145
+ addToSetMap(repeatableTokenToBaseIds, base, targetBase);
11146
+ addToSetMap(repeatableTokenToBaseIds, r.nodeId, targetBase);
11147
+ addToSetMap(repeatableTokenToBaseIds, repeatableLabelKey(r.label), targetBase);
11148
+ addToSetMap(repeatableTokenToBaseIds, repeatableLabelSlug(r.label), targetBase);
11149
+ }
11150
+ }
11151
+ const getRepeatableBaseIdsForToken = (token) => {
11152
+ const direct = repeatableTokenToBaseIds.get(token) ?? repeatableTokenToBaseIds.get(baseId(token));
11153
+ return direct ? Array.from(direct) : [baseId(token)];
11154
+ };
11082
11155
  const entryCountFromList = (baseNodeId) => {
11083
11156
  const item = repeatableList.find((r) => baseId(r.nodeId) === baseNodeId || r.nodeId === baseNodeId);
11084
11157
  return item == null ? void 0 : item.entryCount;
11085
11158
  };
11159
+ const entryFilterFromList = (baseNodeId) => {
11160
+ var _a2;
11161
+ const item = repeatableList.find((r) => baseId(r.nodeId) === baseNodeId || r.nodeId === baseNodeId);
11162
+ const filter = item == null ? void 0 : item.entryFilter;
11163
+ return (filter == null ? void 0 : filter.mode) === "range" && ((_a2 = filter.range) == null ? void 0 : _a2.trim()) ? filter : void 0;
11164
+ };
11086
11165
  const resolvedIdMap = /* @__PURE__ */ new Map();
11166
+ const addResolved = (key, id) => {
11167
+ const arr = resolvedIdMap.get(key);
11168
+ if (arr) {
11169
+ if (!arr.includes(id)) arr.push(id);
11170
+ } else resolvedIdMap.set(key, [id]);
11171
+ };
11087
11172
  const mappedNewIds = /* @__PURE__ */ new Set();
11088
11173
  const elementIdToFieldId = /* @__PURE__ */ new Map();
11089
11174
  if (dynamicFields == null ? void 0 : dynamicFields.length) {
11090
11175
  for (const f of dynamicFields) {
11091
- const elId = (_c = (_b = f.mappings) == null ? void 0 : _b[0]) == null ? void 0 : _c.elementId;
11092
- if (elId) elementIdToFieldId.set(elId, f.id);
11176
+ for (const m of f.mappings ?? []) {
11177
+ const elId = m == null ? void 0 : m.elementId;
11178
+ if (elId) elementIdToFieldId.set(elId, f.id);
11179
+ }
11093
11180
  }
11094
11181
  }
11095
11182
  const repeatableInfosFirst = findRepeatableByNodeIds(pages, nodeIds);
@@ -11102,37 +11189,90 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11102
11189
  for (const r of nestedFirst) {
11103
11190
  if (r.parentBaseNodeId) nestedChildToParent.set(r.baseNodeId, r.parentBaseNodeId);
11104
11191
  }
11192
+ const getNestedBasePairs = (parentToken, childToken) => {
11193
+ const pairs = [];
11194
+ const seen = /* @__PURE__ */ new Set();
11195
+ for (const parentBaseId of getRepeatableBaseIdsForToken(parentToken)) {
11196
+ for (const childBaseId of getRepeatableBaseIdsForToken(childToken)) {
11197
+ const key = `${parentBaseId}::${childBaseId}`;
11198
+ if (seen.has(key)) continue;
11199
+ seen.add(key);
11200
+ pairs.push({ parentBaseId, childBaseId });
11201
+ }
11202
+ }
11203
+ return pairs.length ? pairs : [{ parentBaseId: baseId(parentToken), childBaseId: baseId(childToken) }];
11204
+ };
11205
+ const getNestedCountForBasePair = (parentBaseNodeId, parentIndex1Based, childBaseNodeId) => {
11206
+ let maxCount = 0;
11207
+ for (const { parentBaseId, childBaseId } of getNestedBasePairs(parentBaseNodeId, childBaseNodeId)) {
11208
+ const aliasKey = `${parentBaseId}_${parentIndex1Based}_${childBaseId}`;
11209
+ maxCount = Math.max(
11210
+ maxCount,
11211
+ (repeatableNestedEntryCounts == null ? void 0 : repeatableNestedEntryCounts[aliasKey]) ?? 0,
11212
+ getNestedRepeatableEntryCount(parentBaseId, parentIndex1Based, childBaseId, formValues)
11213
+ );
11214
+ }
11215
+ return Math.max(1, maxCount);
11216
+ };
11105
11217
  const phase1NewToOriginal = /* @__PURE__ */ new Map();
11106
- const topLevelByBase = /* @__PURE__ */ new Map();
11218
+ const entryCountByBase = /* @__PURE__ */ new Map();
11219
+ [...topLevel].sort((a, b) => {
11220
+ if (a.parentChildren !== b.parentChildren) return 0;
11221
+ return b.startIndex - a.startIndex;
11222
+ });
11223
+ const occByParent = /* @__PURE__ */ new Map();
11107
11224
  for (const r of topLevel) {
11108
- if (!topLevelByBase.has(r.baseNodeId)) topLevelByBase.set(r.baseNodeId, []);
11109
- topLevelByBase.get(r.baseNodeId).push(r);
11110
- }
11111
- for (const [, blocks] of topLevelByBase) {
11112
- const first = blocks[0];
11113
- const startIndex = Math.min(...blocks.map((b) => b.startIndex));
11114
- const endIndex = Math.max(...blocks.map((b) => b.startIndex + b.count));
11115
- const count = endIndex - startIndex;
11116
- const { parentChildren, node, baseNodeId } = first;
11225
+ if (!occByParent.has(r.parentChildren)) occByParent.set(r.parentChildren, []);
11226
+ occByParent.get(r.parentChildren).push(r);
11227
+ }
11228
+ const computeN = (baseNodeId, sampleNodeId) => {
11229
+ if (entryCountByBase.has(baseNodeId)) return entryCountByBase.get(baseNodeId);
11117
11230
  const countFromList = entryCountFromList(baseNodeId);
11118
- const countFromState = countFromList ?? (repeatableEntryCounts == null ? void 0 : repeatableEntryCounts[baseNodeId]) ?? (repeatableEntryCounts == null ? void 0 : repeatableEntryCounts[node.id]) ?? repeatableEntryCounts;
11231
+ const countFromState = countFromList ?? (repeatableEntryCounts == null ? void 0 : repeatableEntryCounts[baseNodeId]) ?? (repeatableEntryCounts == null ? void 0 : repeatableEntryCounts[sampleNodeId]) ?? repeatableEntryCounts;
11119
11232
  const N = Math.max(1, countFromState ?? getRepeatableEntryCount(baseNodeId, formValues));
11120
- const clones = [];
11121
- for (let i = 1; i <= N; i++) {
11122
- const [clone, oldToNew] = cloneNodeWithNewIds$1(node, `${baseNodeId}_e${i}`);
11123
- delete clone.repeatableSection;
11124
- clones.push(clone);
11125
- const newToOriginal = /* @__PURE__ */ new Map();
11126
- for (const [oldId, newId] of oldToNew) {
11127
- resolvedIdMap.set(`${baseNodeId}_${i}_${oldId}`, newId);
11128
- newToOriginal.set(newId, oldId);
11129
- const fieldId = elementIdToFieldId.get(oldId);
11130
- if (fieldId) resolvedIdMap.set(`${baseNodeId}_${i}_${fieldId}`, newId);
11131
- mappedNewIds.add(newId);
11132
- }
11133
- phase1NewToOriginal.set(`${baseNodeId}_${i}`, newToOriginal);
11134
- }
11135
- parentChildren.splice(startIndex, count, ...clones);
11233
+ entryCountByBase.set(baseNodeId, N);
11234
+ return N;
11235
+ };
11236
+ const occurrenceIndexByBase = /* @__PURE__ */ new Map();
11237
+ for (const r of topLevel) {
11238
+ const idx = (occurrenceIndexByBase.get(r.baseNodeId) ?? 0) + 1;
11239
+ occurrenceIndexByBase.set(r.baseNodeId, idx);
11240
+ r.__occurrenceIndex = idx;
11241
+ }
11242
+ for (const [parentChildren, blocks] of occByParent) {
11243
+ const sorted = [...blocks].sort((a, b) => b.startIndex - a.startIndex);
11244
+ for (const block of sorted) {
11245
+ const { node, baseNodeId, startIndex, count } = block;
11246
+ const occIdx = block.__occurrenceIndex ?? 1;
11247
+ const N = computeN(baseNodeId, node.id);
11248
+ const entryFilter = ((_b = node.repeatableSection) == null ? void 0 : _b.entryFilter) ?? entryFilterFromList(baseNodeId);
11249
+ let entryIndices;
11250
+ if (entryFilter && entryFilter.mode === "range" && entryFilter.range) {
11251
+ const parsed = parseEntryRange(entryFilter.range, N);
11252
+ entryIndices = parsed && parsed.length ? parsed : Array.from({ length: N }, (_, k) => k + 1);
11253
+ } else {
11254
+ entryIndices = Array.from({ length: N }, (_, k) => k + 1);
11255
+ }
11256
+ const clones = [];
11257
+ for (const i of entryIndices) {
11258
+ const [clone, oldToNew] = cloneNodeWithNewIds$1(node, `${baseNodeId}_o${occIdx}_e${i}`);
11259
+ clone.__repeatableEntryIndex = i;
11260
+ delete clone.repeatableSection;
11261
+ clones.push(clone);
11262
+ const newToOriginal = /* @__PURE__ */ new Map();
11263
+ for (const [oldId, newId] of oldToNew) {
11264
+ addResolved(`${baseNodeId}_${i}_${oldId}`, newId);
11265
+ newToOriginal.set(newId, oldId);
11266
+ const fieldId = elementIdToFieldId.get(oldId);
11267
+ if (fieldId) {
11268
+ for (const alias of getFieldKeyAliases(fieldId)) addResolved(`${baseNodeId}_${i}_${alias}`, newId);
11269
+ }
11270
+ mappedNewIds.add(newId);
11271
+ }
11272
+ phase1NewToOriginal.set(`${baseNodeId}_${i}`, newToOriginal);
11273
+ }
11274
+ parentChildren.splice(startIndex, count, ...clones);
11275
+ }
11136
11276
  }
11137
11277
  if (nestedChildToParent.size > 0) {
11138
11278
  const repeatableInfosSecond = findRepeatableByNodeIds(pages, nodeIds);
@@ -11145,25 +11285,41 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11145
11285
  for (const [childBaseNodeId, blocks] of byChildBase) {
11146
11286
  const parentBaseNodeId = nestedChildToParent.get(childBaseNodeId);
11147
11287
  blocks.forEach((block, parentIndex0) => {
11148
- const parentIndex1Based = parentIndex0 + 1;
11288
+ var _a2;
11289
+ const parentIndex1Based = block.parentEntryIndex ?? parentIndex0 + 1;
11149
11290
  const { parentChildren, startIndex, count, node, baseNodeId } = block;
11150
- const nestedKey = `${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}`;
11151
- const N = Math.max(
11152
- 1,
11153
- (repeatableNestedEntryCounts == null ? void 0 : repeatableNestedEntryCounts[nestedKey]) ?? getNestedRepeatableEntryCount(parentBaseNodeId, parentIndex1Based, baseNodeId, formValues)
11154
- );
11291
+ const N = getNestedCountForBasePair(parentBaseNodeId, parentIndex1Based, baseNodeId);
11155
11292
  const phase1Map = phase1NewToOriginal.get(`${parentBaseNodeId}_${parentIndex1Based}`);
11293
+ const nestedEntryFilter = ((_a2 = node.repeatableSection) == null ? void 0 : _a2.entryFilter) ?? entryFilterFromList(baseNodeId);
11294
+ let nestedEntryIndices;
11295
+ if (nestedEntryFilter && nestedEntryFilter.mode === "range" && nestedEntryFilter.range) {
11296
+ const parsed = parseEntryRange(nestedEntryFilter.range, N);
11297
+ nestedEntryIndices = parsed && parsed.length ? parsed : Array.from({ length: N }, (_, k) => k + 1);
11298
+ } else {
11299
+ nestedEntryIndices = Array.from({ length: N }, (_, k) => k + 1);
11300
+ }
11156
11301
  const clones = [];
11157
- for (let i = 1; i <= N; i++) {
11302
+ for (const i of nestedEntryIndices) {
11158
11303
  const [clone, oldToNew] = cloneNodeWithNewIds$1(node, `${parentBaseNodeId}_p${parentIndex1Based}_${baseNodeId}_e${i}`);
11159
11304
  delete clone.repeatableSection;
11160
11305
  clones.push(clone);
11161
11306
  for (const [oldId, newId] of oldToNew) {
11162
11307
  const originalId = (phase1Map == null ? void 0 : phase1Map.get(oldId)) ?? oldId;
11163
- resolvedIdMap.set(`${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}_${i}_${originalId}`, newId);
11164
- resolvedIdMap.set(`${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}_${i}_${oldId}`, newId);
11308
+ addResolved(`${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}_${i}_${originalId}`, newId);
11309
+ addResolved(`${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}_${i}_${oldId}`, newId);
11310
+ for (const { parentBaseId, childBaseId } of getNestedBasePairs(parentBaseNodeId, baseNodeId)) {
11311
+ addResolved(`${parentBaseId}_${parentIndex1Based}_${childBaseId}_${i}_${originalId}`, newId);
11312
+ addResolved(`${parentBaseId}_${parentIndex1Based}_${childBaseId}_${i}_${oldId}`, newId);
11313
+ }
11165
11314
  const fieldId = elementIdToFieldId.get(originalId) ?? elementIdToFieldId.get(oldId);
11166
- if (fieldId) resolvedIdMap.set(`${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}_${i}_${fieldId}`, newId);
11315
+ if (fieldId) {
11316
+ for (const alias of getFieldKeyAliases(fieldId)) {
11317
+ addResolved(`${parentBaseNodeId}_${parentIndex1Based}_${baseNodeId}_${i}_${alias}`, newId);
11318
+ for (const { parentBaseId, childBaseId } of getNestedBasePairs(parentBaseNodeId, baseNodeId)) {
11319
+ addResolved(`${parentBaseId}_${parentIndex1Based}_${childBaseId}_${i}_${alias}`, newId);
11320
+ }
11321
+ }
11322
+ }
11167
11323
  mappedNewIds.add(newId);
11168
11324
  }
11169
11325
  }
@@ -11171,7 +11327,9 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11171
11327
  });
11172
11328
  }
11173
11329
  }
11174
- cloned.__cloneIdMap = Object.fromEntries(resolvedIdMap);
11330
+ cloned.__cloneIdMap = Object.fromEntries(
11331
+ Array.from(resolvedIdMap.entries()).map(([k, v]) => [k, v.length === 1 ? v[0] : v])
11332
+ );
11175
11333
  cloned.__mappedElementIds = Array.from(mappedNewIds);
11176
11334
  const fieldLabelByKey = /* @__PURE__ */ new Map();
11177
11335
  const fieldTypeByKey = /* @__PURE__ */ new Map();
@@ -11314,10 +11472,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11314
11472
  }
11315
11473
  const byId = dynamicFields.find((f) => f.id === fieldId);
11316
11474
  if (byId) return byId;
11317
- const byElementId = dynamicFields.find((f) => {
11318
- var _a2, _b2;
11319
- return ((_b2 = (_a2 = f.mappings) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.elementId) === fieldId;
11320
- });
11475
+ const byElementId = dynamicFields.find((f) => (f.mappings ?? []).some((m) => (m == null ? void 0 : m.elementId) === fieldId));
11321
11476
  if (byElementId) return byElementId;
11322
11477
  const suffixMatch = dynamicFields.find((f) => {
11323
11478
  if (f.id.endsWith(`_N_${fieldId}`)) return true;
@@ -11333,39 +11488,36 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11333
11488
  const [, parentId, parentIndexStr, childId, childIndexStr, fieldId] = nestedMatch;
11334
11489
  const value = formValues[key];
11335
11490
  const field = getFieldForRepeatableKey(fieldId, key);
11336
- const mapping = (_d = field == null ? void 0 : field.mappings) == null ? void 0 : _d[0];
11337
- if (!mapping) continue;
11338
- const oldElementId = mapping.elementId;
11339
- const mapKeyByElement = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${oldElementId}`;
11340
- const mapKeyByField = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${fieldId}`;
11341
- let elementId = resolvedIdMap.get(mapKeyByElement) ?? resolvedIdMap.get(mapKeyByField);
11342
- if (!elementId) {
11343
- const mapKeyByElementBase = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${baseId(oldElementId)}`;
11344
- elementId = resolvedIdMap.get(mapKeyByElementBase);
11345
- }
11346
- if (!elementId) {
11347
- elementId = findNestedRepeatableElementBySourceId(
11348
- pages,
11349
- baseId(parentId),
11350
- parseInt(parentIndexStr, 10),
11351
- baseId(childId),
11352
- parseInt(childIndexStr, 10),
11353
- oldElementId
11354
- );
11355
- }
11356
- if (!elementId) {
11357
- elementId = findNestedRepeatableElementBySourceId(
11358
- pages,
11359
- baseId(parentId),
11360
- parseInt(parentIndexStr, 10),
11361
- baseId(childId),
11362
- parseInt(childIndexStr, 10),
11363
- baseId(oldElementId)
11364
- );
11491
+ for (const mapping of (field == null ? void 0 : field.mappings) ?? []) {
11492
+ const oldElementId = mapping.elementId;
11493
+ const mapKeyByElement = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${oldElementId}`;
11494
+ let elementIds = resolvedIdMap.get(mapKeyByElement);
11495
+ if (!elementIds || elementIds.length === 0) {
11496
+ const mapKeyByElementBase = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${baseId(oldElementId)}`;
11497
+ elementIds = resolvedIdMap.get(mapKeyByElementBase);
11498
+ }
11499
+ if (!elementIds || elementIds.length === 0) {
11500
+ const fallback = findNestedRepeatableElementBySourceId(
11501
+ pages,
11502
+ baseId(parentId),
11503
+ parseInt(parentIndexStr, 10),
11504
+ baseId(childId),
11505
+ parseInt(childIndexStr, 10),
11506
+ oldElementId
11507
+ ) ?? findNestedRepeatableElementBySourceId(
11508
+ pages,
11509
+ baseId(parentId),
11510
+ parseInt(parentIndexStr, 10),
11511
+ baseId(childId),
11512
+ parseInt(childIndexStr, 10),
11513
+ baseId(oldElementId)
11514
+ );
11515
+ if (fallback) elementIds = [fallback];
11516
+ }
11517
+ if (!elementIds || elementIds.length === 0) continue;
11518
+ const targetProperty = mapping.targetProperty || "text";
11519
+ for (const elementId of elementIds) applyValue(elementId, targetProperty, value, key);
11365
11520
  }
11366
- if (!elementId) continue;
11367
- const targetProperty = mapping.targetProperty || "text";
11368
- applyValue(elementId, targetProperty, value, key);
11369
11521
  }
11370
11522
  }
11371
11523
  }
@@ -11388,18 +11540,29 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11388
11540
  const [, nodeId, indexStr, fieldId] = match;
11389
11541
  const value = formValues[key];
11390
11542
  const field = getFieldForRepeatableKey(fieldId);
11391
- const mapping = (_e = field == null ? void 0 : field.mappings) == null ? void 0 : _e[0];
11392
- if (!mapping) continue;
11393
- const oldElementId = mapping.elementId;
11394
- const mapKeyByElement = `${baseId(nodeId)}_${indexStr}_${oldElementId}`;
11395
- const mapKeyByField = `${baseId(nodeId)}_${indexStr}_${fieldId}`;
11396
- let elementId = resolvedIdMap.get(mapKeyByElement) ?? resolvedIdMap.get(mapKeyByField);
11397
- if (!elementId) {
11398
- elementId = findRepeatableElementBySourceId(pages, baseId(nodeId), parseInt(indexStr, 10), oldElementId);
11543
+ for (const mapping of (field == null ? void 0 : field.mappings) ?? []) {
11544
+ const oldElementId = mapping.elementId;
11545
+ const elementIds = /* @__PURE__ */ new Set();
11546
+ for (const candidateBaseId of getRepeatableBaseIdsForToken(nodeId)) {
11547
+ const mapKeyByElement = `${candidateBaseId}_${indexStr}_${oldElementId}`;
11548
+ const foundByMap = resolvedIdMap.get(mapKeyByElement);
11549
+ for (const id of foundByMap ?? []) elementIds.add(id);
11550
+ if (!(foundByMap == null ? void 0 : foundByMap.length)) {
11551
+ const N = entryCountByBase.get(candidateBaseId);
11552
+ const found = findAllRepeatableElementsBySourceId(
11553
+ pages,
11554
+ candidateBaseId,
11555
+ parseInt(indexStr, 10),
11556
+ oldElementId,
11557
+ N
11558
+ );
11559
+ for (const id of found) elementIds.add(id);
11560
+ }
11561
+ }
11562
+ if (elementIds.size === 0) continue;
11563
+ const targetProperty = mapping.targetProperty || "text";
11564
+ for (const elementId of elementIds) applyValue(elementId, targetProperty, value, key);
11399
11565
  }
11400
- if (!elementId) continue;
11401
- const targetProperty = mapping.targetProperty || "text";
11402
- applyValue(elementId, targetProperty, value, key);
11403
11566
  }
11404
11567
  }
11405
11568
  const repeatableKeySet = new Set(
@@ -11452,7 +11615,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11452
11615
  }
11453
11616
  }
11454
11617
  for (const page of pages) {
11455
- if ((_f = page.children) == null ? void 0 : _f.length) {
11618
+ if ((_c = page.children) == null ? void 0 : _c.length) {
11456
11619
  page.children = applyStackReflowToPageTree(page.children);
11457
11620
  }
11458
11621
  }
@@ -12410,7 +12573,8 @@ function applyThemeVariantToConfig(config, themeConfig, themeId) {
12410
12573
  const cloneIdMap = config.__cloneIdMap || {};
12411
12574
  const pageElements = result.pages.map((page) => flattenAll(page.children || []));
12412
12575
  for (const prop of themeConfig.properties) {
12413
- const value = variant ? (_d = variant.values) == null ? void 0 : _d[prop.id] : prop.defaultValue;
12576
+ const lookupId = prop.linkedTo || prop.id;
12577
+ const value = variant ? (_d = variant.values) == null ? void 0 : _d[lookupId] : prop.defaultValue;
12414
12578
  if (value === void 0) continue;
12415
12579
  if (prop.targetProperty === "backgroundColor" && prop.elementId === "__pageBackground__") {
12416
12580
  result.pages.forEach((p) => {