@moondraft/import 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +169 -0
- package/dist/adapter/convert.d.ts +122 -0
- package/dist/adapter/convert.js +2302 -0
- package/dist/adapter/figma.d.ts +9 -0
- package/dist/adapter/figma.js +30 -0
- package/dist/adapter/file-components.d.ts +14 -0
- package/dist/adapter/file-components.js +262 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +562 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +45 -0
- package/dist/adapter/link.js +688 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +44 -0
- package/dist/adapter/variables.js +103 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +96 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +49 -0
- package/dist/report.d.ts +15 -0
- package/dist/report.js +69 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -0
- package/dist/taco/figma-import/import/carry.js +33 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
- package/dist/taco/figma-import/import/componentProps.js +313 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +55 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
- package/dist/taco/figma-import/import/converters/gradient.js +106 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/group.js +39 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +83 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
- package/dist/taco/figma-import/import/converters/layout.js +168 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
- package/dist/taco/figma-import/import/converters/section.js +35 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
- package/dist/taco/figma-import/import/converters/shared.js +362 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
- package/dist/taco/figma-import/import/converters/text.js +576 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
- package/dist/taco/figma-import/import/converters/vector.js +468 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +163 -0
- package/dist/taco/figma-import/import/instances.js +1323 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +46 -0
- package/dist/taco/figma-import/import/styles.js +42 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +136 -0
- package/dist/taco/figma-import/import/variables.js +762 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +480 -0
- package/dist/taco/figma-import/import/walk.d.ts +159 -0
- package/dist/taco/figma-import/import/walk.js +1297 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
- package/dist/taco/figma-import/kiwi/message.js +145 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5635 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +94 -0
- package/dist/taco/model/component-evaluation.js +548 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +372 -0
- package/dist/taco/model/instance-resolver.js +1748 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +74 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2347 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +54 -4
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type DesignDocument, type DocumentId } from "../model/index.ts";
|
|
2
|
+
import type { FigmaImportResult } from "./import/types.ts";
|
|
3
|
+
export interface FigmaFileDocumentOptions {
|
|
4
|
+
readonly id: DocumentId;
|
|
5
|
+
readonly title: string;
|
|
6
|
+
/** Gives each page and its root frame an id; called once a page. */
|
|
7
|
+
readonly idSource: () => string;
|
|
8
|
+
/**
|
|
9
|
+
* The document the pages go into, such as a host's blank document: its
|
|
10
|
+
* theme, assets and other fields are kept, its pages and layers are
|
|
11
|
+
* replaced.
|
|
12
|
+
*/
|
|
13
|
+
readonly base: DesignDocument;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The document a whole .fig file makes: one page for each imported page
|
|
17
|
+
* (`importFigmaMessage` with `roots: "pages"`), in the file's order and with
|
|
18
|
+
* its name and chosen variable modes, each page's layers under a root frame
|
|
19
|
+
* of its own, every image the layers use as an image asset, every component
|
|
20
|
+
* made from a SYMBOL added to the base's components (their layers are among
|
|
21
|
+
* `nodes`, on no page), and the file's variable collections added to the base
|
|
22
|
+
* theme's. The result is not checked here;
|
|
23
|
+
* `deserializeDocument` (or a store made from it) checks it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function figmaFileDocument(result: Pick<FigmaImportResult, "pages" | "nodes" | "assets" | "components" | "variableCollections"> & Partial<Pick<FigmaImportResult, "textStyles">>, options: FigmaFileDocumentOptions): DesignDocument;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { entryOf, nodeId, pageId, splitCssFontFamilies, } from "../model/index.js";
|
|
2
|
+
/** The size of a page's root frame, as the editor gives a new page; the canvas neither draws nor clips it. */
|
|
3
|
+
const PAGE_ROOT_SIZE = { width: 1920, height: 1080 };
|
|
4
|
+
/**
|
|
5
|
+
* The document a whole .fig file makes: one page for each imported page
|
|
6
|
+
* (`importFigmaMessage` with `roots: "pages"`), in the file's order and with
|
|
7
|
+
* its name and chosen variable modes, each page's layers under a root frame
|
|
8
|
+
* of its own, every image the layers use as an image asset, every component
|
|
9
|
+
* made from a SYMBOL added to the base's components (their layers are among
|
|
10
|
+
* `nodes`, on no page), and the file's variable collections added to the base
|
|
11
|
+
* theme's. The result is not checked here;
|
|
12
|
+
* `deserializeDocument` (or a store made from it) checks it.
|
|
13
|
+
*/
|
|
14
|
+
export function figmaFileDocument(result, options) {
|
|
15
|
+
const { base } = options;
|
|
16
|
+
const nodes = {};
|
|
17
|
+
for (const node of result.nodes)
|
|
18
|
+
nodes[node.id] = node;
|
|
19
|
+
const pageOrder = [];
|
|
20
|
+
const pages = {};
|
|
21
|
+
result.pages.forEach((page, index) => {
|
|
22
|
+
const suffix = options.idSource();
|
|
23
|
+
const id = pageId(`page-${suffix}`);
|
|
24
|
+
const rootId = nodeId(`page-${suffix}-root`);
|
|
25
|
+
const root = {
|
|
26
|
+
id: rootId,
|
|
27
|
+
name: "Page",
|
|
28
|
+
visible: true,
|
|
29
|
+
locked: false,
|
|
30
|
+
opacity: 1,
|
|
31
|
+
type: "frame",
|
|
32
|
+
frame: { x: 0, y: 0, ...PAGE_ROOT_SIZE },
|
|
33
|
+
rotation: 0,
|
|
34
|
+
children: [...page.roots],
|
|
35
|
+
layout: { kind: "absolute" },
|
|
36
|
+
clipContent: false,
|
|
37
|
+
};
|
|
38
|
+
nodes[rootId] = root;
|
|
39
|
+
pages[id] = {
|
|
40
|
+
id,
|
|
41
|
+
name: page.name.trim() === "" ? `Page ${String(index + 1)}` : page.name,
|
|
42
|
+
rootId,
|
|
43
|
+
...(page.variableModes === undefined ? {} : { variableModes: page.variableModes }),
|
|
44
|
+
};
|
|
45
|
+
pageOrder.push(id);
|
|
46
|
+
});
|
|
47
|
+
const assets = { ...base.assets };
|
|
48
|
+
for (const image of Object.values(result.assets)) {
|
|
49
|
+
const asset = {
|
|
50
|
+
id: image.id,
|
|
51
|
+
kind: "image",
|
|
52
|
+
mimeType: image.mimeType,
|
|
53
|
+
source: { kind: "data", data: bytesAsBase64(image.bytes) },
|
|
54
|
+
...(image.width === undefined ? {} : { width: image.width }),
|
|
55
|
+
...(image.height === undefined ? {} : { height: image.height }),
|
|
56
|
+
};
|
|
57
|
+
assets[image.id] = asset;
|
|
58
|
+
}
|
|
59
|
+
const components = { ...base.components };
|
|
60
|
+
for (const component of result.components)
|
|
61
|
+
components[component.id] = component;
|
|
62
|
+
const variableTheme = Object.keys(result.variableCollections).length === 0
|
|
63
|
+
? base.theme
|
|
64
|
+
: { ...base.theme, variableCollections: { ...base.theme.variableCollections, ...result.variableCollections } };
|
|
65
|
+
// An imported text style names no font asset. It takes the document's asset of its own family, weight and slant when
|
|
66
|
+
// there is one; otherwise it has none, and the host binds its runs to a face when the document opens. The default
|
|
67
|
+
// style's fallbacks are kept, as runs without a style have them.
|
|
68
|
+
const fallback = entryOf(base.theme.textStyles, base.theme.defaultTextStyleId);
|
|
69
|
+
const fontAssets = Object.values(assets).filter((asset) => asset.kind === "font");
|
|
70
|
+
const withFonts = (style) => {
|
|
71
|
+
const asset = matchingFontAsset(fontAssets, style);
|
|
72
|
+
const fallbacks = fallback?.fontFallbackAssetIds?.filter((id) => id !== asset?.id);
|
|
73
|
+
return {
|
|
74
|
+
...(asset === undefined ? {} : { fontAssetId: asset.id }),
|
|
75
|
+
...(fallbacks === undefined || fallbacks.length === 0 ? {} : { fontFallbackAssetIds: fallbacks }),
|
|
76
|
+
...style,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const textStyles = Object.entries(result.textStyles ?? {});
|
|
80
|
+
const theme = textStyles.length === 0
|
|
81
|
+
? variableTheme
|
|
82
|
+
: { ...variableTheme, textStyles: { ...variableTheme.textStyles, ...Object.fromEntries(textStyles.map(([id, style]) => [id, withFonts(style)])) } };
|
|
83
|
+
return { ...base, id: options.id, title: options.title, pageOrder, pages, nodes, assets, components, theme };
|
|
84
|
+
}
|
|
85
|
+
/** The font asset of exactly `style`'s face: its first family (without case), its weight and its slant. */
|
|
86
|
+
function matchingFontAsset(assets, style) {
|
|
87
|
+
const family = (splitCssFontFamilies(style.fontFamily)[0] ?? style.fontFamily).replace(/^(["'])(.*)\1$/, "$2").toLowerCase();
|
|
88
|
+
const italic = style.italic === true;
|
|
89
|
+
return assets.find((asset) => asset.family.toLowerCase() === family && asset.weight === style.fontWeight
|
|
90
|
+
&& (asset.style !== "normal") === italic);
|
|
91
|
+
}
|
|
92
|
+
/** Bytes as base64, for an image asset's `data` source. */
|
|
93
|
+
function bytesAsBase64(bytes) {
|
|
94
|
+
let binary = "";
|
|
95
|
+
const chunk = 0x8000;
|
|
96
|
+
for (let start = 0; start < bytes.length; start += chunk) {
|
|
97
|
+
binary += String.fromCharCode(...bytes.subarray(start, start + chunk));
|
|
98
|
+
}
|
|
99
|
+
return btoa(binary);
|
|
100
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type FigmaObject } from "./fields.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a reader for the message's `blobs` list. A blob reference, such as
|
|
4
|
+
* a `Path.commandsBlob`, is the index into that list plus the message's
|
|
5
|
+
* `blobBaseIndex` (0 when absent, as on clipboard messages), as grida reads
|
|
6
|
+
* it (packages/grida-canvas-io-figma/fig-kiwi/index.ts, `getBlobBytes`). The
|
|
7
|
+
* reader gives `undefined` for a reference outside the list, and refuses the
|
|
8
|
+
* message when a blob in it has no `bytes`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function blobReader(message: FigmaObject): (reference: number) => Uint8Array | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { field, isObject } from "./fields.js";
|
|
2
|
+
import { FigmaImportError } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Returns a reader for the message's `blobs` list. A blob reference, such as
|
|
5
|
+
* a `Path.commandsBlob`, is the index into that list plus the message's
|
|
6
|
+
* `blobBaseIndex` (0 when absent, as on clipboard messages), as grida reads
|
|
7
|
+
* it (packages/grida-canvas-io-figma/fig-kiwi/index.ts, `getBlobBytes`). The
|
|
8
|
+
* reader gives `undefined` for a reference outside the list, and refuses the
|
|
9
|
+
* message when a blob in it has no `bytes`.
|
|
10
|
+
*/
|
|
11
|
+
export function blobReader(message) {
|
|
12
|
+
return (reference) => {
|
|
13
|
+
const blobs = field(message, "blobs", "list", "message") ?? [];
|
|
14
|
+
const index = reference - (field(message, "blobBaseIndex", "number", "message") ?? 0);
|
|
15
|
+
if (!Number.isInteger(index) || index < 0 || index >= blobs.length)
|
|
16
|
+
return undefined;
|
|
17
|
+
const blob = blobs[index];
|
|
18
|
+
const bytes = isObject(blob) ? blob["bytes"] : undefined;
|
|
19
|
+
if (!(bytes instanceof Uint8Array))
|
|
20
|
+
throw new FigmaImportError("not-node-changes", `message blobs[${String(index)}] has no bytes`);
|
|
21
|
+
return bytes;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SceneNode } from "../../model/index.ts";
|
|
2
|
+
/** Marks a Taco shadow as one Figma shows behind its node. */
|
|
3
|
+
export declare function markShadowBehind(shadow: object): void;
|
|
4
|
+
/** Marks a Taco image paint as one Figma tiles at `scale`. */
|
|
5
|
+
export declare function markTile(paint: object, scale: number): void;
|
|
6
|
+
/** Moves a mark to a copy of the marked object. */
|
|
7
|
+
export declare function moveMark(from: object, to: object): void;
|
|
8
|
+
/** Adds `data` to the layer's `metadata.figma`. */
|
|
9
|
+
export declare function carry(node: SceneNode, data: NonNullable<SceneNode["metadata"]>): void;
|
|
10
|
+
/** Carries the marks on a converted layer's shadows and fills into its metadata. */
|
|
11
|
+
export declare function carryMarks(node: SceneNode): void;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const marks = new WeakMap();
|
|
2
|
+
/** Marks a Taco shadow as one Figma shows behind its node. */
|
|
3
|
+
export function markShadowBehind(shadow) {
|
|
4
|
+
marks.set(shadow, { behind: true });
|
|
5
|
+
}
|
|
6
|
+
/** Marks a Taco image paint as one Figma tiles at `scale`. */
|
|
7
|
+
export function markTile(paint, scale) {
|
|
8
|
+
marks.set(paint, { tile: scale });
|
|
9
|
+
}
|
|
10
|
+
/** Moves a mark to a copy of the marked object. */
|
|
11
|
+
export function moveMark(from, to) {
|
|
12
|
+
const mark = marks.get(from);
|
|
13
|
+
if (mark !== undefined)
|
|
14
|
+
marks.set(to, mark);
|
|
15
|
+
}
|
|
16
|
+
/** Adds `data` to the layer's `metadata.figma`. */
|
|
17
|
+
export function carry(node, data) {
|
|
18
|
+
const metadata = node.metadata ?? {}, figma = metadata["figma"];
|
|
19
|
+
const had = typeof figma === "object" && figma !== null && !Array.isArray(figma) ? figma : {};
|
|
20
|
+
Object.assign(node, { metadata: { ...metadata, figma: { ...had, ...data } } });
|
|
21
|
+
}
|
|
22
|
+
/** Carries the marks on a converted layer's shadows and fills into its metadata. */
|
|
23
|
+
export function carryMarks(node) {
|
|
24
|
+
const style = node.style;
|
|
25
|
+
const behind = (style?.shadows ?? []).flatMap((shadow, index) => marks.get(shadow)?.behind === true ? [index] : []);
|
|
26
|
+
const tiles = (style?.fills ?? []).flatMap((paint, index) => {
|
|
27
|
+
const scale = marks.get(paint)?.tile;
|
|
28
|
+
return scale === undefined ? [] : [[index, scale]];
|
|
29
|
+
});
|
|
30
|
+
if (behind.length === 0 && tiles.length === 0)
|
|
31
|
+
return;
|
|
32
|
+
carry(node, { ...(behind.length > 0 ? { shadowBehind: behind } : {}), ...(tiles.length > 0 ? { tiles } : {}) });
|
|
33
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type ComponentId, type ComponentPropertyDefinition, type ComponentPropertyId, type ComponentPropertyValue, type NodeExpressions } from "../../model/index.ts";
|
|
2
|
+
import { type FigmaObject } from "./fields.ts";
|
|
3
|
+
import type { FigmaTreeNode } from "./tree.ts";
|
|
4
|
+
/** One Taco property made from a Figma `componentPropDefs` entry, with the Figma ids that name it. */
|
|
5
|
+
export interface ImportedProperty {
|
|
6
|
+
readonly definition: ComponentPropertyDefinition;
|
|
7
|
+
/** The def's own id and, for a variant, the id of the set's def it points at (`parentPropDefId`). */
|
|
8
|
+
readonly figmaIds: readonly string[];
|
|
9
|
+
}
|
|
10
|
+
/** Where component property import sends what it could not express; the walk counts each reason once per kind. */
|
|
11
|
+
export type PropertyReport = (kind: "property-left-out" | "approximated", reason: string) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Figma's name kept as written, in NFC (expressions write it in backticks
|
|
14
|
+
* where it is not an identifier), only made unique: a name `taken` already
|
|
15
|
+
* has gets `_2`, `_3`, ... after it. Characters no name can hold (control,
|
|
16
|
+
* line break and bidirectional formatting ones) are dropped, an empty name
|
|
17
|
+
* becomes `property` and a name is cut to 256 characters, so the result
|
|
18
|
+
* depends only on the name and the names before it.
|
|
19
|
+
*/
|
|
20
|
+
export declare function propertyNameOf(name: string, taken: ReadonlySet<string>): string;
|
|
21
|
+
/**
|
|
22
|
+
* The Taco properties of a SYMBOL, from its `componentPropDefs`. A variant
|
|
23
|
+
* (a SYMBOL inside a COMPONENT_SET) holds only `{id, parentPropDefId}`
|
|
24
|
+
* entries, so the name, type and default come from the set's def. BOOL
|
|
25
|
+
* becomes `boolean`, TEXT `text` and INSTANCE_SWAP `instance`, whose default
|
|
26
|
+
* component comes from `componentOf` (`undefined` when that component is
|
|
27
|
+
* not in the message, which leaves the property out), and whose
|
|
28
|
+
* `preferredValues` become `preferred` through `componentsByKey`; a key that
|
|
29
|
+
* names no imported component is dropped and reported. SLOT becomes `slot`,
|
|
30
|
+
* with its `preferredValues` mapped the same way and its `slotPropConfig`
|
|
31
|
+
* settings (`slotSettingsOf`); its default content is the
|
|
32
|
+
* frames bound to it. VARIANT defs are not properties here: each variant
|
|
33
|
+
* imports as its own component. Other types are reported, and so is a name
|
|
34
|
+
* changed to be unique or
|
|
35
|
+
* to drop characters no name can hold (`propertyNameOf`).
|
|
36
|
+
*/
|
|
37
|
+
export declare function propertiesOf(symbol: FigmaTreeNode, nodes: ReadonlyMap<string, FigmaTreeNode>, componentOf: (symbolId: string) => ComponentId | undefined, componentsByKey: ComponentsByKey, report: PropertyReport): ImportedProperty[];
|
|
38
|
+
/** The Taco components a Figma component key names: a COMPONENT key its SYMBOL, a STATE_GROUP key its set's variants. */
|
|
39
|
+
export type ComponentsByKey = (type: "COMPONENT" | "STATE_GROUP", key: string) => readonly ComponentId[];
|
|
40
|
+
/**
|
|
41
|
+
* Looks components up by the `componentKey` of a SYMBOL or a COMPONENT_SET
|
|
42
|
+
* (a FRAME with `isStateGroup`): a set gives its imported variants in set
|
|
43
|
+
* order. A key no node holds, or whose components were not imported, gives
|
|
44
|
+
* an empty list.
|
|
45
|
+
*/
|
|
46
|
+
export declare function componentsByKeyOf(nodes: ReadonlyMap<string, FigmaTreeNode>, componentOf: (symbolId: string) => ComponentId | undefined): ComponentsByKey;
|
|
47
|
+
/** The property a Figma def id names, by the def's own id or the set def it points at. */
|
|
48
|
+
export declare function propertyByFigmaId(properties: readonly ImportedProperty[], figmaId: string): ImportedProperty | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The expressions that bind a component layer's `componentPropRefs` to the
|
|
51
|
+
* component's properties: VISIBLE computes `visible`, TEXT_DATA on a text
|
|
52
|
+
* layer `characters` and OVERRIDDEN_SYMBOL_ID on a nested instance
|
|
53
|
+
* `componentId`, each reading the property by name (in backticks where
|
|
54
|
+
* the name is not an identifier, `` `Show icon` ``). SLOT_CONTENT_ID is
|
|
55
|
+
* left to `slotBindingOf`. A ref on the component
|
|
56
|
+
* root (which cannot compute fields), to a property that was not imported,
|
|
57
|
+
* or of another field is reported.
|
|
58
|
+
*/
|
|
59
|
+
export declare function propertyExpressions(layer: FigmaTreeNode, isRoot: boolean, properties: readonly ImportedProperty[], report: PropertyReport): NodeExpressions;
|
|
60
|
+
/** The ids of a symbol's VARIANT defs and of its set's, whose values need no Taco property. */
|
|
61
|
+
export declare function variantDefIds(symbol: FigmaTreeNode, nodes: ReadonlyMap<string, FigmaTreeNode>): Set<string>;
|
|
62
|
+
/** The value a `componentPropAssignments` entry gives, by kind; a field is `undefined` when the entry does not hold that kind. */
|
|
63
|
+
export interface AssignedValue {
|
|
64
|
+
readonly bool?: boolean;
|
|
65
|
+
readonly text?: string;
|
|
66
|
+
/** The SYMBOL an instance swap value names. */
|
|
67
|
+
readonly symbolId?: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The value a `componentPropAssignments` entry gives. Older files keep it in
|
|
71
|
+
* `value` (`boolValue`, `textValue.characters`, `guidValue`); newer files
|
|
72
|
+
* leave `value` empty and keep it in `varValue.value` (`boolValue`,
|
|
73
|
+
* `textDataValue.characters`, `symbolIdValue.guid`), as slot values do.
|
|
74
|
+
*/
|
|
75
|
+
export declare function assignedValueOf(assignment: FigmaObject, at: string): AssignedValue;
|
|
76
|
+
/**
|
|
77
|
+
* `componentPropAssignments` as Taco values of `properties`. A VARIANT value
|
|
78
|
+
* (`variantDefs`) and a slot value (which becomes a slot host) are skipped: the variant an instance shows is already its
|
|
79
|
+
* component. An instance swap value names a component from `componentOf`.
|
|
80
|
+
* A value naming no imported property, a swap to a component that is not
|
|
81
|
+
* in the message, or a swap `leftOutSwap` gives a reason to leave out (it
|
|
82
|
+
* would nest a component in itself), is reported and left out.
|
|
83
|
+
*/
|
|
84
|
+
export declare function assignedPropertyValues(list: readonly unknown[] | undefined, properties: readonly ImportedProperty[], variantDefs: ReadonlySet<string>, componentOf: (symbolId: string) => ComponentId | undefined, leftOutSwap: (symbolId: string, property: ComponentPropertyId) => string | undefined, where: string, report: PropertyReport): Record<string, ComponentPropertyValue>;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { EXPRESSION_NAME_MAX_LENGTH, expressionNameProblem, formatExpressionName, normalizeExpressionName, } from "../../model/index.js";
|
|
2
|
+
import { field, guidText, isObject } from "./fields.js";
|
|
3
|
+
/**
|
|
4
|
+
* Figma's name kept as written, in NFC (expressions write it in backticks
|
|
5
|
+
* where it is not an identifier), only made unique: a name `taken` already
|
|
6
|
+
* has gets `_2`, `_3`, ... after it. Characters no name can hold (control,
|
|
7
|
+
* line break and bidirectional formatting ones) are dropped, an empty name
|
|
8
|
+
* becomes `property` and a name is cut to 256 characters, so the result
|
|
9
|
+
* depends only on the name and the names before it.
|
|
10
|
+
*/
|
|
11
|
+
export function propertyNameOf(name, taken) {
|
|
12
|
+
let base = [...normalizeExpressionName(name)].filter((ch) => expressionNameProblem(ch) === undefined).join("");
|
|
13
|
+
if (base === "")
|
|
14
|
+
base = "property";
|
|
15
|
+
const cut = (text, room) => [...text].slice(0, room).join("");
|
|
16
|
+
base = cut(base, EXPRESSION_NAME_MAX_LENGTH);
|
|
17
|
+
let candidate = base;
|
|
18
|
+
for (let n = 2; taken.has(candidate); n++) {
|
|
19
|
+
const suffix = `_${String(n)}`;
|
|
20
|
+
candidate = cut(base, EXPRESSION_NAME_MAX_LENGTH - suffix.length) + suffix;
|
|
21
|
+
}
|
|
22
|
+
return candidate;
|
|
23
|
+
}
|
|
24
|
+
/** The defs of a node's `componentPropDefs`, keyed by id text. */
|
|
25
|
+
function defsById(node) {
|
|
26
|
+
const defs = new Map();
|
|
27
|
+
if (node === undefined)
|
|
28
|
+
return defs;
|
|
29
|
+
const where = `node ${node.id} componentPropDefs`;
|
|
30
|
+
for (const def of field(node.change, "componentPropDefs", "list", `node ${node.id}`) ?? []) {
|
|
31
|
+
if (isObject(def) && def["id"] !== undefined)
|
|
32
|
+
defs.set(guidText(def["id"], where), def);
|
|
33
|
+
}
|
|
34
|
+
return defs;
|
|
35
|
+
}
|
|
36
|
+
/** The defs of the COMPONENT_SET a variant sits in; empty for a SYMBOL that is not a variant. */
|
|
37
|
+
function setDefsOf(symbol, nodes) {
|
|
38
|
+
const owner = symbol.parentId === undefined ? undefined : nodes.get(symbol.parentId);
|
|
39
|
+
return owner?.change["isStateGroup"] === true ? defsById(owner) : new Map();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The Taco properties of a SYMBOL, from its `componentPropDefs`. A variant
|
|
43
|
+
* (a SYMBOL inside a COMPONENT_SET) holds only `{id, parentPropDefId}`
|
|
44
|
+
* entries, so the name, type and default come from the set's def. BOOL
|
|
45
|
+
* becomes `boolean`, TEXT `text` and INSTANCE_SWAP `instance`, whose default
|
|
46
|
+
* component comes from `componentOf` (`undefined` when that component is
|
|
47
|
+
* not in the message, which leaves the property out), and whose
|
|
48
|
+
* `preferredValues` become `preferred` through `componentsByKey`; a key that
|
|
49
|
+
* names no imported component is dropped and reported. SLOT becomes `slot`,
|
|
50
|
+
* with its `preferredValues` mapped the same way and its `slotPropConfig`
|
|
51
|
+
* settings (`slotSettingsOf`); its default content is the
|
|
52
|
+
* frames bound to it. VARIANT defs are not properties here: each variant
|
|
53
|
+
* imports as its own component. Other types are reported, and so is a name
|
|
54
|
+
* changed to be unique or
|
|
55
|
+
* to drop characters no name can hold (`propertyNameOf`).
|
|
56
|
+
*/
|
|
57
|
+
export function propertiesOf(symbol, nodes, componentOf, componentsByKey, report) {
|
|
58
|
+
const setDefs = setDefsOf(symbol, nodes);
|
|
59
|
+
const where = `node ${symbol.id} componentPropDefs`;
|
|
60
|
+
const names = new Set();
|
|
61
|
+
const ids = new Set();
|
|
62
|
+
const properties = [];
|
|
63
|
+
for (const [ownId, own] of defsById(symbol)) {
|
|
64
|
+
const parentId = own["parentPropDefId"] === undefined ? undefined : guidText(own["parentPropDefId"], where);
|
|
65
|
+
const def = { ...(parentId === undefined ? {} : setDefs.get(parentId)), ...own };
|
|
66
|
+
const type = field(def, "type", "string", where);
|
|
67
|
+
if (type === "VARIANT")
|
|
68
|
+
continue;
|
|
69
|
+
const figmaName = field(def, "name", "string", where) ?? "";
|
|
70
|
+
const initial = field(def, "initialValue", "object", where) ?? {};
|
|
71
|
+
// A set's def id is shared by its variants, so a value keeps its property across variants; a second def pointing at it keeps its own id.
|
|
72
|
+
const shared = `figma-${parentId ?? ownId}`;
|
|
73
|
+
const base = { id: (ids.has(shared) ? `figma-${ownId}` : shared), name: propertyNameOf(figmaName, names) };
|
|
74
|
+
let definition;
|
|
75
|
+
if (type === "BOOL" || type === "BOOLEAN") {
|
|
76
|
+
definition = { ...base, kind: "boolean", defaultValue: field(initial, "boolValue", "boolean", where) ?? false };
|
|
77
|
+
}
|
|
78
|
+
else if (type === "TEXT") {
|
|
79
|
+
const text = field(initial, "textValue", "object", where);
|
|
80
|
+
definition = { ...base, kind: "text", defaultValue: (text === undefined ? undefined : field(text, "characters", "string", where)) ?? "" };
|
|
81
|
+
}
|
|
82
|
+
else if (type === "INSTANCE_SWAP") {
|
|
83
|
+
const target = initial["guidValue"] === undefined ? undefined : guidText(initial["guidValue"], where);
|
|
84
|
+
const component = target === undefined ? undefined : componentOf(target);
|
|
85
|
+
if (component === undefined) {
|
|
86
|
+
report("property-left-out", "an instance swap property is left out: its default component is not in the message");
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const preferred = preferredOf(def, componentsByKey, where, report);
|
|
90
|
+
definition = { ...base, kind: "instance", defaultValue: component, ...(preferred.length === 0 ? {} : { preferred }) };
|
|
91
|
+
}
|
|
92
|
+
else if (type === "SLOT") {
|
|
93
|
+
// The default content is the bound frames' own children, so a slot has no default value.
|
|
94
|
+
const preferred = preferredOf(def, componentsByKey, where, report, "a slot");
|
|
95
|
+
definition = { ...base, kind: "slot", ...(preferred.length === 0 ? {} : { preferred }), ...slotSettingsOf(def, where, report) };
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
report("property-left-out", `component property type ${String(type)} is not imported yet`);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (base.name !== normalizeExpressionName(figmaName)) {
|
|
102
|
+
report("approximated", "a component property name is changed: a name used twice gets a number, and control, line break and bidirectional formatting characters are dropped");
|
|
103
|
+
}
|
|
104
|
+
names.add(base.name);
|
|
105
|
+
ids.add(base.id);
|
|
106
|
+
properties.push({ definition, figmaIds: parentId === undefined ? [ownId] : [ownId, parentId] });
|
|
107
|
+
}
|
|
108
|
+
return properties;
|
|
109
|
+
}
|
|
110
|
+
/** Taco's slot settings by the `slotPropConfig` field of a SLOT def that sets each. */
|
|
111
|
+
const SLOT_SETTINGS = { allowPreferredValuesOnly: "preferredOnly", minChildren: "minChildren", maxChildren: "maxChildren", stretchChildOnInsert: "fillCrossAxisOnInsert" };
|
|
112
|
+
/**
|
|
113
|
+
* The settings of a SLOT def's `slotPropConfig`: `allowPreferredValuesOnly`,
|
|
114
|
+
* `minChildren`, `maxChildren` and `stretchChildOnInsert` map to the slot
|
|
115
|
+
* fields of the same meaning, and `displayByDefault: false` to
|
|
116
|
+
* `displayEmptyByDefault`. A zero `maxChildren` sets no limit. Limits that
|
|
117
|
+
* cross (a minimum above the maximum) are dropped, and they and any field
|
|
118
|
+
* Taco has no setting for are reported.
|
|
119
|
+
*/
|
|
120
|
+
function slotSettingsOf(def, where, report) {
|
|
121
|
+
const config = field(def, "slotPropConfig", "object", where);
|
|
122
|
+
const settings = {};
|
|
123
|
+
for (const [name, value] of Object.entries(config ?? {})) {
|
|
124
|
+
const taco = SLOT_SETTINGS[name];
|
|
125
|
+
if (name === "displayByDefault" && typeof value === "boolean") {
|
|
126
|
+
if (!value)
|
|
127
|
+
settings["displayEmptyByDefault"] = true;
|
|
128
|
+
}
|
|
129
|
+
else if (taco !== undefined && (name === "minChildren" || name === "maxChildren") && Number.isInteger(value) && value >= 0) {
|
|
130
|
+
if (value !== 0)
|
|
131
|
+
settings[taco] = value;
|
|
132
|
+
}
|
|
133
|
+
else if (taco !== undefined && typeof value === "boolean") {
|
|
134
|
+
if (value)
|
|
135
|
+
settings[taco] = true;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
report("property-left-out", `a slot setting ${name} is not imported: Taco slots have no such setting`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (typeof settings["minChildren"] === "number" && typeof settings["maxChildren"] === "number" && settings["minChildren"] > settings["maxChildren"]) {
|
|
142
|
+
report("property-left-out", "a slot's child limits are left out: its minimum is above its maximum");
|
|
143
|
+
delete settings["minChildren"];
|
|
144
|
+
delete settings["maxChildren"];
|
|
145
|
+
}
|
|
146
|
+
return settings;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Looks components up by the `componentKey` of a SYMBOL or a COMPONENT_SET
|
|
150
|
+
* (a FRAME with `isStateGroup`): a set gives its imported variants in set
|
|
151
|
+
* order. A key no node holds, or whose components were not imported, gives
|
|
152
|
+
* an empty list.
|
|
153
|
+
*/
|
|
154
|
+
export function componentsByKeyOf(nodes, componentOf) {
|
|
155
|
+
const symbols = new Map();
|
|
156
|
+
const sets = new Map();
|
|
157
|
+
for (const node of nodes.values()) {
|
|
158
|
+
const key = field(node.change, "componentKey", "string", `node ${node.id}`);
|
|
159
|
+
if (key === undefined || key === "")
|
|
160
|
+
continue;
|
|
161
|
+
if (node.type === "SYMBOL" && !symbols.has(key))
|
|
162
|
+
symbols.set(key, node);
|
|
163
|
+
else if (node.change["isStateGroup"] === true && !sets.has(key))
|
|
164
|
+
sets.set(key, node);
|
|
165
|
+
}
|
|
166
|
+
return (type, key) => {
|
|
167
|
+
const found = (type === "COMPONENT" ? symbols : sets).get(key);
|
|
168
|
+
const members = found === undefined ? [] : type === "COMPONENT" ? [found] : found.children.filter((child) => child.type === "SYMBOL");
|
|
169
|
+
return members.flatMap((member) => componentOf(member.id) ?? []);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The components an INSTANCE_SWAP or SLOT def's `preferredValues.instanceSwapValues`
|
|
174
|
+
* name, in order and each once (`what` names the property in the report). A key of a component the import does not
|
|
175
|
+
* hold, or of another type, is dropped and reported each time a symbol
|
|
176
|
+
* reads the def, so a set's def counts once per variant.
|
|
177
|
+
*/
|
|
178
|
+
function preferredOf(def, componentsByKey, where, report, what = "an instance swap") {
|
|
179
|
+
const values = field(def, "preferredValues", "object", where);
|
|
180
|
+
const preferred = new Set();
|
|
181
|
+
for (const value of (values === undefined ? undefined : field(values, "instanceSwapValues", "list", where)) ?? []) {
|
|
182
|
+
const type = isObject(value) ? value["type"] : undefined;
|
|
183
|
+
const key = isObject(value) ? field(value, "key", "string", where) : undefined;
|
|
184
|
+
if (type !== "COMPONENT" && type !== "STATE_GROUP") {
|
|
185
|
+
report("property-left-out", `a preferred component of type ${String(type)} of ${what} property is not imported yet`);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const components = key === undefined ? [] : componentsByKey(type, key);
|
|
189
|
+
if (components.length === 0)
|
|
190
|
+
report("property-left-out", `a preferred component of ${what} property is dropped: its component key is not in the import`);
|
|
191
|
+
for (const component of components)
|
|
192
|
+
preferred.add(component);
|
|
193
|
+
}
|
|
194
|
+
return [...preferred];
|
|
195
|
+
}
|
|
196
|
+
/** The property a Figma def id names, by the def's own id or the set def it points at. */
|
|
197
|
+
export function propertyByFigmaId(properties, figmaId) {
|
|
198
|
+
return properties.find((property) => property.figmaIds.includes(figmaId));
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* The expressions that bind a component layer's `componentPropRefs` to the
|
|
202
|
+
* component's properties: VISIBLE computes `visible`, TEXT_DATA on a text
|
|
203
|
+
* layer `characters` and OVERRIDDEN_SYMBOL_ID on a nested instance
|
|
204
|
+
* `componentId`, each reading the property by name (in backticks where
|
|
205
|
+
* the name is not an identifier, `` `Show icon` ``). SLOT_CONTENT_ID is
|
|
206
|
+
* left to `slotBindingOf`. A ref on the component
|
|
207
|
+
* root (which cannot compute fields), to a property that was not imported,
|
|
208
|
+
* or of another field is reported.
|
|
209
|
+
*/
|
|
210
|
+
export function propertyExpressions(layer, isRoot, properties, report) {
|
|
211
|
+
const where = `node ${layer.id} componentPropRefs`;
|
|
212
|
+
const expressions = {};
|
|
213
|
+
for (const ref of field(layer.change, "componentPropRefs", "list", `node ${layer.id}`) ?? []) {
|
|
214
|
+
if (!isObject(ref) || ref["defID"] === undefined || ref["isDeleted"] === true)
|
|
215
|
+
continue;
|
|
216
|
+
const kind = ref["componentPropNodeField"];
|
|
217
|
+
// A slot binding is no expression: it becomes `FrameNode.slot` (`slotBindingOf`).
|
|
218
|
+
if (kind === "SLOT_CONTENT_ID")
|
|
219
|
+
continue;
|
|
220
|
+
const property = propertyByFigmaId(properties, guidText(ref["defID"], where));
|
|
221
|
+
const target = kind === "VISIBLE" ? "visible" : kind === "TEXT_DATA" && layer.type === "TEXT" ? "characters"
|
|
222
|
+
: kind === "OVERRIDDEN_SYMBOL_ID" && layer.type === "INSTANCE" ? "componentId" : undefined;
|
|
223
|
+
if (target === undefined) {
|
|
224
|
+
report("property-left-out", `a component property bound to ${String(kind)} of a ${layer.type} is not imported yet`);
|
|
225
|
+
}
|
|
226
|
+
else if (isRoot) {
|
|
227
|
+
report("property-left-out", "a component property bound to the component's own frame is left out: a component root cannot compute fields");
|
|
228
|
+
}
|
|
229
|
+
else if (property === undefined) {
|
|
230
|
+
report("property-left-out", "a layer bound to a component property that was not imported keeps its own value");
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
expressions[target] = formatExpressionName(property.definition.name);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return expressions;
|
|
237
|
+
}
|
|
238
|
+
/** The ids of a symbol's VARIANT defs and of its set's, whose values need no Taco property. */
|
|
239
|
+
export function variantDefIds(symbol, nodes) {
|
|
240
|
+
const ids = new Set();
|
|
241
|
+
for (const [id, def] of setDefsOf(symbol, nodes))
|
|
242
|
+
if (def["type"] === "VARIANT")
|
|
243
|
+
ids.add(id);
|
|
244
|
+
for (const [id, def] of defsById(symbol)) {
|
|
245
|
+
const parent = def["parentPropDefId"] === undefined ? undefined : guidText(def["parentPropDefId"], `node ${symbol.id}`);
|
|
246
|
+
if (def["type"] === "VARIANT" || (parent !== undefined && ids.has(parent)))
|
|
247
|
+
ids.add(id);
|
|
248
|
+
}
|
|
249
|
+
return ids;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The value a `componentPropAssignments` entry gives. Older files keep it in
|
|
253
|
+
* `value` (`boolValue`, `textValue.characters`, `guidValue`); newer files
|
|
254
|
+
* leave `value` empty and keep it in `varValue.value` (`boolValue`,
|
|
255
|
+
* `textDataValue.characters`, `symbolIdValue.guid`), as slot values do.
|
|
256
|
+
*/
|
|
257
|
+
export function assignedValueOf(assignment, at) {
|
|
258
|
+
const value = field(assignment, "value", "object", at) ?? {};
|
|
259
|
+
const varValue = field(assignment, "varValue", "object", at);
|
|
260
|
+
const varAt = `${at}.varValue.value`;
|
|
261
|
+
const held = (varValue === undefined ? undefined : field(varValue, "value", "object", `${at}.varValue`)) ?? {};
|
|
262
|
+
const textOf = (object, where) => (object === undefined ? undefined : field(object, "characters", "string", where));
|
|
263
|
+
const bool = field(value, "boolValue", "boolean", at) ?? field(held, "boolValue", "boolean", varAt);
|
|
264
|
+
const text = textOf(field(value, "textValue", "object", at), at) ?? textOf(field(held, "textDataValue", "object", varAt), varAt);
|
|
265
|
+
const swap = field(held, "symbolIdValue", "object", varAt);
|
|
266
|
+
const symbolId = value["guidValue"] !== undefined ? guidText(value["guidValue"], at)
|
|
267
|
+
: swap?.["guid"] !== undefined ? guidText(swap["guid"], `${varAt}.symbolIdValue`) : undefined;
|
|
268
|
+
return { ...(bool === undefined ? {} : { bool }), ...(text === undefined ? {} : { text }), ...(symbolId === undefined ? {} : { symbolId }) };
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* `componentPropAssignments` as Taco values of `properties`. A VARIANT value
|
|
272
|
+
* (`variantDefs`) and a slot value (which becomes a slot host) are skipped: the variant an instance shows is already its
|
|
273
|
+
* component. An instance swap value names a component from `componentOf`.
|
|
274
|
+
* A value naming no imported property, a swap to a component that is not
|
|
275
|
+
* in the message, or a swap `leftOutSwap` gives a reason to leave out (it
|
|
276
|
+
* would nest a component in itself), is reported and left out.
|
|
277
|
+
*/
|
|
278
|
+
export function assignedPropertyValues(list, properties, variantDefs, componentOf, leftOutSwap, where, report) {
|
|
279
|
+
const values = {};
|
|
280
|
+
(list ?? []).forEach((assignment, index) => {
|
|
281
|
+
const at = `${where} componentPropAssignments[${String(index)}]`;
|
|
282
|
+
if (!isObject(assignment) || assignment["defID"] === undefined)
|
|
283
|
+
return;
|
|
284
|
+
const defId = guidText(assignment["defID"], at);
|
|
285
|
+
const property = propertyByFigmaId(properties, defId);
|
|
286
|
+
if (property === undefined) {
|
|
287
|
+
if (!variantDefs.has(defId))
|
|
288
|
+
report("property-left-out", "a component property value names a property that was not imported");
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const { definition } = property;
|
|
292
|
+
if (definition.kind !== "boolean" && definition.kind !== "text" && definition.kind !== "instance")
|
|
293
|
+
return;
|
|
294
|
+
const value = assignedValueOf(assignment, at);
|
|
295
|
+
if (definition.kind === "boolean" && value.bool !== undefined)
|
|
296
|
+
values[definition.id] = value.bool;
|
|
297
|
+
else if (definition.kind === "text" && value.text !== undefined)
|
|
298
|
+
values[definition.id] = value.text;
|
|
299
|
+
else if (definition.kind === "instance" && value.symbolId !== undefined) {
|
|
300
|
+
const component = componentOf(value.symbolId);
|
|
301
|
+
const leftOut = component === undefined ? undefined : leftOutSwap(value.symbolId, definition.id);
|
|
302
|
+
if (component === undefined)
|
|
303
|
+
report("property-left-out", "an instance swap value is left out: its component is not in the message");
|
|
304
|
+
else if (leftOut !== undefined)
|
|
305
|
+
report("property-left-out", `an instance swap value ${leftOut}`);
|
|
306
|
+
else
|
|
307
|
+
values[definition.id] = component;
|
|
308
|
+
}
|
|
309
|
+
else
|
|
310
|
+
report("property-left-out", `a ${definition.kind === "instance" ? "instance swap" : definition.kind} property value is left out: it could not be read`);
|
|
311
|
+
});
|
|
312
|
+
return values;
|
|
313
|
+
}
|