@pairdraw/dom-css 0.21.1 → 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 (37) hide show
  1. package/dist/chunks/behaviours/states/layers.js +78 -12
  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 +15 -2
  16. package/dist/export.js +2 -2
  17. package/dist/index.d.ts +66 -1
  18. package/dist/index.js +5 -2
  19. package/package.json +5 -3
  20. package/src/cssom/browser.ts +63 -0
  21. package/src/export/css.ts +28 -1
  22. package/src/export/index.ts +1 -0
  23. package/src/export/projection.ts +69 -10
  24. package/src/import/flow.ts +127 -6
  25. package/src/import/jsx/source/ast.ts +267 -0
  26. package/src/import/jsx/source/candidates.ts +72 -0
  27. package/src/import/jsx/source/evaluate.ts +300 -0
  28. package/src/import/jsx/source/index.ts +36 -0
  29. package/src/import/jsx/source/render.ts +543 -0
  30. package/src/import/jsx/source/report.ts +49 -0
  31. package/src/import/scene-graph.ts +19 -7
  32. package/src/index.ts +10 -0
  33. package/src/runtime/browser/runtime.ts +13 -5
  34. package/src/runtime/browser/sandbox.ts +176 -27
  35. package/src/runtime/css/scan.ts +45 -0
  36. package/src/runtime/css/url.ts +92 -0
  37. 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(", ");
@@ -1704,12 +1732,44 @@ function addShaderLayerStyle(style) {
1704
1732
  if (!isPositioned(style.position)) style.position = "relative";
1705
1733
  style.isolation = "isolate";
1706
1734
  }
1707
- 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) {
1708
1766
  const fill = node.fills.at(0);
1709
1767
  if (fill?.type !== "IMAGE" || !fill.visible || shaderOfPaint(node, fill)) return;
1710
1768
  if (node.width > 0 && node.height > 0) style["aspect-ratio"] = `${node.width} / ${node.height}`;
1769
+ if (placement) return;
1711
1770
  if (fill.imageScaleMode === "FIT") style["object-fit"] = "contain";
1712
1771
  if (fill.imageScaleMode === "FILL") style["object-fit"] = "cover";
1772
+ if (fill.imageScaleMode === "CROP" || fill.imageScaleMode === "TILE") style["object-fit"] = "cover";
1713
1773
  }
1714
1774
  /** Styles a node takes from its parent's layout, and its own rotation. */
1715
1775
  function addLayoutChild(style, node, parent) {
@@ -1757,16 +1817,17 @@ const CSS_BLEND_MODES = {
1757
1817
  COLOR: "color",
1758
1818
  LUMINOSITY: "luminosity"
1759
1819
  };
1760
- function styleFromSceneNode(node, parent, css, profile) {
1820
+ function styleFromSceneNode(node, parent, css, profile, placement) {
1761
1821
  const style = {};
1762
1822
  addSize(style, node, parent, css);
1763
1823
  addPositioning(style, node, parent);
1764
1824
  addSizeConstraints(style, node, css);
1825
+ if (placement) Object.assign(style, placement.css);
1765
1826
  const fill = fillToCSS(node.fills.at(0), profile);
1766
1827
  if (fill) style["background-color"] = css("fills/0/color", fill);
1767
1828
  const gradients = gradientFillsToCSS(node.fills, node.width, node.height, profile);
1768
1829
  if (gradients) style["background-image"] = gradients;
1769
- addImageStyle(style, node);
1830
+ addImageStyle(style, node, placement?.placement);
1770
1831
  addStroke(style, node, css, profile);
1771
1832
  Object.assign(style, effectsToCSS(node.effects, profile));
1772
1833
  const visibleStrokes = node.strokes.filter((stroke) => stroke.visible);
@@ -1843,6 +1904,7 @@ function attrsForNode(graph, node, { includeSourceIds, includeLayerNames }) {
1843
1904
  if (includeSourceIds) attrs["data-pairdraw-node-id"] = node.id;
1844
1905
  const sourceURL = imageSourceURL(node);
1845
1906
  if (sourceURL) attrs.src = sourceURL;
1907
+ if (backgroundImagePlacement(graph, node)) return attrs;
1846
1908
  const fill = node.fills.at(0);
1847
1909
  if (fill?.type !== "IMAGE" || !fill.imageHash || shaderOfPaint(node, fill)) return attrs;
1848
1910
  const bytes = graph.images.get(fill.imageHash);
@@ -1852,11 +1914,13 @@ function attrsForNode(graph, node, { includeSourceIds, includeLayerNames }) {
1852
1914
  src: imageDataURL(bytes)
1853
1915
  };
1854
1916
  }
1855
- function tagNameForNode(node) {
1917
+ function tagNameForNode(graph, node) {
1856
1918
  if (node.type === "SECTION") return "section";
1857
1919
  const fill = node.fills.at(0);
1858
- if ((fill?.type === "IMAGE" && !shaderOfPaint(node, fill) || imageSourceURL(node)) && node.childIds.length === 0) return "img";
1859
- 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";
1860
1924
  }
1861
1925
  /**
1862
1926
  * A vector layer as the renderer's `<svg>`, placed where the layer's box would be: its
@@ -1929,10 +1993,12 @@ function projectChildren(context, parent, modes, root) {
1929
1993
  }
1930
1994
  function sceneNodeToDesignNode(context, node, inherited, root) {
1931
1995
  if (!node.visible && !context.shown?.has(node.id) || node.internalOnly) return null;
1996
+ if (node.type === "SLICE") return null;
1932
1997
  const { graph, tokens } = context;
1933
1998
  const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
1934
1999
  const entered = tokens?.enter(node, inherited, root);
1935
2000
  const modes = entered?.modes ?? inherited;
2001
+ const placement = backgroundImagePlacement(graph, node);
1936
2002
  const attrs = {
1937
2003
  ...attrsForNode(graph, node, context),
1938
2004
  ...entered?.attrs
@@ -1951,7 +2017,7 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
1951
2017
  };
1952
2018
  }
1953
2019
  if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
1954
- 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));
1955
2021
  if (vector) return vector;
1956
2022
  }
1957
2023
  const children = projectChildren(context, node, modes, false);
@@ -1963,12 +2029,12 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
1963
2029
  sourceSceneNode: node,
1964
2030
  children
1965
2031
  };
1966
- const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace);
2032
+ const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace, placement);
1967
2033
  const shader = layerShader(graph, node);
1968
2034
  if (shader) addShaderLayerStyle(style);
1969
2035
  return {
1970
2036
  type: "element",
1971
- tagName: tagNameForNode(node),
2037
+ tagName: tagNameForNode(graph, node),
1972
2038
  attrs,
1973
2039
  inlineStyle: style,
1974
2040
  sourceSceneNodeId: node.id,
@@ -2177,6 +2243,6 @@ function allElements(root, into = []) {
2177
2243
  return into;
2178
2244
  }
2179
2245
  //#endregion
2180
- export { PLACEMENT, TOKEN_UNIT_PROPERTY, allElements, buildTokenStylesheet, collectionVariables, createTokenValidator, cssNameCodeSyntax, cssNameVariableName, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designNodeDraws, explicitCSSName, isPositioned, layerKind, layersByKey, loadTokenValidator, mergeVariant, modeAttribute, ownLayerName, paintsSomething, 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 };
2181
2247
 
2182
2248
  //# sourceMappingURL=layers.js.map