@pixldocs/canvas-renderer 0.5.41 → 0.5.42

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,157 @@ 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
+ return {
4808
+ color: element.textBgColor,
4809
+ padding: Math.max(0, Number(element.textBgPadding ?? 0)) || 0,
4810
+ rxTL: Math.max(0, Number(element.textBgRxTL ?? 0)) || 0,
4811
+ rxTR: Math.max(0, Number(element.textBgRxTR ?? 0)) || 0,
4812
+ rxBR: Math.max(0, Number(element.textBgRxBR ?? 0)) || 0,
4813
+ rxBL: Math.max(0, Number(element.textBgRxBL ?? 0)) || 0
4814
+ };
4815
+ }
4816
+ function hasTextBackground(cfg) {
4817
+ if (!cfg) return false;
4818
+ const c = (cfg.color || "").toString().trim().toLowerCase();
4819
+ return !!c && c !== "transparent" && c !== "none" && c !== "rgba(0,0,0,0)";
4820
+ }
4821
+ function buildTextShadow(element) {
4822
+ const color = element.textShadowColor;
4823
+ const blur = Number(element.textShadowBlur ?? 0);
4824
+ const ox = Number(element.textShadowOffsetX ?? 0);
4825
+ const oy = Number(element.textShadowOffsetY ?? 0);
4826
+ if (!color || color === "transparent") return null;
4827
+ if (blur === 0 && ox === 0 && oy === 0) return null;
4828
+ return new fabric__namespace.Shadow({
4829
+ color,
4830
+ blur: blur || 0,
4831
+ offsetX: ox || 0,
4832
+ offsetY: oy || 0,
4833
+ affectStroke: false,
4834
+ nonScaling: false
4835
+ });
4836
+ }
4837
+ function buildRoundedRectPath2D(ctx, x, y, w, h, rTL, rTR, rBR, rBL) {
4838
+ const maxR = Math.min(w, h) / 2;
4839
+ const tl = Math.min(Math.max(0, rTL), maxR);
4840
+ const tr = Math.min(Math.max(0, rTR), maxR);
4841
+ const br = Math.min(Math.max(0, rBR), maxR);
4842
+ const bl = Math.min(Math.max(0, rBL), maxR);
4843
+ ctx.beginPath();
4844
+ ctx.moveTo(x + tl, y);
4845
+ ctx.lineTo(x + w - tr, y);
4846
+ if (tr > 0) ctx.quadraticCurveTo(x + w, y, x + w, y + tr);
4847
+ ctx.lineTo(x + w, y + h - br);
4848
+ if (br > 0) ctx.quadraticCurveTo(x + w, y + h, x + w - br, y + h);
4849
+ ctx.lineTo(x + bl, y + h);
4850
+ if (bl > 0) ctx.quadraticCurveTo(x, y + h, x, y + h - bl);
4851
+ ctx.lineTo(x, y + tl);
4852
+ if (tl > 0) ctx.quadraticCurveTo(x, y, x + tl, y);
4853
+ ctx.closePath();
4854
+ }
4855
+ function applyTextBackground(obj, cfg) {
4856
+ var _a;
4857
+ obj[PD_BG_KEY] = { ...cfg };
4858
+ if (obj[PATCHED_KEY]) return;
4859
+ obj[PATCHED_KEY] = true;
4860
+ const originalRender = obj._render.bind(obj);
4861
+ obj._render = function(ctx) {
4862
+ const bg = this[PD_BG_KEY];
4863
+ if (hasTextBackground(bg)) {
4864
+ const w = this.width ?? 0;
4865
+ const h = this.height ?? 0;
4866
+ const pad = Math.max(0, Number(bg.padding ?? 0));
4867
+ const x = -w / 2 - pad;
4868
+ const y = -h / 2 - pad;
4869
+ const bgW = w + pad * 2;
4870
+ const bgH = h + pad * 2;
4871
+ ctx.save();
4872
+ buildRoundedRectPath2D(
4873
+ ctx,
4874
+ x,
4875
+ y,
4876
+ bgW,
4877
+ bgH,
4878
+ bg.rxTL ?? 0,
4879
+ bg.rxTR ?? 0,
4880
+ bg.rxBR ?? 0,
4881
+ bg.rxBL ?? 0
4882
+ );
4883
+ ctx.fillStyle = bg.color;
4884
+ ctx.fill();
4885
+ ctx.restore();
4886
+ }
4887
+ originalRender(ctx);
4888
+ };
4889
+ const originalToObject = obj.toObject.bind(obj);
4890
+ obj.toObject = function(propertiesToInclude) {
4891
+ const out = originalToObject(propertiesToInclude);
4892
+ const bg = this[PD_BG_KEY];
4893
+ if (hasTextBackground(bg)) {
4894
+ out.__pdBg = { ...bg };
4895
+ }
4896
+ return out;
4897
+ };
4898
+ const originalToSVG = (_a = obj.toSVG) == null ? void 0 : _a.bind(obj);
4899
+ if (typeof originalToSVG === "function") {
4900
+ obj.toSVG = function(reviver) {
4901
+ const svg = originalToSVG(reviver);
4902
+ const bg = this[PD_BG_KEY];
4903
+ if (!hasTextBackground(bg)) return svg;
4904
+ const w = this.width ?? 0;
4905
+ const h = this.height ?? 0;
4906
+ const pad = Math.max(0, Number(bg.padding ?? 0));
4907
+ const x = -w / 2 - pad;
4908
+ const y = -h / 2 - pad;
4909
+ const bgW = w + pad * 2;
4910
+ const bgH = h + pad * 2;
4911
+ const d = buildRoundedRectPathD(
4912
+ x,
4913
+ y,
4914
+ bgW,
4915
+ bgH,
4916
+ bg.rxTL ?? 0,
4917
+ bg.rxTR ?? 0,
4918
+ bg.rxBR ?? 0,
4919
+ bg.rxBL ?? 0
4920
+ );
4921
+ const fill = bg.color;
4922
+ const bgPath = `<path d="${d}" fill="${escapeXmlAttr(fill)}" />`;
4923
+ const openTagMatch = svg.match(/^\s*<g\b[^>]*>/);
4924
+ if (openTagMatch) {
4925
+ const openTag = openTagMatch[0];
4926
+ return svg.replace(openTag, openTag + bgPath);
4927
+ }
4928
+ return `<g>${bgPath}${svg}</g>`;
4929
+ };
4930
+ }
4931
+ }
4932
+ function buildRoundedRectPathD(x, y, w, h, rTL, rTR, rBR, rBL) {
4933
+ const maxR = Math.min(w, h) / 2;
4934
+ const tl = Math.min(Math.max(0, rTL), maxR);
4935
+ const tr = Math.min(Math.max(0, rTR), maxR);
4936
+ const br = Math.min(Math.max(0, rBR), maxR);
4937
+ const bl = Math.min(Math.max(0, rBL), maxR);
4938
+ const fmt = (n) => Number.isFinite(n) ? Number(n.toFixed(3)) : 0;
4939
+ const parts = [];
4940
+ parts.push(`M ${fmt(x + tl)} ${fmt(y)}`);
4941
+ parts.push(`L ${fmt(x + w - tr)} ${fmt(y)}`);
4942
+ if (tr > 0) parts.push(`Q ${fmt(x + w)} ${fmt(y)} ${fmt(x + w)} ${fmt(y + tr)}`);
4943
+ parts.push(`L ${fmt(x + w)} ${fmt(y + h - br)}`);
4944
+ if (br > 0) parts.push(`Q ${fmt(x + w)} ${fmt(y + h)} ${fmt(x + w - br)} ${fmt(y + h)}`);
4945
+ parts.push(`L ${fmt(x + bl)} ${fmt(y + h)}`);
4946
+ if (bl > 0) parts.push(`Q ${fmt(x)} ${fmt(y + h)} ${fmt(x)} ${fmt(y + h - bl)}`);
4947
+ parts.push(`L ${fmt(x)} ${fmt(y + tl)}`);
4948
+ if (tl > 0) parts.push(`Q ${fmt(x)} ${fmt(y)} ${fmt(x + tl)} ${fmt(y)}`);
4949
+ parts.push("Z");
4950
+ return parts.join(" ");
4951
+ }
4952
+ function escapeXmlAttr(s) {
4953
+ return String(s).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
4954
+ }
4804
4955
  const TRIANGLE_STROKE_MITER_LIMIT = 1e6;
