@pixldocs/canvas-renderer 0.5.27 → 0.5.28

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
@@ -2571,6 +2571,10 @@ async function _doLoadGoogleFont(fontFamily, weights) {
2571
2571
  return false;
2572
2572
  }
2573
2573
  }
2574
+ const getObjectId = (obj) => obj.__docuforgeId;
2575
+ const setObjectData = (obj, id) => {
2576
+ obj.__docuforgeId = id;
2577
+ };
2574
2578
  const FONTS_TO_PRELOAD = [
2575
2579
  "Open Sans",
2576
2580
  "Playfair Display",
@@ -2710,6 +2714,35 @@ const clearFabricCharCache = () => {
2710
2714
  };
2711
2715
  const clearFontCacheAndRerender = (canvas) => {
2712
2716
  clearFabricCharCache();
2717
+ const collectUnderlineMetrics = (obj) => {
2718
+ var _a, _b;
2719
+ if (obj instanceof fabric.Textbox) {
2720
+ if (!obj.underline) return [];
2721
+ const lineWidths = obj.__lineWidths;
2722
+ return [{
2723
+ id: getObjectId(obj) ?? null,
2724
+ text: (obj.text || "").slice(0, 120),
2725
+ textLength: ((_a = obj.text) == null ? void 0 : _a.length) ?? 0,
2726
+ fontFamily: obj.fontFamily,
2727
+ fontSize: obj.fontSize,
2728
+ fontWeight: obj.fontWeight,
2729
+ width: obj.width ?? null,
2730
+ height: obj.height ?? null,
2731
+ scaleX: obj.scaleX,
2732
+ scaleY: obj.scaleY,
2733
+ lineCount: ((_b = obj.textLines) == null ? void 0 : _b.length) ?? 0,
2734
+ maxLineWidth: lineWidths && lineWidths.length > 0 ? Math.max(...lineWidths) : null
2735
+ }];
2736
+ }
2737
+ if (obj instanceof fabric.Group) {
2738
+ return (obj._objects || []).flatMap((child) => collectUnderlineMetrics(child));
2739
+ }
2740
+ return [];
2741
+ };
2742
+ const beforeMetrics = canvas.getObjects().flatMap(collectUnderlineMetrics);
2743
+ if (beforeMetrics.length > 0) {
2744
+ console.log("[canvas-renderer][underline-debug] before-rerender", beforeMetrics);
2745
+ }
2713
2746
  const resetTextboxLayoutInternals = (obj) => {
2714
2747
  var _a;
2715
2748
  (_a = obj._clearCache) == null ? void 0 : _a.call(obj);
@@ -2724,7 +2757,7 @@ const clearFontCacheAndRerender = (canvas) => {
2724
2757
  obj.dirty = true;
2725
2758
  };
2726
2759
  const fixTextbox = (obj) => {
2727
- var _a, _b;
2760
+ var _a, _b, _c;
2728
2761
  if (obj instanceof fabric.Textbox) {
2729
2762
  const savedWidth = obj.width;
2730
2763
  const savedScaleX = obj.scaleX;
@@ -2737,15 +2770,35 @@ const clearFontCacheAndRerender = (canvas) => {
2737
2770
  obj.initDimensions();
2738
2771
  obj.set({ width: savedWidth, scaleX: savedScaleX, scaleY: savedScaleY, dirty: true });
2739
2772
  }
2740
- (_a = obj._clearCache) == null ? void 0 : _a.call(obj);
2773
+ if (obj.underline) {
2774
+ const lineWidths = obj.__lineWidths;
2775
+ console.log("[canvas-renderer][underline-debug] textbox-rerender", {
2776
+ id: getObjectId(obj) ?? null,
2777
+ text: (obj.text || "").slice(0, 120),
2778
+ fontFamily: obj.fontFamily,
2779
+ fontSize: obj.fontSize,
2780
+ savedWidth,
2781
+ finalWidth: obj.width ?? null,
2782
+ finalHeight: obj.height ?? null,
2783
+ scaleX: obj.scaleX,
2784
+ scaleY: obj.scaleY,
2785
+ lineCount: ((_a = obj.textLines) == null ? void 0 : _a.length) ?? 0,
2786
+ maxLineWidth: lineWidths && lineWidths.length > 0 ? Math.max(...lineWidths) : null
2787
+ });
2788
+ }
2789
+ (_b = obj._clearCache) == null ? void 0 : _b.call(obj);
2741
2790
  obj.setCoords();
2742
2791
  } else if (obj instanceof fabric.Group) {
2743
- (_b = obj._objects) == null ? void 0 : _b.forEach(fixTextbox);
2792
+ (_c = obj._objects) == null ? void 0 : _c.forEach(fixTextbox);
2744
2793
  obj.dirty = true;
2745
2794
  obj.setCoords();
2746
2795
  }
2747
2796
  };
2748
2797
  canvas.getObjects().forEach(fixTextbox);
2798
+ const afterMetrics = canvas.getObjects().flatMap(collectUnderlineMetrics);
2799
+ if (afterMetrics.length > 0) {
2800
+ console.log("[canvas-renderer][underline-debug] after-rerender", afterMetrics);
2801
+ }
2749
2802
  canvas.requestRenderAll();
2750
2803
  };
2751
2804
  const ensureFontLoaded = async (fontFamily) => {
@@ -2783,10 +2836,6 @@ const setupFontLoadingListener = (canvas, afterRerender) => {
2783
2836
  };
2784
2837
  };
2785
2838
  preloadAllFonts();
2786
- const getObjectId = (obj) => obj.__docuforgeId;
2787
- const setObjectData = (obj, id) => {
2788
- obj.__docuforgeId = id;
2789
- };
2790
2839
  const svgTextCache = /* @__PURE__ */ new Map();
2791
2840
  const htmlImageCache = /* @__PURE__ */ new Map();
2792
2841
  const MAX_CACHE_ENTRIES = 200;
@@ -11844,6 +11893,21 @@ async function ensureFontsForResolvedConfig(config) {
11844
11893
  });
11845
11894
  }
