@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,1082 @@
|
|
|
1
|
+
// Component expressions against a document (docs/components/design.md 8.3,
|
|
2
|
+
// 8.5 and 8.7): the checking and evaluation environments built from a
|
|
3
|
+
// component's properties and the document's variables, the expected type and
|
|
4
|
+
// stored value of each computed field, and the operation builders that keep
|
|
5
|
+
// expressions valid through renames and deletions.
|
|
6
|
+
import { canonicalJson, ModelError } from "./codec.js";
|
|
7
|
+
import { effectivePropertyValue, findComponentProperty, isComputableField, propertyValueFieldId, propertyValueFits, } from "./component-properties.js";
|
|
8
|
+
import { checkComponentValidity, componentValidityEnvironment } from "./component-validity.js";
|
|
9
|
+
import { checkExpressionSource } from "./expressions/check.js";
|
|
10
|
+
import { expressionNameProblem, normalizeExpressionName } from "./expressions/names.js";
|
|
11
|
+
import { renameInExpression } from "./expressions/rename.js";
|
|
12
|
+
import { EXPRESSION_TYPES, expressionEnumType, expressionListType, } from "./expressions/types.js";
|
|
13
|
+
import { expressionColorFromHex, zeroExpressionValue } from "./expressions/values.js";
|
|
14
|
+
import { resolveTextStyle } from "./text-measurement.js";
|
|
15
|
+
import { isContainerNode, isValuedComponentProperty, } from "./types.js";
|
|
16
|
+
import { findVariable, locatedVariableValue } from "./variables.js";
|
|
17
|
+
import { entryOf, putEntry } from "./id-table.js";
|
|
18
|
+
import { tableValues } from "./shared-node-table.js";
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Variables
|
|
21
|
+
/**
|
|
22
|
+
* The binding properties that hold a length in pixels. A `number` variable
|
|
23
|
+
* whose scopes are all in this list reads as a `length` in expressions.
|
|
24
|
+
* @since 0.24.0
|
|
25
|
+
*/
|
|
26
|
+
export const LENGTH_VARIABLE_PROPERTIES = [
|
|
27
|
+
"width",
|
|
28
|
+
"height",
|
|
29
|
+
"cornerRadius",
|
|
30
|
+
"strokeWidth",
|
|
31
|
+
"gap",
|
|
32
|
+
"wrapGap",
|
|
33
|
+
"columnGap",
|
|
34
|
+
"rowGap",
|
|
35
|
+
"paddingTop",
|
|
36
|
+
"paddingRight",
|
|
37
|
+
"paddingBottom",
|
|
38
|
+
"paddingLeft",
|
|
39
|
+
"fontSize",
|
|
40
|
+
"letterSpacing",
|
|
41
|
+
];
|
|
42
|
+
/**
|
|
43
|
+
* The type a variable reads as in expressions. Colour, string and boolean
|
|
44
|
+
* variables read as `color`, `string` and `boolean`. A `number` variable
|
|
45
|
+
* reads as a `length` (pixels) when it has scopes and every scope is a
|
|
46
|
+
* length (`LENGTH_VARIABLE_PROPERTIES`), and as a plain `number` otherwise:
|
|
47
|
+
* with no scopes, or any scope such as opacity, font weight or line height.
|
|
48
|
+
* @since 0.24.0
|
|
49
|
+
*/
|
|
50
|
+
export function variableExpressionType(variable) {
|
|
51
|
+
switch (variable.type) {
|
|
52
|
+
case "color":
|
|
53
|
+
return EXPRESSION_TYPES.color;
|
|
54
|
+
case "string":
|
|
55
|
+
return EXPRESSION_TYPES.string;
|
|
56
|
+
case "boolean":
|
|
57
|
+
return EXPRESSION_TYPES.boolean;
|
|
58
|
+
case "number": {
|
|
59
|
+
const scopes = variable.scopes ?? [];
|
|
60
|
+
const isLength = scopes.length > 0 && scopes.every((scope) => LENGTH_VARIABLE_PROPERTIES.includes(scope));
|
|
61
|
+
return isLength ? EXPRESSION_TYPES.length : EXPRESSION_TYPES.number;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The name an expression reads a variable by: `"Collection/Name"`.
|
|
67
|
+
* @since 0.24.0
|
|
68
|
+
*/
|
|
69
|
+
export function variableExpressionName(collectionName, variableName) {
|
|
70
|
+
return `${collectionName}/${variableName}`;
|
|
71
|
+
}
|
|
72
|
+
const variableNameCache = new WeakMap();
|
|
73
|
+
/**
|
|
74
|
+
* The document's variables by `"Collection/Name"`. A name that two
|
|
75
|
+
* variables share is left out, so an expression that uses it shows an
|
|
76
|
+
* unknown-variable error instead of reading either one.
|
|
77
|
+
* @since 0.24.0
|
|
78
|
+
*/
|
|
79
|
+
export function variablesByExpressionName(theme) {
|
|
80
|
+
const cached = variableNameCache.get(theme);
|
|
81
|
+
if (cached !== undefined)
|
|
82
|
+
return cached;
|
|
83
|
+
const byName = new Map();
|
|
84
|
+
const shared = new Set();
|
|
85
|
+
for (const [collectionId, collection] of Object.entries(theme.variableCollections ?? {})) {
|
|
86
|
+
for (const [id, variable] of Object.entries(collection.variables)) {
|
|
87
|
+
const name = variableExpressionName(collection.name, variable.name);
|
|
88
|
+
if (byName.has(name))
|
|
89
|
+
shared.add(name);
|
|
90
|
+
byName.set(name, { collectionId, collection, variableId: id, variable });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
shared.forEach((name) => byName.delete(name));
|
|
94
|
+
variableNameCache.set(theme, byName);
|
|
95
|
+
return byName;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The value of a variable in the mode its collection shows under `modes`
|
|
99
|
+
* (see `resolveVariableValue`; without `modes`, the active mode), typed as
|
|
100
|
+
* `variableExpressionType`.
|
|
101
|
+
* @since 0.24.0
|
|
102
|
+
*/
|
|
103
|
+
export function variableExpressionValue(located, modes) {
|
|
104
|
+
const { variable } = located;
|
|
105
|
+
const value = locatedVariableValue(located, modes);
|
|
106
|
+
if (value === undefined)
|
|
107
|
+
return undefined;
|
|
108
|
+
const type = variableExpressionType(variable);
|
|
109
|
+
switch (type.kind) {
|
|
110
|
+
case "color":
|
|
111
|
+
return { kind: "color", value: expressionColorFromHex(String(value)) };
|
|
112
|
+
case "string":
|
|
113
|
+
return { kind: "string", value: String(value) };
|
|
114
|
+
case "boolean":
|
|
115
|
+
return { kind: "boolean", value: value === true };
|
|
116
|
+
case "length":
|
|
117
|
+
return { kind: "length", value: Number(value) };
|
|
118
|
+
default:
|
|
119
|
+
return { kind: "number", value: Number(value) };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
// Properties
|
|
124
|
+
/**
|
|
125
|
+
* The type a property reads as: `boolean`, `string` (text), `color`,
|
|
126
|
+
* `component` (instance swap), an enum type named `enumName` (the property
|
|
127
|
+
* name by default) with the option names, or for a number property
|
|
128
|
+
* `number`, `length`, `percent` or `angle` by its unit.
|
|
129
|
+
* @since 0.24.0
|
|
130
|
+
*/
|
|
131
|
+
export function componentPropertyExpressionType(property, enumName = property.name) {
|
|
132
|
+
switch (property.kind) {
|
|
133
|
+
case "boolean":
|
|
134
|
+
return EXPRESSION_TYPES.boolean;
|
|
135
|
+
case "number":
|
|
136
|
+
return property.unit === "px"
|
|
137
|
+
? EXPRESSION_TYPES.length
|
|
138
|
+
: property.unit === "%"
|
|
139
|
+
? EXPRESSION_TYPES.percent
|
|
140
|
+
: property.unit === "deg"
|
|
141
|
+
? EXPRESSION_TYPES.angle
|
|
142
|
+
: EXPRESSION_TYPES.number;
|
|
143
|
+
case "text":
|
|
144
|
+
return EXPRESSION_TYPES.string;
|
|
145
|
+
case "enum":
|
|
146
|
+
return expressionEnumType(enumName, property.options.map((option) => option.name));
|
|
147
|
+
case "color":
|
|
148
|
+
return EXPRESSION_TYPES.color;
|
|
149
|
+
case "instance":
|
|
150
|
+
return EXPRESSION_TYPES.component;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The enum type name of a nested component's property, as an outer
|
|
155
|
+
* component's expressions see it: `"<component name>.<property name>"`. A
|
|
156
|
+
* property name has no dot, so it never clashes with the outer component's
|
|
157
|
+
* own enum types, which are named after their properties.
|
|
158
|
+
* @since 0.24.0
|
|
159
|
+
*/
|
|
160
|
+
export function nestedPropertyEnumName(component, property) {
|
|
161
|
+
return `${component.name}.${property.name}`;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The expression value of a property: `value` when it fits the property's
|
|
165
|
+
* kind, else the default. An enum value reads as its option's name.
|
|
166
|
+
* @since 0.24.0
|
|
167
|
+
*/
|
|
168
|
+
export function componentPropertyExpressionValue(property, value) {
|
|
169
|
+
const effective = effectivePropertyValue(property, value);
|
|
170
|
+
switch (property.kind) {
|
|
171
|
+
case "boolean":
|
|
172
|
+
return { kind: "boolean", value: effective === true };
|
|
173
|
+
case "number": {
|
|
174
|
+
const type = componentPropertyExpressionType(property);
|
|
175
|
+
return { kind: type.kind, value: Number(effective) };
|
|
176
|
+
}
|
|
177
|
+
case "text":
|
|
178
|
+
return { kind: "string", value: String(effective) };
|
|
179
|
+
case "enum": {
|
|
180
|
+
const option = property.options.find((candidate) => candidate.id === effective) ?? property.options[0];
|
|
181
|
+
return { kind: "enum", option: option?.name ?? "" };
|
|
182
|
+
}
|
|
183
|
+
case "color":
|
|
184
|
+
return { kind: "color", value: expressionColorFromHex(String(effective)) };
|
|
185
|
+
case "instance":
|
|
186
|
+
return { kind: "component", id: String(effective) };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function componentOf(document, component) {
|
|
190
|
+
if (typeof component !== "string")
|
|
191
|
+
return component;
|
|
192
|
+
const found = entryOf(document.components, component);
|
|
193
|
+
if (found === undefined)
|
|
194
|
+
throw new ModelError("INVALID_TREE", `Missing component ${component}`, `$.components.${component}`);
|
|
195
|
+
return found;
|
|
196
|
+
}
|
|
197
|
+
// ---------------------------------------------------------------------------
|
|
198
|
+
// Environments
|
|
199
|
+
/**
|
|
200
|
+
* The checking environment of a component's expressions: its properties by
|
|
201
|
+
* name, the document's variables by `"Collection/Name"`, and `storedType`
|
|
202
|
+
* for `stored` (leave it out for a condition).
|
|
203
|
+
* @since 0.24.0
|
|
204
|
+
*/
|
|
205
|
+
export function componentExpressionEnvironment(document, component, storedType) {
|
|
206
|
+
const definition = componentOf(document, component);
|
|
207
|
+
const properties = new Map((definition.properties ?? []).filter(isValuedComponentProperty).map((property) => [property.name, componentPropertyExpressionType(property)]));
|
|
208
|
+
const variables = variablesByExpressionName(document.theme);
|
|
209
|
+
return {
|
|
210
|
+
propertyType: (name) => properties.get(name),
|
|
211
|
+
variableType: (name) => {
|
|
212
|
+
const located = variables.get(name);
|
|
213
|
+
return located === undefined ? undefined : variableExpressionType(located.variable);
|
|
214
|
+
},
|
|
215
|
+
...(storedType === undefined ? {} : { storedType }),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The evaluation environment matching `componentExpressionEnvironment`: each
|
|
220
|
+
* property's value from `propertyValues` (an instance's values; values that
|
|
221
|
+
* do not fit and missing ones read as the default), each variable in the
|
|
222
|
+
* mode its collection shows under `modes` (the active mode by default), and
|
|
223
|
+
* `stored`.
|
|
224
|
+
* @since 0.24.0
|
|
225
|
+
*/
|
|
226
|
+
export function componentValueEnvironment(document, component, propertyValues = {}, stored, modes) {
|
|
227
|
+
const definition = componentOf(document, component);
|
|
228
|
+
const values = new Map((definition.properties ?? []).filter(isValuedComponentProperty).map((property) => [
|
|
229
|
+
property.name,
|
|
230
|
+
componentPropertyExpressionValue(property, entryOf(propertyValues, property.id)),
|
|
231
|
+
]));
|
|
232
|
+
const variables = variablesByExpressionName(document.theme);
|
|
233
|
+
return {
|
|
234
|
+
propertyValue: (name) => values.get(name),
|
|
235
|
+
variableValue: (name) => {
|
|
236
|
+
const located = variables.get(name);
|
|
237
|
+
return located === undefined ? undefined : variableExpressionValue(located, modes);
|
|
238
|
+
},
|
|
239
|
+
...(stored === undefined ? {} : { stored }),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
// Fields
|
|
244
|
+
/** The enum type of the `direction` field. @since 0.24.0 */
|
|
245
|
+
export const LAYOUT_DIRECTION_TYPE = expressionEnumType("layout direction", ["horizontal", "vertical"]);
|
|
246
|
+
/** The enum type of the `align` field. @since 0.24.0 */
|
|
247
|
+
export const LAYOUT_ALIGN_TYPE = expressionEnumType("layout align", ["start", "center", "end"]);
|
|
248
|
+
/**
|
|
249
|
+
* The enum type of the `justify` field; `space_between` and the others are
|
|
250
|
+
* the model's `"space-between"` and so on.
|
|
251
|
+
* @since 0.24.0
|
|
252
|
+
*/
|
|
253
|
+
export const LAYOUT_JUSTIFY_TYPE = expressionEnumType("layout justify", [
|
|
254
|
+
"start",
|
|
255
|
+
"center",
|
|
256
|
+
"end",
|
|
257
|
+
"space_between",
|
|
258
|
+
"space_around",
|
|
259
|
+
"space_evenly",
|
|
260
|
+
]);
|
|
261
|
+
const NAMED_FIELD_TYPES = {
|
|
262
|
+
"x": EXPRESSION_TYPES.length,
|
|
263
|
+
"y": EXPRESSION_TYPES.length,
|
|
264
|
+
"rotation": EXPRESSION_TYPES.angle,
|
|
265
|
+
"transform": expressionListType(EXPRESSION_TYPES.number),
|
|
266
|
+
"clipContent": EXPRESSION_TYPES.boolean,
|
|
267
|
+
"paragraphSpacing": EXPRESSION_TYPES.length,
|
|
268
|
+
lineHeightMultiplier: EXPRESSION_TYPES.number,
|
|
269
|
+
constraintSize: EXPRESSION_TYPES.size,
|
|
270
|
+
"resizing.width": expressionEnumType("resizing.width", ["fixed", "hug", "fill"]),
|
|
271
|
+
"resizing.height": expressionEnumType("resizing.height", ["fixed", "hug", "fill"]),
|
|
272
|
+
"layout.widthSizing": expressionEnumType("layout.widthSizing", ["fixed", "hug", "fill"]),
|
|
273
|
+
"layout.heightSizing": expressionEnumType("layout.heightSizing", ["fixed", "hug", "fill"]),
|
|
274
|
+
"constraints.horizontal": expressionEnumType("constraints.horizontal", ["left", "right", "left-right", "center", "scale"]),
|
|
275
|
+
"constraints.vertical": expressionEnumType("constraints.vertical", ["top", "bottom", "top-bottom", "center", "scale"]),
|
|
276
|
+
"startCap": expressionEnumType("startCap", ["none", "round", "square", "line-arrow", "triangle-arrow", "reversed-triangle", "circle-arrow", "diamond-arrow"]),
|
|
277
|
+
"endCap": expressionEnumType("endCap", ["none", "round", "square", "line-arrow", "triangle-arrow", "reversed-triangle", "circle-arrow", "diamond-arrow"]),
|
|
278
|
+
visible: EXPRESSION_TYPES.boolean,
|
|
279
|
+
opacity: EXPRESSION_TYPES.number,
|
|
280
|
+
fills: expressionListType(EXPRESSION_TYPES.paint),
|
|
281
|
+
strokes: expressionListType(EXPRESSION_TYPES.stroke),
|
|
282
|
+
effects: expressionListType(EXPRESSION_TYPES.effect),
|
|
283
|
+
cornerRadius: EXPRESSION_TYPES.length,
|
|
284
|
+
cornerRadii: EXPRESSION_TYPES.corners,
|
|
285
|
+
cornerSmoothing: EXPRESSION_TYPES.number,
|
|
286
|
+
width: EXPRESSION_TYPES.length,
|
|
287
|
+
height: EXPRESSION_TYPES.length,
|
|
288
|
+
minWidth: EXPRESSION_TYPES.length,
|
|
289
|
+
maxWidth: EXPRESSION_TYPES.length,
|
|
290
|
+
minHeight: EXPRESSION_TYPES.length,
|
|
291
|
+
maxHeight: EXPRESSION_TYPES.length,
|
|
292
|
+
gap: EXPRESSION_TYPES.length,
|
|
293
|
+
wrapGap: EXPRESSION_TYPES.length,
|
|
294
|
+
columnGap: EXPRESSION_TYPES.length,
|
|
295
|
+
rowGap: EXPRESSION_TYPES.length,
|
|
296
|
+
padding: EXPRESSION_TYPES.insets,
|
|
297
|
+
direction: LAYOUT_DIRECTION_TYPE,
|
|
298
|
+
align: LAYOUT_ALIGN_TYPE,
|
|
299
|
+
justify: LAYOUT_JUSTIFY_TYPE,
|
|
300
|
+
wrap: EXPRESSION_TYPES.boolean,
|
|
301
|
+
characters: EXPRESSION_TYPES.string,
|
|
302
|
+
fontFamily: EXPRESSION_TYPES.string,
|
|
303
|
+
fontSize: EXPRESSION_TYPES.length,
|
|
304
|
+
fontWeight: EXPRESSION_TYPES.number,
|
|
305
|
+
lineHeight: EXPRESSION_TYPES.percent,
|
|
306
|
+
letterSpacing: EXPRESSION_TYPES.length,
|
|
307
|
+
textColor: EXPRESSION_TYPES.color,
|
|
308
|
+
textStyle: EXPRESSION_TYPES.text_style,
|
|
309
|
+
componentId: EXPRESSION_TYPES.component,
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* The type an expression in `field` must have: `boolean` for a condition,
|
|
313
|
+
* the field's type from the table in design 8.3, or for
|
|
314
|
+
* `propertyValues.<id>` the type of that property of the nested instance's
|
|
315
|
+
* component (with its enum named by `nestedPropertyEnumName`). Undefined
|
|
316
|
+
* for a field the node cannot compute or a property the nested component
|
|
317
|
+
* does not have.
|
|
318
|
+
* @since 0.24.0
|
|
319
|
+
*/
|
|
320
|
+
export function computableFieldType(document, node, field) {
|
|
321
|
+
if (field === "condition")
|
|
322
|
+
return EXPRESSION_TYPES.boolean;
|
|
323
|
+
const propertyId = propertyValueFieldId(field);
|
|
324
|
+
if (propertyId !== undefined) {
|
|
325
|
+
if (node.type !== "instance")
|
|
326
|
+
return undefined;
|
|
327
|
+
const nested = entryOf(document.components, node.componentId);
|
|
328
|
+
const property = nested === undefined ? undefined : findComponentProperty(nested, propertyId);
|
|
329
|
+
return property === undefined || nested === undefined || !isValuedComponentProperty(property)
|
|
330
|
+
? undefined
|
|
331
|
+
: componentPropertyExpressionType(property, nestedPropertyEnumName(nested, property));
|
|
332
|
+
}
|
|
333
|
+
return isComputableField(node, field) ? NAMED_FIELD_TYPES[field] : undefined;
|
|
334
|
+
}
|
|
335
|
+
const length = (value) => ({ kind: "length", value });
|
|
336
|
+
function firstRunStyle(document, node) {
|
|
337
|
+
const run = node.type === "text" ? node.paragraphs[0]?.runs[0] : undefined;
|
|
338
|
+
return resolveTextStyle(document, run?.styleId, run?.overrides);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* A frame's or shape's style with its shared node style applied, as the
|
|
342
|
+
* renderer draws it: each local field wins over the shared one, and a node
|
|
343
|
+
* that sets either corner field owns both corner fields.
|
|
344
|
+
*/
|
|
345
|
+
function effectiveVisualStyle(document, node) {
|
|
346
|
+
const style = "style" in node ? node.style : undefined;
|
|
347
|
+
if (style?.styleId === undefined)
|
|
348
|
+
return style;
|
|
349
|
+
const shared = entryOf(document.theme.nodeStyles, style.styleId);
|
|
350
|
+
if (shared === undefined) {
|
|
351
|
+
throw new ModelError("INVALID_TREE", `Missing node style ${style.styleId}`, `$.nodes.${node.id}.style.styleId`);
|
|
352
|
+
}
|
|
353
|
+
const ownsCorners = style.cornerRadius !== undefined || style.cornerRadii !== undefined;
|
|
354
|
+
return {
|
|
355
|
+
fills: style.fills ?? shared.fills,
|
|
356
|
+
strokes: style.strokes ?? shared.strokes,
|
|
357
|
+
shadows: style.shadows ?? shared.shadows,
|
|
358
|
+
layerBlur: style.layerBlur ?? shared.layerBlur,
|
|
359
|
+
backgroundBlur: style.backgroundBlur ?? shared.backgroundBlur,
|
|
360
|
+
cornerRadius: ownsCorners ? style.cornerRadius : shared.cornerRadius,
|
|
361
|
+
cornerRadii: ownsCorners ? style.cornerRadii : shared.cornerRadii,
|
|
362
|
+
cornerSmoothing: style.cornerSmoothing ?? shared.cornerSmoothing,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function namedStoredValue(document, node, field) {
|
|
366
|
+
const style = effectiveVisualStyle(document, node);
|
|
367
|
+
const layout = node.type === "frame" ? node.layout : undefined;
|
|
368
|
+
const stack = layout?.kind === "horizontal" || layout?.kind === "vertical" ? layout : undefined;
|
|
369
|
+
const resizing = "resizing" in node ? node.resizing : undefined;
|
|
370
|
+
switch (field) {
|
|
371
|
+
case "x":
|
|
372
|
+
case "y": return length("frame" in node ? node.frame[field] : 0);
|
|
373
|
+
case "rotation": return { kind: "angle", value: "rotation" in node ? node.rotation : 0 };
|
|
374
|
+
case "transform": return { kind: "list", items: ("transform" in node ? node.transform : [1, 0, 0, 1, 0, 0]).map((value) => ({ kind: "number", value })) };
|
|
375
|
+
case "clipContent": return { kind: "boolean", value: node.type === "frame" && (node.clipContent ?? false) };
|
|
376
|
+
case "constraintSize": return { kind: "size", width: node.type === "frame" ? (node.constraintSize ?? node.frame).width : 0, height: node.type === "frame" ? (node.constraintSize ?? node.frame).height : 0 };
|
|
377
|
+
case "lineHeightMultiplier": {
|
|
378
|
+
const style = firstRunStyle(document, node);
|
|
379
|
+
return { kind: "number", value: style.lineHeight };
|
|
380
|
+
}
|
|
381
|
+
case "paragraphSpacing": return length(node.type === "text" ? node.paragraphSpacing ?? 0 : 0);
|
|
382
|
+
case "resizing.width":
|
|
383
|
+
case "resizing.height": return { kind: "enum", option: resizing?.[field === "resizing.width" ? "width" : "height"] ?? "fixed" };
|
|
384
|
+
case "layout.widthSizing":
|
|
385
|
+
case "layout.heightSizing": return { kind: "enum", option: layout !== undefined && layout.kind !== "absolute" ? layout[field === "layout.widthSizing" ? "widthSizing" : "heightSizing"] : "fixed" };
|
|
386
|
+
case "constraints.horizontal": return { kind: "enum", option: node.constraints?.horizontal ?? "left" };
|
|
387
|
+
case "constraints.vertical": return { kind: "enum", option: node.constraints?.vertical ?? "top" };
|
|
388
|
+
case "startCap":
|
|
389
|
+
case "endCap": return { kind: "enum", option: field in node ? node[field] ?? "none" : "none" };
|
|
390
|
+
case "visible":
|
|
391
|
+
return { kind: "boolean", value: node.visible };
|
|
392
|
+
case "opacity":
|
|
393
|
+
return { kind: "number", value: node.opacity };
|
|
394
|
+
case "fills":
|
|
395
|
+
return { kind: "list", items: (style?.fills ?? []).map((value) => ({ kind: "paint", value })) };
|
|
396
|
+
case "strokes":
|
|
397
|
+
return { kind: "list", items: (style?.strokes ?? []).map((value) => ({ kind: "stroke", value })) };
|
|
398
|
+
case "effects": {
|
|
399
|
+
const items = (style?.shadows ?? []).map((shadow) => ({
|
|
400
|
+
kind: "effect",
|
|
401
|
+
value: { kind: "shadow", shadow },
|
|
402
|
+
}));
|
|
403
|
+
if (style?.layerBlur !== undefined)
|
|
404
|
+
items.push({ kind: "effect", value: { kind: "layer_blur", radius: style.layerBlur } });
|
|
405
|
+
if (style?.backgroundBlur !== undefined) {
|
|
406
|
+
items.push({ kind: "effect", value: { kind: "background_blur", radius: style.backgroundBlur } });
|
|
407
|
+
}
|
|
408
|
+
return { kind: "list", items };
|
|
409
|
+
}
|
|
410
|
+
case "cornerRadius":
|
|
411
|
+
return length(style?.cornerRadius ?? 0);
|
|
412
|
+
case "cornerRadii": {
|
|
413
|
+
const radius = style?.cornerRadius ?? 0;
|
|
414
|
+
return {
|
|
415
|
+
kind: "corners",
|
|
416
|
+
value: style?.cornerRadii ?? { topLeft: radius, topRight: radius, bottomRight: radius, bottomLeft: radius },
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
case "cornerSmoothing":
|
|
420
|
+
return { kind: "number", value: style?.cornerSmoothing ?? 0 };
|
|
421
|
+
case "width":
|
|
422
|
+
return length("frame" in node ? node.frame.width : 0);
|
|
423
|
+
case "height":
|
|
424
|
+
return length("frame" in node ? node.frame.height : 0);
|
|
425
|
+
case "minWidth":
|
|
426
|
+
case "maxWidth":
|
|
427
|
+
case "minHeight":
|
|
428
|
+
case "maxHeight":
|
|
429
|
+
return length(resizing?.[field] ?? 0);
|
|
430
|
+
case "gap":
|
|
431
|
+
return length(stack?.gap ?? 0);
|
|
432
|
+
case "wrapGap":
|
|
433
|
+
return length(stack?.wrapGap ?? 0);
|
|
434
|
+
case "columnGap":
|
|
435
|
+
return length(layout?.kind === "grid" ? layout.columnGap : 0);
|
|
436
|
+
case "rowGap":
|
|
437
|
+
return length(layout?.kind === "grid" ? layout.rowGap : 0);
|
|
438
|
+
case "padding":
|
|
439
|
+
return {
|
|
440
|
+
kind: "insets",
|
|
441
|
+
value: layout !== undefined && layout.kind !== "absolute" ? layout.padding : { top: 0, right: 0, bottom: 0, left: 0 },
|
|
442
|
+
};
|
|
443
|
+
case "direction":
|
|
444
|
+
return { kind: "enum", option: stack?.kind ?? "horizontal" };
|
|
445
|
+
case "align":
|
|
446
|
+
return { kind: "enum", option: stack?.align ?? "start" };
|
|
447
|
+
case "justify":
|
|
448
|
+
return { kind: "enum", option: (stack?.justify ?? "start").replace(/-/g, "_") };
|
|
449
|
+
case "wrap":
|
|
450
|
+
return { kind: "boolean", value: stack?.wrap ?? false };
|
|
451
|
+
case "characters":
|
|
452
|
+
return {
|
|
453
|
+
kind: "string",
|
|
454
|
+
value: node.type === "text"
|
|
455
|
+
? node.paragraphs.map((paragraph) => paragraph.runs.map((run) => run.text).join("")).join("\n")
|
|
456
|
+
: "",
|
|
457
|
+
};
|
|
458
|
+
case "fontFamily":
|
|
459
|
+
return { kind: "string", value: firstRunStyle(document, node).fontFamily };
|
|
460
|
+
case "fontSize":
|
|
461
|
+
return length(firstRunStyle(document, node).fontSize);
|
|
462
|
+
case "fontWeight":
|
|
463
|
+
return { kind: "number", value: firstRunStyle(document, node).fontWeight };
|
|
464
|
+
case "lineHeight":
|
|
465
|
+
return { kind: "percent", value: firstRunStyle(document, node).lineHeight * 100 };
|
|
466
|
+
case "letterSpacing":
|
|
467
|
+
return length(firstRunStyle(document, node).letterSpacing ?? 0);
|
|
468
|
+
case "textColor":
|
|
469
|
+
return { kind: "color", value: expressionColorFromHex(firstRunStyle(document, node).color) };
|
|
470
|
+
case "textStyle": {
|
|
471
|
+
const resolved = firstRunStyle(document, node);
|
|
472
|
+
return {
|
|
473
|
+
kind: "text_style",
|
|
474
|
+
value: {
|
|
475
|
+
fontFamily: resolved.fontFamily,
|
|
476
|
+
fontWeight: resolved.fontWeight,
|
|
477
|
+
fontSize: resolved.fontSize,
|
|
478
|
+
// The language's line height is a length: the multiplier times the font size.
|
|
479
|
+
lineHeightPixels: resolved.lineHeight * resolved.fontSize,
|
|
480
|
+
letterSpacing: resolved.letterSpacing ?? 0,
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
case "componentId":
|
|
485
|
+
return node.type === "instance" ? { kind: "component", id: node.componentId } : undefined;
|
|
486
|
+
default:
|
|
487
|
+
return undefined;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* The base value of a computed field, which `stored` reads: the plain value
|
|
492
|
+
* stored on the node, as an expression value. Absent optional values read
|
|
493
|
+
* as zero (for example a missing `maxWidth` is 0 px, a missing corner
|
|
494
|
+
* radius 0 px). The text fields read the first run's resolved style. Line
|
|
495
|
+
* height, stored as a multiplier of the font size, reads as a percent (1.5
|
|
496
|
+
* is 150%); inside a `text_style` it reads as a length in pixels, the
|
|
497
|
+
* multiplier times the font size (1.5 at 16 px is 24 px). A nested
|
|
498
|
+
* instance's `propertyValues.<id>` reads the value stored on the node, else
|
|
499
|
+
* the nested component's default. Undefined for a condition, for fields
|
|
500
|
+
* `computableFieldType` does not type, and for a `propertyValues.<id>` on a
|
|
501
|
+
* layer that is not an instance or whose component has no such property.
|
|
502
|
+
* @since 0.24.0
|
|
503
|
+
*/
|
|
504
|
+
export function storedFieldValue(document, node, field) {
|
|
505
|
+
if (field === "condition")
|
|
506
|
+
return undefined;
|
|
507
|
+
const propertyId = propertyValueFieldId(field);
|
|
508
|
+
if (propertyId !== undefined) {
|
|
509
|
+
if (node.type !== "instance")
|
|
510
|
+
return undefined;
|
|
511
|
+
const nested = entryOf(document.components, node.componentId);
|
|
512
|
+
const property = nested === undefined ? undefined : findComponentProperty(nested, propertyId);
|
|
513
|
+
return property === undefined || !isValuedComponentProperty(property) ? undefined : componentPropertyExpressionValue(property, entryOf(node.propertyValues, propertyId));
|
|
514
|
+
}
|
|
515
|
+
const type = computableFieldType(document, node, field);
|
|
516
|
+
if (type === undefined)
|
|
517
|
+
return undefined;
|
|
518
|
+
return namedStoredValue(document, node, field) ?? zeroExpressionValue(type);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* The expected type and checking environment of one field of a layer in
|
|
522
|
+
* `component`, or undefined when the field has no type (see
|
|
523
|
+
* `computableFieldType`).
|
|
524
|
+
* @since 0.24.0
|
|
525
|
+
*/
|
|
526
|
+
export function expressionFieldContext(document, component, node, field) {
|
|
527
|
+
const expected = computableFieldType(document, node, field);
|
|
528
|
+
if (expected === undefined)
|
|
529
|
+
return undefined;
|
|
530
|
+
const storedType = field === "condition" ? undefined : expected;
|
|
531
|
+
return { expected, environment: componentExpressionEnvironment(document, component, storedType) };
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* The evaluation environment of one field of a layer in `component` for an
|
|
535
|
+
* instance with `propertyValues`: `componentValueEnvironment` with `stored`
|
|
536
|
+
* set to `storedFieldValue`.
|
|
537
|
+
* @since 0.24.0
|
|
538
|
+
*/
|
|
539
|
+
export function expressionFieldValueEnvironment(document, component, node, field, propertyValues = {}, modes) {
|
|
540
|
+
return componentValueEnvironment(document, component, propertyValues, storedFieldValue(document, node, field), modes);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* The IDs of a component's layers, root first, in tree order.
|
|
544
|
+
* @since 0.24.0
|
|
545
|
+
*/
|
|
546
|
+
export function componentLayerIds(document, component) {
|
|
547
|
+
const definition = componentOf(document, component);
|
|
548
|
+
const ids = [];
|
|
549
|
+
const visit = (id) => {
|
|
550
|
+
const node = entryOf(document.nodes, id);
|
|
551
|
+
if (node === undefined)
|
|
552
|
+
return;
|
|
553
|
+
ids.push(id);
|
|
554
|
+
if (isContainerNode(node))
|
|
555
|
+
node.children.forEach(visit);
|
|
556
|
+
};
|
|
557
|
+
visit(definition.rootId);
|
|
558
|
+
return ids;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Every expression and condition stored on a component's layers, in tree
|
|
562
|
+
* order, conditions after the node's computed fields.
|
|
563
|
+
* @since 0.24.0
|
|
564
|
+
*/
|
|
565
|
+
export function componentExpressionSites(document, component) {
|
|
566
|
+
const sites = [];
|
|
567
|
+
for (const nodeId of componentLayerIds(document, component)) {
|
|
568
|
+
const node = entryOf(document.nodes, nodeId);
|
|
569
|
+
for (const [field, source] of Object.entries(node.expressions ?? {})) {
|
|
570
|
+
if (source !== undefined)
|
|
571
|
+
sites.push({ nodeId, field: field, source });
|
|
572
|
+
}
|
|
573
|
+
if (node.condition !== undefined)
|
|
574
|
+
sites.push({ nodeId, field: "condition", source: node.condition });
|
|
575
|
+
}
|
|
576
|
+
return sites;
|
|
577
|
+
}
|
|
578
|
+
function withSource(node, field, source) {
|
|
579
|
+
if (field === "condition")
|
|
580
|
+
return { ...node, condition: source };
|
|
581
|
+
return { ...node, expressions: { ...node.expressions, [field]: source } };
|
|
582
|
+
}
|
|
583
|
+
function withoutExpressionField(node, field) {
|
|
584
|
+
const expressions = { ...node.expressions };
|
|
585
|
+
delete expressions[field];
|
|
586
|
+
const next = { ...node };
|
|
587
|
+
if (Object.keys(expressions).length === 0)
|
|
588
|
+
delete next.expressions;
|
|
589
|
+
else
|
|
590
|
+
next.expressions = expressions;
|
|
591
|
+
return next;
|
|
592
|
+
}
|
|
593
|
+
/** Nodes changed by an edit, kept by ID so each is replaced once. */
|
|
594
|
+
class NodeEdits {
|
|
595
|
+
nodes = new Map();
|
|
596
|
+
document;
|
|
597
|
+
constructor(document) {
|
|
598
|
+
this.document = document;
|
|
599
|
+
}
|
|
600
|
+
get(id) {
|
|
601
|
+
return this.nodes.get(id) ?? entryOf(this.document.nodes, id);
|
|
602
|
+
}
|
|
603
|
+
set(node) {
|
|
604
|
+
this.nodes.set(node.id, node);
|
|
605
|
+
}
|
|
606
|
+
operations() {
|
|
607
|
+
return [...this.nodes.values()].map((node) => ({ type: "node.replace", node }));
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Rewrites the expressions of `component`'s layers with `renames`, which
|
|
612
|
+
* describe names in force before the edit. `select` limits the fields.
|
|
613
|
+
* Returns how many references changed.
|
|
614
|
+
*/
|
|
615
|
+
function rewriteComponent(document, component, edits, renames, select = () => true) {
|
|
616
|
+
let count = 0;
|
|
617
|
+
for (const site of componentExpressionSites(document, component)) {
|
|
618
|
+
const node = entryOf(document.nodes, site.nodeId);
|
|
619
|
+
if (!select(node, site.field))
|
|
620
|
+
continue;
|
|
621
|
+
const context = expressionFieldContext(document, component, node, site.field);
|
|
622
|
+
if (context === undefined)
|
|
623
|
+
continue;
|
|
624
|
+
const { source, count: changed } = renameInExpression(site.source, context.expected, context.environment, renames);
|
|
625
|
+
count += changed;
|
|
626
|
+
if (source !== site.source)
|
|
627
|
+
edits.set(withSource(edits.get(site.nodeId), site.field, source));
|
|
628
|
+
}
|
|
629
|
+
const rewriteOverride = (override) => {
|
|
630
|
+
if (override.conditional === undefined)
|
|
631
|
+
return override;
|
|
632
|
+
let changed = false;
|
|
633
|
+
const conditional = override.conditional.map((branch) => {
|
|
634
|
+
const value = rewriteOverride(branch.value);
|
|
635
|
+
const renamed = branch.componentId === component.id
|
|
636
|
+
? renameInExpression(branch.condition, EXPRESSION_TYPES.boolean, componentExpressionEnvironment(document, component), renames)
|
|
637
|
+
: { source: branch.condition, count: 0 };
|
|
638
|
+
count += renamed.count;
|
|
639
|
+
changed ||= value !== branch.value || renamed.source !== branch.condition;
|
|
640
|
+
return { ...branch, condition: renamed.source, value };
|
|
641
|
+
});
|
|
642
|
+
return changed ? { ...override, conditional } : override;
|
|
643
|
+
};
|
|
644
|
+
for (const stored of tableValues(document.nodes)) {
|
|
645
|
+
const node = edits.get(stored.id);
|
|
646
|
+
if (node.type !== "instance" || node.overrides === undefined || !select(node, "condition"))
|
|
647
|
+
continue;
|
|
648
|
+
const overrides = Object.fromEntries(Object.entries(node.overrides).map(([path, override]) => [path, rewriteOverride(override)]));
|
|
649
|
+
if (Object.entries(overrides).some(([path, override]) => override !== node.overrides[path]))
|
|
650
|
+
edits.set({ ...node, overrides });
|
|
651
|
+
}
|
|
652
|
+
return count;
|
|
653
|
+
}
|
|
654
|
+
/** The nested instance nodes, in any component, whose stored component is `componentId`. */
|
|
655
|
+
function nestedInstancesOf(document, componentId) {
|
|
656
|
+
const found = [];
|
|
657
|
+
for (const owner of Object.values(document.components ?? {})) {
|
|
658
|
+
for (const id of componentLayerIds(document, owner)) {
|
|
659
|
+
const node = entryOf(document.nodes, id);
|
|
660
|
+
if (node.type === "instance" && node.componentId === componentId)
|
|
661
|
+
found.push({ owner, node });
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return found;
|
|
665
|
+
}
|
|
666
|
+
function propertyOf(document, componentId, propertyId) {
|
|
667
|
+
const component = componentOf(document, componentId);
|
|
668
|
+
const index = (component.properties ?? []).findIndex((property) => property.id === propertyId);
|
|
669
|
+
if (index < 0) {
|
|
670
|
+
throw new ModelError("INVALID_TREE", `Missing property ${propertyId}`, `$.components.${componentId}.properties`);
|
|
671
|
+
}
|
|
672
|
+
return { component, index, property: component.properties[index] };
|
|
673
|
+
}
|
|
674
|
+
/** `name` in NFC, the form names are stored in; throws for a name no property or option can have. */
|
|
675
|
+
function requireName(name, path) {
|
|
676
|
+
const problem = expressionNameProblem(name);
|
|
677
|
+
if (problem !== undefined)
|
|
678
|
+
throw new ModelError("INVALID_SCHEMA", problem, path);
|
|
679
|
+
return normalizeExpressionName(name);
|
|
680
|
+
}
|
|
681
|
+
function replaceProperty(component, index, property) {
|
|
682
|
+
const properties = [...(component.properties ?? [])];
|
|
683
|
+
if (property === undefined)
|
|
684
|
+
properties.splice(index, 1);
|
|
685
|
+
else
|
|
686
|
+
properties[index] = property;
|
|
687
|
+
const next = { ...component, properties };
|
|
688
|
+
if (properties.length === 0)
|
|
689
|
+
delete next.properties;
|
|
690
|
+
return next;
|
|
691
|
+
}
|
|
692
|
+
/** `next` with its validity expression rewritten by `renames` (names in force in `before`), and how many references changed. */
|
|
693
|
+
function renamedValidity(before, next, renames) {
|
|
694
|
+
if (before.validity === undefined)
|
|
695
|
+
return { component: next, count: 0 };
|
|
696
|
+
const { source, count } = renameInExpression(before.validity, EXPRESSION_TYPES.boolean, componentValidityEnvironment(before), renames);
|
|
697
|
+
return { component: { ...next, validity: source }, count };
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Throws a `ModelError` when `next`, the component after an edit, has a
|
|
701
|
+
* validity expression that no longer checks (it reads a property or an
|
|
702
|
+
* option the edit removes, or one of another kind now): the expression is
|
|
703
|
+
* changed first, never dropped or left broken.
|
|
704
|
+
*/
|
|
705
|
+
function requireCheckingValidity(next, edit) {
|
|
706
|
+
const checked = checkComponentValidity(next);
|
|
707
|
+
if (checked === undefined || checked.ok)
|
|
708
|
+
return;
|
|
709
|
+
throw new ModelError("INVALID_SCHEMA", `${edit} would leave the validity expression of ${next.name}, ${next.validity}, not checking (${checked.errors[0].message}); change the expression first`, `$.components.${next.id}.validity`);
|
|
710
|
+
}
|
|
711
|
+
// ---------------------------------------------------------------------------
|
|
712
|
+
// Usage
|
|
713
|
+
/**
|
|
714
|
+
* How many of `component`'s expressions and conditions name the property
|
|
715
|
+
* (or, with `optionId`, that option of an enum property), plus the
|
|
716
|
+
* `propertyValues.<id>` expressions of nested instances of it in other
|
|
717
|
+
* components that name the option. The editor shows it before a deletion
|
|
718
|
+
* or a kind change ("3 expressions use size").
|
|
719
|
+
* @since 0.24.0
|
|
720
|
+
*/
|
|
721
|
+
export function componentPropertyUsage(document, componentId, propertyId, optionId) {
|
|
722
|
+
const { component, property } = propertyOf(document, componentId, propertyId);
|
|
723
|
+
const option = optionId === undefined || property.kind !== "enum"
|
|
724
|
+
? undefined
|
|
725
|
+
: property.options.find((candidate) => candidate.id === optionId);
|
|
726
|
+
if (optionId !== undefined && option === undefined)
|
|
727
|
+
return 0;
|
|
728
|
+
const countIn = (owner, enumName, select) => {
|
|
729
|
+
let count = 0;
|
|
730
|
+
for (const site of componentExpressionSites(document, owner)) {
|
|
731
|
+
if (!select(site.field))
|
|
732
|
+
continue;
|
|
733
|
+
const node = entryOf(document.nodes, site.nodeId);
|
|
734
|
+
const context = expressionFieldContext(document, owner, node, site.field);
|
|
735
|
+
if (context === undefined)
|
|
736
|
+
continue;
|
|
737
|
+
const { references } = checkExpressionSource(site.source, context.expected, context.environment);
|
|
738
|
+
const uses = references.some((reference) => option === undefined
|
|
739
|
+
? reference.kind === "property" && reference.name === property.name && owner === component
|
|
740
|
+
: reference.kind === "option" && reference.enum === enumName && reference.name === option.name);
|
|
741
|
+
if (uses)
|
|
742
|
+
count += 1;
|
|
743
|
+
}
|
|
744
|
+
return count;
|
|
745
|
+
};
|
|
746
|
+
let total = countIn(component, property.name, () => true);
|
|
747
|
+
if (option !== undefined) {
|
|
748
|
+
const field = `propertyValues.${propertyId}`;
|
|
749
|
+
const owners = new Set(nestedInstancesOf(document, componentId).map(({ owner }) => owner));
|
|
750
|
+
for (const owner of owners) {
|
|
751
|
+
total += countIn(owner, nestedPropertyEnumName(component, property), (candidate) => candidate === field);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return total;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Renames a property and rewrites every expression in the component that
|
|
758
|
+
* reads it, in one edit. Throws a `ModelError` when the name is not an
|
|
759
|
+
* identifier or another property has it.
|
|
760
|
+
* @since 0.24.0
|
|
761
|
+
*/
|
|
762
|
+
export function renameComponentPropertyOperations(document, componentId, propertyId, written) {
|
|
763
|
+
const { component, index, property } = propertyOf(document, componentId, propertyId);
|
|
764
|
+
const path = `$.components.${componentId}.properties[${index}].name`;
|
|
765
|
+
const name = requireName(written, path);
|
|
766
|
+
if (name === property.name)
|
|
767
|
+
return { operations: [], count: 0 };
|
|
768
|
+
if (component.properties.some((other) => other.name === name)) {
|
|
769
|
+
throw new ModelError("INVALID_SCHEMA", `Property name ${name} is used twice`, path);
|
|
770
|
+
}
|
|
771
|
+
const edits = new NodeEdits(document);
|
|
772
|
+
const renames = [{ kind: "property", from: property.name, to: name }];
|
|
773
|
+
const count = rewriteComponent(document, component, edits, renames);
|
|
774
|
+
const renamed = renamedValidity(component, replaceProperty(component, index, { ...property, name }), renames);
|
|
775
|
+
return {
|
|
776
|
+
operations: [
|
|
777
|
+
{ type: "component.replace", component: renamed.component },
|
|
778
|
+
...edits.operations(),
|
|
779
|
+
],
|
|
780
|
+
count: count + renamed.count,
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Renames an option of an enum property and rewrites every expression that
|
|
785
|
+
* names it: in the component, and in `propertyValues.<id>` expressions of
|
|
786
|
+
* nested instances of the component inside other components. Option IDs
|
|
787
|
+
* do not change, so instance values need no edit.
|
|
788
|
+
* @since 0.24.0
|
|
789
|
+
*/
|
|
790
|
+
export function renameComponentPropertyOptionOperations(document, componentId, propertyId, optionId, written) {
|
|
791
|
+
const { component, index, property } = propertyOf(document, componentId, propertyId);
|
|
792
|
+
const optionIndex = property.kind === "enum" ? property.options.findIndex((option) => option.id === optionId) : -1;
|
|
793
|
+
if (property.kind !== "enum" || optionIndex < 0) {
|
|
794
|
+
throw new ModelError("INVALID_TREE", `Missing option ${optionId}`, `$.components.${componentId}.properties[${index}].options`);
|
|
795
|
+
}
|
|
796
|
+
const path = `$.components.${componentId}.properties[${index}].options[${optionIndex}].name`;
|
|
797
|
+
const name = requireName(written, path);
|
|
798
|
+
const option = property.options[optionIndex];
|
|
799
|
+
if (name === option.name)
|
|
800
|
+
return { operations: [], count: 0 };
|
|
801
|
+
if (property.options.some((other) => other.name === name)) {
|
|
802
|
+
throw new ModelError("INVALID_SCHEMA", `Option name ${name} is used twice`, path);
|
|
803
|
+
}
|
|
804
|
+
const edits = new NodeEdits(document);
|
|
805
|
+
let count = rewriteComponent(document, component, edits, [
|
|
806
|
+
{ kind: "option", enum: property.name, from: option.name, to: name },
|
|
807
|
+
]);
|
|
808
|
+
const field = `propertyValues.${propertyId}`;
|
|
809
|
+
const owners = new Set(nestedInstancesOf(document, componentId).map(({ owner }) => owner));
|
|
810
|
+
for (const owner of owners) {
|
|
811
|
+
count += rewriteComponent(document, owner, edits, [{ kind: "option", enum: nestedPropertyEnumName(component, property), from: option.name, to: name }], (node, candidate) => node.type === "instance" && node.componentId === componentId && candidate === field);
|
|
812
|
+
}
|
|
813
|
+
const options = property.options.map((candidate) => (candidate.id === optionId ? { ...candidate, name } : candidate));
|
|
814
|
+
const renamed = renamedValidity(component, replaceProperty(component, index, { ...property, options }), [
|
|
815
|
+
{ kind: "option", enum: property.name, from: option.name, to: name },
|
|
816
|
+
]);
|
|
817
|
+
return {
|
|
818
|
+
operations: [
|
|
819
|
+
{ type: "component.replace", component: renamed.component },
|
|
820
|
+
...edits.operations(),
|
|
821
|
+
],
|
|
822
|
+
count: count + renamed.count,
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Removes the values of one property, where `drop` says so, from every
|
|
827
|
+
* instance of the component: instance nodes on pages and in components,
|
|
828
|
+
* and overrides whose layer is a nested instance of it (or swaps to it).
|
|
829
|
+
*/
|
|
830
|
+
function dropPropertyValues(document, componentId, propertyId, drop, edits) {
|
|
831
|
+
const without = (values) => {
|
|
832
|
+
const value = entryOf(values, propertyId);
|
|
833
|
+
if (value === undefined || !drop(value))
|
|
834
|
+
return false;
|
|
835
|
+
const next = { ...values };
|
|
836
|
+
delete next[propertyId];
|
|
837
|
+
return Object.keys(next).length === 0 ? undefined : next;
|
|
838
|
+
};
|
|
839
|
+
for (const stored of tableValues(document.nodes)) {
|
|
840
|
+
if (stored.type !== "instance")
|
|
841
|
+
continue;
|
|
842
|
+
let node = edits.get(stored.id);
|
|
843
|
+
if (node.componentId === componentId) {
|
|
844
|
+
const values = without(node.propertyValues);
|
|
845
|
+
if (values !== false) {
|
|
846
|
+
const next = { ...node };
|
|
847
|
+
if (values === undefined)
|
|
848
|
+
delete next.propertyValues;
|
|
849
|
+
else
|
|
850
|
+
next.propertyValues = values;
|
|
851
|
+
node = next;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
let overridesChanged = false;
|
|
855
|
+
const overrides = { ...node.overrides };
|
|
856
|
+
for (const [key, override] of Object.entries(node.overrides ?? {})) {
|
|
857
|
+
const lastId = key.split(";").at(-1);
|
|
858
|
+
const layer = entryOf(document.nodes, lastId);
|
|
859
|
+
const target = override.componentId ?? (layer?.type === "instance" ? layer.componentId : undefined);
|
|
860
|
+
if (target !== componentId)
|
|
861
|
+
continue;
|
|
862
|
+
const values = without(override.propertyValues);
|
|
863
|
+
if (values === false)
|
|
864
|
+
continue;
|
|
865
|
+
const next = { ...override };
|
|
866
|
+
if (values === undefined)
|
|
867
|
+
delete next.propertyValues;
|
|
868
|
+
else
|
|
869
|
+
next.propertyValues = values;
|
|
870
|
+
overridesChanged = true;
|
|
871
|
+
if (Object.keys(next).length === 0)
|
|
872
|
+
delete overrides[key];
|
|
873
|
+
else
|
|
874
|
+
putEntry(overrides, key, next);
|
|
875
|
+
}
|
|
876
|
+
if (overridesChanged) {
|
|
877
|
+
const next = { ...node };
|
|
878
|
+
if (Object.keys(overrides).length === 0)
|
|
879
|
+
delete next.overrides;
|
|
880
|
+
else
|
|
881
|
+
next.overrides = overrides;
|
|
882
|
+
node = next;
|
|
883
|
+
}
|
|
884
|
+
if (node !== stored && node !== edits.get(stored.id))
|
|
885
|
+
edits.set(node);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* The operations that drop, from every instance of `componentId` and every
|
|
890
|
+
* override of a nested instance of it, the values that `properties`, the
|
|
891
|
+
* component's properties after an edit, no longer take: each value of a
|
|
892
|
+
* property it leaves out, and each value that does not fit a property's
|
|
893
|
+
* new definition (`propertyValueFits`), so that the instance takes the
|
|
894
|
+
* default. Empty when there is nothing to drop; the definition itself is
|
|
895
|
+
* not written.
|
|
896
|
+
* @since 0.32.0
|
|
897
|
+
*/
|
|
898
|
+
export function propertyValueDropOperations(document, componentId, properties) {
|
|
899
|
+
const component = componentOf(document, componentId);
|
|
900
|
+
const edits = new NodeEdits(document);
|
|
901
|
+
for (const property of component.properties ?? []) {
|
|
902
|
+
const next = properties.find((candidate) => candidate.id === property.id);
|
|
903
|
+
dropPropertyValues(document, componentId, property.id, next === undefined ? () => true : (value) => !propertyValueFits(next, value), edits);
|
|
904
|
+
}
|
|
905
|
+
return edits.operations();
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Deletes a property. In the same edit it removes every instance's value
|
|
909
|
+
* for it (instance nodes and nested instance overrides) and the
|
|
910
|
+
* `propertyValues.<id>` expressions of nested instances of the component.
|
|
911
|
+
* Expressions that read the property are kept and stop checking, so they
|
|
912
|
+
* fall back to the stored value; `count` says how many there are.
|
|
913
|
+
* @since 0.24.0
|
|
914
|
+
*/
|
|
915
|
+
export function deleteComponentPropertyOperations(document, componentId, propertyId) {
|
|
916
|
+
const { component, index, property } = propertyOf(document, componentId, propertyId);
|
|
917
|
+
const next = replaceProperty(component, index, undefined);
|
|
918
|
+
requireCheckingValidity(next, `Deleting ${property.name}`);
|
|
919
|
+
const count = componentPropertyUsage(document, componentId, propertyId);
|
|
920
|
+
const edits = new NodeEdits(document);
|
|
921
|
+
dropPropertyValues(document, componentId, propertyId, () => true, edits);
|
|
922
|
+
const field = `propertyValues.${propertyId}`;
|
|
923
|
+
for (const { node } of nestedInstancesOf(document, componentId)) {
|
|
924
|
+
if (node.expressions?.[field] !== undefined)
|
|
925
|
+
edits.set(withoutExpressionField(edits.get(node.id), field));
|
|
926
|
+
}
|
|
927
|
+
return {
|
|
928
|
+
operations: [
|
|
929
|
+
{ type: "component.replace", component: next },
|
|
930
|
+
...edits.operations(),
|
|
931
|
+
],
|
|
932
|
+
count,
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Deletes one option of an enum property. Instance values that chose it are
|
|
937
|
+
* removed in the same edit, so those instances take the default; when the
|
|
938
|
+
* option was the default, the first remaining option becomes the default.
|
|
939
|
+
* Expressions that name the option are kept and stop checking; `count` says
|
|
940
|
+
* how many. Throws a `ModelError` for the last option.
|
|
941
|
+
* @since 0.24.0
|
|
942
|
+
*/
|
|
943
|
+
export function deleteComponentPropertyOptionOperations(document, componentId, propertyId, optionId) {
|
|
944
|
+
const { component, index, property } = propertyOf(document, componentId, propertyId);
|
|
945
|
+
const path = `$.components.${componentId}.properties[${index}].options`;
|
|
946
|
+
if (property.kind !== "enum" || !property.options.some((option) => option.id === optionId)) {
|
|
947
|
+
throw new ModelError("INVALID_TREE", `Missing option ${optionId}`, path);
|
|
948
|
+
}
|
|
949
|
+
if (property.options.length === 1) {
|
|
950
|
+
throw new ModelError("INVALID_SCHEMA", "An enum property needs at least one option", path);
|
|
951
|
+
}
|
|
952
|
+
const count = componentPropertyUsage(document, componentId, propertyId, optionId);
|
|
953
|
+
const options = property.options.filter((option) => option.id !== optionId);
|
|
954
|
+
const defaultValue = property.defaultValue === optionId ? options[0].id : property.defaultValue;
|
|
955
|
+
const next = replaceProperty(component, index, { ...property, options, defaultValue });
|
|
956
|
+
requireCheckingValidity(next, `Deleting the option ${property.options.find((option) => option.id === optionId).name} of ${property.name}`);
|
|
957
|
+
const edits = new NodeEdits(document);
|
|
958
|
+
dropPropertyValues(document, componentId, propertyId, (value) => value === optionId, edits);
|
|
959
|
+
return {
|
|
960
|
+
operations: [
|
|
961
|
+
{ type: "component.replace", component: next },
|
|
962
|
+
...edits.operations(),
|
|
963
|
+
],
|
|
964
|
+
count,
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Replaces a property's definition with one of another kind (or the same
|
|
969
|
+
* kind with other settings), keeping its ID and name; rename with
|
|
970
|
+
* `renameComponentPropertyOperations`. Instance values that do not fit the
|
|
971
|
+
* new definition are removed in the same edit. Expressions that read the
|
|
972
|
+
* property, and `propertyValues.<id>` expressions of nested instances of
|
|
973
|
+
* the component, are kept and may stop checking; `count` is how many there
|
|
974
|
+
* are (0 when the kind is unchanged and so are a number's unit and an
|
|
975
|
+
* enum's option names: a new option makes a `match` non-exhaustive and a
|
|
976
|
+
* removed one leaves references unresolved). Throws a `ModelError` when a
|
|
977
|
+
* number property's minimum is above its maximum.
|
|
978
|
+
* @since 0.24.0
|
|
979
|
+
*/
|
|
980
|
+
export function changeComponentPropertyOperations(document, componentId, next) {
|
|
981
|
+
const { component, index, property } = propertyOf(document, componentId, next.id);
|
|
982
|
+
if (next.name !== property.name) {
|
|
983
|
+
throw new ModelError("INVALID_SCHEMA", "Changing a property keeps its name; rename it separately", `$.components.${componentId}.properties[${index}].name`);
|
|
984
|
+
}
|
|
985
|
+
if (next.kind === "number" && next.min !== undefined && next.max !== undefined && next.min > next.max) {
|
|
986
|
+
throw new ModelError("INVALID_SCHEMA", "A property's minimum must not be above its maximum", `$.components.${componentId}.properties[${index}].max`);
|
|
987
|
+
}
|
|
988
|
+
const field = `propertyValues.${next.id}`;
|
|
989
|
+
const count = sameExpressionType(property, next)
|
|
990
|
+
? 0
|
|
991
|
+
: componentPropertyUsage(document, componentId, next.id) +
|
|
992
|
+
nestedInstancesOf(document, componentId).filter(({ node }) => node.expressions?.[field] !== undefined).length;
|
|
993
|
+
const replaced = replaceProperty(component, index, next);
|
|
994
|
+
requireCheckingValidity(replaced, `Changing ${property.name}`);
|
|
995
|
+
const edits = new NodeEdits(document);
|
|
996
|
+
// Whether a value fits does not depend on the default, so a new default
|
|
997
|
+
// alone drops no value and reads no instance.
|
|
998
|
+
if (!differsOnlyInDefault(property, next)) {
|
|
999
|
+
dropPropertyValues(document, componentId, next.id, (value) => !propertyValueFits(next, value), edits);
|
|
1000
|
+
}
|
|
1001
|
+
return {
|
|
1002
|
+
operations: [{ type: "component.replace", component: replaced }, ...edits.operations()],
|
|
1003
|
+
count,
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
/** True when `next` is `previous` with another default value, or the same. */
|
|
1007
|
+
function differsOnlyInDefault(previous, next) {
|
|
1008
|
+
const { defaultValue: _previousDefault, ...previousRest } = previous;
|
|
1009
|
+
const { defaultValue: _nextDefault, ...nextRest } = next;
|
|
1010
|
+
return canonicalJson(previousRest) === canonicalJson(nextRest);
|
|
1011
|
+
}
|
|
1012
|
+
/** True when expressions reading `next` check as they did reading `previous`. */
|
|
1013
|
+
function sameExpressionType(previous, next) {
|
|
1014
|
+
if (previous.kind !== next.kind)
|
|
1015
|
+
return false;
|
|
1016
|
+
if (previous.kind === "number" && next.kind === "number")
|
|
1017
|
+
return previous.unit === next.unit;
|
|
1018
|
+
if (previous.kind !== "enum" || next.kind !== "enum")
|
|
1019
|
+
return true;
|
|
1020
|
+
const names = new Set(previous.options.map((option) => option.name));
|
|
1021
|
+
return next.options.length === names.size && next.options.every((option) => names.has(option.name));
|
|
1022
|
+
}
|
|
1023
|
+
function renameVariablesOperations(document, theme, renames) {
|
|
1024
|
+
const edits = new NodeEdits(document);
|
|
1025
|
+
let count = 0;
|
|
1026
|
+
if (renames.length > 0) {
|
|
1027
|
+
for (const component of Object.values(document.components ?? {})) {
|
|
1028
|
+
count += rewriteComponent(document, component, edits, renames);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
return { operations: [{ type: "theme.replace", theme }, ...edits.operations()], count };
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Renames a variable and rewrites every `var("Collection/Name")` that reads
|
|
1035
|
+
* it, in every component, in one edit.
|
|
1036
|
+
* @since 0.24.0
|
|
1037
|
+
*/
|
|
1038
|
+
export function renameVariableOperations(document, variableId, name) {
|
|
1039
|
+
const located = findVariable(document.theme, variableId);
|
|
1040
|
+
if (located === undefined)
|
|
1041
|
+
throw new ModelError("INVALID_TREE", `Missing variable ${variableId}`, "$.theme.variableCollections");
|
|
1042
|
+
if (name === located.variable.name)
|
|
1043
|
+
return { operations: [], count: 0 };
|
|
1044
|
+
const { collectionId, collection, variable } = located;
|
|
1045
|
+
const theme = {
|
|
1046
|
+
...document.theme,
|
|
1047
|
+
variableCollections: {
|
|
1048
|
+
...document.theme.variableCollections,
|
|
1049
|
+
[collectionId]: { ...collection, variables: { ...collection.variables, [variableId]: { ...variable, name } } },
|
|
1050
|
+
},
|
|
1051
|
+
};
|
|
1052
|
+
return renameVariablesOperations(document, theme, [
|
|
1053
|
+
{
|
|
1054
|
+
kind: "variable",
|
|
1055
|
+
from: variableExpressionName(collection.name, variable.name),
|
|
1056
|
+
to: variableExpressionName(collection.name, name),
|
|
1057
|
+
},
|
|
1058
|
+
]);
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Renames a variable collection and rewrites every `var("Collection/Name")`
|
|
1062
|
+
* that reads one of its variables, in every component, in one edit.
|
|
1063
|
+
* @since 0.24.0
|
|
1064
|
+
*/
|
|
1065
|
+
export function renameVariableCollectionOperations(document, collectionId, name) {
|
|
1066
|
+
const collection = entryOf(document.theme.variableCollections, collectionId);
|
|
1067
|
+
if (collection === undefined) {
|
|
1068
|
+
throw new ModelError("INVALID_TREE", `Missing variable collection ${collectionId}`, "$.theme.variableCollections");
|
|
1069
|
+
}
|
|
1070
|
+
if (name === collection.name)
|
|
1071
|
+
return { operations: [], count: 0 };
|
|
1072
|
+
const theme = {
|
|
1073
|
+
...document.theme,
|
|
1074
|
+
variableCollections: { ...document.theme.variableCollections, [collectionId]: { ...collection, name } },
|
|
1075
|
+
};
|
|
1076
|
+
const renames = Object.values(collection.variables).map((variable) => ({
|
|
1077
|
+
kind: "variable",
|
|
1078
|
+
from: variableExpressionName(collection.name, variable.name),
|
|
1079
|
+
to: variableExpressionName(name, variable.name),
|
|
1080
|
+
}));
|
|
1081
|
+
return renameVariablesOperations(document, theme, renames);
|
|
1082
|
+
}
|