@moondraft/import 0.1.0 → 0.2.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 +29 -0
- package/README.md +2 -2
- package/SOURCE.md +229 -4
- package/dist/adapter/convert.d.ts +24 -2
- package/dist/adapter/convert.js +669 -160
- 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 +12 -0
- package/dist/adapter/figma-handoff.js +167 -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 +14 -4
- package/dist/adapter/file-components.d.ts +9 -3
- package/dist/adapter/file-components.js +122 -32
- package/dist/adapter/file-resources.js +316 -27
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/link.d.ts +5 -1
- package/dist/adapter/link.js +308 -104
- package/dist/adapter/payload.js +1 -1
- package/dist/adapter/variables.d.ts +2 -1
- package/dist/adapter/variables.js +5 -2
- package/dist/figma.js +167 -33
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/report.d.ts +28 -1
- package/dist/report.js +102 -29
- 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/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/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.js +8 -0
- 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/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 +1 -1
- package/dist/taco/figma-import/import/converters/image.js +22 -11
- package/dist/taco/figma-import/import/converters/layout.d.ts +3 -4
- package/dist/taco/figma-import/import/converters/layout.js +40 -21
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/rectangle.js +1 -1
- package/dist/taco/figma-import/import/converters/section.d.ts +2 -3
- package/dist/taco/figma-import/import/converters/section.js +3 -9
- package/dist/taco/figma-import/import/converters/shared.d.ts +7 -4
- package/dist/taco/figma-import/import/converters/shared.js +15 -5
- package/dist/taco/figma-import/import/converters/text.d.ts +14 -1
- package/dist/taco/figma-import/import/converters/text.js +143 -12
- 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 +25 -4
- package/dist/taco/figma-import/import/instances.js +324 -117
- 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/variables.d.ts +3 -2
- package/dist/taco/figma-import/import/variables.js +37 -30
- package/dist/taco/figma-import/import/variantTrees.d.ts +1 -1
- package/dist/taco/figma-import/import/variantTrees.js +44 -4
- package/dist/taco/figma-import/import/walk.d.ts +53 -5
- package/dist/taco/figma-import/import/walk.js +273 -106
- package/dist/taco/figma-import/kiwi/message.d.ts +4 -2
- package/dist/taco/figma-import/kiwi/message.js +13 -11
- package/dist/taco/model/codec.d.ts +1 -1
- package/dist/taco/model/codec.js +37 -7
- 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 +48 -18
- package/dist/taco/model/sha256.js +13 -8
- package/dist/taco/model/types.d.ts +23 -1
- package/package.json +3 -2
|
@@ -1,9 +1,52 @@
|
|
|
1
|
+
import * as editorSchema from '@moondraft/editor/schema';
|
|
1
2
|
import { STYLES, VARIABLES } from '@moondraft/editor/schema';
|
|
2
|
-
import {
|
|
3
|
+
import { textColor } from '../taco/figma-import/import/converters/text.js';
|
|
4
|
+
import { styleAtOf, supportsRunPaint } from './link.js';
|
|
5
|
+
import { guidText, isObject } from '../taco/figma-import/import/fields.js';
|
|
3
6
|
import { isDetachedStyle, STYLE_REFERENCE_FIELDS } from '../taco/figma-import/import/styles.js';
|
|
4
7
|
import { layerPathKey, parseLayerPath } from '../taco/model/index.js';
|
|
5
8
|
import { rgba, runStyle } from './convert.js';
|
|
6
|
-
|
|
9
|
+
import { keepDescription } from './figma-descriptions.js';
|
|
10
|
+
// Editor 0.3.0's closed variable schema predates both metadata fields.
|
|
11
|
+
const scopeNames = 'VARIABLE_SCOPES' in editorSchema ? new Set(editorSchema.VARIABLE_SCOPES) : undefined;
|
|
12
|
+
const syntaxPlatforms = new Map([['WEB', 'web'], ['ANDROID', 'android'], ['iOS', 'ios']]);
|
|
13
|
+
function keepVariableMetadata(node, record, report) {
|
|
14
|
+
const { variableScopes, codeSyntax } = node.change;
|
|
15
|
+
const leftOut = (what) => report.push({ kind: 'left-out', what, count: 1 });
|
|
16
|
+
if (!scopeNames) {
|
|
17
|
+
if (Array.isArray(variableScopes) || codeSyntax !== undefined)
|
|
18
|
+
leftOut('variable metadata requires a newer editor');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(variableScopes)) {
|
|
22
|
+
record.scopes = [];
|
|
23
|
+
for (const raw of variableScopes) {
|
|
24
|
+
const scope = raw === 'STROKE' ? 'STROKE_COLOR' : raw;
|
|
25
|
+
if (typeof scope === 'string' && scopeNames.has(scope))
|
|
26
|
+
record.scopes.push(scope);
|
|
27
|
+
else
|
|
28
|
+
leftOut('unsupported variable scope');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (codeSyntax === undefined)
|
|
32
|
+
return;
|
|
33
|
+
if (!isObject(codeSyntax) || codeSyntax.entries !== undefined && !Array.isArray(codeSyntax.entries)) {
|
|
34
|
+
leftOut('unsupported variable code syntax');
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
record.codeSyntax = {};
|
|
38
|
+
for (const entry of codeSyntax.entries ?? []) {
|
|
39
|
+
if (isObject(entry) && typeof entry.platform === 'string' && typeof entry.value === 'string') {
|
|
40
|
+
const platform = syntaxPlatforms.get(entry.platform);
|
|
41
|
+
if (platform) {
|
|
42
|
+
record.codeSyntax[platform] = entry.value;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
leftOut('unsupported variable code syntax entry');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function fileVariables(result, shared, identity, report) {
|
|
7
50
|
const variables = result.sources.variables;
|
|
8
51
|
for (const [id, c] of Object.entries(result.variableCollections)) {
|
|
9
52
|
shared.push({ ...identity(variables.sources.get(id)), id, parent: VARIABLES, kind: 'collection', name: c.name,
|
|
@@ -13,11 +56,21 @@ function fileVariables(result, shared, identity) {
|
|
|
13
56
|
const values = Object.fromEntries(Object.entries(v.values).map(([mode, value]) => [mode, v.type === 'color' ? rgba(value) : value]));
|
|
14
57
|
for (const [mode, alias] of variables.aliases.get(varId) ?? [])
|
|
15
58
|
values[mode] = { alias };
|
|
16
|
-
|
|
59
|
+
const record = { ...identity(node), id: varId, parent: id, name: v.name, kind: 'variable', type: v.type, values };
|
|
60
|
+
keepDescription(node, record, report);
|
|
61
|
+
keepVariableMetadata(node, record, report);
|
|
62
|
+
shared.push(record);
|
|
17
63
|
}
|
|
18
64
|
}
|
|
19
65
|
}
|
|
20
66
|
const corners = ['rectangleTopLeftCornerRadius', 'rectangleTopRightCornerRadius', 'rectangleBottomRightCornerRadius', 'rectangleBottomLeftCornerRadius'];
|
|
67
|
+
const strokeSides = ['Top', 'Right', 'Bottom', 'Left'].map(side => ({
|
|
68
|
+
field: 'border' + side + 'Weight', binding: 'BORDER_' + side.toUpperCase() + '_WEIGHT',
|
|
69
|
+
}));
|
|
70
|
+
const isStrokeSideBinding = (field) => strokeSides.some(side => side.binding === field);
|
|
71
|
+
const strokeSideBindings = (change) => (change.variableConsumptionMap?.entries ?? []).filter((entry) => isStrokeSideBinding(entry.variableField));
|
|
72
|
+
const hasUniformStroke = (change) => change.borderStrokeWeightsIndependent === false ||
|
|
73
|
+
change.strokeWeight !== undefined && !change.borderStrokeWeightsIndependent;
|
|
21
74
|
const fields = {
|
|
22
75
|
WIDTH: 'w', HEIGHT: 'h', STROKE_WEIGHT: 'strokeWidth', OPACITY: 'opacity', VISIBLE: 'visible', TEXT_DATA: 'content',
|
|
23
76
|
STACK_SPACING: 'layoutData.layout.gap', STACK_COUNTER_SPACING: 'layoutData.layout.wrapGap',
|
|
@@ -28,6 +81,8 @@ const fields = {
|
|
|
28
81
|
function paintsAndEffects(change, m, bind, indices) {
|
|
29
82
|
for (const [key, prefix] of [['fillPaints', 'look.fills'], ['strokePaints', 'look.stroke.paint']]) {
|
|
30
83
|
const paints = change[key] ?? [];
|
|
84
|
+
if (!paints.length)
|
|
85
|
+
continue;
|
|
31
86
|
if (key === 'fillPaints' && m.fill && paints.some(p => p.colorVar)) {
|
|
32
87
|
(m.look ??= {}).fills = [{ k: 'solid', c: m.fill, o: 1 }];
|
|
33
88
|
delete m.fill;
|
|
@@ -37,12 +92,13 @@ function paintsAndEffects(change, m, bind, indices) {
|
|
|
37
92
|
const order = indices.paintIndices.get(paints) ?? paints.map((_, i) => i);
|
|
38
93
|
const byIndex = new Map(order.map((index, i) => [index, i]));
|
|
39
94
|
const firstStroke = order.find(i => paints[i]?.visible !== false);
|
|
95
|
+
const glyphPaint = key === 'fillPaints' && m.kind === 'text' ? textColor(paints).paint : undefined;
|
|
40
96
|
paints.forEach((paint, i) => {
|
|
41
97
|
const fill = key === 'fillPaints';
|
|
42
98
|
const index = byIndex.get(i);
|
|
43
99
|
const output = fill ? index !== undefined && m.look?.fills?.[index] : i === firstStroke && m.look?.stroke?.paint;
|
|
44
100
|
const path = fill ? prefix + '.' + index : prefix;
|
|
45
|
-
if (paint.colorVar)
|
|
101
|
+
if (paint.colorVar && paint !== glyphPaint)
|
|
46
102
|
bind(output?.k === 'solid' ? path + '.c' : undefined, paint.colorVar, 'color');
|
|
47
103
|
if (paint.opacityVar)
|
|
48
104
|
bind(undefined, paint.opacityVar, 'number');
|
|
@@ -141,15 +197,13 @@ export function expandedRadiusRoots(result) {
|
|
|
141
197
|
}
|
|
142
198
|
function layerFields(change, m, bind) {
|
|
143
199
|
const entries = change.variableConsumptionMap?.entries ?? [];
|
|
144
|
-
const sides = entries.filter((e) => /^BORDER_(LEFT|RIGHT|TOP|BOTTOM)_WEIGHT$/.test(e.variableField));
|
|
145
|
-
const uniformStroke = sides.length === 4 && sides.every((e) => JSON.stringify(e.variableData) === JSON.stringify(sides[0].variableData));
|
|
146
|
-
if (uniformStroke)
|
|
147
|
-
bind(m.stroke && (!m.component || m.component === m.id) ? 'strokeWidth' : undefined, sides[0].variableData, 'number');
|
|
148
200
|
for (const { variableField, variableData } of entries) {
|
|
149
|
-
if (
|
|
150
|
-
continue;
|
|
201
|
+
if (isStrokeSideBinding(variableField))
|
|
202
|
+
continue; // Installed per side.
|
|
151
203
|
if (variableField === 'FONT_SIZE' && m.styles)
|
|
152
204
|
continue; // Installed per range below.
|
|
205
|
+
if (variableField === 'TEXT_DATA' && variableData === undefined)
|
|
206
|
+
continue; // An empty entry names no variable.
|
|
153
207
|
let path = Object.hasOwn(fields, variableField) ? fields[variableField] : undefined;
|
|
154
208
|
if (path?.startsWith('radius') && ['frame', 'rect'].includes(m.kind))
|
|
155
209
|
continue; // Installed per effective corner.
|
|
@@ -195,7 +249,8 @@ function inheritedLiterals(entry, template, change, expanded, rootBindings) {
|
|
|
195
249
|
for (const [key, field] of [['visible', 'visible'], ['opacity', 'opacity'], ['content', 'textData']]) {
|
|
196
250
|
if (rootBindings || !inherited(key))
|
|
197
251
|
continue;
|
|
198
|
-
|
|
252
|
+
const authored = key === 'content' ? change.textData?.characters !== undefined : field in change;
|
|
253
|
+
if (!authored)
|
|
199
254
|
delete entry[key];
|
|
200
255
|
else if (!(key in entry))
|
|
201
256
|
entry[key] = key === 'content' ? expanded.content : change[field];
|
|
@@ -230,7 +285,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
230
285
|
const { tree, variables, imported, styleLayers, textStyleUses } = result.sources;
|
|
231
286
|
const shared = [];
|
|
232
287
|
const identity = node => ({ name: node.name, key: String(node.change.key || `figma-${fileId()}-${node.id}`) });
|
|
233
|
-
fileVariables(result, shared, identity);
|
|
288
|
+
fileVariables(result, shared, identity, report);
|
|
234
289
|
const bySource = new Map(doc.models.map((m) => [m.sourceId, m]));
|
|
235
290
|
const layerSource = new Map(imported.map(({ figma, node }) => [node.id, { figma, node }]));
|
|
236
291
|
const definitions = new Map();
|
|
@@ -239,6 +294,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
239
294
|
const styleUsers = new Map();
|
|
240
295
|
const records = new Map(doc.models.map(m => [m.id, m]));
|
|
241
296
|
function style(node, record) {
|
|
297
|
+
keepDescription(node, record, report);
|
|
242
298
|
definitions.set(node.id, record);
|
|
243
299
|
byKey.set(record.type + '/' + record.key, record);
|
|
244
300
|
shared.push(record);
|
|
@@ -262,7 +318,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
262
318
|
}
|
|
263
319
|
function variableAt(path, data, type, id) {
|
|
264
320
|
const variable = variables.variableOf(data, id);
|
|
265
|
-
const length = path && /^(?:w|h|strokeWidth|opacity|radius(?:\.[0-3])?|look\.(?:layerBlur|backgroundBlur)\.radius|look\.shadows\.\d+\.blur)$/.test(path);
|
|
321
|
+
const length = path && /^(?:w|h|strokeWidth|look\.stroke\.sides\.[0-3]|opacity|radius(?:\.[0-3])?|look\.(?:layerBlur|backgroundBlur)\.radius|look\.shadows\.\d+\.blur)$/.test(path);
|
|
266
322
|
if (path && 'id' in variable && variable.type === type && (type !== 'number' || (path === 'opacity') === !!variable.opacity)
|
|
267
323
|
&& (!length || variables.valuesMatch(variable.id, n => typeof n === 'number' && n >= 0 && (path !== 'opacity' || n <= 1))))
|
|
268
324
|
return variable.id;
|
|
@@ -289,10 +345,48 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
289
345
|
}
|
|
290
346
|
doc.models = doc.models.filter(m => !styleModels.has(m.id));
|
|
291
347
|
const textStyles = new Map(shared.flatMap(r => r.kind === 'style' && r.type === 'text' ? [[r.id, r]] : []));
|
|
292
|
-
function palette(entry, id) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
348
|
+
function palette(entry, id, occurrence = false) {
|
|
349
|
+
if (!entry.styles)
|
|
350
|
+
return;
|
|
351
|
+
// Expanded palettes may be shared by several linked records. Strip reader metadata on a copy.
|
|
352
|
+
entry.styles = entry.styles.map((source, i) => {
|
|
353
|
+
const { fileFontSize, fileTextColor, filePlainTextColor, fileFillStyle, filePlainFillStyle, ...s } = source;
|
|
354
|
+
const { textStyle, color, paintOpacity, link: _link, figmaHyperlink: _pending, ...local } = s;
|
|
355
|
+
const style = definitions.get(fileFillStyle ?? filePlainFillStyle);
|
|
356
|
+
if (style) {
|
|
357
|
+
styleUsers.get(style.id)?.push(id);
|
|
358
|
+
if (!occurrence) {
|
|
359
|
+
if (filePlainFillStyle)
|
|
360
|
+
lost('instance text paint style kept as plain colour', id);
|
|
361
|
+
else if (supportsRunPaint && style.type === 'paint' && style.value.length === 1 && style.value[0]?.k === 'solid') {
|
|
362
|
+
s.fillStyle = style.id;
|
|
363
|
+
// The style owns both the colour (possibly an alias) and the paint opacity.
|
|
364
|
+
const paint = style.value[0];
|
|
365
|
+
if (Array.isArray(paint.c))
|
|
366
|
+
s.color = paint.c;
|
|
367
|
+
const opacity = paint.visible === false ? 0 : paint.o ?? 1;
|
|
368
|
+
if (opacity !== 1)
|
|
369
|
+
s.paintOpacity = opacity;
|
|
370
|
+
else
|
|
371
|
+
delete s.paintOpacity;
|
|
372
|
+
}
|
|
373
|
+
else
|
|
374
|
+
lost('style reference', id);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (!s.fillStyle && fileTextColor !== undefined && !occurrence && (supportsRunPaint || !style)) {
|
|
378
|
+
if (fileTextColor)
|
|
379
|
+
(entry.vars ??= {})[`styles.${i}.color`] = fileTextColor;
|
|
380
|
+
else
|
|
381
|
+
lost('variable binding', id);
|
|
382
|
+
}
|
|
383
|
+
// If linking kept the instance expanded, its explicitly bound paint still becomes a literal.
|
|
384
|
+
if (!occurrence && !filePlainFillStyle && filePlainTextColor !== undefined)
|
|
385
|
+
lost('instance text colour binding kept as plain colour', id);
|
|
386
|
+
if ((occurrence || filePlainTextColor !== undefined || style && !supportsRunPaint) && paintOpacity !== undefined) {
|
|
387
|
+
s.color = [color[0], color[1], color[2], color[3] * paintOpacity];
|
|
388
|
+
delete s.paintOpacity;
|
|
389
|
+
}
|
|
296
390
|
if (textStyle)
|
|
297
391
|
styleUsers.get(textStyle)?.push(id);
|
|
298
392
|
const styleSize = textStyles.get(textStyle)?.value.size;
|
|
@@ -305,7 +399,8 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
305
399
|
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
306
400
|
else if (fileFontSize && !(inherited && s.textStyle))
|
|
307
401
|
(entry.vars ??= {})['styles.' + i + '.size'] = fileFontSize;
|
|
308
|
-
|
|
402
|
+
return s;
|
|
403
|
+
});
|
|
309
404
|
}
|
|
310
405
|
const lost = (what, id) => report.push({ kind: 'left-out', what, count: 1, ids: [id] });
|
|
311
406
|
const isInstance = (m) => m.component && m.component !== m.id;
|
|
@@ -375,15 +470,135 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
375
470
|
(entry.vars ??= {})['radius.' + i] = variable;
|
|
376
471
|
});
|
|
377
472
|
}
|
|
473
|
+
function installStrokeSides(change, root, entry, id, instance, virtual) {
|
|
474
|
+
const override = instance || virtual;
|
|
475
|
+
const bindings = new Map(strokeSideBindings(change).map(({ variableField, variableData }) => [variableField, variableData]));
|
|
476
|
+
const key = 'look.stroke.sides';
|
|
477
|
+
const target = ['frame', 'rect'].includes(root.kind) && root.stroke && !root.boolean;
|
|
478
|
+
if (!target && bindings.size === 4) {
|
|
479
|
+
const [first, ...rest] = bindings.values();
|
|
480
|
+
// Shapes without independent widths can still use one common source for the whole stroke.
|
|
481
|
+
if (first !== undefined && rest.every(value => JSON.stringify(value) === JSON.stringify(first))) {
|
|
482
|
+
const variable = variableAt(!instance && root.stroke ? 'strokeWidth' : undefined, first, 'number', id);
|
|
483
|
+
if (variable)
|
|
484
|
+
(entry.vars ??= {}).strokeWidth = variable;
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const uniform = hasUniformStroke(change);
|
|
489
|
+
const base = root.look?.stroke?.sides ?? Array(4).fill(root.strokeWidth ?? 0);
|
|
490
|
+
const literals = strokeSides.map((side, i) => {
|
|
491
|
+
const value = change[side.field];
|
|
492
|
+
// An INSTANCE record also repeats its root's stored widths, including inherited values.
|
|
493
|
+
const cleared = bindings.has(side.binding) && bindings.get(side.binding) === undefined;
|
|
494
|
+
return override && !virtual && !uniform && !cleared && value === base[i] ? undefined : value;
|
|
495
|
+
});
|
|
496
|
+
const authored = uniform || literals.some(v => v !== undefined) || [...bindings.values()].includes(undefined);
|
|
497
|
+
if (!bindings.size && !(override && target && authored && (entry[key] || root.look?.stroke?.sides)))
|
|
498
|
+
return;
|
|
499
|
+
if (override && target && authored) {
|
|
500
|
+
const previous = entry[key] ?? base;
|
|
501
|
+
entry[key] = literals.map((v, i) => Math.max(0, (uniform ? change.strokeWeight : v) ?? previous[i]));
|
|
502
|
+
}
|
|
503
|
+
for (const [i, side] of strokeSides.entries()) {
|
|
504
|
+
const path = key + '.' + i;
|
|
505
|
+
if (bindings.has(side.binding)) {
|
|
506
|
+
const data = bindings.get(side.binding);
|
|
507
|
+
const variable = data === undefined ? undefined : variableAt(target ? path : undefined, data, 'number', id);
|
|
508
|
+
if (variable)
|
|
509
|
+
(entry.vars ??= {})[path] = variable;
|
|
510
|
+
else if (override && target)
|
|
511
|
+
(entry.vars ??= {})[path] = null;
|
|
512
|
+
}
|
|
513
|
+
else if (override && target && (uniform || literals[i] !== undefined)) {
|
|
514
|
+
(entry.vars ??= {})[path] ??= null;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
// Only frames and pages store mode choices. Other layers have no children, so a choice there changes only the
|
|
519
|
+
// layer's own bindings: through its variables, its styles' aliases and the aliases those variables follow.
|
|
520
|
+
let sharedById;
|
|
521
|
+
// Choices left unreported on a main's layer, by layer id: an instance override can still bind that layer.
|
|
522
|
+
const unused = new Map();
|
|
523
|
+
const occurrenceModes = new Map();
|
|
524
|
+
function consultsCollection(layer, collection) {
|
|
525
|
+
sharedById ??= new Map(shared.map(r => [r.id, r]));
|
|
526
|
+
const queue = [], seen = new Set();
|
|
527
|
+
const aliases = (value) => {
|
|
528
|
+
if (!value || typeof value !== 'object')
|
|
529
|
+
return;
|
|
530
|
+
if (typeof value.alias === 'string')
|
|
531
|
+
queue.push(value.alias);
|
|
532
|
+
for (const inner of Object.values(value))
|
|
533
|
+
aliases(inner);
|
|
534
|
+
};
|
|
535
|
+
for (const ref of Object.values(layer.vars ?? {}))
|
|
536
|
+
if (typeof ref === 'string')
|
|
537
|
+
queue.push(ref);
|
|
538
|
+
for (const slot of Object.keys(styled))
|
|
539
|
+
if (typeof layer[slot] === 'string')
|
|
540
|
+
queue.push(layer[slot]);
|
|
541
|
+
for (const run of layer.styles ?? []) {
|
|
542
|
+
if (run.textStyle)
|
|
543
|
+
queue.push(run.textStyle);
|
|
544
|
+
if (run.fillStyle)
|
|
545
|
+
queue.push(run.fillStyle);
|
|
546
|
+
}
|
|
547
|
+
while (queue.length) {
|
|
548
|
+
const id = queue.pop();
|
|
549
|
+
if (seen.has(id))
|
|
550
|
+
continue;
|
|
551
|
+
seen.add(id);
|
|
552
|
+
const record = sharedById.get(id);
|
|
553
|
+
if (record?.kind === 'variable') {
|
|
554
|
+
if (record.parent === collection)
|
|
555
|
+
return true;
|
|
556
|
+
aliases(record.values);
|
|
557
|
+
}
|
|
558
|
+
else if (record?.kind === 'style')
|
|
559
|
+
aliases(record.value);
|
|
560
|
+
}
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
// Occurrence entries that name a style, masked once every entry holds its own bindings.
|
|
564
|
+
const masked = [];
|
|
565
|
+
const styled = {
|
|
566
|
+
fillStyle: ['look.fills'],
|
|
567
|
+
strokeStyle: ['look.stroke.paint'],
|
|
568
|
+
effectStyle: ['look.shadows', 'look.layerBlur', 'look.backgroundBlur'],
|
|
569
|
+
};
|
|
570
|
+
function* occurrenceSources(id, outermost) {
|
|
571
|
+
const parts = id.split(';');
|
|
572
|
+
const layer = records.get(parts.at(-1));
|
|
573
|
+
if (layer && isInstance(layer)) {
|
|
574
|
+
yield records.get(layer.component);
|
|
575
|
+
yield layer;
|
|
576
|
+
yield layer.overrides?.[''];
|
|
577
|
+
}
|
|
578
|
+
else
|
|
579
|
+
yield layer;
|
|
580
|
+
for (let i = parts.length - 2; i >= (outermost ? 0 : 1); i--)
|
|
581
|
+
yield records.get(parts[i])?.overrides?.[parts.slice(i + 1).join(';')];
|
|
582
|
+
}
|
|
583
|
+
/** Variable paths bound on a virtual layer beneath its outermost instance: its main layer, then inner instances.
|
|
584
|
+
* A mask at an inner level still counts, so the outer mask holds if that inner style is later detached. */
|
|
585
|
+
function inheritedBindings(id) {
|
|
586
|
+
const paths = new Set();
|
|
587
|
+
for (const source of occurrenceSources(id, false))
|
|
588
|
+
for (const [path, ref] of Object.entries(source?.vars ?? {}))
|
|
589
|
+
if (ref)
|
|
590
|
+
paths.add(path);
|
|
591
|
+
return paths;
|
|
592
|
+
}
|
|
378
593
|
function install(change, m, into, id, virtual = false) {
|
|
379
594
|
const instance = isInstance(m);
|
|
380
595
|
const root = instance ? records.get(m.component) : m;
|
|
381
596
|
const rootBindings = instance ? new Set() : undefined;
|
|
382
597
|
const paint = instance ? appearance(root, m.overrides?.['']) : virtual ? appearance(m, into) : m;
|
|
383
|
-
const lists = {
|
|
598
|
+
const lists = { fillPaints: change.fillPaints, strokePaints: change.strokePaints, effects: change.effects };
|
|
384
599
|
for (const [field, slot, type, list] of slots) {
|
|
385
600
|
const ref = change[field];
|
|
386
|
-
if (!ref)
|
|
601
|
+
if (!ref || m.kind === 'text' && slot === 'fillStyle')
|
|
387
602
|
continue;
|
|
388
603
|
let style = ref.guid ? definitions.get(guidText(ref.guid, field)) : undefined;
|
|
389
604
|
if (style?.type !== type)
|
|
@@ -421,14 +636,18 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
421
636
|
lost('style reference', id);
|
|
422
637
|
}
|
|
423
638
|
else {
|
|
424
|
-
if (!instance)
|
|
639
|
+
if (!instance) {
|
|
425
640
|
into[slot] = style.id;
|
|
641
|
+
if (virtual)
|
|
642
|
+
masked.push([into, id, slot]);
|
|
643
|
+
}
|
|
426
644
|
delete lists[list]; // Variable-backed paints in a named style are authored on its definition.
|
|
427
645
|
}
|
|
428
646
|
}
|
|
429
647
|
const rootEntry = instance && !virtual ? m.overrides?.[''] ?? {} : into;
|
|
430
648
|
if (['frame', 'rect'].includes(m.kind))
|
|
431
649
|
installRadius(change, m, root, rootEntry, id, virtual);
|
|
650
|
+
installStrokeSides(change, root, rootEntry, id, instance, virtual);
|
|
432
651
|
const bind = binding({ id }, (path, variable) => {
|
|
433
652
|
if (virtual && (['w', 'h'].includes(path) || path.startsWith('layoutData.'))
|
|
434
653
|
|| instance && (path.startsWith('look.') || path === 'strokeWidth')) {
|
|
@@ -444,16 +663,20 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
444
663
|
if (virtual && path.startsWith('look.stroke.paint.') && !root.look?.stroke?.paint)
|
|
445
664
|
into['look.stroke.paint'] = paint.look.stroke.paint;
|
|
446
665
|
});
|
|
666
|
+
if (virtual && m.kind === 'text' && !m.bind?.content && change.variableConsumptionMap?.entries?.some((entry) => entry.variableField === 'TEXT_DATA' && entry.variableData === undefined)) {
|
|
667
|
+
// A field-only occurrence entry clears the inherited binding even when the linker found identical text.
|
|
668
|
+
(into.vars ??= {}).content = null;
|
|
669
|
+
into.content ??= expanded.get(id)?.content ?? m.content;
|
|
670
|
+
}
|
|
447
671
|
layerFields(change, m, bind);
|
|
448
672
|
paintsAndEffects(lists, paint, bind, result.sources);
|
|
449
673
|
for (const range of change.textData?.styleOverrideTable ?? []) {
|
|
674
|
+
const glyphPaint = textColor(range.fillPaints ?? []).paint;
|
|
450
675
|
for (const field of range.variableConsumptionMap?.entries ?? [])
|
|
451
676
|
if (field.variableField !== 'FONT_SIZE')
|
|
452
677
|
lost('variable binding', id);
|
|
453
|
-
if (range.styleIdForFill && !isDetachedStyle(range.styleIdForFill))
|
|
454
|
-
lost('style reference', id);
|
|
455
678
|
for (const fill of range.fillPaints ?? []) {
|
|
456
|
-
if (fill.colorVar)
|
|
679
|
+
if (fill.colorVar && fill !== glyphPaint)
|
|
457
680
|
lost('variable binding', id);
|
|
458
681
|
if (fill.opacityVar)
|
|
459
682
|
lost('variable binding', id);
|
|
@@ -467,14 +690,27 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
467
690
|
if (Object.keys(rootEntry).length)
|
|
468
691
|
(m.overrides ??= {})[''] = rootEntry;
|
|
469
692
|
}
|
|
693
|
+
const chosen = new Set();
|
|
470
694
|
if (change.variableModeBySetMap)
|
|
471
695
|
for (const choice of change.variableModeBySetMap.entries ?? []) {
|
|
472
696
|
const mode = variables.modeOf(choice, id);
|
|
473
|
-
if (typeof mode === 'string'
|
|
697
|
+
if (typeof mode === 'string')
|
|
474
698
|
lost('variable mode', id);
|
|
475
|
-
else
|
|
699
|
+
else if (['page', 'frame'].includes(m.kind))
|
|
476
700
|
(into.modes ??= {})[mode.collectionId] = mode.modeId;
|
|
701
|
+
else if (virtual)
|
|
702
|
+
chosen.add(mode.collectionId);
|
|
703
|
+
else if (consultsCollection(m, mode.collectionId))
|
|
704
|
+
lost('variable mode', id);
|
|
705
|
+
else
|
|
706
|
+
unused.set(m.id, (unused.get(m.id) ?? new Set()).add(mode.collectionId));
|
|
477
707
|
}
|
|
708
|
+
if (virtual) {
|
|
709
|
+
for (const collection of unused.get(m.id) ?? [])
|
|
710
|
+
chosen.add(collection);
|
|
711
|
+
if (chosen.size)
|
|
712
|
+
occurrenceModes.set(id, chosen);
|
|
713
|
+
}
|
|
478
714
|
}
|
|
479
715
|
// Instance appearance comes from its main, which may live on a later page.
|
|
480
716
|
const layers = doc.models;
|
|
@@ -503,6 +739,22 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
503
739
|
if (main.look?.[key] !== undefined)
|
|
504
740
|
(m.look ??= {})[key] = structuredClone(main.look[key]);
|
|
505
741
|
}
|
|
742
|
+
if (m.stroke) {
|
|
743
|
+
const own = change.variableConsumptionMap?.entries ?? [];
|
|
744
|
+
const uniform = hasUniformStroke(change);
|
|
745
|
+
const base = main.look?.stroke?.sides ?? Array(4).fill(main.strokeWidth ?? 0);
|
|
746
|
+
const literals = strokeSides.map(({ field }) => uniform ? change.strokeWeight : change[field]);
|
|
747
|
+
const inherited = strokeSideBindings(original).filter(({ variableField }) => !uniform && !own.some((entry) => entry.variableField === variableField) &&
|
|
748
|
+
!strokeSides.some(({ binding }, i) => binding === variableField && literals[i] !== undefined && literals[i] !== base[i]));
|
|
749
|
+
if (inherited.length)
|
|
750
|
+
change.variableConsumptionMap = { ...change.variableConsumptionMap, entries: [...inherited, ...own] };
|
|
751
|
+
if (literals.some(v => v !== undefined)) {
|
|
752
|
+
const stroke = { position: 0, ...m.look?.stroke };
|
|
753
|
+
const previous = stroke.sides ?? base;
|
|
754
|
+
stroke.sides = literals.map((value, i) => Math.max(0, value ?? previous[i]));
|
|
755
|
+
(m.look ??= {}).stroke = stroke;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
506
758
|
}
|
|
507
759
|
const path = [m.sourceId];
|
|
508
760
|
for (let parent = records.get(m.parent); parent; parent = records.get(parent.parent)) {
|
|
@@ -531,9 +783,32 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
531
783
|
const paths = new Set([...Object.keys(m.overrides ?? {}), ...authored?.keys() ?? []]);
|
|
532
784
|
for (const path of paths) {
|
|
533
785
|
const id = m.id + ';' + path, entry = m.overrides?.[path] ?? {};
|
|
534
|
-
palette(entry, id);
|
|
535
786
|
const change = authored?.get(path);
|
|
536
787
|
const target = path ? records.get(path.split(';').at(-1)) : m;
|
|
788
|
+
if (target?.kind === 'text' && change) {
|
|
789
|
+
const textData = change.textData;
|
|
790
|
+
const painted = [change, ...(textData?.styleOverrideTable ?? [])].filter(fields => fields.fillPaints !== undefined || fields.styleIdForFill !== undefined);
|
|
791
|
+
// Even an explicitly painted run that looks identical to its main is a plain override.
|
|
792
|
+
const layer = expanded.get(id);
|
|
793
|
+
if (painted.length && layer?.styles) {
|
|
794
|
+
// The linker packs a full text body for a paint difference. Only authored wording owns the characters.
|
|
795
|
+
if (textData?.characters !== undefined)
|
|
796
|
+
entry.content = layer.content;
|
|
797
|
+
else
|
|
798
|
+
delete entry.content;
|
|
799
|
+
entry.styles ??= layer.styles;
|
|
800
|
+
entry.styleAt ??= styleAtOf(layer);
|
|
801
|
+
for (const fields of painted) {
|
|
802
|
+
const style = fields.styleIdForFill && result.sources.styleNodeFor(fields.styleIdForFill, 'FILL');
|
|
803
|
+
const fills = style?.change.fillPaints ?? fields.fillPaints ?? [];
|
|
804
|
+
if (style || target.styles?.some((s) => s.fillStyle))
|
|
805
|
+
lost('instance text paint style kept as plain colour', id);
|
|
806
|
+
else if (textColor(fills).paint?.colorVar)
|
|
807
|
+
lost('instance text colour binding kept as plain colour', id);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
palette(entry, id, true);
|
|
537
812
|
if (path && target && !isInstance(target)) {
|
|
538
813
|
inheritedLiterals(entry, target, change ?? {}, expanded.get(id));
|
|
539
814
|
if (!change && Object.keys(target.vars ?? {}).some(key => key.startsWith('radius.')))
|
|
@@ -547,6 +822,20 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
547
822
|
delete m.overrides[path];
|
|
548
823
|
}
|
|
549
824
|
}
|
|
825
|
+
// A style override replaces the slot as Figma does: bindings the occurrence inherits there no longer apply.
|
|
826
|
+
for (const [entry, id, slot] of masked)
|
|
827
|
+
for (const path of inheritedBindings(id))
|
|
828
|
+
if (styled[slot].some(p => path.startsWith(p + '.')) && !Object.hasOwn(entry.vars ?? {}, path))
|
|
829
|
+
(entry.vars ??= {})[path] = null;
|
|
830
|
+
for (const [id, choices] of occurrenceModes) {
|
|
831
|
+
let layer = {};
|
|
832
|
+
for (const source of occurrenceSources(id, true))
|
|
833
|
+
if (source)
|
|
834
|
+
layer = { ...layer, ...source, vars: { ...layer.vars, ...source.vars } };
|
|
835
|
+
for (const collection of choices)
|
|
836
|
+
if (consultsCollection(layer, collection))
|
|
837
|
+
lost('variable mode', id);
|
|
838
|
+
}
|
|
550
839
|
// A definition can be unused. Diagnostics for its bindings select only actual layer users.
|
|
551
840
|
for (const entry of report) {
|
|
552
841
|
if (!entry.ids?.some(id => styleUsers.has(id)))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { localBox } from '@moondraft/editor/core';
|
|
2
|
+
import type { Paint } from '@moondraft/editor/schema';
|
|
3
|
+
import type { FigmaImageCrop } from '../taco/figma-import/import/carry.ts';
|
|
4
|
+
type ImagePaint = Extract<Paint, {
|
|
5
|
+
k: 'image';
|
|
6
|
+
}>;
|
|
7
|
+
type Box = ReturnType<typeof localBox>;
|
|
8
|
+
/** Places the full image in the paint box by inverting Figma's layer-to-image UV mapping. */
|
|
9
|
+
export declare function imageCropPlacement({ matrix, aspect }: FigmaImageCrop, paintBox: Box, layerBox?: {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}): {
|
|
15
|
+
fit: 1 | 2;
|
|
16
|
+
transform: NonNullable<ImagePaint['transform']>;
|
|
17
|
+
} | undefined;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { apply, inv, localBox } from '@moondraft/editor/core';
|
|
2
|
+
const tolerance = 1e-4; // The Figma matrix and box are stored as floats.
|
|
3
|
+
const near = (a, b) => Math.abs(a - b) <= tolerance * Math.max(a, b);
|
|
4
|
+
/** Places the full image in the paint box by inverting Figma's layer-to-image UV mapping. */
|
|
5
|
+
export function imageCropPlacement({ matrix, aspect }, paintBox, layerBox = paintBox) {
|
|
6
|
+
const { width, height } = layerBox;
|
|
7
|
+
const [m00, m10, m01, m11] = matrix;
|
|
8
|
+
if (!(width > 0 && height > 0 && paintBox.width > 0 && paintBox.height > 0 && m00 * m11 - m10 * m01 > 0))
|
|
9
|
+
return;
|
|
10
|
+
const imageToLayer = inv([...matrix]);
|
|
11
|
+
const [a, b, c, d] = imageToLayer;
|
|
12
|
+
const ux = a * width, uy = b * height, vx = c * width, vy = d * height;
|
|
13
|
+
const w = Math.hypot(ux, uy), h = Math.hypot(vx, vy);
|
|
14
|
+
if (!(w > 0 && h > 0) || Math.abs((ux / w) * (vx / h) + (uy / w) * (vy / h)) > tolerance)
|
|
15
|
+
return;
|
|
16
|
+
const sx = w / paintBox.width, sy = h / paintBox.height;
|
|
17
|
+
const fit = near(sx, sy) ? 2 : 1;
|
|
18
|
+
// Stretch handles equal zoom on both box axes. Otherwise a cover fit supplies the image's aspect ratio,
|
|
19
|
+
// and one of its axes spans the box exactly. When the image is unavailable, infer its ratio from the crop.
|
|
20
|
+
if (fit === 1 && aspect !== undefined && !near(w / h, aspect))
|
|
21
|
+
return;
|
|
22
|
+
const scale = Math.min(sx, sy);
|
|
23
|
+
const [cx, cy] = apply(imageToLayer, 0.5, 0.5);
|
|
24
|
+
const x = (cx * width + (layerBox.x - paintBox.x)) / paintBox.width - 0.5;
|
|
25
|
+
const y = (cy * height + (layerBox.y - paintBox.y)) / paintBox.height - 0.5;
|
|
26
|
+
const rotation = Math.atan2(uy, ux) * 180 / Math.PI + 0;
|
|
27
|
+
if (!(scale > 0) || ![x, y, scale, rotation].every(Number.isFinite))
|
|
28
|
+
return;
|
|
29
|
+
return { fit, transform: { x, y, scale, rotation } };
|
|
30
|
+
}
|
package/dist/adapter/link.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ export declare const MARKERS: string[];
|
|
|
5
5
|
* @param m - Converted model to update.
|
|
6
6
|
*/
|
|
7
7
|
export declare const unmark: (m: any) => void;
|
|
8
|
+
/** A text's style index per UTF-16 unit, from styleAt or the file's runs ([length, style]). */
|
|
9
|
+
export declare function styleAtOf(m: any): any;
|
|
10
|
+
export declare const supportsRunPaint: boolean;
|
|
8
11
|
/** What linkComponents did: models before and after, instances linked, mains on the page and hidden mains used,
|
|
9
12
|
* the instances left expanded by their first difference, and how variant sets were linked (a main per variant, or
|
|
10
13
|
* one for the set). */
|
|
@@ -39,7 +42,8 @@ export declare function linkComponents(doc: {
|
|
|
39
42
|
/** @internal Links components and tracks where their expanded layers went for import reports. */
|
|
40
43
|
export declare function linkWithLayers(doc: {
|
|
41
44
|
models: any[];
|
|
42
|
-
}, trackLayers?: boolean): {
|
|
45
|
+
}, trackLayers?: boolean, swapBinding?: (source: string) => string | undefined, nativeFile?: boolean): {
|
|
43
46
|
counts: LinkCounts;
|
|
44
47
|
aliases: Map<string, string[]>;
|
|
48
|
+
omittedLinks: Map<string, Set<string>>;
|
|
45
49
|
};
|