@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.
- 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 +54 -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
|
@@ -0,0 +1,1065 @@
|
|
|
1
|
+
import { REKA_ELEMENTS, isTreeNode, resolveToTree } from "./behaviours/elements.js";
|
|
2
|
+
import { BLEND_MODE, CONSTRAINT, ENUM_PROPS, LayerRefs, MASK_TYPE, TEXT_ALIGN, TEXT_ALIGN_VERTICAL, TEXT_AUTO_RESIZE, TEXT_CASE, TEXT_DECORATION, TEXT_DIRECTION, applyStrokeOverrides, assignComponentProperties, assignNamedProperties, bindProperties, componentPropNames as componentPropNames$1, elementPropWarnings, exposeInstance, fillFromValue, inferComponentSetProperties, isColor, isFillValue, isVariable, parseScriptInput, readElementProps } from "./component-properties.js";
|
|
3
|
+
import { DESIGN_JSX_STYLE_KEYS, designJSXProp, renderRekaNode } from "./behaviours/render.js";
|
|
4
|
+
import { DESIGN_JSX_HELPERS, TEXT_CONTENT_NAMES, applyVariableDefinitions, bindVariableProp, designJSXHelpers, isShaderFill, isTextContent, requireVariableId, resolveForLayer, textContentFields, variableModes } from "./export/index.js";
|
|
5
|
+
import { resolveConnectorEnds } from "./connectors.js";
|
|
6
|
+
import { elementBindings, elementNodeType, elementOwnLayer, elementProps, elementSchema } from "./elements.js";
|
|
7
|
+
import { applyTextDetailOverrides, textLengthProp } from "./export/decoration.js";
|
|
8
|
+
import { paintProps, scalarProps } from "./export/bindings.js";
|
|
9
|
+
import { colorToFill, parseColor } from "@pairdraw/scene-graph/color";
|
|
10
|
+
import { TRANSPARENT } from "@pairdraw/scene-graph/constants";
|
|
11
|
+
import * as v from "valibot";
|
|
12
|
+
import { withShaderPaints } from "@pairdraw/scene-graph/shaders";
|
|
13
|
+
import { omitBy } from "es-toolkit/object";
|
|
14
|
+
import { uniq } from "es-toolkit/array";
|
|
15
|
+
import { setInstanceOverride } from "@pairdraw/scene-graph/instances/overrides";
|
|
16
|
+
import { parsePluginVectorPath, parseSVGPathData } from "@pairdraw/scene-graph/parse-path";
|
|
17
|
+
import { mergeVectorNetworks, transformVectorNetwork } from "@pairdraw/scene-graph/vector-network";
|
|
18
|
+
import { parseCSSGridTracks, parseCSSNumber, parseCSSShadows } from "@pairdraw/scene-graph/css";
|
|
19
|
+
import { autoLayoutSizingFields, fillSizingFields, parseAutoLayoutDirection, parseCounterAxisAlign, parsePrimaryAxisAlign } from "@pairdraw/scene-graph/layout";
|
|
20
|
+
import { omit } from "es-toolkit";
|
|
21
|
+
import { createDefaultNode } from "@pairdraw/scene-graph/node-defaults";
|
|
22
|
+
import { parse } from "acorn";
|
|
23
|
+
import { transform } from "sucrase";
|
|
24
|
+
//#region \0rolldown/runtime.js
|
|
25
|
+
var __defProp = Object.defineProperty;
|
|
26
|
+
var __exportAll = (all, no_symbols) => {
|
|
27
|
+
let target = {};
|
|
28
|
+
for (var name in all) __defProp(target, name, {
|
|
29
|
+
get: all[name],
|
|
30
|
+
enumerable: true
|
|
31
|
+
});
|
|
32
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/mini-react.ts
|
|
37
|
+
var mini_react_exports = /* @__PURE__ */ __exportAll({ createElement: () => createElement });
|
|
38
|
+
function createElement(type, props, ...children) {
|
|
39
|
+
const flatChildren = children.flat();
|
|
40
|
+
return {
|
|
41
|
+
type,
|
|
42
|
+
props: {
|
|
43
|
+
...props,
|
|
44
|
+
children: flatChildren.length > 0 ? flatChildren : void 0
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/overrides/effects.ts
|
|
50
|
+
function isEffect(value) {
|
|
51
|
+
return value !== null && typeof value === "object" && "type" in value && "radius" in value && "visible" in value;
|
|
52
|
+
}
|
|
53
|
+
/** `effects` takes structured effects; `shadow` appends its shadows and `blur` a layer blur. */
|
|
54
|
+
function applyEffectOverrides(props, o) {
|
|
55
|
+
if (Array.isArray(props.effects)) {
|
|
56
|
+
const effects = props.effects.filter(isEffect).map((effect) => structuredClone(effect));
|
|
57
|
+
if (effects.length > 0) o.effects = effects;
|
|
58
|
+
}
|
|
59
|
+
const shadows = typeof props.shadow === "string" ? parseCSSShadows(props.shadow) : [];
|
|
60
|
+
if (shadows.length > 0) o.effects = [...o.effects ?? [], ...shadows];
|
|
61
|
+
if (typeof props.blur === "number") o.effects = [...o.effects ?? [], {
|
|
62
|
+
type: "LAYER_BLUR",
|
|
63
|
+
radius: props.blur,
|
|
64
|
+
visible: true,
|
|
65
|
+
color: { ...TRANSPARENT },
|
|
66
|
+
offset: {
|
|
67
|
+
x: 0,
|
|
68
|
+
y: 0
|
|
69
|
+
},
|
|
70
|
+
spread: 0
|
|
71
|
+
}];
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/overrides/state.ts
|
|
75
|
+
function constraintValue(value) {
|
|
76
|
+
return CONSTRAINT.parse(value);
|
|
77
|
+
}
|
|
78
|
+
function isConstraintsValue(value) {
|
|
79
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
80
|
+
}
|
|
81
|
+
function applyConstraints(value, o) {
|
|
82
|
+
if (!isConstraintsValue(value)) return;
|
|
83
|
+
const h = constraintValue(value.horizontal);
|
|
84
|
+
const v = constraintValue(value.vertical);
|
|
85
|
+
if (h) o.horizontalConstraint = h;
|
|
86
|
+
if (v) o.verticalConstraint = v;
|
|
87
|
+
}
|
|
88
|
+
function sizeLimit(value) {
|
|
89
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Visibility, locking, mask, constraint, and size-limit props. Constraints follow Figma's
|
|
93
|
+
* `constraints` object with its values in lowercase, as `blendMode` is.
|
|
94
|
+
*/
|
|
95
|
+
function applyStateOverrides(props, o) {
|
|
96
|
+
if (typeof props.visible === "boolean") o.visible = props.visible;
|
|
97
|
+
if (typeof props.locked === "boolean") o.locked = props.locked;
|
|
98
|
+
applyConstraints(props.constraints, o);
|
|
99
|
+
const minWidth = sizeLimit(props.minW);
|
|
100
|
+
const maxWidth = sizeLimit(props.maxW);
|
|
101
|
+
const minHeight = sizeLimit(props.minH);
|
|
102
|
+
const maxHeight = sizeLimit(props.maxH);
|
|
103
|
+
if (minWidth !== void 0) o.minWidth = minWidth;
|
|
104
|
+
if (maxWidth !== void 0) o.maxWidth = maxWidth;
|
|
105
|
+
if (minHeight !== void 0) o.minHeight = minHeight;
|
|
106
|
+
if (maxHeight !== void 0) o.maxHeight = maxHeight;
|
|
107
|
+
const maskType = props.mask === true ? "ALPHA" : MASK_TYPE.parse(props.mask);
|
|
108
|
+
if (maskType) {
|
|
109
|
+
o.isMask = true;
|
|
110
|
+
o.maskType = maskType;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/props-overrides.ts
|
|
115
|
+
const WEIGHT_MAP = {
|
|
116
|
+
normal: 400,
|
|
117
|
+
medium: 500,
|
|
118
|
+
bold: 700
|
|
119
|
+
};
|
|
120
|
+
function parseDirection(value) {
|
|
121
|
+
if (typeof value !== "string") return void 0;
|
|
122
|
+
return TEXT_DIRECTION.parse(value) ?? "AUTO";
|
|
123
|
+
}
|
|
124
|
+
function numberFromPx(value) {
|
|
125
|
+
if (typeof value === "number") return value;
|
|
126
|
+
return typeof value === "string" ? parseCSSNumber(value) ?? void 0 : void 0;
|
|
127
|
+
}
|
|
128
|
+
/** `props` with the sizes and styles its `style` object gives, where the props leave them unset. */
|
|
129
|
+
function normalizeStyleProps(props) {
|
|
130
|
+
const style = props.style;
|
|
131
|
+
if (style === null || typeof style !== "object" || Array.isArray(style)) return props;
|
|
132
|
+
const source = style;
|
|
133
|
+
const normalized = { ...props };
|
|
134
|
+
for (const [name, keys] of Object.entries(DESIGN_JSX_STYLE_KEYS)) {
|
|
135
|
+
if (designJSXProp(normalized, name) !== void 0) continue;
|
|
136
|
+
const found = keys.find(({ key }) => source[key] !== void 0);
|
|
137
|
+
if (found) normalized[name] = found.px ? numberFromPx(source[found.key]) : source[found.key];
|
|
138
|
+
}
|
|
139
|
+
return normalized;
|
|
140
|
+
}
|
|
141
|
+
function applySizeOverrides(props, o, parentLayout) {
|
|
142
|
+
const w = designJSXProp(props, "w");
|
|
143
|
+
const h = designJSXProp(props, "h");
|
|
144
|
+
if (typeof w === "number") o.width = w;
|
|
145
|
+
if (typeof h === "number") o.height = h;
|
|
146
|
+
if (w === "fill") Object.assign(o, fillSizingFields(parentLayout, "HORIZONTAL"));
|
|
147
|
+
if (h === "fill") Object.assign(o, fillSizingFields(parentLayout, "VERTICAL"));
|
|
148
|
+
if (props.x !== void 0) o.x = props.x;
|
|
149
|
+
if (props.y !== void 0) o.y = props.y;
|
|
150
|
+
if (props.top !== void 0) o.y = props.top;
|
|
151
|
+
if (props.left !== void 0) o.x = props.left;
|
|
152
|
+
if (props.position === "absolute") o.layoutPositioning = "ABSOLUTE";
|
|
153
|
+
if ((props.x !== void 0 || props.y !== void 0 || props.top !== void 0 || props.left !== void 0) && parentLayout !== "NONE") o.layoutPositioning = "ABSOLUTE";
|
|
154
|
+
return {
|
|
155
|
+
w,
|
|
156
|
+
h
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Sets the layer's fills, moving each shader paint's preset into the layer's plugin data,
|
|
161
|
+
* where a shader paint keeps it.
|
|
162
|
+
*/
|
|
163
|
+
function setFills(values, o) {
|
|
164
|
+
const shaders = [];
|
|
165
|
+
const fills = values.map((value) => {
|
|
166
|
+
const fill = fillFromValue(value);
|
|
167
|
+
if (!isShaderFill(fill)) return fill;
|
|
168
|
+
const { shader, ...paint } = fill;
|
|
169
|
+
if (paint.imageHash) shaders.push(shader);
|
|
170
|
+
return paint;
|
|
171
|
+
});
|
|
172
|
+
if (fills.length === 0) return;
|
|
173
|
+
o.fills = fills;
|
|
174
|
+
if (shaders.length > 0) o.pluginData = withShaderPaints({ pluginData: o.pluginData ?? [] }, fills, shaders);
|
|
175
|
+
}
|
|
176
|
+
function applyFillOverride(props, o) {
|
|
177
|
+
if (Array.isArray(props.fills) && props.fills.length === 0) {
|
|
178
|
+
o.fills = [];
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (Array.isArray(props.fills)) {
|
|
182
|
+
setFills(props.fills.filter(isFillValue), o);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const bg = designJSXProp(props, "bg");
|
|
186
|
+
if (isFillValue(bg)) setFills([bg], o);
|
|
187
|
+
}
|
|
188
|
+
function applyCornerOverrides(props, o) {
|
|
189
|
+
const rounded = designJSXProp(props, "rounded");
|
|
190
|
+
if (typeof rounded === "number") o.cornerRadius = rounded;
|
|
191
|
+
if (props.roundedTL !== void 0 || props.roundedTR !== void 0 || props.roundedBL !== void 0 || props.roundedBR !== void 0) {
|
|
192
|
+
o.independentCorners = true;
|
|
193
|
+
if (props.roundedTL !== void 0) o.topLeftRadius = props.roundedTL;
|
|
194
|
+
if (props.roundedTR !== void 0) o.topRightRadius = props.roundedTR;
|
|
195
|
+
if (props.roundedBL !== void 0) o.bottomLeftRadius = props.roundedBL;
|
|
196
|
+
if (props.roundedBR !== void 0) o.bottomRightRadius = props.roundedBR;
|
|
197
|
+
}
|
|
198
|
+
if (props.cornerSmoothing !== void 0) o.cornerSmoothing = props.cornerSmoothing;
|
|
199
|
+
}
|
|
200
|
+
function applyVisualOverrides(props, o) {
|
|
201
|
+
applyFillOverride(props, o);
|
|
202
|
+
applyStrokeOverrides(props, o);
|
|
203
|
+
applyCornerOverrides(props, o);
|
|
204
|
+
if (props.opacity !== void 0) o.opacity = props.opacity;
|
|
205
|
+
applyTransformOverrides(props, o);
|
|
206
|
+
const blendMode = BLEND_MODE.parse(props.blendMode);
|
|
207
|
+
if (blendMode) o.blendMode = blendMode;
|
|
208
|
+
if (props.overflow === "hidden") o.clipsContent = true;
|
|
209
|
+
}
|
|
210
|
+
function applyTransformOverrides(props, o) {
|
|
211
|
+
const rotation = designJSXProp(props, "rotate");
|
|
212
|
+
if (rotation !== void 0) o.rotation = rotation;
|
|
213
|
+
if (props.flipX !== void 0) o.flipX = props.flipX === true;
|
|
214
|
+
if (props.flipY !== void 0) o.flipY = props.flipY === true;
|
|
215
|
+
}
|
|
216
|
+
function applyPaddingOverrides(props, o) {
|
|
217
|
+
const p = designJSXProp(props, "p");
|
|
218
|
+
if (typeof p === "number") {
|
|
219
|
+
o.paddingTop = p;
|
|
220
|
+
o.paddingRight = p;
|
|
221
|
+
o.paddingBottom = p;
|
|
222
|
+
o.paddingLeft = p;
|
|
223
|
+
}
|
|
224
|
+
const px = props.px;
|
|
225
|
+
const py = props.py;
|
|
226
|
+
if (px !== void 0) {
|
|
227
|
+
o.paddingLeft = px;
|
|
228
|
+
o.paddingRight = px;
|
|
229
|
+
}
|
|
230
|
+
if (py !== void 0) {
|
|
231
|
+
o.paddingTop = py;
|
|
232
|
+
o.paddingBottom = py;
|
|
233
|
+
}
|
|
234
|
+
if (props.pt !== void 0) o.paddingTop = props.pt;
|
|
235
|
+
if (props.pr !== void 0) o.paddingRight = props.pr;
|
|
236
|
+
if (props.pb !== void 0) o.paddingBottom = props.pb;
|
|
237
|
+
if (props.pl !== void 0) o.paddingLeft = props.pl;
|
|
238
|
+
}
|
|
239
|
+
const AUTO_LAYOUT_TRIGGER_KEYS = [
|
|
240
|
+
...[
|
|
241
|
+
"p",
|
|
242
|
+
"padding",
|
|
243
|
+
"px",
|
|
244
|
+
"py",
|
|
245
|
+
"pt",
|
|
246
|
+
"pr",
|
|
247
|
+
"pb",
|
|
248
|
+
"pl"
|
|
249
|
+
],
|
|
250
|
+
"justify",
|
|
251
|
+
"justifyContent",
|
|
252
|
+
"items",
|
|
253
|
+
"align",
|
|
254
|
+
"alignItems"
|
|
255
|
+
];
|
|
256
|
+
function hasAutoLayoutTriggerProps(props) {
|
|
257
|
+
return AUTO_LAYOUT_TRIGGER_KEYS.some((k) => props[k] !== void 0);
|
|
258
|
+
}
|
|
259
|
+
function applyGridOverrides(props, o, w, h) {
|
|
260
|
+
o.layoutMode = "GRID";
|
|
261
|
+
if (typeof w === "number") o.width = w;
|
|
262
|
+
if (typeof h === "number") o.height = h;
|
|
263
|
+
if (typeof props.columns === "string") o.gridTemplateColumns = parseCSSGridTracks(props.columns);
|
|
264
|
+
else if (typeof props.columns === "number") o.gridTemplateColumns = Array.from({ length: props.columns }, () => ({
|
|
265
|
+
sizing: "FR",
|
|
266
|
+
value: 1
|
|
267
|
+
}));
|
|
268
|
+
if (typeof props.rows === "string") o.gridTemplateRows = parseCSSGridTracks(props.rows);
|
|
269
|
+
else if (typeof props.rows === "number") o.gridTemplateRows = Array.from({ length: props.rows }, () => ({
|
|
270
|
+
sizing: "FR",
|
|
271
|
+
value: 1
|
|
272
|
+
}));
|
|
273
|
+
if (typeof props.columnGap === "number") o.gridColumnGap = props.columnGap;
|
|
274
|
+
if (typeof props.rowGap === "number") o.gridRowGap = props.rowGap;
|
|
275
|
+
if (typeof props.gap === "number") {
|
|
276
|
+
o.gridColumnGap = props.gap;
|
|
277
|
+
o.gridRowGap = props.gap;
|
|
278
|
+
}
|
|
279
|
+
if (props.rows === void 0 && typeof h !== "number") o.height = 0;
|
|
280
|
+
}
|
|
281
|
+
function applyGridChildOverrides(props, o) {
|
|
282
|
+
const col = designJSXProp(props, "colStart");
|
|
283
|
+
const row = designJSXProp(props, "rowStart");
|
|
284
|
+
const colSpan = props.colSpan ?? 1;
|
|
285
|
+
const rowSpan = props.rowSpan ?? 1;
|
|
286
|
+
if (col !== void 0 || row !== void 0) o.gridPosition = {
|
|
287
|
+
column: col ?? 0,
|
|
288
|
+
row: row ?? 0,
|
|
289
|
+
columnSpan: colSpan,
|
|
290
|
+
rowSpan
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function applyAutoLayoutSizing(o, props, w, h) {
|
|
294
|
+
const flex = typeof props.flex === "string" ? props.flex : "col";
|
|
295
|
+
o.layoutMode = parseAutoLayoutDirection(flex) ?? "HORIZONTAL";
|
|
296
|
+
const own = (dim) => typeof dim === "number" ? "FIXED" : "HUG";
|
|
297
|
+
Object.assign(o, autoLayoutSizingFields(o.layoutMode, own(w), own(h)));
|
|
298
|
+
}
|
|
299
|
+
function applyLayoutAlignmentOverrides(props, o) {
|
|
300
|
+
const justify = designJSXProp(props, "justify");
|
|
301
|
+
if (typeof justify === "string" && justify) o.primaryAxisAlign = parsePrimaryAxisAlign(justify) ?? "MIN";
|
|
302
|
+
const items = designJSXProp(props, "items");
|
|
303
|
+
if (typeof items === "string" && items) o.counterAxisAlign = parseCounterAxisAlign(items) ?? "MIN";
|
|
304
|
+
}
|
|
305
|
+
function shouldEnableAutoLayout(props, isText) {
|
|
306
|
+
if (props.flex !== void 0) return true;
|
|
307
|
+
if (!isText && hasAutoLayoutTriggerProps(props)) return true;
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
function applyLayoutOverrides(props, o, w, h, isText, parentLayout) {
|
|
311
|
+
if (props.grid) {
|
|
312
|
+
applyGridOverrides(props, o, w, h);
|
|
313
|
+
applyPaddingOverrides(props, o);
|
|
314
|
+
if (props.grow !== void 0) o.layoutGrow = props.grow;
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (parentLayout === "GRID") applyGridChildOverrides(props, o);
|
|
318
|
+
if (shouldEnableAutoLayout(props, isText)) applyAutoLayoutSizing(o, props, w, h);
|
|
319
|
+
o.layoutDirection = parseDirection(props.flow ?? (!isText ? props.dir : void 0)) ?? o.layoutDirection;
|
|
320
|
+
if (props.gap !== void 0) o.itemSpacing = props.gap;
|
|
321
|
+
if (props.wrap) {
|
|
322
|
+
o.layoutWrap = "WRAP";
|
|
323
|
+
if (props.rowGap !== void 0) o.counterAxisSpacing = props.rowGap;
|
|
324
|
+
}
|
|
325
|
+
applyLayoutAlignmentOverrides(props, o);
|
|
326
|
+
applyPaddingOverrides(props, o);
|
|
327
|
+
if (props.grow !== void 0) o.layoutGrow = props.grow;
|
|
328
|
+
}
|
|
329
|
+
function applyTextStyleOverrides(props, o) {
|
|
330
|
+
const fontSize = designJSXProp(props, "size");
|
|
331
|
+
if (typeof fontSize === "number") o.fontSize = fontSize;
|
|
332
|
+
const fontFamily = designJSXProp(props, "font");
|
|
333
|
+
if (typeof fontFamily === "string") o.fontFamily = fontFamily;
|
|
334
|
+
const weight = designJSXProp(props, "weight");
|
|
335
|
+
if (typeof props.italic === "boolean") o.italic = props.italic;
|
|
336
|
+
if (typeof weight === "number") o.fontWeight = weight;
|
|
337
|
+
else if (typeof weight === "string") o.fontWeight = WEIGHT_MAP[weight] ?? 400;
|
|
338
|
+
if (typeof props.color === "string" || isColor(props.color)) o.fills = [colorToFill(props.color)];
|
|
339
|
+
const lineHeight = props.lineHeight === "auto" ? null : textLengthProp(props.lineHeight);
|
|
340
|
+
if (lineHeight !== void 0) o.lineHeight = lineHeight;
|
|
341
|
+
const letterSpacing = textLengthProp(props.letterSpacing);
|
|
342
|
+
if (letterSpacing) o.letterSpacing = letterSpacing;
|
|
343
|
+
const textDecoration = TEXT_DECORATION.parse(props.textDecoration);
|
|
344
|
+
if (textDecoration) o.textDecoration = textDecoration;
|
|
345
|
+
applyTextDetailOverrides(props, o);
|
|
346
|
+
const textCase = TEXT_CASE.parse(props.textCase);
|
|
347
|
+
if (textCase) o.textCase = textCase;
|
|
348
|
+
if (props.maxLines !== void 0) {
|
|
349
|
+
o.maxLines = props.maxLines;
|
|
350
|
+
o.textTruncation = "ENDING";
|
|
351
|
+
}
|
|
352
|
+
if (props.truncate) o.textTruncation = "ENDING";
|
|
353
|
+
applyTextAlignmentOverrides(props, o);
|
|
354
|
+
}
|
|
355
|
+
function applyTextAlignmentOverrides(props, o) {
|
|
356
|
+
const textAlign = designJSXProp(props, "textAlign");
|
|
357
|
+
if (typeof textAlign === "string") o.textAlignHorizontal = TEXT_ALIGN.parse(textAlign) ?? "LEFT";
|
|
358
|
+
const textAlignVertical = designJSXProp(props, "textAlignVertical");
|
|
359
|
+
if (typeof textAlignVertical === "string") o.textAlignVertical = TEXT_ALIGN_VERTICAL.parse(textAlignVertical) ?? "TOP";
|
|
360
|
+
}
|
|
361
|
+
function applyTextAutoResize(props, o, parentLayout) {
|
|
362
|
+
const w = designJSXProp(props, "w");
|
|
363
|
+
const hasExplicitWidth = w !== void 0;
|
|
364
|
+
const fillsParent = w === "fill" || props.grow > 0;
|
|
365
|
+
const isInsideAutoLayout = parentLayout !== "NONE";
|
|
366
|
+
if (props.textAutoResize) o.textAutoResize = TEXT_AUTO_RESIZE.parse(props.textAutoResize) ?? "NONE";
|
|
367
|
+
else if (hasExplicitWidth || isInsideAutoLayout && fillsParent) o.textAutoResize = "HEIGHT";
|
|
368
|
+
else o.textAutoResize = "WIDTH_AND_HEIGHT";
|
|
369
|
+
}
|
|
370
|
+
function applyTextOverrides(props, o, parentLayout) {
|
|
371
|
+
applyTextStyleOverrides(props, o);
|
|
372
|
+
o.textDirection = parseDirection(props.dir) ?? o.textDirection;
|
|
373
|
+
applyTextAutoResize(props, o, parentLayout);
|
|
374
|
+
}
|
|
375
|
+
const FULL_TURN_DEGREES = 360;
|
|
376
|
+
function numberProp(value) {
|
|
377
|
+
return typeof value === "number" ? value : void 0;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* An `<Ellipse>`'s arc from its props: angles in degrees, clockwise from three o'clock, as the
|
|
381
|
+
* canvas draws arcs, and the hole as a share of the ellipse. Null when none is set.
|
|
382
|
+
*/
|
|
383
|
+
function arcOverride(props) {
|
|
384
|
+
const start = numberProp(props.startAngle);
|
|
385
|
+
const end = numberProp(props.endAngle);
|
|
386
|
+
const innerRadius = numberProp(props.innerRadius);
|
|
387
|
+
if (start === void 0 && end === void 0 && innerRadius === void 0) return null;
|
|
388
|
+
const startDegrees = start ?? 0;
|
|
389
|
+
return {
|
|
390
|
+
startingAngle: startDegrees * Math.PI / 180,
|
|
391
|
+
endingAngle: (end ?? startDegrees + FULL_TURN_DEGREES) * Math.PI / 180,
|
|
392
|
+
innerRadius: innerRadius ?? 0
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
/** The fields a shape element's own props set, by the layer it makes. */
|
|
396
|
+
function shapeOverrides(nodeType, props) {
|
|
397
|
+
const o = {};
|
|
398
|
+
if (nodeType === "STAR" || nodeType === "POLYGON") {
|
|
399
|
+
const points = numberProp(props.pointCount) ?? numberProp(props.points);
|
|
400
|
+
if (points !== void 0) o.pointCount = points;
|
|
401
|
+
}
|
|
402
|
+
if (nodeType === "STAR") {
|
|
403
|
+
const innerRadius = numberProp(props.innerRadius);
|
|
404
|
+
if (innerRadius !== void 0) o.starInnerRadius = innerRadius;
|
|
405
|
+
}
|
|
406
|
+
if (nodeType === "ELLIPSE") {
|
|
407
|
+
const arc = arcOverride(props);
|
|
408
|
+
if (arc) o.arcData = arc;
|
|
409
|
+
}
|
|
410
|
+
return o;
|
|
411
|
+
}
|
|
412
|
+
function propsToOverrides(props, isText, parentLayout) {
|
|
413
|
+
props = normalizeStyleProps(props);
|
|
414
|
+
const o = {};
|
|
415
|
+
if (props.name) o.name = props.name;
|
|
416
|
+
const { w, h } = applySizeOverrides(props, o, parentLayout);
|
|
417
|
+
applyVisualOverrides(props, o);
|
|
418
|
+
applyLayoutOverrides(props, o, w, h, isText, parentLayout);
|
|
419
|
+
if (isText) applyTextOverrides(props, o, parentLayout);
|
|
420
|
+
applyEffectOverrides(props, o);
|
|
421
|
+
applyStateOverrides(props, o);
|
|
422
|
+
return o;
|
|
423
|
+
}
|
|
424
|
+
//#endregion
|
|
425
|
+
//#region src/overrides/vector.ts
|
|
426
|
+
const FILL_RULES = {
|
|
427
|
+
nonzero: "NONZERO",
|
|
428
|
+
evenodd: "EVENODD"
|
|
429
|
+
};
|
|
430
|
+
const vectorPropsSchema = v.pipe(v.object({
|
|
431
|
+
d: v.optional(v.pipe(v.string(), v.trim(), v.nonEmpty())),
|
|
432
|
+
fillRule: v.optional(v.picklist(["nonzero", "evenodd"])),
|
|
433
|
+
paths: v.optional(v.array(v.object({
|
|
434
|
+
windingRule: v.picklist([
|
|
435
|
+
"NONZERO",
|
|
436
|
+
"EVENODD",
|
|
437
|
+
"NONE"
|
|
438
|
+
]),
|
|
439
|
+
data: v.string()
|
|
440
|
+
})))
|
|
441
|
+
}), v.check((props) => !(props.d && props.paths), "Expected `d` or `paths`, not both"));
|
|
442
|
+
function parsedNetwork(result, which) {
|
|
443
|
+
if (!result.ok) throw new Error(`Invalid <Vector> ${which}: ${result.error}`);
|
|
444
|
+
return result.network;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* The vector network a `<Vector>`'s `d` or `paths` draws, in the coordinates they are written
|
|
448
|
+
* in, or null when it has neither. `d` reads as SVG does, its open subpaths filled only when
|
|
449
|
+
* `fillsOpenSubpaths`; `paths` as Figma's `vectorPaths`.
|
|
450
|
+
*/
|
|
451
|
+
function vectorNetworkProp(props, fillsOpenSubpaths) {
|
|
452
|
+
const parsed = parseScriptInput("Invalid <Vector> props", vectorPropsSchema, props);
|
|
453
|
+
if (parsed.d) {
|
|
454
|
+
const windingRule = FILL_RULES[parsed.fillRule ?? "nonzero"];
|
|
455
|
+
return parsedNetwork(parseSVGPathData(parsed.d, windingRule, { includeOpenRegions: fillsOpenSubpaths }), "`d`");
|
|
456
|
+
}
|
|
457
|
+
if (!parsed.paths) return null;
|
|
458
|
+
return mergeVectorNetworks(parsed.paths.map(({ windingRule, data }, index) => parsedNetwork(parsePluginVectorPath(data, windingRule), `paths[${index}]`)));
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* The size of `node` on the axes `props` set, by `w`/`width` and `h`/`height`, however they set
|
|
462
|
+
* them: a number, a variable, or a layout value. The layer already holds what they resolve to.
|
|
463
|
+
*/
|
|
464
|
+
function vectorSize(props, node) {
|
|
465
|
+
const sized = normalizeStyleProps(props);
|
|
466
|
+
return {
|
|
467
|
+
width: designJSXProp(sized, "w") === void 0 ? void 0 : node.width,
|
|
468
|
+
height: designJSXProp(sized, "h") === void 0 ? void 0 : node.height
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* The fields that place a vector network in its layer. On an axis the author sized, the path is
|
|
473
|
+
* drawn in the layer's own box as written, as an SVG icon is in its view box. On an axis left
|
|
474
|
+
* unsized, the layer takes the network's extent and moves to where it starts, as Figma's
|
|
475
|
+
* `vectorPaths` setter does.
|
|
476
|
+
*/
|
|
477
|
+
function vectorGeometry(network, bounds, size, position) {
|
|
478
|
+
const dx = size.width === void 0 ? bounds.x : 0;
|
|
479
|
+
const dy = size.height === void 0 ? bounds.y : 0;
|
|
480
|
+
return {
|
|
481
|
+
x: position.x + dx,
|
|
482
|
+
y: position.y + dy,
|
|
483
|
+
width: size.width ?? bounds.width,
|
|
484
|
+
height: size.height ?? bounds.height,
|
|
485
|
+
vectorNetwork: transformVectorNetwork([
|
|
486
|
+
1,
|
|
487
|
+
0,
|
|
488
|
+
-dx,
|
|
489
|
+
0,
|
|
490
|
+
1,
|
|
491
|
+
-dy,
|
|
492
|
+
0,
|
|
493
|
+
0,
|
|
494
|
+
1
|
|
495
|
+
], network)
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* A `<Vector>`'s `d` or `paths` as its vector network, the layer sized to it. `vectorBounds`
|
|
500
|
+
* measures the network, curves included, which the engine provides.
|
|
501
|
+
*/
|
|
502
|
+
function placeVectorPaths(graph, node, props, vectorBounds) {
|
|
503
|
+
if (node.type !== "VECTOR") return;
|
|
504
|
+
const network = vectorNetworkProp(props, node.fills.some((fill) => fill.visible) && !node.strokes.some((stroke) => stroke.visible));
|
|
505
|
+
if (!network) return;
|
|
506
|
+
graph.updateNode(node.id, vectorGeometry(network, vectorBounds(network), vectorSize(props, node), node));
|
|
507
|
+
}
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/scalar-bindings.ts
|
|
510
|
+
/** Resolve numeric props before sizing inference; retain real graph bindings afterward. */
|
|
511
|
+
function prepareScalarBindings(graph, props, bindings, isText, resolve) {
|
|
512
|
+
const entries = scalarProps({
|
|
513
|
+
text: isText,
|
|
514
|
+
grid: !!props.grid,
|
|
515
|
+
wrap: !!props.wrap
|
|
516
|
+
});
|
|
517
|
+
const scalarBindings = /* @__PURE__ */ new Map();
|
|
518
|
+
for (const { prop: key, fields } of entries) {
|
|
519
|
+
const value = props[key];
|
|
520
|
+
if (value === void 0) continue;
|
|
521
|
+
for (const field of fields) scalarBindings.delete(field);
|
|
522
|
+
if (!isVariable(value)) continue;
|
|
523
|
+
const variable = graph.variables.get(requireVariableId(graph, value));
|
|
524
|
+
if (variable?.type !== "FLOAT") throw new Error(`Expected a FLOAT variable for ${key}: ${value.name}`);
|
|
525
|
+
const resolved = resolve(variable.id);
|
|
526
|
+
if (typeof resolved !== "number" || !Number.isFinite(resolved)) throw new TypeError(`Cannot resolve numeric variable for ${key}: ${value.name}`);
|
|
527
|
+
props[key] = resolved;
|
|
528
|
+
for (const field of fields) scalarBindings.set(field, variable.id);
|
|
529
|
+
}
|
|
530
|
+
Object.assign(bindings, Object.fromEntries(scalarBindings));
|
|
531
|
+
}
|
|
532
|
+
//#endregion
|
|
533
|
+
//#region src/renderer.ts
|
|
534
|
+
/** The nodes a tree renders as: a fragment's children, or the tree itself. */
|
|
535
|
+
function treeRoots(tree) {
|
|
536
|
+
return tree.type === "" ? tree.children.filter(isTreeNode) : [tree];
|
|
537
|
+
}
|
|
538
|
+
/** Render every root of `tree` into the parent and lay out once. */
|
|
539
|
+
async function renderRoots(services, graph, tree, options = {}) {
|
|
540
|
+
const roots = treeRoots(tree);
|
|
541
|
+
if (roots.length === 0) throw new Error("JSX must return a Figma element (Frame, Text, etc)");
|
|
542
|
+
const parentId = options.parentId ?? graph.getPages()[0].id;
|
|
543
|
+
applyVariableDefinitions(graph, tree);
|
|
544
|
+
const nodes = [];
|
|
545
|
+
const connectors = [];
|
|
546
|
+
const refs = new LayerRefs();
|
|
547
|
+
const onNode = (element, node) => {
|
|
548
|
+
if (element.type === "connector") connectors.push({
|
|
549
|
+
node,
|
|
550
|
+
tree: element
|
|
551
|
+
});
|
|
552
|
+
else refs.mark(element.props.ref, node);
|
|
553
|
+
options.onNode?.(element, node);
|
|
554
|
+
};
|
|
555
|
+
for (const root of roots) {
|
|
556
|
+
const node = await renderNode(services, graph, root, parentId, onNode);
|
|
557
|
+
if (options.x !== void 0) graph.updateNode(node.id, { x: options.x });
|
|
558
|
+
if (options.y !== void 0) graph.updateNode(node.id, { y: options.y });
|
|
559
|
+
nodes.push(node);
|
|
560
|
+
}
|
|
561
|
+
const rootIds = nodes.map((node) => node.id);
|
|
562
|
+
const warnings = [...refs.warnings, ...resolveConnectorEnds(graph, rootIds, connectors, refs)];
|
|
563
|
+
if (!options.deferLayout) services.layout(graph, rootIds);
|
|
564
|
+
return nodes.map((node) => ({
|
|
565
|
+
id: node.id,
|
|
566
|
+
name: node.name,
|
|
567
|
+
type: node.type,
|
|
568
|
+
childIds: node.childIds,
|
|
569
|
+
...warnings.length > 0 && { warnings }
|
|
570
|
+
}));
|
|
571
|
+
}
|
|
572
|
+
/** Render `tree` and return its first root; a fragment's other roots are rendered too. */
|
|
573
|
+
async function renderTree(services, graph, tree, options = {}) {
|
|
574
|
+
const [first] = await renderRoots(services, graph, tree, options);
|
|
575
|
+
return first;
|
|
576
|
+
}
|
|
577
|
+
function isObjectRecord(value) {
|
|
578
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
579
|
+
}
|
|
580
|
+
function preparePropsForRender(graph, source, isText, layer) {
|
|
581
|
+
const props = { ...source };
|
|
582
|
+
const bindings = {};
|
|
583
|
+
if (Array.isArray(props.fills)) props.fills = props.fills.map((value, index) => {
|
|
584
|
+
const fill = { value };
|
|
585
|
+
bindVariableProp(graph, fill, bindings, "value", `fills/${index}/color`, layer);
|
|
586
|
+
return fill.value;
|
|
587
|
+
});
|
|
588
|
+
for (const { prop, fields } of paintProps({ text: isText })) bindVariableProp(graph, props, bindings, prop, fields[0], layer);
|
|
589
|
+
if (isObjectRecord(props.style)) {
|
|
590
|
+
const style = { ...props.style };
|
|
591
|
+
for (const key of ["background", "backgroundColor"]) bindVariableProp(graph, style, bindings, key, "fills/0/color", layer);
|
|
592
|
+
if (isText) bindVariableProp(graph, style, bindings, "color", "fills/0/color", layer);
|
|
593
|
+
bindVariableProp(graph, style, bindings, "borderColor", "strokes/0/color", layer);
|
|
594
|
+
props.style = style;
|
|
595
|
+
}
|
|
596
|
+
prepareScalarBindings(graph, props, bindings, isText, (variableId) => resolveForLayer(graph, variableId, layer.parentId, layer.modes));
|
|
597
|
+
if (isObjectRecord(props.bind)) {
|
|
598
|
+
for (const [field, value] of Object.entries(props.bind)) if (isVariable(value)) bindings[field] = requireVariableId(graph, value);
|
|
599
|
+
else if (typeof value === "string") bindings[field] = value;
|
|
600
|
+
}
|
|
601
|
+
return {
|
|
602
|
+
props,
|
|
603
|
+
bindings
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* An element that draws its own layer. Its props are resolved as any layer's are, so a
|
|
608
|
+
* `designVar()` binds, and its layer props become fields as any layer's do; the element reads the
|
|
609
|
+
* rest itself.
|
|
610
|
+
*/
|
|
611
|
+
function renderOwnLayerElement(graph, tree, parentId, element) {
|
|
612
|
+
const modes = variableModes(graph, tree.props.mode);
|
|
613
|
+
const { props, bindings } = preparePropsForRender(graph, tree.props, false, {
|
|
614
|
+
parentId,
|
|
615
|
+
modes
|
|
616
|
+
});
|
|
617
|
+
const parentLayout = graph.getNode(parentId)?.layoutMode ?? "NONE";
|
|
618
|
+
const layer = omitBy({
|
|
619
|
+
...propsToOverrides(readElementProps(element.layer, props), false, parentLayout),
|
|
620
|
+
...modes && { variableModes: modes }
|
|
621
|
+
}, (value) => value === void 0);
|
|
622
|
+
const node = element.render(graph, {
|
|
623
|
+
...tree,
|
|
624
|
+
props
|
|
625
|
+
}, parentId, layer);
|
|
626
|
+
for (const [field, variableId] of Object.entries(bindings)) graph.bindVariable(element.bindingTarget?.(graph, node, field) ?? node.id, field, variableId);
|
|
627
|
+
return node;
|
|
628
|
+
}
|
|
629
|
+
function applyBindings(graph, nodeId, bindings) {
|
|
630
|
+
for (const [field, variableId] of Object.entries(bindings)) graph.bindVariable(nodeId, field, variableId);
|
|
631
|
+
}
|
|
632
|
+
function applyIconSize(props, overrides, parentLayout, size) {
|
|
633
|
+
const { w, h } = applySizeOverrides(props, overrides, parentLayout);
|
|
634
|
+
if (typeof w !== "number") overrides.width = size;
|
|
635
|
+
if (typeof h !== "number") overrides.height = size;
|
|
636
|
+
}
|
|
637
|
+
async function renderIconNode(services, graph, tree, parentId) {
|
|
638
|
+
const props = tree.props;
|
|
639
|
+
const iconName = props.name;
|
|
640
|
+
if (!iconName) throw new Error("<Icon> requires a name prop (e.g. name=\"lucide:heart\")");
|
|
641
|
+
const size = props.size ?? 24;
|
|
642
|
+
const artwork = await services.icon(iconName, size);
|
|
643
|
+
if (!artwork) throw new Error(`Icon "${iconName}" not found`);
|
|
644
|
+
return placeArtwork(services, graph, artwork, props, size, parentId);
|
|
645
|
+
}
|
|
646
|
+
function placeArtwork(services, graph, artwork, props, size, parentId) {
|
|
647
|
+
const parentLayout = graph.getNode(parentId)?.layoutMode ?? "NONE";
|
|
648
|
+
const overrides = {};
|
|
649
|
+
if (props.label) overrides.name = props.label;
|
|
650
|
+
applyIconSize(props, overrides, parentLayout, size);
|
|
651
|
+
const modes = variableModes(graph, props.mode);
|
|
652
|
+
if (modes) overrides.variableModes = modes;
|
|
653
|
+
const variableId = isVariable(props.color) ? requireVariableId(graph, props.color) : void 0;
|
|
654
|
+
const resolved = variableId && resolveForLayer(graph, variableId, parentId, modes);
|
|
655
|
+
if (isVariable(props.color) && typeof resolved !== "object") throw new Error(`Cannot resolve color variable ${props.color.name} for color`);
|
|
656
|
+
const color = typeof resolved === "object" ? resolved : parseColor(typeof props.color === "string" ? props.color : "#000000");
|
|
657
|
+
const node = services.createArtwork(graph, artwork, {
|
|
658
|
+
parentId,
|
|
659
|
+
size,
|
|
660
|
+
color,
|
|
661
|
+
overrides
|
|
662
|
+
});
|
|
663
|
+
if (variableId) bindSolidPaints(graph, node, variableId);
|
|
664
|
+
return node;
|
|
665
|
+
}
|
|
666
|
+
/** Bind every solid paint of an artwork's paths, so it follows the variable it was drawn in. */
|
|
667
|
+
function bindSolidPaints(graph, node, variableId) {
|
|
668
|
+
for (const field of ["fills", "strokes"]) node[field].forEach((paint, index) => {
|
|
669
|
+
if (paint.type === "SOLID") graph.bindVariable(node.id, `${field}/${index}/color`, variableId);
|
|
670
|
+
});
|
|
671
|
+
for (const child of graph.getChildren(node.id)) bindSolidPaints(graph, child, variableId);
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Render an inline <svg> element into vector nodes through the same artwork path as
|
|
675
|
+
* icons. The body may be string children or a `body` prop; parsed shape children work too.
|
|
676
|
+
*/
|
|
677
|
+
function renderSVGNode(services, graph, tree, parentId) {
|
|
678
|
+
const props = tree.props;
|
|
679
|
+
const explicitW = typeof props.w === "number" ? props.w : 0;
|
|
680
|
+
const explicitH = typeof props.h === "number" ? props.h : 0;
|
|
681
|
+
const size = explicitW > 0 || explicitH > 0 ? Math.max(explicitW, explicitH) : props.size ?? 24;
|
|
682
|
+
const body = typeof props.body === "string" && props.body || tree.children.filter((c) => typeof c === "string").join("");
|
|
683
|
+
const artwork = services.svg({
|
|
684
|
+
body,
|
|
685
|
+
elements: tree.children.filter(isTreeNode),
|
|
686
|
+
props
|
|
687
|
+
}, size);
|
|
688
|
+
if (!artwork) throw new Error("<svg> requires SVG markup, a body prop, or supported SVG shape children");
|
|
689
|
+
return placeArtwork(services, graph, artwork, props, size, parentId);
|
|
690
|
+
}
|
|
691
|
+
function findComponentByName(graph, name) {
|
|
692
|
+
for (const node of graph.getAllNodes()) if (node.type === "COMPONENT" && node.name === name) return node;
|
|
693
|
+
}
|
|
694
|
+
function findVariantInSet(graph, componentSet, props) {
|
|
695
|
+
const requested = Object.fromEntries(Object.entries(props).filter(([key]) => componentSet.componentPropertyDefinitions.some((definition) => definition.type === "VARIANT" && definition.name === key)).map(([key, value]) => [key, String(value)]));
|
|
696
|
+
const variants = graph.getChildren(componentSet.id).filter((node) => node.type === "COMPONENT");
|
|
697
|
+
return variants.find((variant) => Object.entries(requested).every(([key, value]) => variant.componentPropertyValues[key] === value)) ?? variants[0];
|
|
698
|
+
}
|
|
699
|
+
/** The component or component set an element names by id or name. */
|
|
700
|
+
function namedComponent(graph, props) {
|
|
701
|
+
const ref = props.component ?? props.componentId ?? props.of;
|
|
702
|
+
if (typeof ref !== "string") return void 0;
|
|
703
|
+
const byId = graph.getNode(ref);
|
|
704
|
+
if (byId?.type === "COMPONENT" || byId?.type === "COMPONENT_SET") return byId;
|
|
705
|
+
const byName = findComponentByName(graph, ref);
|
|
706
|
+
if (byName) return byName;
|
|
707
|
+
for (const node of graph.getAllNodes()) if (node.type === "COMPONENT_SET" && node.name === ref) return node;
|
|
708
|
+
}
|
|
709
|
+
function resolveComponent(graph, props) {
|
|
710
|
+
const named = namedComponent(graph, props);
|
|
711
|
+
return named?.type === "COMPONENT_SET" ? findVariantInSet(graph, named, props) : named;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* The names an element sets on its component by prop: properties such as `State` or `Title`, and
|
|
715
|
+
* exposed instances.
|
|
716
|
+
*/
|
|
717
|
+
function componentPropNames(graph, props) {
|
|
718
|
+
const component = resolveComponent(graph, props);
|
|
719
|
+
return component ? componentPropNames$1(graph, component) : [];
|
|
720
|
+
}
|
|
721
|
+
async function renderInstanceNode(graph, tree, parentId) {
|
|
722
|
+
const parentLayout = graph.getNode(parentId)?.layoutMode ?? "NONE";
|
|
723
|
+
const modes = variableModes(graph, tree.props.mode);
|
|
724
|
+
const bound = bindProperties(graph, tree.props, "INSTANCE", parentId, componentIdNamed(graph));
|
|
725
|
+
const { props, bindings } = preparePropsForRender(graph, bound.props, false, {
|
|
726
|
+
parentId,
|
|
727
|
+
modes
|
|
728
|
+
});
|
|
729
|
+
const component = resolveComponent(graph, props);
|
|
730
|
+
if (!component) {
|
|
731
|
+
const ref = props.component ?? props.componentId ?? props.of;
|
|
732
|
+
throw new Error(`<Instance> component not found: ${typeof ref === "string" || typeof ref === "number" ? String(ref) : ""}`);
|
|
733
|
+
}
|
|
734
|
+
const overrides = {
|
|
735
|
+
...propsToOverrides(props, false, parentLayout),
|
|
736
|
+
...layerMetadata(modes, bound.references)
|
|
737
|
+
};
|
|
738
|
+
const layout = overrides.layoutMode ?? component.layoutMode;
|
|
739
|
+
if (layout !== "NONE") {
|
|
740
|
+
const axes = layout === "HORIZONTAL" ? ["primaryAxisSizing", "counterAxisSizing"] : ["counterAxisSizing", "primaryAxisSizing"];
|
|
741
|
+
for (const [dimension, field] of [["w", axes[0]], ["h", axes[1]]]) {
|
|
742
|
+
const value = props[dimension];
|
|
743
|
+
if (typeof value === "number") overrides[field] = "FIXED";
|
|
744
|
+
else if (value === "hug" || value === "fill") overrides[field] = "HUG";
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
const instance = graph.createInstance(component.id, parentId, overrides) ?? graph.createNode("FRAME", parentId);
|
|
748
|
+
try {
|
|
749
|
+
for (const [field, value] of Object.entries(overrides)) setInstanceOverride(instance.instanceOverrides, instance.id, instance.id, field, value);
|
|
750
|
+
graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides });
|
|
751
|
+
applyBindings(graph, instance.id, bindings);
|
|
752
|
+
applyInstanceOverrides(graph, instance, tree.props.overrides);
|
|
753
|
+
assignNamedProperties(graph, instance, props, componentIdNamed(graph));
|
|
754
|
+
assignComponentProperties(graph, instance, props.properties, componentIdNamed(graph));
|
|
755
|
+
if (props.exposed === true) exposeInstance(graph, instance);
|
|
756
|
+
return instance;
|
|
757
|
+
} catch (error) {
|
|
758
|
+
graph.deleteNode(instance.id);
|
|
759
|
+
throw error;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Apply child overrides to a freshly created instance. Keys are
|
|
764
|
+
* `childName:prop` (e.g. 'label:text', 'icon:fills'); the child is resolved by
|
|
765
|
+
* name among the instance's descendants, and the value is applied to both the
|
|
766
|
+
* child node and the instance's overrides record so component sync keeps it.
|
|
767
|
+
*/
|
|
768
|
+
function applyInstanceOverrides(graph, instance, overridesProp) {
|
|
769
|
+
if (!overridesProp || typeof overridesProp !== "object") return;
|
|
770
|
+
if (Array.isArray(overridesProp)) return;
|
|
771
|
+
const entries = Object.entries(overridesProp);
|
|
772
|
+
if (entries.length === 0) return;
|
|
773
|
+
const descendants = [];
|
|
774
|
+
const walk = (id) => {
|
|
775
|
+
const node = graph.getNode(id);
|
|
776
|
+
if (!node) return;
|
|
777
|
+
descendants.push(node);
|
|
778
|
+
for (const cid of node.childIds) walk(cid);
|
|
779
|
+
};
|
|
780
|
+
walk(instance.id);
|
|
781
|
+
let mutated = false;
|
|
782
|
+
for (const [key, value] of entries) {
|
|
783
|
+
const sep = key.indexOf(":");
|
|
784
|
+
if (sep === -1) continue;
|
|
785
|
+
const childName = key.slice(0, sep);
|
|
786
|
+
const prop = key.slice(sep + 1);
|
|
787
|
+
const child = descendants.find((n) => n.name === childName);
|
|
788
|
+
if (!child || !(prop in child)) continue;
|
|
789
|
+
graph.updateNode(child.id, { [prop]: value });
|
|
790
|
+
setInstanceOverride(instance.instanceOverrides, instance.id, child.id, prop, value);
|
|
791
|
+
mutated = true;
|
|
792
|
+
}
|
|
793
|
+
if (mutated) graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides });
|
|
794
|
+
}
|
|
795
|
+
async function renderArtworkNode(services, graph, tree, parentId) {
|
|
796
|
+
const bound = bindProperties(graph, tree.props, "VECTOR", parentId, componentIdNamed(graph));
|
|
797
|
+
const artwork = {
|
|
798
|
+
...tree,
|
|
799
|
+
props: bound.props
|
|
800
|
+
};
|
|
801
|
+
const node = tree.type === "icon" ? await renderIconNode(services, graph, artwork, parentId) : renderSVGNode(services, graph, artwork, parentId);
|
|
802
|
+
const metadata = layerMetadata(void 0, bound.references);
|
|
803
|
+
if (typeof bound.props.visible === "boolean") metadata.visible = bound.props.visible;
|
|
804
|
+
if (Object.keys(metadata).length > 0) graph.updateNode(node.id, metadata);
|
|
805
|
+
return node;
|
|
806
|
+
}
|
|
807
|
+
/** The modes a layer pins and the component properties its fields are bound to. */
|
|
808
|
+
function layerMetadata(modes, references) {
|
|
809
|
+
return {
|
|
810
|
+
...modes && { variableModes: modes },
|
|
811
|
+
...references.length > 0 && { componentPropertyReferences: references }
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
/** The component a swap property's default names, by ID or name, as `<Instance of>` finds it. */
|
|
815
|
+
function componentIdNamed(graph) {
|
|
816
|
+
return (name) => resolveComponent(graph, { of: name })?.id;
|
|
817
|
+
}
|
|
818
|
+
/** The fields and variable bindings an element's props set on a `nodeType` node under `parentId`. */
|
|
819
|
+
function elementOverrides(graph, nodeType, tree, parentId) {
|
|
820
|
+
const parentLayout = graph.getNode(parentId)?.layoutMode ?? "NONE";
|
|
821
|
+
const isText = nodeType === "TEXT";
|
|
822
|
+
const modes = variableModes(graph, tree.props.mode);
|
|
823
|
+
const bound = bindProperties(graph, tree.props, nodeType, parentId, componentIdNamed(graph));
|
|
824
|
+
const layer = {
|
|
825
|
+
parentId,
|
|
826
|
+
modes
|
|
827
|
+
};
|
|
828
|
+
const { props, bindings } = preparePropsForRender(graph, bound.props, isText, layer);
|
|
829
|
+
const overrides = {
|
|
830
|
+
...propsToOverrides(props, isText, parentLayout),
|
|
831
|
+
...shapeOverrides(nodeType, props),
|
|
832
|
+
...layerMetadata(modes, bound.references)
|
|
833
|
+
};
|
|
834
|
+
if (isText) {
|
|
835
|
+
const spanOverrides = (spanProps) => propsToOverrides(preparePropsForRender(graph, spanProps, true, layer).props, true, "NONE");
|
|
836
|
+
Object.assign(overrides, textContentFields(tree, props, overrides, spanOverrides));
|
|
837
|
+
}
|
|
838
|
+
return {
|
|
839
|
+
overrides,
|
|
840
|
+
bindings
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
async function renderNode(services, graph, tree, parentId, onNode, scope) {
|
|
844
|
+
const node = await renderNodeContent(services, graph, tree, parentId, onNode, scope);
|
|
845
|
+
onNode?.(tree, node);
|
|
846
|
+
return node;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* The elements rendered their own way rather than as the layer their definition names, or null
|
|
850
|
+
* for any other element.
|
|
851
|
+
*/
|
|
852
|
+
function renderOwnElement(services, graph, tree, parentId) {
|
|
853
|
+
switch (tree.type) {
|
|
854
|
+
case "icon":
|
|
855
|
+
case "svg": return renderArtworkNode(services, graph, tree, parentId);
|
|
856
|
+
case "instance": return renderInstanceNode(graph, tree, parentId);
|
|
857
|
+
default: {
|
|
858
|
+
const element = elementOwnLayer(tree.type);
|
|
859
|
+
return element ? Promise.resolve(renderOwnLayerElement(graph, tree, parentId, element)) : null;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
async function renderNodeContent(services, graph, tree, parentId, onNode, scope) {
|
|
864
|
+
const own = renderOwnElement(services, graph, tree, parentId);
|
|
865
|
+
if (own) return own;
|
|
866
|
+
const reka = await renderRekaNode(graph, tree, parentId, scope, {
|
|
867
|
+
render: (child, childParentId, childScope) => renderNode(services, graph, child, childParentId, onNode, childScope),
|
|
868
|
+
create: (nodeType, element, elementParentId) => {
|
|
869
|
+
const { overrides, bindings } = elementOverrides(graph, nodeType, element, elementParentId);
|
|
870
|
+
const created = graph.createNode(nodeType, elementParentId, overrides);
|
|
871
|
+
applyBindings(graph, created.id, bindings);
|
|
872
|
+
placeVectorPaths(graph, created, element.props, (network) => services.vectorBounds(network));
|
|
873
|
+
return created;
|
|
874
|
+
},
|
|
875
|
+
instance: (element, elementParentId) => renderInstanceNode(graph, element, elementParentId),
|
|
876
|
+
finishSet: (setId) => inferComponentSetProperties(graph, setId)
|
|
877
|
+
});
|
|
878
|
+
if (reka) return reka;
|
|
879
|
+
if (isTextContent(tree)) throw new Error(`<${TEXT_CONTENT_NAMES[tree.type]}> must be inside <Text>`);
|
|
880
|
+
const nodeType = elementNodeType(tree.type);
|
|
881
|
+
if (!nodeType) throw new Error(`Unknown element: <${tree.type}>`);
|
|
882
|
+
const { overrides, bindings } = elementOverrides(graph, nodeType, tree, parentId);
|
|
883
|
+
const node = graph.createNode(nodeType, parentId, overrides);
|
|
884
|
+
applyBindings(graph, node.id, bindings);
|
|
885
|
+
placeVectorPaths(graph, node, tree.props, (network) => services.vectorBounds(network));
|
|
886
|
+
for (const child of tree.children) {
|
|
887
|
+
if (typeof child === "string" || node.type === "TEXT" && isTextContent(child)) continue;
|
|
888
|
+
if (isTreeNode(child)) await renderNode(services, graph, child, node.id, onNode, scope);
|
|
889
|
+
}
|
|
890
|
+
if (node.type === "COMPONENT_SET") inferComponentSetProperties(graph, node.id);
|
|
891
|
+
return node;
|
|
892
|
+
}
|
|
893
|
+
//#endregion
|
|
894
|
+
//#region src/transform.ts
|
|
895
|
+
const options = {
|
|
896
|
+
transforms: ["typescript", "jsx"],
|
|
897
|
+
jsxPragma: "__h",
|
|
898
|
+
jsxFragmentPragma: "__fragment",
|
|
899
|
+
production: true
|
|
900
|
+
};
|
|
901
|
+
const RULE = "Design JSX is one JSX expression, or statements that end with `return` and the JSX";
|
|
902
|
+
function lineCount(text) {
|
|
903
|
+
let lines = 0;
|
|
904
|
+
for (const char of text) if (char === "\n") lines++;
|
|
905
|
+
return lines;
|
|
906
|
+
}
|
|
907
|
+
function transformBody(body, withLines) {
|
|
908
|
+
return withLines ? transform(body, {
|
|
909
|
+
...options,
|
|
910
|
+
production: false,
|
|
911
|
+
filePath: "design.jsx"
|
|
912
|
+
}).code : transform(body, options).code;
|
|
913
|
+
}
|
|
914
|
+
/** Whether a function body returns from its top level, as statements must end. */
|
|
915
|
+
function returns(code) {
|
|
916
|
+
return parse(code, {
|
|
917
|
+
ecmaVersion: "latest",
|
|
918
|
+
allowReturnOutsideFunction: true
|
|
919
|
+
}).body.some((statement) => statement.type === "ReturnStatement");
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* The function body a Design JSX string stands for, tried in order: one JSX expression, sibling
|
|
923
|
+
* elements, or statements that end with `return` and the JSX, as a component's body does. Each
|
|
924
|
+
* keeps the source's lines where they are.
|
|
925
|
+
*/
|
|
926
|
+
function transformProgram(source, withLines) {
|
|
927
|
+
const trimmed = source.trim();
|
|
928
|
+
const lineOffset = lineCount(source.slice(0, source.length - source.trimStart().length));
|
|
929
|
+
const expressions = [`return (${trimmed})`];
|
|
930
|
+
if (trimmed.startsWith("<")) expressions.push(`return (<>${trimmed}</>)`);
|
|
931
|
+
for (const body of expressions) try {
|
|
932
|
+
return {
|
|
933
|
+
code: transformBody(body, withLines),
|
|
934
|
+
lineOffset
|
|
935
|
+
};
|
|
936
|
+
} catch {
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
let code;
|
|
940
|
+
try {
|
|
941
|
+
code = transformBody(trimmed, withLines);
|
|
942
|
+
} catch (error) {
|
|
943
|
+
throw new Error(`${RULE}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
944
|
+
}
|
|
945
|
+
if (!returns(code)) throw new Error(`${RULE}; these statements return nothing.`);
|
|
946
|
+
return {
|
|
947
|
+
code,
|
|
948
|
+
lineOffset
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
/** Transform Design JSX into the body of a function that returns what it renders. */
|
|
952
|
+
function transformDesignJSXExpression(source) {
|
|
953
|
+
return transformProgram(source, false).code;
|
|
954
|
+
}
|
|
955
|
+
/** Like {@link transformDesignJSXExpression}, keeping the source line of every element. */
|
|
956
|
+
function transformDesignJSXProgram(source) {
|
|
957
|
+
return transformProgram(source, true);
|
|
958
|
+
}
|
|
959
|
+
//#endregion
|
|
960
|
+
//#region src/render.ts
|
|
961
|
+
/** `const Switch = { Root: 'Switch.Root', … }`: each Reka namespace as element types. */
|
|
962
|
+
const REKA_ALIASES = Object.entries(REKA_ELEMENTS).map(([namespace, parts]) => `const ${namespace} = ${JSON.stringify(Object.fromEntries(Object.keys(parts).map((part) => [part, `${namespace}.${part}`])))}`).join("\n");
|
|
963
|
+
/** `const solid = __helpers.solid`, …: every helper in the scope of the code. */
|
|
964
|
+
const HELPER_BINDINGS = DESIGN_JSX_HELPERS.map(({ name }) => `const ${name} = __helpers.${name}`).join("\n");
|
|
965
|
+
function stripHTMLComments(jsxString) {
|
|
966
|
+
return jsxString.replace(/<!--[\s\S]*?-->/g, "");
|
|
967
|
+
}
|
|
968
|
+
function unsupportedPropWarnings(graph, tree) {
|
|
969
|
+
const warnings = [];
|
|
970
|
+
collectUnsupportedPropWarnings(graph, tree, warnings);
|
|
971
|
+
return warnings;
|
|
972
|
+
}
|
|
973
|
+
function collectUnsupportedPropWarnings(graph, tree, warnings) {
|
|
974
|
+
const supported = elementProps(tree.type);
|
|
975
|
+
const properties = componentPropNames(graph, tree.props);
|
|
976
|
+
const element = TEXT_CONTENT_NAMES[tree.type] ?? tree.type;
|
|
977
|
+
const schema = elementSchema(tree.type);
|
|
978
|
+
if (schema) warnings.push(...elementPropWarnings(schema, tree.props, element));
|
|
979
|
+
for (const [key, value] of Object.entries(tree.props)) {
|
|
980
|
+
if (!supported.has(key) && !properties.includes(key)) {
|
|
981
|
+
warnings.push(`Unsupported prop "${key}" on <${element}> is ignored.`);
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
const values = ENUM_PROPS[key];
|
|
985
|
+
if (values && typeof value === "string" && values.parse(value) === void 0) warnings.push(`Unknown ${key} "${value}" on <${element}> is ignored; expected ${values.names.join(", ")}.`);
|
|
986
|
+
}
|
|
987
|
+
if (tree.type === "svg") return;
|
|
988
|
+
for (const child of tree.children) if (isTreeNode(child)) collectUnsupportedPropWarnings(graph, child, warnings);
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Build a component function from a JSX string: one JSX expression, sibling elements, or
|
|
992
|
+
* statements that end with `return` and the JSX. The statements run inside the component, so
|
|
993
|
+
* they may shadow an element or helper name. Works in both Node/Bun and the browser.
|
|
994
|
+
*/
|
|
995
|
+
function buildComponent(jsxString, warnings = []) {
|
|
996
|
+
const trimmed = stripHTMLComments(jsxString).trim();
|
|
997
|
+
const aliases = `
|
|
998
|
+
const __h = React.createElement
|
|
999
|
+
const __fragment = ''
|
|
1000
|
+
${elementBindings()}
|
|
1001
|
+
const svg = 'svg'
|
|
1002
|
+
${REKA_ALIASES}
|
|
1003
|
+
${HELPER_BINDINGS}
|
|
1004
|
+
`;
|
|
1005
|
+
const body = transformDesignJSXExpression(trimmed);
|
|
1006
|
+
return new Function("React", "__helpers", `${aliases}\nreturn function __render() {\n${body}\n}`)(mini_react_exports, designJSXHelpers(warnings));
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Render a JSX string into the scene graph.
|
|
1010
|
+
* Works in both Node/Bun and the browser.
|
|
1011
|
+
*/
|
|
1012
|
+
async function renderJSX(services, graph, jsxString, options) {
|
|
1013
|
+
const helperWarnings = [];
|
|
1014
|
+
const element = createElement(buildComponent(jsxString, helperWarnings), null);
|
|
1015
|
+
const tree = resolveToTree(element);
|
|
1016
|
+
if (!tree) throw new Error("JSX must return a Figma element (Frame, Text, etc)");
|
|
1017
|
+
const results = await renderRoots(services, graph, tree, options);
|
|
1018
|
+
const warnings = uniq([
|
|
1019
|
+
...unsupportedPropWarnings(graph, tree),
|
|
1020
|
+
...helperWarnings,
|
|
1021
|
+
...results[0].warnings ?? []
|
|
1022
|
+
]);
|
|
1023
|
+
if (warnings.length > 0) results[0].warnings = warnings;
|
|
1024
|
+
return results;
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* A Design JSX renderer bound to an engine. Rendering needs `services` for icons, inline
|
|
1028
|
+
* SVG, and layout; Pairdraw's engine provides them through `@pairdraw/core/design-jsx`.
|
|
1029
|
+
*/
|
|
1030
|
+
function createDesignJSXRenderer(services) {
|
|
1031
|
+
return {
|
|
1032
|
+
/** Render a Design JSX string into the graph, one result per top-level element. */
|
|
1033
|
+
renderJSX: (graph, jsxString, options) => renderJSX(services, graph, jsxString, options),
|
|
1034
|
+
/** Render a tree built with the element functions into the graph. */
|
|
1035
|
+
renderTree: (graph, tree, options) => renderTree(services, graph, tree, options)
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
//#endregion
|
|
1039
|
+
//#region src/fields.ts
|
|
1040
|
+
/**
|
|
1041
|
+
* The node state a `nodeType` node under `parentId` gets from `attributes`, as rendering
|
|
1042
|
+
* would set it, without creating a node. Diffing two results gives the fields an attribute
|
|
1043
|
+
* change moves, so an update leaves state that JSX does not describe untouched.
|
|
1044
|
+
*/
|
|
1045
|
+
function jsxNodeFields(graph, nodeType, attributes, parentId) {
|
|
1046
|
+
const source = `<Frame ${attributes.map((attribute) => attribute.source).join(" ")} />`;
|
|
1047
|
+
const tree = resolveToTree(createElement(buildComponent(source), null));
|
|
1048
|
+
if (!tree) throw new Error(`Invalid JSX attributes: ${source}`);
|
|
1049
|
+
const { overrides, bindings } = elementOverrides(graph, nodeType, tree, parentId);
|
|
1050
|
+
return {
|
|
1051
|
+
fields: {
|
|
1052
|
+
...omit(createDefaultNode(() => "", nodeType), [
|
|
1053
|
+
"id",
|
|
1054
|
+
"parentId",
|
|
1055
|
+
"childIds"
|
|
1056
|
+
]),
|
|
1057
|
+
...overrides
|
|
1058
|
+
},
|
|
1059
|
+
bindings
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
//#endregion
|
|
1063
|
+
export { buildComponent, createDesignJSXRenderer, createElement, jsxNodeFields, transformDesignJSXExpression, transformDesignJSXProgram };
|
|
1064
|
+
|
|
1065
|
+
//# sourceMappingURL=fields.js.map
|