@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.
- package/dist/chunks/behaviours/states/layers.js +352 -38
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +52 -10
- package/dist/chunks/browser.js.map +1 -1
- package/dist/chunks/export/bundle.js +14 -14
- package/dist/chunks/export/bundle.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 +64 -22
- 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 -10
- 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/bundle.ts +17 -21
- 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 +104 -23
- package/src/export/text.ts +51 -2
- package/src/import/scene-graph.ts +64 -8
- 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/stylesheet.ts +33 -1
- 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([
|
|
@@ -508,6 +663,24 @@ function variantNames(scopes) {
|
|
|
508
663
|
* references, and number formulas `calc()` over them; both are declared again in each mode that
|
|
509
664
|
* changes what they point to.
|
|
510
665
|
*/
|
|
666
|
+
/**
|
|
667
|
+
* A name that took a numeric suffix is an alias nobody can read back from the stylesheet, so it
|
|
668
|
+
* is reported rather than left as a silent second spelling of another token.
|
|
669
|
+
*/
|
|
670
|
+
function numberedNameIssues(variables, names, modeOf) {
|
|
671
|
+
const issues = [];
|
|
672
|
+
for (const variable of variables) {
|
|
673
|
+
const name = names.get(variable.id);
|
|
674
|
+
const base = deriveCSSName(variable);
|
|
675
|
+
if (!name || explicitCSSName(variable) || name === base) continue;
|
|
676
|
+
issues.push({
|
|
677
|
+
message: `"${variable.name}" is written --${name}: --${base} is taken`,
|
|
678
|
+
variableId: variable.id,
|
|
679
|
+
modeId: modeOf(variable)
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
return issues;
|
|
683
|
+
}
|
|
511
684
|
function buildTokenStylesheet(source, { format, include = () => true }, validate) {
|
|
512
685
|
const issues = [];
|
|
513
686
|
const collections = [...source.variableCollections.values()];
|
|
@@ -539,6 +712,7 @@ function buildTokenStylesheet(source, { format, include = () => true }, validate
|
|
|
539
712
|
value: css
|
|
540
713
|
};
|
|
541
714
|
}
|
|
715
|
+
issues.push(...numberedNameIssues(variables, names, defaultModeOf));
|
|
542
716
|
const defaults = /* @__PURE__ */ new Map();
|
|
543
717
|
const theme = [];
|
|
544
718
|
const root = [];
|
|
@@ -675,6 +849,19 @@ function sameColor(a, b) {
|
|
|
675
849
|
function isColor(value) {
|
|
676
850
|
return typeof value === "object" && value !== null && "r" in value && "a" in value;
|
|
677
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
|
+
}
|
|
678
865
|
/** The paint a `fills/0/color` or `strokes/1/color` binding names. */
|
|
679
866
|
function boundPaint(node, field) {
|
|
680
867
|
const [list, index] = field.split("/");
|
|
@@ -775,8 +962,8 @@ var DesignTokens = class {
|
|
|
775
962
|
a: paint.opacity
|
|
776
963
|
}, value);
|
|
777
964
|
}
|
|
778
|
-
const drawn =
|
|
779
|
-
if (
|
|
965
|
+
const drawn = drawnNumber(node, field);
|
|
966
|
+
if (drawn === void 0 || typeof value !== "number") return false;
|
|
780
967
|
if (Math.fround(drawn) !== Math.fround(value)) return false;
|
|
781
968
|
if (field === "opacity") return expression || variableUnit(end) === "none";
|
|
782
969
|
return LENGTH_FIELDS.has(field) && (expression || LENGTH_UNITS.has(variableUnit(end)));
|
|
@@ -832,6 +1019,98 @@ function addGridPlacement(style, node) {
|
|
|
832
1019
|
if (position.row > 0) style["grid-row-start"] = String(position.row);
|
|
833
1020
|
}
|
|
834
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
|
|
835
1114
|
//#region src/export/text.ts
|
|
836
1115
|
const CHARACTER_STYLE_KEYS = [
|
|
837
1116
|
"fontFamily",
|
|
@@ -881,6 +1160,22 @@ function textCaseToCSS(value) {
|
|
|
881
1160
|
if (value === "LOWER") return "lowercase";
|
|
882
1161
|
if (value === "TITLE") return "capitalize";
|
|
883
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
|
+
}
|
|
884
1179
|
function characterStyle(node, override = {}) {
|
|
885
1180
|
const style = Object.fromEntries(CHARACTER_STYLE_KEYS.map((key) => [key, node[key]]));
|
|
886
1181
|
for (const key of CHARACTER_STYLE_KEYS) {
|
|
@@ -948,6 +1243,18 @@ function characterCSS(style, decorated) {
|
|
|
948
1243
|
...decorated ? decorationCSS(style) : {}
|
|
949
1244
|
};
|
|
950
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
|
+
}
|
|
951
1258
|
/** What `css` sets differently from `base`. */
|
|
952
1259
|
function differing(css, base) {
|
|
953
1260
|
return Object.fromEntries(Object.entries(css).filter(([property, value]) => value !== "" && base[property] !== value));
|
|
@@ -1000,7 +1307,7 @@ function inlineContent(projection, start, end) {
|
|
|
1000
1307
|
const from = Math.max(start, range.start);
|
|
1001
1308
|
const to = Math.min(end, range.end);
|
|
1002
1309
|
if (to <= from) continue;
|
|
1003
|
-
const css =
|
|
1310
|
+
const css = spanCSS(range.style, !decorated, base);
|
|
1004
1311
|
const text = node.text.slice(from, to);
|
|
1005
1312
|
const previous = pieces.at(-1);
|
|
1006
1313
|
if (previous && previous.href === range.href && isEqual(previous.css, css)) previous.text += text;
|
|
@@ -1197,7 +1504,8 @@ function hugs(node, axis) {
|
|
|
1197
1504
|
* hugs it (an auto layout frame set to Hug, or auto-sizing text), so the page grows with it.
|
|
1198
1505
|
*/
|
|
1199
1506
|
function addSize(style, node, parent, css) {
|
|
1200
|
-
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));
|
|
1201
1509
|
if (sized("width")) style.width = css("width", px(node.width));
|
|
1202
1510
|
if (sized("height")) style.height = css("height", px(node.height));
|
|
1203
1511
|
}
|
|
@@ -1211,18 +1519,6 @@ function stretches(node, parent, axis) {
|
|
|
1211
1519
|
if (parent.layoutMode === "GRID") return true;
|
|
1212
1520
|
return axis !== (parent.layoutMode === "HORIZONTAL" ? "width" : "height");
|
|
1213
1521
|
}
|
|
1214
|
-
/**
|
|
1215
|
-
* Place a layer at its coordinates when its parent does not lay it out: it is absolutely
|
|
1216
|
-
* positioned, or its parent frame has no auto layout. A frame without auto layout becomes the
|
|
1217
|
-
* containing block of the children it places.
|
|
1218
|
-
*/
|
|
1219
|
-
function addPositioning(style, node, parent) {
|
|
1220
|
-
if (node.layoutPositioning === "ABSOLUTE" || parent !== void 0 && parent.type !== "CANVAS" && parent.layoutMode === "NONE") {
|
|
1221
|
-
style.position = "absolute";
|
|
1222
|
-
style.left = `${node.x}px`;
|
|
1223
|
-
style.top = `${node.y}px`;
|
|
1224
|
-
} else if (node.layoutMode === "NONE" && node.type !== "TEXT" && node.childIds.length > 0) style.position = "relative";
|
|
1225
|
-
}
|
|
1226
1522
|
function addSizeConstraints(style, node, css) {
|
|
1227
1523
|
if (node.minWidth !== null) style["min-width"] = css("minWidth", px(node.minWidth));
|
|
1228
1524
|
if (node.maxWidth !== null) style["max-width"] = css("maxWidth", px(node.maxWidth));
|
|
@@ -1397,6 +1693,8 @@ function styleFromSceneNode(node, parent, css) {
|
|
|
1397
1693
|
addSizeConstraints(style, node, css);
|
|
1398
1694
|
const fill = fillToCSS(node.fills.at(0));
|
|
1399
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;
|
|
1400
1698
|
addImageStyle(style, node);
|
|
1401
1699
|
addStroke(style, node, css);
|
|
1402
1700
|
Object.assign(style, effectsToCSS(node.effects));
|
|
@@ -1437,11 +1735,16 @@ function styleFromTextNode(node, parent, css) {
|
|
|
1437
1735
|
style["font-weight"] = String(node.fontWeight);
|
|
1438
1736
|
if (node.italic) style["font-style"] = "italic";
|
|
1439
1737
|
if (node.lineHeight !== null) style["line-height"] = css("lineHeight", lineHeightToCSS(node.lineHeight));
|
|
1440
|
-
|
|
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
|
+
}
|
|
1441
1745
|
if (node.textAlignHorizontal !== "LEFT") style["text-align"] = node.textAlignHorizontal.toLowerCase();
|
|
1442
1746
|
if (node.opacity < 1) style.opacity = css("opacity", String(node.opacity));
|
|
1443
|
-
|
|
1444
|
-
if (shadow) style["text-shadow"] = shadow;
|
|
1747
|
+
Object.assign(style, textEffectsToCSS(node.effects));
|
|
1445
1748
|
if (node.textDecoration !== "NONE") style["text-decoration-line"] = node.textDecoration === "UNDERLINE" ? "underline" : "line-through";
|
|
1446
1749
|
const textTransform = textCaseToCSS(node.textCase);
|
|
1447
1750
|
if (textTransform) style["text-transform"] = textTransform;
|
|
@@ -1573,15 +1876,26 @@ function sceneGraphToDesignDocument(graph, options = {}) {
|
|
|
1573
1876
|
}
|
|
1574
1877
|
/** Project one node, including its own box, instead of the children of a root. */
|
|
1575
1878
|
function sceneNodeToDesignDocument(graph, nodeId, options = {}) {
|
|
1576
|
-
|
|
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 = {}) {
|
|
1577
1886
|
const context = projectionContext(graph, options);
|
|
1578
1887
|
const modes = context.tokens?.defaultModes() ?? /* @__PURE__ */ new Map();
|
|
1579
|
-
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
|
+
}
|
|
1580
1894
|
return {
|
|
1581
1895
|
type: "document",
|
|
1582
1896
|
sourceGraph: graph,
|
|
1583
1897
|
tokens: context.tokens,
|
|
1584
|
-
children
|
|
1898
|
+
children
|
|
1585
1899
|
};
|
|
1586
1900
|
}
|
|
1587
1901
|
//#endregion
|
|
@@ -1738,6 +2052,6 @@ function allElements(root, into = []) {
|
|
|
1738
2052
|
return into;
|
|
1739
2053
|
}
|
|
1740
2054
|
//#endregion
|
|
1741
|
-
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 };
|
|
1742
2056
|
|
|
1743
2057
|
//# sourceMappingURL=layers.js.map
|