@pixldocs/canvas-renderer 0.5.0 → 0.5.2
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 +220 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +220 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -9275,7 +9275,7 @@ function applyTextCase(text, textCase) {
|
|
|
9275
9275
|
return text;
|
|
9276
9276
|
}
|
|
9277
9277
|
}
|
|
9278
|
-
function setInTree
|
|
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
|
|
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 = (
|
|
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
|
|
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 = (
|
|
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 = (
|
|
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 ((
|
|
9969
|
+
if ((_f = page.children) == null ? void 0 : _f.length) {
|
|
9886
9970
|
page.children = applyStackReflowToPageTree(page.children);
|
|
9887
9971
|
}
|
|
9888
9972
|
}
|
|
@@ -10247,52 +10331,146 @@ 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
|
|
10251
|
-
|
|
10252
|
-
const
|
|
10253
|
-
|
|
10254
|
-
|
|
10255
|
-
|
|
10256
|
-
|
|
10257
|
-
|
|
10258
|
-
|
|
10259
|
-
|
|
10334
|
+
function deriveRepeatablePagesFromTemplate(config, inlineFormSchema, formData) {
|
|
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 normalizeTemplateKeyPrefix = (prefix) => {
|
|
10338
|
+
if (!prefix || typeof prefix !== "string") return void 0;
|
|
10339
|
+
return prefix.replace(/^field_/, "");
|
|
10340
|
+
};
|
|
10341
|
+
const inferEntryCount = (prefix, minEntries) => {
|
|
10342
|
+
const normalizedPrefix = normalizeTemplateKeyPrefix(prefix);
|
|
10343
|
+
if (!normalizedPrefix) return minEntries === 0 ? 0 : void 0;
|
|
10344
|
+
let maxIndex = 0;
|
|
10345
|
+
const keyPrefix = `field_${normalizedPrefix}_`;
|
|
10346
|
+
for (const key of Object.keys(formData ?? {})) {
|
|
10347
|
+
if (!key.startsWith(keyPrefix)) continue;
|
|
10348
|
+
const rest = key.slice(keyPrefix.length);
|
|
10349
|
+
const match = /^(\d+)_/.exec(rest);
|
|
10350
|
+
if (match) maxIndex = Math.max(maxIndex, parseInt(match[1], 10));
|
|
10351
|
+
}
|
|
10352
|
+
if (maxIndex > 0) return maxIndex;
|
|
10353
|
+
return minEntries === 0 ? 0 : void 0;
|
|
10354
|
+
};
|
|
10355
|
+
const collectIds = (nodes, out2) => {
|
|
10356
|
+
for (const n of nodes ?? []) {
|
|
10357
|
+
if (n == null ? void 0 : n.id) out2.add(n.id);
|
|
10358
|
+
if (Array.isArray(n == null ? void 0 : n.children)) collectIds(n.children, out2);
|
|
10260
10359
|
}
|
|
10261
|
-
}
|
|
10262
|
-
|
|
10263
|
-
|
|
10264
|
-
|
|
10265
|
-
|
|
10266
|
-
if (
|
|
10267
|
-
|
|
10268
|
-
|
|
10269
|
-
|
|
10270
|
-
|
|
10271
|
-
|
|
10272
|
-
|
|
10360
|
+
};
|
|
10361
|
+
const dynamicFields = config.dynamicFields;
|
|
10362
|
+
const out = [];
|
|
10363
|
+
for (const page of config.pages) {
|
|
10364
|
+
const boundId = page == null ? void 0 : page.boundRepeatablePageId;
|
|
10365
|
+
if (!boundId) continue;
|
|
10366
|
+
let templateKeyPrefix;
|
|
10367
|
+
let entryCount;
|
|
10368
|
+
if (Array.isArray(schemaPages)) {
|
|
10369
|
+
const found = schemaPages.find((rp) => (rp == null ? void 0 : rp.id) === boundId || (rp == null ? void 0 : rp.id) === page.id);
|
|
10370
|
+
templateKeyPrefix = normalizeTemplateKeyPrefix(found == null ? void 0 : found.templateKeyPrefix);
|
|
10371
|
+
entryCount = inferEntryCount(templateKeyPrefix ?? (found == null ? void 0 : found.templateKeyPrefix) ?? "", found == null ? void 0 : found.minEntries);
|
|
10372
|
+
}
|
|
10373
|
+
if (!templateKeyPrefix && Array.isArray(dynamicFields) && dynamicFields.length > 0) {
|
|
10374
|
+
const idsOnThisPage = /* @__PURE__ */ new Set();
|
|
10375
|
+
collectIds(page.children || [], idsOnThisPage);
|
|
10376
|
+
const prefixCounts = /* @__PURE__ */ new Map();
|
|
10377
|
+
for (const f of dynamicFields) {
|
|
10378
|
+
const m = /^field_(.+)_N_/.exec(f.id);
|
|
10379
|
+
if (!m) continue;
|
|
10380
|
+
const prefix = m[1];
|
|
10381
|
+
for (const map of f.mappings || []) {
|
|
10382
|
+
if (idsOnThisPage.has(map.elementId)) {
|
|
10383
|
+
prefixCounts.set(prefix, (prefixCounts.get(prefix) || 0) + 1);
|
|
10384
|
+
break;
|
|
10385
|
+
}
|
|
10386
|
+
}
|
|
10273
10387
|
}
|
|
10274
|
-
|
|
10275
|
-
|
|
10276
|
-
|
|
10277
|
-
|
|
10388
|
+
let best;
|
|
10389
|
+
let bestCount = 0;
|
|
10390
|
+
for (const [prefix, count] of prefixCounts) {
|
|
10391
|
+
if (count > bestCount) {
|
|
10392
|
+
best = prefix;
|
|
10393
|
+
bestCount = count;
|
|
10278
10394
|
}
|
|
10279
10395
|
}
|
|
10280
|
-
|
|
10396
|
+
if (best) templateKeyPrefix = normalizeTemplateKeyPrefix(best);
|
|
10281
10397
|
}
|
|
10282
|
-
if (
|
|
10283
|
-
|
|
10398
|
+
if (templateKeyPrefix) {
|
|
10399
|
+
out.push({
|
|
10400
|
+
pageId: boundId,
|
|
10401
|
+
templateKeyPrefix,
|
|
10402
|
+
entryCount: entryCount ?? inferEntryCount(templateKeyPrefix)
|
|
10403
|
+
});
|
|
10284
10404
|
}
|
|
10285
10405
|
}
|
|
10286
|
-
return
|
|
10406
|
+
return out;
|
|
10287
10407
|
}
|
|
10288
10408
|
async function resolveTemplateData(options) {
|
|
10289
10409
|
const { templateId, formData, supabaseUrl, supabaseAnonKey } = options;
|
|
10290
10410
|
const template = await fetchRow(supabaseUrl, supabaseAnonKey, "templates", templateId);
|
|
10291
10411
|
let config = template.config;
|
|
10412
|
+
const inlineFormSchema = template.form_schema;
|
|
10292
10413
|
const defaultData = template.default_data;
|
|
10293
|
-
if (
|
|
10294
|
-
|
|
10295
|
-
|
|
10414
|
+
if (inlineFormSchema && typeof inlineFormSchema === "object") {
|
|
10415
|
+
if (!Array.isArray(config.dynamicFields) && Array.isArray(inlineFormSchema.dynamicFields)) {
|
|
10416
|
+
config.dynamicFields = inlineFormSchema.dynamicFields;
|
|
10417
|
+
}
|
|
10418
|
+
if (!Array.isArray(config.fieldGroups) && Array.isArray(inlineFormSchema.fieldGroups)) {
|
|
10419
|
+
config.fieldGroups = inlineFormSchema.fieldGroups;
|
|
10420
|
+
}
|
|
10421
|
+
}
|
|
10422
|
+
normalizeLayoutModes(config);
|
|
10423
|
+
const repeatableSectionsInput = [];
|
|
10424
|
+
if (Array.isArray(inlineFormSchema == null ? void 0 : inlineFormSchema.repeatableSections)) {
|
|
10425
|
+
for (const r of inlineFormSchema.repeatableSections) {
|
|
10426
|
+
if ((r == null ? void 0 : r.nodeId) && (r == null ? void 0 : r.label)) {
|
|
10427
|
+
repeatableSectionsInput.push({
|
|
10428
|
+
nodeId: r.nodeId,
|
|
10429
|
+
label: r.label,
|
|
10430
|
+
minEntries: r.minEntries,
|
|
10431
|
+
maxEntries: r.maxEntries
|
|
10432
|
+
});
|
|
10433
|
+
}
|
|
10434
|
+
}
|
|
10435
|
+
if (repeatableSectionsInput.length > 0) {
|
|
10436
|
+
paintRepeatableSections(config, repeatableSectionsInput);
|
|
10437
|
+
}
|
|
10438
|
+
}
|
|
10439
|
+
const mergedFormData = {
|
|
10440
|
+
...defaultData && typeof defaultData === "object" && !Array.isArray(defaultData) ? defaultData : {},
|
|
10441
|
+
...formData ?? {}
|
|
10442
|
+
};
|
|
10443
|
+
const dynamicFields = config.dynamicFields;
|
|
10444
|
+
if (!Array.isArray(dynamicFields) || dynamicFields.length === 0) {
|
|
10445
|
+
return { config, templateName: template.name || "Untitled", templateId, price: template.price ?? 0 };
|
|
10446
|
+
}
|
|
10447
|
+
const mappings = [];
|
|
10448
|
+
for (const f of dynamicFields) {
|
|
10449
|
+
for (const m of f.mappings || []) {
|
|
10450
|
+
mappings.push({
|
|
10451
|
+
field_key: f.id,
|
|
10452
|
+
element_id: m.elementId,
|
|
10453
|
+
target_property: m.targetProperty || "text"
|
|
10454
|
+
});
|
|
10455
|
+
}
|
|
10456
|
+
}
|
|
10457
|
+
const repeatablePagesInput = deriveRepeatablePagesFromTemplate(config, inlineFormSchema, mergedFormData);
|
|
10458
|
+
const resolvedConfig = applyFormDataToConfig(
|
|
10459
|
+
config,
|
|
10460
|
+
mappings,
|
|
10461
|
+
mergedFormData,
|
|
10462
|
+
repeatableSectionsInput,
|
|
10463
|
+
void 0,
|
|
10464
|
+
void 0,
|
|
10465
|
+
void 0,
|
|
10466
|
+
repeatablePagesInput.length > 0 ? repeatablePagesInput : void 0
|
|
10467
|
+
);
|
|
10468
|
+
return {
|
|
10469
|
+
config: resolvedConfig,
|
|
10470
|
+
templateName: template.name || "Untitled",
|
|
10471
|
+
templateId,
|
|
10472
|
+
price: template.price ?? 0
|
|
10473
|
+
};
|
|
10296
10474
|
}
|
|
10297
10475
|
async function resolveFromForm(options) {
|
|
10298
10476
|
var _a, _b, _c, _d;
|