4805
4956
  const toSafeNumber = (value, fallback = 0) => Number.isFinite(value) ? Math.max(0, Number(value)) : fallback;
4806
4957
  function normalizeShapeType(shapeType) {
@@ -5101,6 +5252,9 @@ function createText(element) {
5101
5252
  textbox.setCoords();
5102
5253
  }
5103
5254
  textbox.dirty = true;
5255
+ applyTextBackground(textbox, extractTextBgConfig(element));
5256
+ const shadow = buildTextShadow(element);
5257
+ if (shadow) textbox.set("shadow", shadow);
5104
5258
  return textbox;
5105
5259
  }
5106
5260
  function createLine(element) {
@@ -7713,7 +7867,19 @@ const PageCanvas = react.forwardRef(
7713
7867
  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
7868
  const fabricText = existingObj.text ?? "";
7715
7869
  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
7870
+ 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.
7871
+ JSON.stringify({
7872
+ c: element.textBgColor ?? null,
7873
+ p: element.textBgPadding ?? 0,
7874
+ tl: element.textBgRxTL ?? 0,
7875
+ tr: element.textBgRxTR ?? 0,
7876
+ br: element.textBgRxBR ?? 0,
7877
+ bl: element.textBgRxBL ?? 0,
7878
+ sc: element.textShadowColor ?? null,
7879
+ sb: element.textShadowBlur ?? 0,
7880
+ sx: element.textShadowOffsetX ?? 0,
7881
+ sy: element.textShadowOffsetY ?? 0
7882
+ }) !== (existingObj.__lastTextBgShadowJson ?? "") || // CRITICAL: Detect gradient fill/stroke changes — serialise to JSON for deep comparison
7717
7883
  JSON.stringify(element.fillGradient || null) !== (existingObj.__lastFillGradientJson ?? "null") || JSON.stringify(element.strokeGradient || null) !== (existingObj.__lastStrokeGradientJson ?? "null");
7718
7884
  const forceApplyFromPanel = syncTriggeredByPanelRef.current;
7719
7885
  const noPropsOrPositionChanged = !positionChanged && !otherPropsChanged;
@@ -8608,6 +8774,26 @@ const PageCanvas = react.forwardRef(
8608
8774
  }
8609
8775
  obj.setCoords();
8610
8776
  obj.dirty = true;
8777
+ try {
8778
+ applyTextBackground(obj, extractTextBgConfig(element));
8779
+ const shadow = buildTextShadow(element);
8780
+ obj.set("shadow", shadow ?? null);
8781
+ obj.__lastTextBgShadowJson = JSON.stringify({
8782
+ c: element.textBgColor ?? null,
8783
+ p: element.textBgPadding ?? 0,
8784
+ tl: element.textBgRxTL ?? 0,
8785
+ tr: element.textBgRxTR ?? 0,
8786
+ br: element.textBgRxBR ?? 0,
8787
+ bl: element.textBgRxBL ?? 0,
8788
+ sc: element.textShadowColor ?? null,
8789
+ sb: element.textShadowBlur ?? 0,
8790
+ sx: element.textShadowOffsetX ?? 0,
8791
+ sy: element.textShadowOffsetY ?? 0
8792
+ });
8793
+ obj.dirty = true;
8794
+ } catch (err) {
8795
+ console.warn("[text-bg] failed to apply background/shadow", err);
8796
+ }
8611
8797
  } else if (!isImage && !isLine) {
8612
8798
  if (obj instanceof fabric__namespace.Circle) {
8613
8799
  obj.set({
@@ -12072,7 +12258,7 @@ function PixldocsPreview(props) {
12072
12258
  !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
12259
  ] });
12074
12260
  }
12075
- const PACKAGE_VERSION = "0.5.40";
12261
+ const PACKAGE_VERSION = "0.5.42";
12076
12262
  let __underlineFixInstalled = false;
12077
12263
  function installUnderlineFix(fab) {
12078
12264
  var _a;