@moondraft/import 0.3.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.
- package/CHANGELOG.md +20 -0
- package/README.md +25 -0
- package/SOURCE.md +49 -0
- package/dist/adapter/convert.js +34 -1
- package/dist/adapter/file-components.d.ts +18 -0
- package/dist/adapter/file-components.js +96 -11
- package/dist/adapter/file-resources.d.ts +6 -1
- package/dist/adapter/file-resources.js +126 -28
- package/dist/adapter/link.d.ts +1 -1
- package/dist/adapter/link.js +138 -87
- package/dist/figma-file.d.ts +3 -2
- package/dist/figma-file.js +7 -2
- package/dist/figma-worker.d.ts +5 -0
- package/dist/figma-worker.js +6 -1
- package/dist/figma.d.ts +7 -1
- package/dist/figma.js +29 -10
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/lazy.d.ts +111 -0
- package/dist/lazy.js +363 -0
- package/dist/report.js +6 -1
- package/dist/serialize.d.ts +9 -0
- package/dist/serialize.js +54 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -1
- package/dist/taco/figma-import/import/carry.js +8 -0
- package/dist/taco/figma-import/import/converters/effects.js +11 -3
- package/dist/taco/figma-import/import/converters/group.d.ts +1 -0
- package/dist/taco/figma-import/import/converters/group.js +5 -2
- package/dist/taco/figma-import/import/converters/section.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/section.js +4 -1
- package/dist/taco/figma-import/import/converters/shared.d.ts +21 -2
- package/dist/taco/figma-import/import/converters/shared.js +53 -7
- package/dist/taco/figma-import/import/converters/text.js +39 -5
- package/dist/taco/figma-import/import/converters/vector.js +5 -3
- package/dist/taco/figma-import/import/instances.d.ts +10 -0
- package/dist/taco/figma-import/import/instances.js +22 -21
- package/dist/taco/figma-import/import/variables.js +8 -2
- package/dist/taco/figma-import/import/walk.js +8 -2
- package/package.json +3 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { carry } from "../carry.js";
|
|
1
|
+
import { carry, carryEffects } from "../carry.js";
|
|
2
|
+
import { nodeEffects } from "./effects.js";
|
|
2
3
|
import { framedBase } from "./shared.js";
|
|
3
4
|
/**
|
|
4
5
|
* A Figma group (a FRAME that `convertFrame` finds to be a group) as a Taco
|
|
@@ -16,9 +17,10 @@ import { framedBase } from "./shared.js";
|
|
|
16
17
|
*
|
|
17
18
|
* A Taco group follows its layers' constraints (see `NodeBase.constraints`).
|
|
18
19
|
* The adapter reads the group's own constraints from its Figma metadata.
|
|
20
|
+
* Moondraft patch: its shadows and blurs are carried there too (`carryEffects`).
|
|
19
21
|
*/
|
|
20
22
|
export const convertGroup = (node, walk) => {
|
|
21
|
-
const base = framedBase(node, walk, "Group");
|
|
23
|
+
const base = framedBase(node, walk, "Group", true);
|
|
22
24
|
if (base === null)
|
|
23
25
|
return null;
|
|
24
26
|
// `framedBase` sets no variable bindings; a group's type allows no width or height binding.
|
|
@@ -26,5 +28,6 @@ export const convertGroup = (node, walk) => {
|
|
|
26
28
|
const group = { ...fields, type: "group", children: walk.convertChildren() };
|
|
27
29
|
if (constraints !== undefined)
|
|
28
30
|
carry(group, { groupConstraints: { ...constraints } });
|
|
31
|
+
carryEffects(group, nodeEffects(node, walk));
|
|
29
32
|
return group;
|
|
30
33
|
};
|
|
@@ -3,6 +3,7 @@ import type { FigmaNodeConverter } from "../walk.ts";
|
|
|
3
3
|
* A Figma SECTION as a Taco frame with free layout. Sections never clip
|
|
4
4
|
* their content in Figma, so `clipContent` is false and `frameMaskDisabled`
|
|
5
5
|
* is ignored. `sectionContentsHidden` (collapsed contents) is reported.
|
|
6
|
-
* Moondraft imports section status from the original Figma nodes.
|
|
6
|
+
* Moondraft imports section status from the original Figma nodes. The frame
|
|
7
|
+
* carries `metadata.figma.section`, which the adapter turns into a section.
|
|
7
8
|
*/
|
|
8
9
|
export declare const convertSection: FigmaNodeConverter;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { carry } from "../carry.js";
|
|
1
2
|
import { field } from "../fields.js";
|
|
2
3
|
import { nodeEffects } from "./effects.js";
|
|
3
4
|
import { framedBase, visualStyle } from "./shared.js";
|
|
@@ -5,7 +6,8 @@ import { framedBase, visualStyle } from "./shared.js";
|
|
|
5
6
|
* A Figma SECTION as a Taco frame with free layout. Sections never clip
|
|
6
7
|
* their content in Figma, so `clipContent` is false and `frameMaskDisabled`
|
|
7
8
|
* is ignored. `sectionContentsHidden` (collapsed contents) is reported.
|
|
8
|
-
* Moondraft imports section status from the original Figma nodes.
|
|
9
|
+
* Moondraft imports section status from the original Figma nodes. The frame
|
|
10
|
+
* carries `metadata.figma.section`, which the adapter turns into a section.
|
|
9
11
|
*/
|
|
10
12
|
export const convertSection = (node, walk) => {
|
|
11
13
|
const where = `node ${node.id}`;
|
|
@@ -25,5 +27,6 @@ export const convertSection = (node, walk) => {
|
|
|
25
27
|
clipContent: false,
|
|
26
28
|
...(Object.keys(style).length > 0 ? { style } : {}),
|
|
27
29
|
};
|
|
30
|
+
carry(frame, { section: true });
|
|
28
31
|
return frame;
|
|
29
32
|
};
|
|
@@ -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
|
-
|
|
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<
|
|
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
|
-
|
|
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
|
-
|
|
122
|
-
|
|
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,
|
|
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
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { field, guidText, isObject } from "../fields.js";
|
|
2
2
|
import { isDetachedStyle, styleBackedList } from "../styles.js";
|
|
3
|
-
import {
|
|
3
|
+
import { carryEffects } from "../carry.js";
|
|
4
|
+
import { nodeEffects } from "./effects.js";
|
|
5
|
+
import { framedBase, hasNonFinite, hexColor, NON_FINITE, readNumbers } from "./shared.js";
|
|
4
6
|
import { sizeLimitsOf } from "./layout.js";
|
|
5
7
|
const ALIGN = { LEFT: "left", CENTER: "center", RIGHT: "right", JUSTIFIED: "justify" };
|
|
6
8
|
const VERTICAL_ALIGN = { TOP: "top", CENTER: "middle", BOTTOM: "bottom" };
|
|
@@ -45,9 +47,12 @@ export const KEPT_LINE_HEIGHT_FIELD = "tacoKeptLineHeight";
|
|
|
45
47
|
export const convertText = (node, walk) => {
|
|
46
48
|
const where = `node ${node.id}`;
|
|
47
49
|
const change = node.change;
|
|
48
|
-
const
|
|
49
|
-
if (
|
|
50
|
+
const framed = framedBase(node, walk, "Text", true);
|
|
51
|
+
if (framed === null)
|
|
50
52
|
return null;
|
|
53
|
+
// Moondraft patch: the layer's shadows and blurs are carried in its metadata (`carryEffects`); the renderer casts
|
|
54
|
+
// them from the glyphs.
|
|
55
|
+
const base = carryEffects(framed, nodeEffects(node, walk));
|
|
51
56
|
// Taco text has no strokes yet, so a text stroke is reported rather than dropped.
|
|
52
57
|
if ((styleBackedList(change, "strokePaints", walk.styleList, where)?.length ?? 0) > 0)
|
|
53
58
|
walk.report("property-left-out", "text strokes are not imported yet");
|
|
@@ -362,7 +367,23 @@ export function inheritOpenTypeToggles(base, own) {
|
|
|
362
367
|
*/
|
|
363
368
|
function styleFields(change, override, under) {
|
|
364
369
|
const style = {};
|
|
370
|
+
let nonFiniteSize = false;
|
|
365
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
|
+
}
|
|
366
387
|
const value = override !== undefined && Object.hasOwn(override, name)
|
|
367
388
|
? override[name]
|
|
368
389
|
: name === "fillPaints" ? change[name] ?? under?.[name]
|
|
@@ -379,8 +400,13 @@ function styleFields(change, override, under) {
|
|
|
379
400
|
}
|
|
380
401
|
// A layer can set its own features alongside a shared typography style. An explicitly empty list clears that side.
|
|
381
402
|
const toggles = inheritOpenTypeToggles(under, change);
|
|
382
|
-
|
|
403
|
+
const out = { ...style, ...(override ? inheritOpenTypeToggles(toggles, override) : toggles) };
|
|
404
|
+
if (nonFiniteSize)
|
|
405
|
+
NON_FINITE_SIZES.add(out);
|
|
406
|
+
return out;
|
|
383
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();
|
|
384
410
|
/**
|
|
385
411
|
* The variable data a run's `variableField` (`LINE_HEIGHT` or `FONT_SIZE`) is bound to, read where `styleFields` reads
|
|
386
412
|
* the field it binds (`name`): an override entry's own binding, or none when the entry sets its own value; else the text
|
|
@@ -511,6 +537,8 @@ function runStyle(style, lookups, report, where, boundLineHeight) {
|
|
|
511
537
|
const family = field(fontName, "family", "string", `${where} fontName`);
|
|
512
538
|
const styleName = field(fontName, "style", "string", `${where} fontName`) ?? "";
|
|
513
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`);
|
|
514
542
|
const { fontWeight: weight, italic } = fontStyleWeight(styleName);
|
|
515
543
|
if (weight === undefined)
|
|
516
544
|
report("approximated", `font style "${styleName}" has no known weight; it is imported as weight 400`);
|
|
@@ -623,7 +651,13 @@ export function textColor(paints, report = () => undefined, where = "text fill")
|
|
|
623
651
|
if (!isObject(paint) || field(paint, "visible", "boolean", where) === false)
|
|
624
652
|
continue;
|
|
625
653
|
const type = field(paint, "type", "string", where) ?? "no type";
|
|
626
|
-
const
|
|
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
|
+
}
|
|
627
661
|
if (color !== undefined || channels === null) {
|
|
628
662
|
report("property-left-out", `a ${type} text fill is left out: only the top solid fill is imported`);
|
|
629
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) =>
|
|
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;
|
|
@@ -37,6 +37,16 @@ export interface StoredFigmaBox {
|
|
|
37
37
|
}
|
|
38
38
|
/** The component bindings the model drops from a layer the override is applied to, so the override must write each again. */
|
|
39
39
|
export declare function bindingsDroppedBy(override: Readonly<Record<string, unknown>>): Set<string>;
|
|
40
|
+
/**
|
|
41
|
+
* `change` with the `overridden` fields merged in and without the component's bindings of the fields they change:
|
|
42
|
+
* Figma unbinds a field that an override sets, so the instance takes the override's value, not the component's variable.
|
|
43
|
+
*/
|
|
44
|
+
export declare function withOverrides(change: FigmaObject, overridden: Readonly<Record<string, unknown>>, replacesTextStyle: boolean): FigmaObject;
|
|
45
|
+
/**
|
|
46
|
+
* A `variableConsumptionMap` with the entries of an override's own map (`over`) in place of those of the same field in
|
|
47
|
+
* `map`, the component layer's.
|
|
48
|
+
*/
|
|
49
|
+
export declare function withBindingEntries(map: unknown, over: unknown): FigmaObject;
|
|
40
50
|
/**
|
|
41
51
|
* Writes `null` in each override for a binding the model would keep from a nested instance's own override (`mergeOverrides`
|
|
42
52
|
* in the model) of a field the override sets plainly. Runs once every instance has its overrides, since the nested instances
|
|
@@ -43,7 +43,7 @@ function overriddenBindingFields(name, value, before) {
|
|
|
43
43
|
* `change` with the `overridden` fields merged in and without the component's bindings of the fields they change:
|
|
44
44
|
* Figma unbinds a field that an override sets, so the instance takes the override's value, not the component's variable.
|
|
45
45
|
*/
|
|
46
|
-
function withOverrides(change, overridden, replacesTextStyle) {
|
|
46
|
+
export function withOverrides(change, overridden, replacesTextStyle) {
|
|
47
47
|
const merged = { ...change, ...overridden };
|
|
48
48
|
const unbound = new Set(Object.entries(overridden).flatMap(([name, value]) => overriddenBindingFields(name, value, change[name])));
|
|
49
49
|
if (replacesTextStyle)
|
|
@@ -110,7 +110,7 @@ function parameterMapExtras(parameterMap, variableMap) {
|
|
|
110
110
|
* A `variableConsumptionMap` with the entries of an override's own map (`over`) in place of those of the same field in
|
|
111
111
|
* `map`, the component layer's.
|
|
112
112
|
*/
|
|
113
|
-
function withBindingEntries(map, over) {
|
|
113
|
+
export function withBindingEntries(map, over) {
|
|
114
114
|
const entriesOf = (value) => isObject(value) && Array.isArray(value["entries"]) ? value["entries"] : [];
|
|
115
115
|
const fieldOf = (entry) => isObject(entry) ? entry["variableField"] : undefined;
|
|
116
116
|
const overEntries = entriesOf(over);
|
|
@@ -738,13 +738,13 @@ export function instanceFields(instance, component, self, context) {
|
|
|
738
738
|
const where = `node ${instance.id}`;
|
|
739
739
|
const overrides = {};
|
|
740
740
|
const overrideAt = (key) => (overrides[key] ??= {});
|
|
741
|
-
const
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
741
|
+
const assignedTexts = new Map();
|
|
742
|
+
// Each text property value at a path, for the texts bound to it that an override restyles. As in `propertyValues`,
|
|
743
|
+
// the last value wins, except that one passed down from a holder (`below`) yields to any other.
|
|
744
|
+
const captureTexts = (shown, path, below = false) => (id, data, text) => {
|
|
745
|
+
const key = `${path}/${id}`;
|
|
746
|
+
if (!below || !assignedTexts.has(key))
|
|
747
|
+
assignedTexts.set(key, { shown, path, id, text, data });
|
|
748
748
|
};
|
|
749
749
|
const componentIdOf = (symbolId) => context.component(symbolId)?.componentId;
|
|
750
750
|
const swapsKept = new Set();
|
|
@@ -1068,7 +1068,7 @@ export function instanceFields(instance, component, self, context) {
|
|
|
1068
1068
|
}
|
|
1069
1069
|
}
|
|
1070
1070
|
for (const [key, { shown: nested, list: values }] of below) {
|
|
1071
|
-
const read = assignedPropertyValues(values, nested.properties, nested.variantDefs, componentIdOf, leftOutValue(nested, key), at, context.report,
|
|
1071
|
+
const read = assignedPropertyValues(values, nested.properties, nested.variantDefs, componentIdOf, leftOutValue(nested, key), at, context.report, captureTexts(nested, key, true));
|
|
1072
1072
|
const override = overrideAt(key);
|
|
1073
1073
|
if (Object.keys(read).length > 0)
|
|
1074
1074
|
override["propertyValues"] = { ...read, ...override["propertyValues"] };
|
|
@@ -1284,7 +1284,7 @@ export function instanceFields(instance, component, self, context) {
|
|
|
1284
1284
|
insideLeftOut.add(entry);
|
|
1285
1285
|
if (shown === undefined || shown === INSIDE_LEFT_OUT)
|
|
1286
1286
|
continue;
|
|
1287
|
-
const values = assignedPropertyValues(assignNestedValues(field(entry, name, "list", at), shown, keys, at), shown.properties, shown.variantDefs, componentIdOf, leftOutValue(shown, target.key), at, context.report,
|
|
1287
|
+
const values = assignedPropertyValues(assignNestedValues(field(entry, name, "list", at), shown, keys, at), shown.properties, shown.variantDefs, componentIdOf, leftOutValue(shown, target.key), at, context.report, captureTexts(shown, target.key));
|
|
1288
1288
|
if (Object.keys(values).length > 0)
|
|
1289
1289
|
gathered.override["propertyValues"] = { ...gathered.override["propertyValues"], ...values };
|
|
1290
1290
|
// Each swap-bound layer the values reach shows the value over a swap made below this instance (`shownAt`).
|
|
@@ -1522,10 +1522,10 @@ export function instanceFields(instance, component, self, context) {
|
|
|
1522
1522
|
return found;
|
|
1523
1523
|
};
|
|
1524
1524
|
// The property values this instance gives its component, read here for the slot frames they show or hide.
|
|
1525
|
-
const propertyValues = assignedPropertyValues(assignNestedValues(field(instance.change, "componentPropAssignments", "list", where), component, [], where), component.properties, component.variantDefs, componentIdOf, leftOutValue(component, ""), where, context.report,
|
|
1526
|
-
for (const { shown, path, id, text, data } of
|
|
1525
|
+
const propertyValues = assignedPropertyValues(assignNestedValues(field(instance.change, "componentPropAssignments", "list", where), component, [], where), component.properties, component.variantDefs, componentIdOf, leftOutValue(component, ""), where, context.report, captureTexts(component, ""));
|
|
1526
|
+
for (const { shown, path, id, text, data } of assignedTexts.values()) {
|
|
1527
1527
|
const table = new Map();
|
|
1528
|
-
for (const row of data["styleOverrideTable"]) {
|
|
1528
|
+
for (const row of Array.isArray(data["styleOverrideTable"]) ? data["styleOverrideTable"] : []) {
|
|
1529
1529
|
if (isObject(row) && typeof row["styleID"] === "number" && row["styleID"] !== 0 && Object.hasOwn(row, "hyperlink"))
|
|
1530
1530
|
table.set(row["styleID"], hyperlinkOf(row["hyperlink"], context.report));
|
|
1531
1531
|
}
|
|
@@ -1538,13 +1538,14 @@ export function instanceFields(instance, component, self, context) {
|
|
|
1538
1538
|
if (!isObject(ref) || propertyByFigmaId(shown.properties, guidText(ref["defID"], where))?.definition.id !== id)
|
|
1539
1539
|
continue;
|
|
1540
1540
|
const key = layerPathKey([...prefix, node.id]);
|
|
1541
|
-
|
|
1542
|
-
//
|
|
1543
|
-
//
|
|
1544
|
-
|
|
1545
|
-
|
|
1541
|
+
// An override that restyles the text (its letter spacing or font, say, or a paint) can produce full paragraphs,
|
|
1542
|
+
// which carry the component's wording. Use their formatting for the assignment unless the occurrence actually
|
|
1543
|
+
// overrode its text body or links. Without them, only an assignment's links need writing.
|
|
1544
|
+
const fields = pending.get(key)?.fields, source = overrides[key]?.["paragraphs"];
|
|
1545
|
+
if (overrides[key]?.["characters"] !== undefined || (source === undefined ? table.size === 0
|
|
1546
|
+
: fields?.["textData"] !== undefined || fields?.["hyperlink"] !== undefined))
|
|
1546
1547
|
continue;
|
|
1547
|
-
const
|
|
1548
|
+
const at = overrideAt(key);
|
|
1548
1549
|
let i = 0, applied = false;
|
|
1549
1550
|
const paragraphs = paragraphsFromCharacters(text, source ?? node.paragraphs).map(paragraph => {
|
|
1550
1551
|
const runs = paragraph.runs.flatMap(run => {
|
|
@@ -1567,7 +1568,7 @@ export function instanceFields(instance, component, self, context) {
|
|
|
1567
1568
|
++i; // The paragraph break also consumes a Figma character style ID.
|
|
1568
1569
|
return { ...paragraph, runs };
|
|
1569
1570
|
});
|
|
1570
|
-
if (applied)
|
|
1571
|
+
if (applied || source !== undefined)
|
|
1571
1572
|
at["paragraphs"] = paragraphs;
|
|
1572
1573
|
}
|
|
1573
1574
|
}
|
|
@@ -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
|
-
|
|
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
|
|
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.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Editable SVG and Figma import for Moondraft.",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"license": "SEE LICENSE IN LICENSE",
|
|
39
39
|
"repository": {
|
|
40
40
|
"type": "git",
|
|
41
|
-
"url": "git+https://github.com/chengluyu/
|
|
41
|
+
"url": "git+https://github.com/chengluyu/moondraft.git",
|
|
42
42
|
"directory": "packages/import"
|
|
43
43
|
},
|
|
44
44
|
"files": [
|
|
@@ -52,6 +52,6 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@moondraft/editor": ">=0.
|
|
55
|
+
"@moondraft/editor": ">=0.6.0 <0.9.0"
|
|
56
56
|
}
|
|
57
57
|
}
|