@moondraft/import 0.3.0 → 0.5.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 +20 -0
- package/README.md +25 -0
- package/SOURCE.md +49 -0
- package/dist/adapter/convert.js +34 -1
- package/dist/adapter/file-components.d.ts +18 -0
- package/dist/adapter/file-components.js +96 -11
- package/dist/adapter/file-resources.d.ts +6 -1
- package/dist/adapter/file-resources.js +126 -28
- package/dist/adapter/link.d.ts +1 -1
- package/dist/adapter/link.js +138 -87
- package/dist/figma-file.d.ts +3 -2
- package/dist/figma-file.js +7 -2
- package/dist/figma-worker.d.ts +5 -0
- package/dist/figma-worker.js +6 -1
- package/dist/figma.d.ts +7 -1
- package/dist/figma.js +29 -10
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/lazy.d.ts +111 -0
- package/dist/lazy.js +363 -0
- package/dist/report.js +6 -1
- package/dist/serialize.d.ts +9 -0
- package/dist/serialize.js +54 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -1
- package/dist/taco/figma-import/import/carry.js +8 -0
- package/dist/taco/figma-import/import/converters/effects.js +11 -3
- package/dist/taco/figma-import/import/converters/group.d.ts +1 -0
- package/dist/taco/figma-import/import/converters/group.js +5 -2
- package/dist/taco/figma-import/import/converters/section.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/section.js +4 -1
- package/dist/taco/figma-import/import/converters/shared.d.ts +21 -2
- package/dist/taco/figma-import/import/converters/shared.js +53 -7
- package/dist/taco/figma-import/import/converters/text.js +39 -5
- package/dist/taco/figma-import/import/converters/vector.js +5 -3
- package/dist/taco/figma-import/import/instances.d.ts +10 -0
- package/dist/taco/figma-import/import/instances.js +22 -21
- package/dist/taco/figma-import/import/variables.js +8 -2
- package/dist/taco/figma-import/import/walk.js +8 -2
- package/package.json +3 -3
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { resolveVirtual } from '@moondraft/editor/core';
|
|
1
2
|
import * as editorSchema from '@moondraft/editor/schema';
|
|
2
3
|
import { STYLES, VARIABLES } from '@moondraft/editor/schema';
|
|
3
4
|
import { typographyVariable } from '../taco/figma-import/import/variables.js';
|
|
4
5
|
import { TEXT_VARIABLE_FIELDS, textColor } from '../taco/figma-import/import/converters/text.js';
|
|
6
|
+
import { hasNonFinite } from '../taco/figma-import/import/converters/shared.js';
|
|
5
7
|
import { applyFileTypography, supportsTextField, supportsTypographyBindings } from './file-typography.js';
|
|
6
8
|
import { variableResolver } from './variables.js';
|
|
7
9
|
import { styleAtOf, supportsRunPaint } from './link.js';
|
|
8
10
|
import { guidText, isObject } from '../taco/figma-import/import/fields.js';
|
|
9
11
|
import { isDetachedStyle, STYLE_REFERENCE_FIELDS } from '../taco/figma-import/import/styles.js';
|
|
12
|
+
import { withBindingEntries, withOverrides } from '../taco/figma-import/import/instances.js';
|
|
10
13
|
import { layerPathKey, parseLayerPath } from '../taco/model/index.js';
|
|
11
14
|
import { rgba, runStyle } from './convert.js';
|
|
12
15
|
import { keepDescription } from './figma-descriptions.js';
|
|
@@ -54,12 +57,22 @@ function fileVariables(result, shared, identity, report) {
|
|
|
54
57
|
for (const [id, c] of Object.entries(result.variableCollections)) {
|
|
55
58
|
shared.push({ ...identity(variables.sources.get(id)), id, parent: VARIABLES, kind: 'collection', name: c.name,
|
|
56
59
|
modes: c.modeOrder.map(id => ({ id, name: c.modes[id].name })), defaultMode: c.defaultModeId });
|
|
60
|
+
// The editor refuses two variables of one name in a collection, which Figma allows: an authored name is kept and
|
|
61
|
+
// each repeat takes the first free `Name 2`, `Name 3`, …; its ID, so aliases to it, is unchanged.
|
|
62
|
+
const authored = new Set(Object.values(c.variables).map(v => v.name)), taken = new Set();
|
|
57
63
|
for (const [varId, v] of Object.entries(c.variables)) {
|
|
58
64
|
const node = variables.sources.get(varId);
|
|
65
|
+
let name = v.name;
|
|
66
|
+
if (taken.has(name)) {
|
|
67
|
+
for (let n = 2; authored.has(name) || taken.has(name); n++)
|
|
68
|
+
name = `${v.name} ${n}`;
|
|
69
|
+
report.push({ kind: 'approximated', what: `variable renamed from ${v.name} to ${name}`, count: 1, ids: [varId], sources: [{ id: node.id, name: v.name }] });
|
|
70
|
+
}
|
|
71
|
+
taken.add(name);
|
|
59
72
|
const values = Object.fromEntries(Object.entries(v.values).map(([mode, value]) => [mode, v.type === 'color' ? rgba(value) : value]));
|
|
60
73
|
for (const [mode, alias] of variables.aliases.get(varId) ?? [])
|
|
61
74
|
values[mode] = { alias };
|
|
62
|
-
const record = { ...identity(node), id: varId, parent: id, name
|
|
75
|
+
const record = { ...identity(node), id: varId, parent: id, name, kind: 'variable', type: v.type, values };
|
|
63
76
|
keepDescription(node, record, report);
|
|
64
77
|
keepVariableMetadata(node, record, report);
|
|
65
78
|
shared.push(record);
|
|
@@ -108,8 +121,10 @@ function paintsAndEffects(change, m, bind, indices) {
|
|
|
108
121
|
bind(undefined, paint.opacityVar, 'number');
|
|
109
122
|
if (!paint.stopsVar)
|
|
110
123
|
return;
|
|
111
|
-
// The reader sorts the stops and samples a new endpoint for stops beyond the paint's range.
|
|
112
|
-
|
|
124
|
+
// The reader sorts the stops and samples a new endpoint for stops beyond the paint's range. It leaves out a stop
|
|
125
|
+
// with a non-finite colour or position (Moondraft patch 0055), so that stop takes no place here either.
|
|
126
|
+
const sorted = (paint.stops ?? []).map((stop, source) => ({ at: stop.position, source, stop }))
|
|
127
|
+
.filter(({ stop }) => !(isObject(stop) && (hasNonFinite(stop, ['position']) || isObject(stop.color) && hasNonFinite(stop.color, ['r', 'g', 'b', 'a']))))
|
|
113
128
|
.sort((a, b) => a.at - b.at);
|
|
114
129
|
const inside = sorted.filter((stop) => stop.at >= 0 && stop.at <= 1);
|
|
115
130
|
const first = Number(sorted[0]?.at < 0);
|
|
@@ -138,6 +153,28 @@ function paintsAndEffects(change, m, bind, indices) {
|
|
|
138
153
|
}
|
|
139
154
|
}
|
|
140
155
|
}
|
|
156
|
+
/** A `variableModeBySetMap` with the choices of an override's own map (`over`) in place of those for the same collections in `map`. */
|
|
157
|
+
function withModeEntries(map, over) {
|
|
158
|
+
const entriesOf = (value) => isObject(value) && Array.isArray(value.entries) ? value.entries : [];
|
|
159
|
+
const collectionOf = (entry) => JSON.stringify(entry?.variableSetID);
|
|
160
|
+
const named = new Set(entriesOf(over).map(collectionOf));
|
|
161
|
+
return { ...isObject(map) && map, ...isObject(over) && over,
|
|
162
|
+
entries: [...entriesOf(map).filter(entry => !named.has(collectionOf(entry))), ...entriesOf(over)] };
|
|
163
|
+
}
|
|
164
|
+
/** `change` with an enclosing instance's override fields (`own`) merged in: its plain fields replace the change's
|
|
165
|
+
* (unbinding the fields they set), and its bindings and modes replace only those of the same fields and collections.
|
|
166
|
+
* A list of paints in the override drops the change's style of that slot unless the override names one. */
|
|
167
|
+
export function withOverrideFields(change, own) {
|
|
168
|
+
const { variableConsumptionMap: map, variableModeBySetMap: modes, ...plain } = own;
|
|
169
|
+
const unbound = withOverrides(change, plain, plain.styleIdForText !== undefined);
|
|
170
|
+
const merged = { ...unbound,
|
|
171
|
+
...map !== undefined && { variableConsumptionMap: withBindingEntries(unbound.variableConsumptionMap, map) },
|
|
172
|
+
...modes !== undefined && { variableModeBySetMap: withModeEntries(unbound.variableModeBySetMap, modes) } };
|
|
173
|
+
for (const [ref, list] of STYLE_REFERENCE_FIELDS)
|
|
174
|
+
if (list in own && !(ref in own))
|
|
175
|
+
delete merged[ref];
|
|
176
|
+
return merged;
|
|
177
|
+
}
|
|
141
178
|
function radiusBindings(change) {
|
|
142
179
|
const bindings = new Map();
|
|
143
180
|
for (const e of change.variableConsumptionMap?.entries ?? []) {
|
|
@@ -251,9 +288,27 @@ function variantFields(change, m, into, records, bind, lost) {
|
|
|
251
288
|
}
|
|
252
289
|
}
|
|
253
290
|
}
|
|
291
|
+
/** A layer's fill list or stroke paint as the renderer draws it (a compact colour is one solid paint). */
|
|
292
|
+
function paintOf(m, key) {
|
|
293
|
+
if (key === 'look.fills')
|
|
294
|
+
return m.look?.fills ?? (m.fill ? [{ k: 'solid', c: m.fill }] : []);
|
|
295
|
+
return m.look?.stroke?.paint ?? (m.stroke ? { k: 'solid', c: m.stroke } : undefined);
|
|
296
|
+
}
|
|
297
|
+
/** Whether two paint values are equal everywhere except at the bound paths below `path` (a missing opacity is 1). */
|
|
298
|
+
function sameExceptBound(a, b, path, bound) {
|
|
299
|
+
if (bound.has(path) || a === b)
|
|
300
|
+
return true;
|
|
301
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object' || Array.isArray(a) !== Array.isArray(b))
|
|
302
|
+
return false;
|
|
303
|
+
if (Array.isArray(a) && a.length !== b.length)
|
|
304
|
+
return false;
|
|
305
|
+
return Object.keys({ ...a, ...b }).every(k => k === 'o' && !Array.isArray(a)
|
|
306
|
+
? (a.o ?? 1) === (b.o ?? 1) : sameExceptBound(a[k], b[k], path + '.' + k, bound));
|
|
307
|
+
}
|
|
254
308
|
// Linking compares painted instances with their mains. A mode difference in that comparison is derived:
|
|
255
309
|
// leaving it as an authored paint override would mask the binding when the mode changes again.
|
|
256
|
-
|
|
310
|
+
// rootBindings: the instance root bindings `change` repeats from the main; `occurrence` when `entry` is not the node's own.
|
|
311
|
+
function inheritedLiterals(entry, template, change, expanded, rootBindings, occurrence = false, dropped) {
|
|
257
312
|
const sources = Object.keys(template.vars ?? {});
|
|
258
313
|
const inherited = (key) => sources.some(path => path === key || path.startsWith(key + '.'));
|
|
259
314
|
const formatting = [
|
|
@@ -266,11 +321,31 @@ function inheritedLiterals(entry, template, change, expanded, rootBindings) {
|
|
|
266
321
|
for (const [key, style, list, ref] of formatting) {
|
|
267
322
|
if (!template[style] && !inherited(key))
|
|
268
323
|
continue;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
324
|
+
// An instance's own record writes a removed fill or effect list as no field; an occurrence entry omits what
|
|
325
|
+
// it inherits. (A removed stroke keeps the instance expanded.)
|
|
326
|
+
const value = entry[key], removed = rootBindings && !occurrence && key !== 'look.stroke.paint' && value !== undefined
|
|
327
|
+
&& (Array.isArray(value) ? !value.length : !value.radius);
|
|
328
|
+
if (!(list in change || ref in change)) {
|
|
329
|
+
if (!removed)
|
|
330
|
+
delete entry[key];
|
|
331
|
+
}
|
|
332
|
+
else if (list in change && !(ref in change)) {
|
|
272
333
|
const bound = sources.filter(path => path.startsWith(key + '.'));
|
|
273
|
-
|
|
334
|
+
const rebound = bound.length > 0 && rootBindings !== undefined && bound.every(path => rootBindings.has(path));
|
|
335
|
+
if (key in entry) {
|
|
336
|
+
// An instance root keeps its main's binding, so its paint resolved in an explicit mode is no override; any other
|
|
337
|
+
// difference (another opacity, an added paint) still is, and the editor cannot hold the binding next to it.
|
|
338
|
+
if (rebound && (key === 'look.fills' || key === 'look.stroke.paint')) {
|
|
339
|
+
if (sameExceptBound(entry[key], paintOf(template, key), key, new Set(bound)))
|
|
340
|
+
delete entry[key];
|
|
341
|
+
else
|
|
342
|
+
dropped?.();
|
|
343
|
+
}
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
// A layer the instance does not show (an occurrence of the component it was swapped from) has no expanded record
|
|
347
|
+
// and takes no literal.
|
|
348
|
+
if (rebound || !expanded)
|
|
274
349
|
continue;
|
|
275
350
|
// The link pass omits equal literals. An authored value still masks the main's source.
|
|
276
351
|
if (key === 'look.fills')
|
|
@@ -287,7 +362,7 @@ function inheritedLiterals(entry, template, change, expanded, rootBindings) {
|
|
|
287
362
|
const authored = key === 'content' ? change.textData?.characters !== undefined : field in change;
|
|
288
363
|
if (!authored)
|
|
289
364
|
delete entry[key];
|
|
290
|
-
else if (!(key in entry))
|
|
365
|
+
else if (!(key in entry) && (expanded || key !== 'content'))
|
|
291
366
|
entry[key] = key === 'content' ? expanded.content : change[field];
|
|
292
367
|
}
|
|
293
368
|
}
|
|
@@ -753,7 +828,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
753
828
|
}
|
|
754
829
|
return false;
|
|
755
830
|
}
|
|
756
|
-
// Occurrence entries that name a style, masked once every entry holds its own bindings.
|
|
831
|
+
// Occurrence entries that name a style or set a paint list, masked once every entry holds its own bindings.
|
|
757
832
|
const masked = [];
|
|
758
833
|
const styled = {
|
|
759
834
|
fillStyle: ['look.fills'],
|
|
@@ -785,7 +860,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
785
860
|
}
|
|
786
861
|
function install(change, m, into, id, virtual = false) {
|
|
787
862
|
const instance = isInstance(m);
|
|
788
|
-
const root = instance ? records.get(m.component) : m;
|
|
863
|
+
const root = instance ? records.get(virtual ? shownMain(id, into, m) : m.component) : m; // an enclosing instance may switch the main
|
|
789
864
|
const rootBindings = instance ? new Set() : undefined;
|
|
790
865
|
const paint = instance ? appearance(root, m.overrides?.['']) : virtual ? appearance(m, into) : m;
|
|
791
866
|
const lists = { fillPaints: change.fillPaints, strokePaints: change.strokePaints, effects: change.effects };
|
|
@@ -837,6 +912,11 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
837
912
|
delete lists[list]; // Variable-backed paints in a named style are authored on its definition.
|
|
838
913
|
}
|
|
839
914
|
}
|
|
915
|
+
// A paint list replaces the slot as a style does: a binding an inner instance gave the old paints goes with them.
|
|
916
|
+
if (virtual && !instance && m.kind !== 'text')
|
|
917
|
+
for (const [list, slot] of [['fillPaints', 'fillStyle'], ['strokePaints', 'strokeStyle']])
|
|
918
|
+
if (change[list] !== undefined)
|
|
919
|
+
masked.push([into, id, slot]);
|
|
840
920
|
const rootEntry = instance && !virtual ? m.overrides?.[''] ?? {} : into;
|
|
841
921
|
if (['frame', 'rect'].includes(m.kind))
|
|
842
922
|
installRadius(change, m, root, rootEntry, id, virtual);
|
|
@@ -879,9 +959,9 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
879
959
|
lost('variable binding', id);
|
|
880
960
|
}
|
|
881
961
|
}
|
|
882
|
-
if (rootBindings
|
|
883
|
-
inheritedLiterals(rootEntry, root, change, expanded.get(id), rootBindings);
|
|
884
|
-
if (Object.keys(rootEntry).length)
|
|
962
|
+
if (rootBindings) {
|
|
963
|
+
inheritedLiterals(rootEntry, root, change, expanded.get(id), rootBindings, virtual, () => lost('instance paint binding kept as plain paint', id));
|
|
964
|
+
if (!virtual && Object.keys(rootEntry).length)
|
|
885
965
|
(m.overrides ??= {})[''] = rootEntry;
|
|
886
966
|
}
|
|
887
967
|
const chosen = new Set();
|
|
@@ -910,6 +990,17 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
910
990
|
const layers = doc.models;
|
|
911
991
|
const mainLayers = new Map(layers.filter(m => m.component === m.id).map(m => [m.sourceId, m]));
|
|
912
992
|
const mains = new Map(result.components.map(c => [c.id, mainLayers.get(c.rootId)]));
|
|
993
|
+
// Only a layer of an expanded instance has an instance above it; the walk up for overrides stops where that ends.
|
|
994
|
+
const instanceSource = (m) => layerSource.get(m.sourceId)?.node.type === 'instance';
|
|
995
|
+
const inside = new Map();
|
|
996
|
+
function insideInstance(m) {
|
|
997
|
+
let known = inside.get(m.id);
|
|
998
|
+
if (known === undefined) {
|
|
999
|
+
const parent = records.get(m.parent);
|
|
1000
|
+
inside.set(m.id, known = instanceSource(m) || !!parent && insideInstance(parent));
|
|
1001
|
+
}
|
|
1002
|
+
return known;
|
|
1003
|
+
}
|
|
913
1004
|
function expandedFields(m, change) {
|
|
914
1005
|
const source = layerSource.get(m.sourceId);
|
|
915
1006
|
if (expandedRadii.has(m.sourceId) && source.node.type === 'instance') {
|
|
@@ -950,19 +1041,17 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
950
1041
|
}
|
|
951
1042
|
}
|
|
952
1043
|
}
|
|
1044
|
+
// A layer under an expanded instance shows what the instances above it override (paints, styles, bindings),
|
|
1045
|
+
// the innermost first so that an outer instance's override wins.
|
|
1046
|
+
// An override's bindings and modes replace only the main's of the same fields and collections, as on a linked instance.
|
|
953
1047
|
const path = [m.sourceId];
|
|
954
|
-
for (let parent = records.get(m.parent); parent; parent = records.get(parent.parent)) {
|
|
955
|
-
if (
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
delete change[ref];
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
if (layerSource.get(parent.sourceId)?.node.type === 'instance')
|
|
965
|
-
path.unshift(parent.sourceId);
|
|
1048
|
+
for (let parent = records.get(m.parent); parent && insideInstance(parent); parent = records.get(parent.parent)) {
|
|
1049
|
+
if (!instanceSource(parent))
|
|
1050
|
+
continue;
|
|
1051
|
+
const own = result.sources.occurrenceFields.get(parent.sourceId)?.get(layerPathKey(path));
|
|
1052
|
+
if (own)
|
|
1053
|
+
change = withOverrideFields(change, own);
|
|
1054
|
+
path.unshift(parent.sourceId);
|
|
966
1055
|
}
|
|
967
1056
|
return change;
|
|
968
1057
|
}
|
|
@@ -970,8 +1059,15 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
970
1059
|
palette(m, m.id);
|
|
971
1060
|
const source = layerSource.get(m.sourceId);
|
|
972
1061
|
if (source)
|
|
973
|
-
install(
|
|
1062
|
+
install(expandedFields(m, source.figma.change), m, m, m.id);
|
|
974
1063
|
}
|
|
1064
|
+
// The main an inner instance occurrence shows: a switch by the outer instance or by a main in between wins.
|
|
1065
|
+
const kids = new Map();
|
|
1066
|
+
for (const m of layers)
|
|
1067
|
+
if (m.parent !== undefined)
|
|
1068
|
+
kids.get(m.parent)?.push(m.id) ?? kids.set(m.parent, [m.id]);
|
|
1069
|
+
const lookup = (id) => { const m = records.get(id); return m && isInstance(m) ? { ...m, children: kids.get(id) ?? [] } : m; };
|
|
1070
|
+
const shownMain = (id, entry, target) => entry.component ?? resolveVirtual(lookup, id, true)?.component ?? target.component;
|
|
975
1071
|
const nestedPalettes = [];
|
|
976
1072
|
for (const m of layers) {
|
|
977
1073
|
const authored = occurrences.get(m.id);
|
|
@@ -1020,6 +1116,8 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
1020
1116
|
if (!change && Object.keys(target.vars ?? {}).some(key => key.startsWith('radius.')))
|
|
1021
1117
|
delete entry.radius;
|
|
1022
1118
|
}
|
|
1119
|
+
else if (path && target && !change)
|
|
1120
|
+
inheritedLiterals(entry, records.get(shownMain(id, entry, target)), {}, expanded.get(id)); // an inner instance's root, shown after enclosing switches
|
|
1023
1121
|
if (change && target)
|
|
1024
1122
|
install(change, target, path ? entry : m, path ? id : m.id, !!path);
|
|
1025
1123
|
if (Object.keys(entry).length)
|
|
@@ -1031,7 +1129,7 @@ export function fileResources(result, doc, occurrences, expanded, expandedRadii,
|
|
|
1031
1129
|
// Install inner bindings before comparing an enclosing palette with the sources it inherits.
|
|
1032
1130
|
nestedPalettes.forEach(level => { for (const [entry, id] of level)
|
|
1033
1131
|
palette(entry, id, true); });
|
|
1034
|
-
// A style override replaces the slot as Figma does: bindings the occurrence inherits there no longer apply.
|
|
1132
|
+
// A style or paint override replaces the slot as Figma does: bindings the occurrence inherits there no longer apply.
|
|
1035
1133
|
for (const [entry, id, slot] of masked)
|
|
1036
1134
|
for (const path of inheritedBindings(id))
|
|
1037
1135
|
if (styled[slot].some(p => path.startsWith(p + '.')) && !Object.hasOwn(entry.vars ?? {}, path))
|
package/dist/adapter/link.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ export declare function linkComponents(doc: {
|
|
|
48
48
|
/** @internal Links components and tracks where their expanded layers went for import reports. */
|
|
49
49
|
export declare function linkWithLayers(doc: {
|
|
50
50
|
models: any[];
|
|
51
|
-
}, trackLayers?: boolean,
|
|
51
|
+
}, trackLayers?: boolean, binding?: (source: string, field: 'componentId' | 'characters', main?: string) => string | undefined, nativeFile?: boolean): {
|
|
52
52
|
counts: LinkCounts;
|
|
53
53
|
aliases: Map<string, string[]>;
|
|
54
54
|
omittedLinks: Map<string, Set<string>>;
|