@pairdraw/scene-graph 0.19.2 → 0.21.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 (285) hide show
  1. package/dist/color/index.d.ts +28 -7
  2. package/dist/color/index.d.ts.map +1 -1
  3. package/dist/color/index.js +40 -15
  4. package/dist/color/index.js.map +1 -1
  5. package/dist/color/management.d.ts +19 -6
  6. package/dist/color/management.d.ts.map +1 -1
  7. package/dist/color/management.js +56 -1
  8. package/dist/color/management.js.map +1 -1
  9. package/dist/color/profile.d.ts +32 -0
  10. package/dist/color/profile.d.ts.map +1 -0
  11. package/dist/color/profile.js +140 -0
  12. package/dist/color/profile.js.map +1 -0
  13. package/dist/comments/index.d.ts +2 -2
  14. package/dist/comments/index.js +2 -2
  15. package/dist/comments/merge.d.ts.map +1 -1
  16. package/dist/comments/merge.js +1 -0
  17. package/dist/comments/merge.js.map +1 -1
  18. package/dist/comments/schema.d.ts +3 -1
  19. package/dist/comments/schema.d.ts.map +1 -1
  20. package/dist/comments/schema.js +19 -2
  21. package/dist/comments/schema.js.map +1 -1
  22. package/dist/comments/threads.d.ts +3 -1
  23. package/dist/comments/threads.d.ts.map +1 -1
  24. package/dist/comments/threads.js +2 -1
  25. package/dist/comments/threads.js.map +1 -1
  26. package/dist/components/index.d.ts +2 -2
  27. package/dist/components/index.js +2 -2
  28. package/dist/components/properties.d.ts.map +1 -1
  29. package/dist/components/properties.js +7 -7
  30. package/dist/components/properties.js.map +1 -1
  31. package/dist/components/variants.d.ts +12 -1
  32. package/dist/components/variants.d.ts.map +1 -1
  33. package/dist/components/variants.js +30 -1
  34. package/dist/components/variants.js.map +1 -1
  35. package/dist/constants.d.ts +25 -1
  36. package/dist/constants.d.ts.map +1 -1
  37. package/dist/constants.js +94 -1
  38. package/dist/constants.js.map +1 -1
  39. package/dist/copy.d.ts.map +1 -1
  40. package/dist/copy.js +17 -1
  41. package/dist/copy.js.map +1 -1
  42. package/dist/css/index.d.ts +2 -2
  43. package/dist/css/index.js +2 -2
  44. package/dist/css/shadows.d.ts +2 -2
  45. package/dist/css/shadows.d.ts.map +1 -1
  46. package/dist/css/shadows.js +4 -4
  47. package/dist/css/shadows.js.map +1 -1
  48. package/dist/css/text.d.ts +17 -1
  49. package/dist/css/text.d.ts.map +1 -1
  50. package/dist/css/text.js +62 -1
  51. package/dist/css/text.js.map +1 -1
  52. package/dist/css/values.d.ts +7 -2
  53. package/dist/css/values.d.ts.map +1 -1
  54. package/dist/css/values.js +8 -4
  55. package/dist/css/values.js.map +1 -1
  56. package/dist/design-memory/index.d.ts +3 -0
  57. package/dist/design-memory/index.js +3 -0
  58. package/dist/design-memory/model.d.ts +33 -0
  59. package/dist/design-memory/model.d.ts.map +1 -0
  60. package/dist/design-memory/model.js +142 -0
  61. package/dist/design-memory/model.js.map +1 -0
  62. package/dist/design-memory/schema.d.ts +76 -0
  63. package/dist/design-memory/schema.d.ts.map +1 -0
  64. package/dist/design-memory/schema.js +72 -0
  65. package/dist/design-memory/schema.js.map +1 -0
  66. package/dist/effects.d.ts +7 -4
  67. package/dist/effects.d.ts.map +1 -1
  68. package/dist/effects.js +9 -4
  69. package/dist/effects.js.map +1 -1
  70. package/dist/empty-values.js +102 -0
  71. package/dist/empty-values.js.map +1 -0
  72. package/dist/fields/components.d.ts +9 -0
  73. package/dist/fields/components.d.ts.map +1 -0
  74. package/dist/fields/components.js +24 -0
  75. package/dist/fields/components.js.map +1 -0
  76. package/dist/fields/index.d.ts +3 -2
  77. package/dist/fields/index.js +3 -2
  78. package/dist/fields/layout.d.ts +3 -1
  79. package/dist/fields/layout.d.ts.map +1 -1
  80. package/dist/fields/layout.js +10 -1
  81. package/dist/fields/layout.js.map +1 -1
  82. package/dist/graph.d.ts +12 -1
  83. package/dist/graph.d.ts.map +1 -1
  84. package/dist/graph.js +39 -7
  85. package/dist/graph.js.map +1 -1
  86. package/dist/grid-values.d.ts +10 -0
  87. package/dist/grid-values.d.ts.map +1 -0
  88. package/dist/grid-values.js +53 -0
  89. package/dist/grid-values.js.map +1 -0
  90. package/dist/hit-test/drop.d.ts +20 -0
  91. package/dist/hit-test/drop.d.ts.map +1 -0
  92. package/dist/hit-test/drop.js +54 -0
  93. package/dist/hit-test/drop.js.map +1 -0
  94. package/dist/hit-test/geometry.js +86 -0
  95. package/dist/hit-test/geometry.js.map +1 -0
  96. package/dist/hit-test/masks.d.ts +14 -0
  97. package/dist/hit-test/masks.d.ts.map +1 -0
  98. package/dist/hit-test/masks.js +41 -0
  99. package/dist/hit-test/masks.js.map +1 -0
  100. package/dist/hit-test/paint.d.ts +6 -0
  101. package/dist/hit-test/paint.d.ts.map +1 -0
  102. package/dist/hit-test/paint.js +19 -0
  103. package/dist/hit-test/paint.js.map +1 -0
  104. package/dist/hit-test.d.ts +4 -15
  105. package/dist/hit-test.d.ts.map +1 -1
  106. package/dist/hit-test.js +5 -173
  107. package/dist/hit-test.js.map +1 -1
  108. package/dist/images.d.ts +3 -2
  109. package/dist/images.d.ts.map +1 -1
  110. package/dist/images.js +18 -2
  111. package/dist/images.js.map +1 -1
  112. package/dist/index.d.ts +2 -2
  113. package/dist/instances/copies.js +2 -1
  114. package/dist/instances/copies.js.map +1 -1
  115. package/dist/instances/fields.d.ts.map +1 -1
  116. package/dist/instances/fields.js +2 -0
  117. package/dist/instances/fields.js.map +1 -1
  118. package/dist/instances/index.d.ts +2 -1
  119. package/dist/instances/index.d.ts.map +1 -1
  120. package/dist/instances/index.js +13 -12
  121. package/dist/instances/index.js.map +1 -1
  122. package/dist/instances/overrides.d.ts +15 -1
  123. package/dist/instances/overrides.d.ts.map +1 -1
  124. package/dist/instances/overrides.js +37 -1
  125. package/dist/instances/overrides.js.map +1 -1
  126. package/dist/instances/source-walk.d.ts +12 -0
  127. package/dist/instances/source-walk.d.ts.map +1 -0
  128. package/dist/instances/swap-overrides.js +3 -3
  129. package/dist/instances/swap-overrides.js.map +1 -1
  130. package/dist/instances/sync.js +9 -7
  131. package/dist/instances/sync.js.map +1 -1
  132. package/dist/node-defaults.d.ts.map +1 -1
  133. package/dist/node-defaults.js +43 -38
  134. package/dist/node-defaults.js.map +1 -1
  135. package/dist/plugin-data/fields.d.ts +90 -0
  136. package/dist/plugin-data/fields.d.ts.map +1 -1
  137. package/dist/plugin-data/fields.js +33 -24
  138. package/dist/plugin-data/fields.js.map +1 -1
  139. package/dist/plugin-data/index.d.ts +2 -2
  140. package/dist/plugin-data/index.js +2 -2
  141. package/dist/plugin-data/schema.d.ts +6 -1
  142. package/dist/plugin-data/schema.d.ts.map +1 -1
  143. package/dist/plugin-data/schema.js +24 -1
  144. package/dist/plugin-data/schema.js.map +1 -1
  145. package/dist/preview.d.ts.map +1 -1
  146. package/dist/preview.js +2 -0
  147. package/dist/preview.js.map +1 -1
  148. package/dist/scaling/node.d.ts.map +1 -1
  149. package/dist/scaling/node.js +5 -1
  150. package/dist/scaling/node.js.map +1 -1
  151. package/dist/shaders/animation.d.ts +21 -0
  152. package/dist/shaders/animation.d.ts.map +1 -0
  153. package/dist/shaders/animation.js +25 -0
  154. package/dist/shaders/animation.js.map +1 -0
  155. package/dist/shaders/index.d.ts +2 -1
  156. package/dist/shaders/index.js +2 -1
  157. package/dist/slots/content.d.ts.map +1 -1
  158. package/dist/slots/content.js +4 -4
  159. package/dist/slots/content.js.map +1 -1
  160. package/dist/statuses/index.d.ts +2 -2
  161. package/dist/statuses/index.js +2 -2
  162. package/dist/statuses/workflow.d.ts +3 -1
  163. package/dist/statuses/workflow.d.ts.map +1 -1
  164. package/dist/statuses/workflow.js +6 -2
  165. package/dist/statuses/workflow.js.map +1 -1
  166. package/dist/token-sources/document.d.ts +26 -0
  167. package/dist/token-sources/document.d.ts.map +1 -0
  168. package/dist/token-sources/document.js +80 -0
  169. package/dist/token-sources/document.js.map +1 -0
  170. package/dist/token-sources/index.d.ts +3 -0
  171. package/dist/token-sources/index.js +3 -0
  172. package/dist/token-sources/schema.d.ts +43 -0
  173. package/dist/token-sources/schema.d.ts.map +1 -0
  174. package/dist/token-sources/schema.js +50 -0
  175. package/dist/token-sources/schema.js.map +1 -0
  176. package/dist/transfer/variables.d.ts.map +1 -1
  177. package/dist/transfer/variables.js +1 -1
  178. package/dist/transfer/variables.js.map +1 -1
  179. package/dist/types.d.ts +135 -15
  180. package/dist/types.d.ts.map +1 -1
  181. package/dist/types.js.map +1 -1
  182. package/dist/variables/bindings.d.ts +6 -5
  183. package/dist/variables/bindings.d.ts.map +1 -1
  184. package/dist/variables/bindings.js +24 -16
  185. package/dist/variables/bindings.js.map +1 -1
  186. package/dist/variables/formula/index.d.ts.map +1 -1
  187. package/dist/variables/formula/index.js +12 -6
  188. package/dist/variables/formula/index.js.map +1 -1
  189. package/dist/variables/index.d.ts +14 -1
  190. package/dist/variables/index.d.ts.map +1 -1
  191. package/dist/variables/index.js +38 -10
  192. package/dist/variables/index.js.map +1 -1
  193. package/dist/variables/paints.d.ts +9 -8
  194. package/dist/variables/paints.d.ts.map +1 -1
  195. package/dist/variables/paints.js +35 -26
  196. package/dist/variables/paints.js.map +1 -1
  197. package/dist/variables/resolver.d.ts +45 -0
  198. package/dist/variables/resolver.d.ts.map +1 -0
  199. package/dist/variables/resolver.js +106 -0
  200. package/dist/variables/resolver.js.map +1 -0
  201. package/dist/variations/copy.d.ts +16 -0
  202. package/dist/variations/copy.d.ts.map +1 -0
  203. package/dist/variations/copy.js +78 -0
  204. package/dist/variations/copy.js.map +1 -0
  205. package/dist/variations/index.d.ts +6 -0
  206. package/dist/variations/index.js +6 -0
  207. package/dist/variations/match.d.ts +23 -0
  208. package/dist/variations/match.d.ts.map +1 -0
  209. package/dist/variations/match.js +80 -0
  210. package/dist/variations/match.js.map +1 -0
  211. package/dist/variations/model.d.ts +39 -0
  212. package/dist/variations/model.d.ts.map +1 -0
  213. package/dist/variations/model.js +90 -0
  214. package/dist/variations/model.js.map +1 -0
  215. package/dist/variations/pick.d.ts +43 -0
  216. package/dist/variations/pick.d.ts.map +1 -0
  217. package/dist/variations/pick.js +211 -0
  218. package/dist/variations/pick.js.map +1 -0
  219. package/dist/variations/schema.d.ts +27 -0
  220. package/dist/variations/schema.d.ts.map +1 -0
  221. package/dist/variations/schema.js +27 -0
  222. package/dist/variations/schema.js.map +1 -0
  223. package/package.json +21 -1
  224. package/src/color/index.ts +43 -9
  225. package/src/color/management.ts +54 -1
  226. package/src/color/profile.ts +131 -0
  227. package/src/comments/merge.ts +3 -0
  228. package/src/comments/schema.ts +32 -2
  229. package/src/comments/threads.ts +5 -2
  230. package/src/components/properties.ts +13 -7
  231. package/src/components/variants.ts +39 -0
  232. package/src/constants.ts +75 -0
  233. package/src/copy.ts +20 -1
  234. package/src/css/index.ts +7 -1
  235. package/src/css/shadows.ts +8 -5
  236. package/src/css/text.ts +62 -0
  237. package/src/css/values.ts +12 -5
  238. package/src/design-memory/index.ts +2 -0
  239. package/src/design-memory/model.ts +143 -0
  240. package/src/design-memory/schema.ts +84 -0
  241. package/src/effects.ts +16 -7
  242. package/src/empty-values.ts +117 -0
  243. package/src/fields/components.ts +21 -0
  244. package/src/fields/index.ts +1 -0
  245. package/src/fields/layout.ts +10 -0
  246. package/src/graph.ts +50 -6
  247. package/src/grid-values.ts +47 -0
  248. package/src/hit-test/drop.ts +90 -0
  249. package/src/hit-test/geometry.ts +131 -0
  250. package/src/hit-test/masks.ts +57 -0
  251. package/src/hit-test/paint.ts +24 -0
  252. package/src/hit-test.ts +8 -277
  253. package/src/images.ts +27 -3
  254. package/src/instances/copies.ts +2 -1
  255. package/src/instances/fields.ts +2 -0
  256. package/src/instances/index.ts +20 -11
  257. package/src/instances/overrides.ts +49 -0
  258. package/src/instances/swap-overrides.ts +3 -2
  259. package/src/instances/sync.ts +11 -7
  260. package/src/node-defaults.ts +51 -41
  261. package/src/plugin-data/fields.ts +34 -32
  262. package/src/plugin-data/index.ts +1 -0
  263. package/src/plugin-data/schema.ts +30 -0
  264. package/src/preview.ts +2 -0
  265. package/src/scaling/node.ts +7 -2
  266. package/src/shaders/animation.ts +39 -0
  267. package/src/shaders/index.ts +1 -0
  268. package/src/slots/content.ts +8 -4
  269. package/src/statuses/workflow.ts +6 -4
  270. package/src/token-sources/document.ts +104 -0
  271. package/src/token-sources/index.ts +2 -0
  272. package/src/token-sources/schema.ts +59 -0
  273. package/src/transfer/variables.ts +2 -1
  274. package/src/types.ts +145 -14
  275. package/src/variables/bindings.ts +35 -18
  276. package/src/variables/formula/index.ts +19 -18
  277. package/src/variables/index.ts +49 -10
  278. package/src/variables/paints.ts +64 -29
  279. package/src/variables/resolver.ts +178 -0
  280. package/src/variations/copy.ts +89 -0
  281. package/src/variations/index.ts +5 -0
  282. package/src/variations/match.ts +100 -0
  283. package/src/variations/model.ts +132 -0
  284. package/src/variations/pick.ts +293 -0
  285. package/src/variations/schema.ts +29 -0
