@pixldocs/canvas-renderer 0.5.90 → 0.5.91

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
@@ -11665,6 +11665,8 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
11665
11665
  }
11666
11666
  return cloned;
11667
11667
  }
11668
+ const ENTRY_ID_KEY = "__entryId";
11669
+ const ENTRY_NAME_KEY = "__entryName";
11668
11670
  function hasBaseNodeId(node) {
11669
11671
  return node.__baseNodeId != null;
11670
11672
  }
@@ -12244,6 +12246,76 @@ function getRenderableFormSections(schema) {
12244
12246
  if (!pages.length) return base;
12245
12247
  return [...base, ...pages.map(repeatablePageToSection)];
12246
12248
  }
12249
+ function isRecord(value) {
12250
+ return !!value && typeof value === "object" && !Array.isArray(value);
12251
+ }
12252
+ function getRepeatableEntryMeta(entry, section) {
12253
+ const entryNameKey = section.entryNameFieldKey;
12254
+ const nameCandidate = entry[ENTRY_NAME_KEY] ?? (entryNameKey ? entry[entryNameKey] : void 0) ?? entry.section_title ?? entry.label ?? entry.name;
12255
+ return {
12256
+ id: typeof entry[ENTRY_ID_KEY] === "string" ? entry[ENTRY_ID_KEY] : void 0,
12257
+ name: typeof nameCandidate === "string" ? nameCandidate : void 0
12258
+ };
12259
+ }
12260
+ function extractSectionStateCandidate(values, sections) {
12261
+ if (!isRecord(values)) return null;
12262
+ const raw = isDefaultDataV2(values) && isRecord(values.sectionState) ? values.sectionState : values;
12263
+ const sectionIds = new Set(sections.map((section) => section.id));
12264
+ const looksLikeSectionState = Object.keys(raw).some((key) => sectionIds.has(key) || key.startsWith("section_"));
12265
+ return looksLikeSectionState ? raw : null;
12266
+ }
12267
+ function entryIdentityTokens(entry, section) {
12268
+ const entryNameKey = section == null ? void 0 : section.entryNameFieldKey;
12269
+ const candidates = [
12270
+ entry[ENTRY_NAME_KEY],
12271
+ entryNameKey ? entry[entryNameKey] : void 0,
12272
+ entry.section_title,
12273
+ entry.label,
12274
+ entry.name
12275
+ ];
12276
+ return Array.from(new Set(
12277
+ candidates.filter((value) => typeof value === "string" && !!value.trim()).map((value) => value.trim().toLowerCase())
12278
+ ));
12279
+ }
12280
+ function mergeRepeatableEntryMeta(target, metaSource, sections) {
12281
+ if (!metaSource) return target;
12282
+ let changed = false;
12283
+ const next = { ...target };
12284
+ const repeatableIds = new Set(sections.filter((section) => section.type === "repeatable").map((section) => section.id));
12285
+ const isRepeatableStateKey = (key) => repeatableIds.has(key) || Array.from(repeatableIds).some((id) => key.endsWith(`_${id}`));
12286
+ const sectionForStateKey = (key) => sections.find((section) => section.type === "repeatable" && (section.id === key || key.endsWith(`_${section.id}`)));
12287
+ for (const [key, targetEntries] of Object.entries(target)) {
12288
+ if (!isRepeatableStateKey(key) || !Array.isArray(targetEntries)) continue;
12289
+ const sourceEntries = metaSource[key];
12290
+ if (!Array.isArray(sourceEntries)) continue;
12291
+ const section = sectionForStateKey(key);
12292
+ const sourceByToken = /* @__PURE__ */ new Map();
12293
+ const duplicateTokens = /* @__PURE__ */ new Set();
12294
+ for (const sourceEntry of sourceEntries) {
12295
+ if (!isRecord(sourceEntry)) continue;
12296
+ for (const token of entryIdentityTokens(sourceEntry, section)) {
12297
+ if (sourceByToken.has(token)) duplicateTokens.add(token);
12298
+ else sourceByToken.set(token, sourceEntry);
12299
+ }
12300
+ }
12301
+ for (const token of duplicateTokens) sourceByToken.delete(token);
12302
+ const mergedEntries = targetEntries.map((entry, index) => {
12303
+ if (!isRecord(entry)) return entry;
12304
+ const sourceEntry = entryIdentityTokens(entry, section).map((token) => sourceByToken.get(token)).find((match) => !!match) ?? sourceEntries[index];
12305
+ if (!isRecord(sourceEntry)) return entry;
12306
+ const id = sourceEntry[ENTRY_ID_KEY];
12307
+ const name = sourceEntry[ENTRY_NAME_KEY];
12308
+ const additions = {};
12309
+ if (typeof id === "string" && id.trim() && typeof entry[ENTRY_ID_KEY] !== "string") additions[ENTRY_ID_KEY] = id;
12310
+ if (typeof name === "string" && name.trim() && typeof entry[ENTRY_NAME_KEY] !== "string") additions[ENTRY_NAME_KEY] = name;
12311
+ if (!Object.keys(additions).length) return entry;
12312
+ changed = true;
12313
+ return { ...entry, ...additions };
12314
+ });
12315
+ next[key] = mergedEntries;
12316
+ }
12317
+ return changed ? next : target;
12318
+ }
12247
12319
  function buildRepeatablePagesInputForApply(schema, sectionState) {
12248
12320
  var _a;
12249
12321
  if (!((_a = schema == null ? void 0 : schema.repeatablePages) == null ? void 0 : _a.length)) return [];
@@ -12490,6 +12562,7 @@ async function resolveFromForm(options) {
12490
12562
  } else {
12491
12563
  inferredSections = [];
12492
12564
  }
12565
+ const defaultFormMetaSectionState = extractSectionStateCandidate(defaultForm == null ? void 0 : defaultForm.values, inferredSections) ?? extractSectionStateCandidate(defaultForm == null ? void 0 : defaultForm.saved_data, inferredSections);
12493
12566
  let mergedSectionState = { ...sectionState };
12494
12567
  const templateDefaultData = templateRow.default_data;
12495
12568
  if (templateDefaultData && isDefaultDataV2(templateDefaultData)) {
@@ -12500,6 +12573,7 @@ async function resolveFromForm(options) {
12500
12573
  }
12501
12574
  }
12502
12575
  }
