@pairdraw/dom-css 0.0.0-stage → 0.16.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/LICENSE +21 -0
- package/README.md +219 -2
- package/dist/browser.d.ts +29 -0
- package/dist/browser.js +2 -0
- package/dist/chunks/behaviours/args.js +59 -0
- package/dist/chunks/behaviours/args.js.map +1 -0
- package/dist/chunks/behaviours/controls.js +109 -0
- package/dist/chunks/behaviours/controls.js.map +1 -0
- package/dist/chunks/behaviours/reset.js +40 -0
- package/dist/chunks/behaviours/reset.js.map +1 -0
- package/dist/chunks/behaviours/roles.js +51 -0
- package/dist/chunks/behaviours/roles.js.map +1 -0
- package/dist/chunks/behaviours/states/conditions.js +103 -0
- package/dist/chunks/behaviours/states/conditions.js.map +1 -0
- package/dist/chunks/behaviours/states/css.js +172 -0
- package/dist/chunks/behaviours/states/css.js.map +1 -0
- package/dist/chunks/behaviours/states/layers.js +1736 -0
- package/dist/chunks/behaviours/states/layers.js.map +1 -0
- package/dist/chunks/behaviours/states/model.js +144 -0
- package/dist/chunks/behaviours/states/model.js.map +1 -0
- package/dist/chunks/behaviours/states/tailwind.js +198 -0
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
- package/dist/chunks/browser.js +1352 -0
- package/dist/chunks/browser.js.map +1 -0
- package/dist/chunks/export/bundle.js +337 -0
- package/dist/chunks/export/bundle.js.map +1 -0
- package/dist/chunks/export/components/drawn.js +60 -0
- package/dist/chunks/export/components/drawn.js.map +1 -0
- package/dist/chunks/export/components/fields.js +178 -0
- package/dist/chunks/export/components/fields.js.map +1 -0
- package/dist/chunks/export/components/groups.js +297 -0
- package/dist/chunks/export/components/groups.js.map +1 -0
- package/dist/chunks/export/components/icons.js +260 -0
- package/dist/chunks/export/components/icons.js.map +1 -0
- package/dist/chunks/export/components/layers.js +1594 -0
- package/dist/chunks/export/components/layers.js.map +1 -0
- package/dist/chunks/export/index.js +477 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/export/storybook/component.js +448 -0
- package/dist/chunks/export/storybook/component.js.map +1 -0
- package/dist/chunks/export/storybook/export.js +410 -0
- package/dist/chunks/export/storybook/export.js.map +1 -0
- package/dist/chunks/import/html.js +406 -0
- package/dist/chunks/import/html.js.map +1 -0
- package/dist/chunks/import/jsx/dev-runtime.js +37 -0
- package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
- package/dist/chunks/tokens/cssom-validator.js +10 -0
- package/dist/chunks/tokens/cssom-validator.js.map +1 -0
- package/dist/export.d.ts +594 -0
- package/dist/export.js +13 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +7 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +20 -0
- package/dist/jsx-runtime.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/scene-graph.d.ts +2 -0
- package/dist/scene-graph.js +2 -0
- package/dist/scene-graph2.d.ts +10 -0
- package/dist/styling.d.ts +93 -0
- package/dist/tailwind.d.ts +39 -0
- package/dist/types.d.ts +89 -0
- package/package.json +73 -4
- package/src/behaviours/args.ts +85 -0
- package/src/behaviours/controls.ts +195 -0
- package/src/behaviours/index.ts +11 -0
- package/src/behaviours/reset.ts +37 -0
- package/src/behaviours/roles.ts +44 -0
- package/src/behaviours/states/conditions.ts +105 -0
- package/src/behaviours/states/css.ts +123 -0
- package/src/behaviours/states/index.ts +4 -0
- package/src/behaviours/states/layers.ts +205 -0
- package/src/behaviours/states/model.ts +175 -0
- package/src/behaviours/states/names.ts +39 -0
- package/src/behaviours/states/tailwind.ts +148 -0
- package/src/behaviours/states/types.ts +56 -0
- package/src/browser.ts +146 -0
- package/src/cssom.d.ts +38 -0
- package/src/export/bundle.ts +461 -0
- package/src/export/components/drawn.ts +73 -0
- package/src/export/components/fields.ts +178 -0
- package/src/export/components/groups.ts +78 -0
- package/src/export/components/icons.ts +230 -0
- package/src/export/components/layers.ts +121 -0
- package/src/export/components/model.ts +544 -0
- package/src/export/components/properties.ts +167 -0
- package/src/export/components/react/fields.ts +129 -0
- package/src/export/components/react/index.ts +606 -0
- package/src/export/components/react/shared.ts +76 -0
- package/src/export/components/references.ts +212 -0
- package/src/export/components/repeats.ts +128 -0
- package/src/export/components/shaders.ts +125 -0
- package/src/export/components/styling.ts +31 -0
- package/src/export/components/tree.ts +72 -0
- package/src/export/components/vue.ts +517 -0
- package/src/export/css.ts +57 -0
- package/src/export/grid.ts +42 -0
- package/src/export/html.ts +163 -0
- package/src/export/index.ts +44 -0
- package/src/export/projection.ts +581 -0
- package/src/export/storybook/component.ts +334 -0
- package/src/export/storybook/export.ts +430 -0
- package/src/export/storybook/groups.ts +161 -0
- package/src/export/storybook/module.ts +319 -0
- package/src/export/storybook/names.ts +51 -0
- package/src/export/tailwind-jsx.ts +156 -0
- package/src/export/text.ts +415 -0
- package/src/import/css-text.ts +4 -0
- package/src/import/css-values.ts +31 -0
- package/src/import/flow.ts +248 -0
- package/src/import/html.ts +88 -0
- package/src/import/jsx/core.ts +130 -0
- package/src/import/jsx/dev-runtime.ts +8 -0
- package/src/import/jsx/runtime.ts +76 -0
- package/src/import/scene-graph.ts +582 -0
- package/src/import/style-attribute.ts +25 -0
- package/src/import/tailwind.ts +54 -0
- package/src/import/text.ts +518 -0
- package/src/index.ts +51 -0
- package/src/runtime/browser/runtime.ts +301 -0
- package/src/runtime/headless/runtime.ts +99 -0
- package/src/runtime/headless/styles.ts +330 -0
- package/src/runtime/index.ts +11 -0
- package/src/tokens/conditions.ts +66 -0
- package/src/tokens/cssom-validator.ts +8 -0
- package/src/tokens/custom-properties.ts +331 -0
- package/src/tokens/expression.ts +128 -0
- package/src/tokens/formula.ts +140 -0
- package/src/tokens/index.ts +43 -0
- package/src/tokens/names.ts +161 -0
- package/src/tokens/references.ts +201 -0
- package/src/tokens/stylesheet.ts +304 -0
- package/src/tokens/validate.ts +86 -0
- package/src/tokens/values.ts +44 -0
- package/src/types.ts +59 -0
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import { fromUint8Array } from 'js-base64'
|
|
2
|
+
|
|
3
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
4
|
+
import { BLACK } from '@pairdraw/scene-graph/constants'
|
|
5
|
+
import { readIcon } from '@pairdraw/scene-graph/icons'
|
|
6
|
+
import { imageMimeType } from '@pairdraw/scene-graph/images'
|
|
7
|
+
import { layoutSizingInParent } from '@pairdraw/scene-graph/layout'
|
|
8
|
+
import { isShaderPresetPaint, shaderOfPaint } from '@pairdraw/scene-graph/shaders'
|
|
9
|
+
import { resolveNodeTextDirection } from '@pairdraw/scene-graph/text/direction'
|
|
10
|
+
|
|
11
|
+
import { DesignTokens, type CSSModes } from '../tokens/references'
|
|
12
|
+
import type { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
|
|
13
|
+
import {
|
|
14
|
+
cssColor,
|
|
15
|
+
dropShadowToCSS,
|
|
16
|
+
effectsToCSS,
|
|
17
|
+
fillToCSS,
|
|
18
|
+
letterSpacingToCSS,
|
|
19
|
+
lineHeightToCSS,
|
|
20
|
+
strokeColorToCSS
|
|
21
|
+
} from './css'
|
|
22
|
+
import { addGridContainer, addGridPlacement } from './grid'
|
|
23
|
+
import { textCaseToCSS, textElementContent } from './text'
|
|
24
|
+
|
|
25
|
+
const DOM_CSS_PLUGIN_ID = 'pairdraw-dom-css'
|
|
26
|
+
const IMAGE_SOURCE_URL_KEY = 'image-source-url'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Draws a vector-shaped layer as an inline `<svg>` element sized to the layer, from the
|
|
30
|
+
* engine's own SVG export. dom-css cannot depend on that engine, so it comes in as an option.
|
|
31
|
+
*/
|
|
32
|
+
export type VectorElementRenderer = (graph: SceneGraph, node: SceneNode) => DesignElement | null
|
|
33
|
+
|
|
34
|
+
export interface SceneGraphToDesignOptions {
|
|
35
|
+
rootId?: string
|
|
36
|
+
/** Write each layer's ID as `data-pairdraw-node-id`. Default true. */
|
|
37
|
+
includeSourceIds?: boolean
|
|
38
|
+
/** Write each layer's own name as `data-name`, as the Tailwind JSX export does. Default false. */
|
|
39
|
+
includeLayerNames?: boolean
|
|
40
|
+
/** Write variable-bound values as `var(--name)` where CSS resolves them as drawn. Default true. */
|
|
41
|
+
tokens?: boolean
|
|
42
|
+
/** Vector, boolean, star, polygon, and icon layers as inline SVG; without it they project as boxes. */
|
|
43
|
+
vectorElement?: VectorElementRenderer
|
|
44
|
+
/** Hidden layers to project as shown, such as the tab panels a component reveals itself. */
|
|
45
|
+
shown?: ReadonlySet<string>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Layers whose shape only paths describe, which CSS boxes cannot draw. */
|
|
49
|
+
const VECTOR_SHAPES = new Set<SceneNode['type']>(['VECTOR', 'BOOLEAN_OPERATION', 'STAR', 'POLYGON'])
|
|
50
|
+
|
|
51
|
+
/** What a box draws itself, which the SVG of a vector layer draws instead. */
|
|
52
|
+
const BOX_PAINT = /^(background|border|box-shadow|outline|filter)/
|
|
53
|
+
|
|
54
|
+
/** A field's CSS: its token reference where one is faithful, otherwise the literal value. */
|
|
55
|
+
type FieldCSS = (field: string, literal: string) => string
|
|
56
|
+
|
|
57
|
+
const px = (value: number) => `${value}px`
|
|
58
|
+
|
|
59
|
+
function nodeChildren(graph: SceneGraph, node: SceneNode): SceneNode[] {
|
|
60
|
+
return node.childIds
|
|
61
|
+
.map((id) => graph.getNode(id))
|
|
62
|
+
.filter((child): child is SceneNode => child !== undefined)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function justifyContentToCSS(value: SceneNode['primaryAxisAlign']): string | undefined {
|
|
66
|
+
if (value === 'CENTER') return 'center'
|
|
67
|
+
if (value === 'MAX') return 'flex-end'
|
|
68
|
+
if (value === 'SPACE_BETWEEN') return 'space-between'
|
|
69
|
+
if (value === 'SPACE_AROUND') return 'space-around'
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Cross-axis alignment of a container's children, or of one child. Auto layout starts children
|
|
75
|
+
* at the start while flexbox stretches them, so start is written out: a child that hugs its
|
|
76
|
+
* content keeps its own size.
|
|
77
|
+
*/
|
|
78
|
+
function alignToCSS(
|
|
79
|
+
value: SceneNode['counterAxisAlign'] | SceneNode['layoutAlignSelf']
|
|
80
|
+
): string | undefined {
|
|
81
|
+
if (value === 'MIN') return 'flex-start'
|
|
82
|
+
if (value === 'CENTER') return 'center'
|
|
83
|
+
if (value === 'MAX') return 'flex-end'
|
|
84
|
+
if (value === 'STRETCH') return 'stretch'
|
|
85
|
+
if (value === 'BASELINE') return 'baseline'
|
|
86
|
+
return undefined
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Whether a layer sizes itself to its content along an axis, so CSS should too. */
|
|
90
|
+
function hugs(node: SceneNode, axis: 'width' | 'height'): boolean {
|
|
91
|
+
if (node.type === 'TEXT')
|
|
92
|
+
return (
|
|
93
|
+
node.textAutoResize === 'WIDTH_AND_HEIGHT' ||
|
|
94
|
+
(axis === 'height' && node.textAutoResize === 'HEIGHT')
|
|
95
|
+
)
|
|
96
|
+
if (node.layoutMode !== 'HORIZONTAL' && node.layoutMode !== 'VERTICAL') return false
|
|
97
|
+
const primary = (node.layoutMode === 'HORIZONTAL') === (axis === 'width')
|
|
98
|
+
return (primary ? node.primaryAxisSizing : node.counterAxisSizing) === 'HUG'
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A layer's size: fixed on the axes the design fixes, and left to the content where the layer
|
|
103
|
+
* hugs it (an auto layout frame set to Hug, or auto-sizing text), so the page grows with it.
|
|
104
|
+
*/
|
|
105
|
+
function addSize(
|
|
106
|
+
style: DesignStyleDeclaration,
|
|
107
|
+
node: SceneNode,
|
|
108
|
+
parent: SceneNode | undefined,
|
|
109
|
+
css: FieldCSS
|
|
110
|
+
): void {
|
|
111
|
+
const sized = (axis: 'width' | 'height') =>
|
|
112
|
+
node[axis] > 0 && !hugs(node, axis) && !stretches(node, parent, axis)
|
|
113
|
+
if (sized('width')) style.width = css('width', px(node.width))
|
|
114
|
+
if (sized('height')) style.height = css('height', px(node.height))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Whether a layer fills an axis by stretching, which a fixed CSS size would prevent: across a
|
|
119
|
+
* flex parent, or either axis of a grid cell. Along a flex parent, fill is `flex-grow`, and the
|
|
120
|
+
* size stays as its basis.
|
|
121
|
+
*/
|
|
122
|
+
function stretches(
|
|
123
|
+
node: SceneNode,
|
|
124
|
+
parent: SceneNode | undefined,
|
|
125
|
+
axis: 'width' | 'height'
|
|
126
|
+
): boolean {
|
|
127
|
+
const sizing = layoutSizingInParent(parent, node, axis === 'width' ? 'HORIZONTAL' : 'VERTICAL')
|
|
128
|
+
if (sizing !== 'FILL' || !parent) return false
|
|
129
|
+
if (parent.layoutMode === 'GRID') return true
|
|
130
|
+
const mainAxis = parent.layoutMode === 'HORIZONTAL' ? 'width' : 'height'
|
|
131
|
+
return axis !== mainAxis
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Place a layer at its coordinates when its parent does not lay it out: it is absolutely
|
|
136
|
+
* positioned, or its parent frame has no auto layout. A frame without auto layout becomes the
|
|
137
|
+
* containing block of the children it places.
|
|
138
|
+
*/
|
|
139
|
+
function addPositioning(
|
|
140
|
+
style: DesignStyleDeclaration,
|
|
141
|
+
node: SceneNode,
|
|
142
|
+
parent: SceneNode | undefined
|
|
143
|
+
): void {
|
|
144
|
+
const placed =
|
|
145
|
+
node.layoutPositioning === 'ABSOLUTE' ||
|
|
146
|
+
(parent !== undefined && parent.type !== 'CANVAS' && parent.layoutMode === 'NONE')
|
|
147
|
+
if (placed) {
|
|
148
|
+
style.position = 'absolute'
|
|
149
|
+
style.left = `${node.x}px`
|
|
150
|
+
style.top = `${node.y}px`
|
|
151
|
+
} else if (node.layoutMode === 'NONE' && node.type !== 'TEXT' && node.childIds.length > 0) {
|
|
152
|
+
style.position = 'relative'
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function addSizeConstraints(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
|
|
157
|
+
if (node.minWidth !== null) style['min-width'] = css('minWidth', px(node.minWidth))
|
|
158
|
+
if (node.maxWidth !== null) style['max-width'] = css('maxWidth', px(node.maxWidth))
|
|
159
|
+
if (node.minHeight !== null) style['min-height'] = css('minHeight', px(node.minHeight))
|
|
160
|
+
if (node.maxHeight !== null) style['max-height'] = css('maxHeight', px(node.maxHeight))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function addCornerRadii(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
|
|
164
|
+
if (node.type === 'ELLIPSE') {
|
|
165
|
+
style['border-radius'] = '50%'
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
if (node.independentCorners) {
|
|
169
|
+
const corners = [
|
|
170
|
+
['border-top-left-radius', 'topLeftRadius', node.topLeftRadius],
|
|
171
|
+
['border-top-right-radius', 'topRightRadius', node.topRightRadius],
|
|
172
|
+
['border-bottom-right-radius', 'bottomRightRadius', node.bottomRightRadius],
|
|
173
|
+
['border-bottom-left-radius', 'bottomLeftRadius', node.bottomLeftRadius]
|
|
174
|
+
] as const
|
|
175
|
+
for (const [property, field, radius] of corners)
|
|
176
|
+
if (radius > 0) style[property] = css(field, px(radius))
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (node.cornerRadius > 0) style['border-radius'] = css('cornerRadius', px(node.cornerRadius))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
|
|
184
|
+
const stroke = node.strokes.at(0)
|
|
185
|
+
const literalColor = strokeColorToCSS(stroke)
|
|
186
|
+
if (!literalColor || !stroke) return
|
|
187
|
+
const color = css('strokes/0/color', literalColor)
|
|
188
|
+
const borderStyle = node.dashPattern.length > 0 ? 'dashed' : 'solid'
|
|
189
|
+
if (!node.independentStrokeWeights) {
|
|
190
|
+
style.border = `${css('strokeWeight', px(stroke.weight))} solid ${color}`
|
|
191
|
+
if (borderStyle !== 'solid') style['border-style'] = borderStyle
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
style['border-style'] = borderStyle
|
|
196
|
+
style['border-color'] = color
|
|
197
|
+
style['border-top-width'] = css('borderTopWeight', px(node.borderTopWeight))
|
|
198
|
+
style['border-right-width'] = css('borderRightWeight', px(node.borderRightWeight))
|
|
199
|
+
style['border-bottom-width'] = css('borderBottomWeight', px(node.borderBottomWeight))
|
|
200
|
+
style['border-left-width'] = css('borderLeftWeight', px(node.borderLeftWeight))
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const PADDING_FIELDS = ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'] as const
|
|
204
|
+
|
|
205
|
+
/** Sides compare by their CSS, so a token and a literal of the same size stay separate. */
|
|
206
|
+
function addPadding(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
|
|
207
|
+
const [top, right, bottom, left] = PADDING_FIELDS.map((field) => css(field, px(node[field])))
|
|
208
|
+
const zero = px(0)
|
|
209
|
+
if ([top, right, bottom, left].every((value) => value === zero)) return
|
|
210
|
+
|
|
211
|
+
if (top === right && right === bottom && bottom === left) {
|
|
212
|
+
style.padding = top
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (top === bottom && right === left) {
|
|
217
|
+
if (top !== zero) style['padding-block'] = top
|
|
218
|
+
if (right !== zero) style['padding-inline'] = right
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (top !== zero) style['padding-top'] = top
|
|
223
|
+
if (right !== zero) style['padding-right'] = right
|
|
224
|
+
if (bottom !== zero) style['padding-bottom'] = bottom
|
|
225
|
+
if (left !== zero) style['padding-left'] = left
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function addFlexGap(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
|
|
229
|
+
if (node.itemSpacing <= 0 && node.counterAxisSpacing <= 0) return
|
|
230
|
+
const item = css('itemSpacing', px(node.itemSpacing))
|
|
231
|
+
if (node.counterAxisSpacing <= 0) {
|
|
232
|
+
style.gap = item
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const counter = css('counterAxisSpacing', px(node.counterAxisSpacing))
|
|
237
|
+
if (node.layoutMode === 'HORIZONTAL') {
|
|
238
|
+
if (node.itemSpacing > 0) style['column-gap'] = item
|
|
239
|
+
style['row-gap'] = counter
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (node.itemSpacing > 0) style['row-gap'] = item
|
|
244
|
+
style['column-gap'] = counter
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Image bytes as a data URL of the type they are encoded in. */
|
|
248
|
+
function imageDataURL(bytes: Uint8Array): string {
|
|
249
|
+
return `data:${imageMimeType(bytes)};base64,${fromUint8Array(bytes)}`
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The shader a layer's fill draws, which the projection keeps as the shader rather than the
|
|
254
|
+
* image of its still frame: the shader of its first fill, as the projection reads one fill.
|
|
255
|
+
*/
|
|
256
|
+
function layerShader(graph: SceneGraph, node: SceneNode): DesignElement['shader'] {
|
|
257
|
+
const fill = node.fills.at(0)
|
|
258
|
+
const shader = fill?.visible ? shaderOfPaint(node, fill) : null
|
|
259
|
+
if (!shader || !isShaderPresetPaint(shader)) return undefined
|
|
260
|
+
const bytes = graph.images.get(shader.image)
|
|
261
|
+
const frame = bytes ? imageDataURL(bytes) : undefined
|
|
262
|
+
return { preset: shader.preset, frame }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const POSITIONED = new Set(['relative', 'absolute', 'fixed', 'sticky'])
|
|
266
|
+
|
|
267
|
+
/** Whether a `position` value makes an element the containing block of what it places. */
|
|
268
|
+
export function isPositioned(position: unknown): boolean {
|
|
269
|
+
return typeof position === 'string' && POSITIONED.has(position)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A shader layer contains what plays behind its content, beneath everything it holds. */
|
|
273
|
+
function addShaderLayerStyle(style: DesignStyleDeclaration): void {
|
|
274
|
+
if (!isPositioned(style.position)) style.position = 'relative'
|
|
275
|
+
style.isolation = 'isolate'
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function addImageStyle(style: DesignStyleDeclaration, node: SceneNode): void {
|
|
279
|
+
const fill = node.fills.at(0)
|
|
280
|
+
if (fill?.type !== 'IMAGE' || !fill.visible || shaderOfPaint(node, fill)) return
|
|
281
|
+
if (node.width > 0 && node.height > 0) style['aspect-ratio'] = `${node.width} / ${node.height}`
|
|
282
|
+
if (fill.imageScaleMode === 'FIT') style['object-fit'] = 'contain'
|
|
283
|
+
if (fill.imageScaleMode === 'FILL') style['object-fit'] = 'cover'
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Styles a node takes from its parent's layout, and its own rotation. */
|
|
287
|
+
function addLayoutChild(
|
|
288
|
+
style: DesignStyleDeclaration,
|
|
289
|
+
node: SceneNode,
|
|
290
|
+
parent: SceneNode | undefined
|
|
291
|
+
): void {
|
|
292
|
+
if (parent?.layoutMode === 'GRID') addGridPlacement(style, node)
|
|
293
|
+
else if (parent && parent.layoutMode !== 'NONE' && node.layoutGrow > 0) style['flex-grow'] = '1'
|
|
294
|
+
if (node.rotation !== 0) style.transform = `rotate(${node.rotation}deg)`
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const SIZE_PROPERTIES = ['width', 'height', 'min-width', 'max-width', 'min-height', 'max-height']
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* A layer's size includes its padding and stroke, as auto layout measures it. CSS sizes the
|
|
301
|
+
* content box unless told otherwise, so a sized box with padding or a border says so.
|
|
302
|
+
*/
|
|
303
|
+
function addBoxSizing(style: DesignStyleDeclaration): void {
|
|
304
|
+
const sized = SIZE_PROPERTIES.some((property) => Object.hasOwn(style, property))
|
|
305
|
+
const boxed = Object.keys(style).some(
|
|
306
|
+
(property) =>
|
|
307
|
+
property.startsWith('padding') ||
|
|
308
|
+
(property.startsWith('border') && !property.endsWith('radius'))
|
|
309
|
+
)
|
|
310
|
+
if (sized && boxed) style['box-sizing'] = 'border-box'
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function styleFromSceneNode(
|
|
314
|
+
node: SceneNode,
|
|
315
|
+
parent: SceneNode | undefined,
|
|
316
|
+
css: FieldCSS
|
|
317
|
+
): DesignStyleDeclaration {
|
|
318
|
+
const style: DesignStyleDeclaration = {}
|
|
319
|
+
addSize(style, node, parent, css)
|
|
320
|
+
addPositioning(style, node, parent)
|
|
321
|
+
addSizeConstraints(style, node, css)
|
|
322
|
+
const fill = fillToCSS(node.fills.at(0))
|
|
323
|
+
if (fill) style['background-color'] = css('fills/0/color', fill)
|
|
324
|
+
addImageStyle(style, node)
|
|
325
|
+
addStroke(style, node, css)
|
|
326
|
+
Object.assign(style, effectsToCSS(node.effects))
|
|
327
|
+
if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
|
|
328
|
+
addCornerRadii(style, node, css)
|
|
329
|
+
if (node.clipsContent) style.overflow = 'hidden'
|
|
330
|
+
const alignSelf = alignToCSS(node.layoutAlignSelf)
|
|
331
|
+
if (alignSelf) style['align-self'] = alignSelf
|
|
332
|
+
|
|
333
|
+
addLayoutChild(style, node, parent)
|
|
334
|
+
if (node.layoutDirection === 'RTL') style.direction = 'rtl'
|
|
335
|
+
|
|
336
|
+
if (node.layoutMode === 'GRID') {
|
|
337
|
+
addGridContainer(style, node, css)
|
|
338
|
+
addPadding(style, node, css)
|
|
339
|
+
} else if (node.layoutMode !== 'NONE') {
|
|
340
|
+
style.display = 'flex'
|
|
341
|
+
// Row is the flexbox default.
|
|
342
|
+
if (node.layoutMode === 'VERTICAL') style['flex-direction'] = 'column'
|
|
343
|
+
const justifyContent = justifyContentToCSS(node.primaryAxisAlign)
|
|
344
|
+
const alignItems = alignToCSS(node.counterAxisAlign)
|
|
345
|
+
if (justifyContent) style['justify-content'] = justifyContent
|
|
346
|
+
if (alignItems) style['align-items'] = alignItems
|
|
347
|
+
if (node.layoutWrap === 'WRAP') style['flex-wrap'] = 'wrap'
|
|
348
|
+
addFlexGap(style, node, css)
|
|
349
|
+
addPadding(style, node, css)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
addBoxSizing(style)
|
|
353
|
+
return style
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function styleFromTextNode(
|
|
357
|
+
node: SceneNode,
|
|
358
|
+
parent: SceneNode | undefined,
|
|
359
|
+
css: FieldCSS
|
|
360
|
+
): DesignStyleDeclaration {
|
|
361
|
+
const style: DesignStyleDeclaration = {}
|
|
362
|
+
addSize(style, node, parent, css)
|
|
363
|
+
addPositioning(style, node, parent)
|
|
364
|
+
addLayoutChild(style, node, parent)
|
|
365
|
+
if (resolveNodeTextDirection(node) === 'RTL') style.direction = 'rtl'
|
|
366
|
+
const color = fillToCSS(node.fills.at(0))
|
|
367
|
+
style.color = color ? css('fills/0/color', color) : cssColor(BLACK)
|
|
368
|
+
style['font-family'] = node.fontFamily
|
|
369
|
+
style['font-size'] = css('fontSize', px(node.fontSize))
|
|
370
|
+
style['font-weight'] = String(node.fontWeight)
|
|
371
|
+
if (node.italic) style['font-style'] = 'italic'
|
|
372
|
+
if (node.lineHeight !== null) {
|
|
373
|
+
style['line-height'] = css('lineHeight', lineHeightToCSS(node.lineHeight))
|
|
374
|
+
}
|
|
375
|
+
if (node.letterSpacing.value !== 0) {
|
|
376
|
+
style['letter-spacing'] = css('letterSpacing', letterSpacingToCSS(node.letterSpacing))
|
|
377
|
+
}
|
|
378
|
+
if (node.textAlignHorizontal !== 'LEFT')
|
|
379
|
+
style['text-align'] = node.textAlignHorizontal.toLowerCase()
|
|
380
|
+
if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
|
|
381
|
+
const shadow = dropShadowToCSS(node.effects[0])
|
|
382
|
+
if (shadow) style['text-shadow'] = shadow
|
|
383
|
+
if (node.textDecoration !== 'NONE') {
|
|
384
|
+
style['text-decoration-line'] =
|
|
385
|
+
node.textDecoration === 'UNDERLINE' ? 'underline' : 'line-through'
|
|
386
|
+
}
|
|
387
|
+
const textTransform = textCaseToCSS(node.textCase)
|
|
388
|
+
if (textTransform) style['text-transform'] = textTransform
|
|
389
|
+
style['white-space'] = node.maxLines === 1 ? 'nowrap' : 'pre-wrap'
|
|
390
|
+
return style
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function imageSourceURL(node: SceneNode): string | undefined {
|
|
394
|
+
return node.pluginData.find(
|
|
395
|
+
(entry) => entry.pluginId === DOM_CSS_PLUGIN_ID && entry.key === IMAGE_SOURCE_URL_KEY
|
|
396
|
+
)?.value
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** A layer's name, unless it only repeats the layer's type. */
|
|
400
|
+
export function ownLayerName(node: SceneNode): string | undefined {
|
|
401
|
+
return node.name && node.name !== node.type ? node.name : undefined
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function attrsForNode(
|
|
405
|
+
graph: SceneGraph,
|
|
406
|
+
node: SceneNode,
|
|
407
|
+
{
|
|
408
|
+
includeSourceIds,
|
|
409
|
+
includeLayerNames
|
|
410
|
+
}: Pick<ProjectionContext, 'includeSourceIds' | 'includeLayerNames'>
|
|
411
|
+
): Record<string, string> {
|
|
412
|
+
const attrs: Record<string, string> = {}
|
|
413
|
+
const name = includeLayerNames ? ownLayerName(node) : undefined
|
|
414
|
+
if (name) attrs['data-name'] = name
|
|
415
|
+
if (includeSourceIds) attrs['data-pairdraw-node-id'] = node.id
|
|
416
|
+
const sourceURL = imageSourceURL(node)
|
|
417
|
+
if (sourceURL) attrs.src = sourceURL
|
|
418
|
+
const fill = node.fills.at(0)
|
|
419
|
+
if (fill?.type !== 'IMAGE' || !fill.imageHash || shaderOfPaint(node, fill)) return attrs
|
|
420
|
+
const bytes = graph.images.get(fill.imageHash)
|
|
421
|
+
if (!bytes) return attrs
|
|
422
|
+
return { ...attrs, src: imageDataURL(bytes) }
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function tagNameForNode(node: SceneNode): string {
|
|
426
|
+
if (node.type === 'SECTION') return 'section'
|
|
427
|
+
const fill = node.fills.at(0)
|
|
428
|
+
const image = fill?.type === 'IMAGE' && !shaderOfPaint(node, fill)
|
|
429
|
+
if ((image || imageSourceURL(node)) && node.childIds.length === 0) return 'img'
|
|
430
|
+
return 'div'
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
interface ProjectionContext {
|
|
434
|
+
graph: SceneGraph
|
|
435
|
+
includeSourceIds: boolean
|
|
436
|
+
includeLayerNames: boolean
|
|
437
|
+
tokens: DesignTokens | undefined
|
|
438
|
+
vectorElement: VectorElementRenderer | undefined
|
|
439
|
+
shown: ReadonlySet<string> | undefined
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* A vector layer as the renderer's `<svg>`, placed where the layer's box would be: its
|
|
444
|
+
* position, size, opacity, and transform stay, and its paint is the SVG's, not the box's.
|
|
445
|
+
*/
|
|
446
|
+
function vectorDesignNode(
|
|
447
|
+
context: ProjectionContext,
|
|
448
|
+
node: SceneNode,
|
|
449
|
+
attrs: Record<string, string>,
|
|
450
|
+
style: DesignStyleDeclaration
|
|
451
|
+
): DesignElement | null {
|
|
452
|
+
const drawn = context.vectorElement?.(context.graph, node)
|
|
453
|
+
if (!drawn) return null
|
|
454
|
+
const placement = Object.fromEntries(
|
|
455
|
+
Object.entries(style).filter(([property]) => !BOX_PAINT.test(property))
|
|
456
|
+
)
|
|
457
|
+
return {
|
|
458
|
+
...drawn,
|
|
459
|
+
attrs: { ...drawn.attrs, ...attrs },
|
|
460
|
+
inlineStyle: { ...drawn.inlineStyle, ...placement },
|
|
461
|
+
sourceSceneNodeId: node.id,
|
|
462
|
+
sourceSceneNode: node
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function sceneNodeToDesignNode(
|
|
467
|
+
context: ProjectionContext,
|
|
468
|
+
node: SceneNode,
|
|
469
|
+
inherited: CSSModes,
|
|
470
|
+
root: boolean
|
|
471
|
+
): DesignNode | null {
|
|
472
|
+
if ((!node.visible && !context.shown?.has(node.id)) || node.internalOnly) return null
|
|
473
|
+
const { graph, tokens } = context
|
|
474
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : undefined
|
|
475
|
+
const entered = tokens?.enter(node, inherited, root)
|
|
476
|
+
const modes = entered?.modes ?? inherited
|
|
477
|
+
const attrs = { ...attrsForNode(graph, node, context), ...entered?.attrs }
|
|
478
|
+
const css: FieldCSS = (field, literal) => tokens?.reference(node, field, modes) ?? literal
|
|
479
|
+
|
|
480
|
+
if (node.type === 'TEXT') {
|
|
481
|
+
const content = textElementContent(node, styleFromTextNode(node, parent, css))
|
|
482
|
+
return {
|
|
483
|
+
type: 'element',
|
|
484
|
+
tagName: content.tagName,
|
|
485
|
+
attrs,
|
|
486
|
+
inlineStyle: content.style,
|
|
487
|
+
sourceSceneNodeId: node.id,
|
|
488
|
+
sourceSceneNode: node,
|
|
489
|
+
children: content.children
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// An icon is one drawing, however many paths it has.
|
|
494
|
+
if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
|
|
495
|
+
const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css))
|
|
496
|
+
if (vector) return vector
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const children = nodeChildren(graph, node)
|
|
500
|
+
.map((child) => sceneNodeToDesignNode(context, child, modes, false))
|
|
501
|
+
.filter((child): child is DesignNode => child !== null)
|
|
502
|
+
|
|
503
|
+
if (node.type === 'CANVAS') {
|
|
504
|
+
return {
|
|
505
|
+
type: 'element',
|
|
506
|
+
tagName: 'main',
|
|
507
|
+
attrs,
|
|
508
|
+
sourceSceneNodeId: node.id,
|
|
509
|
+
sourceSceneNode: node,
|
|
510
|
+
children
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const style = styleFromSceneNode(node, parent, css)
|
|
515
|
+
const shader = layerShader(graph, node)
|
|
516
|
+
if (shader) addShaderLayerStyle(style)
|
|
517
|
+
return {
|
|
518
|
+
type: 'element',
|
|
519
|
+
tagName: tagNameForNode(node),
|
|
520
|
+
attrs,
|
|
521
|
+
inlineStyle: style,
|
|
522
|
+
sourceSceneNodeId: node.id,
|
|
523
|
+
sourceSceneNode: node,
|
|
524
|
+
children,
|
|
525
|
+
shader
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function projectionContext(
|
|
530
|
+
graph: SceneGraph,
|
|
531
|
+
{
|
|
532
|
+
includeSourceIds = true,
|
|
533
|
+
includeLayerNames = false,
|
|
534
|
+
tokens = true,
|
|
535
|
+
vectorElement,
|
|
536
|
+
shown
|
|
537
|
+
}: SceneGraphToDesignOptions
|
|
538
|
+
): ProjectionContext {
|
|
539
|
+
return {
|
|
540
|
+
graph,
|
|
541
|
+
includeSourceIds,
|
|
542
|
+
includeLayerNames,
|
|
543
|
+
tokens: tokens ? new DesignTokens(graph) : undefined,
|
|
544
|
+
vectorElement,
|
|
545
|
+
shown
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function sceneGraphToDesignDocument(
|
|
550
|
+
graph: SceneGraph,
|
|
551
|
+
options: SceneGraphToDesignOptions = {}
|
|
552
|
+
): DesignDocument {
|
|
553
|
+
const root = graph.getNode(options.rootId ?? graph.rootId)
|
|
554
|
+
const context = projectionContext(graph, options)
|
|
555
|
+
const modes = context.tokens?.defaultModes() ?? new Map<string, string>()
|
|
556
|
+
const children = root
|
|
557
|
+
? nodeChildren(graph, root)
|
|
558
|
+
.map((child) => sceneNodeToDesignNode(context, child, modes, true))
|
|
559
|
+
.filter((child): child is DesignNode => child !== null)
|
|
560
|
+
: []
|
|
561
|
+
|
|
562
|
+
return { type: 'document', sourceGraph: graph, tokens: context.tokens, children }
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** Project one node, including its own box, instead of the children of a root. */
|
|
566
|
+
export function sceneNodeToDesignDocument(
|
|
567
|
+
graph: SceneGraph,
|
|
568
|
+
nodeId: string,
|
|
569
|
+
options: Omit<SceneGraphToDesignOptions, 'rootId'> = {}
|
|
570
|
+
): DesignDocument {
|
|
571
|
+
const node = graph.getNode(nodeId)
|
|
572
|
+
const context = projectionContext(graph, options)
|
|
573
|
+
const modes = context.tokens?.defaultModes() ?? new Map<string, string>()
|
|
574
|
+
const projected = node ? sceneNodeToDesignNode(context, node, modes, true) : null
|
|
575
|
+
return {
|
|
576
|
+
type: 'document',
|
|
577
|
+
sourceGraph: graph,
|
|
578
|
+
tokens: context.tokens,
|
|
579
|
+
children: projected ? [projected] : []
|
|
580
|
+
}
|
|
581
|
+
}
|