@pairdraw/design-jsx 0.0.0-stage → 0.16.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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -2
  3. package/dist/chunks/attributes.js +32 -0
  4. package/dist/chunks/attributes.js.map +1 -0
  5. package/dist/chunks/behaviours/elements.js +321 -0
  6. package/dist/chunks/behaviours/elements.js.map +1 -0
  7. package/dist/chunks/behaviours/render.js +368 -0
  8. package/dist/chunks/behaviours/render.js.map +1 -0
  9. package/dist/chunks/component-properties.js +809 -0
  10. package/dist/chunks/component-properties.js.map +1 -0
  11. package/dist/chunks/components.js +78 -0
  12. package/dist/chunks/components.js.map +1 -0
  13. package/dist/chunks/connectors.js +290 -0
  14. package/dist/chunks/connectors.js.map +1 -0
  15. package/dist/chunks/effects.js +57 -0
  16. package/dist/chunks/effects.js.map +1 -0
  17. package/dist/chunks/elements.js +618 -0
  18. package/dist/chunks/elements.js.map +1 -0
  19. package/dist/chunks/export/behaviours.js +113 -0
  20. package/dist/chunks/export/behaviours.js.map +1 -0
  21. package/dist/chunks/export/bindings.js +273 -0
  22. package/dist/chunks/export/bindings.js.map +1 -0
  23. package/dist/chunks/export/decoration.js +96 -0
  24. package/dist/chunks/export/decoration.js.map +1 -0
  25. package/dist/chunks/export/font.js +17 -0
  26. package/dist/chunks/export/font.js.map +1 -0
  27. package/dist/chunks/export/icons.js +34 -0
  28. package/dist/chunks/export/icons.js.map +1 -0
  29. package/dist/chunks/export/index.js +1390 -0
  30. package/dist/chunks/export/index.js.map +1 -0
  31. package/dist/chunks/fields.js +1065 -0
  32. package/dist/chunks/fields.js.map +1 -0
  33. package/dist/chunks/index.js +438 -0
  34. package/dist/chunks/index.js.map +1 -0
  35. package/dist/chunks/jsx-dev-runtime.js +16 -0
  36. package/dist/chunks/jsx-dev-runtime.js.map +1 -0
  37. package/dist/index.d.ts +543 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +11 -0
  40. package/dist/jsx-dev-runtime.d.ts +2 -0
  41. package/dist/jsx-dev-runtime.js +2 -0
  42. package/dist/jsx-runtime.d.ts +44 -0
  43. package/dist/jsx-runtime.d.ts.map +1 -0
  44. package/dist/jsx-runtime.js +2 -0
  45. package/dist/tree.d.ts +261 -0
  46. package/dist/tree.d.ts.map +1 -0
  47. package/package.json +66 -4
  48. package/src/attributes.ts +30 -0
  49. package/src/behaviours/elements.ts +47 -0
  50. package/src/behaviours/index.ts +253 -0
  51. package/src/behaviours/render.ts +144 -0
  52. package/src/component-properties.ts +428 -0
  53. package/src/components.ts +109 -0
  54. package/src/connectors.ts +316 -0
  55. package/src/definitions.ts +277 -0
  56. package/src/effects.ts +80 -0
  57. package/src/elements.ts +321 -0
  58. package/src/enums.ts +182 -0
  59. package/src/export/behaviours.ts +143 -0
  60. package/src/export/bindings.ts +155 -0
  61. package/src/export/decoration.ts +39 -0
  62. package/src/export/font.ts +25 -0
  63. package/src/export/helpers.ts +75 -0
  64. package/src/export/icons.ts +42 -0
  65. package/src/export/index.ts +297 -0
  66. package/src/export/paint.ts +276 -0
  67. package/src/export/props.ts +369 -0
  68. package/src/export/state.ts +52 -0
  69. package/src/export/text.ts +155 -0
  70. package/src/export/value.ts +99 -0
  71. package/src/export/variables.ts +393 -0
  72. package/src/fields.ts +39 -0
  73. package/src/figjam-text.ts +37 -0
  74. package/src/global.d.ts +4 -0
  75. package/src/helpers.ts +135 -0
  76. package/src/index.ts +155 -0
  77. package/src/jsx-dev-runtime.ts +1 -0
  78. package/src/jsx-runtime.ts +51 -0
  79. package/src/mini-react.ts +23 -0
  80. package/src/overrides/effects.ts +39 -0
  81. package/src/overrides/fills.ts +56 -0
  82. package/src/overrides/state.ts +55 -0
  83. package/src/overrides/strokes.ts +109 -0
  84. package/src/overrides/text.ts +116 -0
  85. package/src/overrides/vector.ts +122 -0
  86. package/src/paints.ts +230 -0
  87. package/src/props-overrides.ts +481 -0
  88. package/src/reconcile.ts +134 -0
  89. package/src/reference/authoring.md +76 -0
  90. package/src/reference/examples.ts +49 -0
  91. package/src/reference.ts +19 -0
  92. package/src/refs.ts +48 -0
  93. package/src/render.ts +147 -0
  94. package/src/renderer.ts +598 -0
  95. package/src/scalar-bindings.ts +36 -0
  96. package/src/schema.ts +213 -0
  97. package/src/services.ts +36 -0
  98. package/src/shapes-with-text.ts +73 -0
  99. package/src/stickies.ts +72 -0
  100. package/src/streaming/index.ts +47 -0
  101. package/src/streaming/literals.ts +82 -0
  102. package/src/streaming/projection.ts +189 -0
  103. package/src/text-content.ts +233 -0
  104. package/src/transform.ts +78 -0
  105. package/src/tree.ts +285 -0
  106. package/src/types.ts +13 -0
  107. package/src/variables/graph.ts +265 -0
  108. package/src/variables/index.ts +124 -0
  109. package/src/variables/props.ts +77 -0