11846
11895
  }
11896
+ const PREVIEW_DEBUG_PREFIX = "[canvas-renderer][preview-debug]";
11897
+ function countUnderlinedNodes(config) {
11898
+ var _a;
11899
+ if (!((_a = config == null ? void 0 : config.pages) == null ? void 0 : _a.length)) return 0;
11900
+ let count = 0;
11901
+ const walk = (nodes) => {
11902
+ var _a2;
11903
+ for (const node of nodes || []) {
11904
+ if (node == null ? void 0 : node.underline) count += 1;
11905
+ if ((_a2 = node == null ? void 0 : node.children) == null ? void 0 : _a2.length) walk(node.children);
11906
+ }
11907
+ };
11908
+ for (const page of config.pages) walk(page.children || []);
11909
+ return count;
11910
+ }
11847
11911
  function PixldocsPreview(props) {
11848
11912
  const {
11849
11913
  pageIndex = 0,
@@ -11871,6 +11935,7 @@ function PixldocsPreview(props) {
11871
11935
  if (!isResolveMode) {
11872
11936
  setResolvedConfig(null);
11873
11937
  setCanvasSettled(false);
11938
+ console.log(PREVIEW_DEBUG_PREFIX, "config-mode active");
11874
11939
  return;
11875
11940
  }
11876
11941
  const p = props;
@@ -11879,6 +11944,12 @@ function PixldocsPreview(props) {
11879
11944
  setIsLoading(true);
11880
11945
  setFontsReady(false);
11881
11946
  setCanvasSettled(false);
11947
+ console.log(PREVIEW_DEBUG_PREFIX, "resolve-start", {
11948
+ templateId: p.templateId,
11949
+ formSchemaId: p.formSchemaId,
11950
+ themeId: p.themeId ?? null,
11951
+ pageIndex
11952
+ });
11882
11953
  resolveFromForm({
11883
11954
  templateId: p.templateId,
11884
11955
  formSchemaId: p.formSchemaId,
@@ -11887,15 +11958,22 @@ function PixldocsPreview(props) {
11887
11958
  supabaseUrl: p.supabaseUrl,
11888
11959
  supabaseAnonKey: p.supabaseAnonKey
11889
11960
  }).then((resolved) => {
11961
+ var _a, _b;
11890
11962
  if (!cancelled) {
11963
+ console.log(PREVIEW_DEBUG_PREFIX, "resolve-done", {
11964
+ pages: ((_b = (_a = resolved.config) == null ? void 0 : _a.pages) == null ? void 0 : _b.length) ?? 0,
11965
+ underlinedNodes: countUnderlinedNodes(resolved.config)
11966
+ });
11891
11967
  setResolvedConfig(resolved.config);
11892
11968
  ensureFontsForResolvedConfig(resolved.config).then(() => {
11893
11969
  if (!cancelled) {
11970
+ console.log(PREVIEW_DEBUG_PREFIX, "resolve-mode fonts queued");
11894
11971
  setFontsReady(true);
11895
11972
  setIsLoading(false);
11896
11973
  }
11897
- }).catch(() => {
11974
+ }).catch((err) => {
11898
11975
  if (!cancelled) {
11976
+ console.warn(PREVIEW_DEBUG_PREFIX, "resolve-mode fonts queue failed", err);
11899
11977
  setFontsReady(true);
11900
11978
  setIsLoading(false);
11901
11979
  }
@@ -11904,6 +11982,7 @@ function PixldocsPreview(props) {
11904
11982
  }).catch((err) => {
11905
11983
  if (!cancelled) {
11906
11984
  setIsLoading(false);
11985
+ console.warn(PREVIEW_DEBUG_PREFIX, "resolve-error", err);
11907
11986
  onError == null ? void 0 : onError(err instanceof Error ? err : new Error(String(err)));
11908
11987
  }
11909
11988
  });
@@ -11920,18 +11999,28 @@ function PixldocsPreview(props) {
11920
11999
  ]);
11921
12000
  const config = isResolveMode ? resolvedConfig : props.config;
11922
12001
  useEffect(() => {
11923
- var _a, _b;
12002
+ var _a, _b, _c;
11924
12003
  if (!config) return;
11925
12004
  let cancelled = false;
11926
12005
  setCanvasSettled(false);
11927
12006
  setStabilizationPass(0);
12007
+ console.log(PREVIEW_DEBUG_PREFIX, "config-changed", {
12008
+ pageIndex,
12009
+ pages: ((_a = config.pages) == null ? void 0 : _a.length) ?? 0,
12010
+ underlinedNodes: countUnderlinedNodes(config),
12011
+ isResolveMode
12012
+ });
11928
12013
  const bump = () => {
11929
12014
  if (cancelled) return;
11930
12015
  clearMeasurementCache();
11931
12016
  clearFabricCharCache();
11932
- setFontsReadyVersion((v) => v + 1);
12017
+ setFontsReadyVersion((v) => {
12018
+ const next = v + 1;
12019
+ console.log(PREVIEW_DEBUG_PREFIX, "font-bump", { pageIndex, next, stabilizationPass });
12020
+ return next;
12021
+ });
11933
12022
  };
11934
- (_b = (_a = document.fonts) == null ? void 0 : _a.ready) == null ? void 0 : _b.then(bump);
12023
+ (_c = (_b = document.fonts) == null ? void 0 : _b.ready) == null ? void 0 : _c.then(bump);
11935
12024
  const timeoutId = window.setTimeout(bump, 350);
11936
12025
  return () => {
11937
12026
  cancelled = true;
@@ -11953,17 +12042,28 @@ function PixldocsPreview(props) {
11953
12042
  setFontsReady(false);
11954
12043
  setCanvasSettled(false);
11955
12044
  setStabilizationPass(0);
11956
- ensureFontsForResolvedConfig(config).then(() => setFontsReady(true)).catch(() => setFontsReady(true));
12045
+ ensureFontsForResolvedConfig(config).then(() => {
12046
+ console.log(PREVIEW_DEBUG_PREFIX, "config-mode fonts queued", {
12047
+ pageIndex,
12048
+ underlinedNodes: countUnderlinedNodes(config)
12049
+ });
12050
+ setFontsReady(true);
12051
+ }).catch((err) => {
12052
+ console.warn(PREVIEW_DEBUG_PREFIX, "config-mode fonts queue failed", err);
12053
+ setFontsReady(true);
12054
+ });
11957
12055
  }, [isResolveMode, config]);
11958
12056
  const handleCanvasReady = useCallback(() => {
11959
12057
  if (stabilizationPass === 0) {
12058
+ console.log(PREVIEW_DEBUG_PREFIX, "canvas-ready-pass", { pageIndex, stabilizationPass, action: "stabilize-again" });
11960
12059
  setCanvasSettled(false);
11961
12060
  setStabilizationPass(1);
11962
12061
  return;
11963
12062
  }
12063
+ console.log(PREVIEW_DEBUG_PREFIX, "canvas-ready-pass", { pageIndex, stabilizationPass, action: "settled" });
11964
12064
  setCanvasSettled(true);
11965
12065
  onReady == null ? void 0 : onReady();
11966
- }, [onReady, stabilizationPass]);
12066
+ }, [onReady, pageIndex, stabilizationPass]);
11967
12067
  if (isLoading) {
11968
12068
  return /* @__PURE__ */ jsx("div", { className, style: { ...style, display: "flex", alignItems: "center", justifyContent: "center", minHeight: 200 }, children: /* @__PURE__ */ jsx("div", { style: { color: "#888", fontSize: 14 }, children: "Loading preview..." }) });
11969
12069
  }