@pairdraw/design-jsx 0.19.1 → 0.19.2
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/dist/chunks/behaviours/elements.js +2 -1
- package/dist/chunks/behaviours/elements.js.map +1 -1
- package/dist/chunks/fields.js +111 -29
- package/dist/chunks/fields.js.map +1 -1
- package/dist/chunks/index.js +3 -1
- package/dist/chunks/index.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/behaviours/index.ts +3 -0
- package/src/order.ts +111 -0
- package/src/reconcile.ts +2 -0
- package/src/reference/authoring.md +1 -1
- package/src/render.ts +4 -1
- package/src/renderer.ts +54 -25
- package/src/types.ts +3 -0
package/src/render.ts
CHANGED
|
@@ -281,7 +281,10 @@ export function createDesignJSXRenderer<Artwork>(services: DesignJSXServices<Art
|
|
|
281
281
|
renderJSX(services, graph, jsxString, options),
|
|
282
282
|
/** Render a tree built with the element functions into the graph. */
|
|
283
283
|
renderTree: (graph: SceneGraph, tree: TreeNode, options?: RenderJSXOptions) =>
|
|
284
|
-
renderTree(services, graph, tree, options)
|
|
284
|
+
renderTree(services, graph, tree, options),
|
|
285
|
+
/** Render every top-level element of a tree at once, one result per root. */
|
|
286
|
+
renderRoots: (graph: SceneGraph, tree: TreeNode, options?: RenderJSXOptions) =>
|
|
287
|
+
renderRoots(services, graph, tree, options)
|
|
285
288
|
}
|
|
286
289
|
}
|
|
287
290
|
|
package/src/renderer.ts
CHANGED
|
@@ -17,9 +17,10 @@ import {
|
|
|
17
17
|
} from './component-properties'
|
|
18
18
|
import { CONNECTOR_ELEMENT, resolveConnectorEnds } from './connectors'
|
|
19
19
|
import { elementNodeType, elementOwnLayer, type OwnLayerRenderer } from './elements'
|
|
20
|
+
import { RenderOrder } from './order'
|
|
20
21
|
import { placeVectorPaths } from './overrides/vector'
|
|
21
22
|
import { applySizeOverrides, propsToOverrides, shapeOverrides } from './props-overrides'
|
|
22
|
-
import { LayerRefs } from './refs'
|
|
23
|
+
import { isLayerRef, LayerRefs } from './refs'
|
|
23
24
|
import { prepareScalarBindings } from './scalar-bindings'
|
|
24
25
|
import { readElementProps } from './schema'
|
|
25
26
|
import type { DesignJSXServices } from './services'
|
|
@@ -63,6 +64,10 @@ export async function renderRoots<Artwork>(
|
|
|
63
64
|
const parentId = options.parentId ?? graph.getPages()[0].id
|
|
64
65
|
applyVariableDefinitions(graph, tree)
|
|
65
66
|
|
|
67
|
+
const originalChildCount = graph.getChildren(parentId).length
|
|
68
|
+
// Render roots that define a component before roots that name it, so an `<Instance of={card}>`
|
|
69
|
+
// written before its `<Component ref={card}>` still finds the component (RenderOrder).
|
|
70
|
+
const order = new RenderOrder()
|
|
66
71
|
const nodes: SceneNode[] = []
|
|
67
72
|
const connectors: Array<{ node: SceneNode; tree: TreeNode }> = []
|
|
68
73
|
const refs = new LayerRefs()
|
|
@@ -71,12 +76,15 @@ export async function renderRoots<Artwork>(
|
|
|
71
76
|
else refs.mark(element.props.ref, node)
|
|
72
77
|
options.onNode?.(element, node)
|
|
73
78
|
}
|
|
74
|
-
for (const
|
|
75
|
-
const node = await renderNode(services, graph,
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
|
|
79
|
+
for (const index of order.of(roots)) {
|
|
80
|
+
const node = await renderNode(services, graph, roots[index], parentId, onNode, undefined, refs)
|
|
81
|
+
const { x, y } = options.positions?.[index] ?? options
|
|
82
|
+
if (x !== undefined) graph.updateNode(node.id, { x })
|
|
83
|
+
if (y !== undefined) graph.updateNode(node.id, { y })
|
|
84
|
+
nodes[index] = node
|
|
79
85
|
}
|
|
86
|
+
// Keep the written layer order even though components rendered first.
|
|
87
|
+
nodes.forEach((node, index) => graph.insertChildAt(node.id, parentId, originalChildCount + index))
|
|
80
88
|
|
|
81
89
|
const rootIds = nodes.map((node) => node.id)
|
|
82
90
|
const warnings = [...refs.warnings, ...resolveConnectorEnds(graph, rootIds, connectors, refs)]
|
|
@@ -312,8 +320,16 @@ function selectsVariant(set: SceneNode, props: Record<string, unknown>): boolean
|
|
|
312
320
|
* the props select one of its variants, and the first otherwise; the warning from
|
|
313
321
|
* `componentReferenceWarnings` says so and how to pick one by ID.
|
|
314
322
|
*/
|
|
315
|
-
function namedComponent(
|
|
323
|
+
function namedComponent(
|
|
324
|
+
graph: SceneGraph,
|
|
325
|
+
props: Record<string, unknown>,
|
|
326
|
+
refs?: LayerRefs
|
|
327
|
+
): SceneNode | undefined {
|
|
316
328
|
const ref = props.component ?? props.componentId ?? props.of
|
|
329
|
+
if (isLayerRef(ref)) {
|
|
330
|
+
const marked = graph.getNode(refs?.layerOf(ref) ?? '')
|
|
331
|
+
return marked?.type === 'COMPONENT' || marked?.type === 'COMPONENT_SET' ? marked : undefined
|
|
332
|
+
}
|
|
317
333
|
if (typeof ref !== 'string') return undefined
|
|
318
334
|
|
|
319
335
|
const matches = componentsNamed(graph, ref)
|
|
@@ -369,9 +385,10 @@ function findVariantInSet(
|
|
|
369
385
|
|
|
370
386
|
function resolveComponent(
|
|
371
387
|
graph: SceneGraph,
|
|
372
|
-
props: Record<string, unknown
|
|
388
|
+
props: Record<string, unknown>,
|
|
389
|
+
refs?: LayerRefs
|
|
373
390
|
): SceneNode | undefined {
|
|
374
|
-
const named = namedComponent(graph, props)
|
|
391
|
+
const named = namedComponent(graph, props, refs)
|
|
375
392
|
return named?.type === 'COMPONENT_SET' ? findVariantInSet(graph, named, props) : named
|
|
376
393
|
}
|
|
377
394
|
|
|
@@ -387,16 +404,24 @@ export function componentPropNames(graph: SceneGraph, props: Record<string, unkn
|
|
|
387
404
|
async function renderInstanceNode(
|
|
388
405
|
graph: SceneGraph,
|
|
389
406
|
tree: TreeNode,
|
|
390
|
-
parentId: string
|
|
407
|
+
parentId: string,
|
|
408
|
+
refs?: LayerRefs
|
|
391
409
|
): Promise<SceneNode> {
|
|
392
410
|
const parent = graph.getNode(parentId)
|
|
393
411
|
const parentLayout = parent?.layoutMode ?? 'NONE'
|
|
394
412
|
const modes = variableModes(graph, tree.props.mode)
|
|
395
|
-
const bound = bindProperties(
|
|
413
|
+
const bound = bindProperties(
|
|
414
|
+
graph,
|
|
415
|
+
tree.props,
|
|
416
|
+
'INSTANCE',
|
|
417
|
+
parentId,
|
|
418
|
+
componentIdNamed(graph, refs)
|
|
419
|
+
)
|
|
396
420
|
const { props, bindings } = preparePropsForRender(graph, bound.props, false, { parentId, modes })
|
|
397
|
-
const component = resolveComponent(graph, props)
|
|
421
|
+
const component = resolveComponent(graph, props, refs)
|
|
398
422
|
if (!component) {
|
|
399
423
|
const ref = props.component ?? props.componentId ?? props.of
|
|
424
|
+
if (isLayerRef(ref)) throw new Error('<Instance> ref() marks no component rendered before it')
|
|
400
425
|
const label = typeof ref === 'string' || typeof ref === 'number' ? String(ref) : ''
|
|
401
426
|
throw new Error(`<Instance> component not found: ${label}`)
|
|
402
427
|
}
|
|
@@ -430,8 +455,8 @@ async function renderInstanceNode(
|
|
|
430
455
|
graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
|
|
431
456
|
applyBindings(graph, instance.id, bindings)
|
|
432
457
|
applyInstanceOverrides(graph, instance, tree.props.overrides)
|
|
433
|
-
assignNamedProperties(graph, instance, props, componentIdNamed(graph))
|
|
434
|
-
assignComponentProperties(graph, instance, props.properties, componentIdNamed(graph))
|
|
458
|
+
assignNamedProperties(graph, instance, props, componentIdNamed(graph, refs))
|
|
459
|
+
assignComponentProperties(graph, instance, props.properties, componentIdNamed(graph, refs))
|
|
435
460
|
if (props.exposed === true) exposeInstance(graph, instance)
|
|
436
461
|
return instance
|
|
437
462
|
} catch (error) {
|
|
@@ -512,8 +537,8 @@ function layerMetadata(
|
|
|
512
537
|
}
|
|
513
538
|
|
|
514
539
|
/** The component a swap property's default names, by ID or name, as `<Instance of>` finds it. */
|
|
515
|
-
function componentIdNamed(graph: SceneGraph) {
|
|
516
|
-
return (name: string) => resolveComponent(graph, { of: name })?.id
|
|
540
|
+
function componentIdNamed(graph: SceneGraph, refs?: LayerRefs) {
|
|
541
|
+
return (name: string) => resolveComponent(graph, { of: name }, refs)?.id
|
|
517
542
|
}
|
|
518
543
|
|
|
519
544
|
export interface ElementOverrides {
|
|
@@ -560,9 +585,10 @@ async function renderNode<Artwork>(
|
|
|
560
585
|
tree: TreeNode,
|
|
561
586
|
parentId: string,
|
|
562
587
|
onNode?: RenderOptions['onNode'],
|
|
563
|
-
scope?: RekaScope
|
|
588
|
+
scope?: RekaScope,
|
|
589
|
+
refs?: LayerRefs
|
|
564
590
|
): Promise<SceneNode> {
|
|
565
|
-
const node = await renderNodeContent(services, graph, tree, parentId, onNode, scope)
|
|
591
|
+
const node = await renderNodeContent(services, graph, tree, parentId, onNode, scope, refs)
|
|
566
592
|
onNode?.(tree, node)
|
|
567
593
|
return node
|
|
568
594
|
}
|
|
@@ -575,14 +601,15 @@ function renderOwnElement<Artwork>(
|
|
|
575
601
|
services: DesignJSXServices<Artwork>,
|
|
576
602
|
graph: SceneGraph,
|
|
577
603
|
tree: TreeNode,
|
|
578
|
-
parentId: string
|
|
604
|
+
parentId: string,
|
|
605
|
+
refs?: LayerRefs
|
|
579
606
|
): Promise<SceneNode> | null {
|
|
580
607
|
switch (tree.type) {
|
|
581
608
|
case 'icon':
|
|
582
609
|
case 'svg':
|
|
583
610
|
return renderArtworkNode(services, graph, tree, parentId)
|
|
584
611
|
case 'instance':
|
|
585
|
-
return renderInstanceNode(graph, tree, parentId)
|
|
612
|
+
return renderInstanceNode(graph, tree, parentId, refs)
|
|
586
613
|
default: {
|
|
587
614
|
const element = elementOwnLayer(tree.type)
|
|
588
615
|
return element ? Promise.resolve(renderOwnLayerElement(graph, tree, parentId, element)) : null
|
|
@@ -596,13 +623,14 @@ async function renderNodeContent<Artwork>(
|
|
|
596
623
|
tree: TreeNode,
|
|
597
624
|
parentId: string,
|
|
598
625
|
onNode?: RenderOptions['onNode'],
|
|
599
|
-
scope?: RekaScope
|
|
626
|
+
scope?: RekaScope,
|
|
627
|
+
refs?: LayerRefs
|
|
600
628
|
): Promise<SceneNode> {
|
|
601
|
-
const own = renderOwnElement(services, graph, tree, parentId)
|
|
629
|
+
const own = renderOwnElement(services, graph, tree, parentId, refs)
|
|
602
630
|
if (own) return own
|
|
603
631
|
const reka = await renderRekaNode(graph, tree, parentId, scope, {
|
|
604
632
|
render: (child, childParentId, childScope) =>
|
|
605
|
-
renderNode(services, graph, child, childParentId, onNode, childScope),
|
|
633
|
+
renderNode(services, graph, child, childParentId, onNode, childScope, refs),
|
|
606
634
|
create: (nodeType, element, elementParentId) => {
|
|
607
635
|
const { overrides, bindings } = elementOverrides(graph, nodeType, element, elementParentId)
|
|
608
636
|
const created = graph.createNode(nodeType, elementParentId, overrides)
|
|
@@ -610,7 +638,8 @@ async function renderNodeContent<Artwork>(
|
|
|
610
638
|
placeVectorPaths(graph, created, element.props, (network) => services.vectorBounds(network))
|
|
611
639
|
return created
|
|
612
640
|
},
|
|
613
|
-
instance: (element, elementParentId) =>
|
|
641
|
+
instance: (element, elementParentId) =>
|
|
642
|
+
renderInstanceNode(graph, element, elementParentId, refs),
|
|
614
643
|
finishSet: (setId) => inferComponentSetProperties(graph, setId)
|
|
615
644
|
})
|
|
616
645
|
if (reka) return reka
|
|
@@ -631,7 +660,7 @@ async function renderNodeContent<Artwork>(
|
|
|
631
660
|
// Text holds characters and style runs, not layers: a child layer would exist in the graph but
|
|
632
661
|
// not in the text's export, so it is refused rather than silently lost.
|
|
633
662
|
if (node.type === 'TEXT') throw new Error(`<${child.type}> cannot be inside <Text>`)
|
|
634
|
-
await renderNode(services, graph, child, node.id, onNode, scope)
|
|
663
|
+
await renderNode(services, graph, child, node.id, onNode, scope, refs)
|
|
635
664
|
}
|
|
636
665
|
|
|
637
666
|
if (node.type === 'COMPONENT_SET') inferComponentSetProperties(graph, node.id)
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { SceneNode } from '@pairdraw/scene-graph'
|
|
2
|
+
import type { Vector } from '@pairdraw/scene-graph/primitives'
|
|
2
3
|
|
|
3
4
|
import type { TreeNode } from './tree'
|
|
4
5
|
|
|
5
6
|
export interface RenderOptions {
|
|
6
7
|
x?: number
|
|
7
8
|
y?: number
|
|
9
|
+
/** Each root's position, in the order the roots are written; an entry replaces `x` and `y`. */
|
|
10
|
+
positions?: ReadonlyArray<Partial<Vector>>
|
|
8
11
|
parentId?: string
|
|
9
12
|
/** Called for every layer created from an element, so callers can map source to layers. */
|
|
10
13
|
onNode?: (tree: TreeNode, node: SceneNode) => void
|