@moondraft/import 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +45 -0
- package/dist/adapter/variables.js +106 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5665 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +96 -0
- package/dist/taco/model/component-evaluation.js +555 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +79 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2369 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +55 -4
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
import { field, guidText, isObject } from "../fields.js";
|
|
2
|
+
import { isDetachedStyle, styleBackedList } from "../styles.js";
|
|
3
|
+
import { framedBase, hexColor, readNumbers } from "./shared.js";
|
|
4
|
+
const ALIGN = { LEFT: "left", CENTER: "center", RIGHT: "right", JUSTIFIED: "justify" };
|
|
5
|
+
const VERTICAL_ALIGN = { TOP: "top", CENTER: "middle", BOTTOM: "bottom" };
|
|
6
|
+
const AUTO_RESIZE = { NONE: "none", HEIGHT: "height", WIDTH_AND_HEIGHT: "width-and-height" };
|
|
7
|
+
/** Weights by style-name word, spaces, hyphens and case ignored, as CSS font-weight. */
|
|
8
|
+
const WEIGHTS = {
|
|
9
|
+
thin: 100, hairline: 100, extralight: 200, ultralight: 200, light: 300, regular: 400, normal: 400, book: 400, "": 400,
|
|
10
|
+
medium: 500, semibold: 600, demibold: 600, bold: 700, extrabold: 800, ultrabold: 800, black: 900, heavy: 900,
|
|
11
|
+
};
|
|
12
|
+
/** The weight a Figma font style name such as "Semi Bold Italic" names (undefined when it names no known weight), and whether it is italic. */
|
|
13
|
+
export function fontStyleWeight(styleName) {
|
|
14
|
+
const words = styleName.toLowerCase().replace(/[\s_-]/g, "");
|
|
15
|
+
return { fontWeight: WEIGHTS[words.replace(/italic|oblique/g, "")], italic: /italic|oblique/.test(words) };
|
|
16
|
+
}
|
|
17
|
+
/** Figma's line height when `lineHeight` is 100 percent ("Auto") and the font's own line height is not in the message. */
|
|
18
|
+
const AUTO_LINE_HEIGHT = 1.2;
|
|
19
|
+
const AUTO_LINE_HEIGHT_REASON = `the auto line height is imported as ${String(AUTO_LINE_HEIGHT)} times the font size; the font's own line height is not in the message`;
|
|
20
|
+
const LINE_HEIGHT_VARIABLE_REASON = "line height variable kept as its value: Taco's line height is a multiple of the font size";
|
|
21
|
+
/**
|
|
22
|
+
* A field the importer adds to a text layer's Figma fields, never one Figma writes: the line height in pixels Figma laid
|
|
23
|
+
* out every line of this layer at in an instance (its stored `derivedTextData`), which replaces the line height the
|
|
24
|
+
* layer's runs would take from their text style or variable. The runs stay linked to their style, with this line height
|
|
25
|
+
* as an override.
|
|
26
|
+
*/
|
|
27
|
+
export const KEPT_LINE_HEIGHT_FIELD = "tacoKeptLineHeight";
|
|
28
|
+
/**
|
|
29
|
+
* A Figma TEXT node as a Taco text node. `textData.characters` is split into
|
|
30
|
+
* paragraphs at each newline, and each paragraph into runs where the style id
|
|
31
|
+
* changes. `textData.characterStyleIDs` holds one id per code point and may
|
|
32
|
+
* be shorter than the text: missing ids are 0. Id 0 is the node's own style;
|
|
33
|
+
* every other id names an entry of `textData.styleOverrideTable` whose fields
|
|
34
|
+
* replace the node's. Fonts are named by family, weight and italic only;
|
|
35
|
+
* `fontAssetId` is left for the host to bind. A node or override entry that
|
|
36
|
+
* names a Figma text style (`styleIdForText`) takes the style's typography
|
|
37
|
+
* over the node's stored copies (an override entry's own fields still win, and
|
|
38
|
+
* the colour stays the node's), and its runs link to the shared style the walk made
|
|
39
|
+
* for it (`NodeWalk.textStyle`), with only the fields that differ as overrides.
|
|
40
|
+
* A line height bound to a variable (`LINE_HEIGHT`) is the variable's value in
|
|
41
|
+
* the mode the node uses (`NodeWalk.variableNumber`), as a fixed multiple of the
|
|
42
|
+
* font size, read from the same place as the line height itself (`runBinding`).
|
|
43
|
+
*/
|
|
44
|
+
export const convertText = (node, walk) => {
|
|
45
|
+
const where = `node ${node.id}`;
|
|
46
|
+
const change = node.change;
|
|
47
|
+
const base = framedBase(node, walk, "Text");
|
|
48
|
+
if (base === null)
|
|
49
|
+
return null;
|
|
50
|
+
// Taco text has no strokes yet, so a text stroke is reported rather than dropped.
|
|
51
|
+
if ((styleBackedList(change, "strokePaints", walk.styleList, where)?.length ?? 0) > 0)
|
|
52
|
+
walk.report("property-left-out", "text strokes are not imported yet");
|
|
53
|
+
const textData = field(change, "textData", "object", where) ?? {};
|
|
54
|
+
const characters = field(textData, "characters", "string", `${where} textData`) ?? "";
|
|
55
|
+
const ids = field(textData, "characterStyleIDs", "list", `${where} textData`) ?? [];
|
|
56
|
+
const table = new Map();
|
|
57
|
+
for (const entry of field(textData, "styleOverrideTable", "list", `${where} textData`) ?? []) {
|
|
58
|
+
if (!isObject(entry))
|
|
59
|
+
continue;
|
|
60
|
+
const id = field(entry, "styleID", "number", `${where} styleOverrideTable`);
|
|
61
|
+
if (id !== undefined && id !== 0)
|
|
62
|
+
table.set(id, entry);
|
|
63
|
+
}
|
|
64
|
+
// Each report reason once per node, however many runs share it, and at most
|
|
65
|
+
// MAX_REPORTS reasons per node: each entry carries the node's path, so many
|
|
66
|
+
// style ids must not make many entries.
|
|
67
|
+
// Reasons past the limit are still remembered, so each is counted once.
|
|
68
|
+
const reasons = new Set();
|
|
69
|
+
let reasonsLeftOut = 0;
|
|
70
|
+
const report = (kind, reason) => {
|
|
71
|
+
if (reasons.has(`${kind} ${reason}`))
|
|
72
|
+
return;
|
|
73
|
+
reasons.add(`${kind} ${reason}`);
|
|
74
|
+
if (reasons.size > MAX_REPORTS)
|
|
75
|
+
reasonsLeftOut += 1;
|
|
76
|
+
else
|
|
77
|
+
walk.report(kind, reason);
|
|
78
|
+
};
|
|
79
|
+
// Built once per node so each run's lookups do not scan the whole list.
|
|
80
|
+
const lookups = runLookups(change, where);
|
|
81
|
+
const keptLineHeight = typeof change[KEPT_LINE_HEIGHT_FIELD] === "number" ? change[KEPT_LINE_HEIGHT_FIELD] : undefined;
|
|
82
|
+
const nodeTextStyle = change["styleIdForText"] === undefined ? undefined : walk.textStyle?.(change["styleIdForText"], change);
|
|
83
|
+
if (change["styleIdForText"] !== undefined && walk.textStyle === undefined)
|
|
84
|
+
report("property-left-out", "the Figma text style is not imported; its values are kept as direct formatting");
|
|
85
|
+
// A text layer or styled range that names a fill style is drawn in the style's paints (`styleBackedList`).
|
|
86
|
+
const nodeFills = styleBackedList(change, "fillPaints", walk.styleList, where, report);
|
|
87
|
+
const filled = nodeFills === change["fillPaints"] ? change : { ...change, fillPaints: nodeFills };
|
|
88
|
+
const entryFilled = (override) => {
|
|
89
|
+
if (override?.["styleIdForFill"] === undefined)
|
|
90
|
+
return override;
|
|
91
|
+
const fills = styleBackedList(override, "fillPaints", walk.styleList, `${where} styleOverrideTable`, report);
|
|
92
|
+
return fills === override["fillPaints"] ? override : { ...override, fillPaints: fills };
|
|
93
|
+
};
|
|
94
|
+
const styles = new Map();
|
|
95
|
+
const textStyleOfEntry = (override) => override?.["styleIdForText"] === undefined ? nodeTextStyle : walk.textStyle?.(override["styleIdForText"], change);
|
|
96
|
+
// The line height in pixels each style id takes from a variable, for runs drawn at a bound font size (below).
|
|
97
|
+
const boundPixels = new Map();
|
|
98
|
+
const runIds = new Map();
|
|
99
|
+
// The font size and line height (a multiple of the size) each style id draws at, for list indents (`listIndentOf`).
|
|
100
|
+
const sizes = new Map();
|
|
101
|
+
let missingIds = 0;
|
|
102
|
+
const styleOf = (id) => {
|
|
103
|
+
let style = styles.get(id);
|
|
104
|
+
if (style === undefined) {
|
|
105
|
+
const override = table.get(id);
|
|
106
|
+
if (id !== 0 && override === undefined)
|
|
107
|
+
missingIds += 1;
|
|
108
|
+
const textStyle = textStyleOfEntry(override);
|
|
109
|
+
const fields = styleFields(filled, entryFilled(override), textStyle?.change);
|
|
110
|
+
const boundLineHeight = keptLineHeight ?? boundLineHeightOf(change, override, textStyle?.change, where, walk.variableNumber);
|
|
111
|
+
const said = [];
|
|
112
|
+
const own = runStyle(fields, lookups, (kind, reason) => said.push([kind, reason]), where, boundLineHeight);
|
|
113
|
+
sizes.set(id, { fontSize: own.fontSize ?? 12, lineHeight: own.lineHeight ?? AUTO_LINE_HEIGHT });
|
|
114
|
+
if (keptLineHeight !== undefined)
|
|
115
|
+
boundPixels.set(id, keptLineHeight);
|
|
116
|
+
else if (boundLineHeight !== undefined) {
|
|
117
|
+
boundPixels.set(id, boundLineHeight);
|
|
118
|
+
// Reported only where the variable's value replaces a different stored line height.
|
|
119
|
+
const stored = runStyle(fields, lookups, () => undefined, where).lineHeight * (own.fontSize ?? 12);
|
|
120
|
+
if (Math.abs(stored - boundLineHeight) > 1e-6)
|
|
121
|
+
said.push(["approximated", LINE_HEIGHT_VARIABLE_REASON]);
|
|
122
|
+
}
|
|
123
|
+
const bothAuto = textStyle !== undefined && boundLineHeight === undefined && autoLineHeight(fields) && autoLineHeight(textStyle.change);
|
|
124
|
+
style = textStyle?.shared === undefined ? { overrides: own } : linkedRun(textStyle.shared, own, bothAuto);
|
|
125
|
+
const rangeLink = override?.["hyperlink"];
|
|
126
|
+
const hyperlink = hyperlinkOf(rangeLink === undefined ? change["hyperlink"] : rangeLink, report);
|
|
127
|
+
if (hyperlink)
|
|
128
|
+
style.hyperlink = hyperlink;
|
|
129
|
+
const paints = fields["fillPaints"];
|
|
130
|
+
// A range with its own paints replaces the layer's style, even if the colours match.
|
|
131
|
+
const source = override && ("styleIdForFill" in override || "fillPaints" in override) ? override : change;
|
|
132
|
+
const reference = source["styleIdForFill"];
|
|
133
|
+
if (reference && walk.textFillStyle && !isDetachedStyle(reference)) {
|
|
134
|
+
const guid = walk.textFillStyle(reference);
|
|
135
|
+
if (guid)
|
|
136
|
+
Object.assign(style, walk.plainTextPaint?.(override) ? { filePlainFillStyle: guid } : { fileFillStyle: guid });
|
|
137
|
+
}
|
|
138
|
+
const paint = Array.isArray(paints) && lookups.colors.get(paints)?.paint;
|
|
139
|
+
if (paint && paint["colorVar"] !== undefined && walk.textColorVariable) {
|
|
140
|
+
const variable = walk.textColorVariable(paint["colorVar"]);
|
|
141
|
+
Object.assign(style, walk.plainTextPaint?.(override) ? { filePlainTextColor: variable } : { fileTextColor: variable });
|
|
142
|
+
const opacity = field(paint, "opacity", "number", where) ?? 1;
|
|
143
|
+
if (variable && opacity !== 1 && !walk.copiedPaintAlpha?.(paint, opacity))
|
|
144
|
+
Object.assign(style, { filePaintOpacity: opacity });
|
|
145
|
+
}
|
|
146
|
+
if (walk.fontSizeVariable) {
|
|
147
|
+
const data = runBinding(change, override, textStyle?.change, where, "FONT_SIZE", "fontSize");
|
|
148
|
+
if (data !== undefined) {
|
|
149
|
+
const variable = walk.fontSizeVariable(data);
|
|
150
|
+
// An omitted style binding is reported on the definition, once for all its users.
|
|
151
|
+
if (variable || !textStyle?.shared || data !== bindingIn(textStyle.change, "FONT_SIZE", where))
|
|
152
|
+
Object.assign(style, { fileFontSize: variable });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// A run that keeps its style's auto line height has the style's, which may come from another layer's font metadata.
|
|
156
|
+
const keepsStyleLineHeight = bothAuto && textStyle.shared !== undefined && style.overrides?.lineHeight === undefined;
|
|
157
|
+
for (const [kind, reason] of said)
|
|
158
|
+
if (!(keepsStyleLineHeight && reason === AUTO_LINE_HEIGHT_REASON))
|
|
159
|
+
report(kind, reason);
|
|
160
|
+
styles.set(id, style);
|
|
161
|
+
}
|
|
162
|
+
return style;
|
|
163
|
+
};
|
|
164
|
+
const runOf = (runText, id) => {
|
|
165
|
+
const run = { text: runText, ...styleOf(id) };
|
|
166
|
+
runIds.set(run, id);
|
|
167
|
+
return run;
|
|
168
|
+
};
|
|
169
|
+
const align = ALIGN[field(change, "textAlignHorizontal", "string", where) ?? "LEFT"];
|
|
170
|
+
const paragraphs = [];
|
|
171
|
+
let runs = [];
|
|
172
|
+
let text = "";
|
|
173
|
+
let runId = -1;
|
|
174
|
+
const endRun = () => {
|
|
175
|
+
if (text !== "")
|
|
176
|
+
runs.push(runOf(text, runId));
|
|
177
|
+
text = "";
|
|
178
|
+
};
|
|
179
|
+
let index = 0;
|
|
180
|
+
// `for…of` walks by code point, so an emoji made of two UTF-16 units takes one style id.
|
|
181
|
+
for (const character of characters) {
|
|
182
|
+
const raw = ids[index++];
|
|
183
|
+
const id = typeof raw === "number" ? raw : 0;
|
|
184
|
+
if (character === "\n") {
|
|
185
|
+
endRun();
|
|
186
|
+
paragraphs.push({ runs: runs.length > 0 ? runs : [runOf("", id)], ...(align ? { align } : {}) });
|
|
187
|
+
runs = [];
|
|
188
|
+
runId = -1;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (id !== runId)
|
|
192
|
+
endRun();
|
|
193
|
+
runId = id;
|
|
194
|
+
text += character;
|
|
195
|
+
}
|
|
196
|
+
endRun();
|
|
197
|
+
paragraphs.push({ runs: runs.length > 0 ? runs : [runOf("", typeof ids[index] === "number" ? ids[index] : 0)], ...(align ? { align } : {}) });
|
|
198
|
+
const lists = listsOf(textData, report, where);
|
|
199
|
+
const markerAdvances = bulletAdvancesOf(change, characters);
|
|
200
|
+
lists.forEach((list, line) => {
|
|
201
|
+
const paragraph = paragraphs[line];
|
|
202
|
+
if (list === undefined || paragraph === undefined)
|
|
203
|
+
return;
|
|
204
|
+
const id = runIds.get(paragraph.runs[0]);
|
|
205
|
+
const size = sizes.get(id ?? 0) ?? { fontSize: 12, lineHeight: AUTO_LINE_HEIGHT };
|
|
206
|
+
paragraphs[line] = { ...paragraph, ...listIndentOf(list, size, markerAdvances[line]) };
|
|
207
|
+
});
|
|
208
|
+
// Figma's paragraph spacing is the space between paragraphs, which is the text's `paragraphSpacing`, so a paragraph
|
|
209
|
+
// added later, such as by a text property with line breaks, is apart by it too. Two list items in a row are apart by
|
|
210
|
+
// the list spacing instead, which the later one keeps as its own space above where it differs. A linked text style's
|
|
211
|
+
// value beats the copy stored on the layer, as for the rest of its typography.
|
|
212
|
+
const spacing = spacingOf("paragraphSpacing", nodeTextStyle?.change, change, where);
|
|
213
|
+
const listSpacing = spacingOf("listSpacing", nodeTextStyle?.change, change, where);
|
|
214
|
+
for (let line = 1; line < paragraphs.length; line += 1) {
|
|
215
|
+
const inList = paragraphs[line].list !== undefined && paragraphs[line - 1].list !== undefined;
|
|
216
|
+
if (inList && listSpacing !== spacing)
|
|
217
|
+
paragraphs[line] = { ...paragraphs[line], spacingBefore: listSpacing };
|
|
218
|
+
}
|
|
219
|
+
// Lists are matched to paragraphs by position, so a count mismatch can drop or misplace them.
|
|
220
|
+
if (lists.length !== paragraphs.length && lists.some((list) => list !== undefined)) {
|
|
221
|
+
report("approximated", `the text has ${String(lists.length)} line entr${lists.length === 1 ? "y" : "ies"} for ${String(paragraphs.length)} paragraph${paragraphs.length === 1 ? "" : "s"}; list formatting is matched by position and may be on the wrong paragraphs or left out`);
|
|
222
|
+
}
|
|
223
|
+
if (missingIds > 0) {
|
|
224
|
+
report("approximated", `${String(missingIds)} style id${missingIds === 1 ? " is" : "s are"} not in the style override table; their characters use the node's style`);
|
|
225
|
+
}
|
|
226
|
+
reportLeftOut(change, textData, table, report, where, nodeTextStyle?.change);
|
|
227
|
+
const firstLineHeight = (paragraph) => {
|
|
228
|
+
const id = paragraph?.runs[0] === undefined ? undefined : runIds.get(paragraph.runs[0]);
|
|
229
|
+
const size = sizes.get(id ?? 0) ?? { fontSize: 12, lineHeight: AUTO_LINE_HEIGHT };
|
|
230
|
+
return size.fontSize * size.lineHeight;
|
|
231
|
+
};
|
|
232
|
+
const autoResize = AUTO_RESIZE[field(change, "textAutoResize", "string", where) ?? "NONE"];
|
|
233
|
+
if (autoResize === undefined)
|
|
234
|
+
report("approximated", `textAutoResize ${String(change["textAutoResize"])} is imported as a fixed size`);
|
|
235
|
+
// Last, after every call to `report`, so the count includes them all.
|
|
236
|
+
if (reasonsLeftOut > 0)
|
|
237
|
+
walk.report("approximated", `${String(reasonsLeftOut)} more report line${reasonsLeftOut === 1 ? " is" : "s are"} left out for this text node`);
|
|
238
|
+
const verticalAlign = VERTICAL_ALIGN[field(change, "textAlignVertical", "string", where) ?? "TOP"];
|
|
239
|
+
const textNode = {
|
|
240
|
+
...base,
|
|
241
|
+
type: "text",
|
|
242
|
+
paragraphs,
|
|
243
|
+
verticalAlign: verticalAlign ?? "top",
|
|
244
|
+
overflow: "visible",
|
|
245
|
+
autoResize: autoResize ?? "none",
|
|
246
|
+
wrap: autoResize === "width-and-height" ? "none" : "word",
|
|
247
|
+
...(spacing > 0 ? { paragraphSpacing: spacing } : {}),
|
|
248
|
+
...truncationOf(change, base.frame.height, autoResize ?? "none", firstLineHeight(paragraphs[0]), where),
|
|
249
|
+
};
|
|
250
|
+
// Taco draws every run at the layer's bound font size where it keeps that binding (`bindLayerVariables`), so a line
|
|
251
|
+
// height from a variable is a multiple of that size instead of the stored one, and the drawn line height is the variable's.
|
|
252
|
+
const drawn = boundPixels.size === 0 ? undefined : walk.keptFontSize?.(textNode);
|
|
253
|
+
if (drawn === undefined)
|
|
254
|
+
return textNode;
|
|
255
|
+
const atDrawnSize = (run) => {
|
|
256
|
+
const id = runIds.get(run);
|
|
257
|
+
const pixels = id === undefined ? undefined : boundPixels.get(id);
|
|
258
|
+
if (pixels === undefined)
|
|
259
|
+
return run;
|
|
260
|
+
const lineHeight = pixels / drawn;
|
|
261
|
+
const { lineHeight: _stored, ...rest } = run.overrides ?? {};
|
|
262
|
+
const shared = run.styleId === undefined ? undefined : textStyleOfEntry(table.get(id))?.shared;
|
|
263
|
+
const overrides = shared !== undefined && shared.style.lineHeight === lineHeight ? rest : { ...rest, lineHeight };
|
|
264
|
+
const { overrides: _overrides, ...plain } = run;
|
|
265
|
+
return Object.keys(overrides).length === 0 ? plain : { ...plain, overrides };
|
|
266
|
+
};
|
|
267
|
+
return { ...textNode, paragraphs: textNode.paragraphs.map((paragraph) => ({ ...paragraph, runs: paragraph.runs.map(atDrawnSize) })) };
|
|
268
|
+
};
|
|
269
|
+
/** Most distinct report reasons one text node adds; the rest are counted in one closing line. */
|
|
270
|
+
const MAX_REPORTS = 20;
|
|
271
|
+
/** The fields `runStyle` reads. */
|
|
272
|
+
const STYLE_FIELDS = ["fontName", "fontSize", "lineHeight", "letterSpacing", "textDecoration", "fontVariations", "fillPaints"];
|
|
273
|
+
const FEATURE_SWITCHES = {
|
|
274
|
+
fontVariantCommonLigatures: ["liga", "clig"],
|
|
275
|
+
fontVariantContextualLigatures: ["calt"],
|
|
276
|
+
fontVariantDiscretionaryLigatures: ["dlig"],
|
|
277
|
+
fontVariantHistoricalLigatures: ["hlig"],
|
|
278
|
+
fontVariantOrdinal: ["ordn"],
|
|
279
|
+
fontVariantSlashedZero: ["zero"],
|
|
280
|
+
};
|
|
281
|
+
const FEATURE_CHOICES = {
|
|
282
|
+
fontVariantNumericFigure: { LINING: ["lnum"], OLDSTYLE: ["onum"] },
|
|
283
|
+
fontVariantNumericSpacing: { PROPORTIONAL: ["pnum"], TABULAR: ["tnum"] },
|
|
284
|
+
fontVariantNumericFraction: { DIAGONAL: ["frac"], STACKED: ["afrc"] },
|
|
285
|
+
fontVariantCaps: { SMALL: ["smcp"], ALL_SMALL: ["smcp", "c2sc"], PETITE: ["pcap"], ALL_PETITE: ["pcap", "c2pc"], UNICASE: ["unic"], TITLING: ["titl"] },
|
|
286
|
+
fontVariantPosition: { SUB: ["subs"], SUPER: ["sups"] },
|
|
287
|
+
};
|
|
288
|
+
const FEATURE_VARIANTS = [...Object.keys(FEATURE_SWITCHES), ...Object.keys(FEATURE_CHOICES)];
|
|
289
|
+
/** Figma stores the deviations from its feature defaults in separate on and off lists. */
|
|
290
|
+
export const OPEN_TYPE_TOGGLES = ["toggledOnOTFeatures", "toggledOffOTFeatures"];
|
|
291
|
+
/** Figma fields that specify OpenType features on a layer, range or instance override. */
|
|
292
|
+
export const OPEN_TYPE_FIELDS = [...FEATURE_VARIANTS, ...OPEN_TYPE_TOGGLES];
|
|
293
|
+
/** Keeps omitted toggle lists inherited, except for tags explicitly toggled the other way. */
|
|
294
|
+
export function inheritOpenTypeToggles(base, own) {
|
|
295
|
+
const merged = {};
|
|
296
|
+
for (const [index, name] of OPEN_TYPE_TOGGLES.entries()) {
|
|
297
|
+
const value = own[name] ?? base?.[name];
|
|
298
|
+
if (value === undefined)
|
|
299
|
+
continue;
|
|
300
|
+
const opposite = own[OPEN_TYPE_TOGGLES[1 - index]];
|
|
301
|
+
if (own[name] === undefined && Array.isArray(value) && Array.isArray(opposite) && opposite.length > 0) {
|
|
302
|
+
const tags = new Set(opposite);
|
|
303
|
+
merged[name] = value.filter(tag => !tags.has(tag));
|
|
304
|
+
}
|
|
305
|
+
else
|
|
306
|
+
merged[name] = value;
|
|
307
|
+
}
|
|
308
|
+
return merged;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* The fields a run reads, in this order: an override entry's own, then for
|
|
312
|
+
* typography the Figma text style they name (`under`), then the node's. As in
|
|
313
|
+
* Figma, a linked style's typography beats the copies stored on the layer,
|
|
314
|
+
* which can be stale; the colour (`fillPaints`) is the layer's, and the style's
|
|
315
|
+
* only where the layer has none. A linked style that leaves out its decoration
|
|
316
|
+
* has none, so the layer's stale `textDecoration` is not read either (Figma
|
|
317
|
+
* draws no underline there). Only the fields a run reads are copied, so
|
|
318
|
+
* the work per style id does not grow with the number of other fields on the node.
|
|
319
|
+
*/
|
|
320
|
+
function styleFields(change, override, under) {
|
|
321
|
+
const style = {};
|
|
322
|
+
for (const name of STYLE_FIELDS) {
|
|
323
|
+
const value = override !== undefined && Object.hasOwn(override, name)
|
|
324
|
+
? override[name]
|
|
325
|
+
: name === "fillPaints" ? change[name] ?? under?.[name]
|
|
326
|
+
: name === "textDecoration" && under !== undefined ? under[name]
|
|
327
|
+
: under?.[name] ?? change[name];
|
|
328
|
+
if (value !== undefined)
|
|
329
|
+
style[name] = value;
|
|
330
|
+
}
|
|
331
|
+
for (const name of FEATURE_VARIANTS) {
|
|
332
|
+
const value = override?.[name] ?? change[name] ?? under?.[name];
|
|
333
|
+
if (value !== undefined)
|
|
334
|
+
style[name] = value;
|
|
335
|
+
}
|
|
336
|
+
// A layer can set its own features alongside a shared typography style. An explicitly empty list clears that side.
|
|
337
|
+
const toggles = inheritOpenTypeToggles(under, change);
|
|
338
|
+
return { ...style, ...(override ? inheritOpenTypeToggles(toggles, override) : toggles) };
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The variable data a run's `variableField` (`LINE_HEIGHT` or `FONT_SIZE`) is bound to, read where `styleFields` reads
|
|
342
|
+
* the field it binds (`name`): an override entry's own binding, or none when the entry sets its own value; else the text
|
|
343
|
+
* style's (`under`), else the node's.
|
|
344
|
+
*/
|
|
345
|
+
function runBinding(change, override, under, where, variableField, name) {
|
|
346
|
+
const bound = (fields) => bindingIn(fields, variableField, where);
|
|
347
|
+
if (override !== undefined) {
|
|
348
|
+
const own = bound(override);
|
|
349
|
+
if (own !== undefined || Object.hasOwn(override, name))
|
|
350
|
+
return own;
|
|
351
|
+
}
|
|
352
|
+
return under !== undefined && under[name] !== undefined ? bound(under) : bound(change);
|
|
353
|
+
}
|
|
354
|
+
/** The variable data Figma fields bind `variableField` to, or `undefined`. */
|
|
355
|
+
function bindingIn(fields, variableField, where) {
|
|
356
|
+
const map = field(fields, "variableConsumptionMap", "object", where);
|
|
357
|
+
const entry = (map && field(map, "entries", "list", `${where} variableConsumptionMap`) || [])
|
|
358
|
+
.find((item) => isObject(item) && item["variableField"] === variableField);
|
|
359
|
+
return isObject(entry) ? entry["variableData"] : undefined;
|
|
360
|
+
}
|
|
361
|
+
/** A run's line height in pixels from the variable it is bound to (`runBinding`), or `undefined` without one. */
|
|
362
|
+
function boundLineHeightOf(change, override, under, where, variableNumber) {
|
|
363
|
+
if (variableNumber === undefined)
|
|
364
|
+
return undefined;
|
|
365
|
+
const lineData = runBinding(change, override, under, where, "LINE_HEIGHT", "lineHeight");
|
|
366
|
+
return lineData === undefined ? undefined : variableNumber(lineData);
|
|
367
|
+
}
|
|
368
|
+
/** An empty range hyperlink explicitly clears the layer's hyperlink. */
|
|
369
|
+
export function hyperlinkOf(value, report) {
|
|
370
|
+
if (value === undefined || (isObject(value) && Object.keys(value).length === 0))
|
|
371
|
+
return undefined;
|
|
372
|
+
if (isObject(value)) {
|
|
373
|
+
if (typeof value["url"] === "string" && value["url"] !== "")
|
|
374
|
+
return { kind: "url", url: value["url"] };
|
|
375
|
+
const guid = value["guid"];
|
|
376
|
+
if (isObject(guid) && Number.isInteger(guid["sessionID"]) && Number.isInteger(guid["localID"]))
|
|
377
|
+
return { kind: "target", guid: guidText(guid, "hyperlink") };
|
|
378
|
+
}
|
|
379
|
+
report("property-left-out", "invalid text hyperlink");
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
/** The fields a style has that turn off when a run leaves them out, with the value that turns each off. */
|
|
383
|
+
const STYLE_OFF = [["italic", false], ["underline", false], ["strikethrough", false], ["letterSpacing", 0], ["fontVariations", {}], ["openTypeFeatures", {}]];
|
|
384
|
+
/**
|
|
385
|
+
* A run linked to its shared style, with the fields of `own` that differ from
|
|
386
|
+
* the style's as its overrides. With `bothAuto` (the run's line height and the
|
|
387
|
+
* style's are Figma's "Auto") and the style's font, the run keeps the style's
|
|
388
|
+
* line height, so editing the style's line height reaches it.
|
|
389
|
+
*/
|
|
390
|
+
function linkedRun(shared, own, bothAuto) {
|
|
391
|
+
const style = shared.style;
|
|
392
|
+
const overrides = {};
|
|
393
|
+
for (const [name, value] of Object.entries(own))
|
|
394
|
+
if (JSON.stringify(value) !== JSON.stringify(style[name]))
|
|
395
|
+
overrides[name] = value;
|
|
396
|
+
for (const [name, off] of STYLE_OFF) {
|
|
397
|
+
if (own[name] === undefined && style[name] !== undefined && JSON.stringify(style[name]) !== JSON.stringify(off))
|
|
398
|
+
overrides[name] = off;
|
|
399
|
+
}
|
|
400
|
+
if (bothAuto && !("fontFamily" in overrides || "fontWeight" in overrides || "italic" in overrides))
|
|
401
|
+
delete overrides["lineHeight"];
|
|
402
|
+
return Object.keys(overrides).length === 0 ? { styleId: shared.id } : { styleId: shared.id, overrides: overrides };
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* A Figma text style node (`styleType` TEXT) as a Taco text style, read as a
|
|
406
|
+
* run of a node without overrides is; a value it lacks is Figma's default.
|
|
407
|
+
* An auto line height is the font's own from the style node's font metadata,
|
|
408
|
+
* else from the first of `layers`, the text layers that name the style, that has it.
|
|
409
|
+
* A line height bound to a variable is the number `variableNumber` gives for it; the
|
|
410
|
+
* layers that use the style report it.
|
|
411
|
+
*/
|
|
412
|
+
export function figmaTextStyle(change, name, report, where, layers = [], variableNumber) {
|
|
413
|
+
// A Taco text style binds no font size, so its line height is a multiple of the stored size.
|
|
414
|
+
const boundLineHeight = boundLineHeightOf(change, undefined, undefined, where, variableNumber);
|
|
415
|
+
// `runStyle` names no font asset.
|
|
416
|
+
const { fontAssetId: _fontAssetId, ...own } = runStyle(styleFields(change, undefined), runLookups(change, where, layers), report, where, boundLineHeight);
|
|
417
|
+
return {
|
|
418
|
+
...own,
|
|
419
|
+
name: name.trim() === "" ? "Text style" : name,
|
|
420
|
+
fontFamily: own.fontFamily ?? "Inter",
|
|
421
|
+
fontSize: own.fontSize ?? 12,
|
|
422
|
+
fontWeight: own.fontWeight ?? 400,
|
|
423
|
+
lineHeight: own.lineHeight ?? AUTO_LINE_HEIGHT,
|
|
424
|
+
color: own.color ?? "#000000",
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Fresh run lookups with the font metadata of the node's `derivedTextData`,
|
|
429
|
+
* then each of `more`'s, first entry for each family and style.
|
|
430
|
+
*/
|
|
431
|
+
function runLookups(change, where, more = []) {
|
|
432
|
+
const lookups = { fontMeta: new Map(), variations: new Map(), colors: new Map() };
|
|
433
|
+
const metadata = (node) => field(field(node, "derivedTextData", "object", where) ?? {}, "fontMetaData", "list", where) ?? [];
|
|
434
|
+
for (const entry of [change, ...more].flatMap(metadata)) {
|
|
435
|
+
const key = isObject(entry) && isObject(entry["key"]) ? entry["key"] : undefined;
|
|
436
|
+
if (key === undefined || typeof key["family"] !== "string" || typeof key["style"] !== "string")
|
|
437
|
+
continue;
|
|
438
|
+
const name = fontKey(key["family"], key["style"]);
|
|
439
|
+
if (!lookups.fontMeta.has(name))
|
|
440
|
+
lookups.fontMeta.set(name, entry);
|
|
441
|
+
}
|
|
442
|
+
return lookups;
|
|
443
|
+
}
|
|
444
|
+
const fontKey = (family, style) => `${family}\u0000${style}`;
|
|
445
|
+
/** Whether a style's `lineHeight` is Figma's "Auto", the font's own, as `runStyle` reads it. */
|
|
446
|
+
function autoLineHeight(style) {
|
|
447
|
+
const lineHeight = style["lineHeight"];
|
|
448
|
+
return !isObject(lineHeight) || typeof lineHeight["value"] !== "number" || (lineHeight["units"] === "PERCENT" && lineHeight["value"] === 100);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* The style of one run from the node's fields with an override entry's fields on top. `boundLineHeight`, in pixels,
|
|
452
|
+
* replaces the stored line height: the value of the variable it is bound to, as a multiple of the stored font size.
|
|
453
|
+
*/
|
|
454
|
+
function runStyle(style, lookups, report, where, boundLineHeight) {
|
|
455
|
+
const fontName = field(style, "fontName", "object", where) ?? {};
|
|
456
|
+
const family = field(fontName, "family", "string", `${where} fontName`);
|
|
457
|
+
const styleName = field(fontName, "style", "string", `${where} fontName`) ?? "";
|
|
458
|
+
const fontSize = field(style, "fontSize", "number", where);
|
|
459
|
+
const { fontWeight: weight, italic } = fontStyleWeight(styleName);
|
|
460
|
+
if (weight === undefined)
|
|
461
|
+
report("approximated", `font style "${styleName}" has no known weight; it is imported as weight 400`);
|
|
462
|
+
const fontWeight = weight ?? 400;
|
|
463
|
+
const size = fontSize ?? 12;
|
|
464
|
+
const out = {
|
|
465
|
+
...(family === undefined ? {} : { fontFamily: family }),
|
|
466
|
+
...(styleName && { fontStyle: styleName }),
|
|
467
|
+
...(fontSize === undefined ? {} : { fontSize }),
|
|
468
|
+
fontWeight,
|
|
469
|
+
...(italic ? { italic } : {}),
|
|
470
|
+
};
|
|
471
|
+
const lineHeight = field(style, "lineHeight", "object", where);
|
|
472
|
+
const lineValue = lineHeight && field(lineHeight, "value", "number", `${where} lineHeight`);
|
|
473
|
+
const lineUnits = lineHeight && field(lineHeight, "units", "string", `${where} lineHeight`);
|
|
474
|
+
if (boundLineHeight !== undefined)
|
|
475
|
+
out.lineHeight = boundLineHeight / size;
|
|
476
|
+
else if (lineValue === undefined || (lineUnits === "PERCENT" && lineValue === 100)) {
|
|
477
|
+
// Figma's "Auto" line height is the font's own, which the message may carry.
|
|
478
|
+
const meta = family === undefined ? undefined : lookups.fontMeta.get(fontKey(family, styleName));
|
|
479
|
+
const natural = meta && field(meta, "fontLineHeight", "number", `${where} fontMetaData`);
|
|
480
|
+
if (natural === undefined)
|
|
481
|
+
report("approximated", AUTO_LINE_HEIGHT_REASON);
|
|
482
|
+
out.lineHeight = natural ?? AUTO_LINE_HEIGHT;
|
|
483
|
+
}
|
|
484
|
+
else if (lineUnits === "PIXELS")
|
|
485
|
+
out.lineHeight = lineValue / size;
|
|
486
|
+
else if (lineUnits === "PERCENT")
|
|
487
|
+
out.lineHeight = lineValue / 100;
|
|
488
|
+
else
|
|
489
|
+
out.lineHeight = lineValue;
|
|
490
|
+
const spacing = field(style, "letterSpacing", "object", where);
|
|
491
|
+
const spacingValue = spacing && field(spacing, "value", "number", `${where} letterSpacing`);
|
|
492
|
+
if (spacing && spacingValue !== undefined && spacingValue !== 0) {
|
|
493
|
+
out.letterSpacing = field(spacing, "units", "string", `${where} letterSpacing`) === "PIXELS" ? spacingValue : size * spacingValue / 100;
|
|
494
|
+
}
|
|
495
|
+
const decoration = field(style, "textDecoration", "string", where);
|
|
496
|
+
if (decoration === "UNDERLINE")
|
|
497
|
+
out.underline = true;
|
|
498
|
+
if (decoration === "STRIKETHROUGH")
|
|
499
|
+
out.strikethrough = true;
|
|
500
|
+
const features = openTypeFeatures(style, report, where);
|
|
501
|
+
if (features !== undefined)
|
|
502
|
+
out.openTypeFeatures = features;
|
|
503
|
+
const axes = field(style, "fontVariations", "list", where) ?? [];
|
|
504
|
+
let variations = lookups.variations.get(axes);
|
|
505
|
+
if (variations === undefined) {
|
|
506
|
+
variations = {};
|
|
507
|
+
for (const axis of axes) {
|
|
508
|
+
const tag = isObject(axis) ? axis["axisTag"] : undefined;
|
|
509
|
+
const value = isObject(axis) ? axis["value"] : undefined;
|
|
510
|
+
// `axisTag` packs the four tag letters into a number, first letter in the high byte.
|
|
511
|
+
if (typeof tag === "number" && typeof value === "number")
|
|
512
|
+
variations[String.fromCharCode(tag >>> 24 & 255, tag >>> 16 & 255, tag >>> 8 & 255, tag & 255)] = value;
|
|
513
|
+
}
|
|
514
|
+
lookups.variations.set(axes, variations);
|
|
515
|
+
}
|
|
516
|
+
if (Object.keys(variations).length > 0)
|
|
517
|
+
out.fontVariations = variations;
|
|
518
|
+
const paints = field(style, "fillPaints", "list", where) ?? [];
|
|
519
|
+
let color = lookups.colors.get(paints);
|
|
520
|
+
if (color === undefined) {
|
|
521
|
+
color = textColor(paints, report, where);
|
|
522
|
+
lookups.colors.set(paints, color);
|
|
523
|
+
}
|
|
524
|
+
out.color = color.value;
|
|
525
|
+
return out;
|
|
526
|
+
}
|
|
527
|
+
/** Converts Figma's typography switches and then its explicit feature toggles, which take precedence. */
|
|
528
|
+
function openTypeFeatures(style, report, where) {
|
|
529
|
+
const features = new Map();
|
|
530
|
+
for (const [name, tags] of Object.entries(FEATURE_SWITCHES)) {
|
|
531
|
+
const value = field(style, name, "boolean", where);
|
|
532
|
+
if (value !== undefined)
|
|
533
|
+
for (const tag of tags)
|
|
534
|
+
features.set(tag, value);
|
|
535
|
+
}
|
|
536
|
+
for (const [name, choices] of Object.entries(FEATURE_CHOICES)) {
|
|
537
|
+
const choice = field(style, name, "string", where);
|
|
538
|
+
if (choice !== undefined && Object.hasOwn(choices, choice))
|
|
539
|
+
for (const tag of choices[choice])
|
|
540
|
+
features.set(tag, true);
|
|
541
|
+
}
|
|
542
|
+
for (const name of OPEN_TYPE_TOGGLES) {
|
|
543
|
+
for (const tag of field(style, name, "list", where) ?? []) {
|
|
544
|
+
if (typeof tag === "string" && tag.length === 4 && /^[ -~]+$/.test(tag))
|
|
545
|
+
features.set(tag.toLowerCase(), name === "toggledOnOTFeatures");
|
|
546
|
+
else
|
|
547
|
+
report("property-left-out", "an invalid OpenType feature tag is left out");
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if (features.size > 256)
|
|
551
|
+
report("property-left-out", "OpenType feature settings beyond the first 256 tags are left out");
|
|
552
|
+
if (features.size > 0)
|
|
553
|
+
return Object.fromEntries([...features.keys()].sort().slice(0, 256).map(tag => [tag, features.get(tag)]));
|
|
554
|
+
}
|
|
555
|
+
/** The top visible solid fill as a colour with the paint's opacity in its alpha; other fills are reported. */
|
|
556
|
+
export function textColor(paints, report = () => undefined, where = "text fill") {
|
|
557
|
+
let color;
|
|
558
|
+
let shown;
|
|
559
|
+
for (let i = paints.length - 1; i >= 0; i--) {
|
|
560
|
+
const paint = paints[i];
|
|
561
|
+
if (!isObject(paint) || field(paint, "visible", "boolean", where) === false)
|
|
562
|
+
continue;
|
|
563
|
+
const type = field(paint, "type", "string", where) ?? "no type";
|
|
564
|
+
const channels = type === "SOLID" ? readNumbers(field(paint, "color", "object", where) ?? {}, ["r", "g", "b", "a"], `${where} fill colour`) : null;
|
|
565
|
+
if (color !== undefined || channels === null) {
|
|
566
|
+
report("property-left-out", `a ${type} text fill is left out: only the top solid fill is imported`);
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
shown = paint;
|
|
570
|
+
color = hexColor({ ...channels, a: channels.a * (field(paint, "opacity", "number", where) ?? 1) });
|
|
571
|
+
const blend = field(paint, "blendMode", "string", where);
|
|
572
|
+
if (blend !== undefined && blend !== "NORMAL" && blend !== "PASS_THROUGH")
|
|
573
|
+
report("property-left-out", `the text fill's blend mode ${blend} is left out; its colour is kept`);
|
|
574
|
+
}
|
|
575
|
+
// No visible fill draws no glyphs; the text keeps its layout.
|
|
576
|
+
return { value: color ?? "#00000000", paint: shown };
|
|
577
|
+
}
|
|
578
|
+
/** A spacing in pixels from the Figma text style (`style`), else the node, else 0; negative values are 0. */
|
|
579
|
+
/**
|
|
580
|
+
* The paragraph or list spacing of a text: its linked text style's where it has one, else the layer's. A linked style
|
|
581
|
+
* that leaves the spacing out has none, so the stale copy on the layer is not read (Figma draws no space then).
|
|
582
|
+
*/
|
|
583
|
+
function spacingOf(name, style, change, where) {
|
|
584
|
+
const value = (style === undefined ? field(change, name, "number", where) : field(style, name, "number", `${where} text style`)) ?? 0;
|
|
585
|
+
return Number.isFinite(value) && value > 0 ? value : 0;
|
|
586
|
+
}
|
|
587
|
+
/** Each step of list nesting in Figma, as a multiple of the font size: an item at indentation level n starts its text n steps in. */
|
|
588
|
+
const LIST_INDENT_STEP = 1.5;
|
|
589
|
+
/** The width of Figma's bullet as a multiple of the font size (Inter's "•"), used when the message has no laid-out marker. */
|
|
590
|
+
const BULLET_ADVANCE = 0.5625;
|
|
591
|
+
/**
|
|
592
|
+
* The indent and marker gap that put a list item's text and marker where Figma does. Figma starts the text of an item at
|
|
593
|
+
* indentation level n (Taco level n - 1) at n × 1.5 × the font size and centres a bullet in the 1.5 × font size step
|
|
594
|
+
* before it. Taco adds level × line height for nesting (`resolveTextListLayout`) and draws the marker `gap` before the
|
|
595
|
+
* text, so the indent takes the rest of Figma's offset and the gap puts the bullet's centre half a step before the
|
|
596
|
+
* text. `advance` is the bullet's width as laid out by Figma, when the message has it.
|
|
597
|
+
*/
|
|
598
|
+
function listIndentOf(list, size, advance) {
|
|
599
|
+
const step = LIST_INDENT_STEP * size.fontSize;
|
|
600
|
+
const left = Math.max(0, (list.level + 1) * step - list.level * size.fontSize * size.lineHeight);
|
|
601
|
+
const indent = { left, right: 0, firstLine: -Math.min(step, left) };
|
|
602
|
+
if (list.kind !== "unordered")
|
|
603
|
+
return { list, indent };
|
|
604
|
+
const gap = Math.max(0, (step - (advance ?? BULLET_ADVANCE * size.fontSize)) / 2);
|
|
605
|
+
return { list: { ...list, marker: { ...list.marker, gap } }, indent };
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* The width in pixels of the bullet Figma laid out before each paragraph, from `derivedTextData`: a marker is a glyph with
|
|
609
|
+
* no `firstCharacter` on the baseline of its paragraph's first line. Paragraphs without one are left out.
|
|
610
|
+
*/
|
|
611
|
+
function bulletAdvancesOf(change, characters) {
|
|
612
|
+
const derived = change["derivedTextData"];
|
|
613
|
+
if (!isObject(derived) || !Array.isArray(derived["baselines"]) || !Array.isArray(derived["glyphs"]))
|
|
614
|
+
return [];
|
|
615
|
+
const markers = derived["glyphs"].filter((glyph) => isObject(glyph) && glyph["firstCharacter"] === undefined);
|
|
616
|
+
const lineYs = new Map();
|
|
617
|
+
for (const baseline of derived["baselines"]) {
|
|
618
|
+
if (!isObject(baseline) || typeof baseline["firstCharacter"] !== "number" || !isObject(baseline["position"]))
|
|
619
|
+
continue;
|
|
620
|
+
const y = baseline["position"]["y"];
|
|
621
|
+
if (typeof y === "number")
|
|
622
|
+
lineYs.set(baseline["firstCharacter"], y);
|
|
623
|
+
}
|
|
624
|
+
const advances = [];
|
|
625
|
+
let start = 0;
|
|
626
|
+
for (const paragraph of characters.split("\n")) {
|
|
627
|
+
const y = lineYs.get(start);
|
|
628
|
+
const marker = y === undefined ? undefined : markers.find((glyph) => isObject(glyph["position"]) && glyph["position"]["y"] === y);
|
|
629
|
+
const advance = marker?.["advance"];
|
|
630
|
+
const fontSize = marker?.["fontSize"];
|
|
631
|
+
advances.push(typeof advance === "number" && typeof fontSize === "number" && Number.isFinite(advance * fontSize) ? advance * fontSize : undefined);
|
|
632
|
+
start += [...paragraph].length + 1;
|
|
633
|
+
}
|
|
634
|
+
return advances;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Figma's ending truncation as `truncate` and `maxLines`. Figma reads `maxLines` only while truncation is on. A
|
|
638
|
+
* fixed-size box also ends at its last whole line, so its line count is its height over the first line's height (at
|
|
639
|
+
* least one line), or `maxLines` where that is fewer. The first line's height is Figma's own where the message lays the
|
|
640
|
+
* text out, else the first run's.
|
|
641
|
+
*/
|
|
642
|
+
function truncationOf(change, height, autoResize, firstLineHeight, where) {
|
|
643
|
+
if (field(change, "textTruncation", "string", where) !== "ENDING")
|
|
644
|
+
return {};
|
|
645
|
+
const stored = Math.floor(field(change, "maxLines", "number", where) ?? 0);
|
|
646
|
+
const baseline = isObject(change["derivedTextData"]) && Array.isArray(change["derivedTextData"]["baselines"]) ? change["derivedTextData"]["baselines"][0] : undefined;
|
|
647
|
+
const derived = isObject(baseline) && typeof baseline["lineHeight"] === "number" ? baseline["lineHeight"] : undefined;
|
|
648
|
+
const lineHeight = derived !== undefined && derived > 0 ? derived : firstLineHeight;
|
|
649
|
+
const fitting = autoResize === "none" && Number.isFinite(height / lineHeight) && lineHeight > 0 ? Math.max(1, Math.floor((height + 0.01) / lineHeight)) : undefined;
|
|
650
|
+
const maxLines = stored > 0 ? Math.min(stored, fitting ?? stored) : fitting;
|
|
651
|
+
return { ...(maxLines !== undefined ? { maxLines } : {}), truncate: "ellipsis" };
|
|
652
|
+
}
|
|
653
|
+
/** Reports the text fields Taco does not import yet, one entry for each field. */
|
|
654
|
+
function reportLeftOut(change, textData, table, report, where, textStyle) {
|
|
655
|
+
// The text style's case applies where the node has none of its own.
|
|
656
|
+
const styles = [change, ...table.values(), ...(textStyle !== undefined && change["textCase"] === undefined ? [textStyle] : [])];
|
|
657
|
+
const textCase = styles.map((style) => field(style, "textCase", "string", where)).find((value) => value !== undefined && value !== "ORIGINAL");
|
|
658
|
+
if (textCase !== undefined)
|
|
659
|
+
report("property-left-out", `text case ${textCase} is not imported; the characters are kept as typed`);
|
|
660
|
+
const truncation = field(change, "textTruncation", "string", where);
|
|
661
|
+
if (truncation !== undefined && truncation !== "DISABLED" && truncation !== "ENDING")
|
|
662
|
+
report("property-left-out", `text truncation ${truncation} is not imported`);
|
|
663
|
+
}
|
|
664
|
+
const LIST_KIND = { ORDERED_LIST: "ordered", UNORDERED_LIST: "unordered" };
|
|
665
|
+
/**
|
|
666
|
+
* The list of each paragraph, from `textData.lines` (one entry per paragraph).
|
|
667
|
+
* Figma's `indentationLevel` is 1 for a top-level list item, so Taco's level is
|
|
668
|
+
* one less. Taco draws an ordered item's number as its `start`, or as its
|
|
669
|
+
* paragraph index plus one when `start` is left out (see `resolveTextListLayout` in
|
|
670
|
+
* packages/model/src/text-measurement.ts), so `start` is each item's own number
|
|
671
|
+
* and not only the first number of a run. Items are numbered
|
|
672
|
+
* here the way Figma numbers them: a count for each level that starts at
|
|
673
|
+
* `listStartOffset` + 1 and restarts at a new list, after a plain line, after
|
|
674
|
+
* a shallower item, or when the kind at that level changes. `start` is set
|
|
675
|
+
* only where that number differs from the paragraph index plus one.
|
|
676
|
+
*/
|
|
677
|
+
function listsOf(textData, report, where) {
|
|
678
|
+
const lines = field(textData, "lines", "list", `${where} textData`) ?? [];
|
|
679
|
+
const counts = [];
|
|
680
|
+
const kinds = [];
|
|
681
|
+
return lines.map((line, index) => {
|
|
682
|
+
const type = isObject(line) ? line["lineType"] : undefined;
|
|
683
|
+
const kind = typeof type === "string" ? LIST_KIND[type] : undefined;
|
|
684
|
+
if (kind === undefined) {
|
|
685
|
+
if (type !== undefined && type !== "PLAIN")
|
|
686
|
+
report("property-left-out", `line type ${String(type)} is not imported; the line is a plain paragraph`);
|
|
687
|
+
counts.length = 0;
|
|
688
|
+
kinds.length = 0;
|
|
689
|
+
return undefined;
|
|
690
|
+
}
|
|
691
|
+
const object = line;
|
|
692
|
+
const indentation = field(object, "indentationLevel", "number", `${where} textData line`) ?? 1;
|
|
693
|
+
const level = Number.isInteger(indentation) && indentation > 1 ? indentation - 1 : 0;
|
|
694
|
+
counts.length = Math.min(counts.length, level + 1);
|
|
695
|
+
kinds.length = Math.min(kinds.length, level + 1);
|
|
696
|
+
const restart = object["isFirstLineOfList"] === true || kinds[level] !== kind || counts[level] === undefined;
|
|
697
|
+
kinds[level] = kind;
|
|
698
|
+
if (kind === "unordered") {
|
|
699
|
+
counts[level] = undefined;
|
|
700
|
+
return { kind, level };
|
|
701
|
+
}
|
|
702
|
+
const offset = field(object, "listStartOffset", "number", `${where} textData line`) ?? 0;
|
|
703
|
+
const number = restart ? 1 + (Number.isInteger(offset) && offset > 0 ? offset : 0) : (counts[level] ?? 0) + 1;
|
|
704
|
+
counts[level] = number;
|
|
705
|
+
return number === index + 1 ? { kind, level } : { kind, level, start: number };
|
|
706
|
+
});
|
|
707
|
+
}
|