@moondraft/import 0.4.0 → 0.5.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.
@@ -11,6 +11,17 @@ import type { NodeWalk } from "../walk.ts";
11
11
  * them through `LAYER_BLEND_MODES`).
12
12
  */
13
13
  export declare function blendModeOf(value: string): BlendMode | undefined | null;
14
+ /**
15
+ * Moondraft patch 0055: the words every report entry about a corrupt number uses. The adapter names the Figma node
16
+ * (ID and name) in such entries, so the node can be found in a large file.
17
+ */
18
+ export declare const NON_FINITE = "a non-finite number (NaN or infinity)";
19
+ /** Moondraft patch 0055: the entry for a component or variant set whose repeated `componentKey` takes its fallback key. */
20
+ export declare const DUPLICATE_KEY = "duplicate component key replaced";
21
+ /** Moondraft patch 0055: whether a reader entry's reason is one the adapter names the Figma node in. */
22
+ export declare function namesNode(reason: string): boolean;
23
+ /** Moondraft patch 0055: whether one of `keys` of `object` holds a number that is NaN or infinite. */
24
+ export declare function hasNonFinite(object: FigmaObject, keys: readonly string[]): boolean;
14
25
  export declare function readNumbers<K extends string>(object: FigmaObject, keys: readonly K[], where: string): Record<K, number> | null;
15
26
  /** `#rrggbb`, or `#rrggbbaa` when alpha is below 1, from Figma's 0..1 channels. */
16
27
  export declare function hexColor(color: Record<"r" | "g" | "b" | "a", number>): string;
@@ -31,10 +42,17 @@ export declare function layerBase(node: FigmaTreeNode, walk: NodeWalk, defaultNa
31
42
  * groups) by the walk, as Figma applies none when a section is resized.
32
43
  */
33
44
  export declare function constraintsOf(change: FigmaObject, report: NodeWalk["report"]): ResizeConstraints | undefined;
34
- /** A node's size and transform numbers, or `null` when either is incomplete, so `framedBase` leaves the node out. */
45
+ declare const TRANSFORM_KEYS: readonly ["m00", "m01", "m02", "m10", "m11", "m12"];
46
+ /**
47
+ * A node's size and transform numbers, or `null` when either is incomplete, so `framedBase` leaves the node out.
48
+ * Moondraft patch 0055: a non-finite number does not leave the node, and every layer inside it, out. A non-finite
49
+ * width, height or position (`m02`, `m12`) reads as 0, and a rotation or scale part (`m00`, `m01`, `m10`, `m11`) with
50
+ * a non-finite number reads as no rotation and no scale; `nonFinite` then says so, and `framedBase` reports it.
51
+ */
35
52
  export declare function frameGeometry(node: FigmaTreeNode): {
36
53
  sides: Record<"x" | "y", number>;
37
- m: Record<"m00" | "m01" | "m02" | "m10" | "m11" | "m12", number>;
54
+ m: Record<(typeof TRANSFORM_KEYS)[number], number>;
55
+ nonFinite?: true;
38
56
  } | null;
39
57
  /**
40
58
  * The fields every framed Taco node shares, or `null` after reporting why
@@ -70,3 +88,4 @@ export declare function endCapOf(value: unknown): StrokeCap | undefined;
70
88
  export declare function nodeStrokes(node: FigmaTreeNode, walk: NodeWalk, box?: PaintBounds, sides?: boolean): Stroke[];
71
89
  /** Solid, gradient and image fills and strokes and corners as a Taco style; other paint types are reported and left out. */
72
90
  export declare function visualStyle(node: FigmaTreeNode, walk: NodeWalk): VisualStyle;
91
+ export {};
@@ -20,8 +20,26 @@ export function blendModeOf(value) {
20
20
  }
21
21
  /** Figma's linear dodge and burn, which layers and paints take, as their CSS `plus-` names. */
22
22
  const LAYER_BLEND_MODES = { LINEAR_DODGE: "plus-lighter", LINEAR_BURN: "plus-darker" };
