@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.js CHANGED
@@ -6,6 +6,7 @@ import { forwardRef, useRef, useState, useMemo, useEffect, useCallback, useImper
6
6
  import { flushSync } from "react-dom";
7
7
  import { create } from "zustand";
8
8
  import * as fabric from "fabric";
9
+ import { createClient } from "@supabase/supabase-js";
9
10
  import { createRoot } from "react-dom/client";
10
11
  import { jsPDF, ShadingPattern } from "jspdf";
11
12
  import { svg2pdf } from "svg2pdf.js";
@@ -8764,6 +8765,50 @@ function applyThemeToConfig(config, themeOverrides) {
8764
8765
  }
8765
8766
  return cloned;
8766
8767
  }
8768
+ const SUPABASE_URL = void 0;
8769
+ const SUPABASE_PUBLISHABLE_KEY = void 0;
8770
+ createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, {
8771
+ auth: {
8772
+ storage: localStorage,
8773
+ persistSession: true,
8774
+ autoRefreshToken: true
8775
+ }
8776
+ });
8777
+ function repeatablePageToSection(page) {
8778
+ return {
8779
+ id: page.id,
8780
+ label: page.label,
8781
+ description: page.description,
8782
+ // Place pages after sections by default; explicit order preserved.
8783
+ order: typeof page.order === "number" ? page.order + 1e4 : 1e4,
8784
+ fields: page.fields,
8785
+ repeatable: true,
8786
+ minEntries: page.minEntries,
8787
+ maxEntries: page.maxEntries,
8788
+ templateKeyPrefix: page.templateKeyPrefix,
8789
+ children: page.children
8790
+ };
8791
+ }
8792
+ function getRenderableFormSections(schema) {
8793
+ if (!schema) return [];
8794
+ const base = schema.sections ?? [];
8795
+ const pages = schema.repeatablePages ?? [];
8796
+ if (!pages.length) return base;
8797
+ return [...base, ...pages.map(repeatablePageToSection)];
8798
+ }
8799
+ function buildRepeatablePagesInputForApply(schema, sectionState) {
8800
+ var _a;
8801
+ if (!((_a = schema == null ? void 0 : schema.repeatablePages) == null ? void 0 : _a.length)) return [];
8802
+ return schema.repeatablePages.map((p) => {
8803
+ const entries = sectionState == null ? void 0 : sectionState[p.id];
8804
+ const minEntries = p.minEntries != null ? Math.max(0, p.minEntries) : 1;
8805
+ let entryCount;
8806
+ if (Array.isArray(entries)) {
8807
+ entryCount = minEntries === 0 ? entries.length : Math.max(1, entries.length);
8808
+ }
8809
+ return { pageId: p.id, templateKeyPrefix: p.templateKeyPrefix, entryCount };
8810
+ });
8811
+ }
8767
8812
  function mapFormDefFieldType(t) {
8768
8813
  if (["text", "email", "tel", "textarea", "date", "url", "number", "toggle", "color", "image"].includes(t)) return t;
8769
8814
  if (t === "currency") return "number";
@@ -10312,9 +10357,27 @@ async function fetchDefaultForm(supabaseUrl, anonKey, formSchemaId) {
10312
10357
  const rows = await res.json();
10313
10358
  return rows.length ? rows[0] : null;
10314
10359
  }
10315
- function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10360
+ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema, formData) {
10316
10361
  if (!Array.isArray(config == null ? void 0 : config.pages) || config.pages.length === 0) return [];
10317
10362
  const schemaPages = inlineFormSchema == null ? void 0 : inlineFormSchema.repeatablePages;
10363
+ const normalizeTemplateKeyPrefix = (prefix) => {
10364
+ if (!prefix || typeof prefix !== "string") return void 0;
10365
+ return prefix.replace(/^field_/, "");
10366
+ };
10367
+ const inferEntryCount = (prefix, minEntries) => {
10368
+ const normalizedPrefix = normalizeTemplateKeyPrefix(prefix);
10369
+ if (!normalizedPrefix) return minEntries === 0 ? 0 : void 0;
10370
+ let maxIndex = 0;
10371
+ const keyPrefix = `field_${normalizedPrefix}_`;
10372
+ for (const key of Object.keys(formData ?? {})) {
10373
+ if (!key.startsWith(keyPrefix)) continue;
10374
+ const rest = key.slice(keyPrefix.length);
10375
+ const match = /^(\d+)_/.exec(rest);
10376
+ if (match) maxIndex = Math.max(maxIndex, parseInt(match[1], 10));
10377
+ }
10378
+ if (maxIndex > 0) return maxIndex;
10379
+ return minEntries === 0 ? 0 : void 0;
10380
+ };
10318
10381
  const collectIds = (nodes, out2) => {
10319
10382
  for (const n of nodes ?? []) {
10320
10383
  if (n == null ? void 0 : n.id) out2.add(n.id);
@@ -10327,9 +10390,11 @@ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10327
10390
  const boundId = page == null ? void 0 : page.boundRepeatablePageId;
10328
10391
  if (!boundId) continue;
10329
10392
  let templateKeyPrefix;
10393
+ let entryCount;
10330
10394
  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;
10395
+ const found = schemaPages.find((rp) => (rp == null ? void 0 : rp.id) === boundId || (rp == null ? void 0 : rp.id) === page.id);
10396
+ templateKeyPrefix = normalizeTemplateKeyPrefix(found == null ? void 0 : found.templateKeyPrefix);
10397
+ entryCount = inferEntryCount(templateKeyPrefix ?? (found == null ? void 0 : found.templateKeyPrefix) ?? "", found == null ? void 0 : found.minEntries);
10333
10398
  }
10334
10399
  if (!templateKeyPrefix && Array.isArray(dynamicFields) && dynamicFields.length > 0) {
10335
10400
  const idsOnThisPage = /* @__PURE__ */ new Set();
@@ -10354,10 +10419,14 @@ function deriveRepeatablePagesFromTemplate(config, inlineFormSchema) {
10354
10419
  bestCount = count;
10355
10420
  }
10356
10421
  }
10357
- if (best) templateKeyPrefix = best;
10422
+ if (best) templateKeyPrefix = normalizeTemplateKeyPrefix(best);
10358
10423
  }
10359
10424
  if (templateKeyPrefix) {
10360
- out.push({ pageId: boundId, templateKeyPrefix });
10425
+ out.push({
10426
+ pageId: boundId,
10427
+ templateKeyPrefix,
10428
+ entryCount: entryCount ?? inferEntryCount(templateKeyPrefix)
10429
+ });
10361
10430
  }
10362
10431
  }
