@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,316 @@
1
+ import { compact, countBy } from 'es-toolkit/array'
2
+ import { camelCase } from 'es-toolkit/string'
3
+ import type * as v from 'valibot'
4
+
5
+ import { es } from '@pairdraw/emit'
6
+ import type { ConnectorEndpoint, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
7
+ import { colorToHex } from '@pairdraw/scene-graph/color'
8
+ import {
9
+ boundEndpoint,
10
+ CONNECTOR_COLOR,
11
+ CONNECTOR_NAME,
12
+ CONNECTOR_STROKE_WEIGHT,
13
+ connectorRouteEnds,
14
+ connectorStroke,
15
+ connectorSublayers,
16
+ createConnector,
17
+ freeEndpoint
18
+ } from '@pairdraw/scene-graph/connectors'
19
+ import { isGeneratedFrame } from '@pairdraw/scene-graph/generated'
20
+ import type { Vector } from '@pairdraw/scene-graph/primitives'
21
+
22
+ import { CONNECTOR_LINE, CONNECTOR_SIDE, STROKE_CAP } from './enums'
23
+ import { Reference } from './export/value'
24
+ import { isLayerRef, type LayerRefs } from './refs'
25
+ import { connectorPropsSchema, readElementProps } from './schema'
26
+ import { plainTextOf, type TreeNode } from './tree'
27
+
28
+ /**
29
+ * `<Connector from="A" to="B">`: a FigJam connector whose ends name other layers of the same
30
+ * JSX, or give `{ x, y }` points in its parent. Ends resolve once the whole tree is rendered, and
31
+ * the connector routes once it is laid out.
32
+ */
33
+ export const CONNECTOR_ELEMENT = 'connector'
34
+
35
+ type ConnectorPropValues = v.InferOutput<typeof connectorPropsSchema>
36
+
37
+ /**
38
+ * Makes the connector an element describes, with `layer`'s fields, its ends free until
39
+ * `resolveConnectorEnds`.
40
+ */
41
+ export function renderConnectorNode(
42
+ graph: SceneGraph,
43
+ tree: TreeNode,
44
+ parentId: string,
45
+ layer: Partial<SceneNode>
46
+ ) {
47
+ const props = readElementProps(connectorPropsSchema, tree.props)
48
+ const parent = graph.getNode(parentId)
49
+ const connector = createConnector(
50
+ graph,
51
+ parentId,
52
+ {
53
+ start: freeEndpoint(0, 0),
54
+ end: freeEndpoint(0, 0),
55
+ lineType: props.line,
56
+ startCap: props.startCap ?? 'NONE',
57
+ endCap: props.endCap ?? 'ARROW_LINES',
58
+ text: plainTextOf(tree)
59
+ },
60
+ {
61
+ ...layer,
62
+ // A connector is drawn between layers, not placed by its parent's auto layout.
63
+ ...(parent && parent.layoutMode !== 'NONE' && { layoutPositioning: 'ABSOLUTE' as const })
64
+ }
65
+ )
66
+ const lineLayer = connectorSublayers(graph, connector)?.line
67
+ if (lineLayer) styleLine(graph, lineLayer, props)
68
+ return connector
69
+ }
70
+
71
+ /** The layer a connector's variable binding goes on: its line for the stroke, else itself. */
72
+ export function connectorBindingTarget(graph: SceneGraph, connector: SceneNode, field: string) {
73
+ const onLine = field.startsWith('strokes/') || field === 'strokeWeight'
74
+ return (onLine && connectorSublayers(graph, connector)?.line.id) || connector.id
75
+ }
76
+
77
+ /** Restyles the line by `stroke`, `strokeWidth`, and `dashPattern`; each left out keeps FigJam's. */
78
+ function styleLine(graph: SceneGraph, line: SceneNode, props: ConnectorPropValues) {
79
+ const [stroke = connectorStroke()] = line.strokes
80
+ const color = props.stroke ?? stroke.color
81
+ const weight = props.strokeWidth ?? stroke.weight
82
+ const dashPattern = props.dashPattern ?? line.dashPattern
83
+ graph.updateNode(line.id, {
84
+ // The stroke draws with its own dash, and files keep the line's.
85
+ strokes: [{ ...stroke, color, weight, dashPattern: [...dashPattern] }],
86
+ strokeWeight: weight,
87
+ dashPattern
88
+ })
89
+ }
90
+
91
+ /**
92
+ * The layers JSX writes under `rootIds`, in order, each before its descendants: the layers a
93
+ * connector or sticky note generates are not, so an end never names them.
94
+ */
95
+ export function layersUnder(graph: SceneGraph, rootIds: readonly string[]): SceneNode[] {
96
+ const layers: SceneNode[] = []
97
+ const visit = (id: string) => {
98
+ const node = graph.getNode(id)
99
+ if (!node) return
100
+ layers.push(node)
101
+ if (!isGeneratedFrame(node)) for (const childId of node.childIds) visit(childId)
102
+ }
103
+ for (const id of rootIds) visit(id)
104
+ return layers
105
+ }
106
+
107
+ /** A connector end's prop as a warning quotes it. */
108
+ function endSource(prop: string, value: unknown): string {
109
+ if (isLayerRef(value)) return `${prop}={ref}`
110
+ return `${prop}="${String(value)}"`
111
+ }
112
+
113
+ /**
114
+ * Binds the ends of connectors rendered from `trees` to the layers they refer to: a `ref()` a
115
+ * layer was marked with, or a name among `rootIds`' layers, the first of a name winning. Returns
116
+ * warnings for ends that refer to no layer, and for names that more than one layer has.
117
+ */
118
+ export function resolveConnectorEnds(
119
+ graph: SceneGraph,
120
+ rootIds: readonly string[],
121
+ connectors: ReadonlyArray<{ node: SceneNode; tree: TreeNode }>,
122
+ refs: LayerRefs
123
+ ): string[] {
124
+ if (connectors.length === 0) return []
125
+ const named = new Map<string, string[]>()
126
+ for (const layer of layersUnder(graph, rootIds)) {
127
+ if (layer.type === 'CONNECTOR') continue
128
+ const ids = named.get(layer.name)
129
+ if (ids) ids.push(layer.id)
130
+ else named.set(layer.name, [layer.id])
131
+ }
132
+ const warnings: string[] = []
133
+ const endpoint = (
134
+ prop: 'from' | 'to',
135
+ props: ConnectorPropValues,
136
+ written: TreeNode['props'],
137
+ straight: boolean
138
+ ) => {
139
+ const value = props[prop]
140
+ let id: string | undefined
141
+ if (isLayerRef(value)) id = refs.layerOf(value)
142
+ else if (typeof value === 'object') return freeEndpoint(value.x, value.y)
143
+ else if (typeof value === 'string') {
144
+ const ids = named.get(value) ?? []
145
+ if (ids.length > 1) {
146
+ warnings.push(
147
+ `<Connector> ${endSource(prop, value)} names ${ids.length} layers and joins the first; mark the one to join with ref().`
148
+ )
149
+ }
150
+ id = ids.at(0)
151
+ }
152
+ if (!id) {
153
+ const missing = isLayerRef(value) ? 'refers to no layer' : 'names no layer'
154
+ warnings.push(`<Connector> ${endSource(prop, written[prop])} ${missing}.`)
155
+ return null
156
+ }
157
+ const side = props[prop === 'from' ? 'fromSide' : 'toSide']
158
+ return boundEndpoint(id, straight ? 'CENTER' : (side ?? 'AUTO'))
159
+ }
160
+ for (const { node, tree } of connectors) {
161
+ const straight = node.connectorLineType === 'STRAIGHT'
162
+ const props = readElementProps(connectorPropsSchema, tree.props)
163
+ const start = endpoint('from', props, tree.props, straight)
164
+ const end = endpoint('to', props, tree.props, straight)
165
+ graph.updateNode(node.id, {
166
+ ...(start && { connectorStart: start }),
167
+ ...(end && { connectorEnd: end })
168
+ })
169
+ }
170
+ return warnings
171
+ }
172
+
173
+ /** `card1` for a layer named "Card": a name a declaration can take, numbered to stay unique. */
174
+ function refName(layerName: string, taken: Set<string>): string {
175
+ const camel = camelCase(layerName)
176
+ const base = es.isIdentifierName(camel) ? camel : 'layer'
177
+ let count = 1
178
+ while (taken.has(`${base}${count}`)) count++
179
+ const name = `${base}${count}`
180
+ taken.add(name)
181
+ return name
182
+ }
183
+
184
+ /** What an export of connectors writes: the layers under its roots, and how ends name them. */
185
+ export interface ConnectorExportScope {
186
+ roots: ReadonlySet<string>
187
+ /**
188
+ * The layers connector ends join whose name another layer of the export has too, so the name
189
+ * would not say which: each with the `ref()` the export declares for it, or `null` when the
190
+ * export declares nothing and such an end is written as the point it is drawn at.
191
+ */
192
+ refs: ReadonlyMap<string, string | null>
193
+ }
194
+
195
+ /**
196
+ * Plans how the connectors under `rootIds` write their ends. With `declare`, a joined layer
197
+ * whose name is not unique gets a `ref()` the export declares before its JSX.
198
+ */
199
+ export function connectorExportScope(
200
+ graph: SceneGraph,
201
+ rootIds: readonly string[],
202
+ declare: boolean
203
+ ): ConnectorExportScope {
204
+ const layers = layersUnder(graph, rootIds)
205
+ const connectors = layers.filter((layer) => layer.type === 'CONNECTOR')
206
+ const refs = new Map<string, string | null>()
207
+ if (connectors.length === 0) return { roots: new Set(rootIds), refs }
208
+ const joined = new Set(
209
+ connectors.flatMap((connector) => [
210
+ connector.connectorStart?.nodeId,
211
+ connector.connectorEnd?.nodeId
212
+ ])
213
+ )
214
+ const named = countBy(
215
+ layers.filter((layer) => layer.type !== 'CONNECTOR'),
216
+ (layer) => layer.name
217
+ )
218
+ const taken = new Set<string>()
219
+ for (const layer of layers) {
220
+ if (!joined.has(layer.id) || (named[layer.name] ?? 0) < 2) continue
221
+ refs.set(layer.id, declare ? refName(layer.name, taken) : null)
222
+ }
223
+ return { roots: new Set(rootIds), refs }
224
+ }
225
+
226
+ /** `const card1 = ref()` for each ref the scope declares, one per line. */
227
+ export function connectorRefDeclarations(scope: ConnectorExportScope): string {
228
+ return compact([...scope.refs.values()])
229
+ .map((name) => `const ${name} = ref()`)
230
+ .join('\n')
231
+ }
232
+
233
+ /** How a connector end is written: by its layer's name or ref, or as the point it is drawn at. */
234
+ type EndTarget = { layer: SceneNode; ref?: string } | { point: Vector }
235
+
236
+ function endTarget(
237
+ graph: SceneGraph,
238
+ endpoint: ConnectorEndpoint,
239
+ drawn: Vector | undefined,
240
+ scope: ConnectorExportScope | undefined
241
+ ): EndTarget {
242
+ const layer = endpoint.nodeId ? graph.getNode(endpoint.nodeId) : undefined
243
+ if (!layer) return { point: endpoint.position }
244
+ const exported =
245
+ !scope || graph.closest(layer.id, (ancestor) => scope.roots.has(ancestor.id)) !== undefined
246
+ const ref = scope?.refs.get(layer.id)
247
+ if (exported && ref !== null) return { layer, ref }
248
+ return { point: drawn ?? endpoint.position }
249
+ }
250
+
251
+ /**
252
+ * A connector's props as `<Connector>` writes them, its ends by the names of their layers, or by
253
+ * the refs `scope` declares for layers whose names are not unique. An end on a layer outside the
254
+ * export, or one the scope gives no ref, is written as the point it is drawn at.
255
+ */
256
+ export function connectorProps(
257
+ graph: SceneGraph,
258
+ node: SceneNode,
259
+ scope?: ConnectorExportScope
260
+ ): Array<[string, unknown]> {
261
+ const drawn = connectorRouteEnds(graph, node)
262
+ const target = (endpoint: ConnectorEndpoint | null, at: Vector | undefined) =>
263
+ endpoint ? endTarget(graph, endpoint, at, scope) : null
264
+ const start = target(node.connectorStart, drawn?.start)
265
+ const end = target(node.connectorEnd, drawn?.end)
266
+ const value = (written: EndTarget | null) => {
267
+ if (!written) return null
268
+ if ('point' in written) return { x: written.point.x, y: written.point.y }
269
+ return written.ref ? new Reference(written.ref) : written.layer.name
270
+ }
271
+ const side = (endpoint: ConnectorEndpoint | null, written: EndTarget | null) => {
272
+ if (!endpoint || !written || 'point' in written) return undefined
273
+ const magnet = endpoint.magnet
274
+ if (magnet !== 'TOP' && magnet !== 'RIGHT' && magnet !== 'BOTTOM' && magnet !== 'LEFT') {
275
+ return undefined
276
+ }
277
+ return CONNECTOR_SIDE.print(magnet)
278
+ }
279
+ const props: Array<[string, unknown]> = [
280
+ ['name', node.name === CONNECTOR_NAME ? undefined : node.name],
281
+ ['from', value(start)],
282
+ ['to', value(end)],
283
+ ['fromSide', side(node.connectorStart, start)],
284
+ ['toSide', side(node.connectorEnd, end)],
285
+ [
286
+ 'line',
287
+ node.connectorLineType === 'ELBOWED'
288
+ ? undefined
289
+ : CONNECTOR_LINE.print(node.connectorLineType)
290
+ ],
291
+ [
292
+ 'startCap',
293
+ node.connectorStartCap === 'NONE' ? undefined : STROKE_CAP.print(node.connectorStartCap)
294
+ ],
295
+ [
296
+ 'endCap',
297
+ node.connectorEndCap === 'ARROW_LINES' ? undefined : STROKE_CAP.print(node.connectorEndCap)
298
+ ]
299
+ ]
300
+ const line = connectorSublayers(graph, node)?.line
301
+ const stroke = line?.strokes[0]
302
+ if (stroke && colorToHex(stroke.color) !== colorToHex(CONNECTOR_COLOR)) {
303
+ props.push(['stroke', colorToHex(stroke.color)])
304
+ }
305
+ if (stroke && stroke.weight !== CONNECTOR_STROKE_WEIGHT)
306
+ props.push(['strokeWidth', stroke.weight])
307
+ if (line && line.dashPattern.length > 0) props.push(['dashPattern', [...line.dashPattern]])
308
+ if (node.opacity < 1) props.push(['opacity', Math.round(node.opacity * 100) / 100])
309
+ if (!node.visible) props.push(['visible', false])
310
+ return props.filter(([, value]) => value !== undefined && value !== null)
311
+ }
312
+
313
+ /** The label a connector writes as its children. */
314
+ export function connectorLabel(graph: SceneGraph, node: SceneNode): string {
315
+ return connectorSublayers(graph, node)?.label.text ?? ''
316
+ }
@@ -0,0 +1,277 @@
1
+ type DesignJSXNamedDefinition = {
2
+ name: string
3
+ description: string
4
+ }
5
+
6
+ export type DesignJSXPropertyDefinition = DesignJSXNamedDefinition
7
+ export type DesignJSXHelperDefinition = DesignJSXNamedDefinition
8
+
9
+ /** Props only `<Paragraph>` takes; like every element, it also takes `key`. */
10
+ export const DESIGN_JSX_PARAGRAPH_PROPERTY_NAMES = [
11
+ 'list',
12
+ 'level',
13
+ 'spacing',
14
+ 'listSpacing',
15
+ 'indent'
16
+ ] as const
17
+
18
+ /** Props `<Span>` takes: `<Text>`'s character styles, under the same names, and a link. */
19
+ export const DESIGN_JSX_SPAN_PROPERTY_NAMES = [
20
+ 'style',
21
+ 'size',
22
+ 'fontSize',
23
+ 'font',
24
+ 'fontFamily',
25
+ 'weight',
26
+ 'fontWeight',
27
+ 'italic',
28
+ 'color',
29
+ 'fill',
30
+ 'fills',
31
+ 'lineHeight',
32
+ 'letterSpacing',
33
+ 'textDecoration',
34
+ 'textDecorationStyle',
35
+ 'textDecorationColor',
36
+ 'textDecorationThickness',
37
+ 'textUnderlineOffset',
38
+ 'textDecorationSkipInk',
39
+ 'fontFeatures',
40
+ 'fontVariations',
41
+ 'lang',
42
+ 'textCase',
43
+ 'href'
44
+ ] as const
45
+
46
+ /** Props `<Text>` sets every paragraph's spacing with; a `<Paragraph>` sets its own. */
47
+ export const DESIGN_JSX_TEXT_PARAGRAPH_PROPERTY_NAMES = [
48
+ 'paragraphSpacing',
49
+ 'listSpacing',
50
+ 'paragraphIndent',
51
+ 'hangingList'
52
+ ] as const
53
+
54
+ export const DESIGN_JSX_SUPPORTED_PROPERTY_NAMES = [
55
+ 'name',
56
+ 'key',
57
+ 'ref',
58
+ 'flex',
59
+ 'flow',
60
+ 'dir',
61
+ 'gap',
62
+ 'wrap',
63
+ 'rowGap',
64
+ 'columnGap',
65
+ 'justify',
66
+ 'justifyContent',
67
+ 'items',
68
+ 'align',
69
+ 'alignItems',
70
+ 'grow',
71
+ 'w',
72
+ 'h',
73
+ 'width',
74
+ 'height',
75
+ 'minW',
76
+ 'maxW',
77
+ 'minH',
78
+ 'maxH',
79
+ 'x',
80
+ 'y',
81
+ 'top',
82
+ 'left',
83
+ 'position',
84
+ 'constraints',
85
+ 'p',
86
+ 'padding',
87
+ 'px',
88
+ 'py',
89
+ 'pt',
90
+ 'pr',
91
+ 'pb',
92
+ 'pl',
93
+ 'bg',
94
+ 'fill',
95
+ 'fills',
96
+ 'background',
97
+ 'backgroundColor',
98
+ 'stroke',
99
+ 'border',
100
+ 'borderColor',
101
+ 'strokeWidth',
102
+ 'borderWidth',
103
+ 'strokeAlign',
104
+ 'strokeDash',
105
+ 'strokeCap',
106
+ 'strokeJoin',
107
+ 'dashPattern',
108
+ 'strokes',
109
+ 'strokeWeights',
110
+ 'rounded',
111
+ 'borderRadius',
112
+ 'roundedTL',
113
+ 'roundedTR',
114
+ 'roundedBL',
115
+ 'roundedBR',
116
+ 'cornerRadius',
117
+ 'cornerSmoothing',
118
+ 'opacity',
119
+ 'blendMode',
120
+ 'rotate',
121
+ 'rotation',
122
+ 'flipX',
123
+ 'flipY',
124
+ 'overflow',
125
+ 'mask',
126
+ 'visible',
127
+ 'locked',
128
+ 'shadow',
129
+ 'blur',
130
+ 'effects',
131
+ 'size',
132
+ 'fontSize',
133
+ 'font',
134
+ 'fontFamily',
135
+ 'weight',
136
+ 'fontWeight',
137
+ 'italic',
138
+ 'color',
139
+ 'text',
140
+ 'characters',
141
+ 'content',
142
+ 'value',
143
+ 'title',
144
+ 'textAlign',
145
+ 'textAlignHorizontal',
146
+ 'textHorizontalAlignment',
147
+ 'textAlignVertical',
148
+ 'textVerticalAlignment',
149
+ 'textAutoResize',
150
+ 'lineHeight',
151
+ 'letterSpacing',
152
+ 'textDecoration',
153
+ 'textDecorationStyle',
154
+ 'textDecorationColor',
155
+ 'textDecorationThickness',
156
+ 'textUnderlineOffset',
157
+ 'textDecorationSkipInk',
158
+ 'fontFeatures',
159
+ 'fontVariations',
160
+ 'lang',
161
+ 'fontOpticalSizing',
162
+ 'textCase',
163
+ 'paragraphSpacing',
164
+ 'paragraphIndent',
165
+ 'hangingList',
166
+ 'list',
167
+ 'level',
168
+ 'spacing',
169
+ 'listSpacing',
170
+ 'indent',
171
+ 'maxLines',
172
+ 'truncate',
173
+ 'grid',
174
+ 'columns',
175
+ 'rows',
176
+ 'colStart',
177
+ 'rowStart',
178
+ 'col',
179
+ 'row',
180
+ 'colSpan',
181
+ 'rowSpan',
182
+ 'points',
183
+ 'pointCount',
184
+ 'innerRadius',
185
+ 'startAngle',
186
+ 'endAngle',
187
+ 'd',
188
+ 'fillRule',
189
+ 'paths',
190
+ 'label',
191
+ 'style',
192
+ 'bind',
193
+ 'component',
194
+ 'componentId',
195
+ 'properties',
196
+ 'of',
197
+ 'exposed',
198
+ 'mode',
199
+ // A Reka UI root's behaviour, by its own property names
200
+ 'modelValue',
201
+ 'open',
202
+ 'disabled',
203
+ 'filled',
204
+ 'states',
205
+ 'min',
206
+ 'max',
207
+ 'step',
208
+ 'defaultValue'
209
+ ] as const
210
+
211
+ export const DESIGN_JSX_SUPPORTED_PROPERTIES = new Set<string>(DESIGN_JSX_SUPPORTED_PROPERTY_NAMES)
212
+
213
+ /**
214
+ * Properties accepted under more than one name, keyed by the name Design JSX writes. The
215
+ * renderer reads the first one set, in this order; editors use it to find a value written
216
+ * under any of its names.
217
+ */
218
+ export const DESIGN_JSX_PROPERTY_ALIASES: Readonly<Record<string, readonly string[]>> = {
219
+ w: ['width'],
220
+ h: ['height'],
221
+ bg: ['fill', 'background', 'backgroundColor'],
222
+ stroke: ['border', 'borderColor'],
223
+ rounded: ['cornerRadius', 'borderRadius'],
224
+ rotate: ['rotation'],
225
+ p: ['padding'],
226
+ colStart: ['col'],
227
+ rowStart: ['row'],
228
+ justify: ['justifyContent'],
229
+ items: ['align', 'alignItems'],
230
+ size: ['fontSize'],
231
+ font: ['fontFamily'],
232
+ weight: ['fontWeight'],
233
+ textAlign: ['textAlignHorizontal', 'textHorizontalAlignment'],
234
+ textAlignVertical: ['textVerticalAlignment']
235
+ }
236
+
237
+ /** A CSS-style key read from the `style` prop; `px` values like `'320px'` become numbers. */
238
+ export interface DesignJSXStyleKey {
239
+ key: string
240
+ px?: boolean
241
+ }
242
+
243
+ /**
244
+ * Properties also read from `style={{ … }}`, keyed by the name Design JSX writes, in the order
245
+ * the renderer reads them. An attribute under any of the property's names wins over `style`.
246
+ */
247
+ export const DESIGN_JSX_STYLE_KEYS: Readonly<Record<string, readonly DesignJSXStyleKey[]>> = {
248
+ bg: [{ key: 'background' }, { key: 'backgroundColor' }],
249
+ color: [{ key: 'color' }],
250
+ stroke: [{ key: 'borderColor' }],
251
+ strokeWidth: [{ key: 'borderWidth', px: true }],
252
+ rounded: [{ key: 'borderRadius', px: true }],
253
+ size: [{ key: 'fontSize', px: true }],
254
+ weight: [{ key: 'fontWeight' }],
255
+ w: [{ key: 'width', px: true }],
256
+ h: [{ key: 'height', px: true }],
257
+ opacity: [{ key: 'opacity' }]
258
+ }
259
+
260
+ /** Every name a property is accepted under, the written name first. */
261
+ export function designJSXPropertyNames(name: string): readonly string[] {
262
+ return [name, ...(DESIGN_JSX_PROPERTY_ALIASES[name] ?? [])]
263
+ }
264
+
265
+ /** The value of a property under the first of its names that is set. */
266
+ export function designJSXProp(props: Readonly<Record<string, unknown>>, name: string): unknown {
267
+ for (const key of designJSXPropertyNames(name)) {
268
+ if (props[key] !== undefined && props[key] !== null) return props[key]
269
+ }
270
+ return undefined
271
+ }
272
+
273
+ export const DESIGN_JSX_PROPERTIES: DesignJSXPropertyDefinition[] =
274
+ DESIGN_JSX_SUPPORTED_PROPERTY_NAMES.map((name) => ({
275
+ name,
276
+ description: `Pairdraw ${name} property.`
277
+ }))
package/src/effects.ts ADDED
@@ -0,0 +1,80 @@
1
+ import type { BlendMode, Effect } from '@pairdraw/scene-graph'
2
+ import { parseColor } from '@pairdraw/scene-graph/color'
3
+ import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
4
+ import type { Color, Vector } from '@pairdraw/scene-graph/primitives'
5
+
6
+ export type EffectColor = string | Color
7
+
8
+ export interface ShadowEffectOptions {
9
+ color?: EffectColor
10
+ x?: number
11
+ y?: number
12
+ offset?: Vector
13
+ radius?: number
14
+ spread?: number
15
+ visible?: boolean
16
+ blendMode?: BlendMode
17
+ showShadowBehindNode?: boolean
18
+ }
19
+
20
+ export interface BlurEffectOptions {
21
+ radius?: number
22
+ visible?: boolean
23
+ }
24
+
25
+ /** Radius of a shadow or blur that does not set one. */
26
+ const DEFAULT_EFFECT_RADIUS = 8
27
+
28
+ function toColor(color: EffectColor | undefined): Color {
29
+ if (color === undefined) return { ...TRANSPARENT }
30
+ return typeof color === 'string' ? parseColor(color) : color
31
+ }
32
+
33
+ function shadowEffect(type: 'DROP_SHADOW' | 'INNER_SHADOW', options: ShadowEffectOptions): Effect {
34
+ return {
35
+ type,
36
+ color: toColor(options.color ?? 'rgba(0, 0, 0, 0.25)'),
37
+ offset: options.offset ?? { x: options.x ?? 0, y: options.y ?? 4 },
38
+ radius: options.radius ?? DEFAULT_EFFECT_RADIUS,
39
+ spread: options.spread ?? 0,
40
+ visible: options.visible ?? true,
41
+ blendMode: options.blendMode,
42
+ showShadowBehindNode: options.showShadowBehindNode
43
+ }
44
+ }
45
+
46
+ function blurEffect(
47
+ type: 'LAYER_BLUR' | 'BACKGROUND_BLUR' | 'FOREGROUND_BLUR',
48
+ radiusOrOptions: number | BlurEffectOptions = {}
49
+ ): Effect {
50
+ const options =
51
+ typeof radiusOrOptions === 'number' ? { radius: radiusOrOptions } : radiusOrOptions
52
+ return {
53
+ type,
54
+ color: { ...TRANSPARENT },
55
+ offset: { x: 0, y: 0 },
56
+ radius: options.radius ?? DEFAULT_EFFECT_RADIUS,
57
+ spread: 0,
58
+ visible: options.visible ?? true
59
+ }
60
+ }
61
+
62
+ export function dropShadow(options: ShadowEffectOptions = {}): Effect {
63
+ return shadowEffect('DROP_SHADOW', options)
64
+ }
65
+
66
+ export function innerShadow(options: ShadowEffectOptions = {}): Effect {
67
+ return shadowEffect('INNER_SHADOW', options)
68
+ }
69
+
70
+ export function layerBlur(radiusOrOptions?: number | BlurEffectOptions): Effect {
71
+ return blurEffect('LAYER_BLUR', radiusOrOptions)
72
+ }
73
+
74
+ export function backgroundBlur(radiusOrOptions?: number | BlurEffectOptions): Effect {
75
+ return blurEffect('BACKGROUND_BLUR', radiusOrOptions)
76
+ }
77
+
78
+ export function foregroundBlur(radiusOrOptions?: number | BlurEffectOptions): Effect {
79
+ return blurEffect('FOREGROUND_BLUR', radiusOrOptions)
80
+ }