@moondraft/import 0.2.0 → 0.4.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 +26 -0
- package/SOURCE.md +138 -1
- package/dist/adapter/convert.d.ts +2 -0
- package/dist/adapter/convert.js +192 -85
- package/dist/adapter/figma-handoff.d.ts +24 -2
- package/dist/adapter/figma-handoff.js +39 -18
- package/dist/adapter/figma.js +2 -1
- package/dist/adapter/file-components.d.ts +20 -1
- package/dist/adapter/file-components.js +91 -12
- package/dist/adapter/file-resources.d.ts +5 -0
- package/dist/adapter/file-resources.js +286 -40
- package/dist/adapter/file-typography.d.ts +6 -0
- package/dist/adapter/file-typography.js +44 -0
- package/dist/adapter/link.d.ts +8 -1
- package/dist/adapter/link.js +219 -105
- package/dist/adapter/payload.js +2 -2
- package/dist/adapter/variables.d.ts +1 -1
- package/dist/figma-file.d.ts +1 -0
- package/dist/figma-file.js +1 -0
- package/dist/figma.d.ts +1 -0
- package/dist/figma.js +41 -19
- package/dist/report.d.ts +37 -6
- package/dist/report.js +91 -18
- package/dist/taco/figma-import/figFile.js +3 -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.d.ts +3 -13
- package/dist/taco/figma-import/import/converters/effects.js +4 -34
- package/dist/taco/figma-import/import/converters/exportSettings.js +26 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/frame.js +8 -7
- package/dist/taco/figma-import/import/converters/grid.d.ts +12 -0
- package/dist/taco/figma-import/import/converters/grid.js +97 -0
- 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/image.d.ts +1 -0
- package/dist/taco/figma-import/import/converters/image.js +38 -5
- package/dist/taco/figma-import/import/converters/layout.d.ts +5 -3
- package/dist/taco/figma-import/import/converters/layout.js +26 -16
- package/dist/taco/figma-import/import/converters/rectangle.js +2 -2
- package/dist/taco/figma-import/import/converters/section.js +2 -2
- package/dist/taco/figma-import/import/converters/shared.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/shared.js +8 -5
- package/dist/taco/figma-import/import/converters/text.d.ts +24 -0
- package/dist/taco/figma-import/import/converters/text.js +121 -31
- package/dist/taco/figma-import/import/instances.d.ts +19 -3
- package/dist/taco/figma-import/import/instances.js +337 -60
- package/dist/taco/figma-import/import/types.d.ts +8 -0
- package/dist/taco/figma-import/import/variables.d.ts +2 -0
- package/dist/taco/figma-import/import/variables.js +28 -1
- package/dist/taco/figma-import/import/variantTrees.js +2 -0
- package/dist/taco/figma-import/import/walk.d.ts +9 -2
- package/dist/taco/figma-import/import/walk.js +69 -30
- package/dist/taco/figma-import/kiwi/message.js +52 -10
- package/dist/taco/model/codec.d.ts +1 -1
- package/dist/taco/model/codec.js +40 -6
- package/dist/taco/model/instance-resolver.js +3 -1
- package/dist/taco/model/types.d.ts +36 -6
- package/dist/taco/model/types.js +4 -0
- package/dist/taco/svg-import/style/computed.js +1 -0
- package/package.json +3 -3
package/dist/figma.js
CHANGED
|
@@ -4,14 +4,14 @@ import { base64 } from './adapter/figma.js';
|
|
|
4
4
|
import { fileComponents } from './adapter/file-components.js';
|
|
5
5
|
import { keepDescription } from './adapter/figma-descriptions.js';
|
|
6
6
|
import { figmaLayoutGrids, gridSources } from './adapter/figma-grids.js';
|
|
7
|
-
import { figmaHandoff, handoffSources } from './adapter/figma-handoff.js';
|
|
7
|
+
import { figmaHandoff, handoffOverrides, handoffSources } from './adapter/figma-handoff.js';
|
|
8
8
|
import { figmaHyperlinks } from './adapter/figma-hyperlinks.js';
|
|
9
9
|
import { isObject } from './taco/figma-import/import/fields.js';
|
|
10
10
|
import { sha256Hex } from './taco/model/sha256.js';
|
|
11
11
|
import { expandedRadiusRoots, fileResources } from './adapter/file-resources.js';
|
|
12
12
|
import { readFileMessage, walkMessage } from './taco/figma-import/import/walk.js';
|
|
13
13
|
import { allPages, figmaClipboardPage } from './adapter/convert.js';
|
|
14
|
-
import { convertEntries, droppedFeatures, figmaReportLayers, grouped, readerCount } from './report.js';
|
|
14
|
+
import { convertEntries, droppedFeatures, figmaReportLayers, fontEntries, grouped, leftOutIds, readerCount } from './report.js';
|
|
15
15
|
import { checkAbort } from './figma-file.js';
|
|
16
16
|
import { readFigFile, readFigmaClipboardHtml } from './taco/figma-import/index.js';
|
|
17
17
|
/**
|
|
@@ -25,9 +25,13 @@ export const MISSING_IMAGES = clipboard.MISSING_IMAGE_REPORT;
|
|
|
25
25
|
const STAND_IN = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
26
26
|
const ids = () => { let next = 0; return () => `f${++next}`; };
|
|
27
27
|
function reportOf(result, converted, features, extra, file = false) {
|
|
28
|
-
const { bySource, pages,
|
|
28
|
+
const { bySource, pages, derive } = figmaReportLayers(converted.doc, file);
|
|
29
29
|
const dropped = [];
|
|
30
|
-
for (const { figmaId, what } of features) {
|
|
30
|
+
for (const { figmaId, what, leftOut } of features) {
|
|
31
|
+
if (leftOut) {
|
|
32
|
+
dropped.push({ kind: 'left-out', what, count: 1 });
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
31
35
|
const layer = result.layerIds[figmaId];
|
|
32
36
|
if (layer === undefined)
|
|
33
37
|
continue;
|
|
@@ -39,9 +43,9 @@ function reportOf(result, converted, features, extra, file = false) {
|
|
|
39
43
|
const layer = result.layerIds[entry.nodeId];
|
|
40
44
|
const pageId = pageIds.get(entry.nodeId);
|
|
41
45
|
const ids = pageId ? [pageId] : layer === undefined ? undefined : bySource.get(layer);
|
|
42
|
-
return { kind: entry.kind === 'approximated' ? 'approximated' : 'left-out', ...readerCount(entry
|
|
46
|
+
return { kind: entry.kind === 'approximated' ? 'approximated' : 'left-out', ...readerCount(entry), ...(ids && { ids }) };
|
|
43
47
|
});
|
|
44
|
-
return { report:
|
|
48
|
+
return { report: [...reader, ...dropped, ...convertEntries(converted.report, true), ...extra], derive };
|
|
45
49
|
}
|
|
46
50
|
/**
|
|
47
51
|
* Imports Figma clipboard HTML as editable layers.
|
|
@@ -50,14 +54,15 @@ function reportOf(result, converted, features, extra, file = false) {
|
|
|
50
54
|
* @throws An error if the input contains no readable Figma layers.
|
|
51
55
|
*/
|
|
52
56
|
export function readFigmaClipboard(html) {
|
|
53
|
-
const { meta, message } = readFigmaClipboardHtml(html)
|
|
57
|
+
const { meta, message } = readFigmaClipboardHtml(html);
|
|
54
58
|
const { result, sources } = walkMessage(message, {
|
|
55
59
|
idSource: ids(), roots: 'clipboard-selection', bindVariables: false, textStyles: false,
|
|
56
|
-
images: (
|
|
60
|
+
images: () => ({ bytes: STAND_IN, mimeType: 'image/png', placeholder: true }),
|
|
57
61
|
}, false, tree => new Set([...handoffSources(tree), ...gridSources(tree)]));
|
|
58
62
|
const instanceSources = new Map();
|
|
59
63
|
const converted = figmaClipboardPage(figmaDocument(result, 'Pasted'), instanceSources);
|
|
60
|
-
const
|
|
64
|
+
const leftOut = leftOutIds(sources);
|
|
65
|
+
const extra = figmaHandoff(result, sources, converted.doc, { instanceSources, leftOut, overrides: handoffOverrides(sources.tree) });
|
|
61
66
|
extra.push(...figmaHyperlinks(result, sources, converted.doc, { instanceSources }));
|
|
62
67
|
let fileKey = meta.fileKey;
|
|
63
68
|
for (const entry of figmaLayoutGrids(sources, converted.doc, { instanceSources,
|
|
@@ -78,15 +83,16 @@ export function readFigmaClipboard(html) {
|
|
|
78
83
|
keepDescription(source, model, extra);
|
|
79
84
|
}
|
|
80
85
|
const { payload } = toPayload(converted);
|
|
81
|
-
const assetIds =
|
|
86
|
+
const assetIds = Object.values(result.assets).filter(a => a.placeholder && Object.hasOwn(payload.assets, a.id)).map(a => a.id);
|
|
82
87
|
for (const id of assetIds)
|
|
83
88
|
delete payload.assets[id];
|
|
84
89
|
if (assetIds.length)
|
|
85
90
|
extra.push({ kind: 'left-out', what: MISSING_IMAGES, count: assetIds.length, assetIds });
|
|
86
|
-
return { payload, report: reportOf(result, converted, droppedFeatures(message.nodeChanges), extra).report };
|
|
91
|
+
return { payload, report: grouped(reportOf(result, converted, droppedFeatures(message.nodeChanges, leftOut), extra).report) };
|
|
87
92
|
}
|
|
88
93
|
/**
|
|
89
94
|
* Reads a saved Figma design file on the calling thread.
|
|
95
|
+
* Values in the result may be shared between layers; copy before mutating in place.
|
|
90
96
|
* @param bytes - Saved Figma design file.
|
|
91
97
|
* @param options - Cancellation and progress options.
|
|
92
98
|
* @returns All public pages in file order, editable component and resource records, images and a report of font faces and conversion losses.
|
|
@@ -113,14 +119,23 @@ export function readFigmaFile(bytes, options = {}) {
|
|
|
113
119
|
for (const model of doc.models)
|
|
114
120
|
if (model.kind === 'frame' && model.overrides)
|
|
115
121
|
model.overrides = core.doc.owned(model.overrides, undefined, true);
|
|
116
|
-
core.
|
|
117
|
-
|
|
122
|
+
core.prepareImportLoad(doc)();
|
|
123
|
+
// A main's typography binding over a range that crosses an instance's own text runs stays a value there (issue 867).
|
|
124
|
+
for (const { id, fields } of core.keptTypography())
|
|
125
|
+
for (const field of fields)
|
|
126
|
+
report.push({ kind: 'approximated', what: `typography binding kept as value for text field "${field}"`, count: 1, ids: [id] });
|
|
127
|
+
document = core.releaseJSON();
|
|
118
128
|
}
|
|
119
129
|
// Only surviving assets need data URLs. Keep their encoded strings out of the layout and linking passes.
|
|
120
|
-
for (const [id, asset] of Object.entries(document.assets ?? {}))
|
|
121
|
-
|
|
130
|
+
for (const [id, asset] of Object.entries(document.assets ?? {})) {
|
|
131
|
+
const media = images[id];
|
|
132
|
+
const pixels = asset.mediaType === 'video' ? media : media.poster ?? media;
|
|
133
|
+
asset.src += base64(pixels.bytes);
|
|
134
|
+
if (asset.mediaType === 'video' && media.poster)
|
|
135
|
+
asset.poster = `data:${media.poster.mimeType};base64,${base64(media.poster.bytes)}`;
|
|
136
|
+
}
|
|
122
137
|
checkpoint('convert', total, total);
|
|
123
|
-
return { document, report };
|
|
138
|
+
return { document, report: grouped([...report, ...fontEntries(document)]) };
|
|
124
139
|
}
|
|
125
140
|
// Derived instance entries describe Figma's layout. Their path, box and stored line
|
|
126
141
|
// heights are the inputs to instanceFields. Authored overrides are read separately.
|
|
@@ -186,7 +201,10 @@ function convertFile(bytes, decoded) {
|
|
|
186
201
|
yield change;
|
|
187
202
|
}
|
|
188
203
|
}
|
|
189
|
-
|
|
204
|
+
// Read before `sources` releases the instances' overrides.
|
|
205
|
+
const overrides = handoffOverrides(result.sources.tree);
|
|
206
|
+
const leftOut = leftOutIds(result.sources);
|
|
207
|
+
const features = [...droppedFeatures(sources(), leftOut)];
|
|
190
208
|
const geometry = figmaDocument(result, undefined, () => '');
|
|
191
209
|
if (result.sources.variables)
|
|
192
210
|
geometry.variableAliases = result.sources.variables.aliases;
|
|
@@ -201,7 +219,11 @@ function convertFile(bytes, decoded) {
|
|
|
201
219
|
assetSrc: (_id, asset) => `data:${asset.mimeType};base64,`,
|
|
202
220
|
});
|
|
203
221
|
const extra = [];
|
|
204
|
-
|
|
222
|
+
// Roots that bind their own radii stay expanded before linking (convert.ts leaves them unmarked).
|
|
223
|
+
for (const m of converted.doc.models)
|
|
224
|
+
if (m.fileInstance !== undefined && expandedRadii.has(m.fileInstance))
|
|
225
|
+
converted.expandedReasons.set(m.id, 'root radius bindings');
|
|
226
|
+
const { occurrences, setSources } = fileComponents(result, converted.doc, converted.nestedBooleans, extra, converted.expandedReasons);
|
|
205
227
|
let fileKey = key;
|
|
206
228
|
const fileId = () => String(fileKey ||= sha256Hex(bytes));
|
|
207
229
|
fileResources(result, converted.doc, occurrences, converted.expanded, expandedRadii, extra, fileId);
|
|
@@ -216,7 +238,7 @@ function convertFile(bytes, decoded) {
|
|
|
216
238
|
}
|
|
217
239
|
for (const entry of figmaHyperlinks(result, result.sources, converted.doc, { setSources }))
|
|
218
240
|
extra.push(entry);
|
|
219
|
-
for (const entry of figmaHandoff(result, result.sources, converted.doc, { setSources }))
|
|
241
|
+
for (const entry of figmaHandoff(result, result.sources, converted.doc, { setSources, leftOut, overrides }))
|
|
220
242
|
extra.push(entry);
|
|
221
243
|
for (const entry of figmaLayoutGrids(result.sources, converted.doc, { setSources, fileId }))
|
|
222
244
|
extra.push(entry);
|
package/dist/report.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { ImportReportEntry } from '@moondraft/editor/core';
|
|
2
2
|
import type { DocumentJSON } from '@moondraft/editor/schema';
|
|
3
3
|
import type { ConvertReport } from './adapter/convert.ts';
|
|
4
|
+
import type { FigmaTreeNode } from './taco/figma-import/import/tree.ts';
|
|
4
5
|
/** The most layer IDs a grouped entry keeps as examples. */
|
|
5
6
|
export declare const EXAMPLE_IDS = 5;
|
|
6
|
-
/**
|
|
7
|
-
export declare function readerCount(
|
|
7
|
+
/** A reader entry's description and its occurrence count, which the reader gives as a field when it is more than one. */
|
|
8
|
+
export declare function readerCount(entry: {
|
|
9
|
+
reason: string;
|
|
10
|
+
count?: number;
|
|
11
|
+
}): {
|
|
8
12
|
what: string;
|
|
9
13
|
count: number;
|
|
10
14
|
};
|
|
@@ -18,10 +22,38 @@ export declare function grouped(entries: Iterable<ImportReportEntry>): ImportRep
|
|
|
18
22
|
export declare const DROPPED_FEATURES: {
|
|
19
23
|
readonly interactions: 'prototype interactions are not imported';
|
|
20
24
|
};
|
|
21
|
-
/**
|
|
22
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Describes what a Figma layer the import left out loses with it. That layer has no layer of its own in the result to
|
|
27
|
+
* point to, so the description names it.
|
|
28
|
+
* @param what - What is lost.
|
|
29
|
+
* @param name - The Figma layer's name, or its type when it has none.
|
|
30
|
+
* @returns The description.
|
|
31
|
+
*/
|
|
32
|
+
export declare const onLeftOutLayer: (what: string, name: string) => string;
|
|
33
|
+
/**
|
|
34
|
+
* Lists the Figma nodes the import left out with no imported copy. The reader's omitted subtrees also hold content
|
|
35
|
+
* that is imported elsewhere, such as a slot's content frame, which is imported as the content of the instances that
|
|
36
|
+
* fill the slot; a node that is imported, or holds a node that is, is not left out.
|
|
37
|
+
* @param sources - The reader's tree, its omitted subtrees and the nodes it imported.
|
|
38
|
+
* @returns The Figma IDs of the left-out nodes.
|
|
39
|
+
*/
|
|
40
|
+
export declare function leftOutIds(sources: {
|
|
41
|
+
tree: {
|
|
42
|
+
nodes: ReadonlyMap<string, FigmaTreeNode>;
|
|
43
|
+
};
|
|
44
|
+
omittedSubtrees: Iterable<FigmaTreeNode>;
|
|
45
|
+
imported: Iterable<{
|
|
46
|
+
figma: FigmaTreeNode;
|
|
47
|
+
}>;
|
|
48
|
+
}): Set<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Unsupported features in source order. Linking supplies their final layer IDs later, except on left-out layers,
|
|
51
|
+
* whose entries name the layer instead.
|
|
52
|
+
*/
|
|
53
|
+
export declare function droppedFeatures(changes: Iterable<unknown> | undefined, leftOut: ReadonlySet<string>): Generator<{
|
|
23
54
|
figmaId: string;
|
|
24
|
-
|
|
55
|
+
leftOut: boolean;
|
|
56
|
+
what: string;
|
|
25
57
|
}, void, unknown>;
|
|
26
58
|
/** Builds a lookup from reader IDs to their converted layers. */
|
|
27
59
|
export declare function layersBySource(document: DocumentJSON): Map<string, string[]>;
|
|
@@ -29,7 +61,6 @@ export declare function layersBySource(document: DocumentJSON): Map<string, stri
|
|
|
29
61
|
export declare function figmaReportLayers(document: DocumentJSON, file: boolean): {
|
|
30
62
|
bySource: Map<string, string[]>;
|
|
31
63
|
pages: string[];
|
|
32
|
-
fonts: ImportReportEntry[];
|
|
33
64
|
derive: boolean;
|
|
34
65
|
};
|
|
35
66
|
/** Turns adapter diagnostics into import reports. */
|
package/dist/report.js
CHANGED
|
@@ -5,11 +5,45 @@ const LISTED = 3;
|
|
|
5
5
|
// The parts of a description that vary between entries of one kind: quoted names (with escaped quotes inside), Figma
|
|
6
6
|
// node IDs, numbers and Figma's upper-case enum values ("fill 2 blend mode LINEAR_BURN", "override field \"size\"").
|
|
7
7
|
// Entries that differ only in these parts group.
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
const QUOTED = /"(?:[^"\\]|\\.)*"/y;
|
|
9
|
+
const UNQUOTED = /\b\d+:\d+\b|-?\b\d+(?:\.\d+)?\b|\b[A-Z][A-Z\d]*_[A-Z\d_]*\b|\b[A-Z]{2,}[A-Z\d]*\b/g;
|
|
10
|
+
/**
|
|
11
|
+
* Splits a description into its varying parts and the literal text around them (`literals` has one more item). A
|
|
12
|
+
* quote with no closing quote after it is literal text, and so is every quote after it, so the scan stays linear.
|
|
13
|
+
*/
|
|
14
|
+
function varying(what) {
|
|
15
|
+
const literals = [], parts = [];
|
|
16
|
+
let literal = '', at = 0;
|
|
17
|
+
const unquoted = (end) => {
|
|
18
|
+
const text = what.slice(at, end);
|
|
19
|
+
let last = 0;
|
|
20
|
+
for (const m of text.matchAll(UNQUOTED)) {
|
|
21
|
+
literals.push(literal + text.slice(last, m.index));
|
|
22
|
+
parts.push(m[0]);
|
|
23
|
+
literal = '';
|
|
24
|
+
last = m.index + m[0].length;
|
|
25
|
+
}
|
|
26
|
+
literal += text.slice(last);
|
|
27
|
+
at = end;
|
|
28
|
+
};
|
|
29
|
+
for (let quote = what.indexOf('"'); quote !== -1; quote = what.indexOf('"', at)) {
|
|
30
|
+
QUOTED.lastIndex = quote;
|
|
31
|
+
const m = QUOTED.exec(what);
|
|
32
|
+
if (!m)
|
|
33
|
+
break;
|
|
34
|
+
unquoted(quote);
|
|
35
|
+
literals.push(literal);
|
|
36
|
+
parts.push(m[0]);
|
|
37
|
+
literal = '';
|
|
38
|
+
at = quote + m[0].length;
|
|
39
|
+
}
|
|
40
|
+
unquoted(what.length);
|
|
41
|
+
literals.push(literal);
|
|
42
|
+
return { literals, parts };
|
|
43
|
+
}
|
|
44
|
+
/** A reader entry's description and its occurrence count, which the reader gives as a field when it is more than one. */
|
|
45
|
+
export function readerCount(entry) {
|
|
46
|
+
return { what: entry.reason, count: entry.count ?? 1 };
|
|
13
47
|
}
|
|
14
48
|
/** Lists a part's distinct values: all of them up to `LISTED`, then the first ones and how many more. */
|
|
15
49
|
function listed(values) {
|
|
@@ -23,9 +57,9 @@ function listed(values) {
|
|
|
23
57
|
export function grouped(entries) {
|
|
24
58
|
const out = new Map();
|
|
25
59
|
for (const e of entries) {
|
|
26
|
-
const
|
|
27
|
-
const key = `${e.kind}\u0000${
|
|
28
|
-
const parts =
|
|
60
|
+
const split = e.kind === 'font' ? null : varying(e.what);
|
|
61
|
+
const key = `${e.kind}\u0000${split ? split.literals.join('\u0001') : e.what}`, had = out.get(key);
|
|
62
|
+
const parts = split && split.parts;
|
|
29
63
|
if (had) {
|
|
30
64
|
had.entry.count += e.count;
|
|
31
65
|
if (e.assetIds)
|
|
@@ -41,8 +75,8 @@ export function grouped(entries) {
|
|
|
41
75
|
}
|
|
42
76
|
return [...out.values()].map(({ entry, ids, parts }) => {
|
|
43
77
|
if (parts?.some(values => values.size > 1)) {
|
|
44
|
-
|
|
45
|
-
entry.what =
|
|
78
|
+
const { literals } = varying(entry.what);
|
|
79
|
+
entry.what = literals.map((text, i) => i < parts.length ? text + listed([...parts[i]]) : text).join('');
|
|
46
80
|
}
|
|
47
81
|
if (entry.assetIds)
|
|
48
82
|
entry.assetIds = [...new Set(entry.assetIds)];
|
|
@@ -59,14 +93,48 @@ export const DROPPED_FEATURES = {
|
|
|
59
93
|
};
|
|
60
94
|
const present = (value) => Array.isArray(value) ? value.some(item => item?.isDeleted !== true) : typeof value === 'string' && value.trim() !== '';
|
|
61
95
|
const guidOf = (value) => Number.isInteger(value?.sessionID) && Number.isInteger(value?.localID) ? `${value.sessionID}:${value.localID}` : undefined;
|
|
62
|
-
/**
|
|
63
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Describes what a Figma layer the import left out loses with it. That layer has no layer of its own in the result to
|
|
98
|
+
* point to, so the description names it.
|
|
99
|
+
* @param what - What is lost.
|
|
100
|
+
* @param name - The Figma layer's name, or its type when it has none.
|
|
101
|
+
* @returns The description.
|
|
102
|
+
*/
|
|
103
|
+
export const onLeftOutLayer = (what, name) => `${what} for the left-out layer ${JSON.stringify(name)}`;
|
|
104
|
+
/**
|
|
105
|
+
* Lists the Figma nodes the import left out with no imported copy. The reader's omitted subtrees also hold content
|
|
106
|
+
* that is imported elsewhere, such as a slot's content frame, which is imported as the content of the instances that
|
|
107
|
+
* fill the slot; a node that is imported, or holds a node that is, is not left out.
|
|
108
|
+
* @param sources - The reader's tree, its omitted subtrees and the nodes it imported.
|
|
109
|
+
* @returns The Figma IDs of the left-out nodes.
|
|
110
|
+
*/
|
|
111
|
+
export function leftOutIds(sources) {
|
|
112
|
+
const ids = new Set(), pending = [...sources.omittedSubtrees];
|
|
113
|
+
while (pending.length) {
|
|
114
|
+
const node = pending.pop();
|
|
115
|
+
if (ids.has(node.id))
|
|
116
|
+
continue;
|
|
117
|
+
ids.add(node.id);
|
|
118
|
+
pending.push(...node.children);
|
|
119
|
+
}
|
|
120
|
+
for (const { figma } of sources.imported)
|
|
121
|
+
for (let at = figma; at && ids.delete(at.id); at = at.parentId === undefined ? undefined : sources.tree.nodes.get(at.parentId))
|
|
122
|
+
;
|
|
123
|
+
return ids;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Unsupported features in source order. Linking supplies their final layer IDs later, except on left-out layers,
|
|
127
|
+
* whose entries name the layer instead.
|
|
128
|
+
*/
|
|
129
|
+
export function* droppedFeatures(changes, leftOut) {
|
|
64
130
|
for (const change of changes ?? []) {
|
|
65
131
|
const n = change, figmaId = guidOf(n?.guid);
|
|
66
132
|
if (figmaId === undefined)
|
|
67
133
|
continue;
|
|
134
|
+
const named = leftOut.has(figmaId);
|
|
68
135
|
if (present(n.prototypeInteractions))
|
|
69
|
-
yield { figmaId,
|
|
136
|
+
yield { figmaId, leftOut: named,
|
|
137
|
+
what: named ? onLeftOutLayer(DROPPED_FEATURES.interactions, typeof n.name === 'string' && n.name ? n.name : String(n.type)) : DROPPED_FEATURES.interactions };
|
|
70
138
|
}
|
|
71
139
|
}
|
|
72
140
|
/** Builds a lookup from reader IDs to their converted layers. */
|
|
@@ -87,20 +155,25 @@ function indexLayer(ids, model) {
|
|
|
87
155
|
}
|
|
88
156
|
/** Collect file diagnostics and strip reader IDs before handing the models to the editor. */
|
|
89
157
|
export function figmaReportLayers(document, file) {
|
|
90
|
-
const bySource = new Map(), pages = []
|
|
158
|
+
const bySource = new Map(), pages = [];
|
|
91
159
|
let derive = !!document.shared;
|
|
92
|
-
|
|
160
|
+
const models = document.models;
|
|
161
|
+
for (const [i, layer] of models.entries()) {
|
|
162
|
+
let model = layer;
|
|
93
163
|
indexLayer(bySource, model);
|
|
94
164
|
if (model.kind === 'page')
|
|
95
165
|
pages.push(model.id);
|
|
96
166
|
if (file) {
|
|
97
|
-
|
|
98
|
-
|
|
167
|
+
// A copy without the reader ID: deleting a field leaves V8 a slow, larger hash table for the record.
|
|
168
|
+
if (Object.hasOwn(model, 'sourceId')) {
|
|
169
|
+
const { sourceId: _, ...rest } = model;
|
|
170
|
+
models[i] = model = rest;
|
|
171
|
+
}
|
|
99
172
|
if (model.kind === 'frame' && (model.variantSet || model.props))
|
|
100
173
|
derive = true;
|
|
101
174
|
}
|
|
102
175
|
}
|
|
103
|
-
return { bySource, pages,
|
|
176
|
+
return { bySource, pages, derive };
|
|
104
177
|
}
|
|
105
178
|
/** Turns adapter diagnostics into import reports. */
|
|
106
179
|
export function convertEntries(report, details = false) {
|
|
@@ -40,6 +40,9 @@ export function readFigFile(bytes, options = {}) {
|
|
|
40
40
|
}
|
|
41
41
|
const images = new Map();
|
|
42
42
|
for (const [name, data] of entries) {
|
|
43
|
+
const video = /^videos\/([0-9a-f]{40})(?:\.(?:mp4|webm|mov))?$/i.exec(name);
|
|
44
|
+
if (video)
|
|
45
|
+
images.set(video[1].toLowerCase(), data);
|
|
43
46
|
if (name.startsWith(IMAGES_FOLDER) && name.length > IMAGES_FOLDER.length && !name.endsWith("/")) {
|
|
44
47
|
images.set(name.slice(IMAGES_FOLDER.length), data);
|
|
45
48
|
}
|
|
@@ -17,7 +17,17 @@ export declare function withImageCrop(_key: string, value: unknown): unknown;
|
|
|
17
17
|
/** Moves a mark to a copy of the marked object. */
|
|
18
18
|
export declare function moveMark(from: object, to: object): void;
|
|
19
19
|
/** Adds `data` to the layer's `metadata.figma`. */
|
|
20
|
-
export declare function carry(node:
|
|
20
|
+
export declare function carry(node: {
|
|
21
|
+
metadata?: SceneNode["metadata"];
|
|
22
|
+
}, data: NonNullable<SceneNode["metadata"]>): void;
|
|
23
|
+
/** Carries the shadows and blurs of a text layer or group (`metadata.figma.effects`), with their shadow marks. */
|
|
24
|
+
export declare function carryEffects<T extends {
|
|
25
|
+
metadata?: SceneNode["metadata"];
|
|
26
|
+
}>(node: T, effects: {
|
|
27
|
+
readonly shadows?: readonly object[];
|
|
28
|
+
readonly layerBlur?: number;
|
|
29
|
+
readonly backgroundBlur?: number;
|
|
30
|
+
}): T;
|
|
21
31
|
/** Clears a layer's constraints where Figma does not apply them. */
|
|
22
32
|
export declare function clearConstraints(node: SceneNode): void;
|
|
23
33
|
/** Carries the marks on a converted layer's shadows and fills into its metadata. */
|
|
@@ -36,6 +36,14 @@ export function carry(node, data) {
|
|
|
36
36
|
const had = typeof figma === "object" && figma !== null && !Array.isArray(figma) ? figma : {};
|
|
37
37
|
Object.assign(node, { metadata: { ...metadata, figma: { ...had, ...data } } });
|
|
38
38
|
}
|
|
39
|
+
/** Carries the shadows and blurs of a text layer or group (`metadata.figma.effects`), with their shadow marks. */
|
|
40
|
+
export function carryEffects(node, effects) {
|
|
41
|
+
if (Object.keys(effects).length === 0)
|
|
42
|
+
return node;
|
|
43
|
+
const behind = (effects.shadows ?? []).flatMap((shadow, index) => marks.get(shadow)?.behind === true ? [index] : []);
|
|
44
|
+
carry(node, { effects: JSON.parse(JSON.stringify(effects)), ...(behind.length > 0 ? { shadowBehind: behind } : {}) });
|
|
45
|
+
return node;
|
|
46
|
+
}
|
|
39
47
|
/** Clears a layer's constraints where Figma does not apply them. */
|
|
40
48
|
export function clearConstraints(node) {
|
|
41
49
|
if ("constraints" in node)
|
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
import type { VisualStyle } from "../../../model/index.ts";
|
|
2
2
|
import type { FigmaTreeNode } from "../tree.ts";
|
|
3
3
|
import type { NodeWalk } from "../walk.ts";
|
|
4
|
-
/**
|
|
5
|
-
* True when the renderer draws a node with `style` as a plain rectangle, the
|
|
6
|
-
* only shape whose drop shadow it shrinks for a negative spread
|
|
7
|
-
* (`drawShadowedRect` in packages/render-core/cpp/skia/skia_adapter.cc); every
|
|
8
|
-
* other shape's drop shadow ignores a negative spread (`buildPreparedShadows`).
|
|
9
|
-
* Frames are drawn so when their corners are square; rectangles also need at
|
|
10
|
-
* most one plain solid fill or a stroke with unequal per-side weights, or they are drawn as a path
|
|
11
|
-
* (`compileRectangle` and `needsRegionFillPaints` in packages/render-core/src/page-compiler.ts).
|
|
12
|
-
*/
|
|
13
|
-
export declare function drawnAsPlainRectangle(style: VisualStyle, isFrame: boolean): boolean;
|
|
14
4
|
/**
|
|
15
5
|
* The node's Figma `effects` as Taco shadows, layer blur and background blur,
|
|
16
6
|
* shadows in Figma's order; a node with an effect style takes the style's
|
|
@@ -25,7 +15,7 @@ export declare function drawnAsPlainRectangle(style: VisualStyle, isFrame: boole
|
|
|
25
15
|
* (src/converter/style.py, `convert_effects`), absent offset, radius and
|
|
26
16
|
* spread are 0. Taco has no hidden shadow or blur, one layer blur and one
|
|
27
17
|
* background blur, and at most 16 shadows; what does not fit is reported.
|
|
28
|
-
*
|
|
29
|
-
*
|
|
18
|
+
* Moondraft patch: a negative spread is kept on every shape; the renderer
|
|
19
|
+
* shrinks drop shadows and grows inner shadows' holes by it.
|
|
30
20
|
*/
|
|
31
|
-
export declare function nodeEffects(node: FigmaTreeNode, walk: NodeWalk
|
|
21
|
+
export declare function nodeEffects(node: FigmaTreeNode, walk: NodeWalk): Pick<VisualStyle, "shadows" | "layerBlur" | "backgroundBlur">;
|
|
@@ -7,27 +7,6 @@ const MAX_SHADOWS = 16;
|
|
|
7
7
|
// Prototype-free, so an effect type such as "constructor" finds no entry.
|
|
8
8
|
const SHADOW_KINDS = Object.assign(Object.create(null), { DROP_SHADOW: "drop", INNER_SHADOW: "inner" });
|
|
9
9
|
const BLUR_KINDS = Object.assign(Object.create(null), { FOREGROUND_BLUR: "layerBlur", BACKGROUND_BLUR: "backgroundBlur" });
|
|
10
|
-
/**
|
|
11
|
-
* True when the renderer draws a node with `style` as a plain rectangle, the
|
|
12
|
-
* only shape whose drop shadow it shrinks for a negative spread
|
|
13
|
-
* (`drawShadowedRect` in packages/render-core/cpp/skia/skia_adapter.cc); every
|
|
14
|
-
* other shape's drop shadow ignores a negative spread (`buildPreparedShadows`).
|
|
15
|
-
* Frames are drawn so when their corners are square; rectangles also need at
|
|
16
|
-
* most one plain solid fill or a stroke with unequal per-side weights, or they are drawn as a path
|
|
17
|
-
* (`compileRectangle` and `needsRegionFillPaints` in packages/render-core/src/page-compiler.ts).
|
|
18
|
-
*/
|
|
19
|
-
export function drawnAsPlainRectangle(style, isFrame) {
|
|
20
|
-
const square = (style.cornerRadius ?? 0) === 0 && Object.values(style.cornerRadii ?? {}).every((radius) => radius === 0);
|
|
21
|
-
if (!square || isFrame)
|
|
22
|
-
return square;
|
|
23
|
-
// A stroke with unequal per-side weights makes the rectangle a container, whatever its fills (`hasSidedStroke`).
|
|
24
|
-
const sidedStroke = style.strokes?.some(({ sides }) => sides !== undefined
|
|
25
|
-
&& !(sides.right === sides.top && sides.bottom === sides.top && sides.left === sides.top)) ?? false;
|
|
26
|
-
if (sidedStroke)
|
|
27
|
-
return true;
|
|
28
|
-
const fills = style.fills ?? [];
|
|
29
|
-
return fills.length <= 1 && fills.every((paint) => paint.kind === "solid" && paint.visible !== false && (paint.blendMode ?? "normal") === "normal");
|
|
30
|
-
}
|
|
31
10
|
/**
|
|
32
11
|
* The node's Figma `effects` as Taco shadows, layer blur and background blur,
|
|
33
12
|
* shadows in Figma's order; a node with an effect style takes the style's
|
|
@@ -42,10 +21,10 @@ export function drawnAsPlainRectangle(style, isFrame) {
|
|
|
42
21
|
* (src/converter/style.py, `convert_effects`), absent offset, radius and
|
|
43
22
|
* spread are 0. Taco has no hidden shadow or blur, one layer blur and one
|
|
44
23
|
* background blur, and at most 16 shadows; what does not fit is reported.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
24
|
+
* Moondraft patch: a negative spread is kept on every shape; the renderer
|
|
25
|
+
* shrinks drop shadows and grows inner shadows' holes by it.
|
|
47
26
|
*/
|
|
48
|
-
export function nodeEffects(node, walk
|
|
27
|
+
export function nodeEffects(node, walk) {
|
|
49
28
|
const where = `node ${node.id}`;
|
|
50
29
|
const shadows = [];
|
|
51
30
|
const boundZeroBlurs = new Map();
|
|
@@ -91,7 +70,7 @@ export function nodeEffects(node, walk, plainRectangle = false) {
|
|
|
91
70
|
const offset = field(effect, "offset", "object", at) ?? {};
|
|
92
71
|
const x = field(offset, "x", "number", `${at} offset`) ?? 0;
|
|
93
72
|
const y = field(offset, "y", "number", `${at} offset`) ?? 0;
|
|
94
|
-
|
|
73
|
+
const spread = field(effect, "spread", "number", at) ?? 0;
|
|
95
74
|
const colorObject = field(effect, "color", "object", at);
|
|
96
75
|
const color = colorObject && readNumbers(colorObject, ["r", "g", "b", "a"], `${at} color`);
|
|
97
76
|
if (!color)
|
|
@@ -101,15 +80,6 @@ export function nodeEffects(node, walk, plainRectangle = false) {
|
|
|
101
80
|
// Moondraft patch (../carry.ts): Moondraft knocks a drop shadow out under the node's fill, as Figma does unless
|
|
102
81
|
// showShadowBehindNode is set; such a shadow is marked, and the see-through fill is no longer reported.
|
|
103
82
|
const behind = shadowKind === "drop" && field(effect, "showShadowBehindNode", "boolean", at) === true;
|
|
104
|
-
if (shadowKind === "inner" && spread < 0) {
|
|
105
|
-
// The renderer grows an inner shadow only inward and treats a negative spread as 0.
|
|
106
|
-
walk.report("approximated", `${label} (${type}) spread ${String(spread)} is below 0; Taco draws inner shadows with spread 0`);
|
|
107
|
-
spread = 0;
|
|
108
|
-
}
|
|
109
|
-
if (shadowKind === "drop" && spread < 0 && !plainRectangle) {
|
|
110
|
-
walk.report("approximated", `${label} (${type}) spread ${String(spread)} is below 0; Taco shrinks drop shadows only on square-cornered rectangles and frames, so spread 0 is used`);
|
|
111
|
-
spread = 0;
|
|
112
|
-
}
|
|
113
83
|
const shadow = { kind: shadowKind, x, y, blur: radius, spread, color: hexColor(color) };
|
|
114
84
|
if (behind)
|
|
115
85
|
markShadowBehind(shadow);
|
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
import { defaultExportQuality, exportConstraintProblem, exportSuffixProblem, MAX_EXPORT_SETTINGS } from "../../../model/export-settings.js";
|
|
2
2
|
import { isObject } from "../fields.js";
|
|
3
|
+
/**
|
|
4
|
+
* Figma row fields with no counterpart in a Taco row, each with the formats it
|
|
5
|
+
* affects and the value that changes the file: a row whose field holds
|
|
6
|
+
* another value (Figma's own default included) exports as Taco would anyway.
|
|
7
|
+
*/
|
|
8
|
+
const UNKEPT_FIELDS = [
|
|
9
|
+
{ field: "svgOutlineText", formats: ["svg"], kept: (value) => value !== false },
|
|
10
|
+
{ field: "svgDataName", formats: ["svg"], kept: (value) => value !== true },
|
|
11
|
+
{ field: "svgForceStrokeMasks", formats: ["svg"], kept: (value) => value !== true },
|
|
12
|
+
{ field: "useAbsoluteBounds", formats: ["png", "jpeg", "svg", "pdf"], kept: (value) => value !== true },
|
|
13
|
+
{ field: "colorProfile", formats: ["png", "jpeg", "svg", "pdf"], kept: (value) => value === undefined || value === "DOCUMENT" },
|
|
14
|
+
{ field: "quality", formats: ["jpeg", "pdf"], kept: (value) => value === undefined },
|
|
15
|
+
{ field: "useBicubicSampler", formats: ["png", "jpeg", "pdf"], kept: (value) => value !== false },
|
|
16
|
+
{ field: "pdfPrint", formats: ["pdf"], kept: (value) => value !== true },
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Rows whose unkept fields are reported: the editor keeps the first 32 rows,
|
|
20
|
+
* and the adapter reports each later row whole, so its fields are not
|
|
21
|
+
* reported again.
|
|
22
|
+
*/
|
|
23
|
+
const FIELD_REPORTED_ROWS = 32;
|
|
3
24
|
/** Reads the export rows stored by a layer or by an instance's root override. */
|
|
4
25
|
export function exportSettingsOf(value, report) {
|
|
5
26
|
if (value === undefined)
|
|
@@ -37,6 +58,11 @@ export function exportSettingsOf(value, report) {
|
|
|
37
58
|
report("property-left-out", "an export setting beyond the reader's list limit is left out");
|
|
38
59
|
continue;
|
|
39
60
|
}
|
|
61
|
+
if (settings.length < FIELD_REPORTED_ROWS)
|
|
62
|
+
for (const { field, formats, kept } of UNKEPT_FIELDS) {
|
|
63
|
+
if (formats.includes(format) && !kept(row[field]))
|
|
64
|
+
report("property-left-out", `an export setting's ${field} is left out`);
|
|
65
|
+
}
|
|
40
66
|
settings.push({
|
|
41
67
|
format, constraint: { type: unit, value: amount }, suffix,
|
|
42
68
|
quality: defaultExportQuality(format),
|
|
@@ -2,7 +2,7 @@ import { type FigmaObject } from "../fields.ts";
|
|
|
2
2
|
import type { FigmaNodeConverter } from "../walk.ts";
|
|
3
3
|
/**
|
|
4
4
|
* A Figma FRAME as a Taco frame, with its HORIZONTAL or VERTICAL auto layout
|
|
5
|
-
* as a stack (see `
|
|
5
|
+
* as a stack (see `autoLayoutOf`) and free layout otherwise. Frames clip unless
|
|
6
6
|
* `frameMaskDisabled` is true (grida, packages/grida-canvas-io-figma/lib.ts:4246).
|
|
7
7
|
* Figma stores a group as a FRAME. `resizeToFit` alone does not prove it, so a
|
|
8
8
|
* FRAME counts as a group only when it also clips (`frameMaskDisabled` is
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { carry } from "../carry.js";
|
|
2
2
|
import { field } from "../fields.js";
|
|
3
3
|
import { gridStyleReference } from "../styles.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { nodeEffects } from "./effects.js";
|
|
5
|
+
import { autoLayoutOf } from "./layout.js";
|
|
6
6
|
import { convertGroup } from "./group.js";
|
|
7
7
|
import { framedBase, visualStyle } from "./shared.js";
|
|
8
8
|
/**
|
|
9
9
|
* A Figma FRAME as a Taco frame, with its HORIZONTAL or VERTICAL auto layout
|
|
10
|
-
* as a stack (see `
|
|
10
|
+
* as a stack (see `autoLayoutOf`) and free layout otherwise. Frames clip unless
|
|
11
11
|
* `frameMaskDisabled` is true (grida, packages/grida-canvas-io-figma/lib.ts:4246).
|
|
12
12
|
* Figma stores a group as a FRAME. `resizeToFit` alone does not prove it, so a
|
|
13
13
|
* FRAME counts as a group only when it also clips (`frameMaskDisabled` is
|
|
@@ -18,7 +18,7 @@ import { framedBase, visualStyle } from "./shared.js";
|
|
|
18
18
|
* fields is still a component or an instance, so it imports as a frame.
|
|
19
19
|
*/
|
|
20
20
|
export const convertFrame = (node, walk) => {
|
|
21
|
-
|
|
21
|
+
field(node.change, "stackMode", "string", `node ${node.id}`);
|
|
22
22
|
// The paint lists are checked here, so a malformed list is refused rather than read as "not a group".
|
|
23
23
|
for (const name of ["fillPaints", "strokePaints", "backgroundPaints"])
|
|
24
24
|
field(node.change, name, "list", `node ${node.id}`);
|
|
@@ -28,16 +28,17 @@ export const convertFrame = (node, walk) => {
|
|
|
28
28
|
if (base === null)
|
|
29
29
|
return null;
|
|
30
30
|
const visual = visualStyle(node, walk);
|
|
31
|
-
const style = { ...visual, ...nodeEffects(node, walk
|
|
31
|
+
const style = { ...visual, ...nodeEffects(node, walk) };
|
|
32
32
|
const frame = {
|
|
33
33
|
...base,
|
|
34
34
|
type: "frame",
|
|
35
35
|
children: walk.convertChildren(),
|
|
36
|
-
|
|
36
|
+
// An instance can inherit the track IDs its sizing refers to. Report grid fields once the main is known.
|
|
37
|
+
layout: autoLayoutOf(node.change, walk.report, node.type === "INSTANCE" ? () => { } : undefined) ?? { kind: "absolute" },
|
|
37
38
|
clipContent: node.change["frameMaskDisabled"] !== true,
|
|
38
39
|
...(Object.keys(style).length > 0 ? { style } : {}),
|
|
39
40
|
};
|
|
40
|
-
if (
|
|
41
|
+
if (frame.layout.kind !== "absolute" && node.change["bordersTakeSpace"] === true) {
|
|
41
42
|
carry(frame, { strokesIncluded: true });
|
|
42
43
|
}
|
|
43
44
|
return frame;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GridTrack } from "../../../model/types.ts";
|
|
2
|
+
import { type FigmaObject } from "../fields.ts";
|
|
3
|
+
type Lost = (field: string) => void;
|
|
4
|
+
export declare const gridChildFields: Set<string>;
|
|
5
|
+
export declare function reportUnknownGridFields(change: FigmaObject, lost: Lost): void;
|
|
6
|
+
/** Join sizing records to the ordered GUIDs. Children reference this same order by ID. */
|
|
7
|
+
export declare function gridAxis(change: FigmaObject, name: "gridRows" | "gridColumns", lost: Lost): {
|
|
8
|
+
indices: Map<string, number>;
|
|
9
|
+
tracks: GridTrack[];
|
|
10
|
+
};
|
|
11
|
+
export declare function gridCell(change: FigmaObject, rows: Map<string, number>, columns: Map<string, number>, lost: Lost, override?: boolean): Record<string, string | number>;
|
|
12
|
+
export {};
|