@pairdraw/design-jsx 0.18.0 → 0.19.0-beta.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/dist/chunks/component-properties.js +60 -9
- package/dist/chunks/component-properties.js.map +1 -1
- package/dist/chunks/elements.js +6 -2
- package/dist/chunks/elements.js.map +1 -1
- package/dist/chunks/export/index.js +3 -3
- package/dist/chunks/fields.js +106 -40
- package/dist/chunks/fields.js.map +1 -1
- package/dist/chunks/index.js +1 -1
- package/dist/chunks/index.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/export/paint.ts +10 -3
- package/src/overrides/strokes.ts +16 -12
- package/src/props-overrides.ts +50 -38
- package/src/reference/authoring.md +2 -1
- package/src/render.ts +62 -2
- package/src/renderer.ts +66 -24
- package/src/schema.ts +67 -0
package/src/props-overrides.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Fill, SceneNode, ArcData, NodeType } from '@pairdraw/scene-graph'
|
|
2
|
-
import { colorToFill } from '@pairdraw/scene-graph/color'
|
|
2
|
+
import { colorToFill, tryParseColor } from '@pairdraw/scene-graph/color'
|
|
3
3
|
import { parseCSSGridTracks, parseCSSNumber } from '@pairdraw/scene-graph/css'
|
|
4
4
|
import {
|
|
5
5
|
autoLayoutSizingFields,
|
|
@@ -27,6 +27,7 @@ import { applyStateOverrides } from './overrides/state'
|
|
|
27
27
|
import { applyStrokeOverrides } from './overrides/strokes'
|
|
28
28
|
import { applyTextDetailOverrides, AUTO, textLengthProp } from './overrides/text'
|
|
29
29
|
import { isShaderFill } from './paints'
|
|
30
|
+
import { readLayerProps, type LayerProps } from './schema'
|
|
30
31
|
|
|
31
32
|
const WEIGHT_MAP: Record<string, number> = {
|
|
32
33
|
normal: 400,
|
|
@@ -96,7 +97,11 @@ export function applySizeOverrides(
|
|
|
96
97
|
*/
|
|
97
98
|
function setFills(values: (string | Color | Fill)[], o: Partial<SceneNode>): void {
|
|
98
99
|
const shaders: ShaderPaint[] = []
|
|
99
|
-
|
|
100
|
+
// A colour string no parser reads would otherwise become black, opposite to leaving it out.
|
|
101
|
+
const usable = values.filter(
|
|
102
|
+
(value) => typeof value !== 'string' || tryParseColor(value) !== null
|
|
103
|
+
)
|
|
104
|
+
const fills = usable.map((value) => {
|
|
100
105
|
const fill = fillFromValue(value)
|
|
101
106
|
if (!isShaderFill(fill)) return fill
|
|
102
107
|
const { shader, ...paint } = fill
|
|
@@ -124,9 +129,9 @@ function applyFillOverride(props: Record<string, unknown>, o: Partial<SceneNode>
|
|
|
124
129
|
if (isFillValue(bg)) setFills([bg], o)
|
|
125
130
|
}
|
|
126
131
|
|
|
127
|
-
function applyCornerOverrides(props:
|
|
128
|
-
const rounded =
|
|
129
|
-
if (
|
|
132
|
+
function applyCornerOverrides(props: LayerProps, o: Partial<SceneNode>): void {
|
|
133
|
+
const rounded = props.rounded ?? props.cornerRadius ?? props.borderRadius
|
|
134
|
+
if (rounded !== undefined) o.cornerRadius = rounded
|
|
130
135
|
|
|
131
136
|
if (
|
|
132
137
|
props.roundedTL !== undefined ||
|
|
@@ -135,30 +140,30 @@ function applyCornerOverrides(props: Record<string, unknown>, o: Partial<SceneNo
|
|
|
135
140
|
props.roundedBR !== undefined
|
|
136
141
|
) {
|
|
137
142
|
o.independentCorners = true
|
|
138
|
-
if (props.roundedTL !== undefined) o.topLeftRadius = props.roundedTL
|
|
139
|
-
if (props.roundedTR !== undefined) o.topRightRadius = props.roundedTR
|
|
140
|
-
if (props.roundedBL !== undefined) o.bottomLeftRadius = props.roundedBL
|
|
141
|
-
if (props.roundedBR !== undefined) o.bottomRightRadius = props.roundedBR
|
|
143
|
+
if (props.roundedTL !== undefined) o.topLeftRadius = props.roundedTL
|
|
144
|
+
if (props.roundedTR !== undefined) o.topRightRadius = props.roundedTR
|
|
145
|
+
if (props.roundedBL !== undefined) o.bottomLeftRadius = props.roundedBL
|
|
146
|
+
if (props.roundedBR !== undefined) o.bottomRightRadius = props.roundedBR
|
|
142
147
|
}
|
|
143
148
|
|
|
144
|
-
if (props.cornerSmoothing !== undefined) o.cornerSmoothing = props.cornerSmoothing
|
|
149
|
+
if (props.cornerSmoothing !== undefined) o.cornerSmoothing = props.cornerSmoothing
|
|
145
150
|
}
|
|
146
151
|
|
|
147
|
-
function applyVisualOverrides(props:
|
|
152
|
+
function applyVisualOverrides(props: LayerProps, o: Partial<SceneNode>): void {
|
|
148
153
|
applyFillOverride(props, o)
|
|
149
154
|
applyStrokeOverrides(props, o)
|
|
150
155
|
applyCornerOverrides(props, o)
|
|
151
156
|
|
|
152
|
-
if (props.opacity !== undefined) o.opacity = props.opacity
|
|
157
|
+
if (props.opacity !== undefined) o.opacity = props.opacity
|
|
153
158
|
applyTransformOverrides(props, o)
|
|
154
159
|
const blendMode = BLEND_MODE.parse(props.blendMode)
|
|
155
160
|
if (blendMode) o.blendMode = blendMode
|
|
156
161
|
if (props.overflow === 'hidden') o.clipsContent = true
|
|
157
162
|
}
|
|
158
163
|
|
|
159
|
-
function applyTransformOverrides(props:
|
|
160
|
-
const rotation =
|
|
161
|
-
if (rotation !== undefined) o.rotation = rotation
|
|
164
|
+
function applyTransformOverrides(props: LayerProps, o: Partial<SceneNode>): void {
|
|
165
|
+
const rotation = props.rotate ?? props.rotation
|
|
166
|
+
if (rotation !== undefined) o.rotation = rotation
|
|
162
167
|
if (props.flipX !== undefined) o.flipX = props.flipX === true
|
|
163
168
|
if (props.flipY !== undefined) o.flipY = props.flipY === true
|
|
164
169
|
}
|
|
@@ -245,16 +250,18 @@ function applyGridOverrides(
|
|
|
245
250
|
function applyGridChildOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {
|
|
246
251
|
const col = designJSXProp(props, 'colStart')
|
|
247
252
|
const row = designJSXProp(props, 'rowStart')
|
|
248
|
-
const colSpan = (props
|
|
249
|
-
const rowSpan = (props
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
253
|
+
const colSpan = designJSXProp(props, 'colSpan')
|
|
254
|
+
const rowSpan = designJSXProp(props, 'rowSpan')
|
|
255
|
+
|
|
256
|
+
// A span without a start is CSS `grid-column: span N`, which holds while auto placement picks
|
|
257
|
+
// the column; requiring a start here dropped `colSpan` alone.
|
|
258
|
+
if (col === undefined && row === undefined && colSpan === undefined && rowSpan === undefined)
|
|
259
|
+
return
|
|
260
|
+
o.gridPosition = {
|
|
261
|
+
column: (col as number | undefined) ?? 0,
|
|
262
|
+
row: (row as number | undefined) ?? 0,
|
|
263
|
+
columnSpan: (colSpan as number | undefined) ?? 1,
|
|
264
|
+
rowSpan: (rowSpan as number | undefined) ?? 1
|
|
258
265
|
}
|
|
259
266
|
}
|
|
260
267
|
|
|
@@ -330,7 +337,7 @@ function applyLayoutOverrides(
|
|
|
330
337
|
if (props.grow !== undefined) o.layoutGrow = props.grow as number
|
|
331
338
|
}
|
|
332
339
|
|
|
333
|
-
function applyTextStyleOverrides(props:
|
|
340
|
+
function applyTextStyleOverrides(props: LayerProps, o: Partial<SceneNode>): void {
|
|
334
341
|
const fontSize = designJSXProp(props, 'size')
|
|
335
342
|
if (typeof fontSize === 'number') o.fontSize = fontSize
|
|
336
343
|
|
|
@@ -361,7 +368,7 @@ function applyTextStyleOverrides(props: Record<string, unknown>, o: Partial<Scen
|
|
|
361
368
|
const textCase = TEXT_CASE.parse(props.textCase)
|
|
362
369
|
if (textCase) o.textCase = textCase
|
|
363
370
|
if (props.maxLines !== undefined) {
|
|
364
|
-
o.maxLines = props.maxLines
|
|
371
|
+
o.maxLines = props.maxLines
|
|
365
372
|
o.textTruncation = 'ENDING'
|
|
366
373
|
}
|
|
367
374
|
if (props.truncate) {
|
|
@@ -390,7 +397,8 @@ function applyTextAutoResize(
|
|
|
390
397
|
): void {
|
|
391
398
|
const w = designJSXProp(props, 'w')
|
|
392
399
|
const hasExplicitWidth = w !== undefined
|
|
393
|
-
const
|
|
400
|
+
const grow = typeof props.grow === 'number' ? props.grow : undefined
|
|
401
|
+
const fillsParent = w === 'fill' || (grow !== undefined && grow > 0)
|
|
394
402
|
const isInsideAutoLayout = parentLayout !== 'NONE'
|
|
395
403
|
|
|
396
404
|
// DO NOT CHANGE these defaults without testing headless layout (no CanvasKit).
|
|
@@ -407,12 +415,15 @@ function applyTextAutoResize(
|
|
|
407
415
|
}
|
|
408
416
|
|
|
409
417
|
function applyTextOverrides(
|
|
410
|
-
props:
|
|
418
|
+
props: LayerProps,
|
|
411
419
|
o: Partial<SceneNode>,
|
|
412
420
|
parentLayout: SceneNode['layoutMode']
|
|
413
421
|
): void {
|
|
414
422
|
applyTextStyleOverrides(props, o)
|
|
415
|
-
|
|
423
|
+
// Without `dir` the node keeps the style's own direction, which is auto by default; writing an
|
|
424
|
+
// undefined override would erase it and stop Arabic text from being detected as RTL.
|
|
425
|
+
const direction = parseDirection(props.dir)
|
|
426
|
+
if (direction) o.textDirection = direction
|
|
416
427
|
applyTextAutoResize(props, o, parentLayout)
|
|
417
428
|
}
|
|
418
429
|
|
|
@@ -465,17 +476,18 @@ export function propsToOverrides(
|
|
|
465
476
|
isText: boolean,
|
|
466
477
|
parentLayout: SceneNode['layoutMode']
|
|
467
478
|
): Partial<SceneNode> {
|
|
468
|
-
props
|
|
479
|
+
// Layer props are read once, after `style` gives the ones the props leave unset.
|
|
480
|
+
const layer = readLayerProps(normalizeStyleProps(props))
|
|
469
481
|
const o: Partial<SceneNode> = {}
|
|
470
482
|
|
|
471
|
-
if (
|
|
483
|
+
if (layer.name) o.name = layer.name
|
|
472
484
|
|
|
473
|
-
const { w, h } = applySizeOverrides(
|
|
474
|
-
applyVisualOverrides(
|
|
475
|
-
applyLayoutOverrides(
|
|
476
|
-
if (isText) applyTextOverrides(
|
|
477
|
-
applyEffectOverrides(
|
|
478
|
-
applyStateOverrides(
|
|
485
|
+
const { w, h } = applySizeOverrides(layer, o, parentLayout)
|
|
486
|
+
applyVisualOverrides(layer, o)
|
|
487
|
+
applyLayoutOverrides(layer, o, w, h, isText, parentLayout)
|
|
488
|
+
if (isText) applyTextOverrides(layer, o, parentLayout)
|
|
489
|
+
applyEffectOverrides(layer, o)
|
|
490
|
+
applyStateOverrides(layer, o)
|
|
479
491
|
|
|
480
492
|
return o
|
|
481
493
|
}
|
|
@@ -24,7 +24,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
|
|
|
24
24
|
- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.
|
|
25
25
|
- `rotate` turns a layer by degrees, and `flipX` and `flipY` reflect it across its centre before it turns, as the canvas places it. `Star` takes `points` and `innerRadius` (0–1, how deep its points go); `Polygon` takes `points`. `Ellipse` draws an arc or a donut with `startAngle` and `endAngle` in degrees, clockwise from three o'clock (0 to 360 by default), and `innerRadius` (0–1, the hole): `<Ellipse w={80} h={80} startAngle={0} endAngle={270} innerRadius={0.6} />`.
|
|
26
26
|
- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow` takes a CSS `box-shadow` list, such as `shadow="0 4 8 #0002, inset 0 1 0 #fff"`, and `blur` a layer blur radius; both are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning. A raw shadow needs an effect type, a numeric radius, and a color. A raw blur needs an effect type and a numeric radius, and a missing color is transparent. A shader needs `source`. Anything else is an error that names the prop.
|
|
27
|
-
- Text content belongs inside `Text`. Use `size`, `font`, `weight`, `italic`, `lineHeight`, `letterSpacing`, `textAlign`, `textAlignVertical`, `textDecoration
|
|
27
|
+
- Text content belongs inside `Text`. Use `size`, `font`, `weight`, `italic`, `lineHeight`, `letterSpacing`, `textAlign`, `textAlignVertical`, `textDecoration` (`underline` or `strikethrough`), and `textCase` (`original`, `upper`, `lower`, `title`). Decoration details take CSS's names and values: `textDecorationStyle` (`solid`, `dotted`, `wavy`), `textDecorationColor` (`currentColor` follows the text), `textDecorationThickness` and `textUnderlineOffset` (pixels or `auto`), and `textDecorationSkipInk`. `fontFeatures` turns OpenType features on or off by tag (`fontFeatures={{ tnum: true, liga: false }}`), `fontVariations` sets variable-font axes by tag (`fontVariations={{ wght: 650 }}`), and `lang` names the text's language (`lang="de"`) for shaping and font fallback. A variable font's optical size follows the font size unless `fontOpticalSizing="none"`, as in CSS; only then does an `opsz` in `fontVariations` apply. `lineHeight="auto"` follows the font, and `fills={[]}` paints no text. Verify fonts actually load before judging dimensions; do not assume every font is available.
|
|
28
28
|
- `Span` styles some characters of a `Text` with `Text`'s own prop names: `size`, `font`, `weight`, `italic`, `color`, `fills`, `lineHeight`, `letterSpacing`, `textDecoration`, the decoration details, `fontFeatures`, `fontVariations`, `lang`, and `textCase`; a span's `fontFeatures` and `fontVariations` replace the text's rather than adding to them, and `fontOpticalSizing` is set on the `Text` for all of it. `href` links its characters to a URL without changing how they look. Spans nest; the inner one wins. `Paragraph` writes one paragraph: `list="ordered"` or `list="unordered"` makes it a list item nested at `level` 1–5, and `spacing`, `listSpacing`, and `indent` override the `Text`'s `paragraphSpacing`, `listSpacing`, and `paragraphIndent` for it; `hangingList` on the `Text` hangs list markers outside the text box. A `Text` holds only `Paragraph`s or none; without them, a line break in its text still starts a paragraph.
|
|
29
29
|
- `Icon` uses an Iconify name, size, and color. Prefer icons to emoji when reliable vector output is needed. Image fills belong on appropriate leaf shapes, not containers whose children must remain visible.
|
|
30
30
|
- Pick artwork by where it comes from. A known icon is an `Icon` by its Iconify name. A shape you draw yourself is a `Vector` with its path in SVG syntax, `<Vector d="M11 2L4 11h5l-1 7 7-9h-5z" bg={designVar(…)} />`: one layer, styled with the same props as any other, so its fills can use variables, stack, and take `strokeAlign` and `effects`. `fillRule="evenodd"` cuts holes. An open path with a `stroke` stays open, a line; with only a `bg` it fills as if closed. Given `w` and `h`, the path is drawn in that box as written, as an icon is in its view box; without them the layer fits the path. `paths` takes several paths as export writes them, each `{ windingRule, data }` with `NONZERO`, `EVENODD`, or `NONE` for an open path: `paths={[{ windingRule: "EVENODD", data: "M0 0L10 0L10 10Z" }]}`. Inline `<svg>` is for artwork copied from elsewhere: it draws the SVG's shapes, a frame with a layer per shape in the SVG's own colours, and leaves out its text, so do not use it for a shape you will style or bind, nor for text.
|
|
@@ -46,6 +46,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
|
|
|
46
46
|
- `Instance` resolves an existing component through `of`, `component`, or `componentId`. Component-set children named `variant=Primary`, for example, define variants that can be selected when instantiating the set.
|
|
47
47
|
- `property(name, defaultValue)` binds a field of a component's layer to a component property, created on first use: `text={property('Message', 'Hi')}` on a `Text` makes a TEXT property, `visible={property('Pinned', true)}` a BOOLEAN one, and `of={property('Icon', 'Star')}` on an `Instance` an INSTANCE_SWAP one whose default is the component `of` names. The property belongs to the nearest `Component`, or to its `ComponentSet` for a variant, so the same name in each variant binds one property; one name with two types or two defaults is an error.
|
|
48
48
|
- An `Instance` sets text, boolean, and swap properties by name, the way it picks a variant: `<Instance of="Note" State="Done" Message="Updated review" Pinned={false} />`. `properties` takes the same values keyed by property ID or name, for names that are not JSX identifiers or that design JSX already uses, such as `name`. Only instances accept `properties`.
|
|
49
|
+
- A name that more than one component or component set has joins the component set when the props select one of its variants, and otherwise the first one; the warning lists each candidate’s ID and naming one by its ID always picks it.
|
|
49
50
|
- `exposed` on an `Instance` in a component shows that instance's own properties on the component's instances, as Figma's "Expose properties from nested instances" does; the instance's component needs properties to expose. An outer instance gives an exposed instance its variant and properties as an object under the exposed layer's name: `<Instance of="Card" Title="Welcome" Action={{ Variant: 'Secondary', Label: 'Save' }} />`, or `properties={{ Action: { … } }}`.
|
|
50
51
|
- Instance assignments take `true` / `false` or the native `'true'` / `'false'` for BOOLEAN properties, and a component's name or ID for swaps, as `of` does, whether set as a prop or in `properties`. Assignments persist through component synchronization; unknown IDs and invalid values fail rather than silently creating inert overrides. Select variants through component-set variant props, not through instance property assignments.
|
|
51
52
|
- Reuse existing local or library components before recreating them. Keep meaningful text, visibility, and swap properties exposed rather than hand-editing cloned child nodes.
|
package/src/render.ts
CHANGED
|
@@ -1,19 +1,45 @@
|
|
|
1
|
+
import { pickBy } from 'es-toolkit'
|
|
1
2
|
import { uniq } from 'es-toolkit/array'
|
|
2
3
|
|
|
3
4
|
import type { SceneGraph } from '@pairdraw/scene-graph'
|
|
5
|
+
import { tryParseColor } from '@pairdraw/scene-graph/color'
|
|
4
6
|
|
|
5
7
|
import { REKA_ELEMENTS } from './behaviours'
|
|
6
8
|
import { elementBindings, elementProps, elementSchema } from './elements'
|
|
7
9
|
import { ENUM_PROPS } from './enums'
|
|
8
10
|
import { DESIGN_JSX_HELPERS, designJSXHelpers } from './helpers'
|
|
9
11
|
import * as React from './mini-react'
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
+
import { normalizeStyleProps } from './props-overrides'
|
|
13
|
+
import {
|
|
14
|
+
renderRoots,
|
|
15
|
+
renderTree,
|
|
16
|
+
componentPropNames,
|
|
17
|
+
componentReferenceWarnings,
|
|
18
|
+
type RenderResult
|
|
19
|
+
} from './renderer'
|
|
20
|
+
import { elementPropWarnings, layerPropsSchema, svgLayerPropsSchema } from './schema'
|
|
12
21
|
import type { DesignJSXServices } from './services'
|
|
13
22
|
import { TEXT_CONTENT_NAMES } from './text-content'
|
|
14
23
|
import { transformDesignJSXExpression } from './transform'
|
|
15
24
|
import { isTreeNode, resolveToTree, type TreeNode } from './tree'
|
|
16
25
|
import type { RenderOptions as RenderJSXOptions } from './types'
|
|
26
|
+
import { isVariable } from './variables'
|
|
27
|
+
|
|
28
|
+
const SIZE_PROPS = new Set(['w', 'h'])
|
|
29
|
+
const PAINT_PROPS = new Set(['bg', 'fill', 'stroke'])
|
|
30
|
+
|
|
31
|
+
/** How a warning names a value it could not read. */
|
|
32
|
+
function propValueText(value: unknown): string {
|
|
33
|
+
return typeof value === 'string' ? `"${value}"` : String(value)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function unreadableSize(value: unknown): boolean {
|
|
37
|
+
return !isVariable(value) && typeof value !== 'number' && value !== 'hug' && value !== 'fill'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function unreadablePaint(value: unknown): boolean {
|
|
41
|
+
return typeof value === 'string' && tryParseColor(value) === null
|
|
42
|
+
}
|
|
17
43
|
|
|
18
44
|
/** `const Switch = { Root: 'Switch.Root', … }`: each Reka namespace as element types. */
|
|
19
45
|
const REKA_ALIASES = Object.entries(REKA_ELEMENTS)
|
|
@@ -115,6 +141,23 @@ function stripHTMLComments(jsxString: string): string {
|
|
|
115
141
|
return out
|
|
116
142
|
}
|
|
117
143
|
|
|
144
|
+
/** Why `value` cannot go into the layer field `key` names, or nothing when it can. */
|
|
145
|
+
function valueKindWarning(
|
|
146
|
+
key: string,
|
|
147
|
+
value: unknown,
|
|
148
|
+
element: string,
|
|
149
|
+
type: TreeNode['type']
|
|
150
|
+
): string | undefined {
|
|
151
|
+
// An `<svg>`'s attributes are passed to its markup as written, so a value of the wrong kind is
|
|
152
|
+
// the markup's to read: `fill="none"` and `strokeWidth="2"` are not layer fields.
|
|
153
|
+
if (type === 'svg') return undefined
|
|
154
|
+
if (SIZE_PROPS.has(key) && unreadableSize(value))
|
|
155
|
+
return `Invalid ${key} ${propValueText(value)} on <${element}> is ignored; expected a number, "hug", or "fill".`
|
|
156
|
+
if (PAINT_PROPS.has(key) && unreadablePaint(value))
|
|
157
|
+
return `Invalid ${key} ${propValueText(value)} on <${element}> is ignored; expected a colour or a paint.`
|
|
158
|
+
return undefined
|
|
159
|
+
}
|
|
160
|
+
|
|
118
161
|
function unsupportedPropWarnings(graph: SceneGraph, tree: TreeNode): string[] {
|
|
119
162
|
const warnings: string[] = []
|
|
120
163
|
collectUnsupportedPropWarnings(graph, tree, warnings)
|
|
@@ -128,15 +171,32 @@ function collectUnsupportedPropWarnings(
|
|
|
128
171
|
): void {
|
|
129
172
|
const supported = elementProps(tree.type)
|
|
130
173
|
// An instance sets its component's properties by name, such as `State="On"` or `Title="Hi"`.
|
|
174
|
+
if (tree.type === 'instance') warnings.push(...componentReferenceWarnings(graph, tree.props))
|
|
131
175
|
const properties = componentPropNames(graph, tree.props)
|
|
132
176
|
const element = TEXT_CONTENT_NAMES[tree.type] ?? tree.type
|
|
133
177
|
const schema = elementSchema(tree.type)
|
|
134
178
|
if (schema) warnings.push(...elementPropWarnings(schema, tree.props, element))
|
|
179
|
+
// The layer props an element takes are read by one schema, which its own schema may restate,
|
|
180
|
+
// with the values its `style` gives where the props leave them unset, as rendering reads them.
|
|
181
|
+
const layerSchema = tree.type === 'svg' ? svgLayerPropsSchema : layerPropsSchema
|
|
182
|
+
const layerProps = pickBy(
|
|
183
|
+
normalizeStyleProps(tree.props),
|
|
184
|
+
(_, key) => supported.has(key) && !(schema && Object.hasOwn(schema.entries, key))
|
|
185
|
+
)
|
|
186
|
+
warnings.push(...elementPropWarnings(layerSchema, layerProps, element))
|
|
135
187
|
for (const [key, value] of Object.entries(tree.props)) {
|
|
136
188
|
if (!supported.has(key) && !properties.includes(key)) {
|
|
137
189
|
warnings.push(`Unsupported prop "${key}" on <${element}> is ignored.`)
|
|
138
190
|
continue
|
|
139
191
|
}
|
|
192
|
+
// The element's schema already reported a prop it validates, in its own words; warning
|
|
193
|
+
// again here would name the same value twice, as a connector's stroke once was.
|
|
194
|
+
if (schema && key in schema.entries) continue
|
|
195
|
+
const kindWarning = valueKindWarning(key, value, element, tree.type)
|
|
196
|
+
if (kindWarning) {
|
|
197
|
+
warnings.push(kindWarning)
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
140
200
|
const values = ENUM_PROPS[key]
|
|
141
201
|
if (values && typeof value === 'string' && values.parse(value) === undefined) {
|
|
142
202
|
warnings.push(
|
package/src/renderer.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { omitBy } from 'es-toolkit/object'
|
|
|
3
3
|
import type { NodeType, SceneGraph, SceneNode, VariableModeMap } from '@pairdraw/scene-graph'
|
|
4
4
|
import { parseColor } from '@pairdraw/scene-graph/color'
|
|
5
5
|
import { setInstanceOverride } from '@pairdraw/scene-graph/instances/overrides'
|
|
6
|
+
import { textLayerName } from '@pairdraw/scene-graph/node-defaults'
|
|
6
7
|
|
|
7
8
|
import type { RekaScope } from './behaviours'
|
|
8
9
|
import { renderRekaNode } from './behaviours/render'
|
|
@@ -229,7 +230,7 @@ function placeArtwork<Artwork>(
|
|
|
229
230
|
): SceneNode {
|
|
230
231
|
const parentLayout = graph.getNode(parentId)?.layoutMode ?? 'NONE'
|
|
231
232
|
const overrides: Partial<SceneNode> = {}
|
|
232
|
-
if (props.label) overrides.name = props.label
|
|
233
|
+
if (typeof props.label === 'string' && props.label) overrides.name = props.label
|
|
233
234
|
applyIconSize(props, overrides, parentLayout, size)
|
|
234
235
|
const modes = variableModes(graph, props.mode)
|
|
235
236
|
if (modes) overrides.variableModes = modes
|
|
@@ -283,11 +284,63 @@ function renderSVGNode<Artwork>(
|
|
|
283
284
|
return placeArtwork(services, graph, artwork, props, size, parentId)
|
|
284
285
|
}
|
|
285
286
|
|
|
286
|
-
|
|
287
|
+
/** Every component or set the reference names: one by ID, or each one sharing the name. */
|
|
288
|
+
function componentsNamed(graph: SceneGraph, ref: string): SceneNode[] {
|
|
289
|
+
const byId = graph.getNode(ref)
|
|
290
|
+
if (byId?.type === 'COMPONENT' || byId?.type === 'COMPONENT_SET') return [byId]
|
|
291
|
+
const components: SceneNode[] = []
|
|
292
|
+
const sets: SceneNode[] = []
|
|
287
293
|
for (const node of graph.getAllNodes()) {
|
|
288
|
-
if (node.
|
|
294
|
+
if (node.name !== ref) continue
|
|
295
|
+
if (node.type === 'COMPONENT') components.push(node)
|
|
296
|
+
else if (node.type === 'COMPONENT_SET') sets.push(node)
|
|
289
297
|
}
|
|
290
|
-
return
|
|
298
|
+
return [...components, ...sets]
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Whether `props` selects a variant of `set`, naming one of its variant properties. */
|
|
302
|
+
function selectsVariant(set: SceneNode, props: Record<string, unknown>): boolean {
|
|
303
|
+
return Object.keys(props).some((key) =>
|
|
304
|
+
set.componentPropertyDefinitions.some(
|
|
305
|
+
(definition) => definition.type === 'VARIANT' && definition.name === key
|
|
306
|
+
)
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* The component or component set an element names. A name several of them share uses the set when
|
|
312
|
+
* the props select one of its variants, and the first otherwise; the warning from
|
|
313
|
+
* `componentReferenceWarnings` says so and how to pick one by ID.
|
|
314
|
+
*/
|
|
315
|
+
function namedComponent(graph: SceneGraph, props: Record<string, unknown>): SceneNode | undefined {
|
|
316
|
+
const ref = props.component ?? props.componentId ?? props.of
|
|
317
|
+
if (typeof ref !== 'string') return undefined
|
|
318
|
+
|
|
319
|
+
const matches = componentsNamed(graph, ref)
|
|
320
|
+
if (matches.length <= 1) return matches[0]
|
|
321
|
+
return (
|
|
322
|
+
matches.find((node) => node.type === 'COMPONENT_SET' && selectsVariant(node, props)) ??
|
|
323
|
+
matches[0]
|
|
324
|
+
)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** A name several components or sets share, naming each candidate by its ID. */
|
|
328
|
+
export function componentReferenceWarnings(
|
|
329
|
+
graph: SceneGraph,
|
|
330
|
+
props: Record<string, unknown>
|
|
331
|
+
): string[] {
|
|
332
|
+
const ref = props.component ?? props.componentId ?? props.of
|
|
333
|
+
if (typeof ref !== 'string') return []
|
|
334
|
+
const matches = componentsNamed(graph, ref)
|
|
335
|
+
if (matches.length < 2) return []
|
|
336
|
+
const chosen = namedComponent(graph, props)
|
|
337
|
+
const choices = matches
|
|
338
|
+
.map((node) => `${node.id} (${node.type === 'COMPONENT_SET' ? 'component set' : 'component'})`)
|
|
339
|
+
.join(', ')
|
|
340
|
+
const joined = chosen?.type === 'COMPONENT_SET' ? 'the component set' : 'the first'
|
|
341
|
+
return [
|
|
342
|
+
`<Instance> of "${ref}" names ${matches.length} components and joins ${joined}; name the one you mean by its ID: ${choices}.`
|
|
343
|
+
]
|
|
291
344
|
}
|
|
292
345
|
|
|
293
346
|
function findVariantInSet(
|
|
@@ -314,23 +367,6 @@ function findVariantInSet(
|
|
|
314
367
|
)
|
|
315
368
|
}
|
|
316
369
|
|
|
317
|
-
/** The component or component set an element names by id or name. */
|
|
318
|
-
function namedComponent(graph: SceneGraph, props: Record<string, unknown>): SceneNode | undefined {
|
|
319
|
-
const ref = props.component ?? props.componentId ?? props.of
|
|
320
|
-
if (typeof ref !== 'string') return undefined
|
|
321
|
-
|
|
322
|
-
const byId = graph.getNode(ref)
|
|
323
|
-
if (byId?.type === 'COMPONENT' || byId?.type === 'COMPONENT_SET') return byId
|
|
324
|
-
|
|
325
|
-
const byName = findComponentByName(graph, ref)
|
|
326
|
-
if (byName) return byName
|
|
327
|
-
|
|
328
|
-
for (const node of graph.getAllNodes()) {
|
|
329
|
-
if (node.type === 'COMPONENT_SET' && node.name === ref) return node
|
|
330
|
-
}
|
|
331
|
-
return undefined
|
|
332
|
-
}
|
|
333
|
-
|
|
334
370
|
function resolveComponent(
|
|
335
371
|
graph: SceneGraph,
|
|
336
372
|
props: Record<string, unknown>
|
|
@@ -510,6 +546,10 @@ export function elementOverrides(
|
|
|
510
546
|
const spanOverrides = (spanProps: Record<string, unknown>) =>
|
|
511
547
|
propsToOverrides(preparePropsForRender(graph, spanProps, true, layer).props, true, 'NONE')
|
|
512
548
|
Object.assign(overrides, textContentFields(tree, props, overrides, spanOverrides))
|
|
549
|
+
// As Figma does, a text layer follows its characters until a name is given, and keeps a given
|
|
550
|
+
// name through later edits.
|
|
551
|
+
if (overrides.name) overrides.autoRename = false
|
|
552
|
+
else if (overrides.text !== undefined) overrides.name = textLayerName('TEXT', overrides.text)
|
|
513
553
|
}
|
|
514
554
|
return { overrides, bindings }
|
|
515
555
|
}
|
|
@@ -587,9 +627,11 @@ async function renderNodeContent<Artwork>(
|
|
|
587
627
|
|
|
588
628
|
for (const child of tree.children) {
|
|
589
629
|
if (typeof child === 'string' || (node.type === 'TEXT' && isTextContent(child))) continue
|
|
590
|
-
if (isTreeNode(child))
|
|
591
|
-
|
|
592
|
-
|
|
630
|
+
if (!isTreeNode(child)) continue
|
|
631
|
+
// Text holds characters and style runs, not layers: a child layer would exist in the graph but
|
|
632
|
+
// not in the text's export, so it is refused rather than silently lost.
|
|
633
|
+
if (node.type === 'TEXT') throw new Error(`<${child.type}> cannot be inside <Text>`)
|
|
634
|
+
await renderNode(services, graph, child, node.id, onNode, scope)
|
|
593
635
|
}
|
|
594
636
|
|
|
595
637
|
if (node.type === 'COMPONENT_SET') inferComponentSetProperties(graph, node.id)
|
package/src/schema.ts
CHANGED
|
@@ -54,6 +54,73 @@ export const placedOwnLayerPropsSchema = v.object({
|
|
|
54
54
|
position: v.optional(v.picklist(['absolute']))
|
|
55
55
|
})
|
|
56
56
|
|
|
57
|
+
const finiteNumberSchema = v.pipe(v.number(), v.finite())
|
|
58
|
+
|
|
59
|
+
/** A length that cannot be negative, such as a size, radius, or stroke weight. */
|
|
60
|
+
const layoutLengthSchema = v.pipe(
|
|
61
|
+
finiteNumberSchema,
|
|
62
|
+
v.transform((length) => Math.min(1_000_000, Math.max(0, length)))
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
/** A share from none to all, such as how much a corner is smoothed. */
|
|
66
|
+
const shareSchema = v.pipe(
|
|
67
|
+
finiteNumberSchema,
|
|
68
|
+
v.transform((share) => Math.min(1, Math.max(0, share)))
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The layer props every element reads by the same reader, under each name it is written by. A
|
|
73
|
+
* value of the wrong kind is reported and left out, so it never reaches a layer's fields; one out
|
|
74
|
+
* of range is bounded, as a bound variable's value is.
|
|
75
|
+
*/
|
|
76
|
+
export const layerPropsSchema = v.object({
|
|
77
|
+
name: v.optional(v.string()),
|
|
78
|
+
// An artwork's layer name, since its `name` names the icon.
|
|
79
|
+
label: v.optional(v.string()),
|
|
80
|
+
opacity: v.optional(finiteNumberSchema),
|
|
81
|
+
rotate: v.optional(finiteNumberSchema),
|
|
82
|
+
rotation: v.optional(finiteNumberSchema),
|
|
83
|
+
rounded: v.optional(layoutLengthSchema),
|
|
84
|
+
cornerRadius: v.optional(layoutLengthSchema),
|
|
85
|
+
borderRadius: v.optional(layoutLengthSchema),
|
|
86
|
+
roundedTL: v.optional(layoutLengthSchema),
|
|
87
|
+
roundedTR: v.optional(layoutLengthSchema),
|
|
88
|
+
roundedBL: v.optional(layoutLengthSchema),
|
|
89
|
+
roundedBR: v.optional(layoutLengthSchema),
|
|
90
|
+
cornerSmoothing: v.optional(shareSchema),
|
|
91
|
+
strokeWidth: v.optional(layoutLengthSchema),
|
|
92
|
+
borderWidth: v.optional(layoutLengthSchema),
|
|
93
|
+
maxLines: v.optional(v.pipe(v.number(), v.integer('expected a whole number'), v.minValue(1)))
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
/** An element's props with the layer props read by `layerPropsSchema`. */
|
|
97
|
+
export type LayerProps = Record<string, unknown> & v.InferOutput<typeof layerPropsSchema>
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The layer props an inline `<svg>` reads itself. Its other props, such as `strokeWidth="2"`,
|
|
101
|
+
* are attributes of its markup's root, which the SVG service reads as SVG does.
|
|
102
|
+
*/
|
|
103
|
+
export const svgLayerPropsSchema = v.pick(layerPropsSchema, ['label'])
|
|
104
|
+
|
|
105
|
+
/** `props` with each prop `schema` takes read by it, and every other prop as it is. */
|
|
106
|
+
export function readKnownProps<S extends ElementPropsSchema>(
|
|
107
|
+
schema: S,
|
|
108
|
+
props: Readonly<Record<string, unknown>>
|
|
109
|
+
): Record<string, unknown> & v.InferOutput<S> {
|
|
110
|
+
const read: Record<string, unknown> = { ...props }
|
|
111
|
+
for (const [key, entry] of Object.entries(schema.entries)) {
|
|
112
|
+
if (props[key] === undefined) continue
|
|
113
|
+
const parsed = v.safeParse(entry, props[key])
|
|
114
|
+
read[key] = parsed.success ? parsed.output : undefined
|
|
115
|
+
}
|
|
116
|
+
return read
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** `props` with its layer props read by `layerPropsSchema`, as every element reads them. */
|
|
120
|
+
export function readLayerProps(props: Readonly<Record<string, unknown>>): LayerProps {
|
|
121
|
+
return readKnownProps(layerPropsSchema, props)
|
|
122
|
+
}
|
|
123
|
+
|
|
57
124
|
/** A colour as a string, or as the colour a `designVar()` resolves to. */
|
|
58
125
|
const colorPropSchema = v.union(
|
|
59
126
|
[
|