@pairdraw/dom-css 0.21.0 → 0.22.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.
Files changed (38) hide show
  1. package/dist/chunks/behaviours/states/layers.js +100 -15
  2. package/dist/chunks/behaviours/states/layers.js.map +1 -1
  3. package/dist/chunks/browser.js +373 -37
  4. package/dist/chunks/browser.js.map +1 -1
  5. package/dist/chunks/import/html.js +66 -11
  6. package/dist/chunks/import/html.js.map +1 -1
  7. package/dist/chunks/import/jsx/source/ast.js +207 -0
  8. package/dist/chunks/import/jsx/source/ast.js.map +1 -0
  9. package/dist/chunks/import/jsx/source/candidates.js +74 -0
  10. package/dist/chunks/import/jsx/source/candidates.js.map +1 -0
  11. package/dist/chunks/import/jsx/source/evaluate.js +213 -0
  12. package/dist/chunks/import/jsx/source/evaluate.js.map +1 -0
  13. package/dist/chunks/import/jsx/source/index.js +448 -0
  14. package/dist/chunks/import/jsx/source/index.js.map +1 -0
  15. package/dist/export.d.ts +16 -3
  16. package/dist/export.js +2 -2
  17. package/dist/index.d.ts +66 -1
  18. package/dist/index.js +5 -2
  19. package/dist/styling.d.ts +9 -2
  20. package/package.json +5 -3
  21. package/src/cssom/browser.ts +63 -0
  22. package/src/export/css.ts +28 -1
  23. package/src/export/index.ts +3 -0
  24. package/src/export/projection.ts +97 -13
  25. package/src/import/flow.ts +127 -6
  26. package/src/import/jsx/source/ast.ts +267 -0
  27. package/src/import/jsx/source/candidates.ts +72 -0
  28. package/src/import/jsx/source/evaluate.ts +300 -0
  29. package/src/import/jsx/source/index.ts +36 -0
  30. package/src/import/jsx/source/render.ts +543 -0
  31. package/src/import/jsx/source/report.ts +49 -0
  32. package/src/import/scene-graph.ts +19 -7
  33. package/src/index.ts +10 -0
  34. package/src/runtime/browser/runtime.ts +13 -5
  35. package/src/runtime/browser/sandbox.ts +176 -27
  36. package/src/runtime/css/scan.ts +45 -0
  37. package/src/runtime/css/url.ts +92 -0
  38. package/src/runtime/headless/styles.ts +71 -15
@@ -2,7 +2,7 @@ import { isEqual } from "es-toolkit/predicate";
2
2
  import { fromUint8Array } from "js-base64";
3
3
  import { BLACK } from "@pairdraw/scene-graph/constants";
4
4
  import { readIcon } from "@pairdraw/scene-graph/icons";
5
- import { imageMimeType } from "@pairdraw/scene-graph/images";
5
+ import { imageMimeType, imageSize, makeImageFillLocalMatrix } from "@pairdraw/scene-graph/images";
6
6
  import { layoutSizingInParent } from "@pairdraw/scene-graph/layout";
7
7
  import { isShaderPresetPaint, shaderOfPaint } from "@pairdraw/scene-graph/shaders";
8
8
  import { resolveNodeTextDirection } from "@pairdraw/scene-graph/text/direction";
@@ -172,12 +172,40 @@ function positionToCSS(point) {
172
172
  return `${cssNumber(point.x * 100)}% ${cssNumber(point.y * 100)}%`;
173
173
  }
