@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,15 @@
|
|
|
1
|
+
import { type ExpressionValue } from "./values.ts";
|
|
2
|
+
/** Up to four decimals, without trailing zeros and never in exponent form. */
|
|
3
|
+
export declare function formatExpressionNumber(value: number): string;
|
|
4
|
+
/** A string literal with `"`, `\` and line breaks escaped as the lexer reads them. */
|
|
5
|
+
export declare function formatExpressionString(value: string): string;
|
|
6
|
+
type NumberSource = (value: number) => string;
|
|
7
|
+
/**
|
|
8
|
+
* The value as expression source text that evaluates back to it, such as
|
|
9
|
+
* `8px`, `.large`, `insets(4px, 8px)` or `solid(#ff0000, 0.5)`, or
|
|
10
|
+
* undefined when the language cannot write it: a component (there is no
|
|
11
|
+
* component literal), a gradient with uneven stops or a paint with a
|
|
12
|
+
* blend mode. Numbers keep four decimals unless a formatter is supplied.
|
|
13
|
+
*/
|
|
14
|
+
export declare function expressionValueSource(value: ExpressionValue, format?: NumberSource): string | undefined;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Writes expression values back as source text (docs/components/design.md
|
|
2
|
+
// 8.8). A value table row holds a constant; the field control beside it
|
|
3
|
+
// edits a value, and this turns the value into the constant's source. The
|
|
4
|
+
// same text is what the expression editor shows as a result.
|
|
5
|
+
import { formatExpressionName } from "./names.js";
|
|
6
|
+
import { expressionColorFromHex, expressionColorToHex } from "./values.js";
|
|
7
|
+
/** Up to four decimals, without trailing zeros and never in exponent form. */
|
|
8
|
+
export function formatExpressionNumber(value) {
|
|
9
|
+
if (!Number.isFinite(value))
|
|
10
|
+
return "0";
|
|
11
|
+
// `toFixed` switches to exponent form from 1e21, which the lexer cannot
|
|
12
|
+
// read; numbers that large are whole, so BigInt writes every digit.
|
|
13
|
+
if (Math.abs(value) >= 1e21)
|
|
14
|
+
return BigInt(value).toString();
|
|
15
|
+
const fixed = value.toFixed(4).replace(/\.?0+$/, "");
|
|
16
|
+
return fixed === "-0" ? "0" : fixed;
|
|
17
|
+
}
|
|
18
|
+
/** A string literal with `"`, `\` and line breaks escaped as the lexer reads them. */
|
|
19
|
+
export function formatExpressionString(value) {
|
|
20
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n")}"`;
|
|
21
|
+
}
|
|
22
|
+
const px = (value, format) => `${format(value)}px`;
|
|
23
|
+
/** True when the object has no keys but `allowed` with a defined value. */
|
|
24
|
+
function onlyKeys(value, allowed) {
|
|
25
|
+
return Object.entries(value).every(([key, item]) => item === undefined || allowed.includes(key));
|
|
26
|
+
}
|
|
27
|
+
function hex(color) {
|
|
28
|
+
return expressionColorToHex(expressionColorFromHex(color));
|
|
29
|
+
}
|
|
30
|
+
function evenlySpaced(stops) {
|
|
31
|
+
if (stops.length < 2)
|
|
32
|
+
return false;
|
|
33
|
+
const last = stops.length - 1;
|
|
34
|
+
return stops.every((stop, i) => Math.abs(stop.offset - i / last) < 1e-6);
|
|
35
|
+
}
|
|
36
|
+
function paintSource(paint, format) {
|
|
37
|
+
if (paint.blendMode !== undefined && paint.blendMode !== "normal")
|
|
38
|
+
return undefined;
|
|
39
|
+
if (paint.visible === false) {
|
|
40
|
+
const source = paintSource({ ...paint, visible: true }, format);
|
|
41
|
+
return source === undefined ? undefined : `(${source}) with { visible: false }`;
|
|
42
|
+
}
|
|
43
|
+
const common = ["kind", "visible", "blendMode", "opacity"];
|
|
44
|
+
switch (paint.kind) {
|
|
45
|
+
case "solid": {
|
|
46
|
+
if (!onlyKeys(paint, [...common, "color"]))
|
|
47
|
+
return undefined;
|
|
48
|
+
const opacity = paint.opacity ?? 1;
|
|
49
|
+
return opacity === 1 ? `solid(${hex(paint.color)})` : `solid(${hex(paint.color)}, ${format(opacity)})`;
|
|
50
|
+
}
|
|
51
|
+
case "linear-gradient": return undefined; // The expression constructor cannot encode authored endpoints.
|
|
52
|
+
case "radial-gradient":
|
|
53
|
+
case "angular-gradient": {
|
|
54
|
+
// The constructors have no opacity, anchors or uneven stops.
|
|
55
|
+
if ((paint.opacity ?? 1) !== 1 || !evenlySpaced(paint.stops))
|
|
56
|
+
return undefined;
|
|
57
|
+
const colors = paint.stops.map((stop) => hex(stop.color)).join(", ");
|
|
58
|
+
if (paint.kind === "radial-gradient") {
|
|
59
|
+
if (!onlyKeys(paint, [...common, "stops"]))
|
|
60
|
+
return undefined;
|
|
61
|
+
return `radial(${colors})`;
|
|
62
|
+
}
|
|
63
|
+
if (!onlyKeys(paint, [...common, "angle", "stops"]))
|
|
64
|
+
return undefined;
|
|
65
|
+
return `angular(${format(paint.angle ?? 0)}deg, ${colors})`;
|
|
66
|
+
}
|
|
67
|
+
case "image":
|
|
68
|
+
return `image(${formatExpressionString(paint.assetId)}, .${paint.fit ?? "cover"}) with { opacity: ${format(paint.opacity ?? 1)} }`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function strokeSource(stroke, format) {
|
|
72
|
+
const paint = paintSource(stroke.paint, format);
|
|
73
|
+
if (paint === undefined)
|
|
74
|
+
return undefined;
|
|
75
|
+
const position = stroke.position ?? "center";
|
|
76
|
+
const source = position !== "center" ? `stroke(${paint}, ${px(stroke.width, format)}, .${position})`
|
|
77
|
+
: stroke.width === 1 ? `stroke(${paint})` : `stroke(${paint}, ${px(stroke.width, format)})`;
|
|
78
|
+
const fields = [
|
|
79
|
+
...(stroke.join === undefined ? [] : [`join: .${stroke.join}`]),
|
|
80
|
+
...(stroke.miterAngle === undefined ? [] : [`miter_angle: ${format(stroke.miterAngle)}deg`]),
|
|
81
|
+
...(stroke.dash === undefined ? [] : [`dash: [${stroke.dash.map((n) => px(n, format)).join(", ")}]`]),
|
|
82
|
+
...(stroke.dashCap === undefined ? [] : [`dash_cap: .${stroke.dashCap}`]),
|
|
83
|
+
...(stroke.sides === undefined ? [] : [`sides: ${expressionValueSource({ kind: "insets", value: stroke.sides }, format)}`]),
|
|
84
|
+
];
|
|
85
|
+
return fields.length === 0 ? source : `${source} with { ${fields.join(", ")} }`;
|
|
86
|
+
}
|
|
87
|
+
function shadowSource(shadow, format) {
|
|
88
|
+
if (!onlyKeys(shadow, ["kind", "x", "y", "blur", "spread", "color"]))
|
|
89
|
+
return undefined;
|
|
90
|
+
const name = shadow.kind === "inner" ? "inner_shadow" : "shadow";
|
|
91
|
+
return `${name}(${px(shadow.x, format)}, ${px(shadow.y, format)}, ${px(shadow.blur, format)}, ${px(shadow.spread, format)}, ${hex(shadow.color)})`;
|
|
92
|
+
}
|
|
93
|
+
function effectSource(effect, format) {
|
|
94
|
+
if (effect.kind === "shadow")
|
|
95
|
+
return shadowSource(effect.shadow, format);
|
|
96
|
+
return `${effect.kind}(${px(effect.radius, format)})`;
|
|
97
|
+
}
|
|
98
|
+
function textStyleSource(style, format) {
|
|
99
|
+
return `text_style(${formatExpressionString(style.fontFamily)}, ${format(style.fontWeight)}, ${px(style.fontSize, format)}, ${px(style.lineHeightPixels, format)}, ${px(style.letterSpacing, format)})`;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The value as expression source text that evaluates back to it, such as
|
|
103
|
+
* `8px`, `.large`, `insets(4px, 8px)` or `solid(#ff0000, 0.5)`, or
|
|
104
|
+
* undefined when the language cannot write it: a component (there is no
|
|
105
|
+
* component literal), a gradient with uneven stops or a paint with a
|
|
106
|
+
* blend mode. Numbers keep four decimals unless a formatter is supplied.
|
|
107
|
+
*/
|
|
108
|
+
export function expressionValueSource(value, format = formatExpressionNumber) {
|
|
109
|
+
switch (value.kind) {
|
|
110
|
+
case "boolean":
|
|
111
|
+
return value.value ? "true" : "false";
|
|
112
|
+
case "number":
|
|
113
|
+
return format(value.value);
|
|
114
|
+
case "length":
|
|
115
|
+
return px(value.value, format);
|
|
116
|
+
case "percent":
|
|
117
|
+
return `${format(value.value)}%`;
|
|
118
|
+
case "angle":
|
|
119
|
+
return `${format(value.value)}deg`;
|
|
120
|
+
case "string":
|
|
121
|
+
return formatExpressionString(value.value);
|
|
122
|
+
case "color":
|
|
123
|
+
return expressionColorToHex(value.value);
|
|
124
|
+
case "component":
|
|
125
|
+
return undefined;
|
|
126
|
+
case "enum":
|
|
127
|
+
return `.${formatExpressionName(value.option)}`;
|
|
128
|
+
case "point":
|
|
129
|
+
return `point(${px(value.x, format)}, ${px(value.y, format)})`;
|
|
130
|
+
case "size":
|
|
131
|
+
return `size(${px(value.width, format)}, ${px(value.height, format)})`;
|
|
132
|
+
case "insets": {
|
|
133
|
+
const { top, right, bottom, left } = value.value;
|
|
134
|
+
if (top === right && top === bottom && top === left)
|
|
135
|
+
return `insets(${px(top, format)})`;
|
|
136
|
+
if (top === bottom && left === right)
|
|
137
|
+
return `insets(${px(top, format)}, ${px(right, format)})`;
|
|
138
|
+
return `insets(${px(top, format)}, ${px(right, format)}, ${px(bottom, format)}, ${px(left, format)})`;
|
|
139
|
+
}
|
|
140
|
+
case "corners": {
|
|
141
|
+
const { topLeft, topRight, bottomRight, bottomLeft } = value.value;
|
|
142
|
+
if (topLeft === topRight && topLeft === bottomRight && topLeft === bottomLeft)
|
|
143
|
+
return `corners(${px(topLeft, format)})`;
|
|
144
|
+
return `corners(${px(topLeft, format)}, ${px(topRight, format)}, ${px(bottomRight, format)}, ${px(bottomLeft, format)})`;
|
|
145
|
+
}
|
|
146
|
+
case "paint":
|
|
147
|
+
return paintSource(value.value, format);
|
|
148
|
+
case "stroke":
|
|
149
|
+
return strokeSource(value.value, format);
|
|
150
|
+
case "effect":
|
|
151
|
+
return effectSource(value.value, format);
|
|
152
|
+
case "text_style":
|
|
153
|
+
return textStyleSource(value.value, format);
|
|
154
|
+
case "list": {
|
|
155
|
+
const items = value.items.map((item) => expressionValueSource(item, format));
|
|
156
|
+
if (items.some((item) => item === undefined))
|
|
157
|
+
return undefined;
|
|
158
|
+
return `[${items.join(", ")}]`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** The unit types: numbers that carry a unit and never mix with each other. */
|
|
2
|
+
export type ExpressionUnitTypeKind = "length" | "percent" | "angle";
|
|
3
|
+
/** An enum type: a named set of options. Two enum types are the same when their names match. */
|
|
4
|
+
export interface ExpressionEnumType {
|
|
5
|
+
readonly kind: "enum";
|
|
6
|
+
/**
|
|
7
|
+
* Unique within one checking environment. The property model gives each
|
|
8
|
+
* enum property its own name; layer field enums use the field's name.
|
|
9
|
+
*/
|
|
10
|
+
readonly name: string;
|
|
11
|
+
/** Option names in display order, as written after the dot: `.large`. */
|
|
12
|
+
readonly options: readonly string[];
|
|
13
|
+
}
|
|
14
|
+
/** A type of the expression language. Every value has exactly one. */
|
|
15
|
+
export type ExpressionType = {
|
|
16
|
+
readonly kind: "boolean";
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "number";
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: ExpressionUnitTypeKind;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "string";
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: "color";
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: "component";
|
|
27
|
+
} | ExpressionEnumType | {
|
|
28
|
+
readonly kind: "point";
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: "size";
|
|
31
|
+
} | {
|
|
32
|
+
readonly kind: "insets";
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: "corners";
|
|
35
|
+
} | {
|
|
36
|
+
readonly kind: "paint";
|
|
37
|
+
} | {
|
|
38
|
+
readonly kind: "stroke";
|
|
39
|
+
} | {
|
|
40
|
+
readonly kind: "effect";
|
|
41
|
+
} | {
|
|
42
|
+
readonly kind: "text_style";
|
|
43
|
+
} | {
|
|
44
|
+
readonly kind: "list";
|
|
45
|
+
readonly element: ExpressionType;
|
|
46
|
+
};
|
|
47
|
+
/** The types with a single `kind` and nothing else, ready to use. */
|
|
48
|
+
export declare const EXPRESSION_TYPES: {
|
|
49
|
+
readonly boolean: {
|
|
50
|
+
readonly kind: "boolean";
|
|
51
|
+
};
|
|
52
|
+
readonly number: {
|
|
53
|
+
readonly kind: "number";
|
|
54
|
+
};
|
|
55
|
+
readonly length: {
|
|
56
|
+
readonly kind: "length";
|
|
57
|
+
};
|
|
58
|
+
readonly percent: {
|
|
59
|
+
readonly kind: "percent";
|
|
60
|
+
};
|
|
61
|
+
readonly angle: {
|
|
62
|
+
readonly kind: "angle";
|
|
63
|
+
};
|
|
64
|
+
readonly string: {
|
|
65
|
+
readonly kind: "string";
|
|
66
|
+
};
|
|
67
|
+
readonly color: {
|
|
68
|
+
readonly kind: "color";
|
|
69
|
+
};
|
|
70
|
+
readonly component: {
|
|
71
|
+
readonly kind: "component";
|
|
72
|
+
};
|
|
73
|
+
readonly point: {
|
|
74
|
+
readonly kind: "point";
|
|
75
|
+
};
|
|
76
|
+
readonly size: {
|
|
77
|
+
readonly kind: "size";
|
|
78
|
+
};
|
|
79
|
+
readonly insets: {
|
|
80
|
+
readonly kind: "insets";
|
|
81
|
+
};
|
|
82
|
+
readonly corners: {
|
|
83
|
+
readonly kind: "corners";
|
|
84
|
+
};
|
|
85
|
+
readonly paint: {
|
|
86
|
+
readonly kind: "paint";
|
|
87
|
+
};
|
|
88
|
+
readonly stroke: {
|
|
89
|
+
readonly kind: "stroke";
|
|
90
|
+
};
|
|
91
|
+
readonly effect: {
|
|
92
|
+
readonly kind: "effect";
|
|
93
|
+
};
|
|
94
|
+
readonly text_style: {
|
|
95
|
+
readonly kind: "text_style";
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
/** `list(element)`. */
|
|
99
|
+
export declare function expressionListType(element: ExpressionType): ExpressionType;
|
|
100
|
+
/** An enum type with the given name and options. */
|
|
101
|
+
export declare function expressionEnumType(name: string, options: readonly string[]): ExpressionEnumType;
|
|
102
|
+
/** True when a value of type `a` is also a value of type `b`. Enums match by name. */
|
|
103
|
+
export declare function expressionTypesEqual(a: ExpressionType, b: ExpressionType): boolean;
|
|
104
|
+
/** True for `length`, `percent` and `angle`. */
|
|
105
|
+
export declare function isExpressionUnitType(type: ExpressionType): type is {
|
|
106
|
+
readonly kind: ExpressionUnitTypeKind;
|
|
107
|
+
};
|
|
108
|
+
/** True for `number` and the unit types: what arithmetic, comparison and `min` accept. */
|
|
109
|
+
export declare function isExpressionNumeric(type: ExpressionType): boolean;
|
|
110
|
+
/** The type as the language writes it: `length`, `list(paint)`, `Size (.small, .large)`. */
|
|
111
|
+
export declare function formatExpressionType(type: ExpressionType): string;
|
|
112
|
+
/** The enum type of a stroke's `align` argument and field. */
|
|
113
|
+
export declare const STROKE_ALIGN_TYPE: ExpressionEnumType;
|
|
114
|
+
/**
|
|
115
|
+
* The fields a record type exposes to `value.field` and `value with { field: ... }`,
|
|
116
|
+
* or undefined for a type that is not a record. Field names follow the
|
|
117
|
+
* language's snake_case (`top_left`, `line_height`).
|
|
118
|
+
*/
|
|
119
|
+
export declare function expressionRecordFields(type: ExpressionType): Readonly<Record<string, ExpressionType>> | undefined;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The types of the component expression language (docs/components/design.md 8.4).
|
|
2
|
+
//
|
|
3
|
+
// This file is a contract shared by the checker, the evaluator (part 2) and
|
|
4
|
+
// the component property model (step 3.2). Keep it small.
|
|
5
|
+
import { formatExpressionName } from "./names.js";
|
|
6
|
+
/** The types with a single `kind` and nothing else, ready to use. */
|
|
7
|
+
export const EXPRESSION_TYPES = {
|
|
8
|
+
boolean: { kind: "boolean" },
|
|
9
|
+
number: { kind: "number" },
|
|
10
|
+
length: { kind: "length" },
|
|
11
|
+
percent: { kind: "percent" },
|
|
12
|
+
angle: { kind: "angle" },
|
|
13
|
+
string: { kind: "string" },
|
|
14
|
+
color: { kind: "color" },
|
|
15
|
+
component: { kind: "component" },
|
|
16
|
+
point: { kind: "point" },
|
|
17
|
+
size: { kind: "size" },
|
|
18
|
+
insets: { kind: "insets" },
|
|
19
|
+
corners: { kind: "corners" },
|
|
20
|
+
paint: { kind: "paint" },
|
|
21
|
+
stroke: { kind: "stroke" },
|
|
22
|
+
effect: { kind: "effect" },
|
|
23
|
+
text_style: { kind: "text_style" },
|
|
24
|
+
};
|
|
25
|
+
/** `list(element)`. */
|
|
26
|
+
export function expressionListType(element) {
|
|
27
|
+
return { kind: "list", element };
|
|
28
|
+
}
|
|
29
|
+
/** An enum type with the given name and options. */
|
|
30
|
+
export function expressionEnumType(name, options) {
|
|
31
|
+
return { kind: "enum", name, options };
|
|
32
|
+
}
|
|
33
|
+
/** True when a value of type `a` is also a value of type `b`. Enums match by name. */
|
|
34
|
+
export function expressionTypesEqual(a, b) {
|
|
35
|
+
if (a.kind !== b.kind)
|
|
36
|
+
return false;
|
|
37
|
+
if (a.kind === "enum")
|
|
38
|
+
return a.name === b.name;
|
|
39
|
+
if (a.kind === "list")
|
|
40
|
+
return expressionTypesEqual(a.element, b.element);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/** True for `length`, `percent` and `angle`. */
|
|
44
|
+
export function isExpressionUnitType(type) {
|
|
45
|
+
return type.kind === "length" || type.kind === "percent" || type.kind === "angle";
|
|
46
|
+
}
|
|
47
|
+
/** True for `number` and the unit types: what arithmetic, comparison and `min` accept. */
|
|
48
|
+
export function isExpressionNumeric(type) {
|
|
49
|
+
return type.kind === "number" || isExpressionUnitType(type);
|
|
50
|
+
}
|
|
51
|
+
/** The type as the language writes it: `length`, `list(paint)`, `Size (.small, .large)`. */
|
|
52
|
+
export function formatExpressionType(type) {
|
|
53
|
+
if (type.kind === "list")
|
|
54
|
+
return `list(${formatExpressionType(type.element)})`;
|
|
55
|
+
if (type.kind === "enum")
|
|
56
|
+
return `${type.name} (${type.options.map((o) => `.${formatExpressionName(o)}`).join(", ")})`;
|
|
57
|
+
return type.kind;
|
|
58
|
+
}
|
|
59
|
+
/** The enum type of a stroke's `align` argument and field. */
|
|
60
|
+
export const STROKE_ALIGN_TYPE = expressionEnumType("stroke_align", ["center", "inside", "outside"]);
|
|
61
|
+
const LENGTH = EXPRESSION_TYPES.length;
|
|
62
|
+
const NUMBER = EXPRESSION_TYPES.number;
|
|
63
|
+
/**
|
|
64
|
+
* The fields a record type exposes to `value.field` and `value with { field: ... }`,
|
|
65
|
+
* or undefined for a type that is not a record. Field names follow the
|
|
66
|
+
* language's snake_case (`top_left`, `line_height`).
|
|
67
|
+
*/
|
|
68
|
+
export function expressionRecordFields(type) {
|
|
69
|
+
switch (type.kind) {
|
|
70
|
+
case "color":
|
|
71
|
+
return { red: NUMBER, green: NUMBER, blue: NUMBER, alpha: NUMBER };
|
|
72
|
+
case "point":
|
|
73
|
+
return { x: LENGTH, y: LENGTH };
|
|
74
|
+
case "size":
|
|
75
|
+
return { width: LENGTH, height: LENGTH };
|
|
76
|
+
case "insets":
|
|
77
|
+
return { top: LENGTH, right: LENGTH, bottom: LENGTH, left: LENGTH };
|
|
78
|
+
case "corners":
|
|
79
|
+
return { top_left: LENGTH, top_right: LENGTH, bottom_right: LENGTH, bottom_left: LENGTH };
|
|
80
|
+
case "paint":
|
|
81
|
+
return { opacity: NUMBER, visible: EXPRESSION_TYPES.boolean };
|
|
82
|
+
case "stroke":
|
|
83
|
+
return {
|
|
84
|
+
paint: EXPRESSION_TYPES.paint, width: LENGTH, align: STROKE_ALIGN_TYPE,
|
|
85
|
+
join: expressionEnumType("stroke join", ["miter", "bevel", "round"]),
|
|
86
|
+
miter_angle: EXPRESSION_TYPES.angle,
|
|
87
|
+
dash: expressionListType(LENGTH),
|
|
88
|
+
dash_cap: expressionEnumType("dash cap", ["none", "round", "square"]),
|
|
89
|
+
sides: EXPRESSION_TYPES.insets,
|
|
90
|
+
};
|
|
91
|
+
case "text_style":
|
|
92
|
+
return {
|
|
93
|
+
family: EXPRESSION_TYPES.string,
|
|
94
|
+
weight: NUMBER,
|
|
95
|
+
size: LENGTH,
|
|
96
|
+
line_height: LENGTH,
|
|
97
|
+
letter_spacing: LENGTH,
|
|
98
|
+
};
|
|
99
|
+
default:
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type CornerRadii, type LayoutPadding, type Paint, type Shadow, type Stroke, type TextStyle } from "../types.ts";
|
|
2
|
+
import type { ExpressionColor } from "./ast.ts";
|
|
3
|
+
import type { ExpressionType, ExpressionUnitTypeKind } from "./types.ts";
|
|
4
|
+
/**
|
|
5
|
+
* The parts of a text style an expression can build; the name and colour come from elsewhere.
|
|
6
|
+
* `lineHeightPixels` is the language's `line_height`, a length in pixels. The model's
|
|
7
|
+
* `TextStyle.lineHeight` is a multiple of the font size, so whatever applies this
|
|
8
|
+
* value must divide by `fontSize` first.
|
|
9
|
+
*/
|
|
10
|
+
export type ExpressionTextStyle = Pick<TextStyle, "fontFamily" | "fontWeight" | "fontSize"> & {
|
|
11
|
+
readonly lineHeightPixels: number;
|
|
12
|
+
readonly letterSpacing: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* One effect. The model keeps shadows in `shadows` and the two blurs as
|
|
16
|
+
* plain radii, so this is the union step 3.3 sorts into those fields.
|
|
17
|
+
*/
|
|
18
|
+
export type ExpressionEffect = {
|
|
19
|
+
readonly kind: "shadow";
|
|
20
|
+
readonly shadow: Shadow;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "layer_blur";
|
|
23
|
+
readonly radius: number;
|
|
24
|
+
} | {
|
|
25
|
+
readonly kind: "background_blur";
|
|
26
|
+
readonly radius: number;
|
|
27
|
+
};
|
|
28
|
+
/** A value of the expression language. `kind` matches the type's `kind`. */
|
|
29
|
+
export type ExpressionValue = {
|
|
30
|
+
readonly kind: "boolean";
|
|
31
|
+
readonly value: boolean;
|
|
32
|
+
} | {
|
|
33
|
+
readonly kind: "number";
|
|
34
|
+
readonly value: number;
|
|
35
|
+
}
|
|
36
|
+
/** A length in pixels, a percentage or an angle in degrees. */
|
|
37
|
+
| {
|
|
38
|
+
readonly kind: ExpressionUnitTypeKind;
|
|
39
|
+
readonly value: number;
|
|
40
|
+
} | {
|
|
41
|
+
readonly kind: "string";
|
|
42
|
+
readonly value: string;
|
|
43
|
+
} | {
|
|
44
|
+
readonly kind: "color";
|
|
45
|
+
readonly value: ExpressionColor;
|
|
46
|
+
}
|
|
47
|
+
/** A component, by its id in the document. */
|
|
48
|
+
| {
|
|
49
|
+
readonly kind: "component";
|
|
50
|
+
readonly id: string;
|
|
51
|
+
}
|
|
52
|
+
/** One option of an enum, by name. */
|
|
53
|
+
| {
|
|
54
|
+
readonly kind: "enum";
|
|
55
|
+
readonly option: string;
|
|
56
|
+
} | {
|
|
57
|
+
readonly kind: "point";
|
|
58
|
+
readonly x: number;
|
|
59
|
+
readonly y: number;
|
|
60
|
+
} | {
|
|
61
|
+
readonly kind: "size";
|
|
62
|
+
readonly width: number;
|
|
63
|
+
readonly height: number;
|
|
64
|
+
} | {
|
|
65
|
+
readonly kind: "insets";
|
|
66
|
+
readonly value: LayoutPadding;
|
|
67
|
+
} | {
|
|
68
|
+
readonly kind: "corners";
|
|
69
|
+
readonly value: CornerRadii;
|
|
70
|
+
} | {
|
|
71
|
+
readonly kind: "paint";
|
|
72
|
+
readonly value: Paint;
|
|
73
|
+
} | {
|
|
74
|
+
readonly kind: "stroke";
|
|
75
|
+
readonly value: Stroke;
|
|
76
|
+
} | {
|
|
77
|
+
readonly kind: "effect";
|
|
78
|
+
readonly value: ExpressionEffect;
|
|
79
|
+
} | {
|
|
80
|
+
readonly kind: "text_style";
|
|
81
|
+
readonly value: ExpressionTextStyle;
|
|
82
|
+
} | {
|
|
83
|
+
readonly kind: "list";
|
|
84
|
+
readonly items: readonly ExpressionValue[];
|
|
85
|
+
};
|
|
86
|
+
/** The colour as the model stores it: `#rrggbb`, or `#rrggbbaa` when the alpha is below 1. */
|
|
87
|
+
export declare function expressionColorToHex(color: ExpressionColor): string;
|
|
88
|
+
/** Reads a model colour string; a string that is not a hex colour reads as opaque black. */
|
|
89
|
+
export declare function expressionColorFromHex(text: string): ExpressionColor;
|
|
90
|
+
/**
|
|
91
|
+
* The value `0` of a type: false, 0, "", transparent black, the first
|
|
92
|
+
* option, empty records and lists. It is what a division by zero gives and
|
|
93
|
+
* what a missing value falls back to.
|
|
94
|
+
*/
|
|
95
|
+
export declare function zeroExpressionValue(type: ExpressionType): ExpressionValue;
|
|
96
|
+
/** True when two values of one type are equal, compared by value; what `==` gives. */
|
|
97
|
+
export declare function expressionValuesEqual(a: ExpressionValue, b: ExpressionValue): boolean;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// The values of the component expression language (docs/components/design.md
|
|
2
|
+
// 8.4, "Values as the model stores them"). Where the model has a type of its
|
|
3
|
+
// own, the value wraps it unchanged, so a result can be put straight into a
|
|
4
|
+
// layer field; the exceptions are written down in that section.
|
|
5
|
+
import { DEFAULT_STROKE_MITER_ANGLE } from "../types.js";
|
|
6
|
+
function hex2(channel) {
|
|
7
|
+
return Math.round(Math.min(1, Math.max(0, channel)) * 255)
|
|
8
|
+
.toString(16)
|
|
9
|
+
.padStart(2, "0");
|
|
10
|
+
}
|
|
11
|
+
/** The colour as the model stores it: `#rrggbb`, or `#rrggbbaa` when the alpha is below 1. */
|
|
12
|
+
export function expressionColorToHex(color) {
|
|
13
|
+
const rgb = `#${hex2(color.red)}${hex2(color.green)}${hex2(color.blue)}`;
|
|
14
|
+
return color.alpha >= 1 ? rgb : rgb + hex2(color.alpha);
|
|
15
|
+
}
|
|
16
|
+
/** Reads a model colour string; a string that is not a hex colour reads as opaque black. */
|
|
17
|
+
export function expressionColorFromHex(text) {
|
|
18
|
+
const digits = text.startsWith("#") ? text.slice(1) : "";
|
|
19
|
+
const expanded = digits.length === 3 || digits.length === 4 ? [...digits].map((d) => d + d).join("") : digits;
|
|
20
|
+
if ((expanded.length !== 6 && expanded.length !== 8) || !/^[0-9A-Fa-f]+$/.test(expanded)) {
|
|
21
|
+
return { red: 0, green: 0, blue: 0, alpha: 1 };
|
|
22
|
+
}
|
|
23
|
+
const channel = (i) => parseInt(expanded.slice(i * 2, i * 2 + 2), 16) / 255;
|
|
24
|
+
return { red: channel(0), green: channel(1), blue: channel(2), alpha: expanded.length === 8 ? channel(3) : 1 };
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The value `0` of a type: false, 0, "", transparent black, the first
|
|
28
|
+
* option, empty records and lists. It is what a division by zero gives and
|
|
29
|
+
* what a missing value falls back to.
|
|
30
|
+
*/
|
|
31
|
+
export function zeroExpressionValue(type) {
|
|
32
|
+
switch (type.kind) {
|
|
33
|
+
case "boolean":
|
|
34
|
+
return { kind: "boolean", value: false };
|
|
35
|
+
case "number":
|
|
36
|
+
case "length":
|
|
37
|
+
case "percent":
|
|
38
|
+
case "angle":
|
|
39
|
+
return { kind: type.kind, value: 0 };
|
|
40
|
+
case "string":
|
|
41
|
+
return { kind: "string", value: "" };
|
|
42
|
+
case "color":
|
|
43
|
+
return { kind: "color", value: { red: 0, green: 0, blue: 0, alpha: 0 } };
|
|
44
|
+
case "component":
|
|
45
|
+
return { kind: "component", id: "" };
|
|
46
|
+
case "enum":
|
|
47
|
+
return { kind: "enum", option: type.options[0] ?? "" };
|
|
48
|
+
case "point":
|
|
49
|
+
return { kind: "point", x: 0, y: 0 };
|
|
50
|
+
case "size":
|
|
51
|
+
return { kind: "size", width: 0, height: 0 };
|
|
52
|
+
case "insets":
|
|
53
|
+
return { kind: "insets", value: { top: 0, right: 0, bottom: 0, left: 0 } };
|
|
54
|
+
case "corners":
|
|
55
|
+
return { kind: "corners", value: { topLeft: 0, topRight: 0, bottomRight: 0, bottomLeft: 0 } };
|
|
56
|
+
case "paint":
|
|
57
|
+
return { kind: "paint", value: { kind: "solid", color: "#000000", opacity: 0 } };
|
|
58
|
+
case "stroke":
|
|
59
|
+
return { kind: "stroke", value: { paint: { kind: "solid", color: "#000000", opacity: 0 }, width: 0, position: "center" } };
|
|
60
|
+
case "effect":
|
|
61
|
+
return { kind: "effect", value: { kind: "layer_blur", radius: 0 } };
|
|
62
|
+
case "text_style":
|
|
63
|
+
return { kind: "text_style", value: { fontFamily: "", fontWeight: 400, fontSize: 0, lineHeightPixels: 0, letterSpacing: 0 } };
|
|
64
|
+
case "list":
|
|
65
|
+
return { kind: "list", items: [] };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** True when two values of one type are equal, compared by value; what `==` gives. */
|
|
69
|
+
export function expressionValuesEqual(a, b) {
|
|
70
|
+
if (a.kind !== b.kind)
|
|
71
|
+
return false;
|
|
72
|
+
switch (a.kind) {
|
|
73
|
+
case "list": {
|
|
74
|
+
const items = b.items;
|
|
75
|
+
return a.items.length === items.length && a.items.every((item, i) => expressionValuesEqual(item, items[i]));
|
|
76
|
+
}
|
|
77
|
+
case "component":
|
|
78
|
+
return a.id === b.id;
|
|
79
|
+
case "enum":
|
|
80
|
+
return a.option === b.option;
|
|
81
|
+
case "point":
|
|
82
|
+
return a.x === b.x && a.y === b.y;
|
|
83
|
+
case "size":
|
|
84
|
+
return a.width === b.width && a.height === b.height;
|
|
85
|
+
case "paint":
|
|
86
|
+
return plainEqual(paintWithDefaults(a.value), paintWithDefaults(b.value));
|
|
87
|
+
case "stroke":
|
|
88
|
+
return plainEqual(strokeWithDefaults(a.value), strokeWithDefaults(b.value));
|
|
89
|
+
case "effect":
|
|
90
|
+
return plainEqual(effectWithDefaults(a.value), effectWithDefaults(b.value));
|
|
91
|
+
default:
|
|
92
|
+
return plainEqual(a.value, b.value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// `==` compares what a value means, so an omitted model field equals its
|
|
96
|
+
// documented default (types.ts): a stored `{ kind: "solid", color }` equals
|
|
97
|
+
// `solid(color)`, which writes `opacity: 1`.
|
|
98
|
+
const CENTER = { x: 0.5, y: 0.5 };
|
|
99
|
+
// A colour compares by value, so `#fff`, `#FFFFFF` and `#ffffffff` are equal.
|
|
100
|
+
// A string that is not a hex colour stays as written rather than reading as black.
|
|
101
|
+
function canonicalColor(text) {
|
|
102
|
+
return /^#([0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/.test(text) ? expressionColorToHex(expressionColorFromHex(text)) : text;
|
|
103
|
+
}
|
|
104
|
+
function stopsWithCanonicalColors(stops) {
|
|
105
|
+
return stops.map((stop) => ({ ...stop, color: canonicalColor(stop.color) }));
|
|
106
|
+
}
|
|
107
|
+
function paintWithDefaults(paint) {
|
|
108
|
+
const common = { ...paint, opacity: paint.opacity ?? 1, visible: paint.visible ?? true, blendMode: paint.blendMode ?? "normal" };
|
|
109
|
+
switch (common.kind) {
|
|
110
|
+
case "solid":
|
|
111
|
+
return { ...common, color: canonicalColor(common.color) };
|
|
112
|
+
case "linear-gradient":
|
|
113
|
+
return { ...common, stops: stopsWithCanonicalColors(common.stops) };
|
|
114
|
+
case "radial-gradient":
|
|
115
|
+
return { ...common, stops: stopsWithCanonicalColors(common.stops), center: common.center ?? CENTER, radius: common.radius ?? CENTER };
|
|
116
|
+
case "angular-gradient":
|
|
117
|
+
return { ...common, stops: stopsWithCanonicalColors(common.stops), center: common.center ?? CENTER, angle: common.angle ?? 0 };
|
|
118
|
+
case "image":
|
|
119
|
+
return { ...common, fit: common.fit ?? "cover" };
|
|
120
|
+
default:
|
|
121
|
+
return common;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// An empty dash is a solid stroke, which ignores the dash cap; side weights
|
|
125
|
+
// that all equal the width draw the same as none (types.ts, `Stroke`).
|
|
126
|
+
function strokeWithDefaults(stroke) {
|
|
127
|
+
const { dash, dashCap, sides, ...rest } = stroke;
|
|
128
|
+
const dashed = dash !== undefined && dash.length > 0;
|
|
129
|
+
const uniformSides = sides !== undefined && [sides.top, sides.right, sides.bottom, sides.left].every((side) => side === stroke.width);
|
|
130
|
+
return {
|
|
131
|
+
...rest,
|
|
132
|
+
paint: paintWithDefaults(stroke.paint),
|
|
133
|
+
position: stroke.position ?? "center",
|
|
134
|
+
miterAngle: stroke.miterAngle ?? DEFAULT_STROKE_MITER_ANGLE,
|
|
135
|
+
...(dashed ? { dash, dashCap } : {}),
|
|
136
|
+
...(uniformSides ? {} : { sides }),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function effectWithDefaults(effect) {
|
|
140
|
+
return effect.kind === "shadow" ? { ...effect, shadow: { ...effect.shadow, kind: effect.shadow.kind ?? "drop", color: canonicalColor(effect.shadow.color) } } : effect;
|
|
141
|
+
}
|
|
142
|
+
/** Structural equality over plain data: numbers, strings, booleans, arrays and objects. */
|
|
143
|
+
function plainEqual(a, b) {
|
|
144
|
+
if (a === b)
|
|
145
|
+
return true;
|
|
146
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
147
|
+
return false;
|
|
148
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
149
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length)
|
|
150
|
+
return false;
|
|
151
|
+
return a.every((item, i) => plainEqual(item, b[i]));
|
|
152
|
+
}
|
|
153
|
+
const left = a;
|
|
154
|
+
const right = b;
|
|
155
|
+
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
|
156
|
+
for (const key of keys)
|
|
157
|
+
if (!plainEqual(left[key], right[key]))
|
|
158
|
+
return false;
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The float32 ceiling used by render-core's Skia adapter (`kMaximumSkScalar`). */
|
|
2
|
+
export declare const MAX_RENDER_SCALAR: number;
|
|
3
|
+
/**
|
|
4
|
+
* Maximum magnitude of authored geometry. Leave room for products (text size
|
|
5
|
+
* times line height, transformed paths), rotated bounds, strokes and effects.
|
|
6
|
+
*/
|
|
7
|
+
export declare const MAX_GEOMETRY: number;
|
|
8
|
+
/**
|
|
9
|
+
* Bounds geometry in document/operation JSON without changing the input or
|
|
10
|
+
* unrelated values. Unchanged branches keep their identity. Schema validation
|
|
11
|
+
* still rejects non-finite numbers and invalid signs before this is called.
|
|
12
|
+
* Group sizes are derived from their children and may exceed the authored
|
|
13
|
+
* limit. Clamping them would invalidate the fit and move rotated groups on
|
|
14
|
+
* every subsequent refit.
|
|
15
|
+
*/
|
|
16
|
+
export declare function clampGeometry<T>(value: T): T;
|