@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,562 @@
|
|
|
1
|
+
import { STYLES, VARIABLES } from '@moondraft/editor/schema';
|
|
2
|
+
import { guidText } from '../taco/figma-import/import/fields.js';
|
|
3
|
+
import { isDetachedStyle, STYLE_REFERENCE_FIELDS } from '../taco/figma-import/import/styles.js';
|
|
4
|
+
import { layerPathKey, parseLayerPath } from '../taco/model/index.js';
|
|
5
|
+
import { rgba, runStyle } from './convert.js';
|
|
6
|
+
function fileVariables(result, shared, identity) {
|
|
7
|
+
const variables = result.sources.variables;
|
|
8
|
+
for (const [id, c] of Object.entries(result.variableCollections)) {
|
|
9
|
+
shared.push({ ...identity(variables.sources.get(id)), id, parent: VARIABLES, kind: 'collection', name: c.name,
|
|
10
|
+
modes: c.modeOrder.map(id => ({ id, name: c.modes[id].name })), defaultMode: c.defaultModeId });
|
|
11
|
+
for (const [varId, v] of Object.entries(c.variables)) {
|
|
12
|
+
const node = variables.sources.get(varId);
|
|
13
|
+
const values = Object.fromEntries(Object.entries(v.values).map(([mode, value]) => [mode, v.type === 'color' ? rgba(value) : value]));
|
|
14
|
+
for (const [mode, alias] of variables.aliases.get(varId) ?? [])
|
|
15
|
+
values[mode] = { alias };
|
|
16
|
+
shared.push({ ...identity(node), id: varId, parent: id, name: v.name, kind: 'variable', type: v.type, values });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const corners = ['rectangleTopLeftCornerRadius', 'rectangleTopRightCornerRadius', 'rectangleBottomRightCornerRadius', 'rectangleBottomLeftCornerRadius'];
|
|
21
|
+
const fields = {
|
|
22
|
+
WIDTH: 'w', HEIGHT: 'h', STROKE_WEIGHT: 'strokeWidth', OPACITY: 'opacity', VISIBLE: 'visible', TEXT_DATA: 'content',
|
|
23
|
+
STACK_SPACING: 'layoutData.layout.gap', STACK_COUNTER_SPACING: 'layoutData.layout.wrapGap',
|
|
24
|
+
CORNER_RADIUS: 'radius', RECTANGLE_TOP_LEFT_CORNER_RADIUS: 'radius.0', RECTANGLE_TOP_RIGHT_CORNER_RADIUS: 'radius.1',
|
|
25
|
+
RECTANGLE_BOTTOM_RIGHT_CORNER_RADIUS: 'radius.2', RECTANGLE_BOTTOM_LEFT_CORNER_RADIUS: 'radius.3',
|
|
26
|
+
...Object.fromEntries(['left', 'top', 'right', 'bottom'].map(side => ['STACK_PADDING_' + side.toUpperCase(), 'layoutData.layout.padding.' + side])),
|
|
27
|
+
};
|
|
28
|
+
function paintsAndEffects(change, m, bind, indices) {
|
|
29
|
+
for (const [key, prefix] of [['fillPaints', 'look.fills'], ['strokePaints', 'look.stroke.paint']]) {
|
|
30
|
+
const paints = change[key] ?? [];
|
|
31
|
+
if (key === 'fillPaints' && m.fill && paints.some(p => p.colorVar)) {
|
|
32
|
+
(m.look ??= {}).fills = [{ k: 'solid', c: m.fill, o: 1 }];
|
|
33
|
+
delete m.fill;
|
|
34
|
+
}
|
|
35
|
+
if (key === 'strokePaints' && m.stroke && m.look?.stroke && paints.some(p => p.colorVar))
|
|
36
|
+
m.look.stroke.paint ??= { k: 'solid', c: m.stroke, o: 1 };
|
|
37
|
+
const order = indices.paintIndices.get(paints) ?? paints.map((_, i) => i);
|
|
38
|
+
const byIndex = new Map(order.map((index, i) => [index, i]));
|
|
39
|
+
const firstStroke = order.find(i => paints[i]?.visible !== false);
|
|
40
|
+
paints.forEach((paint, i) => {
|
|
41
|
+
const fill = key === 'fillPaints';
|
|
42
|
+
const index = byIndex.get(i);
|
|
43
|
+
const output = fill ? index !== undefined && m.look?.fills?.[index] : i === firstStroke && m.look?.stroke?.paint;
|
|
44
|
+
const path = fill ? prefix + '.' + index : prefix;
|
|
45
|
+
if (paint.colorVar)
|
|
46
|
+
bind(output?.k === 'solid' ? path + '.c' : undefined, paint.colorVar, 'color');
|
|
47
|
+
if (paint.opacityVar)
|
|
48
|
+
bind(undefined, paint.opacityVar, 'number');
|
|
49
|
+
if (!paint.stopsVar)
|
|
50
|
+
return;
|
|
51
|
+
// The reader sorts the stops and samples a new endpoint for stops beyond the paint's range.
|
|
52
|
+
const sorted = (paint.stops ?? []).map((stop, source) => ({ at: stop.position, source }))
|
|
53
|
+
.sort((a, b) => a.at - b.at);
|
|
54
|
+
const inside = sorted.filter((stop) => stop.at >= 0 && stop.at <= 1);
|
|
55
|
+
const first = Number(sorted[0]?.at < 0);
|
|
56
|
+
const locations = new Map(inside.map((stop, i) => [stop.source, first + i]));
|
|
57
|
+
for (const [j, stop] of paint.stopsVar.entries())
|
|
58
|
+
if (stop.colorVar) {
|
|
59
|
+
const k = locations.get(j);
|
|
60
|
+
if (k === undefined || !output?.stops?.[k])
|
|
61
|
+
bind(undefined, stop.colorVar, 'color');
|
|
62
|
+
else {
|
|
63
|
+
bind(path + '.stops.' + k + '.1', stop.colorVar, 'color');
|
|
64
|
+
if (sorted.length === 1 && output.stops.length === 2)
|
|
65
|
+
bind(path + '.stops.1.1', stop.colorVar, 'color');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
for (const effect of change.effects ?? []) {
|
|
71
|
+
const blur = ['FOREGROUND_BLUR', 'BACKGROUND_BLUR'].includes(effect.type);
|
|
72
|
+
const prefix = m.kind === 'text' ? undefined : indices.effectFields.get(effect);
|
|
73
|
+
if (prefix && blur)
|
|
74
|
+
(m.look ??= {})[prefix.slice(5)] ??= { radius: 0 };
|
|
75
|
+
for (const [field, suffix] of Object.entries({ colorVar: 'c', radiusVar: blur ? 'radius' : 'blur', spreadVar: 'spread', offsetXVar: 'x', offsetYVar: 'y' })) {
|
|
76
|
+
if (effect[field])
|
|
77
|
+
bind(prefix && (!blur || suffix === 'radius') ? prefix + '.' + suffix : undefined, effect[field], field === 'colorVar' ? 'color' : 'number');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function radiusBindings(change) {
|
|
82
|
+
const bindings = new Map();
|
|
83
|
+
for (const e of change.variableConsumptionMap?.entries ?? []) {
|
|
84
|
+
const path = fields[e.variableField];
|
|
85
|
+
if (path?.startsWith('radius'))
|
|
86
|
+
bindings.set(path, e.variableData);
|
|
87
|
+
}
|
|
88
|
+
return bindings;
|
|
89
|
+
}
|
|
90
|
+
// Apply one Figma layer's authored radii to the four corners it inherits.
|
|
91
|
+
function radiiOf(change, inherited) {
|
|
92
|
+
const bindings = radiusBindings(change);
|
|
93
|
+
return corners.map((field, i) => {
|
|
94
|
+
const base = inherited?.[i];
|
|
95
|
+
const own = change.rectangleCornerRadiiIndependent !== false ? change[field] : undefined;
|
|
96
|
+
const value = Math.max(0, own ?? change.cornerRadius ?? base?.value ?? 0);
|
|
97
|
+
const corner = bindings.get('radius.' + i);
|
|
98
|
+
if (corner)
|
|
99
|
+
return { value, binding: { data: corner, specific: true } };
|
|
100
|
+
const overall = bindings.get('radius');
|
|
101
|
+
if (overall && !base?.binding?.specific)
|
|
102
|
+
return { value, binding: { data: overall, specific: false } };
|
|
103
|
+
if (!overall && (own !== undefined || change.cornerRadius !== undefined))
|
|
104
|
+
return { value };
|
|
105
|
+
return base?.binding ? { value, binding: base.binding } : { value };
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/** Finds instance roots that need their own radius bindings. Native instance roots take bindings from their main.
|
|
109
|
+
* @param result - File definitions and original instance fields.
|
|
110
|
+
* @returns The effective corners for each root that must remain an expanded frame.
|
|
111
|
+
* @internal
|
|
112
|
+
*/
|
|
113
|
+
export function expandedRadiusRoots(result) {
|
|
114
|
+
const roots = new Map(Object.entries(result.componentIds).map(([guid, component]) => [component, result.sources.tree.nodes.get(guid)]));
|
|
115
|
+
const nested = new Map();
|
|
116
|
+
const hasRadius = (c) => c.cornerRadius !== undefined || corners.some(field => field in c) || radiusBindings(c).size > 0;
|
|
117
|
+
for (const overrides of result.sources.occurrenceFields.values())
|
|
118
|
+
for (const [path, change] of overrides) {
|
|
119
|
+
if (!hasRadius(change))
|
|
120
|
+
continue;
|
|
121
|
+
const target = parseLayerPath(path).at(-1);
|
|
122
|
+
const changes = nested.get(target);
|
|
123
|
+
if (changes)
|
|
124
|
+
changes.push(change);
|
|
125
|
+
else
|
|
126
|
+
nested.set(target, [change]);
|
|
127
|
+
}
|
|
128
|
+
const expanded = new Map();
|
|
129
|
+
const needsFrame = (main, own) => own.some(c => c.binding) && own.some((c, i) => JSON.stringify(c.binding?.data) !== JSON.stringify(main[i].binding?.data) || !c.binding && c.value !== main[i].value);
|
|
130
|
+
for (const { node, figma } of result.sources.imported) {
|
|
131
|
+
if (node.type !== 'instance' || figma.type !== 'INSTANCE')
|
|
132
|
+
continue;
|
|
133
|
+
const overrides = nested.get(node.id);
|
|
134
|
+
if (!overrides && !hasRadius(figma.change))
|
|
135
|
+
continue;
|
|
136
|
+
const main = radiiOf(roots.get(node.componentId).change), own = radiiOf(figma.change, main);
|
|
137
|
+
if (needsFrame(main, own) || overrides?.some(change => needsFrame(own, radiiOf(change, own))))
|
|
138
|
+
expanded.set(node.id, own);
|
|
139
|
+
}
|
|
140
|
+
return expanded;
|
|
141
|
+
}
|
|
142
|
+
function layerFields(change, m, bind) {
|
|
143
|
+
const entries = change.variableConsumptionMap?.entries ?? [];
|
|
144
|
+
const sides = entries.filter((e) => /^BORDER_(LEFT|RIGHT|TOP|BOTTOM)_WEIGHT$/.test(e.variableField));
|
|
145
|
+
const uniformStroke = sides.length === 4 && sides.every((e) => JSON.stringify(e.variableData) === JSON.stringify(sides[0].variableData));
|
|
146
|
+
if (uniformStroke)
|
|
147
|
+
bind(m.stroke && (!m.component || m.component === m.id) ? 'strokeWidth' : undefined, sides[0].variableData, 'number');
|
|
148
|
+
for (const { variableField, variableData } of entries) {
|
|
149
|
+
if (uniformStroke && sides.some((e) => e.variableField === variableField))
|
|
150
|
+
continue;
|
|
151
|
+
if (variableField === 'FONT_SIZE' && m.styles)
|
|
152
|
+
continue; // Installed per range below.
|
|
153
|
+
let path = Object.hasOwn(fields, variableField) ? fields[variableField] : undefined;
|
|
154
|
+
if (path?.startsWith('radius') && ['frame', 'rect'].includes(m.kind))
|
|
155
|
+
continue; // Installed per effective corner.
|
|
156
|
+
if (path?.startsWith('radius') || (path === 'w' || path === 'h') && (m[path] === undefined || m.autoResize === 2 || path === 'h' && m.autoResize === 1)
|
|
157
|
+
|| path?.startsWith('layoutData.layout.') && (!m.layoutData?.layout || m.layoutData.layout.kind === 'absolute' || m.component && m.component !== m.id)
|
|
158
|
+
|| path === 'content' && m.kind !== 'text'
|
|
159
|
+
|| path === 'strokeWidth' && (!m.stroke || m.component && m.component !== m.id)
|
|
160
|
+
|| path && m.bind?.[path])
|
|
161
|
+
path = undefined;
|
|
162
|
+
bind(path, variableData, path === 'visible' ? 'boolean' : path === 'content' ? 'string' : 'number');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Linking compares painted instances with their mains. A mode difference in that comparison is derived:
|
|
166
|
+
// leaving it as an authored paint override would mask the binding when the mode changes again.
|
|
167
|
+
function inheritedLiterals(entry, template, change, expanded, rootBindings) {
|
|
168
|
+
const sources = Object.keys(template.vars ?? {});
|
|
169
|
+
const inherited = (key) => sources.some(path => path === key || path.startsWith(key + '.'));
|
|
170
|
+
const formatting = [
|
|
171
|
+
['look.fills', 'fillStyle', 'fillPaints', 'styleIdForFill'],
|
|
172
|
+
['look.stroke.paint', 'strokeStyle', 'strokePaints', 'styleIdForStrokeFill'],
|
|
173
|
+
['look.shadows', 'effectStyle', 'effects', 'styleIdForEffect'],
|
|
174
|
+
['look.layerBlur', 'effectStyle', 'effects', 'styleIdForEffect'],
|
|
175
|
+
['look.backgroundBlur', 'effectStyle', 'effects', 'styleIdForEffect'],
|
|
176
|
+
];
|
|
177
|
+
for (const [key, style, list, ref] of formatting) {
|
|
178
|
+
if (!template[style] && !inherited(key))
|
|
179
|
+
continue;
|
|
180
|
+
if (!(list in change || ref in change))
|
|
181
|
+
delete entry[key];
|
|
182
|
+
else if (!(key in entry) && list in change && !(ref in change)) {
|
|
183
|
+
const bound = sources.filter(path => path.startsWith(key + '.'));
|
|
184
|
+
if (bound.length && rootBindings && bound.every(path => rootBindings.has(path)))
|
|
185
|
+
continue;
|
|
186
|
+
// The link pass omits equal literals. An authored value still masks the main's source.
|
|
187
|
+
if (key === 'look.fills')
|
|
188
|
+
entry[key] = expanded.look?.fills ?? (expanded.fill ? [{ k: 'solid', c: expanded.fill }] : []);
|
|
189
|
+
else if (key === 'look.stroke.paint' && expanded.stroke)
|
|
190
|
+
entry[key] = expanded.look?.stroke?.paint ?? { k: 'solid', c: expanded.stroke };
|
|
191
|
+
else if (key !== 'look.stroke.paint')
|
|
192
|
+
entry[key] = expanded.look?.[key.slice(5)] ?? (key === 'look.shadows' ? [] : { radius: 0 });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const [key, field] of [['visible', 'visible'], ['opacity', 'opacity'], ['content', 'textData']]) {
|
|
196
|
+
if (rootBindings || !inherited(key))
|
|
197
|
+
continue;
|
|
198
|
+
if (!(field in change))
|
|
199
|
+
delete entry[key];
|
|
200
|
+
else if (!(key in entry))
|
|
201
|
+
entry[key] = key === 'content' ? expanded.content : change[field];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// A style alias supplies its colour in the user's mode. All other paint fields, including
|
|
205
|
+
// the unit-box gradient geometry, must match the paint converted on that user's box.
|
|
206
|
+
function sameStyleValue(style, literal) {
|
|
207
|
+
if (style === literal)
|
|
208
|
+
return true;
|
|
209
|
+
if (typeof style === 'number' && typeof literal === 'number')
|
|
210
|
+
return Math.fround(style) === Math.fround(literal);
|
|
211
|
+
if (style?.alias)
|
|
212
|
+
return literal !== undefined;
|
|
213
|
+
if (!style || !literal || typeof style !== 'object' || typeof literal !== 'object')
|
|
214
|
+
return false;
|
|
215
|
+
if (Array.isArray(style) && style.length !== literal.length)
|
|
216
|
+
return false;
|
|
217
|
+
return Object.keys({ ...style, ...literal }).every(k => k === 'o' ? (style.o ?? 1) === (literal.o ?? 1) : sameStyleValue(style[k], literal[k]));
|
|
218
|
+
}
|
|
219
|
+
/** Writes the shared resources, layer references and mode choices of a saved file.
|
|
220
|
+
* @param result - Reader output retaining the source relationships.
|
|
221
|
+
* @param doc - Converted layers to associate with their definitions.
|
|
222
|
+
* @param occurrences - Original override fields at their native instance paths.
|
|
223
|
+
* @param expanded - Converted instance literals before the linker removes equal fields.
|
|
224
|
+
* @param expandedRadii - Effective corners of instance roots kept as frames.
|
|
225
|
+
* @param report - Receives unsupported bindings and style uses.
|
|
226
|
+
* @param fileId - Stable Figma file key or digest for unpublished resources.
|
|
227
|
+
* @returns Nothing.
|
|
228
|
+
*/
|
|
229
|
+
export function fileResources(result, doc, occurrences, expanded, expandedRadii, report, fileId) {
|
|
230
|
+
const { tree, variables, imported, styleLayers, textStyleUses } = result.sources;
|
|
231
|
+
const shared = [];
|
|
232
|
+
const identity = node => ({ name: node.name, key: String(node.change.key || `figma-${fileId()}-${node.id}`) });
|
|
233
|
+
fileVariables(result, shared, identity);
|
|
234
|
+
const bySource = new Map(doc.models.map((m) => [m.sourceId, m]));
|
|
235
|
+
const layerSource = new Map(imported.map(({ figma, node }) => [node.id, { figma, node }]));
|
|
236
|
+
const definitions = new Map();
|
|
237
|
+
const byKey = new Map();
|
|
238
|
+
const styleModels = new Set();
|
|
239
|
+
const styleUsers = new Map();
|
|
240
|
+
const records = new Map(doc.models.map(m => [m.id, m]));
|
|
241
|
+
function style(node, record) {
|
|
242
|
+
definitions.set(node.id, record);
|
|
243
|
+
byKey.set(record.type + '/' + record.key, record);
|
|
244
|
+
shared.push(record);
|
|
245
|
+
}
|
|
246
|
+
const textDefinitions = new Map();
|
|
247
|
+
for (const [node, use] of textStyleUses) {
|
|
248
|
+
const { id, style: text } = use.shared;
|
|
249
|
+
const { color, ...value } = runStyle(text);
|
|
250
|
+
textDefinitions.set(id, value);
|
|
251
|
+
styleUsers.set(id, []);
|
|
252
|
+
const fields = node.change.variableConsumptionMap?.entries ?? [];
|
|
253
|
+
for (const e of fields)
|
|
254
|
+
if (e.variableField !== 'FONT_SIZE')
|
|
255
|
+
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
256
|
+
const entry = fields.find((e) => e.variableField === 'FONT_SIZE');
|
|
257
|
+
const size = entry && variables.positiveLengthOf(entry.variableData, node.id);
|
|
258
|
+
if (entry && !size)
|
|
259
|
+
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
260
|
+
style(node, { ...identity(node), id, parent: STYLES, kind: 'style', type: 'text',
|
|
261
|
+
value: size ? { ...value, size: { alias: size } } : value });
|
|
262
|
+
}
|
|
263
|
+
function variableAt(path, data, type, id) {
|
|
264
|
+
const variable = variables.variableOf(data, id);
|
|
265
|
+
const length = path && /^(?:w|h|strokeWidth|opacity|radius(?:\.[0-3])?|look\.(?:layerBlur|backgroundBlur)\.radius|look\.shadows\.\d+\.blur)$/.test(path);
|
|
266
|
+
if (path && 'id' in variable && variable.type === type && (type !== 'number' || (path === 'opacity') === !!variable.opacity)
|
|
267
|
+
&& (!length || variables.valuesMatch(variable.id, n => typeof n === 'number' && n >= 0 && (path !== 'opacity' || n <= 1))))
|
|
268
|
+
return variable.id;
|
|
269
|
+
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
270
|
+
}
|
|
271
|
+
const binding = (m, target) => (path, data, type) => {
|
|
272
|
+
const variable = variableAt(path, data, type, m.id);
|
|
273
|
+
if (variable)
|
|
274
|
+
target(path, variable);
|
|
275
|
+
};
|
|
276
|
+
for (const [guid, layer] of styleLayers) {
|
|
277
|
+
const node = tree.nodes.get(guid), model = bySource.get(layer.id);
|
|
278
|
+
styleModels.add(model.id);
|
|
279
|
+
styleUsers.set(layer.id, []);
|
|
280
|
+
paintsAndEffects(node.change, model, binding(layer, (path, alias) => {
|
|
281
|
+
const parts = path.split('.'), last = parts.pop();
|
|
282
|
+
parts.reduce((v, part) => v[part], model)[last] = { alias };
|
|
283
|
+
}), result.sources);
|
|
284
|
+
const base = { ...identity(node), id: layer.id, parent: STYLES, kind: 'style' };
|
|
285
|
+
if (node.change.styleType === 'FILL')
|
|
286
|
+
style(node, { ...base, type: 'paint', value: model.look?.fills ?? (model.fill ? [{ k: 'solid', c: model.fill, o: 1 }] : []) });
|
|
287
|
+
else
|
|
288
|
+
style(node, { ...base, type: 'effect', value: { shadows: [], ...model.look } });
|
|
289
|
+
}
|
|
290
|
+
doc.models = doc.models.filter(m => !styleModels.has(m.id));
|
|
291
|
+
const textStyles = new Map(shared.flatMap(r => r.kind === 'style' && r.type === 'text' ? [[r.id, r]] : []));
|
|
292
|
+
function palette(entry, id) {
|
|
293
|
+
for (const [i, s] of (entry.styles ?? []).entries()) {
|
|
294
|
+
const { fileFontSize, textStyle, color, ...local } = s;
|
|
295
|
+
delete s.fileFontSize;
|
|
296
|
+
if (textStyle)
|
|
297
|
+
styleUsers.get(textStyle)?.push(id);
|
|
298
|
+
const styleSize = textStyles.get(textStyle)?.value.size;
|
|
299
|
+
const inherited = typeof styleSize === 'object' && styleSize.alias === fileFontSize;
|
|
300
|
+
if (textStyle && (JSON.stringify(local) !== JSON.stringify(textDefinitions.get(textStyle)) || (fileFontSize || fileFontSize === null && typeof styleSize === 'object') && !inherited)) {
|
|
301
|
+
delete s.textStyle;
|
|
302
|
+
report.push({ kind: 'approximated', what: 'text style with local formatting', count: 1, ids: [id] });
|
|
303
|
+
}
|
|
304
|
+
if (fileFontSize === null)
|
|
305
|
+
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
306
|
+
else if (fileFontSize && !(inherited && s.textStyle))
|
|
307
|
+
(entry.vars ??= {})['styles.' + i + '.size'] = fileFontSize;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const lost = (what, id) => report.push({ kind: 'left-out', what, count: 1, ids: [id] });
|
|
311
|
+
const isInstance = (m) => m.component && m.component !== m.id;
|
|
312
|
+
const slots = [
|
|
313
|
+
['styleIdForFill', 'fillStyle', 'paint', 'fillPaints'],
|
|
314
|
+
['styleIdForStrokeFill', 'strokeStyle', 'paint', 'strokePaints'],
|
|
315
|
+
['styleIdForEffect', 'effectStyle', 'effect', 'effects'],
|
|
316
|
+
];
|
|
317
|
+
// A virtual layer takes the main's formatting beneath any explicitly authored paints or effects.
|
|
318
|
+
function appearance(base, entry) {
|
|
319
|
+
const m = { ...base, look: { ...base.look, ...(base.look?.stroke && { stroke: { ...base.look.stroke } }) } };
|
|
320
|
+
if (entry?.['look.fills']) {
|
|
321
|
+
m.look.fills = entry['look.fills'];
|
|
322
|
+
delete m.fill;
|
|
323
|
+
}
|
|
324
|
+
if (entry?.['look.stroke.paint'] && m.look.stroke)
|
|
325
|
+
m.look.stroke.paint = entry['look.stroke.paint'];
|
|
326
|
+
for (const field of ['shadows', 'backgroundBlur', 'layerBlur'])
|
|
327
|
+
if (entry?.['look.' + field])
|
|
328
|
+
m.look[field] = entry['look.' + field];
|
|
329
|
+
return m;
|
|
330
|
+
}
|
|
331
|
+
function installRadius(change, m, root, entry, id, virtual) {
|
|
332
|
+
const expanded = expandedRadii.get(m.sourceId);
|
|
333
|
+
const local = radiusBindings(change);
|
|
334
|
+
const literal = change.cornerRadius !== undefined || corners.some(field => field in change);
|
|
335
|
+
const override = virtual || isInstance(m);
|
|
336
|
+
if ((!expanded || virtual) && !literal && !local.size) {
|
|
337
|
+
if (override && Object.keys(root.vars ?? {}).some(path => path.startsWith('radius.')))
|
|
338
|
+
delete entry.radius;
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
let inherited = override ? expanded ?? radiiOf(layerSource.get(root.sourceId).figma.change) : undefined;
|
|
342
|
+
if (virtual) {
|
|
343
|
+
const parts = id.split(';');
|
|
344
|
+
for (let i = parts.length - 2; i > 0; i--) {
|
|
345
|
+
const inner = occurrences.get(parts[i])?.get(parts.slice(i + 1).join(';'));
|
|
346
|
+
if (inner)
|
|
347
|
+
inherited = radiiOf(inner, inherited);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const effective = expanded && !virtual ? expanded : radiiOf(change, inherited);
|
|
351
|
+
const values = effective.map(c => c.value);
|
|
352
|
+
const stored = values.every(v => v === values[0]) ? values[0] : values;
|
|
353
|
+
if (isInstance(m)) {
|
|
354
|
+
if (effective.some(c => c.binding))
|
|
355
|
+
delete entry.radius; // A linked root inherits its main's sources.
|
|
356
|
+
else
|
|
357
|
+
entry.radius = stored;
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const resolved = new Map();
|
|
361
|
+
for (const c of effective)
|
|
362
|
+
if (c.binding && !resolved.has(c.binding.data))
|
|
363
|
+
resolved.set(c.binding.data, variableAt('radius', c.binding.data, 'number', id));
|
|
364
|
+
// A literal masks the entire tuple, so reinstate the unaffected corners' bindings beside it.
|
|
365
|
+
const writeLiteral = !!expanded || override && literal || [...resolved.values()].includes(undefined);
|
|
366
|
+
if (writeLiteral)
|
|
367
|
+
entry.radius = stored;
|
|
368
|
+
else if (override)
|
|
369
|
+
delete entry.radius;
|
|
370
|
+
effective.forEach((c, i) => {
|
|
371
|
+
if (!c.binding || !writeLiteral && inherited?.[i]?.binding === c.binding)
|
|
372
|
+
return;
|
|
373
|
+
const variable = resolved.get(c.binding.data);
|
|
374
|
+
if (variable)
|
|
375
|
+
(entry.vars ??= {})['radius.' + i] = variable;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
function install(change, m, into, id, virtual = false) {
|
|
379
|
+
const instance = isInstance(m);
|
|
380
|
+
const root = instance ? records.get(m.component) : m;
|
|
381
|
+
const rootBindings = instance ? new Set() : undefined;
|
|
382
|
+
const paint = instance ? appearance(root, m.overrides?.['']) : virtual ? appearance(m, into) : m;
|
|
383
|
+
const lists = { ...change };
|
|
384
|
+
for (const [field, slot, type, list] of slots) {
|
|
385
|
+
const ref = change[field];
|
|
386
|
+
if (!ref)
|
|
387
|
+
continue;
|
|
388
|
+
let style = ref.guid ? definitions.get(guidText(ref.guid, field)) : undefined;
|
|
389
|
+
if (style?.type !== type)
|
|
390
|
+
style = byKey.get(type + '/' + ref.assetRef?.key);
|
|
391
|
+
const fills = paint.look?.fills ?? (paint.fill ? [{ k: 'solid', c: paint.fill, o: 1 }] : []);
|
|
392
|
+
const stroke = paint.look?.stroke?.paint ?? (paint.stroke && { k: 'solid', c: paint.stroke, o: 1 });
|
|
393
|
+
const effects = style?.type === 'effect' && {
|
|
394
|
+
shadows: paint.look?.shadows ?? [],
|
|
395
|
+
layerBlur: paint.look?.layerBlur ?? (style.value.layerBlur && { radius: 0 }),
|
|
396
|
+
backgroundBlur: paint.look?.backgroundBlur ?? (style.value.backgroundBlur && { radius: 0 }),
|
|
397
|
+
};
|
|
398
|
+
if (style && !isDetachedStyle(ref))
|
|
399
|
+
styleUsers.get(style.id)?.push(id);
|
|
400
|
+
const differs = style?.type === 'paint' && !sameStyleValue(style.value, slot === 'fillStyle' ? fills : stroke ? [stroke] : [])
|
|
401
|
+
|| style?.type === 'effect' && !sameStyleValue(style.value, effects);
|
|
402
|
+
if (isDetachedStyle(ref) || !style || differs) {
|
|
403
|
+
if (differs)
|
|
404
|
+
lost('style reference', id);
|
|
405
|
+
if (virtual && !instance && m[slot]) {
|
|
406
|
+
into[slot] = null;
|
|
407
|
+
if (slot === 'fillStyle')
|
|
408
|
+
into['look.fills'] = fills;
|
|
409
|
+
else if (slot === 'strokeStyle' && paint.look.stroke)
|
|
410
|
+
into['look.stroke.paint'] = stroke;
|
|
411
|
+
else if (slot === 'effectStyle') {
|
|
412
|
+
into['look.shadows'] = paint.look.shadows ?? [];
|
|
413
|
+
into['look.layerBlur'] = paint.look.layerBlur ?? { radius: 0 };
|
|
414
|
+
into['look.backgroundBlur'] = paint.look.backgroundBlur ?? { radius: 0 };
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
if (m.kind === 'text' || instance && root[slot] !== style.id
|
|
420
|
+
|| slot === 'strokeStyle' && (!paint.look?.stroke || style.type !== 'paint' || style.value.length !== 1)) {
|
|
421
|
+
lost('style reference', id);
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
if (!instance)
|
|
425
|
+
into[slot] = style.id;
|
|
426
|
+
delete lists[list]; // Variable-backed paints in a named style are authored on its definition.
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const rootEntry = instance && !virtual ? m.overrides?.[''] ?? {} : into;
|
|
430
|
+
if (['frame', 'rect'].includes(m.kind))
|
|
431
|
+
installRadius(change, m, root, rootEntry, id, virtual);
|
|
432
|
+
const bind = binding({ id }, (path, variable) => {
|
|
433
|
+
if (virtual && (['w', 'h'].includes(path) || path.startsWith('layoutData.'))
|
|
434
|
+
|| instance && (path.startsWith('look.') || path === 'strokeWidth')) {
|
|
435
|
+
if (root.vars?.[path] !== variable)
|
|
436
|
+
lost('variable binding', id);
|
|
437
|
+
else
|
|
438
|
+
rootBindings?.add(path);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
(into.vars ??= {})[path] = variable;
|
|
442
|
+
if (virtual && path.startsWith('look.fills.') && !root.look?.fills)
|
|
443
|
+
into['look.fills'] = paint.look.fills;
|
|
444
|
+
if (virtual && path.startsWith('look.stroke.paint.') && !root.look?.stroke?.paint)
|
|
445
|
+
into['look.stroke.paint'] = paint.look.stroke.paint;
|
|
446
|
+
});
|
|
447
|
+
layerFields(change, m, bind);
|
|
448
|
+
paintsAndEffects(lists, paint, bind, result.sources);
|
|
449
|
+
for (const range of change.textData?.styleOverrideTable ?? []) {
|
|
450
|
+
for (const field of range.variableConsumptionMap?.entries ?? [])
|
|
451
|
+
if (field.variableField !== 'FONT_SIZE')
|
|
452
|
+
lost('variable binding', id);
|
|
453
|
+
if (range.styleIdForFill && !isDetachedStyle(range.styleIdForFill))
|
|
454
|
+
lost('style reference', id);
|
|
455
|
+
for (const fill of range.fillPaints ?? []) {
|
|
456
|
+
if (fill.colorVar)
|
|
457
|
+
lost('variable binding', id);
|
|
458
|
+
if (fill.opacityVar)
|
|
459
|
+
lost('variable binding', id);
|
|
460
|
+
for (const stop of fill.stopsVar ?? [])
|
|
461
|
+
if (stop.colorVar)
|
|
462
|
+
lost('variable binding', id);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (rootBindings && !virtual) {
|
|
466
|
+
inheritedLiterals(rootEntry, root, change, expanded.get(id), rootBindings);
|
|
467
|
+
if (Object.keys(rootEntry).length)
|
|
468
|
+
(m.overrides ??= {})[''] = rootEntry;
|
|
469
|
+
}
|
|
470
|
+
if (change.variableModeBySetMap)
|
|
471
|
+
for (const choice of change.variableModeBySetMap.entries ?? []) {
|
|
472
|
+
const mode = variables.modeOf(choice, id);
|
|
473
|
+
if (typeof mode === 'string' || !['page', 'frame'].includes(m.kind))
|
|
474
|
+
lost('variable mode', id);
|
|
475
|
+
else
|
|
476
|
+
(into.modes ??= {})[mode.collectionId] = mode.modeId;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
// Instance appearance comes from its main, which may live on a later page.
|
|
480
|
+
const layers = doc.models;
|
|
481
|
+
const mainLayers = new Map(layers.filter(m => m.component === m.id).map(m => [m.sourceId, m]));
|
|
482
|
+
const mains = new Map(result.components.map(c => [c.id, mainLayers.get(c.rootId)]));
|
|
483
|
+
function expandedFields(m, change) {
|
|
484
|
+
const source = layerSource.get(m.sourceId);
|
|
485
|
+
if (expandedRadii.has(m.sourceId) && source.node.type === 'instance') {
|
|
486
|
+
const main = mains.get(source.node.componentId);
|
|
487
|
+
const original = layerSource.get(main.sourceId).figma.change;
|
|
488
|
+
change = { ...change };
|
|
489
|
+
// The linked root would inherit its main's formatting. Keep those same literals and sources on the frame.
|
|
490
|
+
for (const [ref, slot, , list] of slots) {
|
|
491
|
+
if (ref in change || list in change)
|
|
492
|
+
continue;
|
|
493
|
+
if (ref in original)
|
|
494
|
+
change[ref] = original[ref];
|
|
495
|
+
if (list in original)
|
|
496
|
+
change[list] = original[list];
|
|
497
|
+
const keys = slot === 'fillStyle' ? ['fill'] : slot === 'strokeStyle' ? ['stroke', 'strokeWidth'] : [];
|
|
498
|
+
const look = slot === 'fillStyle' ? ['fills'] : slot === 'strokeStyle' ? ['stroke'] : ['shadows', 'layerBlur', 'backgroundBlur'];
|
|
499
|
+
for (const key of keys)
|
|
500
|
+
if (main[key] !== undefined)
|
|
501
|
+
m[key] = main[key];
|
|
502
|
+
for (const key of look)
|
|
503
|
+
if (main.look?.[key] !== undefined)
|
|
504
|
+
(m.look ??= {})[key] = structuredClone(main.look[key]);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const path = [m.sourceId];
|
|
508
|
+
for (let parent = records.get(m.parent); parent; parent = records.get(parent.parent)) {
|
|
509
|
+
if (expandedRadii.has(parent.sourceId)) {
|
|
510
|
+
const own = result.sources.occurrenceFields.get(parent.sourceId)?.get(layerPathKey(path));
|
|
511
|
+
if (own) {
|
|
512
|
+
change = { ...change, ...own };
|
|
513
|
+
for (const [ref, list] of STYLE_REFERENCE_FIELDS)
|
|
514
|
+
if (list in own && !(ref in own))
|
|
515
|
+
delete change[ref];
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (layerSource.get(parent.sourceId)?.node.type === 'instance')
|
|
519
|
+
path.unshift(parent.sourceId);
|
|
520
|
+
}
|
|
521
|
+
return change;
|
|
522
|
+
}
|
|
523
|
+
for (const m of [...layers.filter(m => !isInstance(m)), ...layers.filter(isInstance)]) {
|
|
524
|
+
palette(m, m.id);
|
|
525
|
+
const source = layerSource.get(m.sourceId);
|
|
526
|
+
if (source)
|
|
527
|
+
install(expandedRadii.size ? expandedFields(m, source.figma.change) : source.figma.change, m, m, m.id);
|
|
528
|
+
}
|
|
529
|
+
for (const m of layers) {
|
|
530
|
+
const authored = occurrences.get(m.id);
|
|
531
|
+
const paths = new Set([...Object.keys(m.overrides ?? {}), ...authored?.keys() ?? []]);
|
|
532
|
+
for (const path of paths) {
|
|
533
|
+
const id = m.id + ';' + path, entry = m.overrides?.[path] ?? {};
|
|
534
|
+
palette(entry, id);
|
|
535
|
+
const change = authored?.get(path);
|
|
536
|
+
const target = path ? records.get(path.split(';').at(-1)) : m;
|
|
537
|
+
if (path && target && !isInstance(target)) {
|
|
538
|
+
inheritedLiterals(entry, target, change ?? {}, expanded.get(id));
|
|
539
|
+
if (!change && Object.keys(target.vars ?? {}).some(key => key.startsWith('radius.')))
|
|
540
|
+
delete entry.radius;
|
|
541
|
+
}
|
|
542
|
+
if (change && target)
|
|
543
|
+
install(change, target, path ? entry : m, path ? id : m.id, !!path);
|
|
544
|
+
if (Object.keys(entry).length)
|
|
545
|
+
(m.overrides ??= {})[path] = entry;
|
|
546
|
+
else if (m.overrides)
|
|
547
|
+
delete m.overrides[path];
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
// A definition can be unused. Diagnostics for its bindings select only actual layer users.
|
|
551
|
+
for (const entry of report) {
|
|
552
|
+
if (!entry.ids?.some(id => styleUsers.has(id)))
|
|
553
|
+
continue;
|
|
554
|
+
const users = entry.ids.flatMap(id => styleUsers.get(id) ?? [id]);
|
|
555
|
+
if (users.length)
|
|
556
|
+
entry.ids = users;
|
|
557
|
+
else
|
|
558
|
+
delete entry.ids;
|
|
559
|
+
}
|
|
560
|
+
if (shared.length)
|
|
561
|
+
doc.shared = shared;
|
|
562
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { toDocument, type AssetFile, type ConvertOptions, type ConvertReport, type TacoDocument } from './convert.ts';
|
|
2
|
+
export { linkComponents, unmark, MARKERS, type LinkCounts } from './link.ts';
|
|
3
|
+
export { arcNetwork, ellipseNetwork, network } from './shapes.ts';
|
|
4
|
+
export { variableResolver, type VariableCollection, type VariableValue } from './variables.ts';
|
|
5
|
+
export { svgDocument, toPayload, type PayloadOptions } from './payload.ts';
|
|
6
|
+
export { figmaDocument } from './figma.ts';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The taco → Moondraft adapter: toDocument converts a page of a taco document into a Moondraft DocumentJSON and
|
|
2
|
+
// reports what it dropped; toPayload makes the result a clipboard paste payload (svgDocument wraps an SVG import as a
|
|
3
|
+
// taco document first); linkComponents links a converted page's expanded instances to their mains.
|
|
4
|
+
export { toDocument } from './convert.js';
|
|
5
|
+
export { linkComponents, unmark, MARKERS } from './link.js';
|
|
6
|
+
export { arcNetwork, ellipseNetwork, network } from './shapes.js';
|
|
7
|
+
export { variableResolver } from './variables.js';
|
|
8
|
+
export { svgDocument, toPayload } from './payload.js';
|
|
9
|
+
export { figmaDocument } from './figma.js';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** The converter's markers on the expanded models (instanceOf, mainOf, and setOf: an instance's variant set name). */
|
|
2
|
+
export declare const MARKERS: string[];
|
|
3
|
+
/**
|
|
4
|
+
* Removes component linking markers from a model.
|
|
5
|
+
* @param m - Converted model to update.
|
|
6
|
+
*/
|
|
7
|
+
export declare const unmark: (m: any) => void;
|
|
8
|
+
/** What linkComponents did: models before and after, instances linked, mains on the page and hidden mains used,
|
|
9
|
+
* the instances left expanded by their first difference, and how variant sets were linked (a main per variant, or
|
|
10
|
+
* one for the set). */
|
|
11
|
+
export interface LinkCounts {
|
|
12
|
+
/** Number of models before linking. */
|
|
13
|
+
before: number;
|
|
14
|
+
/** Number of models after linking. */
|
|
15
|
+
after: number;
|
|
16
|
+
/** Number of instances linked to mains. */
|
|
17
|
+
linked: number;
|
|
18
|
+
/** Number of component mains on the page. */
|
|
19
|
+
mains: number;
|
|
20
|
+
/** Number of hidden component mains used by linked instances. */
|
|
21
|
+
hidden: number;
|
|
22
|
+
/** Number of instances left expanded, grouped by their first difference from a main. */
|
|
23
|
+
unlinked: Map<string, number>;
|
|
24
|
+
/** Number of variant sets represented by separate mains for each variant. */
|
|
25
|
+
variantSets: number;
|
|
26
|
+
/** Number of variant sets that share one main across variants. */
|
|
27
|
+
sharedSets: number;
|
|
28
|
+
}
|
|
29
|
+
/** Links the instances of a converted page (its models, in file order) to their mains, in place. Returns counts.
|
|
30
|
+
* A hidden main only one instance would use (not a main on the page) costs more than that instance's expansion, so
|
|
31
|
+
* the page is linked again without it, that instance left expanded, until no hidden main is used once (an instance
|
|
32
|
+
* left expanded can make another's use count change).
|
|
33
|
+
* @param doc - Converted models in file order.
|
|
34
|
+
* @returns Counts of links, mains and instances left expanded.
|
|
35
|
+
*/
|
|
36
|
+
export declare function linkComponents(doc: {
|
|
37
|
+
models: any[];
|
|
38
|
+
}): LinkCounts;
|
|
39
|
+
/** @internal Links components and tracks where their expanded layers went for import reports. */
|
|
40
|
+
export declare function linkWithLayers(doc: {
|
|
41
|
+
models: any[];
|
|
42
|
+
}, trackLayers?: boolean): {
|
|
43
|
+
counts: LinkCounts;
|
|
44
|
+
aliases: Map<string, string[]>;
|
|
45
|
+
};
|