@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
|
@@ -1,9 +1,55 @@
|
|
|
1
|
+
import * as editorSchema from '@moondraft/editor/schema';
|
|
1
2
|
import { STYLES, VARIABLES } from '@moondraft/editor/schema';
|
|
2
|
-
import {
|
|
3
|
+
import { typographyVariable } from '../taco/figma-import/import/variables.js';
|
|
4
|
+
import { TEXT_VARIABLE_FIELDS, textColor } from '../taco/figma-import/import/converters/text.js';
|
|
5
|
+
import { applyFileTypography, supportsTextField, supportsTypographyBindings } from './file-typography.js';
|
|
6
|
+
import { variableResolver } from './variables.js';
|
|
7
|
+
import { styleAtOf, supportsRunPaint } from './link.js';
|
|
8
|
+
import { guidText, isObject } from '../taco/figma-import/import/fields.js';
|
|
3
9
|
import { isDetachedStyle, STYLE_REFERENCE_FIELDS } from '../taco/figma-import/import/styles.js';
|
|
4
10
|
import { layerPathKey, parseLayerPath } from '../taco/model/index.js';
|
|
5
11
|
import { rgba, runStyle } from './convert.js';
|
|
6
|
-
|
|
12
|
+
import { keepDescription } from './figma-descriptions.js';
|
|
13
|
+
// Editor 0.3.0's closed variable schema predates both metadata fields.
|
|
14
|
+
const scopeNames = 'VARIABLE_SCOPES' in editorSchema ? new Set(editorSchema.VARIABLE_SCOPES) : undefined;
|
|
15
|
+
const syntaxPlatforms = new Map([['WEB', 'web'], ['ANDROID', 'android'], ['iOS', 'ios']]);
|
|
16
|
+
function keepVariableMetadata(node, record, report) {
|
|
17
|
+
const { variableScopes, codeSyntax } = node.change;
|
|
18
|
+
const leftOut = (what) => report.push({ kind: 'left-out', what, count: 1 });
|
|
19
|
+
if (!scopeNames) {
|
|
20
|
+
if (Array.isArray(variableScopes) || codeSyntax !== undefined)
|
|
21
|
+
leftOut('variable metadata requires a newer editor');
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(variableScopes)) {
|
|
25
|
+
record.scopes = [];
|
|
26
|
+
for (const raw of variableScopes) {
|
|
27
|
+
const scope = raw === 'STROKE' ? 'STROKE_COLOR' : raw;
|
|
28
|
+
if (typeof scope === 'string' && scopeNames.has(scope))
|
|
29
|
+
record.scopes.push(scope);
|
|
30
|
+
else
|
|
31
|
+
leftOut('unsupported variable scope');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (codeSyntax === undefined)
|
|
35
|
+
return;
|
|
36
|
+
if (!isObject(codeSyntax) || codeSyntax.entries !== undefined && !Array.isArray(codeSyntax.entries)) {
|
|
37
|
+
leftOut('unsupported variable code syntax');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
record.codeSyntax = {};
|
|
41
|
+
for (const entry of codeSyntax.entries ?? []) {
|
|
42
|
+
if (isObject(entry) && typeof entry.platform === 'string' && typeof entry.value === 'string') {
|
|
43
|
+
const platform = syntaxPlatforms.get(entry.platform);
|
|
44
|
+
if (platform) {
|
|
45
|
+
record.codeSyntax[platform] = entry.value;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
leftOut('unsupported variable code syntax entry');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function fileVariables(result, shared, identity, report) {
|
|
7
53
|
const variables = result.sources.variables;
|
|
8
54
|
for (const [id, c] of Object.entries(result.variableCollections)) {
|
|
9
55
|
shared.push({ ...identity(variables.sources.get(id)), id, parent: VARIABLES, kind: 'collection', name: c.name,
|
|
@@ -13,13 +59,24 @@ function fileVariables(result, shared, identity) {
|
|
|
13
59
|
const values = Object.fromEntries(Object.entries(v.values).map(([mode, value]) => [mode, v.type === 'color' ? rgba(value) : value]));
|
|
14
60
|
for (const [mode, alias] of variables.aliases.get(varId) ?? [])
|
|
15
61
|
values[mode] = { alias };
|
|
16
|
-
|
|
62
|
+
const record = { ...identity(node), id: varId, parent: id, name: v.name, kind: 'variable', type: v.type, values };
|
|
63
|
+
keepDescription(node, record, report);
|
|
64
|
+
keepVariableMetadata(node, record, report);
|
|
65
|
+
shared.push(record);
|
|
17
66
|
}
|
|
18
67
|
}
|
|
19
68
|
}
|
|
20
69
|
const corners = ['rectangleTopLeftCornerRadius', 'rectangleTopRightCornerRadius', 'rectangleBottomRightCornerRadius', 'rectangleBottomLeftCornerRadius'];
|
|
70
|
+
const strokeSides = ['Top', 'Right', 'Bottom', 'Left'].map(side => ({
|
|
71
|
+
field: 'border' + side + 'Weight', binding: 'BORDER_' + side.toUpperCase() + '_WEIGHT',
|
|
72
|
+
}));
|
|
73
|
+
const isStrokeSideBinding = (field) => strokeSides.some(side => side.binding === field);
|
|
74
|
+
const strokeSideBindings = (change) => (change.variableConsumptionMap?.entries ?? []).filter((entry) => isStrokeSideBinding(entry.variableField));
|
|
75
|
+
const hasUniformStroke = (change) => change.borderStrokeWeightsIndependent === false ||
|
|
76
|
+
change.strokeWeight !== undefined && !change.borderStrokeWeightsIndependent;
|
|
21
77
|
const fields = {
|
|
22
78
|
WIDTH: 'w', HEIGHT: 'h', STROKE_WEIGHT: 'strokeWidth', OPACITY: 'opacity', VISIBLE: 'visible', TEXT_DATA: 'content',
|
|
79
|
+
GRID_COLUMN_GAP: 'layoutData.layout.columnGap', GRID_ROW_GAP: 'layoutData.layout.rowGap',
|
|
23
80
|
STACK_SPACING: 'layoutData.layout.gap', STACK_COUNTER_SPACING: 'layoutData.layout.wrapGap',
|
|
24
81
|
CORNER_RADIUS: 'radius', RECTANGLE_TOP_LEFT_CORNER_RADIUS: 'radius.0', RECTANGLE_TOP_RIGHT_CORNER_RADIUS: 'radius.1',
|
|
25
82
|
RECTANGLE_BOTTOM_RIGHT_CORNER_RADIUS: 'radius.2', RECTANGLE_BOTTOM_LEFT_CORNER_RADIUS: 'radius.3',
|
|
@@ -28,6 +85,8 @@ const fields = {
|
|
|
28
85
|
function paintsAndEffects(change, m, bind, indices) {
|
|
29
86
|
for (const [key, prefix] of [['fillPaints', 'look.fills'], ['strokePaints', 'look.stroke.paint']]) {
|
|
30
87
|
const paints = change[key] ?? [];
|
|
88
|
+
if (!paints.length)
|
|
89
|
+
continue;
|
|
31
90
|
if (key === 'fillPaints' && m.fill && paints.some(p => p.colorVar)) {
|
|
32
91
|
(m.look ??= {}).fills = [{ k: 'solid', c: m.fill, o: 1 }];
|
|
33
92
|
delete m.fill;
|
|
@@ -37,12 +96,13 @@ function paintsAndEffects(change, m, bind, indices) {
|
|
|
37
96
|
const order = indices.paintIndices.get(paints) ?? paints.map((_, i) => i);
|
|
38
97
|
const byIndex = new Map(order.map((index, i) => [index, i]));
|
|
39
98
|
const firstStroke = order.find(i => paints[i]?.visible !== false);
|
|
99
|
+
const glyphPaint = key === 'fillPaints' && m.kind === 'text' ? textColor(paints).paint : undefined;
|
|
40
100
|
paints.forEach((paint, i) => {
|
|
41
101
|
const fill = key === 'fillPaints';
|
|
42
102
|
const index = byIndex.get(i);
|
|
43
103
|
const output = fill ? index !== undefined && m.look?.fills?.[index] : i === firstStroke && m.look?.stroke?.paint;
|
|
44
104
|
const path = fill ? prefix + '.' + index : prefix;
|
|
45
|
-
if (paint.colorVar)
|
|
105
|
+
if (paint.colorVar && paint !== glyphPaint)
|
|
46
106
|
bind(output?.k === 'solid' ? path + '.c' : undefined, paint.colorVar, 'color');
|
|
47
107
|
if (paint.opacityVar)
|
|
48
108
|
bind(undefined, paint.opacityVar, 'number');
|
|
@@ -141,15 +201,15 @@ export function expandedRadiusRoots(result) {
|
|
|
141
201
|
}
|
|
142
202
|
function layerFields(change, m, bind) {
|
|
143
203
|
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
204
|
for (const { variableField, variableData } of entries) {
|
|
149
|
-
if (
|
|
150
|
-
continue;
|
|
151
|
-
if (variableField
|
|
205
|
+
if (isStrokeSideBinding(variableField))
|
|
206
|
+
continue; // Installed per side.
|
|
207
|
+
if (Object.hasOwn(TEXT_VARIABLE_FIELDS, variableField) && m.styles)
|
|
152
208
|
continue; // Installed per range below.
|
|
209
|
+
if (variableField === 'TEXT_DATA' && variableData === undefined)
|
|
210
|
+
continue; // An empty entry names no variable.
|
|
211
|
+
if (variableField === 'VARIANT_PROPERTIES')
|
|
212
|
+
continue; // Installed per variant property (install).
|
|
153
213
|
let path = Object.hasOwn(fields, variableField) ? fields[variableField] : undefined;
|
|
154
214
|
if (path?.startsWith('radius') && ['frame', 'rect'].includes(m.kind))
|
|
155
215
|
continue; // Installed per effective corner.
|
|
@@ -159,7 +219,36 @@ function layerFields(change, m, bind) {
|
|
|
159
219
|
|| path === 'strokeWidth' && (!m.stroke || m.component && m.component !== m.id)
|
|
160
220
|
|| path && m.bind?.[path])
|
|
161
221
|
path = undefined;
|
|
162
|
-
|
|
222
|
+
// Figma binds visibility to a boolean variable through IS_TRUTHY(variable).
|
|
223
|
+
const truthy = path === 'visible' && variableData?.dataType === 'EXPRESSION' && variableData.value?.expressionValue;
|
|
224
|
+
const data = truthy && truthy.expressionFunction === 'IS_TRUTHY' && truthy.expressionArguments?.length === 1 ? truthy.expressionArguments[0] : variableData;
|
|
225
|
+
bind(path, data, path === 'visible' ? 'boolean' : path === 'content' ? 'string' : 'number');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Binds an instance's variant choices: Figma's RESOLVE_VARIANT expression maps variant property names to string or
|
|
230
|
+
* boolean variables. Each becomes `component.<property id>` on the instance (or its occurrence entry `into`); an
|
|
231
|
+
* expression of another kind, a property its set does not declare, or a layer that is not an instance is reported.
|
|
232
|
+
*/
|
|
233
|
+
function variantFields(change, m, into, records, bind, lost) {
|
|
234
|
+
for (const { variableField, variableData } of change.variableConsumptionMap?.entries ?? []) {
|
|
235
|
+
if (variableField !== 'VARIANT_PROPERTIES' || variableData === undefined)
|
|
236
|
+
continue;
|
|
237
|
+
const expression = variableData.dataType === 'EXPRESSION' ? variableData.value?.expressionValue : undefined;
|
|
238
|
+
const main = m.component && m.component !== m.id ? records.get(into.component ?? m.component) : undefined;
|
|
239
|
+
const props = main?.variant ? records.get(main.parent)?.variantSet?.props : undefined;
|
|
240
|
+
const choices = expression?.expressionFunction === 'RESOLVE_VARIANT' ? expression.expressionArguments?.[0]?.value?.mapValue?.values : undefined;
|
|
241
|
+
if (!props || !Array.isArray(choices)) {
|
|
242
|
+
lost();
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
for (const { key, value } of choices) {
|
|
246
|
+
const prop = props.find((p) => p.kind === 'variant' && p.name === key);
|
|
247
|
+
if (prop)
|
|
248
|
+
bind('component.' + prop.id, value, value?.resolvedDataType === 'BOOLEAN' ? 'boolean' : 'string');
|
|
249
|
+
else
|
|
250
|
+
lost();
|
|
251
|
+
}
|
|
163
252
|
}
|
|
164
253
|
}
|
|
165
254
|
// Linking compares painted instances with their mains. A mode difference in that comparison is derived:
|
|
@@ -195,7 +284,8 @@ function inheritedLiterals(entry, template, change, expanded, rootBindings) {
|
|
|
195
284
|
for (const [key, field] of [['visible', 'visible'], ['opacity', 'opacity'], ['content', 'textData']]) {
|
|
196
285
|
if (rootBindings || !inherited(key))
|
|
197
286
|
continue;
|
|
198
|
-
|
|
287
|
+
const authored = key === 'content' ? change.textData?.characters !== undefined : field in change;
|
|
288
|
+
if (!authored)
|
|
199
289
|
delete entry[key];
|
|
200
290
|
else if (!(key in entry))
|
|
201
291
|
entry[key] = key === 'content' ? expanded.content : change[field];
|
|
@@ -216,6 +306,7 @@ function sameStyleValue(style, literal) {
|
|
|
216
306
|
return false;
|
|
217
307
|
return Object.keys({ ...style, ...literal }).every(k => k === 'o' ? (style.o ?? 1) === (literal.o ?? 1) : sameStyleValue(style[k], literal[k]));
|
|
218
308
|
}
|
|
309
|
+
const TYPOGRAPHY_FIELDS = ['size', 'family', 'style', 'weight', 'lineHeight', 'letterSpacing'];
|
|
219
310
|
/** Writes the shared resources, layer references and mode choices of a saved file.
|
|
220
311
|
* @param result - Reader output retaining the source relationships.
|
|
221
312
|
* @param doc - Converted layers to associate with their definitions.
|
|
@@ -230,7 +321,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
230
321
|
const { tree, variables, imported, styleLayers, textStyleUses } = result.sources;
|
|
231
322
|
const shared = [];
|
|
232
323
|
const identity = node => ({ name: node.name, key: String(node.change.key || `figma-${fileId()}-${node.id}`) });
|
|
233
|
-
fileVariables(result, shared, identity);
|
|
324
|
+
fileVariables(result, shared, identity, report);
|
|
234
325
|
const bySource = new Map(doc.models.map((m) => [m.sourceId, m]));
|
|
235
326
|
const layerSource = new Map(imported.map(({ figma, node }) => [node.id, { figma, node }]));
|
|
236
327
|
const definitions = new Map();
|
|
@@ -239,30 +330,40 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
239
330
|
const styleUsers = new Map();
|
|
240
331
|
const records = new Map(doc.models.map(m => [m.id, m]));
|
|
241
332
|
function style(node, record) {
|
|
333
|
+
keepDescription(node, record, report);
|
|
242
334
|
definitions.set(node.id, record);
|
|
243
335
|
byKey.set(record.type + '/' + record.key, record);
|
|
244
336
|
shared.push(record);
|
|
245
337
|
}
|
|
246
|
-
const
|
|
338
|
+
const textStyleBindings = new Map();
|
|
339
|
+
const resolveLiteral = supportsTypographyBindings ? undefined : variableResolver(result.variableCollections);
|
|
247
340
|
for (const [node, use] of textStyleUses) {
|
|
248
341
|
const { id, style: text } = use.shared;
|
|
249
|
-
const
|
|
250
|
-
textDefinitions.set(id, value);
|
|
342
|
+
const literal = runStyle(text);
|
|
251
343
|
styleUsers.set(id, []);
|
|
252
344
|
const fields = node.change.variableConsumptionMap?.entries ?? [];
|
|
253
|
-
|
|
254
|
-
|
|
345
|
+
const aliases = {};
|
|
346
|
+
for (const { variableField, variableData } of fields) {
|
|
347
|
+
const field = Object.hasOwn(TEXT_VARIABLE_FIELDS, variableField) && TEXT_VARIABLE_FIELDS[variableField][0];
|
|
348
|
+
const alias = field && typographyVariable(variables, variableField, variableData, node.id);
|
|
349
|
+
if (field && alias)
|
|
350
|
+
aliases[field] = alias;
|
|
351
|
+
if (!field || !alias || !supportsTextField(field))
|
|
255
352
|
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
353
|
+
}
|
|
354
|
+
textStyleBindings.set(id, aliases);
|
|
355
|
+
const percentTracking = node.change.letterSpacing?.units === 'PERCENT';
|
|
356
|
+
applyFileTypography(literal, aliases, percentTracking, id => resolveLiteral?.(id, {}, variables?.aliases));
|
|
357
|
+
const { color, ...value } = literal;
|
|
358
|
+
const definition = { ...value };
|
|
359
|
+
for (const [field, alias] of Object.entries(aliases))
|
|
360
|
+
if (supportsTextField(field))
|
|
361
|
+
Object.assign(definition, { [field]: { alias } });
|
|
362
|
+
style(node, { ...identity(node), id, parent: STYLES, kind: 'style', type: 'text', value: definition });
|
|
262
363
|
}
|
|
263
364
|
function variableAt(path, data, type, id) {
|
|
264
365
|
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);
|
|
366
|
+
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
367
|
if (path && 'id' in variable && variable.type === type && (type !== 'number' || (path === 'opacity') === !!variable.opacity)
|
|
267
368
|
&& (!length || variables.valuesMatch(variable.id, n => typeof n === 'number' && n >= 0 && (path !== 'opacity' || n <= 1))))
|
|
268
369
|
return variable.id;
|
|
@@ -289,23 +390,210 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
289
390
|
}
|
|
290
391
|
doc.models = doc.models.filter(m => !styleModels.has(m.id));
|
|
291
392
|
const textStyles = new Map(shared.flatMap(r => r.kind === 'style' && r.type === 'text' ? [[r.id, r]] : []));
|
|
292
|
-
function palette(entry, id) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
393
|
+
function palette(entry, id, occurrence = false) {
|
|
394
|
+
if (!entry.styles)
|
|
395
|
+
return;
|
|
396
|
+
const inherited = {};
|
|
397
|
+
if (occurrence)
|
|
398
|
+
for (const source of occurrenceSources(id, false))
|
|
399
|
+
Object.assign(inherited, source?.vars);
|
|
400
|
+
const reaches = occurrence ? inheritedReach(entry, id) : () => undefined;
|
|
401
|
+
// Expanded palettes may be shared by several linked records. Strip reader metadata on a copy.
|
|
402
|
+
const original = entry.styles;
|
|
403
|
+
entry.styles = original.map((source, i) => {
|
|
404
|
+
const { fileTypography = {}, fileOwnTypography = {}, fileTextColor, filePlainTextColor, fileFillStyle, filePlainFillStyle, ...s } = source;
|
|
405
|
+
const { textStyle, color, paintOpacity, link: _link, figmaHyperlink: _pending, ...local } = s;
|
|
406
|
+
const style = definitions.get(fileFillStyle ?? filePlainFillStyle);
|
|
407
|
+
if (style) {
|
|
408
|
+
styleUsers.get(style.id)?.push(id);
|
|
409
|
+
if (!occurrence) {
|
|
410
|
+
if (filePlainFillStyle)
|
|
411
|
+
lost('instance text paint style kept as plain colour', id);
|
|
412
|
+
else if (supportsRunPaint && style.type === 'paint' && style.value.length === 1 && style.value[0]?.k === 'solid') {
|
|
413
|
+
s.fillStyle = style.id;
|
|
414
|
+
// The style owns both the colour (possibly an alias) and the paint opacity.
|
|
415
|
+
const paint = style.value[0];
|
|
416
|
+
if (Array.isArray(paint.c))
|
|
417
|
+
s.color = paint.c;
|
|
418
|
+
const opacity = paint.visible === false ? 0 : paint.o ?? 1;
|
|
419
|
+
if (opacity !== 1)
|
|
420
|
+
s.paintOpacity = opacity;
|
|
421
|
+
else
|
|
422
|
+
delete s.paintOpacity;
|
|
423
|
+
}
|
|
424
|
+
else
|
|
425
|
+
lost('style reference', id);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (!s.fillStyle && fileTextColor !== undefined && !occurrence && (supportsRunPaint || !style)) {
|
|
429
|
+
if (fileTextColor)
|
|
430
|
+
(entry.vars ??= {})[`styles.${i}.color`] = fileTextColor;
|
|
431
|
+
else
|
|
432
|
+
lost('variable binding', id);
|
|
433
|
+
}
|
|
434
|
+
// If linking kept the instance expanded, its explicitly bound paint still becomes a literal.
|
|
435
|
+
if (!occurrence && !filePlainFillStyle && filePlainTextColor !== undefined)
|
|
436
|
+
lost('instance text colour binding kept as plain colour', id);
|
|
437
|
+
if ((occurrence || filePlainTextColor !== undefined || style && !supportsRunPaint) && paintOpacity !== undefined) {
|
|
438
|
+
s.color = [color[0], color[1], color[2], color[3] * paintOpacity];
|
|
439
|
+
delete s.paintOpacity;
|
|
440
|
+
}
|
|
296
441
|
if (textStyle)
|
|
297
442
|
styleUsers.get(textStyle)?.push(id);
|
|
298
|
-
const
|
|
299
|
-
const
|
|
300
|
-
|
|
443
|
+
const definition = textStyles.get(textStyle)?.value;
|
|
444
|
+
const refs = Object.entries(fileTypography);
|
|
445
|
+
const sharedRefs = textStyleBindings.get(textStyle);
|
|
446
|
+
const sameSources = refs.every(([field, ref]) => ref === null || sharedRefs?.[field] === ref) &&
|
|
447
|
+
Object.entries(sharedRefs ?? {}).every(([field, ref]) => ref === fileTypography[field]);
|
|
448
|
+
if (textStyle && (!sameSources || !sameStyleValue(definition, local))) {
|
|
301
449
|
delete s.textStyle;
|
|
302
450
|
report.push({ kind: 'approximated', what: 'text style with local formatting', count: 1, ids: [id] });
|
|
303
451
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
452
|
+
for (const [field, ref] of refs) {
|
|
453
|
+
if (!supportsTextField(field)) {
|
|
454
|
+
if (!s.textStyle || !ref || sharedRefs?.[field] !== ref)
|
|
455
|
+
lost('variable binding', id);
|
|
456
|
+
}
|
|
457
|
+
else if (ref === null)
|
|
458
|
+
lost('variable binding', id);
|
|
459
|
+
// An occurrence's own binding stays its own, even to the main's variable, so a later main rebind leaves it.
|
|
460
|
+
else if (!s.textStyle && (reaches(i, field) !== ref || fileOwnTypography[field] === ref))
|
|
461
|
+
(entry.vars ??= {})[`styles.${i}.${field}`] = ref;
|
|
462
|
+
}
|
|
463
|
+
return s;
|
|
464
|
+
});
|
|
465
|
+
// A plain occurrence value masks the main's source on each own slot that source reaches, including an
|
|
466
|
+
// equal-valued override. A slot the main's range binding crosses keeps its value unmasked (reported by the editor).
|
|
467
|
+
if (occurrence)
|
|
468
|
+
for (let i = 0; i < original.length; i++)
|
|
469
|
+
for (const field of TYPOGRAPHY_FIELDS) {
|
|
470
|
+
const path = `styles.${i}.${field}`;
|
|
471
|
+
if (reaches(i, field) && !entry.vars?.[path] && !original[i]?.fileTypography?.[field])
|
|
472
|
+
(entry.vars ??= {})[path] = null;
|
|
473
|
+
}
|
|
474
|
+
if (occurrence)
|
|
475
|
+
followInheritedPaint(entry, id, inherited);
|
|
476
|
+
}
|
|
477
|
+
/** The main's typography reference that reaches each own slot of an occurrence palette, the way the editor maps it
|
|
478
|
+
* (core/components.ts inheritRunBindings): a binding over the whole inherited text reaches every slot; a range binding
|
|
479
|
+
* reaches a slot only if it covers all of that slot's characters, and none when the occurrence has its own text. */
|
|
480
|
+
function inheritedReach(entry, id) {
|
|
481
|
+
const { vars, at: baseAt, content } = inheritedTypography(id);
|
|
482
|
+
const ownContent = 'content' in entry && entry.content !== content;
|
|
483
|
+
const reach = runReach(vars, baseAt, styleAtOf(entry), ownContent);
|
|
484
|
+
return (slot, field) => reach.get(field)?.get(slot) ?? undefined;
|
|
485
|
+
}
|
|
486
|
+
/** Per typography field and own slot, the reference `inherited` (keyed by the slots of `before`) gives all of the
|
|
487
|
+
* slot's characters in `after`, or `null` once they differ (a crossing). O(the text) per field. */
|
|
488
|
+
function runReach(inherited, before, after, ownContent) {
|
|
489
|
+
const at = (runs, c) => runs.length ? runs[Math.min(c, runs.length - 1)] : 0;
|
|
490
|
+
const out = new Map();
|
|
491
|
+
for (const field of TYPOGRAPHY_FIELDS) {
|
|
492
|
+
const refAt = (c) => inherited[`styles.${at(before, c)}.${field}`];
|
|
493
|
+
let whole = refAt(0), bound = whole !== undefined;
|
|
494
|
+
for (let c = 1; c < Math.max(before.length, 1) && (whole !== undefined || !bound); c++) {
|
|
495
|
+
const ref = refAt(c);
|
|
496
|
+
if (ref !== whole)
|
|
497
|
+
whole = undefined;
|
|
498
|
+
if (ref !== undefined)
|
|
499
|
+
bound = true;
|
|
500
|
+
}
|
|
501
|
+
if (!bound)
|
|
502
|
+
continue;
|
|
503
|
+
const slots = new Map(), seen = new Set();
|
|
504
|
+
out.set(field, slots);
|
|
505
|
+
if (whole !== undefined) {
|
|
506
|
+
for (let c = 0; c < Math.max(after.length, 1); c++)
|
|
507
|
+
slots.set(at(after, c), whole);
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (ownContent)
|
|
511
|
+
continue;
|
|
512
|
+
for (let c = 0; c < Math.max(after.length, 1); c++) {
|
|
513
|
+
const s = at(after, c), ref = refAt(c) ?? null;
|
|
514
|
+
if (!seen.has(s)) {
|
|
515
|
+
seen.add(s);
|
|
516
|
+
slots.set(s, ref);
|
|
517
|
+
}
|
|
518
|
+
else if (slots.get(s) !== ref)
|
|
519
|
+
slots.set(s, null);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
/** The typography bindings an occurrence inherits beneath its own level, keyed by the slots of the innermost level
|
|
525
|
+
* that styles it, with that level's runs and text. Mapped level by level from the main layer out, as the editor
|
|
526
|
+
* resolves nested instances (core/components.ts applyOverride): each level that brings its own palette takes the
|
|
527
|
+
* bindings below it by character; its own bindings and masks then replace them on its slots. */
|
|
528
|
+
function inheritedTypography(id) {
|
|
529
|
+
let vars = {}, at = [], content, styled = false;
|
|
530
|
+
const run = /^styles\.(\d+)\.(size|family|style|weight|lineHeight|letterSpacing)$/;
|
|
531
|
+
const own = (source) => {
|
|
532
|
+
for (const [path, ref] of Object.entries(source.vars ?? {})) {
|
|
533
|
+
if (!run.test(path))
|
|
534
|
+
continue;
|
|
535
|
+
if (ref)
|
|
536
|
+
vars[path] = ref;
|
|
537
|
+
else
|
|
538
|
+
delete vars[path];
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
for (const source of occurrenceSources(id, false)) {
|
|
542
|
+
if (!source)
|
|
543
|
+
continue;
|
|
544
|
+
if (styled && source.styles) {
|
|
545
|
+
const after = styleAtOf(source), reach = runReach(vars, at, after, 'content' in source && source.content !== content);
|
|
546
|
+
vars = {};
|
|
547
|
+
own(source);
|
|
548
|
+
for (const [field, slots] of reach)
|
|
549
|
+
for (const [slot, ref] of slots) {
|
|
550
|
+
const path = `styles.${slot}.${field}`;
|
|
551
|
+
if (ref && !source.styles[slot]?.textStyle && !Object.hasOwn(source.vars ?? {}, path))
|
|
552
|
+
vars[path] = ref;
|
|
553
|
+
}
|
|
554
|
+
at = after;
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
own(source);
|
|
558
|
+
if (source.styles)
|
|
559
|
+
at = styleAtOf(source);
|
|
560
|
+
}
|
|
561
|
+
if ('content' in source)
|
|
562
|
+
content = source.content;
|
|
563
|
+
styled ||= !!source.styles;
|
|
308
564
|
}
|
|
565
|
+
return { vars, at, content };
|
|
566
|
+
}
|
|
567
|
+
// Fields a typography token also decides when it resolves (shared.ts resolveTypography, resizedStyle).
|
|
568
|
+
const resolvedWith = {
|
|
569
|
+
family: ['faceId'], style: ['weight', 'italic', 'faceId', 'variations'], weight: ['style', 'faceId', 'variations'],
|
|
570
|
+
size: ['lineHeight', 'letterSpacing'], lineHeight: ['lineHeightMode'], letterSpacing: [],
|
|
571
|
+
};
|
|
572
|
+
/** An occurrence palette that differs from the inherited one only in its own typography tokens stays derived, as the
|
|
573
|
+
* editor's own binding writes it, so the main's text colour source keeps drawing it. */
|
|
574
|
+
function followInheritedPaint(entry, id, inherited) {
|
|
575
|
+
let base;
|
|
576
|
+
for (const source of occurrenceSources(id, false))
|
|
577
|
+
if (source?.styles)
|
|
578
|
+
base = source;
|
|
579
|
+
const bound = Object.entries(entry.vars ?? {}).flatMap(([path, ref]) => {
|
|
580
|
+
const match = ref ? /^styles\.(\d+)\.(size|family|style|weight|lineHeight|letterSpacing)$/.exec(path) : null;
|
|
581
|
+
return match ? [[Number(match[1]), match[2]]] : [];
|
|
582
|
+
});
|
|
583
|
+
if (!base || !bound.length || entry.styles.length !== base.styles.length)
|
|
584
|
+
return;
|
|
585
|
+
if (!Object.entries(inherited).some(([path, ref]) => ref && /^styles\.\d+\.color$/.test(path)) && !base.styles.some((s) => s.fillStyle))
|
|
586
|
+
return;
|
|
587
|
+
if (entry.styleAt && !sameStyleValue(entry.styleAt, styleAtOf(base)))
|
|
588
|
+
return;
|
|
589
|
+
const free = entry.styles.map(() => new Set(['color', 'paintOpacity', 'fillStyle']));
|
|
590
|
+
for (const [slot, field] of bound)
|
|
591
|
+
for (const key of [field, ...resolvedWith[field]])
|
|
592
|
+
free[slot].add(key);
|
|
593
|
+
const derived = entry.styles.every((run, i) => Object.keys({ ...run, ...base.styles[i] })
|
|
594
|
+
.every(key => free[i].has(key) || sameStyleValue(run[key], base.styles[i][key])));
|
|
595
|
+
if (derived)
|
|
596
|
+
entry.sharedFields = ['styles'];
|
|
309
597
|
}
|
|
310
598
|
const lost = (what, id) => report.push({ kind: 'left-out', what, count: 1, ids: [id] });
|
|
311
599
|
const isInstance = (m) => m.component && m.component !== m.id;
|
|
@@ -375,15 +663,135 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
375
663
|
(entry.vars ??= {})['radius.' + i] = variable;
|
|
376
664
|
});
|
|
377
665
|
}
|
|
666
|
+
function installStrokeSides(change, root, entry, id, instance, virtual) {
|
|
667
|
+
const override = instance || virtual;
|
|
668
|
+
const bindings = new Map(strokeSideBindings(change).map(({ variableField, variableData }) => [variableField, variableData]));
|
|
669
|
+
const key = 'look.stroke.sides';
|
|
670
|
+
const target = ['frame', 'rect'].includes(root.kind) && root.stroke && !root.boolean;
|
|
671
|
+
if (!target && bindings.size === 4) {
|
|
672
|
+
const [first, ...rest] = bindings.values();
|
|
673
|
+
// Shapes without independent widths can still use one common source for the whole stroke.
|
|
674
|
+
if (first !== undefined && rest.every(value => JSON.stringify(value) === JSON.stringify(first))) {
|
|
675
|
+
const variable = variableAt(!instance && root.stroke ? 'strokeWidth' : undefined, first, 'number', id);
|
|
676
|
+
if (variable)
|
|
677
|
+
(entry.vars ??= {}).strokeWidth = variable;
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const uniform = hasUniformStroke(change);
|
|
682
|
+
const base = root.look?.stroke?.sides ?? Array(4).fill(root.strokeWidth ?? 0);
|
|
683
|
+
const literals = strokeSides.map((side, i) => {
|
|
684
|
+
const value = change[side.field];
|
|
685
|
+
// An INSTANCE record also repeats its root's stored widths, including inherited values.
|
|
686
|
+
const cleared = bindings.has(side.binding) && bindings.get(side.binding) === undefined;
|
|
687
|
+
return override && !virtual && !uniform && !cleared && value === base[i] ? undefined : value;
|
|
688
|
+
});
|
|
689
|
+
const authored = uniform || literals.some(v => v !== undefined) || [...bindings.values()].includes(undefined);
|
|
690
|
+
if (!bindings.size && !(override && target && authored && (entry[key] || root.look?.stroke?.sides)))
|
|
691
|
+
return;
|
|
692
|
+
if (override && target && authored) {
|
|
693
|
+
const previous = entry[key] ?? base;
|
|
694
|
+
entry[key] = literals.map((v, i) => Math.max(0, (uniform ? change.strokeWeight : v) ?? previous[i]));
|
|
695
|
+
}
|
|
696
|
+
for (const [i, side] of strokeSides.entries()) {
|
|
697
|
+
const path = key + '.' + i;
|
|
698
|
+
if (bindings.has(side.binding)) {
|
|
699
|
+
const data = bindings.get(side.binding);
|
|
700
|
+
const variable = data === undefined ? undefined : variableAt(target ? path : undefined, data, 'number', id);
|
|
701
|
+
if (variable)
|
|
702
|
+
(entry.vars ??= {})[path] = variable;
|
|
703
|
+
else if (override && target)
|
|
704
|
+
(entry.vars ??= {})[path] = null;
|
|
705
|
+
}
|
|
706
|
+
else if (override && target && (uniform || literals[i] !== undefined)) {
|
|
707
|
+
(entry.vars ??= {})[path] ??= null;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
// Only frames and pages store mode choices. Other layers have no children, so a choice there changes only the
|
|
712
|
+
// layer's own bindings: through its variables, its styles' aliases and the aliases those variables follow.
|
|
713
|
+
let sharedById;
|
|
714
|
+
// Choices left unreported on a main's layer, by layer id: an instance override can still bind that layer.
|
|
715
|
+
const unused = new Map();
|
|
716
|
+
const occurrenceModes = new Map();
|
|
717
|
+
function consultsCollection(layer, collection) {
|
|
718
|
+
sharedById ??= new Map(shared.map(r => [r.id, r]));
|
|
719
|
+
const queue = [], seen = new Set();
|
|
720
|
+
const aliases = (value) => {
|
|
721
|
+
if (!value || typeof value !== 'object')
|
|
722
|
+
return;
|
|
723
|
+
if (typeof value.alias === 'string')
|
|
724
|
+
queue.push(value.alias);
|
|
725
|
+
for (const inner of Object.values(value))
|
|
726
|
+
aliases(inner);
|
|
727
|
+
};
|
|
728
|
+
for (const ref of Object.values(layer.vars ?? {}))
|
|
729
|
+
if (typeof ref === 'string')
|
|
730
|
+
queue.push(ref);
|
|
731
|
+
for (const slot of Object.keys(styled))
|
|
732
|
+
if (typeof layer[slot] === 'string')
|
|
733
|
+
queue.push(layer[slot]);
|
|
734
|
+
for (const run of layer.styles ?? []) {
|
|
735
|
+
if (run.textStyle)
|
|
736
|
+
queue.push(run.textStyle);
|
|
737
|
+
if (run.fillStyle)
|
|
738
|
+
queue.push(run.fillStyle);
|
|
739
|
+
}
|
|
740
|
+
while (queue.length) {
|
|
741
|
+
const id = queue.pop();
|
|
742
|
+
if (seen.has(id))
|
|
743
|
+
continue;
|
|
744
|
+
seen.add(id);
|
|
745
|
+
const record = sharedById.get(id);
|
|
746
|
+
if (record?.kind === 'variable') {
|
|
747
|
+
if (record.parent === collection)
|
|
748
|
+
return true;
|
|
749
|
+
aliases(record.values);
|
|
750
|
+
}
|
|
751
|
+
else if (record?.kind === 'style')
|
|
752
|
+
aliases(record.value);
|
|
753
|
+
}
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
// Occurrence entries that name a style, masked once every entry holds its own bindings.
|
|
757
|
+
const masked = [];
|
|
758
|
+
const styled = {
|
|
759
|
+
fillStyle: ['look.fills'],
|
|
760
|
+
strokeStyle: ['look.stroke.paint'],
|
|
761
|
+
effectStyle: ['look.shadows', 'look.layerBlur', 'look.backgroundBlur'],
|
|
762
|
+
};
|
|
763
|
+
function* occurrenceSources(id, outermost) {
|
|
764
|
+
const parts = id.split(';');
|
|
765
|
+
const layer = records.get(parts.at(-1));
|
|
766
|
+
if (layer && isInstance(layer)) {
|
|
767
|
+
yield records.get(layer.component);
|
|
768
|
+
yield layer;
|
|
769
|
+
yield layer.overrides?.[''];
|
|
770
|
+
}
|
|
771
|
+
else
|
|
772
|
+
yield layer;
|
|
773
|
+
for (let i = parts.length - 2; i >= (outermost ? 0 : 1); i--)
|
|
774
|
+
yield records.get(parts[i])?.overrides?.[parts.slice(i + 1).join(';')];
|
|
775
|
+
}
|
|
776
|
+
/** Variable paths bound on a virtual layer beneath its outermost instance: its main layer, then inner instances.
|
|
777
|
+
* A mask at an inner level still counts, so the outer mask holds if that inner style is later detached. */
|
|
778
|
+
function inheritedBindings(id) {
|
|
779
|
+
const paths = new Set();
|
|
780
|
+
for (const source of occurrenceSources(id, false))
|
|
781
|
+
for (const [path, ref] of Object.entries(source?.vars ?? {}))
|
|
782
|
+
if (ref)
|
|
783
|
+
paths.add(path);
|
|
784
|
+
return paths;
|
|
785
|
+
}
|
|
378
786
|
function install(change, m, into, id, virtual = false) {
|
|
379
787
|
const instance = isInstance(m);
|
|
380
788
|
const root = instance ? records.get(m.component) : m;
|
|
381
789
|
const rootBindings = instance ? new Set() : undefined;
|
|
382
790
|
const paint = instance ? appearance(root, m.overrides?.['']) : virtual ? appearance(m, into) : m;
|
|
383
|
-
const lists = {
|
|
791
|
+
const lists = { fillPaints: change.fillPaints, strokePaints: change.strokePaints, effects: change.effects };
|
|
384
792
|
for (const [field, slot, type, list] of slots) {
|
|
385
793
|
const ref = change[field];
|
|
386
|
-
if (!ref)
|
|
794
|
+
if (!ref || m.kind === 'text' && slot === 'fillStyle')
|
|
387
795
|
continue;
|
|
388
796
|
let style = ref.guid ? definitions.get(guidText(ref.guid, field)) : undefined;
|
|
389
797
|
if (style?.type !== type)
|
|
@@ -421,14 +829,18 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
421
829
|
lost('style reference', id);
|
|
422
830
|
}
|
|
423
831
|
else {
|
|
424
|
-
if (!instance)
|
|
832
|
+
if (!instance) {
|
|
425
833
|
into[slot] = style.id;
|
|
834
|
+
if (virtual)
|
|
835
|
+
masked.push([into, id, slot]);
|
|
836
|
+
}
|
|
426
837
|
delete lists[list]; // Variable-backed paints in a named style are authored on its definition.
|
|
427
838
|
}
|
|
428
839
|
}
|
|
429
840
|
const rootEntry = instance && !virtual ? m.overrides?.[''] ?? {} : into;
|
|
430
841
|
if (['frame', 'rect'].includes(m.kind))
|
|
431
842
|
installRadius(change, m, root, rootEntry, id, virtual);
|
|
843
|
+
installStrokeSides(change, root, rootEntry, id, instance, virtual);
|
|
432
844
|
const bind = binding({ id }, (path, variable) => {
|
|
433
845
|
if (virtual && (['w', 'h'].includes(path) || path.startsWith('layoutData.'))
|
|
434
846
|
|| instance && (path.startsWith('look.') || path === 'strokeWidth')) {
|
|
@@ -444,16 +856,21 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
444
856
|
if (virtual && path.startsWith('look.stroke.paint.') && !root.look?.stroke?.paint)
|
|
445
857
|
into['look.stroke.paint'] = paint.look.stroke.paint;
|
|
446
858
|
});
|
|
859
|
+
if (virtual && m.kind === 'text' && !m.bind?.content && change.variableConsumptionMap?.entries?.some((entry) => entry.variableField === 'TEXT_DATA' && entry.variableData === undefined)) {
|
|
860
|
+
// A field-only occurrence entry clears the inherited binding even when the linker found identical text.
|
|
861
|
+
(into.vars ??= {}).content = null;
|
|
862
|
+
into.content ??= expanded.get(id)?.content ?? m.content;
|
|
863
|
+
}
|
|
447
864
|
layerFields(change, m, bind);
|
|
865
|
+
variantFields(change, m, into, records, bind, () => lost('variable binding', id));
|
|
448
866
|
paintsAndEffects(lists, paint, bind, result.sources);
|
|
449
867
|
for (const range of change.textData?.styleOverrideTable ?? []) {
|
|
868
|
+
const glyphPaint = textColor(range.fillPaints ?? []).paint;
|
|
450
869
|
for (const field of range.variableConsumptionMap?.entries ?? [])
|
|
451
|
-
if (field.variableField
|
|
870
|
+
if (!Object.hasOwn(TEXT_VARIABLE_FIELDS, field.variableField))
|
|
452
871
|
lost('variable binding', id);
|
|
453
|
-
if (range.styleIdForFill && !isDetachedStyle(range.styleIdForFill))
|
|
454
|
-
lost('style reference', id);
|
|
455
872
|
for (const fill of range.fillPaints ?? []) {
|
|
456
|
-
if (fill.colorVar)
|
|
873
|
+
if (fill.colorVar && fill !== glyphPaint)
|
|
457
874
|
lost('variable binding', id);
|
|
458
875
|
if (fill.opacityVar)
|
|
459
876
|
lost('variable binding', id);
|
|
@@ -467,14 +884,27 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
467
884
|
if (Object.keys(rootEntry).length)
|
|
468
885
|
(m.overrides ??= {})[''] = rootEntry;
|
|
469
886
|
}
|
|
887
|
+
const chosen = new Set();
|
|
470
888
|
if (change.variableModeBySetMap)
|
|
471
889
|
for (const choice of change.variableModeBySetMap.entries ?? []) {
|
|
472
890
|
const mode = variables.modeOf(choice, id);
|
|
473
|
-
if (typeof mode === 'string'
|
|
891
|
+
if (typeof mode === 'string')
|
|
474
892
|
lost('variable mode', id);
|
|
475
|
-
else
|
|
893
|
+
else if (['page', 'frame'].includes(m.kind))
|
|
476
894
|
(into.modes ??= {})[mode.collectionId] = mode.modeId;
|
|
895
|
+
else if (virtual)
|
|
896
|
+
chosen.add(mode.collectionId);
|
|
897
|
+
else if (consultsCollection(m, mode.collectionId))
|
|
898
|
+
lost('variable mode', id);
|
|
899
|
+
else
|
|
900
|
+
unused.set(m.id, (unused.get(m.id) ?? new Set()).add(mode.collectionId));
|
|
477
901
|
}
|
|
902
|
+
if (virtual) {
|
|
903
|
+
for (const collection of unused.get(m.id) ?? [])
|
|
904
|
+
chosen.add(collection);
|
|
905
|
+
if (chosen.size)
|
|
906
|
+
occurrenceModes.set(id, chosen);
|
|
907
|
+
}
|
|
478
908
|
}
|
|
479
909
|
// Instance appearance comes from its main, which may live on a later page.
|
|
480
910
|
const layers = doc.models;
|
|
@@ -503,6 +933,22 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
503
933
|
if (main.look?.[key] !== undefined)
|
|
504
934
|
(m.look ??= {})[key] = structuredClone(main.look[key]);
|
|
505
935
|
}
|
|
936
|
+
if (m.stroke) {
|
|
937
|
+
const own = change.variableConsumptionMap?.entries ?? [];
|
|
938
|
+
const uniform = hasUniformStroke(change);
|
|
939
|
+
const base = main.look?.stroke?.sides ?? Array(4).fill(main.strokeWidth ?? 0);
|
|
940
|
+
const literals = strokeSides.map(({ field }) => uniform ? change.strokeWeight : change[field]);
|
|
941
|
+
const inherited = strokeSideBindings(original).filter(({ variableField }) => !uniform && !own.some((entry) => entry.variableField === variableField) &&
|
|
942
|
+
!strokeSides.some(({ binding }, i) => binding === variableField && literals[i] !== undefined && literals[i] !== base[i]));
|
|
943
|
+
if (inherited.length)
|
|
944
|
+
change.variableConsumptionMap = { ...change.variableConsumptionMap, entries: [...inherited, ...own] };
|
|
945
|
+
if (literals.some(v => v !== undefined)) {
|
|
946
|
+
const stroke = { position: 0, ...m.look?.stroke };
|
|
947
|
+
const previous = stroke.sides ?? base;
|
|
948
|
+
stroke.sides = literals.map((value, i) => Math.max(0, value ?? previous[i]));
|
|
949
|
+
(m.look ??= {}).stroke = stroke;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
506
952
|
}
|
|
507
953
|
const path = [m.sourceId];
|
|
508
954
|
for (let parent = records.get(m.parent); parent; parent = records.get(parent.parent)) {
|
|
@@ -526,14 +972,49 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
526
972
|
if (source)
|
|
527
973
|
install(expandedRadii.size ? expandedFields(m, source.figma.change) : source.figma.change, m, m, m.id);
|
|
528
974
|
}
|
|
975
|
+
const nestedPalettes = [];
|
|
529
976
|
for (const m of layers) {
|
|
530
977
|
const authored = occurrences.get(m.id);
|
|
531
978
|
const paths = new Set([...Object.keys(m.overrides ?? {}), ...authored?.keys() ?? []]);
|
|
532
979
|
for (const path of paths) {
|
|
533
980
|
const id = m.id + ';' + path, entry = m.overrides?.[path] ?? {};
|
|
534
|
-
palette(entry, id);
|
|
535
981
|
const change = authored?.get(path);
|
|
536
982
|
const target = path ? records.get(path.split(';').at(-1)) : m;
|
|
983
|
+
if (target?.kind === 'text' && change) {
|
|
984
|
+
const textData = change.textData;
|
|
985
|
+
// The linker packs a full body for formatting. An instance that only changes typography still follows the main's wording.
|
|
986
|
+
if (entry.styles && textData?.characters === undefined && entry.content === target.content &&
|
|
987
|
+
(Object.values(TEXT_VARIABLE_FIELDS).some(([, name]) => name in change) ||
|
|
988
|
+
change.variableConsumptionMap?.entries?.some((e) => Object.hasOwn(TEXT_VARIABLE_FIELDS, e.variableField))))
|
|
989
|
+
delete entry.content;
|
|
990
|
+
const painted = [change, ...(textData?.styleOverrideTable ?? [])].filter(fields => fields.fillPaints !== undefined || fields.styleIdForFill !== undefined);
|
|
991
|
+
// Even an explicitly painted run that looks identical to its main is a plain override.
|
|
992
|
+
const layer = expanded.get(id);
|
|
993
|
+
if (painted.length && layer?.styles) {
|
|
994
|
+
// The linker packs a full text body for a paint difference. Only authored wording owns the characters.
|
|
995
|
+
if (textData?.characters !== undefined)
|
|
996
|
+
entry.content = layer.content;
|
|
997
|
+
else
|
|
998
|
+
delete entry.content;
|
|
999
|
+
entry.styles ??= layer.styles;
|
|
1000
|
+
entry.styleAt ??= styleAtOf(layer);
|
|
1001
|
+
for (const fields of painted) {
|
|
1002
|
+
const style = fields.styleIdForFill && result.sources.styleNodeFor(fields.styleIdForFill, 'FILL');
|
|
1003
|
+
const fills = style?.change.fillPaints ?? fields.fillPaints ?? [];
|
|
1004
|
+
if (style || target.styles?.some((s) => s.fillStyle))
|
|
1005
|
+
lost('instance text paint style kept as plain colour', id);
|
|
1006
|
+
else if (textColor(fills).paint?.colorVar)
|
|
1007
|
+
lost('instance text colour binding kept as plain colour', id);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
if (entry.styles) {
|
|
1012
|
+
const depth = path.split(';').length;
|
|
1013
|
+
if (depth === 1)
|
|
1014
|
+
palette(entry, id, true);
|
|
1015
|
+
else
|
|
1016
|
+
(nestedPalettes[depth] ??= []).push([entry, id]);
|
|
1017
|
+
}
|
|
537
1018
|
if (path && target && !isInstance(target)) {
|
|
538
1019
|
inheritedLiterals(entry, target, change ?? {}, expanded.get(id));
|
|
539
1020
|
if (!change && Object.keys(target.vars ?? {}).some(key => key.startsWith('radius.')))
|
|
@@ -547,6 +1028,23 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
547
1028
|
delete m.overrides[path];
|
|
548
1029
|
}
|
|
549
1030
|
}
|
|
1031
|
+
// Install inner bindings before comparing an enclosing palette with the sources it inherits.
|
|
1032
|
+
nestedPalettes.forEach(level => { for (const [entry, id] of level)
|
|
1033
|
+
palette(entry, id, true); });
|
|
1034
|
+
// A style override replaces the slot as Figma does: bindings the occurrence inherits there no longer apply.
|
|
1035
|
+
for (const [entry, id, slot] of masked)
|
|
1036
|
+
for (const path of inheritedBindings(id))
|
|
1037
|
+
if (styled[slot].some(p => path.startsWith(p + '.')) && !Object.hasOwn(entry.vars ?? {}, path))
|
|
1038
|
+
(entry.vars ??= {})[path] = null;
|
|
1039
|
+
for (const [id, choices] of occurrenceModes) {
|
|
1040
|
+
let layer = {};
|
|
1041
|
+
for (const source of occurrenceSources(id, true))
|
|
1042
|
+
if (source)
|
|
1043
|
+
layer = { ...layer, ...source, vars: { ...layer.vars, ...source.vars } };
|
|
1044
|
+
for (const collection of choices)
|
|
1045
|
+
if (consultsCollection(layer, collection))
|
|
1046
|
+
lost('variable mode', id);
|
|
1047
|
+
}
|
|
550
1048
|
// A definition can be unused. Diagnostics for its bindings select only actual layer users.
|
|
551
1049
|
for (const entry of report) {
|
|
552
1050
|
if (!entry.ids?.some(id => styleUsers.has(id)))
|