@moondraft/import 0.0.0-stage → 0.1.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/CHANGELOG.md +7 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +169 -0
- package/dist/adapter/convert.d.ts +122 -0
- package/dist/adapter/convert.js +2302 -0
- package/dist/adapter/figma.d.ts +9 -0
- package/dist/adapter/figma.js +30 -0
- package/dist/adapter/file-components.d.ts +14 -0
- package/dist/adapter/file-components.js +262 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +562 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +45 -0
- package/dist/adapter/link.js +688 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +44 -0
- package/dist/adapter/variables.js +103 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +96 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +49 -0
- package/dist/report.d.ts +15 -0
- package/dist/report.js +69 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -0
- package/dist/taco/figma-import/import/carry.js +33 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
- package/dist/taco/figma-import/import/componentProps.js +313 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +55 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
- package/dist/taco/figma-import/import/converters/gradient.js +106 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/group.js +39 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +83 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
- package/dist/taco/figma-import/import/converters/layout.js +168 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
- package/dist/taco/figma-import/import/converters/section.js +35 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
- package/dist/taco/figma-import/import/converters/shared.js +362 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
- package/dist/taco/figma-import/import/converters/text.js +576 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
- package/dist/taco/figma-import/import/converters/vector.js +468 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +163 -0
- package/dist/taco/figma-import/import/instances.js +1323 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +46 -0
- package/dist/taco/figma-import/import/styles.js +42 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +136 -0
- package/dist/taco/figma-import/import/variables.js +762 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +480 -0
- package/dist/taco/figma-import/import/walk.d.ts +159 -0
- package/dist/taco/figma-import/import/walk.js +1297 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
- package/dist/taco/figma-import/kiwi/message.js +145 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5635 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +94 -0
- package/dist/taco/model/component-evaluation.js +548 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +372 -0
- package/dist/taco/model/instance-resolver.js +1748 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +74 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2347 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +54 -4
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import { EXPRESSION_SOURCE_LIMIT, componentNestingGraph, findComponentCycle, formatExpressionName, layerPathKey, parseLayerPath, } from "../../model/index.js";
|
|
2
|
+
import { field, guidText, isObject } from "./fields.js";
|
|
3
|
+
import { mergeVariantTrees } from "./variantTrees.js";
|
|
4
|
+
import { propertyNameOf } from "./componentProps.js";
|
|
5
|
+
/** The variants of each set, in the set's child order, keyed by the set's guid. */
|
|
6
|
+
function variantsBySet(input) {
|
|
7
|
+
const sets = new Map();
|
|
8
|
+
const byId = new Map(input.variants.map((variant) => [variant.symbol.id, variant]));
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
for (const variant of input.variants) {
|
|
11
|
+
const set = variant.symbol.parentId === undefined ? undefined : input.figmaNodes.get(variant.symbol.parentId);
|
|
12
|
+
if (set?.change["isStateGroup"] !== true || seen.has(set))
|
|
13
|
+
continue;
|
|
14
|
+
seen.add(set);
|
|
15
|
+
sets.set(set, set.children.flatMap((child) => byId.get(child.id) ?? []));
|
|
16
|
+
}
|
|
17
|
+
return sets;
|
|
18
|
+
}
|
|
19
|
+
/** A part of a name that only lists `property=value` pairs, such as `variant=img, type=success`. */
|
|
20
|
+
const VARIANT_PART = /^[^=,]+=[^,]*(?:,[^=,]+=[^,]*)*$/;
|
|
21
|
+
/** The same, with no `/` in a property name, so only values can hold one. */
|
|
22
|
+
const VARIANT_LIST = /^[^=,/]+=[^,]*(?:,[^=,/]+=[^,]*)*$/;
|
|
23
|
+
/**
|
|
24
|
+
* A component's name without the parts, between `/`, that only list variant
|
|
25
|
+
* values: `Avatar / variant=img, type=success` gives `Avatar`, and
|
|
26
|
+
* `size=md` gives an empty name. A value may hold a `/`: a name that is only
|
|
27
|
+
* a list, such as `size=md, item=initials/icon`, gives an empty name, and
|
|
28
|
+
* `Avatar / item=initials/icon, size=md` gives `Avatar`. A name with no such
|
|
29
|
+
* part is kept as written.
|
|
30
|
+
*/
|
|
31
|
+
export function withoutVariantParts(name) {
|
|
32
|
+
// A variant's own name is only its values, so slashes in it are in values.
|
|
33
|
+
if (VARIANT_LIST.test(name.trim()))
|
|
34
|
+
return "";
|
|
35
|
+
const parts = [];
|
|
36
|
+
for (const part of name.split("/")) {
|
|
37
|
+
const last = parts.at(-1);
|
|
38
|
+
// A part that goes on with `name=value` pairs but is not a list by itself carries on the list before it.
|
|
39
|
+
if (last !== undefined && part.includes("=") && !VARIANT_PART.test(part.trim()) && VARIANT_LIST.test(`${last}/${part}`.trim()))
|
|
40
|
+
parts[parts.length - 1] = `${last}/${part}`;
|
|
41
|
+
else
|
|
42
|
+
parts.push(part);
|
|
43
|
+
}
|
|
44
|
+
const kept = parts.filter((part) => !VARIANT_PART.test(part.trim()));
|
|
45
|
+
return kept.length === parts.length ? name : kept.map((part) => part.trim()).filter((part) => part !== "").join(" / ");
|
|
46
|
+
}
|
|
47
|
+
/** The `name=value` pairs of a variant's name, by name; empty when the name is not such a list. */
|
|
48
|
+
function namedValues(name) {
|
|
49
|
+
const values = new Map();
|
|
50
|
+
for (const part of name.split(",")) {
|
|
51
|
+
const at = part.indexOf("=");
|
|
52
|
+
if (at > 0)
|
|
53
|
+
values.set(part.slice(0, at).trim(), part.slice(at + 1).trim());
|
|
54
|
+
}
|
|
55
|
+
return values;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Plans one set, or gives the reason it stays one component per variant:
|
|
59
|
+
* a variant without a value for each VARIANT property. A variant with the
|
|
60
|
+
* same values as an earlier one is one of the plan's `duplicates`.
|
|
61
|
+
* @param set - Figma component set.
|
|
62
|
+
* @param allVariants - Imported member mains in Figma order.
|
|
63
|
+
* @returns The ordered axes and choices, or the reason a set cannot be represented.
|
|
64
|
+
*/
|
|
65
|
+
export function planSet(set, allVariants) {
|
|
66
|
+
const where = `node ${set.id} componentPropDefs`;
|
|
67
|
+
const defs = (field(set.change, "componentPropDefs", "list", `node ${set.id}`) ?? [])
|
|
68
|
+
.filter((def) => isObject(def) && def["type"] === "VARIANT" && def["id"] !== undefined)
|
|
69
|
+
.map((def, at) => ({ id: guidText(def["id"], where), name: String(def["name"] ?? ""), sort: def["sortPosition"], at }));
|
|
70
|
+
if (defs.length === 0 && allVariants.length)
|
|
71
|
+
return "the set has no variant properties";
|
|
72
|
+
// Figma lists a set's properties by sortPosition.
|
|
73
|
+
defs.sort((a, b) => (typeof a.sort === "string" && typeof b.sort === "string" && a.sort !== b.sort ? (a.sort < b.sort ? -1 : 1) : a.at - b.at));
|
|
74
|
+
const orders = new Map();
|
|
75
|
+
for (const order of field(set.change, "stateGroupPropertyValueOrders", "list", `node ${set.id}`) ?? []) {
|
|
76
|
+
if (isObject(order) && typeof order["property"] === "string" && Array.isArray(order["values"]))
|
|
77
|
+
orders.set(order["property"], order["values"].filter((value) => typeof value === "string"));
|
|
78
|
+
}
|
|
79
|
+
const raw = [];
|
|
80
|
+
for (const variant of allVariants) {
|
|
81
|
+
const specs = new Map();
|
|
82
|
+
for (const spec of field(variant.symbol.change, "variantPropSpecs", "list", `node ${variant.symbol.id}`) ?? []) {
|
|
83
|
+
if (isObject(spec) && spec["propDefId"] !== undefined && typeof spec["value"] === "string")
|
|
84
|
+
specs.set(guidText(spec["propDefId"], `node ${variant.symbol.id} variantPropSpecs`), spec["value"]);
|
|
85
|
+
}
|
|
86
|
+
const named = namedValues(variant.symbol.name);
|
|
87
|
+
const values = new Map();
|
|
88
|
+
for (const def of defs) {
|
|
89
|
+
const value = specs.get(def.id) ?? named.get(def.name);
|
|
90
|
+
if (value === undefined)
|
|
91
|
+
return `variant ${JSON.stringify(variant.symbol.name)} has no value for ${JSON.stringify(def.name)}`;
|
|
92
|
+
values.set(def.id, value);
|
|
93
|
+
}
|
|
94
|
+
raw.push(values);
|
|
95
|
+
}
|
|
96
|
+
// Each set of values once, at its first variant; a later variant with the same values is a duplicate of that one.
|
|
97
|
+
const combinations = new Map();
|
|
98
|
+
const kept = [];
|
|
99
|
+
const keptRaw = [];
|
|
100
|
+
const duplicates = [];
|
|
101
|
+
raw.forEach((byDef, at) => {
|
|
102
|
+
const combination = JSON.stringify([...byDef.values()]);
|
|
103
|
+
const twin = combinations.get(combination);
|
|
104
|
+
if (twin !== undefined) {
|
|
105
|
+
duplicates.push({ variant: allVariants[at], twin });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
combinations.set(combination, kept.length);
|
|
109
|
+
kept.push(allVariants[at]);
|
|
110
|
+
keptRaw.push(byDef);
|
|
111
|
+
});
|
|
112
|
+
return planOf(set, defs, orders, kept, keptRaw, duplicates);
|
|
113
|
+
}
|
|
114
|
+
/** The plan of a set whose `variants` each have their own values (`raw`, by property def id). */
|
|
115
|
+
function planOf(set, defs, orders, variants, raw, duplicates) {
|
|
116
|
+
const taken = new Set();
|
|
117
|
+
const properties = defs.map((def) => {
|
|
118
|
+
const used = new Set(raw.map((values) => values.get(def.id)));
|
|
119
|
+
// The set's value order first, then values it does not list in the order the variants use them.
|
|
120
|
+
const ordered = [...(orders.get(def.name) ?? []).filter((value) => !variants.length || used.has(value)), ...used];
|
|
121
|
+
const id = `figma-${def.id}`;
|
|
122
|
+
const name = propertyNameOf(def.name, taken);
|
|
123
|
+
taken.add(name);
|
|
124
|
+
// Option names follow the same rules as property names: NFC, no control characters, not empty, unique.
|
|
125
|
+
const takenOptions = new Set();
|
|
126
|
+
const options = [...new Set(ordered)].map((value) => {
|
|
127
|
+
const optionName = propertyNameOf(value === "" ? "empty" : value, takenOptions);
|
|
128
|
+
takenOptions.add(optionName);
|
|
129
|
+
return { id: `${id}=${value}`, name: optionName };
|
|
130
|
+
});
|
|
131
|
+
return { id, name, kind: "enum", options, defaultValue: "" };
|
|
132
|
+
});
|
|
133
|
+
const values = raw.map((byDef) => Object.fromEntries(defs.map((def, at) => [properties[at].id, `${properties[at].id}=${byDef.get(def.id)}`])));
|
|
134
|
+
// Figma's default variant is the top-left one.
|
|
135
|
+
const placeOf = (variant) => {
|
|
136
|
+
const transform = variant.symbol.change["transform"];
|
|
137
|
+
return isObject(transform) ? [Number(transform["m12"]) || 0, Number(transform["m02"]) || 0] : [0, 0];
|
|
138
|
+
};
|
|
139
|
+
let defaultIndex = 0;
|
|
140
|
+
variants.forEach((variant, at) => {
|
|
141
|
+
const [y, x] = placeOf(variant);
|
|
142
|
+
const [bestY, bestX] = placeOf(variants[defaultIndex]);
|
|
143
|
+
if (y < bestY || (y === bestY && x < bestX))
|
|
144
|
+
defaultIndex = at;
|
|
145
|
+
});
|
|
146
|
+
return {
|
|
147
|
+
set, variants, duplicates, values, defaultIndex,
|
|
148
|
+
properties: properties.map((property) => ({ ...property, defaultValue: values[defaultIndex]?.[property.id] ?? property.options[0]?.id ?? "" })),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** `property == .option` for one value, names written as expressions read them. */
|
|
152
|
+
function equalsSource(property, optionId) {
|
|
153
|
+
const option = property.options.find((candidate) => candidate.id === optionId);
|
|
154
|
+
return `${formatExpressionName(property.name)} == .${formatExpressionName(option.name)}`;
|
|
155
|
+
}
|
|
156
|
+
/** The condition that shows one variant: all its values. */
|
|
157
|
+
function conditionOf(plan, index) {
|
|
158
|
+
return plan.properties.map((property) => equalsSource(property, plan.values[index][property.id])).join(" and ");
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* A boolean expression true for exactly `tuples` (option ids, one per
|
|
162
|
+
* property of `order`), factored: options of the first property that lead to
|
|
163
|
+
* the same rest share one term (`size == .sm or size == .md`, or
|
|
164
|
+
* `size != .lg` for all but one), a property the rest does not depend on is
|
|
165
|
+
* left out, and a rest that holds every combination is no condition. An
|
|
166
|
+
* empty result is true.
|
|
167
|
+
*/
|
|
168
|
+
function factored(order, tuples) {
|
|
169
|
+
if (order.length === 0)
|
|
170
|
+
return "";
|
|
171
|
+
const [property, ...rest] = order;
|
|
172
|
+
const every = order.reduce((count, each) => count * each.options.length, 1);
|
|
173
|
+
if (tuples.length === every)
|
|
174
|
+
return "";
|
|
175
|
+
const byOption = new Map();
|
|
176
|
+
for (const tuple of tuples) {
|
|
177
|
+
const [first, ...others] = tuple;
|
|
178
|
+
let list = byOption.get(first);
|
|
179
|
+
if (list === undefined)
|
|
180
|
+
byOption.set(first, list = []);
|
|
181
|
+
list.push(others);
|
|
182
|
+
}
|
|
183
|
+
// Options in option order, grouped by the expression of their rest.
|
|
184
|
+
const groups = new Map();
|
|
185
|
+
for (const option of property.options) {
|
|
186
|
+
const subTuples = byOption.get(option.id);
|
|
187
|
+
if (subTuples === undefined)
|
|
188
|
+
continue;
|
|
189
|
+
const sub = factored(rest, subTuples);
|
|
190
|
+
let list = groups.get(sub);
|
|
191
|
+
if (list === undefined)
|
|
192
|
+
groups.set(sub, list = []);
|
|
193
|
+
list.push(option.id);
|
|
194
|
+
}
|
|
195
|
+
const name = formatExpressionName(property.name);
|
|
196
|
+
const literal = (id) => `.${formatExpressionName(property.options.find((option) => option.id === id).name)}`;
|
|
197
|
+
const terms = [...groups].map(([sub, options]) => {
|
|
198
|
+
const missing = property.options.filter((option) => !options.includes(option.id));
|
|
199
|
+
const condition = missing.length === 0 ? ""
|
|
200
|
+
: options.length === 1 ? `${name} == ${literal(options[0])}`
|
|
201
|
+
: missing.length === 1 ? `${name} != ${literal(missing[0].id)}`
|
|
202
|
+
: `(${options.map((id) => `${name} == ${literal(id)}`).join(" or ")})`;
|
|
203
|
+
const wrapped = sub.includes(" or ") ? `(${sub})` : sub;
|
|
204
|
+
return [condition, wrapped].filter((part) => part !== "").join(" and ");
|
|
205
|
+
});
|
|
206
|
+
return terms.join(" or ");
|
|
207
|
+
}
|
|
208
|
+
/** Every order of `items` when there are at most six, else `items` as they are. */
|
|
209
|
+
function orders(items) {
|
|
210
|
+
if (items.length > 6)
|
|
211
|
+
return [[...items]];
|
|
212
|
+
if (items.length <= 1)
|
|
213
|
+
return [[...items]];
|
|
214
|
+
return items.flatMap((item, at) => orders([...items.slice(0, at), ...items.slice(at + 1)]).map((others) => [item, ...others]));
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The validity expression, or `undefined` when the set has every
|
|
218
|
+
* combination: the shortest of the factored valid combinations and the
|
|
219
|
+
* `not` of the factored missing ones (when the space has at most 4096
|
|
220
|
+
* combinations), over every order of the properties. `null` when even that
|
|
221
|
+
* is over the model's limit for an expression.
|
|
222
|
+
*/
|
|
223
|
+
function validityOf(plan) {
|
|
224
|
+
const every = plan.properties.reduce((count, property) => count * property.options.length, 1);
|
|
225
|
+
if (every === plan.variants.length)
|
|
226
|
+
return undefined;
|
|
227
|
+
const valid = plan.values.map((values) => plan.properties.map((property) => values[property.id]));
|
|
228
|
+
let missing;
|
|
229
|
+
if (every <= 4096) {
|
|
230
|
+
const have = new Set(valid.map((tuple) => tuple.join("\u0000")));
|
|
231
|
+
missing = plan.properties.reduce((combinations, property) => combinations.flatMap((combination) => property.options.map((option) => [...combination, option.id])), [[]])
|
|
232
|
+
.filter((tuple) => !have.has(tuple.join("\u0000")));
|
|
233
|
+
}
|
|
234
|
+
let best;
|
|
235
|
+
for (const order of orders(plan.properties.map((_, at) => at))) {
|
|
236
|
+
const properties = order.map((at) => plan.properties[at]);
|
|
237
|
+
const reorder = (tuple) => order.map((at) => tuple[at]);
|
|
238
|
+
const candidates = [factored(properties, valid.map(reorder))];
|
|
239
|
+
if (missing !== undefined)
|
|
240
|
+
candidates.push(`not (${factored(properties, missing.map(reorder))})`);
|
|
241
|
+
for (const candidate of candidates)
|
|
242
|
+
if (best === undefined || candidate.length < best.length)
|
|
243
|
+
best = candidate;
|
|
244
|
+
}
|
|
245
|
+
return best !== undefined && best.length <= EXPRESSION_SOURCE_LIMIT ? best : null;
|
|
246
|
+
}
|
|
247
|
+
/** The node's subtree ids inside one component, through children only. */
|
|
248
|
+
function subtreeIds(rootId, nodeOf) {
|
|
249
|
+
const ids = [];
|
|
250
|
+
for (const pending = [rootId]; pending.length > 0;) {
|
|
251
|
+
const id = pending.pop();
|
|
252
|
+
ids.push(id);
|
|
253
|
+
const node = nodeOf(id);
|
|
254
|
+
if (node !== undefined && "children" in node)
|
|
255
|
+
pending.push(...node.children);
|
|
256
|
+
}
|
|
257
|
+
return ids;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The sets whose merge would put a component inside itself: a variant that
|
|
261
|
+
* holds an instance of another variant of its own set, or sets whose variants
|
|
262
|
+
* hold each other. Each cycle found leaves its sets one component per variant.
|
|
263
|
+
*/
|
|
264
|
+
function setsInCycles(input, plans) {
|
|
265
|
+
const document = {
|
|
266
|
+
nodes: Object.fromEntries(input.nodes.map((node) => [node.id, node])),
|
|
267
|
+
components: Object.fromEntries(input.components.map((component) => [component.id, component])),
|
|
268
|
+
};
|
|
269
|
+
const graph = componentNestingGraph(document);
|
|
270
|
+
const excluded = new Set();
|
|
271
|
+
for (;;) {
|
|
272
|
+
const group = new Map();
|
|
273
|
+
const planOf = new Map();
|
|
274
|
+
for (const plan of plans) {
|
|
275
|
+
if (excluded.has(plan.set))
|
|
276
|
+
continue;
|
|
277
|
+
for (const variant of [...plan.variants, ...plan.duplicates.map((duplicate) => duplicate.variant)]) {
|
|
278
|
+
group.set(variant.componentId, plan.variants[plan.defaultIndex].componentId);
|
|
279
|
+
planOf.set(plan.variants[plan.defaultIndex].componentId, plan);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const of = (id) => group.get(id) ?? id;
|
|
283
|
+
const nests = new Map();
|
|
284
|
+
const nestedBy = new Map();
|
|
285
|
+
for (const [from, tos] of graph.nests) {
|
|
286
|
+
for (const to of tos) {
|
|
287
|
+
let set = nests.get(of(from));
|
|
288
|
+
if (set === undefined)
|
|
289
|
+
nests.set(of(from), set = new Set());
|
|
290
|
+
set.add(of(to));
|
|
291
|
+
let back = nestedBy.get(of(to));
|
|
292
|
+
if (back === undefined)
|
|
293
|
+
nestedBy.set(of(to), back = new Set());
|
|
294
|
+
back.add(of(from));
|
|
295
|
+
}
|
|
296
|
+
if (!nests.has(of(from)))
|
|
297
|
+
nests.set(of(from), new Set());
|
|
298
|
+
}
|
|
299
|
+
const cycle = findComponentCycle({ nests, nestedBy });
|
|
300
|
+
if (cycle === null)
|
|
301
|
+
return excluded;
|
|
302
|
+
const sets = cycle.flatMap((id) => planOf.get(id)?.set ?? []);
|
|
303
|
+
// A cycle through plain components only cannot be: the import already cut those.
|
|
304
|
+
if (sets.length === 0)
|
|
305
|
+
return excluded;
|
|
306
|
+
for (const set of sets)
|
|
307
|
+
excluded.add(set);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/** Sets one axis of a sizing override, keeping the other. */
|
|
311
|
+
function withSizing(override, axis, value) {
|
|
312
|
+
const { sizing, ...rest } = override ?? {};
|
|
313
|
+
const { [axis]: _old, ...other } = sizing ?? {};
|
|
314
|
+
const next = value === undefined ? other : { ...other, [axis]: value };
|
|
315
|
+
const result = Object.keys(next).length === 0 ? rest : { ...rest, sizing: next };
|
|
316
|
+
return Object.keys(result).length === 0 ? undefined : result;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Merges each component set into one component (see the top of this file).
|
|
320
|
+
* A set whose variants cannot be told apart by their values, or whose merge
|
|
321
|
+
* would nest a component in itself, stays one component per variant and is
|
|
322
|
+
* reported.
|
|
323
|
+
*/
|
|
324
|
+
export function mergeComponentSets(input) {
|
|
325
|
+
const plans = [];
|
|
326
|
+
for (const [set, variants] of variantsBySet(input)) {
|
|
327
|
+
const plan = planSet(set, variants);
|
|
328
|
+
const validity = typeof plan === "string" ? undefined : validityOf(plan);
|
|
329
|
+
if (typeof plan === "string")
|
|
330
|
+
input.report(set, "approximated", `the component set is imported as one component per variant: ${plan}`);
|
|
331
|
+
else if (validity === null || plan.variants.some((_, at) => conditionOf(plan, at).length > EXPRESSION_SOURCE_LIMIT))
|
|
332
|
+
input.report(set, "approximated", "the component set is imported as one component per variant: the expression of its combinations is too long");
|
|
333
|
+
else {
|
|
334
|
+
for (const { variant, twin } of plan.duplicates) {
|
|
335
|
+
input.report(variant.symbol, "approximated", `the variant has the same values as ${JSON.stringify(plan.variants[twin].symbol.name)}: its layers are kept but never shown, and its instances show that variant`);
|
|
336
|
+
}
|
|
337
|
+
plans.push({ ...plan, ...(validity === undefined ? {} : { validity }) });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const cycles = setsInCycles(input, plans);
|
|
341
|
+
for (const set of cycles)
|
|
342
|
+
input.report(set, "approximated", "the component set is imported as one component per variant: as one component it would contain an instance of itself");
|
|
343
|
+
const merged = plans.filter((plan) => !cycles.has(plan.set));
|
|
344
|
+
const componentOf = new Map();
|
|
345
|
+
if (merged.length === 0)
|
|
346
|
+
return { nodes: [...input.nodes], components: [...input.components], componentOf, layerOf: new Map() };
|
|
347
|
+
const byId = new Map(input.nodes.map((node) => [node.id, node]));
|
|
348
|
+
const replaced = new Map();
|
|
349
|
+
const current = (id) => replaced.get(id) ?? byId.get(id);
|
|
350
|
+
// The set component, the root and the values of each variant's component.
|
|
351
|
+
const variantOf = new Map();
|
|
352
|
+
const definitions = new Map();
|
|
353
|
+
const dropped = new Set();
|
|
354
|
+
const rootsBefore = new Map();
|
|
355
|
+
for (const plan of merged) {
|
|
356
|
+
const main = plan.variants[plan.defaultIndex];
|
|
357
|
+
const rootId = input.newId();
|
|
358
|
+
const name = input.nameOf(plan.set);
|
|
359
|
+
// The variants' other properties, each once; a name another property has gets a number, and the expressions reading it follow.
|
|
360
|
+
const taken = new Set(plan.properties.map((property) => property.name));
|
|
361
|
+
const others = [];
|
|
362
|
+
const named = new Map();
|
|
363
|
+
for (const variant of [...plan.variants, ...plan.duplicates.map((duplicate) => duplicate.variant)]) {
|
|
364
|
+
const renames = new Map();
|
|
365
|
+
for (const property of input.components.find((component) => component.id === variant.componentId)?.properties ?? []) {
|
|
366
|
+
let finalName = named.get(property.id);
|
|
367
|
+
if (finalName === undefined) {
|
|
368
|
+
finalName = taken.has(property.name) ? propertyNameOf(property.name, taken) : property.name;
|
|
369
|
+
if (finalName !== property.name)
|
|
370
|
+
input.report(plan.set, "approximated", "a component property name is changed: a name used twice gets a number, and control, line break and bidirectional formatting characters are dropped");
|
|
371
|
+
taken.add(finalName);
|
|
372
|
+
named.set(property.id, finalName);
|
|
373
|
+
others.push({ ...property, name: finalName });
|
|
374
|
+
}
|
|
375
|
+
if (finalName !== property.name)
|
|
376
|
+
renames.set(formatExpressionName(property.name), formatExpressionName(finalName));
|
|
377
|
+
}
|
|
378
|
+
if (renames.size > 0) {
|
|
379
|
+
for (const id of subtreeIds(variant.root.id, current)) {
|
|
380
|
+
const node = current(id);
|
|
381
|
+
if (node.expressions === undefined)
|
|
382
|
+
continue;
|
|
383
|
+
const expressions = Object.fromEntries(Object.entries(node.expressions).map(([key, source]) => [key, renames.get(source) ?? source]));
|
|
384
|
+
replaced.set(id, { ...node, expressions });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
definitions.set(main.componentId, {
|
|
389
|
+
id: main.componentId, key: input.keyOf(plan.set), name, rootId,
|
|
390
|
+
properties: [...plan.properties, ...others],
|
|
391
|
+
...(plan.validity === undefined ? {} : { validity: plan.validity }),
|
|
392
|
+
});
|
|
393
|
+
plan.variants.forEach((variant, at) => {
|
|
394
|
+
if (variant !== main)
|
|
395
|
+
dropped.add(variant.componentId);
|
|
396
|
+
// A shared property whose default this variant sets apart keeps that default in the variant's values.
|
|
397
|
+
const ownDefaults = {};
|
|
398
|
+
for (const property of input.components.find((component) => component.id === variant.componentId)?.properties ?? []) {
|
|
399
|
+
const kept = others.find((other) => other.id === property.id);
|
|
400
|
+
const own = "defaultValue" in property ? property.defaultValue : undefined;
|
|
401
|
+
if (kept !== undefined && own !== undefined && JSON.stringify(own) !== JSON.stringify("defaultValue" in kept ? kept.defaultValue : undefined))
|
|
402
|
+
ownDefaults[property.id] = own;
|
|
403
|
+
}
|
|
404
|
+
variantOf.set(variant.componentId, { set: plan.set, component: main.componentId, root: variant.root, setRoot: rootId, values: { ...ownDefaults, ...plan.values[at] } });
|
|
405
|
+
componentOf.set(variant.symbol.id, main.componentId);
|
|
406
|
+
const root = current(variant.root.id);
|
|
407
|
+
rootsBefore.set(variant.root.id, root);
|
|
408
|
+
// A locked aspect ratio never shaped an instance of the variant, so it does not shape the frame that now stands for it.
|
|
409
|
+
const { aspectRatio: _ratio, aspectRatioLocked: _locked, ...unlocked } = root;
|
|
410
|
+
// An instance's own opacity stood in for the variant root's, so the root, now a layer inside it, is opaque.
|
|
411
|
+
replaced.set(variant.root.id, { ...unlocked, opacity: 1, condition: conditionOf(plan, at) });
|
|
412
|
+
});
|
|
413
|
+
// A duplicate stands for its twin: an instance of it shows the twin, sized as the twin's root.
|
|
414
|
+
for (const { variant, twin } of plan.duplicates) {
|
|
415
|
+
dropped.add(variant.componentId);
|
|
416
|
+
variantOf.set(variant.componentId, variantOf.get(plan.variants[twin].componentId));
|
|
417
|
+
componentOf.set(variant.symbol.id, main.componentId);
|
|
418
|
+
const root = current(variant.root.id);
|
|
419
|
+
rootsBefore.set(variant.root.id, root);
|
|
420
|
+
replaced.set(variant.root.id, { ...root, opacity: 1, condition: "false" });
|
|
421
|
+
}
|
|
422
|
+
componentOf.set(plan.set.id, main.componentId);
|
|
423
|
+
const size = main.root.frame;
|
|
424
|
+
const setRoot = {
|
|
425
|
+
id: rootId, type: "frame", name, visible: true, locked: false, opacity: 1, frame: { x: 0, y: 0, width: size.width, height: size.height }, rotation: 0,
|
|
426
|
+
children: [...plan.variants, ...plan.duplicates.map((duplicate) => duplicate.variant)].map((variant) => variant.root.id),
|
|
427
|
+
layout: { kind: "vertical", widthSizing: "hug", heightSizing: "hug", gap: 0, padding: { top: 0, right: 0, bottom: 0, left: 0 }, align: "start", justify: "start" },
|
|
428
|
+
clipContent: false,
|
|
429
|
+
};
|
|
430
|
+
replaced.set(rootId, setRoot);
|
|
431
|
+
}
|
|
432
|
+
// A value naming a variant names its set's component; the values it held are the variant's.
|
|
433
|
+
const swapTo = (value) => variantOf.get(value)?.component ?? value;
|
|
434
|
+
const withSwaps = (values) => (values === undefined ? undefined : Object.fromEntries(Object.entries(values).map(([id, value]) => [id, swapTo(value)])));
|
|
435
|
+
for (const [id, variant] of variantOf)
|
|
436
|
+
variantOf.set(id, { ...variant, values: withSwaps(variant.values) });
|
|
437
|
+
const components = [];
|
|
438
|
+
const definitionOf = new Map();
|
|
439
|
+
for (const component of input.components) {
|
|
440
|
+
if (dropped.has(component.id))
|
|
441
|
+
continue;
|
|
442
|
+
const base = definitions.get(component.id) ?? component;
|
|
443
|
+
const properties = base.properties?.map((property) => {
|
|
444
|
+
if (property.kind !== "instance" && property.kind !== "slot")
|
|
445
|
+
return property;
|
|
446
|
+
const preferred = property.preferred === undefined ? undefined : [...new Set(property.preferred.map((id) => variantOf.get(id)?.component ?? id))];
|
|
447
|
+
const withPreferred = preferred === undefined ? property : { ...property, preferred };
|
|
448
|
+
return property.kind === "instance" ? { ...withPreferred, defaultValue: variantOf.get(property.defaultValue)?.component ?? property.defaultValue } : withPreferred;
|
|
449
|
+
});
|
|
450
|
+
const definition = properties === undefined ? base : { ...base, properties };
|
|
451
|
+
components.push(definition);
|
|
452
|
+
definitionOf.set(definition.id, definition);
|
|
453
|
+
}
|
|
454
|
+
// Each instance of a variant: the set's component with the variant's values. Where Figma's instance is not the variant's own
|
|
455
|
+
// size (resized, or filling its parent), the set's root keeps the instance's size and the variant's frame fills it.
|
|
456
|
+
const rewriteInstance = (node) => {
|
|
457
|
+
const overrides = {};
|
|
458
|
+
for (const [key, override] of Object.entries(node.overrides ?? {})) {
|
|
459
|
+
const swapped = override.componentId === undefined ? undefined : variantOf.get(override.componentId);
|
|
460
|
+
const propertyValues = swapped === undefined ? withSwaps(override.propertyValues) : { ...swapped.values, ...withSwaps(override.propertyValues) };
|
|
461
|
+
overrides[key] = {
|
|
462
|
+
...override,
|
|
463
|
+
...(swapped === undefined ? {} : { componentId: swapped.component }),
|
|
464
|
+
...(propertyValues === undefined ? {} : { propertyValues }),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
const variant = variantOf.get(node.componentId);
|
|
468
|
+
let next = { ...node, ...(node.propertyValues === undefined ? {} : { propertyValues: withSwaps(node.propertyValues) }) };
|
|
469
|
+
if (variant !== undefined) {
|
|
470
|
+
const root = rootsBefore.get(variant.root.id) ?? variant.root;
|
|
471
|
+
const variantKey = layerPathKey([root.id]);
|
|
472
|
+
const setKey = layerPathKey([variant.setRoot]);
|
|
473
|
+
for (const axis of ["width", "height"]) {
|
|
474
|
+
const own = overrides[variantKey]?.sizing?.[axis];
|
|
475
|
+
const layoutSizing = root.layout.kind === "absolute" ? "fixed" : axis === "width" ? root.layout.widthSizing : root.layout.heightSizing;
|
|
476
|
+
const sizing = own ?? layoutSizing;
|
|
477
|
+
let fill = node.resizing?.[axis] === "fill";
|
|
478
|
+
// A size bound to a variable holds only on a Fixed axis, as the variant's own root was.
|
|
479
|
+
if (!fill && sizing !== "hug" && (Math.abs(node.frame[axis] - root.frame[axis]) > 0.01 || node.variableBindings?.[axis] !== undefined)) {
|
|
480
|
+
fill = true;
|
|
481
|
+
const at = withSizing(overrides[setKey], axis, "fixed");
|
|
482
|
+
if (at === undefined)
|
|
483
|
+
delete overrides[setKey];
|
|
484
|
+
else
|
|
485
|
+
overrides[setKey] = at;
|
|
486
|
+
}
|
|
487
|
+
if (fill) {
|
|
488
|
+
const at = withSizing(overrides[variantKey], axis, "fill");
|
|
489
|
+
if (at !== undefined)
|
|
490
|
+
overrides[variantKey] = at;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
next = { ...next, componentId: variant.component, propertyValues: { ...variant.values, ...next.propertyValues } };
|
|
494
|
+
}
|
|
495
|
+
const { overrides: _old, ...rest } = next;
|
|
496
|
+
return Object.keys(overrides).length === 0 ? rest : { ...rest, overrides };
|
|
497
|
+
};
|
|
498
|
+
for (const node of input.nodes) {
|
|
499
|
+
const at = current(node.id);
|
|
500
|
+
if (at.type === "instance")
|
|
501
|
+
replaced.set(at.id, rewriteInstance(at));
|
|
502
|
+
}
|
|
503
|
+
// An instance swap value that named a variant shows the set's component: the layers that read the property take the variant's values.
|
|
504
|
+
// In a set, only the variant an instance shows is searched, picked by its values.
|
|
505
|
+
const variantRoots = new Map();
|
|
506
|
+
for (const plan of merged) {
|
|
507
|
+
variantRoots.set(plan.variants[plan.defaultIndex].componentId, {
|
|
508
|
+
properties: definitions.get(plan.variants[plan.defaultIndex].componentId).properties.filter((property) => property.kind === "enum").slice(0, plan.properties.length),
|
|
509
|
+
roots: plan.variants.map((variant, at) => ({ rootId: variant.root.id, values: plan.values[at] })),
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
const searchedRoot = (componentId, values) => {
|
|
513
|
+
const set = variantRoots.get(componentId);
|
|
514
|
+
if (set === undefined)
|
|
515
|
+
return definitionOf.get(componentId)?.rootId;
|
|
516
|
+
return set.roots.find((root) => set.properties.every((property) => (values[property.id] ?? property.defaultValue) === root.values[property.id]))?.rootId;
|
|
517
|
+
};
|
|
518
|
+
const bound = new Map();
|
|
519
|
+
const boundLayers = (componentId, values, propertyId) => {
|
|
520
|
+
const rootId = searchedRoot(componentId, values);
|
|
521
|
+
const property = definitionOf.get(componentId)?.properties?.find((candidate) => candidate.id === propertyId);
|
|
522
|
+
if (rootId === undefined || property === undefined)
|
|
523
|
+
return [];
|
|
524
|
+
const cacheKey = `${rootId}\u0000${propertyId}`;
|
|
525
|
+
const known = bound.get(cacheKey);
|
|
526
|
+
if (known !== undefined)
|
|
527
|
+
return known;
|
|
528
|
+
const source = formatExpressionName(property.name);
|
|
529
|
+
const layers = subtreeIds(rootId, current).filter((id) => id !== rootId && current(id)?.expressions?.componentId === source);
|
|
530
|
+
bound.set(cacheKey, layers);
|
|
531
|
+
return layers;
|
|
532
|
+
};
|
|
533
|
+
// The component, and its property values, shown at the end of a layer path inside an instance of `componentId` with `values`:
|
|
534
|
+
// through nested instances, their swap overrides, and the swaps their component's instance swap properties compute.
|
|
535
|
+
const shownAt = (componentId, values, path, overrides) => {
|
|
536
|
+
let shown = { componentId, values };
|
|
537
|
+
// The overrides of each instance on the way, from the outermost, with the index their keys start at.
|
|
538
|
+
const scopes = [{ overrides, from: 0 }];
|
|
539
|
+
for (let at = 0; at < path.length; at++) {
|
|
540
|
+
const layer = current(path[at]);
|
|
541
|
+
if (layer?.type !== "instance")
|
|
542
|
+
return undefined;
|
|
543
|
+
// An outer instance's override wins over an inner one's.
|
|
544
|
+
const found = scopes.map(({ overrides: within, from }) => within[layerPathKey(path.slice(from, at + 1))]);
|
|
545
|
+
const reads = layer.expressions?.componentId;
|
|
546
|
+
const property = reads === undefined ? undefined : definitionOf.get(shown.componentId)?.properties?.find((candidate) => formatExpressionName(candidate.name) === reads);
|
|
547
|
+
const computed = property?.kind === "instance" ? (shown.values[property.id] ?? property.defaultValue) : undefined;
|
|
548
|
+
const swapped = found.find((override) => override?.componentId !== undefined)?.componentId;
|
|
549
|
+
const given = Object.assign({}, ...[...found].reverse().map((override) => override?.propertyValues ?? {}));
|
|
550
|
+
shown = { componentId: swapped ?? computed ?? layer.componentId, values: { ...layer.propertyValues, ...given } };
|
|
551
|
+
if (layer.overrides !== undefined)
|
|
552
|
+
scopes.push({ overrides: layer.overrides, from: at + 1 });
|
|
553
|
+
}
|
|
554
|
+
return shown;
|
|
555
|
+
};
|
|
556
|
+
const originalValue = (values, id) => values?.[id];
|
|
557
|
+
const rootBefore = new Map(input.components.map((component) => [component.id, byId.get(component.rootId)]));
|
|
558
|
+
// The axes on which a nested instance keeps its own size when it is swapped, as Figma keeps a resized instance's size:
|
|
559
|
+
// `fill` where it fills its parent, `resized` where, on an axis that does not hug, Figma shows it at another size than its
|
|
560
|
+
// component's or binds its size to a variable.
|
|
561
|
+
const keptAxes = (layer) => {
|
|
562
|
+
const root = rootBefore.get(layer.componentId);
|
|
563
|
+
const kept = {};
|
|
564
|
+
if (root?.type !== "frame")
|
|
565
|
+
return kept;
|
|
566
|
+
for (const axis of ["width", "height"]) {
|
|
567
|
+
const own = layer.overrides?.[layerPathKey([root.id])]?.sizing?.[axis];
|
|
568
|
+
const sizing = own ?? (root.layout.kind === "absolute" ? "fixed" : axis === "width" ? root.layout.widthSizing : root.layout.heightSizing);
|
|
569
|
+
if (layer.resizing?.[axis] === "fill")
|
|
570
|
+
kept[axis] = "fill";
|
|
571
|
+
else if (sizing !== "hug" && (Math.abs(layer.frame[axis] - root.frame[axis]) > 0.01 || layer.variableBindings?.[axis] !== undefined))
|
|
572
|
+
kept[axis] = "resized";
|
|
573
|
+
}
|
|
574
|
+
return kept;
|
|
575
|
+
};
|
|
576
|
+
// A nested instance at `path` swapped to a variant keeps its size there: the set's root keeps the size, and the variant's frame fills it.
|
|
577
|
+
const keepSize = (overrides, path, layer, variant) => {
|
|
578
|
+
if (layer?.type !== "instance")
|
|
579
|
+
return;
|
|
580
|
+
for (const [axis, kind] of Object.entries(keptAxes(layer))) {
|
|
581
|
+
const variantKey = layerPathKey([...path, variant.root.id]);
|
|
582
|
+
overrides[variantKey] = withSizing(overrides[variantKey], axis, "fill");
|
|
583
|
+
if (kind === "resized") {
|
|
584
|
+
const setKey = layerPathKey([...path, variant.setRoot]);
|
|
585
|
+
overrides[setKey] = withSizing(overrides[setKey], axis, "fixed");
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
for (const node of input.nodes) {
|
|
590
|
+
const original = byId.get(node.id);
|
|
591
|
+
const instance = current(node.id);
|
|
592
|
+
if (original?.type !== "instance" || instance?.type !== "instance")
|
|
593
|
+
continue;
|
|
594
|
+
const overrides = { ...instance.overrides };
|
|
595
|
+
const give = (path, shown, values, before) => {
|
|
596
|
+
for (const id of Object.keys(values ?? {})) {
|
|
597
|
+
const variant = variantOf.get(originalValue(before, id));
|
|
598
|
+
if (variant === undefined)
|
|
599
|
+
continue;
|
|
600
|
+
if (shown === undefined) {
|
|
601
|
+
input.report(variant.set, "approximated", "an instance swap value that names a variant, set inside a nested instance the import cannot follow, shows the set at its default values");
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
for (const layer of boundLayers(shown.componentId, shown.values, id)) {
|
|
605
|
+
const key = layerPathKey([...path, layer]);
|
|
606
|
+
overrides[key] = { ...overrides[key], propertyValues: { ...variant.values, ...overrides[key]?.propertyValues } };
|
|
607
|
+
keepSize(overrides, [...path, layer], byId.get(layer), variant);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
give([], { componentId: instance.componentId, values: instance.propertyValues ?? {} }, instance.propertyValues, original.propertyValues);
|
|
612
|
+
for (const [key, override] of Object.entries(original.overrides ?? {})) {
|
|
613
|
+
const variant = override.componentId === undefined ? undefined : variantOf.get(override.componentId);
|
|
614
|
+
const path = variant === undefined ? null : parseLayerPath(key);
|
|
615
|
+
if (variant !== undefined && path !== null)
|
|
616
|
+
keepSize(overrides, path, byId.get(path.at(-1)), variant);
|
|
617
|
+
}
|
|
618
|
+
// Shorter paths first, so the values an override gives a nested instance pick the variant a deeper override is read in.
|
|
619
|
+
const byDepth = Object.entries(instance.overrides ?? {}).map(([key, override]) => ({ key, override, path: parseLayerPath(key) }))
|
|
620
|
+
.sort((a, b) => (a.path?.length ?? 0) - (b.path?.length ?? 0));
|
|
621
|
+
for (const { key, override, path } of byDepth) {
|
|
622
|
+
if (override.propertyValues === undefined)
|
|
623
|
+
continue;
|
|
624
|
+
give(path ?? [], path === null ? undefined : shownAt(instance.componentId, instance.propertyValues ?? {}, path, overrides), override.propertyValues, original.overrides?.[key]?.propertyValues);
|
|
625
|
+
}
|
|
626
|
+
if (Object.keys(overrides).length > 0)
|
|
627
|
+
replaced.set(node.id, { ...instance, overrides });
|
|
628
|
+
}
|
|
629
|
+
const nodes = [];
|
|
630
|
+
const setRootBefore = new Map();
|
|
631
|
+
for (const plan of merged)
|
|
632
|
+
setRootBefore.set(plan.variants[0].root.id, variantOf.get(plan.variants[0].componentId).setRoot);
|
|
633
|
+
for (const node of input.nodes) {
|
|
634
|
+
// A set's root goes just before its first variant's root, so parents stay before their children.
|
|
635
|
+
const setRoot = setRootBefore.get(node.id);
|
|
636
|
+
if (setRoot !== undefined)
|
|
637
|
+
nodes.push(replaced.get(setRoot));
|
|
638
|
+
nodes.push(current(node.id));
|
|
639
|
+
}
|
|
640
|
+
const trees = mergeVariantTrees(nodes, components, merged.map((plan) => ({
|
|
641
|
+
component: definitions.get(plan.variants[plan.defaultIndex].componentId),
|
|
642
|
+
roots: plan.variants.map((variant) => variant.root.id), values: plan.values, properties: plan.properties,
|
|
643
|
+
presence: (indices) => factored(plan.properties, indices.map((at) => plan.properties.map((property) => plan.values[at][property.id]))) || "true",
|
|
644
|
+
split: (node, count, fields) => input.report(plan.set, "approximated", `split layer ${node} into ${String(count)} groups: ${fields.join(", ")}`),
|
|
645
|
+
})), input.theme);
|
|
646
|
+
return { nodes: trees.nodes, components, componentOf, layerOf: trees.layerOf };
|
|
647
|
+
}
|