@moondraft/import 0.2.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 +15 -0
- package/SOURCE.md +115 -1
- package/dist/adapter/convert.d.ts +2 -0
- package/dist/adapter/convert.js +175 -84
- package/dist/adapter/figma-handoff.d.ts +24 -2
- package/dist/adapter/figma-handoff.js +39 -18
- package/dist/adapter/figma.js +2 -1
- package/dist/adapter/file-components.d.ts +2 -1
- package/dist/adapter/file-components.js +5 -2
- package/dist/adapter/file-resources.js +234 -25
- package/dist/adapter/file-typography.d.ts +6 -0
- package/dist/adapter/file-typography.js +44 -0
- package/dist/adapter/link.d.ts +7 -0
- package/dist/adapter/link.js +87 -24
- package/dist/adapter/payload.js +2 -2
- package/dist/adapter/variables.d.ts +1 -1
- package/dist/figma-file.d.ts +1 -0
- package/dist/figma-file.js +1 -0
- package/dist/figma.d.ts +1 -0
- package/dist/figma.js +41 -19
- package/dist/report.d.ts +37 -6
- package/dist/report.js +91 -18
- package/dist/taco/figma-import/figFile.js +3 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +3 -13
- package/dist/taco/figma-import/import/converters/effects.js +4 -34
- package/dist/taco/figma-import/import/converters/exportSettings.js +26 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/frame.js +8 -7
- package/dist/taco/figma-import/import/converters/grid.d.ts +12 -0
- package/dist/taco/figma-import/import/converters/grid.js +97 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +1 -0
- package/dist/taco/figma-import/import/converters/image.js +38 -5
- package/dist/taco/figma-import/import/converters/layout.d.ts +5 -3
- package/dist/taco/figma-import/import/converters/layout.js +26 -16
- package/dist/taco/figma-import/import/converters/rectangle.js +2 -2
- package/dist/taco/figma-import/import/converters/section.js +2 -2
- package/dist/taco/figma-import/import/converters/shared.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/shared.js +8 -5
- package/dist/taco/figma-import/import/converters/text.d.ts +24 -0
- package/dist/taco/figma-import/import/converters/text.js +114 -29
- package/dist/taco/figma-import/import/instances.d.ts +9 -3
- package/dist/taco/figma-import/import/instances.js +319 -43
- package/dist/taco/figma-import/import/types.d.ts +8 -0
- package/dist/taco/figma-import/import/variables.d.ts +2 -0
- package/dist/taco/figma-import/import/variables.js +28 -1
- package/dist/taco/figma-import/import/variantTrees.js +2 -0
- package/dist/taco/figma-import/import/walk.d.ts +9 -2
- package/dist/taco/figma-import/import/walk.js +69 -30
- package/dist/taco/figma-import/kiwi/message.js +52 -10
- package/dist/taco/model/codec.d.ts +1 -1
- package/dist/taco/model/codec.js +40 -6
- package/dist/taco/model/instance-resolver.js +3 -1
- package/dist/taco/model/types.d.ts +36 -6
- package/dist/taco/model/types.js +4 -0
- package/dist/taco/svg-import/style/computed.js +1 -0
- package/package.json +2 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isObject } from '../taco/figma-import/import/fields.js';
|
|
2
|
+
import { onLeftOutLayer } from '../report.js';
|
|
2
3
|
const feature = {
|
|
3
4
|
annotations: 'developer annotations are not imported',
|
|
4
5
|
measurements: 'developer measurements are not imported',
|
|
@@ -52,6 +53,25 @@ function* canvasStatuses(tree) {
|
|
|
52
53
|
}
|
|
53
54
|
}
|
|
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
|
+
}
|
|
55
75
|
/** Keeps handoff-bearing clipboard layers distinct when merging variant trees. */
|
|
56
76
|
export function handoffSources(tree) {
|
|
57
77
|
const ids = new Set();
|
|
@@ -66,8 +86,12 @@ export function handoffSources(tree) {
|
|
|
66
86
|
ids.add(target);
|
|
67
87
|
return ids;
|
|
68
88
|
}
|
|
69
|
-
/**
|
|
70
|
-
|
|
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) {
|
|
71
95
|
const entries = [];
|
|
72
96
|
const report = (what, ids) => {
|
|
73
97
|
entries.push({ kind: 'left-out', what, count: 1, ...(ids?.length && { ids }) });
|
|
@@ -85,7 +109,7 @@ export function figmaHandoff(result, sources, doc, options = {}) {
|
|
|
85
109
|
else if (status !== undefined)
|
|
86
110
|
mappedStatus.set(target, { status, pageId });
|
|
87
111
|
}
|
|
88
|
-
const hasHandoff = (node) => node !== undefined && (mappedStatus.has(node.id) || handoffFields.some(field => node.change[field] !== undefined));
|
|
112
|
+
const hasHandoff = (node) => node !== undefined && (mappedStatus.has(node.id) || options.overrides.has(node.id) || handoffFields.some(field => node.change[field] !== undefined));
|
|
89
113
|
const byReaderId = new Map();
|
|
90
114
|
const layers = new Map();
|
|
91
115
|
for (const { node, figma } of sources.imported)
|
|
@@ -111,23 +135,14 @@ export function figmaHandoff(result, sources, doc, options = {}) {
|
|
|
111
135
|
for (const { nodeId, kind } of result.report)
|
|
112
136
|
if (kind === 'node-left-out' && hasHandoff(sources.tree.nodes.get(nodeId)))
|
|
113
137
|
scoped.add(nodeId);
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const node = pending.pop();
|
|
118
|
-
if (visited.has(node.id))
|
|
119
|
-
continue;
|
|
120
|
-
visited.add(node.id);
|
|
121
|
-
if (hasHandoff(node))
|
|
122
|
-
scoped.add(node.id);
|
|
123
|
-
for (const child of node.children)
|
|
124
|
-
pending.push(child);
|
|
125
|
-
}
|
|
138
|
+
for (const id of options.leftOut)
|
|
139
|
+
if (hasHandoff(sources.tree.nodes.get(id)))
|
|
140
|
+
scoped.add(id);
|
|
126
141
|
for (const [id, { pageId }] of mappedStatus)
|
|
127
142
|
if (pageId !== undefined)
|
|
128
143
|
scoped.add(id);
|
|
129
144
|
for (const id of scoped) {
|
|
130
|
-
const
|
|
145
|
+
const node = sources.tree.nodes.get(id), change = node?.change;
|
|
131
146
|
const targets = layers.get(id) ?? [];
|
|
132
147
|
const ids = targets.map(layer => layer.id);
|
|
133
148
|
const mapped = mappedStatus.get(id);
|
|
@@ -155,8 +170,14 @@ export function figmaHandoff(result, sources, doc, options = {}) {
|
|
|
155
170
|
else
|
|
156
171
|
omitted.add(feature[field]);
|
|
157
172
|
}
|
|
158
|
-
for (const
|
|
159
|
-
|
|
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
|
+
}
|
|
160
181
|
if (Object.keys(data).length)
|
|
161
182
|
for (const layer of targets) {
|
|
162
183
|
const existing = layer.data?.figma;
|
package/dist/adapter/figma.js
CHANGED
|
@@ -35,6 +35,7 @@ export function figmaDocument(result, name, encodeImage = base64) {
|
|
|
35
35
|
components[c.id] = c;
|
|
36
36
|
const assets = {};
|
|
37
37
|
for (const i of Object.values(result.assets))
|
|
38
|
-
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) } };
|
|
39
40
|
return { pageOrder, pages, nodes, components, assets, componentPlacements: result.componentPlacements, theme: { variableCollections: result.variableCollections, textStyles: result.textStyles ?? {} } };
|
|
40
41
|
}
|
|
@@ -12,9 +12,10 @@ export type FileMessage = ReturnType<typeof readFileMessage>;
|
|
|
12
12
|
* @param doc - Linked document, with source IDs for the original nodes.
|
|
13
13
|
* @param nestedBooleans - Enclosing instance assignments carried through expanded layers and their linked paths.
|
|
14
14
|
* @param report - Receives occurrences the native document cannot represent.
|
|
15
|
+
* @param expandedReasons - Per instance linking left expanded, by layer ID: linking's reason, which its report entry names.
|
|
15
16
|
* @returns Authored override fields and the Figma sources of newly created sets.
|
|
16
17
|
*/
|
|
17
|
-
export declare function fileComponents(result: FileMessage, doc: DocumentJSON, nestedBooleans: ReadonlyMap<string, NestedBooleanAssignments>, report: ImportReportEntry[]): {
|
|
18
|
+
export declare function fileComponents(result: FileMessage, doc: DocumentJSON, nestedBooleans: ReadonlyMap<string, NestedBooleanAssignments>, report: ImportReportEntry[], expandedReasons?: ReadonlyMap<string, string>): {
|
|
18
19
|
occurrences: Map<string, Map<string, Readonly<Record<string, unknown>>>>;
|
|
19
20
|
setSources: Map<string, FigmaTreeNode>;
|
|
20
21
|
};
|
|
@@ -2,14 +2,16 @@ import { definitions as propertyDefinitions, effectiveRecord, isInstance, ownerO
|
|
|
2
2
|
import { guidText } from '../taco/figma-import/import/fields.js';
|
|
3
3
|
import { formatExpressionName, parseLayerPath } from '../taco/model/index.js';
|
|
4
4
|
import { keepDescription } from './figma-descriptions.js';
|
|
5
|
+
import { expansionReason } from './link.js';
|
|
5
6
|
/** Installs local property IDs, their bindings, instance assignments and slot content.
|
|
6
7
|
* @param result - Original Figma component declarations and placements.
|
|
7
8
|
* @param doc - Linked document, with source IDs for the original nodes.
|
|
8
9
|
* @param nestedBooleans - Enclosing instance assignments carried through expanded layers and their linked paths.
|
|
9
10
|
* @param report - Receives occurrences the native document cannot represent.
|
|
11
|
+
* @param expandedReasons - Per instance linking left expanded, by layer ID: linking's reason, which its report entry names.
|
|
10
12
|
* @returns Authored override fields and the Figma sources of newly created sets.
|
|
11
13
|
*/
|
|
12
|
-
export function fileComponents(result, doc, nestedBooleans, report) {
|
|
14
|
+
export function fileComponents(result, doc, nestedBooleans, report, expandedReasons = new Map()) {
|
|
13
15
|
const models = doc.models;
|
|
14
16
|
const byId = new Map(models.map(m => [m.id, m]));
|
|
15
17
|
const mains = new Map(), bySource = new Map();
|
|
@@ -224,7 +226,8 @@ export function fileComponents(result, doc, nestedBooleans, report) {
|
|
|
224
226
|
count: Object.keys(nested).length, ids: [m.id] });
|
|
225
227
|
}
|
|
226
228
|
if (!m.component) {
|
|
227
|
-
|
|
229
|
+
const reason = expandedReasons.get(m.id);
|
|
230
|
+
report.push({ kind: 'approximated', what: reason ? `expanded instance: ${expansionReason(reason)}` : 'expanded instance', count: 1, ids: [m.id] });
|
|
228
231
|
continue;
|
|
229
232
|
}
|
|
230
233
|
const fields = result.sources.occurrenceFields.get(n.id);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import * as editorSchema from '@moondraft/editor/schema';
|
|
2
2
|
import { STYLES, VARIABLES } from '@moondraft/editor/schema';
|
|
3
|
-
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';
|
|
4
7
|
import { styleAtOf, supportsRunPaint } from './link.js';
|
|
5
8
|
import { guidText, isObject } from '../taco/figma-import/import/fields.js';
|
|
6
9
|
import { isDetachedStyle, STYLE_REFERENCE_FIELDS } from '../taco/figma-import/import/styles.js';
|
|
@@ -73,6 +76,7 @@ const hasUniformStroke = (change) => change.borderStrokeWeightsIndependent === f
|
|
|
73
76
|
change.strokeWeight !== undefined && !change.borderStrokeWeightsIndependent;
|
|
74
77
|
const fields = {
|
|
75
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',
|
|
76
80
|
STACK_SPACING: 'layoutData.layout.gap', STACK_COUNTER_SPACING: 'layoutData.layout.wrapGap',
|
|
77
81
|
CORNER_RADIUS: 'radius', RECTANGLE_TOP_LEFT_CORNER_RADIUS: 'radius.0', RECTANGLE_TOP_RIGHT_CORNER_RADIUS: 'radius.1',
|
|
78
82
|
RECTANGLE_BOTTOM_RIGHT_CORNER_RADIUS: 'radius.2', RECTANGLE_BOTTOM_LEFT_CORNER_RADIUS: 'radius.3',
|
|
@@ -200,10 +204,12 @@ function layerFields(change, m, bind) {
|
|
|
200
204
|
for (const { variableField, variableData } of entries) {
|
|
201
205
|
if (isStrokeSideBinding(variableField))
|
|
202
206
|
continue; // Installed per side.
|
|
203
|
-
if (variableField
|
|
207
|
+
if (Object.hasOwn(TEXT_VARIABLE_FIELDS, variableField) && m.styles)
|
|
204
208
|
continue; // Installed per range below.
|
|
205
209
|
if (variableField === 'TEXT_DATA' && variableData === undefined)
|
|
206
210
|
continue; // An empty entry names no variable.
|
|
211
|
+
if (variableField === 'VARIANT_PROPERTIES')
|
|
212
|
+
continue; // Installed per variant property (install).
|
|
207
213
|
let path = Object.hasOwn(fields, variableField) ? fields[variableField] : undefined;
|
|
208
214
|
if (path?.startsWith('radius') && ['frame', 'rect'].includes(m.kind))
|
|
209
215
|
continue; // Installed per effective corner.
|
|
@@ -213,7 +219,36 @@ function layerFields(change, m, bind) {
|
|
|
213
219
|
|| path === 'strokeWidth' && (!m.stroke || m.component && m.component !== m.id)
|
|
214
220
|
|| path && m.bind?.[path])
|
|
215
221
|
path = undefined;
|
|
216
|
-
|
|
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
|
+
}
|
|
217
252
|
}
|
|
218
253
|
}
|
|
219
254
|
// Linking compares painted instances with their mains. A mode difference in that comparison is derived:
|
|
@@ -271,6 +306,7 @@ function sameStyleValue(style, literal) {
|
|
|
271
306
|
return false;
|
|
272
307
|
return Object.keys({ ...style, ...literal }).every(k => k === 'o' ? (style.o ?? 1) === (literal.o ?? 1) : sameStyleValue(style[k], literal[k]));
|
|
273
308
|
}
|
|
309
|
+
const TYPOGRAPHY_FIELDS = ['size', 'family', 'style', 'weight', 'lineHeight', 'letterSpacing'];
|
|
274
310
|
/** Writes the shared resources, layer references and mode choices of a saved file.
|
|
275
311
|
* @param result - Reader output retaining the source relationships.
|
|
276
312
|
* @param doc - Converted layers to associate with their definitions.
|
|
@@ -299,22 +335,31 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
299
335
|
byKey.set(record.type + '/' + record.key, record);
|
|
300
336
|
shared.push(record);
|
|
301
337
|
}
|
|
302
|
-
const
|
|
338
|
+
const textStyleBindings = new Map();
|
|
339
|
+
const resolveLiteral = supportsTypographyBindings ? undefined : variableResolver(result.variableCollections);
|
|
303
340
|
for (const [node, use] of textStyleUses) {
|
|
304
341
|
const { id, style: text } = use.shared;
|
|
305
|
-
const
|
|
306
|
-
textDefinitions.set(id, value);
|
|
342
|
+
const literal = runStyle(text);
|
|
307
343
|
styleUsers.set(id, []);
|
|
308
344
|
const fields = node.change.variableConsumptionMap?.entries ?? [];
|
|
309
|
-
|
|
310
|
-
|
|
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))
|
|
311
352
|
report.push({ kind: 'left-out', what: 'variable binding', count: 1, ids: [id] });
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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 });
|
|
318
363
|
}
|
|
319
364
|
function variableAt(path, data, type, id) {
|
|
320
365
|
const variable = variables.variableOf(data, id);
|
|
@@ -348,9 +393,15 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
348
393
|
function palette(entry, id, occurrence = false) {
|
|
349
394
|
if (!entry.styles)
|
|
350
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;
|
|
351
401
|
// Expanded palettes may be shared by several linked records. Strip reader metadata on a copy.
|
|
352
|
-
|
|
353
|
-
|
|
402
|
+
const original = entry.styles;
|
|
403
|
+
entry.styles = original.map((source, i) => {
|
|
404
|
+
const { fileTypography = {}, fileOwnTypography = {}, fileTextColor, filePlainTextColor, fileFillStyle, filePlainFillStyle, ...s } = source;
|
|
354
405
|
const { textStyle, color, paintOpacity, link: _link, figmaHyperlink: _pending, ...local } = s;
|
|
355
406
|
const style = definitions.get(fileFillStyle ?? filePlainFillStyle);
|
|
356
407
|
if (style) {
|
|
@@ -389,18 +440,160 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
389
440
|
}
|
|
390
441
|
if (textStyle)
|
|
391
442
|
styleUsers.get(textStyle)?.push(id);
|
|
392
|
-
const
|
|
393
|
-
const
|
|
394
|
-
|
|
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))) {
|
|
395
449
|
delete s.textStyle;
|
|
396
450
|
report.push({ kind: 'approximated', what: 'text style with local formatting', count: 1, ids: [id] });
|
|
397
451
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
+
}
|
|
402
463
|
return s;
|
|
403
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;
|
|
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'];
|
|
404
597
|
}
|
|
405
598
|
const lost = (what, id) => report.push({ kind: 'left-out', what, count: 1, ids: [id] });
|
|
406
599
|
const isInstance = (m) => m.component && m.component !== m.id;
|
|
@@ -669,11 +862,12 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
669
862
|
into.content ??= expanded.get(id)?.content ?? m.content;
|
|
670
863
|
}
|
|
671
864
|
layerFields(change, m, bind);
|
|
865
|
+
variantFields(change, m, into, records, bind, () => lost('variable binding', id));
|
|
672
866
|
paintsAndEffects(lists, paint, bind, result.sources);
|
|
673
867
|
for (const range of change.textData?.styleOverrideTable ?? []) {
|
|
674
868
|
const glyphPaint = textColor(range.fillPaints ?? []).paint;
|
|
675
869
|
for (const field of range.variableConsumptionMap?.entries ?? [])
|
|
676
|
-
if (field.variableField
|
|
870
|
+
if (!Object.hasOwn(TEXT_VARIABLE_FIELDS, field.variableField))
|
|
677
871
|
lost('variable binding', id);
|
|
678
872
|
for (const fill of range.fillPaints ?? []) {
|
|
679
873
|
if (fill.colorVar && fill !== glyphPaint)
|
|
@@ -778,6 +972,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
778
972
|
if (source)
|
|
779
973
|
install(expandedRadii.size ? expandedFields(m, source.figma.change) : source.figma.change, m, m, m.id);
|
|
780
974
|
}
|
|
975
|
+
const nestedPalettes = [];
|
|
781
976
|
for (const m of layers) {
|
|
782
977
|
const authored = occurrences.get(m.id);
|
|
783
978
|
const paths = new Set([...Object.keys(m.overrides ?? {}), ...authored?.keys() ?? []]);
|
|
@@ -787,6 +982,11 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
787
982
|
const target = path ? records.get(path.split(';').at(-1)) : m;
|
|
788
983
|
if (target?.kind === 'text' && change) {
|
|
789
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;
|
|
790
990
|
const painted = [change, ...(textData?.styleOverrideTable ?? [])].filter(fields => fields.fillPaints !== undefined || fields.styleIdForFill !== undefined);
|
|
791
991
|
// Even an explicitly painted run that looks identical to its main is a plain override.
|
|
792
992
|
const layer = expanded.get(id);
|
|
@@ -808,7 +1008,13 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
808
1008
|
}
|
|
809
1009
|
}
|
|
810
1010
|
}
|
|
811
|
-
|
|
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
|
+
}
|
|
812
1018
|
if (path && target && !isInstance(target)) {
|
|
813
1019
|
inheritedLiterals(entry, target, change ?? {}, expanded.get(id));
|
|
814
1020
|
if (!change && Object.keys(target.vars ?? {}).some(key => key.startsWith('radius.')))
|
|
@@ -822,6 +1028,9 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
822
1028
|
delete m.overrides[path];
|
|
823
1029
|
}
|
|
824
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); });
|
|
825
1034
|
// A style override replaces the slot as Figma does: bindings the occurrence inherits there no longer apply.
|
|
826
1035
|
for (const [entry, id, slot] of masked)
|
|
827
1036
|
for (const path of inheritedBindings(id))
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TextStyle } from '@moondraft/editor/schema';
|
|
2
|
+
import type { VariableValue } from './variables.ts';
|
|
3
|
+
export declare const supportsTypographyBindings: boolean;
|
|
4
|
+
export declare const supportsTextField: (field: string) => boolean;
|
|
5
|
+
/** Keeps the drawn metrics and, on an older peer, the literal face in the selected mode. */
|
|
6
|
+
export declare function applyFileTypography(style: TextStyle, refs: Readonly<Record<string, string | null>>, percentTracking: boolean, resolve: (id: string) => VariableValue | undefined): void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as editor from '@moondraft/editor/core';
|
|
2
|
+
import { fontStyleWeight } from '../taco/figma-import/import/converters/text.js';
|
|
3
|
+
// Font size bindings predate the peer’s typography binding lookup.
|
|
4
|
+
export const supportsTypographyBindings = 'bindingField' in editor && !!editor.bindingField('styles.0.family');
|
|
5
|
+
export const supportsTextField = (field) => field === 'size' || supportsTypographyBindings;
|
|
6
|
+
/** Keeps the drawn metrics and, on an older peer, the literal face in the selected mode. */
|
|
7
|
+
export function applyFileTypography(style, refs, percentTracking, resolve) {
|
|
8
|
+
if (!supportsTypographyBindings) {
|
|
9
|
+
const family = refs.family && resolve(refs.family), name = refs.style && resolve(refs.style);
|
|
10
|
+
const weight = refs.weight && resolve(refs.weight);
|
|
11
|
+
if (typeof family === 'string')
|
|
12
|
+
style.family = family;
|
|
13
|
+
if (typeof name === 'string') {
|
|
14
|
+
const metrics = fontStyleWeight(name);
|
|
15
|
+
style.style = name;
|
|
16
|
+
style.weight = metrics.fontWeight ?? 400;
|
|
17
|
+
style.italic = metrics.italic;
|
|
18
|
+
}
|
|
19
|
+
if (typeof weight === 'number') {
|
|
20
|
+
style.weight = weight;
|
|
21
|
+
if (!name)
|
|
22
|
+
style.style = editor.fontStyleName(weight, style.italic);
|
|
23
|
+
}
|
|
24
|
+
if ((name || weight) && style.variations?.wght !== undefined)
|
|
25
|
+
style.variations = { ...style.variations, wght: style.weight };
|
|
26
|
+
}
|
|
27
|
+
if (refs.lineHeight) {
|
|
28
|
+
const pixels = resolve(refs.lineHeight);
|
|
29
|
+
if (typeof pixels === 'number')
|
|
30
|
+
style.lineHeight = pixels;
|
|
31
|
+
if (supportsTypographyBindings)
|
|
32
|
+
style.lineHeightMode = 'px';
|
|
33
|
+
}
|
|
34
|
+
if (refs.letterSpacing && (percentTracking || !supportsTypographyBindings)) {
|
|
35
|
+
const spacing = resolve(refs.letterSpacing);
|
|
36
|
+
if (typeof spacing === 'number') {
|
|
37
|
+
const boundSize = percentTracking && refs.size ? resolve(refs.size) : undefined;
|
|
38
|
+
const size = typeof boundSize === 'number' ? boundSize : style.size;
|
|
39
|
+
style.letterSpacing = percentTracking ? spacing * size / 100 : spacing;
|
|
40
|
+
}
|
|
41
|
+
if (percentTracking)
|
|
42
|
+
style.letterSpacingMode = 'percent';
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/adapter/link.d.ts
CHANGED
|
@@ -5,6 +5,12 @@ 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
|
+
/**
|
|
9
|
+
* Describes in plain words why an instance stays expanded, from linking's reason for it (LinkCounts.unlinked).
|
|
10
|
+
* @param reason - Linking's reason.
|
|
11
|
+
* @returns What the report says after "expanded instance: ".
|
|
12
|
+
*/
|
|
13
|
+
export declare function expansionReason(reason: string): string;
|
|
8
14
|
/** A text's style index per UTF-16 unit, from styleAt or the file's runs ([length, style]). */
|
|
9
15
|
export declare function styleAtOf(m: any): any;
|
|
10
16
|
export declare const supportsRunPaint: boolean;
|
|
@@ -46,4 +52,5 @@ export declare function linkWithLayers(doc: {
|
|
|
46
52
|
counts: LinkCounts;
|
|
47
53
|
aliases: Map<string, string[]>;
|
|
48
54
|
omittedLinks: Map<string, Set<string>>;
|
|
55
|
+
expandedReasons: Map<string, string>;
|
|
49
56
|
};
|