12576
+ mergedSectionState = mergeRepeatableEntryMeta(mergedSectionState, defaultFormMetaSectionState, inferredSections);
12503
12577
  const flatFormData = flattenSectionStateToFormData(mergedSectionState, inferredSections);
12504
12578
  const dynamicFields = templateConfig.dynamicFields || [];
12505
12579
  const mappings = [];
@@ -12521,10 +12595,7 @@ async function resolveFromForm(options) {
12521
12595
  const entries = mergedSectionState[s.id] ?? [];
12522
12596
  const nodeId = s.treeNodeId ?? s.id;
12523
12597
  const schemaRepeatable = repeatableFromSchemaByBase.get(baseId(nodeId));
12524
- const entryMeta = entries.map((e) => ({
12525
- id: typeof (e == null ? void 0 : e.__entryId) === "string" ? e.__entryId : void 0,
12526
- name: typeof (e == null ? void 0 : e.__entryName) === "string" ? e.__entryName : void 0
12527
- }));
12598
+ const entryMeta = entries.map((e) => getRepeatableEntryMeta(e, s));
12528
12599
  return { nodeId, label: s.label, entryCount: Math.max(1, entries.length), entryFilter: schemaRepeatable == null ? void 0 : schemaRepeatable.entryFilter, entryMeta };
12529
12600
  });
12530
12601
  const nestedRepeatables = inferredSections.filter((s) => s.type === "repeatable" && s.parentId != null).map((s) => {
@@ -12538,10 +12609,7 @@ async function resolveFromForm(options) {
12538
12609
  const merged = [];
12539
12610
  for (let pi = 0; pi < parentEntries.length; pi++) {
12540
12611
  const childEntries = mergedSectionState[`${parentId}_${pi}_${s.id}`] ?? [];
12541
- const meta = childEntries.map((e) => ({
12542
- id: typeof (e == null ? void 0 : e.__entryId) === "string" ? e.__entryId : void 0,
12543
- name: typeof (e == null ? void 0 : e.__entryName) === "string" ? e.__entryName : void 0
12544
- }));
12612
+ const meta = childEntries.map((e) => getRepeatableEntryMeta(e, s));
12545
12613
  nestedEntryMeta[`${baseId(parentNodeId)}_${pi + 1}_${baseId(nodeId)}`] = meta;
12546
12614
  merged.push(...meta);
12547
12615
  }