@pixldocs/canvas-renderer 0.5.12 → 0.5.14

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 CHANGED
@@ -2718,18 +2718,37 @@ const clearFabricCharCache = () => {
2718
2718
  };
2719
2719
  const clearFontCacheAndRerender = (canvas) => {
2720
2720
  clearFabricCharCache();
2721
- const fixTextbox = (obj) => {
2721
+ const resetTextboxLayoutInternals = (obj) => {
2722
2722
  var _a;
2723
+ (_a = obj._clearCache) == null ? void 0 : _a.call(obj);
2724
+ obj.__charBounds = [];
2725
+ obj.__lineWidths = [];
2726
+ obj.__lineHeights = [];
2727
+ obj.__graphemeLines = [];
2728
+ obj._textLines = [];
2729
+ obj.textLines = [];
2730
+ obj._styleMap = null;
2731
+ obj.styleMap = null;
2732
+ obj.dirty = true;
2733
+ };
2734
+ const fixTextbox = (obj) => {
2735
+ var _a, _b;
2723
2736
  if (obj instanceof fabric__namespace.Textbox) {
2724
2737
  const savedWidth = obj.width;
2725
- obj.dirty = true;
2738
+ const savedScaleX = obj.scaleX;
2739
+ const savedScaleY = obj.scaleY;
2740
+ resetTextboxLayoutInternals(obj);
2726
2741
  obj.initDimensions();
2727
- if (savedWidth != null && Math.abs((obj.width ?? 0) - savedWidth) > 0.01) {
2728
- obj.width = savedWidth;
2742
+ if (savedWidth != null) {
2743
+ obj.set({ width: savedWidth, scaleX: savedScaleX, scaleY: savedScaleY });
2744
+ resetTextboxLayoutInternals(obj);
2745
+ obj.initDimensions();
2746
+ obj.set({ width: savedWidth, scaleX: savedScaleX, scaleY: savedScaleY, dirty: true });
2729
2747
  }
2748
+ (_a = obj._clearCache) == null ? void 0 : _a.call(obj);
2730
2749
  obj.setCoords();
2731
2750
  } else if (obj instanceof fabric__namespace.Group) {
2732
- (_a = obj._objects) == null ? void 0 : _a.forEach(fixTextbox);
2751
+ (_b = obj._objects) == null ? void 0 : _b.forEach(fixTextbox);
2733
2752
  obj.dirty = true;
2734
2753
  obj.setCoords();
2735
2754
  }
@@ -11843,6 +11862,7 @@ function PixldocsPreview(props) {
11843
11862
  const [fontsReady, setFontsReady] = react.useState(false);
11844
11863
  const [fontsReadyVersion, setFontsReadyVersion] = react.useState(0);
11845
11864
  const [canvasSettled, setCanvasSettled] = react.useState(false);
11865
+ const [stabilizationPass, setStabilizationPass] = react.useState(0);
11846
11866
  const isResolveMode = !("config" in props && props.config);
11847
11867
  react.useEffect(() => {
11848
11868
  if (!isResolveMode) {
@@ -11901,6 +11921,7 @@ function PixldocsPreview(props) {
11901
11921
  if (!config) return;
11902
11922
  let cancelled = false;
11903
11923
  setCanvasSettled(false);
11924
+ setStabilizationPass(0);
11904
11925
  const bump = () => {
11905
11926
  if (cancelled) return;
11906
11927
  clearMeasurementCache();
@@ -11913,22 +11934,32 @@ function PixldocsPreview(props) {
11913
11934
  window.clearTimeout(timeoutId);
11914
11935
  };
11915
11936
  }, [config]);
11916
- const previewKey = react.useMemo(() => `${pageIndex}-${fontsReadyVersion}`, [pageIndex, fontsReadyVersion]);
11937
+ const previewKey = react.useMemo(
11938
+ () => `${pageIndex}-${fontsReadyVersion}-${stabilizationPass}`,
11939
+ [pageIndex, fontsReadyVersion, stabilizationPass]
11940
+ );
11917
11941
  react.useEffect(() => {
11918
11942
  if (isResolveMode) return;
11919
11943
  if (!config) {
11920
11944
  setFontsReady(false);
11921
11945
  setCanvasSettled(false);
11946
+ setStabilizationPass(0);
11922
11947
  return;
11923
11948
  }
11924
11949
  setFontsReady(false);
11925
11950
  setCanvasSettled(false);
11951
+ setStabilizationPass(0);
11926
11952
  ensureFontsForResolvedConfig(config).then(() => setFontsReady(true)).catch(() => setFontsReady(true));
11927
11953
  }, [isResolveMode, config]);
11928
11954
  const handleCanvasReady = react.useCallback(() => {
11955
+ if (stabilizationPass === 0) {
11956
+ setCanvasSettled(false);
11957
+ setStabilizationPass(1);
11958
+ return;
11959
+ }
11929
11960
  setCanvasSettled(true);
11930
11961
  onReady == null ? void 0 : onReady();
11931
- }, [onReady]);
11962
+ }, [onReady, stabilizationPass]);
11932
11963
  if (isLoading) {
11933
11964
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className, style: { ...style, display: "flex", alignItems: "center", justifyContent: "center", minHeight: 200 }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { color: "#888", fontSize: 14 }, children: "Loading preview..." }) });
11934
11965
  }
@@ -12342,6 +12373,7 @@ class PixldocsRenderer {
12342
12373
  cleanup();
12343
12374
  reject(new Error("Render timeout (30s)"));
12344
12375
  }, 3e4);
12376
+ let stabilizationPass = 0;
12345
12377
  const cleanup = () => {
12346
12378
  clearTimeout(timeout);
12347
12379
  try {
@@ -12351,6 +12383,19 @@ class PixldocsRenderer {
12351
12383
  container.remove();
12352
12384
  };
12353
12385
  const onReady = () => {
12386
+ if (stabilizationPass === 0) {
12387
+ stabilizationPass = 1;
12388
+ root.render(
12389
+ react.createElement(PreviewCanvas2, {
12390
+ config,
12391
+ pageIndex,
12392
+ zoom: pixelRatio,
12393
+ absoluteZoom: true,
12394
+ onReady
12395
+ })
12396
+ );
12397
+ return;
12398
+ }
12354
12399
  const expectedImageCount = this.getExpectedImageCount(config, pageIndex);
12355
12400
  this.waitForCanvasImages(container, expectedImageCount).then(async () => {
12356
12401
  try {
@@ -12422,6 +12467,7 @@ class PixldocsRenderer {
12422
12467
  cleanup();
12423
12468
  reject(new Error("SVG render timeout (30s)"));
12424
12469
  }, 3e4);
12470
+ let stabilizationPass = 0;
12425
12471
  const cleanup = () => {
12426
12472
  clearTimeout(timeout);
12427
12473
  try {
@@ -12431,6 +12477,19 @@ class PixldocsRenderer {
12431
12477
  container.remove();
12432
12478
  };
12433
12479
  const onReady = () => {
12480
+ if (stabilizationPass === 0) {
12481
+ stabilizationPass = 1;
12482
+ root.render(
12483
+ react.createElement(PreviewCanvas2, {
12484
+ config,
12485
+ pageIndex,
12486
+ zoom: 1,
12487
+ absoluteZoom: true,
12488
+ onReady
12489
+ })
12490
+ );
12491
+ return;
12492
+ }
12434
12493
  const expectedImageCount = this.getExpectedImageCount(config, pageIndex);
12435
12494
  this.waitForCanvasImages(container, expectedImageCount).then(async () => {
12436
12495
  var _a, _b;
@@ -12594,14 +12653,30 @@ class PixldocsRenderer {
12594
12653
  scaleX: obj.scaleX,
12595
12654
  scaleY: obj.scaleY
12596
12655
  };
12597
- (_a3 = obj._clearCache) == null ? void 0 : _a3.call(obj);
12598
- obj.__charBounds = [];
12599
- obj.__lineWidths = [];
12600
- obj.__lineHeights = [];
12601
- obj._textLines = [];
12602
- obj.textLines = [];
12603
- obj.dirty = true;
12656
+ const resetTextboxLayoutInternals = () => {
12657
+ var _a4;
12658
+ (_a4 = obj._clearCache) == null ? void 0 : _a4.call(obj);
12659
+ obj.__charBounds = [];
12660
+ obj.__lineWidths = [];
12661
+ obj.__lineHeights = [];
12662
+ obj.__graphemeLines = [];
12663
+ obj._textLines = [];
12664
+ obj.textLines = [];
12665
+ obj._styleMap = null;
12666
+ obj.styleMap = null;
12667
+ obj.dirty = true;
12668
+ };
12669
+ resetTextboxLayoutInternals();
12604
12670
  obj.initDimensions();
12671
+ if (saved.width != null) {
12672
+ (_a3 = obj.set) == null ? void 0 : _a3.call(obj, {
12673
+ width: saved.width,
12674
+ scaleX: saved.scaleX,
12675
+ scaleY: saved.scaleY
12676
+ });
12677
+ resetTextboxLayoutInternals();
12678
+ obj.initDimensions();
12679
+ }
12605
12680
  (_b2 = obj.set) == null ? void 0 : _b2.call(obj, {
12606
12681
  width: saved.width,
12607
12682
  scaleX: saved.scaleX,