@moondraft/import 0.1.0 → 0.2.0

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