@pairdraw/dom-css 0.20.0 → 0.21.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 +81 -61
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +573 -38
- package/dist/chunks/browser.js.map +1 -1
- package/dist/export.d.ts +3 -3
- package/dist/export.js +2 -2
- package/dist/styling.d.ts +9 -2
- package/package.json +3 -3
- package/src/export/css.ts +36 -20
- package/src/export/index.ts +2 -0
- package/src/export/projection.ts +62 -21
- package/src/export/text.ts +32 -16
- package/src/import/background-pictures.ts +430 -0
- package/src/import/scene-graph.ts +53 -51
- 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
package/dist/export.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration, DesignText } from "./types.js";
|
|
2
|
-
import { COMPONENT_STYLINGS, ComponentStyling, ExportHTMLBundle, ExportHTMLBundleOptions, ExportHTMLFile, SceneGraphToDesignOptions, TailwindStylesheetId, TailwindStylesheetLoader, VectorElementRenderer, WebFontFaceAsset, WebFontFaceRequest, WebFontFaceResolver, exportHTMLBundle, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, serializeHTML } from "./styling.js";
|
|
2
|
+
import { COMPONENT_STYLINGS, ComponentStyling, ExportHTMLBundle, ExportHTMLBundleOptions, ExportHTMLFile, SceneGraphToDesignOptions, TailwindStylesheetId, TailwindStylesheetLoader, VectorElementRenderer, WebFontFaceAsset, WebFontFaceRequest, WebFontFaceResolver, designNodeDraws, exportHTMLBundle, paintsSomething, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, serializeHTML } from "./styling.js";
|
|
3
3
|
import { ThemeNamespace } from "twirlwind";
|
|
4
4
|
import { Behaviour } from "@pairdraw/scene-graph/behaviours";
|
|
5
5
|
import { es } from "@pairdraw/emit";
|
|
@@ -412,7 +412,7 @@ type TokenStylesheet = {
|
|
|
412
412
|
css: string;
|
|
413
413
|
issues: TokenStylesheetIssue[];
|
|
414
414
|
};
|
|
415
|
-
type TokenSource = Pick<SceneGraph, 'variables' | 'variableCollections'>;
|
|
415
|
+
type TokenSource = Pick<SceneGraph, 'variables' | 'variableCollections' | 'documentColorSpace'>;
|
|
416
416
|
/** The attribute a collection's manual modes are switched by, unless it names its own. */
|
|
417
417
|
declare function defaultModeAttributeName(collection: VariableCollection): string;
|
|
418
418
|
/** The attribute that puts an element in a mode with no condition of its own: `data-theme`. */
|
|
@@ -596,5 +596,5 @@ declare function stateTailwindClasses(styles: StateStyles, options?: StateTailwi
|
|
|
596
596
|
/** The state styles as Tailwind utilities on each element of the merged markup. */
|
|
597
597
|
declare function stateStylesToTailwind(styles: StateStyles, options?: StateTailwindOptions): DesignDocument;
|
|
598
598
|
//#endregion
|
|
599
|
-
export { BUTTON_RESET, type BehaviourArgs, type BooleanArg, type BooleanModel, COMPONENT_STYLINGS, type CSSExpressionProblem, type CSSValueToken, type CSSValueTokenKind, type ComponentStyling, type ControlModel, type ControlRole, type CustomPropertyDeclaration, type CustomPropertyScope, type CustomPropertySheet, type DesignDocument, type DesignElement, type DesignNode, type ExportHTMLBundle, type ExportHTMLBundleOptions, type ExportHTMLFile, type FeatureCondition, ICON_PACKAGES, INPUT_RESET, type IconFramework, type IconPackage, LAYER_COMPONENT_FRAMEWORKS, type LayerComponent, type LayerComponentFramework, type LayerMarkup, type SceneGraphToDesignOptions, type StateCondition, type StateElement, type StateNode, type StateRule, type StateStyles, type StateStylesheet, type StateTailwindOptions, type StatesModel, TOKEN_UNIT_PROPERTY, type TailwindJSXWithLayers, type TailwindStylesheetId, type TailwindStylesheetLoader, type TokenStylesheet, type TokenStylesheetFormat, type TokenStylesheetIssue, type TokenStylesheetOptions, type TokenValidator, type VectorElementRenderer, type WebFontFaceAsset, type WebFontFaceRequest, type WebFontFaceResolver, behaviourArgs, behaviourControls, booleanOf, buildTokenStylesheet, collectionVariables, conditionSelector, controlRoles, createTokenValidator, cssExpressionProblem, cssNameCodeSyntax, cssNameVariableName, cssTokenProperty, cssValueTokens, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designDocumentToTailwindJSX, explicitCSSName, exportHTMLBundle, featureConditionCSS, groupOf, iconPackagesFor, isIconPackage, layerComponents, layerMarkup, loadTokenValidator, modeAttribute, modeConditionKey, modeConditionName, parseCSSName, parseFeatureCondition, readCustomProperties, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, sceneNodesToTailwindJSX, sceneNodesToTailwindJSXWithLayers, serializeHTML, stateStyles, stateStylesToCSS, stateStylesToTailwind, stateTailwindClasses, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
|
|
599
|
+
export { BUTTON_RESET, type BehaviourArgs, type BooleanArg, type BooleanModel, COMPONENT_STYLINGS, type CSSExpressionProblem, type CSSValueToken, type CSSValueTokenKind, type ComponentStyling, type ControlModel, type ControlRole, type CustomPropertyDeclaration, type CustomPropertyScope, type CustomPropertySheet, type DesignDocument, type DesignElement, type DesignNode, type ExportHTMLBundle, type ExportHTMLBundleOptions, type ExportHTMLFile, type FeatureCondition, ICON_PACKAGES, INPUT_RESET, type IconFramework, type IconPackage, LAYER_COMPONENT_FRAMEWORKS, type LayerComponent, type LayerComponentFramework, type LayerMarkup, type SceneGraphToDesignOptions, type StateCondition, type StateElement, type StateNode, type StateRule, type StateStyles, type StateStylesheet, type StateTailwindOptions, type StatesModel, TOKEN_UNIT_PROPERTY, type TailwindJSXWithLayers, type TailwindStylesheetId, type TailwindStylesheetLoader, type TokenStylesheet, type TokenStylesheetFormat, type TokenStylesheetIssue, type TokenStylesheetOptions, type TokenValidator, type VectorElementRenderer, type WebFontFaceAsset, type WebFontFaceRequest, type WebFontFaceResolver, behaviourArgs, behaviourControls, booleanOf, buildTokenStylesheet, collectionVariables, conditionSelector, controlRoles, createTokenValidator, cssExpressionProblem, cssNameCodeSyntax, cssNameVariableName, cssTokenProperty, cssValueTokens, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designDocumentToTailwindJSX, designNodeDraws, explicitCSSName, exportHTMLBundle, featureConditionCSS, groupOf, iconPackagesFor, isIconPackage, layerComponents, layerMarkup, loadTokenValidator, modeAttribute, modeConditionKey, modeConditionName, paintsSomething, parseCSSName, parseFeatureCondition, readCustomProperties, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, sceneNodesToTailwindJSX, sceneNodesToTailwindJSXWithLayers, serializeHTML, stateStyles, stateStylesToCSS, stateStylesToTailwind, stateTailwindClasses, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
|
|
600
600
|
//# sourceMappingURL=export.d.ts.map
|
package/dist/export.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TOKEN_UNIT_PROPERTY, buildTokenStylesheet, collectionVariables, createTokenValidator, cssNameCodeSyntax, cssNameVariableName, defaultModeAttributeName, defaultModeCondition, deriveCSSName, explicitCSSName, loadTokenValidator, modeAttribute, parseCSSName, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit } from "./chunks/behaviours/states/layers.js";
|
|
1
|
+
import { TOKEN_UNIT_PROPERTY, buildTokenStylesheet, collectionVariables, createTokenValidator, cssNameCodeSyntax, cssNameVariableName, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designNodeDraws, explicitCSSName, loadTokenValidator, modeAttribute, paintsSomething, parseCSSName, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit } from "./chunks/behaviours/states/layers.js";
|
|
2
2
|
import { serializeHTML, stateStylesToTailwind, stateTailwindClasses } from "./chunks/behaviours/states/tailwind.js";
|
|
3
3
|
import { exportHTMLBundle } from "./chunks/export/bundle.js";
|
|
4
4
|
import { behaviourArgs } from "./chunks/behaviours/args.js";
|
|
@@ -10,4 +10,4 @@ import { ICON_PACKAGES, iconPackagesFor, isIconPackage } from "./chunks/export/c
|
|
|
10
10
|
import { cssExpressionProblem, cssTokenProperty, cssValueTokens, designDocumentToTailwindJSX, featureConditionCSS, modeConditionKey, modeConditionName, parseFeatureCondition, readCustomProperties, sceneNodesToTailwindJSX, sceneNodesToTailwindJSXWithLayers } from "./chunks/export/index.js";
|
|
11
11
|
import { behaviourControls, booleanOf } from "./chunks/behaviours/controls.js";
|
|
12
12
|
import { controlRoles, groupOf } from "./chunks/behaviours/roles.js";
|
|
13
|
-
export { BUTTON_RESET, COMPONENT_STYLINGS, ICON_PACKAGES, INPUT_RESET, LAYER_COMPONENT_FRAMEWORKS, TOKEN_UNIT_PROPERTY, behaviourArgs, behaviourControls, booleanOf, buildTokenStylesheet, collectionVariables, conditionSelector, controlRoles, createTokenValidator, cssExpressionProblem, cssNameCodeSyntax, cssNameVariableName, cssTokenProperty, cssValueTokens, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designDocumentToTailwindJSX, explicitCSSName, exportHTMLBundle, featureConditionCSS, groupOf, iconPackagesFor, isIconPackage, layerComponents, layerMarkup, loadTokenValidator, modeAttribute, modeConditionKey, modeConditionName, parseCSSName, parseFeatureCondition, readCustomProperties, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, sceneNodesToTailwindJSX, sceneNodesToTailwindJSXWithLayers, serializeHTML, stateStyles, stateStylesToCSS, stateStylesToTailwind, stateTailwindClasses, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
|
|
13
|
+
export { BUTTON_RESET, COMPONENT_STYLINGS, ICON_PACKAGES, INPUT_RESET, LAYER_COMPONENT_FRAMEWORKS, TOKEN_UNIT_PROPERTY, behaviourArgs, behaviourControls, booleanOf, buildTokenStylesheet, collectionVariables, conditionSelector, controlRoles, createTokenValidator, cssExpressionProblem, cssNameCodeSyntax, cssNameVariableName, cssTokenProperty, cssValueTokens, defaultModeAttributeName, defaultModeCondition, deriveCSSName, designDocumentToTailwindJSX, designNodeDraws, explicitCSSName, exportHTMLBundle, featureConditionCSS, groupOf, iconPackagesFor, isIconPackage, layerComponents, layerMarkup, loadTokenValidator, modeAttribute, modeConditionKey, modeConditionName, paintsSomething, parseCSSName, parseFeatureCondition, readCustomProperties, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, sceneNodesToTailwindJSX, sceneNodesToTailwindJSXWithLayers, serializeHTML, stateStyles, stateStylesToCSS, stateStylesToTailwind, stateTailwindClasses, tokenNumberToCSS, tokenSlug, tokenStylesheet, variableCSSNames, variableNamespace, variableUnit };
|
package/dist/styling.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DesignDocument, DesignElement, DesignNode } from "./types.js";
|
|
1
|
+
import { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from "./types.js";
|
|
2
2
|
import { SceneGraph, SceneNode } from "@pairdraw/scene-graph";
|
|
3
3
|
//#region src/export/projection.d.ts
|
|
4
4
|
/**
|
|
@@ -19,6 +19,13 @@ interface SceneGraphToDesignOptions {
|
|
|
19
19
|
/** Hidden layers to project as shown, such as the tab panels a component reveals itself. */
|
|
20
20
|
shown?: ReadonlySet<string>;
|
|
21
21
|
}
|
|
22
|
+
/** Whether a projected style paints; a radius or a placement property on its own does not. */
|
|
23
|
+
declare function paintsSomething(style: DesignStyleDeclaration): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Whether a projected layer paints or writes text. A frame with no paint and no children projects
|
|
26
|
+
* to a bare element with only its size, so a selection of such layers draws nothing.
|
|
27
|
+
*/
|
|
28
|
+
declare function designNodeDraws(node: DesignNode): boolean;
|
|
22
29
|
declare function sceneGraphToDesignDocument(graph: SceneGraph, options?: SceneGraphToDesignOptions): DesignDocument;
|
|
23
30
|
/** Project one node, including its own box, instead of the children of a root. */
|
|
24
31
|
declare function sceneNodeToDesignDocument(graph: SceneGraph, nodeId: string, options?: Omit<SceneGraphToDesignOptions, 'rootId'>): DesignDocument;
|
|
@@ -94,5 +101,5 @@ declare function serializeHTML(document: DesignDocument, options?: SerializeHTML
|
|
|
94
101
|
declare const COMPONENT_STYLINGS: readonly ["css", "tailwind"];
|
|
95
102
|
type ComponentStyling = (typeof COMPONENT_STYLINGS)[number];
|
|
96
103
|
//#endregion
|
|
97
|
-
export { COMPONENT_STYLINGS, ComponentStyling, ExportHTMLBundle, ExportHTMLBundleOptions, ExportHTMLFile, SceneGraphToDesignOptions, SerializeHTMLOptions, TailwindStylesheetId, TailwindStylesheetLoader, VectorElementRenderer, WebFontFaceAsset, WebFontFaceRequest, WebFontFaceResolver, exportHTMLBundle, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, serializeHTML, serializeNode };
|
|
104
|
+
export { COMPONENT_STYLINGS, ComponentStyling, ExportHTMLBundle, ExportHTMLBundleOptions, ExportHTMLFile, SceneGraphToDesignOptions, SerializeHTMLOptions, TailwindStylesheetId, TailwindStylesheetLoader, VectorElementRenderer, WebFontFaceAsset, WebFontFaceRequest, WebFontFaceResolver, designNodeDraws, exportHTMLBundle, paintsSomething, sceneGraphToDesignDocument, sceneNodeToDesignDocument, sceneNodesToDesignDocument, serializeHTML, serializeNode };
|
|
98
105
|
//# sourceMappingURL=styling.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/dom-css",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@acemir/cssom": "^0.9.31",
|
|
14
|
-
"@pairdraw/emit": "^0.
|
|
14
|
+
"@pairdraw/emit": "^0.21.1",
|
|
15
15
|
"css-tree": "^3.2.1",
|
|
16
16
|
"cssesc": "^3.0.0",
|
|
17
17
|
"dedent": "^1.7.2",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"twirlwind": "^0.4.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@pairdraw/scene-graph": "^0.
|
|
27
|
+
"@pairdraw/scene-graph": "^0.21.1"
|
|
28
28
|
},
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
package/src/export/css.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
ColorProfile,
|
|
2
3
|
Effect,
|
|
3
4
|
Fill,
|
|
4
5
|
GradientStop,
|
|
@@ -7,21 +8,21 @@ import type {
|
|
|
7
8
|
Stroke,
|
|
8
9
|
TextLength
|
|
9
10
|
} from '@pairdraw/scene-graph'
|
|
10
|
-
import {
|
|
11
|
+
import { colorToDocumentCSS } from '@pairdraw/scene-graph/color'
|
|
11
12
|
import { blurSigma } from '@pairdraw/scene-graph/geometry'
|
|
12
13
|
import { gradientPoint } from '@pairdraw/scene-graph/gradient'
|
|
13
14
|
import type { Color, Vector } from '@pairdraw/scene-graph/primitives'
|
|
14
15
|
|
|
15
16
|
import type { DesignStyleDeclaration } from '../types'
|
|
16
17
|
|
|
17
|
-
/**
|
|
18
|
-
export function cssColor(color: Color): string {
|
|
19
|
-
return
|
|
18
|
+
/** A color as CSS in the document's profile, so a Display P3 color keeps its gamut. */
|
|
19
|
+
export function cssColor(color: Color, profile: ColorProfile = 'srgb'): string {
|
|
20
|
+
return colorToDocumentCSS(color, profile)
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export function fillToCSS(fill: Fill | undefined
|
|
23
|
+
export function fillToCSS(fill: Fill | undefined, profile: ColorProfile = 'srgb') {
|
|
23
24
|
if (fill?.type !== 'SOLID' || !fill.visible) return undefined
|
|
24
|
-
return cssColor({ ...fill.color, a: fill.opacity })
|
|
25
|
+
return cssColor({ ...fill.color, a: fill.opacity }, profile)
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/** A number CSS reads the same back: six decimals, no trailing zeros or negative zero. */
|
|
@@ -30,8 +31,8 @@ function cssNumber(value: number): string {
|
|
|
30
31
|
return String(rounded === 0 ? 0 : rounded)
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
function stopToCSS(stop: GradientStop, opacity: number): string {
|
|
34
|
-
const color = cssColor({ ...stop.color, a: stop.color.a * opacity })
|
|
34
|
+
function stopToCSS(stop: GradientStop, opacity: number, profile: ColorProfile): string {
|
|
35
|
+
const color = cssColor({ ...stop.color, a: stop.color.a * opacity }, profile)
|
|
35
36
|
return `${color} ${cssNumber(stop.position * 100)}%`
|
|
36
37
|
}
|
|
37
38
|
|
|
@@ -45,13 +46,18 @@ function positionToCSS(point: Vector): string {
|
|
|
45
46
|
* maps the box's unit square onto gradient space; its inverse gives the line's ends, the
|
|
46
47
|
* ellipse's centre and radii, or the sweep's start ray. Diamond gradients have no CSS spelling.
|
|
47
48
|
*/
|
|
48
|
-
export function gradientFillToCSS(
|
|
49
|
+
export function gradientFillToCSS(
|
|
50
|
+
fill: Fill,
|
|
51
|
+
width: number,
|
|
52
|
+
height: number,
|
|
53
|
+
profile: ColorProfile = 'srgb'
|
|
54
|
+
): string | undefined {
|
|
49
55
|
const transform = fill.gradientTransform
|
|
50
56
|
const stops = fill.gradientStops
|
|
51
57
|
if (!fill.visible || !transform || !stops || stops.length === 0 || width <= 0 || height <= 0) {
|
|
52
58
|
return undefined
|
|
53
59
|
}
|
|
54
|
-
const painted = stops.map((stop) => stopToCSS(stop, fill.opacity)).join(', ')
|
|
60
|
+
const painted = stops.map((stop) => stopToCSS(stop, fill.opacity, profile)).join(', ')
|
|
55
61
|
if (fill.type === 'GRADIENT_LINEAR') {
|
|
56
62
|
const start = gradientPoint(transform, { x: 0, y: 0.5 })
|
|
57
63
|
const end = gradientPoint(transform, { x: 1, y: 0.5 })
|
|
@@ -90,11 +96,12 @@ export function gradientFillToCSS(fill: Fill, width: number, height: number): st
|
|
|
90
96
|
export function gradientFillsToCSS(
|
|
91
97
|
fills: Fill[],
|
|
92
98
|
width: number,
|
|
93
|
-
height: number
|
|
99
|
+
height: number,
|
|
100
|
+
profile: ColorProfile = 'srgb'
|
|
94
101
|
): string | undefined {
|
|
95
102
|
const layers: string[] = []
|
|
96
103
|
for (let index = fills.length - 1; index >= 0; index--) {
|
|
97
|
-
const css = gradientFillToCSS(fills[index], width, height)
|
|
104
|
+
const css = gradientFillToCSS(fills[index], width, height, profile)
|
|
98
105
|
if (css) layers.push(css)
|
|
99
106
|
}
|
|
100
107
|
return layers.length > 0 ? layers.join(', ') : undefined
|
|
@@ -121,9 +128,12 @@ export function borderWidths(node: SceneNode): Record<BorderSide, number> {
|
|
|
121
128
|
}
|
|
122
129
|
}
|
|
123
130
|
|
|
124
|
-
export function strokeColorToCSS(
|
|
131
|
+
export function strokeColorToCSS(
|
|
132
|
+
stroke: Stroke | undefined,
|
|
133
|
+
profile: ColorProfile = 'srgb'
|
|
134
|
+
): string | undefined {
|
|
125
135
|
if (!stroke?.visible) return undefined
|
|
126
|
-
return cssColor({ ...stroke.color, a: stroke.opacity })
|
|
136
|
+
return cssColor({ ...stroke.color, a: stroke.opacity }, profile)
|
|
127
137
|
}
|
|
128
138
|
|
|
129
139
|
function px(value: number): string {
|
|
@@ -134,7 +144,7 @@ function px(value: number): string {
|
|
|
134
144
|
* A shadow as `box-shadow` writes it, or as `text-shadow` does without its spread. CSS blurs a
|
|
135
145
|
* shadow by half its blur length, so the length is twice the sigma the canvas blurs it by.
|
|
136
146
|
*/
|
|
137
|
-
function shadowToCSS(effect: ShadowEffect, spread = true): string {
|
|
147
|
+
function shadowToCSS(effect: ShadowEffect, spread = true, profile: ColorProfile = 'srgb'): string {
|
|
138
148
|
const inset = effect.type === 'INNER_SHADOW' ? 'inset ' : ''
|
|
139
149
|
const parts = [
|
|
140
150
|
`${inset}${px(effect.offset.x)}`,
|
|
@@ -142,7 +152,7 @@ function shadowToCSS(effect: ShadowEffect, spread = true): string {
|
|
|
142
152
|
px(2 * blurSigma(effect.radius))
|
|
143
153
|
]
|
|
144
154
|
if (spread) parts.push(px(effect.spread))
|
|
145
|
-
parts.push(cssColor(effect.color))
|
|
155
|
+
parts.push(cssColor(effect.color, profile))
|
|
146
156
|
return parts.join(' ')
|
|
147
157
|
}
|
|
148
158
|
|
|
@@ -174,12 +184,15 @@ function addBlurs(style: DesignStyleDeclaration, visible: Effect[]): void {
|
|
|
174
184
|
* Every visible shadow, in order, plus layer and background blur, blurred as the canvas draws
|
|
175
185
|
* them: CSS's `blur()` takes the Gaussian sigma itself.
|
|
176
186
|
*/
|
|
177
|
-
export function effectsToCSS(
|
|
187
|
+
export function effectsToCSS(
|
|
188
|
+
effects: Effect[],
|
|
189
|
+
profile: ColorProfile = 'srgb'
|
|
190
|
+
): DesignStyleDeclaration {
|
|
178
191
|
const style: DesignStyleDeclaration = {}
|
|
179
192
|
const visible = effects.filter((effect) => effect.visible)
|
|
180
193
|
const shadows = visible.filter(isShadow)
|
|
181
194
|
if (shadows.length > 0)
|
|
182
|
-
style['box-shadow'] = shadows.map((effect) => shadowToCSS(effect)).join(', ')
|
|
195
|
+
style['box-shadow'] = shadows.map((effect) => shadowToCSS(effect, true, profile)).join(', ')
|
|
183
196
|
addBlurs(style, visible)
|
|
184
197
|
return style
|
|
185
198
|
}
|
|
@@ -188,12 +201,15 @@ export function effectsToCSS(effects: Effect[]): DesignStyleDeclaration {
|
|
|
188
201
|
* Text's effects: each visible drop shadow as `text-shadow`, which has no spread and shadows the
|
|
189
202
|
* glyphs, and its layer and background blurs. An inner shadow has no form CSS gives text.
|
|
190
203
|
*/
|
|
191
|
-
export function textEffectsToCSS(
|
|
204
|
+
export function textEffectsToCSS(
|
|
205
|
+
effects: Effect[],
|
|
206
|
+
profile: ColorProfile = 'srgb'
|
|
207
|
+
): DesignStyleDeclaration {
|
|
192
208
|
const style: DesignStyleDeclaration = {}
|
|
193
209
|
const visible = effects.filter((effect) => effect.visible)
|
|
194
210
|
const shadows = visible.filter(isDropShadow)
|
|
195
211
|
if (shadows.length > 0) {
|
|
196
|
-
style['text-shadow'] = shadows.map((effect) => shadowToCSS(effect, false)).join(', ')
|
|
212
|
+
style['text-shadow'] = shadows.map((effect) => shadowToCSS(effect, false, profile)).join(', ')
|
|
197
213
|
}
|
|
198
214
|
addBlurs(style, visible)
|
|
199
215
|
return style
|
package/src/export/index.ts
CHANGED
package/src/export/projection.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isEqual } from 'es-toolkit/predicate'
|
|
2
2
|
import { fromUint8Array } from 'js-base64'
|
|
3
3
|
|
|
4
|
-
import type { SceneGraph, SceneNode, Stroke } from '@pairdraw/scene-graph'
|
|
4
|
+
import type { ColorProfile, SceneGraph, SceneNode, Stroke } from '@pairdraw/scene-graph'
|
|
5
5
|
import { BLACK } from '@pairdraw/scene-graph/constants'
|
|
6
6
|
import { readIcon } from '@pairdraw/scene-graph/icons'
|
|
7
7
|
import { imageMimeType } from '@pairdraw/scene-graph/images'
|
|
@@ -52,8 +52,33 @@ export interface SceneGraphToDesignOptions {
|
|
|
52
52
|
/** Layers whose shape only paths describe, which CSS boxes cannot draw. */
|
|
53
53
|
const VECTOR_SHAPES = new Set<SceneNode['type']>(['VECTOR', 'BOOLEAN_OPERATION', 'STAR', 'POLYGON'])
|
|
54
54
|
|
|
55
|
-
/** What a
|
|
56
|
-
const
|
|
55
|
+
/** What a vector's own SVG draws, so the placement wrapper leaves it out; a backdrop stays on the wrapper. */
|
|
56
|
+
const VECTOR_WRAPPER_PAINT = /^(background|border|box-shadow|outline|filter)/
|
|
57
|
+
|
|
58
|
+
/** Every CSS property that paints a layer, wherever its paint is written. */
|
|
59
|
+
export const PAINT_PROPERTY =
|
|
60
|
+
/^(background|backdrop-filter|border|box-shadow|outline|filter|color|fill|stroke|text-shadow)/
|
|
61
|
+
|
|
62
|
+
/** Whether a projected style paints; a radius or a placement property on its own does not. */
|
|
63
|
+
export function paintsSomething(style: DesignStyleDeclaration): boolean {
|
|
64
|
+
return Object.keys(style).some(
|
|
65
|
+
(property) => PAINT_PROPERTY.test(property) && !property.endsWith('radius')
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Whether a projected layer paints or writes text. A frame with no paint and no children projects
|
|
71
|
+
* to a bare element with only its size, so a selection of such layers draws nothing.
|
|
72
|
+
*/
|
|
73
|
+
export function designNodeDraws(node: DesignNode): boolean {
|
|
74
|
+
if (node.type === 'text') return node.text.trim().length > 0
|
|
75
|
+
if (node.shader?.frame) return true
|
|
76
|
+
if (node.tagName === 'svg' || node.tagName === 'img' || node.tagName === 'canvas') return true
|
|
77
|
+
return (
|
|
78
|
+
paintsSomething({ ...node.computedStyle, ...node.inlineStyle }) ||
|
|
79
|
+
node.children.some(designNodeDraws)
|
|
80
|
+
)
|
|
81
|
+
}
|
|
57
82
|
|
|
58
83
|
/** A field's CSS: its token reference where one is faithful, otherwise the literal value. */
|
|
59
84
|
type FieldCSS = (field: string, literal: string) => string
|
|
@@ -166,9 +191,14 @@ function addCornerRadii(style: DesignStyleDeclaration, node: SceneNode, css: Fie
|
|
|
166
191
|
if (node.cornerRadius > 0) style['border-radius'] = css('cornerRadius', px(node.cornerRadius))
|
|
167
192
|
}
|
|
168
193
|
|
|
169
|
-
function addStroke(
|
|
194
|
+
function addStroke(
|
|
195
|
+
style: DesignStyleDeclaration,
|
|
196
|
+
node: SceneNode,
|
|
197
|
+
css: FieldCSS,
|
|
198
|
+
profile: ColorProfile
|
|
199
|
+
): void {
|
|
170
200
|
const stroke = node.strokes.find((item) => item.visible)
|
|
171
|
-
const literalColor = strokeColorToCSS(stroke)
|
|
201
|
+
const literalColor = strokeColorToCSS(stroke, profile)
|
|
172
202
|
if (!literalColor || !stroke) return
|
|
173
203
|
const color = css('strokes/0/color', literalColor)
|
|
174
204
|
const borderStyle = node.dashPattern.length > 0 ? 'dashed' : 'solid'
|
|
@@ -193,14 +223,14 @@ function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS
|
|
|
193
223
|
* dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
|
|
194
224
|
* stroke order that alternates them is approximated.
|
|
195
225
|
*/
|
|
196
|
-
function extraStrokeShadows(strokes: Stroke[], first: Stroke): string[] {
|
|
226
|
+
function extraStrokeShadows(strokes: Stroke[], first: Stroke, profile: ColorProfile): string[] {
|
|
197
227
|
const outer: string[] = []
|
|
198
228
|
const inner: string[] = []
|
|
199
229
|
let outside = 0
|
|
200
230
|
let inside = first.weight
|
|
201
231
|
for (const stroke of strokes) {
|
|
202
232
|
if (!stroke.visible) continue
|
|
203
|
-
const color = strokeColorToCSS(stroke)
|
|
233
|
+
const color = strokeColorToCSS(stroke, profile)
|
|
204
234
|
if (!color) continue
|
|
205
235
|
if (stroke.align !== 'OUTSIDE') {
|
|
206
236
|
inside += stroke.align === 'CENTER' ? stroke.weight / 2 : stroke.weight
|
|
@@ -352,22 +382,23 @@ const CSS_BLEND_MODES: Partial<Record<SceneNode['blendMode'], string>> = {
|
|
|
352
382
|
function styleFromSceneNode(
|
|
353
383
|
node: SceneNode,
|
|
354
384
|
parent: SceneNode | undefined,
|
|
355
|
-
css: FieldCSS
|
|
385
|
+
css: FieldCSS,
|
|
386
|
+
profile: ColorProfile
|
|
356
387
|
): DesignStyleDeclaration {
|
|
357
388
|
const style: DesignStyleDeclaration = {}
|
|
358
389
|
addSize(style, node, parent, css)
|
|
359
390
|
addPositioning(style, node, parent)
|
|
360
391
|
addSizeConstraints(style, node, css)
|
|
361
|
-
const fill = fillToCSS(node.fills.at(0))
|
|
392
|
+
const fill = fillToCSS(node.fills.at(0), profile)
|
|
362
393
|
if (fill) style['background-color'] = css('fills/0/color', fill)
|
|
363
|
-
const gradients = gradientFillsToCSS(node.fills, node.width, node.height)
|
|
394
|
+
const gradients = gradientFillsToCSS(node.fills, node.width, node.height, profile)
|
|
364
395
|
if (gradients) style['background-image'] = gradients
|
|
365
396
|
addImageStyle(style, node)
|
|
366
|
-
addStroke(style, node, css)
|
|
367
|
-
Object.assign(style, effectsToCSS(node.effects))
|
|
397
|
+
addStroke(style, node, css, profile)
|
|
398
|
+
Object.assign(style, effectsToCSS(node.effects, profile))
|
|
368
399
|
const visibleStrokes = node.strokes.filter((stroke) => stroke.visible)
|
|
369
400
|
if (visibleStrokes.length > 1) {
|
|
370
|
-
const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0])
|
|
401
|
+
const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0], profile)
|
|
371
402
|
if (rings.length > 0) {
|
|
372
403
|
style['box-shadow'] = [rings.join(', '), style['box-shadow']]
|
|
373
404
|
.filter((shadow): shadow is string => Boolean(shadow))
|
|
@@ -408,15 +439,16 @@ function styleFromSceneNode(
|
|
|
408
439
|
function styleFromTextNode(
|
|
409
440
|
node: SceneNode,
|
|
410
441
|
parent: SceneNode | undefined,
|
|
411
|
-
css: FieldCSS
|
|
442
|
+
css: FieldCSS,
|
|
443
|
+
profile: ColorProfile
|
|
412
444
|
): DesignStyleDeclaration {
|
|
413
445
|
const style: DesignStyleDeclaration = {}
|
|
414
446
|
addSize(style, node, parent, css)
|
|
415
447
|
addPositioning(style, node, parent)
|
|
416
448
|
addLayoutChild(style, node, parent)
|
|
417
449
|
if (resolveNodeTextDirection(node) === 'RTL') style.direction = 'rtl'
|
|
418
|
-
const color = fillToCSS(node.fills.at(0))
|
|
419
|
-
style.color = color ? css('fills/0/color', color) : cssColor(BLACK)
|
|
450
|
+
const color = fillToCSS(node.fills.at(0), profile)
|
|
451
|
+
style.color = color ? css('fills/0/color', color) : cssColor(BLACK, profile)
|
|
420
452
|
style['font-family'] = node.fontFamily
|
|
421
453
|
style['font-size'] = css('fontSize', px(node.fontSize))
|
|
422
454
|
style['font-weight'] = String(node.fontWeight)
|
|
@@ -435,7 +467,7 @@ function styleFromTextNode(
|
|
|
435
467
|
if (node.textAlignHorizontal !== 'LEFT')
|
|
436
468
|
style['text-align'] = node.textAlignHorizontal.toLowerCase()
|
|
437
469
|
if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
|
|
438
|
-
Object.assign(style, textEffectsToCSS(node.effects))
|
|
470
|
+
Object.assign(style, textEffectsToCSS(node.effects, profile))
|
|
439
471
|
if (node.textDecoration !== 'NONE') {
|
|
440
472
|
style['text-decoration-line'] =
|
|
441
473
|
node.textDecoration === 'UNDERLINE' ? 'underline' : 'line-through'
|
|
@@ -508,7 +540,7 @@ function vectorDesignNode(
|
|
|
508
540
|
const drawn = context.vectorElement?.(context.graph, node)
|
|
509
541
|
if (!drawn) return null
|
|
510
542
|
const placement = Object.fromEntries(
|
|
511
|
-
Object.entries(style).filter(([property]) => !
|
|
543
|
+
Object.entries(style).filter(([property]) => !VECTOR_WRAPPER_PAINT.test(property))
|
|
512
544
|
)
|
|
513
545
|
return {
|
|
514
546
|
...drawn,
|
|
@@ -587,7 +619,11 @@ function sceneNodeToDesignNode(
|
|
|
587
619
|
const css: FieldCSS = (field, literal) => tokens?.reference(node, field, modes) ?? literal
|
|
588
620
|
|
|
589
621
|
if (node.type === 'TEXT') {
|
|
590
|
-
const content = textElementContent(
|
|
622
|
+
const content = textElementContent(
|
|
623
|
+
node,
|
|
624
|
+
styleFromTextNode(node, parent, css, graph.documentColorSpace),
|
|
625
|
+
graph.documentColorSpace
|
|
626
|
+
)
|
|
591
627
|
return {
|
|
592
628
|
type: 'element',
|
|
593
629
|
tagName: content.tagName,
|
|
@@ -601,7 +637,12 @@ function sceneNodeToDesignNode(
|
|
|
601
637
|
|
|
602
638
|
// An icon is one drawing, however many paths it has.
|
|
603
639
|
if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
|
|
604
|
-
const vector = vectorDesignNode(
|
|
640
|
+
const vector = vectorDesignNode(
|
|
641
|
+
context,
|
|
642
|
+
node,
|
|
643
|
+
attrs,
|
|
644
|
+
styleFromSceneNode(node, parent, css, graph.documentColorSpace)
|
|
645
|
+
)
|
|
605
646
|
if (vector) return vector
|
|
606
647
|
}
|
|
607
648
|
|
|
@@ -618,7 +659,7 @@ function sceneNodeToDesignNode(
|
|
|
618
659
|
}
|
|
619
660
|
}
|
|
620
661
|
|
|
621
|
-
const style = styleFromSceneNode(node, parent, css)
|
|
662
|
+
const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace)
|
|
622
663
|
const shader = layerShader(graph, node)
|
|
623
664
|
if (shader) addShaderLayerStyle(style)
|
|
624
665
|
return {
|
package/src/export/text.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { isEqual } from 'es-toolkit/predicate'
|
|
|
3
3
|
|
|
4
4
|
import type {
|
|
5
5
|
CharacterStyleOverride,
|
|
6
|
+
ColorProfile,
|
|
6
7
|
HyperlinkTarget,
|
|
7
8
|
SceneNode,
|
|
8
9
|
TextLength
|
|
@@ -157,7 +158,7 @@ function styledRanges(node: SceneNode): StyledRange[] {
|
|
|
157
158
|
}
|
|
158
159
|
|
|
159
160
|
/** Underline or strikethrough as CSS writes it: Figma's offset is CSS's, from the baseline. */
|
|
160
|
-
function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
|
|
161
|
+
function decorationCSS(style: CharacterStyle, profile: ColorProfile): DesignStyleDeclaration {
|
|
161
162
|
if (style.textDecoration === 'NONE') return {}
|
|
162
163
|
const underline = style.textDecoration === 'UNDERLINE'
|
|
163
164
|
const css: DesignStyleDeclaration = {
|
|
@@ -166,7 +167,10 @@ function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
|
|
|
166
167
|
if (style.textDecorationStyle !== 'SOLID') {
|
|
167
168
|
css['text-decoration-style'] = style.textDecorationStyle.toLowerCase()
|
|
168
169
|
}
|
|
169
|
-
const color = fillToCSS(
|
|
170
|
+
const color = fillToCSS(
|
|
171
|
+
style.textDecorationFills.find((fill) => fill.visible),
|
|
172
|
+
profile
|
|
173
|
+
)
|
|
170
174
|
if (color) css['text-decoration-color'] = color
|
|
171
175
|
if (style.textDecorationThickness !== null) {
|
|
172
176
|
css['text-decoration-thickness'] = px(style.textDecorationThickness)
|
|
@@ -178,9 +182,13 @@ function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
|
|
|
178
182
|
return css
|
|
179
183
|
}
|
|
180
184
|
|
|
181
|
-
function characterCSS(
|
|
185
|
+
function characterCSS(
|
|
186
|
+
style: CharacterStyle,
|
|
187
|
+
decorated: boolean,
|
|
188
|
+
profile: ColorProfile
|
|
189
|
+
): DesignStyleDeclaration {
|
|
182
190
|
return {
|
|
183
|
-
color: fillToCSS(style.fills.at(0)) ?? '',
|
|
191
|
+
color: fillToCSS(style.fills.at(0), profile) ?? '',
|
|
184
192
|
'font-family': style.fontFamily,
|
|
185
193
|
'font-size': px(style.fontSize),
|
|
186
194
|
'font-weight': String(style.fontWeight),
|
|
@@ -188,7 +196,7 @@ function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDec
|
|
|
188
196
|
'letter-spacing': letterSpacingToCSS(style.letterSpacing),
|
|
189
197
|
'line-height': style.lineHeight === null ? 'normal' : lineHeightToCSS(style.lineHeight),
|
|
190
198
|
'text-transform': textCaseToCSS(style.textCase) ?? 'none',
|
|
191
|
-
...(decorated ? decorationCSS(style) : {})
|
|
199
|
+
...(decorated ? decorationCSS(style, profile) : {})
|
|
192
200
|
}
|
|
193
201
|
}
|
|
194
202
|
|
|
@@ -200,9 +208,10 @@ function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDec
|
|
|
200
208
|
function spanCSS(
|
|
201
209
|
style: CharacterStyle,
|
|
202
210
|
decorated: boolean,
|
|
203
|
-
base: DesignStyleDeclaration
|
|
211
|
+
base: DesignStyleDeclaration,
|
|
212
|
+
profile: ColorProfile
|
|
204
213
|
): DesignStyleDeclaration {
|
|
205
|
-
const own = characterCSS(style, decorated)
|
|
214
|
+
const own = characterCSS(style, decorated, profile)
|
|
206
215
|
const css = differing(own, base)
|
|
207
216
|
const { unit, value } = style.letterSpacing
|
|
208
217
|
if (unit === 'PERCENT' && value !== 0 && 'font-size' in css) {
|
|
@@ -225,10 +234,14 @@ function differing(
|
|
|
225
234
|
* Whether the text decorates every character alike. CSS draws an element's decoration through
|
|
226
235
|
* its descendants, which cannot take it off, so mixed decorations go on the spans instead.
|
|
227
236
|
*/
|
|
228
|
-
function uniformDecoration(
|
|
237
|
+
function uniformDecoration(
|
|
238
|
+
node: SceneNode,
|
|
239
|
+
ranges: readonly StyledRange[],
|
|
240
|
+
profile: ColorProfile
|
|
241
|
+
): boolean {
|
|
229
242
|
const decorations = ranges
|
|
230
243
|
.filter((range) => node.text.slice(range.start, range.end).replaceAll('\n', '') !== '')
|
|
231
|
-
.map((range) => decorationCSS(range.style))
|
|
244
|
+
.map((range) => decorationCSS(range.style, profile))
|
|
232
245
|
return decorations.every((decoration) => isEqual(decoration, decorations[0]))
|
|
233
246
|
}
|
|
234
247
|
|
|
@@ -237,6 +250,7 @@ interface TextProjection {
|
|
|
237
250
|
ranges: StyledRange[]
|
|
238
251
|
base: DesignStyleDeclaration
|
|
239
252
|
decorated: boolean
|
|
253
|
+
profile: ColorProfile
|
|
240
254
|
}
|
|
241
255
|
|
|
242
256
|
function textNode(text: string): DesignNode {
|
|
@@ -265,13 +279,13 @@ function linkElement(href: string, css: DesignStyleDeclaration, text: string): D
|
|
|
265
279
|
* linked characters are a link instead.
|
|
266
280
|
*/
|
|
267
281
|
function inlineContent(projection: TextProjection, start: number, end: number): DesignNode[] {
|
|
268
|
-
const { node, ranges, base, decorated } = projection
|
|
282
|
+
const { node, ranges, base, decorated, profile } = projection
|
|
269
283
|
const pieces: { css: DesignStyleDeclaration; text: string; href: string | null }[] = []
|
|
270
284
|
for (const range of ranges) {
|
|
271
285
|
const from = Math.max(start, range.start)
|
|
272
286
|
const to = Math.min(end, range.end)
|
|
273
287
|
if (to <= from) continue
|
|
274
|
-
const css = spanCSS(range.style, !decorated, base)
|
|
288
|
+
const css = spanCSS(range.style, !decorated, base, profile)
|
|
275
289
|
const text = node.text.slice(from, to)
|
|
276
290
|
const previous = pieces.at(-1)
|
|
277
291
|
if (previous && previous.href === range.href && isEqual(previous.css, css)) {
|
|
@@ -443,19 +457,21 @@ function groupEm(
|
|
|
443
457
|
*/
|
|
444
458
|
export function textElementContent(
|
|
445
459
|
node: SceneNode,
|
|
446
|
-
layerStyle: DesignStyleDeclaration
|
|
460
|
+
layerStyle: DesignStyleDeclaration,
|
|
461
|
+
profile: ColorProfile = 'srgb'
|
|
447
462
|
): { tagName: string; style: DesignStyleDeclaration; children: DesignNode[] } {
|
|
448
463
|
const ranges = styledRanges(node)
|
|
449
|
-
const decorated = uniformDecoration(node, ranges)
|
|
464
|
+
const decorated = uniformDecoration(node, ranges, profile)
|
|
450
465
|
const style = Object.fromEntries(
|
|
451
466
|
Object.entries(layerStyle).filter(([property]) => !DECORATION_PROPERTIES.has(property))
|
|
452
467
|
)
|
|
453
|
-
if (decorated) Object.assign(style, decorationCSS(characterStyle(node)))
|
|
468
|
+
if (decorated) Object.assign(style, decorationCSS(characterStyle(node), profile))
|
|
454
469
|
const projection: TextProjection = {
|
|
455
470
|
node,
|
|
456
471
|
ranges,
|
|
457
|
-
base: characterCSS(characterStyle(node), decorated),
|
|
458
|
-
decorated
|
|
472
|
+
base: characterCSS(characterStyle(node), decorated, profile),
|
|
473
|
+
decorated,
|
|
474
|
+
profile
|
|
459
475
|
}
|
|
460
476
|
if (!needsBlocks(node)) {
|
|
461
477
|
return { tagName: 'span', style, children: inlineContent(projection, 0, node.text.length) }
|