@pairdraw/scene-graph 0.21.0 → 0.22.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 (100) hide show
  1. package/dist/constants.d.ts +10 -4
  2. package/dist/constants.d.ts.map +1 -1
  3. package/dist/constants.js +60 -2
  4. package/dist/constants.js.map +1 -1
  5. package/dist/copy.js +7 -0
  6. package/dist/copy.js.map +1 -1
  7. package/dist/effects.d.ts +9 -1
  8. package/dist/effects.d.ts.map +1 -1
  9. package/dist/effects.js +10 -1
  10. package/dist/effects.js.map +1 -1
  11. package/dist/fields/layout.d.ts +1 -1
  12. package/dist/fields/layout.d.ts.map +1 -1
  13. package/dist/fields/layout.js +3 -1
  14. package/dist/fields/layout.js.map +1 -1
  15. package/dist/geometry.d.ts.map +1 -1
  16. package/dist/geometry.js +3 -3
  17. package/dist/geometry.js.map +1 -1
  18. package/dist/graph.d.ts +4 -1
  19. package/dist/graph.d.ts.map +1 -1
  20. package/dist/graph.js +9 -0
  21. package/dist/graph.js.map +1 -1
  22. package/dist/hit-test.d.ts.map +1 -1
  23. package/dist/hit-test.js +1 -1
  24. package/dist/hit-test.js.map +1 -1
  25. package/dist/images.d.ts +17 -1
  26. package/dist/images.d.ts.map +1 -1
  27. package/dist/images.js +41 -1
  28. package/dist/images.js.map +1 -1
  29. package/dist/index.d.ts +2 -2
  30. package/dist/instances/fields.d.ts +5 -3
  31. package/dist/instances/fields.d.ts.map +1 -1
  32. package/dist/instances/fields.js +5 -0
  33. package/dist/instances/fields.js.map +1 -1
  34. package/dist/instances/index.d.ts +2 -2
  35. package/dist/instances/index.d.ts.map +1 -1
  36. package/dist/instances/index.js +8 -3
  37. package/dist/instances/index.js.map +1 -1
  38. package/dist/instances/placement.js +1 -1
  39. package/dist/instances/placement.js.map +1 -1
  40. package/dist/instances/sync.js +24 -5
  41. package/dist/instances/sync.js.map +1 -1
  42. package/dist/node-defaults.d.ts.map +1 -1
  43. package/dist/node-defaults.js +4 -0
  44. package/dist/node-defaults.js.map +1 -1
  45. package/dist/resize.d.ts +9 -2
  46. package/dist/resize.d.ts.map +1 -1
  47. package/dist/resize.js +20 -8
  48. package/dist/resize.js.map +1 -1
  49. package/dist/slots/content.d.ts +46 -1
  50. package/dist/slots/content.d.ts.map +1 -1
  51. package/dist/slots/content.js +146 -2
  52. package/dist/slots/content.js.map +1 -1
  53. package/dist/slots/index.d.ts +3 -2
  54. package/dist/slots/index.js +3 -2
  55. package/dist/slots/owners.d.ts +17 -0
  56. package/dist/slots/owners.d.ts.map +1 -0
  57. package/dist/slots/owners.js +39 -0
  58. package/dist/slots/owners.js.map +1 -0
  59. package/dist/types.d.ts +115 -5
  60. package/dist/types.d.ts.map +1 -1
  61. package/dist/types.js.map +1 -1
  62. package/dist/variables/bindings.d.ts +9 -3
  63. package/dist/variables/bindings.d.ts.map +1 -1
  64. package/dist/variables/bindings.js +128 -26
  65. package/dist/variables/bindings.js.map +1 -1
  66. package/dist/variables/fields.d.ts +20 -1
  67. package/dist/variables/fields.d.ts.map +1 -1
  68. package/dist/variables/fields.js +29 -3
  69. package/dist/variables/fields.js.map +1 -1
  70. package/dist/variables/index.d.ts +3 -3
  71. package/dist/variables/index.d.ts.map +1 -1
  72. package/dist/variables/index.js +25 -4
  73. package/dist/variables/index.js.map +1 -1
  74. package/dist/variant-properties.d.ts +7 -2
  75. package/dist/variant-properties.d.ts.map +1 -1
  76. package/dist/variant-properties.js +35 -3
  77. package/dist/variant-properties.js.map +1 -1
  78. package/package.json +1 -1
  79. package/src/constants.ts +32 -3
  80. package/src/copy.ts +7 -0
  81. package/src/effects.ts +21 -0
  82. package/src/fields/layout.ts +3 -1
  83. package/src/geometry.ts +5 -3
  84. package/src/graph.ts +13 -0
  85. package/src/hit-test.ts +7 -1
  86. package/src/images.ts +61 -2
  87. package/src/instances/fields.ts +5 -0
  88. package/src/instances/index.ts +10 -1
  89. package/src/instances/placement.ts +2 -1
  90. package/src/instances/sync.ts +38 -5
  91. package/src/node-defaults.ts +4 -0
  92. package/src/resize.ts +44 -6
  93. package/src/slots/content.ts +208 -1
  94. package/src/slots/index.ts +1 -0
  95. package/src/slots/owners.ts +45 -0
  96. package/src/types.ts +207 -3
  97. package/src/variables/bindings.ts +155 -33
  98. package/src/variables/fields.ts +41 -2
  99. package/src/variables/index.ts +40 -1
  100. package/src/variant-properties.ts +36 -2
