@pairdraw/dom-css 0.18.0 → 0.19.0

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.
@@ -1,14 +1,16 @@
1
1
  import { serializeHTML } from "./behaviours/states/tailwind.js";
2
+ import { isEqual } from "es-toolkit/predicate";
2
3
  import { isValid, toUint8Array } from "js-base64";
3
4
  import { TRANSPARENT } from "@pairdraw/scene-graph/constants";
4
5
  import { computeImageHash } from "@pairdraw/scene-graph/images";
5
6
  import { autoLayoutSizingFields, fillSizingFields, layoutSizing, textAutoResizeFor } from "@pairdraw/scene-graph/layout";
6
7
  import { countBy } from "es-toolkit/array";
7
- import { isEqual } from "es-toolkit/predicate";
8
+ import { blurRadiusForSigma } from "@pairdraw/scene-graph/geometry";
8
9
  import { pick } from "es-toolkit";
9
- import { parseCSSAlignItems, parseCSSAlignSelf, parseCSSColor, parseCSSFlexDirection, parseCSSFontFamily, parseCSSJustifyContent, parseCSSLetterSpacing, parseCSSLineHeight, parseCSSNumber, parseCSSShadows } from "@pairdraw/scene-graph/css";
10
+ import { parseCSSAlignItems, parseCSSAlignSelf, parseCSSBackgroundImages, parseCSSColor, parseCSSFlexDirection, parseCSSFontFamily, parseCSSJustifyContent, parseCSSLetterSpacing, parseCSSLineHeight, parseCSSNumber, parseCSSShadows } from "@pairdraw/scene-graph/css";
10
11
  import { SceneGraph } from "@pairdraw/scene-graph";
11
12
  import { normalizeEffects } from "@pairdraw/scene-graph/effects";
13
+ import { textLayerName } from "@pairdraw/scene-graph/node-defaults";
12
14
  //#region src/runtime/browser/runtime.ts
