@moondraft/import 0.1.0 → 0.3.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 +44 -0
- package/README.md +2 -2
- package/SOURCE.md +343 -4
- package/dist/adapter/convert.d.ts +26 -2
- package/dist/adapter/convert.js +817 -217
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +34 -0
- package/dist/adapter/figma-handoff.js +188 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +4 -1
- package/dist/adapter/figma.js +15 -4
- package/dist/adapter/file-components.d.ts +10 -3
- package/dist/adapter/file-components.js +126 -33
- package/dist/adapter/file-resources.js +545 -47
- package/dist/adapter/file-typography.d.ts +6 -0
- package/dist/adapter/file-typography.js +44 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/link.d.ts +12 -1
- package/dist/adapter/link.js +380 -113
- package/dist/adapter/payload.js +3 -3
- package/dist/adapter/variables.d.ts +2 -1
- package/dist/adapter/variables.js +5 -2
- 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 +191 -35
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/report.d.ts +59 -1
- package/dist/report.js +176 -30
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +3 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +1 -1
- package/dist/taco/figma-import/figFile.js +3 -0
- package/dist/taco/figma-import/import/carry.d.ts +14 -1
- package/dist/taco/figma-import/import/carry.js +34 -1
- package/dist/taco/figma-import/import/componentProps.d.ts +10 -4
- package/dist/taco/figma-import/import/componentProps.js +29 -13
- package/dist/taco/figma-import/import/componentSets.d.ts +2 -0
- package/dist/taco/figma-import/import/componentSets.js +1 -1
- 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.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +75 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/frame.js +15 -6
- package/dist/taco/figma-import/import/converters/gradient.d.ts +2 -0
- package/dist/taco/figma-import/import/converters/gradient.js +3 -1
- 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 +2 -4
- package/dist/taco/figma-import/import/converters/group.js +6 -15
- package/dist/taco/figma-import/import/converters/image.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/image.js +60 -16
- package/dist/taco/figma-import/import/converters/layout.d.ts +8 -7
- package/dist/taco/figma-import/import/converters/layout.js +66 -37
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/rectangle.js +3 -3
- package/dist/taco/figma-import/import/converters/section.d.ts +2 -3
- package/dist/taco/figma-import/import/converters/section.js +5 -11
- package/dist/taco/figma-import/import/converters/shared.d.ts +9 -5
- package/dist/taco/figma-import/import/converters/shared.js +23 -10
- package/dist/taco/figma-import/import/converters/text.d.ts +38 -1
- package/dist/taco/figma-import/import/converters/text.js +248 -32
- package/dist/taco/figma-import/import/converters/vector.d.ts +36 -15
- package/dist/taco/figma-import/import/converters/vector.js +214 -73
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +19 -3
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +147 -29
- package/dist/taco/figma-import/import/instances.d.ts +32 -5
- package/dist/taco/figma-import/import/instances.js +613 -130
- package/dist/taco/figma-import/import/styles.d.ts +2 -0
- package/dist/taco/figma-import/import/styles.js +6 -0
- package/dist/taco/figma-import/import/types.d.ts +8 -0
- package/dist/taco/figma-import/import/variables.d.ts +5 -2
- package/dist/taco/figma-import/import/variables.js +65 -31
- package/dist/taco/figma-import/import/variantTrees.d.ts +1 -1
- package/dist/taco/figma-import/import/variantTrees.js +46 -4
- package/dist/taco/figma-import/import/walk.d.ts +62 -7
- package/dist/taco/figma-import/import/walk.js +328 -122
- package/dist/taco/figma-import/kiwi/message.d.ts +4 -2
- package/dist/taco/figma-import/kiwi/message.js +59 -15
- package/dist/taco/model/codec.d.ts +2 -2
- package/dist/taco/model/codec.js +72 -8
- package/dist/taco/model/component-evaluation.d.ts +2 -0
- package/dist/taco/model/component-evaluation.js +7 -0
- package/dist/taco/model/instance-resolver.d.ts +6 -2
- package/dist/taco/model/instance-resolver.js +50 -18
- package/dist/taco/model/sha256.js +13 -8
- package/dist/taco/model/types.d.ts +57 -5
- package/dist/taco/model/types.js +4 -0
- package/dist/taco/svg-import/style/computed.js +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ImportReportEntry } from '@moondraft/editor/core';
|
|
2
|
+
import type { FigmaTreeNode } from '../taco/figma-import/import/tree.ts';
|
|
3
|
+
/** Copies the source description onto its converted definition, reporting any discarded formatting. */
|
|
4
|
+
export declare function keepDescription(node: FigmaTreeNode, record: {
|
|
5
|
+
id: string;
|
|
6
|
+
description?: string | undefined;
|
|
7
|
+
}, report: ImportReportEntry[], ids?: string[]): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { decodeHTML } from 'entities/decode';
|
|
2
|
+
// Kiwi stores Figma's rich descriptions as HTML and may also carry an older plain-text projection.
|
|
3
|
+
const RICH_DESCRIPTION = /^\s*<(p|div|h[1-6]|ol|ul|li|blockquote|pre|span|strong|em|b|i|u|s|strike|del|a|code)(?=[\s>])[\s\S]*<\/\1\s*>/i;
|
|
4
|
+
const TAG = /<\/?([a-z][\w:-]*)(?=[\s/>])(?:[^"'<>]|"[^"]*"|'[^']*')*>|<!--[\s\S]*?-->/gi;
|
|
5
|
+
const BLOCK = /^(?:p|div|h[1-6]|ol|ul|li|blockquote|pre)$/;
|
|
6
|
+
function flatten(html) {
|
|
7
|
+
let text = '';
|
|
8
|
+
let end = 0;
|
|
9
|
+
for (const tag of html.matchAll(TAG)) {
|
|
10
|
+
text += decodeHTML(html.slice(end, tag.index));
|
|
11
|
+
const name = tag[1]?.toLowerCase();
|
|
12
|
+
if (name === 'br' || name && BLOCK.test(name) && text && !text.endsWith('\n'))
|
|
13
|
+
text += '\n';
|
|
14
|
+
end = tag.index + tag[0].length;
|
|
15
|
+
}
|
|
16
|
+
return (text + decodeHTML(html.slice(end))).trimEnd();
|
|
17
|
+
}
|
|
18
|
+
/** Copies the source description onto its converted definition, reporting any discarded formatting. */
|
|
19
|
+
export function keepDescription(node, record, report, ids) {
|
|
20
|
+
const fallback = node.change[node.change.styleType ? 'styleDescription' : 'symbolDescription'];
|
|
21
|
+
for (const value of [node.change.description, fallback]) {
|
|
22
|
+
if (typeof value !== 'string' || !value.trim())
|
|
23
|
+
continue;
|
|
24
|
+
if (RICH_DESCRIPTION.test(value)) {
|
|
25
|
+
const description = flatten(value);
|
|
26
|
+
report.push({ kind: 'approximated', what: 'description formatting flattened to plain text', count: 1, ids: ids ?? [record.id] });
|
|
27
|
+
if (description.trim())
|
|
28
|
+
record.description = description;
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
record.description = value;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ImportReportEntry } from '@moondraft/editor/core';
|
|
2
|
+
import { type DocumentJSON } from '@moondraft/editor/schema';
|
|
3
|
+
import type { FigmaTree, FigmaTreeNode } from '../taco/figma-import/import/tree.ts';
|
|
4
|
+
import type { walkMessage } from '../taco/figma-import/import/walk.ts';
|
|
5
|
+
/** Keeps gridded layers distinct when clipboard variants share a tree. */
|
|
6
|
+
export declare function gridSources(tree: FigmaTree): ReadonlySet<string>;
|
|
7
|
+
/** Writes native frame grids after component linking, while the original Figma nodes are still available. */
|
|
8
|
+
export declare function figmaLayoutGrids(sources: ReturnType<typeof walkMessage>['sources'], doc: DocumentJSON, options: {
|
|
9
|
+
fileId: () => string;
|
|
10
|
+
instanceSources?: ReadonlyMap<string, string>;
|
|
11
|
+
setSources?: ReadonlyMap<string, FigmaTreeNode>;
|
|
12
|
+
}): ImportReportEntry[];
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { STYLES } from '@moondraft/editor/schema';
|
|
2
|
+
import { isObject } from '../taco/figma-import/import/fields.js';
|
|
3
|
+
import { gridStyleReference } from '../taco/figma-import/import/styles.js';
|
|
4
|
+
import { EXAMPLE_IDS } from '../report.js';
|
|
5
|
+
import { keepDescription } from './figma-descriptions.js';
|
|
6
|
+
const MAX_GRIDS = 64;
|
|
7
|
+
const MAX_BANDS = 1000;
|
|
8
|
+
const alignments = { MIN: 'min', CENTER: 'center', MAX: 'max', STRETCH: 'stretch' };
|
|
9
|
+
const finite = (n) => typeof n === 'number' && Number.isFinite(n);
|
|
10
|
+
const hasGrids = ({ change }) => (change.layoutGrids !== undefined && (!Array.isArray(change.layoutGrids) || change.layoutGrids.length > 0))
|
|
11
|
+
|| gridStyleReference(change) !== undefined;
|
|
12
|
+
/** Keeps gridded layers distinct when clipboard variants share a tree. */
|
|
13
|
+
export function gridSources(tree) {
|
|
14
|
+
const ids = new Set();
|
|
15
|
+
for (const node of tree.nodes.values())
|
|
16
|
+
if (node.change.styleType !== 'GRID' && hasGrids(node))
|
|
17
|
+
ids.add(node.id);
|
|
18
|
+
return ids;
|
|
19
|
+
}
|
|
20
|
+
function layoutGrid(value) {
|
|
21
|
+
if (!isObject(value))
|
|
22
|
+
return 'invalid layout grid';
|
|
23
|
+
const { pattern, axis, type, numSections, sectionSize, gutterSize = 0, offset = 0, color, visible } = value;
|
|
24
|
+
const rgba = isObject(color) ? [color.r, color.g, color.b, color.a] : [];
|
|
25
|
+
if (color !== undefined && (rgba.length !== 4 || !rgba.every(v => finite(v) && v >= 0 && v <= 1)))
|
|
26
|
+
return 'invalid layout grid color';
|
|
27
|
+
if (visible !== undefined && typeof visible !== 'boolean')
|
|
28
|
+
return 'invalid layout grid visible';
|
|
29
|
+
const shared = { ...(color !== undefined && { color: rgba }), ...(visible !== undefined && { visible }) };
|
|
30
|
+
const size = finite(sectionSize) && sectionSize > 0 ? sectionSize : undefined;
|
|
31
|
+
if (pattern === 'GRID')
|
|
32
|
+
return size === undefined ? 'invalid layout grid sectionSize' : { pattern: 'grid', sectionSize: size, ...shared };
|
|
33
|
+
if (pattern !== 'STRIPES')
|
|
34
|
+
return 'unsupported layout grid pattern';
|
|
35
|
+
if (axis !== 'X' && axis !== 'Y')
|
|
36
|
+
return 'unsupported layout grid axis';
|
|
37
|
+
const alignment = typeof type === 'string' && Object.hasOwn(alignments, type) ? alignments[type] : undefined;
|
|
38
|
+
const count = numSections === -1 ? 'auto' : numSections;
|
|
39
|
+
if (alignment === undefined)
|
|
40
|
+
return 'unsupported layout grid type';
|
|
41
|
+
if (!finite(gutterSize) || gutterSize < 0)
|
|
42
|
+
return 'invalid layout grid gutterSize';
|
|
43
|
+
if (!finite(offset))
|
|
44
|
+
return 'invalid layout grid offset';
|
|
45
|
+
if (count !== 'auto' && (!finite(count) || !Number.isInteger(count) || count < 1 || count > MAX_BANDS))
|
|
46
|
+
return 'invalid layout grid numSections';
|
|
47
|
+
// A fixed stretch grid uses the frame's available space for band size. The editor still requires a positive literal.
|
|
48
|
+
const bandSize = size ?? (alignment === 'stretch' && count !== 'auto' ? 1 : undefined);
|
|
49
|
+
if (bandSize === undefined)
|
|
50
|
+
return 'invalid layout grid sectionSize';
|
|
51
|
+
return { pattern: axis === 'X' ? 'columns' : 'rows', alignment, count, sectionSize: bandSize, gutter: gutterSize, offset, ...shared };
|
|
52
|
+
}
|
|
53
|
+
const squareFields = new Set(['pattern', 'sectionSize', 'visible', 'color']);
|
|
54
|
+
const bandFields = new Set([...squareFields, 'axis', 'type', 'numSections', 'gutterSize', 'offset']);
|
|
55
|
+
function readGrids(raw, lost) {
|
|
56
|
+
const grids = [];
|
|
57
|
+
if (raw === undefined)
|
|
58
|
+
return grids;
|
|
59
|
+
if (!Array.isArray(raw)) {
|
|
60
|
+
lost('invalid or unsupported layout grid');
|
|
61
|
+
return grids;
|
|
62
|
+
}
|
|
63
|
+
for (const [i, value] of raw.entries()) {
|
|
64
|
+
if (grids.length === MAX_GRIDS) {
|
|
65
|
+
lost('layout grids beyond the frame limit', raw.length - i);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
const grid = layoutGrid(value);
|
|
69
|
+
if (typeof grid === 'string') {
|
|
70
|
+
lost(grid);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
grids.push(grid);
|
|
74
|
+
const fields = grid.pattern === 'grid' ? squareFields : bandFields;
|
|
75
|
+
for (const key of Object.keys(value))
|
|
76
|
+
if (!fields.has(key))
|
|
77
|
+
lost('layout grid field ' + key);
|
|
78
|
+
if (value.sectionSize !== undefined && value.sectionSize !== grid.sectionSize)
|
|
79
|
+
lost('layout grid field sectionSize');
|
|
80
|
+
}
|
|
81
|
+
return grids;
|
|
82
|
+
}
|
|
83
|
+
/** Writes native frame grids after component linking, while the original Figma nodes are still available. */
|
|
84
|
+
export function figmaLayoutGrids(sources, doc, options) {
|
|
85
|
+
const byReaderId = new Map();
|
|
86
|
+
const byFigmaId = new Map();
|
|
87
|
+
function addSource(id, figma) {
|
|
88
|
+
if (!hasGrids(figma))
|
|
89
|
+
return;
|
|
90
|
+
let source = byFigmaId.get(figma.id);
|
|
91
|
+
if (!source) {
|
|
92
|
+
const reference = gridStyleReference(figma.change);
|
|
93
|
+
const styleNode = reference === undefined ? undefined : sources.styleNodeFor(reference, 'GRID');
|
|
94
|
+
byFigmaId.set(figma.id, source = { figma, reference, styleNode, frames: [] });
|
|
95
|
+
}
|
|
96
|
+
byReaderId.set(id, source);
|
|
97
|
+
}
|
|
98
|
+
for (const { node, figma } of sources.imported)
|
|
99
|
+
addSource(node.id, figma);
|
|
100
|
+
for (const [id, figma] of options.setSources ?? [])
|
|
101
|
+
addSource(id, figma);
|
|
102
|
+
const frameIdsByStyle = new Map();
|
|
103
|
+
for (const frame of doc.models) {
|
|
104
|
+
if (frame.kind !== 'frame' || frame.group)
|
|
105
|
+
continue;
|
|
106
|
+
const id = options.instanceSources?.get(frame.id) ?? frame.sourceId;
|
|
107
|
+
const source = id === undefined ? undefined : byReaderId.get(id);
|
|
108
|
+
if (!source)
|
|
109
|
+
continue;
|
|
110
|
+
source.frames.push(frame);
|
|
111
|
+
if (source.styleNode) {
|
|
112
|
+
const style = source.styleNode.id;
|
|
113
|
+
const ids = frameIdsByStyle.get(style);
|
|
114
|
+
if (!ids)
|
|
115
|
+
frameIdsByStyle.set(style, [frame.id]);
|
|
116
|
+
else if (ids.length < EXAMPLE_IDS)
|
|
117
|
+
ids.push(frame.id);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const entries = [];
|
|
121
|
+
const losses = new Map();
|
|
122
|
+
function lost(what, ids, count = 1) {
|
|
123
|
+
let entry = losses.get(what);
|
|
124
|
+
if (!entry) {
|
|
125
|
+
entry = { kind: 'left-out', what, count: 0, ids: [] };
|
|
126
|
+
entries.push(entry);
|
|
127
|
+
losses.set(what, entry);
|
|
128
|
+
}
|
|
129
|
+
entry.count += count;
|
|
130
|
+
for (const id of ids) {
|
|
131
|
+
if (entry.ids.length === EXAMPLE_IDS)
|
|
132
|
+
break;
|
|
133
|
+
if (!entry.ids.includes(id))
|
|
134
|
+
entry.ids.push(id);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const styles = new Map();
|
|
138
|
+
function keepStyle(node) {
|
|
139
|
+
const existing = styles.get(node.id);
|
|
140
|
+
if (existing)
|
|
141
|
+
return existing;
|
|
142
|
+
const id = 'grid-' + node.id;
|
|
143
|
+
const ids = frameIdsByStyle.get(node.id) ?? [];
|
|
144
|
+
const record = { kind: 'style', type: 'grid', id, parent: STYLES, name: node.name,
|
|
145
|
+
key: String(node.change.key || `figma-${options.fileId()}-${node.id}`),
|
|
146
|
+
value: readGrids(node.change.layoutGrids, (what, count) => lost(what, ids, count)) };
|
|
147
|
+
keepDescription(node, record, entries, ids);
|
|
148
|
+
(doc.shared ??= []).push(record);
|
|
149
|
+
styles.set(node.id, record);
|
|
150
|
+
return record;
|
|
151
|
+
}
|
|
152
|
+
// Files carry their entire style library. A clipboard payload carries the selection's dependencies.
|
|
153
|
+
if (options.setSources)
|
|
154
|
+
for (const node of sources.tree.nodes.values())
|
|
155
|
+
if (node.change.styleType === 'GRID')
|
|
156
|
+
keepStyle(node);
|
|
157
|
+
for (const { figma, frames, reference, styleNode } of byFigmaId.values()) {
|
|
158
|
+
if (!frames.length) {
|
|
159
|
+
lost('layout grids left out with their frame', []);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const ids = frames.slice(0, EXAMPLE_IDS).map(frame => frame.id);
|
|
163
|
+
const style = styleNode && keepStyle(styleNode);
|
|
164
|
+
const grids = style?.value ?? readGrids(figma.change.layoutGrids, (what, count) => lost(what, ids, count));
|
|
165
|
+
for (const frame of frames) {
|
|
166
|
+
if (grids.length)
|
|
167
|
+
frame.layoutGrids = grids;
|
|
168
|
+
if (style)
|
|
169
|
+
frame.gridStyle = style.id;
|
|
170
|
+
}
|
|
171
|
+
if (reference !== undefined && !style)
|
|
172
|
+
lost('unavailable grid style reference', ids);
|
|
173
|
+
}
|
|
174
|
+
return entries;
|
|
175
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ImportReportEntry } from '@moondraft/editor/core';
|
|
2
|
+
import type { DocumentJSON } from '@moondraft/editor/schema';
|
|
3
|
+
import type { FigmaTree, FigmaTreeNode } from '../taco/figma-import/import/tree.ts';
|
|
4
|
+
import type { readFileMessage } from '../taco/figma-import/import/walk.ts';
|
|
5
|
+
import type { FigmaImportResult } from '../taco/figma-import/index.ts';
|
|
6
|
+
declare const feature: {
|
|
7
|
+
readonly annotations: 'developer annotations are not imported';
|
|
8
|
+
readonly measurements: 'developer measurements are not imported';
|
|
9
|
+
readonly sectionStatus: 'section status is not imported';
|
|
10
|
+
readonly sectionStatusInfo: 'section status is not imported';
|
|
11
|
+
readonly devStatus: 'section status is not imported';
|
|
12
|
+
};
|
|
13
|
+
type Field = keyof typeof feature;
|
|
14
|
+
/**
|
|
15
|
+
* Finds the instances whose overrides hold active annotations or measurements. Read it before the instances' override
|
|
16
|
+
* data is released.
|
|
17
|
+
* @param tree - The reader's Figma tree.
|
|
18
|
+
* @returns The handoff fields set by each such instance's overrides, by Figma ID.
|
|
19
|
+
*/
|
|
20
|
+
export declare function handoffOverrides(tree: FigmaTree): Map<string, readonly Field[]>;
|
|
21
|
+
/** Keeps handoff-bearing clipboard layers distinct when merging variant trees. */
|
|
22
|
+
export declare function handoffSources(tree: FigmaTree): ReadonlySet<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Installs Figma handoff metadata on the layers that survived conversion and reports any omitted fields: those of
|
|
25
|
+
* layers left out (`leftOut`, named in the report) and the handoff that instances set in overrides (`overrides`), which
|
|
26
|
+
* is not kept.
|
|
27
|
+
*/
|
|
28
|
+
export declare function figmaHandoff(result: FigmaImportResult, sources: ReturnType<typeof readFileMessage>['sources'], doc: DocumentJSON, options: {
|
|
29
|
+
instanceSources?: ReadonlyMap<string, string>;
|
|
30
|
+
setSources?: ReadonlyMap<string, FigmaTreeNode>;
|
|
31
|
+
leftOut: ReadonlySet<string>;
|
|
32
|
+
overrides: ReadonlyMap<string, readonly Field[]>;
|
|
33
|
+
}): ImportReportEntry[];
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { isObject } from '../taco/figma-import/import/fields.js';
|
|
2
|
+
import { onLeftOutLayer } from '../report.js';
|
|
3
|
+
const feature = {
|
|
4
|
+
annotations: 'developer annotations are not imported',
|
|
5
|
+
measurements: 'developer measurements are not imported',
|
|
6
|
+
sectionStatus: 'section status is not imported',
|
|
7
|
+
sectionStatusInfo: 'section status is not imported',
|
|
8
|
+
devStatus: 'section status is not imported',
|
|
9
|
+
};
|
|
10
|
+
const handoffFields = Object.keys(feature);
|
|
11
|
+
const statusFields = ['sectionStatus', 'sectionStatusInfo', 'devStatus'];
|
|
12
|
+
function isJSON(value) {
|
|
13
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
14
|
+
return true;
|
|
15
|
+
if (typeof value === 'number')
|
|
16
|
+
return Number.isFinite(value);
|
|
17
|
+
if (Array.isArray(value))
|
|
18
|
+
return value.every(isJSON);
|
|
19
|
+
return isObject(value) && Object.values(value).every(isJSON);
|
|
20
|
+
}
|
|
21
|
+
/** A decoded list can also contain tombstones. Keep the active entries in source order. */
|
|
22
|
+
function activeList(value) {
|
|
23
|
+
if (!Array.isArray(value))
|
|
24
|
+
return value;
|
|
25
|
+
const active = value.filter(item => !isObject(item) || item.isDeleted !== true);
|
|
26
|
+
return active.length ? active : undefined;
|
|
27
|
+
}
|
|
28
|
+
function statusName(field, value) {
|
|
29
|
+
if (field === 'sectionStatus')
|
|
30
|
+
return value;
|
|
31
|
+
return isObject(value) ? value[field === 'devStatus' ? 'type' : 'status'] : undefined;
|
|
32
|
+
}
|
|
33
|
+
function activeStatus(field, value) {
|
|
34
|
+
const name = statusName(field, value);
|
|
35
|
+
return typeof name === 'string' && name.length > 0 && name !== 'NONE';
|
|
36
|
+
}
|
|
37
|
+
function* canvasStatuses(tree) {
|
|
38
|
+
for (const node of tree.nodes.values()) {
|
|
39
|
+
if (node.type !== 'CANVAS')
|
|
40
|
+
continue;
|
|
41
|
+
const map = node.change.handoffStatusMap;
|
|
42
|
+
if (!isObject(map) || !Array.isArray(map.entries))
|
|
43
|
+
continue;
|
|
44
|
+
for (const entry of map.entries) {
|
|
45
|
+
if (!isObject(entry))
|
|
46
|
+
continue;
|
|
47
|
+
const status = entry.handoffStatus, guid = entry.guid;
|
|
48
|
+
if (statusName('sectionStatusInfo', status) === 'NONE')
|
|
49
|
+
continue;
|
|
50
|
+
const target = isObject(guid) && Number.isInteger(guid.sessionID) && Number.isInteger(guid.localID)
|
|
51
|
+
? `${guid.sessionID}:${guid.localID}` : undefined;
|
|
52
|
+
yield { canvas: node.id, target, status };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const overrideFields = ['annotations', 'measurements'];
|
|
57
|
+
/**
|
|
58
|
+
* Finds the instances whose overrides hold active annotations or measurements. Read it before the instances' override
|
|
59
|
+
* data is released.
|
|
60
|
+
* @param tree - The reader's Figma tree.
|
|
61
|
+
* @returns The handoff fields set by each such instance's overrides, by Figma ID.
|
|
62
|
+
*/
|
|
63
|
+
export function handoffOverrides(tree) {
|
|
64
|
+
const found = new Map();
|
|
65
|
+
for (const node of tree.nodes.values()) {
|
|
66
|
+
const overrides = isObject(node.change.symbolData) ? node.change.symbolData.symbolOverrides : undefined;
|
|
67
|
+
if (!Array.isArray(overrides))
|
|
68
|
+
continue;
|
|
69
|
+
const fields = overrideFields.filter(field => overrides.some(entry => isObject(entry) && activeList(entry[field]) !== undefined));
|
|
70
|
+
if (fields.length)
|
|
71
|
+
found.set(node.id, fields);
|
|
72
|
+
}
|
|
73
|
+
return found;
|
|
74
|
+
}
|
|
75
|
+
/** Keeps handoff-bearing clipboard layers distinct when merging variant trees. */
|
|
76
|
+
export function handoffSources(tree) {
|
|
77
|
+
const ids = new Set();
|
|
78
|
+
for (const node of tree.nodes.values()) {
|
|
79
|
+
const { annotations, measurements } = node.change;
|
|
80
|
+
if (activeList(annotations) !== undefined || activeList(measurements) !== undefined ||
|
|
81
|
+
statusFields.some(field => activeStatus(field, node.change[field])))
|
|
82
|
+
ids.add(node.id);
|
|
83
|
+
}
|
|
84
|
+
for (const { target, status } of canvasStatuses(tree))
|
|
85
|
+
if (target !== undefined && activeStatus('sectionStatusInfo', status))
|
|
86
|
+
ids.add(target);
|
|
87
|
+
return ids;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Installs Figma handoff metadata on the layers that survived conversion and reports any omitted fields: those of
|
|
91
|
+
* layers left out (`leftOut`, named in the report) and the handoff that instances set in overrides (`overrides`), which
|
|
92
|
+
* is not kept.
|
|
93
|
+
*/
|
|
94
|
+
export function figmaHandoff(result, sources, doc, options) {
|
|
95
|
+
const entries = [];
|
|
96
|
+
const report = (what, ids) => {
|
|
97
|
+
entries.push({ kind: 'left-out', what, count: 1, ...(ids?.length && { ids }) });
|
|
98
|
+
};
|
|
99
|
+
// Figma also records handoff status by target GUID on the canvas. A layer's own active status takes precedence.
|
|
100
|
+
const pages = doc.models.filter(m => m.kind === 'page');
|
|
101
|
+
const byPage = new Map(result.pages.map((page, i) => [page.figmaId, pages[i]?.id]));
|
|
102
|
+
const mappedStatus = new Map();
|
|
103
|
+
for (const { canvas, target, status } of canvasStatuses(sources.tree)) {
|
|
104
|
+
const pageId = byPage.get(canvas);
|
|
105
|
+
if (target === undefined) {
|
|
106
|
+
if (pageId !== undefined)
|
|
107
|
+
report(feature.sectionStatus, [pageId]);
|
|
108
|
+
}
|
|
109
|
+
else if (status !== undefined)
|
|
110
|
+
mappedStatus.set(target, { status, pageId });
|
|
111
|
+
}
|
|
112
|
+
const hasHandoff = (node) => node !== undefined && (mappedStatus.has(node.id) || options.overrides.has(node.id) || handoffFields.some(field => node.change[field] !== undefined));
|
|
113
|
+
const byReaderId = new Map();
|
|
114
|
+
const layers = new Map();
|
|
115
|
+
for (const { node, figma } of sources.imported)
|
|
116
|
+
if (hasHandoff(figma))
|
|
117
|
+
byReaderId.set(node.id, figma);
|
|
118
|
+
for (const [id, figma] of options.setSources ?? [])
|
|
119
|
+
if (hasHandoff(figma))
|
|
120
|
+
byReaderId.set(id, figma);
|
|
121
|
+
for (const model of doc.models) {
|
|
122
|
+
if (model.kind === 'page')
|
|
123
|
+
continue;
|
|
124
|
+
const source = options.instanceSources?.get(model.id) ?? model.sourceId;
|
|
125
|
+
const figma = source === undefined ? undefined : byReaderId.get(source);
|
|
126
|
+
if (figma === undefined)
|
|
127
|
+
continue;
|
|
128
|
+
const copies = layers.get(figma.id);
|
|
129
|
+
if (copies)
|
|
130
|
+
copies.push(model);
|
|
131
|
+
else
|
|
132
|
+
layers.set(figma.id, [model]);
|
|
133
|
+
}
|
|
134
|
+
const scoped = new Set([...byReaderId.values()].map(node => node.id));
|
|
135
|
+
for (const { nodeId, kind } of result.report)
|
|
136
|
+
if (kind === 'node-left-out' && hasHandoff(sources.tree.nodes.get(nodeId)))
|
|
137
|
+
scoped.add(nodeId);
|
|
138
|
+
for (const id of options.leftOut)
|
|
139
|
+
if (hasHandoff(sources.tree.nodes.get(id)))
|
|
140
|
+
scoped.add(id);
|
|
141
|
+
for (const [id, { pageId }] of mappedStatus)
|
|
142
|
+
if (pageId !== undefined)
|
|
143
|
+
scoped.add(id);
|
|
144
|
+
for (const id of scoped) {
|
|
145
|
+
const node = sources.tree.nodes.get(id), change = node?.change;
|
|
146
|
+
const targets = layers.get(id) ?? [];
|
|
147
|
+
const ids = targets.map(layer => layer.id);
|
|
148
|
+
const mapped = mappedStatus.get(id);
|
|
149
|
+
const fields = {
|
|
150
|
+
annotations: activeList(change?.annotations),
|
|
151
|
+
measurements: activeList(change?.measurements),
|
|
152
|
+
sectionStatus: change?.sectionStatus,
|
|
153
|
+
sectionStatusInfo: change?.sectionStatusInfo,
|
|
154
|
+
devStatus: change?.devStatus,
|
|
155
|
+
};
|
|
156
|
+
for (const field of statusFields)
|
|
157
|
+
if (statusName(field, fields[field]) === 'NONE')
|
|
158
|
+
fields[field] = undefined;
|
|
159
|
+
if (mapped !== undefined && !statusFields.some(field => activeStatus(field, fields[field]) && isJSON(fields[field])))
|
|
160
|
+
fields.sectionStatusInfo = mapped.status;
|
|
161
|
+
const data = {};
|
|
162
|
+
const omitted = new Set();
|
|
163
|
+
for (const field of handoffFields) {
|
|
164
|
+
const value = fields[field];
|
|
165
|
+
if (value === undefined)
|
|
166
|
+
continue;
|
|
167
|
+
const valid = field === 'annotations' || field === 'measurements' || activeStatus(field, value);
|
|
168
|
+
if (valid && targets.length && isJSON(value))
|
|
169
|
+
data[field] = value;
|
|
170
|
+
else
|
|
171
|
+
omitted.add(feature[field]);
|
|
172
|
+
}
|
|
173
|
+
for (const field of options.overrides.get(id) ?? [])
|
|
174
|
+
omitted.add(feature[field]);
|
|
175
|
+
for (const what of omitted) {
|
|
176
|
+
if (options.leftOut.has(id) && node)
|
|
177
|
+
report(onLeftOutLayer(what, node.name || node.type));
|
|
178
|
+
else
|
|
179
|
+
report(what, ids.length || mapped?.pageId === undefined ? ids : [mapped.pageId]);
|
|
180
|
+
}
|
|
181
|
+
if (Object.keys(data).length)
|
|
182
|
+
for (const layer of targets) {
|
|
183
|
+
const existing = layer.data?.figma;
|
|
184
|
+
layer.data = { ...layer.data, figma: { ...(isObject(existing) ? existing : {}), ...data } };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return entries;
|
|
188
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DocumentJSON, TextStyle } from '@moondraft/editor/schema';
|
|
2
|
+
import type { ImportReportEntry } from '@moondraft/editor/core';
|
|
3
|
+
import type { readFileMessage } from '../taco/figma-import/import/walk.ts';
|
|
4
|
+
import type { FigmaImportResult, FigmaTreeNode } from '../taco/figma-import/index.ts';
|
|
5
|
+
/** Text style in the Figma adapter before resolving its native destination. @internal */
|
|
6
|
+
export type FigmaTextStyle = TextStyle & {
|
|
7
|
+
figmaHyperlink?: string;
|
|
8
|
+
};
|
|
9
|
+
/** Resolves text link GUIDs after the importer assigns native page, layer and component IDs. */
|
|
10
|
+
export declare function figmaHyperlinks(result: FigmaImportResult, sources: ReturnType<typeof readFileMessage>['sources'], doc: DocumentJSON, options?: {
|
|
11
|
+
instanceSources?: ReadonlyMap<string, string>;
|
|
12
|
+
setSources?: ReadonlyMap<string, FigmaTreeNode>;
|
|
13
|
+
}): ImportReportEntry[];
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import * as editorSchema from '@moondraft/editor/schema';
|
|
2
|
+
/** Resolves text link GUIDs after the importer assigns native page, layer and component IDs. */
|
|
3
|
+
export function figmaHyperlinks(result, sources, doc, options = {}) {
|
|
4
|
+
const pending = new Map();
|
|
5
|
+
function find(styles, id) {
|
|
6
|
+
if (!Array.isArray(styles))
|
|
7
|
+
return;
|
|
8
|
+
for (const style of styles) {
|
|
9
|
+
const guid = style.figmaHyperlink;
|
|
10
|
+
if (guid === undefined)
|
|
11
|
+
continue;
|
|
12
|
+
let entries = pending.get(guid);
|
|
13
|
+
if (!entries)
|
|
14
|
+
pending.set(guid, entries = new Map());
|
|
15
|
+
let ids = entries.get(style);
|
|
16
|
+
if (!ids)
|
|
17
|
+
entries.set(style, ids = new Set());
|
|
18
|
+
ids.add(id);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for (const model of doc.models) {
|
|
22
|
+
if (model.kind === 'text')
|
|
23
|
+
find(model.styles, model.id);
|
|
24
|
+
if (model.kind === 'frame' && model.overrides)
|
|
25
|
+
for (const [path, entry] of Object.entries(model.overrides))
|
|
26
|
+
find(entry.styles, path ? `${model.id};${path}` : model.id);
|
|
27
|
+
}
|
|
28
|
+
if (!pending.size)
|
|
29
|
+
return [];
|
|
30
|
+
const targets = new Map(), layers = new Map();
|
|
31
|
+
const pages = doc.models.filter(m => m.kind === 'page');
|
|
32
|
+
for (const [i, page] of result.pages.entries()) {
|
|
33
|
+
const model = pages[i];
|
|
34
|
+
if (model && pending.has(page.figmaId))
|
|
35
|
+
targets.set(page.figmaId, editorSchema.documentTextLink('page', model.id));
|
|
36
|
+
}
|
|
37
|
+
// layerIds also maps omitted layers to their nearest imported ancestor. Only
|
|
38
|
+
// consider GUIDs for which the reader actually converted a layer.
|
|
39
|
+
for (const { figma } of sources.imported)
|
|
40
|
+
if (pending.has(figma.id)) {
|
|
41
|
+
const layer = result.layerIds[figma.id];
|
|
42
|
+
if (layer !== undefined)
|
|
43
|
+
layers.set(figma.id, layer);
|
|
44
|
+
}
|
|
45
|
+
for (const [layer, figma] of options.setSources ?? [])
|
|
46
|
+
if (pending.has(figma.id))
|
|
47
|
+
layers.set(figma.id, layer);
|
|
48
|
+
const wanted = new Set(layers.values()), bySource = new Map(), inMain = new Set();
|
|
49
|
+
for (const model of doc.models) {
|
|
50
|
+
if (model.kind === 'page')
|
|
51
|
+
continue;
|
|
52
|
+
const main = model.component === model.id || inMain.has(model.parent ?? '');
|
|
53
|
+
if (main)
|
|
54
|
+
inMain.add(model.id);
|
|
55
|
+
const source = options.instanceSources?.get(model.id) ?? model.sourceId;
|
|
56
|
+
if (source !== undefined && wanted.has(source) && (!bySource.has(source) || main))
|
|
57
|
+
bySource.set(source, model.id);
|
|
58
|
+
}
|
|
59
|
+
for (const [guid, source] of layers) {
|
|
60
|
+
const id = bySource.get(source);
|
|
61
|
+
if (id !== undefined)
|
|
62
|
+
targets.set(guid, editorSchema.documentTextLink('node', id));
|
|
63
|
+
}
|
|
64
|
+
const report = [];
|
|
65
|
+
for (const [guid, entries] of pending) {
|
|
66
|
+
const target = targets.get(guid);
|
|
67
|
+
for (const [style, ids] of entries) {
|
|
68
|
+
delete style.figmaHyperlink;
|
|
69
|
+
if (target)
|
|
70
|
+
style.link = target;
|
|
71
|
+
else {
|
|
72
|
+
report.push({ kind: 'left-out', what: 'Text hyperlink whose target was not imported', count: ids.size, ids: [...ids] });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return report;
|
|
77
|
+
}
|
package/dist/adapter/figma.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { FigmaImportResult } from '../taco/figma-import/index.ts';
|
|
2
2
|
import type { TacoDocument } from './convert.ts';
|
|
3
|
+
/** Encodes inline image bytes without building an image-sized binary string. */
|
|
4
|
+
export declare function base64(bytes: Uint8Array): string;
|
|
3
5
|
/**
|
|
4
6
|
* Wraps reader output as a Taco document for conversion.
|
|
5
7
|
* @param result - Pages, components, assets and styles from the Figma reader.
|
|
6
8
|
* @param name - Display name for the clipboard's page. Saved files keep their page names.
|
|
9
|
+
* @param encodeImage - Encodes inline bytes. File import can defer encoding until after linking.
|
|
7
10
|
* @returns The source document with roots grouped by page.
|
|
8
11
|
*/
|
|
9
|
-
export declare function figmaDocument(result: FigmaImportResult, name?: string): TacoDocument;
|
|
12
|
+
export declare function figmaDocument(result: FigmaImportResult, name?: string, encodeImage?: typeof base64): TacoDocument;
|
package/dist/adapter/figma.js
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/** Encodes inline image bytes without building an image-sized binary string. */
|
|
2
|
+
export function base64(bytes) {
|
|
3
|
+
const native = bytes.toBase64;
|
|
4
|
+
if (native)
|
|
5
|
+
return native.call(bytes);
|
|
6
|
+
// A multiple of three leaves padding only on the final piece.
|
|
7
|
+
const pieces = [], size = 3 * 8192;
|
|
8
|
+
for (let i = 0; i < bytes.length; i += size)
|
|
9
|
+
pieces.push(btoa(String.fromCharCode(...bytes.subarray(i, i + size))));
|
|
10
|
+
return pieces.join('');
|
|
11
|
+
}
|
|
3
12
|
/**
|
|
4
13
|
* Wraps reader output as a Taco document for conversion.
|
|
5
14
|
* @param result - Pages, components, assets and styles from the Figma reader.
|
|
6
15
|
* @param name - Display name for the clipboard's page. Saved files keep their page names.
|
|
16
|
+
* @param encodeImage - Encodes inline bytes. File import can defer encoding until after linking.
|
|
7
17
|
* @returns The source document with roots grouped by page.
|
|
8
18
|
*/
|
|
9
|
-
export function figmaDocument(result, name) {
|
|
19
|
+
export function figmaDocument(result, name, encodeImage = base64) {
|
|
10
20
|
const nodes = {};
|
|
11
21
|
for (const n of result.nodes)
|
|
12
22
|
nodes[n.id] = n;
|
|
@@ -25,6 +35,7 @@ export function figmaDocument(result, name) {
|
|
|
25
35
|
components[c.id] = c;
|
|
26
36
|
const assets = {};
|
|
27
37
|
for (const i of Object.values(result.assets))
|
|
28
|
-
assets[i.id] = { id: i.id, kind:
|
|
38
|
+
assets[i.id] = { id: i.id, kind: i.mimeType.startsWith('video/') ? 'video' : 'image',
|
|
39
|
+
...(i.poster && { poster: `data:${i.poster.mimeType};base64,${encodeImage(i.poster.bytes)}` }), mimeType: i.mimeType, ...(i.width !== undefined ? { width: i.width } : {}), ...(i.height !== undefined ? { height: i.height } : {}), source: { kind: 'data', data: encodeImage(i.bytes) } };
|
|
29
40
|
return { pageOrder, pages, nodes, components, assets, componentPlacements: result.componentPlacements, theme: { variableCollections: result.variableCollections, textStyles: result.textStyles ?? {} } };
|
|
30
41
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { DocumentJSON } from '@moondraft/editor/schema';
|
|
2
|
-
import type
|
|
2
|
+
import { type ImportReportEntry } from '@moondraft/editor/core';
|
|
3
|
+
import type { FigmaTreeNode } from '../taco/figma-import/import/tree.ts';
|
|
3
4
|
import type { readFileMessage } from '../taco/figma-import/import/walk.ts';
|
|
5
|
+
import type { NestedBooleanAssignments } from './convert.ts';
|
|
4
6
|
/** Reader output with the original set members and resource identities.
|
|
5
7
|
* @internal
|
|
6
8
|
*/
|
|
@@ -8,7 +10,12 @@ export type FileMessage = ReturnType<typeof readFileMessage>;
|
|
|
8
10
|
/** Installs local property IDs, their bindings, instance assignments and slot content.
|
|
9
11
|
* @param result - Original Figma component declarations and placements.
|
|
10
12
|
* @param doc - Linked document, with source IDs for the original nodes.
|
|
13
|
+
* @param nestedBooleans - Enclosing instance assignments carried through expanded layers and their linked paths.
|
|
11
14
|
* @param report - Receives occurrences the native document cannot represent.
|
|
12
|
-
* @
|
|
15
|
+
* @param expandedReasons - Per instance linking left expanded, by layer ID: linking's reason, which its report entry names.
|
|
16
|
+
* @returns Authored override fields and the Figma sources of newly created sets.
|
|
13
17
|
*/
|
|
14
|
-
export declare function fileComponents(result: FileMessage, doc: DocumentJSON, report: ImportReportEntry[]
|
|
18
|
+
export declare function fileComponents(result: FileMessage, doc: DocumentJSON, nestedBooleans: ReadonlyMap<string, NestedBooleanAssignments>, report: ImportReportEntry[], expandedReasons?: ReadonlyMap<string, string>): {
|
|
19
|
+
occurrences: Map<string, Map<string, Readonly<Record<string, unknown>>>>;
|
|
20
|
+
setSources: Map<string, FigmaTreeNode>;
|
|
21
|
+
};
|