174
174
  /**
175
+ * A diamond gradient as the Pairdraw spelling `get_css` reads and `set_fill` parses: CSS has no
176
+ * diamond form, and the stops are the same colours and positions every other gradient emits.
177
+ */
178
+ function diamondFillToCSS(fill, profile = "srgb") {
179
+ if (fill.type !== "GRADIENT_DIAMOND" || !fill.visible) return void 0;
180
+ const stops = fill.gradientStops;
181
+ if (!stops || stops.length === 0) return void 0;
182
+ return `diamond-gradient(${stops.map((stop) => stopToCSS(stop, fill.opacity, profile)).join(", ")})`;
183
+ }
184
+ /**
185
+ * A stored gradient transform as the matrix the geometry reads. A document or tool boundary can
186
+ * still hand over Figma's two rows of three, which would make every matrix field undefined and the
187
+ * CSS angle NaN; both shapes are accepted here.
188
+ */
189
+ function gradientTransformMatrix(transform) {
190
+ if (!transform) return void 0;
191
+ if (!Array.isArray(transform)) return transform;
192
+ const [[m00, m01, m02], [m10, m11, m12]] = transform;
193
+ return {
194
+ m00,
195
+ m01,
196
+ m02,
197
+ m10,
198
+ m11,
199
+ m12
200
+ };
201
+ }
202
+ /**
175
203
  * A gradient paint as the CSS gradient that draws it in a `width` × `height` box. The transform
176
204
  * maps the box's unit square onto gradient space; its inverse gives the line's ends, the
177
205
  * ellipse's centre and radii, or the sweep's start ray. Diamond gradients have no CSS spelling.
178
206
  */
