@pairdraw/design-jsx 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.
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 +54 -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,37 @@
1
+ import type { SceneNode } from '@pairdraw/scene-graph'
2
+ import {
3
+ figJamTextSize,
4
+ figJamTextSizeFields,
5
+ figJamTypeface,
6
+ figJamTypefaceFields,
7
+ type FigJamTextSize,
8
+ type FigJamTypeface
9
+ } from '@pairdraw/scene-graph/figjam'
10
+
11
+ import { FIGJAM_TEXT_SIZE, FIGJAM_TYPEFACE } from './enums'
12
+
13
+ /**
14
+ * The fields a sticky note's body or a shape's label takes from `textSize` and `typeface`, as
15
+ * FigJam's text menus set them; the typeface keeps the size the text ends up at.
16
+ */
17
+ export function figJamTextFields(
18
+ text: Pick<SceneNode, 'fontSize'>,
19
+ textSize: FigJamTextSize | undefined,
20
+ typeface: FigJamTypeface | undefined
21
+ ): Partial<SceneNode> {
22
+ const sized = textSize ? figJamTextSizeFields(textSize, typeface) : {}
23
+ if (!typeface) return sized
24
+ return figJamTypefaceFields(typeface, { fontSize: sized.fontSize ?? text.fontSize })
25
+ }
26
+
27
+ /** `textSize` and `typeface` as an element writes them, each left out at FigJam's default. */
28
+ export function figJamTextProps(
29
+ text: Pick<SceneNode, 'fontSize' | 'fontFamily'>
30
+ ): Array<[string, string]> {
31
+ const props: Array<[string, string]> = []
32
+ const size = figJamTextSize(text)
33
+ if (size !== 'small') props.push(['textSize', FIGJAM_TEXT_SIZE.print(size)])
34
+ const typeface = figJamTypeface(text)
35
+ if (typeface && typeface !== 'simple') props.push(['typeface', FIGJAM_TYPEFACE.print(typeface)])
36
+ return props
37
+ }
@@ -0,0 +1,4 @@
1
+ declare module '*?raw' {
2
+ const content: string
3
+ export default content
4
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,135 @@
1
+ import { difference } from 'es-toolkit/array'
2
+ import { isPlainObject } from 'es-toolkit/predicate'
3
+
4
+ import { property } from './component-properties'
5
+ import type { DesignJSXHelperDefinition } from './definitions'
6
+ import {
7
+ backgroundBlur,
8
+ dropShadow,
9
+ foregroundBlur,
10
+ innerShadow,
11
+ layerBlur,
12
+ type BlurEffectOptions,
13
+ type ShadowEffectOptions
14
+ } from './effects'
15
+ import {
16
+ angularGradient,
17
+ diamondGradient,
18
+ gradient,
19
+ linearGradient,
20
+ radialGradient,
21
+ shader,
22
+ shaderSource,
23
+ solid,
24
+ type GradientPaintOptions,
25
+ type ShaderSourceOptions,
26
+ type SolidPaintOptions
27
+ } from './paints'
28
+ import { ref } from './refs'
29
+ import { defineMode, defineVars, designMode, designVar } from './variables'
30
+
31
+ /** Every option a helper reads; `satisfies` keeps the list equal to the options type. */
32
+ type OptionKeys<Options> = Record<keyof Options, true>
33
+
34
+ const SHADOW_OPTIONS = {
35
+ color: true,
36
+ x: true,
37
+ y: true,
38
+ offset: true,
39
+ radius: true,
40
+ spread: true,
41
+ visible: true,
42
+ blendMode: true,
43
+ showShadowBehindNode: true
44
+ } satisfies OptionKeys<ShadowEffectOptions>
45
+
46
+ const BLUR_OPTIONS = {
47
+ radius: true,
48
+ visible: true
49
+ } satisfies OptionKeys<BlurEffectOptions>
50
+
51
+ const SOLID_OPTIONS = {
52
+ opacity: true,
53
+ visible: true,
54
+ blendMode: true
55
+ } satisfies OptionKeys<SolidPaintOptions>
56
+
57
+ const SHADER_SOURCE_OPTIONS = {
58
+ ...SOLID_OPTIONS,
59
+ uniforms: true
60
+ } satisfies OptionKeys<ShaderSourceOptions>
61
+
62
+ const GRADIENT_OPTIONS = {
63
+ ...SOLID_OPTIONS,
64
+ transform: true
65
+ } satisfies OptionKeys<GradientPaintOptions>
66
+
67
+ /** Option names people reach for that the helpers spell as Figma's effects do. */
68
+ const FIGMA_OPTION_NAMES: Record<string, string> = { blur: 'radius' }
69
+
70
+ function unsupportedOptionWarning(name: string, key: string, known: string[]): string {
71
+ const figmaName = FIGMA_OPTION_NAMES[key]
72
+ const hint =
73
+ figmaName && known.includes(figmaName)
74
+ ? `Use "${figmaName}", the name Figma uses.`
75
+ : `Supported options: ${known.join(', ')}.`
76
+ return `Unsupported option "${key}" in ${name}() is ignored. ${hint}`
77
+ }
78
+
79
+ /**
80
+ * Wrap a helper so each option it ignores adds a warning instead of vanishing, which is
81
+ * what happens to a misspelled option. `optionsAt` is the argument holding the options.
82
+ */
83
+ function checked<Args extends unknown[], Result>(
84
+ warnings: string[],
85
+ name: string,
86
+ helper: (...args: Args) => Result,
87
+ options: object,
88
+ optionsAt: number
89
+ ): (...args: Args) => Result {
90
+ const known = Object.keys(options)
91
+ return (...args) => {
92
+ const passed = args[optionsAt]
93
+ if (isPlainObject(passed)) {
94
+ for (const key of difference(Object.keys(passed), known)) {
95
+ warnings.push(unsupportedOptionWarning(name, key, known))
96
+ }
97
+ }
98
+ return helper(...args)
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Every helper Design JSX code can call, reporting ignored options to `warnings`. This is the
104
+ * one list of them: the string renderer's scope, the code panel's sandbox, the editor's
105
+ * completions, and the authoring reference all read it.
106
+ */
107
+ export function designJSXHelpers(warnings: string[]) {
108
+ return {
109
+ solid: checked(warnings, 'solid', solid, SOLID_OPTIONS, 1),
110
+ gradient: checked(warnings, 'gradient', gradient, GRADIENT_OPTIONS, 2),
111
+ linearGradient: checked(warnings, 'linearGradient', linearGradient, GRADIENT_OPTIONS, 1),
112
+ radialGradient: checked(warnings, 'radialGradient', radialGradient, GRADIENT_OPTIONS, 1),
113
+ angularGradient: checked(warnings, 'angularGradient', angularGradient, GRADIENT_OPTIONS, 1),
114
+ diamondGradient: checked(warnings, 'diamondGradient', diamondGradient, GRADIENT_OPTIONS, 1),
115
+ shader: checked(warnings, 'shader', shader, SOLID_OPTIONS, 1),
116
+ shaderSource: checked(warnings, 'shaderSource', shaderSource, SHADER_SOURCE_OPTIONS, 1),
117
+ dropShadow: checked(warnings, 'dropShadow', dropShadow, SHADOW_OPTIONS, 0),
118
+ innerShadow: checked(warnings, 'innerShadow', innerShadow, SHADOW_OPTIONS, 0),
119
+ layerBlur: checked(warnings, 'layerBlur', layerBlur, BLUR_OPTIONS, 0),
120
+ backgroundBlur: checked(warnings, 'backgroundBlur', backgroundBlur, BLUR_OPTIONS, 0),
121
+ foregroundBlur: checked(warnings, 'foregroundBlur', foregroundBlur, BLUR_OPTIONS, 0),
122
+ defineVars,
123
+ defineMode,
124
+ designVar,
125
+ designMode,
126
+ property,
127
+ ref
128
+ }
129
+ }
130
+
131
+ export type DesignJSXHelperName = keyof ReturnType<typeof designJSXHelpers>
132
+
133
+ export const DESIGN_JSX_HELPERS: DesignJSXHelperDefinition[] = Object.keys(
134
+ designJSXHelpers([])
135
+ ).map((name) => ({ name, description: `Pairdraw ${name} helper.` }))
package/src/index.ts ADDED
@@ -0,0 +1,155 @@
1
+ export {
2
+ Frame,
3
+ Text,
4
+ Span,
5
+ Paragraph,
6
+ Rectangle,
7
+ Ellipse,
8
+ Line,
9
+ Star,
10
+ Polygon,
11
+ Vector,
12
+ Group,
13
+ Section,
14
+ Component,
15
+ ComponentSet,
16
+ Instance,
17
+ View,
18
+ Rect,
19
+ Page,
20
+ INTRINSIC_ELEMENTS
21
+ } from './components'
22
+ export {
23
+ Accordion,
24
+ Button,
25
+ Checkbox,
26
+ Collapsible,
27
+ NumberField,
28
+ Progress,
29
+ RadioGroup,
30
+ Slider,
31
+ Switch,
32
+ Tabs,
33
+ Textarea,
34
+ TextField,
35
+ Toggle,
36
+ ToggleGroup,
37
+ type RekaElement,
38
+ type RekaProps
39
+ } from './behaviours/elements'
40
+
41
+ export {
42
+ type TreeNode,
43
+ type BaseProps,
44
+ type EllipseProps,
45
+ type VectorProps,
46
+ type ComponentProps,
47
+ type InstanceProps,
48
+ type TextProps,
49
+ type SpanProps,
50
+ type ParagraphProps,
51
+ type StyleProps,
52
+ type PaintProp,
53
+ isTreeNode,
54
+ node,
55
+ resolveToTree
56
+ } from './tree'
57
+
58
+ export type { RenderResult } from './renderer'
59
+
60
+ export {
61
+ backgroundBlur,
62
+ dropShadow,
63
+ foregroundBlur,
64
+ innerShadow,
65
+ layerBlur,
66
+ type BlurEffectOptions,
67
+ type EffectColor,
68
+ type ShadowEffectOptions
69
+ } from './effects'
70
+
71
+ export {
72
+ angularGradient,
73
+ diamondGradient,
74
+ gradient,
75
+ linearGradient,
76
+ radialGradient,
77
+ shader,
78
+ shaderSource,
79
+ solid,
80
+ type GradientPaintOptions,
81
+ type PaintColor,
82
+ type PaintStop,
83
+ type ShaderFill,
84
+ type ShaderSourceOptions,
85
+ type SolidPaintOptions
86
+ } from './paints'
87
+
88
+ export {
89
+ defineMode,
90
+ defineVars,
91
+ designMode,
92
+ designVar,
93
+ isMode,
94
+ isVariable,
95
+ isVariableInput,
96
+ type DesignMode,
97
+ type DesignVariable,
98
+ type VariableInput
99
+ } from './variables'
100
+ export { isComponentProperty, property, type ComponentProperty } from './component-properties'
101
+ export { isLayerRef, ref, type LayerRef } from './refs'
102
+
103
+ export { createElement } from './mini-react'
104
+
105
+ export { buildComponent, createDesignJSXRenderer, type DesignJSXRenderer } from './render'
106
+ export type { ArtworkPlacement, DesignJSXServices, SVGSource } from './services'
107
+ export type { RenderOptions } from './types'
108
+ export {
109
+ DESIGN_JSX_ELEMENTS,
110
+ elementPropsByName,
111
+ type DesignJSXElementDefinition
112
+ } from './elements'
113
+ export { DESIGN_JSX_HELPERS, designJSXHelpers, type DesignJSXHelperName } from './helpers'
114
+ export {
115
+ DESIGN_JSX_PROPERTIES,
116
+ DESIGN_JSX_SUPPORTED_PROPERTIES,
117
+ DESIGN_JSX_SUPPORTED_PROPERTY_NAMES,
118
+ DESIGN_JSX_PROPERTY_ALIASES,
119
+ DESIGN_JSX_PARAGRAPH_PROPERTY_NAMES,
120
+ DESIGN_JSX_SPAN_PROPERTY_NAMES,
121
+ DESIGN_JSX_STYLE_KEYS,
122
+ DESIGN_JSX_TEXT_PARAGRAPH_PROPERTY_NAMES,
123
+ type DesignJSXStyleKey,
124
+ designJSXProp,
125
+ designJSXPropertyNames,
126
+ type DesignJSXHelperDefinition,
127
+ type DesignJSXPropertyDefinition
128
+ } from './definitions'
129
+ export {
130
+ transformDesignJSXExpression,
131
+ transformDesignJSXProgram,
132
+ type DesignJSXProgram
133
+ } from './transform'
134
+
135
+ export {
136
+ designJSXElement,
137
+ sceneNodeAttributes,
138
+ sceneNodeToJSX,
139
+ selectionToJSX,
140
+ selectionToJSXWithLayers,
141
+ type DesignJSXElement,
142
+ type DesignJSXWithLayers,
143
+ type JSXAttributeSource,
144
+ type JSXExportOptions
145
+ } from './export'
146
+ export { parseJSXAttributes } from './attributes'
147
+ export { jsxNodeFields, type JSXNodeFields } from './fields'
148
+ export { reconcileRenderedLayers } from './reconcile'
149
+ export { JSX_REFERENCE, AUTHORING_EXAMPLES, type AuthoringExample } from './reference'
150
+ export {
151
+ createStreamingJSXParser,
152
+ type JSXPreviewNode,
153
+ type JSXPreviewPending,
154
+ type JSXPreviewSnapshot
155
+ } from './streaming'
@@ -0,0 +1 @@
1
+ export { Fragment, jsxDEV, type JSX } from './jsx-runtime'
@@ -0,0 +1,51 @@
1
+ import {
2
+ FRAGMENT,
3
+ node,
4
+ type BaseProps,
5
+ type EllipseProps,
6
+ type ParagraphProps,
7
+ type SpanProps,
8
+ type TreeNode,
9
+ type TextProps
10
+ } from './tree'
11
+
12
+ export function jsx(type: string | ((props: BaseProps) => TreeNode), props: BaseProps): TreeNode {
13
+ if (typeof type === 'function') {
14
+ return type(props)
15
+ }
16
+ return node(type, props)
17
+ }
18
+
19
+ export const jsxs = jsx
20
+ export const jsxDEV = jsx
21
+
22
+ /** Groups siblings the way `<>…</>` does in `renderJSX` strings: a node with an empty type. */
23
+ export function Fragment({ children }: { children?: unknown }): TreeNode {
24
+ return node(FRAGMENT, { children })
25
+ }
26
+
27
+ export namespace JSX {
28
+ export type Element = TreeNode
29
+
30
+ export interface IntrinsicElements {
31
+ frame: BaseProps
32
+ text: TextProps
33
+ 'text-span': SpanProps
34
+ 'text-paragraph': ParagraphProps
35
+ rectangle: BaseProps
36
+ ellipse: BaseProps & EllipseProps
37
+ line: BaseProps
38
+ star: BaseProps & { points?: number; innerRadius?: number }
39
+ polygon: BaseProps & { pointCount?: number }
40
+ vector: BaseProps
41
+ group: BaseProps
42
+ section: BaseProps
43
+ component: BaseProps
44
+ 'component-set': BaseProps
45
+ instance: BaseProps & { component?: string; componentId?: string; of?: string }
46
+ }
47
+
48
+ export interface ElementChildrenAttribute {
49
+ children: unknown
50
+ }
51
+ }
@@ -0,0 +1,23 @@
1
+ export type ComponentType = (props: Record<string, unknown>) => ReactNode
2
+
3
+ export type ReactElement = {
4
+ type: string | ComponentType
5
+ props: Record<string, unknown> & { children?: ReactNode[] }
6
+ }
7
+
8
+ export type ReactNode = ReactElement | string | number | null | undefined | ReactNode[]
9
+
10
+ export function createElement(
11
+ type: string | ComponentType,
12
+ props: Record<string, unknown> | null,
13
+ ...children: ReactNode[]
14
+ ): ReactElement {
15
+ const flatChildren = children.flat()
16
+ return {
17
+ type,
18
+ props: {
19
+ ...props,
20
+ children: flatChildren.length > 0 ? flatChildren : undefined
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,39 @@
1
+ import type { Effect, SceneNode } from '@pairdraw/scene-graph'
2
+ import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
3
+ import { parseCSSShadows } from '@pairdraw/scene-graph/css'
4
+
5
+ function isEffect(value: unknown): value is Effect {
6
+ return (
7
+ value !== null &&
8
+ typeof value === 'object' &&
9
+ 'type' in value &&
10
+ 'radius' in value &&
11
+ 'visible' in value
12
+ )
13
+ }
14
+
15
+ /** `effects` takes structured effects; `shadow` appends its shadows and `blur` a layer blur. */
16
+ export function applyEffectOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {
17
+ if (Array.isArray(props.effects)) {
18
+ const effects = props.effects.filter(isEffect).map((effect) => structuredClone(effect))
19
+ if (effects.length > 0) o.effects = effects
20
+ }
21
+
22
+ // A CSS shadow list, as `box-shadow` takes it.
23
+ const shadows = typeof props.shadow === 'string' ? parseCSSShadows(props.shadow) : []
24
+ if (shadows.length > 0) o.effects = [...(o.effects ?? []), ...shadows]
25
+
26
+ if (typeof props.blur === 'number') {
27
+ o.effects = [
28
+ ...(o.effects ?? []),
29
+ {
30
+ type: 'LAYER_BLUR',
31
+ radius: props.blur,
32
+ visible: true,
33
+ color: { ...TRANSPARENT },
34
+ offset: { x: 0, y: 0 },
35
+ spread: 0
36
+ }
37
+ ]
38
+ }
39
+ }
@@ -0,0 +1,56 @@
1
+ import * as v from 'valibot'
2
+
3
+ import type { Fill, GradientTransform } from '@pairdraw/scene-graph'
4
+ import { colorToFill } from '@pairdraw/scene-graph/color'
5
+ import { paintTransformSchema } from '@pairdraw/scene-graph/paints'
6
+ import type { Color } from '@pairdraw/scene-graph/primitives'
7
+
8
+ export function isColor(value: unknown): value is Color {
9
+ return (
10
+ value !== null &&
11
+ typeof value === 'object' &&
12
+ 'r' in value &&
13
+ 'g' in value &&
14
+ 'b' in value &&
15
+ 'a' in value
16
+ )
17
+ }
18
+
19
+ function isFill(value: unknown): value is Fill {
20
+ return (
21
+ value !== null &&
22
+ typeof value === 'object' &&
23
+ 'type' in value &&
24
+ 'color' in value &&
25
+ 'visible' in value
26
+ )
27
+ }
28
+
29
+ /** A paint as props write it: a colour string, a colour, or a paint from a helper. */
30
+ export function isFillValue(value: unknown): value is string | Color | Fill {
31
+ return typeof value === 'string' || isColor(value) || isFill(value)
32
+ }
33
+
34
+ const TRANSFORM_FIELDS = ['gradientTransform', 'imageTransform'] as const
35
+
36
+ /** Worded as `parseScriptInput` words it; that lives in `schema.ts`, which imports this module. */
37
+ function parseTransform(
38
+ field: (typeof TRANSFORM_FIELDS)[number],
39
+ value: unknown
40
+ ): GradientTransform {
41
+ const result = v.safeParse(paintTransformSchema, value)
42
+ if (result.success) return result.output
43
+ throw new Error(
44
+ `A paint's ${field} expects Figma's [[a, b, c], [d, e, f]] or { m00, m01, m02, m10, m11, m12 }:\n${v.summarize(result.issues)}`
45
+ )
46
+ }
47
+
48
+ export function fillFromValue(value: string | Color | Fill): Fill {
49
+ if (!isFill(value)) return colorToFill(value)
50
+ const fill = structuredClone(value)
51
+ // A paint written as an object, as export writes an image, gives its transforms as Figma's rows.
52
+ for (const field of TRANSFORM_FIELDS) {
53
+ if (fill[field] !== undefined) fill[field] = parseTransform(field, fill[field])
54
+ }
55
+ return fill
56
+ }
@@ -0,0 +1,55 @@
1
+ import { CONSTRAINT, MASK_TYPE } from '#design-jsx/enums'
2
+
3
+ import type { ConstraintType, SceneNode } from '@pairdraw/scene-graph'
4
+
5
+ function constraintValue(value: unknown): ConstraintType | undefined {
6
+ return CONSTRAINT.parse(value)
7
+ }
8
+
9
+ /** The `constraints` prop, shaped like Figma's `constraints` object. */
10
+ interface ConstraintsValue {
11
+ horizontal?: unknown
12
+ vertical?: unknown
13
+ }
14
+
15
+ function isConstraintsValue(value: unknown): value is ConstraintsValue {
16
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
17
+ }
18
+
19
+ function applyConstraints(value: unknown, o: Partial<SceneNode>): void {
20
+ if (!isConstraintsValue(value)) return
21
+ const h = constraintValue(value.horizontal)
22
+ const v = constraintValue(value.vertical)
23
+ if (h) o.horizontalConstraint = h
24
+ if (v) o.verticalConstraint = v
25
+ }
26
+
27
+ function sizeLimit(value: unknown): number | undefined {
28
+ return typeof value === 'number' && Number.isFinite(value) ? value : undefined
29
+ }
30
+
31
+ /**
32
+ * Visibility, locking, mask, constraint, and size-limit props. Constraints follow Figma's
33
+ * `constraints` object with its values in lowercase, as `blendMode` is.
34
+ */
35
+ export function applyStateOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {
36
+ if (typeof props.visible === 'boolean') o.visible = props.visible
37
+ if (typeof props.locked === 'boolean') o.locked = props.locked
38
+ applyConstraints(props.constraints, o)
39
+
40
+ const minWidth = sizeLimit(props.minW)
41
+ const maxWidth = sizeLimit(props.maxW)
42
+ const minHeight = sizeLimit(props.minH)
43
+ const maxHeight = sizeLimit(props.maxH)
44
+ if (minWidth !== undefined) o.minWidth = minWidth
45
+ if (maxWidth !== undefined) o.maxWidth = maxWidth
46
+ if (minHeight !== undefined) o.minHeight = minHeight
47
+ if (maxHeight !== undefined) o.maxHeight = maxHeight
48
+
49
+ // `mask` alone masks by alpha; an unknown mask type leaves the layer as it was.
50
+ const maskType = props.mask === true ? 'ALPHA' : MASK_TYPE.parse(props.mask)
51
+ if (maskType) {
52
+ o.isMask = true
53
+ o.maskType = maskType
54
+ }
55
+ }
@@ -0,0 +1,109 @@
1
+ import { designJSXProp } from '#design-jsx/definitions'
2
+ import { STROKE_ALIGN, STROKE_CAP, STROKE_JOIN } from '#design-jsx/enums'
3
+
4
+ import type { SceneNode, Stroke } from '@pairdraw/scene-graph'
5
+ import { parseColor } from '@pairdraw/scene-graph/color'
6
+ import type { Color } from '@pairdraw/scene-graph/primitives'
7
+
8
+ /** One entry of the `strokes` prop; enums are lowercase, as `blendMode` is. */
9
+ export interface StrokeValue {
10
+ color: string | Color
11
+ /** Defaults to the colour's alpha. */
12
+ opacity?: number
13
+ weight?: number
14
+ align?: string
15
+ dash?: number[]
16
+ cap?: string
17
+ join?: string
18
+ visible?: boolean
19
+ }
20
+
21
+ function isColor(value: unknown): value is Color {
22
+ return value !== null && typeof value === 'object' && 'r' in value && 'a' in value
23
+ }
24
+
25
+ export function isStrokeValue(value: unknown): value is StrokeValue {
26
+ if (value === null || typeof value !== 'object' || !('color' in value)) return false
27
+ return typeof value.color === 'string' || isColor(value.color)
28
+ }
29
+
30
+ function dashValue(value: unknown): number[] | undefined {
31
+ if (!Array.isArray(value)) return undefined
32
+ const dash = value.filter((item): item is number => typeof item === 'number')
33
+ return dash.length > 0 ? dash : undefined
34
+ }
35
+
36
+ function toStroke(value: StrokeValue): Stroke {
37
+ const color = typeof value.color === 'string' ? parseColor(value.color) : value.color
38
+ const stroke: Stroke = {
39
+ type: 'SOLID',
40
+ color,
41
+ opacity: typeof value.opacity === 'number' ? value.opacity : color.a,
42
+ visible: value.visible ?? true,
43
+ weight: value.weight ?? 1,
44
+ align: STROKE_ALIGN.parse(value.align) ?? 'INSIDE'
45
+ }
46
+ const dash = dashValue(value.dash)
47
+ if (dash) stroke.dashPattern = dash
48
+ const cap = STROKE_CAP.parse(value.cap)
49
+ if (cap) stroke.cap = cap
50
+ const join = STROKE_JOIN.parse(value.join)
51
+ if (join) stroke.join = join
52
+ return stroke
53
+ }
54
+
55
+ /** `strokeWeights` names each side; a side it leaves out has no stroke. */
56
+ interface StrokeWeights {
57
+ top?: unknown
58
+ right?: unknown
59
+ bottom?: unknown
60
+ left?: unknown
61
+ }
62
+
63
+ function isStrokeWeights(value: unknown): value is StrokeWeights {
64
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
65
+ }
66
+
67
+ function sideWeight(value: unknown): number {
68
+ return typeof value === 'number' ? value : 0
69
+ }
70
+
71
+ function applyStrokeWeights(value: unknown, o: Partial<SceneNode>): void {
72
+ if (!isStrokeWeights(value)) return
73
+ o.independentStrokeWeights = true
74
+ o.borderTopWeight = sideWeight(value.top)
75
+ o.borderRightWeight = sideWeight(value.right)
76
+ o.borderBottomWeight = sideWeight(value.bottom)
77
+ o.borderLeftWeight = sideWeight(value.left)
78
+ }
79
+
80
+ /** `strokes` takes structured strokes; `stroke` and its sibling props describe a single one. */
81
+ export function applyStrokeOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {
82
+ applyStrokeWeights(props.strokeWeights, o)
83
+ // Node-level cap and join, which strokes without their own use.
84
+ const nodeCap = STROKE_CAP.parse(props.strokeCap)
85
+ if (nodeCap) o.strokeCap = nodeCap
86
+ const nodeJoin = STROKE_JOIN.parse(props.strokeJoin)
87
+ if (nodeJoin) o.strokeJoin = nodeJoin
88
+ // The node's own dash pattern, separate from the stroke-local dash `strokeDash` sets.
89
+ const nodeDash = dashValue(props.dashPattern)
90
+ if (nodeDash) o.dashPattern = nodeDash
91
+
92
+ if (Array.isArray(props.strokes)) {
93
+ const strokes = props.strokes.filter(isStrokeValue).map(toStroke)
94
+ if (strokes.length > 0) o.strokes = strokes
95
+ return
96
+ }
97
+
98
+ const color = designJSXProp(props, 'stroke')
99
+ if (typeof color !== 'string' && !isColor(color)) return
100
+ o.strokes = [
101
+ toStroke({
102
+ color,
103
+ weight:
104
+ (props.strokeWidth as number | undefined) ?? (props.borderWidth as number | undefined),
105
+ align: props.strokeAlign as string | undefined,
106
+ dash: props.strokeDash as number[] | undefined
107
+ })
108
+ ]
109
+ }