@@ -0,0 +1,276 @@
1
+ import type {
2
+ Effect,
3
+ Fill,
4
+ GradientTransform,
5
+ SceneNode,
6
+ ShadowEffect,
7
+ Stroke
8
+ } from '@pairdraw/scene-graph'
9
+ import { gradientTransformRows, isIdentityGradientTransform } from '@pairdraw/scene-graph/gradient'
10
+ import { isShaderPresetPaint, shaderOfPaint } from '@pairdraw/scene-graph/shaders'
11
+
12
+ import { STROKE_ALIGN, STROKE_CAP, STROKE_JOIN } from '../enums'
13
+ import { formatColor, formatShadow } from './helpers'
14
+ import { helperCall, plainValue, type HelperCall, type JSXProp, type JSXValue } from './value'
15
+
16
+ const GRADIENT_HELPERS: Partial<Record<Fill['type'], string>> = {
17
+ GRADIENT_LINEAR: 'linearGradient',
18
+ GRADIENT_RADIAL: 'radialGradient',
19
+ GRADIENT_ANGULAR: 'angularGradient',
20
+ GRADIENT_DIAMOND: 'diamondGradient'
21
+ }
22
+
23
+ function isIdentity(transform: GradientTransform | undefined): boolean {
24
+ return !transform || isIdentityGradientTransform(transform)
25
+ }
26
+
27
+ /** A paint transform as authors write it, in Figma's rows. */
28
+ function rows(transform: GradientTransform | undefined) {
29
+ return transform && gradientTransformRows(transform)
30
+ }
31
+
32
+ /** Paints and effects imported from Figma often spell out the default blend mode. */
33
+ function blendMode(mode: Fill['blendMode']): Fill['blendMode'] {
34
+ return mode === 'NORMAL' || mode === 'PASS_THROUGH' ? undefined : mode
35
+ }
36
+
37
+ type Options = Record<string, JSXValue | undefined>
38
+
39
+ /** Helper options with defaults left out, or `undefined` when none differ. */
40
+ function options(entries: Options): Options | undefined {
41
+ return Object.values(entries).some((value) => value !== undefined) ? entries : undefined
42
+ }
43
+
44
+ function fillValue(fill: Fill, node: SceneNode): JSXValue {
45
+ const visible = fill.visible ? undefined : false
46
+ const shader = shaderOfPaint(node, fill)
47
+ if (shader && isShaderPresetPaint(shader)) {
48
+ // A preset with nothing but effects is written as just its effects.
49
+ const { components, ...rest } = shader.preset
50
+ return helperCall(
51
+ 'shader',
52
+ plainValue(Object.keys(rest).length === 0 ? components : shader.preset),
53
+ options({
54
+ opacity: fill.opacity === 1 ? undefined : fill.opacity,
55
+ visible,
56
+ blendMode: blendMode(fill.blendMode)
57
+ })
58
+ )
59
+ }
60
+ // Code the user wrote comes back as the same paint only through its own helper.
61
+ if (shader) {
62
+ const uniforms = shader.uniforms ?? {}
63
+ return helperCall(
64
+ 'shaderSource',
65
+ shader.source,
66
+ options({
67
+ uniforms: Object.keys(uniforms).length === 0 ? undefined : plainValue(uniforms),
68
+ opacity: fill.opacity === 1 ? undefined : fill.opacity,
69
+ visible,
70
+ blendMode: blendMode(fill.blendMode)
71
+ })
72
+ )
73
+ }
74
+ if (fill.type === 'SOLID') {
75
+ const color = formatColor(fill.color, fill.color.a)
76
+ const opacity = fill.opacity === fill.color.a ? undefined : fill.opacity
77
+ return helperCall(
78
+ 'solid',
79
+ color,
80
+ options({ opacity, visible, blendMode: blendMode(fill.blendMode) })
81
+ )
82
+ }
83
+ const helper = GRADIENT_HELPERS[fill.type]
84
+ if (helper && fill.gradientStops) {
85
+ const stops = fill.gradientStops.map((stop) => [
86
+ formatColor(stop.color, stop.color.a),
87
+ stop.position
88
+ ])
89
+ return helperCall(
90
+ helper,
91
+ stops,
92
+ options({
93
+ opacity: fill.opacity === 1 ? undefined : fill.opacity,
94
+ visible,
95
+ blendMode: blendMode(fill.blendMode),
96
+ transform: isIdentity(fill.gradientTransform)
97
+ ? undefined
98
+ : plainValue(rows(fill.gradientTransform))
99
+ })
100
+ )
101
+ }
102
+ // Image, pattern, and other paints have no helper; they render back from their fields.
103
+ return plainValue({
104
+ ...fill,
105
+ gradientTransform: rows(fill.gradientTransform),
106
+ imageTransform: rows(fill.imageTransform)
107
+ })
108
+ }
109
+
110
+ /** A single visible solid paint fits the `bg` or text `color` shorthand. */
111
+ function solidShorthand(fills: Fill[]): string | null {
112
+ if (fills.length !== 1) return null
113
+ const [fill] = fills
114
+ if (fill.type !== 'SOLID' || !fill.visible || blendMode(fill.blendMode)) return null
115
+ // The shorthand has one alpha; a fill whose opacity differs from its colour's needs `solid`.
116
+ if (fill.opacity !== fill.color.a) return null
117
+ return formatColor(fill.color, fill.color.a)
118
+ }
119
+
120
+ /** One paint as a colour prop takes it: a colour string when it is plain, its helper otherwise. */
121
+ export function paintPropValue(fill: Fill, node: SceneNode): JSXValue {
122
+ return solidShorthand([fill]) ?? fillValue(fill, node)
123
+ }
124
+
125
+ export function collectFillProps(node: SceneNode, props: JSXProp[]): void {
126
+ if (node.fills.length === 0) {
127
+ // A text is painted black unless it says it is not painted.
128
+ if (node.type === 'TEXT') props.push(['fills', []])
129
+ return
130
+ }
131
+ const shorthand = solidShorthand(node.fills)
132
+ if (shorthand) props.push([node.type === 'TEXT' ? 'color' : 'bg', shorthand])
133
+ else props.push(['fills', node.fills.map((fill) => fillValue(fill, node))])
134
+ }
135
+
136
+ interface StrokeValue {
137
+ [key: string]: JSXValue | undefined
138
+ color: string
139
+ /** Only when it differs from the colour's alpha, which the colour already carries. */
140
+ opacity?: number
141
+ weight?: number
142
+ align?: string
143
+ dash?: number[]
144
+ cap?: string
145
+ join?: string
146
+ visible?: false
147
+ }
148
+
149
+ function strokeValue(stroke: Stroke): StrokeValue {
150
+ return {
151
+ color: formatColor(stroke.color, stroke.color.a),
152
+ opacity: stroke.opacity === stroke.color.a ? undefined : stroke.opacity,
153
+ weight: stroke.weight === 1 ? undefined : stroke.weight,
154
+ align: stroke.align === 'INSIDE' ? undefined : STROKE_ALIGN.print(stroke.align),
155
+ dash: stroke.dashPattern && stroke.dashPattern.length > 0 ? [...stroke.dashPattern] : undefined,
156
+ cap: stroke.cap && STROKE_CAP.print(stroke.cap),
157
+ join: stroke.join && STROKE_JOIN.print(stroke.join),
158
+ visible: stroke.visible ? undefined : false
159
+ }
160
+ }
161
+
162
+ const STROKE_SHORTHAND: Record<string, string> = {
163
+ color: 'stroke',
164
+ weight: 'strokeWidth',
165
+ align: 'strokeAlign',
166
+ dash: 'strokeDash'
167
+ }
168
+
169
+ /** Node-level cap and join apply to strokes that do not set their own. */
170
+ const DEFAULT_STROKE_CAP = 'NONE'
171
+ const DEFAULT_STROKE_JOIN = 'MITER'
172
+
173
+ /**
174
+ * The shorthand props describe one stroke, and `strokeCap`/`strokeJoin` also set the node's
175
+ * cap and join; they fit only when the stroke agrees with the node on both.
176
+ */
177
+ function fitsStrokeShorthand(node: SceneNode, stroke: StrokeValue): boolean {
178
+ if (stroke.visible !== undefined || stroke.opacity !== undefined) return false
179
+ const nodeCap = STROKE_CAP.print(node.strokeCap)
180
+ const nodeJoin = STROKE_JOIN.print(node.strokeJoin)
181
+ return (stroke.cap ?? nodeCap) === nodeCap && (stroke.join ?? nodeJoin) === nodeJoin
182
+ }
183
+
184
+ export function collectStrokeProps(node: SceneNode, props: JSXProp[]): void {
185
+ const strokes = node.strokes.map(strokeValue)
186
+ const [only] = strokes
187
+ const shorthand = strokes.length === 1 && fitsStrokeShorthand(node, only)
188
+ if (shorthand) {
189
+ for (const [key, value] of Object.entries(only)) {
190
+ if (value !== undefined && key !== 'cap' && key !== 'join') {
191
+ props.push([STROKE_SHORTHAND[key], value])
192
+ }
193
+ }
194
+ } else if (strokes.length > 0) {
195
+ props.push(['strokes', strokes])
196
+ }
197
+ if (node.strokeCap !== DEFAULT_STROKE_CAP)
198
+ props.push(['strokeCap', STROKE_CAP.print(node.strokeCap)])
199
+ if (node.strokeJoin !== DEFAULT_STROKE_JOIN) {
200
+ props.push(['strokeJoin', STROKE_JOIN.print(node.strokeJoin)])
201
+ }
202
+ if (node.dashPattern.length > 0) props.push(['dashPattern', [...node.dashPattern]])
203
+ if (node.independentStrokeWeights) {
204
+ props.push([
205
+ 'strokeWeights',
206
+ {
207
+ top: node.borderTopWeight,
208
+ right: node.borderRightWeight,
209
+ bottom: node.borderBottomWeight,
210
+ left: node.borderLeftWeight
211
+ }
212
+ ])
213
+ }
214
+ }
215
+
216
+ const EFFECT_HELPERS: Record<ShadowEffect['type'], string> = {
217
+ DROP_SHADOW: 'dropShadow',
218
+ INNER_SHADOW: 'innerShadow',
219
+ LAYER_BLUR: 'layerBlur',
220
+ BACKGROUND_BLUR: 'backgroundBlur',
221
+ FOREGROUND_BLUR: 'foregroundBlur'
222
+ }
223
+
224
+ function effectValue(effect: ShadowEffect): HelperCall {
225
+ const visible = effect.visible ? undefined : false
226
+ if (effect.type === 'DROP_SHADOW' || effect.type === 'INNER_SHADOW') {
227
+ return helperCall(
228
+ EFFECT_HELPERS[effect.type],
229
+ options({
230
+ x: effect.offset.x,
231
+ y: effect.offset.y,
232
+ radius: effect.radius,
233
+ spread: effect.spread === 0 ? undefined : effect.spread,
234
+ color: formatColor(effect.color, effect.color.a),
235
+ visible,
236
+ blendMode: blendMode(effect.blendMode),
237
+ showShadowBehindNode: effect.showShadowBehindNode
238
+ })
239
+ )
240
+ }
241
+ return helperCall(
242
+ EFFECT_HELPERS[effect.type],
243
+ visible === undefined ? effect.radius : { radius: effect.radius, visible }
244
+ )
245
+ }
246
+
247
+ function plainDropShadow(effect: Effect): string | null {
248
+ const plain =
249
+ effect.type === 'DROP_SHADOW' &&
250
+ effect.spread === 0 &&
251
+ !blendMode(effect.blendMode) &&
252
+ effect.showShadowBehindNode === undefined
253
+ return plain ? formatShadow(effect) : null
254
+ }
255
+
256
+ /**
257
+ * `shadow` and `blur` describe a plain drop shadow followed by a layer blur, each optional,
258
+ * in the order the renderer appends them; anything else needs `effects`.
259
+ */
260
+ function effectShorthand(effects: ShadowEffect[]): JSXProp[] | null {
261
+ if (effects.some((effect) => !effect.visible)) return null
262
+ const shadow = effects.length > 0 ? plainDropShadow(effects[0]) : null
263
+ const rest = shadow ? effects.slice(1) : effects
264
+ if (rest.length > 1 || rest.some((effect) => effect.type !== 'LAYER_BLUR')) return null
265
+ const props: JSXProp[] = []
266
+ if (shadow) props.push(['shadow', shadow])
267
+ for (const blur of rest) props.push(['blur', blur.radius])
268
+ return props
269
+ }
270
+
271
+ export function collectEffectProps(node: SceneNode, props: JSXProp[]): void {
272
+ // A shader effect has no JSX helper yet; leaving it out keeps the source importable.
273
+ const effects = node.effects.filter((effect): effect is ShadowEffect => effect.type !== 'SHADER')
274
+ if (effects.length === 0) return
275
+ props.push(...(effectShorthand(effects) ?? [['effects', effects.map(effectValue)]]))
276
+ }
@@ -0,0 +1,369 @@
1
+ import { isEqual, omitBy, pick } from 'es-toolkit'
2
+
3
+ import type { ArcData, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
4
+ import { DEFAULT_FONT_FAMILY } from '@pairdraw/scene-graph/constants'
5
+ import { layoutSizing } from '@pairdraw/scene-graph/layout'
6
+ import { createDefaultNode } from '@pairdraw/scene-graph/node-defaults'
7
+ import { resolveNodeTextDirection } from '@pairdraw/scene-graph/text/direction'
8
+ import { vectorNetworkPaths } from '@pairdraw/scene-graph/vector-network'
9
+
10
+ import { BLEND_MODE, TEXT_ALIGN, TEXT_ALIGN_VERTICAL, TEXT_CASE, TEXT_DECORATION } from '../enums'
11
+ import { textLengthJSX } from '../overrides/text'
12
+ import { collectDecorationProps } from './decoration'
13
+ import { collectFontSettingProps, FONT_SETTING_FIELDS } from './font'
14
+ import {
15
+ collectCornerRadii,
16
+ collectPadding,
17
+ emitPadding,
18
+ formatTracks,
19
+ getNodeContext
20
+ } from './helpers'
21
+ import { collectEffectProps, collectFillProps, collectStrokeProps } from './paint'
22
+ import { collectStateProps } from './state'
23
+ import type { JSXProp } from './value'
24
+ import { variableReferences, type VariableExport } from './variables'
25
+
26
+ export type { JSXProp } from './value'
27
+
28
+ /** The element that represents each node type; other types are not exported. */
29
+
30
+ function collectGridSizingProps(node: SceneNode, props: JSXProp[]): void {
31
+ props.push(['grid', true])
32
+ if (node.gridTemplateColumns.length > 0)
33
+ props.push(['columns', formatTracks(node.gridTemplateColumns)])
34
+ if (node.gridTemplateRows.length > 0) props.push(['rows', formatTracks(node.gridTemplateRows)])
35
+ if (node.width > 0) props.push(['w', node.width])
36
+ if (node.gridTemplateRows.length > 0 && node.height > 0) props.push(['h', node.height])
37
+ if (node.gridColumnGap > 0) props.push(['columnGap', node.gridColumnGap])
38
+ if (node.gridRowGap > 0) props.push(['rowGap', node.gridRowGap])
39
+ }
40
+
41
+ function collectFlexSizingProps(node: SceneNode, props: JSXProp[]): void {
42
+ props.push(['flex', node.layoutMode === 'HORIZONTAL' ? 'row' : 'col'])
43
+ if (node.layoutDirection === 'RTL') props.push(['dir', 'rtl'])
44
+ const primaryAxis = node.layoutMode === 'HORIZONTAL' ? 'width' : 'height'
45
+ const crossAxis = node.layoutMode === 'HORIZONTAL' ? 'height' : 'width'
46
+
47
+ if (node.primaryAxisSizing !== 'HUG') {
48
+ props.push([primaryAxis === 'width' ? 'w' : 'h', node[primaryAxis]])
49
+ }
50
+ if (node.counterAxisSizing !== 'HUG') {
51
+ props.push([crossAxis === 'width' ? 'w' : 'h', node[crossAxis]])
52
+ }
53
+ }
54
+
55
+ function collectGridPositionProps(node: SceneNode, props: JSXProp[]): void {
56
+ if (!node.gridPosition) return
57
+ const pos = node.gridPosition
58
+ if (pos.column > 0) props.push(['colStart', pos.column])
59
+ if (pos.row > 0) props.push(['rowStart', pos.row])
60
+ if (pos.columnSpan > 1) props.push(['colSpan', pos.columnSpan])
61
+ if (pos.rowSpan > 1) props.push(['rowSpan', pos.rowSpan])
62
+ }
63
+
64
+ function collectFlexAlignmentProps(node: SceneNode, props: JSXProp[]): void {
65
+ if (node.itemSpacing > 0) props.push(['gap', node.itemSpacing])
66
+
67
+ if (node.layoutWrap === 'WRAP') {
68
+ props.push(['wrap', true])
69
+ if (node.counterAxisSpacing > 0) props.push(['rowGap', node.counterAxisSpacing])
70
+ }
71
+
72
+ if (node.primaryAxisAlign === 'CENTER') props.push(['justify', 'center'])
73
+ else if (node.primaryAxisAlign === 'MAX') props.push(['justify', 'end'])
74
+ else if (node.primaryAxisAlign === 'SPACE_BETWEEN') props.push(['justify', 'between'])
75
+ else if (node.primaryAxisAlign === 'SPACE_AROUND') props.push(['justify', 'around'])
76
+
77
+ if (node.counterAxisAlign === 'CENTER') props.push(['items', 'center'])
78
+ else if (node.counterAxisAlign === 'MAX') props.push(['items', 'end'])
79
+ else if (node.counterAxisAlign === 'STRETCH') props.push(['items', 'stretch'])
80
+ else if (node.counterAxisAlign === 'BASELINE') props.push(['items', 'baseline'])
81
+ }
82
+
83
+ function collectAutoLayoutPaddingProps(node: SceneNode, props: JSXProp[]): void {
84
+ const pad = collectPadding(node)
85
+ if (!pad) return
86
+ props.push(
87
+ ...emitPadding(
88
+ pad,
89
+ (v) => ['p', v] as JSXProp,
90
+ (y, x) =>
91
+ [
92
+ ['py', y],
93
+ ['px', x]
94
+ ] as JSXProp[],
95
+ ({ pt, pr, pb, pl }) => {
96
+ const r: JSXProp[] = []
97
+ if (pt > 0) r.push(['pt', pt])
98
+ if (pr > 0) r.push(['pr', pr])
99
+ if (pb > 0) r.push(['pb', pb])
100
+ if (pl > 0) r.push(['pl', pl])
101
+ return r
102
+ }
103
+ )
104
+ )
105
+ }
106
+
107
+ function collectCornerRadiiProps(node: SceneNode, props: JSXProp[]): void {
108
+ const corners = collectCornerRadii(node)
109
+ if (!corners) return
110
+ const { tl, tr, br, bl } = corners
111
+ if (tl === tr && tr === br && br === bl) {
112
+ props.push(['rounded', tl])
113
+ } else {
114
+ if (tl > 0) props.push(['roundedTL', tl])
115
+ if (tr > 0) props.push(['roundedTR', tr])
116
+ if (br > 0) props.push(['roundedBR', br])
117
+ if (bl > 0) props.push(['roundedBL', bl])
118
+ }
119
+ }
120
+
121
+ function collectAppearanceProps(node: SceneNode, props: JSXProp[]): void {
122
+ collectFillProps(node, props)
123
+ collectStrokeProps(node, props)
124
+ collectCornerRadiiProps(node, props)
125
+
126
+ if (node.cornerSmoothing > 0) props.push(['cornerSmoothing', node.cornerSmoothing])
127
+ if (node.opacity < 1) props.push(['opacity', Math.round(node.opacity * 100) / 100])
128
+ if (node.rotation !== 0) props.push(['rotate', Math.round(node.rotation * 100) / 100])
129
+ if (node.flipX) props.push(['flipX', true])
130
+ if (node.flipY) props.push(['flipY', true])
131
+ if (node.blendMode !== 'PASS_THROUGH' && node.blendMode !== 'NORMAL') {
132
+ props.push(['blendMode', BLEND_MODE.print(node.blendMode)])
133
+ }
134
+ if (node.clipsContent) props.push(['overflow', 'hidden'])
135
+ collectEffectProps(node, props)
136
+ }
137
+
138
+ function collectPositionProps(
139
+ node: SceneNode,
140
+ ctx: ReturnType<typeof getNodeContext>,
141
+ props: JSXProp[]
142
+ ): void {
143
+ if (ctx.parentIsAutoLayout || ctx.parentIsGrid) {
144
+ // Absolute children of a layout keep their position; the rest are placed by it.
145
+ if (node.layoutPositioning !== 'ABSOLUTE') return
146
+ props.push(['position', 'absolute'], ['x', node.x], ['y', node.y])
147
+ return
148
+ }
149
+ if (node.x !== 0) props.push(['x', node.x])
150
+ if (node.y !== 0) props.push(['y', node.y])
151
+ }
152
+
153
+ /** A layer's opacity and whether it is hidden, for an element that writes its own props. */
154
+ export function presenceProps(node: SceneNode): JSXProp[] {
155
+ const props: JSXProp[] = []
156
+ if (node.opacity < 1) props.push(['opacity', Math.round(node.opacity * 100) / 100])
157
+ if (!node.visible) props.push(['visible', false])
158
+ return props
159
+ }
160
+
161
+ /** Where a layer sits in its parent, as the export writes any layer's position. */
162
+ export function positionProps(node: SceneNode, graph: SceneGraph): JSXProp[] {
163
+ const props: JSXProp[] = []
164
+ collectPositionProps(node, getNodeContext(node, graph), props)
165
+ return props
166
+ }
167
+
168
+ function collectSizingProps(
169
+ node: SceneNode,
170
+ ctx: ReturnType<typeof getNodeContext>,
171
+ graph: SceneGraph,
172
+ props: JSXProp[]
173
+ ): void {
174
+ if (ctx.isGrid) collectGridSizingProps(node, props)
175
+ else if (ctx.isFlex) collectFlexSizingProps(node, props)
176
+ else if (node.type === 'TEXT') collectTextSizingProps(node, graph, props)
177
+ else {
178
+ if (node.width > 0) props.push(['w', node.width])
179
+ if (node.height > 0) props.push(['h', node.height])
180
+ }
181
+
182
+ if (!ctx.parentIsAutoLayout) return
183
+ const parent = node.parentId ? graph.getNode(node.parentId) : undefined
184
+ // A flex child's main-axis fill keeps its grow factor; every other fill is `"fill"`.
185
+ const growAxis = parent?.layoutMode === 'VERTICAL' ? 'VERTICAL' : 'HORIZONTAL'
186
+ const keepsGrow = parent?.layoutMode !== 'GRID' && node.layoutGrow > 0
187
+ if (keepsGrow) props.push(['grow', node.layoutGrow])
188
+ for (const [axis, key] of [
189
+ ['HORIZONTAL', 'w'],
190
+ ['VERTICAL', 'h']
191
+ ] as const) {
192
+ if (keepsGrow && axis === growAxis) continue
193
+ if (layoutSizing(graph, node, axis) !== 'FILL') continue
194
+ // Stretch inherited from the parent's alignment is written on the parent.
195
+ if (axis !== growAxis && node.layoutAlignSelf !== 'STRETCH') continue
196
+ // Fill replaces the size the node had before it filled.
197
+ const index = props.findIndex(([k]) => k === key)
198
+ if (index === -1) props.push([key, 'fill'])
199
+ else props[index] = [key, 'fill']
200
+ }
201
+ }
202
+
203
+ function collectTextSizingProps(node: SceneNode, graph: SceneGraph, props: JSXProp[]): void {
204
+ const autoResize = node.textAutoResize
205
+ const emitH = autoResize === 'NONE' || autoResize === 'TRUNCATE'
206
+ // Don't emit fixed w when text stretches to fill parent — the layoutAlignSelf
207
+ // check below will emit w="fill" instead. Without this guard, w={computedPx}
208
+ // gets emitted first and blocks the fill detection.
209
+ const isFillWidth =
210
+ node.layoutAlignSelf === 'STRETCH' &&
211
+ (() => {
212
+ const parent = node.parentId ? graph.getNode(node.parentId) : null
213
+ return parent?.layoutMode === 'VERTICAL'
214
+ })()
215
+ const isGrowWidth =
216
+ node.layoutGrow > 0 &&
217
+ (() => {
218
+ const parent = node.parentId ? graph.getNode(node.parentId) : null
219
+ return parent?.layoutMode === 'HORIZONTAL'
220
+ })()
221
+ const emitW = autoResize !== 'WIDTH_AND_HEIGHT' && !isFillWidth && !isGrowWidth
222
+ if (emitW && node.width > 0) props.push(['w', node.width])
223
+ if (emitH && node.height > 0) props.push(['h', node.height])
224
+ }
225
+
226
+ const DECORATION_FIELDS = [
227
+ 'textDecorationStyle',
228
+ 'textDecorationFills',
229
+ 'textDecorationThickness',
230
+ 'textUnderlineOffset',
231
+ 'textDecorationSkipInk'
232
+ ] as const
233
+
234
+ const TEXT_DEFAULTS = pick(
235
+ createDefaultNode(() => '', 'TEXT'),
236
+ [...DECORATION_FIELDS, ...FONT_SETTING_FIELDS]
237
+ )
238
+
239
+ /** The fields of `node` that differ from a new text's. */
240
+ function ownTextFields<K extends keyof typeof TEXT_DEFAULTS>(
241
+ node: SceneNode,
242
+ fields: readonly K[]
243
+ ) {
244
+ return omitBy(pick(node, fields), (value, key) => isEqual(value, TEXT_DEFAULTS[key]))
245
+ }
246
+
247
+ /** A text's font settings, and whether its optical size follows the font size. */
248
+ function collectTextFontSettingProps(node: SceneNode, props: JSXProp[]): void {
249
+ collectFontSettingProps(ownTextFields(node, FONT_SETTING_FIELDS), props)
250
+ if (!node.opticalSizeFromFontSize) props.push(['fontOpticalSizing', 'none'])
251
+ }
252
+
253
+ /** A font weight as `weight` writes it, by name for the weights that have one. */
254
+ export function fontWeightValue(weight: number): string | number {
255
+ if (weight === 700) return 'bold'
256
+ if (weight === 500) return 'medium'
257
+ return weight
258
+ }
259
+
260
+ function collectTextNodeProps(node: SceneNode, props: JSXProp[]): void {
261
+ const direction = resolveNodeTextDirection(node)
262
+ if (node.fontSize !== 14) props.push(['size', node.fontSize])
263
+ if (node.fontFamily && node.fontFamily !== DEFAULT_FONT_FAMILY)
264
+ props.push(['font', node.fontFamily])
265
+ if (node.fontWeight !== 400) props.push(['weight', fontWeightValue(node.fontWeight)])
266
+ if (direction === 'RTL') props.push(['dir', 'rtl'])
267
+ if (node.italic) props.push(['italic', true])
268
+ if (node.textAlignHorizontal !== 'LEFT') {
269
+ props.push(['textAlign', TEXT_ALIGN.print(node.textAlignHorizontal)])
270
+ }
271
+ if (node.textAlignVertical !== 'TOP') {
272
+ props.push(['textAlignVertical', TEXT_ALIGN_VERTICAL.print(node.textAlignVertical)])
273
+ }
274
+ if (node.lineHeight != null) props.push(['lineHeight', textLengthJSX(node.lineHeight)])
275
+ if (node.letterSpacing.value !== 0) {
276
+ props.push(['letterSpacing', textLengthJSX(node.letterSpacing)])
277
+ }
278
+ if (node.textDecoration !== 'NONE')
279
+ props.push(['textDecoration', TEXT_DECORATION.print(node.textDecoration)])
280
+ collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props)
281
+ collectTextFontSettingProps(node, props)
282
+ if (node.textCase !== 'ORIGINAL') props.push(['textCase', TEXT_CASE.print(node.textCase)])
283
+ if (node.maxLines != null) props.push(['maxLines', node.maxLines])
284
+ if (node.textTruncation === 'ENDING' && node.maxLines == null) props.push(['truncate', true])
285
+ if (node.paragraphSpacing !== 0) props.push(['paragraphSpacing', node.paragraphSpacing])
286
+ if (node.listSpacing !== 0) props.push(['listSpacing', node.listSpacing])
287
+ if (node.paragraphIndent !== 0) props.push(['paragraphIndent', node.paragraphIndent])
288
+ if (node.hangingList) props.push(['hangingList', true])
289
+ }
290
+
291
+ const TEXT_AUTO_RESIZE: Record<SceneNode['textAutoResize'], string> = {
292
+ NONE: 'none',
293
+ WIDTH_AND_HEIGHT: 'width',
294
+ HEIGHT: 'height',
295
+ TRUNCATE: 'truncate'
296
+ }
297
+
298
+ /** The renderer infers auto-resize from the width props; say so only when it differs. */
299
+ function collectTextAutoResizeProp(
300
+ node: SceneNode,
301
+ ctx: ReturnType<typeof getNodeContext>,
302
+ props: JSXProp[]
303
+ ): void {
304
+ const hasWidth = props.some(([key]) => key === 'w')
305
+ const grows = ctx.parentIsAutoLayout && node.layoutGrow > 0
306
+ const inferred = hasWidth || grows ? 'HEIGHT' : 'WIDTH_AND_HEIGHT'
307
+ if (node.textAutoResize !== inferred) {
308
+ props.push(['textAutoResize', TEXT_AUTO_RESIZE[node.textAutoResize]])
309
+ }
310
+ }
311
+
312
+ const roundDegrees = (radians: number) => Math.round(((radians * 180) / Math.PI) * 10_000) / 10_000
313
+
314
+ /** An ellipse's arc, written only where it is not a whole ellipse. */
315
+ function collectArcProps(arc: ArcData | null, props: JSXProp[]): void {
316
+ if (!arc) return
317
+ const start = roundDegrees(arc.startingAngle)
318
+ const end = roundDegrees(arc.endingAngle)
319
+ if (start !== 0) props.push(['startAngle', start])
320
+ // A whole turn from the start is the default end.
321
+ if (Math.abs(end - (start + 360)) > 1e-6) props.push(['endAngle', end])
322
+ if (arc.innerRadius !== 0) props.push(['innerRadius', arc.innerRadius])
323
+ }
324
+
325
+ function collectShapeNodeProps(node: SceneNode, props: JSXProp[]): void {
326
+ if (node.type === 'ELLIPSE') collectArcProps(node.arcData, props)
327
+ if (node.type === 'STAR') {
328
+ if (node.pointCount !== 5) props.push(['points', node.pointCount])
329
+ if (node.starInnerRadius !== 0.382) props.push(['innerRadius', node.starInnerRadius])
330
+ }
331
+ if (node.type === 'POLYGON' && node.pointCount !== 3) {
332
+ props.push(['points', node.pointCount])
333
+ }
334
+ if (node.type === 'VECTOR' && node.vectorNetwork) {
335
+ // Figma's own form of a vector's geometry, which <Vector> reads back as written.
336
+ const paths = vectorNetworkPaths(node.vectorNetwork)
337
+ if (paths.length > 0) props.push(['paths', paths.map((path) => ({ ...path }))])
338
+ }
339
+ }
340
+
341
+ /**
342
+ * The props of `node`, in the order they are printed, with the variables it binds written as
343
+ * `variables` writes them: by default, by reference to the document's.
344
+ */
345
+ export function collectProps(
346
+ node: SceneNode,
347
+ graph: SceneGraph,
348
+ variables: VariableExport = variableReferences(graph)
349
+ ): JSXProp[] {
350
+ const props: JSXProp[] = []
351
+ const ctx = getNodeContext(node, graph)
352
+
353
+ if (node.name && node.name !== node.type) props.push(['name', node.name])
354
+
355
+ collectPositionProps(node, ctx, props)
356
+ collectSizingProps(node, ctx, graph, props)
357
+ if (ctx.parentIsGrid) collectGridPositionProps(node, props)
358
+ if (ctx.isFlex) collectFlexAlignmentProps(node, props)
359
+ if (ctx.isAutoLayout) collectAutoLayoutPaddingProps(node, props)
360
+ collectAppearanceProps(node, props)
361
+ if (node.type === 'TEXT') {
362
+ collectTextNodeProps(node, props)
363
+ collectTextAutoResizeProp(node, ctx, props)
364
+ }
365
+ collectShapeNodeProps(node, props)
366
+ collectStateProps(node, ctx, props, variables)
367
+
368
+ return props
369
+ }
@@ -0,0 +1,52 @@
1
+ import type { SceneNode } from '@pairdraw/scene-graph'
2
+
3
+ import { CONSTRAINT, MASK_TYPE } from '../enums'
4
+ import { collectBindingProps, collectModeProps } from './bindings'
5
+ import type { getNodeContext } from './helpers'
6
+ import type { JSXProp } from './value'
7
+ import type { VariableExport } from './variables'
8
+
9
+ /** Constraints apply where a parent does not lay its children out. */
10
+ function hasConstraints(node: SceneNode, ctx: ReturnType<typeof getNodeContext>): boolean {
11
+ return !ctx.parentIsAutoLayout || node.layoutPositioning === 'ABSOLUTE'
12
+ }
13
+
14
+ function collectConstraintProps(
15
+ node: SceneNode,
16
+ ctx: ReturnType<typeof getNodeContext>,
17
+ props: JSXProp[]
18
+ ): void {
19
+ if (!hasConstraints(node, ctx)) return
20
+ const horizontal = node.horizontalConstraint === 'MIN' ? undefined : node.horizontalConstraint
21
+ const vertical = node.verticalConstraint === 'MIN' ? undefined : node.verticalConstraint
22
+ if (!horizontal && !vertical) return
23
+ props.push([
24
+ 'constraints',
25
+ {
26
+ horizontal: horizontal && CONSTRAINT.print(horizontal),
27
+ vertical: vertical && CONSTRAINT.print(vertical)
28
+ }
29
+ ])
30
+ }
31
+
32
+ function collectSizeLimitProps(node: SceneNode, props: JSXProp[]): void {
33
+ if (node.minWidth != null) props.push(['minW', node.minWidth])
34
+ if (node.maxWidth != null) props.push(['maxW', node.maxWidth])
35
+ if (node.minHeight != null) props.push(['minH', node.minHeight])
36
+ if (node.maxHeight != null) props.push(['maxH', node.maxHeight])
37
+ }
38
+
39
+ export function collectStateProps(
40
+ node: SceneNode,
41
+ ctx: ReturnType<typeof getNodeContext>,
42
+ props: JSXProp[],
43
+ variables: VariableExport
44
+ ): void {
45
+ collectConstraintProps(node, ctx, props)
46
+ collectSizeLimitProps(node, props)
47
+ if (node.isMask) props.push(['mask', MASK_TYPE.print(node.maskType)])
48
+ if (!node.visible) props.push(['visible', false])
49
+ if (node.locked) props.push(['locked', true])
50
+ collectModeProps(node, props, variables)
51
+ collectBindingProps(node, props, variables)
52
+ }