@moondraft/import 0.0.0-stage → 0.2.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 +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -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 +45 -0
- package/dist/adapter/variables.js +106 -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 +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -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 +41 -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 +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -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/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -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 +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -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 +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -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 +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -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 +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -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 +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -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 +5665 -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 +96 -0
- package/dist/taco/model/component-evaluation.js +555 -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 +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -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 +79 -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 +2369 -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 +55 -4
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
// Links imported instances to their mains (plan task 44). The converter expands every instance into concrete layers
|
|
2
|
+
// and marks each expanded instance root (instanceOf: its component's id) and each component root met on the page
|
|
3
|
+
// (mainOf). This pass, run last (after tools/host_doc/supplement.mjs, which works on the expanded layers, and tools/host_doc/settle.mjs's load in
|
|
4
|
+
// the editor), turns each component into a main (a frame whose `component` is its own id,
|
|
5
|
+
// packages/editor/src/core/components.ts) and each expanded instance that its main can show into an instance of it: a
|
|
6
|
+
// frame naming the main, with no children, whose overrides (by layer path, SPEC 19.3) hold what its expanded layers
|
|
7
|
+
// show unlike the main's. The effective records (the main's layers with those overrides applied) equal the expanded
|
|
8
|
+
// ones in every field the renderer draws, so the page looks the same; an instance whose expansion differs in a field
|
|
9
|
+
// that takes no override (a path's network change beyond layout, a stroke's position or weight, a layer the main lacks, the root's own
|
|
10
|
+
// fills) stays expanded, and so do the instances inside it, which are linked on their own when they can be. A stroke's
|
|
11
|
+
// other colour is a paint override (the renderer draws a stroke's paint in place of its compact colour).
|
|
12
|
+
// - A component whose root is on the page has it as its main. Any other gets a hidden main under '@components' (core's
|
|
13
|
+
// parent of mains that are not drawn): a copy of its largest expanded instance with every layer its other expansions
|
|
14
|
+
// have and it lacks added, hidden, so that its instances, which hide what their variants do not show and show what
|
|
15
|
+
// the largest does not, find every layer they draw in it. Layers are matched by source id, each to the first of its
|
|
16
|
+
// id after the last matched.
|
|
17
|
+
// - Mains are linked inside first (their nested instances become instances of their own mains), then the page.
|
|
18
|
+
// Override paths cross a nested instance as the renderer's virtual ids do: its id, then the layer ids in its main.
|
|
19
|
+
// - Names and the flags core's layout does not read inside an instance (remeasured, boxScale) are not compared. The
|
|
20
|
+
// layout inputs it reads (layoutData, field by field, keepSize, storedHeight, storedRow) are stored where they
|
|
21
|
+
// differ, beside the transforms and sizes the page was linked with (settle.mjs makes those the editor's own). Core
|
|
22
|
+
// lays an instance out again (packages/editor/src/core/layout/layout.ts instanceLayout) only when it or its main
|
|
23
|
+
// changes, and the transforms and sizes it writes then are its output alone; where that layout, run here, does not
|
|
24
|
+
// give a layer's linked box (within 0.01 px), the box is stored too (storedBox, with the instance's size), and core's
|
|
25
|
+
// layout starts from it until the main edits that layer's box.
|
|
26
|
+
// - An instance has no children to hug: where its expanded root is an auto layout filling a parent that hugs it, the
|
|
27
|
+
// parent hugged that root's content (layout.mjs natural), so the instance keeps its size there ('fixed') and the
|
|
28
|
+
// parent lays out as it did.
|
|
29
|
+
// - What the renderer does differently from core is avoided: it culls an instance's content by the instance's box (an
|
|
30
|
+
// instance whose layers draw outside it, a 114 px logo in a 29 px frame that does not clip or a card's shadow that
|
|
31
|
+
// reaches well past it, stays expanded); its text body field carries the text's box (see difference).
|
|
32
|
+
// The markers are removed whether or not anything is linked.
|
|
33
|
+
import { Properties, TextStyleSchema } from '@moondraft/editor/schema';
|
|
34
|
+
import { instanceLayout, legacyAuto, netBounds, sizingOf } from '@moondraft/editor/core';
|
|
35
|
+
/** The converter's markers on the expanded models (instanceOf, mainOf, and setOf: an instance's variant set name). */
|
|
36
|
+
export const MARKERS = ['instanceOf', 'mainOf', 'setOf', 'variant'];
|
|
37
|
+
/** The component of a hidden main's key (linkOnce variantKey). */
|
|
38
|
+
const componentOf = (vk) => vk.split('\u0000')[0];
|
|
39
|
+
/**
|
|
40
|
+
* Removes component linking markers from a model.
|
|
41
|
+
* @param m - Converted model to update.
|
|
42
|
+
*/
|
|
43
|
+
export const unmark = (m) => { for (const k of MARKERS)
|
|
44
|
+
delete m[k]; };
|
|
45
|
+
/** Keys that draw nothing, or name the node and its place: never compared. */
|
|
46
|
+
const IGNORED = new Set(['id', 'parent', 'children', 'z', 'order', 'sourceId', 'fileInstance', 'fileSlotHost', 'fileFills', 'name', 'component', 'overrides', 'instanceOf', 'mainOf', 'setOf', 'variant',
|
|
47
|
+
'layoutData', 'keepSize', 'remeasured', 'storedHeight', 'storedRow', 'storedBox', 'boxScale', 'locked', 'runs', 'styleAt', 'fill', 'look']);
|
|
48
|
+
/** An instance's own record (SPEC 19.2): the rest of an instance's appearance is its main root's. */
|
|
49
|
+
const OWN = new Set(['t', 'w', 'h', 'opacity', 'visible', 'blend', 'mask', 'exports']);
|
|
50
|
+
const same = (a, b, tolerance = 0) => {
|
|
51
|
+
if (a === b)
|
|
52
|
+
return true;
|
|
53
|
+
if (tolerance && typeof a === 'number' && typeof b === 'number')
|
|
54
|
+
return Math.abs(a - b) < tolerance;
|
|
55
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null || Array.isArray(a) !== Array.isArray(b))
|
|
56
|
+
return false;
|
|
57
|
+
const ka = Object.keys(a).filter((k) => a[k] !== undefined), kb = Object.keys(b).filter((k) => b[k] !== undefined);
|
|
58
|
+
return ka.length === kb.length && ka.every((k) => same(a[k], b[k], tolerance));
|
|
59
|
+
};
|
|
60
|
+
/** What a layer fills with: its look's fills, else its compact fill as one solid paint (the renderer draws both so). */
|
|
61
|
+
const fillsOf = (m) => m.look?.fills ?? (m.fill ? [{ k: 'solid', c: m.fill }] : []);
|
|
62
|
+
/** A text's style index per UTF-16 unit, from styleAt or the file's runs ([length, style]). */
|
|
63
|
+
export function styleAtOf(m) {
|
|
64
|
+
if (m.styleAt)
|
|
65
|
+
return m.styleAt;
|
|
66
|
+
const at = [];
|
|
67
|
+
let s = 0;
|
|
68
|
+
for (const [n, k] of m.runs ?? []) {
|
|
69
|
+
if (k >= 0)
|
|
70
|
+
s = k;
|
|
71
|
+
for (let i = 0; i < n; i++)
|
|
72
|
+
at.push(s);
|
|
73
|
+
}
|
|
74
|
+
return at;
|
|
75
|
+
}
|
|
76
|
+
// The editor peer must resolve a run's paint before an instance can inherit its main's colour source.
|
|
77
|
+
export const supportsRunPaint = 'fillStyle' in TextStyleSchema.properties;
|
|
78
|
+
const supportsFrameOverrides = 'layoutData.flow' in Properties;
|
|
79
|
+
const linkedTextColor = (style) => style.fileFillStyle ? supportsRunPaint : !!style.fileTextColor;
|
|
80
|
+
/** Can this occurrence inherit the main's fitted runs, including their colour sources? */
|
|
81
|
+
function inheritsTextRuns(occurrence, main, runs, mainRuns, nativeFile) {
|
|
82
|
+
const bound = occurrence.styles.some(linkedTextColor);
|
|
83
|
+
// A link alone cannot own a native instance palette. Wording with its own unbound formatting can.
|
|
84
|
+
if (!bound && !(nativeFile && occurrence.content === main.content &&
|
|
85
|
+
[...occurrence.styles, ...main.styles].some(style => style.link || style.figmaHyperlink)))
|
|
86
|
+
return false;
|
|
87
|
+
let differentLink = false;
|
|
88
|
+
const matched = new Map();
|
|
89
|
+
const last = Math.min(main.content.length, mainRuns.length) - 1;
|
|
90
|
+
// An empty text still has an insertion style in slot zero.
|
|
91
|
+
for (let i = 0; i < Math.max(1, runs.length); i++) {
|
|
92
|
+
const slot = runs[i] ?? 0, from = runs.length ? mainRuns[Math.min(i, last)] ?? 0 : 0;
|
|
93
|
+
if (matched.get(slot) === from)
|
|
94
|
+
continue;
|
|
95
|
+
const style = occurrence.styles[slot], inherited = main.styles[from];
|
|
96
|
+
// A shared variable can draw a different literal in the occurrence's mode.
|
|
97
|
+
if (!inherited)
|
|
98
|
+
return false;
|
|
99
|
+
const fitted = linkedTextColor(style) ? { ...style, color: inherited.color } : style;
|
|
100
|
+
if (!same(fitted, inherited)) {
|
|
101
|
+
if (!nativeFile || !same({ ...fitted, link: inherited.link, figmaHyperlink: inherited.figmaHyperlink }, inherited))
|
|
102
|
+
return false;
|
|
103
|
+
differentLink = true;
|
|
104
|
+
}
|
|
105
|
+
matched.set(slot, from);
|
|
106
|
+
}
|
|
107
|
+
return differentLink ? 'link-only' : bound;
|
|
108
|
+
}
|
|
109
|
+
/** One override record applied to a copy of a record, as core applies it (components.ts applyOverride: a stored box is
|
|
110
|
+
* the instance's layout's alone). */
|
|
111
|
+
function applied(record, over) {
|
|
112
|
+
const out = { ...record };
|
|
113
|
+
for (const [key, value] of Object.entries(over)) {
|
|
114
|
+
if (key === 'storedBox')
|
|
115
|
+
continue;
|
|
116
|
+
if (key === 'layoutData.flow') {
|
|
117
|
+
const flow = value, data = out.layoutData;
|
|
118
|
+
out.layoutData = { ...data, layout: { ...data?.layout, ...flow }, padding: { ...data?.layout?.padding, ...data?.padding, ...flow.padding } };
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!key.includes('.')) {
|
|
122
|
+
out[key] = value;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const path = key.split('.');
|
|
126
|
+
let at = out;
|
|
127
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
128
|
+
const next = { ...at[path[i]] };
|
|
129
|
+
at[path[i]] = next;
|
|
130
|
+
at = next;
|
|
131
|
+
}
|
|
132
|
+
at[path[path.length - 1]] = value;
|
|
133
|
+
if (key === 'look.fills')
|
|
134
|
+
delete out.fill;
|
|
135
|
+
}
|
|
136
|
+
if (over.styleAt)
|
|
137
|
+
delete out.runs;
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
/** The keys of a record that differ from another's as one override (null: none), or the key that takes no override
|
|
141
|
+
* (a string). own: whether the record is a layer's (its own keys take overrides) or a root's (they are the instance's). */
|
|
142
|
+
function difference(E, R, own, rootOverride = false, omitLink) {
|
|
143
|
+
const rec = {};
|
|
144
|
+
for (const k of new Set([...Object.keys(E), ...Object.keys(R)])) {
|
|
145
|
+
if (IGNORED.has(k) || same(E[k], R[k]))
|
|
146
|
+
continue;
|
|
147
|
+
if (OWN.has(k) && !own)
|
|
148
|
+
continue;
|
|
149
|
+
if (k === 't' || k === 'opacity' || k === 'blend' || k === 'visible') {
|
|
150
|
+
const fallback = { opacity: 1, blend: 0, visible: true }[k];
|
|
151
|
+
if (!same(E[k] ?? fallback, R[k] ?? fallback))
|
|
152
|
+
rec[k] = E[k] ?? fallback;
|
|
153
|
+
}
|
|
154
|
+
else if (k === 'w' || k === 'h') {
|
|
155
|
+
rec.w = E.w;
|
|
156
|
+
rec.h = E.h;
|
|
157
|
+
}
|
|
158
|
+
// Layout owns a stretched path's network override. keep() checks it against the
|
|
159
|
+
// main laid out with all sibling overrides and stored boxes before accepting the link.
|
|
160
|
+
else if (k === 'net' && E.kind === 'path' && own)
|
|
161
|
+
rec.net = E.net;
|
|
162
|
+
else if (k === 'clip') {
|
|
163
|
+
if (!!E.clip !== !!R.clip) {
|
|
164
|
+
if (!supportsFrameOverrides)
|
|
165
|
+
return 'clip';
|
|
166
|
+
rec.clip = !!E.clip;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else if (k === 'radius' || k === 'smoothing') {
|
|
170
|
+
if (!same(E[k] ?? 0, R[k] ?? 0)) {
|
|
171
|
+
rec[k] = E[k] ?? 0;
|
|
172
|
+
// Older peers encode both values in the corners field.
|
|
173
|
+
if (!supportsFrameOverrides)
|
|
174
|
+
Object.assign(rec, { radius: E.radius ?? 0, smoothing: E.smoothing ?? 0 });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (k === 'content' || k === 'styles' || k === 'para')
|
|
178
|
+
continue; // the text body, below
|
|
179
|
+
// An instance cannot override a text's line limit or ellipsis, so a linked instance shows its main's rather than
|
|
180
|
+
// losing its link over them.
|
|
181
|
+
else if (k === 'maxLines' || k === 'truncate')
|
|
182
|
+
continue;
|
|
183
|
+
else if ((k === 'stroke' || k === 'strokeWidth') && E.kind !== 'text')
|
|
184
|
+
continue; // the stroke, below
|
|
185
|
+
else
|
|
186
|
+
return k;
|
|
187
|
+
}
|
|
188
|
+
if (E.kind === 'text') {
|
|
189
|
+
const runs = styleAtOf(E), mainRuns = styleAtOf(R), para = E.para ?? [];
|
|
190
|
+
// The renderer's text body field carries the text's box, taken from the main's layer with this record applied
|
|
191
|
+
// (wire.ts overrideBase), and replaces an inner instance's whole body, box included: a text's size and body go
|
|
192
|
+
// together, so neither an inner size nor the main's box shows through.
|
|
193
|
+
const inheritsPalette = inheritsTextRuns(E, R, runs, mainRuns, omitLink !== undefined);
|
|
194
|
+
if (inheritsPalette === 'link-only')
|
|
195
|
+
omitLink?.();
|
|
196
|
+
if (E.content !== R.content || rec.w !== undefined || !same(para, R.para ?? [])
|
|
197
|
+
|| (!inheritsPalette && (!same(E.styles, R.styles) || !same(runs, mainRuns)))) {
|
|
198
|
+
Object.assign(rec, { content: E.content, para, w: E.w, h: E.h });
|
|
199
|
+
// A plain body owns its palette and indices together. A character-only override can inherit both.
|
|
200
|
+
if (!inheritsPalette)
|
|
201
|
+
Object.assign(rec, { styles: E.styles, styleAt: runs });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (E.kind !== 'text') {
|
|
205
|
+
if (!same(fillsOf(E), fillsOf(R))) {
|
|
206
|
+
if (!own && !rootOverride)
|
|
207
|
+
return 'fills';
|
|
208
|
+
rec['look.fills'] = fillsOf(E);
|
|
209
|
+
}
|
|
210
|
+
if (!same(E.stroke, R.stroke) && (!E.stroke || !R.stroke))
|
|
211
|
+
return 'stroke';
|
|
212
|
+
const { paint: pe, sides: es, ...se } = E.look?.stroke ?? {}, { paint: pr, sides: rs, ...sr } = R.look?.stroke ?? {};
|
|
213
|
+
if (!same(se, sr) || !!E.look?.stroke !== !!R.look?.stroke)
|
|
214
|
+
return 'look.stroke';
|
|
215
|
+
if ((es || rs) && E.stroke && R.stroke) {
|
|
216
|
+
const sides = es ?? Array(4).fill(E.strokeWidth);
|
|
217
|
+
if (!same(sides, rs ?? Array(4).fill(R.strokeWidth)))
|
|
218
|
+
rec['look.stroke.sides'] = sides;
|
|
219
|
+
}
|
|
220
|
+
else if (!same(E.strokeWidth, R.strokeWidth))
|
|
221
|
+
return 'strokeWidth';
|
|
222
|
+
// What a drawn stroke paints with: its look's paint, else its compact colour as one solid paint (the renderer
|
|
223
|
+
// draws a paint in place of the compact colour), so an expansion's other colour is a paint override.
|
|
224
|
+
const strokePaint = (m, p) => p ?? (m.stroke ? { k: 'solid', c: m.stroke } : undefined);
|
|
225
|
+
const qe = strokePaint(E, pe), qr = strokePaint(R, pr);
|
|
226
|
+
if (!same(qe, qr)) {
|
|
227
|
+
if ((!own && !rootOverride) || !qe || !E.look?.stroke)
|
|
228
|
+
return same(E.stroke, R.stroke) ? 'look.stroke.paint' : 'stroke';
|
|
229
|
+
rec['look.stroke.paint'] = qe;
|
|
230
|
+
}
|
|
231
|
+
const effects = (m) => [m.look?.shadows ?? [], m.look?.layerBlur ?? { radius: 0 }, m.look?.backgroundBlur ?? { radius: 0 }];
|
|
232
|
+
const [a, b] = [effects(E), effects(R)];
|
|
233
|
+
if (!a.every((v, i) => same(v, b[i]))) {
|
|
234
|
+
if (!own && !rootOverride)
|
|
235
|
+
return 'effects';
|
|
236
|
+
Object.assign(rec, { 'look.shadows': a[0], 'look.layerBlur': a[1], 'look.backgroundBlur': a[2] });
|
|
237
|
+
}
|
|
238
|
+
for (const k of Object.keys({ ...E.look, ...R.look }))
|
|
239
|
+
if (!['fills', 'stroke', 'shadows', 'layerBlur', 'backgroundBlur'].includes(k) && !same(E.look?.[k], R.look?.[k]))
|
|
240
|
+
return `look.${k}`;
|
|
241
|
+
}
|
|
242
|
+
if (!own && !rootOverride) {
|
|
243
|
+
const unsupported = Object.keys(rec).find(k => k !== 'clip' && k !== 'smoothing');
|
|
244
|
+
if (unsupported)
|
|
245
|
+
return unsupported;
|
|
246
|
+
}
|
|
247
|
+
return Object.keys(rec).length ? rec : null;
|
|
248
|
+
}
|
|
249
|
+
// Core lays an instance out again (a resize of it, an edit of its main, an override that hides a layer or changes a
|
|
250
|
+
// text) from its layers' layout inputs: the main's, or those its overrides hold (components.ts LAYOUT_INPUTS), so an
|
|
251
|
+
// expansion laid out from other inputs than its main's (a sizing Figma overrode, a box the converter held) stores
|
|
252
|
+
// its own. What core's layout reads besides geometry:
|
|
253
|
+
/** of a layer, its layoutData and the flags that hold an imported box (absent: 0 or false), as an override; */
|
|
254
|
+
const layerInputs = (m) => ({ layoutData: m.layoutData ?? {}, keepSize: !!m.keepSize, storedHeight: m.storedHeight ?? 0, storedRow: m.storedRow ?? 0 });
|
|
255
|
+
/** What a layoutData field reads as when absent, where an override can say so (a field without one, the auto layout or an
|
|
256
|
+
* arc's box, is absent only in a whole layoutData). */
|
|
257
|
+
const NEUTRAL = { sizing: {}, resizing: {}, constraints: {}, padding: {}, layoutPositioning: 'auto', rehug: false, aspectRatioLocked: false, instance: false, relaid: false };
|
|
258
|
+
/** The override that makes layoutData `main` read as `own`: each field that differs alone ('layoutData.sizing'), so
|
|
259
|
+
* that the main's later edits of the others (its padding and gap) still reach the layer, or the whole layoutData when a
|
|
260
|
+
* field the main has cannot be cleared alone. Empty when none differs. */
|
|
261
|
+
function dataOverride(own = {}, main = {}) {
|
|
262
|
+
const out = {};
|
|
263
|
+
for (const k of new Set([...Object.keys(own), ...Object.keys(main)])) {
|
|
264
|
+
if (same(own[k], main[k]))
|
|
265
|
+
continue;
|
|
266
|
+
if (own[k] === undefined && !(k in NEUTRAL))
|
|
267
|
+
return { layoutData: own };
|
|
268
|
+
out[`layoutData.${k}`] = own[k] ?? NEUTRAL[k];
|
|
269
|
+
}
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
272
|
+
/** A layoutData with an override record's layoutData fields applied (as core's applyOverride does). */
|
|
273
|
+
function withData(data, over) {
|
|
274
|
+
if (!over)
|
|
275
|
+
return data;
|
|
276
|
+
let out = over.layoutData ?? data;
|
|
277
|
+
for (const [k, v] of Object.entries(over))
|
|
278
|
+
if (k.startsWith('layoutData.'))
|
|
279
|
+
out = { ...out, [k.slice(11)]: v };
|
|
280
|
+
return out;
|
|
281
|
+
}
|
|
282
|
+
/** The content settings this frame changes relative to its component, including individual padding sides. */
|
|
283
|
+
function flowOverride(own = {}, main = {}) {
|
|
284
|
+
const a = own.layout ?? {}, b = main.layout ?? {};
|
|
285
|
+
const flow = {};
|
|
286
|
+
const defaults = { kind: 'absolute', gap: 0, align: 'start', justify: 'start', wrap: false,
|
|
287
|
+
wrapGap: a.gap ?? 0, alignContent: 'auto', widthSizing: 'fixed', heightSizing: 'fixed', strokesIncluded: false };
|
|
288
|
+
for (const [key, fallback] of Object.entries(defaults)) {
|
|
289
|
+
if (!same(a[key] ?? fallback, b[key] ?? fallback))
|
|
290
|
+
flow[key] = a[key] ?? fallback;
|
|
291
|
+
}
|
|
292
|
+
// Hug/Fixed can be carried in sizing even when the stack itself is identical.
|
|
293
|
+
if (a.kind && a.kind !== 'absolute')
|
|
294
|
+
for (const axis of ['width', 'height']) {
|
|
295
|
+
const local = sizingOf(own, axis), inherited = sizingOf(main, axis);
|
|
296
|
+
if (local !== inherited)
|
|
297
|
+
flow[axis + 'Sizing'] = local;
|
|
298
|
+
}
|
|
299
|
+
const pad = { ...a.padding, ...own.padding }, base = { ...b.padding, ...main.padding };
|
|
300
|
+
const sides = Object.fromEntries(['top', 'right', 'bottom', 'left'].filter(k => (pad[k] ?? 0) !== (base[k] ?? 0)).map(k => [k, pad[k] ?? 0]));
|
|
301
|
+
if (Object.keys(sides).length)
|
|
302
|
+
flow.padding = sides;
|
|
303
|
+
return Object.keys(flow).length ? { 'layoutData.flow': flow } : {};
|
|
304
|
+
}
|
|
305
|
+
/** of an instance in its parent (P), its own layoutData as linking gives it: no auto layout (its main's), and a size
|
|
306
|
+
* that hugs held ('fixed': it has no children to hug), so a parent that hugs it lays out as it did. */
|
|
307
|
+
function instanceData(E, P) {
|
|
308
|
+
const { layout: _layout, padding: _padding, box: _box, ...data } = E.layoutData ?? {};
|
|
309
|
+
const fixed = (o) => o && Object.fromEntries(Object.entries(o).map(([k, v]) => [k, v === 'hug' ? 'fixed' : v]));
|
|
310
|
+
// The axes it hugged are recorded (heldHug), so that a swap in the editor hugs them again.
|
|
311
|
+
const held = ['width', 'height'].filter((a) => data.sizing?.[a] === 'hug' || data.resizing?.[a] === 'hug');
|
|
312
|
+
if (held.length)
|
|
313
|
+
data.heldHug = held;
|
|
314
|
+
if (data.sizing)
|
|
315
|
+
data.sizing = fixed(data.sizing);
|
|
316
|
+
if (data.resizing)
|
|
317
|
+
data.resizing = fixed(data.resizing);
|
|
318
|
+
const auto = ['horizontal', 'vertical'].includes(E.layoutData?.layout?.kind);
|
|
319
|
+
for (const axis of ['width', 'height'])
|
|
320
|
+
if (auto && sizingOf(E.layoutData, axis) === 'fill' && sizingOf(P?.layoutData, axis) === 'hug')
|
|
321
|
+
data.sizing = { ...data.sizing, [axis]: 'fixed' };
|
|
322
|
+
return data;
|
|
323
|
+
}
|
|
324
|
+
/** How far core's layout of a linked instance may place a layer from its expanded box (px; 1e-4 of the linear part)
|
|
325
|
+
* before the box is stored. */
|
|
326
|
+
const EPSILON = 0.01;
|
|
327
|
+
/** How far (px) a layer's shadow or blur may reach past its box and still count as inside it (overflows). The renderer
|
|
328
|
+
* bounds an instance by its own box and its root's effects, not its content's: a card's 56 px shadow tail inside a
|
|
329
|
+
* Modal instance was cut 22 px below it, while the HeroUI pages' tails of up to 8 px drew as expanded. */
|
|
330
|
+
const SLACK = 8;
|
|
331
|
+
/** Whether core's layout of an instance (laid: its output for the layer, absent where it shows its main's R) gives the
|
|
332
|
+
* expanded record E's transform and size. */
|
|
333
|
+
function gives(laid, { E, R }) {
|
|
334
|
+
const t = laid?.t ?? R.t ?? [1, 0, 0, 1, 0, 0], e = E.t ?? [1, 0, 0, 1, 0, 0];
|
|
335
|
+
if (t.some((v, i) => Math.abs(v - e[i]) > (i < 4 ? 1e-4 : EPSILON)))
|
|
336
|
+
return false;
|
|
337
|
+
const size = (k) => (laid && k in laid ? laid[k] : R[k]);
|
|
338
|
+
return ['w', 'h'].every((k) => E[k] === size(k) || (E[k] !== undefined && size(k) !== undefined && Math.abs(E[k] - size(k)) <= EPSILON));
|
|
339
|
+
}
|
|
340
|
+
/** Links the instances of a converted page (its models, in file order) to their mains, in place. Returns counts.
|
|
341
|
+
* A hidden main only one instance would use (not a main on the page) costs more than that instance's expansion, so
|
|
342
|
+
* the page is linked again without it, that instance left expanded, until no hidden main is used once (an instance
|
|
343
|
+
* left expanded can make another's use count change).
|
|
344
|
+
* @param doc - Converted models in file order.
|
|
345
|
+
* @returns Counts of links, mains and instances left expanded.
|
|
346
|
+
*/
|
|
347
|
+
export function linkComponents(doc) {
|
|
348
|
+
return linkWithLayers(doc, false).counts;
|
|
349
|
+
}
|
|
350
|
+
/** @internal Links components and tracks where their expanded layers went for import reports. */
|
|
351
|
+
export function linkWithLayers(doc, trackLayers = true, swapBinding, nativeFile = false) {
|
|
352
|
+
// Variant sets (convert.ts markSet): one hidden main per variant used, so each instance's main is its own variant
|
|
353
|
+
// and its overrides are its own changes alone; but a showcase of a set (each variant once or twice) costs a main per
|
|
354
|
+
// variant. So per set: the page is linked both ways and a set keeps a main per variant where that stores no more
|
|
355
|
+
// than one main for the set, a model (a layer) weighing as three override keys; otherwise its one main is named for
|
|
356
|
+
// the set alone ("Button", no variant: the editor shows no variant it is not). Over the 84 exports a weight of 1
|
|
357
|
+
// grew the models 13%, 3 grows them 4% (39583 to 41158) and still cuts the override keys by 23%; 6 kept the models
|
|
358
|
+
// level but cut the keys by 19% only. The used-once rule stays per component: a variant's main used once is kept
|
|
359
|
+
// when its set is used more than once, so every variant of a set can be swapped to.
|
|
360
|
+
const sets = new Set(), placed = new Set();
|
|
361
|
+
for (const model of doc.models) {
|
|
362
|
+
if (model.setOf && model.variant)
|
|
363
|
+
sets.add(model.instanceOf);
|
|
364
|
+
if (model.mainOf !== undefined && model.kind === 'frame')
|
|
365
|
+
placed.add(model.mainOf);
|
|
366
|
+
}
|
|
367
|
+
const run = (each, compareCosts, report = false) => {
|
|
368
|
+
const skip = new Set();
|
|
369
|
+
for (;;) {
|
|
370
|
+
const d = { models: doc.models }, aliases = new Map();
|
|
371
|
+
const { once, omittedLinks, ...r } = linkOnce(d, skip, each, compareCosts, report ? aliases : undefined, swapBinding, nativeFile);
|
|
372
|
+
if (!once.length)
|
|
373
|
+
return { d, r, aliases, omittedLinks };
|
|
374
|
+
for (const comp of once)
|
|
375
|
+
skip.add(comp);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
let chosen = new Set();
|
|
379
|
+
// A placed main wins in both trials. With all sets placed the costs tie.
|
|
380
|
+
if ([...sets].every(set => placed.has(set)))
|
|
381
|
+
chosen = sets;
|
|
382
|
+
else {
|
|
383
|
+
const shared = run(new Set(), true).r.cost, split = run(sets, true).r.cost;
|
|
384
|
+
const W = 3, of = (m, c) => { const [n, k] = m.get(c) ?? [0, 0]; return n * W + k; };
|
|
385
|
+
chosen = new Set([...sets].filter((c) => of(split, c) <= of(shared, c)));
|
|
386
|
+
}
|
|
387
|
+
const { d, r: { cost: _cost, ...r }, aliases, omittedLinks } = run(chosen, false, trackLayers);
|
|
388
|
+
// Detach only the linked output. Trial passes and discarded expansions never need deep copies.
|
|
389
|
+
doc.models = nativeFile ? d.models : structuredClone(d.models);
|
|
390
|
+
const counts = { ...r, variantSets: chosen.size, sharedSets: sets.size - chosen.size };
|
|
391
|
+
return { counts, aliases, omittedLinks };
|
|
392
|
+
}
|
|
393
|
+
/** linkComponents with no hidden main for the components in `skip`; also returns the components whose hidden main is
|
|
394
|
+
* used once (once). */
|
|
395
|
+
function linkOnce(doc, skip, each, compareCosts, aliases, swapBinding, nativeFile = false) {
|
|
396
|
+
const omittedLinks = new Map();
|
|
397
|
+
const recordAlias = (from, to) => {
|
|
398
|
+
if (!aliases)
|
|
399
|
+
return;
|
|
400
|
+
let ids = aliases.get(from);
|
|
401
|
+
if (!ids)
|
|
402
|
+
aliases.set(from, ids = []);
|
|
403
|
+
ids.push(to);
|
|
404
|
+
};
|
|
405
|
+
// The hidden main an expanded instance takes: its component's, or its variant's for a set in `each`.
|
|
406
|
+
const variantKey = (m) => (each.has(m.instanceOf) && m.setOf && m.variant ? `${m.instanceOf}\u0000${m.variant}` : m.instanceOf);
|
|
407
|
+
// Each trial replaces records in its own index. Input records and their nested values stay read-only.
|
|
408
|
+
const byId = new Map(), kids = new Map();
|
|
409
|
+
const mains = new Map(), instances = new Map();
|
|
410
|
+
for (const m of doc.models) {
|
|
411
|
+
byId.set(m.id, m);
|
|
412
|
+
const parent = m.parent ?? '';
|
|
413
|
+
if (!kids.has(parent))
|
|
414
|
+
kids.set(parent, []);
|
|
415
|
+
kids.get(parent).push(m.id);
|
|
416
|
+
if (m.mainOf !== undefined && !mains.has(m.mainOf) && m.kind === 'frame')
|
|
417
|
+
mains.set(m.mainOf, m.id);
|
|
418
|
+
if (m.instanceOf !== undefined) {
|
|
419
|
+
const k = variantKey(m);
|
|
420
|
+
if (!instances.has(k))
|
|
421
|
+
instances.set(k, []);
|
|
422
|
+
instances.get(k).push(m.id);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const kidsOf = (id) => kids.get(id) ?? [];
|
|
426
|
+
const size = (id) => 1 + kidsOf(id).reduce((n, c) => n + size(c), 0);
|
|
427
|
+
const before = doc.models.length;
|
|
428
|
+
// Whether the layers under a frame draw outside its box, with their drop shadows and layer blurs where they reach
|
|
429
|
+
// further than SLACK, as the renderer reaches them (the offset plus 1.5 blur and the spread; crates/scn-core
|
|
430
|
+
// derived.rs effect_reach, with the spread shadow_spread draws: below 0 only on a frame with square corners);
|
|
431
|
+
// clipping frames bound their own children.
|
|
432
|
+
const square = (m) => m.kind === 'frame' && (Array.isArray(m.radius) ? m.radius.every((r) => !r) : !m.radius);
|
|
433
|
+
const reach = (m) => {
|
|
434
|
+
const blur = 1.5 * (m.look?.layerBlur?.radius ?? 0), out = [blur, blur, blur, blur]; // left, top, right, bottom
|
|
435
|
+
for (const s of m.look?.shadows ?? [])
|
|
436
|
+
if (!s.inner && s.c?.[3] !== 0) {
|
|
437
|
+
const spread = square(m) ? s.spread ?? 0 : Math.max(0, s.spread ?? 0), x = s.x ?? 0, y = s.y ?? 0;
|
|
438
|
+
const e = 1.5 * (s.blur ?? 0) + spread;
|
|
439
|
+
out[0] = Math.max(out[0], e - x);
|
|
440
|
+
out[1] = Math.max(out[1], e - y);
|
|
441
|
+
out[2] = Math.max(out[2], e + x);
|
|
442
|
+
out[3] = Math.max(out[3], e + y);
|
|
443
|
+
}
|
|
444
|
+
return out.map((v) => (v > SLACK ? v : 0));
|
|
445
|
+
};
|
|
446
|
+
const mul = (A, B) => [A[0] * B[0] + A[2] * B[1], A[1] * B[0] + A[3] * B[1], A[0] * B[2] + A[2] * B[3], A[1] * B[2] + A[3] * B[3], A[0] * B[4] + A[2] * B[5] + A[4], A[1] * B[4] + A[3] * B[5] + A[5]];
|
|
447
|
+
function overflows(root) {
|
|
448
|
+
const R = byId.get(root), stack = kidsOf(root).map((c) => [c, byId.get(c).t]);
|
|
449
|
+
while (stack.length) {
|
|
450
|
+
const [id, T] = stack.pop(), m = byId.get(id);
|
|
451
|
+
if (m.visible === false)
|
|
452
|
+
continue;
|
|
453
|
+
const b = m.kind === 'path' ? netBounds(m.net) : { x: 0, y: 0, width: m.w ?? 0, height: m.h ?? 0 }, [l, t, r, u] = reach(m);
|
|
454
|
+
for (const [x, y] of [[b.x - l, b.y - t], [b.x + b.width + r, b.y - t], [b.x - l, b.y + b.height + u], [b.x + b.width + r, b.y + b.height + u]]) {
|
|
455
|
+
const X = T[0] * x + T[2] * y + T[4], Y = T[1] * x + T[3] * y + T[5];
|
|
456
|
+
if (X < -0.5 || Y < -0.5 || X > R.w + 0.5 || Y > R.h + 0.5)
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
if (!m.clip)
|
|
460
|
+
for (const c of kidsOf(id))
|
|
461
|
+
stack.push([c, mul(T, byId.get(c).t)]);
|
|
462
|
+
}
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
const key = (m) => m.fileInstance ?? m.sourceId ?? `~${m.kind}~${m.name ?? ''}`;
|
|
466
|
+
// Hidden mains (taken before anything is linked): a copy of each component's largest expansion, with every layer
|
|
467
|
+
// another expansion has and it lacks, hidden (see union).
|
|
468
|
+
let serial = 0;
|
|
469
|
+
const fresh = () => { let id; do
|
|
470
|
+
id = `c${(serial++).toString(36)}`;
|
|
471
|
+
while (byId.has(id)); return id; };
|
|
472
|
+
const copy = (id, parent) => {
|
|
473
|
+
const m = { ...byId.get(id), id: fresh(), parent };
|
|
474
|
+
recordAlias(id, m.id);
|
|
475
|
+
byId.set(m.id, m);
|
|
476
|
+
kids.set(m.id, kidsOf(id).map((c) => copy(c, m.id).id));
|
|
477
|
+
return m;
|
|
478
|
+
};
|
|
479
|
+
// Adds to main layer `u` the layers under expansion layer `e` that it lacks: the most of e's layers kept in order (by
|
|
480
|
+
// key) pair with the main's, and a copy of each other one goes in, hidden (the main shows what its largest expansion
|
|
481
|
+
// does, and an instance that shows the layer overrides its visibility), after the layers it follows there; and so on
|
|
482
|
+
// down the layers match pairs (by key, each with the first of its key after the last paired). A copy that would give
|
|
483
|
+
// the main more layers of a key than the expansion has (one it has, in another order) is not added: plan gives null
|
|
484
|
+
// and the expansion adds nothing (it stays expanded), so the main holds no layer twice.
|
|
485
|
+
function plan(e, u) {
|
|
486
|
+
const ek = kidsOf(e), uk = kidsOf(u), K = (x) => key(byId.get(x)), n = ek.length, m = uk.length;
|
|
487
|
+
const list = [], added = new Map();
|
|
488
|
+
let i = 0, j = 0;
|
|
489
|
+
while (i < n && j < m && K(ek[i]) === K(uk[j])) {
|
|
490
|
+
list.push(uk[j++]);
|
|
491
|
+
i++;
|
|
492
|
+
}
|
|
493
|
+
// The common prefix is already paired. Only allocate an LCS table for an ambiguous suffix.
|
|
494
|
+
const start = i, L = [];
|
|
495
|
+
if (i < n && j < m) {
|
|
496
|
+
for (let x = i; x <= n; x++)
|
|
497
|
+
L[x - start] = new Int32Array(m - start + 1);
|
|
498
|
+
for (let x = n - 1; x >= i; x--)
|
|
499
|
+
for (let y = m - 1; y >= j; y--) {
|
|
500
|
+
const row = L[x - start], below = L[x - start + 1], col = y - start;
|
|
501
|
+
row[col] = K(ek[x]) === K(uk[y]) ? below[col + 1] + 1 : Math.max(below[col], row[col + 1]);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
while (i < n) {
|
|
505
|
+
if (j < m && K(ek[i]) === K(uk[j])) {
|
|
506
|
+
list.push(uk[j++]);
|
|
507
|
+
i++;
|
|
508
|
+
}
|
|
509
|
+
else if (j >= m || L[i - start + 1][j - start] >= L[i - start][j - start + 1]) {
|
|
510
|
+
const k = K(ek[i]);
|
|
511
|
+
added.set(k, (added.get(k) ?? 0) + 1);
|
|
512
|
+
list.push({ copy: ek[i++] });
|
|
513
|
+
}
|
|
514
|
+
else
|
|
515
|
+
list.push(uk[j++]);
|
|
516
|
+
}
|
|
517
|
+
while (j < m)
|
|
518
|
+
list.push(uk[j++]);
|
|
519
|
+
if (added.size) {
|
|
520
|
+
const remaining = new Map();
|
|
521
|
+
for (const id of ek) {
|
|
522
|
+
const k = K(id);
|
|
523
|
+
remaining.set(k, (remaining.get(k) ?? 0) + 1);
|
|
524
|
+
}
|
|
525
|
+
for (const id of uk) {
|
|
526
|
+
const k = K(id);
|
|
527
|
+
remaining.set(k, (remaining.get(k) ?? 0) - 1);
|
|
528
|
+
}
|
|
529
|
+
for (const [k, count] of added)
|
|
530
|
+
if (count > remaining.get(k))
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
const subs = [];
|
|
534
|
+
let last = -1;
|
|
535
|
+
for (const c of ek) {
|
|
536
|
+
const k = K(c);
|
|
537
|
+
let x;
|
|
538
|
+
do {
|
|
539
|
+
x = list[++last];
|
|
540
|
+
} while (K(typeof x === 'string' ? x : x.copy) !== k);
|
|
541
|
+
if (typeof x !== 'string')
|
|
542
|
+
continue;
|
|
543
|
+
const sub = plan(c, x);
|
|
544
|
+
if (!sub)
|
|
545
|
+
return null;
|
|
546
|
+
subs.push([x, sub]);
|
|
547
|
+
}
|
|
548
|
+
return { list, subs };
|
|
549
|
+
}
|
|
550
|
+
function union(u, p) {
|
|
551
|
+
kids.set(u, p.list.map((x) => { if (typeof x === 'string')
|
|
552
|
+
return x; const m = copy(x.copy, u); m.visible = false; return m.id; }));
|
|
553
|
+
for (const [x, sub] of p.subs)
|
|
554
|
+
union(x, sub);
|
|
555
|
+
}
|
|
556
|
+
const hidden = new Map(); // hidden main: its variant key (variantKey)
|
|
557
|
+
for (const [vk, list] of instances) {
|
|
558
|
+
const comp = componentOf(vk);
|
|
559
|
+
if (mains.has(comp) || skip.has(vk))
|
|
560
|
+
continue;
|
|
561
|
+
// Largest first (the first of the largest in file order).
|
|
562
|
+
const frames = list.filter((id) => byId.get(id).kind === 'frame').map((id) => [id, size(id)]).sort((a, b) => b[1] - a[1]).map(([id]) => id);
|
|
563
|
+
if (!frames.length)
|
|
564
|
+
continue;
|
|
565
|
+
const root = copy(frames[0], '@components');
|
|
566
|
+
delete root.exports; // the copied root held an instance's own settings
|
|
567
|
+
if (!kids.has('@components'))
|
|
568
|
+
kids.set('@components', []);
|
|
569
|
+
kids.get('@components').push(root.id);
|
|
570
|
+
// A variant's hidden main is named for its set and variant (the set is not on the page to hold it): "Button/variant=…".
|
|
571
|
+
const E0 = byId.get(frames[0]);
|
|
572
|
+
if (E0.setOf && E0.variant)
|
|
573
|
+
root.name = vk === comp ? E0.setOf : `${E0.setOf}/${E0.variant}`;
|
|
574
|
+
unmark(root);
|
|
575
|
+
root.mainOf = comp;
|
|
576
|
+
// An expansion that stays expanded whatever its main (its layers draw outside it, or in another order) adds nothing.
|
|
577
|
+
for (const id of frames.slice(1))
|
|
578
|
+
if (byId.get(id).clip || !overflows(id)) {
|
|
579
|
+
const p = plan(id, root.id);
|
|
580
|
+
if (p)
|
|
581
|
+
union(root.id, p);
|
|
582
|
+
}
|
|
583
|
+
mains.set(vk, root.id);
|
|
584
|
+
hidden.set(root.id, vk);
|
|
585
|
+
}
|
|
586
|
+
const isInstance = (m) => m?.kind === 'frame' && m.component !== undefined && m.component !== m.id;
|
|
587
|
+
// A main's effective tree: per layer, its path (from the main's child down), what it shows and its children.
|
|
588
|
+
const trees = new Map();
|
|
589
|
+
function swappedNode(n, main) {
|
|
590
|
+
ensure(main);
|
|
591
|
+
return { ...n, main, rec: n.recordOf(main), kids: n.childrenOf(main) };
|
|
592
|
+
}
|
|
593
|
+
function reroot(n, prefix, overrides) {
|
|
594
|
+
const over = overrides?.[n.path];
|
|
595
|
+
if (n.instance && over?.component && over.component !== n.main)
|
|
596
|
+
n = swappedNode(n, over.component);
|
|
597
|
+
// Keep each holder's overrides in scope when a later placement chooses another main.
|
|
598
|
+
const underHolder = (child) => reroot(child, prefix, overrides);
|
|
599
|
+
return { ...n, path: `${prefix};${n.path}`, rec: over ? applied(n.rec, over) : n.rec,
|
|
600
|
+
...(n.instance && over && { recordOf: (main) => applied(n.recordOf(main), over) }),
|
|
601
|
+
...(n.instance && { childrenOf: (main) => n.childrenOf(main).map(underHolder) }),
|
|
602
|
+
kids: n.kids.map(underHolder), data: withData(n.data, over) };
|
|
603
|
+
}
|
|
604
|
+
function tree(main) {
|
|
605
|
+
if (trees.has(main))
|
|
606
|
+
return trees.get(main);
|
|
607
|
+
const build = (id, path, inDefinition = true) => {
|
|
608
|
+
const m = byId.get(id);
|
|
609
|
+
if (!isInstance(m))
|
|
610
|
+
return { path, rec: m, kids: kidsOf(id).map((c) => build(c, `${path};${c}`, inDefinition && !m.fileInstance)) };
|
|
611
|
+
const own = Object.fromEntries(Object.entries(m).filter(([key]) => OWN.has(key) || key === 'fileInstance'));
|
|
612
|
+
const recordOf = (main) => {
|
|
613
|
+
const root = Object.fromEntries(Object.entries(byId.get(main)).filter(([key]) => !OWN.has(key)));
|
|
614
|
+
const rec = { ...root, ...own };
|
|
615
|
+
return m.overrides?.[''] ? applied(rec, m.overrides['']) : rec;
|
|
616
|
+
};
|
|
617
|
+
const childrenOf = (main) => tree(main).map((child) => reroot(child, path, m.overrides));
|
|
618
|
+
const binding = inDefinition && m.fileInstance && swapBinding?.(m.fileInstance);
|
|
619
|
+
return { path, binding, rec: recordOf(m.component), main: m.component, recordOf, instance: true, data: m.layoutData ?? {}, childrenOf, kids: childrenOf(m.component) };
|
|
620
|
+
};
|
|
621
|
+
const t = kidsOf(main).map((c) => build(c, c));
|
|
622
|
+
trees.set(main, t);
|
|
623
|
+
return t;
|
|
624
|
+
}
|
|
625
|
+
// A group's fill allocation can resize a descendant path through scale constraints.
|
|
626
|
+
function fillsPath(path) {
|
|
627
|
+
for (let m = path; m; m = byId.get(m.parent)) {
|
|
628
|
+
if ((m === path || m.group) && (sizingOf(m.layoutData, 'width') === 'fill' || sizingOf(m.layoutData, 'height') === 'fill'))
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
// Fills `out` with the overrides that make main-tree node list `mk` show concrete children `ek`, inside an instance
|
|
634
|
+
// expanded at `size`, and `geometry` with each matched layer's expanded record, its main's and the box to store if
|
|
635
|
+
// core's layout does not give the expanded one; a reason if none can.
|
|
636
|
+
function match(ek, mk, out, size, geometry, linkPaths, fills, bound = new Map()) {
|
|
637
|
+
const used = new Set();
|
|
638
|
+
let next = 0;
|
|
639
|
+
for (const id of ek) {
|
|
640
|
+
const E = byId.get(id), k = key(E);
|
|
641
|
+
while (next < mk.length && key(mk[next].rec) !== k)
|
|
642
|
+
next++;
|
|
643
|
+
if (next === mk.length)
|
|
644
|
+
return mk.some((n) => key(n.rec) === k) ? 'layer order' : 'a layer the main lacks';
|
|
645
|
+
used.add(next);
|
|
646
|
+
const n = mk[next++];
|
|
647
|
+
let R = n.rec, children = n.kids;
|
|
648
|
+
const swap = E.fileInstance && n.instance && mains.get(E.instanceOf);
|
|
649
|
+
if (n.binding && swap) {
|
|
650
|
+
// A property on one occurrence selects the same main for every layer bound to it.
|
|
651
|
+
if (bound.has(n.binding) && bound.get(n.binding) !== swap)
|
|
652
|
+
return 'conflicting bound swaps';
|
|
653
|
+
bound.set(n.binding, swap);
|
|
654
|
+
}
|
|
655
|
+
if (swap && swap !== n.main) {
|
|
656
|
+
const swapped = swappedNode(n, swap);
|
|
657
|
+
R = swapped.rec;
|
|
658
|
+
children = swapped.kids;
|
|
659
|
+
out[n.path] = { component: swap };
|
|
660
|
+
}
|
|
661
|
+
if (E.kind !== R.kind)
|
|
662
|
+
return 'kind';
|
|
663
|
+
// A nested instance's appearance is its main's (the renderer draws its template root): only its own keys differ.
|
|
664
|
+
const d = difference(E, R, true, false, linkPaths && (() => linkPaths.add(n.path)));
|
|
665
|
+
if (typeof d === 'string')
|
|
666
|
+
return d;
|
|
667
|
+
const fillPath = E.kind === 'path' && fillsPath(E);
|
|
668
|
+
if (d?.net && !fillPath)
|
|
669
|
+
return 'net';
|
|
670
|
+
if (n.instance && !E.fileInstance) {
|
|
671
|
+
const own = difference(E, R, false);
|
|
672
|
+
if (typeof own === 'string')
|
|
673
|
+
return own;
|
|
674
|
+
}
|
|
675
|
+
const inputs = n.instance ? flowOverride(E.layoutData, R.layoutData) : {};
|
|
676
|
+
if (!supportsFrameOverrides && inputs['layoutData.flow'])
|
|
677
|
+
return 'layout';
|
|
678
|
+
if (n.instance)
|
|
679
|
+
Object.assign(inputs, dataOverride(instanceData(E, byId.get(E.parent)), n.data));
|
|
680
|
+
else {
|
|
681
|
+
const a = layerInputs(E), b = layerInputs(R);
|
|
682
|
+
// Resizing a parametric path scales its box and network together.
|
|
683
|
+
// Use the main's box as the input to the same resize during layout.
|
|
684
|
+
if (fillPath && E.shape && a.layoutData.box && b.layoutData.box)
|
|
685
|
+
a.layoutData = { ...a.layoutData, box: b.layoutData.box };
|
|
686
|
+
if (E.kind === 'frame' && supportsFrameOverrides) {
|
|
687
|
+
const { layout: _a, padding: _ap, ...own } = a.layoutData;
|
|
688
|
+
const { layout: _b, padding: _bp, ...base } = b.layoutData;
|
|
689
|
+
Object.assign(inputs, dataOverride(own, base), flowOverride(a.layoutData, b.layoutData));
|
|
690
|
+
}
|
|
691
|
+
else
|
|
692
|
+
Object.assign(inputs, dataOverride(a.layoutData, b.layoutData));
|
|
693
|
+
if (a.keepSize !== b.keepSize)
|
|
694
|
+
inputs.keepSize = a.keepSize;
|
|
695
|
+
if (a.storedHeight !== b.storedHeight || a.storedRow !== b.storedRow) {
|
|
696
|
+
Object.assign(inputs, { storedHeight: a.storedHeight, storedRow: a.storedRow });
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
// The box the expansion has where it differs from the main's, kept (keep) where core's layout of the instance does
|
|
700
|
+
// not give it.
|
|
701
|
+
geometry.set(n.path, { E, R, fillPath, box: { ...(d && 't' in d && { t: d.t }), ...(d && 'w' in d && { w: d.w, h: d.h }), at: size } });
|
|
702
|
+
if (d || Object.keys(inputs).length)
|
|
703
|
+
out[n.path] = { ...out[n.path], ...d, ...inputs };
|
|
704
|
+
if (E.fileSlotHost && fills?.includes(E.fileSlotHost))
|
|
705
|
+
continue;
|
|
706
|
+
const r = match(kidsOf(id), children, out, size, geometry, linkPaths, fills, n.instance ? undefined : bound);
|
|
707
|
+
if (r)
|
|
708
|
+
return r;
|
|
709
|
+
}
|
|
710
|
+
mk.forEach((n, i) => { if (!used.has(i) && n.rec.visible !== false)
|
|
711
|
+
out[n.path] = { ...out[n.path], visible: false }; });
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
// Core's view of a record (its load's defaults, its children), and of each main (its component its own id).
|
|
715
|
+
const views = new Map(), asMain = new Set(mains.values());
|
|
716
|
+
const view = (x) => {
|
|
717
|
+
if (x === undefined || !byId.has(x))
|
|
718
|
+
return undefined;
|
|
719
|
+
let v = views.get(x);
|
|
720
|
+
if (!v) {
|
|
721
|
+
v = { parent: '', t: [1, 0, 0, 1, 0, 0], opacity: 1, ...legacyAuto(byId.get(x)), children: kidsOf(x) };
|
|
722
|
+
if (asMain.has(x))
|
|
723
|
+
v.component = x;
|
|
724
|
+
views.set(x, v);
|
|
725
|
+
}
|
|
726
|
+
return v;
|
|
727
|
+
};
|
|
728
|
+
// Stores the boxes (storedBox) of instance I's layers that core's layout of it (layout.ts instanceLayout: from the
|
|
729
|
+
// main at its size, with its other overrides, resized to the instance's) does not give, within EPSILON. A box only
|
|
730
|
+
// constraints or auto layout give is not stored, so a later edit of the main reaches it. A stored box changes how
|
|
731
|
+
// the others lay out (the instance then starts at the imported size), so they are checked again with it until none
|
|
732
|
+
// is added.
|
|
733
|
+
function keep(I, geometry) {
|
|
734
|
+
const stored = new Set(), get = (x) => (x === I.id ? { ...I, children: [] } : view(x));
|
|
735
|
+
for (let round = 0; round <= geometry.size; round++) {
|
|
736
|
+
const laid = instanceLayout(get, I.id);
|
|
737
|
+
let added = false;
|
|
738
|
+
for (const [path, g] of geometry)
|
|
739
|
+
if (!stored.has(path) && !gives(laid.get(path), g)) {
|
|
740
|
+
stored.add(path);
|
|
741
|
+
I.overrides[path] = { ...I.overrides[path], storedBox: g.box };
|
|
742
|
+
added = true;
|
|
743
|
+
}
|
|
744
|
+
if (!added) {
|
|
745
|
+
for (const [path, { E, R, fillPath }] of geometry) {
|
|
746
|
+
// The direct resize and a resize through the main may differ by roundoff.
|
|
747
|
+
if (fillPath && !same(E.net, laid.get(path)?.net ?? R.net, 1e-6))
|
|
748
|
+
return 'net';
|
|
749
|
+
}
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
const state = new Map(), reasons = new Map();
|
|
755
|
+
let linked = 0;
|
|
756
|
+
const ensure = (main) => { if (state.has(main))
|
|
757
|
+
return; state.set(main, 'busy'); for (const c of [...kidsOf(main)])
|
|
758
|
+
walk(c); state.set(main, 'done'); };
|
|
759
|
+
function tryLink(id) {
|
|
760
|
+
const E = byId.get(id), main = mains.get(E.instanceOf) ?? mains.get(variantKey(E));
|
|
761
|
+
if (main === undefined)
|
|
762
|
+
return skip.has(variantKey(E)) ? 'used once' : 'no main';
|
|
763
|
+
if (E.kind !== 'frame')
|
|
764
|
+
return 'not a frame';
|
|
765
|
+
if (!E.clip && overflows(id))
|
|
766
|
+
return 'content outside the instance';
|
|
767
|
+
if (state.get(main) === 'busy')
|
|
768
|
+
return 'inside its main';
|
|
769
|
+
ensure(main);
|
|
770
|
+
const M = byId.get(main);
|
|
771
|
+
const root = difference(E, M, false, !!E.fileInstance);
|
|
772
|
+
if (typeof root === 'string')
|
|
773
|
+
return `root ${root}`;
|
|
774
|
+
const overrides = {}, geometry = new Map();
|
|
775
|
+
const content = flowOverride(E.layoutData, M.layoutData);
|
|
776
|
+
if (!supportsFrameOverrides && content['layoutData.flow'])
|
|
777
|
+
return 'root layout';
|
|
778
|
+
if (root || Object.keys(content).length)
|
|
779
|
+
overrides[''] = { ...root, ...content };
|
|
780
|
+
const linkPaths = nativeFile ? new Set() : undefined;
|
|
781
|
+
const r = match(kidsOf(id), tree(main), overrides, { width: E.w, height: E.h }, geometry, linkPaths, E.fileFills);
|
|
782
|
+
if (r)
|
|
783
|
+
return r;
|
|
784
|
+
const data = instanceData(E, byId.get(E.parent));
|
|
785
|
+
const flow = content['layoutData.flow'];
|
|
786
|
+
for (const axis of ['width', 'height'])
|
|
787
|
+
if (flow?.[axis + 'Sizing'] && flow[axis + 'Sizing'] !== 'fill') {
|
|
788
|
+
if (data.sizing) {
|
|
789
|
+
delete data.sizing[axis];
|
|
790
|
+
if (!Object.keys(data.sizing).length)
|
|
791
|
+
delete data.sizing;
|
|
792
|
+
}
|
|
793
|
+
if (data.heldHug) {
|
|
794
|
+
data.heldHug = data.heldHug.filter((held) => held !== axis);
|
|
795
|
+
if (!data.heldHug.length)
|
|
796
|
+
delete data.heldHug;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
const I = { id, kind: 'frame', parent: E.parent, ...(E.sourceId !== undefined && { sourceId: E.sourceId }), name: E.name, t: E.t, opacity: E.opacity ?? 1, w: E.w, h: E.h, component: main };
|
|
800
|
+
if (E.fileInstance)
|
|
801
|
+
I.fileInstance = E.fileInstance;
|
|
802
|
+
for (const k of ['visible', 'blend', 'mask', 'exports'])
|
|
803
|
+
if (E[k] !== undefined)
|
|
804
|
+
I[k] = E[k];
|
|
805
|
+
if (Object.keys(data).length)
|
|
806
|
+
I.layoutData = data;
|
|
807
|
+
I.overrides = overrides;
|
|
808
|
+
const mismatch = keep(I, geometry);
|
|
809
|
+
if (mismatch)
|
|
810
|
+
return mismatch;
|
|
811
|
+
if (!Object.keys(overrides).length)
|
|
812
|
+
delete I.overrides;
|
|
813
|
+
if (aliases)
|
|
814
|
+
for (const [path, { E }] of geometry)
|
|
815
|
+
recordAlias(E.id, `${id};${path}`);
|
|
816
|
+
byId.set(id, I);
|
|
817
|
+
kids.delete(id);
|
|
818
|
+
views.delete(id);
|
|
819
|
+
if (linkPaths?.size)
|
|
820
|
+
omittedLinks.set(id, linkPaths);
|
|
821
|
+
linked++;
|
|
822
|
+
return null;
|
|
823
|
+
}
|
|
824
|
+
function walk(id) {
|
|
825
|
+
const m = byId.get(id);
|
|
826
|
+
if (m.mainOf !== undefined && mains.get(m.mainOf) === id)
|
|
827
|
+
return ensure(id);
|
|
828
|
+
if (m.instanceOf !== undefined) {
|
|
829
|
+
const r = tryLink(id);
|
|
830
|
+
if (!r)
|
|
831
|
+
return;
|
|
832
|
+
reasons.set(r, (reasons.get(r) ?? 0) + 1);
|
|
833
|
+
}
|
|
834
|
+
for (const c of [...kidsOf(id)])
|
|
835
|
+
walk(c);
|
|
836
|
+
}
|
|
837
|
+
for (const c of [...kidsOf(''), ...kidsOf('@components')])
|
|
838
|
+
walk(c);
|
|
839
|
+
// Mains: on the page, and the hidden ones an instance on the page uses, or one inside a hidden main kept (not an
|
|
840
|
+
// instance left inside a linked expansion, nor in a hidden main nothing uses); used: per main, its instances there.
|
|
841
|
+
const used = new Map();
|
|
842
|
+
const visit = (id) => { const m = byId.get(id); if (isInstance(m)) {
|
|
843
|
+
used.set(m.component, (used.get(m.component) ?? 0) + 1);
|
|
844
|
+
if (used.get(m.component) === 1 && hidden.has(m.component))
|
|
845
|
+
visit(m.component);
|
|
846
|
+
} for (const c of kidsOf(id))
|
|
847
|
+
visit(c); };
|
|
848
|
+
if (hidden.size)
|
|
849
|
+
for (const c of kidsOf(''))
|
|
850
|
+
visit(c);
|
|
851
|
+
// Used once: a component (all its variants together) one instance uses; a variant used once of a component used
|
|
852
|
+
// more keeps its main, so that every instance of a set is an instance of its own variant (the panel's variants).
|
|
853
|
+
const uses = new Map();
|
|
854
|
+
for (const [id, vk] of hidden)
|
|
855
|
+
uses.set(componentOf(vk), (uses.get(componentOf(vk)) ?? 0) + (used.get(id) ?? 0));
|
|
856
|
+
const once = [...hidden].filter(([id, vk]) => used.get(id) === 1 && uses.get(componentOf(vk)) === 1).map(([, vk]) => vk);
|
|
857
|
+
if (hidden.size)
|
|
858
|
+
kids.set('@components', kidsOf('@components').filter((id) => !hidden.has(id) || used.has(id)));
|
|
859
|
+
for (const id of mains.values()) {
|
|
860
|
+
const m = byId.get(id);
|
|
861
|
+
if (m && (!hidden.has(id) || used.has(id)))
|
|
862
|
+
byId.set(id, { ...m, component: id });
|
|
863
|
+
}
|
|
864
|
+
// What each component stores: its used hidden mains, its instances with their override keys, its expansions left.
|
|
865
|
+
const cost = new Map(), add = (c, n, k = 0) => { const v = cost.get(c) ?? [0, 0]; cost.set(c, [v[0] + n, v[1] + k]); };
|
|
866
|
+
const keysOf = (I) => Object.values(I.overrides ?? {}).reduce((n, r) => n + Object.keys(r).length, 0);
|
|
867
|
+
if (compareCosts)
|
|
868
|
+
for (const [id, vk] of hidden)
|
|
869
|
+
if (used.has(id))
|
|
870
|
+
add(componentOf(vk), size(id));
|
|
871
|
+
const tally = (id) => {
|
|
872
|
+
const m = byId.get(id);
|
|
873
|
+
if (isInstance(m) && hidden.has(m.component))
|
|
874
|
+
add(componentOf(hidden.get(m.component)), 1, keysOf(m));
|
|
875
|
+
else if (m.instanceOf !== undefined)
|
|
876
|
+
add(m.instanceOf, size(id));
|
|
877
|
+
for (const c of kidsOf(id))
|
|
878
|
+
tally(c);
|
|
879
|
+
};
|
|
880
|
+
if (compareCosts)
|
|
881
|
+
for (const c of kidsOf(''))
|
|
882
|
+
tally(c);
|
|
883
|
+
const models = [];
|
|
884
|
+
const emit = (id) => { const m = { ...byId.get(id) }; unmark(m); delete m.fileSlotHost; delete m.fileFills; models.push(m); for (const c of kidsOf(id))
|
|
885
|
+
emit(c); };
|
|
886
|
+
for (const c of kidsOf(''))
|
|
887
|
+
emit(c);
|
|
888
|
+
for (const c of kidsOf('@components'))
|
|
889
|
+
emit(c);
|
|
890
|
+
doc.models = models;
|
|
891
|
+
return { omittedLinks, cost, before, after: models.length, linked, mains: mains.size - hidden.size, hidden: [...hidden.keys()].filter((id) => used.has(id)).length, unlinked: reasons, once };
|
|
892
|
+
}
|