@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.js CHANGED
@@ -9256,7 +9256,7 @@ function applyTextCase(text, textCase) {
9256
9256
  return text;
9257
9257
  }
9258
9258
  }
9259
- function setInTree$1(nodes, elementId, targetProperty, value) {
9259
+ function setInTree(nodes, elementId, targetProperty, value) {
9260
9260
  for (const node of nodes) {
9261
9261
  if (node.id === elementId) {
9262
9262
  if (targetProperty === "link") {
@@ -9283,7 +9283,7 @@ function setInTree$1(nodes, elementId, targetProperty, value) {
9283
9283
  return true;
9284
9284
  }
9285
9285
  if (node.children && Array.isArray(node.children)) {
9286
- if (setInTree$1(node.children, elementId, targetProperty, value)) return true;
9286
+ if (setInTree(node.children, elementId, targetProperty, value)) return true;
9287
9287
  }
9288
9288
  }
9289
9289
  return false;
@@ -9524,11 +9524,54 @@ function getNestedRepeatableEntryCount(parentId, parentIndex, childId, formValue
9524
9524
  return Math.max(1, maxIndex);
9525
9525
  }
9526
9526
  function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsFromSchema, repeatableEntryCounts, repeatableNestedEntryCounts, displayFormatMap, repeatablePagesFromSchema) {
9527
- var _a, _b, _c, _d, _e;
9527
+ var _a, _b, _c, _d, _e, _f;
9528
9528
  const cloned = JSON.parse(JSON.stringify(config));
9529
9529
  if (!cloned.pages) return cloned;
9530
9530
  const dynamicFields = cloned.dynamicFields;
9531
9531
  let pages = cloned.pages;
9532
+ if (repeatablePagesFromSchema == null ? void 0 : repeatablePagesFromSchema.length) {
9533
+ const expanded = [];
9534
+ for (const page of pages) {
9535
+ const boundId = page.boundRepeatablePageId;
9536
+ const schemaEntry = boundId ? repeatablePagesFromSchema.find((p) => p.pageId === boundId) : void 0;
9537
+ if (!schemaEntry) {
9538
+ expanded.push(page);
9539
+ continue;
9540
+ }
9541
+ const prefix = schemaEntry.templateKeyPrefix;
9542
+ let inferredN = 1;
9543
+ const keyPrefix = `field_${prefix}_`;
9544
+ for (const k of Object.keys(formValues)) {
9545
+ if (!k.startsWith(keyPrefix)) continue;
9546
+ const rest = k.slice(keyPrefix.length);
9547
+ const m = /^(\d+)_/.exec(rest);
9548
+ if (m) inferredN = Math.max(inferredN, parseInt(m[1], 10));
9549
+ }
9550
+ const N = Math.max(0, schemaEntry.entryCount ?? inferredN);
9551
+ if (N === 0) {
9552
+ continue;
9553
+ }
9554
+ const basePageId = baseId(page.id);
9555
+ for (let i = 1; i <= N; i++) {
9556
+ const clonedPage = JSON.parse(JSON.stringify(page));
9557
+ clonedPage.id = N === 1 && i === 1 ? page.id : `${basePageId}_${i}`;
9558
+ clonedPage.__repeatablePagePrefix = prefix;
9559
+ clonedPage.__repeatablePageEntryIndex = i;
9560
+ clonedPage.__repeatablePageBaseId = basePageId;
9561
+ if ((_a = clonedPage.children) == null ? void 0 : _a.length) {
9562
+ const suffix = `${basePageId}_p${i}`;
9563
+ const reidChildren = (nodes) => nodes.map((n) => {
9564
+ const [reclone] = cloneNodeWithNewIds$1(n, suffix);
9565
+ return reclone;
9566
+ });
9567
+ if (i > 1) clonedPage.children = reidChildren(clonedPage.children);
9568
+ }
9569
+ expanded.push(clonedPage);
9570
+ }
9571
+ }
9572
+ pages = expanded;
9573
+ cloned.pages = pages;
9574
+ }
9532
9575
  const repeatableList = (repeatableSectionsFromSchema == null ? void 0 : repeatableSectionsFromSchema.length) ? repeatableSectionsFromSchema : getRepeatableFromConfig(pages);
9533
9576
  const nodeIds = repeatableList.map((r) => r.nodeId);
9534
9577
  const entryCountFromList = (baseNodeId) => {
@@ -9540,7 +9583,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9540
9583
  const elementIdToFieldId = /* @__PURE__ */ new Map();
9541
9584
  if (dynamicFields == null ? void 0 : dynamicFields.length) {
9542
9585
  for (const f of dynamicFields) {
9543
- const elId = (_b = (_a = f.mappings) == null ? void 0 : _a[0]) == null ? void 0 : _b.elementId;
9586
+ const elId = (_c = (_b = f.mappings) == null ? void 0 : _b[0]) == null ? void 0 : _c.elementId;
9544
9587
  if (elId) elementIdToFieldId.set(elId, f.id);
9545
9588
  }
9546
9589
  }
@@ -9754,7 +9797,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9754
9797
  }
9755
9798
  }
9756
9799
  for (const page of pages) {
9757
- if (page.children && setInTree$1(page.children, elementId, targetProperty, effectiveValue)) return true;
9800
+ if (page.children && setInTree(page.children, elementId, targetProperty, effectiveValue)) return true;
9758
9801
  }
9759
9802
  return false;
9760
9803
  };
@@ -9785,7 +9828,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9785
9828
  const [, parentId, parentIndexStr, childId, childIndexStr, fieldId] = nestedMatch;
9786
9829
  const value = formValues[key];
9787
9830
  const field = getFieldForRepeatableKey(fieldId, key);
9788
- const mapping = (_c = field == null ? void 0 : field.mappings) == null ? void 0 : _c[0];
9831
+ const mapping = (_d = field == null ? void 0 : field.mappings) == null ? void 0 : _d[0];
9789
9832
  if (!mapping) continue;
9790
9833
  const oldElementId = mapping.elementId;
9791
9834
  const mapKeyByElement = `${baseId(parentId)}_${parentIndexStr}_${baseId(childId)}_${childIndexStr}_${oldElementId}`;
@@ -9822,7 +9865,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9822
9865
  }
9823
9866
  }