13
15
  const DEFAULT_COMPUTED_PROPERTIES = [
14
16
  "align-items",
@@ -208,7 +210,7 @@ async function computeStylesInIframe(browserDocument, designDocument, cssText, o
208
210
  const content = iframeDocument.createElement("div");
209
211
  content.innerHTML = serializeHTML(designDocument);
210
212
  iframeDocument.body.append(content);
211
- await requestFrame(iframeDocument);
213
+ await requestFrame(browserDocument);
212
214
  return copyComputedStyles(designDocument, content, options);
213
215
  } finally {
214
216
  iframe.remove();
@@ -786,6 +788,16 @@ function applyParagraphs(node, rich) {
786
788
  //#region src/import/scene-graph.ts
787
789
  const DOM_CSS_PLUGIN_ID = "pairdraw-dom-css";
788
790
  const IMAGE_SOURCE_URL_KEY = "image-source-url";
791
+ /**
792
+ * CSS shadows as the layer's effects, each blurred as the browser blurs it: CSS blurs a shadow
793
+ * by half its blur length, which the canvas draws from the radius that gives that sigma.
794
+ */
795
+ function cssShadows(value) {
796
+ return parseCSSShadows(value).map((effect) => effect.type === "SHADER" ? effect : {
797
+ ...effect,
798
+ radius: blurRadiusForSigma(effect.radius / 2)
799
+ });
800
+ }
789
801
  function textContent(node) {
790
802
  if (node.type === "text") return node.text;
791
803
  return node.children.map(textContent).join("");
@@ -817,6 +829,34 @@ function firstCSSNumber(style, ...properties) {
817
829
  function fillsFromStyle(style, property) {
818
830
  return colorToFillFromCSS(pickStyle(style, property));
819
831
  }
832
+ /**
833
+ * The `background-image` layers as fills, bottom-first as Pairdraw paints them: CSS paints the
834
+ * first layer on top, so the list reverses. A `url()` layer becomes an image fill when its
835
+ * bytes are inline; a remote address is skipped, as an unfetchable background paints nothing.
836
+ */
837
+ function backgroundFills(graph, node, style) {
838
+ const layers = parseCSSBackgroundImages(pickStyle(style, "background-image"), node.width, node.height);
839
+ const fills = [];
840
+ for (const layer of layers) {
841
+ if (layer.kind === "gradient") {
842
+ fills.push(layer.fill);
843
+ continue;
844
+ }
845
+ const bytes = bytesFromDataURL(layer.url);
846
+ if (!bytes) continue;
847
+ const imageHash = computeImageHash(bytes);
848
+ graph.images.set(imageHash, bytes);
849
+ fills.push({
850
+ type: "IMAGE",
851
+ imageHash,
852
+ imageScaleMode: "FILL",
853
+ color: { ...TRANSPARENT },
854
+ opacity: 1,
855
+ visible: true
856
+ });
857
+ }
858
+ return fills.reverse();
859
+ }
820
860
  function aspectRatioFromCSS(value) {
821
861
  if (!value || value === "auto") return null;
822
862
  const parts = value.split("/").map((part) => Number.parseFloat(part.trim())).filter(Number.isFinite);
@@ -1001,7 +1041,8 @@ function applyElementStyle(graph, node, element, style) {
1001
1041
  applyPositioning(node, style);
1002
1042
  applyPadding(node, style);
1003
1043
  const fills = fillsFromStyle(style, "background-color");
1004
- if (fills.length > 0) node.fills = fills;
1044
+ const background = backgroundFills(graph, node, style);
1045
+ if (fills.length > 0 || background.length > 0) node.fills = [...fills, ...background];
1005
1046
  applyImageFill(graph, node, element, style);
1006
1047
  const strokes = colorToStrokeFromCSS(firstStrokeColor(style), strokeWeightFromStyle(style)?.toString());
1007
1048
  if (strokes.length > 0) {
@@ -1013,7 +1054,7 @@ function applyElementStyle(graph, node, element, style) {
1013
1054
  node.strokes = strokes;
1014
1055
  setBorderWeights(node, style, strokes[0]);
1015
1056
  }
1016
- const effects = parseCSSShadows(pickStyle(style, "box-shadow"));
1057
+ const effects = cssShadows(pickStyle(style, "box-shadow"));
1017
1058
  if (effects.length > 0) node.effects = normalizeEffects(effects);
1018
1059
  const opacity = parseCSSNumber(pickStyle(style, "opacity"));
1019
1060
  if (opacity !== null) node.opacity = opacity;
@@ -1045,7 +1086,7 @@ function applyTextStyle(node, style) {
1045
1086
  Object.assign(node, characterStyleFromCSS(style, pick(node, CHARACTER_FIELDS)));
1046
1087
  const opacity = parseCSSNumber(pickStyle(style, "opacity"));
1047
1088
  if (opacity !== null) node.opacity = opacity;
1048
- const effects = parseCSSShadows(pickStyle(style, "text-shadow"));
1089
+ const effects = cssShadows(pickStyle(style, "text-shadow"));
1049
1090
  if (effects.length > 0) node.effects = normalizeEffects(effects);
1050
1091
  const textAlign = pickStyle(style, "text-align")?.toUpperCase();
1051
1092
  if (textAlign === "CENTER" || textAlign === "RIGHT" || textAlign === "JUSTIFIED") node.textAlignHorizontal = textAlign;
@@ -1062,7 +1103,7 @@ function createRichTextNode(graph, parentId, element, style, display, parentFlow
1062
1103
  const base = pick(node, CHARACTER_FIELDS);
1063
1104
  const rich = blocks ? blockRichText(element, style, base) : inlineRichText(element, style, base);
1064
1105
  node.text = rich.text;
1065
- node.name = rich.text.slice(0, 32) || "Text";
1106
+ node.name = textLayerName("TEXT", rich.text);
1066
1107
  node.styleRuns = rich.styleRuns;
1067
1108
  if (blocks) applyParagraphs(node, rich);
1068
1109
  applyFlowSizing(graph, node, style, display, parentFlow);
@@ -1084,7 +1125,7 @@ function holdsRichText(element) {
1084
1125
  }
1085
1126
  function createTextNode(graph, parentId, text, style, display, parentFlow) {
1086
1127
  const node = graph.createNode("TEXT", parentId, {
1087
- name: text.slice(0, 32) || "Text",
1128
+ name: textLayerName("TEXT", text),
1088
1129
  text
1089
1130
  });
1090
1131
  applyTextStyle(node, style);