@pairdraw/dom-css 0.17.0 → 0.19.0-beta.1

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,13 +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";
12
+ import { normalizeEffects } from "@pairdraw/scene-graph/effects";
13
+ import { textLayerName } from "@pairdraw/scene-graph/node-defaults";
11
14
  //#region src/runtime/browser/runtime.ts
12
15
  const DEFAULT_COMPUTED_PROPERTIES = [
13
16
  "align-items",
@@ -207,7 +210,7 @@ async function computeStylesInIframe(browserDocument, designDocument, cssText, o
207
210
  const content = iframeDocument.createElement("div");
208
211
  content.innerHTML = serializeHTML(designDocument);
209
212
  iframeDocument.body.append(content);
210
- await requestFrame(iframeDocument);
213
+ await requestFrame(browserDocument);
211
214
  return copyComputedStyles(designDocument, content, options);
212
215
  } finally {
213
216
  iframe.remove();
@@ -785,6 +788,16 @@ function applyParagraphs(node, rich) {
785
788
  //#region src/import/scene-graph.ts
786
789
  const DOM_CSS_PLUGIN_ID = "pairdraw-dom-css";
787
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
+ }
788
801
  function textContent(node) {
789
802
  if (node.type === "text") return node.text;
790
803
  return node.children.map(textContent).join("");
@@ -816,6 +829,34 @@ function firstCSSNumber(style, ...properties) {
816
829
  function fillsFromStyle(style, property) {
817
830
  return colorToFillFromCSS(pickStyle(style, property));
818
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
+ }
819
860
  function aspectRatioFromCSS(value) {
820
861
  if (!value || value === "auto") return null;
821
862
  const parts = value.split("/").map((part) => Number.parseFloat(part.trim())).filter(Number.isFinite);
@@ -1000,7 +1041,8 @@ function applyElementStyle(graph, node, element, style) {
1000
1041
  applyPositioning(node, style);
1001
1042
  applyPadding(node, style);
1002
1043
  const fills = fillsFromStyle(style, "background-color");
1003
- 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];
1004
1046
  applyImageFill(graph, node, element, style);
1005
1047
  const strokes = colorToStrokeFromCSS(firstStrokeColor(style), strokeWeightFromStyle(style)?.toString());
1006
1048
  if (strokes.length > 0) {
@@ -1012,8 +1054,8 @@ function applyElementStyle(graph, node, element, style) {
1012
1054
  node.strokes = strokes;
1013
1055
  setBorderWeights(node, style, strokes[0]);
1014
1056
  }
1015
- const effects = parseCSSShadows(pickStyle(style, "box-shadow"));
1016
- if (effects.length > 0) node.effects = effects;
1057
+ const effects = cssShadows(pickStyle(style, "box-shadow"));
1058
+ if (effects.length > 0) node.effects = normalizeEffects(effects);
1017
1059
  const opacity = parseCSSNumber(pickStyle(style, "opacity"));
1018
1060
  if (opacity !== null) node.opacity = opacity;
1019
1061
  applyCornerRadii(node, style);
@@ -1044,8 +1086,8 @@ function applyTextStyle(node, style) {
1044
1086
  Object.assign(node, characterStyleFromCSS(style, pick(node, CHARACTER_FIELDS)));
1045
1087
  const opacity = parseCSSNumber(pickStyle(style, "opacity"));
1046
1088
  if (opacity !== null) node.opacity = opacity;
1047
- const effects = parseCSSShadows(pickStyle(style, "text-shadow"));
1048
- if (effects.length > 0) node.effects = effects;
1089
+ const effects = cssShadows(pickStyle(style, "text-shadow"));
1090
+ if (effects.length > 0) node.effects = normalizeEffects(effects);
1049
1091
  const textAlign = pickStyle(style, "text-align")?.toUpperCase();
1050
1092
  if (textAlign === "CENTER" || textAlign === "RIGHT" || textAlign === "JUSTIFIED") node.textAlignHorizontal = textAlign;
1051
1093
  if (pickStyle(style, "white-space") === "nowrap") node.maxLines = 1;
@@ -1061,7 +1103,7 @@ function createRichTextNode(graph, parentId, element, style, display, parentFlow
1061
1103
  const base = pick(node, CHARACTER_FIELDS);
1062
1104
  const rich = blocks ? blockRichText(element, style, base) : inlineRichText(element, style, base);
1063
1105
  node.text = rich.text;
1064
- node.name = rich.text.slice(0, 32) || "Text";
1106
+ node.name = textLayerName("TEXT", rich.text);
1065
1107
  node.styleRuns = rich.styleRuns;
1066
1108
  if (blocks) applyParagraphs(node, rich);
1067
1109
  applyFlowSizing(graph, node, style, display, parentFlow);
@@ -1083,7 +1125,7 @@ function holdsRichText(element) {
1083
1125
  }
1084
1126
  function createTextNode(graph, parentId, text, style, display, parentFlow) {
1085
1127
  const node = graph.createNode("TEXT", parentId, {
1086
- name: text.slice(0, 32) || "Text",
1128
+ name: textLayerName("TEXT", text),
1087
1129
  text
1088
1130
  });
1089
1131
  applyTextStyle(node, style);