9824
9867
  const repeatablePagePrefixSet = new Set(
9825
- [].map((p) => `field_${p.templateKeyPrefix}_`)
9868
+ (repeatablePagesFromSchema ?? []).map((p) => `field_${p.templateKeyPrefix}_`)
9826
9869
  );
9827
9870
  if (dynamicFields == null ? void 0 : dynamicFields.length) {
9828
9871
  for (const key of Object.keys(formValues)) {
@@ -9840,7 +9883,7 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9840
9883
  const [, nodeId, indexStr, fieldId] = match;
9841
9884
  const value = formValues[key];
9842
9885
  const field = getFieldForRepeatableKey(fieldId);
9843
- const mapping = (_d = field == null ? void 0 : field.mappings) == null ? void 0 : _d[0];
9886
+ const mapping = (_e = field == null ? void 0 : field.mappings) == null ? void 0 : _e[0];
9844
9887
  if (!mapping) continue;
9845
9888
  const oldElementId = mapping.elementId;
9846
9889
  const mapKeyByElement = `${baseId(nodeId)}_${indexStr}_${oldElementId}`;
@@ -9862,8 +9905,49 @@ function applyFormDataToConfig(config, mappings, formValues, repeatableSectionsF
9862
9905
  const value = formValues[m.field_key];
9863
9906
  applyValue(m.element_id, m.target_property, value, m.field_key);
9864
9907
  }
9908
+ if ((repeatablePagesFromSchema == null ? void 0 : repeatablePagesFromSchema.length) && (dynamicFields == null ? void 0 : dynamicFields.length)) {
9909
+ const findInPage = (page, originalId) => {
9910
+ var _a2;
9911
+ if (!((_a2 = page.children) == null ? void 0 : _a2.length)) return void 0;
9912
+ const findIn = (nodes) => {
9913
+ var _a3;
9914
+ for (const n of nodes) {
9915
+ const src = n.__sourceId;
9916
+ if (n.id === originalId || src === originalId || baseId(n.id) === baseId(originalId)) return n.id;
9917
+ if (isGroup(n) && ((_a3 = n.children) == null ? void 0 : _a3.length)) {
9918
+ const f = findIn(n.children);
9919
+ if (f) return f;
9920
+ }
9921
+ }
9922
+ return void 0;
9923
+ };
9924
+ return findIn(page.children);
9925
+ };
9926
+ for (const page of pages) {
9927
+ const meta = page;
9928
+ const prefix = meta.__repeatablePagePrefix;
9929
+ const entryIndex = meta.__repeatablePageEntryIndex;
9930
+ if (!prefix || !entryIndex) continue;
9931
+ const fieldIdPrefix = `field_${prefix}_N_`;
9932
+ for (const field of dynamicFields) {
9933
+ if (!field.id.startsWith(fieldIdPrefix)) continue;
9934
+ const fieldKeySuffix = field.id.slice(fieldIdPrefix.length);
9935
+ const formKey = `field_${prefix}_${entryIndex}_${fieldKeySuffix}`;
9936
+ if (!(formKey in formValues)) continue;
9937
+ const value = formValues[formKey];
9938
+ for (const m of field.mappings ?? []) {
9939
+ const originalId = m.elementId;
9940
+ const targetProperty = m.targetProperty || "text";
9941
+ if (!originalId) continue;
9942
+ const resolvedId = findInPage(page, originalId);
9943
+ if (!resolvedId) continue;
9944
+ if (page.children) setInTree(page.children, resolvedId, targetProperty, value);
9945
+ }
9946
+ }
9947
+ }
9948
+ }
9865
9949
  for (const page of pages) {
9866
- if ((_e = page.children) == null ? void 0 : _e.length) {
9950
+ if ((_f = page.children) == null ? void 0 : _f.length) {
9867
9951
  page.children = applyStackReflowToPageTree(page.children);
9868
9952
  }
9869
9953
  }
@@ -10228,52 +10312,122 @@ async function fetchDefaultForm(supabaseUrl, anonKey, formSchemaId) {
10228
10312
  const rows = await res.json();
10229
10313
  return rows.length ? rows[0] : null;
10230
10314
  }
10231
- function applyFormDataSimple(config, formData) {
10232
- var _a;
10233
- const cloned = JSON.parse(JSON.stringify(config));
10234
- if (!cloned.pages || !((_a = cloned.dynamicFields) == null ? void 0 : _a.length)) return cloned;
10235
- const dynamicFields = cloned.dynamicFields;
10236
- for (const field of dynamicFields) {
10237
- const value = formData[field.id];
10238
- if (value === void 0) continue;
10239
- for (const mapping of field.mappings) {
10240
- setInTree(cloned.pages.flatMap((p) => p.children), mapping.elementId, mapping.targetProperty, value);
10315
+ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10316
+ if (!Array.isArray(config == null ? void 0 : config.pages) || config.pages.length === 0) return [];
10317
+ const schemaPages = inlineFormSchema == null ? void 0 : inlineFormSchema.repeatablePages;
10318
+ const collectIds = (nodes, out2) => {
10319
+ for (const n of nodes ?? []) {
10320
+ if (n == null ? void 0 : n.id) out2.add(n.id);
10321
+ if (Array.isArray(n == null ? void 0 : n.children)) collectIds(n.children, out2);
10241
10322
  }
10242
- }
10243
- return cloned;
10244
- }
10245
- function setInTree(nodes, elementId, targetProperty, value) {
10246
- for (const node of nodes) {
10247
- if (node.id === elementId) {
10248
- if (targetProperty === "text" && node.type === "text" && typeof value === "string") {
10249
- node[targetProperty] = value === "" ? " " : value;
10250
- } else if (targetProperty === "link") {
10251
- node.linkConfig = { ...node.linkConfig || {}, url: String(value ?? "") };
10252
- } else {
10253
- node[targetProperty] = value;
10323
+ };
10324
+ const dynamicFields = config.dynamicFields;
10325
+ const out = [];
10326
+ for (const page of config.pages) {
10327
+ const boundId = page == null ? void 0 : page.boundRepeatablePageId;
10328
+ if (!boundId) continue;
10329
+ let templateKeyPrefix;
10330
+ if (Array.isArray(schemaPages)) {
10331
+ const found = schemaPages.find((rp) => (rp == null ? void 0 : rp.id) === boundId);
10332
+ if (found == null ? void 0 : found.templateKeyPrefix) templateKeyPrefix = found.templateKeyPrefix;
10333
+ }
10334
+ if (!templateKeyPrefix && Array.isArray(dynamicFields) && dynamicFields.length > 0) {
10335
+ const idsOnThisPage = /* @__PURE__ */ new Set();
10336
+ collectIds(page.children || [], idsOnThisPage);
10337
+ const prefixCounts = /* @__PURE__ */ new Map();
10338
+ for (const f of dynamicFields) {
10339
+ const m = /^field_(.+)_N_/.exec(f.id);
10340
+ if (!m) continue;
10341
+ const prefix = m[1];
10342
+ for (const map of f.mappings || []) {
10343
+ if (idsOnThisPage.has(map.elementId)) {
10344
+ prefixCounts.set(prefix, (prefixCounts.get(prefix) || 0) + 1);
10345
+ break;
10346
+ }
10347
+ }
10254
10348
  }
10255
- if (targetProperty === "text" && node.type === "text") {
10256
- const overflowPolicy = String(node.overflowPolicy ?? "grow-and-push");
10257
- if (overflowPolicy !== "auto-shrink") {
10258
- delete node.height;
10349
+ let best;
10350
+ let bestCount = 0;
10351
+ for (const [prefix, count] of prefixCounts) {
10352
+ if (count > bestCount) {
10353
+ best = prefix;
10354
+ bestCount = count;
10259
10355
  }
10260
10356
  }
10261
- return true;
10357
+ if (best) templateKeyPrefix = best;
10262
10358
  }
10263
- if (node.children && Array.isArray(node.children)) {
10264
- if (setInTree(node.children, elementId, targetProperty, value)) return true;
10359
+ if (templateKeyPrefix) {
10360
+ out.push({ pageId: boundId, templateKeyPrefix });
10265
10361
  }
10266
10362
  }
10267
- return false;
10363
+ return out;
10268
10364
  }
10269
10365
  async function resolveTemplateData(options) {
10270
10366
  const { templateId, formData, supabaseUrl, supabaseAnonKey } = options;
10271
10367
  const template = await fetchRow(supabaseUrl, supabaseAnonKey, "templates", templateId);
10272
10368
  let config = template.config;
10369
+ const inlineFormSchema = template.form_schema;
10273
10370
  const defaultData = template.default_data;
10274
- if (defaultData) config = applyFormDataSimple(config, defaultData);
10275
- if (formData && Object.keys(formData).length > 0) config = applyFormDataSimple(config, formData);
10276
- return { config, templateName: template.name || "Untitled", templateId, price: template.price ?? 0 };
10371
+ if (inlineFormSchema && typeof inlineFormSchema === "object") {
10372
+ if (!Array.isArray(config.dynamicFields) && Array.isArray(inlineFormSchema.dynamicFields)) {
10373
+ config.dynamicFields = inlineFormSchema.dynamicFields;
10374
+ }
10375
+ if (!Array.isArray(config.fieldGroups) && Array.isArray(inlineFormSchema.fieldGroups)) {
10376
+ config.fieldGroups = inlineFormSchema.fieldGroups;
10377
+ }
10378
+ }
10379
+ normalizeLayoutModes(config);
10380
+ const repeatableSectionsInput = [];
10381
+ if (Array.isArray(inlineFormSchema == null ? void 0 : inlineFormSchema.repeatableSections)) {
10382
+ for (const r of inlineFormSchema.repeatableSections) {
10383
+ if ((r == null ? void 0 : r.nodeId) && (r == null ? void 0 : r.label)) {
10384
+ repeatableSectionsInput.push({
10385
+ nodeId: r.nodeId,
10386
+ label: r.label,
10387
+ minEntries: r.minEntries,
10388
+ maxEntries: r.maxEntries
10389
+ });
10390
+ }
10391
+ }
10392
+ if (repeatableSectionsInput.length > 0) {
10393
+ paintRepeatableSections(config, repeatableSectionsInput);
10394
+ }
10395
+ }
10396
+ const mergedFormData = {
10397
+ ...defaultData && typeof defaultData === "object" && !Array.isArray(defaultData) ? defaultData : {},
10398
+ ...formData ?? {}
10399
+ };
10400
+ const dynamicFields = config.dynamicFields;
10401
+ if (!Array.isArray(dynamicFields) || dynamicFields.length === 0) {
10402
+ return { config, templateName: template.name || "Untitled", templateId, price: template.price ?? 0 };
10403
+ }
10404
+ const mappings = [];
10405
+ for (const f of dynamicFields) {
10406
+ for (const m of f.mappings || []) {
10407
+ mappings.push({
10408
+ field_key: f.id,
10409
+ element_id: m.elementId,
10410
+ target_property: m.targetProperty || "text"
10411
+ });
10412
+ }
10413
+ }
10414
+ const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema);
10415
+ const resolvedConfig = applyFormDataToConfig(
10416
+ config,
10417
+ mappings,
10418
+ mergedFormData,
10419
+ repeatableSectionsInput,
10420
+ void 0,
10421
+ void 0,
10422
+ void 0,
10423
+ repeatablePagesInput.length > 0 ? repeatablePagesInput : void 0
10424
+ );
10425
+ return {
10426
+ config: resolvedConfig,
10427
+ templateName: template.name || "Untitled",
10428
+ templateId,
10429
+ price: template.price ?? 0
10430
+ };
10277
10431
  }
10278
10432
  async function resolveFromForm(options) {
10279
10433
  var _a, _b, _c, _d;