@pixldocs/canvas-renderer 0.5.41 → 0.5.43

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
@@ -4801,6 +4801,170 @@ function calculateScaleSnapGuides(scalingObj, corner, canvas, canvasWidth, canva
4801
4801
  return true;
4802
4802
  });
4803
4803
  }
4804
+ const PD_BG_KEY = "__pdBg";
4805
+ const PATCHED_KEY = "__pdBgPatched";
4806
+ function extractTextBgConfig(element) {
4807
+ const legacy = Math.max(0, Number(element.textBgPadding ?? 0)) || 0;
4808
+ const pick = (v) => {
4809
+ const n = Number(v);
4810
+ return Number.isFinite(n) && n >= 0 ? n : void 0;
4811
+ };
4812
+ return {
4813
+ color: element.textBgColor,
4814
+ padTop: pick(element.textBgPaddingTop) ?? legacy,
4815
+ padRight: pick(element.textBgPaddingRight) ?? legacy,
4816
+ padBottom: pick(element.textBgPaddingBottom) ?? legacy,
4817
+ padLeft: pick(element.textBgPaddingLeft) ?? legacy,
4818
+ rxTL: Math.max(0, Number(element.textBgRxTL ?? 0)) || 0,
4819
+ rxTR: Math.max(0, Number(element.textBgRxTR ?? 0)) || 0,
4820
+ rxBR: Math.max(0, Number(element.textBgRxBR ?? 0)) || 0,
4821
+ rxBL: Math.max(0, Number(element.textBgRxBL ?? 0)) || 0
4822
+ };
4823
+ }
4824
+ function hasTextBackground(cfg) {
4825
+ if (!cfg) return false;
4826
+ const c = (cfg.color || "").toString().trim().toLowerCase();
4827
+ return !!c && c !== "transparent" && c !== "none" && c !== "rgba(0,0,0,0)";
4828
+ }
4829
+ function buildTextShadow(element) {
4830
+ const color = element.textShadowColor;
4831
+ const blur = Number(element.textShadowBlur ?? 0);
4832
+ const ox = Number(element.textShadowOffsetX ?? 0);
4833
+ const oy = Number(element.textShadowOffsetY ?? 0);
4834
+ if (!color || color === "transparent") return null;
4835
+ return new fabric__namespace.Shadow({
4836
+ color: String(color),
4837
+ blur: blur || 0,
4838
+ offsetX: ox || 0,
4839
+ offsetY: oy || 0,
4840
+ affectStroke: false,
4841
+ nonScaling: false
4842
+ });
4843
+ }
4844
+ function buildRoundedRectPath2D(ctx, x, y, w, h, rTL, rTR, rBR, rBL) {
4845
+ const maxR = Math.min(w, h) / 2;
4846
+ const tl = Math.min(Math.max(0, rTL), maxR);
4847
+ const tr = Math.min(Math.max(0, rTR), maxR);
4848
+ const br = Math.min(Math.max(0, rBR), maxR);
4849
+ const bl = Math.min(Math.max(0, rBL), maxR);
4850
+ ctx.beginPath();
4851
+ ctx.moveTo(x + tl, y);
4852
+ ctx.lineTo(x + w - tr, y);
4853
+ if (tr > 0) ctx.quadraticCurveTo(x + w, y, x + w, y + tr);
4854
+ ctx.lineTo(x + w, y + h - br);
4855
+ if (br > 0) ctx.quadraticCurveTo(x + w, y + h, x + w - br, y + h);
4856
+ ctx.lineTo(x + bl, y + h);
4857
+ if (bl > 0) ctx.quadraticCurveTo(x, y + h, x, y + h - bl);
4858
+ ctx.lineTo(x, y + tl);
4859
+ if (tl > 0) ctx.quadraticCurveTo(x, y, x + tl, y);
4860
+ ctx.closePath();
4861
+ }
4862
+ function applyTextBackground(obj, cfg) {
4863
+ var _a;
4864
+ obj[PD_BG_KEY] = { ...cfg };
4865
+ if (obj[PATCHED_KEY]) return;
4866
+ obj[PATCHED_KEY] = true;
4867
+ const originalRender = obj._render.bind(obj);
4868
+ obj._render = function(ctx) {
4869
+ const bg = this[PD_BG_KEY];
4870
+ if (hasTextBackground(bg)) {
4871
+ const w = this.width ?? 0;
4872
+ const h = this.height ?? 0;
4873
+ const pT = Math.max(0, Number(bg.padTop ?? 0));
4874
+ const pR = Math.max(0, Number(bg.padRight ?? 0));
4875
+ const pB = Math.max(0, Number(bg.padBottom ?? 0));
4876
+ const pL = Math.max(0, Number(bg.padLeft ?? 0));
4877
+ const x = -w / 2 - pL;
4878
+ const y = -h / 2 - pT;
4879
+ const bgW = w + pL + pR;
4880
+ const bgH = h + pT + pB;
4881
+ ctx.save();
4882
+ buildRoundedRectPath2D(
4883
+ ctx,
4884
+ x,
4885
+ y,
4886
+ bgW,
4887
+ bgH,
4888
+ bg.rxTL ?? 0,
4889
+ bg.rxTR ?? 0,
4890
+ bg.rxBR ?? 0,
4891
+ bg.rxBL ?? 0
4892
+ );
4893
+ ctx.fillStyle = bg.color;
4894
+ ctx.fill();
4895
+ ctx.restore();
4896
+ }
4897
+ originalRender(ctx);
4898
+ };
4899
+ const originalToObject = obj.toObject.bind(obj);
4900
+ obj.toObject = function(propertiesToInclude) {
4901
+ const out = originalToObject(propertiesToInclude);
4902
+ const bg = this[PD_BG_KEY];
4903
+ if (hasTextBackground(bg)) {
4904
+ out.__pdBg = { ...bg };
4905
+ }
4906
+ return out;
4907
+ };
4908
+ const originalToSVG = (_a = obj.toSVG) == null ? void 0 : _a.bind(obj);
4909
+ if (typeof originalToSVG === "function") {
4910
+ obj.toSVG = function(reviver) {
4911
+ const svg = originalToSVG(reviver);
4912
+ const bg = this[PD_BG_KEY];
4913
+ if (!hasTextBackground(bg)) return svg;
4914
+ const w = this.width ?? 0;
4915
+ const h = this.height ?? 0;
4916
+ const pT = Math.max(0, Number(bg.padTop ?? 0));
4917
+ const pR = Math.max(0, Number(bg.padRight ?? 0));
4918
+ const pB = Math.max(0, Number(bg.padBottom ?? 0));
4919
+ const pL = Math.max(0, Number(bg.padLeft ?? 0));
4920
+ const x = -w / 2 - pL;
4921
+ const y = -h / 2 - pT;
4922
+ const bgW = w + pL + pR;
4923
+ const bgH = h + pT + pB;
4924
+ const d = buildRoundedRectPathD(
4925
+ x,
4926
+ y,
4927
+ bgW,
4928
+ bgH,
4929
+ bg.rxTL ?? 0,
4930
+ bg.rxTR ?? 0,
4931
+ bg.rxBR ?? 0,
4932
+ bg.rxBL ?? 0
4933
+ );
4934
+ const fill = bg.color;
4935
+ const bgPath = `<path d="${d}" fill="${escapeXmlAttr(fill)}" />`;
4936
+ const openTagMatch = svg.match(/^\s*<g\b[^>]*>/);
4937
+ if (openTagMatch) {
4938
+ const openTag = openTagMatch[0];
4939
+ return svg.replace(openTag, openTag + bgPath);
4940
+ }
4941
+ return `<g>${bgPath}${svg}</g>`;
4942
+ };
4943
+ }
4944
+ }
4945
+ function buildRoundedRectPathD(x, y, w, h, rTL, rTR, rBR, rBL) {
4946
+ const maxR = Math.min(w, h) / 2;
4947
+ const tl = Math.min(Math.max(0, rTL), maxR);
4948
+ const tr = Math.min(Math.max(0, rTR), maxR);
4949
+ const br = Math.min(Math.max(0, rBR), maxR);
4950
+ const bl = Math.min(Math.max(0, rBL), maxR);
4951
+ const fmt = (n) => Number.isFinite(n) ? Number(n.toFixed(3)) : 0;
4952
+ const parts = [];
4953
+ parts.push(`M ${fmt(x + tl)} ${fmt(y)}`);
4954
+ parts.push(`L ${fmt(x + w - tr)} ${fmt(y)}`);
4955
+ if (tr > 0) parts.push(`Q ${fmt(x + w)} ${fmt(y)} ${fmt(x + w)} ${fmt(y + tr)}`);
4956
+ parts.push(`L ${fmt(x + w)} ${fmt(y + h - br)}`);
4957
+ if (br > 0) parts.push(`Q ${fmt(x + w)} ${fmt(y + h)} ${fmt(x + w - br)} ${fmt(y + h)}`);
4958
+ parts.push(`L ${fmt(x + bl)} ${fmt(y + h)}`);
4959
+ if (bl > 0) parts.push(`Q ${fmt(x)} ${fmt(y + h)} ${fmt(x)} ${fmt(y + h - bl)}`);
4960
+ parts.push(`L ${fmt(x)} ${fmt(y + tl)}`);
4961
+ if (tl > 0) parts.push(`Q ${fmt(x)} ${fmt(y)} ${fmt(x + tl)} ${fmt(y)}`);
4962
+ parts.push("Z");
4963
+ return parts.join(" ");
4964
+ }
4965
+ function escapeXmlAttr(s) {
4966
+ return String(s).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
4967
+ }
4804
4968
  const TRIANGLE_STROKE_MITER_LIMIT = 1e6;
