@pixldocs/canvas-renderer 0.5.0 → 0.5.1

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
@@ -9275,7 +9275,7 @@ function applyTextCase(text, textCase) {
9275
9275
  return text;
9276
9276
  }
9277
9277
  }
9278
- function setInTree$1(nodes, elementId, targetProperty, value) {
9278
+ function setInTree(nodes, elementId, targetProperty, value) {
9279
9279
  for (const node of nodes) {
9280
9280
  if (node.id === elementId) {
9281
9281
  if (targetProperty === "link") {
@@ -9302,7 +9302,7 @@ function setInTree$1(nodes, elementId, targetProperty, value) {
9302
9302
  return true;
9303
9303
  }
9304
9304
  if (node.children && Array.isArray(node.children)) {
9305
- if (setInTree$1(node.children, elementId, targetProperty, value)) return true;
9305
+ if (setInTree(node.children, elementId, targetProperty, value)) return true;
9306
9306
  }
9307
9307
  }
9308
9308
  return false;
@@ -9543,11 +9543,54 @@ function getNestedRepeatableEntryCount(parentId, parentIndex, childId, formValue
9543
9543
  return Math.max(1, maxIndex);
9544
9544
  }
9545
9545
  function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsFromSchema, repeatableEntryCounts, repeatableNestedEntryCounts, displayFormatMap, repeatablePagesFromSchema) {
9546
- var _a, _b, _c, _d, _e;
9546
+ var _a, _b, _c, _d, _e, _f;
9547
9547
  const cloned = JSON.parse(JSON.stringify(config));
9548
9548
  if (!cloned.pages) return cloned;
9549
9549
  const dynamicFields = cloned.dynamicFields;
9550
9550
  let pages = cloned.pages;
9551
+ if (repeatablePagesFromSchema == null ? void 0 : repeatablePagesFromSchema.length) {
9552
+ const expanded = [];
9553
+ for (const page of pages) {
9554
+ const boundId = page.boundRepeatablePageId;
9555
+ const schemaEntry = boundId ? repeatablePagesFromSchema.find((p) => p.pageId === boundId) : void 0;
9556
+ if (!schemaEntry) {
9557
+ expanded.push(page);
9558
+ continue;
9559
+ }
9560
+ const prefix = schemaEntry.templateKeyPrefix;
9561
+ let inferredN = 1;
9562
+ const keyPrefix = `field_${prefix}_`;
9563
+ for (const k of Object.keys(formValues)) {
9564
+ if (!k.startsWith(keyPrefix)) continue;
9565
+ const rest = k.slice(keyPrefix.length);
9566
+ const m = /^(\d+)_/.exec(rest);
9567
+ if (m) inferredN = Math.max(inferredN, parseInt(m[1], 10));
9568
+ }
9569
+ const N = Math.max(0, schemaEntry.entryCount ?? inferredN);
9570
+ if (N === 0) {
9571
+ continue;
9572
+ }
9573
+ const basePageId = baseId(page.id);
9574
+ for (let i = 1; i <= N; i++) {
9575
+ const clonedPage = JSON.parse(JSON.stringify(page));
9576
+ clonedPage.id = N === 1 && i === 1 ? page.id : `${basePageId}_${i}`;
9577
+ clonedPage.__repeatablePagePrefix = prefix;
9578
+ clonedPage.__repeatablePageEntryIndex = i;
9579
+ clonedPage.__repeatablePageBaseId = basePageId;
9580
+ if ((_a = clonedPage.children) == null ? void 0 : _a.length) {
9581
+ const suffix = `${basePageId}_p${i}`;
9582
+ const reidChildren = (nodes) => nodes.map((n) => {
9583
+ const [reclone] = cloneNodeWithNewIds$1(n, suffix);
9584
+ return reclone;
9585
+ });
9586
+ if (i > 1) clonedPage.children = reidChildren(clonedPage.children);
9587
+ }
9588
+ expanded.push(clonedPage);
9589
+ }
9590
+ }
9591
+ pages = expanded;
9592
+ cloned.pages = pages;
9593
+ }
9551
9594
  const repeatableList = (repeatableSectionsFromSchema == null ? void 0 : repeatableSectionsFromSchema.length) ? repeatableSectionsFromSchema : getRepeatableFromConfig(pages);
9552
9595
  const nodeIds = repeatableList.map((r) => r.nodeId);
9553
9596
  const entryCountFromList = (baseNodeId) => {
@@ -9559,7 +9602,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9559
9602
  const elementIdToFieldId = /* @__PURE__ */ new Map();
9560
9603
  if (dynamicFields == null ? void 0 : dynamicFields.length) {
9561
9604
  for (const f of dynamicFields) {
9562
- const elId = (_b = (_a = f.mappings) == null ? void 0 : _a[0]) == null ? void 0 : _b.elementId;
9605
+ const elId = (_c = (_b = f.mappings) == null ? void 0 : _b[0]) == null ? void 0 : _c.elementId;
9563
9606
  if (elId) elementIdToFieldId.set(elId, f.id);
9564
9607
  }
9565
9608
  }
@@ -9773,7 +9816,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9773
9816
  }
9774
9817
  }
9775
9818
  for (const page of pages) {
9776
- if (page.children && setInTree$1(page.children, elementId, targetProperty, effectiveValue)) return true;
9819
+ if (page.children && setInTree(page.children, elementId, targetProperty, effectiveValue)) return true;
9777
9820
  }
9778
9821
  return false;
9779
9822
  };
@@ -9804,7 +9847,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9804
9847
  const [, parentId, parentIndexStr, childId, childIndexStr, fieldId] = nestedMatch;
9805
9848
  const value = formValues[key];
9806
9849
  const field = getFieldForRepeatableKey(fieldId, key);
9807
- const mapping = (_c = field == null ? void 0 : field.mappings) == null ? void 0 : _c[0];
9850
+ const mapping = (_d = field == null ? void 0 : field.mappings) == null ? void 0 : _d[0];
9808
9851
  if (!mapping) continue;
9809
9852
  const oldElementId = mapping.elementId;
9810
9853
  const mapKeyByElement = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${oldElementId}`;
@@ -9841,7 +9884,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9841
9884
  }
9842
9885
  }
9843
9886
  const repeatablePagePrefixSet = new Set(
9844
- [].map((p) => `field_${p.templateKeyPrefix}_`)
9887
+ (repeatablePagesFromSchema ?? []).map((p) => `field_${p.templateKeyPrefix}_`)
9845
9888
  );
9846
9889
  if (dynamicFields == null ? void 0 : dynamicFields.length) {
9847
9890
  for (const key of Object.keys(formValues)) {
@@ -9859,7 +9902,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9859
9902
  const [, nodeId, indexStr, fieldId] = match;
9860
9903
  const value = formValues[key];
9861
9904
  const field = getFieldForRepeatableKey(fieldId);
9862
- const mapping = (_d = field == null ? void 0 : field.mappings) == null ? void 0 : _d[0];
9905
+ const mapping = (_e = field == null ? void 0 : field.mappings) == null ? void 0 : _e[0];
9863
9906
  if (!mapping) continue;
9864
9907
  const oldElementId = mapping.elementId;
9865
9908
  const mapKeyByElement = `${baseId(nodeId)}_${indexStr}_${oldElementId}`;
@@ -9881,8 +9924,49 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9881
9924
  const value = formValues[m.field_key];
9882
9925
  applyValue(m.element_id, m.target_property, value, m.field_key);
9883
9926
  }
9927
+ if ((repeatablePagesFromSchema == null ? void 0 : repeatablePagesFromSchema.length) && (dynamicFields == null ? void 0 : dynamicFields.length)) {
9928
+ const findInPage = (page, originalId) => {
9929
+ var _a2;
9930
+ if (!((_a2 = page.children) == null ? void 0 : _a2.length)) return void 0;
9931
+ const findIn = (nodes) => {
9932
+ var _a3;
9933
+ for (const n of nodes) {
9934
+ const src = n.__sourceId;
9935
+ if (n.id === originalId || src === originalId || baseId(n.id) === baseId(originalId)) return n.id;
9936
+ if (isGroup(n) && ((_a3 = n.children) == null ? void 0 : _a3.length)) {
9937
+ const f = findIn(n.children);
9938
+ if (f) return f;
9939
+ }
9940
+ }
9941
+ return void 0;
9942
+ };
9943
+ return findIn(page.children);
9944
+ };
9945
+ for (const page of pages) {
9946
+ const meta = page;
9947
+ const prefix = meta.__repeatablePagePrefix;
9948
+ const entryIndex = meta.__repeatablePageEntryIndex;
9949
+ if (!prefix || !entryIndex) continue;
9950
+ const fieldIdPrefix = `field_${prefix}_N_`;
9951
+ for (const field of dynamicFields) {
9952
+ if (!field.id.startsWith(fieldIdPrefix)) continue;
9953
+ const fieldKeySuffix = field.id.slice(fieldIdPrefix.length);
9954
+ const formKey = `field_${prefix}_${entryIndex}_${fieldKeySuffix}`;
9955
+ if (!(formKey in formValues)) continue;
9956
+ const value = formValues[formKey];
9957
+ for (const m of field.mappings ?? []) {
9958
+ const originalId = m.elementId;
9959
+ const targetProperty = m.targetProperty || "text";
9960
+ if (!originalId) continue;
9961
+ const resolvedId = findInPage(page, originalId);
9962
+ if (!resolvedId) continue;
9963
+ if (page.children) setInTree(page.children, resolvedId, targetProperty, value);
9964
+ }
9965
+ }
9966
+ }
9967
+ }
9884
9968
  for (const page of pages) {
9885
- if ((_e = page.children) == null ? void 0 : _e.length) {
9969
+ if ((_f = page.children) == null ? void 0 : _f.length) {
9886
9970
  page.children = applyStackReflowToPageTree(page.children);
9887
9971
  }
9888
9972
  }
@@ -10247,52 +10331,122 @@ async function fetchDefaultForm(supabaseUrl, anonKey, formSchemaId) {
10247
10331
  const rows = await res.json();
10248
10332
  return rows.length ? rows[0] : null;
10249
10333
  }
10250
- function applyFormDataSimple(config, formData) {
10251
- var _a;
10252
- const cloned = JSON.parse(JSON.stringify(config));
10253
- if (!cloned.pages || !((_a = cloned.dynamicFields) == null ? void 0 : _a.length)) return cloned;
10254
- const dynamicFields = cloned.dynamicFields;
10255
- for (const field of dynamicFields) {
10256
- const value = formData[field.id];
10257
- if (value === void 0) continue;
10258
- for (const mapping of field.mappings) {
10259
- setInTree(cloned.pages.flatMap((p) => p.children), mapping.elementId, mapping.targetProperty, value);
10334
+ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10335
+ if (!Array.isArray(config == null ? void 0 : config.pages) || config.pages.length === 0) return [];
10336
+ const schemaPages = inlineFormSchema == null ? void 0 : inlineFormSchema.repeatablePages;
10337
+ const collectIds = (nodes, out2) => {
10338
+ for (const n of nodes ?? []) {
10339
+ if (n == null ? void 0 : n.id) out2.add(n.id);
10340
+ if (Array.isArray(n == null ? void 0 : n.children)) collectIds(n.children, out2);
10260
10341
  }
10261
- }
10262
- return cloned;
10263
- }
10264
- function setInTree(nodes, elementId, targetProperty, value) {
10265
- for (const node of nodes) {
10266
- if (node.id === elementId) {
10267
- if (targetProperty === "text" && node.type === "text" && typeof value === "string") {
10268
- node[targetProperty] = value === "" ? " " : value;
10269
- } else if (targetProperty === "link") {
10270
- node.linkConfig = { ...node.linkConfig || {}, url: String(value ?? "") };
10271
- } else {
10272
- node[targetProperty] = value;
10342
+ };
10343
+ const dynamicFields = config.dynamicFields;
10344
+ const out = [];
10345
+ for (const page of config.pages) {
10346
+ const boundId = page == null ? void 0 : page.boundRepeatablePageId;
10347
+ if (!boundId) continue;
10348
+ let templateKeyPrefix;
10349
+ if (Array.isArray(schemaPages)) {
10350
+ const found = schemaPages.find((rp) => (rp == null ? void 0 : rp.id) === boundId);
10351
+ if (found == null ? void 0 : found.templateKeyPrefix) templateKeyPrefix = found.templateKeyPrefix;
10352
+ }
10353
+ if (!templateKeyPrefix && Array.isArray(dynamicFields) && dynamicFields.length > 0) {
10354
+ const idsOnThisPage = /* @__PURE__ */ new Set();
10355
+ collectIds(page.children || [], idsOnThisPage);
10356
+ const prefixCounts = /* @__PURE__ */ new Map();
10357
+ for (const f of dynamicFields) {
10358
+ const m = /^field_(.+)_N_/.exec(f.id);
10359
+ if (!m) continue;
10360
+ const prefix = m[1];
10361
+ for (const map of f.mappings || []) {
10362
+ if (idsOnThisPage.has(map.elementId)) {
10363
+ prefixCounts.set(prefix, (prefixCounts.get(prefix) || 0) + 1);
10364
+ break;
10365
+ }
10366
+ }
10273
10367
  }
10274
- if (targetProperty === "text" && node.type === "text") {
10275
- const overflowPolicy = String(node.overflowPolicy ?? "grow-and-push");
10276
- if (overflowPolicy !== "auto-shrink") {
10277
- delete node.height;
10368
+ let best;
10369
+ let bestCount = 0;
10370
+ for (const [prefix, count] of prefixCounts) {
10371
+ if (count > bestCount) {
10372
+ best = prefix;
10373
+ bestCount = count;
10278
10374
  }
10279
10375
  }
10280
- return true;
10376
+ if (best) templateKeyPrefix = best;
10281
10377
  }
10282
- if (node.children && Array.isArray(node.children)) {
10283
- if (setInTree(node.children, elementId, targetProperty, value)) return true;
10378
+ if (templateKeyPrefix) {
10379
+ out.push({ pageId: boundId, templateKeyPrefix });
10284
10380
  }
10285
10381
  }
10286
- return false;
10382
+ return out;
10287
10383
  }
10288
10384
  async function resolveTemplateData(options) {
10289
10385
  const { templateId, formData, supabaseUrl, supabaseAnonKey } = options;
10290
10386
  const template = await fetchRow(supabaseUrl, supabaseAnonKey, "templates", templateId);
10291
10387
  let config = template.config;
10388
+ const inlineFormSchema = template.form_schema;
10292
10389
  const defaultData = template.default_data;
10293
- if (defaultData) config = applyFormDataSimple(config, defaultData);
10294
- if (formData && Object.keys(formData).length > 0) config = applyFormDataSimple(config, formData);
10295
- return { config, templateName: template.name || "Untitled", templateId, price: template.price ?? 0 };
10390
+ if (inlineFormSchema && typeof inlineFormSchema === "object") {
10391
+ if (!Array.isArray(config.dynamicFields) && Array.isArray(inlineFormSchema.dynamicFields)) {
10392
+ config.dynamicFields = inlineFormSchema.dynamicFields;
10393
+ }
10394
+ if (!Array.isArray(config.fieldGroups) && Array.isArray(inlineFormSchema.fieldGroups)) {
10395
+ config.fieldGroups = inlineFormSchema.fieldGroups;
10396
+ }
10397
+ }
10398
+ normalizeLayoutModes(config);
10399
+ const repeatableSectionsInput = [];
10400
+ if (Array.isArray(inlineFormSchema == null ? void 0 : inlineFormSchema.repeatableSections)) {
10401
+ for (const r of inlineFormSchema.repeatableSections) {
10402
+ if ((r == null ? void 0 : r.nodeId) && (r == null ? void 0 : r.label)) {
10403
+ repeatableSectionsInput.push({
10404
+ nodeId: r.nodeId,
10405
+ label: r.label,
10406
+ minEntries: r.minEntries,
10407
+ maxEntries: r.maxEntries
10408
+ });
10409
+ }
10410
+ }
10411
+ if (repeatableSectionsInput.length > 0) {
10412
+ paintRepeatableSections(config, repeatableSectionsInput);
10413
+ }
10414
+ }
10415
+ const mergedFormData = {
10416
+ ...defaultData && typeof defaultData === "object" && !Array.isArray(defaultData) ? defaultData : {},
10417
+ ...formData ?? {}
10418
+ };
10419
+ const dynamicFields = config.dynamicFields;
10420
+ if (!Array.isArray(dynamicFields) || dynamicFields.length === 0) {
10421
+ return { config, templateName: template.name || "Untitled", templateId, price: template.price ?? 0 };
10422
+ }
10423
+ const mappings = [];
10424
+ for (const f of dynamicFields) {
10425
+ for (const m of f.mappings || []) {
10426
+ mappings.push({
10427
+ field_key: f.id,
10428
+ element_id: m.elementId,
10429
+ target_property: m.targetProperty || "text"
10430
+ });
10431
+ }
10432
+ }
10433
+ const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema);
10434
+ const resolvedConfig = applyFormDataToConfig(
10435
+ config,
10436
+ mappings,
10437
+ mergedFormData,
10438
+ repeatableSectionsInput,
10439
+ void 0,
10440
+ void 0,
10441
+ void 0,
10442
+ repeatablePagesInput.length > 0 ? repeatablePagesInput : void 0
10443
+ );
10444
+ return {
10445
+ config: resolvedConfig,
10446
+ templateName: template.name || "Untitled",
10447
+ templateId,
10448
+ price: template.price ?? 0
10449
+ };
10296
10450
  }
10297
10451
  async function resolveFromForm(options) {
10298
10452
  var _a, _b, _c, _d;