@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,1778 @@
|
|
|
1
|
+
import { ModelError } from "./codec.js";
|
|
2
|
+
import { UNCOMPUTED_COMPONENT, checkComponentProgram, evaluateComponentProgram, filledPropertyValues, paragraphsFromCharacters, } from "./component-evaluation.js";
|
|
3
|
+
import { componentsNestedIn, layerPathKey, nestingGraphOf, virtualNodeId, } from "./components.js";
|
|
4
|
+
import { componentExpressionEnvironment, componentValueEnvironment } from "./component-expressions.js";
|
|
5
|
+
import { checkExpressionSource } from "./expressions/check.js";
|
|
6
|
+
import { parseExpression } from "./expressions/parser.js";
|
|
7
|
+
import { evaluateExpression } from "./expressions/evaluate.js";
|
|
8
|
+
import { EXPRESSION_TYPES } from "./expressions/types.js";
|
|
9
|
+
import { ExpressionCache } from "./expressions/cache.js";
|
|
10
|
+
import { fitGroupToChildren } from "./group-bounds.js";
|
|
11
|
+
import { constrainChildren, scaleLayerInParent } from "./layout/constraints.js";
|
|
12
|
+
import { layoutFrameTree } from "./layout/frame-layout.js";
|
|
13
|
+
import { getDerivedAutoSizedTextFrame, getDrawnTextSizesVersion } from "./text-measurement.js";
|
|
14
|
+
import { collectingTextLayoutReads, collectTextLayoutReads, noteTextLayoutReads, textMeasurementPaused, withoutTextMeasurement, } from "./text-reads.js";
|
|
15
|
+
import { textSizerFor } from "./text-sizer.js";
|
|
16
|
+
import { applyVariableBindings, bindingsDependOnModes, inheritVariableModes, inheritedVariableModes, variableModesAt, variableModesKey, } from "./variable-modes.js";
|
|
17
|
+
import { isContainerNode, LAYER_PATH_SEPARATOR, ownedChildIds, } from "./types.js";
|
|
18
|
+
import { entryOf, putEntry } from "./id-table.js";
|
|
19
|
+
import { nodesChangedBetween } from "./operations.js";
|
|
20
|
+
import { keptParents } from "./parent-table.js";
|
|
21
|
+
import { tableValues } from "./shared-node-table.js";
|
|
22
|
+
import { slotOwnersOfChanges } from "./slots.js";
|
|
23
|
+
/** How many earlier states of a component's tree a cache keeps. */
|
|
24
|
+
const EARLIER_TREE_STATES = 3;
|
|
25
|
+
/** Gives `componentId` a new state with a version no earlier state had. */
|
|
26
|
+
function setComponentState(cache, componentId, definition, nodes) {
|
|
27
|
+
const previous = cache.versions.get(componentId);
|
|
28
|
+
const version = (cache.lastVersions.get(componentId) ?? 0) + 1;
|
|
29
|
+
cache.lastVersions.set(componentId, version);
|
|
30
|
+
const earlier = previous === undefined
|
|
31
|
+
? []
|
|
32
|
+
: [previous, ...previous.earlier.map(({ version, definition, nodes }) => ({ version, definition, nodes }))]
|
|
33
|
+
.filter((state) => state.nodes !== undefined)
|
|
34
|
+
.slice(0, EARLIER_TREE_STATES);
|
|
35
|
+
cache.versions.set(componentId, { version, definition, nodes, earlier });
|
|
36
|
+
if (nodes !== undefined)
|
|
37
|
+
indexTree(cache, componentId, previous?.nodes, nodes);
|
|
38
|
+
}
|
|
39
|
+
/** Points `treeOwners` at the nodes of `componentId`'s tree now, in place of those it held. */
|
|
40
|
+
function indexTree(cache, componentId, previous, nodes) {
|
|
41
|
+
if (previous === nodes)
|
|
42
|
+
return;
|
|
43
|
+
previous?.forEach((node) => {
|
|
44
|
+
if (cache.treeOwners.get(node.id) === componentId)
|
|
45
|
+
cache.treeOwners.delete(node.id);
|
|
46
|
+
});
|
|
47
|
+
nodes?.forEach((node) => cache.treeOwners.set(node.id, componentId));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Tells `cache` that the slot content of `instanceIds` changed in a node
|
|
51
|
+
* record that is written in place: the content of a slot each owns, or of a
|
|
52
|
+
* slot inside that content. A new record object is read from the edits that
|
|
53
|
+
* made it, but a write to the record the cache already read is not seen, so
|
|
54
|
+
* code that writes slot hosts or content into a working record calls this
|
|
55
|
+
* with every owner up the chain (`slotOwnersOf`).
|
|
56
|
+
* @since 0.37.0
|
|
57
|
+
*/
|
|
58
|
+
export function noteSlotContentChanged(cache, instanceIds) {
|
|
59
|
+
if (!isCacheState(cache))
|
|
60
|
+
return;
|
|
61
|
+
for (const id of instanceIds)
|
|
62
|
+
cache.contentVersions.set(id, (cache.contentVersions.get(id) ?? 0) + 1);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Tells `cache` that the layer trees of `componentIds` changed in a node
|
|
66
|
+
* record that is written in place. The cache notices a new record object by
|
|
67
|
+
* itself, but not a write to the record it already read, so code that lays
|
|
68
|
+
* out a working record calls this after each write to a component's layers.
|
|
69
|
+
*/
|
|
70
|
+
export function noteComponentTreesChanged(cache, componentIds) {
|
|
71
|
+
if (!isCacheState(cache))
|
|
72
|
+
return;
|
|
73
|
+
for (const id of componentIds) {
|
|
74
|
+
const entry = cache.versions.get(id);
|
|
75
|
+
if (entry === undefined)
|
|
76
|
+
continue;
|
|
77
|
+
setComponentState(cache, id, entry.definition, undefined);
|
|
78
|
+
cache.writtenTrees.add(id);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The text measurements read to compute a cached result, when it was
|
|
83
|
+
* computed while they were being collected (`collectTextLayoutReads`).
|
|
84
|
+
*/
|
|
85
|
+
const cachedReads = new WeakMap();
|
|
86
|
+
/**
|
|
87
|
+
* Whether a cached `result` can be given while text reads are collected: it
|
|
88
|
+
* can when they are not, or when it says which ones it read, which it then
|
|
89
|
+
* reads again.
|
|
90
|
+
*/
|
|
91
|
+
function reusable(result) {
|
|
92
|
+
if (!collectingTextLayoutReads())
|
|
93
|
+
return true;
|
|
94
|
+
const reads = cachedReads.get(result);
|
|
95
|
+
if (reads === undefined)
|
|
96
|
+
return false;
|
|
97
|
+
noteTextLayoutReads(reads);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
/** `compute()`, which keeps the text reads of what it computes when they are being collected. */
|
|
101
|
+
function computeCached(compute) {
|
|
102
|
+
if (!collectingTextLayoutReads())
|
|
103
|
+
return compute();
|
|
104
|
+
const { value, keys } = collectTextLayoutReads(compute);
|
|
105
|
+
cachedReads.set(value, keys);
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* `computeCached`, which also keeps the text reads while none are being
|
|
110
|
+
* collected, once the document's sizer holds drawn sizes: a resolved
|
|
111
|
+
* instance then stays current across a text size recorded for another text
|
|
112
|
+
* (`textSizesCurrent`). With no drawn size yet, no key is worked out.
|
|
113
|
+
*/
|
|
114
|
+
function computeKeepingReads(document, compute) {
|
|
115
|
+
if (collectingTextLayoutReads() || textSizerFor(document.id).drawnSizeCount === 0)
|
|
116
|
+
return computeCached(compute);
|
|
117
|
+
const { value, keys } = collectTextLayoutReads(compute);
|
|
118
|
+
cachedReads.set(value, keys);
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Whether a cached instance is current for the drawn text sizes: it is when
|
|
123
|
+
* none was recorded since it was resolved, or when none it read changed
|
|
124
|
+
* since. A result whose reads are not known is current only at its version.
|
|
125
|
+
*/
|
|
126
|
+
function textSizesCurrent(document, entry) {
|
|
127
|
+
const version = getDrawnTextSizesVersion();
|
|
128
|
+
if (entry.textSizesVersion === version)
|
|
129
|
+
return true;
|
|
130
|
+
const reads = cachedReads.get(entry.result);
|
|
131
|
+
if (reads === undefined)
|
|
132
|
+
return false;
|
|
133
|
+
const changed = textSizerFor(document.id).changedKeysSince(entry.textSizesVersion);
|
|
134
|
+
if (changed === undefined)
|
|
135
|
+
return false;
|
|
136
|
+
for (const key of reads)
|
|
137
|
+
if (changed.has(key))
|
|
138
|
+
return false;
|
|
139
|
+
entry.textSizesVersion = version;
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Whether a drawn text size that `result`, an instance `resolveInstance`
|
|
144
|
+
* resolved with a cache, read may have changed since the drawn text sizes
|
|
145
|
+
* version `version` (`getDrawnTextSizesVersion`). True when the sizes it
|
|
146
|
+
* read are not known (resolved without a cache, or before any size was
|
|
147
|
+
* drawn) or the sizer no longer lists its changes that far back; false when
|
|
148
|
+
* no size was recorded since, or none it read changed. A caller that keeps
|
|
149
|
+
* resolved instances re-resolves only those for which this is true.
|
|
150
|
+
* @since 0.37.0
|
|
151
|
+
*/
|
|
152
|
+
export function resolvedTextSizesChangedSince(document, result, version) {
|
|
153
|
+
if (version === getDrawnTextSizesVersion())
|
|
154
|
+
return false;
|
|
155
|
+
const reads = cachedReads.get(result);
|
|
156
|
+
if (reads === undefined)
|
|
157
|
+
return true;
|
|
158
|
+
const changed = textSizerFor(document.id).changedKeysSince(version);
|
|
159
|
+
if (changed === undefined)
|
|
160
|
+
return true;
|
|
161
|
+
if (changed.size < reads.size) {
|
|
162
|
+
for (const key of changed)
|
|
163
|
+
if (reads.has(key))
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
for (const key of reads)
|
|
168
|
+
if (changed.has(key))
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The entry of the node last read for `node`'s ID when that node is another
|
|
175
|
+
* object with the same fields, kept for `node` from then on. A layout pass
|
|
176
|
+
* writes many instances again as new objects with nothing changed.
|
|
177
|
+
*/
|
|
178
|
+
function equalNodeEntries(cache, node) {
|
|
179
|
+
const last = cache.lastInstanceNodes.get(node.id)?.deref();
|
|
180
|
+
if (last === undefined || last === node)
|
|
181
|
+
return undefined;
|
|
182
|
+
const entries = cache.instances.get(last);
|
|
183
|
+
if (entries === undefined || !sameValue(last, node))
|
|
184
|
+
return undefined;
|
|
185
|
+
cache.instances.set(node, entries);
|
|
186
|
+
return entries;
|
|
187
|
+
}
|
|
188
|
+
/** How many results one instance node object keeps, for the documents that read it with different component trees. */
|
|
189
|
+
const INSTANCE_ENTRIES = 3;
|
|
190
|
+
/** Whether two plain data values (as stored in a document) have the same contents. */
|
|
191
|
+
function sameValue(left, right) {
|
|
192
|
+
if (left === right)
|
|
193
|
+
return true;
|
|
194
|
+
if (typeof left !== "object" || typeof right !== "object" || left === null || right === null)
|
|
195
|
+
return false;
|
|
196
|
+
if (Array.isArray(left)) {
|
|
197
|
+
if (!Array.isArray(right) || left.length !== right.length)
|
|
198
|
+
return false;
|
|
199
|
+
for (let index = 0; index < left.length; index += 1)
|
|
200
|
+
if (!sameValue(left[index], right[index]))
|
|
201
|
+
return false;
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (Array.isArray(right))
|
|
205
|
+
return false;
|
|
206
|
+
const leftKeys = Object.keys(left);
|
|
207
|
+
if (leftKeys.length !== Object.keys(right).length)
|
|
208
|
+
return false;
|
|
209
|
+
for (const key of leftKeys) {
|
|
210
|
+
if (!Object.prototype.hasOwnProperty.call(right, key))
|
|
211
|
+
return false;
|
|
212
|
+
if (!sameValue(left[key], right[key]))
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
/** Makes an instance cache. A one-pass consumer may keep component computations
|
|
218
|
+
* without retaining the complete resolved tree of every occurrence.
|
|
219
|
+
*/
|
|
220
|
+
export function createInstanceResolveCache(retainTrees = true) {
|
|
221
|
+
const state = {
|
|
222
|
+
retainTrees,
|
|
223
|
+
lastNodes: undefined,
|
|
224
|
+
lastTheme: undefined,
|
|
225
|
+
lastAssets: undefined,
|
|
226
|
+
lastComponents: undefined,
|
|
227
|
+
lastDocumentId: undefined,
|
|
228
|
+
themeVersion: 0,
|
|
229
|
+
definitionsVersion: 0,
|
|
230
|
+
versions: new Map(),
|
|
231
|
+
lastVersions: new Map(),
|
|
232
|
+
writtenTrees: new Set(),
|
|
233
|
+
lastInstanceNodes: new Map(),
|
|
234
|
+
forgetInstanceNodes: new FinalizationRegistry(({ id, reference }) => {
|
|
235
|
+
if (state.lastInstanceNodes.get(id) === reference)
|
|
236
|
+
state.lastInstanceNodes.delete(id);
|
|
237
|
+
}),
|
|
238
|
+
treeOwners: new Map(),
|
|
239
|
+
workingDocuments: new WeakMap(),
|
|
240
|
+
programs: new Map(),
|
|
241
|
+
expressionCaches: new Map(),
|
|
242
|
+
instances: new WeakMap(),
|
|
243
|
+
components: new Map(),
|
|
244
|
+
lastDocument: undefined,
|
|
245
|
+
contentVersions: new Map(),
|
|
246
|
+
contentEpoch: 0,
|
|
247
|
+
holdsContent: false,
|
|
248
|
+
stats: { hits: 0, misses: 0, evaluations: 0, contentReads: 0, treeReads: 0 },
|
|
249
|
+
clear() {
|
|
250
|
+
state.lastNodes = undefined;
|
|
251
|
+
state.lastComponents = undefined;
|
|
252
|
+
state.lastDocument = undefined;
|
|
253
|
+
state.contentVersions.clear();
|
|
254
|
+
state.holdsContent = false;
|
|
255
|
+
// `lastVersions` stays, so a version number is never given twice and a
|
|
256
|
+
// signature made before the clear cannot match a tree read after it.
|
|
257
|
+
state.versions.clear();
|
|
258
|
+
state.treeOwners.clear();
|
|
259
|
+
state.workingDocuments = new WeakMap();
|
|
260
|
+
state.writtenTrees.clear();
|
|
261
|
+
state.programs.clear();
|
|
262
|
+
state.expressionCaches.clear();
|
|
263
|
+
state.instances = new WeakMap();
|
|
264
|
+
state.lastInstanceNodes.clear();
|
|
265
|
+
state.components.clear();
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
return state;
|
|
269
|
+
}
|
|
270
|
+
const sharedCache = createInstanceResolveCache();
|
|
271
|
+
/**
|
|
272
|
+
* The cache that everything resolving the open document's instances shares:
|
|
273
|
+
* the layout cascade, the page compiler, the accessibility tree, the
|
|
274
|
+
* selection and the text font scan. Entries are kept by instance node object
|
|
275
|
+
* and checked against the document they are read for, so the stored document
|
|
276
|
+
* and the laid-out one each hit their own, and the instances both share are
|
|
277
|
+
* resolved once for all of them. The host clears it when another document is
|
|
278
|
+
* opened.
|
|
279
|
+
* @since 0.37.0
|
|
280
|
+
*/
|
|
281
|
+
export function sharedInstanceResolveCache() {
|
|
282
|
+
return sharedCache;
|
|
283
|
+
}
|
|
284
|
+
/** The node objects of a component's own layer tree, nested instances included but not expanded. */
|
|
285
|
+
function componentTreeNodes(document, rootId) {
|
|
286
|
+
const collected = [];
|
|
287
|
+
const visit = (id) => {
|
|
288
|
+
const node = entryOf(document.nodes, id);
|
|
289
|
+
if (node === undefined)
|
|
290
|
+
return;
|
|
291
|
+
collected.push(node);
|
|
292
|
+
ownedChildIds(node).forEach(visit);
|
|
293
|
+
};
|
|
294
|
+
visit(rootId);
|
|
295
|
+
return collected;
|
|
296
|
+
}
|
|
297
|
+
function sameNodes(left, right) {
|
|
298
|
+
return left !== undefined && left.length === right.length && left.every((node, index) => node === right[index]);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Tells `cache` that `working` holds the node entries of `base` at every ID
|
|
302
|
+
* but `changedNodeIds`, for a document built outside recorded edits (the
|
|
303
|
+
* cascade's view of its working record). The cache then reads again only the
|
|
304
|
+
* component trees holding one of those nodes when it moves between `working`
|
|
305
|
+
* and documents linked to `base`, instead of every tree. Later writes into
|
|
306
|
+
* `working` in place are told with `noteComponentTreesChanged`.
|
|
307
|
+
* @since 0.37.0
|
|
308
|
+
*/
|
|
309
|
+
export function noteWorkingDocument(cache, working, base, changedNodeIds) {
|
|
310
|
+
if (!isCacheState(cache))
|
|
311
|
+
return;
|
|
312
|
+
cache.workingDocuments.set(working, { base, changedNodeIds: new Set(changedNodeIds) });
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* The IDs of every node that may differ between the last document synced and
|
|
316
|
+
* `document`, from the edits that made them (`nodesChangedBetween`) and the
|
|
317
|
+
* working documents callers noted; undefined when nothing links the two.
|
|
318
|
+
*/
|
|
319
|
+
function nodesChangedSinceLast(cache, document) {
|
|
320
|
+
const last = cache.lastDocument;
|
|
321
|
+
if (last === undefined)
|
|
322
|
+
return undefined;
|
|
323
|
+
const direct = nodesChangedBetween(last, document);
|
|
324
|
+
if (direct !== undefined)
|
|
325
|
+
return direct;
|
|
326
|
+
const from = cache.workingDocuments.get(last);
|
|
327
|
+
const to = cache.workingDocuments.get(document);
|
|
328
|
+
if (from === undefined && to === undefined)
|
|
329
|
+
return undefined;
|
|
330
|
+
const between = nodesChangedBetween(from?.base ?? last, to?.base ?? document);
|
|
331
|
+
if (between === undefined)
|
|
332
|
+
return undefined;
|
|
333
|
+
return new Set([...between, ...(from?.changedNodeIds ?? []), ...(to?.changedNodeIds ?? [])]);
|
|
334
|
+
}
|
|
335
|
+
/** Brings the cache's component versions up to date with `document`. */
|
|
336
|
+
function syncCache(cache, document) {
|
|
337
|
+
if (cache.lastTheme !== document.theme || cache.lastAssets !== document.assets ||
|
|
338
|
+
cache.lastDocumentId !== document.id) {
|
|
339
|
+
cache.themeVersion += 1;
|
|
340
|
+
cache.lastTheme = document.theme;
|
|
341
|
+
cache.lastAssets = document.assets;
|
|
342
|
+
cache.lastDocumentId = document.id;
|
|
343
|
+
}
|
|
344
|
+
const nodesChanged = cache.lastNodes !== document.nodes;
|
|
345
|
+
// Trees written in place in the last document are read again in this one.
|
|
346
|
+
const written = new Set(cache.writtenTrees);
|
|
347
|
+
const changedNodeIds = nodesChanged && cache.lastNodes !== undefined ? nodesChangedSinceLast(cache, document) : new Set();
|
|
348
|
+
if (nodesChanged) {
|
|
349
|
+
keepWrittenTrees(cache);
|
|
350
|
+
noteSlotContentChanges(cache, document);
|
|
351
|
+
}
|
|
352
|
+
cache.lastDocument = document;
|
|
353
|
+
if (!nodesChanged && cache.lastComponents === document.components)
|
|
354
|
+
return;
|
|
355
|
+
const firstRead = cache.lastNodes === undefined;
|
|
356
|
+
const componentsChanged = cache.lastComponents !== document.components;
|
|
357
|
+
cache.lastNodes = document.nodes;
|
|
358
|
+
cache.lastComponents = document.components;
|
|
359
|
+
const components = document.components ?? {};
|
|
360
|
+
let definitionsChanged = false;
|
|
361
|
+
if (componentsChanged) {
|
|
362
|
+
for (const [id, entry] of [...cache.versions]) {
|
|
363
|
+
if (entryOf(components, id) === undefined) {
|
|
364
|
+
definitionsChanged = true;
|
|
365
|
+
indexTree(cache, id, entry.nodes, undefined);
|
|
366
|
+
cache.versions.delete(id);
|
|
367
|
+
cache.programs.delete(id);
|
|
368
|
+
cache.expressionCaches.delete(id);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
for (const definition of changedTrees(cache, components, firstRead ? undefined : changedNodeIds, written, componentsChanged)) {
|
|
373
|
+
const nodes = componentTreeNodes(document, definition.rootId);
|
|
374
|
+
cache.stats.treeReads += 1;
|
|
375
|
+
const entry = cache.versions.get(definition.id);
|
|
376
|
+
if (entry?.definition !== definition)
|
|
377
|
+
definitionsChanged = true;
|
|
378
|
+
if (entry === undefined)
|
|
379
|
+
setComponentState(cache, definition.id, definition, nodes);
|
|
380
|
+
else if (entry.definition !== definition || !sameNodes(entry.nodes, nodes)) {
|
|
381
|
+
// A tree seen before takes its version again: the stored document and
|
|
382
|
+
// a laid-out one read in turn do not make each other's entries miss.
|
|
383
|
+
const seen = entry.earlier.find((state) => state.definition === definition && sameNodes(state.nodes, nodes));
|
|
384
|
+
if (seen === undefined)
|
|
385
|
+
setComponentState(cache, definition.id, definition, nodes);
|
|
386
|
+
else {
|
|
387
|
+
cache.versions.set(definition.id, {
|
|
388
|
+
...seen,
|
|
389
|
+
earlier: [entry, ...entry.earlier.filter((state) => state !== seen)]
|
|
390
|
+
.map(({ version, definition, nodes }) => ({ version, definition, nodes }))
|
|
391
|
+
.filter((state) => state.nodes !== undefined)
|
|
392
|
+
.slice(0, EARLIER_TREE_STATES),
|
|
393
|
+
});
|
|
394
|
+
indexTree(cache, definition.id, entry.nodes, seen.nodes);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (definitionsChanged)
|
|
399
|
+
cache.definitionsVersion += 1;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* The components whose trees may differ from the cache's: every one when
|
|
403
|
+
* nothing links the documents (`changedNodeIds` undefined, a load), else
|
|
404
|
+
* those with a new definition, a tree written in place, or a node in
|
|
405
|
+
* `changedNodeIds`. A node added to or removed from a tree changes its
|
|
406
|
+
* parent's children, so the parent names the tree.
|
|
407
|
+
*/
|
|
408
|
+
function changedTrees(cache, components, changedNodeIds, written, componentsChanged) {
|
|
409
|
+
if (changedNodeIds === undefined)
|
|
410
|
+
return Object.values(components);
|
|
411
|
+
const ids = new Set(written);
|
|
412
|
+
for (const id of changedNodeIds) {
|
|
413
|
+
const owner = cache.treeOwners.get(id);
|
|
414
|
+
if (owner !== undefined)
|
|
415
|
+
ids.add(owner);
|
|
416
|
+
}
|
|
417
|
+
if (componentsChanged) {
|
|
418
|
+
for (const definition of Object.values(components)) {
|
|
419
|
+
if (cache.versions.get(definition.id)?.definition !== definition)
|
|
420
|
+
ids.add(definition.id);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
const definitions = [];
|
|
424
|
+
for (const id of ids) {
|
|
425
|
+
const definition = entryOf(components, id);
|
|
426
|
+
if (definition !== undefined)
|
|
427
|
+
definitions.push(definition);
|
|
428
|
+
}
|
|
429
|
+
return definitions;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Reads the trees written in place in the last document's record as they
|
|
433
|
+
* now are, before another document is read, so that a document holding the
|
|
434
|
+
* same node objects (the laid-out document the record became) takes their
|
|
435
|
+
* version instead of a new one.
|
|
436
|
+
*/
|
|
437
|
+
function keepWrittenTrees(cache) {
|
|
438
|
+
const written = cache.lastDocument;
|
|
439
|
+
if (cache.writtenTrees.size === 0 || written === undefined)
|
|
440
|
+
return;
|
|
441
|
+
for (const id of cache.writtenTrees) {
|
|
442
|
+
const entry = cache.versions.get(id);
|
|
443
|
+
if (entry === undefined || entry.nodes !== undefined)
|
|
444
|
+
continue;
|
|
445
|
+
const nodes = componentTreeNodes(written, entry.definition.rootId);
|
|
446
|
+
cache.stats.treeReads += 1;
|
|
447
|
+
cache.versions.set(id, { ...entry, nodes });
|
|
448
|
+
indexTree(cache, id, entry.earlier[0]?.nodes, nodes);
|
|
449
|
+
}
|
|
450
|
+
cache.writtenTrees.clear();
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Raises the content version of every instance whose slot content changed
|
|
454
|
+
* between the last document synced and `document`, read from the edits that
|
|
455
|
+
* made them (`nodesChangedBetween`, undo and redo included) and the slot
|
|
456
|
+
* owners of the nodes they changed: one parent walk per changed node, no
|
|
457
|
+
* walk over content. When no recorded edit links the two (a load), or no
|
|
458
|
+
* parent table is kept for the new nodes (finding owners would build one
|
|
459
|
+
* from every node), the content epoch rises instead and each cached
|
|
460
|
+
* instance checks its content once. Nothing is read while no cached
|
|
461
|
+
* instance drew any content.
|
|
462
|
+
*/
|
|
463
|
+
function noteSlotContentChanges(cache, document) {
|
|
464
|
+
if (!cache.holdsContent)
|
|
465
|
+
return;
|
|
466
|
+
const previous = cache.lastDocument;
|
|
467
|
+
const changed = previous === undefined || keptParents(document.nodes) === undefined
|
|
468
|
+
? undefined
|
|
469
|
+
: nodesChangedBetween(previous, document);
|
|
470
|
+
if (changed === undefined) {
|
|
471
|
+
cache.contentEpoch += 1;
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
for (const owner of slotOwnersOfChanges(document, changed)) {
|
|
475
|
+
cache.contentVersions.set(owner.instanceId, (cache.contentVersions.get(owner.instanceId) ?? 0) + 1);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/** Whether every stored content node of a cached result is still the same object in `document`. */
|
|
479
|
+
function sameContent(cache, document, content) {
|
|
480
|
+
cache.stats.contentReads += content.length;
|
|
481
|
+
return content.every((node) => entryOf(document.nodes, node.id) === node);
|
|
482
|
+
}
|
|
483
|
+
function cacheSignature(cache, componentIds, withTextSizes = true) {
|
|
484
|
+
return [
|
|
485
|
+
`theme@${cache.themeVersion}`,
|
|
486
|
+
// A text size the renderer measured since changes what a resolved text takes.
|
|
487
|
+
...(withTextSizes ? [`text-sizes@${getDrawnTextSizesVersion()}`] : []),
|
|
488
|
+
// A definition added or removed can change what a computed swap shows.
|
|
489
|
+
`definitions@${cache.definitionsVersion}`,
|
|
490
|
+
...componentIds.map((id) => `${id}@${cache.versions.get(id)?.version ?? 0}`),
|
|
491
|
+
].join(",");
|
|
492
|
+
}
|
|
493
|
+
function isCacheState(cache) {
|
|
494
|
+
return cache !== undefined && "versions" in cache;
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* The component's checked expressions, from the cache while its version,
|
|
498
|
+
* every component definition and the theme are unchanged.
|
|
499
|
+
*/
|
|
500
|
+
function componentProgram(document, component, state) {
|
|
501
|
+
if (state === undefined)
|
|
502
|
+
return { program: checkComponentProgram(document, component) };
|
|
503
|
+
syncCache(state, document);
|
|
504
|
+
const signature = [
|
|
505
|
+
state.versions.get(component.id)?.version ?? 0,
|
|
506
|
+
`definitions@${state.definitionsVersion}`,
|
|
507
|
+
`theme@${state.themeVersion}`,
|
|
508
|
+
`text-sizes@${getDrawnTextSizesVersion()}`,
|
|
509
|
+
].join("/");
|
|
510
|
+
const cached = state.programs.get(component.id);
|
|
511
|
+
if (cached !== undefined && cached.signature === signature)
|
|
512
|
+
return cached;
|
|
513
|
+
let expressions = state.expressionCaches.get(component.id);
|
|
514
|
+
if (expressions === undefined) {
|
|
515
|
+
expressions = new ExpressionCache();
|
|
516
|
+
state.expressionCaches.set(component.id, expressions);
|
|
517
|
+
}
|
|
518
|
+
const entry = {
|
|
519
|
+
signature,
|
|
520
|
+
program: checkComponentProgram(document, component, expressions, signature),
|
|
521
|
+
results: new Map(),
|
|
522
|
+
};
|
|
523
|
+
state.programs.set(component.id, entry);
|
|
524
|
+
return entry;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* The component's layers for these property values after conditions and
|
|
528
|
+
* computed fields, shared by every instance with the same values (design
|
|
529
|
+
* 8.6). A component with nothing to compute skips evaluation.
|
|
530
|
+
*/
|
|
531
|
+
function computedComponent(document, component, propertyValues, modes, state) {
|
|
532
|
+
const { program, results } = componentProgram(document, component, state);
|
|
533
|
+
if (program.empty)
|
|
534
|
+
return UNCOMPUTED_COMPONENT;
|
|
535
|
+
// Instances in different variable modes read different values (design 7.3).
|
|
536
|
+
// Computed texts that size themselves keep their stored frames while layout is left out.
|
|
537
|
+
const key = `${JSON.stringify(filledPropertyValues(component, propertyValues))}|${variableModesKey(document.theme, modes)}${textMeasurementPaused() ? "|unmeasured" : ""}`;
|
|
538
|
+
const cached = results?.get(key);
|
|
539
|
+
if (cached !== undefined && reusable(cached))
|
|
540
|
+
return cached;
|
|
541
|
+
if (state !== undefined)
|
|
542
|
+
state.stats.evaluations += 1;
|
|
543
|
+
const computed = computeCached(() => evaluateComponentProgram(document, component, program, propertyValues, modes));
|
|
544
|
+
results?.set(key, computed);
|
|
545
|
+
return computed;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* The expressions and conditions on `componentId`'s layers that do not parse
|
|
549
|
+
* or check, in tree order. Each is left out when the component is resolved:
|
|
550
|
+
* the field shows its stored value, and a condition counts as no condition
|
|
551
|
+
* (design 8.5). After them come the computed values the default property
|
|
552
|
+
* values make invalid, such as a font family with no font in the document,
|
|
553
|
+
* which show their stored value too. A nested component's errors are listed
|
|
554
|
+
* under its own ID.
|
|
555
|
+
* @since 0.25.0
|
|
556
|
+
*/
|
|
557
|
+
export function componentExpressionErrors(document, componentId, options) {
|
|
558
|
+
const component = entryOf(document.components, componentId);
|
|
559
|
+
if (component === undefined) {
|
|
560
|
+
throw new ModelError("INVALID_TREE", `Missing component ${componentId}`, `$.components.${componentId}`);
|
|
561
|
+
}
|
|
562
|
+
const cache = options?.cache;
|
|
563
|
+
const state = isCacheState(cache) ? cache : undefined;
|
|
564
|
+
const { errors } = componentProgram(document, component, state).program;
|
|
565
|
+
// Then what the default property values make invalid (a font family with no font).
|
|
566
|
+
return [...errors, ...computedComponent(document, component, undefined, undefined, state).errors];
|
|
567
|
+
}
|
|
568
|
+
/** Which variable properties a plain override value replaces (design 3.5, rule 2). */
|
|
569
|
+
export const PROPERTIES_DROPPED_BY_FIELD = {
|
|
570
|
+
visible: ["visibility"],
|
|
571
|
+
opacity: ["opacity"],
|
|
572
|
+
fills: ["fillColor"],
|
|
573
|
+
strokes: ["strokeColor", "strokeWidth"],
|
|
574
|
+
};
|
|
575
|
+
/** The variable property each side of a `padding` override replaces: a side drops only its own binding. */
|
|
576
|
+
export const PADDING_PROPERTIES = {
|
|
577
|
+
top: "paddingTop",
|
|
578
|
+
right: "paddingRight",
|
|
579
|
+
bottom: "paddingBottom",
|
|
580
|
+
left: "paddingLeft",
|
|
581
|
+
};
|
|
582
|
+
/** The variable property each `textStyle` override member replaces: a member drops only its own binding. */
|
|
583
|
+
export const TEXT_STYLE_PROPERTIES = {
|
|
584
|
+
fontFamily: "fontFamily",
|
|
585
|
+
fontSize: "fontSize",
|
|
586
|
+
fontWeight: "fontWeight",
|
|
587
|
+
lineHeight: "lineHeight",
|
|
588
|
+
letterSpacing: "letterSpacing",
|
|
589
|
+
color: "textColor",
|
|
590
|
+
};
|
|
591
|
+
/** The bindings a layer ends up with: the component's, less the overridden fields, plus the override's. */
|
|
592
|
+
function resolveBindings(bindings, override) {
|
|
593
|
+
const next = { ...bindings };
|
|
594
|
+
for (const [field, properties] of Object.entries(PROPERTIES_DROPPED_BY_FIELD)) {
|
|
595
|
+
if (override[field] === undefined)
|
|
596
|
+
continue;
|
|
597
|
+
properties.forEach((property) => delete next[property]);
|
|
598
|
+
}
|
|
599
|
+
for (const key of Object.keys(override.textStyle ?? {})) {
|
|
600
|
+
const property = TEXT_STYLE_PROPERTIES[key];
|
|
601
|
+
if (property !== undefined)
|
|
602
|
+
delete next[property];
|
|
603
|
+
}
|
|
604
|
+
for (const key of ["gap", "wrapGap"])
|
|
605
|
+
if (override.layout?.[key] !== undefined)
|
|
606
|
+
delete next[key];
|
|
607
|
+
for (const side of Object.keys(override.padding ?? {})) {
|
|
608
|
+
const property = PADDING_PROPERTIES[side];
|
|
609
|
+
if (property !== undefined)
|
|
610
|
+
delete next[property];
|
|
611
|
+
}
|
|
612
|
+
for (const [property, value] of Object.entries(override.variableBindings ?? {})) {
|
|
613
|
+
if (value === null)
|
|
614
|
+
delete next[property];
|
|
615
|
+
else if (value !== undefined)
|
|
616
|
+
next[property] = value;
|
|
617
|
+
}
|
|
618
|
+
return Object.keys(next).length === 0 ? undefined : next;
|
|
619
|
+
}
|
|
620
|
+
/** `outer` on top of `inner`, field by field; bindings merge per property. */
|
|
621
|
+
function mergeOverrides(inner, outer) {
|
|
622
|
+
if (inner === undefined)
|
|
623
|
+
return outer;
|
|
624
|
+
if (outer === undefined)
|
|
625
|
+
return inner;
|
|
626
|
+
const merged = { ...inner, ...outer };
|
|
627
|
+
if (inner.conditional !== undefined && outer.conditional !== undefined)
|
|
628
|
+
merged.conditional = [...inner.conditional, ...outer.conditional];
|
|
629
|
+
// A text override is either characters or paragraphs; the outer one wins whole.
|
|
630
|
+
if (outer.characters !== undefined)
|
|
631
|
+
delete merged.paragraphs;
|
|
632
|
+
if (outer.paragraphs !== undefined)
|
|
633
|
+
delete merged.characters;
|
|
634
|
+
// Outer paragraphs carry their own alignment, which an inner alignment override would hide.
|
|
635
|
+
if (outer.paragraphs !== undefined && outer.textAlign === undefined)
|
|
636
|
+
delete merged.textAlign;
|
|
637
|
+
if (inner.variableBindings !== undefined && outer.variableBindings !== undefined) {
|
|
638
|
+
merged.variableBindings = { ...inner.variableBindings, ...outer.variableBindings };
|
|
639
|
+
}
|
|
640
|
+
if (inner.propertyValues !== undefined && outer.propertyValues !== undefined) {
|
|
641
|
+
merged.propertyValues = { ...inner.propertyValues, ...outer.propertyValues };
|
|
642
|
+
}
|
|
643
|
+
// Direct formatting is partial: an outer line height must keep the inner font and other members.
|
|
644
|
+
if (inner.textStyle !== undefined && outer.textStyle !== undefined)
|
|
645
|
+
merged.textStyle = { ...inner.textStyle, ...outer.textStyle };
|
|
646
|
+
// Padding is overridden side by side, so an outer side keeps the inner override's other sides.
|
|
647
|
+
if (inner.layout && outer.layout)
|
|
648
|
+
merged.layout = { ...inner.layout, ...outer.layout };
|
|
649
|
+
if (inner.sizing !== undefined && outer.sizing !== undefined)
|
|
650
|
+
merged.sizing = { ...inner.sizing, ...outer.sizing };
|
|
651
|
+
if (inner.padding !== undefined && outer.padding !== undefined)
|
|
652
|
+
merged.padding = { ...inner.padding, ...outer.padding };
|
|
653
|
+
return merged;
|
|
654
|
+
}
|
|
655
|
+
function conditionalOverride(document, component, values, modes, override) {
|
|
656
|
+
if (override?.conditional === undefined)
|
|
657
|
+
return override;
|
|
658
|
+
const { conditional, ...plain } = override;
|
|
659
|
+
let selected;
|
|
660
|
+
const pending = [];
|
|
661
|
+
for (const branch of conditional) {
|
|
662
|
+
if (branch.componentId !== component.id) {
|
|
663
|
+
pending.push({ ...branch, value: conditionalOverride(document, component, values, modes, branch.value) });
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
const checked = checkExpressionSource(branch.condition, EXPRESSION_TYPES.boolean, componentExpressionEnvironment(document, component));
|
|
667
|
+
if (!checked.ok)
|
|
668
|
+
continue;
|
|
669
|
+
const parsed = parseExpression(branch.condition);
|
|
670
|
+
if (!parsed.ok)
|
|
671
|
+
continue;
|
|
672
|
+
const result = evaluateExpression(parsed.expression, componentValueEnvironment(document, component, values, undefined, modes));
|
|
673
|
+
if (result.kind === "boolean" && result.value)
|
|
674
|
+
selected = mergeOverrides(selected, conditionalOverride(document, component, values, modes, branch.value));
|
|
675
|
+
}
|
|
676
|
+
const result = mergeOverrides(selected, plain);
|
|
677
|
+
return pending.length === 0 ? result : { ...result, conditional: [...result.conditional ?? [], ...pending] };
|
|
678
|
+
}
|
|
679
|
+
function conditionalInstanceOverrides(document, component, values, modes, node) {
|
|
680
|
+
if (!Object.values(node.overrides ?? {}).some((override) => override.conditional !== undefined))
|
|
681
|
+
return node;
|
|
682
|
+
return { ...node, overrides: Object.fromEntries(Object.entries(node.overrides).map(([path, override]) => [path, conditionalOverride(document, component, values, modes, override)])) };
|
|
683
|
+
}
|
|
684
|
+
function overriddenParagraphs(node, override) {
|
|
685
|
+
let paragraphs;
|
|
686
|
+
if (override.characters !== undefined)
|
|
687
|
+
paragraphs = paragraphsFromCharacters(override.characters, node.paragraphs);
|
|
688
|
+
else if (override.paragraphs !== undefined)
|
|
689
|
+
paragraphs = override.paragraphs;
|
|
690
|
+
if (override.textStyleId !== undefined || override.textStyle !== undefined)
|
|
691
|
+
paragraphs = restyledParagraphs(paragraphs ?? node.paragraphs, override);
|
|
692
|
+
const align = override.textAlign;
|
|
693
|
+
if (align === undefined)
|
|
694
|
+
return paragraphs;
|
|
695
|
+
return (paragraphs ?? node.paragraphs).map((paragraph) => ({ ...paragraph, align }));
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* `paragraphs` as an instance draws them under an override: its `textStyleId` (null takes the style away) and its
|
|
699
|
+
* `textStyle` over every run, the `textStyle` over each run's own overrides. List markers are left as they are.
|
|
700
|
+
*/
|
|
701
|
+
export function restyledParagraphs(paragraphs, override) {
|
|
702
|
+
if (override.textStyleId === undefined && override.textStyle === undefined)
|
|
703
|
+
return paragraphs;
|
|
704
|
+
const restyle = (run) => {
|
|
705
|
+
let next = run;
|
|
706
|
+
if (override.textStyleId === null) {
|
|
707
|
+
const { styleId: _styleId, ...rest } = next;
|
|
708
|
+
next = rest;
|
|
709
|
+
}
|
|
710
|
+
else if (override.textStyleId !== undefined)
|
|
711
|
+
next = { ...next, styleId: override.textStyleId };
|
|
712
|
+
if (override.textStyle !== undefined)
|
|
713
|
+
next = { ...next, overrides: { ...next.overrides, ...override.textStyle } };
|
|
714
|
+
return next;
|
|
715
|
+
};
|
|
716
|
+
return paragraphs.map((paragraph) => ({ ...paragraph, runs: paragraph.runs.map(restyle) }));
|
|
717
|
+
}
|
|
718
|
+
function overriddenStyle(style, override) {
|
|
719
|
+
const next = { ...style };
|
|
720
|
+
let changed = false;
|
|
721
|
+
if (override.styleId === null) {
|
|
722
|
+
delete next.styleId;
|
|
723
|
+
changed = true;
|
|
724
|
+
}
|
|
725
|
+
else if (override.styleId !== undefined) {
|
|
726
|
+
next.styleId = override.styleId;
|
|
727
|
+
changed = true;
|
|
728
|
+
}
|
|
729
|
+
for (const field of ["fills", "strokes", "shadows", "layerBlur", "backgroundBlur", "cornerSmoothing"]) {
|
|
730
|
+
if (override[field] === undefined)
|
|
731
|
+
continue;
|
|
732
|
+
next[field] = override[field];
|
|
733
|
+
changed = true;
|
|
734
|
+
}
|
|
735
|
+
if (!changed)
|
|
736
|
+
return style;
|
|
737
|
+
return next;
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* The component node with one layer's override applied, in component
|
|
741
|
+
* coordinates. An overridden text layer that auto-resizes is measured again.
|
|
742
|
+
*/
|
|
743
|
+
/** The override without `"fill"` sizing, which has no effect under the component root's path. */
|
|
744
|
+
function withoutFillSizing(override) {
|
|
745
|
+
const sizing = override?.sizing;
|
|
746
|
+
if (sizing === undefined || (sizing.width !== "fill" && sizing.height !== "fill"))
|
|
747
|
+
return override;
|
|
748
|
+
const kept = { ...(sizing.width === "fill" ? {} : { width: sizing.width }), ...(sizing.height === "fill" ? {} : { height: sizing.height }) };
|
|
749
|
+
const { sizing: _, ...rest } = override;
|
|
750
|
+
return Object.keys(kept).length === 0 ? rest : { ...rest, sizing: kept };
|
|
751
|
+
}
|
|
752
|
+
function applyOverride(document, node, override) {
|
|
753
|
+
if (override === undefined)
|
|
754
|
+
return node;
|
|
755
|
+
if (node.type === "frame" && override.layout) {
|
|
756
|
+
const layout = override.layout.kind === "absolute" ? { kind: "absolute" } :
|
|
757
|
+
{ gap: 0, align: "start", justify: "start", padding: { top: 0, right: 0, bottom: 0, left: 0 },
|
|
758
|
+
widthSizing: "fixed", heightSizing: "fixed", ...node.layout, ...override.layout };
|
|
759
|
+
node = { ...node, layout };
|
|
760
|
+
}
|
|
761
|
+
const base = { ...node };
|
|
762
|
+
if (node.type === "frame" && override.clipContent !== undefined)
|
|
763
|
+
base.clipContent = override.clipContent;
|
|
764
|
+
if (override.visible !== undefined)
|
|
765
|
+
base.visible = override.visible;
|
|
766
|
+
if (override.opacity !== undefined)
|
|
767
|
+
base.opacity = override.opacity;
|
|
768
|
+
if (override.name !== undefined)
|
|
769
|
+
base.name = override.name;
|
|
770
|
+
const bindings = resolveBindings(node.variableBindings, override);
|
|
771
|
+
if (bindings === undefined)
|
|
772
|
+
delete base.variableBindings;
|
|
773
|
+
else
|
|
774
|
+
base.variableBindings = bindings;
|
|
775
|
+
if ("style" in node || node.type === "frame") {
|
|
776
|
+
const style = overriddenStyle(node.style, override);
|
|
777
|
+
if (style !== node.style)
|
|
778
|
+
base.style = style;
|
|
779
|
+
}
|
|
780
|
+
if (node.type === "text") {
|
|
781
|
+
const paragraphs = overriddenParagraphs(node, override);
|
|
782
|
+
if (paragraphs !== undefined) {
|
|
783
|
+
base.paragraphs = paragraphs;
|
|
784
|
+
if (node.autoResize !== "none")
|
|
785
|
+
base.frame = getDerivedAutoSizedTextFrame(document, node, paragraphs);
|
|
786
|
+
}
|
|
787
|
+
for (const key of ["maxLines", "truncate"]) {
|
|
788
|
+
if (override[key] === null)
|
|
789
|
+
delete base[key];
|
|
790
|
+
else if (override[key] !== undefined)
|
|
791
|
+
base[key] = override[key];
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
if (node.type === "image" && override.imageAssetId !== undefined)
|
|
795
|
+
base.assetId = override.imageAssetId;
|
|
796
|
+
if (node.type === "instance" && override.propertyValues !== undefined) {
|
|
797
|
+
base.propertyValues = { ...node.propertyValues, ...override.propertyValues };
|
|
798
|
+
}
|
|
799
|
+
if (node.type === "frame" && override.sizing !== undefined) {
|
|
800
|
+
const { width, height } = override.sizing;
|
|
801
|
+
// Fill is the layer's place in its parent's auto layout, so its own Hug on that axis keeps the size it is given.
|
|
802
|
+
if (node.layout.kind !== "absolute") {
|
|
803
|
+
base.layout = {
|
|
804
|
+
...node.layout,
|
|
805
|
+
widthSizing: width === "fill" ? (node.layout.widthSizing === "hug" ? "fixed" : node.layout.widthSizing) : width ?? node.layout.widthSizing,
|
|
806
|
+
heightSizing: height === "fill" ? (node.layout.heightSizing === "hug" ? "fixed" : node.layout.heightSizing) : height ?? node.layout.heightSizing,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
if (width === "fill" || height === "fill") {
|
|
810
|
+
base.resizing = { ...node.resizing, ...(width === "fill" ? { width } : {}), ...(height === "fill" ? { height } : {}) };
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
if (node.type === "frame" && override.padding !== undefined && node.layout.kind !== "absolute") {
|
|
814
|
+
const layout = (base.layout ?? node.layout);
|
|
815
|
+
base.layout = { ...layout, padding: { ...layout.padding, ...override.padding } };
|
|
816
|
+
}
|
|
817
|
+
return base;
|
|
818
|
+
}
|
|
819
|
+
function invalidTree(message, path) {
|
|
820
|
+
return new ModelError("INVALID_TREE", message, path);
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Builds the frame standing for an instance: the instance's placement and
|
|
824
|
+
* `NodeBase` fields with the component root's layout, style and clipping.
|
|
825
|
+
* Hug on an axis the instance fills becomes Fixed, since the parent gives
|
|
826
|
+
* the size.
|
|
827
|
+
*/
|
|
828
|
+
function instanceFrame(instance, root, virtualId, childIds) {
|
|
829
|
+
const { type: _type, componentId: _componentId, overrides: _overrides, adjustments: _adjustments, ...placement } = instance;
|
|
830
|
+
const layout = root.layout.kind === "absolute" ? root.layout : {
|
|
831
|
+
...root.layout,
|
|
832
|
+
widthSizing: instance.resizing?.width === "fill" ? "fixed" : root.layout.widthSizing,
|
|
833
|
+
heightSizing: instance.resizing?.height === "fill" ? "fixed" : root.layout.heightSizing,
|
|
834
|
+
};
|
|
835
|
+
const bindings = { ...root.variableBindings, ...instance.variableBindings };
|
|
836
|
+
return {
|
|
837
|
+
...placement,
|
|
838
|
+
id: virtualId,
|
|
839
|
+
type: "frame",
|
|
840
|
+
children: childIds,
|
|
841
|
+
layout,
|
|
842
|
+
...(root.style === undefined ? {} : { style: root.style }),
|
|
843
|
+
...(root.clipContent === undefined ? {} : { clipContent: root.clipContent }),
|
|
844
|
+
...(Object.keys(bindings).length === 0 ? {} : { variableBindings: bindings }),
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Whether an instance was resized away from its component's size, which a
|
|
849
|
+
* swap keeps (as in Figma). An axis on which the component root hugs, after
|
|
850
|
+
* the instance's own sizing override and Fill, does not count: its size
|
|
851
|
+
* follows the content, so a longer label is not a resize.
|
|
852
|
+
*/
|
|
853
|
+
export function resizedAwayFromComponent(document, instance) {
|
|
854
|
+
const root = entryOf(document.nodes, entryOf(document.components, instance.componentId)?.rootId ?? "");
|
|
855
|
+
if (root === undefined || !("frame" in root))
|
|
856
|
+
return false;
|
|
857
|
+
const sizing = withoutFillSizing(entryOf(instance.overrides, layerPathKey([root.id])))?.sizing;
|
|
858
|
+
const hugs = (axis) => root.type === "frame" && root.layout.kind !== "absolute"
|
|
859
|
+
&& instance.resizing?.[axis] !== "fill"
|
|
860
|
+
&& (sizing?.[axis] ?? (axis === "width" ? root.layout.widthSizing : root.layout.heightSizing)) === "hug";
|
|
861
|
+
return ["width", "height"].some((axis) => !hugs(axis) && Math.abs(root.frame[axis] - instance.frame[axis]) > 0.01);
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* The nested instance after an outer override swaps its component. As in
|
|
865
|
+
* Figma, an instance resized away from its old component's size keeps its
|
|
866
|
+
* size on both axes (Figma stores the size as one override); one at that
|
|
867
|
+
* size, or differing only on axes it hugs, takes the new root's natural
|
|
868
|
+
* size. A hugging axis is laid out anyway.
|
|
869
|
+
*/
|
|
870
|
+
function swappedInstance(document, stored, componentId) {
|
|
871
|
+
if (componentId === undefined || componentId === stored.componentId)
|
|
872
|
+
return stored;
|
|
873
|
+
const root = entryOf(document.nodes, entryOf(document.components, componentId)?.rootId ?? "");
|
|
874
|
+
if (root?.type !== "frame")
|
|
875
|
+
return { ...stored, componentId };
|
|
876
|
+
if (resizedAwayFromComponent(document, stored))
|
|
877
|
+
return { ...stored, componentId };
|
|
878
|
+
return {
|
|
879
|
+
...stored,
|
|
880
|
+
componentId,
|
|
881
|
+
frame: { ...stored.frame, width: root.frame.width, height: root.frame.height },
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* A nested instance whose expression computes its component: the swap
|
|
886
|
+
* takes the target root's size first, then the axes the instance also
|
|
887
|
+
* computes keep their computed size (computed fields share one level).
|
|
888
|
+
*/
|
|
889
|
+
function computedSwappedInstance(document, component, state, stored, base, componentId) {
|
|
890
|
+
const swapped = swappedInstance(document, { ...base, componentId: stored.componentId }, componentId);
|
|
891
|
+
if (swapped.componentId === stored.componentId || (stored.expressions?.width === undefined && stored.expressions?.height === undefined)) {
|
|
892
|
+
return swapped;
|
|
893
|
+
}
|
|
894
|
+
const fields = componentProgram(document, component, state).program.layers.get(stored.id)?.fields ?? [];
|
|
895
|
+
const computed = (axis) => fields.some(({ field }) => field === axis);
|
|
896
|
+
return {
|
|
897
|
+
...swapped,
|
|
898
|
+
frame: {
|
|
899
|
+
...swapped.frame,
|
|
900
|
+
width: computed("width") ? base.frame.width : swapped.frame.width,
|
|
901
|
+
height: computed("height") ? base.frame.height : swapped.frame.height,
|
|
902
|
+
},
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* The component a nested instance shows when an expression computes
|
|
907
|
+
* `computedId` for it: the stored one when `computedId` is missing or its
|
|
908
|
+
* tree contains, directly or through nesting, one of the `enclosing`
|
|
909
|
+
* components, which would nest a component in itself (design 8.6).
|
|
910
|
+
* A swap refused for nesting adds the components whose trees decided it to
|
|
911
|
+
* `refused`, so a cached result is dropped when one of them changes.
|
|
912
|
+
*/
|
|
913
|
+
function computedSwapTarget(document, storedId, computedId, enclosing, refused) {
|
|
914
|
+
if (computedId === storedId || entryOf(document.components, computedId) === undefined)
|
|
915
|
+
return storedId;
|
|
916
|
+
if (enclosing.includes(computedId))
|
|
917
|
+
return storedId;
|
|
918
|
+
const nested = componentsNestedIn(nestingGraphOf(document), computedId);
|
|
919
|
+
if (!enclosing.some((id) => nested.has(id)))
|
|
920
|
+
return computedId;
|
|
921
|
+
for (const id of [computedId, ...nested])
|
|
922
|
+
if (!refused.includes(id))
|
|
923
|
+
refused.push(id);
|
|
924
|
+
return storedId;
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Expands `instance` (the outer instance, or a nested one whose outer
|
|
928
|
+
* overrides are already applied to it) into `expansion` under `virtualId`.
|
|
929
|
+
* `prefix` is this instance's path inside the enclosing component (empty for
|
|
930
|
+
* the outer one) and `outer` looks up overrides stored there; `absolutePrefix`
|
|
931
|
+
* is its path from the outer instance, for virtual IDs. `selfOverride` is
|
|
932
|
+
* the outer override stored under this nested instance's own key: it is
|
|
933
|
+
* what `editTargetFor` writes for the nested instance's paints and sizing,
|
|
934
|
+
* so it applies to the nested component root, above the nested node's own
|
|
935
|
+
* entry for that root.
|
|
936
|
+
*/
|
|
937
|
+
function expandInstance(expansion, instance, virtualId, prefix, absolutePrefix, outer, selfOverride, depth, stack, inherited, scope) {
|
|
938
|
+
const { document } = expansion;
|
|
939
|
+
const component = entryOf(document.components, instance.componentId);
|
|
940
|
+
if (component === undefined) {
|
|
941
|
+
throw invalidTree(`Missing component ${instance.componentId}`, `$.nodes.${instance.id}.componentId`);
|
|
942
|
+
}
|
|
943
|
+
if (stack.includes(component.id)) {
|
|
944
|
+
throw invalidTree(`Component ${component.id} contains itself`, `$.components.${component.id}`);
|
|
945
|
+
}
|
|
946
|
+
const root = entryOf(document.nodes, component.rootId);
|
|
947
|
+
if (root?.type !== "frame") {
|
|
948
|
+
throw invalidTree(`Component ${component.id} has no root frame`, `$.components.${component.id}.rootId`);
|
|
949
|
+
}
|
|
950
|
+
if (!expansion.componentIds.includes(component.id))
|
|
951
|
+
expansion.componentIds.push(component.id);
|
|
952
|
+
for (const ids of expansion.consulted)
|
|
953
|
+
ids.add(component.id);
|
|
954
|
+
const lookup = (pathInside) => mergeOverrides(conditionalOverride(document, component, instance.propertyValues, instanceModes, entryOf(instance.overrides, layerPathKey(pathInside))), conditionalOverride(document, component, instance.propertyValues, instanceModes, outer([...prefix, ...pathInside])));
|
|
955
|
+
/** For text only: whether this instance or one around it overrides a layer at or under `pathInside` in a way that can change text. */
|
|
956
|
+
const overridden = (pathInside) => (overridesReachText(document, instance.overrides, pathInside) || scope.overriddenAround?.([...prefix, ...pathInside]) === true);
|
|
957
|
+
const nextStack = [...stack, component.id];
|
|
958
|
+
// The component root's modes, then the instance's own (design 7.3).
|
|
959
|
+
const instanceModes = inheritVariableModes(inheritVariableModes(inherited, root.variableModes), instance.variableModes);
|
|
960
|
+
// Conditions, then computed fields (design 8.6); overrides go on top below.
|
|
961
|
+
const computed = computedComponent(document, component, instance.propertyValues, instanceModes, expansion.cache);
|
|
962
|
+
expansion.expressionErrors.push(...computed.errors);
|
|
963
|
+
const shownChildren = (children) => (computed.leftOut.size === 0 ? children : children.filter((childId) => !computed.leftOut.has(childId)));
|
|
964
|
+
// Slots, design 3: an instance around this one fills a slot first (the
|
|
965
|
+
// outermost first), then this instance's own node, else the slot frame
|
|
966
|
+
// shows its default content.
|
|
967
|
+
const slotPropertyIds = new Set((component.properties ?? []).filter((property) => property.kind === "slot").map((property) => property.id));
|
|
968
|
+
const ownHost = (relativePath, propertyId) => {
|
|
969
|
+
const hostId = entryOf(entryOf(instance.slots, layerPathKey(relativePath)), propertyId);
|
|
970
|
+
const host = hostId === undefined ? undefined : entryOf(document.nodes, hostId);
|
|
971
|
+
return host?.type === "frame" && host.slotHost === true ? { host, expand: scope.expandOwnContent } : undefined;
|
|
972
|
+
};
|
|
973
|
+
const findHost = (relativePath, propertyId) => (scope.parentHosts?.([...prefix, ...relativePath], propertyId) ?? ownHost(relativePath, propertyId));
|
|
974
|
+
// Layers of an instance inside slot content have paths relative to that instance.
|
|
975
|
+
const pathInstance = scope.layerInstanceId === expansion.instanceId ? {} : { pathInstanceId: scope.layerInstanceId };
|
|
976
|
+
/** The slots a shown frame already took the host of: a second frame bound to one shows its default content. */
|
|
977
|
+
const claimed = new Set();
|
|
978
|
+
const expandNode = (nodeId, pathInside, nodeDepth, parentModes,
|
|
979
|
+
// A layer above this one in the component is hidden, so a slot frame here shows no content.
|
|
980
|
+
ancestorHidden = false) => {
|
|
981
|
+
const stored = entryOf(document.nodes, nodeId);
|
|
982
|
+
if (stored === undefined)
|
|
983
|
+
throw invalidTree(`Missing node ${nodeId}`, `$.nodes.${nodeId}`);
|
|
984
|
+
// Text only: a layer that draws no text and holds no layers is left out.
|
|
985
|
+
if (expansion.textOnly && drawsNoText(stored))
|
|
986
|
+
return virtualNodeId(scope.layerInstanceId, [...absolutePrefix, ...pathInside]);
|
|
987
|
+
const evaluated = computed.nodes.get(nodeId) ?? stored;
|
|
988
|
+
const base = evaluated.type === "instance" ? conditionalInstanceOverrides(document, component, instance.propertyValues, instanceModes, evaluated) : evaluated;
|
|
989
|
+
const path = [...absolutePrefix, ...pathInside];
|
|
990
|
+
const layerId = virtualNodeId(scope.layerInstanceId, path);
|
|
991
|
+
const override = lookup(pathInside);
|
|
992
|
+
if (stored.type === "instance" && base.type === "instance") {
|
|
993
|
+
// A computed component that is missing or would nest a component in
|
|
994
|
+
// itself is ignored, as an instance swap property default may be.
|
|
995
|
+
const computedTarget = computedSwapTarget(document, stored.componentId, base.componentId, nextStack, expansion.refusedIds);
|
|
996
|
+
if (computedTarget !== base.componentId && entryOf(document.components, base.componentId) !== undefined) {
|
|
997
|
+
expansion.swapsRefused += 1;
|
|
998
|
+
}
|
|
999
|
+
if (expansion.consulted.length > 0 && base.componentId !== stored.componentId) {
|
|
1000
|
+
for (const id of [base.componentId, ...componentsNestedIn(nestingGraphOf(document), base.componentId)]) {
|
|
1001
|
+
for (const ids of expansion.consulted)
|
|
1002
|
+
ids.add(id);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
// An override swap sits above computed values, so it resets both axes.
|
|
1006
|
+
const swapped = override?.componentId === undefined
|
|
1007
|
+
? computedSwappedInstance(document, component, expansion.cache, stored, base, computedTarget)
|
|
1008
|
+
: swappedInstance(document, { ...base, componentId: stored.componentId }, override.componentId);
|
|
1009
|
+
const nested = applyOverride(document, swapped, override);
|
|
1010
|
+
const slotsAround = scope.slotsAround === true || instance.slots !== undefined;
|
|
1011
|
+
// Text only: a nested instance that no slot content or override around it reaches lists the same text as
|
|
1012
|
+
// every other with the same setup, so its texts are kept for the document and taken from there.
|
|
1013
|
+
const shared = expansion.textOnly && scope.layerInstanceId === expansion.instanceId && !slotsAround &&
|
|
1014
|
+
nested.slots === undefined && !overridden(pathInside)
|
|
1015
|
+
? sharedNestedTexts(document, nested, parentModes)
|
|
1016
|
+
: undefined;
|
|
1017
|
+
const known = shared?.texts.get(shared.key);
|
|
1018
|
+
// Kept texts hold while no component around this instance is one they consulted.
|
|
1019
|
+
if (known !== undefined && !nextStack.some((id) => known.consulted.has(id))) {
|
|
1020
|
+
for (const ids of expansion.consulted)
|
|
1021
|
+
for (const id of known.consulted)
|
|
1022
|
+
ids.add(id);
|
|
1023
|
+
for (const text of known.texts) {
|
|
1024
|
+
const textPath = [...path, ...text.path];
|
|
1025
|
+
const id = virtualNodeId(scope.layerInstanceId, textPath);
|
|
1026
|
+
expansion.texts.push({ virtualId: id, sourceNodeId: text.sourceNodeId, path: textPath, node: { ...text.node, id } });
|
|
1027
|
+
}
|
|
1028
|
+
return layerId;
|
|
1029
|
+
}
|
|
1030
|
+
const first = expansion.texts.length;
|
|
1031
|
+
const refusedBefore = expansion.swapsRefused;
|
|
1032
|
+
const consulted = new Set();
|
|
1033
|
+
if (shared !== undefined)
|
|
1034
|
+
expansion.consulted.push(consulted);
|
|
1035
|
+
try {
|
|
1036
|
+
expandInstance(expansion, nested, layerId, pathInside, path, lookup, override, nodeDepth, nextStack, parentModes, {
|
|
1037
|
+
layerInstanceId: scope.layerInstanceId,
|
|
1038
|
+
parentHosts: findHost,
|
|
1039
|
+
// The hosts a nested instance node lists belong to this component, so
|
|
1040
|
+
// their content is expanded as this component's layers.
|
|
1041
|
+
expandOwnContent: (host, contentDepth, modes) => shownChildren(host.children)
|
|
1042
|
+
.map((childId) => expandNode(childId, [childId], contentDepth, modes)),
|
|
1043
|
+
slotsAround,
|
|
1044
|
+
overriddenAround: overridden,
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
finally {
|
|
1048
|
+
if (shared !== undefined)
|
|
1049
|
+
expansion.consulted.pop();
|
|
1050
|
+
}
|
|
1051
|
+
// A swap refused under this instance depends on the components around it, so its texts are not kept.
|
|
1052
|
+
if (expansion.swapsRefused === refusedBefore)
|
|
1053
|
+
shared?.texts.set(shared.key, {
|
|
1054
|
+
texts: expansion.texts.slice(first).map((text) => ({ ...text, path: text.path.slice(path.length) })),
|
|
1055
|
+
consulted,
|
|
1056
|
+
});
|
|
1057
|
+
return layerId;
|
|
1058
|
+
}
|
|
1059
|
+
// Bound fields read their variables in the layer's modes, after
|
|
1060
|
+
// overrides, which may rebind them.
|
|
1061
|
+
const layerModes = inheritVariableModes(parentModes, base.variableModes);
|
|
1062
|
+
const unbound = applyOverride(document, base, override);
|
|
1063
|
+
const node = applyVariableBindings(document, unbound, layerModes);
|
|
1064
|
+
let childIds = [];
|
|
1065
|
+
let placed = { ...node, id: layerId };
|
|
1066
|
+
let slot;
|
|
1067
|
+
let filled;
|
|
1068
|
+
if (stored.type === "frame" && stored.slot !== undefined && slotPropertyIds.has(stored.slot)) {
|
|
1069
|
+
// A frame hidden, or under a hidden layer, by a property binding, a computed field or an override shows no content; a
|
|
1070
|
+
// later shown frame takes it.
|
|
1071
|
+
filled = claimed.has(stored.slot) || ancestorHidden || !node.visible ? undefined : findHost([], stored.slot);
|
|
1072
|
+
if (filled !== undefined)
|
|
1073
|
+
claimed.add(stored.slot);
|
|
1074
|
+
slot = {
|
|
1075
|
+
propertyId: stored.slot,
|
|
1076
|
+
pathKey: layerPathKey(absolutePrefix),
|
|
1077
|
+
...(filled === undefined ? {} : { hostId: filled.host.id }),
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
if (isContainerNode(node)) {
|
|
1081
|
+
if (filled === undefined) {
|
|
1082
|
+
const hidden = ancestorHidden || !node.visible;
|
|
1083
|
+
childIds = shownChildren(node.children).map((childId) => expandNode(childId, [childId], nodeDepth + 1, layerModes, hidden));
|
|
1084
|
+
}
|
|
1085
|
+
else {
|
|
1086
|
+
// The slot frame keeps its own layout, sizing and paints, and lays out the host's content.
|
|
1087
|
+
const hostId = filled.host.id;
|
|
1088
|
+
childIds = filled.expand(filled.host, nodeDepth + 1, layerModes);
|
|
1089
|
+
childIds.forEach((childId) => {
|
|
1090
|
+
const meta = expansion.meta.get(childId);
|
|
1091
|
+
if (meta !== undefined)
|
|
1092
|
+
expansion.meta.set(childId, { ...meta, slotHostId: hostId });
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
placed = { ...placed, children: childIds };
|
|
1096
|
+
}
|
|
1097
|
+
putEntry(expansion.nodes, layerId, placed);
|
|
1098
|
+
if (expansion.textOnly && (placed.type === "text" || placed.type === "code")) {
|
|
1099
|
+
expansion.texts.push({ virtualId: layerId, sourceNodeId: nodeId, path, node: placed, ...pathInstance });
|
|
1100
|
+
}
|
|
1101
|
+
// Without layout, frames stay as stored.
|
|
1102
|
+
if (expansion.layout && placed.type === "group" && unbound.type === "group") {
|
|
1103
|
+
// A mode that resizes the group scales its layers, as a group resize does.
|
|
1104
|
+
scaleGroupChildren(expansion.nodes, placed, unbound.frame);
|
|
1105
|
+
fitGroup(expansion.nodes, placed);
|
|
1106
|
+
}
|
|
1107
|
+
if (expansion.layout && placed.type === "frame" && stored.type === "frame" && filled === undefined) {
|
|
1108
|
+
// A computed size or a mode that resizes the frame: children follow
|
|
1109
|
+
// their constraints from the stored size, deepest frames first
|
|
1110
|
+
// (`expandNode` returns after its children). Slot content is placed
|
|
1111
|
+
// in its host, which the cascade keeps at the slot frame's size.
|
|
1112
|
+
constrainChildren(expansion.nodes, layerId, base.type === "frame" ? base.constraintSize ?? stored.frame : stored.frame, placed.frame).forEach((child) => {
|
|
1113
|
+
putEntry(expansion.nodes, child.id, child);
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
expansion.meta.set(layerId, {
|
|
1117
|
+
sourceNodeId: nodeId,
|
|
1118
|
+
path,
|
|
1119
|
+
childIds,
|
|
1120
|
+
inheritedVariableModes: parentModes,
|
|
1121
|
+
variableModes: layerModes,
|
|
1122
|
+
...(slot === undefined ? {} : { slot }),
|
|
1123
|
+
...pathInstance,
|
|
1124
|
+
});
|
|
1125
|
+
expansion.depth.set(layerId, nodeDepth);
|
|
1126
|
+
return layerId;
|
|
1127
|
+
};
|
|
1128
|
+
const rootOverride = withoutFillSizing(mergeOverrides(lookup([root.id]), selfOverride));
|
|
1129
|
+
const rootNode = applyOverride(document, computed.nodes.get(root.id) ?? root, rootOverride);
|
|
1130
|
+
const childIds = shownChildren(rootNode.children).map((childId) => expandNode(childId, [childId], depth + 1, instanceModes));
|
|
1131
|
+
putEntry(expansion.nodes, virtualId, applyVariableBindings(document, instanceFrame(instance, rootNode, virtualId, childIds), instanceModes));
|
|
1132
|
+
if (expansion.layout) {
|
|
1133
|
+
// A resized instance, or one whose size a mode changes: children follow
|
|
1134
|
+
// their constraints from the stored root size, which their positions are
|
|
1135
|
+
// relative to, whatever size an expression computes for the root. In an
|
|
1136
|
+
// auto layout root only the absolute children follow; the layout places
|
|
1137
|
+
// the others.
|
|
1138
|
+
const bound = entryOf(expansion.nodes, virtualId);
|
|
1139
|
+
constrainChildren(expansion.nodes, virtualId, rootNode.constraintSize ?? root.frame, bound.frame).forEach((node) => {
|
|
1140
|
+
putEntry(expansion.nodes, node.id, node);
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
expansion.meta.set(virtualId, {
|
|
1144
|
+
sourceNodeId: root.id,
|
|
1145
|
+
path: absolutePrefix.length === 0 ? [root.id] : absolutePrefix,
|
|
1146
|
+
childIds,
|
|
1147
|
+
instance: {
|
|
1148
|
+
instanceId: instance.id,
|
|
1149
|
+
componentId: component.id,
|
|
1150
|
+
node: instance,
|
|
1151
|
+
...(instance.propertyValues === undefined ? {} : { propertyValues: instance.propertyValues }),
|
|
1152
|
+
},
|
|
1153
|
+
inheritedVariableModes: inherited,
|
|
1154
|
+
variableModes: instanceModes,
|
|
1155
|
+
...pathInstance,
|
|
1156
|
+
});
|
|
1157
|
+
expansion.depth.set(virtualId, depth);
|
|
1158
|
+
}
|
|
1159
|
+
/** Per document, the texts of nested instances by setup (`sharedNestedTexts`), with paths from the nested instance. */
|
|
1160
|
+
const nestedTextsByDocument = new WeakMap();
|
|
1161
|
+
/**
|
|
1162
|
+
* Where the texts of a nested instance are kept for text only: by its
|
|
1163
|
+
* component, property values, overrides and variable modes, those it
|
|
1164
|
+
* inherits too. Kept texts are taken only under components that are none of
|
|
1165
|
+
* those they consulted.
|
|
1166
|
+
*/
|
|
1167
|
+
function sharedNestedTexts(document, nested, inherited) {
|
|
1168
|
+
let texts = nestedTextsByDocument.get(document);
|
|
1169
|
+
if (texts === undefined) {
|
|
1170
|
+
texts = new Map();
|
|
1171
|
+
nestedTextsByDocument.set(document, texts);
|
|
1172
|
+
}
|
|
1173
|
+
const key = JSON.stringify([
|
|
1174
|
+
nested.componentId,
|
|
1175
|
+
nested.propertyValues ?? null,
|
|
1176
|
+
nested.overrides ?? null,
|
|
1177
|
+
nested.variableModes ?? null,
|
|
1178
|
+
variableModesKey(document.theme, inherited),
|
|
1179
|
+
]);
|
|
1180
|
+
return { texts, key };
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Whether `overrides` holds an override at or under `pathInside` that can
|
|
1184
|
+
* change a text: one on a text, code or instance layer, or the visibility or
|
|
1185
|
+
* bindings of a container (which decide whether a slot frame shows content).
|
|
1186
|
+
* The fills, strokes, shadows, sizing and the like of other layers cannot.
|
|
1187
|
+
*/
|
|
1188
|
+
function overridesReachText(document, overrides, pathInside) {
|
|
1189
|
+
if (overrides === undefined)
|
|
1190
|
+
return false;
|
|
1191
|
+
const key = layerPathKey(pathInside);
|
|
1192
|
+
for (const [overrideKey, override] of Object.entries(overrides)) {
|
|
1193
|
+
if (overrideKey !== key && !overrideKey.startsWith(`${key}${LAYER_PATH_SEPARATOR}`))
|
|
1194
|
+
continue;
|
|
1195
|
+
const layer = entryOf(document.nodes, overrideKey.slice(overrideKey.lastIndexOf(LAYER_PATH_SEPARATOR) + 1));
|
|
1196
|
+
if (layer === undefined || drawsNoText(layer))
|
|
1197
|
+
continue;
|
|
1198
|
+
if (!isContainerNode(layer) || override.visible !== undefined || override.variableBindings !== undefined)
|
|
1199
|
+
return true;
|
|
1200
|
+
}
|
|
1201
|
+
return false;
|
|
1202
|
+
}
|
|
1203
|
+
/** True for a layer that is not text or code and holds no layers that could be: `instanceTextLayers` leaves it out. */
|
|
1204
|
+
function drawsNoText(node) {
|
|
1205
|
+
return node.type !== "text" && node.type !== "code" && node.type !== "instance" && !isContainerNode(node);
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* A `ContentExpander` for the hosts a stored instance lists: each content
|
|
1209
|
+
* layer is the stored node under its stored ID (`expandStored`).
|
|
1210
|
+
*/
|
|
1211
|
+
function storedContent(expansion, stack) {
|
|
1212
|
+
return (host, depth, modes) => {
|
|
1213
|
+
expansion.content.push(host);
|
|
1214
|
+
return host.children.map((childId) => expandStored(expansion, childId, depth, modes, stack));
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Expands the stored node `nodeId`, a layer of a stored instance's slot
|
|
1219
|
+
* content, under its stored ID: its bound fields read their variables in
|
|
1220
|
+
* the modes it inherits, a stored instance is expanded by `expandInstance`
|
|
1221
|
+
* under its own ID (with its own slots), and the layers it owns
|
|
1222
|
+
* (`ownedChildIds`) follow. `stack` holds the components around the
|
|
1223
|
+
* outermost instance, as `expandInstance` takes them.
|
|
1224
|
+
*/
|
|
1225
|
+
function expandStored(expansion, nodeId, depth, parentModes, stack) {
|
|
1226
|
+
const { document } = expansion;
|
|
1227
|
+
const stored = entryOf(document.nodes, nodeId);
|
|
1228
|
+
if (stored === undefined)
|
|
1229
|
+
throw invalidTree(`Missing node ${nodeId}`, `$.nodes.${nodeId}`);
|
|
1230
|
+
expansion.content.push(stored);
|
|
1231
|
+
if (expansion.textOnly && drawsNoText(stored))
|
|
1232
|
+
return nodeId;
|
|
1233
|
+
if (stored.type === "instance") {
|
|
1234
|
+
expandInstance(expansion, stored, nodeId, [], [], () => undefined, undefined, depth, stack, parentModes, {
|
|
1235
|
+
layerInstanceId: nodeId,
|
|
1236
|
+
parentHosts: undefined,
|
|
1237
|
+
expandOwnContent: storedContent(expansion, stack),
|
|
1238
|
+
});
|
|
1239
|
+
return nodeId;
|
|
1240
|
+
}
|
|
1241
|
+
const layerModes = inheritVariableModes(parentModes, stored.variableModes);
|
|
1242
|
+
const node = applyVariableBindings(document, stored, layerModes);
|
|
1243
|
+
let childIds = [];
|
|
1244
|
+
let placed = node;
|
|
1245
|
+
if (isContainerNode(node)) {
|
|
1246
|
+
childIds = ownedChildIds(node).map((childId) => expandStored(expansion, childId, depth + 1, layerModes, stack));
|
|
1247
|
+
placed = { ...node, children: childIds };
|
|
1248
|
+
}
|
|
1249
|
+
putEntry(expansion.nodes, nodeId, placed);
|
|
1250
|
+
if (expansion.textOnly && (placed.type === "text" || placed.type === "code")) {
|
|
1251
|
+
expansion.texts.push({ virtualId: nodeId, sourceNodeId: nodeId, path: [nodeId], node: placed });
|
|
1252
|
+
}
|
|
1253
|
+
// Without layout, frames stay as stored.
|
|
1254
|
+
if (expansion.layout && placed.type === "group" && stored.type === "group") {
|
|
1255
|
+
scaleGroupChildren(expansion.nodes, placed, stored.frame);
|
|
1256
|
+
fitGroup(expansion.nodes, placed);
|
|
1257
|
+
}
|
|
1258
|
+
if (expansion.layout && placed.type === "frame" && stored.type === "frame") {
|
|
1259
|
+
constrainChildren(expansion.nodes, nodeId, stored.frame, placed.frame).forEach((child) => {
|
|
1260
|
+
putEntry(expansion.nodes, child.id, child);
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
expansion.meta.set(nodeId, {
|
|
1264
|
+
sourceNodeId: nodeId,
|
|
1265
|
+
path: [nodeId],
|
|
1266
|
+
childIds,
|
|
1267
|
+
inheritedVariableModes: parentModes,
|
|
1268
|
+
variableModes: layerModes,
|
|
1269
|
+
});
|
|
1270
|
+
expansion.depth.set(nodeId, depth);
|
|
1271
|
+
return nodeId;
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Fits a group to its children again, as a mode or an override may have
|
|
1275
|
+
* resized one of them; the stored frame is the union for the stored values.
|
|
1276
|
+
*/
|
|
1277
|
+
function fitGroup(nodes, group) {
|
|
1278
|
+
const fitted = fitGroupToChildren(nodes, group);
|
|
1279
|
+
if (fitted === null)
|
|
1280
|
+
return;
|
|
1281
|
+
putEntry(nodes, group.id, fitted.group);
|
|
1282
|
+
fitted.children.forEach((child) => { putEntry(nodes, child.id, child); });
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* Scales a group's layers from the group's size `from` to its current size,
|
|
1286
|
+
* when a variable mode gave it another size than the stored one.
|
|
1287
|
+
*/
|
|
1288
|
+
function scaleGroupChildren(nodes, group, from) {
|
|
1289
|
+
if (from.width === group.frame.width && from.height === group.frame.height)
|
|
1290
|
+
return;
|
|
1291
|
+
const scaleX = from.width === 0 ? 1 : group.frame.width / from.width;
|
|
1292
|
+
const scaleY = from.height === 0 ? 1 : group.frame.height / from.height;
|
|
1293
|
+
group.children.forEach((childId) => {
|
|
1294
|
+
const child = entryOf(nodes, childId);
|
|
1295
|
+
if (child === undefined)
|
|
1296
|
+
throw invalidTree(`Missing node ${childId}`, `$.nodes.${childId}`);
|
|
1297
|
+
scaleLayerInParent(nodes, child, scaleX, scaleY).forEach((node) => { putEntry(nodes, node.id, node); });
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Lays out every auto layout frame and fits every group, deepest first, so
|
|
1302
|
+
* a group fits the final size of an auto layout frame inside it and an
|
|
1303
|
+
* auto layout frame around a group measures the group's final size.
|
|
1304
|
+
*/
|
|
1305
|
+
function settleLayout(nodes, depth) {
|
|
1306
|
+
tableValues(nodes)
|
|
1307
|
+
.filter((node) => node.type === "group" || (node.type === "frame" && node.layout.kind !== "absolute"))
|
|
1308
|
+
.sort((left, right) => (depth.get(right.id) ?? 0) - (depth.get(left.id) ?? 0))
|
|
1309
|
+
.forEach(({ id }) => {
|
|
1310
|
+
const node = entryOf(nodes, id);
|
|
1311
|
+
if (node.type === "group")
|
|
1312
|
+
fitGroup(nodes, node);
|
|
1313
|
+
else
|
|
1314
|
+
layoutFrameTree(nodes, id);
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
/** The most times one frame is laid out again for text whose height changed with its width. */
|
|
1318
|
+
const MAX_TEXT_PASSES = 3;
|
|
1319
|
+
/**
|
|
1320
|
+
* Lays out every auto layout frame of the expansion and refits every group
|
|
1321
|
+
* to its children, deepest first, so a frame measures the final size of the
|
|
1322
|
+
* groups inside it. Height-auto text that a frame narrows or widens (Fill,
|
|
1323
|
+
* stretch) is measured again at its new width, and the layers between it
|
|
1324
|
+
* and that frame are settled before the frame is laid out again.
|
|
1325
|
+
*/
|
|
1326
|
+
function layoutExpansion(expansion) {
|
|
1327
|
+
const { nodes, depth } = expansion;
|
|
1328
|
+
const byDepth = (left, right) => (depth.get(right) ?? 0) - (depth.get(left) ?? 0);
|
|
1329
|
+
const parents = new Map();
|
|
1330
|
+
expansion.meta.forEach((meta, id) => meta.childIds.forEach((childId) => parents.set(childId, id)));
|
|
1331
|
+
const measuredWidth = new Map();
|
|
1332
|
+
tableValues(nodes).forEach((node) => {
|
|
1333
|
+
if (node.type === "text" && node.autoResize === "height")
|
|
1334
|
+
measuredWidth.set(node.id, node.frame.width);
|
|
1335
|
+
});
|
|
1336
|
+
const settle = (id) => {
|
|
1337
|
+
const node = entryOf(nodes, id);
|
|
1338
|
+
if (node?.type === "frame" && node.layout.kind !== "absolute") {
|
|
1339
|
+
layoutFrameTree(nodes, id);
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
if (node?.type !== "group")
|
|
1343
|
+
return;
|
|
1344
|
+
const fitted = fitGroupToChildren(nodes, node);
|
|
1345
|
+
if (fitted === null)
|
|
1346
|
+
return;
|
|
1347
|
+
putEntry(nodes, id, fitted.group);
|
|
1348
|
+
fitted.children.forEach((child) => { putEntry(nodes, child.id, child); });
|
|
1349
|
+
};
|
|
1350
|
+
/** Measures height-auto text whose width layout changed, and returns the IDs measured. */
|
|
1351
|
+
const remeasure = () => {
|
|
1352
|
+
const measured = [];
|
|
1353
|
+
measuredWidth.forEach((width, id) => {
|
|
1354
|
+
const node = entryOf(nodes, id);
|
|
1355
|
+
if (node?.type !== "text" || node.frame.width === width)
|
|
1356
|
+
return;
|
|
1357
|
+
measuredWidth.set(id, node.frame.width);
|
|
1358
|
+
const frame = getDerivedAutoSizedTextFrame(expansion.document, node);
|
|
1359
|
+
if (frame.height === node.frame.height)
|
|
1360
|
+
return;
|
|
1361
|
+
putEntry(nodes, id, { ...node, frame });
|
|
1362
|
+
measured.push(id);
|
|
1363
|
+
});
|
|
1364
|
+
return measured;
|
|
1365
|
+
};
|
|
1366
|
+
const ids = tableValues(nodes)
|
|
1367
|
+
.filter((node) => node.type === "group" || (node.type === "frame" && node.layout.kind !== "absolute"))
|
|
1368
|
+
.map((node) => node.id)
|
|
1369
|
+
.sort(byDepth);
|
|
1370
|
+
ids.forEach((id) => {
|
|
1371
|
+
if (entryOf(nodes, id)?.type === "group") {
|
|
1372
|
+
settle(id);
|
|
1373
|
+
return;
|
|
1374
|
+
}
|
|
1375
|
+
for (let pass = 0; pass < MAX_TEXT_PASSES; pass += 1) {
|
|
1376
|
+
layoutFrameTree(nodes, id);
|
|
1377
|
+
const measured = remeasure();
|
|
1378
|
+
if (measured.length === 0)
|
|
1379
|
+
return;
|
|
1380
|
+
const between = new Set();
|
|
1381
|
+
measured.forEach((textId) => {
|
|
1382
|
+
for (let current = parents.get(textId); current !== undefined && current !== id; current = parents.get(current)) {
|
|
1383
|
+
between.add(current);
|
|
1384
|
+
}
|
|
1385
|
+
});
|
|
1386
|
+
[...between].sort(byDepth).forEach(settle);
|
|
1387
|
+
}
|
|
1388
|
+
layoutFrameTree(nodes, id);
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
function buildLayers(expansion, layers, id) {
|
|
1392
|
+
const meta = expansion.meta.get(id);
|
|
1393
|
+
const node = entryOf(expansion.nodes, id);
|
|
1394
|
+
if (meta === undefined || node === undefined)
|
|
1395
|
+
throw invalidTree(`Missing resolved layer ${id}`, `$.nodes.${id}`);
|
|
1396
|
+
const layer = {
|
|
1397
|
+
virtualId: id,
|
|
1398
|
+
sourceNodeId: meta.sourceNodeId,
|
|
1399
|
+
path: meta.path,
|
|
1400
|
+
node,
|
|
1401
|
+
children: meta.childIds.map((childId) => buildLayers(expansion, layers, childId)),
|
|
1402
|
+
...(meta.instance === undefined ? {} : { instance: meta.instance }),
|
|
1403
|
+
...(meta.inheritedVariableModes === undefined ? {} : { inheritedVariableModes: meta.inheritedVariableModes }),
|
|
1404
|
+
...(meta.variableModes === undefined ? {} : { variableModes: meta.variableModes }),
|
|
1405
|
+
...(meta.slot === undefined ? {} : { slot: meta.slot }),
|
|
1406
|
+
...(meta.slotHostId === undefined ? {} : { slotHostId: meta.slotHostId }),
|
|
1407
|
+
...(meta.pathInstanceId === undefined ? {} : { pathInstanceId: meta.pathInstanceId }),
|
|
1408
|
+
};
|
|
1409
|
+
layers.set(id, layer);
|
|
1410
|
+
return layer;
|
|
1411
|
+
}
|
|
1412
|
+
function resolveInstanceUncached(document, instance, cache, modes, enclosing = [], layout = true) {
|
|
1413
|
+
const expansion = expandStoredInstance(document, instance, cache, modes, enclosing, false, layout);
|
|
1414
|
+
if (layout)
|
|
1415
|
+
layoutExpansion(expansion);
|
|
1416
|
+
const layers = new Map();
|
|
1417
|
+
const root = buildLayers(expansion, layers, instance.id);
|
|
1418
|
+
const result = { instanceId: instance.id, componentId: instance.componentId, root,
|
|
1419
|
+
layers,
|
|
1420
|
+
componentIds: expansion.componentIds,
|
|
1421
|
+
expressionErrors: expansion.expressionErrors,
|
|
1422
|
+
};
|
|
1423
|
+
refusedSwapDependencies.set(result, expansion.refusedIds);
|
|
1424
|
+
if (expansion.content.length > 0)
|
|
1425
|
+
drawnContent.set(result, expansion.content);
|
|
1426
|
+
return result;
|
|
1427
|
+
}
|
|
1428
|
+
/** The flattened tree of a stored instance, before layout. */
|
|
1429
|
+
function expandStoredInstance(document, instance, cache, modes, enclosing, textOnly, layout = !textOnly) {
|
|
1430
|
+
const expansion = {
|
|
1431
|
+
document,
|
|
1432
|
+
instanceId: instance.id,
|
|
1433
|
+
nodes: {},
|
|
1434
|
+
meta: new Map(),
|
|
1435
|
+
componentIds: [],
|
|
1436
|
+
refusedIds: [],
|
|
1437
|
+
swapsRefused: 0,
|
|
1438
|
+
expressionErrors: [],
|
|
1439
|
+
depth: new Map(),
|
|
1440
|
+
cache,
|
|
1441
|
+
content: [],
|
|
1442
|
+
textOnly,
|
|
1443
|
+
layout,
|
|
1444
|
+
texts: [],
|
|
1445
|
+
consulted: [],
|
|
1446
|
+
};
|
|
1447
|
+
expandInstance(expansion, instance, instance.id, [], [], () => undefined, undefined, 0, enclosing, modes, {
|
|
1448
|
+
layerInstanceId: instance.id,
|
|
1449
|
+
parentHosts: undefined,
|
|
1450
|
+
expandOwnContent: storedContent(expansion, enclosing),
|
|
1451
|
+
});
|
|
1452
|
+
return expansion;
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* The text and code layers of the stored instance `instanceId`, in the
|
|
1456
|
+
* order `resolveInstance` lists its layers, with the words, styles and fonts
|
|
1457
|
+
* it gives them, but without layout: no auto layout, constraint or group
|
|
1458
|
+
* fitting runs and no text is measured, so each keeps the frame its
|
|
1459
|
+
* component stores, an overridden or computed text that sizes itself too.
|
|
1460
|
+
* The instance's other layers are not built. For readers of words and
|
|
1461
|
+
* fonts, such as a document's font scan, that must not measure text. A
|
|
1462
|
+
* cache may be shared with `resolveInstance`; the components computed
|
|
1463
|
+
* without measuring are kept apart in it.
|
|
1464
|
+
*
|
|
1465
|
+
* Throws as `resolveInstance` does.
|
|
1466
|
+
* @since 0.37.0
|
|
1467
|
+
*/
|
|
1468
|
+
export function instanceTextLayers(document, instanceId, options) {
|
|
1469
|
+
const node = entryOf(document.nodes, instanceId);
|
|
1470
|
+
if (node?.type !== "instance") {
|
|
1471
|
+
throw new ModelError("INVALID_TREE", `Node ${instanceId} is not an instance`, `$.nodes.${instanceId}`);
|
|
1472
|
+
}
|
|
1473
|
+
const cache = isCacheState(options?.cache) ? options.cache : undefined;
|
|
1474
|
+
if (cache !== undefined)
|
|
1475
|
+
syncCache(cache, document);
|
|
1476
|
+
const modes = options?.variableModes ?? inheritedVariableModes(document, instanceId);
|
|
1477
|
+
const expansion = withoutTextMeasurement(() => expandStoredInstance(document, node, cache, modes, [], true));
|
|
1478
|
+
return expansion.texts;
|
|
1479
|
+
}
|
|
1480
|
+
/** Per resolved instance that draws slot content of stored instances: the stored hosts and content nodes it read. */
|
|
1481
|
+
const drawnContent = new WeakMap();
|
|
1482
|
+
/** Per resolved instance: the components whose trees refused a computed swap in it. */
|
|
1483
|
+
const refusedSwapDependencies = new WeakMap();
|
|
1484
|
+
/**
|
|
1485
|
+
* Every component a resolved instance's rendering goes through, each once: those it draws (its own, then each
|
|
1486
|
+
* nested one after overrides, property values, their defaults and swaps) and those whose trees refused a computed
|
|
1487
|
+
* swap in it. A change to any of them can change what the instance shows.
|
|
1488
|
+
*/
|
|
1489
|
+
export function resolvedInstanceComponents(result) {
|
|
1490
|
+
const ids = [...result.componentIds];
|
|
1491
|
+
for (const id of refusedSwapDependencies.get(result) ?? [])
|
|
1492
|
+
if (!ids.includes(id))
|
|
1493
|
+
ids.push(id);
|
|
1494
|
+
return ids;
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Derives the layers an instance shows: virtual IDs, overrides applied by
|
|
1498
|
+
* precedence, nested instances expanded, and the component root's auto
|
|
1499
|
+
* layout run for the instance's size and overridden text. A hugging axis
|
|
1500
|
+
* takes its natural size, whatever the stored frame says; a fixed axis and
|
|
1501
|
+
* an axis the instance fills keep the stored size.
|
|
1502
|
+
*
|
|
1503
|
+
* With component properties (design 8.6, since 0.25.0), each component in
|
|
1504
|
+
* the instance is resolved in this order: conditions leave layers out (with
|
|
1505
|
+
* their sublayers, taking no space in auto layout), computed fields are
|
|
1506
|
+
* evaluated for the instance's property values and written into the model
|
|
1507
|
+
* fields, overrides go on top (an override wins over a computed value), and
|
|
1508
|
+
* then layout runs. A nested instance's property values are its outer
|
|
1509
|
+
* override, else its expression over the outer properties, else its stored
|
|
1510
|
+
* value, else the nested component's default. An expression that does not
|
|
1511
|
+
* check is skipped (see `componentExpressionErrors`). A resized instance
|
|
1512
|
+
* places the root's children by their constraints (`constrainChildren`):
|
|
1513
|
+
* every child of a root without auto layout, and the absolute children of
|
|
1514
|
+
* an auto layout root.
|
|
1515
|
+
*
|
|
1516
|
+
* Throws a `ModelError` with code `INVALID_TREE` when `instanceId` is not
|
|
1517
|
+
* an instance node or its component is missing.
|
|
1518
|
+
* @since 0.23.0
|
|
1519
|
+
*/
|
|
1520
|
+
export function resolveInstance(document, instanceId, options) {
|
|
1521
|
+
const node = entryOf(document.nodes, instanceId);
|
|
1522
|
+
if (node?.type !== "instance") {
|
|
1523
|
+
throw new ModelError("INVALID_TREE", `Node ${instanceId} is not an instance`, `$.nodes.${instanceId}`);
|
|
1524
|
+
}
|
|
1525
|
+
const cache = options?.cache;
|
|
1526
|
+
const modes = options?.variableModes ?? inheritedVariableModes(document, instanceId);
|
|
1527
|
+
if (!isCacheState(cache))
|
|
1528
|
+
return resolveInstanceUncached(document, node, undefined, modes, [], options?.layout);
|
|
1529
|
+
syncCache(cache, document);
|
|
1530
|
+
if (!cache.retainTrees || options?.layout === false)
|
|
1531
|
+
return resolveInstanceUncached(document, node, cache, modes, [], options?.layout);
|
|
1532
|
+
const modesKey = `modes:${variableModesKey(document.theme, modes)}`;
|
|
1533
|
+
const entries = cache.instances.get(node) ?? equalNodeEntries(cache, node) ?? [];
|
|
1534
|
+
if (cache.lastInstanceNodes.get(instanceId)?.deref() !== node) {
|
|
1535
|
+
const reference = new WeakRef(node);
|
|
1536
|
+
cache.lastInstanceNodes.set(instanceId, reference);
|
|
1537
|
+
cache.forgetInstanceNodes.register(node, { id: instanceId, reference });
|
|
1538
|
+
}
|
|
1539
|
+
const contentVersion = cache.contentVersions.get(instanceId) ?? 0;
|
|
1540
|
+
const entry = entries.find((candidate) => candidate.signature === `${cacheSignature(cache, candidate.dependencyIds, false)},${modesKey}`);
|
|
1541
|
+
// Slot content edits raise the content version, so a hit reads no content;
|
|
1542
|
+
// only after a load is the content compared, once per cached instance.
|
|
1543
|
+
if (entry !== undefined && entry.contentVersion === contentVersion
|
|
1544
|
+
&& textSizesCurrent(document, entry)
|
|
1545
|
+
&& (entry.contentEpoch === cache.contentEpoch || sameContent(cache, document, entry.content))
|
|
1546
|
+
&& reusable(entry.result)) {
|
|
1547
|
+
entry.contentEpoch = cache.contentEpoch;
|
|
1548
|
+
cache.stats.hits += 1;
|
|
1549
|
+
return entry.result;
|
|
1550
|
+
}
|
|
1551
|
+
cache.stats.misses += 1;
|
|
1552
|
+
const textSizesVersion = getDrawnTextSizesVersion();
|
|
1553
|
+
const result = computeKeepingReads(document, () => resolveInstanceUncached(document, node, cache, modes));
|
|
1554
|
+
const dependencyIds = resolvedInstanceComponents(result);
|
|
1555
|
+
const content = drawnContent.get(result) ?? [];
|
|
1556
|
+
if (content.length > 0)
|
|
1557
|
+
cache.holdsContent = true;
|
|
1558
|
+
const signature = `${cacheSignature(cache, dependencyIds, false)},${modesKey}`;
|
|
1559
|
+
cache.instances.set(node, [
|
|
1560
|
+
{ signature, dependencyIds, result, contentVersion, content, contentEpoch: cache.contentEpoch, textSizesVersion },
|
|
1561
|
+
...entries.filter((earlier) => earlier !== entry && earlier.signature !== signature),
|
|
1562
|
+
].slice(0, INSTANCE_ENTRIES));
|
|
1563
|
+
return result;
|
|
1564
|
+
}
|
|
1565
|
+
function resolveComponentUncached(document, componentId, options, componentIds) {
|
|
1566
|
+
const component = entryOf(document.components, componentId);
|
|
1567
|
+
if (component === undefined) {
|
|
1568
|
+
throw new ModelError("INVALID_TREE", `Missing component ${componentId}`, `$.components.${componentId}`);
|
|
1569
|
+
}
|
|
1570
|
+
componentIds.push(componentId);
|
|
1571
|
+
const state = isCacheState(options?.cache) ? options.cache : undefined;
|
|
1572
|
+
const layout = options?.layout !== false;
|
|
1573
|
+
const resolveNested = (node) => {
|
|
1574
|
+
const resolved = node === entryOf(document.nodes, node.id)
|
|
1575
|
+
? resolveInstance(document, node.id, { ...(options?.cache && { cache: options.cache }), layout })
|
|
1576
|
+
: resolveInstanceUncached(document, node, state, inheritedVariableModes(document, node.id), [componentId], layout);
|
|
1577
|
+
resolvedInstanceComponents(resolved).forEach((id) => { if (!componentIds.includes(id))
|
|
1578
|
+
componentIds.push(id); });
|
|
1579
|
+
return resolved;
|
|
1580
|
+
};
|
|
1581
|
+
// Stored values are written for the active modes; a layer under a mode
|
|
1582
|
+
// chosen inside the component reads its variables again.
|
|
1583
|
+
const rebound = (node) => {
|
|
1584
|
+
const modes = variableModesAt(document, node.id);
|
|
1585
|
+
return bindingsDependOnModes(document.theme, node, modes) ? applyVariableBindings(document, node, modes) : node;
|
|
1586
|
+
};
|
|
1587
|
+
const computed = computedComponent(document, component, undefined, variableModesAt(document, component.rootId), state);
|
|
1588
|
+
// A layer under a mode other than the active one may change size, so the
|
|
1589
|
+
// stored layout no longer holds and the component is laid out again.
|
|
1590
|
+
const underModes = componentTreeNodes(document, component.rootId)
|
|
1591
|
+
.some((node) => variableModesKey(document.theme, variableModesAt(document, node.id)) !== "");
|
|
1592
|
+
if (computed === UNCOMPUTED_COMPONENT && !underModes) {
|
|
1593
|
+
const visit = (nodeId) => {
|
|
1594
|
+
const stored = entryOf(document.nodes, nodeId);
|
|
1595
|
+
if (stored === undefined)
|
|
1596
|
+
throw invalidTree(`Missing node ${nodeId}`, `$.nodes.${nodeId}`);
|
|
1597
|
+
if (stored.type === "instance")
|
|
1598
|
+
return resolveNested(conditionalInstanceOverrides(document, component, undefined, options?.variableModes, stored)).root;
|
|
1599
|
+
const node = rebound(stored);
|
|
1600
|
+
return {
|
|
1601
|
+
virtualId: nodeId,
|
|
1602
|
+
sourceNodeId: nodeId,
|
|
1603
|
+
path: [nodeId],
|
|
1604
|
+
node,
|
|
1605
|
+
children: isContainerNode(node) ? node.children.map(visit) : [],
|
|
1606
|
+
};
|
|
1607
|
+
};
|
|
1608
|
+
return visit(component.rootId);
|
|
1609
|
+
}
|
|
1610
|
+
// The component at its defaults: conditions and computed fields, then its
|
|
1611
|
+
// auto layout, with each nested instance at its resolved size.
|
|
1612
|
+
const nodes = {};
|
|
1613
|
+
const depths = new Map();
|
|
1614
|
+
const nested = new Map();
|
|
1615
|
+
const collect = (nodeId, depth) => {
|
|
1616
|
+
const stored = entryOf(document.nodes, nodeId);
|
|
1617
|
+
if (stored === undefined)
|
|
1618
|
+
throw invalidTree(`Missing node ${nodeId}`, `$.nodes.${nodeId}`);
|
|
1619
|
+
let node = computed.nodes.get(nodeId) ?? stored;
|
|
1620
|
+
if (node.type === "instance" && stored.type === "instance") {
|
|
1621
|
+
const target = computedSwapTarget(document, stored.componentId, node.componentId, [componentId], componentIds);
|
|
1622
|
+
node = computedSwappedInstance(document, component, state, stored, node, target);
|
|
1623
|
+
node = conditionalInstanceOverrides(document, component, undefined, options?.variableModes, node);
|
|
1624
|
+
const resolved = resolveNested(node);
|
|
1625
|
+
nested.set(nodeId, resolved);
|
|
1626
|
+
const { width, height } = resolved.root.node.frame;
|
|
1627
|
+
if (width !== node.frame.width || height !== node.frame.height)
|
|
1628
|
+
node = { ...node, frame: { ...node.frame, width, height } };
|
|
1629
|
+
}
|
|
1630
|
+
else if (isContainerNode(node)) {
|
|
1631
|
+
const children = node.children.filter((childId) => !computed.leftOut.has(childId));
|
|
1632
|
+
node = { ...rebound(node), children };
|
|
1633
|
+
children.forEach((childId) => collect(childId, depth + 1));
|
|
1634
|
+
if (layout && node.type === "frame" && stored.type === "frame") {
|
|
1635
|
+
// A computed size: children follow their constraints from the stored size.
|
|
1636
|
+
putEntry(nodes, nodeId, node);
|
|
1637
|
+
constrainChildren(nodes, nodeId, node.constraintSize ?? stored.frame, node.frame).forEach((child) => {
|
|
1638
|
+
putEntry(nodes, child.id, child);
|
|
1639
|
+
});
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
if (node.type !== "instance" && !isContainerNode(node))
|
|
1643
|
+
node = rebound(node);
|
|
1644
|
+
putEntry(nodes, nodeId, node);
|
|
1645
|
+
if (layout && node.type === "group")
|
|
1646
|
+
fitGroup(nodes, node);
|
|
1647
|
+
depths.set(nodeId, depth);
|
|
1648
|
+
};
|
|
1649
|
+
collect(component.rootId, 0);
|
|
1650
|
+
if (layout)
|
|
1651
|
+
settleLayout(nodes, depths);
|
|
1652
|
+
const visit = (nodeId) => {
|
|
1653
|
+
const node = entryOf(nodes, nodeId);
|
|
1654
|
+
if (node.type === "instance") {
|
|
1655
|
+
const first = nested.get(nodeId);
|
|
1656
|
+
const same = first !== undefined && sameFrame(first.root.node.frame, node.frame);
|
|
1657
|
+
return (same ? first : resolveNested(node)).root;
|
|
1658
|
+
}
|
|
1659
|
+
return {
|
|
1660
|
+
virtualId: nodeId,
|
|
1661
|
+
sourceNodeId: nodeId,
|
|
1662
|
+
path: [nodeId],
|
|
1663
|
+
node,
|
|
1664
|
+
children: isContainerNode(node) ? node.children.map(visit) : [],
|
|
1665
|
+
};
|
|
1666
|
+
};
|
|
1667
|
+
return visit(component.rootId);
|
|
1668
|
+
}
|
|
1669
|
+
function sameFrame(left, right) {
|
|
1670
|
+
return left.x === right.x && left.y === right.y && left.width === right.width && left.height === right.height;
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Derives the component view: the component's own layer tree at its natural
|
|
1674
|
+
* size with no overrides, as `ResolvedLayer`s whose `virtualId`s are the
|
|
1675
|
+
* stored node IDs. Isolation mode draws it; a nested instance inside it is
|
|
1676
|
+
* expanded as in `resolveInstance`, under its own stored ID. A component
|
|
1677
|
+
* with properties or expressions shows its default values (design 8.9):
|
|
1678
|
+
* conditions and computed fields are evaluated with the defaults, and its
|
|
1679
|
+
* auto layout frames are laid out again.
|
|
1680
|
+
* @since 0.23.0
|
|
1681
|
+
*/
|
|
1682
|
+
export function resolveComponent(document, componentId, options) {
|
|
1683
|
+
const cache = options?.cache;
|
|
1684
|
+
if (!isCacheState(cache))
|
|
1685
|
+
return resolveComponentUncached(document, componentId, options, []);
|
|
1686
|
+
syncCache(cache, document);
|
|
1687
|
+
const drawn = [];
|
|
1688
|
+
if (!cache.retainTrees || options?.layout === false)
|
|
1689
|
+
return resolveComponentUncached(document, componentId, options, drawn);
|
|
1690
|
+
const cached = cache.components.get(componentId);
|
|
1691
|
+
if (cached !== undefined && cached.signature === cacheSignature(cache, cached.componentIds) && reusable(cached.result)) {
|
|
1692
|
+
cache.stats.hits += 1;
|
|
1693
|
+
return cached.result;
|
|
1694
|
+
}
|
|
1695
|
+
cache.stats.misses += 1;
|
|
1696
|
+
const result = computeCached(() => resolveComponentUncached(document, componentId, options, drawn));
|
|
1697
|
+
cache.components.set(componentId, { signature: cacheSignature(cache, drawn), componentIds: drawn, result });
|
|
1698
|
+
return result;
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* The size an instance takes on each axis it hugs, from its resolved root.
|
|
1702
|
+
* `undefined` on an axis the instance keeps as stored (Fixed, Fill, or a
|
|
1703
|
+
* component root without auto layout).
|
|
1704
|
+
* @since 0.23.0
|
|
1705
|
+
*/
|
|
1706
|
+
export function huggedInstanceSize(document, instanceId, options) {
|
|
1707
|
+
return huggedSizeOf(resolveInstance(document, instanceId, options));
|
|
1708
|
+
}
|
|
1709
|
+
/** The `huggedInstanceSize` of an instance already resolved. */
|
|
1710
|
+
export function huggedSizeOf(resolved) {
|
|
1711
|
+
const root = resolved.root.node;
|
|
1712
|
+
if (root.type !== "frame" || root.layout.kind === "absolute")
|
|
1713
|
+
return {};
|
|
1714
|
+
return {
|
|
1715
|
+
...(root.layout.widthSizing === "hug" ? { width: root.frame.width } : {}),
|
|
1716
|
+
...(root.layout.heightSizing === "hug" ? { height: root.frame.height } : {}),
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
const drawnNodesByDocument = new WeakMap();
|
|
1720
|
+
const drawnNodesResolveCache = createInstanceResolveCache();
|
|
1721
|
+
/**
|
|
1722
|
+
* Every node the renderer draws, for code that scans the document for the
|
|
1723
|
+
* fonts, characters or assets its text uses: the stored nodes (page layers
|
|
1724
|
+
* and component layers, but no slot host and no slot content), then the layers of every stored instance outside
|
|
1725
|
+
* slot content as
|
|
1726
|
+
* `resolveInstance` derives them, with the instance's overrides, property
|
|
1727
|
+
* values and nested instances applied and the virtual ID as `id`. Text or a
|
|
1728
|
+
* font that exists only in an override is found here and nowhere in
|
|
1729
|
+
* `document.nodes`. An instance that cannot be resolved (a missing component
|
|
1730
|
+
* or a nesting cycle) adds no layers; the compiler reports that itself.
|
|
1731
|
+
* Slot content is listed once, as the layers of the instance that draws it
|
|
1732
|
+
* (since 0.37.0), so content its instance does not draw, such as dormant
|
|
1733
|
+
* content whose slot frame a condition leaves out, is not listed.
|
|
1734
|
+
* The same document object gives the same array, and unchanged instances
|
|
1735
|
+
* give the same layer objects, so per-node caches keyed by object stay warm.
|
|
1736
|
+
* @since 0.26.0
|
|
1737
|
+
*/
|
|
1738
|
+
export function drawnNodes(document) {
|
|
1739
|
+
const textSizesVersion = getDrawnTextSizesVersion();
|
|
1740
|
+
const cached = drawnNodesByDocument.get(document);
|
|
1741
|
+
if (cached !== undefined && cached.textSizesVersion === textSizesVersion)
|
|
1742
|
+
return cached.nodes;
|
|
1743
|
+
const stored = tableValues(document.nodes);
|
|
1744
|
+
// The hosts instances list and their content are drawn by those instances,
|
|
1745
|
+
// and an unlisted host holds content no instance shows.
|
|
1746
|
+
const inContent = new Set();
|
|
1747
|
+
const pending = [];
|
|
1748
|
+
for (const node of stored)
|
|
1749
|
+
if (node.type === "instance" && node.slots !== undefined)
|
|
1750
|
+
pending.push(...ownedChildIds(node));
|
|
1751
|
+
while (pending.length > 0) {
|
|
1752
|
+
const id = pending.pop();
|
|
1753
|
+
if (inContent.has(id))
|
|
1754
|
+
continue;
|
|
1755
|
+
inContent.add(id);
|
|
1756
|
+
const node = entryOf(document.nodes, id);
|
|
1757
|
+
if (node !== undefined)
|
|
1758
|
+
pending.push(...ownedChildIds(node));
|
|
1759
|
+
}
|
|
1760
|
+
const nodes = stored.filter((node) => !inContent.has(node.id) && (node.type !== "frame" || node.slotHost !== true));
|
|
1761
|
+
for (const node of stored) {
|
|
1762
|
+
if (node.type !== "instance" || inContent.has(node.id))
|
|
1763
|
+
continue;
|
|
1764
|
+
let resolved;
|
|
1765
|
+
try {
|
|
1766
|
+
resolved = resolveInstance(document, node.id, { cache: drawnNodesResolveCache });
|
|
1767
|
+
}
|
|
1768
|
+
catch (error) {
|
|
1769
|
+
if (error instanceof ModelError)
|
|
1770
|
+
continue;
|
|
1771
|
+
throw error;
|
|
1772
|
+
}
|
|
1773
|
+
for (const layer of resolved.layers.values())
|
|
1774
|
+
nodes.push(layer.node);
|
|
1775
|
+
}
|
|
1776
|
+
drawnNodesByDocument.set(document, { textSizesVersion, nodes });
|
|
1777
|
+
return nodes;
|
|
1778
|
+
}
|