@unlev/exeq 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.mjs CHANGED
@@ -344,6 +344,8 @@ function PdfViewer({
344
344
  selectedIds: selectedFieldIds,
345
345
  mode,
346
346
  currentSigner,
347
+ pageWidthPt: page.pdfWidth,
348
+ pageHeightPt: page.pdfHeight,
347
349
  renderContent: renderFieldContent
348
350
  },
349
351
  field.id
@@ -434,6 +436,8 @@ function FieldOverlayItem({
434
436
  selectedIds,
435
437
  mode,
436
438
  currentSigner,
439
+ pageWidthPt,
440
+ pageHeightPt,
437
441
  renderContent
438
442
  }) {
439
443
  const overlayRef = useRef(null);
@@ -560,7 +564,7 @@ function FieldOverlayItem({
560
564
  onMouseDown: handleMouseDown,
561
565
  children: [
562
566
  mode === "designer" && !isRedact && /* @__PURE__ */ jsx("div", { className: "field-overlay-label", style: { backgroundColor: color }, children: field.label }),
563
- renderContent ? renderContent(field) : /* @__PURE__ */ jsx("div", { className: "field-overlay-placeholder", children: field.value || field.placeholder }),
567
+ renderContent ? renderContent(field, pageWidthPt, pageHeightPt) : /* @__PURE__ */ jsx("div", { className: "field-overlay-placeholder", children: field.value || field.placeholder }),
564
568
  mode === "designer" && isSelected && !field.locked && /* @__PURE__ */ jsx(
565
569
  "div",
566
570
  {
@@ -2083,7 +2087,7 @@ function DesignerView({
2083
2087
  }
2084
2088
 
2085
2089
  // src/components/pdf-builder/SignerView.tsx
2086
- import { useState as useState7, useCallback as useCallback5, useEffect as useEffect3, useRef as useRef5 } from "react";
2090
+ import { useState as useState7, useCallback as useCallback5, useEffect as useEffect3, useRef as useRef5, useLayoutEffect } from "react";
2087
2091
 
2088
2092
  // src/utils/pdfFiller.ts
2089
2093
  import {
@@ -2210,6 +2214,19 @@ function resolveAllFormulas(fields, customTransforms, values) {
2210
2214
  });
2211
2215
  }
2212
2216
 
2217
+ // src/utils/fitText.ts
2218
+ function fitFontSize(o) {
2219
+ let size = o.autoShrink ? Math.min(o.fontSize, o.fieldHeightPt * 0.7) : o.fontSize;
2220
+ if (o.autoShrink && o.value) {
2221
+ const padding = 4;
2222
+ while (size > 4) {
2223
+ if (o.measure(o.value, size) <= o.fieldWidthPt - padding) break;
2224
+ size -= 0.5;
2225
+ }
2226
+ }
2227
+ return size;
2228
+ }
2229
+
2213
2230
  // src/utils/pdfFiller.ts
2214
2231
  var FONT_VARIANTS = {
2215
2232
  Helvetica: [StandardFonts.Helvetica, StandardFonts.HelveticaBold, StandardFonts.HelveticaOblique, StandardFonts.HelveticaBoldOblique],
@@ -2301,14 +2318,14 @@ async function renderFieldsOnPages(pages, fields, getFont, getSignature) {
2301
2318
  const font = await getFont(field.fontFamily || "Helvetica", field.bold, field.italic);
2302
2319
  const spacing = field.letterSpacing || 0;
2303
2320
  const textWidthAtSize = (text, size) => font.widthOfTextAtSize(text, size) + spacing * (text.length - 1);
2304
- let fontSize = field.autoShrink ? Math.min(field.fontSize, h * 0.7) : field.fontSize;
2305
- if (field.autoShrink) {
2306
- const padding = 4;
2307
- while (fontSize > 4) {
2308
- if (textWidthAtSize(field.value, fontSize) <= w - padding) break;
2309
- fontSize -= 0.5;
2310
- }
2311
- }
2321
+ const fontSize = fitFontSize({
2322
+ value: field.value,
2323
+ fontSize: field.fontSize,
2324
+ autoShrink: field.autoShrink,
2325
+ fieldWidthPt: w,
2326
+ fieldHeightPt: h,
2327
+ measure: textWidthAtSize
2328
+ });
2312
2329
  const textX = x + 2;
2313
2330
  const baselineY = y + h * 0.3;
2314
2331
  if (spacing > 0) {
@@ -2562,6 +2579,41 @@ function FieldNavigator({
2562
2579
 
2563
2580
  // src/components/pdf-builder/SignerView.tsx
2564
2581
  import { Fragment as Fragment3, jsx as jsx6, jsxs as jsxs6 } from "react/jsx-runtime";
2582
+ var useIsoLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect3;
2583
+ function shrinkToFit(el, maxPt, selfBox) {
2584
+ if (!el) return;
2585
+ const box = selfBox ? el : el.parentElement;
2586
+ if (!box) return;
2587
+ let pt = Math.max(1, maxPt);
2588
+ el.style.fontSize = `${pt}pt`;
2589
+ let guard = 0;
2590
+ while (pt > 2 && guard++ < 400 && (el.scrollWidth > box.clientWidth + 0.5 || el.scrollHeight > box.clientHeight + 0.5)) {
2591
+ pt -= 0.5;
2592
+ el.style.fontSize = `${pt}pt`;
2593
+ }
2594
+ }
2595
+ function FitInput({
2596
+ maxPt,
2597
+ ...rest
2598
+ }) {
2599
+ const ref = useRef5(null);
2600
+ useIsoLayoutEffect(() => {
2601
+ shrinkToFit(ref.current, maxPt, true);
2602
+ });
2603
+ return /* @__PURE__ */ jsx6("input", { ref, ...rest });
2604
+ }
2605
+ function FitText({
2606
+ maxPt,
2607
+ className,
2608
+ style,
2609
+ children
2610
+ }) {
2611
+ const ref = useRef5(null);
2612
+ useIsoLayoutEffect(() => {
2613
+ shrinkToFit(ref.current, maxPt, false);
2614
+ });
2615
+ return /* @__PURE__ */ jsx6("div", { ref, className, style, children });
2616
+ }
2565
2617
  function SignerView({
2566
2618
  apiKey,
2567
2619
  initialPdfUrl,
@@ -2818,7 +2870,7 @@ ${row.join(",")}`, "csv");
2818
2870
  return null;
2819
2871
  }
2820
2872
  if (field.formula) {
2821
- return /* @__PURE__ */ jsx6("div", { className: "field-overlay-value formula", children: field.value || "..." });
2873
+ return /* @__PURE__ */ jsx6(FitText, { maxPt: field.fontSize, className: "field-overlay-value formula", style: getCssTextStyle(field), children: field.value || "..." });
2822
2874
  }
2823
2875
  const editable = field.assignee === signer;
2824
2876
  if (!editable) {
@@ -2828,7 +2880,7 @@ ${row.join(",")}`, "csv");
2828
2880
  if (field.type === "checkbox") {
2829
2881
  return /* @__PURE__ */ jsx6("div", { className: "field-checkbox-display readonly", children: field.value === "true" ? "\u2713" : "" });
2830
2882
  }
2831
- return /* @__PURE__ */ jsx6("div", { className: "field-overlay-placeholder readonly", children: field.value || field.placeholder });
2883
+ return /* @__PURE__ */ jsx6(FitText, { maxPt: field.fontSize, className: "field-overlay-placeholder readonly", style: getCssTextStyle(field), children: field.value || field.placeholder });
2832
2884
  }
2833
2885
  if (isSignatureField(field)) {
2834
2886
  if (field.value) {
@@ -2850,10 +2902,9 @@ ${row.join(",")}`, "csv");
2850
2902
  );
2851
2903
  }
2852
2904
  if (field.type === "signed-date") {
2853
- return /* @__PURE__ */ jsx6("div", { className: "field-overlay-value", children: field.value || (/* @__PURE__ */ new Date()).toLocaleDateString() });
2905
+ return /* @__PURE__ */ jsx6(FitText, { maxPt: field.fontSize, className: "field-overlay-value", style: getCssTextStyle(field), children: field.value || (/* @__PURE__ */ new Date()).toLocaleDateString() });
2854
2906
  }
2855
2907
  const fontStyle = {
2856
- fontSize: `${field.fontSize}pt`,
2857
2908
  letterSpacing: field.letterSpacing ? `${field.letterSpacing}pt` : void 0,
2858
2909
  lineHeight: field.lineHeight ? `${field.lineHeight}` : void 0,
2859
2910
  ...getCssTextStyle(field)
@@ -2867,7 +2918,7 @@ ${row.join(",")}`, "csv");
2867
2918
  onChange: (e) => handleFieldUpdate(field.id, e.target.value),
2868
2919
  onFocus: () => setSelectedFieldId(field.id),
2869
2920
  onClick: (e) => e.stopPropagation(),
2870
- style: fontStyle,
2921
+ style: { fontSize: `${field.fontSize}pt`, ...fontStyle },
2871
2922
  children: [
2872
2923
  /* @__PURE__ */ jsx6("option", { value: "", children: field.placeholder || "Select..." }),
2873
2924
  (field.options || []).map((opt) => /* @__PURE__ */ jsx6("option", { value: opt, children: opt }, opt))
@@ -2876,8 +2927,9 @@ ${row.join(",")}`, "csv");
2876
2927
  );
2877
2928
  }
2878
2929
  return /* @__PURE__ */ jsx6(
2879
- "input",
2930
+ FitInput,
2880
2931
  {
2932
+ maxPt: field.fontSize,
2881
2933
  type: field.textSubtype === "email" ? "email" : field.textSubtype === "number" ? "number" : field.textSubtype === "phone" ? "tel" : field.textSubtype === "date" ? "date" : "text",
2882
2934
  className: "field-inline-input",
2883
2935
  value: field.value,