@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.
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/chunks/attributes.js +32 -0
- package/dist/chunks/attributes.js.map +1 -0
- package/dist/chunks/behaviours/elements.js +321 -0
- package/dist/chunks/behaviours/elements.js.map +1 -0
- package/dist/chunks/behaviours/render.js +368 -0
- package/dist/chunks/behaviours/render.js.map +1 -0
- package/dist/chunks/component-properties.js +809 -0
- package/dist/chunks/component-properties.js.map +1 -0
- package/dist/chunks/components.js +78 -0
- package/dist/chunks/components.js.map +1 -0
- package/dist/chunks/connectors.js +290 -0
- package/dist/chunks/connectors.js.map +1 -0
- package/dist/chunks/effects.js +57 -0
- package/dist/chunks/effects.js.map +1 -0
- package/dist/chunks/elements.js +618 -0
- package/dist/chunks/elements.js.map +1 -0
- package/dist/chunks/export/behaviours.js +113 -0
- package/dist/chunks/export/behaviours.js.map +1 -0
- package/dist/chunks/export/bindings.js +273 -0
- package/dist/chunks/export/bindings.js.map +1 -0
- package/dist/chunks/export/decoration.js +96 -0
- package/dist/chunks/export/decoration.js.map +1 -0
- package/dist/chunks/export/font.js +17 -0
- package/dist/chunks/export/font.js.map +1 -0
- package/dist/chunks/export/icons.js +34 -0
- package/dist/chunks/export/icons.js.map +1 -0
- package/dist/chunks/export/index.js +1390 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/fields.js +1065 -0
- package/dist/chunks/fields.js.map +1 -0
- package/dist/chunks/index.js +438 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/chunks/jsx-dev-runtime.js +16 -0
- package/dist/chunks/jsx-dev-runtime.js.map +1 -0
- package/dist/index.d.ts +543 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-runtime.d.ts +44 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +2 -0
- package/dist/tree.d.ts +261 -0
- package/dist/tree.d.ts.map +1 -0
- package/package.json +66 -4
- package/src/attributes.ts +30 -0
- package/src/behaviours/elements.ts +47 -0
- package/src/behaviours/index.ts +253 -0
- package/src/behaviours/render.ts +144 -0
- package/src/component-properties.ts +428 -0
- package/src/components.ts +109 -0
- package/src/connectors.ts +316 -0
- package/src/definitions.ts +277 -0
- package/src/effects.ts +80 -0
- package/src/elements.ts +321 -0
- package/src/enums.ts +182 -0
- package/src/export/behaviours.ts +143 -0
- package/src/export/bindings.ts +155 -0
- package/src/export/decoration.ts +39 -0
- package/src/export/font.ts +25 -0
- package/src/export/helpers.ts +75 -0
- package/src/export/icons.ts +42 -0
- package/src/export/index.ts +297 -0
- package/src/export/paint.ts +276 -0
- package/src/export/props.ts +369 -0
- package/src/export/state.ts +52 -0
- package/src/export/text.ts +155 -0
- package/src/export/value.ts +99 -0
- package/src/export/variables.ts +393 -0
- package/src/fields.ts +39 -0
- package/src/figjam-text.ts +37 -0
- package/src/global.d.ts +4 -0
- package/src/helpers.ts +135 -0
- package/src/index.ts +155 -0
- package/src/jsx-dev-runtime.ts +1 -0
- package/src/jsx-runtime.ts +51 -0
- package/src/mini-react.ts +23 -0
- package/src/overrides/effects.ts +39 -0
- package/src/overrides/fills.ts +56 -0
- package/src/overrides/state.ts +55 -0
- package/src/overrides/strokes.ts +109 -0
- package/src/overrides/text.ts +116 -0
- package/src/overrides/vector.ts +122 -0
- package/src/paints.ts +230 -0
- package/src/props-overrides.ts +481 -0
- package/src/reconcile.ts +134 -0
- package/src/reference/authoring.md +76 -0
- package/src/reference/examples.ts +49 -0
- package/src/reference.ts +19 -0
- package/src/refs.ts +48 -0
- package/src/render.ts +147 -0
- package/src/renderer.ts +598 -0
- package/src/scalar-bindings.ts +36 -0
- package/src/schema.ts +213 -0
- package/src/services.ts +36 -0
- package/src/shapes-with-text.ts +73 -0
- package/src/stickies.ts +72 -0
- package/src/streaming/index.ts +47 -0
- package/src/streaming/literals.ts +82 -0
- package/src/streaming/projection.ts +189 -0
- package/src/text-content.ts +233 -0
- package/src/transform.ts +78 -0
- package/src/tree.ts +285 -0
- package/src/types.ts +13 -0
- package/src/variables/graph.ts +265 -0
- package/src/variables/index.ts +124 -0
- package/src/variables/props.ts +77 -0
package/src/elements.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { difference } from 'es-toolkit/array'
|
|
2
|
+
|
|
3
|
+
import type { NodeType, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
4
|
+
|
|
5
|
+
import { REKA_ELEMENTS, rekaRole } from './behaviours'
|
|
6
|
+
import { connectorBindingTarget, renderConnectorNode } from './connectors'
|
|
7
|
+
import {
|
|
8
|
+
DESIGN_JSX_PARAGRAPH_PROPERTY_NAMES,
|
|
9
|
+
DESIGN_JSX_SPAN_PROPERTY_NAMES,
|
|
10
|
+
DESIGN_JSX_SUPPORTED_PROPERTY_NAMES,
|
|
11
|
+
DESIGN_JSX_TEXT_PARAGRAPH_PROPERTY_NAMES
|
|
12
|
+
} from './definitions'
|
|
13
|
+
import {
|
|
14
|
+
connectorPropsSchema,
|
|
15
|
+
ownLayerPropsSchema,
|
|
16
|
+
placedOwnLayerPropsSchema,
|
|
17
|
+
shapeLayerPropsSchema,
|
|
18
|
+
shapePropsSchema,
|
|
19
|
+
stickyPropsSchema,
|
|
20
|
+
type ElementPropsSchema
|
|
21
|
+
} from './schema'
|
|
22
|
+
import { renderShapeNode } from './shapes-with-text'
|
|
23
|
+
import { renderStickyNode } from './stickies'
|
|
24
|
+
import type { TreeNode } from './tree'
|
|
25
|
+
|
|
26
|
+
/** How an element that draws a layer of its own, such as a sticky note, renders it. */
|
|
27
|
+
export interface OwnLayerRenderer {
|
|
28
|
+
/** The layer props it takes, made into fields as any layer's are. */
|
|
29
|
+
layer: ElementPropsSchema
|
|
30
|
+
render(graph: SceneGraph, tree: TreeNode, parentId: string, layer: Partial<SceneNode>): SceneNode
|
|
31
|
+
/** The layer a variable binding of `field` goes on, when not the element's own. */
|
|
32
|
+
bindingTarget?(graph: SceneGraph, node: SceneNode, field: string): string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* One design JSX element: what JSX names it, what it makes, the props it takes, and which layers
|
|
37
|
+
* export writes as it. The renderer, the export, the prop checks, the streaming preview, and the
|
|
38
|
+
* authoring reference all read this list rather than keeping their own.
|
|
39
|
+
*/
|
|
40
|
+
export type DesignJSXElementDefinition = {
|
|
41
|
+
name: string
|
|
42
|
+
/** The type its tree nodes carry, such as `frame`. */
|
|
43
|
+
runtimeType: string
|
|
44
|
+
description: string
|
|
45
|
+
/** The layer a plain element makes; an element rendered its own way, such as `Icon`, has none. */
|
|
46
|
+
nodeType?: NodeType
|
|
47
|
+
/** The props it takes; every layer prop when left out. */
|
|
48
|
+
props?: readonly string[]
|
|
49
|
+
/** The props it takes and what each holds, for an element that reads them by this schema. */
|
|
50
|
+
schema?: ElementPropsSchema
|
|
51
|
+
/** How it draws its own layer, for an element that does. */
|
|
52
|
+
ownLayer?: OwnLayerRenderer
|
|
53
|
+
/** The layers export writes as this element. */
|
|
54
|
+
writes?: readonly NodeType[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function rekaDescription(namespace: string, part: string): string {
|
|
58
|
+
const role = rekaRole(`${namespace}.${part}`)
|
|
59
|
+
if (role?.role === 'root')
|
|
60
|
+
return `A main component that behaves as Reka UI's ${namespace} ${part}.`
|
|
61
|
+
if (role?.role === 'input') return `The text layer of a ${namespace}'s text property.`
|
|
62
|
+
return `The ${namespace} ${part}, drawn by a slot of its component.`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A Reka UI element as the element list describes it. */
|
|
66
|
+
function rekaElementDefinitions(): DesignJSXElementDefinition[] {
|
|
67
|
+
return Object.entries(REKA_ELEMENTS).flatMap(([namespace, parts]) =>
|
|
68
|
+
Object.keys(parts).map((part) => {
|
|
69
|
+
const type = `${namespace}.${part}`
|
|
70
|
+
return { name: type, runtimeType: type, description: rekaDescription(namespace, part) }
|
|
71
|
+
})
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const DESIGN_JSX_ELEMENTS: DesignJSXElementDefinition[] = [
|
|
76
|
+
{
|
|
77
|
+
name: 'Frame',
|
|
78
|
+
runtimeType: 'frame',
|
|
79
|
+
description: 'A frame or auto-layout container.',
|
|
80
|
+
nodeType: 'FRAME',
|
|
81
|
+
// A set and an instance read back as frames: JSX cannot name what they come from.
|
|
82
|
+
writes: ['FRAME', 'COMPONENT_SET', 'INSTANCE']
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'Text',
|
|
86
|
+
runtimeType: 'text',
|
|
87
|
+
description: 'A text layer.',
|
|
88
|
+
nodeType: 'TEXT',
|
|
89
|
+
writes: ['TEXT']
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'Span',
|
|
93
|
+
runtimeType: 'text-span',
|
|
94
|
+
description: 'Characters inside a Text, styled apart from the rest.',
|
|
95
|
+
props: ['key', ...DESIGN_JSX_SPAN_PROPERTY_NAMES]
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'Paragraph',
|
|
99
|
+
runtimeType: 'text-paragraph',
|
|
100
|
+
description: 'One paragraph of a Text, plain or a list item.',
|
|
101
|
+
props: ['key', ...DESIGN_JSX_PARAGRAPH_PROPERTY_NAMES]
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'Rectangle',
|
|
105
|
+
runtimeType: 'rectangle',
|
|
106
|
+
description: 'A rectangle layer.',
|
|
107
|
+
nodeType: 'RECTANGLE',
|
|
108
|
+
writes: ['RECTANGLE', 'ROUNDED_RECTANGLE']
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'Ellipse',
|
|
112
|
+
runtimeType: 'ellipse',
|
|
113
|
+
description: 'An ellipse layer.',
|
|
114
|
+
nodeType: 'ELLIPSE',
|
|
115
|
+
writes: ['ELLIPSE']
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: 'Line',
|
|
119
|
+
runtimeType: 'line',
|
|
120
|
+
description: 'A line layer.',
|
|
121
|
+
nodeType: 'LINE',
|
|
122
|
+
writes: ['LINE']
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: 'Star',
|
|
126
|
+
runtimeType: 'star',
|
|
127
|
+
description: 'A star layer.',
|
|
128
|
+
nodeType: 'STAR',
|
|
129
|
+
writes: ['STAR']
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'Polygon',
|
|
133
|
+
runtimeType: 'polygon',
|
|
134
|
+
description: 'A polygon layer.',
|
|
135
|
+
nodeType: 'POLYGON',
|
|
136
|
+
writes: ['POLYGON']
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: 'Vector',
|
|
140
|
+
runtimeType: 'vector',
|
|
141
|
+
description: 'A vector layer.',
|
|
142
|
+
nodeType: 'VECTOR',
|
|
143
|
+
writes: ['VECTOR']
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'Group',
|
|
147
|
+
runtimeType: 'group',
|
|
148
|
+
description: 'A group of layers.',
|
|
149
|
+
nodeType: 'GROUP',
|
|
150
|
+
writes: ['GROUP']
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: 'Section',
|
|
154
|
+
runtimeType: 'section',
|
|
155
|
+
description: 'A canvas section.',
|
|
156
|
+
nodeType: 'SECTION',
|
|
157
|
+
writes: ['SECTION']
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: 'Component',
|
|
161
|
+
runtimeType: 'component',
|
|
162
|
+
description: 'A reusable component.',
|
|
163
|
+
nodeType: 'COMPONENT',
|
|
164
|
+
writes: ['COMPONENT']
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: 'ComponentSet',
|
|
168
|
+
runtimeType: 'component-set',
|
|
169
|
+
description: 'A component variant set.',
|
|
170
|
+
nodeType: 'COMPONENT_SET'
|
|
171
|
+
},
|
|
172
|
+
{ name: 'Instance', runtimeType: 'instance', description: 'An instance of a component.' },
|
|
173
|
+
{ name: 'View', runtimeType: 'frame', description: 'An alias for Frame.', nodeType: 'FRAME' },
|
|
174
|
+
{
|
|
175
|
+
name: 'Rect',
|
|
176
|
+
runtimeType: 'rectangle',
|
|
177
|
+
description: 'An alias for Rectangle.',
|
|
178
|
+
nodeType: 'RECTANGLE'
|
|
179
|
+
},
|
|
180
|
+
{ name: 'Icon', runtimeType: 'icon', description: 'An Iconify icon.' },
|
|
181
|
+
{
|
|
182
|
+
name: 'Sticky',
|
|
183
|
+
runtimeType: 'sticky',
|
|
184
|
+
description: 'A FigJam sticky note; its text is the body.',
|
|
185
|
+
schema: stickyPropsSchema,
|
|
186
|
+
ownLayer: { layer: placedOwnLayerPropsSchema, render: renderStickyNode },
|
|
187
|
+
writes: ['STICKY']
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: 'Shape',
|
|
191
|
+
runtimeType: 'shape',
|
|
192
|
+
description: 'A FigJam shape with text; its text is the label.',
|
|
193
|
+
schema: shapePropsSchema,
|
|
194
|
+
ownLayer: { layer: shapeLayerPropsSchema, render: renderShapeNode },
|
|
195
|
+
writes: ['SHAPE_WITH_TEXT']
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: 'Connector',
|
|
199
|
+
runtimeType: 'connector',
|
|
200
|
+
description:
|
|
201
|
+
'A FigJam connector between two layers of the same JSX, named by `from` and `to`, or points.',
|
|
202
|
+
schema: connectorPropsSchema,
|
|
203
|
+
ownLayer: {
|
|
204
|
+
layer: ownLayerPropsSchema,
|
|
205
|
+
render: renderConnectorNode,
|
|
206
|
+
bindingTarget: connectorBindingTarget
|
|
207
|
+
},
|
|
208
|
+
writes: ['CONNECTOR']
|
|
209
|
+
},
|
|
210
|
+
...rekaElementDefinitions()
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
/** Props an inline `<svg>` takes besides a layer's: its markup's view box and body. */
|
|
214
|
+
const SVG_PROPERTY_NAMES = ['viewBox', 'body'] as const
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Lowercase tags JSX also accepts, as HTML names them, with the layer each makes; they are
|
|
218
|
+
* not written back.
|
|
219
|
+
*/
|
|
220
|
+
const INTRINSIC_ELEMENTS: Readonly<Record<string, NodeType | null>> = {
|
|
221
|
+
componentset: 'COMPONENT_SET',
|
|
222
|
+
div: 'FRAME',
|
|
223
|
+
main: 'FRAME',
|
|
224
|
+
header: 'FRAME',
|
|
225
|
+
footer: 'FRAME',
|
|
226
|
+
nav: 'FRAME',
|
|
227
|
+
article: 'FRAME',
|
|
228
|
+
aside: 'FRAME',
|
|
229
|
+
span: 'TEXT',
|
|
230
|
+
p: 'TEXT',
|
|
231
|
+
h1: 'TEXT',
|
|
232
|
+
h2: 'TEXT',
|
|
233
|
+
h3: 'TEXT',
|
|
234
|
+
h4: 'TEXT',
|
|
235
|
+
h5: 'TEXT',
|
|
236
|
+
h6: 'TEXT',
|
|
237
|
+
// Inline SVG markup, which the artwork service draws.
|
|
238
|
+
svg: null
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Paragraph props describe a paragraph of a text, never a layer, unless a text sets them too. */
|
|
242
|
+
const LAYER_PROPS: ReadonlySet<string> = new Set<string>(
|
|
243
|
+
difference(
|
|
244
|
+
DESIGN_JSX_SUPPORTED_PROPERTY_NAMES,
|
|
245
|
+
difference(DESIGN_JSX_PARAGRAPH_PROPERTY_NAMES, DESIGN_JSX_TEXT_PARAGRAPH_PROPERTY_NAMES)
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
const BY_RUNTIME_TYPE = new Map(
|
|
250
|
+
DESIGN_JSX_ELEMENTS.map((element): [string, DesignJSXElementDefinition] => [
|
|
251
|
+
element.runtimeType,
|
|
252
|
+
element
|
|
253
|
+
]).reverse()
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
const PROPS_BY_RUNTIME_TYPE = new Map<string, ReadonlySet<string>>([
|
|
257
|
+
['svg', new Set([...LAYER_PROPS, ...SVG_PROPERTY_NAMES])],
|
|
258
|
+
...DESIGN_JSX_ELEMENTS.flatMap((element): [string, ReadonlySet<string>][] => {
|
|
259
|
+
const props = element.schema ? Object.keys(element.schema.entries) : element.props
|
|
260
|
+
return props ? [[element.runtimeType, new Set(props)]] : []
|
|
261
|
+
})
|
|
262
|
+
])
|
|
263
|
+
|
|
264
|
+
const TAG_BY_NODE_TYPE = new Map(
|
|
265
|
+
DESIGN_JSX_ELEMENTS.flatMap((element) =>
|
|
266
|
+
(element.writes ?? []).map((type): [NodeType, string] => [type, element.name])
|
|
267
|
+
)
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
/** The layer an element of `runtimeType` makes, for an element without its own renderer. */
|
|
271
|
+
export function elementNodeType(runtimeType: string): NodeType | undefined {
|
|
272
|
+
return BY_RUNTIME_TYPE.get(runtimeType)?.nodeType ?? INTRINSIC_ELEMENTS[runtimeType] ?? undefined
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The props an element of `runtimeType` takes. */
|
|
276
|
+
export function elementProps(runtimeType: string): ReadonlySet<string> {
|
|
277
|
+
return PROPS_BY_RUNTIME_TYPE.get(runtimeType) ?? LAYER_PROPS
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const BY_NAME = new Map(
|
|
281
|
+
DESIGN_JSX_ELEMENTS.map((element): [string, DesignJSXElementDefinition] => [
|
|
282
|
+
element.name,
|
|
283
|
+
element
|
|
284
|
+
])
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The props an element written `<name>` takes, as the renderer checks them, for an editor that
|
|
289
|
+
* checks them as they are typed; null for a name that is no element, and for an instance, which
|
|
290
|
+
* also takes its component's properties by name.
|
|
291
|
+
*/
|
|
292
|
+
export function elementPropsByName(name: string): ReadonlySet<string> | null {
|
|
293
|
+
const element = BY_NAME.get(name)
|
|
294
|
+
if (!element) return Object.hasOwn(INTRINSIC_ELEMENTS, name) ? elementProps(name) : null
|
|
295
|
+
return element.runtimeType === 'instance' ? null : elementProps(element.runtimeType)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The schema an element of `runtimeType` reads its props by, if it has one. */
|
|
299
|
+
export function elementSchema(runtimeType: string): ElementPropsSchema | undefined {
|
|
300
|
+
return BY_RUNTIME_TYPE.get(runtimeType)?.schema
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** How an element of `runtimeType` draws its own layer, if it does. */
|
|
304
|
+
export function elementOwnLayer(runtimeType: string): OwnLayerRenderer | undefined {
|
|
305
|
+
return BY_RUNTIME_TYPE.get(runtimeType)?.ownLayer
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The element export writes a layer of `type` as, if any. */
|
|
309
|
+
export function elementTag(type: NodeType): string | undefined {
|
|
310
|
+
return TAG_BY_NODE_TYPE.get(type)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* `const Frame = 'frame', …`: the element names a JSX string's code can use, bound to their
|
|
315
|
+
* runtime types. Reka UI elements, named `Switch.Root`, are bound by their namespaces.
|
|
316
|
+
*/
|
|
317
|
+
export function elementBindings(): string {
|
|
318
|
+
return DESIGN_JSX_ELEMENTS.filter((element) => !element.name.includes('.'))
|
|
319
|
+
.map((element) => `const ${element.name} = '${element.runtimeType}'`)
|
|
320
|
+
.join('\n')
|
|
321
|
+
}
|
package/src/enums.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
BlendMode,
|
|
5
|
+
ConnectorLineType,
|
|
6
|
+
ConnectorMagnet,
|
|
7
|
+
ConstraintType,
|
|
8
|
+
MaskType,
|
|
9
|
+
SceneNode,
|
|
10
|
+
Stroke,
|
|
11
|
+
StrokeCap,
|
|
12
|
+
StrokeJoin,
|
|
13
|
+
TextAutoResize,
|
|
14
|
+
TextCase,
|
|
15
|
+
TextDecoration,
|
|
16
|
+
TextDecorationStyle,
|
|
17
|
+
TextDirection
|
|
18
|
+
} from '@pairdraw/scene-graph'
|
|
19
|
+
import type { FigJamTextSize, FigJamTypeface } from '@pairdraw/scene-graph/figjam'
|
|
20
|
+
import {
|
|
21
|
+
SHAPE_WITH_TEXT_TYPES,
|
|
22
|
+
type ShapeWithTextType
|
|
23
|
+
} from '@pairdraw/scene-graph/shape-with-text'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A Figma enum as design JSX spells it. Reading and writing come from one table, so whatever the
|
|
27
|
+
* export writes reads back, and a value the table does not know is reported instead of reaching
|
|
28
|
+
* the document.
|
|
29
|
+
*/
|
|
30
|
+
export interface JSXEnum<T extends string> {
|
|
31
|
+
/** The value a spelling names, read case-insensitively, or undefined for none. */
|
|
32
|
+
parse(value: unknown): T | undefined
|
|
33
|
+
/** How the export spells a value. */
|
|
34
|
+
print(value: T): string
|
|
35
|
+
/** Every spelling it reads, aliases included. */
|
|
36
|
+
readonly names: readonly string[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `spellings` names every value of the enum, which the type checks, and is what the export writes;
|
|
41
|
+
* `aliases` are further spellings it only reads.
|
|
42
|
+
*/
|
|
43
|
+
function jsxEnum<T extends string>(
|
|
44
|
+
spellings: Readonly<Record<T, string>>,
|
|
45
|
+
aliases: Readonly<Record<string, T>> = {}
|
|
46
|
+
): JSXEnum<T> {
|
|
47
|
+
const entries = Object.entries(spellings) as [T, string][]
|
|
48
|
+
const byName = new Map<string, T>(
|
|
49
|
+
[...entries.map(([value, name]): [string, T] => [name, value]), ...Object.entries(aliases)].map(
|
|
50
|
+
([name, value]): [string, T] => [name.toLowerCase(), value]
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
return {
|
|
54
|
+
parse: (value) => (typeof value === 'string' ? byName.get(value.toLowerCase()) : undefined),
|
|
55
|
+
print: (value) => spellings[value],
|
|
56
|
+
names: [...entries.map(([, name]) => name), ...Object.keys(aliases)]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A prop that takes one of the enum's spellings, read as the value it names. */
|
|
61
|
+
export function enumProp<T extends string>(values: JSXEnum<T>) {
|
|
62
|
+
return v.pipe(
|
|
63
|
+
v.string(),
|
|
64
|
+
v.rawTransform(({ dataset, addIssue, NEVER }) => {
|
|
65
|
+
const value = values.parse(dataset.value)
|
|
66
|
+
if (value !== undefined) return value
|
|
67
|
+
addIssue({ message: `expected ${values.names.join(', ')}` })
|
|
68
|
+
return NEVER
|
|
69
|
+
})
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** An enum spelled by each value's lowercase name, as most of design JSX's enums are. */
|
|
74
|
+
function lowercaseEnum<T extends string>(
|
|
75
|
+
values: readonly T[],
|
|
76
|
+
aliases: Readonly<Record<string, T>> = {}
|
|
77
|
+
): JSXEnum<T> {
|
|
78
|
+
const spellings = Object.fromEntries(values.map((value) => [value, value.toLowerCase()]))
|
|
79
|
+
return jsxEnum(spellings as Record<T, string>, aliases)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const STROKE_CAP = lowercaseEnum<StrokeCap>([
|
|
83
|
+
'NONE',
|
|
84
|
+
'ROUND',
|
|
85
|
+
'SQUARE',
|
|
86
|
+
'ARROW_LINES',
|
|
87
|
+
'ARROW_EQUILATERAL',
|
|
88
|
+
'TRIANGLE_FILLED',
|
|
89
|
+
'DIAMOND_FILLED',
|
|
90
|
+
'CIRCLE_FILLED'
|
|
91
|
+
])
|
|
92
|
+
export const STROKE_JOIN = lowercaseEnum<StrokeJoin>(['MITER', 'BEVEL', 'ROUND'])
|
|
93
|
+
export const STROKE_ALIGN = lowercaseEnum<Stroke['align']>(['INSIDE', 'CENTER', 'OUTSIDE'])
|
|
94
|
+
export const CONSTRAINT = lowercaseEnum<ConstraintType>([
|
|
95
|
+
'MIN',
|
|
96
|
+
'CENTER',
|
|
97
|
+
'MAX',
|
|
98
|
+
'STRETCH',
|
|
99
|
+
'SCALE'
|
|
100
|
+
])
|
|
101
|
+
export const MASK_TYPE = lowercaseEnum<MaskType>(['ALPHA', 'LUMINANCE', 'VECTOR'])
|
|
102
|
+
export const BLEND_MODE = lowercaseEnum<BlendMode>([
|
|
103
|
+
'NORMAL',
|
|
104
|
+
'DARKEN',
|
|
105
|
+
'MULTIPLY',
|
|
106
|
+
'LINEAR_BURN',
|
|
107
|
+
'COLOR_BURN',
|
|
108
|
+
'LIGHTEN',
|
|
109
|
+
'SCREEN',
|
|
110
|
+
'LINEAR_DODGE',
|
|
111
|
+
'COLOR_DODGE',
|
|
112
|
+
'OVERLAY',
|
|
113
|
+
'SOFT_LIGHT',
|
|
114
|
+
'HARD_LIGHT',
|
|
115
|
+
'DIFFERENCE',
|
|
116
|
+
'EXCLUSION',
|
|
117
|
+
'HUE',
|
|
118
|
+
'SATURATION',
|
|
119
|
+
'COLOR',
|
|
120
|
+
'LUMINOSITY',
|
|
121
|
+
'PASS_THROUGH'
|
|
122
|
+
])
|
|
123
|
+
export const TEXT_ALIGN = lowercaseEnum<SceneNode['textAlignHorizontal']>(
|
|
124
|
+
['LEFT', 'CENTER', 'RIGHT', 'JUSTIFIED'],
|
|
125
|
+
{ left_align: 'LEFT', center_align: 'CENTER', right_align: 'RIGHT' }
|
|
126
|
+
)
|
|
127
|
+
export const TEXT_ALIGN_VERTICAL = lowercaseEnum<SceneNode['textAlignVertical']>([
|
|
128
|
+
'TOP',
|
|
129
|
+
'CENTER',
|
|
130
|
+
'BOTTOM'
|
|
131
|
+
])
|
|
132
|
+
export const TEXT_AUTO_RESIZE = jsxEnum<TextAutoResize>({
|
|
133
|
+
NONE: 'none',
|
|
134
|
+
WIDTH_AND_HEIGHT: 'width',
|
|
135
|
+
HEIGHT: 'height',
|
|
136
|
+
TRUNCATE: 'truncate'
|
|
137
|
+
})
|
|
138
|
+
export const TEXT_DIRECTION = lowercaseEnum<TextDirection>(['AUTO', 'LTR', 'RTL'])
|
|
139
|
+
export const TEXT_DECORATION = lowercaseEnum<TextDecoration>(['NONE', 'UNDERLINE', 'STRIKETHROUGH'])
|
|
140
|
+
export const TEXT_DECORATION_STYLE = lowercaseEnum<TextDecorationStyle>(['SOLID', 'DOTTED', 'WAVY'])
|
|
141
|
+
export const TEXT_CASE = lowercaseEnum<TextCase>(['ORIGINAL', 'UPPER', 'LOWER', 'TITLE'])
|
|
142
|
+
|
|
143
|
+
export const FIGJAM_TEXT_SIZE = jsxEnum<FigJamTextSize>({
|
|
144
|
+
small: 'small',
|
|
145
|
+
medium: 'medium',
|
|
146
|
+
large: 'large',
|
|
147
|
+
extraLarge: 'extraLarge',
|
|
148
|
+
huge: 'huge'
|
|
149
|
+
})
|
|
150
|
+
/** FigJam's typefaces, as its text menus name them. */
|
|
151
|
+
export const FIGJAM_TYPEFACE = jsxEnum<FigJamTypeface>({
|
|
152
|
+
simple: 'simple',
|
|
153
|
+
bookish: 'bookish',
|
|
154
|
+
technical: 'technical',
|
|
155
|
+
scribbled: 'scribbled'
|
|
156
|
+
})
|
|
157
|
+
/** The kinds a `<Shape>` draws, as FigJam names them: `rounded_rectangle`, `eng_database`. */
|
|
158
|
+
export const SHAPE_KIND = lowercaseEnum<ShapeWithTextType>(SHAPE_WITH_TEXT_TYPES)
|
|
159
|
+
export const CONNECTOR_LINE = lowercaseEnum<ConnectorLineType>(['ELBOWED', 'STRAIGHT', 'CURVED'])
|
|
160
|
+
/** The sides a `<Connector>` end attaches by; the other magnets come only from files. */
|
|
161
|
+
export const CONNECTOR_SIDE = lowercaseEnum<
|
|
162
|
+
Extract<ConnectorMagnet, 'AUTO' | 'TOP' | 'RIGHT' | 'BOTTOM' | 'LEFT' | 'CENTER'>
|
|
163
|
+
>(['AUTO', 'TOP', 'RIGHT', 'BOTTOM', 'LEFT', 'CENTER'])
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The layer props whose values are one of an enum's spellings, for checking what JSX passes. An
|
|
167
|
+
* element with a props schema, such as `<Connector>`, checks its enums by `enumProp` instead.
|
|
168
|
+
*/
|
|
169
|
+
export const ENUM_PROPS: Readonly<Partial<Record<string, JSXEnum<string>>>> = {
|
|
170
|
+
strokeCap: STROKE_CAP,
|
|
171
|
+
strokeJoin: STROKE_JOIN,
|
|
172
|
+
strokeAlign: STROKE_ALIGN,
|
|
173
|
+
mask: MASK_TYPE,
|
|
174
|
+
blendMode: BLEND_MODE,
|
|
175
|
+
textAlign: TEXT_ALIGN,
|
|
176
|
+
textAlignVertical: TEXT_ALIGN_VERTICAL,
|
|
177
|
+
textAutoResize: TEXT_AUTO_RESIZE,
|
|
178
|
+
dir: TEXT_DIRECTION,
|
|
179
|
+
textDecoration: TEXT_DECORATION,
|
|
180
|
+
textDecorationStyle: TEXT_DECORATION_STYLE,
|
|
181
|
+
textCase: TEXT_CASE
|
|
182
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
2
|
+
import {
|
|
3
|
+
behaviourToSpec,
|
|
4
|
+
readBehaviour,
|
|
5
|
+
textBinding,
|
|
6
|
+
type BehaviourKind
|
|
7
|
+
} from '@pairdraw/scene-graph/behaviours'
|
|
8
|
+
import { instanceMainComponent } from '@pairdraw/scene-graph/instances'
|
|
9
|
+
import { slotPropertyId } from '@pairdraw/scene-graph/slots'
|
|
10
|
+
|
|
11
|
+
import { REKA_ELEMENTS, rekaRole } from '../behaviours'
|
|
12
|
+
import type { JSXProp, JSXValue } from './value'
|
|
13
|
+
|
|
14
|
+
/** The Reka namespace and element a kind's component is written as. */
|
|
15
|
+
const ROOTS: Record<BehaviourKind, [namespace: string, element: string]> = {
|
|
16
|
+
button: ['Button', 'Root'],
|
|
17
|
+
toggle: ['Toggle', 'Root'],
|
|
18
|
+
switch: ['Switch', 'Root'],
|
|
19
|
+
checkbox: ['Checkbox', 'Root'],
|
|
20
|
+
radio: ['RadioGroup', 'Item'],
|
|
21
|
+
radioGroup: ['RadioGroup', 'Root'],
|
|
22
|
+
toggleGroup: ['ToggleGroup', 'Root'],
|
|
23
|
+
slider: ['Slider', 'Root'],
|
|
24
|
+
progress: ['Progress', 'Root'],
|
|
25
|
+
tabs: ['Tabs', 'Root'],
|
|
26
|
+
collapsible: ['Collapsible', 'Root'],
|
|
27
|
+
accordion: ['Accordion', 'Root'],
|
|
28
|
+
numberField: ['NumberField', 'Root'],
|
|
29
|
+
textField: ['TextField', 'Root'],
|
|
30
|
+
textarea: ['Textarea', 'Root']
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** How the layers of a component with a behaviour are written as Reka elements. */
|
|
34
|
+
export interface RekaExport {
|
|
35
|
+
/** Element names by layer id: the root, its parts, its input, and repeated parts. */
|
|
36
|
+
tags: Map<string, string>
|
|
37
|
+
/** Props a layer adds: the root's behaviour, an item's component. */
|
|
38
|
+
props: Map<string, JSXProp[]>
|
|
39
|
+
/** Containers JSX leaves implicit, written as their children alone. */
|
|
40
|
+
flatten: Set<string>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The element a part is written as in a namespace, such as `Thumb` for a switch's thumb. */
|
|
44
|
+
function elementFor(namespace: string, match: (role: ReturnType<typeof rekaRole>) => boolean) {
|
|
45
|
+
const parts = REKA_ELEMENTS[namespace] ?? {}
|
|
46
|
+
const found = Object.keys(parts).find((element) => match(rekaRole(`${namespace}.${element}`)))
|
|
47
|
+
return found ? `${namespace}.${found}` : undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function rootProps(graph: SceneGraph, owner: SceneNode): JSXProp[] {
|
|
51
|
+
const behaviour = readBehaviour(owner)
|
|
52
|
+
if (!behaviour) return []
|
|
53
|
+
const spec = behaviourToSpec(graph, owner, behaviour)
|
|
54
|
+
const props: JSXProp[] = []
|
|
55
|
+
const valueProp = (value: NonNullable<typeof spec.values>[string]): JSXValue =>
|
|
56
|
+
typeof value === 'string' ? value : { property: value.property, on: value.on, off: value.off }
|
|
57
|
+
for (const [valueId, value] of Object.entries(spec.values ?? {})) {
|
|
58
|
+
if (Object.hasOwn(behaviour.texts, valueId)) continue
|
|
59
|
+
const name = valueId === 'value' ? 'modelValue' : valueId
|
|
60
|
+
props.push([name, valueProp(value)])
|
|
61
|
+
}
|
|
62
|
+
const range = spec.numbers?.value
|
|
63
|
+
if (range) {
|
|
64
|
+
for (const key of ['min', 'max', 'step'] as const) {
|
|
65
|
+
const value = range[key]
|
|
66
|
+
if (value !== undefined) props.push([key, value])
|
|
67
|
+
}
|
|
68
|
+
if (range.default !== undefined) props.push(['defaultValue', range.default])
|
|
69
|
+
}
|
|
70
|
+
if (spec.states)
|
|
71
|
+
props.push(['states', typeof spec.states === 'string' ? spec.states : spec.states.property])
|
|
72
|
+
return props
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A part's slot frame as its element, and its children as the repeated parts or items. */
|
|
76
|
+
function exportPart(
|
|
77
|
+
graph: SceneGraph,
|
|
78
|
+
namespace: string,
|
|
79
|
+
layer: SceneNode,
|
|
80
|
+
partId: string,
|
|
81
|
+
result: RekaExport
|
|
82
|
+
): void {
|
|
83
|
+
const tag = elementFor(namespace, (role) => role?.role === 'part' && role.part === partId)
|
|
84
|
+
const repeated = elementFor(
|
|
85
|
+
namespace,
|
|
86
|
+
(role) => role?.role === 'repeat' && role.container === partId
|
|
87
|
+
)
|
|
88
|
+
if (tag) result.tags.set(layer.id, tag)
|
|
89
|
+
else result.flatten.add(layer.id)
|
|
90
|
+
for (const child of graph.getChildren(layer.id)) {
|
|
91
|
+
if (repeated) result.tags.set(child.id, repeated)
|
|
92
|
+
const item = child.type === 'INSTANCE' ? instanceMainComponent(graph, child) : undefined
|
|
93
|
+
if (partId !== 'items' || !item) continue
|
|
94
|
+
// A group's items are written as the group's Item, naming their component.
|
|
95
|
+
const set = item.parentId ? graph.getNode(item.parentId) : undefined
|
|
96
|
+
result.tags.set(child.id, `${namespace}.Item`)
|
|
97
|
+
result.props.set(child.id, [['of', (set?.type === 'COMPONENT_SET' ? set : item).id]])
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Every layer below a node, depth first. */
|
|
102
|
+
function descendants(graph: SceneGraph, node: SceneNode): SceneNode[] {
|
|
103
|
+
return graph.getChildren(node.id).flatMap((child) => [child, ...descendants(graph, child)])
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* How a main component or component set with a behaviour is written: `Switch.Root` and its
|
|
108
|
+
* `Switch.Thumb`, `TextField.Input`, tab triggers and panels, and a group's items as
|
|
109
|
+
* `RadioGroup.Item of="…"`. Null for other layers.
|
|
110
|
+
*/
|
|
111
|
+
export function rekaExport(graph: SceneGraph, owner: SceneNode): RekaExport | null {
|
|
112
|
+
const behaviour = readBehaviour(owner)
|
|
113
|
+
if (!behaviour || (owner.type !== 'COMPONENT' && owner.type !== 'COMPONENT_SET')) return null
|
|
114
|
+
const [namespace, element] = ROOTS[behaviour.kind]
|
|
115
|
+
const result: RekaExport = {
|
|
116
|
+
tags: new Map([[owner.id, `${namespace}.${element}`]]),
|
|
117
|
+
props: new Map([[owner.id, rootProps(graph, owner)]]),
|
|
118
|
+
flatten: new Set()
|
|
119
|
+
}
|
|
120
|
+
const textIds = new Map(
|
|
121
|
+
Object.keys(behaviour.texts).map((valueId) => [textBinding(behaviour, valueId), valueId])
|
|
122
|
+
)
|
|
123
|
+
for (const layer of descendants(graph, owner)) {
|
|
124
|
+
if (layer.type === 'INSTANCE') continue
|
|
125
|
+
const partId = Object.entries(behaviour.parts).find(
|
|
126
|
+
([, id]) => id === slotPropertyId(layer)
|
|
127
|
+
)?.[0]
|
|
128
|
+
if (partId) {
|
|
129
|
+
exportPart(graph, namespace, layer, partId, result)
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
const reference = layer.componentPropertyReferences.find((item) => item.field === 'TEXT')
|
|
133
|
+
const valueId = reference && textIds.get(reference.propertyId)
|
|
134
|
+
if (layer.type === 'TEXT' && valueId) {
|
|
135
|
+
const tag = elementFor(
|
|
136
|
+
namespace,
|
|
137
|
+
(role) => role?.role === 'input' && role.valueId === valueId
|
|
138
|
+
)
|
|
139
|
+
if (tag) result.tags.set(layer.id, tag)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return result
|
|
143
|
+
}
|