23
+ /**
24
+ * Moondraft patch 0055: the words every report entry about a corrupt number uses. The adapter names the Figma node
25
+ * (ID and name) in such entries, so the node can be found in a large file.
26
+ */
27
+ export const NON_FINITE = "a non-finite number (NaN or infinity)";
28
+ /** Moondraft patch 0055: the entry for a component or variant set whose repeated `componentKey` takes its fallback key. */
29
+ export const DUPLICATE_KEY = "duplicate component key replaced";
30
+ /** Moondraft patch 0055: whether a reader entry's reason is one the adapter names the Figma node in. */
31
+ export function namesNode(reason) {
32
+ return reason === DUPLICATE_KEY || reason.includes(NON_FINITE);
33
+ }
34
+ /** Moondraft patch 0055: whether one of `keys` of `object` holds a number that is NaN or infinite. */
35
+ export function hasNonFinite(object, keys) {
36
+ return keys.some((key) => typeof object[key] === "number" && !Number.isFinite(object[key]));
37
+ }
23
38
  export function readNumbers(object, keys, where) {
24
39
  const out = {};
40
+ // Moondraft patch 0055: a non-finite number reads as absent, so one corrupt value does not refuse the message.
41
+ if (hasNonFinite(object, keys))
42
+ return null;
25
43
  for (const key of keys) {
26
44
  const value = field(object, key, "number", where);
27
45
  if (value === undefined)
@@ -115,14 +133,31 @@ export function constraintsOf(change, report) {
115
133
  const constraints = { horizontal: read("horizontalConstraint", HORIZONTAL), vertical: read("verticalConstraint", VERTICAL) };
116
134
  return constraints.horizontal === "left" && constraints.vertical === "top" ? undefined : constraints;
117
135
  }
118
- /** A node's size and transform numbers, or `null` when either is incomplete, so `framedBase` leaves the node out. */
136
+ const TRANSFORM_KEYS = ["m00", "m01", "m02", "m10", "m11", "m12"];
137
+ const LINEAR_KEYS = ["m00", "m01", "m10", "m11"];
138
+ const IDENTITY = { m00: 1, m01: 0, m10: 0, m11: 1 };
139
+ /**
140
+ * A node's size and transform numbers, or `null` when either is incomplete, so `framedBase` leaves the node out.
141
+ * Moondraft patch 0055: a non-finite number does not leave the node, and every layer inside it, out. A non-finite
142
+ * width, height or position (`m02`, `m12`) reads as 0, and a rotation or scale part (`m00`, `m01`, `m10`, `m11`) with
143
+ * a non-finite number reads as no rotation and no scale; `nonFinite` then says so, and `framedBase` reports it.
144
+ */
119
145
  export function frameGeometry(node) {
120
146
  const where = `node ${node.id}`;
121
- const size = field(node.change, "size", "object", where);
122
- const transform = field(node.change, "transform", "object", where);
147
+ let size = field(node.change, "size", "object", where);
148
+ let transform = field(node.change, "transform", "object", where);
149
+ const nonFinite = !!size && hasNonFinite(size, ["x", "y"]) || !!transform && hasNonFinite(transform, TRANSFORM_KEYS);
150
+ if (nonFinite) {
151
+ const zero = (object, key) => hasNonFinite(object, [key]) ? 0 : object[key];
152
+ if (size)
153
+ size = { ...size, x: zero(size, "x"), y: zero(size, "y") };
154
+ if (transform) {
155
+ transform = { ...transform, ...(hasNonFinite(transform, LINEAR_KEYS) && IDENTITY), m02: zero(transform, "m02"), m12: zero(transform, "m12") };
156
+ }
157
+ }
123
158
  const sides = size && readNumbers(size, ["x", "y"], `${where} size`);
124
- const m = transform && readNumbers(transform, ["m00", "m01", "m02", "m10", "m11", "m12"], `${where} transform`);
125
- return sides && m ? { sides, m } : null;
159
+ const m = transform && readNumbers(transform, TRANSFORM_KEYS, `${where} transform`);
160
+ return sides && m ? { sides, m, ...(nonFinite && { nonFinite: true }) } : null;
126
161
  }
127
162
  /**
128
163
  * The fields every framed Taco node shares, or `null` after reporting why
@@ -138,6 +173,9 @@ export function framedBase(node, walk, defaultName, effectsImported = false) {
138
173
  return null;
139
174
  }
140
175
  const { sides, m } = geometry;
176
+ if (geometry.nonFinite) {
177
+ walk.report("approximated", `the size or transform has ${NON_FINITE}; a non-finite size or position reads as 0, a non-finite rotation or scale as none`);
178
+ }
141
179
  const { x: width, y: height } = sides;
142
180
  const flipX = m.m00 * m.m11 - m.m01 * m.m10 < 0;
143
181
  // With a flip the matrix is R·diag(-1, 1), so R's first column is minus the matrix's.
@@ -177,7 +215,7 @@ function boundStopColor(walk, paint, index, at) {
177
215
  return { r: channel(1), g: channel(3), b: channel(5), a: hex.length === 9 ? channel(7) : 1 };
178
216
  }
179
217
  /** A Figma gradient paint as a Taco paint, or a reason it is left out. */
180
- function gradientPaint(node, walk, paint, type, at, box) {
218
+ function gradientPaint(node, walk, paint, type, at, label, box) {
181
219
  const where = `node ${node.id}`;
182
220
  const size = field(node.change, "size", "object", where);
183
221
  const sides = size && readNumbers(size, ["x", "y"], `${where} size`);
@@ -185,6 +223,11 @@ function gradientPaint(node, walk, paint, type, at, box) {
185
223
  const m = transform && readNumbers(transform, ["m00", "m01", "m02", "m10", "m11", "m12"], `${at} transform`);
186
224
  const stops = [];
187
225
  for (const [index, stop] of (field(paint, "stops", "list", at) ?? []).entries()) {
226
+ // Moondraft patch 0055: a stop with a non-finite colour or position is left out; the gradient keeps the others.
227
+ if (isObject(stop) && (hasNonFinite(stop, ["position"]) || isObject(stop["color"]) && hasNonFinite(stop["color"], ["r", "g", "b", "a"]))) {
228
+ walk.report("property-left-out", `${label} (${type}) stop ${String(index + 1)} is left out: its colour or position has ${NON_FINITE}`);
229
+ continue;
230
+ }
188
231
  const colorObject = isObject(stop) ? field(stop, "color", "object", `${at} stops[${String(index)}]`) : undefined;
189
232
  const color = colorObject && readNumbers(colorObject, ["r", "g", "b", "a"], `${at} stops[${String(index)}] color`);
190
233
  const position = isObject(stop) ? field(stop, "position", "number", `${at} stops[${String(index)}]`) : undefined;
@@ -226,7 +269,7 @@ export function nodePaints(node, walk, list, box, source = styleBackedList(node.
226
269
  const type = field(paint, "type", "string", at);
227
270
  let base;
228
271
  if (type?.startsWith("GRADIENT_")) {
229
- const gradient = gradientPaint(node, walk, paint, type, at, typeof box === "function" ? box() : box);
272
+ const gradient = gradientPaint(node, walk, paint, type, at, label, typeof box === "function" ? box() : box);
230
273
  if (typeof gradient === "string")
231
274
  return walk.report("property-left-out", `${label} (${type}) is left out: ${gradient}`);
232
275
  if (gradient.approximated)
@@ -244,6 +287,9 @@ export function nodePaints(node, walk, list, box, source = styleBackedList(node.
244
287
  else {
245
288
  const colorObject = field(paint, "color", "object", at);
246
289
  const color = colorObject && readNumbers(colorObject, ["r", "g", "b", "a"], `${at} color`);
290
+ if (type === "SOLID" && colorObject && hasNonFinite(colorObject, ["r", "g", "b", "a"])) {
291
+ return walk.report("property-left-out", `${label} (SOLID) is left out: its colour has ${NON_FINITE}`);
292
+ }
247
293
  if (type !== "SOLID" || !color) {
248
294
  return walk.report("property-left-out", `${label} (${type ?? "no type"}) is left out: only solid paints with a colour, gradient paints and image paints are imported`);
249
295
  }
@@ -2,7 +2,7 @@ import { field, guidText, isObject } from "../fields.js";
2
2
  import { isDetachedStyle, styleBackedList } from "../styles.js";
3
3
  import { carryEffects } from "../carry.js";
4
4
  import { nodeEffects } from "./effects.js";
5
- import { framedBase, hexColor, readNumbers } from "./shared.js";
5
+ import { framedBase, hasNonFinite, hexColor, NON_FINITE, readNumbers } from "./shared.js";
6
6
  import { sizeLimitsOf } from "./layout.js";
7
7
  const ALIGN = { LEFT: "left", CENTER: "center", RIGHT: "right", JUSTIFIED: "justify" };
8
8
  const VERTICAL_ALIGN = { TOP: "top", CENTER: "middle", BOTTOM: "bottom" };
@@ -367,7 +367,23 @@ export function inheritOpenTypeToggles(base, own) {
367
367
  */
368
368
  function styleFields(change, override, under) {
369
369
  const style = {};
370
+ let nonFiniteSize = false;
370
371
  for (const name of STYLE_FIELDS) {
372
+ if (name === "fontSize") {
373
+ // Moondraft patch 0055: a non-finite size gives way to the next size beneath it (the text style's, then the
374
+ // node's), else it is absent and the default applies; `runStyle` reports it.
375
+ const corrupt = (size) => typeof size === "number" && !Number.isFinite(size);
376
+ const own = override !== undefined && Object.hasOwn(override, name);
377
+ let value = own ? override[name] : under?.[name] ?? change[name];
378
+ if (corrupt(value)) {
379
+ nonFiniteSize = true;
380
+ const below = own ? [under?.[name], change[name]] : under?.[name] != null ? [change[name]] : [];
381
+ value = below.find((size) => size != null && !corrupt(size));
382
+ }
383
+ if (value !== undefined)
384
+ style[name] = value;
385
+ continue;
386
+ }
371
387
  const value = override !== undefined && Object.hasOwn(override, name)
372
388
  ? override[name]
373
389
  : name === "fillPaints" ? change[name] ?? under?.[name]
@@ -384,8 +400,13 @@ function styleFields(change, override, under) {
384
400
  }
385
401
  // A layer can set its own features alongside a shared typography style. An explicitly empty list clears that side.
386
402
  const toggles = inheritOpenTypeToggles(under, change);
387
- return { ...style, ...(override ? inheritOpenTypeToggles(toggles, override) : toggles) };
403
+ const out = { ...style, ...(override ? inheritOpenTypeToggles(toggles, override) : toggles) };
404
+ if (nonFiniteSize)
405
+ NON_FINITE_SIZES.add(out);
406
+ return out;
388
407
  }
408
+ /** Moondraft patch 0055: the style fields (`styleFields`) whose font size passed over a non-finite one. */
409
+ const NON_FINITE_SIZES = new WeakSet();
389
410
  /**
390
411
  * The variable data a run's `variableField` (`LINE_HEIGHT` or `FONT_SIZE`) is bound to, read where `styleFields` reads
391
412
  * the field it binds (`name`): an override entry's own binding, or none when the entry sets its own value; else the text
@@ -516,6 +537,8 @@ function runStyle(style, lookups, report, where, boundLineHeight) {
516
537
  const family = field(fontName, "family", "string", `${where} fontName`);
517
538
  const styleName = field(fontName, "style", "string", `${where} fontName`) ?? "";
518
539
  const fontSize = field(style, "fontSize", "number", where);
540
+ if (NON_FINITE_SIZES.has(style))
541
+ report("approximated", `the font size has ${NON_FINITE}; the size beneath it, or the default size, is used`);
519
542
  const { fontWeight: weight, italic } = fontStyleWeight(styleName);
520
543
  if (weight === undefined)
521
544
  report("approximated", `font style "${styleName}" has no known weight; it is imported as weight 400`);
@@ -628,7 +651,13 @@ export function textColor(paints, report = () => undefined, where = "text fill")
628
651
  if (!isObject(paint) || field(paint, "visible", "boolean", where) === false)
629
652
  continue;
630
653
  const type = field(paint, "type", "string", where) ?? "no type";
631
- const channels = type === "SOLID" ? readNumbers(field(paint, "color", "object", where) ?? {}, ["r", "g", "b", "a"], `${where} fill colour`) : null;
654
+ const colorObject = type === "SOLID" ? field(paint, "color", "object", where) ?? {} : undefined;
655
+ const channels = colorObject ? readNumbers(colorObject, ["r", "g", "b", "a"], `${where} fill colour`) : null;
656
+ if (colorObject && hasNonFinite(colorObject, ["r", "g", "b", "a"])) {
657
+ // Moondraft patch 0055: a non-finite channel leaves out only this fill; a fill below it can show.
658
+ report("property-left-out", `a SOLID text fill is left out: its colour has ${NON_FINITE}`);
659
+ continue;
660
+ }
632
661
  if (color !== undefined || channels === null) {
633
662
  report("property-left-out", `a ${type} text fill is left out: only the top solid fill is imported`);
634
663
  continue;
@@ -3,7 +3,7 @@ import { field, isObject } from "../fields.js";
3
3
  import { nodeEffects } from "./effects.js";
4
4
  import { sizeLimitsOf } from "./layout.js";
5
5
  import { isFigmaGroup } from "./frame.js";
6
- import { endCapOf, frameGeometry, framedBase, layerBase, nodePaints, nodeStrokes, STROKE_CAPS, visualStyle } from "./shared.js";
6
+ import { endCapOf, frameGeometry, framedBase, hasNonFinite, layerBase, NON_FINITE, nodePaints, nodeStrokes, STROKE_CAPS, visualStyle } from "./shared.js";
7
7
  import { NetworkRefused, applyRegionStyles, regionBoxes, vectorDataNetwork, vectorStyleTable } from "./vectorNetwork.js";
8
8
  import { carry, imageCropOf } from "../carry.js";
9
9
  /**
@@ -222,9 +222,11 @@ function pathPaintBox(network) {
222
222
  export function pathTransform(node, walk) {
223
223
  const where = `node ${node.id}`;
224
224
  const m = field(node.change, "transform", "object", where);
225
- const values = m && ["m00", "m10", "m01", "m11", "m02", "m12"].map((key) => field(m, key, "number", `${where} transform`));
225
+ const values = m && ["m00", "m10", "m01", "m11", "m02", "m12"].map((key) =>
226
+ // Moondraft patch 0055: a non-finite number reads as absent, so the node is left out instead of refusing the message.
227
+ typeof m[key] === "number" && !Number.isFinite(m[key]) ? undefined : field(m, key, "number", `${where} transform`));
226
228
  if (!values || values.some((value) => value === undefined)) {
227
- walk.report("node-left-out", "the node has no complete transform");
229
+ walk.report("node-left-out", m && hasNonFinite(m, ["m00", "m10", "m01", "m11", "m02", "m12"]) ? `the transform has ${NON_FINITE}` : "the node has no complete transform");
228
230
  return null;
229
231
  }
230
232
  const transform = values;
@@ -1,6 +1,6 @@
1
1
  import { nodeSupportsVariableProperty, VARIABLE_PROPERTY_TYPES, variablePropertiesOfField, } from "../../model/index.js";
2
2
  import { field, guidText, isObject } from "./fields.js";
3
- import { hexColor, readNumbers } from "./converters/shared.js";
3
+ import { hasNonFinite, hexColor, NON_FINITE, readNumbers } from "./converters/shared.js";
4
4
  import { fontStyleName } from '@moondraft/editor/core';
5
5
  import { fontStyleWeight } from "./converters/text.js";
6
6
  import { styleBackedList } from "./styles.js";
@@ -161,6 +161,9 @@ export function readFigmaVariables(nodes, newId, report, keepAliases = false) {
161
161
  const read = dataType === "COLOR" && type === "color"
162
162
  ? (isObject(value["colorValue"]) ? readNumbers(value["colorValue"], ["r", "g", "b", "a"], `${where} colorValue`) : null)
163
163
  : undefined;
164
+ // Moondraft patch 0055: a colour with a non-finite channel is left out like one without channels, and says why.
165
+ if (read === null && isObject(value["colorValue"]) && hasNonFinite(value["colorValue"], ["r", "g", "b", "a"]))
166
+ return { reason: `a colour with ${NON_FINITE}` };
164
167
  if (read !== undefined)
165
168
  return read === null ? { reason: "a colour without channels" } : { value: hexColor(read) };
166
169
  const plain = dataType === "FLOAT" && type === "number" ? field(value, "floatValue", "number", where)
@@ -307,6 +310,8 @@ export function readFigmaVariables(nodes, newId, report, keepAliases = false) {
307
310
  const key = refKey(entry["variableSetID"], `${at} variableSetID`);
308
311
  if (key === undefined || setsByKey.get(key) !== set)
309
312
  continue;
313
+ if (entry["variableModeID"] === undefined)
314
+ continue; // Moondraft patch 0055: a stale entry names no mode.
310
315
  const id = guidText(entry["variableModeID"], `${at} variableModeID`);
311
316
  if (set.modes.some((candidate) => candidate.figma === id)) {
312
317
  mode = id;
@@ -366,7 +371,8 @@ export function readFigmaVariables(nodes, newId, report, keepAliases = false) {
366
371
  return "its collection is not in the message";
367
372
  if (typeof set === "string")
368
373
  return set.replace(/^its /, "the ");
369
- const mode = guidText(entry["variableModeID"], `${where} variableModeID`);
374
+ // Moondraft patch 0055: a stale entry without a mode is left out like one naming a deleted mode.
375
+ const mode = entry["variableModeID"] === undefined ? undefined : guidText(entry["variableModeID"], `${where} variableModeID`);
370
376
  const found = set.modes.find((candidate) => candidate.figma === mode);
371
377
  return found === undefined ? `the mode is not in collection ${quoted(collections[set.id].name)}` : { collectionId: set.id, modeId: found.id };
372
378
  },
@@ -20,7 +20,7 @@ import { convertBooleanOperation, convertStarOrPolygon, convertVector, whyFlatte
20
20
  import { blobReader } from "./blobs.js";
21
21
  import { carry, carryMarks, clearConstraints, withImageCrop } from "./carry.js";
22
22
  import { nodeEffects } from "./converters/effects.js";
23
- import { frameGeometry, visualStyle } from "./converters/shared.js";
23
+ import { DUPLICATE_KEY, frameGeometry, visualStyle } from "./converters/shared.js";
24
24
  import { bindLayerVariables, figmaVariableModes, foldsVariableAlpha, keptFontSize, typographyVariable, readFigmaVariables } from "./variables.js";
25
25
  import { buildFigmaTree, chooseRoots, rootSourceOf, withAncestorTransforms } from "./tree.js";
26
26
  import { DEFAULT_FIGMA_IMPORT_LIMITS, FigmaImportError } from "./types.js";
@@ -860,8 +860,14 @@ export function walkMessage(message, options, native, distinctSources) {
860
860
  delete root.flipX;
861
861
  delete root.flipY;
862
862
  const componentId = componentIds.get(symbol.id);
863
+ const key = keyOf(symbol);
864
+ // Moondraft patch 0055: a repeated Figma key that the file's key source replaced is reported, as for variant sets.
865
+ const authored = symbol.change["componentKey"];
866
+ if (options.componentKeySource === undefined && rootSource === "pages" && typeof authored === "string" && authored !== "" && key !== authored) {
867
+ report("approximated", DUPLICATE_KEY);
868
+ }
863
869
  components.push({
864
- id: componentId, key: keyOf(symbol), name: componentNameOf(symbol), rootId: root.id,
870
+ id: componentId, key, name: componentNameOf(symbol), rootId: root.id,
865
871
  ...(properties.length > 0 ? { properties: properties.map((property) => property.definition) } : {}),
866
872
  });
867
873
  importedComponents.set(symbol.id, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moondraft/import",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "Editable SVG and Figma import for Moondraft.",
6
6
  "sideEffects": false,
@@ -52,6 +52,6 @@
52
52
  "access": "public"
53
53
  },
54
54
  "peerDependencies": {
55
- "@moondraft/editor": ">=0.6.0 <0.7.0"
55
+ "@moondraft/editor": ">=0.6.0 <0.9.0"
56
56
  }
57
57
  }