@pixldocs/canvas-renderer 0.5.1 → 0.5.3

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
@@ -8,6 +8,7 @@ const react = require("react");
8
8
  const reactDom = require("react-dom");
9
9
  const zustand = require("zustand");
10
10
  const fabric = require("fabric");
11
+ const supabaseJs = require("@supabase/supabase-js");
11
12
  const client = require("react-dom/client");
12
13
  const jspdf = require("jspdf");
13
14
  const svg2pdf_js = require("svg2pdf.js");
@@ -8783,6 +8784,50 @@ function applyThemeToConfig(config, themeOverrides) {
8783
8784
  }
8784
8785
  return cloned;
8785
8786
  }
8787
+ const SUPABASE_URL = void 0;
8788
+ const SUPABASE_PUBLISHABLE_KEY = void 0;
8789
+ supabaseJs.createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, {
8790
+ auth: {
8791
+ storage: localStorage,
8792
+ persistSession: true,
8793
+ autoRefreshToken: true
8794
+ }
8795
+ });
8796
+ function repeatablePageToSection(page) {
8797
+ return {
8798
+ id: page.id,
8799
+ label: page.label,
8800
+ description: page.description,
8801
+ // Place pages after sections by default; explicit order preserved.
8802
+ order: typeof page.order === "number" ? page.order + 1e4 : 1e4,
8803
+ fields: page.fields,
8804
+ repeatable: true,
8805
+ minEntries: page.minEntries,
8806
+ maxEntries: page.maxEntries,
8807
+ templateKeyPrefix: page.templateKeyPrefix,
8808
+ children: page.children
8809
+ };
8810
+ }
8811
+ function getRenderableFormSections(schema) {
8812
+ if (!schema) return [];
8813
+ const base = schema.sections ?? [];
8814
+ const pages = schema.repeatablePages ?? [];
8815
+ if (!pages.length) return base;
8816
+ return [...base, ...pages.map(repeatablePageToSection)];
8817
+ }
8818
+ function buildRepeatablePagesInputForApply(schema, sectionState) {
8819
+ var _a;
8820
+ if (!((_a = schema == null ? void 0 : schema.repeatablePages) == null ? void 0 : _a.length)) return [];
8821
+ return schema.repeatablePages.map((p) => {
8822
+ const entries = sectionState == null ? void 0 : sectionState[p.id];
8823
+ const minEntries = p.minEntries != null ? Math.max(0, p.minEntries) : 1;
8824
+ let entryCount;
8825
+ if (Array.isArray(entries)) {
8826
+ entryCount = minEntries === 0 ? entries.length : Math.max(1, entries.length);
8827
+ }
8828
+ return { pageId: p.id, templateKeyPrefix: p.templateKeyPrefix, entryCount };
8829
+ });
8830
+ }
8786
8831
  function mapFormDefFieldType(t) {
8787
8832
  if (["text", "email", "tel", "textarea", "date", "url", "number", "toggle", "color", "image"].includes(t)) return t;
8788
8833
  if (t === "currency") return "number";
@@ -10331,9 +10376,27 @@ async function fetchDefaultForm(supabaseUrl, anonKey, formSchemaId) {
10331
10376
  const rows = await res.json();
10332
10377
  return rows.length ? rows[0] : null;
10333
10378
  }
10334
- function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10379
+ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema, formData) {
10335
10380
  if (!Array.isArray(config == null ? void 0 : config.pages) || config.pages.length === 0) return [];
10336
10381
  const schemaPages = inlineFormSchema == null ? void 0 : inlineFormSchema.repeatablePages;
10382
+ const normalizeTemplateKeyPrefix = (prefix) => {
10383
+ if (!prefix || typeof prefix !== "string") return void 0;
10384
+ return prefix.replace(/^field_/, "");
10385
+ };
10386
+ const inferEntryCount = (prefix, minEntries) => {
10387
+ const normalizedPrefix = normalizeTemplateKeyPrefix(prefix);
10388
+ if (!normalizedPrefix) return minEntries === 0 ? 0 : void 0;
10389
+ let maxIndex = 0;
10390
+ const keyPrefix = `field_${normalizedPrefix}_`;
10391
+ for (const key of Object.keys(formData ?? {})) {
10392
+ if (!key.startsWith(keyPrefix)) continue;
10393
+ const rest = key.slice(keyPrefix.length);
10394
+ const match = /^(\d+)_/.exec(rest);
10395
+ if (match) maxIndex = Math.max(maxIndex, parseInt(match[1], 10));
10396
+ }
10397
+ if (maxIndex > 0) return maxIndex;
10398
+ return minEntries === 0 ? 0 : void 0;
10399
+ };
10337
10400
  const collectIds = (nodes, out2) => {
10338
10401
  for (const n of nodes ?? []) {
10339
10402
  if (n == null ? void 0 : n.id) out2.add(n.id);
@@ -10346,9 +10409,11 @@ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10346
10409
  const boundId = page == null ? void 0 : page.boundRepeatablePageId;
10347
10410
  if (!boundId) continue;
10348
10411
  let templateKeyPrefix;
10412
+ let entryCount;
10349
10413
  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;
10414
+ const found = schemaPages.find((rp) => (rp == null ? void 0 : rp.id) === boundId || (rp == null ? void 0 : rp.id) === page.id);
10415
+ templateKeyPrefix = normalizeTemplateKeyPrefix(found == null ? void 0 : found.templateKeyPrefix);
10416
+ entryCount = inferEntryCount(templateKeyPrefix ?? (found == null ? void 0 : found.templateKeyPrefix) ?? "", found == null ? void 0 : found.minEntries);
10352
10417
  }
10353
10418
  if (!templateKeyPrefix && Array.isArray(dynamicFields) && dynamicFields.length > 0) {
10354
10419
  const idsOnThisPage = /* @__PURE__ */ new Set();
@@ -10373,10 +10438,14 @@ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10373
10438
  bestCount = count;
10374
10439
  }
10375
10440
  }
10376
- if (best) templateKeyPrefix = best;
10441
+ if (best) templateKeyPrefix = normalizeTemplateKeyPrefix(best);
10377
10442
  }