179
207
  function gradientFillToCSS(fill, width, height, profile = "srgb") {
180
- const transform = fill.gradientTransform;
208
+ const transform = gradientTransformMatrix(fill.gradientTransform);
181
209
  const stops = fill.gradientStops;
182
210
  if (!fill.visible || !transform || !stops || stops.length === 0 || width <= 0 || height <= 0) return;
183
211
  const painted = stops.map((stop) => stopToCSS(stop, fill.opacity, profile)).join(", ");
@@ -1471,8 +1499,27 @@ const VECTOR_SHAPES = /* @__PURE__ */ new Set([
1471
1499
  "STAR",
1472
1500
  "POLYGON"
1473
1501
  ]);
1474
- /** What a box draws itself, which the SVG of a vector layer draws instead. */
1475
- const BOX_PAINT = /^(background|border|box-shadow|outline|filter)/;
1502
+ /** What a vector's own SVG draws, so the placement wrapper leaves it out; a backdrop stays on the wrapper. */
1503
+ const VECTOR_WRAPPER_PAINT = /^(background|border|box-shadow|outline|filter)/;
1504
+ /** Every CSS property that paints a layer, wherever its paint is written. */
1505
+ const PAINT_PROPERTY = /^(background|backdrop-filter|border|box-shadow|outline|filter|color|fill|stroke|text-shadow)/;
1506
+ /** Whether a projected style paints; a radius or a placement property on its own does not. */
1507
+ function paintsSomething(style) {
1508
+ return Object.keys(style).some((property) => PAINT_PROPERTY.test(property) && !property.endsWith("radius"));
1509
+ }
1510
+ /**
1511
+ * Whether a projected layer paints or writes text. A frame with no paint and no children projects
1512
+ * to a bare element with only its size, so a selection of such layers draws nothing.
1513
+ */
1514
+ function designNodeDraws(node) {
1515
+ if (node.type === "text") return node.text.trim().length > 0;
1516
+ if (node.shader?.frame) return true;
1517
+ if (node.tagName === "svg" || node.tagName === "img" || node.tagName === "canvas") return true;
1518
+ return paintsSomething({
1519
+ ...node.computedStyle,
1520
+ ...node.inlineStyle
1521
+ }) || node.children.some(designNodeDraws);
1522
+ }
1476
1523
  const px = (value) => `${value}px`;
1477
1524
  function nodeChildren(graph, node) {
1478
1525
  return node.childIds.map((id) => graph.getNode(id)).filter((child) => child !== void 0);
@@ -1685,12 +1732,44 @@ function addShaderLayerStyle(style) {
1685
1732
  if (!isPositioned(style.position)) style.position = "relative";
1686
1733
  style.isolation = "isolate";
1687
1734
  }
1688
- function addImageStyle(style, node) {
1735
+ /**
1736
+ * Where a crop or tile puts its image in the layer, as CSS background placement. A crop or tile
1737
+ * turned by `imageRotation` (or by rotation inside a crop transform) has no CSS background form;
1738
+ * null keeps the image element for those, covering the box as the closest export.
1739
+ */
1740
+ function backgroundImagePlacement(graph, node) {
1741
+ const fill = node.fills.at(0);
1742
+ if (fill?.type !== "IMAGE" || !fill.visible || !fill.imageHash) return null;
1743
+ if (shaderOfPaint(node, fill)) return null;
1744
+ const mode = fill.imageScaleMode ?? "FILL";
1745
+ if (mode !== "CROP" && mode !== "TILE") return null;
1746
+ const bytes = graph.images.get(fill.imageHash);
1747
+ const natural = bytes ? imageSize(bytes) : null;
1748
+ if (!bytes || !natural) return null;
1749
+ const [m00, m01, m02, m10, m11, m12] = makeImageFillLocalMatrix(fill, node, natural.width, natural.height);
1750
+ if (!(Math.abs(m01) < 1e-6 && Math.abs(m10) < 1e-6 && m00 > 0 && m11 > 0)) return null;
1751
+ const url = imageDataURL(bytes);
1752
+ return {
1753
+ placement: {
1754
+ url,
1755
+ mode
1756
+ },
1757
+ css: {
1758
+ "background-image": `url("${url}")`,
1759
+ "background-size": `${m00 * natural.width}px ${m11 * natural.height}px`,
1760
+ "background-position": `${m02}px ${m12}px`,
1761
+ "background-repeat": mode === "TILE" ? "repeat" : "no-repeat"
1762
+ }
1763
+ };
1764
+ }
1765
+ function addImageStyle(style, node, placement) {
1689
1766
  const fill = node.fills.at(0);
1690
1767
  if (fill?.type !== "IMAGE" || !fill.visible || shaderOfPaint(node, fill)) return;
1691
1768
  if (node.width > 0 && node.height > 0) style["aspect-ratio"] = `${node.width} / ${node.height}`;
1769
+ if (placement) return;
1692
1770
  if (fill.imageScaleMode === "FIT") style["object-fit"] = "contain";
1693
1771
  if (fill.imageScaleMode === "FILL") style["object-fit"] = "cover";
1772
+ if (fill.imageScaleMode === "CROP" || fill.imageScaleMode === "TILE") style["object-fit"] = "cover";
1694
1773
  }
1695
1774
  /** Styles a node takes from its parent's layout, and its own rotation. */
1696
1775
  function addLayoutChild(style, node, parent) {
@@ -1738,16 +1817,17 @@ const CSS_BLEND_MODES = {
1738
1817
  COLOR: "color",
1739
1818
  LUMINOSITY: "luminosity"
1740
1819
  };
1741
- function styleFromSceneNode(node, parent, css, profile) {
1820
+ function styleFromSceneNode(node, parent, css, profile, placement) {
1742
1821
  const style = {};
1743
1822
  addSize(style, node, parent, css);
1744
1823
  addPositioning(style, node, parent);
1745
1824
  addSizeConstraints(style, node, css);
1825
+ if (placement) Object.assign(style, placement.css);
1746
1826
  const fill = fillToCSS(node.fills.at(0), profile);
1747
1827
  if (fill) style["background-color"] = css("fills/0/color", fill);
1748
1828
  const gradients = gradientFillsToCSS(node.fills, node.width, node.height, profile);
1749
1829
  if (gradients) style["background-image"] = gradients;
1750
- addImageStyle(style, node);
1830
+ addImageStyle(style, node, placement?.placement);
1751
1831
  addStroke(style, node, css, profile);
1752
1832
  Object.assign(style, effectsToCSS(node.effects, profile));
1753
1833
  const visibleStrokes = node.strokes.filter((stroke) => stroke.visible);
@@ -1824,6 +1904,7 @@ function attrsForNode(graph, node, { includeSourceIds, includeLayerNames }) {
1824
1904
  if (includeSourceIds) attrs["data-pairdraw-node-id"] = node.id;
1825
1905
  const sourceURL = imageSourceURL(node);
1826
1906
  if (sourceURL) attrs.src = sourceURL;
1907
+ if (backgroundImagePlacement(graph, node)) return attrs;
1827
1908
  const fill = node.fills.at(0);
1828
1909
  if (fill?.type !== "IMAGE" || !fill.imageHash || shaderOfPaint(node, fill)) return attrs;
1829
1910
  const bytes = graph.images.get(fill.imageHash);
@@ -1833,11 +1914,13 @@ function attrsForNode(graph, node, { includeSourceIds, includeLayerNames }) {
1833
1914
  src: imageDataURL(bytes)
1834
1915
  };
1835
1916
  }
1836
- function tagNameForNode(node) {
1917
+ function tagNameForNode(graph, node) {
1837
1918
  if (node.type === "SECTION") return "section";
1838
1919
  const fill = node.fills.at(0);
1839
- if ((fill?.type === "IMAGE" && !shaderOfPaint(node, fill) || imageSourceURL(node)) && node.childIds.length === 0) return "img";
1840
- return "div";
1920
+ const image = fill?.type === "IMAGE" && !shaderOfPaint(node, fill);
1921
+ if (!(image || imageSourceURL(node)) || node.childIds.length > 0) return "div";
1922
+ if (image && backgroundImagePlacement(graph, node)) return "div";
1923
+ return "img";
1841
1924
  }
1842
1925
  /**
1843
1926
  * A vector layer as the renderer's `<svg>`, placed where the layer's box would be: its
@@ -1846,7 +1929,7 @@ function tagNameForNode(node) {
1846
1929
  function vectorDesignNode(context, node, attrs, style) {
1847
1930
  const drawn = context.vectorElement?.(context.graph, node);
1848
1931
  if (!drawn) return null;
1849
- const placement = Object.fromEntries(Object.entries(style).filter(([property]) => !BOX_PAINT.test(property)));
1932
+ const placement = Object.fromEntries(Object.entries(style).filter(([property]) => !VECTOR_WRAPPER_PAINT.test(property)));
1850
1933
  return {
1851
1934
  ...drawn,
1852
1935
  attrs: {
@@ -1910,10 +1993,12 @@ function projectChildren(context, parent, modes, root) {
1910
1993
  }
1911
1994
  function sceneNodeToDesignNode(context, node, inherited, root) {
1912
1995
  if (!node.visible && !context.shown?.has(node.id) || node.internalOnly) return null;
1996
+ if (node.type === "SLICE") return null;
1913
1997
  const { graph, tokens } = context;
1914
1998
  const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
1915
1999
  const entered = tokens?.enter(node, inherited, root);
1916
2000
  const modes = entered?.modes ?? inherited;
2001
+ const placement = backgroundImagePlacement(graph, node);
1917
2002
  const attrs = {
1918
2003
  ...attrsForNode(graph, node, context),
1919
2004
  ...entered?.attrs
@@ -1932,7 +2017,7 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
1932
2017
  };
1933
2018
  }
1934
2019
  if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
1935
- const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css, graph.documentColorSpace));
2020
+ const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css, graph.documentColorSpace, placement));
1936
2021
  if (vector) return vector;
1937
2022
  }
1938
2023
  const children = projectChildren(context, node, modes, false);
@@ -1944,12 +2029,12 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
1944
2029
  sourceSceneNode: node,
1945
2030
  children
1946
2031
  };
1947
- const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace);
2032
+ const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace, placement);
1948
2033
  const shader = layerShader(graph, node);
1949
2034
  if (shader) addShaderLayerStyle(style);
1950
2035
  return {
1951
2036
  type: "element",
1952
- tagName: tagNameForNode(node),
2037
+ tagName: tagNameForNode(graph, node),
1953
2038
  attrs,
1954
2039
  inlineStyle: style,
1955
2040
  sourceSceneNodeId: node.id,
@@ -2158,6 +2243,6 @@ function allElements(root, into = []) {
2158
2243
  return into;
2159
2244
  }
2160
2245
  //#endregion
2161
- export { PLACEMENT, TOKEN_UNIT_PROPERTY, allElements, buildTokenStylesheet, collectionVariables, createTokenValidator, cssNameCodeSyntax, cssNameVariableName, defaultModeAttributeName, defaultModeCondition, deriveCSSName, explicitCSSName, isPositioned, layerKind, layersByKey, loadTokenValidator, mergeVariant, modeAttribute, ownLayerName, parseCSSName, projectVariant, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, stateElement, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
2246
+ export { PLACEMENT, TOKEN_UNIT_PROPERTY, allElements, buildTokenStylesheet, collectionVariables, createTokenValidator, cssNameCodeSyntax, cssNameVariableName, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designNodeDraws, diamondFillToCSS, explicitCSSName, gradientFillToCSS, isPositioned, layerKind, layersByKey, loadTokenValidator, mergeVariant, modeAttribute, ownLayerName, paintsSomething, parseCSSName, projectVariant, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, stateElement, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
2162
2247
 
2163
2248
  //# sourceMappingURL=layers.js.map