@@ -2,6 +2,7 @@ import { compact, partition, sortBy, uniq } from 'es-toolkit/array'
2
2
  import { omit, omitBy } from 'es-toolkit/object'
3
3
 
4
4
  import { BLACK } from '../constants'
5
+ import { effectSupportsBinding } from '../effects'
5
6
  import type { SceneGraph } from '../graph'
6
7
  import { findInstanceAncestor } from '../instances'
7
8
  import {
@@ -26,6 +27,8 @@ import {
26
27
  assignVariableBindingUnits
27
28
  } from './bindings'
28
29
  import {
30
+ effectBindingTarget,
31
+ effectColorBindingTarget,
29
32
  BOOLEAN_BINDING_FIELDS,
30
33
  ICON_BINDING_FIELDS,
31
34
  NUMERIC_FIELDS,
@@ -360,6 +363,31 @@ const ACCEPTED_BINDING_FIELDS: readonly string[] = [
360
363
  * Throws when a variable of `type` cannot bind to `field`, or for a field no binding takes. A
361
364
  * paint target is checked by its caller, which also validates its index.
362
365
  */
366
+ /** A blur has no spread, offset or colour to bind; only a shadow effect has those fields. */
367
+ function assertEffectFieldBinding(node: SceneNode, field: string): void {
368
+ const numeric = effectBindingTarget(field)
369
+ const color = effectColorBindingTarget(field)
370
+ const effectIndex = numeric?.index ?? color?.index
371
+ if (effectIndex === undefined) return
372
+ const effect = node.effects.at(effectIndex)
373
+ // A binding can arrive before its effect (paths are inert until then); only a present one's
374
+ // shape can reject it.
375
+ if (!effect) return
376
+ const bound = numeric?.field ?? 'color'
377
+ if (!effectSupportsBinding(effect, bound))
378
+ throw new Error(`Cannot bind ${bound} on a ${effect.type} effect`)
379
+ }
380
+
381
+ function isColorBindingField(field: string): boolean {
382
+ return paintBindingTarget(field) !== null || effectColorBindingTarget(field) !== null
383
+ }
384
+
385
+ /** A shadow's colour takes only a colour variable; the check lives out of the big filter. */
386
+ function rejectWrongEffectColor(field: string, type: string): void {
387
+ if (effectColorBindingTarget(field) && type !== 'COLOR')
388
+ throw new Error(`Cannot bind ${type} variable to shadow colour "${field}"`)
389
+ }
390
+
363
391
  function checkBindingFieldType(field: string, type: VariableType): void {
364
392
  if (isNumericVariableBindingField(field) && type !== 'FLOAT') {
365
393
  throw new Error(`Cannot bind ${type} variable to scalar field "${field}"`)
@@ -382,6 +410,8 @@ function checkBindingFieldType(field: string, type: VariableType): void {
382
410
  throw new Error(`Cannot bind ${type} variable to icon field "${field}"`)
383
411
  }
384
412
 
413
+ rejectWrongEffectColor(field, type)
414
+
385
415
  // A shader uniform takes a number, a colour, or a boolean; its shader declares which.
386
416
  const uniform = uniformBindingTarget(field)
387
417
  if (uniform && type !== 'FLOAT' && type !== 'COLOR' && type !== 'BOOLEAN') {
@@ -393,7 +423,7 @@ function checkBindingFieldType(field: string, type: VariableType): void {
393
423
  STRING_BINDING_FIELDS.has(field) ||
394
424
  BOOLEAN_BINDING_FIELDS.has(field) ||
395
425
  ICON_BINDING_FIELDS.has(field) ||
396
- paintBindingTarget(field) !== null ||
426
+ isColorBindingField(field) ||
397
427
  uniform !== null
398
428
 
399
429
  if (!isKnownField) {
@@ -437,6 +467,7 @@ export function bindVariable(
437
467
  }
438
468
 
439
469
  checkBindingFieldType(field, variable.type)
470
+ assertEffectFieldBinding(node, field)
440
471
 
441
472
  // A shader input belongs to one paint, so its index must exist on the layer.
442
473
  const uniform = uniformBindingTarget(field)
@@ -541,3 +572,11 @@ export * from './text'
541
572
  export * from './token'
542
573
  export * from './units'
543
574
  export * from './value'
575
+
576
+ export {
577
+ effectBindingTarget,
578
+ effectColorBindingTarget,
579
+ layoutGridBindingTarget,
580
+ type EffectBindingField,
581
+ type LayoutGridBindingField
582
+ } from './fields'
@@ -86,13 +86,47 @@ export function deriveNamedVariantProperties(
86
86
  return { definitions, variants }
87
87
  }
88
88
 
89
- /** Variant properties from component names: slash paths first, else `Property=Value` pairs. */
89
+ /**
90
+ * A plain-named set's single property, as Figma makes it: `Property 1` with each component's
91
+ * trimmed name as an option, and the variants renamed after those values.
92
+ */
93
+ export function derivePlainNameVariantProperties(
94
+ components: ReadonlyArray<Pick<SceneNode, 'id' | 'name'>>,
95
+ createPropertyId: () => string
96
+ ): DerivedVariantProperties | null {
97
+ if (components.length === 0) return null
98
+ // A repeated or empty name still needs a distinct option, so later ones count up.
99
+ const seen = new Map<string, number>()
100
+ const values = components.map((component) => {
101
+ const base = component.name.trim() || 'Variant'
102
+ const count = (seen.get(base) ?? 0) + 1
103
+ seen.set(base, count)
104
+ return count === 1 ? base : `${base} ${count}`
105
+ })
106
+ const definition: ComponentPropertyDefinition = {
107
+ id: createPropertyId(),
108
+ name: 'Property 1',
109
+ type: 'VARIANT',
110
+ defaultValue: values[0] ?? '',
111
+ variantOptions: [...new Set(values)]
112
+ }
113
+ const variants = new Map(
114
+ components.map((component, index) => {
115
+ const value = values[index] ?? ''
116
+ return [component.id, { componentPropertyValues: { 'Property 1': value }, name: value }]
117
+ })
118
+ )
119
+ return { definitions: [definition], variants }
120
+ }
121
+
122
+ /** Variant properties from component names: slash paths, `Property=Value` pairs, else the names. */
90
123
  export function deriveVariantProperties(
91
124
  components: ReadonlyArray<Pick<SceneNode, 'id' | 'name'>>,
92
125
  createPropertyId: () => string
93
126
  ): DerivedVariantProperties | null {
94
127
  return (
95
128
  deriveSlashVariantProperties(components, createPropertyId) ??
96
- deriveNamedVariantProperties(components, createPropertyId)
129
+ deriveNamedVariantProperties(components, createPropertyId) ??
130
+ derivePlainNameVariantProperties(components, createPropertyId)
97
131
  )
98
132
  }