@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,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connectors used their own end style names before schema 0.20.0. These
|
|
3
|
+
* helpers give them the line end style names. They are internal to the
|
|
4
|
+
* model package: the codec and the store use them while loading.
|
|
5
|
+
*/
|
|
6
|
+
/** The connector end styles before 0.20.0, and the line end style each became. */
|
|
7
|
+
const LEGACY_CONNECTOR_CAPS = {
|
|
8
|
+
arrow: "triangle-arrow",
|
|
9
|
+
dot: "circle-arrow",
|
|
10
|
+
};
|
|
11
|
+
function renameLegacyConnectorCaps(value) {
|
|
12
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
13
|
+
return value;
|
|
14
|
+
const node = value;
|
|
15
|
+
if (node.type !== "connector")
|
|
16
|
+
return node;
|
|
17
|
+
const rename = (cap) => typeof cap === "string" && Object.hasOwn(LEGACY_CONNECTOR_CAPS, cap) ? LEGACY_CONNECTOR_CAPS[cap] : cap;
|
|
18
|
+
const startCap = rename(node.startCap);
|
|
19
|
+
const endCap = rename(node.endCap);
|
|
20
|
+
return startCap === node.startCap && endCap === node.endCap ? node : { ...node, startCap, endCap };
|
|
21
|
+
}
|
|
22
|
+
/** The node map with its connectors renamed; the same map when no connector has an old name. */
|
|
23
|
+
function renameLegacyConnectorCapsInNodeMap(value) {
|
|
24
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
25
|
+
return value;
|
|
26
|
+
const entries = Object.entries(value);
|
|
27
|
+
let changed = false;
|
|
28
|
+
const renamed = entries.map(([key, node]) => {
|
|
29
|
+
const next = renameLegacyConnectorCaps(node);
|
|
30
|
+
if (next !== node)
|
|
31
|
+
changed = true;
|
|
32
|
+
return [key, next];
|
|
33
|
+
});
|
|
34
|
+
return changed ? Object.fromEntries(renamed) : value;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Gives connectors in a document the line end style names: `"arrow"`
|
|
38
|
+
* becomes `"triangle-arrow"` and `"dot"` becomes `"circle-arrow"`. Leaves
|
|
39
|
+
* anything else as it is. The 0.19.0 to 0.20.0 step uses it, and so does
|
|
40
|
+
* loading a 0.20.0 document, because unreleased builds of 0.20.0 still wrote
|
|
41
|
+
* the old names.
|
|
42
|
+
*/
|
|
43
|
+
export function renameLegacyConnectorCapsInDocument(value) {
|
|
44
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
45
|
+
return value;
|
|
46
|
+
const document = value;
|
|
47
|
+
if (document.nodes === undefined)
|
|
48
|
+
return document;
|
|
49
|
+
const nodes = renameLegacyConnectorCapsInNodeMap(document.nodes);
|
|
50
|
+
return nodes === document.nodes ? document : { ...document, nodes };
|
|
51
|
+
}
|
|
52
|
+
/** The operation form of `renameLegacyConnectorCapsInDocument`. */
|
|
53
|
+
export function renameLegacyConnectorCapsInOperation(operation) {
|
|
54
|
+
if (typeof operation !== "object" || operation === null || Array.isArray(operation))
|
|
55
|
+
return operation;
|
|
56
|
+
const item = operation;
|
|
57
|
+
switch (item.type) {
|
|
58
|
+
case "node.insert":
|
|
59
|
+
case "page.insert": {
|
|
60
|
+
if (typeof item.subtree !== "object" || item.subtree === null)
|
|
61
|
+
return item;
|
|
62
|
+
const subtree = item.subtree;
|
|
63
|
+
return { ...item, subtree: { ...subtree, nodes: renameLegacyConnectorCapsInNodeMap(subtree.nodes) } };
|
|
64
|
+
}
|
|
65
|
+
case "node.replace":
|
|
66
|
+
return { ...item, node: renameLegacyConnectorCaps(item.node) };
|
|
67
|
+
default:
|
|
68
|
+
return item;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { BuildValue } from "./node-builder.ts";
|
|
2
|
+
import type { Rect, SceneNode } from "./types.ts";
|
|
3
|
+
declare const angle: unique symbol;
|
|
4
|
+
/** Parses a linear paint constructor until its owner supplies a paint box.
|
|
5
|
+
* @param degrees - CSS angle in degrees clockwise from up.
|
|
6
|
+
* @param stops - Parsed colour stops.
|
|
7
|
+
* @returns The pending syntax value.
|
|
8
|
+
*/
|
|
9
|
+
export declare function linearCall(degrees: number, stops: BuildValue): {
|
|
10
|
+
[angle]: number;
|
|
11
|
+
stops: BuildValue;
|
|
12
|
+
};
|
|
13
|
+
/** Projects a CSS gradient direction onto the owning layer's local box.
|
|
14
|
+
* @param degrees - CSS angle clockwise from up.
|
|
15
|
+
* @param box - Local paint dimensions.
|
|
16
|
+
* @returns The gradient endpoints in unit-box coordinates.
|
|
17
|
+
*/
|
|
18
|
+
export declare function linearEndpoints(degrees: number, box: Pick<Rect, "width" | "height">): {
|
|
19
|
+
from: {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
};
|
|
23
|
+
to: {
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/** Resolves a parsed constructor against its owner's local box.
|
|
29
|
+
* @param value - Parsed or stored paint.
|
|
30
|
+
* @param box - Supplies the owner's local dimensions when the value is a constructor.
|
|
31
|
+
* @returns The paint with unit-box endpoints, or the original value.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveLinearCall(value: BuildValue, box: () => {
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
}): BuildValue;
|
|
37
|
+
/** Finds the local paint dimensions of an authored layer.
|
|
38
|
+
* @param node - Layer owning the paint.
|
|
39
|
+
* @returns Its frame or untransformed network bounds.
|
|
40
|
+
*/
|
|
41
|
+
export declare function paintBox(node: SceneNode): Pick<Rect, "width" | "height">;
|
|
42
|
+
/** Resolves constructors in a layer argument, including nested fill and stroke lists.
|
|
43
|
+
* @param value - Parsed argument.
|
|
44
|
+
* @param node - Layer that will draw the paint.
|
|
45
|
+
* @returns The argument with its linear paint calls resolved.
|
|
46
|
+
*/
|
|
47
|
+
export declare function resolveLinearCalls(value: BuildValue, node: SceneNode): BuildValue;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { getVectorNetworkBounds } from "./geometry.js";
|
|
2
|
+
// Only syntax can create this marker. Stored linear paints always carry endpoints.
|
|
3
|
+
const angle = Symbol("linear gradient angle");
|
|
4
|
+
/** Parses a linear paint constructor until its owner supplies a paint box.
|
|
5
|
+
* @param degrees - CSS angle in degrees clockwise from up.
|
|
6
|
+
* @param stops - Parsed colour stops.
|
|
7
|
+
* @returns The pending syntax value.
|
|
8
|
+
*/
|
|
9
|
+
export function linearCall(degrees, stops) {
|
|
10
|
+
return { [angle]: degrees, stops };
|
|
11
|
+
}
|
|
12
|
+
/** Projects a CSS gradient direction onto the owning layer's local box.
|
|
13
|
+
* @param degrees - CSS angle clockwise from up.
|
|
14
|
+
* @param box - Local paint dimensions.
|
|
15
|
+
* @returns The gradient endpoints in unit-box coordinates.
|
|
16
|
+
*/
|
|
17
|
+
export function linearEndpoints(degrees, box) {
|
|
18
|
+
const theta = (degrees - 90) * Math.PI / 180;
|
|
19
|
+
const dx = Math.cos(theta), dy = Math.sin(theta);
|
|
20
|
+
const half = (box.width * Math.abs(dx) + box.height * Math.abs(dy)) / 2;
|
|
21
|
+
const x = box.width ? dx * half / box.width : 0, y = box.height ? dy * half / box.height : 0;
|
|
22
|
+
return { from: { x: 0.5 - x, y: 0.5 - y }, to: { x: 0.5 + x, y: 0.5 + y } };
|
|
23
|
+
}
|
|
24
|
+
function isLinearCall(value) {
|
|
25
|
+
return value !== null && typeof value === "object" && angle in value;
|
|
26
|
+
}
|
|
27
|
+
/** Resolves a parsed constructor against its owner's local box.
|
|
28
|
+
* @param value - Parsed or stored paint.
|
|
29
|
+
* @param box - Supplies the owner's local dimensions when the value is a constructor.
|
|
30
|
+
* @returns The paint with unit-box endpoints, or the original value.
|
|
31
|
+
*/
|
|
32
|
+
export function resolveLinearCall(value, box) {
|
|
33
|
+
if (!isLinearCall(value))
|
|
34
|
+
return value;
|
|
35
|
+
return { kind: "linear-gradient", ...linearEndpoints(value[angle], box()), stops: value.stops };
|
|
36
|
+
}
|
|
37
|
+
/** Finds the local paint dimensions of an authored layer.
|
|
38
|
+
* @param node - Layer owning the paint.
|
|
39
|
+
* @returns Its frame or untransformed network bounds.
|
|
40
|
+
*/
|
|
41
|
+
export function paintBox(node) {
|
|
42
|
+
return "frame" in node ? node.frame : getVectorNetworkBounds(node.vectorNetwork) ?? { width: 0, height: 0 };
|
|
43
|
+
}
|
|
44
|
+
/** Resolves constructors in a layer argument, including nested fill and stroke lists.
|
|
45
|
+
* @param value - Parsed argument.
|
|
46
|
+
* @param node - Layer that will draw the paint.
|
|
47
|
+
* @returns The argument with its linear paint calls resolved.
|
|
48
|
+
*/
|
|
49
|
+
export function resolveLinearCalls(value, node) {
|
|
50
|
+
if (!value || typeof value !== "object")
|
|
51
|
+
return value;
|
|
52
|
+
const paint = resolveLinearCall(value, () => paintBox(node));
|
|
53
|
+
if (paint !== value)
|
|
54
|
+
return paint;
|
|
55
|
+
if (Array.isArray(value))
|
|
56
|
+
return value.map(item => resolveLinearCalls(item, node));
|
|
57
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, resolveLinearCalls(v, node)]));
|
|
58
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { AssetId, CodeNode, CodeStyleId, ComponentId, ComponentPropertyValues, ConnectorEndpoint, ConnectorNode, DesignDocument, EllipseNode, FrameNode, GroupNode, ImageNode, InstanceNode, LayerOverride, Layout, LineNode, NodeId, PolygonNode, Rect, RectangleNode, SceneNode, StackAutoLayout, StarNode, TextNode, VideoNode } from "./types.ts";
|
|
2
|
+
/** Shapes drawn by dragging a box. */
|
|
3
|
+
export type ShapeDrawingKind = "rectangle" | "ellipse" | "polygon" | "star";
|
|
4
|
+
/** Shapes drawn by dragging from one endpoint to the other. */
|
|
5
|
+
export type LineDrawingKind = "line" | "arrow";
|
|
6
|
+
/** What a drawing gesture produced, in the parent's local coordinates. */
|
|
7
|
+
export type ShapeCreationRequest = {
|
|
8
|
+
readonly kind: ShapeDrawingKind;
|
|
9
|
+
readonly frame: Rect;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: LineDrawingKind;
|
|
12
|
+
readonly start: {
|
|
13
|
+
readonly x: number;
|
|
14
|
+
readonly y: number;
|
|
15
|
+
};
|
|
16
|
+
readonly end: {
|
|
17
|
+
readonly x: number;
|
|
18
|
+
readonly y: number;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
/** The name a new shape gets, before its number. */
|
|
22
|
+
export declare const SHAPE_LABELS: Readonly<Record<ShapeDrawingKind | LineDrawingKind, string>>;
|
|
23
|
+
/**
|
|
24
|
+
* The fill a new rectangle, ellipse, polygon or star gets. The Paint tool of
|
|
25
|
+
* vector edit mode gives the same fill to a region on a layer without fills.
|
|
26
|
+
*/
|
|
27
|
+
export declare const DEFAULT_SHAPE_FILL: Readonly<{
|
|
28
|
+
kind: "solid";
|
|
29
|
+
color: "#d9d9dd";
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* The defaults of a newly drawn shape. A rectangle, ellipse, polygon or star
|
|
33
|
+
* gets `DEFAULT_SHAPE_FILL` and no stroke; a line gets a 1px black center
|
|
34
|
+
* stroke and no fill. An arrow is a line whose end cap is a line arrow.
|
|
35
|
+
*
|
|
36
|
+
* Figma's help center states no defaults; its Plugin API pages are the
|
|
37
|
+
* reference: a new rectangle, ellipse, polygon or star has a default fill and
|
|
38
|
+
* no stroke, and a new line "has a black stroke, with weight 1".
|
|
39
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createrectangle/
|
|
40
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createellipse/
|
|
41
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createpolygon/
|
|
42
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createline/
|
|
43
|
+
* Figma does not document its default fill colour; ours is our own choice.
|
|
44
|
+
*/
|
|
45
|
+
export declare function newShapeNode(id: NodeId, name: string, request: ShapeCreationRequest): RectangleNode | EllipseNode | PolygonNode | StarNode | LineNode;
|
|
46
|
+
/** A new frame as the Frame tool draws it: free layout, no clipping, a white fill. */
|
|
47
|
+
export declare function newFrameNode(id: NodeId, name: string, frame: Rect): FrameNode;
|
|
48
|
+
/**
|
|
49
|
+
* The group or frame the Group, Frame selection and Add auto layout commands
|
|
50
|
+
* wrap layers in, with no children yet: no fill, and for a frame `layout`
|
|
51
|
+
* (free layout when not given).
|
|
52
|
+
*/
|
|
53
|
+
export declare function newWrapperNode(kind: "group" | "frame", id: NodeId, name: string, frame: Rect, layout?: Layout): GroupNode | FrameNode;
|
|
54
|
+
/**
|
|
55
|
+
* A row or column auto layout as Add auto layout starts it: hugging its
|
|
56
|
+
* content both ways, with the default gap, no padding, and its children at
|
|
57
|
+
* the start. Add auto layout then reads the gap, padding and alignment from
|
|
58
|
+
* where the layers sit.
|
|
59
|
+
*/
|
|
60
|
+
export declare function newStackAutoLayout(kind: StackAutoLayout["kind"]): StackAutoLayout;
|
|
61
|
+
/** How the Text tool makes a text: sized to its content ("auto"), or in the drawn box ("fixed"). */
|
|
62
|
+
export type TextCreationMode = "fixed" | "auto";
|
|
63
|
+
/**
|
|
64
|
+
* A new, empty text layer in the document's default text style. "auto" sizes
|
|
65
|
+
* itself to its content and starts one line high; "fixed" keeps `frame` and
|
|
66
|
+
* wraps its words.
|
|
67
|
+
*/
|
|
68
|
+
export declare function newTextNode(document: DesignDocument, id: NodeId, name: string, frame: Rect, mode: TextCreationMode): TextNode;
|
|
69
|
+
/** The code style a new code block uses: its id, and the font family and size the block is written in. */
|
|
70
|
+
export interface NewCodeStyle {
|
|
71
|
+
readonly codeStyleId: CodeStyleId;
|
|
72
|
+
readonly fontFamily: string;
|
|
73
|
+
readonly fontSize: number;
|
|
74
|
+
}
|
|
75
|
+
/** A new, empty TypeScript code block as the Code tool draws it, with line numbers and no wrapping. */
|
|
76
|
+
export declare function newCodeNode(id: NodeId, name: string, frame: Rect, style: NewCodeStyle): CodeNode;
|
|
77
|
+
/** A new image or video layer showing `assetId`, filling its box (`cover`), as a pasted or dropped file makes it. */
|
|
78
|
+
export declare function newMediaNode(kind: "image" | "video", id: NodeId, name: string, frame: Rect, assetId: AssetId, alt: string): ImageNode | VideoNode;
|
|
79
|
+
/** The two ends of a new connector, in its parent's coordinates. */
|
|
80
|
+
export interface NewConnectorEnds {
|
|
81
|
+
readonly start: ConnectorEndpoint;
|
|
82
|
+
readonly end: ConnectorEndpoint;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A connector as the Connector tool creates it: bent (elbow routing, as
|
|
86
|
+
* FigJam's X draws), with a 2 pixel black stroke and an arrowhead at the end,
|
|
87
|
+
* its frame the box around both ends and each end relative to that frame.
|
|
88
|
+
*/
|
|
89
|
+
export declare function newConnectorNode(ends: NewConnectorEnds, id: NodeId, name: string): ConnectorNode;
|
|
90
|
+
/**
|
|
91
|
+
* Numbers for the names of new layers ("Rectangle 3"): each call answers the
|
|
92
|
+
* next number not yet used after `label` in `document`, starting after the
|
|
93
|
+
* count of the layers `countNode` counts (by default, those whose name starts
|
|
94
|
+
* with `label`).
|
|
95
|
+
*/
|
|
96
|
+
export declare function nodeSequenceAllocator(document: DesignDocument, label: string, countNode?: (node: SceneNode) => boolean): () => number;
|
|
97
|
+
/** Why `newInstanceNode` refused to build an instance. */
|
|
98
|
+
export type InstanceBuildErrorKind =
|
|
99
|
+
/** The document has no component with the given ID. */
|
|
100
|
+
"missing-component"
|
|
101
|
+
/** The component's root layer is missing or has no frame to size from. */
|
|
102
|
+
| "missing-component-root"
|
|
103
|
+
/** A property value names a property the component does not have. */
|
|
104
|
+
| "unknown-property"
|
|
105
|
+
/** A property value does not fit its property's kind (see `propertyValueFits`). */
|
|
106
|
+
| "invalid-property-value"
|
|
107
|
+
/**
|
|
108
|
+
* The values are not a combination the component's validity expression
|
|
109
|
+
* allows (see `invalidCombinationProblem`, whose message the error has).
|
|
110
|
+
* @since 0.35.0
|
|
111
|
+
*/
|
|
112
|
+
| "invalid-combination";
|
|
113
|
+
/** The error `newInstanceNode` throws; `kind` says why. */
|
|
114
|
+
export declare class InstanceBuildError extends Error {
|
|
115
|
+
readonly kind: InstanceBuildErrorKind;
|
|
116
|
+
readonly componentId: ComponentId;
|
|
117
|
+
/** The property ID, for `unknown-property` and `invalid-property-value`. */
|
|
118
|
+
readonly propertyId: string | undefined;
|
|
119
|
+
constructor(kind: InstanceBuildErrorKind, message: string, componentId: ComponentId, propertyId?: string);
|
|
120
|
+
}
|
|
121
|
+
/** What `newInstanceNode` builds: the component, where, and the instance's own values. */
|
|
122
|
+
export interface NewInstanceRequest {
|
|
123
|
+
readonly componentId: ComponentId;
|
|
124
|
+
/** The instance's top-left corner in its parent's coordinates; 0 when omitted. */
|
|
125
|
+
readonly x?: number;
|
|
126
|
+
readonly y?: number;
|
|
127
|
+
/** Values for the component's properties, keyed by property ID. Omitted or empty means every default. */
|
|
128
|
+
readonly propertyValues?: ComponentPropertyValues;
|
|
129
|
+
/**
|
|
130
|
+
* Changes to the component's layers, keyed by layer path (see
|
|
131
|
+
* `InstanceNode.overrides`). Stored as given; the store's checks refuse a
|
|
132
|
+
* malformed override when the instance is inserted.
|
|
133
|
+
*/
|
|
134
|
+
readonly overrides?: Readonly<Record<string, LayerOverride>>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A new instance of a component, as the place-instance command makes it: the
|
|
138
|
+
* component's name, the size of the component's root, visible, unlocked,
|
|
139
|
+
* opaque and unrotated, with the given property values and overrides (empty
|
|
140
|
+
* ones are left out). `makeId` gives the instance's ID. Throws an
|
|
141
|
+
* `InstanceBuildError` when the component or its root is missing, a
|
|
142
|
+
* property value names an unknown property or does not fit its kind, or the
|
|
143
|
+
* values are not a valid combination of the component.
|
|
144
|
+
*/
|
|
145
|
+
export declare function newInstanceNode(document: DesignDocument, request: NewInstanceRequest, makeId: () => NodeId): InstanceNode;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { findComponentProperty, propertyValueFits } from "./component-properties.js";
|
|
2
|
+
import { invalidCombinationProblem } from "./component-validity.js";
|
|
3
|
+
import { entryOf } from "./id-table.js";
|
|
4
|
+
import { createFrameLayout } from "./layout/frame-layout.js";
|
|
5
|
+
import { DEFAULT_POLYGON_POINT_COUNT, DEFAULT_STAR_INNER_RADIUS, DEFAULT_STAR_POINT_COUNT, lineFrameFromEndpoints, } from "./shape-geometry.js";
|
|
6
|
+
import { tableKeys } from "./shared-node-table.js";
|
|
7
|
+
/** The name a new shape gets, before its number. */
|
|
8
|
+
export const SHAPE_LABELS = {
|
|
9
|
+
rectangle: "Rectangle",
|
|
10
|
+
ellipse: "Ellipse",
|
|
11
|
+
polygon: "Polygon",
|
|
12
|
+
star: "Star",
|
|
13
|
+
line: "Line",
|
|
14
|
+
arrow: "Arrow",
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The fill a new rectangle, ellipse, polygon or star gets. The Paint tool of
|
|
18
|
+
* vector edit mode gives the same fill to a region on a layer without fills.
|
|
19
|
+
*/
|
|
20
|
+
export const DEFAULT_SHAPE_FILL = Object.freeze({ kind: "solid", color: "#d9d9dd" });
|
|
21
|
+
/**
|
|
22
|
+
* The defaults of a newly drawn shape. A rectangle, ellipse, polygon or star
|
|
23
|
+
* gets `DEFAULT_SHAPE_FILL` and no stroke; a line gets a 1px black center
|
|
24
|
+
* stroke and no fill. An arrow is a line whose end cap is a line arrow.
|
|
25
|
+
*
|
|
26
|
+
* Figma's help center states no defaults; its Plugin API pages are the
|
|
27
|
+
* reference: a new rectangle, ellipse, polygon or star has a default fill and
|
|
28
|
+
* no stroke, and a new line "has a black stroke, with weight 1".
|
|
29
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createrectangle/
|
|
30
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createellipse/
|
|
31
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createpolygon/
|
|
32
|
+
* https://developers.figma.com/docs/plugins/api/properties/figma-createline/
|
|
33
|
+
* Figma does not document its default fill colour; ours is our own choice.
|
|
34
|
+
*/
|
|
35
|
+
export function newShapeNode(id, name, request) {
|
|
36
|
+
const base = { id, name, visible: true, locked: false, opacity: 1, type: "shape" };
|
|
37
|
+
if ("start" in request) {
|
|
38
|
+
const { frame, rotation } = lineFrameFromEndpoints(request.start, request.end);
|
|
39
|
+
return {
|
|
40
|
+
...base,
|
|
41
|
+
shape: "line",
|
|
42
|
+
frame,
|
|
43
|
+
rotation,
|
|
44
|
+
startCap: "none",
|
|
45
|
+
endCap: request.kind === "arrow" ? "line-arrow" : "none",
|
|
46
|
+
style: {
|
|
47
|
+
strokes: [{ paint: { kind: "solid", color: "#000000" }, width: 1, position: "center" }],
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const style = { fills: [DEFAULT_SHAPE_FILL] };
|
|
52
|
+
const framed = { ...base, frame: request.frame, rotation: 0, style };
|
|
53
|
+
switch (request.kind) {
|
|
54
|
+
case "rectangle":
|
|
55
|
+
return { ...framed, shape: "rectangle" };
|
|
56
|
+
case "ellipse":
|
|
57
|
+
return { ...framed, shape: "ellipse" };
|
|
58
|
+
case "polygon":
|
|
59
|
+
return { ...framed, shape: "polygon", pointCount: DEFAULT_POLYGON_POINT_COUNT };
|
|
60
|
+
case "star":
|
|
61
|
+
return {
|
|
62
|
+
...framed,
|
|
63
|
+
shape: "star",
|
|
64
|
+
pointCount: DEFAULT_STAR_POINT_COUNT,
|
|
65
|
+
innerRadius: DEFAULT_STAR_INNER_RADIUS,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** A new frame as the Frame tool draws it: free layout, no clipping, a white fill. */
|
|
70
|
+
export function newFrameNode(id, name, frame) {
|
|
71
|
+
return {
|
|
72
|
+
id, name, visible: true, locked: false, opacity: 1, type: "frame", frame, rotation: 0, children: [],
|
|
73
|
+
layout: { kind: "absolute" }, clipContent: false, style: { fills: [{ kind: "solid", color: "#ffffff" }] },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The group or frame the Group, Frame selection and Add auto layout commands
|
|
78
|
+
* wrap layers in, with no children yet: no fill, and for a frame `layout`
|
|
79
|
+
* (free layout when not given).
|
|
80
|
+
*/
|
|
81
|
+
export function newWrapperNode(kind, id, name, frame, layout) {
|
|
82
|
+
const shared = { id, name, visible: true, locked: false, opacity: 1, frame, rotation: 0, children: [] };
|
|
83
|
+
return kind === "group"
|
|
84
|
+
? { ...shared, type: "group" }
|
|
85
|
+
: { ...shared, type: "frame", layout: layout ?? createFrameLayout("absolute") };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A row or column auto layout as Add auto layout starts it: hugging its
|
|
89
|
+
* content both ways, with the default gap, no padding, and its children at
|
|
90
|
+
* the start. Add auto layout then reads the gap, padding and alignment from
|
|
91
|
+
* where the layers sit.
|
|
92
|
+
*/
|
|
93
|
+
export function newStackAutoLayout(kind) {
|
|
94
|
+
const layout = createFrameLayout(kind);
|
|
95
|
+
if (layout.kind !== kind)
|
|
96
|
+
throw new TypeError("Expected a stack auto layout");
|
|
97
|
+
return { ...layout, widthSizing: "hug", heightSizing: "hug" };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A new, empty text layer in the document's default text style. "auto" sizes
|
|
101
|
+
* itself to its content and starts one line high; "fixed" keeps `frame` and
|
|
102
|
+
* wraps its words.
|
|
103
|
+
*/
|
|
104
|
+
export function newTextNode(document, id, name, frame, mode) {
|
|
105
|
+
const textStyle = entryOf(document.theme.textStyles, document.theme.defaultTextStyleId);
|
|
106
|
+
const fontSize = textStyle?.fontSize ?? 40;
|
|
107
|
+
const lineHeight = textStyle?.lineHeight ?? 1.2;
|
|
108
|
+
return {
|
|
109
|
+
id,
|
|
110
|
+
name,
|
|
111
|
+
visible: true,
|
|
112
|
+
locked: false,
|
|
113
|
+
opacity: 1,
|
|
114
|
+
type: "text",
|
|
115
|
+
frame: mode === "auto"
|
|
116
|
+
? {
|
|
117
|
+
x: frame.x,
|
|
118
|
+
y: frame.y,
|
|
119
|
+
width: Math.max(1, Math.ceil(fontSize * 0.5)),
|
|
120
|
+
height: Math.max(1, Math.ceil(fontSize * lineHeight)),
|
|
121
|
+
}
|
|
122
|
+
: frame,
|
|
123
|
+
rotation: 0,
|
|
124
|
+
paragraphs: [{ runs: [{ text: "" }] }],
|
|
125
|
+
verticalAlign: "top",
|
|
126
|
+
overflow: "visible",
|
|
127
|
+
autoResize: mode === "auto" ? "width-and-height" : "none",
|
|
128
|
+
wrap: mode === "auto" ? "none" : "word",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** A new, empty TypeScript code block as the Code tool draws it, with line numbers and no wrapping. */
|
|
132
|
+
export function newCodeNode(id, name, frame, style) {
|
|
133
|
+
return {
|
|
134
|
+
id,
|
|
135
|
+
name,
|
|
136
|
+
visible: true,
|
|
137
|
+
locked: false,
|
|
138
|
+
opacity: 1,
|
|
139
|
+
type: "code",
|
|
140
|
+
frame,
|
|
141
|
+
rotation: 0,
|
|
142
|
+
source: "",
|
|
143
|
+
language: "typescript",
|
|
144
|
+
fontFamily: style.fontFamily,
|
|
145
|
+
fontSize: style.fontSize,
|
|
146
|
+
codeStyleId: style.codeStyleId,
|
|
147
|
+
lineNumbers: true,
|
|
148
|
+
autoResize: "none",
|
|
149
|
+
wordWrap: false,
|
|
150
|
+
decorations: [],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** A new image or video layer showing `assetId`, filling its box (`cover`), as a pasted or dropped file makes it. */
|
|
154
|
+
export function newMediaNode(kind, id, name, frame, assetId, alt) {
|
|
155
|
+
return { id, name, visible: true, locked: false, opacity: 1, type: kind, frame, rotation: 0, assetId, fit: "cover", alt };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* A connector as the Connector tool creates it: bent (elbow routing, as
|
|
159
|
+
* FigJam's X draws), with a 2 pixel black stroke and an arrowhead at the end,
|
|
160
|
+
* its frame the box around both ends and each end relative to that frame.
|
|
161
|
+
*/
|
|
162
|
+
export function newConnectorNode(ends, id, name) {
|
|
163
|
+
const { start, end } = ends;
|
|
164
|
+
const x = Math.min(start.x, end.x);
|
|
165
|
+
const y = Math.min(start.y, end.y);
|
|
166
|
+
return {
|
|
167
|
+
id,
|
|
168
|
+
name,
|
|
169
|
+
visible: true,
|
|
170
|
+
locked: false,
|
|
171
|
+
opacity: 1,
|
|
172
|
+
type: "connector",
|
|
173
|
+
frame: { x, y, width: Math.abs(end.x - start.x), height: Math.abs(end.y - start.y) },
|
|
174
|
+
rotation: 0,
|
|
175
|
+
start: { ...start, x: start.x - x, y: start.y - y },
|
|
176
|
+
end: { ...end, x: end.x - x, y: end.y - y },
|
|
177
|
+
routing: "elbow",
|
|
178
|
+
stroke: { paint: { kind: "solid", color: "#000000" }, width: 2 },
|
|
179
|
+
startCap: "none",
|
|
180
|
+
endCap: "triangle-arrow",
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Numbers for the names of new layers ("Rectangle 3"): each call answers the
|
|
185
|
+
* next number not yet used after `label` in `document`, starting after the
|
|
186
|
+
* count of the layers `countNode` counts (by default, those whose name starts
|
|
187
|
+
* with `label`).
|
|
188
|
+
*/
|
|
189
|
+
export function nodeSequenceAllocator(document, label, countNode = (node) => node.name.startsWith(`${label} `)) {
|
|
190
|
+
const prefix = `${label} `;
|
|
191
|
+
const used = new Set();
|
|
192
|
+
let count = 0;
|
|
193
|
+
for (const id of tableKeys(document.nodes)) {
|
|
194
|
+
const node = entryOf(document.nodes, id);
|
|
195
|
+
if (node === undefined)
|
|
196
|
+
continue;
|
|
197
|
+
if (countNode(node))
|
|
198
|
+
count += 1;
|
|
199
|
+
if (!node.name.startsWith(prefix))
|
|
200
|
+
continue;
|
|
201
|
+
const suffix = node.name.slice(prefix.length);
|
|
202
|
+
if (!/^[1-9]\d*$/.test(suffix))
|
|
203
|
+
continue;
|
|
204
|
+
const sequence = Number(suffix);
|
|
205
|
+
if (Number.isSafeInteger(sequence))
|
|
206
|
+
used.add(sequence);
|
|
207
|
+
}
|
|
208
|
+
let sequence = count + 1;
|
|
209
|
+
return () => {
|
|
210
|
+
while (used.has(sequence))
|
|
211
|
+
sequence += 1;
|
|
212
|
+
used.add(sequence);
|
|
213
|
+
return sequence++;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** The error `newInstanceNode` throws; `kind` says why. */
|
|
217
|
+
export class InstanceBuildError extends Error {
|
|
218
|
+
kind;
|
|
219
|
+
componentId;
|
|
220
|
+
/** The property ID, for `unknown-property` and `invalid-property-value`. */
|
|
221
|
+
propertyId;
|
|
222
|
+
constructor(kind, message, componentId, propertyId) {
|
|
223
|
+
super(message);
|
|
224
|
+
this.kind = kind;
|
|
225
|
+
this.componentId = componentId;
|
|
226
|
+
this.propertyId = propertyId;
|
|
227
|
+
this.name = "InstanceBuildError";
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* A new instance of a component, as the place-instance command makes it: the
|
|
232
|
+
* component's name, the size of the component's root, visible, unlocked,
|
|
233
|
+
* opaque and unrotated, with the given property values and overrides (empty
|
|
234
|
+
* ones are left out). `makeId` gives the instance's ID. Throws an
|
|
235
|
+
* `InstanceBuildError` when the component or its root is missing, a
|
|
236
|
+
* property value names an unknown property or does not fit its kind, or the
|
|
237
|
+
* values are not a valid combination of the component.
|
|
238
|
+
*/
|
|
239
|
+
export function newInstanceNode(document, request, makeId) {
|
|
240
|
+
const { componentId } = request;
|
|
241
|
+
const component = entryOf(document.components, componentId);
|
|
242
|
+
if (component === undefined) {
|
|
243
|
+
throw new InstanceBuildError("missing-component", `There is no component ${componentId}.`, componentId);
|
|
244
|
+
}
|
|
245
|
+
const root = entryOf(document.nodes, component.rootId);
|
|
246
|
+
if (root === undefined || !("frame" in root)) {
|
|
247
|
+
throw new InstanceBuildError("missing-component-root", `The component ${component.name} has no root layer to size an instance from.`, componentId);
|
|
248
|
+
}
|
|
249
|
+
const propertyValues = request.propertyValues ?? {};
|
|
250
|
+
for (const [propertyId, value] of Object.entries(propertyValues)) {
|
|
251
|
+
const property = findComponentProperty(component, propertyId);
|
|
252
|
+
if (property === undefined) {
|
|
253
|
+
throw new InstanceBuildError("unknown-property", `The component ${component.name} has no property ${propertyId}.`, componentId, propertyId);
|
|
254
|
+
}
|
|
255
|
+
if (!propertyValueFits(property, value)) {
|
|
256
|
+
throw new InstanceBuildError("invalid-property-value", `The value ${JSON.stringify(value)} does not fit the ${property.kind} property ${property.name} of ${component.name}.`, componentId, propertyId);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const problem = invalidCombinationProblem(component, propertyValues);
|
|
260
|
+
if (problem !== undefined)
|
|
261
|
+
throw new InstanceBuildError("invalid-combination", problem, componentId);
|
|
262
|
+
const overrides = request.overrides ?? {};
|
|
263
|
+
const id = makeId();
|
|
264
|
+
return {
|
|
265
|
+
id,
|
|
266
|
+
type: "instance",
|
|
267
|
+
componentId,
|
|
268
|
+
name: component.name,
|
|
269
|
+
visible: true,
|
|
270
|
+
locked: false,
|
|
271
|
+
opacity: 1,
|
|
272
|
+
frame: { x: request.x ?? 0, y: request.y ?? 0, width: root.frame.width, height: root.frame.height },
|
|
273
|
+
rotation: 0,
|
|
274
|
+
...(Object.keys(overrides).length > 0 ? { overrides: { ...overrides } } : {}),
|
|
275
|
+
...(Object.keys(propertyValues).length > 0 ? { propertyValues: { ...propertyValues } } : {}),
|
|
276
|
+
};
|
|
277
|
+
}
|