@pairdraw/design-jsx 0.0.0-stage → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/chunks/attributes.js +32 -0
- package/dist/chunks/attributes.js.map +1 -0
- package/dist/chunks/behaviours/elements.js +321 -0
- package/dist/chunks/behaviours/elements.js.map +1 -0
- package/dist/chunks/behaviours/render.js +368 -0
- package/dist/chunks/behaviours/render.js.map +1 -0
- package/dist/chunks/component-properties.js +809 -0
- package/dist/chunks/component-properties.js.map +1 -0
- package/dist/chunks/components.js +78 -0
- package/dist/chunks/components.js.map +1 -0
- package/dist/chunks/connectors.js +290 -0
- package/dist/chunks/connectors.js.map +1 -0
- package/dist/chunks/effects.js +57 -0
- package/dist/chunks/effects.js.map +1 -0
- package/dist/chunks/elements.js +618 -0
- package/dist/chunks/elements.js.map +1 -0
- package/dist/chunks/export/behaviours.js +113 -0
- package/dist/chunks/export/behaviours.js.map +1 -0
- package/dist/chunks/export/bindings.js +273 -0
- package/dist/chunks/export/bindings.js.map +1 -0
- package/dist/chunks/export/decoration.js +96 -0
- package/dist/chunks/export/decoration.js.map +1 -0
- package/dist/chunks/export/font.js +17 -0
- package/dist/chunks/export/font.js.map +1 -0
- package/dist/chunks/export/icons.js +34 -0
- package/dist/chunks/export/icons.js.map +1 -0
- package/dist/chunks/export/index.js +1390 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/fields.js +1065 -0
- package/dist/chunks/fields.js.map +1 -0
- package/dist/chunks/index.js +438 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/chunks/jsx-dev-runtime.js +16 -0
- package/dist/chunks/jsx-dev-runtime.js.map +1 -0
- package/dist/index.d.ts +543 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-runtime.d.ts +44 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +2 -0
- package/dist/tree.d.ts +261 -0
- package/dist/tree.d.ts.map +1 -0
- package/package.json +66 -4
- package/src/attributes.ts +30 -0
- package/src/behaviours/elements.ts +47 -0
- package/src/behaviours/index.ts +253 -0
- package/src/behaviours/render.ts +144 -0
- package/src/component-properties.ts +428 -0
- package/src/components.ts +109 -0
- package/src/connectors.ts +316 -0
- package/src/definitions.ts +277 -0
- package/src/effects.ts +80 -0
- package/src/elements.ts +321 -0
- package/src/enums.ts +182 -0
- package/src/export/behaviours.ts +143 -0
- package/src/export/bindings.ts +155 -0
- package/src/export/decoration.ts +39 -0
- package/src/export/font.ts +25 -0
- package/src/export/helpers.ts +75 -0
- package/src/export/icons.ts +42 -0
- package/src/export/index.ts +297 -0
- package/src/export/paint.ts +276 -0
- package/src/export/props.ts +369 -0
- package/src/export/state.ts +52 -0
- package/src/export/text.ts +155 -0
- package/src/export/value.ts +99 -0
- package/src/export/variables.ts +393 -0
- package/src/fields.ts +39 -0
- package/src/figjam-text.ts +37 -0
- package/src/global.d.ts +4 -0
- package/src/helpers.ts +135 -0
- package/src/index.ts +155 -0
- package/src/jsx-dev-runtime.ts +1 -0
- package/src/jsx-runtime.ts +51 -0
- package/src/mini-react.ts +23 -0
- package/src/overrides/effects.ts +39 -0
- package/src/overrides/fills.ts +56 -0
- package/src/overrides/state.ts +55 -0
- package/src/overrides/strokes.ts +109 -0
- package/src/overrides/text.ts +116 -0
- package/src/overrides/vector.ts +122 -0
- package/src/paints.ts +230 -0
- package/src/props-overrides.ts +481 -0
- package/src/reconcile.ts +134 -0
- package/src/reference/authoring.md +76 -0
- package/src/reference/examples.ts +49 -0
- package/src/reference.ts +19 -0
- package/src/refs.ts +48 -0
- package/src/render.ts +147 -0
- package/src/renderer.ts +598 -0
- package/src/scalar-bindings.ts +36 -0
- package/src/schema.ts +213 -0
- package/src/services.ts +36 -0
- package/src/shapes-with-text.ts +73 -0
- package/src/stickies.ts +72 -0
- package/src/streaming/index.ts +47 -0
- package/src/streaming/literals.ts +82 -0
- package/src/streaming/projection.ts +189 -0
- package/src/text-content.ts +233 -0
- package/src/transform.ts +78 -0
- package/src/tree.ts +285 -0
- package/src/types.ts +13 -0
- package/src/variables/graph.ts +265 -0
- package/src/variables/index.ts +124 -0
- package/src/variables/props.ts +77 -0
|
@@ -0,0 +1,1390 @@
|
|
|
1
|
+
import { isTreeNode } from "../behaviours/elements.js";
|
|
2
|
+
import { backgroundBlur, dropShadow, foregroundBlur, innerShadow, layerBlur } from "../effects.js";
|
|
3
|
+
import { BLEND_MODE, CONSTRAINT, MASK_TYPE, TEXT_ALIGN, TEXT_ALIGN_VERTICAL, TEXT_CASE, TEXT_DECORATION, defineMode, defineVars, designMode, designVar, isMode, isVariable, parseScriptInput, property, ref } from "../component-properties.js";
|
|
4
|
+
import { Reference, connectorExportScope, connectorLabel, connectorProps, connectorRefDeclarations, helperCall, layersUnder, plainValue, propAttribute, valueSyntax } from "../connectors.js";
|
|
5
|
+
import { DESIGN_JSX_ELEMENTS, collectCornerRadii, collectEffectProps, collectFillProps, collectPadding, collectStrokeProps, elementTag, emitPadding, formatColor, formatTracks, getNodeContext, shapeWithTextLabelText, shapeWithTextProps, stickyBody, stickyProps } from "../elements.js";
|
|
6
|
+
import { AUTO, collectDecorationProps, textLengthJSX } from "./decoration.js";
|
|
7
|
+
import { collectBindingProps, collectModeProps } from "./bindings.js";
|
|
8
|
+
import { rekaExport } from "./behaviours.js";
|
|
9
|
+
import { iconProps } from "./icons.js";
|
|
10
|
+
import { FONT_SETTING_FIELDS, collectFontSettingProps } from "./font.js";
|
|
11
|
+
import { colorToFill, parseColor, tryParseColor } from "@pairdraw/scene-graph/color";
|
|
12
|
+
import { DEFAULT_FONT_FAMILY, TRANSPARENT } from "@pairdraw/scene-graph/constants";
|
|
13
|
+
import * as v from "valibot";
|
|
14
|
+
import { IDENTITY_GRADIENT_TRANSFORM } from "@pairdraw/scene-graph/gradient";
|
|
15
|
+
import { paintTransformSchema } from "@pairdraw/scene-graph/paints";
|
|
16
|
+
import { SHADER_SOURCE_MAX_BYTES, createShaderPaint, createShaderSourcePaint, shaderPresetSchema } from "@pairdraw/scene-graph/shaders";
|
|
17
|
+
import { isGeneratedFrame } from "@pairdraw/scene-graph/generated";
|
|
18
|
+
import { isPlainObject } from "es-toolkit/predicate";
|
|
19
|
+
import { compact, difference } from "es-toolkit/array";
|
|
20
|
+
import { camelCase } from "es-toolkit/string";
|
|
21
|
+
import { jsx } from "@pairdraw/emit";
|
|
22
|
+
import { vectorNetworkPaths } from "@pairdraw/scene-graph/vector-network";
|
|
23
|
+
import { layoutSizing } from "@pairdraw/scene-graph/layout";
|
|
24
|
+
import { isEmptyObject, isEqual, omitBy, pick } from "es-toolkit";
|
|
25
|
+
import { MAX_LIST_INDENTATION, PLAIN_PARAGRAPH, hasParagraphSpacing, paragraphCount, paragraphStyleAt, textParagraphRanges, trimParagraphStyles, withParagraphSpacing } from "@pairdraw/scene-graph/text";
|
|
26
|
+
import { getNodeVariableModeId, modesDefaultFirst, resolveVariableInModes } from "@pairdraw/scene-graph/variables";
|
|
27
|
+
import { createDefaultNode } from "@pairdraw/scene-graph/node-defaults";
|
|
28
|
+
import { resolveNodeTextDirection } from "@pairdraw/scene-graph/text/direction";
|
|
29
|
+
//#region src/paints.ts
|
|
30
|
+
const colorSchema = v.union([v.string(), v.object({
|
|
31
|
+
r: v.number(),
|
|
32
|
+
g: v.number(),
|
|
33
|
+
b: v.number(),
|
|
34
|
+
a: v.number()
|
|
35
|
+
})]);
|
|
36
|
+
const stopsSchema = v.array(v.union([v.tuple([colorSchema, v.number()]), v.object({
|
|
37
|
+
color: colorSchema,
|
|
38
|
+
position: v.number()
|
|
39
|
+
})], (issue) => `Expected [color, position] or { color, position } but received ${issue.received}`));
|
|
40
|
+
const GRADIENT_HELPERS = {
|
|
41
|
+
GRADIENT_LINEAR: "linearGradient",
|
|
42
|
+
GRADIENT_RADIAL: "radialGradient",
|
|
43
|
+
GRADIENT_ANGULAR: "angularGradient",
|
|
44
|
+
GRADIENT_DIAMOND: "diamondGradient"
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Scripts call the helpers with whatever they guess, so a wrong call says what it expects
|
|
48
|
+
* and what was wrong, as Valibot describes it.
|
|
49
|
+
*/
|
|
50
|
+
function parseStops(type, stops) {
|
|
51
|
+
return parseScriptInput(`${GRADIENT_HELPERS[type]}() expects an array of stops, such as [['#3b82f6', 0], ['#8b5cf6', 1]]`, stopsSchema, stops);
|
|
52
|
+
}
|
|
53
|
+
function parseTransform(type, transform) {
|
|
54
|
+
return parseScriptInput(`${GRADIENT_HELPERS[type]}() expects a transform as Figma's [[a, b, c], [d, e, f]], such as [[0, 1, 0], [-1, 0, 1]], or { m00, m01, m02, m10, m11, m12 }`, paintTransformSchema, transform);
|
|
55
|
+
}
|
|
56
|
+
function toColor(color) {
|
|
57
|
+
return typeof color === "string" ? parseColor(color) : color;
|
|
58
|
+
}
|
|
59
|
+
function toStop(stop) {
|
|
60
|
+
if ("color" in stop) return {
|
|
61
|
+
color: toColor(stop.color),
|
|
62
|
+
position: stop.position
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
color: toColor(stop[0]),
|
|
66
|
+
position: stop[1]
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function solid(color, options = {}) {
|
|
70
|
+
const fill = colorToFill(color);
|
|
71
|
+
return {
|
|
72
|
+
...fill,
|
|
73
|
+
opacity: options.opacity ?? fill.opacity,
|
|
74
|
+
visible: options.visible ?? true,
|
|
75
|
+
blendMode: options.blendMode
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function gradient(type, stops, options = {}) {
|
|
79
|
+
return {
|
|
80
|
+
type,
|
|
81
|
+
color: { ...TRANSPARENT },
|
|
82
|
+
opacity: options.opacity ?? 1,
|
|
83
|
+
visible: options.visible ?? true,
|
|
84
|
+
blendMode: options.blendMode,
|
|
85
|
+
gradientStops: parseStops(type, stops).map(toStop),
|
|
86
|
+
gradientTransform: options.transform === void 0 ? { ...IDENTITY_GRADIENT_TRANSFORM } : parseTransform(type, options.transform)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Whether a paint value came from {@link shader} or {@link shaderSource}. */
|
|
90
|
+
function isShaderFill(value) {
|
|
91
|
+
return "shader" in value;
|
|
92
|
+
}
|
|
93
|
+
const shaderSourceSchema = v.pipe(v.string(), v.trim(), v.nonEmpty("Expected the shader code to write"), v.check((source) => new TextEncoder().encode(source).length <= SHADER_SOURCE_MAX_BYTES, `Expected at most ${SHADER_SOURCE_MAX_BYTES} bytes of shader code`));
|
|
94
|
+
/** The inputs a user-written shader was given values for. */
|
|
95
|
+
const shaderUniformsSchema = v.record(v.string(), v.union([
|
|
96
|
+
v.number(),
|
|
97
|
+
v.boolean(),
|
|
98
|
+
v.string()
|
|
99
|
+
]));
|
|
100
|
+
/**
|
|
101
|
+
* A paint that draws shader code written here rather than a preset from the `shaders` library.
|
|
102
|
+
* Its inputs are the `@directive` ones the code declares.
|
|
103
|
+
*/
|
|
104
|
+
function shaderSource(source, options = {}) {
|
|
105
|
+
const code = parseScriptInput("shaderSource() expects the shader code as a string", shaderSourceSchema, source);
|
|
106
|
+
const uniforms = options.uniforms === void 0 ? {} : parseScriptInput("shaderSource() expects its uniforms as { name: value }", shaderUniformsSchema, options.uniforms);
|
|
107
|
+
const { paint, shader } = createShaderSourcePaint(code);
|
|
108
|
+
return {
|
|
109
|
+
...paint,
|
|
110
|
+
opacity: options.opacity ?? 1,
|
|
111
|
+
visible: options.visible ?? true,
|
|
112
|
+
blendMode: options.blendMode,
|
|
113
|
+
shader: {
|
|
114
|
+
...shader,
|
|
115
|
+
uniforms
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const shaderInputSchema = v.union([shaderPresetSchema, v.pipe(v.array(v.unknown()), v.transform((components) => ({ components })), shaderPresetSchema)], (issue) => `Expected a preset { components: [...] } or an array of effects but received ${issue.received}`);
|
|
120
|
+
/**
|
|
121
|
+
* A paint that draws a shader from the `shaders` library: a preset as shaders.com exports it, or
|
|
122
|
+
* just its effects, such as `shader([{ type: 'Aurora' }])`.
|
|
123
|
+
*/
|
|
124
|
+
function shader(preset, options = {}) {
|
|
125
|
+
const parsed = parseScriptInput("shader() expects a preset such as { components: [{ type: 'Aurora' }] } or an array of effects", shaderInputSchema, preset);
|
|
126
|
+
const { paint, shader } = createShaderPaint(parsed);
|
|
127
|
+
return {
|
|
128
|
+
...paint,
|
|
129
|
+
opacity: options.opacity ?? 1,
|
|
130
|
+
visible: options.visible ?? true,
|
|
131
|
+
blendMode: options.blendMode,
|
|
132
|
+
shader
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function linearGradient(stops, options) {
|
|
136
|
+
return gradient("GRADIENT_LINEAR", stops, options);
|
|
137
|
+
}
|
|
138
|
+
function radialGradient(stops, options) {
|
|
139
|
+
return gradient("GRADIENT_RADIAL", stops, options);
|
|
140
|
+
}
|
|
141
|
+
function angularGradient(stops, options) {
|
|
142
|
+
return gradient("GRADIENT_ANGULAR", stops, options);
|
|
143
|
+
}
|
|
144
|
+
function diamondGradient(stops, options) {
|
|
145
|
+
return gradient("GRADIENT_DIAMOND", stops, options);
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/helpers.ts
|
|
149
|
+
const SHADOW_OPTIONS = {
|
|
150
|
+
color: true,
|
|
151
|
+
x: true,
|
|
152
|
+
y: true,
|
|
153
|
+
offset: true,
|
|
154
|
+
radius: true,
|
|
155
|
+
spread: true,
|
|
156
|
+
visible: true,
|
|
157
|
+
blendMode: true,
|
|
158
|
+
showShadowBehindNode: true
|
|
159
|
+
};
|
|
160
|
+
const BLUR_OPTIONS = {
|
|
161
|
+
radius: true,
|
|
162
|
+
visible: true
|
|
163
|
+
};
|
|
164
|
+
const SOLID_OPTIONS = {
|
|
165
|
+
opacity: true,
|
|
166
|
+
visible: true,
|
|
167
|
+
blendMode: true
|
|
168
|
+
};
|
|
169
|
+
const SHADER_SOURCE_OPTIONS = {
|
|
170
|
+
...SOLID_OPTIONS,
|
|
171
|
+
uniforms: true
|
|
172
|
+
};
|
|
173
|
+
const GRADIENT_OPTIONS = {
|
|
174
|
+
...SOLID_OPTIONS,
|
|
175
|
+
transform: true
|
|
176
|
+
};
|
|
177
|
+
/** Option names people reach for that the helpers spell as Figma's effects do. */
|
|
178
|
+
const FIGMA_OPTION_NAMES = { blur: "radius" };
|
|
179
|
+
function unsupportedOptionWarning(name, key, known) {
|
|
180
|
+
const figmaName = FIGMA_OPTION_NAMES[key];
|
|
181
|
+
return `Unsupported option "${key}" in ${name}() is ignored. ${figmaName && known.includes(figmaName) ? `Use "${figmaName}", the name Figma uses.` : `Supported options: ${known.join(", ")}.`}`;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Wrap a helper so each option it ignores adds a warning instead of vanishing, which is
|
|
185
|
+
* what happens to a misspelled option. `optionsAt` is the argument holding the options.
|
|
186
|
+
*/
|
|
187
|
+
function checked(warnings, name, helper, options, optionsAt) {
|
|
188
|
+
const known = Object.keys(options);
|
|
189
|
+
return (...args) => {
|
|
190
|
+
const passed = args[optionsAt];
|
|
191
|
+
if (isPlainObject(passed)) for (const key of difference(Object.keys(passed), known)) warnings.push(unsupportedOptionWarning(name, key, known));
|
|
192
|
+
return helper(...args);
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Every helper Design JSX code can call, reporting ignored options to `warnings`. This is the
|
|
197
|
+
* one list of them: the string renderer's scope, the code panel's sandbox, the editor's
|
|
198
|
+
* completions, and the authoring reference all read it.
|
|
199
|
+
*/
|
|
200
|
+
function designJSXHelpers(warnings) {
|
|
201
|
+
return {
|
|
202
|
+
solid: checked(warnings, "solid", solid, SOLID_OPTIONS, 1),
|
|
203
|
+
gradient: checked(warnings, "gradient", gradient, GRADIENT_OPTIONS, 2),
|
|
204
|
+
linearGradient: checked(warnings, "linearGradient", linearGradient, GRADIENT_OPTIONS, 1),
|
|
205
|
+
radialGradient: checked(warnings, "radialGradient", radialGradient, GRADIENT_OPTIONS, 1),
|
|
206
|
+
angularGradient: checked(warnings, "angularGradient", angularGradient, GRADIENT_OPTIONS, 1),
|
|
207
|
+
diamondGradient: checked(warnings, "diamondGradient", diamondGradient, GRADIENT_OPTIONS, 1),
|
|
208
|
+
shader: checked(warnings, "shader", shader, SOLID_OPTIONS, 1),
|
|
209
|
+
shaderSource: checked(warnings, "shaderSource", shaderSource, SHADER_SOURCE_OPTIONS, 1),
|
|
210
|
+
dropShadow: checked(warnings, "dropShadow", dropShadow, SHADOW_OPTIONS, 0),
|
|
211
|
+
innerShadow: checked(warnings, "innerShadow", innerShadow, SHADOW_OPTIONS, 0),
|
|
212
|
+
layerBlur: checked(warnings, "layerBlur", layerBlur, BLUR_OPTIONS, 0),
|
|
213
|
+
backgroundBlur: checked(warnings, "backgroundBlur", backgroundBlur, BLUR_OPTIONS, 0),
|
|
214
|
+
foregroundBlur: checked(warnings, "foregroundBlur", foregroundBlur, BLUR_OPTIONS, 0),
|
|
215
|
+
defineVars,
|
|
216
|
+
defineMode,
|
|
217
|
+
designVar,
|
|
218
|
+
designMode,
|
|
219
|
+
property,
|
|
220
|
+
ref
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const DESIGN_JSX_HELPERS = Object.keys(designJSXHelpers([])).map((name) => ({
|
|
224
|
+
name,
|
|
225
|
+
description: `Pairdraw ${name} helper.`
|
|
226
|
+
}));
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/variables/graph.ts
|
|
229
|
+
/**
|
|
230
|
+
* Every collection a value declares, through the variables and modes in it and their aliases;
|
|
231
|
+
* a collection comes after the ones it aliases, as it would be written.
|
|
232
|
+
*/
|
|
233
|
+
function collectDefinitions(value, found, visiting = /* @__PURE__ */ new Set()) {
|
|
234
|
+
const definition = isVariable(value) || isMode(value) ? value.collection : void 0;
|
|
235
|
+
if (definition && typeof definition !== "string") {
|
|
236
|
+
const known = found.get(definition.name);
|
|
237
|
+
if (known === definition || visiting.has(definition)) return;
|
|
238
|
+
if (known && !isEqual(known, definition)) throw new Error(`Collection ${definition.name} is defined twice with different variables`);
|
|
239
|
+
visiting.add(definition);
|
|
240
|
+
for (const mode of definition.modes) collectDefinitions(mode.values, found, visiting);
|
|
241
|
+
found.set(definition.name, definition);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (isTreeNode(value)) {
|
|
245
|
+
collectDefinitions(value.props, found, visiting);
|
|
246
|
+
for (const child of value.children) collectDefinitions(child, found, visiting);
|
|
247
|
+
} else if (Array.isArray(value)) for (const item of value) collectDefinitions(item, found, visiting);
|
|
248
|
+
else if (isPlainObject(value)) for (const item of Object.values(value)) collectDefinitions(item, found, visiting);
|
|
249
|
+
}
|
|
250
|
+
/** The one collection with this name; a name several collections share is an error. */
|
|
251
|
+
function collectionNamed(graph, name) {
|
|
252
|
+
const matches = [...graph.variableCollections.values()].filter((item) => item.name === name);
|
|
253
|
+
if (matches.length > 1) throw new Error(`More than one variable collection is named ${name}; rename one to use it`);
|
|
254
|
+
return matches[0];
|
|
255
|
+
}
|
|
256
|
+
function variableNamed(graph, collection, name) {
|
|
257
|
+
for (const id of collection.variableIds) {
|
|
258
|
+
const variable = graph.variables.get(id);
|
|
259
|
+
if (variable?.name === name) return variable;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/** The collection, with its modes in place, by the definition's names. */
|
|
263
|
+
function ensureCollection(graph, definition) {
|
|
264
|
+
let collection = collectionNamed(graph, definition.name);
|
|
265
|
+
if (!collection) {
|
|
266
|
+
collection = graph.createCollection(definition.name);
|
|
267
|
+
graph.renameMode(collection.id, collection.defaultModeId, definition.modes[0].name);
|
|
268
|
+
}
|
|
269
|
+
for (const mode of definition.modes) if (!collection.modes.some((existing) => existing.name === mode.name)) graph.createMode(collection.id, mode.name);
|
|
270
|
+
const first = collection.modes.find((mode) => mode.name === definition.modes[0].name);
|
|
271
|
+
if (first && collection.defaultModeId !== first.modeId) graph.setDefaultMode(collection.id, first.modeId);
|
|
272
|
+
return collection;
|
|
273
|
+
}
|
|
274
|
+
function literalType(value) {
|
|
275
|
+
if (typeof value === "number") return "FLOAT";
|
|
276
|
+
if (typeof value === "boolean") return "BOOLEAN";
|
|
277
|
+
return "COLOR";
|
|
278
|
+
}
|
|
279
|
+
/** The type a defined variable holds: its first value's, or for an alias, its target's. */
|
|
280
|
+
function definedType(graph, definition, name, visiting = /* @__PURE__ */ new Set()) {
|
|
281
|
+
const key = `${definition.name}\u0000${name}`;
|
|
282
|
+
if (visiting.has(key)) throw new Error(`Variable ${name} aliases itself`);
|
|
283
|
+
visiting.add(key);
|
|
284
|
+
const value = definition.modes[0].values[name];
|
|
285
|
+
if (!isVariable(value)) return literalType(value);
|
|
286
|
+
if (typeof value.collection === "object") return definedType(graph, value.collection, value.name, visiting);
|
|
287
|
+
const target = graph.variables.get(requireVariableId(graph, value));
|
|
288
|
+
if (!target) throw new Error(`Unknown variable: ${value.name}`);
|
|
289
|
+
return target.type;
|
|
290
|
+
}
|
|
291
|
+
function storedValue(graph, type, name, value) {
|
|
292
|
+
if (isVariable(value)) return { aliasId: requireVariableId(graph, value) };
|
|
293
|
+
if (literalType(value) !== type) throw new Error(`Variable ${name} mixes ${type} with ${literalType(value)} values`);
|
|
294
|
+
if (typeof value !== "string") return value;
|
|
295
|
+
const color = tryParseColor(value);
|
|
296
|
+
if (!color) throw new Error(`Variable ${name} has "${value}", which is not a color`);
|
|
297
|
+
return color;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Create or update the collections a tree defines, by name: their modes, their variables, and
|
|
301
|
+
* each variable's value in each mode, a mode's missing values taken from the first mode.
|
|
302
|
+
*/
|
|
303
|
+
function applyVariableDefinitions(graph, tree) {
|
|
304
|
+
const found = /* @__PURE__ */ new Map();
|
|
305
|
+
collectDefinitions(tree, found);
|
|
306
|
+
const definitions = [...found.values()];
|
|
307
|
+
const collections = definitions.map((definition) => ensureCollection(graph, definition));
|
|
308
|
+
const variables = definitions.map((definition, index) => {
|
|
309
|
+
const collection = collections[index];
|
|
310
|
+
return Object.keys(definition.modes[0].values).map((name) => {
|
|
311
|
+
const type = definedType(graph, definition, name);
|
|
312
|
+
const existing = variableNamed(graph, collection, name);
|
|
313
|
+
if (existing && existing.type !== type) throw new Error(`Variable ${name} in ${definition.name} is ${existing.type}, not ${type}`);
|
|
314
|
+
return existing ?? graph.createVariable(name, type, collection.id);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
definitions.forEach((definition, index) => {
|
|
318
|
+
const collection = collections[index];
|
|
319
|
+
const [first] = definition.modes;
|
|
320
|
+
for (const variable of variables[index]) for (const mode of definition.modes) {
|
|
321
|
+
const modeId = collection.modes.find((existing) => existing.name === mode.name)?.modeId;
|
|
322
|
+
if (!modeId) continue;
|
|
323
|
+
const value = mode.values[variable.name] ?? first.values[variable.name];
|
|
324
|
+
variable.valuesByMode[modeId] = storedValue(graph, variable.type, variable.name, value);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/** The ID of the variable a handle refers to: by collection and name, or by ID or name. */
|
|
329
|
+
function resolveVariableId(graph, variable) {
|
|
330
|
+
if (variable.collection) {
|
|
331
|
+
const collection = collectionNamed(graph, typeof variable.collection === "string" ? variable.collection : variable.collection.name);
|
|
332
|
+
return collection && variableNamed(graph, collection, variable.name)?.id;
|
|
333
|
+
}
|
|
334
|
+
if (graph.variables.has(variable.name)) return variable.name;
|
|
335
|
+
const matches = [...graph.variables.values()].filter((item) => item.name === variable.name);
|
|
336
|
+
if (matches.length > 1) {
|
|
337
|
+
const [first] = matches;
|
|
338
|
+
const collection = graph.variableCollections.get(first.collectionId)?.name ?? "";
|
|
339
|
+
throw new Error(`Variable name ${variable.name} is in more than one collection; name one, as in designVar('${collection}', '${variable.name}')`);
|
|
340
|
+
}
|
|
341
|
+
return matches[0]?.id;
|
|
342
|
+
}
|
|
343
|
+
function requireVariableId(graph, variable) {
|
|
344
|
+
const id = resolveVariableId(graph, variable);
|
|
345
|
+
if (id) return id;
|
|
346
|
+
const collection = typeof variable.collection === "object" ? variable.collection.name : variable.collection;
|
|
347
|
+
throw new Error(collection ? `Unknown variable ${variable.name} in ${collection}` : `Unknown variable: ${variable.name}`);
|
|
348
|
+
}
|
|
349
|
+
/** The collection and mode IDs a layer's `mode` prop pins, by their names. */
|
|
350
|
+
function variableModes(graph, value) {
|
|
351
|
+
if (value === void 0) return void 0;
|
|
352
|
+
const modes = Array.isArray(value) ? value : [value];
|
|
353
|
+
const result = {};
|
|
354
|
+
for (const mode of modes) {
|
|
355
|
+
if (!isMode(mode)) throw new Error("mode takes defineMode() or designMode() modes");
|
|
356
|
+
const name = typeof mode.collection === "string" ? mode.collection : mode.collection.name;
|
|
357
|
+
const collection = typeof mode.collection === "string" && graph.variableCollections.get(name) || collectionNamed(graph, name);
|
|
358
|
+
if (!collection) throw new Error(`Unknown variable collection: ${name}. A defineVars() collection is added once a layer binds one of its variables`);
|
|
359
|
+
const modeId = collection.modes.find((item) => item.name === mode.name)?.modeId;
|
|
360
|
+
if (!modeId) throw new Error(`Collection ${name} has no mode named ${mode.name}`);
|
|
361
|
+
result[collection.id] = modeId;
|
|
362
|
+
}
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* A variable's value for a layer created under `parentId`, in the modes the layer pins or else
|
|
367
|
+
* the ones it inherits.
|
|
368
|
+
*/
|
|
369
|
+
function resolveForLayer(graph, variableId, parentId, modes) {
|
|
370
|
+
try {
|
|
371
|
+
return resolveVariableInModes(graph, variableId, (collectionId) => modes?.[collectionId] ?? getNodeVariableModeId(graph, parentId, collectionId));
|
|
372
|
+
} catch {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/** Bind the variable in `target[key]`, if it holds one, and put its value for the layer there. */
|
|
377
|
+
function bindVariableProp(graph, target, bindings, key, field, layer) {
|
|
378
|
+
const value = target[key];
|
|
379
|
+
if (!isVariable(value)) return;
|
|
380
|
+
const variableId = requireVariableId(graph, value);
|
|
381
|
+
bindings[field] = variableId;
|
|
382
|
+
const resolved = resolveForLayer(graph, variableId, layer.parentId, layer.modes);
|
|
383
|
+
if (resolved === void 0) throw new Error(`Cannot resolve variable ${value.name} for ${key}`);
|
|
384
|
+
target[key] = resolved;
|
|
385
|
+
}
|
|
386
|
+
//#endregion
|
|
387
|
+
//#region src/text-content.ts
|
|
388
|
+
/** `<Span>`: characters of a text styled apart from the rest of it. */
|
|
389
|
+
const SPAN = "text-span";
|
|
390
|
+
/** `<Paragraph>`: one paragraph of a text, with its list and spacing. */
|
|
391
|
+
const PARAGRAPH = "text-paragraph";
|
|
392
|
+
/** The names `<Span>` and `<Paragraph>` are written under, for messages about them. */
|
|
393
|
+
const TEXT_CONTENT_NAMES = {
|
|
394
|
+
[SPAN]: "Span",
|
|
395
|
+
[PARAGRAPH]: "Paragraph"
|
|
396
|
+
};
|
|
397
|
+
/** The character styles a `<Span>` sets, as the fields `<Text>`'s props set on a text. */
|
|
398
|
+
const SPAN_STYLE_FIELDS = [
|
|
399
|
+
"fontFamily",
|
|
400
|
+
"fontSize",
|
|
401
|
+
"fontWeight",
|
|
402
|
+
"italic",
|
|
403
|
+
"fills",
|
|
404
|
+
"letterSpacing",
|
|
405
|
+
"lineHeight",
|
|
406
|
+
"textDecoration",
|
|
407
|
+
"textDecorationStyle",
|
|
408
|
+
"textDecorationFills",
|
|
409
|
+
"textDecorationThickness",
|
|
410
|
+
"textUnderlineOffset",
|
|
411
|
+
"textDecorationSkipInk",
|
|
412
|
+
"fontFeatures",
|
|
413
|
+
"fontVariations",
|
|
414
|
+
"textLanguage",
|
|
415
|
+
"textCase"
|
|
416
|
+
];
|
|
417
|
+
const lengthSchema = v.pipe(v.number(), v.finite());
|
|
418
|
+
const paragraphPropsSchema = v.pipe(v.object({
|
|
419
|
+
list: v.optional(v.picklist(["ordered", "unordered"])),
|
|
420
|
+
level: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(MAX_LIST_INDENTATION))),
|
|
421
|
+
spacing: v.optional(lengthSchema),
|
|
422
|
+
listSpacing: v.optional(lengthSchema),
|
|
423
|
+
indent: v.optional(lengthSchema)
|
|
424
|
+
}), v.check((props) => props.level === void 0 || props.list !== void 0, "`level` nests list items; set `list` too"));
|
|
425
|
+
/** Whether a child is `<Span>` or `<Paragraph>`, which are part of their text, not layers. */
|
|
426
|
+
function isTextContent(child) {
|
|
427
|
+
return isTreeNode(child) && (child.type === SPAN || child.type === PARAGRAPH);
|
|
428
|
+
}
|
|
429
|
+
const spanLinkSchema = v.object({ href: v.optional(v.pipe(v.string(), v.trim(), v.nonEmpty())) });
|
|
430
|
+
function spanStyle(props, spanOverrides) {
|
|
431
|
+
const style = omitBy(pick(spanOverrides(props), SPAN_STYLE_FIELDS), (value) => value === void 0);
|
|
432
|
+
const { href } = parseScriptInput("Invalid <Span> props", spanLinkSchema, props);
|
|
433
|
+
if (href) style.hyperlink = {
|
|
434
|
+
type: "URL",
|
|
435
|
+
value: href
|
|
436
|
+
};
|
|
437
|
+
return style;
|
|
438
|
+
}
|
|
439
|
+
function appendContent(segments, children, inherited, owner, spanOverrides) {
|
|
440
|
+
for (const child of children) if (typeof child === "string") segments.push({
|
|
441
|
+
length: child.length,
|
|
442
|
+
style: inherited
|
|
443
|
+
});
|
|
444
|
+
else if (child.type === SPAN) {
|
|
445
|
+
const style = {
|
|
446
|
+
...inherited,
|
|
447
|
+
...spanStyle(child.props, spanOverrides)
|
|
448
|
+
};
|
|
449
|
+
appendContent(segments, child.children, style, "<Span>", spanOverrides);
|
|
450
|
+
} else if (child.type === PARAGRAPH) throw new Error(`<Paragraph> must be a direct child of <Text>, not of ${owner}`);
|
|
451
|
+
else throw new Error(`${owner} can hold only text and <Span>, not <${child.type}>`);
|
|
452
|
+
}
|
|
453
|
+
function textOf(children) {
|
|
454
|
+
return children.map((child) => typeof child === "string" ? child : textOf(child.children)).join("");
|
|
455
|
+
}
|
|
456
|
+
function paragraphStyle(props) {
|
|
457
|
+
const { list, level, spacing, listSpacing, indent } = parseScriptInput("Invalid <Paragraph> props", paragraphPropsSchema, props);
|
|
458
|
+
const style = list ? {
|
|
459
|
+
listType: list === "ordered" ? "ORDERED" : "UNORDERED",
|
|
460
|
+
indentation: level ?? 1
|
|
461
|
+
} : { ...PLAIN_PARAGRAPH };
|
|
462
|
+
if (spacing !== void 0) style.paragraphSpacing = spacing;
|
|
463
|
+
if (listSpacing !== void 0) style.listSpacing = listSpacing;
|
|
464
|
+
if (indent !== void 0) style.paragraphIndent = indent;
|
|
465
|
+
return style;
|
|
466
|
+
}
|
|
467
|
+
/** The `<Paragraph>` children of a text, or null without any; other content beside them is an error. */
|
|
468
|
+
function paragraphElements(tree) {
|
|
469
|
+
const paragraphs = tree.children.filter((child) => isTreeNode(child) && child.type === PARAGRAPH);
|
|
470
|
+
if (paragraphs.length === 0) return null;
|
|
471
|
+
if (tree.children.some((child) => typeof child === "string" ? child.trim() !== "" : child.type !== PARAGRAPH)) throw new Error("<Text> mixes <Paragraph> with other content: put all of its text in <Paragraph> elements, or use none");
|
|
472
|
+
return paragraphs;
|
|
473
|
+
}
|
|
474
|
+
/** Runs for the segments, holding only what differs from the text's own style. */
|
|
475
|
+
function styleRuns(segments, textStyle) {
|
|
476
|
+
const runs = [];
|
|
477
|
+
let start = 0;
|
|
478
|
+
for (const segment of segments) {
|
|
479
|
+
const style = omitBy(segment.style, (value, key) => isEqual(value, textStyle[key]));
|
|
480
|
+
if (segment.length > 0 && !isEmptyObject(style)) {
|
|
481
|
+
const previous = runs.at(-1);
|
|
482
|
+
if (previous && previous.start + previous.length === start && isEqual(previous.style, style)) previous.length += segment.length;
|
|
483
|
+
else runs.push({
|
|
484
|
+
start,
|
|
485
|
+
length: segment.length,
|
|
486
|
+
style
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
start += segment.length;
|
|
490
|
+
}
|
|
491
|
+
return runs;
|
|
492
|
+
}
|
|
493
|
+
/** The style of the last character appended, which a newline after it takes, as in Figma. */
|
|
494
|
+
function lastStyle(segments) {
|
|
495
|
+
return segments.findLast((segment) => segment.length > 0)?.style ?? {};
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* The text a `<Text>` element's children and text props give. Strings alone are its text, as
|
|
499
|
+
* before; `<Span>` children also give style runs and `<Paragraph>` children paragraph styles.
|
|
500
|
+
* `overrides` are the text's own fields, which runs leave out.
|
|
501
|
+
*/
|
|
502
|
+
function textContentFields(tree, props, overrides, spanOverrides) {
|
|
503
|
+
if (!tree.children.some(isTextContent)) {
|
|
504
|
+
const childText = tree.children.filter((c) => typeof c === "string").join("");
|
|
505
|
+
const propText = props.text ?? props.characters ?? props.content ?? props.label ?? props.value ?? props.title;
|
|
506
|
+
if (childText) return { text: childText };
|
|
507
|
+
return typeof propText === "string" ? { text: propText } : {};
|
|
508
|
+
}
|
|
509
|
+
const textStyle = pick(createDefaultNode(() => "", "TEXT", overrides), SPAN_STYLE_FIELDS);
|
|
510
|
+
const segments = [];
|
|
511
|
+
const paragraphs = paragraphElements(tree);
|
|
512
|
+
if (!paragraphs) {
|
|
513
|
+
appendContent(segments, tree.children, {}, "<Text>", spanOverrides);
|
|
514
|
+
return {
|
|
515
|
+
text: textOf(tree.children),
|
|
516
|
+
styleRuns: styleRuns(segments, textStyle)
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
const styles = [];
|
|
520
|
+
const lines = [];
|
|
521
|
+
for (const paragraph of paragraphs) {
|
|
522
|
+
if (lines.length > 0) segments.push({
|
|
523
|
+
length: 1,
|
|
524
|
+
style: lastStyle(segments)
|
|
525
|
+
});
|
|
526
|
+
appendContent(segments, paragraph.children, {}, "<Paragraph>", spanOverrides);
|
|
527
|
+
const content = textOf(paragraph.children);
|
|
528
|
+
const style = paragraphStyle(paragraph.props);
|
|
529
|
+
for (let index = 0; index < paragraphCount(content); index++) styles.push(style);
|
|
530
|
+
lines.push(content);
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
text: lines.join("\n"),
|
|
534
|
+
styleRuns: styleRuns(segments, textStyle),
|
|
535
|
+
textParagraphs: trimParagraphStyles(styles, styles.length)
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
//#endregion
|
|
539
|
+
//#region src/export/state.ts
|
|
540
|
+
/** Constraints apply where a parent does not lay its children out. */
|
|
541
|
+
function hasConstraints(node, ctx) {
|
|
542
|
+
return !ctx.parentIsAutoLayout || node.layoutPositioning === "ABSOLUTE";
|
|
543
|
+
}
|
|
544
|
+
function collectConstraintProps(node, ctx, props) {
|
|
545
|
+
if (!hasConstraints(node, ctx)) return;
|
|
546
|
+
const horizontal = node.horizontalConstraint === "MIN" ? void 0 : node.horizontalConstraint;
|
|
547
|
+
const vertical = node.verticalConstraint === "MIN" ? void 0 : node.verticalConstraint;
|
|
548
|
+
if (!horizontal && !vertical) return;
|
|
549
|
+
props.push(["constraints", {
|
|
550
|
+
horizontal: horizontal && CONSTRAINT.print(horizontal),
|
|
551
|
+
vertical: vertical && CONSTRAINT.print(vertical)
|
|
552
|
+
}]);
|
|
553
|
+
}
|
|
554
|
+
function collectSizeLimitProps(node, props) {
|
|
555
|
+
if (node.minWidth != null) props.push(["minW", node.minWidth]);
|
|
556
|
+
if (node.maxWidth != null) props.push(["maxW", node.maxWidth]);
|
|
557
|
+
if (node.minHeight != null) props.push(["minH", node.minHeight]);
|
|
558
|
+
if (node.maxHeight != null) props.push(["maxH", node.maxHeight]);
|
|
559
|
+
}
|
|
560
|
+
function collectStateProps(node, ctx, props, variables) {
|
|
561
|
+
collectConstraintProps(node, ctx, props);
|
|
562
|
+
collectSizeLimitProps(node, props);
|
|
563
|
+
if (node.isMask) props.push(["mask", MASK_TYPE.print(node.maskType)]);
|
|
564
|
+
if (!node.visible) props.push(["visible", false]);
|
|
565
|
+
if (node.locked) props.push(["locked", true]);
|
|
566
|
+
collectModeProps(node, props, variables);
|
|
567
|
+
collectBindingProps(node, props, variables);
|
|
568
|
+
}
|
|
569
|
+
//#endregion
|
|
570
|
+
//#region src/export/variables.ts
|
|
571
|
+
/**
|
|
572
|
+
* Variables and modes by reference to the document's own, as `designVar` and `designMode` find
|
|
573
|
+
* them: by name when it is unique, then by collection and name, and by ID otherwise.
|
|
574
|
+
*/
|
|
575
|
+
function variableReferences(graph) {
|
|
576
|
+
const finds = (handle, variableId) => {
|
|
577
|
+
try {
|
|
578
|
+
return resolveVariableId(graph, handle) === variableId;
|
|
579
|
+
} catch {
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
const findsMode = (collection, mode, collectionId, modeId) => {
|
|
584
|
+
try {
|
|
585
|
+
return variableModes(graph, designMode(collection, mode))?.[collectionId] === modeId;
|
|
586
|
+
} catch {
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
return {
|
|
591
|
+
variable(variableId) {
|
|
592
|
+
const variable = graph.variables.get(variableId);
|
|
593
|
+
if (!variable) return variableId;
|
|
594
|
+
if (finds(designVar(variable.name), variableId)) return helperCall("designVar", variable.name);
|
|
595
|
+
const collection = graph.variableCollections.get(variable.collectionId);
|
|
596
|
+
return collection && finds(designVar(collection.name, variable.name), variableId) ? helperCall("designVar", collection.name, variable.name) : variableId;
|
|
597
|
+
},
|
|
598
|
+
mode(collectionId, modeId) {
|
|
599
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
600
|
+
const mode = collection?.modes.find((item) => item.modeId === modeId);
|
|
601
|
+
if (!collection || !mode) return modeId;
|
|
602
|
+
const named = [collection.name, collectionId].find((name) => findsMode(name, mode.name, collectionId, modeId));
|
|
603
|
+
return named ? helperCall("designMode", named, mode.name) : modeId;
|
|
604
|
+
},
|
|
605
|
+
declarations: () => ""
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
/** The variables the layers under `rootIds` bind. */
|
|
609
|
+
function boundVariableIds(graph, rootIds) {
|
|
610
|
+
return new Set(layersUnder(graph, rootIds).flatMap((layer) => Object.values(layer.boundVariables)));
|
|
611
|
+
}
|
|
612
|
+
/** A value `defineVars` can write: a color, a number, a boolean, or an alias. */
|
|
613
|
+
function definable(variable) {
|
|
614
|
+
if (variable.type === "STRING") return false;
|
|
615
|
+
return Object.values(variable.valuesByMode).every((value) => typeof value === "number" || typeof value === "boolean" || typeof value === "object" && ("aliasId" in value || "r" in value));
|
|
616
|
+
}
|
|
617
|
+
const TAKEN = /* @__PURE__ */ new Set([
|
|
618
|
+
...[
|
|
619
|
+
"await",
|
|
620
|
+
"break",
|
|
621
|
+
"case",
|
|
622
|
+
"catch",
|
|
623
|
+
"class",
|
|
624
|
+
"const",
|
|
625
|
+
"continue",
|
|
626
|
+
"debugger",
|
|
627
|
+
"default",
|
|
628
|
+
"delete",
|
|
629
|
+
"do",
|
|
630
|
+
"else",
|
|
631
|
+
"enum",
|
|
632
|
+
"export",
|
|
633
|
+
"extends",
|
|
634
|
+
"false",
|
|
635
|
+
"finally",
|
|
636
|
+
"for",
|
|
637
|
+
"function",
|
|
638
|
+
"if",
|
|
639
|
+
"implements",
|
|
640
|
+
"import",
|
|
641
|
+
"in",
|
|
642
|
+
"instanceof",
|
|
643
|
+
"interface",
|
|
644
|
+
"let",
|
|
645
|
+
"new",
|
|
646
|
+
"null",
|
|
647
|
+
"package",
|
|
648
|
+
"private",
|
|
649
|
+
"protected",
|
|
650
|
+
"public",
|
|
651
|
+
"return",
|
|
652
|
+
"static",
|
|
653
|
+
"super",
|
|
654
|
+
"switch",
|
|
655
|
+
"this",
|
|
656
|
+
"throw",
|
|
657
|
+
"true",
|
|
658
|
+
"try",
|
|
659
|
+
"typeof",
|
|
660
|
+
"var",
|
|
661
|
+
"void",
|
|
662
|
+
"while",
|
|
663
|
+
"with",
|
|
664
|
+
"yield",
|
|
665
|
+
"arguments",
|
|
666
|
+
"eval",
|
|
667
|
+
"undefined",
|
|
668
|
+
"NaN",
|
|
669
|
+
"Infinity"
|
|
670
|
+
],
|
|
671
|
+
...DESIGN_JSX_ELEMENTS.map((element) => element.name),
|
|
672
|
+
...DESIGN_JSX_HELPERS.map((helper) => helper.name),
|
|
673
|
+
"svg",
|
|
674
|
+
"React"
|
|
675
|
+
]);
|
|
676
|
+
function handleName(name, taken, fallback) {
|
|
677
|
+
const camel = camelCase(name);
|
|
678
|
+
const base = /^[A-Za-z_$][\w$]*$/.test(camel) ? camel : fallback;
|
|
679
|
+
let candidate = base;
|
|
680
|
+
for (let count = 2; taken.has(candidate); count++) candidate = `${base}${count}`;
|
|
681
|
+
taken.add(candidate);
|
|
682
|
+
return candidate;
|
|
683
|
+
}
|
|
684
|
+
/** The variable a value aliases, if it is an alias. */
|
|
685
|
+
function aliasOf(value) {
|
|
686
|
+
return typeof value === "object" && "aliasId" in value ? value.aliasId : void 0;
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Whether rendering finds `variable` again by its collection's name and its own, as definitions
|
|
690
|
+
* are found: a name another collection or variable shares stays a reference rather than redefine
|
|
691
|
+
* whichever comes first.
|
|
692
|
+
*/
|
|
693
|
+
function findsByName(graph, variable) {
|
|
694
|
+
const collection = graph.variableCollections.get(variable.collectionId);
|
|
695
|
+
if (!collection) return false;
|
|
696
|
+
try {
|
|
697
|
+
return collectionNamed(graph, collection.name) === collection && resolveVariableId(graph, designVar(collection.name, variable.name)) === variable.id;
|
|
698
|
+
} catch {
|
|
699
|
+
return false;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
/** The variables the layers bind that JSX can define, with every variable they alias. */
|
|
703
|
+
function definableVariables(graph, rootIds) {
|
|
704
|
+
const included = /* @__PURE__ */ new Set();
|
|
705
|
+
const include = (variableId) => {
|
|
706
|
+
const variable = graph.variables.get(variableId);
|
|
707
|
+
if (!variable || included.has(variableId) || !definable(variable)) return;
|
|
708
|
+
if (!findsByName(graph, variable)) return;
|
|
709
|
+
included.add(variableId);
|
|
710
|
+
for (const value of Object.values(variable.valuesByMode)) {
|
|
711
|
+
const target = aliasOf(value);
|
|
712
|
+
if (target) include(target);
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
for (const id of boundVariableIds(graph, rootIds)) include(id);
|
|
716
|
+
return included;
|
|
717
|
+
}
|
|
718
|
+
/** The variables by collection, each in its collection's order, as the variables panel lists them. */
|
|
719
|
+
function byCollectionOf(graph, included) {
|
|
720
|
+
const byCollection = /* @__PURE__ */ new Map();
|
|
721
|
+
for (const id of included) {
|
|
722
|
+
const variable = graph.variables.get(id);
|
|
723
|
+
if (variable) byCollection.set(variable.collectionId, [...byCollection.get(variable.collectionId) ?? [], variable]);
|
|
724
|
+
}
|
|
725
|
+
for (const [collectionId, variables] of byCollection) {
|
|
726
|
+
const listed = graph.variableCollections.get(collectionId)?.variableIds ?? [];
|
|
727
|
+
variables.sort((a, b) => listed.indexOf(a.id) - listed.indexOf(b.id));
|
|
728
|
+
}
|
|
729
|
+
return byCollection;
|
|
730
|
+
}
|
|
731
|
+
/** The collections in the order they are declared: after the ones their first mode aliases. */
|
|
732
|
+
function declarationOrder(graph, byCollection) {
|
|
733
|
+
const order = [];
|
|
734
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
735
|
+
const visit = (collectionId) => {
|
|
736
|
+
if (order.includes(collectionId) || visiting.has(collectionId)) return;
|
|
737
|
+
visiting.add(collectionId);
|
|
738
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
739
|
+
for (const variable of byCollection.get(collectionId) ?? []) {
|
|
740
|
+
const target = collection && aliasOf(variable.valuesByMode[collection.defaultModeId]);
|
|
741
|
+
const aliased = target ? graph.variables.get(target) : void 0;
|
|
742
|
+
if (aliased) visit(aliased.collectionId);
|
|
743
|
+
}
|
|
744
|
+
order.push(collectionId);
|
|
745
|
+
};
|
|
746
|
+
for (const id of byCollection.keys()) visit(id);
|
|
747
|
+
return order;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* What an export declares: the variables it can define and still have its code run. A first-mode
|
|
751
|
+
* value can only name a collection declared before its own, so one that aliases its own
|
|
752
|
+
* collection, or a later one in a cycle, is written by reference, which keeps the alias; and a
|
|
753
|
+
* declared value may alias only a declared variable, so one that aliases a reference, in any mode,
|
|
754
|
+
* is written by reference too.
|
|
755
|
+
*/
|
|
756
|
+
function declaredVariables(graph, rootIds) {
|
|
757
|
+
const included = definableVariables(graph, rootIds);
|
|
758
|
+
let byCollection = byCollectionOf(graph, included);
|
|
759
|
+
const order = declarationOrder(graph, byCollection);
|
|
760
|
+
const declaredEarlier = (variable) => {
|
|
761
|
+
const collection = graph.variableCollections.get(variable.collectionId);
|
|
762
|
+
const target = collection && aliasOf(variable.valuesByMode[collection.defaultModeId]);
|
|
763
|
+
const aliased = target && included.has(target) ? graph.variables.get(target) : void 0;
|
|
764
|
+
return !aliased || order.indexOf(aliased.collectionId) < order.indexOf(variable.collectionId);
|
|
765
|
+
};
|
|
766
|
+
const aliasesDeclared = (variable) => Object.values(variable.valuesByMode).every((value) => {
|
|
767
|
+
const target = aliasOf(value);
|
|
768
|
+
return !target || included.has(target);
|
|
769
|
+
});
|
|
770
|
+
for (const variable of [...byCollection.values()].flat()) if (!declaredEarlier(variable)) included.delete(variable.id);
|
|
771
|
+
for (let dropped = 1; dropped > 0;) {
|
|
772
|
+
const unresolved = [...included].flatMap((id) => {
|
|
773
|
+
const variable = graph.variables.get(id);
|
|
774
|
+
return variable && !aliasesDeclared(variable) ? [id] : [];
|
|
775
|
+
});
|
|
776
|
+
for (const id of unresolved) included.delete(id);
|
|
777
|
+
dropped = unresolved.length;
|
|
778
|
+
}
|
|
779
|
+
const used = /* @__PURE__ */ new Set();
|
|
780
|
+
const use = (variableId) => {
|
|
781
|
+
const variable = graph.variables.get(variableId);
|
|
782
|
+
if (!variable || used.has(variableId) || !included.has(variableId)) return;
|
|
783
|
+
used.add(variableId);
|
|
784
|
+
for (const value of Object.values(variable.valuesByMode)) {
|
|
785
|
+
const target = aliasOf(value);
|
|
786
|
+
if (target) use(target);
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
for (const id of boundVariableIds(graph, rootIds)) use(id);
|
|
790
|
+
for (const id of included) if (!used.has(id)) included.delete(id);
|
|
791
|
+
byCollection = byCollectionOf(graph, included);
|
|
792
|
+
return {
|
|
793
|
+
included,
|
|
794
|
+
byCollection,
|
|
795
|
+
order
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Writes the collections the layers use as `defineVars`, with a `defineMode` for each mode after
|
|
800
|
+
* the first, so the JSX carries its tokens: only the variables it binds and those they alias.
|
|
801
|
+
* Variables JSX cannot define, such as formulas, and collections whose name is not unique are
|
|
802
|
+
* written by reference.
|
|
803
|
+
*/
|
|
804
|
+
function definitionExport(graph, rootIds) {
|
|
805
|
+
const references = variableReferences(graph);
|
|
806
|
+
const { included, byCollection, order } = declaredVariables(graph, rootIds);
|
|
807
|
+
const taken = new Set(TAKEN);
|
|
808
|
+
const defined = /* @__PURE__ */ new Map();
|
|
809
|
+
for (const collectionId of order) {
|
|
810
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
811
|
+
if (!collection || !byCollection.get(collectionId)?.length) continue;
|
|
812
|
+
defined.set(collectionId, {
|
|
813
|
+
collection,
|
|
814
|
+
name: handleName(collection.name, taken, "vars"),
|
|
815
|
+
variables: byCollection.get(collectionId) ?? [],
|
|
816
|
+
modes: /* @__PURE__ */ new Map()
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
for (const entry of defined.values()) for (const mode of modesDefaultFirst(entry.collection).slice(1)) entry.modes.set(mode.modeId, handleName(mode.name, taken, `${entry.name}Mode`));
|
|
820
|
+
const variable = (variableId) => {
|
|
821
|
+
const found = graph.variables.get(variableId);
|
|
822
|
+
const entry = found && included.has(variableId) ? defined.get(found.collectionId) : void 0;
|
|
823
|
+
return entry && found ? new Reference(entry.name, found.name) : references.variable(variableId);
|
|
824
|
+
};
|
|
825
|
+
const value = (raw) => {
|
|
826
|
+
if (typeof raw === "number" || typeof raw === "boolean") return raw;
|
|
827
|
+
if (typeof raw !== "object") return void 0;
|
|
828
|
+
if ("aliasId" in raw) return variable(raw.aliasId);
|
|
829
|
+
return "r" in raw ? formatColor(raw, raw.a) : void 0;
|
|
830
|
+
};
|
|
831
|
+
const values = (variables, modeId, base) => Object.fromEntries(variables.flatMap((item) => {
|
|
832
|
+
const written = value(item.valuesByMode[modeId]);
|
|
833
|
+
const first = base === void 0 ? void 0 : value(item.valuesByMode[base]);
|
|
834
|
+
if (written === void 0) return [];
|
|
835
|
+
if (base !== void 0 && isEqual(written, first)) return [];
|
|
836
|
+
return [[item.name, written]];
|
|
837
|
+
}));
|
|
838
|
+
return {
|
|
839
|
+
variable,
|
|
840
|
+
mode(collectionId, modeId) {
|
|
841
|
+
const name = defined.get(collectionId)?.modes.get(modeId);
|
|
842
|
+
return name ? new Reference(name) : references.mode(collectionId, modeId);
|
|
843
|
+
},
|
|
844
|
+
declarations() {
|
|
845
|
+
const statements = [];
|
|
846
|
+
const declare = (name, value) => statements.push(`const ${name} = ${jsx.printJSX(valueSyntax(value))}`);
|
|
847
|
+
for (const entry of defined.values()) {
|
|
848
|
+
const [first] = modesDefaultFirst(entry.collection);
|
|
849
|
+
const options = first.name === "Mode 1" ? void 0 : { mode: first.name };
|
|
850
|
+
const call = helperCall("defineVars", entry.collection.name, values(entry.variables, first.modeId), options);
|
|
851
|
+
declare(entry.name, call);
|
|
852
|
+
}
|
|
853
|
+
for (const entry of defined.values()) {
|
|
854
|
+
const [first] = modesDefaultFirst(entry.collection);
|
|
855
|
+
for (const [modeId, name] of entry.modes) {
|
|
856
|
+
const mode = entry.collection.modes.find((item) => item.modeId === modeId);
|
|
857
|
+
declare(name, helperCall("defineMode", new Reference(entry.name), mode?.name ?? "", values(entry.variables, modeId, first.modeId)));
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return statements.join("\n");
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* How an export writes variables: with `declare`, as the definitions its JSX carries, otherwise
|
|
866
|
+
* by reference to the document's own.
|
|
867
|
+
*/
|
|
868
|
+
function variableExport(graph, rootIds, declare) {
|
|
869
|
+
return declare ? definitionExport(graph, rootIds) : variableReferences(graph);
|
|
870
|
+
}
|
|
871
|
+
//#endregion
|
|
872
|
+
//#region src/export/props.ts
|
|
873
|
+
/** The element that represents each node type; other types are not exported. */
|
|
874
|
+
function collectGridSizingProps(node, props) {
|
|
875
|
+
props.push(["grid", true]);
|
|
876
|
+
if (node.gridTemplateColumns.length > 0) props.push(["columns", formatTracks(node.gridTemplateColumns)]);
|
|
877
|
+
if (node.gridTemplateRows.length > 0) props.push(["rows", formatTracks(node.gridTemplateRows)]);
|
|
878
|
+
if (node.width > 0) props.push(["w", node.width]);
|
|
879
|
+
if (node.gridTemplateRows.length > 0 && node.height > 0) props.push(["h", node.height]);
|
|
880
|
+
if (node.gridColumnGap > 0) props.push(["columnGap", node.gridColumnGap]);
|
|
881
|
+
if (node.gridRowGap > 0) props.push(["rowGap", node.gridRowGap]);
|
|
882
|
+
}
|
|
883
|
+
function collectFlexSizingProps(node, props) {
|
|
884
|
+
props.push(["flex", node.layoutMode === "HORIZONTAL" ? "row" : "col"]);
|
|
885
|
+
if (node.layoutDirection === "RTL") props.push(["dir", "rtl"]);
|
|
886
|
+
const primaryAxis = node.layoutMode === "HORIZONTAL" ? "width" : "height";
|
|
887
|
+
const crossAxis = node.layoutMode === "HORIZONTAL" ? "height" : "width";
|
|
888
|
+
if (node.primaryAxisSizing !== "HUG") props.push([primaryAxis === "width" ? "w" : "h", node[primaryAxis]]);
|
|
889
|
+
if (node.counterAxisSizing !== "HUG") props.push([crossAxis === "width" ? "w" : "h", node[crossAxis]]);
|
|
890
|
+
}
|
|
891
|
+
function collectGridPositionProps(node, props) {
|
|
892
|
+
if (!node.gridPosition) return;
|
|
893
|
+
const pos = node.gridPosition;
|
|
894
|
+
if (pos.column > 0) props.push(["colStart", pos.column]);
|
|
895
|
+
if (pos.row > 0) props.push(["rowStart", pos.row]);
|
|
896
|
+
if (pos.columnSpan > 1) props.push(["colSpan", pos.columnSpan]);
|
|
897
|
+
if (pos.rowSpan > 1) props.push(["rowSpan", pos.rowSpan]);
|
|
898
|
+
}
|
|
899
|
+
function collectFlexAlignmentProps(node, props) {
|
|
900
|
+
if (node.itemSpacing > 0) props.push(["gap", node.itemSpacing]);
|
|
901
|
+
if (node.layoutWrap === "WRAP") {
|
|
902
|
+
props.push(["wrap", true]);
|
|
903
|
+
if (node.counterAxisSpacing > 0) props.push(["rowGap", node.counterAxisSpacing]);
|
|
904
|
+
}
|
|
905
|
+
if (node.primaryAxisAlign === "CENTER") props.push(["justify", "center"]);
|
|
906
|
+
else if (node.primaryAxisAlign === "MAX") props.push(["justify", "end"]);
|
|
907
|
+
else if (node.primaryAxisAlign === "SPACE_BETWEEN") props.push(["justify", "between"]);
|
|
908
|
+
else if (node.primaryAxisAlign === "SPACE_AROUND") props.push(["justify", "around"]);
|
|
909
|
+
if (node.counterAxisAlign === "CENTER") props.push(["items", "center"]);
|
|
910
|
+
else if (node.counterAxisAlign === "MAX") props.push(["items", "end"]);
|
|
911
|
+
else if (node.counterAxisAlign === "STRETCH") props.push(["items", "stretch"]);
|
|
912
|
+
else if (node.counterAxisAlign === "BASELINE") props.push(["items", "baseline"]);
|
|
913
|
+
}
|
|
914
|
+
function collectAutoLayoutPaddingProps(node, props) {
|
|
915
|
+
const pad = collectPadding(node);
|
|
916
|
+
if (!pad) return;
|
|
917
|
+
props.push(...emitPadding(pad, (v) => ["p", v], (y, x) => [["py", y], ["px", x]], ({ pt, pr, pb, pl }) => {
|
|
918
|
+
const r = [];
|
|
919
|
+
if (pt > 0) r.push(["pt", pt]);
|
|
920
|
+
if (pr > 0) r.push(["pr", pr]);
|
|
921
|
+
if (pb > 0) r.push(["pb", pb]);
|
|
922
|
+
if (pl > 0) r.push(["pl", pl]);
|
|
923
|
+
return r;
|
|
924
|
+
}));
|
|
925
|
+
}
|
|
926
|
+
function collectCornerRadiiProps(node, props) {
|
|
927
|
+
const corners = collectCornerRadii(node);
|
|
928
|
+
if (!corners) return;
|
|
929
|
+
const { tl, tr, br, bl } = corners;
|
|
930
|
+
if (tl === tr && tr === br && br === bl) props.push(["rounded", tl]);
|
|
931
|
+
else {
|
|
932
|
+
if (tl > 0) props.push(["roundedTL", tl]);
|
|
933
|
+
if (tr > 0) props.push(["roundedTR", tr]);
|
|
934
|
+
if (br > 0) props.push(["roundedBR", br]);
|
|
935
|
+
if (bl > 0) props.push(["roundedBL", bl]);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
function collectAppearanceProps(node, props) {
|
|
939
|
+
collectFillProps(node, props);
|
|
940
|
+
collectStrokeProps(node, props);
|
|
941
|
+
collectCornerRadiiProps(node, props);
|
|
942
|
+
if (node.cornerSmoothing > 0) props.push(["cornerSmoothing", node.cornerSmoothing]);
|
|
943
|
+
if (node.opacity < 1) props.push(["opacity", Math.round(node.opacity * 100) / 100]);
|
|
944
|
+
if (node.rotation !== 0) props.push(["rotate", Math.round(node.rotation * 100) / 100]);
|
|
945
|
+
if (node.flipX) props.push(["flipX", true]);
|
|
946
|
+
if (node.flipY) props.push(["flipY", true]);
|
|
947
|
+
if (node.blendMode !== "PASS_THROUGH" && node.blendMode !== "NORMAL") props.push(["blendMode", BLEND_MODE.print(node.blendMode)]);
|
|
948
|
+
if (node.clipsContent) props.push(["overflow", "hidden"]);
|
|
949
|
+
collectEffectProps(node, props);
|
|
950
|
+
}
|
|
951
|
+
function collectPositionProps(node, ctx, props) {
|
|
952
|
+
if (ctx.parentIsAutoLayout || ctx.parentIsGrid) {
|
|
953
|
+
if (node.layoutPositioning !== "ABSOLUTE") return;
|
|
954
|
+
props.push(["position", "absolute"], ["x", node.x], ["y", node.y]);
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (node.x !== 0) props.push(["x", node.x]);
|
|
958
|
+
if (node.y !== 0) props.push(["y", node.y]);
|
|
959
|
+
}
|
|
960
|
+
/** A layer's opacity and whether it is hidden, for an element that writes its own props. */
|
|
961
|
+
function presenceProps(node) {
|
|
962
|
+
const props = [];
|
|
963
|
+
if (node.opacity < 1) props.push(["opacity", Math.round(node.opacity * 100) / 100]);
|
|
964
|
+
if (!node.visible) props.push(["visible", false]);
|
|
965
|
+
return props;
|
|
966
|
+
}
|
|
967
|
+
/** Where a layer sits in its parent, as the export writes any layer's position. */
|
|
968
|
+
function positionProps(node, graph) {
|
|
969
|
+
const props = [];
|
|
970
|
+
collectPositionProps(node, getNodeContext(node, graph), props);
|
|
971
|
+
return props;
|
|
972
|
+
}
|
|
973
|
+
function collectSizingProps(node, ctx, graph, props) {
|
|
974
|
+
if (ctx.isGrid) collectGridSizingProps(node, props);
|
|
975
|
+
else if (ctx.isFlex) collectFlexSizingProps(node, props);
|
|
976
|
+
else if (node.type === "TEXT") collectTextSizingProps(node, graph, props);
|
|
977
|
+
else {
|
|
978
|
+
if (node.width > 0) props.push(["w", node.width]);
|
|
979
|
+
if (node.height > 0) props.push(["h", node.height]);
|
|
980
|
+
}
|
|
981
|
+
if (!ctx.parentIsAutoLayout) return;
|
|
982
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
983
|
+
const growAxis = parent?.layoutMode === "VERTICAL" ? "VERTICAL" : "HORIZONTAL";
|
|
984
|
+
const keepsGrow = parent?.layoutMode !== "GRID" && node.layoutGrow > 0;
|
|
985
|
+
if (keepsGrow) props.push(["grow", node.layoutGrow]);
|
|
986
|
+
for (const [axis, key] of [["HORIZONTAL", "w"], ["VERTICAL", "h"]]) {
|
|
987
|
+
if (keepsGrow && axis === growAxis) continue;
|
|
988
|
+
if (layoutSizing(graph, node, axis) !== "FILL") continue;
|
|
989
|
+
if (axis !== growAxis && node.layoutAlignSelf !== "STRETCH") continue;
|
|
990
|
+
const index = props.findIndex(([k]) => k === key);
|
|
991
|
+
if (index === -1) props.push([key, "fill"]);
|
|
992
|
+
else props[index] = [key, "fill"];
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
function collectTextSizingProps(node, graph, props) {
|
|
996
|
+
const autoResize = node.textAutoResize;
|
|
997
|
+
const emitH = autoResize === "NONE" || autoResize === "TRUNCATE";
|
|
998
|
+
const isFillWidth = node.layoutAlignSelf === "STRETCH" && (() => {
|
|
999
|
+
return (node.parentId ? graph.getNode(node.parentId) : null)?.layoutMode === "VERTICAL";
|
|
1000
|
+
})();
|
|
1001
|
+
const isGrowWidth = node.layoutGrow > 0 && (() => {
|
|
1002
|
+
return (node.parentId ? graph.getNode(node.parentId) : null)?.layoutMode === "HORIZONTAL";
|
|
1003
|
+
})();
|
|
1004
|
+
if (autoResize !== "WIDTH_AND_HEIGHT" && !isFillWidth && !isGrowWidth && node.width > 0) props.push(["w", node.width]);
|
|
1005
|
+
if (emitH && node.height > 0) props.push(["h", node.height]);
|
|
1006
|
+
}
|
|
1007
|
+
const DECORATION_FIELDS = [
|
|
1008
|
+
"textDecorationStyle",
|
|
1009
|
+
"textDecorationFills",
|
|
1010
|
+
"textDecorationThickness",
|
|
1011
|
+
"textUnderlineOffset",
|
|
1012
|
+
"textDecorationSkipInk"
|
|
1013
|
+
];
|
|
1014
|
+
const TEXT_DEFAULTS = pick(createDefaultNode(() => "", "TEXT"), [...DECORATION_FIELDS, ...FONT_SETTING_FIELDS]);
|
|
1015
|
+
/** The fields of `node` that differ from a new text's. */
|
|
1016
|
+
function ownTextFields(node, fields) {
|
|
1017
|
+
return omitBy(pick(node, fields), (value, key) => isEqual(value, TEXT_DEFAULTS[key]));
|
|
1018
|
+
}
|
|
1019
|
+
/** A text's font settings, and whether its optical size follows the font size. */
|
|
1020
|
+
function collectTextFontSettingProps(node, props) {
|
|
1021
|
+
collectFontSettingProps(ownTextFields(node, FONT_SETTING_FIELDS), props);
|
|
1022
|
+
if (!node.opticalSizeFromFontSize) props.push(["fontOpticalSizing", "none"]);
|
|
1023
|
+
}
|
|
1024
|
+
/** A font weight as `weight` writes it, by name for the weights that have one. */
|
|
1025
|
+
function fontWeightValue(weight) {
|
|
1026
|
+
if (weight === 700) return "bold";
|
|
1027
|
+
if (weight === 500) return "medium";
|
|
1028
|
+
return weight;
|
|
1029
|
+
}
|
|
1030
|
+
function collectTextNodeProps(node, props) {
|
|
1031
|
+
const direction = resolveNodeTextDirection(node);
|
|
1032
|
+
if (node.fontSize !== 14) props.push(["size", node.fontSize]);
|
|
1033
|
+
if (node.fontFamily && node.fontFamily !== DEFAULT_FONT_FAMILY) props.push(["font", node.fontFamily]);
|
|
1034
|
+
if (node.fontWeight !== 400) props.push(["weight", fontWeightValue(node.fontWeight)]);
|
|
1035
|
+
if (direction === "RTL") props.push(["dir", "rtl"]);
|
|
1036
|
+
if (node.italic) props.push(["italic", true]);
|
|
1037
|
+
if (node.textAlignHorizontal !== "LEFT") props.push(["textAlign", TEXT_ALIGN.print(node.textAlignHorizontal)]);
|
|
1038
|
+
if (node.textAlignVertical !== "TOP") props.push(["textAlignVertical", TEXT_ALIGN_VERTICAL.print(node.textAlignVertical)]);
|
|
1039
|
+
if (node.lineHeight != null) props.push(["lineHeight", textLengthJSX(node.lineHeight)]);
|
|
1040
|
+
if (node.letterSpacing.value !== 0) props.push(["letterSpacing", textLengthJSX(node.letterSpacing)]);
|
|
1041
|
+
if (node.textDecoration !== "NONE") props.push(["textDecoration", TEXT_DECORATION.print(node.textDecoration)]);
|
|
1042
|
+
collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props);
|
|
1043
|
+
collectTextFontSettingProps(node, props);
|
|
1044
|
+
if (node.textCase !== "ORIGINAL") props.push(["textCase", TEXT_CASE.print(node.textCase)]);
|
|
1045
|
+
if (node.maxLines != null) props.push(["maxLines", node.maxLines]);
|
|
1046
|
+
if (node.textTruncation === "ENDING" && node.maxLines == null) props.push(["truncate", true]);
|
|
1047
|
+
if (node.paragraphSpacing !== 0) props.push(["paragraphSpacing", node.paragraphSpacing]);
|
|
1048
|
+
if (node.listSpacing !== 0) props.push(["listSpacing", node.listSpacing]);
|
|
1049
|
+
if (node.paragraphIndent !== 0) props.push(["paragraphIndent", node.paragraphIndent]);
|
|
1050
|
+
if (node.hangingList) props.push(["hangingList", true]);
|
|
1051
|
+
}
|
|
1052
|
+
const TEXT_AUTO_RESIZE = {
|
|
1053
|
+
NONE: "none",
|
|
1054
|
+
WIDTH_AND_HEIGHT: "width",
|
|
1055
|
+
HEIGHT: "height",
|
|
1056
|
+
TRUNCATE: "truncate"
|
|
1057
|
+
};
|
|
1058
|
+
/** The renderer infers auto-resize from the width props; say so only when it differs. */
|
|
1059
|
+
function collectTextAutoResizeProp(node, ctx, props) {
|
|
1060
|
+
const hasWidth = props.some(([key]) => key === "w");
|
|
1061
|
+
const grows = ctx.parentIsAutoLayout && node.layoutGrow > 0;
|
|
1062
|
+
const inferred = hasWidth || grows ? "HEIGHT" : "WIDTH_AND_HEIGHT";
|
|
1063
|
+
if (node.textAutoResize !== inferred) props.push(["textAutoResize", TEXT_AUTO_RESIZE[node.textAutoResize]]);
|
|
1064
|
+
}
|
|
1065
|
+
const roundDegrees = (radians) => Math.round(radians * 180 / Math.PI * 1e4) / 1e4;
|
|
1066
|
+
/** An ellipse's arc, written only where it is not a whole ellipse. */
|
|
1067
|
+
function collectArcProps(arc, props) {
|
|
1068
|
+
if (!arc) return;
|
|
1069
|
+
const start = roundDegrees(arc.startingAngle);
|
|
1070
|
+
const end = roundDegrees(arc.endingAngle);
|
|
1071
|
+
if (start !== 0) props.push(["startAngle", start]);
|
|
1072
|
+
if (Math.abs(end - (start + 360)) > 1e-6) props.push(["endAngle", end]);
|
|
1073
|
+
if (arc.innerRadius !== 0) props.push(["innerRadius", arc.innerRadius]);
|
|
1074
|
+
}
|
|
1075
|
+
function collectShapeNodeProps(node, props) {
|
|
1076
|
+
if (node.type === "ELLIPSE") collectArcProps(node.arcData, props);
|
|
1077
|
+
if (node.type === "STAR") {
|
|
1078
|
+
if (node.pointCount !== 5) props.push(["points", node.pointCount]);
|
|
1079
|
+
if (node.starInnerRadius !== .382) props.push(["innerRadius", node.starInnerRadius]);
|
|
1080
|
+
}
|
|
1081
|
+
if (node.type === "POLYGON" && node.pointCount !== 3) props.push(["points", node.pointCount]);
|
|
1082
|
+
if (node.type === "VECTOR" && node.vectorNetwork) {
|
|
1083
|
+
const paths = vectorNetworkPaths(node.vectorNetwork);
|
|
1084
|
+
if (paths.length > 0) props.push(["paths", paths.map((path) => ({ ...path }))]);
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* The props of `node`, in the order they are printed, with the variables it binds written as
|
|
1089
|
+
* `variables` writes them: by default, by reference to the document's.
|
|
1090
|
+
*/
|
|
1091
|
+
function collectProps(node, graph, variables = variableReferences(graph)) {
|
|
1092
|
+
const props = [];
|
|
1093
|
+
const ctx = getNodeContext(node, graph);
|
|
1094
|
+
if (node.name && node.name !== node.type) props.push(["name", node.name]);
|
|
1095
|
+
collectPositionProps(node, ctx, props);
|
|
1096
|
+
collectSizingProps(node, ctx, graph, props);
|
|
1097
|
+
if (ctx.parentIsGrid) collectGridPositionProps(node, props);
|
|
1098
|
+
if (ctx.isFlex) collectFlexAlignmentProps(node, props);
|
|
1099
|
+
if (ctx.isAutoLayout) collectAutoLayoutPaddingProps(node, props);
|
|
1100
|
+
collectAppearanceProps(node, props);
|
|
1101
|
+
if (node.type === "TEXT") {
|
|
1102
|
+
collectTextNodeProps(node, props);
|
|
1103
|
+
collectTextAutoResizeProp(node, ctx, props);
|
|
1104
|
+
}
|
|
1105
|
+
collectShapeNodeProps(node, props);
|
|
1106
|
+
collectStateProps(node, ctx, props, variables);
|
|
1107
|
+
return props;
|
|
1108
|
+
}
|
|
1109
|
+
//#endregion
|
|
1110
|
+
//#region src/export/text.ts
|
|
1111
|
+
/**
|
|
1112
|
+
* Each character's style where it differs from the text's own, as a span writes it, with the
|
|
1113
|
+
* URL it links to; a span cannot write a link to a node.
|
|
1114
|
+
*/
|
|
1115
|
+
function characterStyles(node) {
|
|
1116
|
+
const styles = Array.from({ length: node.text.length }, () => ({}));
|
|
1117
|
+
for (const run of node.styleRuns) {
|
|
1118
|
+
const style = pick(run.style, SPAN_STYLE_FIELDS);
|
|
1119
|
+
if (run.style.hyperlink?.type === "URL") style.hyperlink = run.style.hyperlink;
|
|
1120
|
+
const end = Math.min(run.start + run.length, styles.length);
|
|
1121
|
+
for (let index = run.start; index < end; index++) styles[index] = {
|
|
1122
|
+
...styles[index],
|
|
1123
|
+
...style
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
return styles.map((style) => omitBy(style, (value, key) => value === void 0 || key !== "hyperlink" && isEqual(value, node[key])));
|
|
1127
|
+
}
|
|
1128
|
+
function spanProps(style, node) {
|
|
1129
|
+
const props = [];
|
|
1130
|
+
if (style.fontSize !== void 0) props.push(["size", style.fontSize]);
|
|
1131
|
+
if (style.fontFamily !== void 0) props.push(["font", style.fontFamily]);
|
|
1132
|
+
if (style.fontWeight !== void 0) props.push(["weight", fontWeightValue(style.fontWeight)]);
|
|
1133
|
+
if (style.italic !== void 0) props.push(["italic", style.italic]);
|
|
1134
|
+
if (style.fills) collectFillProps({
|
|
1135
|
+
...node,
|
|
1136
|
+
fills: style.fills
|
|
1137
|
+
}, props);
|
|
1138
|
+
if (style.lineHeight !== void 0) props.push(["lineHeight", style.lineHeight ? textLengthJSX(style.lineHeight) : AUTO]);
|
|
1139
|
+
if (style.letterSpacing !== void 0) props.push(["letterSpacing", textLengthJSX(style.letterSpacing)]);
|
|
1140
|
+
if (style.textDecoration !== void 0) props.push(["textDecoration", TEXT_DECORATION.print(style.textDecoration)]);
|
|
1141
|
+
collectDecorationProps(style, node, props);
|
|
1142
|
+
collectFontSettingProps(style, props);
|
|
1143
|
+
if (style.textCase !== void 0) props.push(["textCase", TEXT_CASE.print(style.textCase)]);
|
|
1144
|
+
if (style.hyperlink) props.push(["href", style.hyperlink.value]);
|
|
1145
|
+
return props;
|
|
1146
|
+
}
|
|
1147
|
+
/** `text[start, end)` as text and a `<Span>` for every stretch styled apart from the text. */
|
|
1148
|
+
function inlineContent(node, styles, start, end) {
|
|
1149
|
+
const content = [];
|
|
1150
|
+
let from = start;
|
|
1151
|
+
for (let index = start + 1; index <= end; index++) {
|
|
1152
|
+
if (index < end && isEqual(styles[index], styles[from])) continue;
|
|
1153
|
+
const text = jsx.inlineText(node.text.slice(from, index));
|
|
1154
|
+
const style = styles[from];
|
|
1155
|
+
content.push(isEmptyObject(style) ? text : jsx.element("Span", spanProps(style, node).map(propAttribute), [text], 0, true));
|
|
1156
|
+
from = index;
|
|
1157
|
+
}
|
|
1158
|
+
return content;
|
|
1159
|
+
}
|
|
1160
|
+
/** `<Paragraph>`'s prop for each spacing a paragraph can set, in the order they are written. */
|
|
1161
|
+
const PARAGRAPH_SPACING_PROPS = [
|
|
1162
|
+
["paragraphSpacing", "spacing"],
|
|
1163
|
+
["listSpacing", "listSpacing"],
|
|
1164
|
+
["paragraphIndent", "indent"]
|
|
1165
|
+
];
|
|
1166
|
+
/** A paragraph's style with only the spacing that differs from the text's own. */
|
|
1167
|
+
function ownParagraphStyle(node, paragraph) {
|
|
1168
|
+
let style = paragraphStyleAt(node.textParagraphs, paragraph);
|
|
1169
|
+
for (const [field] of PARAGRAPH_SPACING_PROPS) if (style[field] === node[field]) style = withParagraphSpacing(style, field, void 0);
|
|
1170
|
+
return style;
|
|
1171
|
+
}
|
|
1172
|
+
function paragraphProps(style) {
|
|
1173
|
+
const props = [];
|
|
1174
|
+
if (style.listType !== "NONE") {
|
|
1175
|
+
props.push(["list", style.listType === "ORDERED" ? "ordered" : "unordered"]);
|
|
1176
|
+
const level = Math.min(style.indentation, MAX_LIST_INDENTATION);
|
|
1177
|
+
if (level > 1) props.push(["level", level]);
|
|
1178
|
+
}
|
|
1179
|
+
for (const [field, name] of PARAGRAPH_SPACING_PROPS) {
|
|
1180
|
+
const value = style[field];
|
|
1181
|
+
if (value !== void 0) props.push([name, value]);
|
|
1182
|
+
}
|
|
1183
|
+
return props;
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* A text's content as JSX children: its text alone when it is styled as a whole, with
|
|
1187
|
+
* `<Span>`s for characters styled apart, and as `<Paragraph>`s when a paragraph is a list item
|
|
1188
|
+
* or has spacing other than the text's.
|
|
1189
|
+
*/
|
|
1190
|
+
function textChildren(node, depth) {
|
|
1191
|
+
const styles = characterStyles(node);
|
|
1192
|
+
const ranges = textParagraphRanges(node.text);
|
|
1193
|
+
const paragraphs = ranges.map((_, index) => ownParagraphStyle(node, index));
|
|
1194
|
+
if (!paragraphs.some((style) => style.listType !== "NONE" || hasParagraphSpacing(style))) {
|
|
1195
|
+
if (styles.every(isEmptyObject)) return {
|
|
1196
|
+
children: node.text ? [jsx.text(node.text)] : [],
|
|
1197
|
+
inline: true
|
|
1198
|
+
};
|
|
1199
|
+
return {
|
|
1200
|
+
children: inlineContent(node, styles, 0, node.text.length),
|
|
1201
|
+
inline: true
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
return {
|
|
1205
|
+
children: ranges.map(({ start, end }, index) => jsx.element("Paragraph", paragraphProps(paragraphs[index]).map(propAttribute), inlineContent(node, styles, start, end), depth + 1, true)),
|
|
1206
|
+
inline: false
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
//#endregion
|
|
1210
|
+
//#region src/export/index.ts
|
|
1211
|
+
const NO_REKA = {
|
|
1212
|
+
tags: /* @__PURE__ */ new Map(),
|
|
1213
|
+
props: /* @__PURE__ */ new Map(),
|
|
1214
|
+
flatten: /* @__PURE__ */ new Set()
|
|
1215
|
+
};
|
|
1216
|
+
function mergeReka(outer, inner) {
|
|
1217
|
+
if (!inner) return outer;
|
|
1218
|
+
return {
|
|
1219
|
+
tags: new Map([...outer.tags, ...inner.tags]),
|
|
1220
|
+
props: new Map([...outer.props, ...inner.props]),
|
|
1221
|
+
flatten: /* @__PURE__ */ new Set([...outer.flatten, ...inner.flatten])
|
|
1222
|
+
};
|
|
1223
|
+
}
|
|
1224
|
+
/** How many JSX elements the syntax holds, as an editor finds them in pre-order. */
|
|
1225
|
+
function elementCount(nodes) {
|
|
1226
|
+
let count = 0;
|
|
1227
|
+
for (const node of nodes) {
|
|
1228
|
+
if (node.type !== "JSXElement") continue;
|
|
1229
|
+
const children = node.children ?? [];
|
|
1230
|
+
count += 1 + elementCount(children);
|
|
1231
|
+
}
|
|
1232
|
+
return count;
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* The name an element named by its text writes: none while the layer still follows its text, and
|
|
1236
|
+
* its name once renamed, even to its text, so reading it back keeps the name from following it.
|
|
1237
|
+
*/
|
|
1238
|
+
function textNamedName(node) {
|
|
1239
|
+
return node.autoRename ? [] : [["name", node.name]];
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* The layers whose elements take props of their own rather than the layer props: every generated
|
|
1243
|
+
* frame, whose sublayers its element writes as props and text.
|
|
1244
|
+
*/
|
|
1245
|
+
const OWN_ELEMENTS = {
|
|
1246
|
+
STICKY: (graph, node) => ({
|
|
1247
|
+
props: [
|
|
1248
|
+
...textNamedName(node),
|
|
1249
|
+
...positionProps(node, graph),
|
|
1250
|
+
...stickyProps(graph, node).map(([key, value]) => [key, plainValue(value)]),
|
|
1251
|
+
...presenceProps(node)
|
|
1252
|
+
],
|
|
1253
|
+
text: stickyBody(graph, node)
|
|
1254
|
+
}),
|
|
1255
|
+
SHAPE_WITH_TEXT: (graph, node) => ({
|
|
1256
|
+
props: [
|
|
1257
|
+
...textNamedName(node),
|
|
1258
|
+
...positionProps(node, graph),
|
|
1259
|
+
...shapeWithTextProps(graph, node),
|
|
1260
|
+
...presenceProps(node)
|
|
1261
|
+
],
|
|
1262
|
+
text: shapeWithTextLabelText(graph, node)
|
|
1263
|
+
}),
|
|
1264
|
+
CONNECTOR: (graph, node, scope) => ({
|
|
1265
|
+
props: connectorProps(graph, node, scope).map(([key, value]) => [key, plainValue(value)]),
|
|
1266
|
+
text: connectorLabel(graph, node)
|
|
1267
|
+
})
|
|
1268
|
+
};
|
|
1269
|
+
/** What a layer whose element takes props of its own writes, or undefined for another layer. */
|
|
1270
|
+
function ownElement(graph, node, scope) {
|
|
1271
|
+
return isGeneratedFrame(node) ? OWN_ELEMENTS[node.type](graph, node, scope) : void 0;
|
|
1272
|
+
}
|
|
1273
|
+
/** The layers an export writes, by the roots it starts from. */
|
|
1274
|
+
function nodeToJSX(node, graph, depth, scope, variables, layerIds, outer = NO_REKA) {
|
|
1275
|
+
const reka = mergeReka(outer, rekaExport(graph, node));
|
|
1276
|
+
const ref = scope.refs.get(node.id);
|
|
1277
|
+
const withRef = (props) => ref ? [...props, ["ref", new Reference(ref)]] : props;
|
|
1278
|
+
const children = (childDepth) => graph.getChildren(node.id).flatMap((child) => nodeToJSX(child, graph, childDepth, scope, variables, layerIds, reka));
|
|
1279
|
+
if (reka.flatten.has(node.id)) return children(depth);
|
|
1280
|
+
const icon = iconProps(node, graph);
|
|
1281
|
+
if (icon) {
|
|
1282
|
+
layerIds?.push(node.id);
|
|
1283
|
+
return [jsx.element("Icon", withRef(icon).map(propAttribute), [], depth)];
|
|
1284
|
+
}
|
|
1285
|
+
const tag = reka.tags.get(node.id) ?? elementTag(node.type);
|
|
1286
|
+
if (!tag) return [];
|
|
1287
|
+
layerIds?.push(node.id);
|
|
1288
|
+
const own = ownElement(graph, node, scope);
|
|
1289
|
+
if (own) {
|
|
1290
|
+
const children = own.text ? [jsx.text(own.text)] : [];
|
|
1291
|
+
return [jsx.element(tag, own.props.map(propAttribute), children, depth, true)];
|
|
1292
|
+
}
|
|
1293
|
+
const ownProps = reka.props.get(node.id);
|
|
1294
|
+
if (ownProps?.some(([name]) => name === "of")) return [jsx.element(tag, withRef(ownProps).map(propAttribute), [], depth)];
|
|
1295
|
+
const attributes = withRef([...collectProps(node, graph, variables), ...ownProps ?? []]).map(propAttribute);
|
|
1296
|
+
if (node.type === "TEXT") {
|
|
1297
|
+
const { children: content, inline } = textChildren(node, depth);
|
|
1298
|
+
for (let count = elementCount(content); count > 0; count--) layerIds?.push(null);
|
|
1299
|
+
return [jsx.element(tag, attributes, content, depth, inline)];
|
|
1300
|
+
}
|
|
1301
|
+
return [jsx.element(tag, attributes, children(depth + 1), depth)];
|
|
1302
|
+
}
|
|
1303
|
+
/** Printed values break lines only between tokens, never inside a string, so joining is lossless. */
|
|
1304
|
+
function oneLine(source) {
|
|
1305
|
+
return source.replace(/\s*\n\s*/g, " ");
|
|
1306
|
+
}
|
|
1307
|
+
/**
|
|
1308
|
+
* The attributes `sceneNodeToJSX` prints for a node, each on one line, with a text node's
|
|
1309
|
+
* content, or a connector's label, as a `text` attribute. `null` for a node the export does not
|
|
1310
|
+
* write. A connector's ends are written by their layers' names.
|
|
1311
|
+
*/
|
|
1312
|
+
function sceneNodeAttributes(nodeId, graph) {
|
|
1313
|
+
const node = graph.getNode(nodeId);
|
|
1314
|
+
if (!node || !elementTag(node.type)) return null;
|
|
1315
|
+
const own = ownElement(graph, node);
|
|
1316
|
+
const props = own ? own.props : collectProps(node, graph);
|
|
1317
|
+
if (node.type === "TEXT") props.push(["text", node.text]);
|
|
1318
|
+
if (own?.text) props.push(["text", own.text]);
|
|
1319
|
+
return props.map((prop) => ({
|
|
1320
|
+
name: prop[0],
|
|
1321
|
+
source: oneLine(jsx.printJSX(propAttribute(prop)))
|
|
1322
|
+
}));
|
|
1323
|
+
}
|
|
1324
|
+
/** What an element holds between its tags, printed as `sceneNodeToJSX` prints it. */
|
|
1325
|
+
function contentBetween(tag, children, inline) {
|
|
1326
|
+
if (children.length === 0) return null;
|
|
1327
|
+
const printed = jsx.printJSX(jsx.element(tag, [], children, 0, inline));
|
|
1328
|
+
return printed.slice(tag.length + 2, printed.lastIndexOf(`</${tag}>`));
|
|
1329
|
+
}
|
|
1330
|
+
function designJSXElement(nodeId, graph) {
|
|
1331
|
+
const node = graph.getNode(nodeId);
|
|
1332
|
+
const tag = node && elementTag(node.type);
|
|
1333
|
+
const printed = sceneNodeAttributes(nodeId, graph);
|
|
1334
|
+
if (!node || !tag || !printed) return null;
|
|
1335
|
+
const own = ownElement(graph, node);
|
|
1336
|
+
const hasContent = node.type === "TEXT" || own !== void 0;
|
|
1337
|
+
const attributes = Object.fromEntries(printed.filter(({ name }) => !hasContent || name !== "text").map(({ name, source }) => [name, source]));
|
|
1338
|
+
if (node.type === "TEXT") {
|
|
1339
|
+
const { children, inline } = textChildren(node, 0);
|
|
1340
|
+
return {
|
|
1341
|
+
tag,
|
|
1342
|
+
attributes,
|
|
1343
|
+
text: contentBetween(tag, children, inline),
|
|
1344
|
+
childIds: []
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
if (own) return {
|
|
1348
|
+
tag,
|
|
1349
|
+
attributes,
|
|
1350
|
+
text: contentBetween(tag, own.text ? [jsx.text(own.text)] : [], true),
|
|
1351
|
+
childIds: []
|
|
1352
|
+
};
|
|
1353
|
+
return {
|
|
1354
|
+
tag,
|
|
1355
|
+
attributes,
|
|
1356
|
+
text: null,
|
|
1357
|
+
childIds: graph.getChildren(node.id).filter((child) => elementTag(child.type)).map((child) => child.id)
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
function sceneNodeToJSX(nodeId, graph) {
|
|
1361
|
+
return selectionToJSX([nodeId], graph);
|
|
1362
|
+
}
|
|
1363
|
+
function selectionToJSX(nodeIds, graph) {
|
|
1364
|
+
return selectionToJSXWithLayers(nodeIds, graph).code;
|
|
1365
|
+
}
|
|
1366
|
+
function selectionToJSXWithLayers(nodeIds, graph, options = {}) {
|
|
1367
|
+
const layerIds = [];
|
|
1368
|
+
const declare = options.declarations ?? true;
|
|
1369
|
+
const scope = connectorExportScope(graph, nodeIds, declare);
|
|
1370
|
+
const variables = variableExport(graph, nodeIds, declare);
|
|
1371
|
+
const roots = compact(nodeIds.map((id) => {
|
|
1372
|
+
const node = graph.getNode(id);
|
|
1373
|
+
const syntax = node ? nodeToJSX(node, graph, 0, scope, variables, layerIds).at(0) : void 0;
|
|
1374
|
+
return syntax ? jsx.printJSX(syntax) : "";
|
|
1375
|
+
}));
|
|
1376
|
+
const elements = roots.join("\n\n");
|
|
1377
|
+
const declarations = compact([variables.declarations(), connectorRefDeclarations(scope)]).join("\n\n");
|
|
1378
|
+
if (!declarations || !elements) return {
|
|
1379
|
+
code: elements,
|
|
1380
|
+
layerIds
|
|
1381
|
+
};
|
|
1382
|
+
return {
|
|
1383
|
+
code: `${declarations}\n\nreturn ${roots.length > 1 ? `<>\n${elements}\n</>` : elements}`,
|
|
1384
|
+
layerIds
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
//#endregion
|
|
1388
|
+
export { DESIGN_JSX_HELPERS, TEXT_CONTENT_NAMES, angularGradient, applyVariableDefinitions, bindVariableProp, designJSXElement, designJSXHelpers, diamondGradient, gradient, isShaderFill, isTextContent, linearGradient, radialGradient, requireVariableId, resolveForLayer, sceneNodeAttributes, sceneNodeToJSX, selectionToJSX, selectionToJSXWithLayers, shader, shaderSource, solid, textContentFields, variableModes };
|
|
1389
|
+
|
|
1390
|
+
//# sourceMappingURL=index.js.map
|