@campxdev/pdfme 1.3.0 → 1.3.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.
@@ -3,7 +3,7 @@
3
3
  var helper = require('./helper-DGH62Z2s.js');
4
4
  var SignaturePad = require('signature_pad');
5
5
  var lucide = require('lucide');
6
- var index = require('./index-CsEKt088.js');
6
+ var index = require('./index-BGO0T6u7.js');
7
7
  var buffer = require('buffer');
8
8
  var colors = require('./colors-BeBcxfhb.js');
9
9
  var uuid = require('uuid');
@@ -2372,15 +2372,21 @@ const variablePrefixSuffixWidget = (props) => {
2372
2372
  rootElement.appendChild(container);
2373
2373
  };
2374
2374
  const mapDynamicVariables$1 = (props) => {
2375
- const { rootElement, changeSchemas, activeSchema } = props;
2375
+ const { rootElement, changeSchemas, activeSchema, options } = props;
2376
2376
  const rtSchema = activeSchema;
2377
2377
  const text = rtSchema.text ?? '';
2378
2378
  if (!text) {
2379
2379
  rootElement.style.display = 'none';
2380
2380
  return;
2381
2381
  }
2382
+ const textVars = options?.variables?.textVariables ?? [];
2383
+ const defaults = {};
2384
+ for (const v of textVars) {
2385
+ if (v.defaultValue)
2386
+ defaults[v.value] = v.defaultValue;
2387
+ }
2382
2388
  const variables = JSON.parse((rtSchema.content && rtSchema.content !== '' ? rtSchema.content : '{}'));
2383
- const variablesChanged = updateVariablesFromText(text, variables);
2389
+ const variablesChanged = updateVariablesFromText(text, variables, defaults);
2384
2390
  const varNames = Object.keys(variables);
2385
2391
  if (variablesChanged) {
2386
2392
  changeSchemas([
@@ -2458,7 +2464,7 @@ const extractDotPaths = (expr) => {
2458
2464
  }
2459
2465
  return Array.from(paths);
2460
2466
  };
2461
- const buildNestedDefault = (obj, paths) => {
2467
+ const buildNestedDefault = (obj, paths, defaults) => {
2462
2468
  let added = false;
2463
2469
  for (const path of paths) {
2464
2470
  const parts = path.split('.');
@@ -2475,13 +2481,13 @@ const buildNestedDefault = (obj, paths) => {
2475
2481
  const leaf = parts[parts.length - 1];
2476
2482
  const isOldLeafFormat = leaf in current && (/^[A-Z][A-Z0-9_]+$/.test(String(current[leaf])) || /^\{\{.*\}\}$/.test(String(current[leaf])));
2477
2483
  if (!(leaf in current) || isOldLeafFormat) {
2478
- current[leaf] = path.replace(/\./g, ' ');
2484
+ current[leaf] = defaults?.[path] ?? path.replace(/\./g, ' ');
2479
2485
  added = true;
2480
2486
  }
2481
2487
  }
2482
2488
  return added;
2483
2489
  };
2484
- const updateVariablesFromText = (text, variables) => {
2490
+ const updateVariablesFromText = (text, variables, defaults) => {
2485
2491
  const blockRegex = /\{([^{}]+)\}/g;
2486
2492
  const allPaths = new Set();
2487
2493
  let blockMatch;
@@ -2514,7 +2520,7 @@ const updateVariablesFromText = (text, variables) => {
2514
2520
  /* not JSON, will rebuild */
2515
2521
  }
2516
2522
  }
2517
- const added = buildNestedDefault(obj, paths);
2523
+ const added = buildNestedDefault(obj, paths, defaults);
2518
2524
  if (!(root in variables) || added) {
2519
2525
  variables[root] = JSON.stringify(obj);
2520
2526
  changed = true;
@@ -2533,7 +2539,7 @@ const updateVariablesFromText = (text, variables) => {
2533
2539
  })();
2534
2540
  const isOldFormat = typeof existingVal === 'string' && (/^[A-Z][A-Z0-9_]*$/.test(existingVal) || /^\{\{.*\}\}$/.test(existingVal));
2535
2541
  if (!(root in variables) || isStaleObject || isOldFormat) {
2536
- variables[root] = root;
2542
+ variables[root] = defaults?.[root] ?? root;
2537
2543
  changed = true;
2538
2544
  }
2539
2545
  }
@@ -2585,34 +2591,11 @@ const uiRender$1 = async (arg) => {
2585
2591
  throw new Error('Text block not found. Ensure the text block has an id of "text-" + schema.id');
2586
2592
  }
2587
2593
  if (mode === 'designer') {
2588
- // Show formatted display initially: {value} → {{ value }}
2589
- if (text) {
2590
- textBlock.textContent = formatTemplateDisplay$1(text);
2591
- }
2592
- textBlock.addEventListener('focus', () => {
2593
- // Switch to raw template for editing
2594
- textBlock.textContent = text;
2595
- const sel = window.getSelection();
2596
- const range = document.createRange();
2597
- range.selectNodeContents(textBlock);
2598
- range.collapse(false);
2599
- sel?.removeAllRanges();
2600
- sel?.addRange(range);
2601
- });
2602
- textBlock.addEventListener('blur', () => {
2603
- text = textBlock.textContent || '';
2604
- if (onChange) {
2605
- onChange({ key: 'text', value: text });
2606
- }
2607
- // Show formatted display again
2608
- textBlock.textContent = formatTemplateDisplay$1(text);
2609
- });
2610
2594
  textBlock.addEventListener('keyup', (event) => {
2611
- const currentText = textBlock.textContent || '';
2595
+ text = textBlock.textContent || '';
2612
2596
  if (keyPressShouldBeChecked(event)) {
2613
- const newNumVariables = countUniqueVariableNames(currentText);
2597
+ const newNumVariables = countUniqueVariableNames(text);
2614
2598
  if (numVariables !== newNumVariables) {
2615
- text = currentText;
2616
2599
  if (onChange) {
2617
2600
  onChange({ key: 'text', value: text });
2618
2601
  }
@@ -2873,7 +2856,7 @@ const singleVariablePickerWidget = (props) => {
2873
2856
  rootElement.appendChild(container);
2874
2857
  };
2875
2858
  const mapDynamicVariables = (props) => {
2876
- const { rootElement, changeSchemas, activeSchema } = props;
2859
+ const { rootElement, changeSchemas, activeSchema, options } = props;
2877
2860
  const svtSchema = activeSchema;
2878
2861
  const text = svtSchema.text ?? '';
2879
2862
  if (!text) {
@@ -2900,7 +2883,10 @@ const mapDynamicVariables = (props) => {
2900
2883
  return false;
2901
2884
  }
2902
2885
  })();
2903
- variables[variable] = (!existingVal || isOldFormat || isStaleObject) ? variable.replace(/\./g, ' ') : existingVal;
2886
+ const textVars = options?.variables?.textVariables ?? [];
2887
+ const apiDefault = textVars.find((v) => v.value === variable)?.defaultValue;
2888
+ const fallbackDefault = variable.replace(/\./g, ' ');
2889
+ variables[variable] = (!existingVal || isOldFormat || isStaleObject) ? (apiDefault ?? fallbackDefault) : existingVal;
2904
2890
  }
2905
2891
  const content = varNames.length > 0 ? JSON.stringify(variables) : '';
2906
2892
  const changed = JSON.stringify(svtSchema.variables ?? []) !== JSON.stringify(varNames) ||
@@ -2959,9 +2945,19 @@ const formatTemplateDisplay = (text) => text.replace(/\{([^{}]+)\}/g, (_, expr)
2959
2945
  const uiRender = async (arg) => {
2960
2946
  const { mode, schema, options, _cache } = arg;
2961
2947
  if (mode === 'designer') {
2962
- // ReadOnly preview — show {{ variable }} formatted display
2948
+ // ReadOnly preview — show actual default value if available, else {{ variable }}
2963
2949
  const text = schema.text ?? '';
2964
- const displayText = text ? formatTemplateDisplay(text) : '';
2950
+ let displayText = '';
2951
+ if (text) {
2952
+ try {
2953
+ const content = JSON.parse(schema.content || '{}');
2954
+ const firstValue = Object.values(content)[0];
2955
+ displayText = (typeof firstValue === 'string' && firstValue) ? firstValue : formatTemplateDisplay(text);
2956
+ }
2957
+ catch {
2958
+ displayText = formatTemplateDisplay(text);
2959
+ }
2960
+ }
2965
2961
  const font = options?.font || helper.getDefaultFont();
2966
2962
  const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
2967
2963
  const textBlock = buildStyledTextContainer(arg, fontKitFont, displayText);
@@ -6,7 +6,7 @@ var helper = require('./chunks/helper-DGH62Z2s.js');
6
6
  var pluginRegistry = require('./chunks/pluginRegistry-D2vr9MUy.js');
7
7
  var expression = require('./chunks/expression-CoTzcL7v.js');
8
8
  var fontkit = require('fontkit');
9
- var index = require('./chunks/index-CsEKt088.js');
9
+ var index = require('./chunks/index-BGO0T6u7.js');
10
10
  require('pako');
11
11
  require('@pdf-lib/standard-fonts');
12
12
  require('@pdf-lib/upng');
package/dist/cjs/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  var ui = require('./ui.js');
4
- var index = require('./chunks/index-CsEKt088.js');
4
+ var index = require('./chunks/index-BGO0T6u7.js');
5
5
  var converter = require('./converter.js');
6
6
  var manipulator = require('./manipulator.js');
7
7
  var printDesignerEditor = require('./print-designer-editor.js');
8
8
  var schemas = require('./schemas.js');
9
- var fontSizePxWidget = require('./chunks/fontSizePxWidget-Drk8HKGH.js');
9
+ var fontSizePxWidget = require('./chunks/fontSizePxWidget-Dmj19RJR.js');
10
10
  var generator = require('./generator.js');
11
11
  var helper = require('./chunks/helper-DGH62Z2s.js');
12
12
  var common = require('./chunks/index-CoNR0xQU.js');
@@ -4,9 +4,9 @@ var React$c = require('react');
4
4
  var helper = require('./chunks/helper-DGH62Z2s.js');
5
5
  var pluginRegistry = require('./chunks/pluginRegistry-D2vr9MUy.js');
6
6
  var expression = require('./chunks/expression-CoTzcL7v.js');
7
- var index = require('./chunks/index-CsEKt088.js');
7
+ var index = require('./chunks/index-BGO0T6u7.js');
8
8
  var ReactDOM__default = require('react-dom');
9
- var fontSizePxWidget = require('./chunks/fontSizePxWidget-Drk8HKGH.js');
9
+ var fontSizePxWidget = require('./chunks/fontSizePxWidget-Dmj19RJR.js');
10
10
  var fontSizeTransform = require('./chunks/fontSizeTransform-CQQ_O42f.js');
11
11
  require('zod');
12
12
  require('buffer');
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./chunks/index-CsEKt088.js');
4
- var fontSizePxWidget = require('./chunks/fontSizePxWidget-Drk8HKGH.js');
3
+ var index = require('./chunks/index-BGO0T6u7.js');
4
+ var fontSizePxWidget = require('./chunks/fontSizePxWidget-Dmj19RJR.js');
5
5
  var lucide = require('lucide');
6
6
  var helper = require('./chunks/helper-DGH62Z2s.js');
7
7
  var AirDatepicker = require('air-datepicker');
package/dist/cjs/ui.js CHANGED
@@ -85648,16 +85648,19 @@ const UseDynamicFontSize = (a) => {
85648
85648
  }
85649
85649
  s.appendChild(E);
85650
85650
  }, mapDynamicVariables$1 = (a) => {
85651
- const { rootElement: s, changeSchemas: A, activeSchema: c } = a, u = c, f = u.text ?? "";
85652
- if (!f) {
85651
+ const { rootElement: s, changeSchemas: A, activeSchema: c, options: u } = a, f = c, m = f.text ?? "";
85652
+ if (!m) {
85653
85653
  s.style.display = "none";
85654
85654
  return;
85655
85655
  }
85656
- const m = JSON.parse(u.content && u.content !== "" ? u.content : "{}"), E = updateVariablesFromText(f, m), C = Object.keys(m);
85657
- E && A([
85658
- { key: "content", value: JSON.stringify(m), schemaId: c.id },
85659
- { key: "variables", value: C, schemaId: c.id },
85660
- { key: "readOnly", value: C.length === 0, schemaId: c.id }
85656
+ const E = u?.variables?.textVariables ?? [], C = {};
85657
+ for (const d of E)
85658
+ d.defaultValue && (C[d.value] = d.defaultValue);
85659
+ const g = JSON.parse(f.content && f.content !== "" ? f.content : "{}"), B = updateVariablesFromText(m, g, C), w = Object.keys(g);
85660
+ B && A([
85661
+ { key: "content", value: JSON.stringify(g), schemaId: c.id },
85662
+ { key: "variables", value: w, schemaId: c.id },
85663
+ { key: "readOnly", value: w.length === 0, schemaId: c.id }
85661
85664
  ]), s.style.display = "none";
85662
85665
  }, propPanel$1 = {
85663
85666
  schema: (a) => {
@@ -85741,57 +85744,57 @@ const UseDynamicFontSize = (a) => {
85741
85744
  RESERVED_NAMES$1.has(E) || c.add(f);
85742
85745
  }
85743
85746
  return Array.from(c);
85744
- }, buildNestedDefault = (a, s) => {
85745
- let A = false;
85746
- for (const c of s) {
85747
- const u = c.split(".");
85748
- if (u.length <= 1)
85747
+ }, buildNestedDefault = (a, s, A) => {
85748
+ let c = false;
85749
+ for (const u of s) {
85750
+ const f = u.split(".");
85751
+ if (f.length <= 1)
85749
85752
  continue;
85750
- let f = a;
85751
- for (let C = 1; C < u.length - 1; C++)
85752
- (!(u[C] in f) || typeof f[u[C]] != "object" || f[u[C]] === null) && (f[u[C]] = {}, A = true), f = f[u[C]];
85753
- const m = u[u.length - 1], E = m in f && (/^[A-Z][A-Z0-9_]+$/.test(String(f[m])) || /^\{\{.*\}\}$/.test(String(f[m])));
85754
- (!(m in f) || E) && (f[m] = c.replace(/\./g, " "), A = true);
85753
+ let m = a;
85754
+ for (let g = 1; g < f.length - 1; g++)
85755
+ (!(f[g] in m) || typeof m[f[g]] != "object" || m[f[g]] === null) && (m[f[g]] = {}, c = true), m = m[f[g]];
85756
+ const E = f[f.length - 1], C = E in m && (/^[A-Z][A-Z0-9_]+$/.test(String(m[E])) || /^\{\{.*\}\}$/.test(String(m[E])));
85757
+ (!(E in m) || C) && (m[E] = A?.[u] ?? u.replace(/\./g, " "), c = true);
85755
85758
  }
85756
- return A;
85757
- }, updateVariablesFromText = (a, s) => {
85758
- const A = /\{([^{}]+)\}/g, c = /* @__PURE__ */ new Set();
85759
- let u;
85760
- for (; (u = A.exec(a)) !== null; )
85761
- for (const C of extractDotPaths(u[1]))
85762
- c.add(C);
85763
- const f = /* @__PURE__ */ new Map();
85764
- for (const C of c) {
85765
- const g = C.split(".")[0];
85766
- f.has(g) || f.set(g, []), f.get(g).push(C);
85767
- }
85768
- const m = new Set(f.keys());
85769
- let E = false;
85770
- for (const [C, g] of f)
85771
- if (g.some((w) => w.includes("."))) {
85772
- let w = {};
85773
- if (C in s)
85759
+ return c;
85760
+ }, updateVariablesFromText = (a, s, A) => {
85761
+ const c = /\{([^{}]+)\}/g, u = /* @__PURE__ */ new Set();
85762
+ let f;
85763
+ for (; (f = c.exec(a)) !== null; )
85764
+ for (const g of extractDotPaths(f[1]))
85765
+ u.add(g);
85766
+ const m = /* @__PURE__ */ new Map();
85767
+ for (const g of u) {
85768
+ const B = g.split(".")[0];
85769
+ m.has(B) || m.set(B, []), m.get(B).push(g);
85770
+ }
85771
+ const E = new Set(m.keys());
85772
+ let C = false;
85773
+ for (const [g, B] of m)
85774
+ if (B.some((d) => d.includes("."))) {
85775
+ let d = {};
85776
+ if (g in s)
85774
85777
  try {
85775
- const p = JSON.parse(s[C]);
85776
- typeof p == "object" && p !== null && (w = p);
85778
+ const v = JSON.parse(s[g]);
85779
+ typeof v == "object" && v !== null && (d = v);
85777
85780
  } catch {
85778
85781
  }
85779
- const d = buildNestedDefault(w, g);
85780
- (!(C in s) || d) && (s[C] = JSON.stringify(w), E = true);
85782
+ const p = buildNestedDefault(d, B, A);
85783
+ (!(g in s) || p) && (s[g] = JSON.stringify(d), C = true);
85781
85784
  } else {
85782
- const w = s[C], d = typeof w == "string" && (() => {
85785
+ const d = s[g], p = typeof d == "string" && (() => {
85783
85786
  try {
85784
- const v = JSON.parse(w);
85785
- return typeof v == "object" && v !== null;
85787
+ const y = JSON.parse(d);
85788
+ return typeof y == "object" && y !== null;
85786
85789
  } catch {
85787
85790
  return false;
85788
85791
  }
85789
- })(), p = typeof w == "string" && (/^[A-Z][A-Z0-9_]*$/.test(w) || /^\{\{.*\}\}$/.test(w));
85790
- (!(C in s) || d || p) && (s[C] = C, E = true);
85792
+ })(), v = typeof d == "string" && (/^[A-Z][A-Z0-9_]*$/.test(d) || /^\{\{.*\}\}$/.test(d));
85793
+ (!(g in s) || p || v) && (s[g] = A?.[g] ?? g, C = true);
85791
85794
  }
85792
- for (const C of Object.keys(s))
85793
- m.has(C) || (delete s[C], E = true);
85794
- return E;
85795
+ for (const g of Object.keys(s))
85796
+ E.has(g) || (delete s[g], C = true);
85797
+ return C;
85795
85798
  }, formatTemplateDisplay$1 = (a) => a.replace(/\{([^{}]+)\}/g, (s, A) => "{{ " + A.trim().replace(/\./g, " ") + " }}"), uiRender$1 = async (a) => {
85796
85799
  const { value: s, schema: A, rootElement: c, mode: u, onChange: f, pageContext: m, ...E } = a;
85797
85800
  if (!A.text) {
@@ -85820,19 +85823,12 @@ const UseDynamicFontSize = (a) => {
85820
85823
  const B = c.querySelector("#text-" + String(A.id));
85821
85824
  if (!B)
85822
85825
  throw new Error('Text block not found. Ensure the text block has an id of "text-" + schema.id');
85823
- u === "designer" && (C && (B.textContent = formatTemplateDisplay$1(C)), B.addEventListener("focus", () => {
85824
- B.textContent = C;
85825
- const w = window.getSelection(), d = document.createRange();
85826
- d.selectNodeContents(B), d.collapse(false), w?.removeAllRanges(), w?.addRange(d);
85827
- }), B.addEventListener("blur", () => {
85828
- C = B.textContent || "", f && f({ key: "text", value: C }), B.textContent = formatTemplateDisplay$1(C);
85829
- }), B.addEventListener("keyup", (w) => {
85830
- const d = B.textContent || "";
85831
- if (keyPressShouldBeChecked(w)) {
85832
- const p = countUniqueVariableNames(d);
85833
- g !== p && (C = d, f && f({ key: "text", value: C }), g = p);
85826
+ u === "designer" && B.addEventListener("keyup", (w) => {
85827
+ if (C = B.textContent || "", keyPressShouldBeChecked(w)) {
85828
+ const d = countUniqueVariableNames(C);
85829
+ g !== d && (f && f({ key: "text", value: C }), g = d);
85834
85830
  }
85835
- }));
85831
+ });
85836
85832
  }, refreshExpressionSpans = (a, s, A) => {
85837
85833
  a.querySelectorAll("[data-expr]").forEach((c) => {
85838
85834
  const u = c.getAttribute("data-expr");
@@ -85996,28 +85992,28 @@ const UseDynamicFontSize = (a) => {
85996
85992
  A([{ key: "text", value: v, schemaId: c.id }]), g.value = "";
85997
85993
  }, E.appendChild(C), E.appendChild(g), s.appendChild(E);
85998
85994
  }, mapDynamicVariables = (a) => {
85999
- const { rootElement: s, changeSchemas: A, activeSchema: c } = a, u = c, f = u.text ?? "";
86000
- if (!f) {
85995
+ const { rootElement: s, changeSchemas: A, activeSchema: c, options: u } = a, f = c, m = f.text ?? "";
85996
+ if (!m) {
86001
85997
  s.style.display = "none";
86002
85998
  return;
86003
85999
  }
86004
- const m = [...f.matchAll(/\{([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)*)\}/g)].map((p) => p[1]), C = [...new Set(m)][0], g = C ? [C] : [], B = {};
86005
- if (C) {
86006
- const v = JSON.parse(u.content && u.content !== "" ? u.content : "{}")[C], y = typeof v == "string" && (/^[A-Z][A-Z0-9_]*$/.test(v) || /^\{\{.*\}\}$/.test(v)), b = typeof v == "string" && (() => {
86000
+ const E = [...m.matchAll(/\{([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)*)\}/g)].map((v) => v[1]), g = [...new Set(E)][0], B = g ? [g] : [], w = {};
86001
+ if (g) {
86002
+ const y = JSON.parse(f.content && f.content !== "" ? f.content : "{}")[g], b = typeof y == "string" && (/^[A-Z][A-Z0-9_]*$/.test(y) || /^\{\{.*\}\}$/.test(y)), Q = typeof y == "string" && (() => {
86007
86003
  try {
86008
- const Q = JSON.parse(v);
86009
- return typeof Q == "object" && Q !== null;
86004
+ const x = JSON.parse(y);
86005
+ return typeof x == "object" && x !== null;
86010
86006
  } catch {
86011
86007
  return false;
86012
86008
  }
86013
- })();
86014
- B[C] = !v || y || b ? C.replace(/\./g, " ") : v;
86009
+ })(), I = (u?.variables?.textVariables ?? []).find((x) => x.value === g)?.defaultValue, S = g.replace(/\./g, " ");
86010
+ w[g] = !y || b || Q ? I ?? S : y;
86015
86011
  }
86016
- const w = g.length > 0 ? JSON.stringify(B) : "";
86017
- (JSON.stringify(u.variables ?? []) !== JSON.stringify(g) || (u.content ?? "") !== w) && A([
86018
- { key: "content", value: w, schemaId: c.id },
86019
- { key: "variables", value: g, schemaId: c.id },
86020
- { key: "readOnly", value: g.length === 0, schemaId: c.id }
86012
+ const d = B.length > 0 ? JSON.stringify(w) : "";
86013
+ (JSON.stringify(f.variables ?? []) !== JSON.stringify(B) || (f.content ?? "") !== d) && A([
86014
+ { key: "content", value: d, schemaId: c.id },
86015
+ { key: "variables", value: B, schemaId: c.id },
86016
+ { key: "readOnly", value: B.length === 0, schemaId: c.id }
86021
86017
  ]), s.style.display = "none";
86022
86018
  }, propPanel = {
86023
86019
  schema: (a) => {
@@ -86057,7 +86053,16 @@ const UseDynamicFontSize = (a) => {
86057
86053
  }, formatTemplateDisplay = (a) => a.replace(/\{([^{}]+)\}/g, (s, A) => "{{ " + A.trim().replace(/\./g, " ") + " }}"), uiRender = async (a) => {
86058
86054
  const { mode: s, schema: A, options: c, _cache: u } = a;
86059
86055
  if (s === "designer") {
86060
- const f = A.text ?? "", m = f ? formatTemplateDisplay(f) : "", E = c?.font || getDefaultFont(), C = await getFontKitFont(A.fontName, E, u), g = buildStyledTextContainer(a, C, m);
86056
+ const f = A.text ?? "";
86057
+ let m = "";
86058
+ if (f)
86059
+ try {
86060
+ const B = JSON.parse(A.content || "{}"), w = Object.values(B)[0];
86061
+ m = typeof w == "string" && w ? w : formatTemplateDisplay(f);
86062
+ } catch {
86063
+ m = formatTemplateDisplay(f);
86064
+ }
86065
+ const E = c?.font || getDefaultFont(), C = await getFontKitFont(A.fontName, E, u), g = buildStyledTextContainer(a, C, m);
86061
86066
  g.textContent = m, g.style.cursor = "default";
86062
86067
  return;
86063
86068
  }
@@ -1,7 +1,7 @@
1
1
  import { E as px2mm, Z as ZOOM, c as b64toUint8Array, y as mm2pt, r as getFallbackFontName, D as DEFAULT_FONT_NAME, F as px2pt, A as pt2px } from './helper-DSxGxZ0j.js';
2
2
  import SignaturePad from 'signature_pad';
3
3
  import { Image, PenTool, Minus, Square, Circle, Table as Table$1, QrCode, Barcode } from 'lucide';
4
- import { D as DEFAULT_OPACITY, c as createSvgStr, i as isEditable, d as addAlphaToHex, r as readFile, e as convertForPdfLayoutProps, H as HEX_COLOR_PATTERN, h as hex2PrintingColor, f as rotatePoint, j as getDefaultCellStyles, u as uiRender$2, p as pdfRender$2, k as createErrorElm } from './index-D_-j4c4P.js';
4
+ import { D as DEFAULT_OPACITY, c as createSvgStr, i as isEditable, d as addAlphaToHex, r as readFile, e as convertForPdfLayoutProps, H as HEX_COLOR_PATTERN, h as hex2PrintingColor, f as rotatePoint, j as getDefaultCellStyles, u as uiRender$2, p as pdfRender$2, k as createErrorElm } from './index-iZeHwQ5z.js';
5
5
  import { Buffer } from 'buffer';
6
6
  import { t as toRadians } from './colors-jzbEzNi4.js';
7
7
  import { v1 } from 'uuid';
@@ -2350,15 +2350,21 @@ const variablePrefixSuffixWidget = (props) => {
2350
2350
  rootElement.appendChild(container);
2351
2351
  };
2352
2352
  const mapDynamicVariables$1 = (props) => {
2353
- const { rootElement, changeSchemas, activeSchema } = props;
2353
+ const { rootElement, changeSchemas, activeSchema, options } = props;
2354
2354
  const rtSchema = activeSchema;
2355
2355
  const text = rtSchema.text ?? '';
2356
2356
  if (!text) {
2357
2357
  rootElement.style.display = 'none';
2358
2358
  return;
2359
2359
  }
2360
+ const textVars = options?.variables?.textVariables ?? [];
2361
+ const defaults = {};
2362
+ for (const v of textVars) {
2363
+ if (v.defaultValue)
2364
+ defaults[v.value] = v.defaultValue;
2365
+ }
2360
2366
  const variables = JSON.parse((rtSchema.content && rtSchema.content !== '' ? rtSchema.content : '{}'));
2361
- const variablesChanged = updateVariablesFromText(text, variables);
2367
+ const variablesChanged = updateVariablesFromText(text, variables, defaults);
2362
2368
  const varNames = Object.keys(variables);
2363
2369
  if (variablesChanged) {
2364
2370
  changeSchemas([
@@ -2436,7 +2442,7 @@ const extractDotPaths = (expr) => {
2436
2442
  }
2437
2443
  return Array.from(paths);
2438
2444
  };
2439
- const buildNestedDefault = (obj, paths) => {
2445
+ const buildNestedDefault = (obj, paths, defaults) => {
2440
2446
  let added = false;
2441
2447
  for (const path of paths) {
2442
2448
  const parts = path.split('.');
@@ -2453,13 +2459,13 @@ const buildNestedDefault = (obj, paths) => {
2453
2459
  const leaf = parts[parts.length - 1];
2454
2460
  const isOldLeafFormat = leaf in current && (/^[A-Z][A-Z0-9_]+$/.test(String(current[leaf])) || /^\{\{.*\}\}$/.test(String(current[leaf])));
2455
2461
  if (!(leaf in current) || isOldLeafFormat) {
2456
- current[leaf] = path.replace(/\./g, ' ');
2462
+ current[leaf] = defaults?.[path] ?? path.replace(/\./g, ' ');
2457
2463
  added = true;
2458
2464
  }
2459
2465
  }
2460
2466
  return added;
2461
2467
  };
2462
- const updateVariablesFromText = (text, variables) => {
2468
+ const updateVariablesFromText = (text, variables, defaults) => {
2463
2469
  const blockRegex = /\{([^{}]+)\}/g;
2464
2470
  const allPaths = new Set();
2465
2471
  let blockMatch;
@@ -2492,7 +2498,7 @@ const updateVariablesFromText = (text, variables) => {
2492
2498
  /* not JSON, will rebuild */
2493
2499
  }
2494
2500
  }
2495
- const added = buildNestedDefault(obj, paths);
2501
+ const added = buildNestedDefault(obj, paths, defaults);
2496
2502
  if (!(root in variables) || added) {
2497
2503
  variables[root] = JSON.stringify(obj);
2498
2504
  changed = true;
@@ -2511,7 +2517,7 @@ const updateVariablesFromText = (text, variables) => {
2511
2517
  })();
2512
2518
  const isOldFormat = typeof existingVal === 'string' && (/^[A-Z][A-Z0-9_]*$/.test(existingVal) || /^\{\{.*\}\}$/.test(existingVal));
2513
2519
  if (!(root in variables) || isStaleObject || isOldFormat) {
2514
- variables[root] = root;
2520
+ variables[root] = defaults?.[root] ?? root;
2515
2521
  changed = true;
2516
2522
  }
2517
2523
  }
@@ -2563,34 +2569,11 @@ const uiRender$1 = async (arg) => {
2563
2569
  throw new Error('Text block not found. Ensure the text block has an id of "text-" + schema.id');
2564
2570
  }
2565
2571
  if (mode === 'designer') {
2566
- // Show formatted display initially: {value} → {{ value }}
2567
- if (text) {
2568
- textBlock.textContent = formatTemplateDisplay$1(text);
2569
- }
2570
- textBlock.addEventListener('focus', () => {
2571
- // Switch to raw template for editing
2572
- textBlock.textContent = text;
2573
- const sel = window.getSelection();
2574
- const range = document.createRange();
2575
- range.selectNodeContents(textBlock);
2576
- range.collapse(false);
2577
- sel?.removeAllRanges();
2578
- sel?.addRange(range);
2579
- });
2580
- textBlock.addEventListener('blur', () => {
2581
- text = textBlock.textContent || '';
2582
- if (onChange) {
2583
- onChange({ key: 'text', value: text });
2584
- }
2585
- // Show formatted display again
2586
- textBlock.textContent = formatTemplateDisplay$1(text);
2587
- });
2588
2572
  textBlock.addEventListener('keyup', (event) => {
2589
- const currentText = textBlock.textContent || '';
2573
+ text = textBlock.textContent || '';
2590
2574
  if (keyPressShouldBeChecked(event)) {
2591
- const newNumVariables = countUniqueVariableNames(currentText);
2575
+ const newNumVariables = countUniqueVariableNames(text);
2592
2576
  if (numVariables !== newNumVariables) {
2593
- text = currentText;
2594
2577
  if (onChange) {
2595
2578
  onChange({ key: 'text', value: text });
2596
2579
  }
@@ -2851,7 +2834,7 @@ const singleVariablePickerWidget = (props) => {
2851
2834
  rootElement.appendChild(container);
2852
2835
  };
2853
2836
  const mapDynamicVariables = (props) => {
2854
- const { rootElement, changeSchemas, activeSchema } = props;
2837
+ const { rootElement, changeSchemas, activeSchema, options } = props;
2855
2838
  const svtSchema = activeSchema;
2856
2839
  const text = svtSchema.text ?? '';
2857
2840
  if (!text) {
@@ -2878,7 +2861,10 @@ const mapDynamicVariables = (props) => {
2878
2861
  return false;
2879
2862
  }
2880
2863
  })();
2881
- variables[variable] = (!existingVal || isOldFormat || isStaleObject) ? variable.replace(/\./g, ' ') : existingVal;
2864
+ const textVars = options?.variables?.textVariables ?? [];
2865
+ const apiDefault = textVars.find((v) => v.value === variable)?.defaultValue;
2866
+ const fallbackDefault = variable.replace(/\./g, ' ');
2867
+ variables[variable] = (!existingVal || isOldFormat || isStaleObject) ? (apiDefault ?? fallbackDefault) : existingVal;
2882
2868
  }
2883
2869
  const content = varNames.length > 0 ? JSON.stringify(variables) : '';
2884
2870
  const changed = JSON.stringify(svtSchema.variables ?? []) !== JSON.stringify(varNames) ||
@@ -2937,9 +2923,19 @@ const formatTemplateDisplay = (text) => text.replace(/\{([^{}]+)\}/g, (_, expr)
2937
2923
  const uiRender = async (arg) => {
2938
2924
  const { mode, schema, options, _cache } = arg;
2939
2925
  if (mode === 'designer') {
2940
- // ReadOnly preview — show {{ variable }} formatted display
2926
+ // ReadOnly preview — show actual default value if available, else {{ variable }}
2941
2927
  const text = schema.text ?? '';
2942
- const displayText = text ? formatTemplateDisplay(text) : '';
2928
+ let displayText = '';
2929
+ if (text) {
2930
+ try {
2931
+ const content = JSON.parse(schema.content || '{}');
2932
+ const firstValue = Object.values(content)[0];
2933
+ displayText = (typeof firstValue === 'string' && firstValue) ? firstValue : formatTemplateDisplay(text);
2934
+ }
2935
+ catch {
2936
+ displayText = formatTemplateDisplay(text);
2937
+ }
2938
+ }
2943
2939
  const font = options?.font || getDefaultFont();
2944
2940
  const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
2945
2941
  const textBlock = buildStyledTextContainer(arg, fontKitFont, displayText);
@@ -4,7 +4,7 @@ import { v as isBlankPdf, o as getB64BasePdf, y as mm2pt, f as checkGenerateProp
4
4
  import { p as pluginRegistry, g as getDynamicTemplate } from './chunks/pluginRegistry-Bgrz5qWG.js';
5
5
  import { r as replacePlaceholders } from './chunks/expression-B-F1KCfk.js';
6
6
  import * as fontkit from 'fontkit';
7
- import { b as builtInPlugins, g as getDynamicHeightsForTable } from './chunks/index-D_-j4c4P.js';
7
+ import { b as builtInPlugins, g as getDynamicHeightsForTable } from './chunks/index-iZeHwQ5z.js';
8
8
  import 'pako';
9
9
  import '@pdf-lib/standard-fonts';
10
10
  import '@pdf-lib/upng';
package/dist/esm/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  export { Designer, Form, Viewer } from './ui.js';
2
- export { b as builtInPlugins, g as getDynamicHeightsForTable, s as richText, a as singleVariableText, t as text } from './chunks/index-D_-j4c4P.js';
2
+ export { b as builtInPlugins, g as getDynamicHeightsForTable, s as richText, a as singleVariableText, t as text } from './chunks/index-iZeHwQ5z.js';
3
3
  export { img2pdf, pdf2img, pdf2size } from './converter.js';
4
4
  export { insert, merge, move, organize, remove, rotate, split } from './manipulator.js';
5
5
  export { Canvas, PropPanel as DesignerPropPanel, PdfmeProvider, SchemaList, convertPdfToBase64, plugins, useDesigner, useDesignerContext } from './print-designer-editor.js';
6
6
  export { barcodes, checkbox, date, dateTime, multiVariableText, radioGroup, select, svg, table, time } from './schemas.js';
7
- export { d as dynamicBarcode, a as dynamicQrCode, b as dynamicTable, e as ellipse, f as fontSizePxWidget, i as image, l as line, r as rectangle, s as signature, v as variableBarcodes } from './chunks/fontSizePxWidget-CbzQrSSM.js';
7
+ export { d as dynamicBarcode, a as dynamicQrCode, b as dynamicTable, e as ellipse, f as fontSizePxWidget, i as image, l as line, r as rectangle, s as signature, v as variableBarcodes } from './chunks/fontSizePxWidget-BHHixwEk.js';
8
8
  export { generate } from './generator.js';
9
9
  export { B as BLANK_A4_PDF, a as BLANK_PDF, C as CUSTOM_A4_PDF, D as DEFAULT_FONT_NAME, M as MM_TO_PT_RATIO, P as PT_TO_MM_RATIO, b as PT_TO_PX_RATIO, Z as ZOOM, c as b64toUint8Array, d as checkDesignerProps, e as checkFont, f as checkGenerateProps, g as checkInputs, h as checkPreviewProps, i as checkTemplate, j as checkUIOptions, k as checkUIProps, l as cloneDeep, m as getAllFonts, n as getAllFontsWithGoogle, o as getB64BasePdf, p as getBuiltinFontsData, q as getDefaultFont, r as getFallbackFontName, s as getGoogleFontsData, t as getInputFromTemplate, u as googleFonts, v as isBlankPdf, w as isGoogleFont, x as isHexValid, y as mm2pt, z as pt2mm, A as pt2px, E as px2mm, F as px2pt } from './chunks/helper-DSxGxZ0j.js';
10
10
  export { PDFME_VERSION } from './chunks/index-DJkUkUo9.js';
@@ -4,11 +4,11 @@ import { v as isBlankPdf, Z as ZOOM, n as getAllFontsWithGoogle, f as checkGener
4
4
  export { m as getAllFonts, p as getBuiltinFontsData, s as getGoogleFontsData, t as getInputFromTemplate, F as px2pt } from './chunks/helper-DSxGxZ0j.js';
5
5
  import { p as pluginRegistry, g as getDynamicTemplate } from './chunks/pluginRegistry-Bgrz5qWG.js';
6
6
  import { r as replacePlaceholders } from './chunks/expression-B-F1KCfk.js';
7
- import { s as schema, a as schema$1, t as textSchema, g as getDynamicHeightsForTable, b as builtInPlugins } from './chunks/index-D_-j4c4P.js';
7
+ import { s as schema, a as schema$1, t as textSchema, g as getDynamicHeightsForTable, b as builtInPlugins } from './chunks/index-iZeHwQ5z.js';
8
8
  import * as ReactDOM__default from 'react-dom';
9
9
  import ReactDOM__default__default, { unstable_batchedUpdates, createPortal, flushSync, findDOMNode as findDOMNode$1 } from 'react-dom';
10
- import { d as dynamicBarcode, a as dynamicQrCode, e as ellipse, r as rectangle$1, l as lineSchema, b as dynamicTableSchema, s as signature, i as imageSchema } from './chunks/fontSizePxWidget-CbzQrSSM.js';
11
- export { f as fontSizePxWidget } from './chunks/fontSizePxWidget-CbzQrSSM.js';
10
+ import { d as dynamicBarcode, a as dynamicQrCode, e as ellipse, r as rectangle$1, l as lineSchema, b as dynamicTableSchema, s as signature, i as imageSchema } from './chunks/fontSizePxWidget-BHHixwEk.js';
11
+ export { f as fontSizePxWidget } from './chunks/fontSizePxWidget-BHHixwEk.js';
12
12
  export { t as templatePtToPx, a as templatePxToPt } from './chunks/fontSizeTransform-CkTVJdRF.js';
13
13
  import 'zod';
14
14
  import 'buffer';
@@ -1,7 +1,7 @@
1
- import { p as pdfRender$3, l as propPanel$2, u as uiRender$3, i as isEditable, m as getFontKitFont, n as buildStyledTextContainer, o as makeElementPlainTextContentEditable, c as createSvgStr, e as convertForPdfLayoutProps, d as addAlphaToHex, k as createErrorElm, D as DEFAULT_OPACITY, H as HEX_COLOR_PATTERN, q as getBodyWithRange, v as createSingleTable, w as getBody, j as getDefaultCellStyles, x as getColumnStylesPropPanelSchema, y as getCellPropPanelSchema, z as DEFAULT_FONT_COLOR, A as DEFAULT_CHARACTER_SPACING, B as DEFAULT_FONT_SIZE, C as DEFAULT_ALIGNMENT, E as getExtraFormatterSchema, F as Formatter, t as textSchema, G as DEFAULT_LINE_HEIGHT, V as VERTICAL_ALIGN_MIDDLE, I as mapVerticalAlignToFlex } from './chunks/index-D_-j4c4P.js';
2
- export { b as builtInPlugins, g as getDynamicHeightsForTable, s as richText, a as singleVariableText } from './chunks/index-D_-j4c4P.js';
3
- import { c as validateVariables, g as substituteVariables, h as createInsertVariableWidget, j as validateBarcodeInput, k as createBarCode, D as DEFAULT_BARCODE_COLOR, m as DEFAULT_BARCODE_BG_COLOR, n as DEFAULT_BARCODE_INCLUDETEXT, B as BARCODE_TYPES, r as rectangle, o as cellSchema } from './chunks/fontSizePxWidget-CbzQrSSM.js';
4
- export { d as dynamicBarcode, a as dynamicQrCode, b as dynamicTable, e as ellipse, f as fontSizePxWidget, i as image, l as line, s as signature, v as variableBarcodes } from './chunks/fontSizePxWidget-CbzQrSSM.js';
1
+ import { p as pdfRender$3, l as propPanel$2, u as uiRender$3, i as isEditable, m as getFontKitFont, n as buildStyledTextContainer, o as makeElementPlainTextContentEditable, c as createSvgStr, e as convertForPdfLayoutProps, d as addAlphaToHex, k as createErrorElm, D as DEFAULT_OPACITY, H as HEX_COLOR_PATTERN, q as getBodyWithRange, v as createSingleTable, w as getBody, j as getDefaultCellStyles, x as getColumnStylesPropPanelSchema, y as getCellPropPanelSchema, z as DEFAULT_FONT_COLOR, A as DEFAULT_CHARACTER_SPACING, B as DEFAULT_FONT_SIZE, C as DEFAULT_ALIGNMENT, E as getExtraFormatterSchema, F as Formatter, t as textSchema, G as DEFAULT_LINE_HEIGHT, V as VERTICAL_ALIGN_MIDDLE, I as mapVerticalAlignToFlex } from './chunks/index-iZeHwQ5z.js';
2
+ export { b as builtInPlugins, g as getDynamicHeightsForTable, s as richText, a as singleVariableText } from './chunks/index-iZeHwQ5z.js';
3
+ import { c as validateVariables, g as substituteVariables, h as createInsertVariableWidget, j as validateBarcodeInput, k as createBarCode, D as DEFAULT_BARCODE_COLOR, m as DEFAULT_BARCODE_BG_COLOR, n as DEFAULT_BARCODE_INCLUDETEXT, B as BARCODE_TYPES, r as rectangle, o as cellSchema } from './chunks/fontSizePxWidget-BHHixwEk.js';
4
+ export { d as dynamicBarcode, a as dynamicQrCode, b as dynamicTable, e as ellipse, f as fontSizePxWidget, i as image, l as line, s as signature, v as variableBarcodes } from './chunks/fontSizePxWidget-BHHixwEk.js';
5
5
  import { Type, Route, QrCode, Barcode, Table, CalendarClock, Calendar, Clock, ChevronDown, CircleDot, Circle, SquareCheck, Square } from 'lucide';
6
6
  import { q as getDefaultFont, E as px2mm, Z as ZOOM, r as getFallbackFontName, D as DEFAULT_FONT_NAME } from './chunks/helper-DSxGxZ0j.js';
7
7
  import AirDatepicker from 'air-datepicker';
package/dist/esm/ui.js CHANGED
@@ -85646,16 +85646,19 @@ const UseDynamicFontSize = (a) => {
85646
85646
  }
85647
85647
  s.appendChild(E);
85648
85648
  }, mapDynamicVariables$1 = (a) => {
85649
- const { rootElement: s, changeSchemas: A, activeSchema: c } = a, u = c, f = u.text ?? "";
85650
- if (!f) {
85649
+ const { rootElement: s, changeSchemas: A, activeSchema: c, options: u } = a, f = c, m = f.text ?? "";
85650
+ if (!m) {
85651
85651
  s.style.display = "none";
85652
85652
  return;
85653
85653
  }
85654
- const m = JSON.parse(u.content && u.content !== "" ? u.content : "{}"), E = updateVariablesFromText(f, m), C = Object.keys(m);
85655
- E && A([
85656
- { key: "content", value: JSON.stringify(m), schemaId: c.id },
85657
- { key: "variables", value: C, schemaId: c.id },
85658
- { key: "readOnly", value: C.length === 0, schemaId: c.id }
85654
+ const E = u?.variables?.textVariables ?? [], C = {};
85655
+ for (const d of E)
85656
+ d.defaultValue && (C[d.value] = d.defaultValue);
85657
+ const g = JSON.parse(f.content && f.content !== "" ? f.content : "{}"), B = updateVariablesFromText(m, g, C), w = Object.keys(g);
85658
+ B && A([
85659
+ { key: "content", value: JSON.stringify(g), schemaId: c.id },
85660
+ { key: "variables", value: w, schemaId: c.id },
85661
+ { key: "readOnly", value: w.length === 0, schemaId: c.id }
85659
85662
  ]), s.style.display = "none";
85660
85663
  }, propPanel$1 = {
85661
85664
  schema: (a) => {
@@ -85739,57 +85742,57 @@ const UseDynamicFontSize = (a) => {
85739
85742
  RESERVED_NAMES$1.has(E) || c.add(f);
85740
85743
  }
85741
85744
  return Array.from(c);
85742
- }, buildNestedDefault = (a, s) => {
85743
- let A = false;
85744
- for (const c of s) {
85745
- const u = c.split(".");
85746
- if (u.length <= 1)
85745
+ }, buildNestedDefault = (a, s, A) => {
85746
+ let c = false;
85747
+ for (const u of s) {
85748
+ const f = u.split(".");
85749
+ if (f.length <= 1)
85747
85750
  continue;
85748
- let f = a;
85749
- for (let C = 1; C < u.length - 1; C++)
85750
- (!(u[C] in f) || typeof f[u[C]] != "object" || f[u[C]] === null) && (f[u[C]] = {}, A = true), f = f[u[C]];
85751
- const m = u[u.length - 1], E = m in f && (/^[A-Z][A-Z0-9_]+$/.test(String(f[m])) || /^\{\{.*\}\}$/.test(String(f[m])));
85752
- (!(m in f) || E) && (f[m] = c.replace(/\./g, " "), A = true);
85751
+ let m = a;
85752
+ for (let g = 1; g < f.length - 1; g++)
85753
+ (!(f[g] in m) || typeof m[f[g]] != "object" || m[f[g]] === null) && (m[f[g]] = {}, c = true), m = m[f[g]];
85754
+ const E = f[f.length - 1], C = E in m && (/^[A-Z][A-Z0-9_]+$/.test(String(m[E])) || /^\{\{.*\}\}$/.test(String(m[E])));
85755
+ (!(E in m) || C) && (m[E] = A?.[u] ?? u.replace(/\./g, " "), c = true);
85753
85756
  }
85754
- return A;
85755
- }, updateVariablesFromText = (a, s) => {
85756
- const A = /\{([^{}]+)\}/g, c = /* @__PURE__ */ new Set();
85757
- let u;
85758
- for (; (u = A.exec(a)) !== null; )
85759
- for (const C of extractDotPaths(u[1]))
85760
- c.add(C);
85761
- const f = /* @__PURE__ */ new Map();
85762
- for (const C of c) {
85763
- const g = C.split(".")[0];
85764
- f.has(g) || f.set(g, []), f.get(g).push(C);
85765
- }
85766
- const m = new Set(f.keys());
85767
- let E = false;
85768
- for (const [C, g] of f)
85769
- if (g.some((w) => w.includes("."))) {
85770
- let w = {};
85771
- if (C in s)
85757
+ return c;
85758
+ }, updateVariablesFromText = (a, s, A) => {
85759
+ const c = /\{([^{}]+)\}/g, u = /* @__PURE__ */ new Set();
85760
+ let f;
85761
+ for (; (f = c.exec(a)) !== null; )
85762
+ for (const g of extractDotPaths(f[1]))
85763
+ u.add(g);
85764
+ const m = /* @__PURE__ */ new Map();
85765
+ for (const g of u) {
85766
+ const B = g.split(".")[0];
85767
+ m.has(B) || m.set(B, []), m.get(B).push(g);
85768
+ }
85769
+ const E = new Set(m.keys());
85770
+ let C = false;
85771
+ for (const [g, B] of m)
85772
+ if (B.some((d) => d.includes("."))) {
85773
+ let d = {};
85774
+ if (g in s)
85772
85775
  try {
85773
- const p = JSON.parse(s[C]);
85774
- typeof p == "object" && p !== null && (w = p);
85776
+ const v = JSON.parse(s[g]);
85777
+ typeof v == "object" && v !== null && (d = v);
85775
85778
  } catch {
85776
85779
  }
85777
- const d = buildNestedDefault(w, g);
85778
- (!(C in s) || d) && (s[C] = JSON.stringify(w), E = true);
85780
+ const p = buildNestedDefault(d, B, A);
85781
+ (!(g in s) || p) && (s[g] = JSON.stringify(d), C = true);
85779
85782
  } else {
85780
- const w = s[C], d = typeof w == "string" && (() => {
85783
+ const d = s[g], p = typeof d == "string" && (() => {
85781
85784
  try {
85782
- const v = JSON.parse(w);
85783
- return typeof v == "object" && v !== null;
85785
+ const y = JSON.parse(d);
85786
+ return typeof y == "object" && y !== null;
85784
85787
  } catch {
85785
85788
  return false;
85786
85789
  }
85787
- })(), p = typeof w == "string" && (/^[A-Z][A-Z0-9_]*$/.test(w) || /^\{\{.*\}\}$/.test(w));
85788
- (!(C in s) || d || p) && (s[C] = C, E = true);
85790
+ })(), v = typeof d == "string" && (/^[A-Z][A-Z0-9_]*$/.test(d) || /^\{\{.*\}\}$/.test(d));
85791
+ (!(g in s) || p || v) && (s[g] = A?.[g] ?? g, C = true);
85789
85792
  }
85790
- for (const C of Object.keys(s))
85791
- m.has(C) || (delete s[C], E = true);
85792
- return E;
85793
+ for (const g of Object.keys(s))
85794
+ E.has(g) || (delete s[g], C = true);
85795
+ return C;
85793
85796
  }, formatTemplateDisplay$1 = (a) => a.replace(/\{([^{}]+)\}/g, (s, A) => "{{ " + A.trim().replace(/\./g, " ") + " }}"), uiRender$1 = async (a) => {
85794
85797
  const { value: s, schema: A, rootElement: c, mode: u, onChange: f, pageContext: m, ...E } = a;
85795
85798
  if (!A.text) {
@@ -85818,19 +85821,12 @@ const UseDynamicFontSize = (a) => {
85818
85821
  const B = c.querySelector("#text-" + String(A.id));
85819
85822
  if (!B)
85820
85823
  throw new Error('Text block not found. Ensure the text block has an id of "text-" + schema.id');
85821
- u === "designer" && (C && (B.textContent = formatTemplateDisplay$1(C)), B.addEventListener("focus", () => {
85822
- B.textContent = C;
85823
- const w = window.getSelection(), d = document.createRange();
85824
- d.selectNodeContents(B), d.collapse(false), w?.removeAllRanges(), w?.addRange(d);
85825
- }), B.addEventListener("blur", () => {
85826
- C = B.textContent || "", f && f({ key: "text", value: C }), B.textContent = formatTemplateDisplay$1(C);
85827
- }), B.addEventListener("keyup", (w) => {
85828
- const d = B.textContent || "";
85829
- if (keyPressShouldBeChecked(w)) {
85830
- const p = countUniqueVariableNames(d);
85831
- g !== p && (C = d, f && f({ key: "text", value: C }), g = p);
85824
+ u === "designer" && B.addEventListener("keyup", (w) => {
85825
+ if (C = B.textContent || "", keyPressShouldBeChecked(w)) {
85826
+ const d = countUniqueVariableNames(C);
85827
+ g !== d && (f && f({ key: "text", value: C }), g = d);
85832
85828
  }
85833
- }));
85829
+ });
85834
85830
  }, refreshExpressionSpans = (a, s, A) => {
85835
85831
  a.querySelectorAll("[data-expr]").forEach((c) => {
85836
85832
  const u = c.getAttribute("data-expr");
@@ -85994,28 +85990,28 @@ const UseDynamicFontSize = (a) => {
85994
85990
  A([{ key: "text", value: v, schemaId: c.id }]), g.value = "";
85995
85991
  }, E.appendChild(C), E.appendChild(g), s.appendChild(E);
85996
85992
  }, mapDynamicVariables = (a) => {
85997
- const { rootElement: s, changeSchemas: A, activeSchema: c } = a, u = c, f = u.text ?? "";
85998
- if (!f) {
85993
+ const { rootElement: s, changeSchemas: A, activeSchema: c, options: u } = a, f = c, m = f.text ?? "";
85994
+ if (!m) {
85999
85995
  s.style.display = "none";
86000
85996
  return;
86001
85997
  }
86002
- const m = [...f.matchAll(/\{([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)*)\}/g)].map((p) => p[1]), C = [...new Set(m)][0], g = C ? [C] : [], B = {};
86003
- if (C) {
86004
- const v = JSON.parse(u.content && u.content !== "" ? u.content : "{}")[C], y = typeof v == "string" && (/^[A-Z][A-Z0-9_]*$/.test(v) || /^\{\{.*\}\}$/.test(v)), b = typeof v == "string" && (() => {
85998
+ const E = [...m.matchAll(/\{([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)*)\}/g)].map((v) => v[1]), g = [...new Set(E)][0], B = g ? [g] : [], w = {};
85999
+ if (g) {
86000
+ const y = JSON.parse(f.content && f.content !== "" ? f.content : "{}")[g], b = typeof y == "string" && (/^[A-Z][A-Z0-9_]*$/.test(y) || /^\{\{.*\}\}$/.test(y)), Q = typeof y == "string" && (() => {
86005
86001
  try {
86006
- const Q = JSON.parse(v);
86007
- return typeof Q == "object" && Q !== null;
86002
+ const x = JSON.parse(y);
86003
+ return typeof x == "object" && x !== null;
86008
86004
  } catch {
86009
86005
  return false;
86010
86006
  }
86011
- })();
86012
- B[C] = !v || y || b ? C.replace(/\./g, " ") : v;
86007
+ })(), I = (u?.variables?.textVariables ?? []).find((x) => x.value === g)?.defaultValue, S = g.replace(/\./g, " ");
86008
+ w[g] = !y || b || Q ? I ?? S : y;
86013
86009
  }
86014
- const w = g.length > 0 ? JSON.stringify(B) : "";
86015
- (JSON.stringify(u.variables ?? []) !== JSON.stringify(g) || (u.content ?? "") !== w) && A([
86016
- { key: "content", value: w, schemaId: c.id },
86017
- { key: "variables", value: g, schemaId: c.id },
86018
- { key: "readOnly", value: g.length === 0, schemaId: c.id }
86010
+ const d = B.length > 0 ? JSON.stringify(w) : "";
86011
+ (JSON.stringify(f.variables ?? []) !== JSON.stringify(B) || (f.content ?? "") !== d) && A([
86012
+ { key: "content", value: d, schemaId: c.id },
86013
+ { key: "variables", value: B, schemaId: c.id },
86014
+ { key: "readOnly", value: B.length === 0, schemaId: c.id }
86019
86015
  ]), s.style.display = "none";
86020
86016
  }, propPanel = {
86021
86017
  schema: (a) => {
@@ -86055,7 +86051,16 @@ const UseDynamicFontSize = (a) => {
86055
86051
  }, formatTemplateDisplay = (a) => a.replace(/\{([^{}]+)\}/g, (s, A) => "{{ " + A.trim().replace(/\./g, " ") + " }}"), uiRender = async (a) => {
86056
86052
  const { mode: s, schema: A, options: c, _cache: u } = a;
86057
86053
  if (s === "designer") {
86058
- const f = A.text ?? "", m = f ? formatTemplateDisplay(f) : "", E = c?.font || getDefaultFont(), C = await getFontKitFont(A.fontName, E, u), g = buildStyledTextContainer(a, C, m);
86054
+ const f = A.text ?? "";
86055
+ let m = "";
86056
+ if (f)
86057
+ try {
86058
+ const B = JSON.parse(A.content || "{}"), w = Object.values(B)[0];
86059
+ m = typeof w == "string" && w ? w : formatTemplateDisplay(f);
86060
+ } catch {
86061
+ m = formatTemplateDisplay(f);
86062
+ }
86063
+ const E = c?.font || getDefaultFont(), C = await getFontKitFont(A.fontName, E, u), g = buildStyledTextContainer(a, C, m);
86059
86064
  g.textContent = m, g.style.cursor = "default";
86060
86065
  return;
86061
86066
  }
@@ -3,6 +3,7 @@ export type { Template, Schema, SchemaForUI, ChangeSchemas, Size, BasePdf, Font,
3
3
  export type TemplateVariable = {
4
4
  label: string;
5
5
  value: string;
6
+ defaultValue?: string;
6
7
  };
7
8
  export type TemplateVariables = {
8
9
  textVariables: TemplateVariable[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/pdfme",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "sideEffects": false,
5
5
  "author": "hand-dot",
6
6
  "license": "MIT",