@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.
Files changed (98) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +2 -2
  3. package/SOURCE.md +343 -4
  4. package/dist/adapter/convert.d.ts +26 -2
  5. package/dist/adapter/convert.js +817 -217
  6. package/dist/adapter/figma-descriptions.d.ts +7 -0
  7. package/dist/adapter/figma-descriptions.js +34 -0
  8. package/dist/adapter/figma-grids.d.ts +12 -0
  9. package/dist/adapter/figma-grids.js +175 -0
  10. package/dist/adapter/figma-handoff.d.ts +34 -0
  11. package/dist/adapter/figma-handoff.js +188 -0
  12. package/dist/adapter/figma-hyperlinks.d.ts +13 -0
  13. package/dist/adapter/figma-hyperlinks.js +77 -0
  14. package/dist/adapter/figma.d.ts +4 -1
  15. package/dist/adapter/figma.js +15 -4
  16. package/dist/adapter/file-components.d.ts +10 -3
  17. package/dist/adapter/file-components.js +126 -33
  18. package/dist/adapter/file-resources.js +545 -47
  19. package/dist/adapter/file-typography.d.ts +6 -0
  20. package/dist/adapter/file-typography.js +44 -0
  21. package/dist/adapter/image-crop.d.ts +18 -0
  22. package/dist/adapter/image-crop.js +30 -0
  23. package/dist/adapter/link.d.ts +12 -1
  24. package/dist/adapter/link.js +380 -113
  25. package/dist/adapter/payload.js +3 -3
  26. package/dist/adapter/variables.d.ts +2 -1
  27. package/dist/adapter/variables.js +5 -2
  28. package/dist/figma-file.d.ts +1 -0
  29. package/dist/figma-file.js +1 -0
  30. package/dist/figma.d.ts +1 -0
  31. package/dist/figma.js +191 -35
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.js +1 -0
  34. package/dist/report.d.ts +59 -1
  35. package/dist/report.js +176 -30
  36. package/dist/serialize.d.ts +7 -0
  37. package/dist/serialize.js +79 -0
  38. package/dist/taco/figma-import/container/kiwiContainer.d.ts +3 -0
  39. package/dist/taco/figma-import/container/kiwiContainer.js +1 -1
  40. package/dist/taco/figma-import/figFile.js +3 -0
  41. package/dist/taco/figma-import/import/carry.d.ts +14 -1
  42. package/dist/taco/figma-import/import/carry.js +34 -1
  43. package/dist/taco/figma-import/import/componentProps.d.ts +10 -4
  44. package/dist/taco/figma-import/import/componentProps.js +29 -13
  45. package/dist/taco/figma-import/import/componentSets.d.ts +2 -0
  46. package/dist/taco/figma-import/import/componentSets.js +1 -1
  47. package/dist/taco/figma-import/import/converters/effects.d.ts +3 -13
  48. package/dist/taco/figma-import/import/converters/effects.js +4 -34
  49. package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
  50. package/dist/taco/figma-import/import/converters/exportSettings.js +75 -0
  51. package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
  52. package/dist/taco/figma-import/import/converters/frame.js +15 -6
  53. package/dist/taco/figma-import/import/converters/gradient.d.ts +2 -0
  54. package/dist/taco/figma-import/import/converters/gradient.js +3 -1
  55. package/dist/taco/figma-import/import/converters/grid.d.ts +12 -0
  56. package/dist/taco/figma-import/import/converters/grid.js +97 -0
  57. package/dist/taco/figma-import/import/converters/group.d.ts +2 -4
  58. package/dist/taco/figma-import/import/converters/group.js +6 -15
  59. package/dist/taco/figma-import/import/converters/image.d.ts +2 -1
  60. package/dist/taco/figma-import/import/converters/image.js +60 -16
  61. package/dist/taco/figma-import/import/converters/layout.d.ts +8 -7
  62. package/dist/taco/figma-import/import/converters/layout.js +66 -37
  63. package/dist/taco/figma-import/import/converters/rectangle.d.ts +1 -1
  64. package/dist/taco/figma-import/import/converters/rectangle.js +3 -3
  65. package/dist/taco/figma-import/import/converters/section.d.ts +2 -3
  66. package/dist/taco/figma-import/import/converters/section.js +5 -11
  67. package/dist/taco/figma-import/import/converters/shared.d.ts +9 -5
  68. package/dist/taco/figma-import/import/converters/shared.js +23 -10
  69. package/dist/taco/figma-import/import/converters/text.d.ts +38 -1
  70. package/dist/taco/figma-import/import/converters/text.js +248 -32
  71. package/dist/taco/figma-import/import/converters/vector.d.ts +36 -15
  72. package/dist/taco/figma-import/import/converters/vector.js +214 -73
  73. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +19 -3
  74. package/dist/taco/figma-import/import/converters/vectorNetwork.js +147 -29
  75. package/dist/taco/figma-import/import/instances.d.ts +32 -5
  76. package/dist/taco/figma-import/import/instances.js +613 -130
  77. package/dist/taco/figma-import/import/styles.d.ts +2 -0
  78. package/dist/taco/figma-import/import/styles.js +6 -0
  79. package/dist/taco/figma-import/import/types.d.ts +8 -0
  80. package/dist/taco/figma-import/import/variables.d.ts +5 -2
  81. package/dist/taco/figma-import/import/variables.js +65 -31
  82. package/dist/taco/figma-import/import/variantTrees.d.ts +1 -1
  83. package/dist/taco/figma-import/import/variantTrees.js +46 -4
  84. package/dist/taco/figma-import/import/walk.d.ts +62 -7
  85. package/dist/taco/figma-import/import/walk.js +328 -122
  86. package/dist/taco/figma-import/kiwi/message.d.ts +4 -2
  87. package/dist/taco/figma-import/kiwi/message.js +59 -15
  88. package/dist/taco/model/codec.d.ts +2 -2
  89. package/dist/taco/model/codec.js +72 -8
  90. package/dist/taco/model/component-evaluation.d.ts +2 -0
  91. package/dist/taco/model/component-evaluation.js +7 -0
  92. package/dist/taco/model/instance-resolver.d.ts +6 -2
  93. package/dist/taco/model/instance-resolver.js +50 -18
  94. package/dist/taco/model/sha256.js +13 -8
  95. package/dist/taco/model/types.d.ts +57 -5
  96. package/dist/taco/model/types.js +4 -0
  97. package/dist/taco/svg-import/style/computed.js +1 -0
  98. package/package.json +3 -2