@@ -0,0 +1,84 @@
1
+ import * as v from 'valibot'
2
+
3
+ /** How many values, decisions and examples one document's memory keeps. */
4
+ export const DESIGN_MEMORY_LIMITS = {
5
+ colors: 128,
6
+ fonts: 16,
7
+ numbers: 32,
8
+ decisions: 40,
9
+ decisionLength: 400,
10
+ examples: 12,
11
+ fontLength: 100
12
+ } as const
13
+
14
+ /** The kinds of value a memory allows, each with its own list and enforcement. */
15
+ export const DESIGN_MEMORY_GROUPS = ['colors', 'fonts', 'fontSizes', 'spacing', 'radius'] as const
16
+ export type DesignMemoryGroup = (typeof DESIGN_MEMORY_GROUPS)[number]
17
+
18
+ /**
19
+ * What happens to an agent's edit that uses a value the memory does not allow: it is undone and
20
+ * the agent told the allowed values, it goes through with a warning, or nothing is checked.
21
+ * People's own edits are never refused; lint lists them.
22
+ */
23
+ export const DESIGN_MEMORY_ENFORCEMENTS = ['refuse', 'warn', 'off'] as const
24
+ export type DesignMemoryEnforcement = (typeof DESIGN_MEMORY_ENFORCEMENTS)[number]
25
+
26
+ export const DESIGN_DECISION_AUTHORS = ['person', 'agent'] as const
27
+ export type DesignDecisionAuthor = (typeof DESIGN_DECISION_AUTHORS)[number]
28
+
29
+ const enforcementSchema = v.picklist(DESIGN_MEMORY_ENFORCEMENTS)
30
+
31
+ /** A color as a memory keeps it: `#RRGGBB`, since alpha is never compared. */
32
+ const hexColorSchema = v.pipe(v.string(), v.regex(/^#[0-9A-F]{6}$/))
33
+ const fontFamilySchema = v.pipe(
34
+ v.string(),
35
+ v.trim(),
36
+ v.nonEmpty(),
37
+ v.maxLength(DESIGN_MEMORY_LIMITS.fontLength)
38
+ )
39
+ const fontSizeSchema = v.pipe(v.number(), v.finite(), v.gtValue(0))
40
+ const lengthSchema = v.pipe(v.number(), v.finite(), v.minValue(0))
41
+
42
+ /**
43
+ * One group: `fromSystem` allows what the document's variables and styles hold, and `values`
44
+ * adds values of the person's own.
45
+ */
46
+ function valueGroup<T>(value: v.GenericSchema<unknown, T>, max: number) {
47
+ return v.object({
48
+ fromSystem: v.boolean(),
49
+ values: v.pipe(v.array(value), v.maxLength(max)),
50
+ enforce: enforcementSchema
51
+ })
52
+ }
53
+
54
+ const decisionSchema = v.object({
55
+ id: v.pipe(v.string(), v.nonEmpty()),
56
+ text: v.pipe(
57
+ v.string(),
58
+ v.trim(),
59
+ v.nonEmpty(),
60
+ v.maxLength(DESIGN_MEMORY_LIMITS.decisionLength)
61
+ ),
62
+ author: v.picklist(DESIGN_DECISION_AUTHORS)
63
+ })
64
+
65
+ const exampleSchema = v.object({ nodeId: v.pipe(v.string(), v.nonEmpty()) })
66
+
67
+ /**
68
+ * A document's design rules for agents: the values its designs may use, decisions written down
69
+ * about them, and frames pinned as examples to follow.
70
+ */
71
+ export const designMemorySchema = v.object({
72
+ version: v.literal(1),
73
+ colors: valueGroup(hexColorSchema, DESIGN_MEMORY_LIMITS.colors),
74
+ fonts: valueGroup(fontFamilySchema, DESIGN_MEMORY_LIMITS.fonts),
75
+ fontSizes: valueGroup(fontSizeSchema, DESIGN_MEMORY_LIMITS.numbers),
76
+ spacing: valueGroup(lengthSchema, DESIGN_MEMORY_LIMITS.numbers),
77
+ radius: valueGroup(lengthSchema, DESIGN_MEMORY_LIMITS.numbers),
78
+ decisions: v.pipe(v.array(decisionSchema), v.maxLength(DESIGN_MEMORY_LIMITS.decisions)),
79
+ examples: v.pipe(v.array(exampleSchema), v.maxLength(DESIGN_MEMORY_LIMITS.examples))
80
+ })
81
+
82
+ export type DesignMemory = v.InferOutput<typeof designMemorySchema>
83
+ export type DesignDecision = DesignMemory['decisions'][number]
84
+ export type DesignExample = DesignMemory['examples'][number]
package/src/effects.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Vector } from './primitives'
2
- import type { Effect, ShaderEffect, ShadowEffect } from './types'
2
+ import type { Effect, ShadowEffect } from './types'
3
3
 
4
4
  const SHADOW_OFFSET: Vector = { x: 0, y: 4 }
5
5
  const BLUR_OFFSET: Vector = { x: 0, y: 0 }
@@ -9,6 +9,9 @@ type ShadowInput = Omit<ShadowEffect, 'offset'> & {
9
9
  offset?: Partial<Vector> | null
10
10
  }
11
11
 
12
+ /** An effect as a caller may pass it, before its geometry is made finite. */
13
+ type EffectInput = Effect | ShadowInput
14
+
12
15
  function fallbackOffset(type: ShadowEffect['type']): Vector {
13
16
  return type === 'DROP_SHADOW' || type === 'INNER_SHADOW' ? SHADOW_OFFSET : BLUR_OFFSET
14
17
  }
@@ -41,6 +44,11 @@ function isStoredShadow(effect: ShadowInput): effect is ShadowEffect {
41
44
  )
42
45
  }