4805
4969
  const toSafeNumber = (value, fallback = 0) => Number.isFinite(value) ? Math.max(0, Number(value)) : fallback;
4806
4970
  function normalizeShapeType(shapeType) {
@@ -5101,6 +5265,9 @@ function createText(element) {
5101
5265
  textbox.setCoords();
5102
5266
  }
5103
5267
  textbox.dirty = true;
5268
+ applyTextBackground(textbox, extractTextBgConfig(element));
5269
+ const shadow = buildTextShadow(element);
5270
+ if (shadow) textbox.set("shadow", shadow);
5104
5271
  return textbox;
5105
5272
  }
5106
5273
  function createLine(element) {
@@ -7713,7 +7880,23 @@ const PageCanvas = react.forwardRef(
7713
7880
  const resolvedSizeForCompare = (pageChildren == null ? void 0 : pageChildren.length) ? getNodeBounds(element, pageChildren) : { width: typeof element.width === "number" ? element.width : 0, height: typeof element.height === "number" ? element.height : 0 };
7714
7881
  const fabricText = existingObj.text ?? "";
7715
7882
  const storeText = element.text ?? "";
7716
- const otherPropsChanged = Math.abs((existingObj.width ?? 0) - resolvedSizeForCompare.width) > 0.1 || Math.abs((existingObj.height ?? 0) - resolvedSizeForCompare.height) > 0.1 || Math.abs((existingObj.angle ?? 0) - (element.angle ?? 0)) > 0.1 || Math.abs((existingObj.scaleX ?? 1) - (element.scaleX ?? 1)) > 0.01 || Math.abs((existingObj.scaleY ?? 1) - (element.scaleY ?? 1)) > 0.01 || (existingObj.flipX ?? false) !== (element.flipX ?? false) || (existingObj.flipY ?? false) !== (element.flipY ?? false) || fabricText !== storeText || existingObj.fill !== (element.fill ?? "") || existingObj.stroke !== (element.stroke ?? "") || Math.abs((existingObj.strokeWidth ?? 0) - (element.strokeWidth ?? 0)) > 0.01 || Math.abs((existingObj.opacity ?? 1) - (element.opacity ?? 1)) > 0.01 || (existingObj.fontSize ?? 0) !== (element.fontSize ?? 0) || (existingObj.fontFamily ?? "") !== (element.fontFamily ?? "") || // CRITICAL: Detect gradient fill/stroke changes serialise to JSON for deep comparison
7883
+ const otherPropsChanged = Math.abs((existingObj.width ?? 0) - resolvedSizeForCompare.width) > 0.1 || Math.abs((existingObj.height ?? 0) - resolvedSizeForCompare.height) > 0.1 || Math.abs((existingObj.angle ?? 0) - (element.angle ?? 0)) > 0.1 || Math.abs((existingObj.scaleX ?? 1) - (element.scaleX ?? 1)) > 0.01 || Math.abs((existingObj.scaleY ?? 1) - (element.scaleY ?? 1)) > 0.01 || (existingObj.flipX ?? false) !== (element.flipX ?? false) || (existingObj.flipY ?? false) !== (element.flipY ?? false) || fabricText !== storeText || existingObj.fill !== (element.fill ?? "") || existingObj.stroke !== (element.stroke ?? "") || Math.abs((existingObj.strokeWidth ?? 0) - (element.strokeWidth ?? 0)) > 0.01 || Math.abs((existingObj.opacity ?? 1) - (element.opacity ?? 1)) > 0.01 || (existingObj.fontSize ?? 0) !== (element.fontSize ?? 0) || (existingObj.fontFamily ?? "") !== (element.fontFamily ?? "") || // Detect text background + shadow changes so panel edits flow into Fabric.
7884
+ JSON.stringify({
7885
+ c: element.textBgColor ?? null,
7886
+ p: element.textBgPadding ?? 0,
7887
+ pt: element.textBgPaddingTop ?? null,
7888
+ pr: element.textBgPaddingRight ?? null,
7889
+ pb: element.textBgPaddingBottom ?? null,
7890
+ pl: element.textBgPaddingLeft ?? null,
7891
+ tl: element.textBgRxTL ?? 0,
7892
+ tr: element.textBgRxTR ?? 0,
7893
+ br: element.textBgRxBR ?? 0,
7894
+ bl: element.textBgRxBL ?? 0,
7895
+ sc: element.textShadowColor ?? null,
7896
+ sb: element.textShadowBlur ?? 0,
7897
+ sx: element.textShadowOffsetX ?? 0,
7898
+ sy: element.textShadowOffsetY ?? 0
7899
+ }) !== (existingObj.__lastTextBgShadowJson ?? "") || // CRITICAL: Detect gradient fill/stroke changes — serialise to JSON for deep comparison
7717
7900
  JSON.stringify(element.fillGradient || null) !== (existingObj.__lastFillGradientJson ?? "null") || JSON.stringify(element.strokeGradient || null) !== (existingObj.__lastStrokeGradientJson ?? "null");
7718
7901
  const forceApplyFromPanel = syncTriggeredByPanelRef.current;
7719
7902
  const noPropsOrPositionChanged = !positionChanged && !otherPropsChanged;
@@ -8148,7 +8331,7 @@ const PageCanvas = react.forwardRef(
8148
8331
  });
8149
8332
  }, [selectedIds, isActive, ready, elements]);