10363
10432
  return out;
@@ -10411,7 +10480,7 @@ async function resolveTemplateData(options) {
10411
10480
  });
10412
10481
  }
10413
10482
  }
10414
- const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema);
10483
+ const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema, mergedFormData);
10415
10484
  const resolvedConfig = applyFormDataToConfig(
10416
10485
  config,
10417
10486
  mappings,
@@ -10430,7 +10499,7 @@ async function resolveTemplateData(options) {
10430
10499
  };
10431
10500
  }
10432
10501
  async function resolveFromForm(options) {
10433
- var _a, _b, _c, _d;
10502
+ var _a, _b, _c;
10434
10503
  const { templateId, formSchemaId, sectionState, themeId, supabaseUrl, supabaseAnonKey } = options;
10435
10504
  const [templateRow, formSchemaRow, defaultForm] = await Promise.all([
10436
10505
  fetchRow(supabaseUrl, supabaseAnonKey, "templates", templateId),
@@ -10439,6 +10508,7 @@ async function resolveFromForm(options) {
10439
10508
  ]);
10440
10509
  const templateConfig = templateRow.config;
10441
10510
  const templateFormSchema = templateRow.form_schema;
10511
+ const formSchema = formSchemaRow.schema;
10442
10512
  if (templateFormSchema) {
10443
10513
  if (!Array.isArray(templateConfig.dynamicFields) && Array.isArray(templateFormSchema.dynamicFields)) {
10444
10514
  templateConfig.dynamicFields = templateFormSchema.dynamicFields;
@@ -10452,7 +10522,7 @@ async function resolveFromForm(options) {
10452
10522
  if ((repeatableFromSchema == null ? void 0 : repeatableFromSchema.length) && templateConfig.pages) {
10453
10523
  paintRepeatableSections(templateConfig, repeatableFromSchema);
10454
10524
  }
10455
- const schemaSections = (_a = formSchemaRow.schema) == null ? void 0 : _a.sections;
10525
+ const schemaSections = getRenderableFormSections(formSchema);
10456
10526
  const repeatableNodeMap = /* @__PURE__ */ new Map();
10457
10527
  if (repeatableFromSchema) {
10458
10528
  for (const r of repeatableFromSchema) {
@@ -10463,12 +10533,12 @@ async function resolveFromForm(options) {
10463
10533
  let inferredSections;
10464
10534
  if (schemaSections == null ? void 0 : schemaSections.length) {
10465
10535
  inferredSections = formDefSectionsToInferred(schemaSections, repeatableNodeMap);
10466
- } else if ((_b = templateConfig.dynamicFields) == null ? void 0 : _b.length) {
10536
+ } else if ((_a = templateConfig.dynamicFields) == null ? void 0 : _a.length) {
10467
10537
  const groups = templateConfig.fieldGroups || [];
10468
10538
  inferredSections = inferFormSchemaFromTemplate(
10469
10539
  templateConfig.dynamicFields,
10470
10540
  groups,
10471
- ((_c = templateConfig.pages) == null ? void 0 : _c.length) ? { pages: templateConfig.pages } : void 0
10541
+ ((_b = templateConfig.pages) == null ? void 0 : _b.length) ? { pages: templateConfig.pages } : void 0
10472
10542
  );
10473
10543
  } else {
10474
10544
  inferredSections = [];
@@ -10553,9 +10623,10 @@ async function resolveFromForm(options) {
10553
10623
  repeatableList.length > 0 ? repeatableList : repeatableFromSchema ?? [],
10554
10624
  void 0,
10555
10625
  Object.keys(repeatableNestedEntryCounts).length > 0 ? repeatableNestedEntryCounts : void 0,
10556
- displayFormatMap.size > 0 ? displayFormatMap : void 0
10626
+ displayFormatMap.size > 0 ? displayFormatMap : void 0,
10627
+ buildRepeatablePagesInputForApply(formSchema, mergedSectionState)
10557
10628
  );
10558
- if ((_d = resolvedConfig.themeConfig) == null ? void 0 : _d.variables) {
10629
+ if ((_c = resolvedConfig.themeConfig) == null ? void 0 : _c.variables) {
10559
10630
  const baseOverrides = {};
10560
10631
  for (const [key, def] of Object.entries(resolvedConfig.themeConfig.variables)) {
10561
10632
  baseOverrides[key] = def.value;