@pairdraw/dom-css 0.18.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.
- package/dist/chunks/behaviours/states/layers.js +333 -38
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +49 -8
- package/dist/chunks/browser.js.map +1 -1
- package/dist/chunks/export/components/groups.js +1 -1
- package/dist/chunks/export/storybook/component.js +47 -17
- package/dist/chunks/export/storybook/component.js.map +1 -1
- package/dist/chunks/export/storybook/export.js +63 -21
- package/dist/chunks/export/storybook/export.js.map +1 -1
- package/dist/chunks/import/html.js.map +1 -1
- package/dist/export.d.ts +7 -3
- package/dist/export.js +2 -2
- package/dist/styling.d.ts +6 -1
- package/package.json +3 -3
- package/src/cssom/types.ts +22 -0
- package/src/export/css.ts +191 -17
- package/src/export/index.ts +1 -0
- package/src/export/position.ts +111 -0
- package/src/export/projection.ts +40 -37
- package/src/export/storybook/component.ts +69 -14
- package/src/export/storybook/export.ts +103 -22
- package/src/export/text.ts +51 -2
- package/src/import/scene-graph.ts +61 -6
- package/src/import/style-attribute.ts +1 -1
- package/src/runtime/browser/runtime.ts +3 -1
- package/src/runtime/headless/styles.ts +6 -6
- package/src/tokens/references.ts +16 -2
- package/src/tokens/validate.ts +2 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isEqual } from "es-toolkit/predicate";
|
|
1
2
|
import { fromUint8Array } from "js-base64";
|
|
2
3
|
import { BLACK } from "@pairdraw/scene-graph/constants";
|
|
3
4
|
import { readIcon } from "@pairdraw/scene-graph/icons";
|
|
@@ -5,13 +6,14 @@ import { imageMimeType } from "@pairdraw/scene-graph/images";
|
|
|
5
6
|
import { layoutSizingInParent } from "@pairdraw/scene-graph/layout";
|
|
6
7
|
import { isShaderPresetPaint, shaderOfPaint } from "@pairdraw/scene-graph/shaders";
|
|
7
8
|
import { resolveNodeTextDirection } from "@pairdraw/scene-graph/text/direction";
|
|
8
|
-
import { compact } from "es-toolkit/array";
|
|
9
|
+
import { compact, uniq } from "es-toolkit/array";
|
|
9
10
|
import valueParser from "postcss-value-parser";
|
|
10
11
|
import { themeNamespaces } from "twirlwind";
|
|
11
12
|
import { foldFormula, tokenNumberInUnit, variableValueReferences } from "@pairdraw/scene-graph/variables";
|
|
12
13
|
import { colorToCSS, colorToHex } from "@pairdraw/scene-graph/color";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
14
|
+
import { blurSigma } from "@pairdraw/scene-graph/geometry";
|
|
15
|
+
import { gradientPoint } from "@pairdraw/scene-graph/gradient";
|
|
16
|
+
import { hasTextList, listNumberFormat, paragraphSpacingAt, paragraphStyleAt, pixelLength, textLengthPixels, textListItems, textParagraphRanges } from "@pairdraw/scene-graph/text";
|
|
15
17
|
import { omit } from "es-toolkit/object";
|
|
16
18
|
import { behaviourOwner, layerPath, partBinding, readBehaviour } from "@pairdraw/scene-graph/behaviours";
|
|
17
19
|
import { instanceMainComponent } from "@pairdraw/scene-graph/instances";
|
|
@@ -154,6 +156,105 @@ function fillToCSS(fill) {
|
|
|
154
156
|
a: fill.opacity
|
|
155
157
|
});
|
|
156
158
|
}
|
|
159
|
+
/** A number CSS reads the same back: six decimals, no trailing zeros or negative zero. */
|
|
160
|
+
function cssNumber(value) {
|
|
161
|
+
const rounded = Number(value.toFixed(6));
|
|
162
|
+
return String(rounded === 0 ? 0 : rounded);
|
|
163
|
+
}
|
|
164
|
+
function stopToCSS(stop, opacity) {
|
|
165
|
+
return `${cssColor({
|
|
166
|
+
...stop.color,
|
|
167
|
+
a: stop.color.a * opacity
|
|
168
|
+
})} ${cssNumber(stop.position * 100)}%`;
|
|
169
|
+
}
|
|
170
|
+
/** The unit-square point as a CSS position pair, percentages of the box's width and height. */
|
|
171
|
+
function positionToCSS(point) {
|
|
172
|
+
return `${cssNumber(point.x * 100)}% ${cssNumber(point.y * 100)}%`;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* A gradient paint as the CSS gradient that draws it in a `width` × `height` box. The transform
|
|
176
|
+
* maps the box's unit square onto gradient space; its inverse gives the line's ends, the
|
|
177
|
+
* ellipse's centre and radii, or the sweep's start ray. Diamond gradients have no CSS spelling.
|
|
178
|
+
*/
|
|
179
|
+
function gradientFillToCSS(fill, width, height) {
|
|
180
|
+
const transform = fill.gradientTransform;
|
|
181
|
+
const stops = fill.gradientStops;
|
|
182
|
+
if (!fill.visible || !transform || !stops || stops.length === 0 || width <= 0 || height <= 0) return;
|
|
183
|
+
const painted = stops.map((stop) => stopToCSS(stop, fill.opacity)).join(", ");
|
|
184
|
+
if (fill.type === "GRADIENT_LINEAR") {
|
|
185
|
+
const start = gradientPoint(transform, {
|
|
186
|
+
x: 0,
|
|
187
|
+
y: .5
|
|
188
|
+
});
|
|
189
|
+
const end = gradientPoint(transform, {
|
|
190
|
+
x: 1,
|
|
191
|
+
y: .5
|
|
192
|
+
});
|
|
193
|
+
const dx = (end.x - start.x) * width;
|
|
194
|
+
const dy = (end.y - start.y) * height;
|
|
195
|
+
if (dx === 0 && dy === 0) return void 0;
|
|
196
|
+
return `linear-gradient(${cssNumber(Math.atan2(dx, -dy) * 180 / Math.PI)}deg, ${painted})`;
|
|
197
|
+
}
|
|
198
|
+
if (fill.type !== "GRADIENT_RADIAL" && fill.type !== "GRADIENT_ANGULAR") return void 0;
|
|
199
|
+
const center = gradientPoint(transform, {
|
|
200
|
+
x: .5,
|
|
201
|
+
y: .5
|
|
202
|
+
});
|
|
203
|
+
const edgeX = gradientPoint(transform, {
|
|
204
|
+
x: 1,
|
|
205
|
+
y: .5
|
|
206
|
+
});
|
|
207
|
+
const edgeY = gradientPoint(transform, {
|
|
208
|
+
x: .5,
|
|
209
|
+
y: 1
|
|
210
|
+
});
|
|
211
|
+
const rx = Math.hypot((edgeX.x - center.x) * width, (edgeX.y - center.y) * height);
|
|
212
|
+
const ry = Math.hypot((edgeY.x - center.x) * width, (edgeY.y - center.y) * height);
|
|
213
|
+
if (rx <= 0 || ry <= 0) return void 0;
|
|
214
|
+
const at = `at ${positionToCSS(center)}`;
|
|
215
|
+
if (fill.type === "GRADIENT_RADIAL") return `radial-gradient(${Math.abs(rx - ry) < 1e-6 ? `circle ${cssNumber(rx)}px` : `ellipse ${cssNumber(rx)}px ${cssNumber(ry)}px`} ${at}, ${painted})`;
|
|
216
|
+
return `conic-gradient(from ${cssNumber(Math.atan2((edgeX.y - center.y) * height, (edgeX.x - center.x) * width) * 180 / Math.PI + 90)}deg ${at}, ${painted})`;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The paint stack as `background-image`, top layer first as CSS paints it. Solid paints stay
|
|
220
|
+
* out; the bottom one becomes `background-color`, and a solid above another paint has no CSS
|
|
221
|
+
* spelling here. Image paints export through the element's own source, not this list.
|
|
222
|
+
*/
|
|
223
|
+
function gradientFillsToCSS(fills, width, height) {
|
|
224
|
+
const layers = [];
|
|
225
|
+
for (let index = fills.length - 1; index >= 0; index--) {
|
|
226
|
+
const css = gradientFillToCSS(fills[index], width, height);
|
|
227
|
+
if (css) layers.push(css);
|
|
228
|
+
}
|
|
229
|
+
return layers.length > 0 ? layers.join(", ") : void 0;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The width of each side of the border a layer's first stroke projects as, zero without one. A
|
|
233
|
+
* border insets the box its absolutely positioned children are placed in, where a stroke does not.
|
|
234
|
+
*/
|
|
235
|
+
function borderWidths(node) {
|
|
236
|
+
if (!strokeColorToCSS(node.strokes.at(0))) return {
|
|
237
|
+
top: 0,
|
|
238
|
+
right: 0,
|
|
239
|
+
bottom: 0,
|
|
240
|
+
left: 0
|
|
241
|
+
};
|
|
242
|
+
if (!node.independentStrokeWeights) {
|
|
243
|
+
const weight = node.strokes[0]?.weight ?? 0;
|
|
244
|
+
return {
|
|
245
|
+
top: weight,
|
|
246
|
+
right: weight,
|
|
247
|
+
bottom: weight,
|
|
248
|
+
left: weight
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
top: node.borderTopWeight,
|
|
253
|
+
right: node.borderRightWeight,
|
|
254
|
+
bottom: node.borderBottomWeight,
|
|
255
|
+
left: node.borderLeftWeight
|
|
256
|
+
};
|
|
257
|
+
}
|
|
157
258
|
function strokeColorToCSS(stroke) {
|
|
158
259
|
if (!stroke?.visible) return void 0;
|
|
159
260
|
return cssColor({
|
|
@@ -161,23 +262,59 @@ function strokeColorToCSS(stroke) {
|
|
|
161
262
|
a: stroke.opacity
|
|
162
263
|
});
|
|
163
264
|
}
|
|
164
|
-
function
|
|
165
|
-
return `${
|
|
265
|
+
function px$2(value) {
|
|
266
|
+
return `${Number(value.toFixed(3))}px`;
|
|
166
267
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
268
|
+
/**
|
|
269
|
+
* A shadow as `box-shadow` writes it, or as `text-shadow` does without its spread. CSS blurs a
|
|
270
|
+
* shadow by half its blur length, so the length is twice the sigma the canvas blurs it by.
|
|
271
|
+
*/
|
|
272
|
+
function shadowToCSS(effect, spread = true) {
|
|
273
|
+
const parts = [
|
|
274
|
+
`${effect.type === "INNER_SHADOW" ? "inset " : ""}${px$2(effect.offset.x)}`,
|
|
275
|
+
px$2(effect.offset.y),
|
|
276
|
+
px$2(2 * blurSigma(effect.radius))
|
|
277
|
+
];
|
|
278
|
+
if (spread) parts.push(px$2(effect.spread));
|
|
279
|
+
parts.push(cssColor(effect.color));
|
|
280
|
+
return parts.join(" ");
|
|
281
|
+
}
|
|
282
|
+
const isShadow = (effect) => effect.type === "DROP_SHADOW" || effect.type === "INNER_SHADOW";
|
|
283
|
+
/**
|
|
284
|
+
* The blurs as the canvas draws them: the first visible layer blur as `filter`, and every
|
|
285
|
+
* background blur in turn as `backdrop-filter`.
|
|
286
|
+
*/
|
|
287
|
+
const isLayerBlur = (effect) => effect.type === "LAYER_BLUR" || effect.type === "FOREGROUND_BLUR";
|
|
288
|
+
const isBackgroundBlur = (effect) => effect.type === "BACKGROUND_BLUR";
|
|
289
|
+
const isDropShadow = (effect) => effect.type === "DROP_SHADOW";
|
|
290
|
+
function addBlurs(style, visible) {
|
|
291
|
+
const layer = visible.find(isLayerBlur);
|
|
292
|
+
if (layer) style.filter = `blur(${px$2(blurSigma(layer.radius))})`;
|
|
293
|
+
const backdrop = visible.filter(isBackgroundBlur);
|
|
294
|
+
if (backdrop.length > 0) style["backdrop-filter"] = backdrop.map((effect) => `blur(${px$2(blurSigma(effect.radius))})`).join(" ");
|
|
170
295
|
}
|
|
171
|
-
/**
|
|
296
|
+
/**
|
|
297
|
+
* Every visible shadow, in order, plus layer and background blur, blurred as the canvas draws
|
|
298
|
+
* them: CSS's `blur()` takes the Gaussian sigma itself.
|
|
299
|
+
*/
|
|
172
300
|
function effectsToCSS(effects) {
|
|
173
301
|
const style = {};
|
|
174
302
|
const visible = effects.filter((effect) => effect.visible);
|
|
175
|
-
const shadows = visible.filter(
|
|
176
|
-
if (shadows.length > 0) style["box-shadow"] = shadows.map(shadowToCSS).join(", ");
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
303
|
+
const shadows = visible.filter(isShadow);
|
|
304
|
+
if (shadows.length > 0) style["box-shadow"] = shadows.map((effect) => shadowToCSS(effect)).join(", ");
|
|
305
|
+
addBlurs(style, visible);
|
|
306
|
+
return style;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Text's effects: each visible drop shadow as `text-shadow`, which has no spread and shadows the
|
|
310
|
+
* glyphs, and its layer and background blurs. An inner shadow has no form CSS gives text.
|
|
311
|
+
*/
|
|
312
|
+
function textEffectsToCSS(effects) {
|
|
313
|
+
const style = {};
|
|
314
|
+
const visible = effects.filter((effect) => effect.visible);
|
|
315
|
+
const shadows = visible.filter(isDropShadow);
|
|
316
|
+
if (shadows.length > 0) style["text-shadow"] = shadows.map((effect) => shadowToCSS(effect, false)).join(", ");
|
|
317
|
+
addBlurs(style, visible);
|
|
181
318
|
return style;
|
|
182
319
|
}
|
|
183
320
|
/** A line height as CSS writes it: a percent as the unitless multiple that follows the font size. */
|
|
@@ -188,6 +325,24 @@ function lineHeightToCSS(length) {
|
|
|
188
325
|
function letterSpacingToCSS(length) {
|
|
189
326
|
return length.unit === "PIXELS" ? `${length.value}px` : `${length.value / 100}em`;
|
|
190
327
|
}
|
|
328
|
+
/**
|
|
329
|
+
* Room for the spacing CSS puts after a line's last letter. A layer fits and aligns its lines by
|
|
330
|
+
* their glyphs, without that spacing, so a spaced text's box is one spacing wider than its own
|
|
331
|
+
* width and a negative margin takes the spacing back: lines break and align as on the canvas, and
|
|
332
|
+
* the text takes the room it does there. Browsers put the spacing on a glyph's right in either
|
|
333
|
+
* direction, so the margin is on the right. `spacing` is the CSS the text's letter spacing is
|
|
334
|
+
* written as, which may refer to a token.
|
|
335
|
+
*/
|
|
336
|
+
function letterSpacingRoomCSS(length, spacing, width) {
|
|
337
|
+
const literal = spacing === letterSpacingToCSS(length);
|
|
338
|
+
return {
|
|
339
|
+
...width !== void 0 && { width: `calc(${width} + ${spacing})` },
|
|
340
|
+
"margin-right": literal ? letterSpacingToCSS({
|
|
341
|
+
...length,
|
|
342
|
+
value: -length.value
|
|
343
|
+
}) : `calc(-1 * ${spacing})`
|
|
344
|
+
};
|
|
345
|
+
}
|
|
191
346
|
//#endregion
|
|
192
347
|
//#region src/tokens/values.ts
|
|
193
348
|
const UNITLESS_SCOPES = /* @__PURE__ */ new Set([
|
|
@@ -694,6 +849,19 @@ function sameColor(a, b) {
|
|
|
694
849
|
function isColor(value) {
|
|
695
850
|
return typeof value === "object" && value !== null && "r" in value && "a" in value;
|
|
696
851
|
}
|
|
852
|
+
/**
|
|
853
|
+
* A field's value as the number a token gives it. A text's letter spacing and line height are
|
|
854
|
+
* lengths: in pixels they match by their value, while a percent of the font size, or automatic
|
|
855
|
+
* line height, has no length token to match and stays literal.
|
|
856
|
+
*/
|
|
857
|
+
function drawnNumber(node, field) {
|
|
858
|
+
if (field === "letterSpacing" || field === "lineHeight") {
|
|
859
|
+
const length = node[field];
|
|
860
|
+
return length?.unit === "PIXELS" ? length.value : void 0;
|
|
861
|
+
}
|
|
862
|
+
const value = Reflect.get(node, field);
|
|
863
|
+
return typeof value === "number" ? value : void 0;
|
|
864
|
+
}
|
|
697
865
|
/** The paint a `fills/0/color` or `strokes/1/color` binding names. */
|
|
698
866
|
function boundPaint(node, field) {
|
|
699
867
|
const [list, index] = field.split("/");
|
|
@@ -794,8 +962,8 @@ var DesignTokens = class {
|
|
|
794
962
|
a: paint.opacity
|
|
795
963
|
}, value);
|
|
796
964
|
}
|
|
797
|
-
const drawn =
|
|
798
|
-
if (
|
|
965
|
+
const drawn = drawnNumber(node, field);
|
|
966
|
+
if (drawn === void 0 || typeof value !== "number") return false;
|
|
799
967
|
if (Math.fround(drawn) !== Math.fround(value)) return false;
|
|
800
968
|
if (field === "opacity") return expression || variableUnit(end) === "none";
|
|
801
969
|
return LENGTH_FIELDS.has(field) && (expression || LENGTH_UNITS.has(variableUnit(end)));
|
|
@@ -851,6 +1019,98 @@ function addGridPlacement(style, node) {
|
|
|
851
1019
|
if (position.row > 0) style["grid-row-start"] = String(position.row);
|
|
852
1020
|
}
|
|
853
1021
|
//#endregion
|
|
1022
|
+
//#region src/export/position.ts
|
|
1023
|
+
const AXES = {
|
|
1024
|
+
width: {
|
|
1025
|
+
constraint: "horizontalConstraint",
|
|
1026
|
+
start: "left",
|
|
1027
|
+
end: "right",
|
|
1028
|
+
at: "x"
|
|
1029
|
+
},
|
|
1030
|
+
height: {
|
|
1031
|
+
constraint: "verticalConstraint",
|
|
1032
|
+
start: "top",
|
|
1033
|
+
end: "bottom",
|
|
1034
|
+
at: "y"
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
const length = (value) => `${Number(value.toFixed(3))}px`;
|
|
1038
|
+
const percent = (value) => `${Number((value * 100).toFixed(4))}%`;
|
|
1039
|
+
/** A percentage of the containing block moved by a length, as `calc()` when it moves. */
|
|
1040
|
+
function shifted(base, by) {
|
|
1041
|
+
if (Number(by.toFixed(3)) === 0) return base;
|
|
1042
|
+
return `calc(${base} ${by < 0 ? "-" : "+"} ${length(Math.abs(by))})`;
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* A placed layer along one axis, measured in the box CSS places it in: inside the parent's
|
|
1046
|
+
* border, which a stroke on the canvas does not take from its children. A parent with no size
|
|
1047
|
+
* on the axis leaves its box to the page, so its children keep their start and size there.
|
|
1048
|
+
*/
|
|
1049
|
+
function placedAxis(node, parent, axis) {
|
|
1050
|
+
const { constraint, start, end, at } = AXES[axis];
|
|
1051
|
+
const border = parent ? borderWidths(parent) : void 0;
|
|
1052
|
+
const inset = border?.[start] ?? 0;
|
|
1053
|
+
const room = (parent?.[axis] ?? 0) - inset - (border?.[end] ?? 0);
|
|
1054
|
+
const kind = parent && room > 0 ? node[constraint] : "MIN";
|
|
1055
|
+
const before = node[at] - inset;
|
|
1056
|
+
return {
|
|
1057
|
+
kind,
|
|
1058
|
+
room,
|
|
1059
|
+
before,
|
|
1060
|
+
after: room - before - node[axis]
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Whether a placed layer's constraint sizes it along an axis: Left and Right and Scale both size
|
|
1065
|
+
* it from the parent, so it takes no fixed size.
|
|
1066
|
+
*/
|
|
1067
|
+
function constraintSizes(node, parent, axis) {
|
|
1068
|
+
const { kind } = placedAxis(node, parent, axis);
|
|
1069
|
+
return kind === "STRETCH" || kind === "SCALE";
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* Whether a layer's parent leaves it to its coordinates: it is absolutely positioned, or its
|
|
1073
|
+
* parent frame has no auto layout. The parent it is placed in is the one its constraints follow.
|
|
1074
|
+
*/
|
|
1075
|
+
function placement(node, parent) {
|
|
1076
|
+
const frame = parent?.type === "CANVAS" ? void 0 : parent;
|
|
1077
|
+
if (node.layoutPositioning === "ABSOLUTE" || frame?.layoutMode === "NONE") return { parent: frame };
|
|
1078
|
+
return null;
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Place a layer by its constraints when its parent does not lay it out. A frame without auto
|
|
1082
|
+
* layout becomes the containing block of the children it places.
|
|
1083
|
+
*/
|
|
1084
|
+
function addPositioning(style, node, parent) {
|
|
1085
|
+
const placed = placement(node, parent);
|
|
1086
|
+
if (placed) {
|
|
1087
|
+
style.position = "absolute";
|
|
1088
|
+
addConstrainedPlacement(style, node, placed.parent);
|
|
1089
|
+
} else if (node.layoutMode === "NONE" && node.type !== "TEXT" && node.childIds.length > 0) style.position = "relative";
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Place a layer by its constraints, so it follows the parent when the page resizes it as the
|
|
1093
|
+
* canvas does (`resize.ts` in `@pairdraw/scene-graph`): Left and Top keep the start edge,
|
|
1094
|
+
* Right and Bottom the end edge, Center the offset from the centre, Left and Right both edges,
|
|
1095
|
+
* and Scale its share of the parent. A stretched size is written out rather than left to both
|
|
1096
|
+
* edges, which an image would not fill.
|
|
1097
|
+
*/
|
|
1098
|
+
function addConstrainedPlacement(style, node, parent) {
|
|
1099
|
+
for (const axis of ["width", "height"]) {
|
|
1100
|
+
const { start, end } = AXES[axis];
|
|
1101
|
+
const { kind, room, before, after } = placedAxis(node, parent, axis);
|
|
1102
|
+
if (kind === "MAX") style[end] = length(after);
|
|
1103
|
+
else if (kind === "CENTER") style[start] = shifted("50%", before - room / 2);
|
|
1104
|
+
else if (kind === "STRETCH") {
|
|
1105
|
+
style[start] = length(before);
|
|
1106
|
+
style[axis] = shifted("100%", node[axis] - room);
|
|
1107
|
+
} else if (kind === "SCALE") {
|
|
1108
|
+
style[start] = percent(before / room);
|
|
1109
|
+
style[axis] = percent(node[axis] / room);
|
|
1110
|
+
} else style[start] = length(before);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
//#endregion
|
|
854
1114
|
//#region src/export/text.ts
|
|
855
1115
|
const CHARACTER_STYLE_KEYS = [
|
|
856
1116
|
"fontFamily",
|
|
@@ -900,6 +1160,22 @@ function textCaseToCSS(value) {
|
|
|
900
1160
|
if (value === "LOWER") return "lowercase";
|
|
901
1161
|
if (value === "TITLE") return "capitalize";
|
|
902
1162
|
}
|
|
1163
|
+
/**
|
|
1164
|
+
* The letter spacing every character of a text has, from its run or the layer, or `undefined`
|
|
1165
|
+
* when characters differ or there are none: then no one spacing ends every line. Characters
|
|
1166
|
+
* spaced and sized as the layer is give the layer's own spacing; otherwise each spacing is
|
|
1167
|
+
* resolved at its characters' font size, as CSS resolves `em` where a run sets its own size.
|
|
1168
|
+
*/
|
|
1169
|
+
function uniformLetterSpacing(node) {
|
|
1170
|
+
const spacings = styledRanges(node).map(({ style }) => ({
|
|
1171
|
+
length: style.letterSpacing,
|
|
1172
|
+
fontSize: style.fontSize
|
|
1173
|
+
}));
|
|
1174
|
+
if (spacings.length === 0) return void 0;
|
|
1175
|
+
if (spacings.every(({ length, fontSize }) => isEqual(length, node.letterSpacing) && (length.unit === "PIXELS" || fontSize === node.fontSize))) return node.letterSpacing;
|
|
1176
|
+
const pixels = uniq(spacings.map(({ length, fontSize }) => textLengthPixels(length, fontSize)));
|
|
1177
|
+
return pixels.length === 1 ? pixelLength(pixels[0]) : void 0;
|
|
1178
|
+
}
|
|
903
1179
|
function characterStyle(node, override = {}) {
|
|
904
1180
|
const style = Object.fromEntries(CHARACTER_STYLE_KEYS.map((key) => [key, node[key]]));
|
|
905
1181
|
for (const key of CHARACTER_STYLE_KEYS) {
|
|
@@ -967,6 +1243,18 @@ function characterCSS(style, decorated) {
|
|
|
967
1243
|
...decorated ? decorationCSS(style) : {}
|
|
968
1244
|
};
|
|
969
1245
|
}
|
|
1246
|
+
/**
|
|
1247
|
+
* What a run's span sets beyond the text's own style. A percent letter spacing is written again
|
|
1248
|
+
* where the span changes the font size: CSS inherits spacing already resolved at the text's size,
|
|
1249
|
+
* while the canvas spaces each run by its own size.
|
|
1250
|
+
*/
|
|
1251
|
+
function spanCSS(style, decorated, base) {
|
|
1252
|
+
const own = characterCSS(style, decorated);
|
|
1253
|
+
const css = differing(own, base);
|
|
1254
|
+
const { unit, value } = style.letterSpacing;
|
|
1255
|
+
if (unit === "PERCENT" && value !== 0 && "font-size" in css) css["letter-spacing"] = own["letter-spacing"];
|
|
1256
|
+
return css;
|
|
1257
|
+
}
|
|
970
1258
|
/** What `css` sets differently from `base`. */
|
|
971
1259
|
function differing(css, base) {
|
|
972
1260
|
return Object.fromEntries(Object.entries(css).filter(([property, value]) => value !== "" && base[property] !== value));
|
|
@@ -1019,7 +1307,7 @@ function inlineContent(projection, start, end) {
|
|
|
1019
1307
|
const from = Math.max(start, range.start);
|
|
1020
1308
|
const to = Math.min(end, range.end);
|
|
1021
1309
|
if (to <= from) continue;
|
|
1022
|
-
const css =
|
|
1310
|
+
const css = spanCSS(range.style, !decorated, base);
|
|
1023
1311
|
const text = node.text.slice(from, to);
|
|
1024
1312
|
const previous = pieces.at(-1);
|
|
1025
1313
|
if (previous && previous.href === range.href && isEqual(previous.css, css)) previous.text += text;
|
|
@@ -1216,7 +1504,8 @@ function hugs(node, axis) {
|
|
|
1216
1504
|
* hugs it (an auto layout frame set to Hug, or auto-sizing text), so the page grows with it.
|
|
1217
1505
|
*/
|
|
1218
1506
|
function addSize(style, node, parent, css) {
|
|
1219
|
-
const
|
|
1507
|
+
const placed = placement(node, parent);
|
|
1508
|
+
const sized = (axis) => node[axis] > 0 && !hugs(node, axis) && !stretches(node, parent, axis) && !(placed && constraintSizes(node, placed.parent, axis));
|
|
1220
1509
|
if (sized("width")) style.width = css("width", px(node.width));
|
|
1221
1510
|
if (sized("height")) style.height = css("height", px(node.height));
|
|
1222
1511
|
}
|
|
@@ -1230,18 +1519,6 @@ function stretches(node, parent, axis) {
|
|
|
1230
1519
|
if (parent.layoutMode === "GRID") return true;
|
|
1231
1520
|
return axis !== (parent.layoutMode === "HORIZONTAL" ? "width" : "height");
|
|
1232
1521
|
}
|
|
1233
|
-
/**
|
|
1234
|
-
* Place a layer at its coordinates when its parent does not lay it out: it is absolutely
|
|
1235
|
-
* positioned, or its parent frame has no auto layout. A frame without auto layout becomes the
|
|
1236
|
-
* containing block of the children it places.
|
|
1237
|
-
*/
|
|
1238
|
-
function addPositioning(style, node, parent) {
|
|
1239
|
-
if (node.layoutPositioning === "ABSOLUTE" || parent !== void 0 && parent.type !== "CANVAS" && parent.layoutMode === "NONE") {
|
|
1240
|
-
style.position = "absolute";
|
|
1241
|
-
style.left = `${node.x}px`;
|
|
1242
|
-
style.top = `${node.y}px`;
|
|
1243
|
-
} else if (node.layoutMode === "NONE" && node.type !== "TEXT" && node.childIds.length > 0) style.position = "relative";
|
|
1244
|
-
}
|
|
1245
1522
|
function addSizeConstraints(style, node, css) {
|
|
1246
1523
|
if (node.minWidth !== null) style["min-width"] = css("minWidth", px(node.minWidth));
|
|
1247
1524
|
if (node.maxWidth !== null) style["max-width"] = css("maxWidth", px(node.maxWidth));
|
|
@@ -1416,6 +1693,8 @@ function styleFromSceneNode(node, parent, css) {
|
|
|
1416
1693
|
addSizeConstraints(style, node, css);
|
|
1417
1694
|
const fill = fillToCSS(node.fills.at(0));
|
|
1418
1695
|
if (fill) style["background-color"] = css("fills/0/color", fill);
|
|
1696
|
+
const gradients = gradientFillsToCSS(node.fills, node.width, node.height);
|
|
1697
|
+
if (gradients) style["background-image"] = gradients;
|
|
1419
1698
|
addImageStyle(style, node);
|
|
1420
1699
|
addStroke(style, node, css);
|
|
1421
1700
|
Object.assign(style, effectsToCSS(node.effects));
|
|
@@ -1456,11 +1735,16 @@ function styleFromTextNode(node, parent, css) {
|
|
|
1456
1735
|
style["font-weight"] = String(node.fontWeight);
|
|
1457
1736
|
if (node.italic) style["font-style"] = "italic";
|
|
1458
1737
|
if (node.lineHeight !== null) style["line-height"] = css("lineHeight", lineHeightToCSS(node.lineHeight));
|
|
1459
|
-
|
|
1738
|
+
const spacing = css("letterSpacing", letterSpacingToCSS(node.letterSpacing));
|
|
1739
|
+
if (node.letterSpacing.value !== 0) style["letter-spacing"] = spacing;
|
|
1740
|
+
const ending = uniformLetterSpacing(node);
|
|
1741
|
+
if (ending && ending.value !== 0) {
|
|
1742
|
+
const written = isEqual(ending, node.letterSpacing) ? spacing : letterSpacingToCSS(ending);
|
|
1743
|
+
Object.assign(style, letterSpacingRoomCSS(ending, written, style.width));
|
|
1744
|
+
}
|
|
1460
1745
|
if (node.textAlignHorizontal !== "LEFT") style["text-align"] = node.textAlignHorizontal.toLowerCase();
|
|
1461
1746
|
if (node.opacity < 1) style.opacity = css("opacity", String(node.opacity));
|
|
1462
|
-
|
|
1463
|
-
if (shadow) style["text-shadow"] = shadow;
|
|
1747
|
+
Object.assign(style, textEffectsToCSS(node.effects));
|
|
1464
1748
|
if (node.textDecoration !== "NONE") style["text-decoration-line"] = node.textDecoration === "UNDERLINE" ? "underline" : "line-through";
|
|
1465
1749
|
const textTransform = textCaseToCSS(node.textCase);
|
|
1466
1750
|
if (textTransform) style["text-transform"] = textTransform;
|
|
@@ -1592,15 +1876,26 @@ function sceneGraphToDesignDocument(graph, options = {}) {
|
|
|
1592
1876
|
}
|
|
1593
1877
|
/** Project one node, including its own box, instead of the children of a root. */
|
|
1594
1878
|
function sceneNodeToDesignDocument(graph, nodeId, options = {}) {
|
|
1595
|
-
|
|
1879
|
+
return sceneNodesToDesignDocument(graph, [nodeId], options);
|
|
1880
|
+
}
|
|
1881
|
+
/**
|
|
1882
|
+
* Project several nodes, including each own box, through one token collector: the document then
|
|
1883
|
+
* declares the tokens every root references, not only those of the last one.
|
|
1884
|
+
*/
|
|
1885
|
+
function sceneNodesToDesignDocument(graph, nodeIds, options = {}) {
|
|
1596
1886
|
const context = projectionContext(graph, options);
|
|
1597
1887
|
const modes = context.tokens?.defaultModes() ?? /* @__PURE__ */ new Map();
|
|
1598
|
-
const
|
|
1888
|
+
const children = [];
|
|
1889
|
+
for (const nodeId of nodeIds) {
|
|
1890
|
+
const node = graph.getNode(nodeId);
|
|
1891
|
+
const projected = node ? sceneNodeToDesignNode(context, node, modes, true) : null;
|
|
1892
|
+
if (projected) children.push(projected);
|
|
1893
|
+
}
|
|
1599
1894
|
return {
|
|
1600
1895
|
type: "document",
|
|
1601
1896
|
sourceGraph: graph,
|
|
1602
1897
|
tokens: context.tokens,
|
|
1603
|
-
children
|
|
1898
|
+
children
|
|
1604
1899
|
};
|
|
1605
1900
|
}
|
|
1606
1901
|
//#endregion
|
|
@@ -1757,6 +2052,6 @@ function allElements(root, into = []) {
|
|
|
1757
2052
|
return into;
|
|
1758
2053
|
}
|
|
1759
2054
|
//#endregion
|
|
1760
|
-
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, stateElement, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
|
|
2055
|
+
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 };
|
|
1761
2056
|
|
|
1762
2057
|
//# sourceMappingURL=layers.js.map
|