8150
8333
  const updateFabricObject = (obj, element, skipPositionUpdate = false) => {
8151
- var _a, _b;
8334
+ var _a, _b, _c;
8152
8335
  const fc = fabricRef.current;
8153
8336
  if (fc && isTransforming(fc)) {
8154
8337
  return;
@@ -8608,6 +8791,37 @@ const PageCanvas = react.forwardRef(
8608
8791
  }
8609
8792
  obj.setCoords();
8610
8793
  obj.dirty = true;
8794
+ try {
8795
+ applyTextBackground(obj, extractTextBgConfig(element));
8796
+ const shadow = buildTextShadow(element);
8797
+ obj.set("shadow", shadow ?? null);
8798
+ try {
8799
+ obj._cacheCanvas = null;
8800
+ obj._cacheContext = null;
8801
+ } catch {
8802
+ }
8803
+ obj.dirty = true;
8804
+ (_c = obj.setCoords) == null ? void 0 : _c.call(obj);
8805
+ obj.__lastTextBgShadowJson = JSON.stringify({
8806
+ c: element.textBgColor ?? null,
8807
+ p: element.textBgPadding ?? 0,
8808
+ pt: element.textBgPaddingTop ?? null,
8809
+ pr: element.textBgPaddingRight ?? null,
8810
+ pb: element.textBgPaddingBottom ?? null,
8811
+ pl: element.textBgPaddingLeft ?? null,
8812
+ tl: element.textBgRxTL ?? 0,
8813
+ tr: element.textBgRxTR ?? 0,
8814
+ br: element.textBgRxBR ?? 0,
8815
+ bl: element.textBgRxBL ?? 0,
8816
+ sc: element.textShadowColor ?? null,
8817
+ sb: element.textShadowBlur ?? 0,
8818
+ sx: element.textShadowOffsetX ?? 0,
8819
+ sy: element.textShadowOffsetY ?? 0
8820
+ });
8821
+ obj.dirty = true;
8822
+ } catch (err) {
8823
+ console.warn("[text-bg] failed to apply background/shadow", err);
8824
+ }
8611
8825
  } else if (!isImage && !isLine) {
8612
8826
  if (obj instanceof fabric__namespace.Circle) {
8613
8827
  obj.set({
@@ -12072,7 +12286,7 @@ function PixldocsPreview(props) {
12072
12286
  !canvasSettled && /* @__PURE__ */ jsxRuntime.jsx("div", { style: { position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", minHeight: 200 }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { color: "#888", fontSize: 14 }, children: "Loading preview..." }) })
12073
12287
  ] });
12074
12288
  }
12075
- const PACKAGE_VERSION = "0.5.40";
12289
+ const PACKAGE_VERSION = "0.5.43";
12076
12290
  let __underlineFixInstalled = false;
12077
12291
  function installUnderlineFix(fab) {
12078
12292
  var _a;