43
46
 
47
+ /** An effect that carries geometry of its own, which has to be finite to render. */
48
+ function isOffsetEffect(effect: EffectInput): effect is ShadowInput {
49
+ return effect.type !== 'SHADER' && effect.type !== 'NOISE' && effect.type !== 'TEXTURE'
50
+ }
51
+
44
52
  function withFiniteOffset(effect: ShadowInput): Effect {
45
53
  const offset = finiteOffset(effect)
46
54
  if (!offset) {
@@ -50,19 +58,20 @@ function withFiniteOffset(effect: ShadowInput): Effect {
50
58
  return { ...effect, offset }
51
59
  }
52
60
 
53
- function isStoredEffect(effect: ShaderEffect | ShadowInput): effect is Effect {
54
- return effect.type === 'SHADER' || isStoredShadow(effect)
61
+ function isStoredEffect(effect: EffectInput): effect is Effect {
62
+ return !isOffsetEffect(effect) || isStoredShadow(effect)
55
63
  }
56
64
 
57
65
  /**
58
66
  * Shadows and blurs keep a finite offset. A drop or inner shadow missing one uses `{x: 0, y: 4}`,
59
- * the same default as `dropShadow()` and `set_effects`; a blur uses `{x: 0, y: 0}`. Shader effects
60
- * are left alone. An array that is already valid is returned as itself.
67
+ * the same default as `dropShadow()` and `set_effects`; a blur uses `{x: 0, y: 0}`. Shader, noise,
68
+ * and texture effects carry no offset and are left alone. An array that is already valid is
69
+ * returned as itself.
61
70
  */
62
- export function normalizeEffects(effects: (ShaderEffect | ShadowInput)[]): Effect[] {
71
+ export function normalizeEffects(effects: EffectInput[]): Effect[] {
63
72
  let next: Effect[] | null = null
64
73
  for (const [index, effect] of effects.entries()) {
65
- const normalized = effect.type === 'SHADER' ? effect : withFiniteOffset(effect)
74
+ const normalized = isOffsetEffect(effect) ? withFiniteOffset(effect) : effect
66
75
  if (normalized === effect) {
67
76
  if (next && isStoredEffect(effect)) next.push(effect)
68
77
  continue
@@ -0,0 +1,117 @@
1
+ import { isEmptyObject, isPlainObject } from 'es-toolkit'
2
+
3
+ import {
4
+ EMPTY_INSTANCE_OVERRIDES,
5
+ isEmptyInstanceOverrideState,
6
+ type InstanceOverrideState
7
+ } from './instances/overrides'
8
+ import type { SceneNode, SourceMetadata } from './types'
9
+
10
+ /**
11
+ * The empty list and record every layer holds until a field gets a value of its own. All layers
12
+ * share them, so they are frozen: a field is replaced, never written into, and a write into one
13
+ * throws instead of reaching every layer of every document.
14
+ */
15
+ export const EMPTY_LIST: never[] = Object.freeze<never[]>([]) as never[]
16
+ export const EMPTY_RECORD: Record<string, never> = Object.freeze({})
17
+
18
+ /** The child list is the graph's to grow in place, so every layer keeps its own. */
19
+ const OWN_LIST_FIELDS: ReadonlySet<string> = new Set<keyof SceneNode>(['childIds'])
20
+
21
+ /** Fields keyed by name, such as bindings and property values, that start empty. */
22
+ const RECORD_FIELDS: ReadonlySet<string> = new Set<keyof SceneNode>([
23
+ 'componentPropertyAssignments',
24
+ 'componentPropertyValues',
25
+ 'boundVariables',
26
+ 'variableBindingScales',
27
+ 'variableAssignmentScales',
28
+ 'variableModes'
29
+ ])
30
+
31
+ const SOURCE_LIST_FIELDS = ['editedFields'] as const
32
+ const FIG_SOURCE_LIST_FIELDS = [
33
+ 'symbolOverrides',
34
+ 'componentPropAssignments',
35
+ 'derivedSymbolData'
36
+ ] as const
37
+
38
+ function sharedEmptyList(value: unknown): unknown {
39
+ return Array.isArray(value) && value.length === 0 ? EMPTY_LIST : value
40
+ }
41
+
42
+ function sharedEmptyRecord(value: unknown): unknown {
43
+ return isPlainObject(value) && isEmptyObject(value) ? EMPTY_RECORD : value
44
+ }
45
+
46
+ function sharedEmptyValue(field: string, value: unknown): unknown {
47
+ if (field === 'instanceOverrides') return sharedEmptyOverrideState(value)
48
+ if (Array.isArray(value)) return OWN_LIST_FIELDS.has(field) ? value : sharedEmptyList(value)
49
+ return RECORD_FIELDS.has(field) ? sharedEmptyRecord(value) : value
50
+ }
51
+
52
+ /** An override state with nothing in it, so a layer that cleared its overrides shares the empty one. */
53
+ function sharedEmptyOverrideState(value: unknown): unknown {
54
+ if (!isPlainObject(value)) return value
55
+ const { self, descendants } = value as Partial<InstanceOverrideState>
56
+ if (!(self instanceof Map) || !(descendants instanceof Map)) return value
57
+ return isEmptyInstanceOverrideState(value as InstanceOverrideState)
58
+ ? EMPTY_INSTANCE_OVERRIDES
59
+ : value
60
+ }
61
+
62
+ /**
63
+ * `record` with the empty lists among `lists` and the empty record at `recordField` swapped for
64
+ * the shared ones: a copy when anything changes, so the caller's object is never written into.
65
+ * A field the record lacks stays absent.
66
+ */
67
+ function withSharedEmpties<T extends object>(
68
+ record: T,
69
+ lists: readonly string[],
70
+ recordField?: string
71
+ ): T {
72
+ const fields: [string, (value: unknown) => unknown][] = lists.map((field) => [
73
+ field,
74
+ sharedEmptyList
75
+ ])
76
+ if (recordField !== undefined) fields.push([recordField, sharedEmptyRecord])
77
+ let copy: T | null = null
78
+ for (const [field, shared] of fields) {
79
+ if (!Object.hasOwn(record, field)) continue
80
+ const value: unknown = Reflect.get(record, field)
81
+ const next = shared(value)
82
+ if (next === value) continue
83
+ copy ??= { ...record }
84
+ Reflect.set(copy, field, next)
85
+ }
86
+ return copy ?? record
87
+ }
88
+
89
+ /**
90
+ * Source metadata with its empty lists and raw fields swapped for the shared ones. The metadata
91
+ * object and its `fig` part stay the layer's own, since readers and edits write their scalars in
92
+ * place; metadata missing parts, as libraries and plugins may give, is kept as given.
93
+ */
94
+ function withSharedEmptySource(source: SourceMetadata): SourceMetadata {
95
+ const outer = withSharedEmpties(source, SOURCE_LIST_FIELDS)
96
+ const fig: unknown = Reflect.get(source, 'fig')
97
+ if (!isPlainObject(fig)) return outer
98
+ const sharedFig = withSharedEmpties(fig, FIG_SOURCE_LIST_FIELDS, 'rawNodeFields')
99
+ if (sharedFig === fig) return outer
100
+ return { ...outer, fig: { ...source.fig, ...sharedFig } }
101
+ }
102
+
103
+ /**
104
+ * Swap the `fields` of `node` that hold an empty list or record, its source metadata's included,
105
+ * for the shared ones. Writing known keys keeps the shape every layer shares.
106
+ */
107
+ export function shareEmptyNodeValues(node: SceneNode, fields: Iterable<string>): void {
108
+ for (const field of fields) {
109
+ const value: unknown = Reflect.get(node, field)
110
+ if (field === 'source') {
111
+ if (isPlainObject(value)) node.source = withSharedEmptySource(node.source)
112
+ continue
113
+ }
114
+ const shared = sharedEmptyValue(field, value)
115
+ if (shared !== value) Reflect.set(node, field, shared)
116
+ }
117
+ }
@@ -0,0 +1,21 @@
1
+ import type { SceneNode } from '../types'
2
+
3
+ /**
4
+ * What makes a layer a main component beyond its type: its key, its properties and variant
5
+ * specs, and how it is published to libraries.
6
+ */
7
+ export const MAIN_COMPONENT_FIELDS = [
8
+ 'componentKey',
9
+ 'componentPropertyDefinitions',
10
+ 'variantPropSpecs',
11
+ 'sourceLibraryKey',
12
+ 'publishId',
13
+ 'overrideKey',
14
+ 'sharedSymbolVersion',
15
+ 'publishedVersion',
16
+ 'librarySource',
17
+ 'isPublishable',
18
+ 'isSymbolPublishable',
19
+ 'symbolDescription',
20
+ 'symbolLinks'
21
+ ] as const satisfies readonly (keyof SceneNode)[]
@@ -1,4 +1,5 @@
1
1
  export * from './appearance'
2
+ export * from './components'
2
3
  export * from './geometry'
3
4
  export * from './layout'
4
5
  export * from './text'
@@ -24,6 +24,16 @@ export const AUTO_LAYOUT_FIELDS = [
24
24
  'gridRowGap'
25
25
  ] as const satisfies readonly (keyof SceneNode)[]
26
26
 
27
+ /** How a layer sits in its parent: its constraints and its place in an auto layout or grid. */
28
+ export const PARENT_LAYOUT_FIELDS = [
29
+ 'horizontalConstraint',
30
+ 'verticalConstraint',
31
+ 'layoutPositioning',
32
+ 'layoutGrow',
33
+ 'layoutAlignSelf',
34
+ 'gridPosition'
35
+ ] as const satisfies readonly (keyof SceneNode)[]
36
+
27
37
  /** Size limits a layer keeps however it is resized. */
28
38
  export const SIZE_CONSTRAINT_FIELDS = [
29
39
  'minWidth',
package/src/graph.ts CHANGED
@@ -14,16 +14,21 @@ import {
14
14
  } from './coordinate'
15
15
  import { cloneNodeProps, type NodeCloneMode } from './copy'
16
16
  import { normalizeEffects } from './effects'
17
+ import { shareEmptyNodeValues } from './empty-values'
17
18
  import { bindNodeEvents } from './events'
18
19
  import { TRANSFORM_FIELDS } from './fields/geometry'
19
20
  import { LAYOUT_AFFECTING_KEYS } from './fields/layout'
20
21
  import { withGeneratedFrameSize } from './generated'
22
+ import { gridLayoutChanges } from './grid-values'
21
23
  import * as HitTest from './hit-test'
22
24
  import { generateId } from './ids'
23
25
  import * as Instances from './instances'
26
+ import { ownInstanceOverrides } from './instances/overrides'
24
27
  import { LayoutWrites } from './layout/writes'
25
28
  import Matrix from './matrix'
26
29
  import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
30
+ import { withPluginData } from './plugin-data/field'
31
+ import { PAIRDRAW_PLUGIN_DATA } from './plugin-data/fields'
27
32
  import { updateNodePreview, type NodePreviewObserver } from './preview'
28
33
  import type { Color, Rect, Vector } from './primitives'
29
34
  /* eslint-disable max-lines -- SceneGraph is the facade over the domain modules */
@@ -345,6 +350,16 @@ export class SceneGraph {
345
350
  return Variables.getVariablesByType(this, type)
346
351
  }
347
352
 
353
+ /** Every variable but the deleted ones, which only their bindings still reach. */
354
+ getLiveVariables(): Variable[] {
355
+ return Variables.liveVariables(this)
356
+ }
357
+
358
+ /** Every collection but the deleted ones. */
359
+ getLiveCollections(): VariableCollection[] {
360
+ return Variables.liveCollections(this)
361
+ }
362
+
348
363
  bindVariable(nodeId: string, field: string, variableId: string): void {
349
364
  Variables.bindVariable(this, nodeId, field, variableId)
350
365
  }
@@ -495,7 +510,7 @@ export class SceneGraph {
495
510
  const node = createDefaultNode(
496
511
  () => this.generateEntityId(),
497
512
  type,
498
- pageLayoutOverrides(type, overrides)
513
+ pageLayoutOverrides(type, gridLayoutChanges(overrides))
499
514
  )
500
515
  Object.assign(node, strokeLineChanges(node, overrides))
501
516
  this.willChangeNode(parent)
@@ -514,7 +529,11 @@ export class SceneGraph {
514
529
  if (parentId === id) throw new Error(`Node "${id}" cannot be its own parent`)
515
530
  const parent = parentId === null ? undefined : this.nodes.get(parentId)
516
531
  if (parentId !== null && !parent) throw new Error(`Parent "${parentId}" does not exist`)
517
- const node = createDefaultNode(() => id, type, pageLayoutOverrides(type, overrides))
532
+ const node = createDefaultNode(
533
+ () => id,
534
+ type,
535
+ pageLayoutOverrides(type, gridLayoutChanges(overrides))
536
+ )
518
537
  // `overrides` may be another node's snapshot, ID included.
519
538
  node.id = id
520
539
  if (parent && !parent.childIds.includes(id)) {
@@ -668,7 +687,7 @@ export class SceneGraph {
668
687
 
669
688
  const node = this.nodes.get(id)
670
689
  if (!node) return
671
- const given = finiteGeometry(omitBy(changes, isUndefined))
690
+ const given = gridLayoutChanges(finiteGeometry(omitBy(changes, isUndefined)))
672
691
  assertPageLayout(node, given)
673
692
  this.applyNodeChanges(node, this.impliedChanges(node, given))
674
693
  }
@@ -727,6 +746,7 @@ export class SceneGraph {
727
746
  this.scopeDepths.sourceMetadata === 0 && !this.isApplyingDerivedLayout
728
747
  )
729
748
  Object.assign(node, changes)
749
+ shareEmptyNodeValues(node, Object.keys(changes))
730
750
  if (changes.fills) removeStaleBindings(node, 'fills', changes)
731
751
  if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
732
752
  // A bound paint written keeps what it was given until its variable's color changes.
@@ -870,15 +890,31 @@ export class SceneGraph {
870
890
  }
871
891
  }
872
892
 
873
- for (const childId of Array.from(node.childIds)) {
874
- this.deleteNode(childId)
893
+ this.deleteSubtree(node)
894
+ }
895
+
896
+ /**
897
+ * Deletes `node` and its layers, children first. Their parent goes with them, so its child
898
+ * list is emptied once instead of being filtered for each child, which kept deleting a frame
899
+ * of thousands of layers quadratic. The list is emptied before the children go, so a listener
900
+ * of a child's deletion never finds it still listed.
901
+ */
902
+ private deleteSubtree(node: SceneNode): void {
903
+ const { id, childIds } = node
904
+ node.childIds = []
905
+ for (const childId of childIds) {
906
+ const child = this.nodes.get(childId)
907
+ if (!child) continue
908
+ this.willChangeNode(child)
909
+ this.deleteSubtree(child)
875
910
  }
876
911
 
877
912
  if (node.type === 'INSTANCE' && node.componentId) this.unindexInstance(node.componentId, id)
878
913
  // An override names a layer of its instance: one left naming a deleted layer would be written
879
914
  // as a claim for nothing, and reopening would restore a layer the document no longer has.
880
915
  for (let owner = node.parentId ? this.nodes.get(node.parentId) : undefined; owner;) {
881
- if (owner.type === 'INSTANCE') owner.instanceOverrides.descendants.delete(id)
916
+ if (owner.type === 'INSTANCE' && owner.instanceOverrides.descendants.has(id))
917
+ ownInstanceOverrides(owner).descendants.delete(id)
882
918
  owner = owner.parentId ? this.nodes.get(owner.parentId) : undefined
883
919
  }
884
920
  this.connectorIds.delete(id)
@@ -959,6 +995,14 @@ export class SceneGraph {
959
995
  if (props.source) props.source = { ...props.source, id: null, orderKey: null }
960
996
  // A copy is a new design nobody has reviewed: Figma's duplicate and clone() drop the status.
961
997
  props.status = null
998
+ // A stand-in mark names one layer's place in a version of a design; a copy is not that.
999
+ if (props.pluginData) {
1000
+ props.pluginData = withPluginData(
1001
+ props.pluginData,
1002
+ PAIRDRAW_PLUGIN_DATA.variationStandIn,
1003
+ undefined
1004
+ )
1005
+ }
962
1006
  const clone = this.createNode(src.type, parentId, { ...props, ...overrides })
963
1007
  Variables.copyAppliedBoundPaints(src, clone)
964
1008
  copies.set(src.id, clone.id)
@@ -0,0 +1,47 @@
1
+ import type { GridTrack, SceneNode } from './types'
2
+
3
+ /**
4
+ * A gap or track a canvas can lay out. Figma holds gaps at zero or more and every track at a
5
+ * finite, usable size: a fixed or auto track that is negative or not a number becomes auto, and a
6
+ * fractional track needs a positive weight.
7
+ */
8
+ function safeTrack(track: GridTrack): GridTrack {
9
+ const value = track.value
10
+ const finite = Number.isFinite(value)
11
+ switch (track.sizing) {
12
+ case 'FR':
13
+ return finite && value > 0 ? track : { sizing: 'FR', value: 1 }
14
+ case 'FIXED':
15
+ return finite && value >= 0 ? track : { sizing: 'AUTO', value: 0 }
16
+ case 'AUTO':
17
+ return finite && value === 0 ? track : { sizing: 'AUTO', value: 0 }
18
+ default:
19
+ return { sizing: 'AUTO', value: 0 }
20
+ }
21
+ }
22
+
23
+ function safeGap(gap: number): number {
24
+ return Number.isFinite(gap) && gap >= 0 ? gap : 0
25
+ }
26
+
27
+ /**
28
+ * `changes` with grid gaps and tracks held to values the canvas can lay out. Only fields that
29
+ * needed holding move, so a write that already fits changes nothing.
30
+ */
31
+ export function gridLayoutChanges(changes: Partial<SceneNode>): Partial<SceneNode> {
32
+ const fixed: Partial<SceneNode> = {}
33
+ const { gridRowGap, gridColumnGap, gridTemplateRows, gridTemplateColumns } = changes
34
+ if (gridRowGap !== undefined && safeGap(gridRowGap) !== gridRowGap) fixed.gridRowGap = 0
35
+ if (gridColumnGap !== undefined && safeGap(gridColumnGap) !== gridColumnGap) {
36
+ fixed.gridColumnGap = 0
37
+ }
38
+ const rows = gridTemplateRows?.map(safeTrack)
39
+ if (rows?.some((track, index) => track !== gridTemplateRows?.[index])) {
40
+ fixed.gridTemplateRows = rows
41
+ }
42
+ const columns = gridTemplateColumns?.map(safeTrack)
43
+ if (columns?.some((track, index) => track !== gridTemplateColumns?.[index])) {
44
+ fixed.gridTemplateColumns = columns
45
+ }
46
+ return Object.keys(fixed).length === 0 ? changes : { ...changes, ...fixed }
47
+ }
@@ -0,0 +1,90 @@
1
+ // Where dragged layers land: the frame, section, component, or instance under the point.
2
+ import type { SceneGraph } from '../graph'
3
+ import type { NodeType, SceneNode } from '../types'
4
+ import { containsPoint } from './geometry'
5
+
6
+ /** Which layers take a drop, and which are only looked through, as in Figma. */
7
+ interface DropRules {
8
+ takes: (node: SceneNode) => boolean
9
+ passThrough: ReadonlySet<NodeType>
10
+ }
11
+
12
+ const LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])
13
+ const COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])
14
+
15
+ function dropRules(options: DropTargetOptions): DropRules {
16
+ const { componentSetIds } = options
17
+ if (!componentSetIds) {
18
+ return {
19
+ takes: (node) => LAYER_TARGETS.has(node.type),
20
+ passThrough: new Set(['GROUP', 'COMPONENT_SET'])
21
+ }
22
+ }
23
+ // Components never go into other components, and a set only takes back its own variants.
24
+ return {
25
+ takes: (node) =>
26
+ COMPONENT_TARGETS.has(node.type) ||
27
+ (node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),
28
+ passThrough: new Set(['GROUP'])
29
+ }
30
+ }
31
+
32
+ function dropTargetIn(
33
+ graph: SceneGraph,
34
+ px: number,
35
+ py: number,
36
+ parent: SceneNode,
37
+ excludeIds: ReadonlySet<string>,
38
+ rules: DropRules,
39
+ transformCache: Map<string, boolean>
40
+ ): SceneNode | null {
41
+ // A clipped-away part of a child is not under the cursor.
42
+ if (
43
+ parent.type !== 'CANVAS' &&
44
+ parent.clipsContent &&
45
+ !containsPoint(px, py, parent, graph, transformCache)
46
+ )
47
+ return null
48
+
49
+ for (let i = parent.childIds.length - 1; i >= 0; i--) {
50
+ const childId = parent.childIds[i]
51
+ if (excludeIds.has(childId)) continue
52
+ const child = graph.nodes.get(childId)
53
+ if (!child || child.internalOnly || !child.visible || child.locked) continue
54
+ const target = rules.takes(child)
55
+ if (!target && !rules.passThrough.has(child.type)) continue
56
+
57
+ const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)
58
+ if (deeper) return deeper
59
+ if (target && containsPoint(px, py, child, graph, transformCache)) return child
60
+ }
61
+
62
+ return null
63
+ }
64
+
65
+ export interface DropTargetOptions {
66
+ /**
67
+ * Set when the dropped layers are all main components: the component sets they come from.
68
+ * Components then go only into frames, sections, instances, and those sets.
69
+ */
70
+ componentSetIds?: ReadonlySet<string>
71
+ }
72
+
73
+ /**
74
+ * The topmost unlocked frame, section, component, or instance under the point, in its rotated
75
+ * shape and inside its clipping ancestors. Groups and component sets are looked through but never
76
+ * returned, except that a set takes dropped components; locked layers and their contents are
77
+ * skipped.
78
+ */
79
+ export function hitTestDropTarget(
80
+ graph: SceneGraph,
81
+ px: number,
82
+ py: number,
83
+ excludeIds: ReadonlySet<string>,
84
+ scopeId?: string,
85
+ options: DropTargetOptions = {}
86
+ ): SceneNode | null {
87
+ const scope = graph.nodes.get(scopeId ?? graph.rootId)
88
+ if (!scope) return null
89
+ return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())
90
+ }