@pixldocs/canvas-renderer 0.5.11 → 0.5.13

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
@@ -2699,18 +2699,37 @@ const clearFabricCharCache = () => {
2699
2699
  };
2700
2700
  const clearFontCacheAndRerender = (canvas) => {
2701
2701
  clearFabricCharCache();
2702
- const fixTextbox = (obj) => {
2702
+ const resetTextboxLayoutInternals = (obj) => {
2703
2703
  var _a;
2704
+ (_a = obj._clearCache) == null ? void 0 : _a.call(obj);
2705
+ obj.__charBounds = [];
2706
+ obj.__lineWidths = [];
2707
+ obj.__lineHeights = [];
2708
+ obj.__graphemeLines = [];
2709
+ obj._textLines = [];
2710
+ obj.textLines = [];
2711
+ obj._styleMap = null;
2712
+ obj.styleMap = null;
2713
+ obj.dirty = true;
2714
+ };
2715
+ const fixTextbox = (obj) => {
2716
+ var _a, _b;
2704
2717
  if (obj instanceof fabric.Textbox) {
2705
2718
  const savedWidth = obj.width;
2706
- obj.dirty = true;
2719
+ const savedScaleX = obj.scaleX;
2720
+ const savedScaleY = obj.scaleY;
2721
+ resetTextboxLayoutInternals(obj);
2707
2722
  obj.initDimensions();
2708
- if (savedWidth != null && Math.abs((obj.width ?? 0) - savedWidth) > 0.01) {
2709
- obj.width = savedWidth;
2723
+ if (savedWidth != null) {
2724
+ obj.set({ width: savedWidth, scaleX: savedScaleX, scaleY: savedScaleY });
2725
+ resetTextboxLayoutInternals(obj);
2726
+ obj.initDimensions();
2727
+ obj.set({ width: savedWidth, scaleX: savedScaleX, scaleY: savedScaleY, dirty: true });
2710
2728
  }
2729
+ (_a = obj._clearCache) == null ? void 0 : _a.call(obj);
2711
2730
  obj.setCoords();
2712
2731
  } else if (obj instanceof fabric.Group) {
2713
- (_a = obj._objects) == null ? void 0 : _a.forEach(fixTextbox);
2732
+ (_b = obj._objects) == null ? void 0 : _b.forEach(fixTextbox);
2714
2733
  obj.dirty = true;
2715
2734
  obj.setCoords();
2716
2735
  }
@@ -11823,10 +11842,12 @@ function PixldocsPreview(props) {
11823
11842
  const [isLoading, setIsLoading] = useState(false);
11824
11843
  const [fontsReady, setFontsReady] = useState(false);
11825
11844
  const [fontsReadyVersion, setFontsReadyVersion] = useState(0);
11845
+ const [canvasSettled, setCanvasSettled] = useState(false);
11826
11846
  const isResolveMode = !("config" in props && props.config);
11827
11847
  useEffect(() => {
11828
11848
  if (!isResolveMode) {
11829
11849
  setResolvedConfig(null);
11850
+ setCanvasSettled(false);
11830
11851
  return;
11831
11852
  }
11832
11853
  const p = props;
@@ -11834,6 +11855,7 @@ function PixldocsPreview(props) {
11834
11855
  let cancelled = false;
11835
11856
  setIsLoading(true);
11836
11857
  setFontsReady(false);
11858
+ setCanvasSettled(false);
11837
11859
  resolveFromForm({
11838
11860
  templateId: p.templateId,
11839
11861
  formSchemaId: p.formSchemaId,
@@ -11878,6 +11900,7 @@ function PixldocsPreview(props) {
11878
11900
  var _a, _b;
11879
11901
  if (!config) return;
11880
11902
  let cancelled = false;
11903
+ setCanvasSettled(false);
11881
11904
  const bump = () => {
11882
11905
  if (cancelled) return;
11883
11906
  clearMeasurementCache();
@@ -11895,11 +11918,17 @@ function PixldocsPreview(props) {
11895
11918
  if (isResolveMode) return;
11896
11919
  if (!config) {
11897
11920
  setFontsReady(false);
11921
+ setCanvasSettled(false);
11898
11922
  return;
11899
11923
  }
11900
11924
  setFontsReady(false);
11925
+ setCanvasSettled(false);
11901
11926
  ensureFontsForResolvedConfig(config).then(() => setFontsReady(true)).catch(() => setFontsReady(true));
11902
11927
  }, [isResolveMode, config]);
11928
+ const handleCanvasReady = useCallback(() => {
11929
+ setCanvasSettled(true);
11930
+ onReady == null ? void 0 : onReady();
11931
+ }, [onReady]);
11903
11932
  if (isLoading) {
11904
11933
  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..." }) });
11905
11934
  }
@@ -11907,18 +11936,21 @@ function PixldocsPreview(props) {
11907
11936
  if (!fontsReady) {
11908
11937
  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..." }) });
11909
11938
  }
11910
- return /* @__PURE__ */ jsx("div", { className, style, children: /* @__PURE__ */ jsx(
11911
- PreviewCanvas,
11912
- {
11913
- config,
11914
- pageIndex,
11915
- zoom,
11916
- absoluteZoom,
11917
- onDynamicFieldClick,
11918
- onReady
11919
- },
11920
- previewKey
11921
- ) });
11939
+ return /* @__PURE__ */ jsxs("div", { className, style: { ...style, position: "relative" }, children: [
11940
+ /* @__PURE__ */ jsx("div", { style: { visibility: canvasSettled ? "visible" : "hidden" }, children: /* @__PURE__ */ jsx(
11941
+ PreviewCanvas,
11942
+ {
11943
+ config,
11944
+ pageIndex,
11945
+ zoom,
11946
+ absoluteZoom,
11947
+ onDynamicFieldClick,
11948
+ onReady: handleCanvasReady
11949
+ },
11950
+ previewKey
11951
+ ) }),
11952
+ !canvasSettled && /* @__PURE__ */ jsx("div", { style: { position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", minHeight: 200 }, children: /* @__PURE__ */ jsx("div", { style: { color: "#888", fontSize: 14 }, children: "Loading preview..." }) })
11953
+ ] });
11922
11954
  }
11923
11955
  class PixldocsRenderer {
11924
11956
  constructor(config) {
@@ -12562,14 +12594,30 @@ class PixldocsRenderer {
12562
12594
  scaleX: obj.scaleX,
12563
12595
  scaleY: obj.scaleY
12564
12596
  };
12565
- (_a3 = obj._clearCache) == null ? void 0 : _a3.call(obj);
12566
- obj.__charBounds = [];
12567
- obj.__lineWidths = [];
12568
- obj.__lineHeights = [];
12569
- obj._textLines = [];
12570
- obj.textLines = [];
12571
- obj.dirty = true;
12597
+ const resetTextboxLayoutInternals = () => {
12598
+ var _a4;
12599
+ (_a4 = obj._clearCache) == null ? void 0 : _a4.call(obj);
12600
+ obj.__charBounds = [];
12601
+ obj.__lineWidths = [];
12602
+ obj.__lineHeights = [];
12603
+ obj.__graphemeLines = [];
12604
+ obj._textLines = [];
12605
+ obj.textLines = [];
12606
+ obj._styleMap = null;
12607
+ obj.styleMap = null;
12608
+ obj.dirty = true;
12609
+ };
12610
+ resetTextboxLayoutInternals();
12572
12611
  obj.initDimensions();
12612
+ if (saved.width != null) {
12613
+ (_a3 = obj.set) == null ? void 0 : _a3.call(obj, {
12614
+ width: saved.width,
12615
+ scaleX: saved.scaleX,
12616
+ scaleY: saved.scaleY
12617
+ });
12618
+ resetTextboxLayoutInternals();
12619
+ obj.initDimensions();
12620
+ }
12573
12621
  (_b2 = obj.set) == null ? void 0 : _b2.call(obj, {
12574
12622
  width: saved.width,
12575
12623
  scaleX: saved.scaleX,