10378
10443
  if (templateKeyPrefix) {
10379
- out.push({ pageId: boundId, templateKeyPrefix });
10444
+ out.push({
10445
+ pageId: boundId,
10446
+ templateKeyPrefix,
10447
+ entryCount: entryCount ?? inferEntryCount(templateKeyPrefix)
10448
+ });
10380
10449
  }
10381
10450
  }
10382
10451
  return out;
@@ -10430,7 +10499,7 @@ async function resolveTemplateData(options) {
10430
10499
  });
10431
10500
  }
10432
10501
  }
10433
- const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema);
10502
+ const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema, mergedFormData);
10434
10503
  const resolvedConfig = applyFormDataToConfig(
10435
10504
  config,
10436
10505
  mappings,
@@ -10449,7 +10518,7 @@ async function resolveTemplateData(options) {
10449
10518
  };
10450
10519
  }
10451
10520
  async function resolveFromForm(options) {
10452
- var _a, _b, _c, _d;
10521
+ var _a, _b, _c;
10453
10522
  const { templateId, formSchemaId, sectionState, themeId, supabaseUrl, supabaseAnonKey } = options;
10454
10523
  const [templateRow, formSchemaRow, defaultForm] = await Promise.all([
10455
10524
  fetchRow(supabaseUrl, supabaseAnonKey, "templates", templateId),
@@ -10458,6 +10527,7 @@ async function resolveFromForm(options) {
10458
10527
  ]);
10459
10528
  const templateConfig = templateRow.config;
10460
10529
  const templateFormSchema = templateRow.form_schema;
10530
+ const formSchema = formSchemaRow.schema;
10461
10531
  if (templateFormSchema) {
10462
10532
  if (!Array.isArray(templateConfig.dynamicFields) && Array.isArray(templateFormSchema.dynamicFields)) {
10463
10533
  templateConfig.dynamicFields = templateFormSchema.dynamicFields;
@@ -10471,7 +10541,7 @@ async function resolveFromForm(options) {
10471
10541
  if ((repeatableFromSchema == null ? void 0 : repeatableFromSchema.length) && templateConfig.pages) {
10472
10542
  paintRepeatableSections(templateConfig, repeatableFromSchema);
10473
10543
  }
10474
- const schemaSections = (_a = formSchemaRow.schema) == null ? void 0 : _a.sections;
10544
+ const schemaSections = getRenderableFormSections(formSchema);
10475
10545
  const repeatableNodeMap = /* @__PURE__ */ new Map();
10476
10546
  if (repeatableFromSchema) {
10477
10547
  for (const r of repeatableFromSchema) {
@@ -10482,12 +10552,12 @@ async function resolveFromForm(options) {
10482
10552
  let inferredSections;
10483
10553
  if (schemaSections == null ? void 0 : schemaSections.length) {
10484
10554
  inferredSections = formDefSectionsToInferred(schemaSections, repeatableNodeMap);
10485
- } else if ((_b = templateConfig.dynamicFields) == null ? void 0 : _b.length) {
10555
+ } else if ((_a = templateConfig.dynamicFields) == null ? void 0 : _a.length) {
10486
10556
  const groups = templateConfig.fieldGroups || [];
10487
10557
  inferredSections = inferFormSchemaFromTemplate(
10488
10558
  templateConfig.dynamicFields,
10489
10559
  groups,
10490
- ((_c = templateConfig.pages) == null ? void 0 : _c.length) ? { pages: templateConfig.pages } : void 0
10560
+ ((_b = templateConfig.pages) == null ? void 0 : _b.length) ? { pages: templateConfig.pages } : void 0
10491
10561
  );
10492
10562
  } else {
10493
10563
  inferredSections = [];
@@ -10572,9 +10642,10 @@ async function resolveFromForm(options) {
10572
10642
  repeatableList.length > 0 ? repeatableList : repeatableFromSchema ?? [],
10573
10643
  void 0,
10574
10644
  Object.keys(repeatableNestedEntryCounts).length > 0 ? repeatableNestedEntryCounts : void 0,
10575
- displayFormatMap.size > 0 ? displayFormatMap : void 0
10645
+ displayFormatMap.size > 0 ? displayFormatMap : void 0,
10646
+ buildRepeatablePagesInputForApply(formSchema, mergedSectionState)
10576
10647
  );
10577
- if ((_d = resolvedConfig.themeConfig) == null ? void 0 : _d.variables) {
10648
+ if ((_c = resolvedConfig.themeConfig) == null ? void 0 : _c.variables) {
10578
10649
  const baseOverrides = {};
10579
10650
  for (const [key, def] of Object.entries(resolvedConfig.themeConfig.variables)) {
10580
10651
  baseOverrides[key] = def.value;