@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
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Importer } from '@moondraft/editor/core';
|
|
2
|
+
import { type SvgParse } from './taco/svg-import/public-types.ts';
|
|
3
|
+
import type { OpenFigmaOptions } from './figma-file.ts';
|
|
4
|
+
export type { ImportDocument, ImportEnv, ImportInput, ImportPayload, ImportProgress, ImportRead, ImportReportEntry, Importer } from '@moondraft/editor/core';
|
|
5
|
+
export { openFigmaFile, type OpenFigmaOptions, type WorkerLike } from './figma-file.ts';
|
|
6
|
+
/** Options for registering an SVG importer. */
|
|
7
|
+
export interface SvgImporterOptions {
|
|
8
|
+
/** Parses markup into an element tree (default: the browser's DOMParser). */
|
|
9
|
+
parseSvg?: SvgParse;
|
|
10
|
+
}
|
|
11
|
+
/** SVG files (image/svg+xml, or a .svg name) and SVG markup pasted as text, as editable layers. A file over the
|
|
12
|
+
* importer's byte cap (DEFAULT_SVG_IMPORT_LIMITS.inputBytes) is refused before it is read.
|
|
13
|
+
* @param options - Parser configuration.
|
|
14
|
+
* @returns An importer that produces editable layer payloads.
|
|
15
|
+
*/
|
|
16
|
+
export declare function svgImporter(options?: SvgImporterOptions): Importer;
|
|
17
|
+
/** Options for registering a Figma importer. */
|
|
18
|
+
export interface FigmaImporterOptions {
|
|
19
|
+
/** Factory for a dedicated worker. Defaults to a module worker when available. Pass null to read on this thread. */
|
|
20
|
+
worker?: OpenFigmaOptions['worker'];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Imports Figma clipboard HTML as editable layers and .fig files as full documents.
|
|
24
|
+
* @param options - Worker configuration for file imports.
|
|
25
|
+
* @returns An importer for Figma files and clipboard content.
|
|
26
|
+
*/
|
|
27
|
+
export declare function figmaImporter(options?: FigmaImporterOptions): Importer;
|
|
28
|
+
export { payloadFromSyntax } from './syntax.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DEFAULT_SVG_IMPORT_LIMITS } from './taco/svg-import/public-types.js';
|
|
2
|
+
import { checkAbort } from './figma-file.js';
|
|
3
|
+
export { openFigmaFile } from './figma-file.js';
|
|
4
|
+
const SVG_TEXT = /^\s*(?:<\?xml[^>]*>\s*)?(?:<!--[\s\S]*?-->\s*)*(?:<!doctype[^>]*>\s*)?<svg[\s>]/i;
|
|
5
|
+
const nameOf = (file) => file?.name ?? '';
|
|
6
|
+
const bytesOf = async (file) => new Uint8Array(await file.arrayBuffer());
|
|
7
|
+
const mb = (n) => `${Math.round(n / 1024 / 1024)} MB`;
|
|
8
|
+
/** SVG files (image/svg+xml, or a .svg name) and SVG markup pasted as text, as editable layers. A file over the
|
|
9
|
+
* importer's byte cap (DEFAULT_SVG_IMPORT_LIMITS.inputBytes) is refused before it is read.
|
|
10
|
+
* @param options - Parser configuration.
|
|
11
|
+
* @returns An importer that produces editable layer payloads.
|
|
12
|
+
*/
|
|
13
|
+
export function svgImporter(options = {}) {
|
|
14
|
+
const cap = DEFAULT_SVG_IMPORT_LIMITS.inputBytes;
|
|
15
|
+
return {
|
|
16
|
+
name: 'SVG',
|
|
17
|
+
accepts: ({ file, html, text }) => (file ? /svg/i.test(file.type) || /\.svg$/i.test(nameOf(file)) : !html?.trim() && !!text && SVG_TEXT.test(text)),
|
|
18
|
+
async read(input, env) {
|
|
19
|
+
const file = input.file;
|
|
20
|
+
if (file && file.size > cap)
|
|
21
|
+
throw new Error(`${nameOf(file) || 'the file'} is ${mb(file.size)}, over the ${mb(cap)} an SVG may have`);
|
|
22
|
+
const { readSvg } = await import('./svg.js'), markup = file ? await file.text() : input.text ?? '';
|
|
23
|
+
checkAbort(env.signal);
|
|
24
|
+
const name = nameOf(file).replace(/\.svg$/i, '');
|
|
25
|
+
return readSvg(markup, { fonts: env.fonts, ...(name ? { name } : {}), ...(options.parseSvg ? { parseSvg: options.parseSvg } : {}) });
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Imports Figma clipboard HTML as editable layers and .fig files as full documents.
|
|
31
|
+
* @param options - Worker configuration for file imports.
|
|
32
|
+
* @returns An importer for Figma files and clipboard content.
|
|
33
|
+
*/
|
|
34
|
+
export function figmaImporter(options = {}) {
|
|
35
|
+
const holds = (html) => !!html && html.includes('(figmeta)') && html.includes('(figma)');
|
|
36
|
+
return {
|
|
37
|
+
name: 'Figma',
|
|
38
|
+
accepts: ({ file, html }) => (file ? /\.fig$/i.test(nameOf(file)) : holds(html)),
|
|
39
|
+
async read(input, env) {
|
|
40
|
+
if (!input.file)
|
|
41
|
+
return (await import('./figma.js')).readFigmaClipboard(input.html ?? '');
|
|
42
|
+
const { openFigmaFile } = await import('./figma-file.js');
|
|
43
|
+
return openFigmaFile(await bytesOf(input.file), {
|
|
44
|
+
signal: env.signal, progress: env.progress, ...(options.worker !== undefined ? { worker: options.worker } : {}),
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export { payloadFromSyntax } from './syntax.js';
|
package/dist/report.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ImportReportEntry } from '@moondraft/editor/core';
|
|
2
|
+
import type { DocumentJSON } from '@moondraft/editor/schema';
|
|
3
|
+
import type { ConvertReport } from './adapter/convert.ts';
|
|
4
|
+
/** Groups entries by kind and description, summing counts and keeping unique layer IDs. */
|
|
5
|
+
export declare function grouped(entries: Iterable<ImportReportEntry>): ImportReportEntry[];
|
|
6
|
+
/** Builds a lookup from reader IDs to their converted layers. */
|
|
7
|
+
export declare function layersBySource(document: DocumentJSON): Map<string, string[]>;
|
|
8
|
+
/** Turns adapter diagnostics into import reports. */
|
|
9
|
+
export declare function convertEntries(report: ConvertReport, details?: boolean): ImportReportEntry[];
|
|
10
|
+
/**
|
|
11
|
+
* Collects each distinct face used by a text layer or an instance override.
|
|
12
|
+
* @param document - Converted document with its layer and override IDs.
|
|
13
|
+
* @returns One occurrence per face and layer, ready to group with other report entries.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fontEntries(document: DocumentJSON): ImportReportEntry[];
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Groups entries by kind and description, summing counts and keeping unique layer IDs. */
|
|
2
|
+
export function grouped(entries) {
|
|
3
|
+
const out = new Map();
|
|
4
|
+
for (const e of entries) {
|
|
5
|
+
const key = `${e.kind}\u0000${e.what}`, had = out.get(key);
|
|
6
|
+
if (had) {
|
|
7
|
+
had.count += e.count;
|
|
8
|
+
if (e.ids)
|
|
9
|
+
(had.ids ??= []).push(...e.ids);
|
|
10
|
+
}
|
|
11
|
+
else
|
|
12
|
+
out.set(key, { ...e, ...(e.ids && { ids: [...e.ids] }) });
|
|
13
|
+
}
|
|
14
|
+
for (const e of out.values())
|
|
15
|
+
if (e.ids)
|
|
16
|
+
e.ids = [...new Set(e.ids)];
|
|
17
|
+
return [...out.values()];
|
|
18
|
+
}
|
|
19
|
+
/** Builds a lookup from reader IDs to their converted layers. */
|
|
20
|
+
export function layersBySource(document) {
|
|
21
|
+
const ids = new Map();
|
|
22
|
+
for (const model of document.models) {
|
|
23
|
+
const { sourceId } = model;
|
|
24
|
+
if (sourceId === undefined)
|
|
25
|
+
continue;
|
|
26
|
+
const found = ids.get(sourceId);
|
|
27
|
+
if (found)
|
|
28
|
+
found.push(model.id);
|
|
29
|
+
else
|
|
30
|
+
ids.set(sourceId, [model.id]);
|
|
31
|
+
}
|
|
32
|
+
return ids;
|
|
33
|
+
}
|
|
34
|
+
/** Turns adapter diagnostics into import reports. */
|
|
35
|
+
export function convertEntries(report, details = false) {
|
|
36
|
+
const entries = (counts, category, kind, prefix = '') => [...counts].map(([what, count]) => {
|
|
37
|
+
const ids = report.layerIds.get(`${category}:${what}`);
|
|
38
|
+
return { kind, what: prefix + what, count, ...(ids?.length && { ids }) };
|
|
39
|
+
});
|
|
40
|
+
return [
|
|
41
|
+
...entries(report.dropped, 'dropped', 'left-out'),
|
|
42
|
+
...(details ? [
|
|
43
|
+
...entries(report.approximations, 'note', 'approximated'),
|
|
44
|
+
...entries(report.expressionErrors, 'expression', 'left-out', 'Expression error: '),
|
|
45
|
+
] : []),
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Collects each distinct face used by a text layer or an instance override.
|
|
50
|
+
* @param document - Converted document with its layer and override IDs.
|
|
51
|
+
* @returns One occurrence per face and layer, ready to group with other report entries.
|
|
52
|
+
*/
|
|
53
|
+
export function fontEntries(document) {
|
|
54
|
+
const entries = [];
|
|
55
|
+
function faces(styles, id) {
|
|
56
|
+
for (const what of new Set(styles?.map(s => s.family + '/' + s.style)))
|
|
57
|
+
entries.push({ kind: 'font', what, count: 1, ids: [id] });
|
|
58
|
+
}
|
|
59
|
+
for (const m of document.models) {
|
|
60
|
+
if (m.kind === 'page')
|
|
61
|
+
continue;
|
|
62
|
+
if (m.styles)
|
|
63
|
+
faces(m.styles, m.id);
|
|
64
|
+
for (const [path, override] of Object.entries(m.overrides ?? {}))
|
|
65
|
+
if (override.styles)
|
|
66
|
+
faces(override.styles, m.id + ';' + path);
|
|
67
|
+
}
|
|
68
|
+
return entries;
|
|
69
|
+
}
|
package/dist/svg.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ImportEnv, ImportPayload } from '@moondraft/editor/core';
|
|
2
|
+
import { type SvgMeasureText, type SvgParse } from './taco/svg-import/index.ts';
|
|
3
|
+
/** Options for reading editable layers from SVG. */
|
|
4
|
+
export interface SvgReadOptions {
|
|
5
|
+
/** The root layer's name (default: the SVG's <title>, else "SVG"). */
|
|
6
|
+
name?: string;
|
|
7
|
+
/** Parses markup into an element tree (default: the browser's DOMParser). */
|
|
8
|
+
parseSvg?: SvgParse;
|
|
9
|
+
/** Loaded faces used to choose a family from the SVG's list. With no faces, Inter is treated as available. If no family matches, the first requested family is kept. */
|
|
10
|
+
fonts?: ImportEnv['fonts'];
|
|
11
|
+
/** Measures a text run (default: an estimate from Inter's average advance and its ascent and descent). */
|
|
12
|
+
measureText?: SvgMeasureText;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parses SVG with the browser's XML parser.
|
|
16
|
+
* @param text - SVG markup.
|
|
17
|
+
* @returns The document element, which may be a parser error element.
|
|
18
|
+
*/
|
|
19
|
+
export declare const domParseSvg: SvgParse;
|
|
20
|
+
/**
|
|
21
|
+
* Estimates a text run's metrics from Inter's average advance, ascent and descent.
|
|
22
|
+
* @param run - Text and font sizing for the run.
|
|
23
|
+
* @returns The estimated width, ascent and descent.
|
|
24
|
+
*/
|
|
25
|
+
export declare const estimateText: SvgMeasureText;
|
|
26
|
+
/** Imports SVG markup as a paste payload, with what was approximated or left out. Throws when the importer refuses
|
|
27
|
+
* (over DEFAULT_SVG_IMPORT_LIMITS.inputBytes, not well-formed, not an SVG). The markup goes through importSvgMarkup,
|
|
28
|
+
* which counts its bytes against the cap and imports nested data-URI SVG images.
|
|
29
|
+
* @param markup - SVG markup.
|
|
30
|
+
* @param options - Parser, font and measurement settings.
|
|
31
|
+
* @returns Paste payload and report of conversion losses and font faces used by imported text.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readSvg(markup: string, options?: SvgReadOptions): ImportPayload;
|
package/dist/svg.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { svgDocument, toDocument, toPayload } from './adapter/index.js';
|
|
2
|
+
import { convertEntries, fontEntries, grouped, layersBySource } from './report.js';
|
|
3
|
+
import { importSvgMarkup, isSvgImportRefusal } from './taco/svg-import/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Parses SVG with the browser's XML parser.
|
|
6
|
+
* @param text - SVG markup.
|
|
7
|
+
* @returns The document element, which may be a parser error element.
|
|
8
|
+
*/
|
|
9
|
+
export const domParseSvg = (text) => {
|
|
10
|
+
if (typeof DOMParser === 'undefined')
|
|
11
|
+
throw new Error('no SVG parser here: pass parseSvg');
|
|
12
|
+
return new DOMParser().parseFromString(text, 'image/svg+xml').documentElement;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Estimates a text run's metrics from Inter's average advance, ascent and descent.
|
|
16
|
+
* @param run - Text and font sizing for the run.
|
|
17
|
+
* @returns The estimated width, ascent and descent.
|
|
18
|
+
*/
|
|
19
|
+
export const estimateText = (run) => {
|
|
20
|
+
const { text, fontSize, letterSpacing } = run, length = [...text].length;
|
|
21
|
+
return { width: length * fontSize * 0.55 + Math.max(0, length - 1) * letterSpacing, ascent: fontSize * 0.97, descent: fontSize * 0.24 };
|
|
22
|
+
};
|
|
23
|
+
/** Imports SVG markup as a paste payload, with what was approximated or left out. Throws when the importer refuses
|
|
24
|
+
* (over DEFAULT_SVG_IMPORT_LIMITS.inputBytes, not well-formed, not an SVG). The markup goes through importSvgMarkup,
|
|
25
|
+
* which counts its bytes against the cap and imports nested data-URI SVG images.
|
|
26
|
+
* @param markup - SVG markup.
|
|
27
|
+
* @param options - Parser, font and measurement settings.
|
|
28
|
+
* @returns Paste payload and report of conversion losses and font faces used by imported text.
|
|
29
|
+
*/
|
|
30
|
+
export function readSvg(markup, options = {}) {
|
|
31
|
+
const fonts = options.fonts ?? [], families = new Set(fonts.map((f) => f.family.toLowerCase()));
|
|
32
|
+
const fontAvailable = (family) => (families.size ? families.has(family.toLowerCase()) : /^inter$/i.test(family));
|
|
33
|
+
let next = 0;
|
|
34
|
+
const result = importSvgMarkup(markup, { parseSvg: options.parseSvg ?? domParseSvg, idSource: () => `s${++next}`, fontAvailable, measureText: options.measureText ?? estimateText, ...(options.name !== undefined ? { name: options.name } : {}) });
|
|
35
|
+
if (isSvgImportRefusal(result))
|
|
36
|
+
throw new Error(result.message);
|
|
37
|
+
const converted = toDocument(svgDocument(result, options.name ?? result.root.name), 0);
|
|
38
|
+
const { payload, report } = toPayload(converted), bySource = layersBySource(converted.doc);
|
|
39
|
+
const taco = result.report.filter((e) => e.kind !== 'auto-layout').map((e) => {
|
|
40
|
+
const ids = e.nodeId === undefined ? undefined : bySource.get(e.nodeId);
|
|
41
|
+
return { kind: e.kind, what: e.what, count: 1, ...(ids && { ids }) };
|
|
42
|
+
});
|
|
43
|
+
return { payload, report: grouped([...taco, ...convertEntries(report), ...fontEntries(converted.doc)]) };
|
|
44
|
+
}
|
package/dist/syntax.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Payload } from '@moondraft/editor/core';
|
|
2
|
+
/**
|
|
3
|
+
* Builds editable layers from the agent build tool's layer syntax on an isolated core.
|
|
4
|
+
* @param text - Source text accepted by the build tool.
|
|
5
|
+
* @returns The built layers as a paste payload.
|
|
6
|
+
* @throws An error if the build is refused or produces no layers.
|
|
7
|
+
*/
|
|
8
|
+
export declare function payloadFromSyntax(text: string): Payload;
|
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createAgentTools } from '@moondraft/editor/agent';
|
|
2
|
+
import { clipboard, createCore } from '@moondraft/editor/core';
|
|
3
|
+
/**
|
|
4
|
+
* Builds editable layers from the agent build tool's layer syntax on an isolated core.
|
|
5
|
+
* @param text - Source text accepted by the build tool.
|
|
6
|
+
* @returns The built layers as a paste payload.
|
|
7
|
+
* @throws An error if the build is refused or produces no layers.
|
|
8
|
+
*/
|
|
9
|
+
export function payloadFromSyntax(text) {
|
|
10
|
+
const core = createCore();
|
|
11
|
+
const build = createAgentTools(core).find((tool) => tool.name === 'build');
|
|
12
|
+
const result = build.run({ do: text });
|
|
13
|
+
if (!result.ok)
|
|
14
|
+
throw new Error(result.text);
|
|
15
|
+
const payload = clipboard.copy(core, core.doc.children(''));
|
|
16
|
+
if (!payload)
|
|
17
|
+
throw new Error('build produced no layers');
|
|
18
|
+
return payload;
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type KiwiContainerHeader, type KiwiContainerOptions } from "./container/kiwiContainer.ts";
|
|
2
|
+
import type { KiwiObject, KiwiSchema } from "./kiwi/types.ts";
|
|
3
|
+
/** The JSON Figma puts in the `figmeta` marker. */
|
|
4
|
+
export interface FigmaClipboardMeta {
|
|
5
|
+
readonly fileKey: string;
|
|
6
|
+
readonly pasteID: number;
|
|
7
|
+
/** `"scene"` for copied layers. */
|
|
8
|
+
readonly dataType: string;
|
|
9
|
+
}
|
|
10
|
+
export interface FigmaClipboard {
|
|
11
|
+
readonly meta: FigmaClipboardMeta;
|
|
12
|
+
readonly header: KiwiContainerHeader;
|
|
13
|
+
readonly schema: KiwiSchema;
|
|
14
|
+
readonly message: KiwiObject;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reads the `text/html` Figma writes when layers are copied. The HTML holds
|
|
18
|
+
* two comments, `<!--(figmeta)BASE64(/figmeta)-->` with JSON about the copy
|
|
19
|
+
* and `<!--(figma)BASE64(/figma)-->` with a Kiwi container. Some browsers
|
|
20
|
+
* deliver the comment brackets escaped as `<!--` and `-->`; both
|
|
21
|
+
* spellings are accepted. The HTML is searched as text, so this runs
|
|
22
|
+
* without a DOM. Chunks are limited to `FIGMA_READ_LIMITS.maxPasteChunkBytes`
|
|
23
|
+
* unless `options.maxDecompressedChunkBytes` says otherwise.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readFigmaClipboardHtml(html: string, options?: KiwiContainerOptions): FigmaClipboard;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { FigmaReadError } from "./container/error.js";
|
|
2
|
+
import { FIGMA_READ_LIMITS } from "./container/limits.js";
|
|
3
|
+
import { readKiwiContainer } from "./container/kiwiContainer.js";
|
|
4
|
+
/**
|
|
5
|
+
* Reads the `text/html` Figma writes when layers are copied. The HTML holds
|
|
6
|
+
* two comments, `<!--(figmeta)BASE64(/figmeta)-->` with JSON about the copy
|
|
7
|
+
* and `<!--(figma)BASE64(/figma)-->` with a Kiwi container. Some browsers
|
|
8
|
+
* deliver the comment brackets escaped as `<!--` and `-->`; both
|
|
9
|
+
* spellings are accepted. The HTML is searched as text, so this runs
|
|
10
|
+
* without a DOM. Chunks are limited to `FIGMA_READ_LIMITS.maxPasteChunkBytes`
|
|
11
|
+
* unless `options.maxDecompressedChunkBytes` says otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export function readFigmaClipboardHtml(html, options = {}) {
|
|
14
|
+
if (typeof html !== "string") {
|
|
15
|
+
throw new TypeError("readFigmaClipboardHtml expects a string");
|
|
16
|
+
}
|
|
17
|
+
const meta = readMeta(decodeBase64(findMarker(html, "figmeta"), "figmeta"));
|
|
18
|
+
const { header, schema, message } = readKiwiContainer(decodeBase64(findMarker(html, "figma"), "figma"), {
|
|
19
|
+
maxDecompressedChunkBytes: options.maxDecompressedChunkBytes ?? FIGMA_READ_LIMITS.maxPasteChunkBytes,
|
|
20
|
+
});
|
|
21
|
+
return { meta, header, schema, message };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Finds the one `(name)…(/name)` marker inside a comment. The search moves
|
|
25
|
+
* forward only: each `indexOf` starts where the last one stopped, so the
|
|
26
|
+
* time is linear in the HTML size even for many unclosed openers.
|
|
27
|
+
*/
|
|
28
|
+
function findMarker(html, name) {
|
|
29
|
+
const opener = `(${name})`;
|
|
30
|
+
const closer = `(/${name})`;
|
|
31
|
+
const payloads = [];
|
|
32
|
+
let at = 0;
|
|
33
|
+
for (;;) {
|
|
34
|
+
const open = html.indexOf(opener, at);
|
|
35
|
+
if (open < 0)
|
|
36
|
+
break;
|
|
37
|
+
at = open + opener.length;
|
|
38
|
+
if (!html.startsWith("<!--", open - 4) && !html.startsWith("<!--", open - 7))
|
|
39
|
+
continue;
|
|
40
|
+
let close = html.indexOf(closer, at);
|
|
41
|
+
while (close >= 0 && !html.startsWith("-->", close + closer.length) && !html.startsWith("-->", close + closer.length)) {
|
|
42
|
+
close = html.indexOf(closer, close + closer.length);
|
|
43
|
+
}
|
|
44
|
+
// No closed comment after this opener means none after any later opener either.
|
|
45
|
+
if (close < 0)
|
|
46
|
+
break;
|
|
47
|
+
payloads.push(html.slice(at, close));
|
|
48
|
+
at = close + closer.length;
|
|
49
|
+
}
|
|
50
|
+
if (payloads.length === 0) {
|
|
51
|
+
throw new FigmaReadError("missing-marker", `the HTML has no (${name}) marker`);
|
|
52
|
+
}
|
|
53
|
+
if (payloads.length > 1) {
|
|
54
|
+
throw new FigmaReadError("duplicate-marker", `the HTML has ${payloads.length} (${name}) markers, expected one`);
|
|
55
|
+
}
|
|
56
|
+
return payloads[0];
|
|
57
|
+
}
|
|
58
|
+
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
59
|
+
function decodeBase64(text, name) {
|
|
60
|
+
const compact = text.replace(/[\t\n\f\r ]+/g, "");
|
|
61
|
+
if (compact.length === 0 || !BASE64.test(compact)) {
|
|
62
|
+
throw new FigmaReadError("bad-base64", `the (${name}) marker does not hold valid base64`);
|
|
63
|
+
}
|
|
64
|
+
const binary = atob(compact);
|
|
65
|
+
const bytes = new Uint8Array(binary.length);
|
|
66
|
+
for (let index = 0; index < binary.length; index += 1)
|
|
67
|
+
bytes[index] = binary.charCodeAt(index);
|
|
68
|
+
return bytes;
|
|
69
|
+
}
|
|
70
|
+
function readMeta(bytes) {
|
|
71
|
+
let value;
|
|
72
|
+
try {
|
|
73
|
+
value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
77
|
+
throw new FigmaReadError("bad-meta", `the (figmeta) marker is not UTF-8 JSON: ${reason}`);
|
|
78
|
+
}
|
|
79
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
80
|
+
throw new FigmaReadError("bad-meta", "the (figmeta) JSON is not an object");
|
|
81
|
+
}
|
|
82
|
+
const { fileKey, pasteID, dataType } = value;
|
|
83
|
+
if (typeof fileKey !== "string" || typeof pasteID !== "number" || typeof dataType !== "string") {
|
|
84
|
+
throw new FigmaReadError("bad-meta", "the (figmeta) JSON needs a string fileKey, a number pasteID and a string dataType");
|
|
85
|
+
}
|
|
86
|
+
return { fileKey, pasteID, dataType };
|
|
87
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How much data a Figma clipboard holds, read before any of it is decoded,
|
|
3
|
+
* so a paste can plan its progress bar up front.
|
|
4
|
+
*/
|
|
5
|
+
export interface FigmaClipboardSize {
|
|
6
|
+
/** Bytes of the Kiwi container (the decoded `(figma)` marker). */
|
|
7
|
+
readonly containerBytes: number;
|
|
8
|
+
/** Bytes of the message chunk as stored, still compressed. */
|
|
9
|
+
readonly messageCompressedBytes: number;
|
|
10
|
+
/** How the message chunk is compressed. */
|
|
11
|
+
readonly messageCompression: "zstd" | "deflate";
|
|
12
|
+
/**
|
|
13
|
+
* Bytes of the message once decompressed, from the zstd frame header's
|
|
14
|
+
* content size; null when the frame does not say (Figma's clipboard always
|
|
15
|
+
* does) or the chunk is deflate, which never says.
|
|
16
|
+
*/
|
|
17
|
+
readonly messageBytes: number | null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Reads how large the clipboard's Figma data is without decoding it: it
|
|
21
|
+
* finds the `(figma)` marker and decodes only the few base64 characters
|
|
22
|
+
* that hold the container header, the two chunk lengths and the message's
|
|
23
|
+
* zstd frame header (base64 maps every 4 characters to 3 bytes, so any byte
|
|
24
|
+
* can be decoded on its own). The cost does not grow with the clipboard's
|
|
25
|
+
* size beyond the marker search.
|
|
26
|
+
*
|
|
27
|
+
* Returns null when the HTML has no readable marker or the container is cut
|
|
28
|
+
* short; the full read (`readFigmaClipboardHtml`) then says why. This is
|
|
29
|
+
* only an estimate for planning: it checks no more than it needs.
|
|
30
|
+
*/
|
|
31
|
+
export declare function peekFigmaClipboardSize(html: string): FigmaClipboardSize | null;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { startsWithZstdMagic } from "./container/compression.js";
|
|
2
|
+
/** The container's 8-byte magic and u32 version, then the schema chunk's u32 length. */
|
|
3
|
+
const HEADER_BYTES = 12;
|
|
4
|
+
/** The most bytes a zstd frame header takes: magic, descriptor, window, dictionary id, content size. */
|
|
5
|
+
const ZSTD_FRAME_HEADER_BYTES = 4 + 1 + 1 + 4 + 8;
|
|
6
|
+
/**
|
|
7
|
+
* Reads how large the clipboard's Figma data is without decoding it: it
|
|
8
|
+
* finds the `(figma)` marker and decodes only the few base64 characters
|
|
9
|
+
* that hold the container header, the two chunk lengths and the message's
|
|
10
|
+
* zstd frame header (base64 maps every 4 characters to 3 bytes, so any byte
|
|
11
|
+
* can be decoded on its own). The cost does not grow with the clipboard's
|
|
12
|
+
* size beyond the marker search.
|
|
13
|
+
*
|
|
14
|
+
* Returns null when the HTML has no readable marker or the container is cut
|
|
15
|
+
* short; the full read (`readFigmaClipboardHtml`) then says why. This is
|
|
16
|
+
* only an estimate for planning: it checks no more than it needs.
|
|
17
|
+
*/
|
|
18
|
+
export function peekFigmaClipboardSize(html) {
|
|
19
|
+
const payload = markerPayload(html);
|
|
20
|
+
if (payload === null)
|
|
21
|
+
return null;
|
|
22
|
+
const text = /[\t\n\f\r ]/.test(payload) ? payload.replace(/[\t\n\f\r ]+/g, "") : payload;
|
|
23
|
+
if (text.length % 4 !== 0 || text.length === 0)
|
|
24
|
+
return null;
|
|
25
|
+
const padding = text.endsWith("==") ? 2 : text.endsWith("=") ? 1 : 0;
|
|
26
|
+
const containerBytes = (text.length / 4) * 3 - padding;
|
|
27
|
+
const bytesAt = (start, count) => {
|
|
28
|
+
if (start < 0 || start + count > containerBytes)
|
|
29
|
+
return null;
|
|
30
|
+
const first = Math.floor(start / 3);
|
|
31
|
+
const last = Math.ceil((start + count) / 3);
|
|
32
|
+
let binary;
|
|
33
|
+
try {
|
|
34
|
+
binary = atob(text.slice(first * 4, last * 4));
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const bytes = new Uint8Array(count);
|
|
40
|
+
for (let index = 0; index < count; index += 1)
|
|
41
|
+
bytes[index] = binary.charCodeAt(start - first * 3 + index);
|
|
42
|
+
return bytes;
|
|
43
|
+
};
|
|
44
|
+
const u32 = (start) => {
|
|
45
|
+
const bytes = bytesAt(start, 4);
|
|
46
|
+
return bytes === null ? null : (bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24)) >>> 0;
|
|
47
|
+
};
|
|
48
|
+
const schemaBytes = u32(HEADER_BYTES);
|
|
49
|
+
if (schemaBytes === null)
|
|
50
|
+
return null;
|
|
51
|
+
const messageLengthAt = HEADER_BYTES + 4 + schemaBytes;
|
|
52
|
+
const messageCompressedBytes = u32(messageLengthAt);
|
|
53
|
+
if (messageCompressedBytes === null)
|
|
54
|
+
return null;
|
|
55
|
+
const messageAt = messageLengthAt + 4;
|
|
56
|
+
if (messageCompressedBytes === 0 || messageAt + messageCompressedBytes > containerBytes)
|
|
57
|
+
return null;
|
|
58
|
+
const head = bytesAt(messageAt, Math.min(ZSTD_FRAME_HEADER_BYTES, messageCompressedBytes));
|
|
59
|
+
if (head === null)
|
|
60
|
+
return null;
|
|
61
|
+
if (!startsWithZstdMagic(head)) {
|
|
62
|
+
return { containerBytes, messageCompressedBytes, messageCompression: "deflate", messageBytes: null };
|
|
63
|
+
}
|
|
64
|
+
return { containerBytes, messageCompressedBytes, messageCompression: "zstd", messageBytes: zstdContentSize(head) };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The content size a zstd frame header gives (RFC 8878, section 3.1.1.1),
|
|
68
|
+
* or null when the header leaves it out or is cut short.
|
|
69
|
+
*/
|
|
70
|
+
function zstdContentSize(head) {
|
|
71
|
+
if (head.length < 5)
|
|
72
|
+
return null;
|
|
73
|
+
const descriptor = head[4];
|
|
74
|
+
const contentSizeFlag = descriptor >> 6;
|
|
75
|
+
const singleSegment = (descriptor >> 5) & 1;
|
|
76
|
+
const dictionaryIdBytes = [0, 1, 2, 4][descriptor & 3];
|
|
77
|
+
const contentSizeBytes = [singleSegment ? 1 : 0, 2, 4, 8][contentSizeFlag];
|
|
78
|
+
if (contentSizeBytes === 0)
|
|
79
|
+
return null;
|
|
80
|
+
const at = 5 + (singleSegment ? 0 : 1) + dictionaryIdBytes;
|
|
81
|
+
if (at + contentSizeBytes > head.length)
|
|
82
|
+
return null;
|
|
83
|
+
let size = 0;
|
|
84
|
+
for (let index = contentSizeBytes - 1; index >= 0; index -= 1)
|
|
85
|
+
size = size * 256 + head[at + index];
|
|
86
|
+
// A 2-byte size is stored less 256.
|
|
87
|
+
return contentSizeFlag === 1 ? size + 256 : size;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The text between the first `(figma)` opener inside a comment and the
|
|
91
|
+
* `(/figma)` closer after it, or null when there is none. Unlike the full
|
|
92
|
+
* read, a second marker is not looked for.
|
|
93
|
+
*/
|
|
94
|
+
function markerPayload(html) {
|
|
95
|
+
const opener = "(figma)";
|
|
96
|
+
let at = 0;
|
|
97
|
+
for (;;) {
|
|
98
|
+
const open = html.indexOf(opener, at);
|
|
99
|
+
if (open < 0)
|
|
100
|
+
return null;
|
|
101
|
+
at = open + opener.length;
|
|
102
|
+
if (!html.startsWith("<!--", open - 4) && !html.startsWith("<!--", open - 7))
|
|
103
|
+
continue;
|
|
104
|
+
const close = html.indexOf("(/figma)", at);
|
|
105
|
+
return close < 0 ? null : html.slice(at, close);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The four bytes every zstd frame starts with. */
|
|
2
|
+
export declare const ZSTD_MAGIC: readonly [40, 181, 47, 253];
|
|
3
|
+
export declare function startsWithZstdMagic(bytes: Uint8Array): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Decompresses one chunk: zstd when it starts with the zstd magic, raw
|
|
6
|
+
* deflate otherwise. `offset` is where the chunk starts in the container and
|
|
7
|
+
* is reported with any refusal.
|
|
8
|
+
*/
|
|
9
|
+
export declare function decompressChunk(bytes: Uint8Array, offset: number, maxBytes: number): Uint8Array;
|