@pairdraw/dom-css 0.19.2 → 0.21.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.
- package/dist/chunks/behaviours/states/layers.js +140 -59
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +585 -74
- package/dist/chunks/browser.js.map +1 -1
- package/dist/export.d.ts +1 -1
- package/package.json +3 -3
- package/src/export/css.ts +39 -22
- package/src/export/projection.ts +125 -24
- package/src/export/text.ts +32 -16
- package/src/import/background-pictures.ts +430 -0
- package/src/import/fonts.ts +10 -29
- package/src/import/scene-graph.ts +62 -59
- package/src/import/stacking.ts +109 -0
- package/src/runtime/browser/runtime.ts +9 -3
- package/src/runtime/browser/sandbox.ts +70 -0
- package/src/tokens/names.ts +3 -3
- package/src/tokens/stylesheet.ts +13 -4
|
@@ -10,7 +10,7 @@ import { compact, uniq } from "es-toolkit/array";
|
|
|
10
10
|
import valueParser from "postcss-value-parser";
|
|
11
11
|
import { themeNamespaces } from "twirlwind";
|
|
12
12
|
import { foldFormula, tokenNumberInUnit, variableValueReferences } from "@pairdraw/scene-graph/variables";
|
|
13
|
-
import {
|
|
13
|
+
import { colorToDocumentCSS } from "@pairdraw/scene-graph/color";
|
|
14
14
|
import { blurSigma } from "@pairdraw/scene-graph/geometry";
|
|
15
15
|
import { gradientPoint } from "@pairdraw/scene-graph/gradient";
|
|
16
16
|
import { hasTextList, listNumberFormat, paragraphSpacingAt, paragraphStyleAt, pixelLength, textLengthPixels, textListItems, textParagraphRanges } from "@pairdraw/scene-graph/text";
|
|
@@ -21,7 +21,7 @@ import { instanceSlotFrames, slotPropertyId } from "@pairdraw/scene-graph/slots"
|
|
|
21
21
|
//#region src/tokens/names.ts
|
|
22
22
|
/** Every variable, collection by collection, in each collection's own order. */
|
|
23
23
|
function collectionVariables(source) {
|
|
24
|
-
return compact([...source.variableCollections.values()].flatMap((collection) => collection.variableIds.map((id) => source.variables.get(id))));
|
|
24
|
+
return compact([...source.variableCollections.values()].filter((collection) => !collection.deleted).flatMap((collection) => collection.variableIds.map((id) => source.variables.get(id))));
|
|
25
25
|
}
|
|
26
26
|
const SCOPE_NAMESPACES = {
|
|
27
27
|
CORNER_RADIUS: "radius",
|
|
@@ -145,27 +145,27 @@ function cssNameVariableName(name) {
|
|
|
145
145
|
}
|
|
146
146
|
//#endregion
|
|
147
147
|
//#region src/export/css.ts
|
|
148
|
-
/**
|
|
149
|
-
function cssColor(color) {
|
|
150
|
-
return
|
|
148
|
+
/** A color as CSS in the document's profile, so a Display P3 color keeps its gamut. */
|
|
149
|
+
function cssColor(color, profile = "srgb") {
|
|
150
|
+
return colorToDocumentCSS(color, profile);
|
|
151
151
|
}
|
|
152
|
-
function fillToCSS(fill) {
|
|
152
|
+
function fillToCSS(fill, profile = "srgb") {
|
|
153
153
|
if (fill?.type !== "SOLID" || !fill.visible) return void 0;
|
|
154
154
|
return cssColor({
|
|
155
155
|
...fill.color,
|
|
156
156
|
a: fill.opacity
|
|
157
|
-
});
|
|
157
|
+
}, profile);
|
|
158
158
|
}
|
|
159
159
|
/** A number CSS reads the same back: six decimals, no trailing zeros or negative zero. */
|
|
160
160
|
function cssNumber(value) {
|
|
161
161
|
const rounded = Number(value.toFixed(6));
|
|
162
162
|
return String(rounded === 0 ? 0 : rounded);
|
|
163
163
|
}
|
|
164
|
-
function stopToCSS(stop, opacity) {
|
|
164
|
+
function stopToCSS(stop, opacity, profile) {
|
|
165
165
|
return `${cssColor({
|
|
166
166
|
...stop.color,
|
|
167
167
|
a: stop.color.a * opacity
|
|
168
|
-
})} ${cssNumber(stop.position * 100)}%`;
|
|
168
|
+
}, profile)} ${cssNumber(stop.position * 100)}%`;
|
|
169
169
|
}
|
|
170
170
|
/** The unit-square point as a CSS position pair, percentages of the box's width and height. */
|
|
171
171
|
function positionToCSS(point) {
|
|
@@ -176,11 +176,11 @@ function positionToCSS(point) {
|
|
|
176
176
|
* maps the box's unit square onto gradient space; its inverse gives the line's ends, the
|
|
177
177
|
* ellipse's centre and radii, or the sweep's start ray. Diamond gradients have no CSS spelling.
|
|
178
178
|
*/
|
|
179
|
-
function gradientFillToCSS(fill, width, height) {
|
|
179
|
+
function gradientFillToCSS(fill, width, height, profile = "srgb") {
|
|
180
180
|
const transform = fill.gradientTransform;
|
|
181
181
|
const stops = fill.gradientStops;
|
|
182
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(", ");
|
|
183
|
+
const painted = stops.map((stop) => stopToCSS(stop, fill.opacity, profile)).join(", ");
|
|
184
184
|
if (fill.type === "GRADIENT_LINEAR") {
|
|
185
185
|
const start = gradientPoint(transform, {
|
|
186
186
|
x: 0,
|
|
@@ -220,10 +220,10 @@ function gradientFillToCSS(fill, width, height) {
|
|
|
220
220
|
* out; the bottom one becomes `background-color`, and a solid above another paint has no CSS
|
|
221
221
|
* spelling here. Image paints export through the element's own source, not this list.
|
|
222
222
|
*/
|
|
223
|
-
function gradientFillsToCSS(fills, width, height) {
|
|
223
|
+
function gradientFillsToCSS(fills, width, height, profile = "srgb") {
|
|
224
224
|
const layers = [];
|
|
225
225
|
for (let index = fills.length - 1; index >= 0; index--) {
|
|
226
|
-
const css = gradientFillToCSS(fills[index], width, height);
|
|
226
|
+
const css = gradientFillToCSS(fills[index], width, height, profile);
|
|
227
227
|
if (css) layers.push(css);
|
|
228
228
|
}
|
|
229
229
|
return layers.length > 0 ? layers.join(", ") : void 0;
|
|
@@ -233,14 +233,15 @@ function gradientFillsToCSS(fills, width, height) {
|
|
|
233
233
|
* border insets the box its absolutely positioned children are placed in, where a stroke does not.
|
|
234
234
|
*/
|
|
235
235
|
function borderWidths(node) {
|
|
236
|
-
|
|
236
|
+
const stroke = node.strokes.find((item) => item.visible);
|
|
237
|
+
if (!strokeColorToCSS(stroke)) return {
|
|
237
238
|
top: 0,
|
|
238
239
|
right: 0,
|
|
239
240
|
bottom: 0,
|
|
240
241
|
left: 0
|
|
241
242
|
};
|
|
242
243
|
if (!node.independentStrokeWeights) {
|
|
243
|
-
const weight =
|
|
244
|
+
const weight = stroke?.weight ?? 0;
|
|
244
245
|
return {
|
|
245
246
|
top: weight,
|
|
246
247
|
right: weight,
|
|
@@ -255,12 +256,12 @@ function borderWidths(node) {
|
|
|
255
256
|
left: node.borderLeftWeight
|
|
256
257
|
};
|
|
257
258
|
}
|
|
258
|
-
function strokeColorToCSS(stroke) {
|
|
259
|
+
function strokeColorToCSS(stroke, profile = "srgb") {
|
|
259
260
|
if (!stroke?.visible) return void 0;
|
|
260
261
|
return cssColor({
|
|
261
262
|
...stroke.color,
|
|
262
263
|
a: stroke.opacity
|
|
263
|
-
});
|
|
264
|
+
}, profile);
|
|
264
265
|
}
|
|
265
266
|
function px$2(value) {
|
|
266
267
|
return `${Number(value.toFixed(3))}px`;
|
|
@@ -269,14 +270,14 @@ function px$2(value) {
|
|
|
269
270
|
* A shadow as `box-shadow` writes it, or as `text-shadow` does without its spread. CSS blurs a
|
|
270
271
|
* shadow by half its blur length, so the length is twice the sigma the canvas blurs it by.
|
|
271
272
|
*/
|
|
272
|
-
function shadowToCSS(effect, spread = true) {
|
|
273
|
+
function shadowToCSS(effect, spread = true, profile = "srgb") {
|
|
273
274
|
const parts = [
|
|
274
275
|
`${effect.type === "INNER_SHADOW" ? "inset " : ""}${px$2(effect.offset.x)}`,
|
|
275
276
|
px$2(effect.offset.y),
|
|
276
277
|
px$2(2 * blurSigma(effect.radius))
|
|
277
278
|
];
|
|
278
279
|
if (spread) parts.push(px$2(effect.spread));
|
|
279
|
-
parts.push(cssColor(effect.color));
|
|
280
|
+
parts.push(cssColor(effect.color, profile));
|
|
280
281
|
return parts.join(" ");
|
|
281
282
|
}
|
|
282
283
|
const isShadow = (effect) => effect.type === "DROP_SHADOW" || effect.type === "INNER_SHADOW";
|
|
@@ -297,11 +298,11 @@ function addBlurs(style, visible) {
|
|
|
297
298
|
* Every visible shadow, in order, plus layer and background blur, blurred as the canvas draws
|
|
298
299
|
* them: CSS's `blur()` takes the Gaussian sigma itself.
|
|
299
300
|
*/
|
|
300
|
-
function effectsToCSS(effects) {
|
|
301
|
+
function effectsToCSS(effects, profile = "srgb") {
|
|
301
302
|
const style = {};
|
|
302
303
|
const visible = effects.filter((effect) => effect.visible);
|
|
303
304
|
const shadows = visible.filter(isShadow);
|
|
304
|
-
if (shadows.length > 0) style["box-shadow"] = shadows.map((effect) => shadowToCSS(effect)).join(", ");
|
|
305
|
+
if (shadows.length > 0) style["box-shadow"] = shadows.map((effect) => shadowToCSS(effect, true, profile)).join(", ");
|
|
305
306
|
addBlurs(style, visible);
|
|
306
307
|
return style;
|
|
307
308
|
}
|
|
@@ -309,11 +310,11 @@ function effectsToCSS(effects) {
|
|
|
309
310
|
* Text's effects: each visible drop shadow as `text-shadow`, which has no spread and shadows the
|
|
310
311
|
* glyphs, and its layer and background blurs. An inner shadow has no form CSS gives text.
|
|
311
312
|
*/
|
|
312
|
-
function textEffectsToCSS(effects) {
|
|
313
|
+
function textEffectsToCSS(effects, profile = "srgb") {
|
|
313
314
|
const style = {};
|
|
314
315
|
const visible = effects.filter((effect) => effect.visible);
|
|
315
316
|
const shadows = visible.filter(isDropShadow);
|
|
316
|
-
if (shadows.length > 0) style["text-shadow"] = shadows.map((effect) => shadowToCSS(effect, false)).join(", ");
|
|
317
|
+
if (shadows.length > 0) style["text-shadow"] = shadows.map((effect) => shadowToCSS(effect, false, profile)).join(", ");
|
|
317
318
|
addBlurs(style, visible);
|
|
318
319
|
return style;
|
|
319
320
|
}
|
|
@@ -609,7 +610,7 @@ function quoteString(value) {
|
|
|
609
610
|
return `"${value.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"").replaceAll("\n", "\\a ")}"`;
|
|
610
611
|
}
|
|
611
612
|
/** One mode's value as CSS, or why it has none. */
|
|
612
|
-
function modeValue(variable, value, modeId, names, variables) {
|
|
613
|
+
function modeValue(variable, value, modeId, names, variables, profile) {
|
|
613
614
|
const expression = variable.expressions?.[modeId];
|
|
614
615
|
if (expression) return expression.css;
|
|
615
616
|
if (value === void 0) return { issue: "has no value in this mode" };
|
|
@@ -622,7 +623,7 @@ function modeValue(variable, value, modeId, names, variables) {
|
|
|
622
623
|
const target = names.get(value.aliasId);
|
|
623
624
|
return target ? `var(--${target})` : { issue: "aliases a variable that does not exist" };
|
|
624
625
|
}
|
|
625
|
-
if (variable.type === "COLOR" && typeof value === "object") return cssColor(value);
|
|
626
|
+
if (variable.type === "COLOR" && typeof value === "object") return cssColor(value, profile);
|
|
626
627
|
if (variable.type === "FLOAT" && typeof value === "number") return tokenNumberToCSS(value, variableUnit(variable));
|
|
627
628
|
if (variable.type === "STRING" && typeof value === "string") return variableNamespace(variable) === "font" ? value : quoteString(value);
|
|
628
629
|
if (variable.type === "BOOLEAN") return { issue: "is a boolean, which CSS has no value for" };
|
|
@@ -690,7 +691,7 @@ function buildTokenStylesheet(source, { format, include = () => true }, validate
|
|
|
690
691
|
const defaultModeOf = (variable) => source.variableCollections.get(variable.collectionId)?.defaultModeId ?? "";
|
|
691
692
|
function declare(variable, modeId, value) {
|
|
692
693
|
const name = names.get(variable.id) ?? variable.id;
|
|
693
|
-
const css = modeValue(variable, value, modeId, names, source.variables);
|
|
694
|
+
const css = modeValue(variable, value, modeId, names, source.variables, source.documentColorSpace);
|
|
694
695
|
if (typeof css !== "string") {
|
|
695
696
|
issues.push({
|
|
696
697
|
message: `${variable.name} ${css.issue}`,
|
|
@@ -1218,21 +1219,21 @@ function styledRanges(node) {
|
|
|
1218
1219
|
return ranges;
|
|
1219
1220
|
}
|
|
1220
1221
|
/** Underline or strikethrough as CSS writes it: Figma's offset is CSS's, from the baseline. */
|
|
1221
|
-
function decorationCSS(style) {
|
|
1222
|
+
function decorationCSS(style, profile) {
|
|
1222
1223
|
if (style.textDecoration === "NONE") return {};
|
|
1223
1224
|
const underline = style.textDecoration === "UNDERLINE";
|
|
1224
1225
|
const css = { "text-decoration-line": underline ? "underline" : "line-through" };
|
|
1225
1226
|
if (style.textDecorationStyle !== "SOLID") css["text-decoration-style"] = style.textDecorationStyle.toLowerCase();
|
|
1226
|
-
const color = fillToCSS(style.textDecorationFills.find((fill) => fill.visible));
|
|
1227
|
+
const color = fillToCSS(style.textDecorationFills.find((fill) => fill.visible), profile);
|
|
1227
1228
|
if (color) css["text-decoration-color"] = color;
|
|
1228
1229
|
if (style.textDecorationThickness !== null) css["text-decoration-thickness"] = px$1(style.textDecorationThickness);
|
|
1229
1230
|
if (underline && style.textUnderlineOffset !== null) css["text-underline-offset"] = px$1(style.textUnderlineOffset);
|
|
1230
1231
|
if (underline && !style.textDecorationSkipInk) css["text-decoration-skip-ink"] = "none";
|
|
1231
1232
|
return css;
|
|
1232
1233
|
}
|
|
1233
|
-
function characterCSS(style, decorated) {
|
|
1234
|
+
function characterCSS(style, decorated, profile) {
|
|
1234
1235
|
return {
|
|
1235
|
-
color: fillToCSS(style.fills.at(0)) ?? "",
|
|
1236
|
+
color: fillToCSS(style.fills.at(0), profile) ?? "",
|
|
1236
1237
|
"font-family": style.fontFamily,
|
|
1237
1238
|
"font-size": px$1(style.fontSize),
|
|
1238
1239
|
"font-weight": String(style.fontWeight),
|
|
@@ -1240,7 +1241,7 @@ function characterCSS(style, decorated) {
|
|
|
1240
1241
|
"letter-spacing": letterSpacingToCSS(style.letterSpacing),
|
|
1241
1242
|
"line-height": style.lineHeight === null ? "normal" : lineHeightToCSS(style.lineHeight),
|
|
1242
1243
|
"text-transform": textCaseToCSS(style.textCase) ?? "none",
|
|
1243
|
-
...decorated ? decorationCSS(style) : {}
|
|
1244
|
+
...decorated ? decorationCSS(style, profile) : {}
|
|
1244
1245
|
};
|
|
1245
1246
|
}
|
|
1246
1247
|
/**
|
|
@@ -1248,8 +1249,8 @@ function characterCSS(style, decorated) {
|
|
|
1248
1249
|
* where the span changes the font size: CSS inherits spacing already resolved at the text's size,
|
|
1249
1250
|
* while the canvas spaces each run by its own size.
|
|
1250
1251
|
*/
|
|
1251
|
-
function spanCSS(style, decorated, base) {
|
|
1252
|
-
const own = characterCSS(style, decorated);
|
|
1252
|
+
function spanCSS(style, decorated, base, profile) {
|
|
1253
|
+
const own = characterCSS(style, decorated, profile);
|
|
1253
1254
|
const css = differing(own, base);
|
|
1254
1255
|
const { unit, value } = style.letterSpacing;
|
|
1255
1256
|
if (unit === "PERCENT" && value !== 0 && "font-size" in css) css["letter-spacing"] = own["letter-spacing"];
|
|
@@ -1263,8 +1264,8 @@ function differing(css, base) {
|
|
|
1263
1264
|
* Whether the text decorates every character alike. CSS draws an element's decoration through
|
|
1264
1265
|
* its descendants, which cannot take it off, so mixed decorations go on the spans instead.
|
|
1265
1266
|
*/
|
|
1266
|
-
function uniformDecoration(node, ranges) {
|
|
1267
|
-
const decorations = ranges.filter((range) => node.text.slice(range.start, range.end).replaceAll("\n", "") !== "").map((range) => decorationCSS(range.style));
|
|
1267
|
+
function uniformDecoration(node, ranges, profile) {
|
|
1268
|
+
const decorations = ranges.filter((range) => node.text.slice(range.start, range.end).replaceAll("\n", "") !== "").map((range) => decorationCSS(range.style, profile));
|
|
1268
1269
|
return decorations.every((decoration) => isEqual(decoration, decorations[0]));
|
|
1269
1270
|
}
|
|
1270
1271
|
function textNode(text) {
|
|
@@ -1301,13 +1302,13 @@ function linkElement(href, css, text) {
|
|
|
1301
1302
|
* linked characters are a link instead.
|
|
1302
1303
|
*/
|
|
1303
1304
|
function inlineContent(projection, start, end) {
|
|
1304
|
-
const { node, ranges, base, decorated } = projection;
|
|
1305
|
+
const { node, ranges, base, decorated, profile } = projection;
|
|
1305
1306
|
const pieces = [];
|
|
1306
1307
|
for (const range of ranges) {
|
|
1307
1308
|
const from = Math.max(start, range.start);
|
|
1308
1309
|
const to = Math.min(end, range.end);
|
|
1309
1310
|
if (to <= from) continue;
|
|
1310
|
-
const css = spanCSS(range.style, !decorated, base);
|
|
1311
|
+
const css = spanCSS(range.style, !decorated, base, profile);
|
|
1311
1312
|
const text = node.text.slice(from, to);
|
|
1312
1313
|
const previous = pieces.at(-1);
|
|
1313
1314
|
if (previous && previous.href === range.href && isEqual(previous.css, css)) previous.text += text;
|
|
@@ -1436,16 +1437,17 @@ function groupEm(projection, paragraphs, item) {
|
|
|
1436
1437
|
* its content. Runs of characters styled apart become spans; text with lists, paragraph
|
|
1437
1438
|
* spacing, or a first-line indent becomes blocks, which a `<div>` holds.
|
|
1438
1439
|
*/
|
|
1439
|
-
function textElementContent(node, layerStyle) {
|
|
1440
|
+
function textElementContent(node, layerStyle, profile = "srgb") {
|
|
1440
1441
|
const ranges = styledRanges(node);
|
|
1441
|
-
const decorated = uniformDecoration(node, ranges);
|
|
1442
|
+
const decorated = uniformDecoration(node, ranges, profile);
|
|
1442
1443
|
const style = Object.fromEntries(Object.entries(layerStyle).filter(([property]) => !DECORATION_PROPERTIES.has(property)));
|
|
1443
|
-
if (decorated) Object.assign(style, decorationCSS(characterStyle(node)));
|
|
1444
|
+
if (decorated) Object.assign(style, decorationCSS(characterStyle(node), profile));
|
|
1444
1445
|
const projection = {
|
|
1445
1446
|
node,
|
|
1446
1447
|
ranges,
|
|
1447
|
-
base: characterCSS(characterStyle(node), decorated),
|
|
1448
|
-
decorated
|
|
1448
|
+
base: characterCSS(characterStyle(node), decorated, profile),
|
|
1449
|
+
decorated,
|
|
1450
|
+
profile
|
|
1449
1451
|
};
|
|
1450
1452
|
if (!needsBlocks(node)) return {
|
|
1451
1453
|
tagName: "span",
|
|
@@ -1558,9 +1560,9 @@ function addCornerRadii(style, node, css) {
|
|
|
1558
1560
|
}
|
|
1559
1561
|
if (node.cornerRadius > 0) style["border-radius"] = css("cornerRadius", px(node.cornerRadius));
|
|
1560
1562
|
}
|
|
1561
|
-
function addStroke(style, node, css) {
|
|
1562
|
-
const stroke = node.strokes.
|
|
1563
|
-
const literalColor = strokeColorToCSS(stroke);
|
|
1563
|
+
function addStroke(style, node, css, profile) {
|
|
1564
|
+
const stroke = node.strokes.find((item) => item.visible);
|
|
1565
|
+
const literalColor = strokeColorToCSS(stroke, profile);
|
|
1564
1566
|
if (!literalColor || !stroke) return;
|
|
1565
1567
|
const color = css("strokes/0/color", literalColor);
|
|
1566
1568
|
const borderStyle = node.dashPattern.length > 0 ? "dashed" : "solid";
|
|
@@ -1576,6 +1578,33 @@ function addStroke(style, node, css) {
|
|
|
1576
1578
|
style["border-bottom-width"] = css("borderBottomWeight", px(node.borderBottomWeight));
|
|
1577
1579
|
style["border-left-width"] = css("borderLeftWeight", px(node.borderLeftWeight));
|
|
1578
1580
|
}
|
|
1581
|
+
/**
|
|
1582
|
+
* CSS gives an element one border, so the visible strokes after the first become rings in
|
|
1583
|
+
* `box-shadow`, in layer order. An outside ring grows from the border's outer edge, an inside one
|
|
1584
|
+
* from its inner edge, and a centred stroke gets a ring on each side of the border. Rings cannot
|
|
1585
|
+
* dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
|
|
1586
|
+
* stroke order that alternates them is approximated.
|
|
1587
|
+
*/
|
|
1588
|
+
function extraStrokeShadows(strokes, first, profile) {
|
|
1589
|
+
const outer = [];
|
|
1590
|
+
const inner = [];
|
|
1591
|
+
let outside = 0;
|
|
1592
|
+
let inside = first.weight;
|
|
1593
|
+
for (const stroke of strokes) {
|
|
1594
|
+
if (!stroke.visible) continue;
|
|
1595
|
+
const color = strokeColorToCSS(stroke, profile);
|
|
1596
|
+
if (!color) continue;
|
|
1597
|
+
if (stroke.align !== "OUTSIDE") {
|
|
1598
|
+
inside += stroke.align === "CENTER" ? stroke.weight / 2 : stroke.weight;
|
|
1599
|
+
inner.push(`inset 0 0 0 ${px(inside)} ${color}`);
|
|
1600
|
+
}
|
|
1601
|
+
if (stroke.align !== "INSIDE") {
|
|
1602
|
+
outside += stroke.align === "CENTER" ? stroke.weight / 2 : stroke.weight;
|
|
1603
|
+
outer.push(`0 0 0 ${px(outside)} ${color}`);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
return [...outer, ...inner];
|
|
1607
|
+
}
|
|
1579
1608
|
const PADDING_FIELDS = [
|
|
1580
1609
|
"paddingTop",
|
|
1581
1610
|
"paddingRight",
|
|
@@ -1709,18 +1738,23 @@ const CSS_BLEND_MODES = {
|
|
|
1709
1738
|
COLOR: "color",
|
|
1710
1739
|
LUMINOSITY: "luminosity"
|
|
1711
1740
|
};
|
|
1712
|
-
function styleFromSceneNode(node, parent, css) {
|
|
1741
|
+
function styleFromSceneNode(node, parent, css, profile) {
|
|
1713
1742
|
const style = {};
|
|
1714
1743
|
addSize(style, node, parent, css);
|
|
1715
1744
|
addPositioning(style, node, parent);
|
|
1716
1745
|
addSizeConstraints(style, node, css);
|
|
1717
|
-
const fill = fillToCSS(node.fills.at(0));
|
|
1746
|
+
const fill = fillToCSS(node.fills.at(0), profile);
|
|
1718
1747
|
if (fill) style["background-color"] = css("fills/0/color", fill);
|
|
1719
|
-
const gradients = gradientFillsToCSS(node.fills, node.width, node.height);
|
|
1748
|
+
const gradients = gradientFillsToCSS(node.fills, node.width, node.height, profile);
|
|
1720
1749
|
if (gradients) style["background-image"] = gradients;
|
|
1721
1750
|
addImageStyle(style, node);
|
|
1722
|
-
addStroke(style, node, css);
|
|
1723
|
-
Object.assign(style, effectsToCSS(node.effects));
|
|
1751
|
+
addStroke(style, node, css, profile);
|
|
1752
|
+
Object.assign(style, effectsToCSS(node.effects, profile));
|
|
1753
|
+
const visibleStrokes = node.strokes.filter((stroke) => stroke.visible);
|
|
1754
|
+
if (visibleStrokes.length > 1) {
|
|
1755
|
+
const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0], profile);
|
|
1756
|
+
if (rings.length > 0) style["box-shadow"] = [rings.join(", "), style["box-shadow"]].filter((shadow) => Boolean(shadow)).join(", ");
|
|
1757
|
+
}
|
|
1724
1758
|
if (node.opacity < 1) style.opacity = css("opacity", String(node.opacity));
|
|
1725
1759
|
const blend = CSS_BLEND_MODES[node.blendMode];
|
|
1726
1760
|
if (blend) style["mix-blend-mode"] = blend;
|
|
@@ -1747,14 +1781,14 @@ function styleFromSceneNode(node, parent, css) {
|
|
|
1747
1781
|
addBoxSizing(style);
|
|
1748
1782
|
return style;
|
|
1749
1783
|
}
|
|
1750
|
-
function styleFromTextNode(node, parent, css) {
|
|
1784
|
+
function styleFromTextNode(node, parent, css, profile) {
|
|
1751
1785
|
const style = {};
|
|
1752
1786
|
addSize(style, node, parent, css);
|
|
1753
1787
|
addPositioning(style, node, parent);
|
|
1754
1788
|
addLayoutChild(style, node, parent);
|
|
1755
1789
|
if (resolveNodeTextDirection(node) === "RTL") style.direction = "rtl";
|
|
1756
|
-
const color = fillToCSS(node.fills.at(0));
|
|
1757
|
-
style.color = color ? css("fills/0/color", color) : cssColor(BLACK);
|
|
1790
|
+
const color = fillToCSS(node.fills.at(0), profile);
|
|
1791
|
+
style.color = color ? css("fills/0/color", color) : cssColor(BLACK, profile);
|
|
1758
1792
|
style["font-family"] = node.fontFamily;
|
|
1759
1793
|
style["font-size"] = css("fontSize", px(node.fontSize));
|
|
1760
1794
|
style["font-weight"] = String(node.fontWeight);
|
|
@@ -1769,7 +1803,7 @@ function styleFromTextNode(node, parent, css) {
|
|
|
1769
1803
|
}
|
|
1770
1804
|
if (node.textAlignHorizontal !== "LEFT") style["text-align"] = node.textAlignHorizontal.toLowerCase();
|
|
1771
1805
|
if (node.opacity < 1) style.opacity = css("opacity", String(node.opacity));
|
|
1772
|
-
Object.assign(style, textEffectsToCSS(node.effects));
|
|
1806
|
+
Object.assign(style, textEffectsToCSS(node.effects, profile));
|
|
1773
1807
|
if (node.textDecoration !== "NONE") style["text-decoration-line"] = node.textDecoration === "UNDERLINE" ? "underline" : "line-through";
|
|
1774
1808
|
const textTransform = textCaseToCSS(node.textCase);
|
|
1775
1809
|
if (textTransform) style["text-transform"] = textTransform;
|
|
@@ -1827,6 +1861,53 @@ function vectorDesignNode(context, node, attrs, style) {
|
|
|
1827
1861
|
sourceSceneNode: node
|
|
1828
1862
|
};
|
|
1829
1863
|
}
|
|
1864
|
+
/** The outline a vector mask clips siblings to, in the sibling's own box; null where CSS cannot. */
|
|
1865
|
+
function vectorMaskClip(mask, sibling) {
|
|
1866
|
+
if (mask.maskType !== "VECTOR" || mask.childIds.length > 0 || mask.rotation !== 0) return null;
|
|
1867
|
+
const left = mask.x - sibling.x;
|
|
1868
|
+
const top = mask.y - sibling.y;
|
|
1869
|
+
if (mask.type === "RECTANGLE") {
|
|
1870
|
+
const right = left + mask.width;
|
|
1871
|
+
const bottom = top + mask.height;
|
|
1872
|
+
return `polygon(${px(left)} ${px(top)}, ${px(right)} ${px(top)}, ${px(right)} ${px(bottom)}, ${px(left)} ${px(bottom)})`;
|
|
1873
|
+
}
|
|
1874
|
+
if (mask.type === "ELLIPSE") {
|
|
1875
|
+
const rx = mask.width / 2;
|
|
1876
|
+
const ry = mask.height / 2;
|
|
1877
|
+
return `ellipse(${px(rx)} ${px(ry)} at ${px(left + rx)} ${px(top + ry)})`;
|
|
1878
|
+
}
|
|
1879
|
+
return null;
|
|
1880
|
+
}
|
|
1881
|
+
/**
|
|
1882
|
+
* Projects a parent's children as the canvas draws them: a mask layer is never a layer of its
|
|
1883
|
+
* own, and one vector mask clips the siblings after it. Alpha and luminance masks have no
|
|
1884
|
+
* faithful CSS form over several elements, and two masks combine, so those stay unclipped.
|
|
1885
|
+
*/
|
|
1886
|
+
function projectChildren(context, parent, modes, root) {
|
|
1887
|
+
const projected = [];
|
|
1888
|
+
let masks = [];
|
|
1889
|
+
let afterMask = false;
|
|
1890
|
+
for (const child of nodeChildren(context.graph, parent)) {
|
|
1891
|
+
if (child.visible && child.isMask) {
|
|
1892
|
+
masks = afterMask ? [...masks, child] : [child];
|
|
1893
|
+
afterMask = true;
|
|
1894
|
+
continue;
|
|
1895
|
+
}
|
|
1896
|
+
afterMask = false;
|
|
1897
|
+
const node = sceneNodeToDesignNode(context, child, modes, root);
|
|
1898
|
+
if (!node) continue;
|
|
1899
|
+
const mask = masks.length === 1 ? masks[0] : null;
|
|
1900
|
+
if (mask && node.type === "element") {
|
|
1901
|
+
const clip = vectorMaskClip(mask, child);
|
|
1902
|
+
if (clip) node.inlineStyle = {
|
|
1903
|
+
...node.inlineStyle,
|
|
1904
|
+
"clip-path": clip
|
|
1905
|
+
};
|
|
1906
|
+
}
|
|
1907
|
+
projected.push(node);
|
|
1908
|
+
}
|
|
1909
|
+
return projected;
|
|
1910
|
+
}
|
|
1830
1911
|
function sceneNodeToDesignNode(context, node, inherited, root) {
|
|
1831
1912
|
if (!node.visible && !context.shown?.has(node.id) || node.internalOnly) return null;
|
|
1832
1913
|
const { graph, tokens } = context;
|
|
@@ -1839,7 +1920,7 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
|
|
|
1839
1920
|
};
|
|
1840
1921
|
const css = (field, literal) => tokens?.reference(node, field, modes) ?? literal;
|
|
1841
1922
|
if (node.type === "TEXT") {
|
|
1842
|
-
const content = textElementContent(node, styleFromTextNode(node, parent, css));
|
|
1923
|
+
const content = textElementContent(node, styleFromTextNode(node, parent, css, graph.documentColorSpace), graph.documentColorSpace);
|
|
1843
1924
|
return {
|
|
1844
1925
|
type: "element",
|
|
1845
1926
|
tagName: content.tagName,
|
|
@@ -1851,10 +1932,10 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
|
|
|
1851
1932
|
};
|
|
1852
1933
|
}
|
|
1853
1934
|
if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
|
|
1854
|
-
const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css));
|
|
1935
|
+
const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css, graph.documentColorSpace));
|
|
1855
1936
|
if (vector) return vector;
|
|
1856
1937
|
}
|
|
1857
|
-
const children =
|
|
1938
|
+
const children = projectChildren(context, node, modes, false);
|
|
1858
1939
|
if (node.type === "CANVAS") return {
|
|
1859
1940
|
type: "element",
|
|
1860
1941
|
tagName: "main",
|
|
@@ -1863,7 +1944,7 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
|
|
|
1863
1944
|
sourceSceneNode: node,
|
|
1864
1945
|
children
|
|
1865
1946
|
};
|
|
1866
|
-
const style = styleFromSceneNode(node, parent, css);
|
|
1947
|
+
const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace);
|
|
1867
1948
|
const shader = layerShader(graph, node);
|
|
1868
1949
|
if (shader) addShaderLayerStyle(style);
|
|
1869
1950
|
return {
|
|
@@ -1891,7 +1972,7 @@ function sceneGraphToDesignDocument(graph, options = {}) {
|
|
|
1891
1972
|
const root = graph.getNode(options.rootId ?? graph.rootId);
|
|
1892
1973
|
const context = projectionContext(graph, options);
|
|
1893
1974
|
const modes = context.tokens?.defaultModes() ?? /* @__PURE__ */ new Map();
|
|
1894
|
-
const children = root ?
|
|
1975
|
+
const children = root ? projectChildren(context, root, modes, true) : [];
|
|
1895
1976
|
return {
|
|
1896
1977
|
type: "document",
|
|
1897
1978
|
sourceGraph: graph,
|