@@ -1,17 +1,23 @@
1
+ import { definitions as propertyDefinitions, effectiveRecord, isInstance, ownerOf, resolveVirtual, splitVirtual } from '@moondraft/editor/core';
1
2
  import { guidText } from '../taco/figma-import/import/fields.js';
2
3
  import { formatExpressionName, parseLayerPath } from '../taco/model/index.js';
4
+ import { keepDescription } from './figma-descriptions.js';
5
+ import { expansionReason } from './link.js';
3
6
  /** Installs local property IDs, their bindings, instance assignments and slot content.
4
7
  * @param result - Original Figma component declarations and placements.
5
8
  * @param doc - Linked document, with source IDs for the original nodes.
9
+ * @param nestedBooleans - Enclosing instance assignments carried through expanded layers and their linked paths.
6
10
  * @param report - Receives occurrences the native document cannot represent.
7
- * @returns Authored Figma fields keyed by the surviving instance and native override path.
11
+ * @param expandedReasons - Per instance linking left expanded, by layer ID: linking's reason, which its report entry names.
12
+ * @returns Authored override fields and the Figma sources of newly created sets.
8
13
  */
9
- export function fileComponents(result, doc, report) {
14
+ export function fileComponents(result, doc, nestedBooleans, report, expandedReasons = new Map()) {
10
15
  const models = doc.models;
11
16
  const byId = new Map(models.map(m => [m.id, m]));
12
17
  const mains = new Map(), bySource = new Map();
13
18
  const source = new Map(result.nodes.map(n => [n.id, n]));
14
19
  const componentOfRoot = new Map(result.components.map(c => [c.rootId, c.id]));
20
+ const componentSources = new Map(Object.entries(result.componentIds).map(([guid, id]) => [id, result.sources.tree.nodes.get(guid)]));
15
21
  for (const m of models) {
16
22
  if (m.component === m.id)
17
23
  mains.set(componentOfRoot.get(m.sourceId), m);
@@ -59,17 +65,23 @@ export function fileComponents(result, doc, report) {
59
65
  return refs;
60
66
  }
61
67
  const components = new Map(result.components.map(c => [c.id, c]));
68
+ const importedSets = new Map(result.sources.imported.filter(({ figma }) => figma.change.isStateGroup === true)
69
+ .map(({ node, figma }) => [figma.id, node.id]));
70
+ const setSources = new Map();
62
71
  for (const [guid, { plan, properties }] of result.sources.variants) {
63
72
  const members = plan.variants;
64
- let set = bySource.get(result.layerIds[guid]);
73
+ const sourceId = importedSets.get(guid);
74
+ let set = sourceId === undefined ? undefined : bySource.get(sourceId);
65
75
  if (!set) {
66
76
  set = { id: 'set-' + guid, kind: 'frame', name: plan.set.name, parent: '@components', w: 0, h: 0 };
67
77
  set.sourceId = set.id;
68
78
  result.layerIds[guid] = set.sourceId;
79
+ setSources.set(set.sourceId, plan.set);
69
80
  models.push(set);
70
81
  byId.set(set.id, set);
71
82
  }
72
83
  set.key = plan.set.change.componentKey || 'figma-' + guid;
84
+ keepDescription(plan.set, set, report);
73
85
  const variantDefs = new Map(plan.set.change.componentPropDefs.map(d => ['figma-' + guidText(d.id, guid), d]));
74
86
  const orders = new Map((plan.set.change.stateGroupPropertyValueOrders ?? []).map(o => [o.property, o.values]));
75
87
  const axes = plan.properties.flatMap(p => {
@@ -112,6 +124,7 @@ export function fileComponents(result, doc, report) {
112
124
  for (const c of result.components) {
113
125
  const main = mains.get(c.id);
114
126
  main.key = c.key;
127
+ keepDescription(componentSources.get(c.id), main, report);
115
128
  if (!definitions.has(c.id))
116
129
  definitions.set(c.id, declare(main, c.properties ?? [], false));
117
130
  else
@@ -131,11 +144,19 @@ export function fileComponents(result, doc, report) {
131
144
  pending.push(child);
132
145
  }
133
146
  }
134
- function values(component, assignments) {
147
+ const componentOfMain = (id) => componentOfRoot.get(byId.get(id)?.sourceId);
148
+ function values(component, assignments, id) {
135
149
  const refs = definitions.get(component);
136
150
  return Object.fromEntries(Object.entries(assignments).flatMap(([key, value]) => {
137
151
  const p = refs.get(key);
138
- return p ? [[p.id, p.kind === 'swap' ? mains.get(value).id : value]] : [];
152
+ // The selected variant is already represented by the linked main.
153
+ if (p?.kind === 'variant')
154
+ return [];
155
+ if (!p) {
156
+ report.push({ kind: 'left-out', what: 'component property unavailable on the imported instance', count: 1, ids: [id] });
157
+ return [];
158
+ }
159
+ return [[p.id, p.kind === 'swap' ? mains.get(value).id : value]];
139
160
  }));
140
161
  }
141
162
  // Reader paths cross instances and name the final layer. Native paths also include each ordinary parent.
@@ -148,6 +169,7 @@ export function fileComponents(result, doc, report) {
148
169
  }).join(';');
149
170
  }
150
171
  const occurrences = new Map();
172
+ const instanceOverrides = new Map();
151
173
  const hosts = new Set(result.nodes.filter(n => n.type === 'frame' && n.slotHost).map(n => n.id));
152
174
  const usedHosts = new Set();
153
175
  const slots = new Set();
@@ -175,8 +197,9 @@ export function fileComponents(result, doc, report) {
175
197
  for (const [field, expression] of Object.entries(n.expressions ?? {})) {
176
198
  const p = properties.find(p => formatExpressionName(p.name) === expression);
177
199
  const bind = { characters: 'content', visible: 'visible', componentId: 'component' }[field];
178
- if (p && bind && original && (bind !== 'component' || m.component)) {
179
- (m.bind ??= {})[bind] = definitions.get(component.id).get(p.id).id;
200
+ const ref = p && definitions.get(component.id).get(p.id);
201
+ if (ref && ref.kind !== 'variant' && bind && original && (bind !== 'component' || m.component)) {
202
+ (m.bind ??= {})[bind] = ref.id;
180
203
  }
181
204
  else
182
205
  report.push({ kind: 'approximated', what: 'expression', count: 1, ids: [m.id] });
@@ -196,19 +219,36 @@ export function fileComponents(result, doc, report) {
196
219
  }
197
220
  if (n.type !== 'instance' || m.component === m.id)
198
221
  continue;
222
+ const fills = n.slots ?? {};
223
+ for (const [path, nested] of Object.entries(fills)) {
224
+ if (path)
225
+ report.push({ kind: 'approximated', what: 'a nested slot assignment cannot be kept editable because its fill needs a stored instance owner',
226
+ count: Object.keys(nested).length, ids: [m.id] });
227
+ }
199
228
  if (!m.component) {
200
- report.push({ kind: 'approximated', what: 'expanded instance', count: 1, ids: [m.id] });
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] });
201
231
  continue;
202
232
  }
203
233
  const fields = result.sources.occurrenceFields.get(n.id);
204
234
  if (fields)
205
235
  occurrences.set(m.id, new Map([...fields].map(([key, change]) => [overridePath(key), change])));
206
- const refs = definitions.get(n.componentId);
207
- const assignments = values(n.componentId, n.propertyValues ?? {});
208
- // A member may override a set declaration's default. Native defaults belong to the set.
209
- for (const p of components.get(n.componentId).properties ?? []) {
236
+ // The editor resolves a stored bound swap from the enclosing main or set's declaration.
237
+ let retainedMain = m.component;
238
+ if (m.bind?.component) {
239
+ const owner = ownerOf(id => byId.get(id), enclosing.get(m.id));
240
+ const swap = propertyDefinitions(owner).find(p => p.id === m.bind.component);
241
+ if (swap?.kind === 'swap')
242
+ retainedMain = swap.default;
243
+ }
244
+ const shown = componentOfMain(retainedMain), refs = definitions.get(shown);
245
+ const inherited = nestedBooleans.get(m.id);
246
+ const assignments = values(shown, inherited?.componentId === shown
247
+ ? { ...n.propertyValues, ...inherited.values } : n.propertyValues ?? {}, m.id);
248
+ // A member may override a set declaration's default. A variant axis is selected by the component itself.
249
+ for (const p of components.get(shown).properties ?? []) {
210
250
  const ref = refs.get(p.id);
211
- if (!ref || ref.id in assignments)
251
+ if (!ref || ref.kind === 'variant' || ref.id in assignments)
212
252
  continue;
213
253
  const value = p.kind === 'instance' ? mains.get(p.defaultValue).id : p.defaultValue;
214
254
  if (value !== ref.default)
@@ -216,25 +256,13 @@ export function fileComponents(result, doc, report) {
216
256
  }
217
257
  if (Object.keys(assignments).length)
218
258
  m.props = assignments;
219
- for (const [key, entry] of Object.entries(n.overrides ?? {}))
220
- if (entry.propertyValues) {
221
- const target = source.get(parseLayerPath(key).at(-1));
222
- const path = overridePath(key);
223
- ((m.overrides ??= {})[path] ??= {}).props = values(entry.componentId ?? target.componentId, entry.propertyValues);
224
- }
225
- for (const [path, fills] of Object.entries(n.slots ?? {})) {
226
- for (const [slot, id] of Object.entries(fills)) {
227
- const host = bySource.get(id);
228
- if (path) {
229
- // Native fill frames belong to a stored instance. A virtual instance cannot own one.
230
- report.push({ kind: 'approximated', what: 'nested slot fill', count: 1, ids: [m.id] });
231
- }
232
- else {
233
- usedHosts.add(host.id);
234
- host.parent = m.id;
235
- host.fillsSlot = slot;
236
- }
237
- }
259
+ if (n.overrides)
260
+ instanceOverrides.set(m.id, n.overrides);
261
+ for (const [slot, id] of Object.entries(fills[''] ?? {})) {
262
+ const host = bySource.get(id);
263
+ usedHosts.add(host.id);
264
+ host.parent = m.id;
265
+ host.fillsSlot = slot;
238
266
  }
239
267
  }
240
268
  // Reparented private fills and hidden set members must follow their stored parents on load.
@@ -249,6 +277,71 @@ export function fileComponents(result, doc, report) {
249
277
  else
250
278
  children.set(parent, [m]);
251
279
  }
280
+ // Resolve virtual paths after slot fills have moved to their native parents.
281
+ const get = (id) => {
282
+ const layer = byId.get(id);
283
+ return isInstance(layer) ? { ...layer, children: children.get(id)?.map(child => child.id) ?? [] } : layer;
284
+ };
285
+ for (const [holderId, overrides] of instanceOverrides) {
286
+ const holder = byId.get(holderId);
287
+ for (const [key, entry] of Object.entries(overrides)) {
288
+ if (!entry.componentId && !entry.propertyValues)
289
+ continue;
290
+ const path = overridePath(key), id = holderId + ';' + path;
291
+ const target = resolveVirtual(get, id, true);
292
+ // The linker compares appearances. An explicit swap can select the same main as an inherited swap.
293
+ if (entry.componentId && target?.component && target.component === mains.get(entry.componentId)?.id) {
294
+ ((holder.overrides ??= {})[path] ??= {}).component = target.component;
295
+ }
296
+ if (!entry.propertyValues)
297
+ continue;
298
+ if (!target?.component || !isInstance(target.layer)) {
299
+ report.push({ kind: 'left-out', what: 'component property override requires a nested instance', count: 1, ids: [path && target ? id : holderId] });
300
+ continue;
301
+ }
302
+ const assignments = values(componentOfMain(target.component), entry.propertyValues, id);
303
+ if (Object.keys(assignments).length)
304
+ ((holder.overrides ??= {})[path] ??= {}).props = assignments;
305
+ }
306
+ }
307
+ // Load derives a bound component from the property on its enclosing instance occurrence.
308
+ // Keep the literal for path resolution and store its controlling value alongside the other assignments.
309
+ for (const holder of models) {
310
+ if (!isInstance(holder))
311
+ continue;
312
+ for (const [path, entry] of Object.entries(holder.overrides ?? {})) {
313
+ if (!entry.component)
314
+ continue;
315
+ const target = resolveVirtual(get, holder.id + ';' + path, true);
316
+ const binding = target?.layer.bind?.component;
317
+ if (!target || !binding)
318
+ continue;
319
+ const at = target.levels.at(-1)[1];
320
+ const owner = at === 0 ? holder : (holder.overrides[target.parts.slice(1, at + 1).join(';')] ??= {});
321
+ (owner.props ??= {})[binding] = entry.component;
322
+ }
323
+ }
324
+ // A flattened ancestor can also leave an assignment inside a surviving linked instance.
325
+ // Apply inherited paths before the longer paths of the instances that use them.
326
+ for (const [id, inherited] of [...nestedBooleans].filter(([id]) => id.includes(';')).sort(([a], [b]) => a.length - b.length)) {
327
+ const target = resolveVirtual(get, id);
328
+ if (!target?.component || componentOfMain(target.component) !== inherited.componentId)
329
+ continue;
330
+ const current = effectiveRecord(get, id)?.props;
331
+ const refs = definitions.get(inherited.componentId);
332
+ const assignments = {};
333
+ for (const [key, value] of Object.entries(inherited.values)) {
334
+ const def = refs.get(key);
335
+ if (def?.kind !== 'boolean' || current?.[def.id] === value)
336
+ continue;
337
+ assignments[def.id] = value;
338
+ }
339
+ if (Object.keys(assignments).length) {
340
+ const [holderId, path] = splitVirtual(id), holder = byId.get(holderId);
341
+ const entry = ((holder.overrides ??= {})[path] ??= {});
342
+ entry.props = { ...entry.props, ...assignments };
343
+ }
344
+ }
252
345
  doc.models = [];
253
346
  function append(parent) {
254
347
  for (const m of children.get(parent) ?? []) {
@@ -258,5 +351,5 @@ export function fileComponents(result, doc, report) {
258
351
  }
259
352
  append('');
260
353
  append('@components');
261
- return occurrences;
354
+ return { occurrences, setSources };
262
355
  }