@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,688 @@
|
|
|
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, 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 { instanceLayout, legacyAuto, netBounds, sizingOf } from '@moondraft/editor/core';
|
|
34
|
+
/** The converter's markers on the expanded models (instanceOf, mainOf, and setOf: an instance's variant set name). */
|
|
35
|
+
export const MARKERS = ['instanceOf', 'mainOf', 'setOf', 'variant'];
|
|
36
|
+
/** The component of a hidden main's key (linkOnce variantKey). */
|
|
37
|
+
const componentOf = (vk) => vk.split('\u0000')[0];
|
|
38
|
+
/**
|
|
39
|
+
* Removes component linking markers from a model.
|
|
40
|
+
* @param m - Converted model to update.
|
|
41
|
+
*/
|
|
42
|
+
export const unmark = (m) => { for (const k of MARKERS)
|
|
43
|
+
delete m[k]; };
|
|
44
|
+
/** Keys that draw nothing, or name the node and its place: never compared. */
|
|
45
|
+
const IGNORED = new Set(['id', 'parent', 'children', 'z', 'order', 'sourceId', 'fileInstance', 'fileSlotHost', 'fileFills', 'name', 'component', 'overrides', 'instanceOf', 'mainOf', 'setOf', 'variant',
|
|
46
|
+
'layoutData', 'keepSize', 'remeasured', 'storedHeight', 'storedRow', 'storedBox', 'boxScale', 'locked', 'runs', 'styleAt', 'fill', 'look']);
|
|
47
|
+
/** An instance's own record (SPEC 19.2): the rest of an instance's appearance is its main root's. */
|
|
48
|
+
const OWN = new Set(['t', 'w', 'h', 'opacity', 'visible', 'blend', 'mask', 'exports']);
|
|
49
|
+
const same = (a, b) => {
|
|
50
|
+
if (a === b)
|
|
51
|
+
return true;
|
|
52
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null || Array.isArray(a) !== Array.isArray(b))
|
|
53
|
+
return false;
|
|
54
|
+
const ka = Object.keys(a).filter((k) => a[k] !== undefined), kb = Object.keys(b).filter((k) => b[k] !== undefined);
|
|
55
|
+
return ka.length === kb.length && ka.every((k) => same(a[k], b[k]));
|
|
56
|
+
};
|
|
57
|
+
/** What a layer fills with: its look's fills, else its compact fill as one solid paint (the renderer draws both so). */
|
|
58
|
+
const fillsOf = (m) => m.look?.fills ?? (m.fill ? [{ k: 'solid', c: m.fill }] : []);
|
|
59
|
+
/** A text's style index per UTF-16 unit, from styleAt or the file's runs ([length, style]). */
|
|
60
|
+
function styleAtOf(m) {
|
|
61
|
+
if (m.styleAt)
|
|
62
|
+
return m.styleAt;
|
|
63
|
+
const at = [];
|
|
64
|
+
let s = 0;
|
|
65
|
+
for (const [n, k] of m.runs ?? []) {
|
|
66
|
+
if (k >= 0)
|
|
67
|
+
s = k;
|
|
68
|
+
for (let i = 0; i < n; i++)
|
|
69
|
+
at.push(s);
|
|
70
|
+
}
|
|
71
|
+
return at;
|
|
72
|
+
}
|
|
73
|
+
/** One override record applied to a copy of a record, as core applies it (components.ts applyOverride: a stored box is
|
|
74
|
+
* the instance's layout's alone). */
|
|
75
|
+
function applied(record, over) {
|
|
76
|
+
const out = { ...record };
|
|
77
|
+
for (const [key, value] of Object.entries(over)) {
|
|
78
|
+
if (key === 'storedBox')
|
|
79
|
+
continue;
|
|
80
|
+
if (!key.includes('.')) {
|
|
81
|
+
out[key] = value;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const path = key.split('.');
|
|
85
|
+
let at = out;
|
|
86
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
87
|
+
const next = { ...at[path[i]] };
|
|
88
|
+
at[path[i]] = next;
|
|
89
|
+
at = next;
|
|
90
|
+
}
|
|
91
|
+
at[path[path.length - 1]] = value;
|
|
92
|
+
if (key === 'look.fills')
|
|
93
|
+
delete out.fill;
|
|
94
|
+
}
|
|
95
|
+
if (over.styleAt)
|
|
96
|
+
delete out.runs;
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
/** The keys of a record that differ from another's as one override (null: none), or the key that takes no override
|
|
100
|
+
* (a string). own: whether the record is a layer's (its own keys take overrides) or a root's (they are the instance's). */
|
|
101
|
+
function difference(E, R, own, rootOverride = false) {
|
|
102
|
+
const rec = {};
|
|
103
|
+
for (const k of new Set([...Object.keys(E), ...Object.keys(R)])) {
|
|
104
|
+
if (IGNORED.has(k) || same(E[k], R[k]))
|
|
105
|
+
continue;
|
|
106
|
+
if (OWN.has(k) && !own)
|
|
107
|
+
continue;
|
|
108
|
+
if (k === 't' || k === 'opacity' || k === 'blend' || k === 'visible') {
|
|
109
|
+
const fallback = { opacity: 1, blend: 0, visible: true }[k];
|
|
110
|
+
if (!same(E[k] ?? fallback, R[k] ?? fallback))
|
|
111
|
+
rec[k] = E[k] ?? fallback;
|
|
112
|
+
}
|
|
113
|
+
else if (k === 'w' || k === 'h') {
|
|
114
|
+
rec.w = E.w;
|
|
115
|
+
rec.h = E.h;
|
|
116
|
+
}
|
|
117
|
+
else if (k === 'radius' || k === 'smoothing') {
|
|
118
|
+
if (!same(E.radius ?? 0, R.radius ?? 0) || !same(E.smoothing ?? 0, R.smoothing ?? 0)) {
|
|
119
|
+
rec.radius = E.radius ?? 0;
|
|
120
|
+
rec.smoothing = E.smoothing ?? 0;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (k === 'content' || k === 'styles' || k === 'para')
|
|
124
|
+
continue; // the text body, below
|
|
125
|
+
else if ((k === 'stroke' || k === 'strokeWidth') && E.kind !== 'text')
|
|
126
|
+
continue; // the stroke, below
|
|
127
|
+
else
|
|
128
|
+
return k;
|
|
129
|
+
}
|
|
130
|
+
if (E.kind === 'text') {
|
|
131
|
+
const body = (m) => [m.content, m.styles, styleAtOf(m), m.para ?? []];
|
|
132
|
+
const [a, b] = [body(E), body(R)];
|
|
133
|
+
// The renderer's text body field carries the text's box, taken from the main's layer with this record applied
|
|
134
|
+
// (wire.ts overrideBase), and replaces an inner instance's whole body, box included: a text's size and body go
|
|
135
|
+
// together, so neither an inner size nor the main's box shows through.
|
|
136
|
+
if (!a.every((v, i) => same(v, b[i])) || rec.w !== undefined)
|
|
137
|
+
Object.assign(rec, { content: a[0], styles: a[1], styleAt: a[2], para: a[3], w: E.w, h: E.h });
|
|
138
|
+
}
|
|
139
|
+
if (E.kind !== 'text') {
|
|
140
|
+
if (!same(fillsOf(E), fillsOf(R))) {
|
|
141
|
+
if (!own && !rootOverride)
|
|
142
|
+
return 'fills';
|
|
143
|
+
rec['look.fills'] = fillsOf(E);
|
|
144
|
+
}
|
|
145
|
+
if (!same(E.strokeWidth, R.strokeWidth))
|
|
146
|
+
return 'strokeWidth';
|
|
147
|
+
if (!same(E.stroke, R.stroke) && (!E.stroke || !R.stroke))
|
|
148
|
+
return 'stroke';
|
|
149
|
+
const { paint: pe, ...se } = E.look?.stroke ?? {}, { paint: pr, ...sr } = R.look?.stroke ?? {};
|
|
150
|
+
if (!same(se, sr) || !!E.look?.stroke !== !!R.look?.stroke)
|
|
151
|
+
return 'look.stroke';
|
|
152
|
+
// What a drawn stroke paints with: its look's paint, else its compact colour as one solid paint (the renderer
|
|
153
|
+
// draws a paint in place of the compact colour), so an expansion's other colour is a paint override.
|
|
154
|
+
const strokePaint = (m, p) => p ?? (m.stroke ? { k: 'solid', c: m.stroke } : undefined);
|
|
155
|
+
const qe = strokePaint(E, pe), qr = strokePaint(R, pr);
|
|
156
|
+
if (!same(qe, qr)) {
|
|
157
|
+
if ((!own && !rootOverride) || !qe || !E.look?.stroke)
|
|
158
|
+
return same(E.stroke, R.stroke) ? 'look.stroke.paint' : 'stroke';
|
|
159
|
+
rec['look.stroke.paint'] = qe;
|
|
160
|
+
}
|
|
161
|
+
const effects = (m) => [m.look?.shadows ?? [], m.look?.layerBlur ?? { radius: 0 }, m.look?.backgroundBlur ?? { radius: 0 }];
|
|
162
|
+
const [a, b] = [effects(E), effects(R)];
|
|
163
|
+
if (!a.every((v, i) => same(v, b[i]))) {
|
|
164
|
+
if (!own && !rootOverride)
|
|
165
|
+
return 'effects';
|
|
166
|
+
Object.assign(rec, { 'look.shadows': a[0], 'look.layerBlur': a[1], 'look.backgroundBlur': a[2] });
|
|
167
|
+
}
|
|
168
|
+
for (const k of Object.keys({ ...E.look, ...R.look }))
|
|
169
|
+
if (!['fills', 'stroke', 'shadows', 'layerBlur', 'backgroundBlur'].includes(k) && !same(E.look?.[k], R.look?.[k]))
|
|
170
|
+
return `look.${k}`;
|
|
171
|
+
}
|
|
172
|
+
if (!own && !rootOverride && Object.keys(rec).length)
|
|
173
|
+
return Object.keys(rec)[0];
|
|
174
|
+
return Object.keys(rec).length ? rec : null;
|
|
175
|
+
}
|
|
176
|
+
// Core lays an instance out again (a resize of it, an edit of its main, an override that hides a layer or changes a
|
|
177
|
+
// text) from its layers' layout inputs: the main's, or those its overrides hold (components.ts LAYOUT_INPUTS), so an
|
|
178
|
+
// expansion laid out from other inputs than its main's (a sizing Figma overrode, a box the converter held) stores
|
|
179
|
+
// its own. What core's layout reads besides geometry:
|
|
180
|
+
/** of a layer, its layoutData and the flags that hold an imported box (absent: 0 or false), as an override; */
|
|
181
|
+
const layerInputs = (m) => ({ layoutData: m.layoutData ?? {}, keepSize: !!m.keepSize, storedHeight: m.storedHeight ?? 0, storedRow: m.storedRow ?? 0 });
|
|
182
|
+
/** What a layoutData field reads as when absent, where an override can say so (a field without one, the auto layout or an
|
|
183
|
+
* arc's box, is absent only in a whole layoutData). */
|
|
184
|
+
const NEUTRAL = { sizing: {}, resizing: {}, constraints: {}, padding: {}, layoutPositioning: 'auto', rehug: false, aspectRatioLocked: false, instance: false, relaid: false };
|
|
185
|
+
/** The override that makes layoutData `main` read as `own`: each field that differs alone ('layoutData.sizing'), so
|
|
186
|
+
* that the main's later edits of the others (its padding and gap) still reach the layer, or the whole layoutData when a
|
|
187
|
+
* field the main has cannot be cleared alone. Empty when none differs. */
|
|
188
|
+
function dataOverride(own = {}, main = {}) {
|
|
189
|
+
const out = {};
|
|
190
|
+
for (const k of new Set([...Object.keys(own), ...Object.keys(main)])) {
|
|
191
|
+
if (same(own[k], main[k]))
|
|
192
|
+
continue;
|
|
193
|
+
if (own[k] === undefined && !(k in NEUTRAL))
|
|
194
|
+
return { layoutData: own };
|
|
195
|
+
out[`layoutData.${k}`] = own[k] ?? NEUTRAL[k];
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
/** A layoutData with an override record's layoutData fields applied (as core's applyOverride does). */
|
|
200
|
+
function withData(data, over) {
|
|
201
|
+
if (!over)
|
|
202
|
+
return data;
|
|
203
|
+
let out = over.layoutData ?? data;
|
|
204
|
+
for (const [k, v] of Object.entries(over))
|
|
205
|
+
if (k.startsWith('layoutData.'))
|
|
206
|
+
out = { ...out, [k.slice(11)]: v };
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
/** of an instance's content, its main root's auto layout and padding (an instance whose content's differ stays
|
|
210
|
+
* expanded: an override of the instance's layoutData would not reach its main's content on the renderer's side); */
|
|
211
|
+
const contentInputs = (m) => [m.layoutData?.layout, m.layoutData?.padding];
|
|
212
|
+
/** of an instance in its parent (P), its own layoutData as linking gives it: no auto layout (its main's), and a size
|
|
213
|
+
* that hugs held ('fixed': it has no children to hug), so a parent that hugs it lays out as it did. */
|
|
214
|
+
function instanceData(E, P) {
|
|
215
|
+
const { layout: _layout, box: _box, ...data } = E.layoutData ?? {};
|
|
216
|
+
const fixed = (o) => o && Object.fromEntries(Object.entries(o).map(([k, v]) => [k, v === 'hug' ? 'fixed' : v]));
|
|
217
|
+
// The axes it hugged are recorded (heldHug), so that a swap in the editor hugs them again.
|
|
218
|
+
const held = ['width', 'height'].filter((a) => data.sizing?.[a] === 'hug' || data.resizing?.[a] === 'hug');
|
|
219
|
+
if (held.length)
|
|
220
|
+
data.heldHug = held;
|
|
221
|
+
if (data.sizing)
|
|
222
|
+
data.sizing = fixed(data.sizing);
|
|
223
|
+
if (data.resizing)
|
|
224
|
+
data.resizing = fixed(data.resizing);
|
|
225
|
+
const auto = ['horizontal', 'vertical'].includes(E.layoutData?.layout?.kind);
|
|
226
|
+
for (const axis of ['width', 'height'])
|
|
227
|
+
if (auto && sizingOf(E.layoutData, axis) === 'fill' && sizingOf(P?.layoutData, axis) === 'hug')
|
|
228
|
+
data.sizing = { ...data.sizing, [axis]: 'fixed' };
|
|
229
|
+
return data;
|
|
230
|
+
}
|
|
231
|
+
/** How far core's layout of a linked instance may place a layer from its expanded box (px; 1e-4 of the linear part)
|
|
232
|
+
* before the box is stored. */
|
|
233
|
+
const EPSILON = 0.01;
|
|
234
|
+
/** How far (px) a layer's shadow or blur may reach past its box and still count as inside it (overflows). The renderer
|
|
235
|
+
* 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
|
|
236
|
+
* Modal instance was cut 22 px below it, while the HeroUI pages' tails of up to 8 px drew as expanded. */
|
|
237
|
+
const SLACK = 8;
|
|
238
|
+
/** Whether core's layout of an instance (laid: its output for the layer, absent where it shows its main's R) gives the
|
|
239
|
+
* expanded record E's transform and size. */
|
|
240
|
+
function gives(laid, { E, R }) {
|
|
241
|
+
const t = laid?.t ?? R.t ?? [1, 0, 0, 1, 0, 0], e = E.t ?? [1, 0, 0, 1, 0, 0];
|
|
242
|
+
if (t.some((v, i) => Math.abs(v - e[i]) > (i < 4 ? 1e-4 : EPSILON)))
|
|
243
|
+
return false;
|
|
244
|
+
const size = (k) => (laid && k in laid ? laid[k] : R[k]);
|
|
245
|
+
return ['w', 'h'].every((k) => E[k] === size(k) || (E[k] !== undefined && size(k) !== undefined && Math.abs(E[k] - size(k)) <= EPSILON));
|
|
246
|
+
}
|
|
247
|
+
/** Links the instances of a converted page (its models, in file order) to their mains, in place. Returns counts.
|
|
248
|
+
* A hidden main only one instance would use (not a main on the page) costs more than that instance's expansion, so
|
|
249
|
+
* the page is linked again without it, that instance left expanded, until no hidden main is used once (an instance
|
|
250
|
+
* left expanded can make another's use count change).
|
|
251
|
+
* @param doc - Converted models in file order.
|
|
252
|
+
* @returns Counts of links, mains and instances left expanded.
|
|
253
|
+
*/
|
|
254
|
+
export function linkComponents(doc) {
|
|
255
|
+
return linkWithLayers(doc, false).counts;
|
|
256
|
+
}
|
|
257
|
+
/** @internal Links components and tracks where their expanded layers went for import reports. */
|
|
258
|
+
export function linkWithLayers(doc, trackLayers = true) {
|
|
259
|
+
// Variant sets (convert.ts markSet): one hidden main per variant used, so each instance's main is its own variant
|
|
260
|
+
// and its overrides are its own changes alone; but a showcase of a set (each variant once or twice) costs a main per
|
|
261
|
+
// variant. So per set: the page is linked both ways and a set keeps a main per variant where that stores no more
|
|
262
|
+
// than one main for the set, a model (a layer) weighing as three override keys; otherwise its one main is named for
|
|
263
|
+
// the set alone ("Button", no variant: the editor shows no variant it is not). Over the 84 exports a weight of 1
|
|
264
|
+
// grew the models 13%, 3 grows them 4% (39583 to 41158) and still cuts the override keys by 23%; 6 kept the models
|
|
265
|
+
// level but cut the keys by 19% only. The used-once rule stays per component: a variant's main used once is kept
|
|
266
|
+
// when its set is used more than once, so every variant of a set can be swapped to.
|
|
267
|
+
const sets = new Set(doc.models.filter((m) => m.setOf && m.variant).map((m) => m.instanceOf));
|
|
268
|
+
const run = (each, report = false) => {
|
|
269
|
+
const skip = new Set();
|
|
270
|
+
for (;;) {
|
|
271
|
+
const d = { models: structuredClone(doc.models) }, aliases = new Map();
|
|
272
|
+
const { once, ...r } = linkOnce(d, skip, each, report ? aliases : undefined);
|
|
273
|
+
if (!once.length)
|
|
274
|
+
return { d, r, aliases };
|
|
275
|
+
for (const comp of once)
|
|
276
|
+
skip.add(comp);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
let chosen = new Set();
|
|
280
|
+
if (sets.size) {
|
|
281
|
+
const shared = run(new Set()).r.cost, split = run(sets).r.cost;
|
|
282
|
+
const W = 3, of = (m, c) => { const [n, k] = m.get(c) ?? [0, 0]; return n * W + k; };
|
|
283
|
+
chosen = new Set([...sets].filter((c) => of(split, c) <= of(shared, c)));
|
|
284
|
+
}
|
|
285
|
+
const { d, r: { cost: _cost, ...r }, aliases } = run(chosen, trackLayers);
|
|
286
|
+
doc.models = d.models;
|
|
287
|
+
const counts = { ...r, variantSets: chosen.size, sharedSets: sets.size - chosen.size };
|
|
288
|
+
return { counts, aliases };
|
|
289
|
+
}
|
|
290
|
+
/** linkComponents with no hidden main for the components in `skip`; also returns the components whose hidden main is
|
|
291
|
+
* used once (once). */
|
|
292
|
+
function linkOnce(doc, skip, each = new Set(), aliases) {
|
|
293
|
+
const recordAlias = (from, to) => {
|
|
294
|
+
if (!aliases)
|
|
295
|
+
return;
|
|
296
|
+
let ids = aliases.get(from);
|
|
297
|
+
if (!ids)
|
|
298
|
+
aliases.set(from, ids = []);
|
|
299
|
+
ids.push(to);
|
|
300
|
+
};
|
|
301
|
+
// The hidden main an expanded instance takes: its component's, or its variant's for a set in `each`.
|
|
302
|
+
const variantKey = (m) => (each.has(m.instanceOf) && m.setOf && m.variant ? `${m.instanceOf}\u0000${m.variant}` : m.instanceOf);
|
|
303
|
+
const byId = new Map(doc.models.map((m) => [m.id, m])), kids = new Map();
|
|
304
|
+
for (const m of doc.models) {
|
|
305
|
+
const p = m.parent ?? '';
|
|
306
|
+
if (!kids.has(p))
|
|
307
|
+
kids.set(p, []);
|
|
308
|
+
kids.get(p).push(m.id);
|
|
309
|
+
}
|
|
310
|
+
const kidsOf = (id) => kids.get(id) ?? [];
|
|
311
|
+
const size = (id) => 1 + kidsOf(id).reduce((n, c) => n + size(c), 0);
|
|
312
|
+
const before = doc.models.length;
|
|
313
|
+
const mains = new Map(), instances = new Map();
|
|
314
|
+
for (const m of doc.models) {
|
|
315
|
+
if (m.mainOf !== undefined && !mains.has(m.mainOf) && m.kind === 'frame')
|
|
316
|
+
mains.set(m.mainOf, m.id);
|
|
317
|
+
if (m.instanceOf !== undefined) {
|
|
318
|
+
const k = variantKey(m);
|
|
319
|
+
if (!instances.has(k))
|
|
320
|
+
instances.set(k, []);
|
|
321
|
+
instances.get(k).push(m.id);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// Whether the layers under a frame draw outside its box, with their drop shadows and layer blurs where they reach
|
|
325
|
+
// further than SLACK, as the renderer reaches them (the offset plus 1.5 blur and the spread; crates/scn-core
|
|
326
|
+
// derived.rs effect_reach, with the spread shadow_spread draws: below 0 only on a frame with square corners);
|
|
327
|
+
// clipping frames bound their own children.
|
|
328
|
+
const square = (m) => m.kind === 'frame' && (Array.isArray(m.radius) ? m.radius.every((r) => !r) : !m.radius);
|
|
329
|
+
const reach = (m) => {
|
|
330
|
+
const blur = 1.5 * (m.look?.layerBlur?.radius ?? 0), out = [blur, blur, blur, blur]; // left, top, right, bottom
|
|
331
|
+
for (const s of m.look?.shadows ?? [])
|
|
332
|
+
if (!s.inner && s.c?.[3] !== 0) {
|
|
333
|
+
const spread = square(m) ? s.spread ?? 0 : Math.max(0, s.spread ?? 0), x = s.x ?? 0, y = s.y ?? 0;
|
|
334
|
+
const e = 1.5 * (s.blur ?? 0) + spread;
|
|
335
|
+
out[0] = Math.max(out[0], e - x);
|
|
336
|
+
out[1] = Math.max(out[1], e - y);
|
|
337
|
+
out[2] = Math.max(out[2], e + x);
|
|
338
|
+
out[3] = Math.max(out[3], e + y);
|
|
339
|
+
}
|
|
340
|
+
return out.map((v) => (v > SLACK ? v : 0));
|
|
341
|
+
};
|
|
342
|
+
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]];
|
|
343
|
+
function overflows(root) {
|
|
344
|
+
const R = byId.get(root), stack = kidsOf(root).map((c) => [c, byId.get(c).t]);
|
|
345
|
+
while (stack.length) {
|
|
346
|
+
const [id, T] = stack.pop(), m = byId.get(id);
|
|
347
|
+
if (m.visible === false)
|
|
348
|
+
continue;
|
|
349
|
+
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);
|
|
350
|
+
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]]) {
|
|
351
|
+
const X = T[0] * x + T[2] * y + T[4], Y = T[1] * x + T[3] * y + T[5];
|
|
352
|
+
if (X < -0.5 || Y < -0.5 || X > R.w + 0.5 || Y > R.h + 0.5)
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
if (!m.clip)
|
|
356
|
+
for (const c of kidsOf(id))
|
|
357
|
+
stack.push([c, mul(T, byId.get(c).t)]);
|
|
358
|
+
}
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
const key = (m) => m.fileInstance ?? m.sourceId ?? `~${m.kind}~${m.name ?? ''}`;
|
|
362
|
+
// Hidden mains (taken before anything is linked): a copy of each component's largest expansion, with every layer
|
|
363
|
+
// another expansion has and it lacks, hidden (see union).
|
|
364
|
+
let serial = 0;
|
|
365
|
+
const fresh = () => { let id; do
|
|
366
|
+
id = `c${(serial++).toString(36)}`;
|
|
367
|
+
while (byId.has(id)); return id; };
|
|
368
|
+
const copy = (id, parent) => {
|
|
369
|
+
const m = { ...byId.get(id), id: fresh(), parent };
|
|
370
|
+
recordAlias(id, m.id);
|
|
371
|
+
byId.set(m.id, m);
|
|
372
|
+
kids.set(m.id, kidsOf(id).map((c) => copy(c, m.id).id));
|
|
373
|
+
return m;
|
|
374
|
+
};
|
|
375
|
+
// Adds to main layer `u` the layers under expansion layer `e` that it lacks: the most of e's layers kept in order (by
|
|
376
|
+
// key) pair with the main's, and a copy of each other one goes in, hidden (the main shows what its largest expansion
|
|
377
|
+
// does, and an instance that shows the layer overrides its visibility), after the layers it follows there; and so on
|
|
378
|
+
// down the layers match pairs (by key, each with the first of its key after the last paired). A copy that would give
|
|
379
|
+
// the main more layers of a key than the expansion has (one it has, in another order) is not added: plan gives null
|
|
380
|
+
// and the expansion adds nothing (it stays expanded), so the main holds no layer twice.
|
|
381
|
+
function plan(e, u) {
|
|
382
|
+
const ek = kidsOf(e), uk = kidsOf(u), K = (x) => key(byId.get(x)), n = ek.length, m = uk.length;
|
|
383
|
+
const L = Array.from({ length: n + 1 }, () => new Int32Array(m + 1));
|
|
384
|
+
for (let i = n - 1; i >= 0; i--)
|
|
385
|
+
for (let j = m - 1; j >= 0; j--)
|
|
386
|
+
L[i][j] = K(ek[i]) === K(uk[j]) ? L[i + 1][j + 1] + 1 : Math.max(L[i + 1][j], L[i][j + 1]);
|
|
387
|
+
const list = [], added = new Map();
|
|
388
|
+
let i = 0, j = 0;
|
|
389
|
+
while (i < n) {
|
|
390
|
+
if (j < m && K(ek[i]) === K(uk[j])) {
|
|
391
|
+
list.push(uk[j++]);
|
|
392
|
+
i++;
|
|
393
|
+
}
|
|
394
|
+
else if (j >= m || L[i + 1][j] >= L[i][j + 1]) {
|
|
395
|
+
const k = K(ek[i]);
|
|
396
|
+
added.set(k, (added.get(k) ?? 0) + 1);
|
|
397
|
+
list.push({ copy: ek[i++] });
|
|
398
|
+
}
|
|
399
|
+
else
|
|
400
|
+
list.push(uk[j++]);
|
|
401
|
+
}
|
|
402
|
+
list.push(...uk.slice(j));
|
|
403
|
+
const count = (ids, k) => ids.filter((x) => K(x) === k).length;
|
|
404
|
+
for (const [k, a] of added)
|
|
405
|
+
if (count(uk, k) + a > count(ek, k))
|
|
406
|
+
return null;
|
|
407
|
+
const subs = [];
|
|
408
|
+
let last = -1;
|
|
409
|
+
for (const c of ek) {
|
|
410
|
+
last = list.findIndex((x, p) => p > last && K(typeof x === 'string' ? x : x.copy) === K(c));
|
|
411
|
+
const x = list[last];
|
|
412
|
+
if (typeof x !== 'string')
|
|
413
|
+
continue;
|
|
414
|
+
const sub = plan(c, x);
|
|
415
|
+
if (!sub)
|
|
416
|
+
return null;
|
|
417
|
+
subs.push([x, sub]);
|
|
418
|
+
}
|
|
419
|
+
return { list, subs };
|
|
420
|
+
}
|
|
421
|
+
function union(u, p) {
|
|
422
|
+
kids.set(u, p.list.map((x) => { if (typeof x === 'string')
|
|
423
|
+
return x; const m = copy(x.copy, u); m.visible = false; return m.id; }));
|
|
424
|
+
for (const [x, sub] of p.subs)
|
|
425
|
+
union(x, sub);
|
|
426
|
+
}
|
|
427
|
+
const hidden = new Map(); // hidden main: its variant key (variantKey)
|
|
428
|
+
for (const [vk, list] of instances) {
|
|
429
|
+
const comp = componentOf(vk);
|
|
430
|
+
if (mains.has(comp) || skip.has(vk))
|
|
431
|
+
continue;
|
|
432
|
+
// Largest first (the first of the largest in file order).
|
|
433
|
+
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);
|
|
434
|
+
if (!frames.length)
|
|
435
|
+
continue;
|
|
436
|
+
const root = copy(frames[0], '@components');
|
|
437
|
+
delete root.exports; // the copied root held an instance's own settings
|
|
438
|
+
if (!kids.has('@components'))
|
|
439
|
+
kids.set('@components', []);
|
|
440
|
+
kids.get('@components').push(root.id);
|
|
441
|
+
// A variant's hidden main is named for its set and variant (the set is not on the page to hold it): "Button/variant=…".
|
|
442
|
+
const E0 = byId.get(frames[0]);
|
|
443
|
+
if (E0.setOf && E0.variant)
|
|
444
|
+
root.name = vk === comp ? E0.setOf : `${E0.setOf}/${E0.variant}`;
|
|
445
|
+
unmark(root);
|
|
446
|
+
root.mainOf = comp;
|
|
447
|
+
// An expansion that stays expanded whatever its main (its layers draw outside it, or in another order) adds nothing.
|
|
448
|
+
for (const id of frames.slice(1))
|
|
449
|
+
if (byId.get(id).clip || !overflows(id)) {
|
|
450
|
+
const p = plan(id, root.id);
|
|
451
|
+
if (p)
|
|
452
|
+
union(root.id, p);
|
|
453
|
+
}
|
|
454
|
+
mains.set(vk, root.id);
|
|
455
|
+
hidden.set(root.id, vk);
|
|
456
|
+
}
|
|
457
|
+
const isInstance = (m) => m?.kind === 'frame' && m.component !== undefined && m.component !== m.id;
|
|
458
|
+
// A main's effective tree: per layer, its path (from the main's child down), what it shows and its children.
|
|
459
|
+
const trees = new Map();
|
|
460
|
+
const reroot = (n, prefix, overrides) => ({ path: `${prefix};${n.path}`, rec: overrides?.[n.path] ? applied(n.rec, overrides[n.path]) : n.rec, kids: n.kids.map((k) => reroot(k, prefix, overrides)), instance: n.instance, main: n.main, rootOverride: n.rootOverride, data: withData(n.data, overrides?.[n.path]) });
|
|
461
|
+
function tree(main) {
|
|
462
|
+
if (trees.has(main))
|
|
463
|
+
return trees.get(main);
|
|
464
|
+
const build = (id, path) => {
|
|
465
|
+
const m = byId.get(id);
|
|
466
|
+
if (!isInstance(m))
|
|
467
|
+
return { path, rec: m, kids: kidsOf(id).map((c) => build(c, `${path};${c}`)) };
|
|
468
|
+
const M = byId.get(m.component), rec = { ...(m.fileInstance && { fileInstance: m.fileInstance }), ...Object.fromEntries(Object.entries(M).filter(([k]) => !OWN.has(k))), ...Object.fromEntries(Object.entries(m).filter(([k]) => OWN.has(k))) };
|
|
469
|
+
return { path, rec: m.overrides?.[''] ? applied(rec, m.overrides['']) : rec, main: m.component, rootOverride: m.overrides?.[''], instance: true, data: m.layoutData ?? {}, kids: tree(m.component).map((n) => reroot(n, path, m.overrides)) };
|
|
470
|
+
};
|
|
471
|
+
const t = kidsOf(main).map((c) => build(c, c));
|
|
472
|
+
trees.set(main, t);
|
|
473
|
+
return t;
|
|
474
|
+
}
|
|
475
|
+
// Fills `out` with the overrides that make main-tree node list `mk` show concrete children `ek`, inside an instance
|
|
476
|
+
// expanded at `size`, and `geometry` with each matched layer's expanded record, its main's and the box to store if
|
|
477
|
+
// core's layout does not give the expanded one; a reason if none can.
|
|
478
|
+
function match(ek, mk, out, size, geometry, fills) {
|
|
479
|
+
const at = new Map(), used = new Set();
|
|
480
|
+
mk.forEach((n, i) => { const k = key(n.rec); if (!at.has(k))
|
|
481
|
+
at.set(k, []); at.get(k).push(i); });
|
|
482
|
+
let last = -1;
|
|
483
|
+
for (const id of ek) {
|
|
484
|
+
const E = byId.get(id), k = key(E), i = at.get(k)?.find((j) => j > last);
|
|
485
|
+
if (i === undefined)
|
|
486
|
+
return at.has(k) ? 'layer order' : 'a layer the main lacks';
|
|
487
|
+
last = i;
|
|
488
|
+
used.add(i);
|
|
489
|
+
const n = mk[i];
|
|
490
|
+
let R = n.rec, children = n.kids;
|
|
491
|
+
const swap = E.fileInstance && n.instance && mains.get(E.instanceOf);
|
|
492
|
+
if (swap && swap !== n.main) {
|
|
493
|
+
ensure(swap);
|
|
494
|
+
const root = byId.get(swap);
|
|
495
|
+
R = { ...(n.rootOverride ? applied(root, n.rootOverride) : root), ...Object.fromEntries(Object.entries(R).filter(([key]) => OWN.has(key))) };
|
|
496
|
+
children = tree(swap).map((child) => reroot(child, n.path, undefined));
|
|
497
|
+
out[n.path] = { component: swap };
|
|
498
|
+
}
|
|
499
|
+
if (E.kind !== R.kind)
|
|
500
|
+
return 'kind';
|
|
501
|
+
// A nested instance's appearance is its main's (the renderer draws its template root): only its own keys differ.
|
|
502
|
+
const d = difference(E, R, true);
|
|
503
|
+
if (typeof d === 'string')
|
|
504
|
+
return d;
|
|
505
|
+
if (n.instance && !E.fileInstance) {
|
|
506
|
+
const own = difference(E, R, false);
|
|
507
|
+
if (typeof own === 'string')
|
|
508
|
+
return own;
|
|
509
|
+
}
|
|
510
|
+
if (n.instance && !same(contentInputs(E), contentInputs(R)))
|
|
511
|
+
return 'layout';
|
|
512
|
+
const inputs = {};
|
|
513
|
+
if (n.instance)
|
|
514
|
+
Object.assign(inputs, dataOverride(instanceData(E, byId.get(E.parent)), n.data));
|
|
515
|
+
else {
|
|
516
|
+
const a = layerInputs(E), b = layerInputs(R);
|
|
517
|
+
for (const k of Object.keys(a))
|
|
518
|
+
if (!same(a[k], b[k]))
|
|
519
|
+
Object.assign(inputs, k === 'layoutData' ? dataOverride(a[k], b[k]) : { [k]: a[k] });
|
|
520
|
+
if ('storedHeight' in inputs || 'storedRow' in inputs)
|
|
521
|
+
Object.assign(inputs, { storedHeight: a.storedHeight, storedRow: a.storedRow });
|
|
522
|
+
}
|
|
523
|
+
// The box the expansion has where it differs from the main's, kept (keep) where core's layout of the instance does
|
|
524
|
+
// not give it.
|
|
525
|
+
geometry.set(n.path, { E, R, box: { ...(d && 't' in d && { t: d.t }), ...(d && 'w' in d && { w: d.w, h: d.h }), at: size } });
|
|
526
|
+
if (d || Object.keys(inputs).length)
|
|
527
|
+
out[n.path] = { ...out[n.path], ...d, ...inputs };
|
|
528
|
+
if (E.fileSlotHost && fills?.includes(E.fileSlotHost))
|
|
529
|
+
continue;
|
|
530
|
+
const r = match(kidsOf(id), children, out, size, geometry, fills);
|
|
531
|
+
if (r)
|
|
532
|
+
return r;
|
|
533
|
+
}
|
|
534
|
+
mk.forEach((n, i) => { if (!used.has(i) && n.rec.visible !== false)
|
|
535
|
+
out[n.path] = { ...out[n.path], visible: false }; });
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
// Core's view of a record (its load's defaults, its children), and of each main (its component its own id).
|
|
539
|
+
const views = new Map(), asMain = new Set(mains.values());
|
|
540
|
+
const view = (x) => {
|
|
541
|
+
if (x === undefined || !byId.has(x))
|
|
542
|
+
return undefined;
|
|
543
|
+
let v = views.get(x);
|
|
544
|
+
if (!v) {
|
|
545
|
+
v = { parent: '', t: [1, 0, 0, 1, 0, 0], opacity: 1, ...legacyAuto(byId.get(x)), children: kidsOf(x) };
|
|
546
|
+
if (asMain.has(x))
|
|
547
|
+
v.component = x;
|
|
548
|
+
views.set(x, v);
|
|
549
|
+
}
|
|
550
|
+
return v;
|
|
551
|
+
};
|
|
552
|
+
// Stores the boxes (storedBox) of instance I's layers that core's layout of it (layout.ts instanceLayout: from the
|
|
553
|
+
// main at its size, with its other overrides, resized to the instance's) does not give, within EPSILON. A box only
|
|
554
|
+
// constraints or auto layout give is not stored, so a later edit of the main reaches it. A stored box changes how
|
|
555
|
+
// the others lay out (the instance then starts at the imported size), so they are checked again with it until none
|
|
556
|
+
// is added.
|
|
557
|
+
function keep(I, geometry) {
|
|
558
|
+
const stored = new Set(), get = (x) => (x === I.id ? { ...I, children: [] } : view(x));
|
|
559
|
+
for (let round = 0; round <= geometry.size; round++) {
|
|
560
|
+
const laid = instanceLayout(get, I.id);
|
|
561
|
+
let added = false;
|
|
562
|
+
for (const [path, g] of geometry)
|
|
563
|
+
if (!stored.has(path) && !gives(laid.get(path), g)) {
|
|
564
|
+
stored.add(path);
|
|
565
|
+
I.overrides[path] = { ...I.overrides[path], storedBox: g.box };
|
|
566
|
+
added = true;
|
|
567
|
+
}
|
|
568
|
+
if (!added)
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const state = new Map(), reasons = new Map();
|
|
573
|
+
let linked = 0;
|
|
574
|
+
const ensure = (main) => { if (state.has(main))
|
|
575
|
+
return; state.set(main, 'busy'); for (const c of [...kidsOf(main)])
|
|
576
|
+
walk(c); state.set(main, 'done'); };
|
|
577
|
+
function tryLink(id) {
|
|
578
|
+
const E = byId.get(id), main = mains.get(E.instanceOf) ?? mains.get(variantKey(E));
|
|
579
|
+
if (main === undefined)
|
|
580
|
+
return skip.has(variantKey(E)) ? 'used once' : 'no main';
|
|
581
|
+
if (E.kind !== 'frame')
|
|
582
|
+
return 'not a frame';
|
|
583
|
+
if (!E.clip && overflows(id))
|
|
584
|
+
return 'content outside the instance';
|
|
585
|
+
if (state.get(main) === 'busy')
|
|
586
|
+
return 'inside its main';
|
|
587
|
+
ensure(main);
|
|
588
|
+
const M = byId.get(main);
|
|
589
|
+
const root = difference(E, M, false, !!E.fileInstance);
|
|
590
|
+
if (typeof root === 'string')
|
|
591
|
+
return `root ${root}`;
|
|
592
|
+
if (!same(contentInputs(E), contentInputs(M)))
|
|
593
|
+
return 'root layout';
|
|
594
|
+
const overrides = {}, geometry = new Map();
|
|
595
|
+
if (root)
|
|
596
|
+
overrides[''] = root;
|
|
597
|
+
const r = match(kidsOf(id), tree(main), overrides, { width: E.w, height: E.h }, geometry, E.fileFills);
|
|
598
|
+
if (r)
|
|
599
|
+
return r;
|
|
600
|
+
const data = instanceData(E, byId.get(E.parent));
|
|
601
|
+
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 };
|
|
602
|
+
if (E.fileInstance)
|
|
603
|
+
I.fileInstance = E.fileInstance;
|
|
604
|
+
for (const k of ['visible', 'blend', 'mask', 'exports'])
|
|
605
|
+
if (E[k] !== undefined)
|
|
606
|
+
I[k] = E[k];
|
|
607
|
+
if (Object.keys(data).length)
|
|
608
|
+
I.layoutData = data;
|
|
609
|
+
I.overrides = overrides;
|
|
610
|
+
keep(I, geometry);
|
|
611
|
+
if (!Object.keys(overrides).length)
|
|
612
|
+
delete I.overrides;
|
|
613
|
+
if (aliases)
|
|
614
|
+
for (const [path, { E }] of geometry)
|
|
615
|
+
recordAlias(E.id, `${id};${path}`);
|
|
616
|
+
byId.set(id, I);
|
|
617
|
+
kids.delete(id);
|
|
618
|
+
views.delete(id);
|
|
619
|
+
linked++;
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
function walk(id) {
|
|
623
|
+
const m = byId.get(id);
|
|
624
|
+
if (m.mainOf !== undefined && mains.get(m.mainOf) === id)
|
|
625
|
+
return ensure(id);
|
|
626
|
+
if (m.instanceOf !== undefined) {
|
|
627
|
+
const r = tryLink(id);
|
|
628
|
+
if (!r)
|
|
629
|
+
return;
|
|
630
|
+
reasons.set(r, (reasons.get(r) ?? 0) + 1);
|
|
631
|
+
}
|
|
632
|
+
for (const c of [...kidsOf(id)])
|
|
633
|
+
walk(c);
|
|
634
|
+
}
|
|
635
|
+
for (const c of [...kidsOf(''), ...kidsOf('@components')])
|
|
636
|
+
walk(c);
|
|
637
|
+
// Mains: on the page, and the hidden ones an instance on the page uses, or one inside a hidden main kept (not an
|
|
638
|
+
// instance left inside a linked expansion, nor in a hidden main nothing uses); used: per main, its instances there.
|
|
639
|
+
const used = new Map();
|
|
640
|
+
const visit = (id) => { const m = byId.get(id); if (isInstance(m)) {
|
|
641
|
+
used.set(m.component, (used.get(m.component) ?? 0) + 1);
|
|
642
|
+
if (used.get(m.component) === 1 && hidden.has(m.component))
|
|
643
|
+
visit(m.component);
|
|
644
|
+
} for (const c of kidsOf(id))
|
|
645
|
+
visit(c); };
|
|
646
|
+
for (const c of kidsOf(''))
|
|
647
|
+
visit(c);
|
|
648
|
+
// Used once: a component (all its variants together) one instance uses; a variant used once of a component used
|
|
649
|
+
// more keeps its main, so that every instance of a set is an instance of its own variant (the panel's variants).
|
|
650
|
+
const uses = new Map();
|
|
651
|
+
for (const [id, vk] of hidden)
|
|
652
|
+
uses.set(componentOf(vk), (uses.get(componentOf(vk)) ?? 0) + (used.get(id) ?? 0));
|
|
653
|
+
const once = [...hidden].filter(([id, vk]) => used.get(id) === 1 && uses.get(componentOf(vk)) === 1).map(([, vk]) => vk);
|
|
654
|
+
for (const id of hidden.keys())
|
|
655
|
+
if (!used.has(id))
|
|
656
|
+
kids.set('@components', kidsOf('@components').filter((c) => c !== id));
|
|
657
|
+
for (const id of mains.values()) {
|
|
658
|
+
const m = byId.get(id);
|
|
659
|
+
if (m && (!hidden.has(id) || used.has(id)))
|
|
660
|
+
m.component = id;
|
|
661
|
+
}
|
|
662
|
+
// What each component stores: its used hidden mains, its instances with their override keys, its expansions left.
|
|
663
|
+
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]); };
|
|
664
|
+
const keysOf = (I) => Object.values(I.overrides ?? {}).reduce((n, r) => n + Object.keys(r).length, 0);
|
|
665
|
+
for (const [id, vk] of hidden)
|
|
666
|
+
if (used.has(id))
|
|
667
|
+
add(componentOf(vk), size(id));
|
|
668
|
+
const tally = (id) => {
|
|
669
|
+
const m = byId.get(id);
|
|
670
|
+
if (isInstance(m) && hidden.has(m.component))
|
|
671
|
+
add(componentOf(hidden.get(m.component)), 1, keysOf(m));
|
|
672
|
+
else if (m.instanceOf !== undefined)
|
|
673
|
+
add(m.instanceOf, size(id));
|
|
674
|
+
for (const c of kidsOf(id))
|
|
675
|
+
tally(c);
|
|
676
|
+
};
|
|
677
|
+
for (const c of kidsOf(''))
|
|
678
|
+
tally(c);
|
|
679
|
+
const models = [];
|
|
680
|
+
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))
|
|
681
|
+
emit(c); };
|
|
682
|
+
for (const c of kidsOf(''))
|
|
683
|
+
emit(c);
|
|
684
|
+
for (const c of kidsOf('@components'))
|
|
685
|
+
emit(c);
|
|
686
|
+
doc.models = models;
|
|
687
|
+
return { cost, before, after: models.length, linked, mains: mains.size - hidden.size, hidden: [...hidden.keys()].filter((id) => used.has(id)).length, unlinked: reasons, once };
|
|
688
|
+
}
|