@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.5

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 (283) hide show
  1. package/dist/arrays.d.ts +17 -0
  2. package/dist/arrays.d.ts.map +1 -0
  3. package/dist/arrays.js +38 -0
  4. package/dist/arrays.js.map +1 -0
  5. package/dist/buffered-events.js +1 -1
  6. package/dist/buffered-events.js.map +1 -1
  7. package/dist/clipping.d.ts +7 -1
  8. package/dist/clipping.d.ts.map +1 -1
  9. package/dist/clipping.js +23 -19
  10. package/dist/clipping.js.map +1 -1
  11. package/dist/components/index.d.ts +2 -2
  12. package/dist/components/index.js +2 -2
  13. package/dist/components/properties.d.ts +1 -3
  14. package/dist/components/properties.d.ts.map +1 -1
  15. package/dist/components/properties.js +2 -9
  16. package/dist/components/properties.js.map +1 -1
  17. package/dist/corners.d.ts +22 -0
  18. package/dist/corners.d.ts.map +1 -0
  19. package/dist/corners.js +52 -0
  20. package/dist/corners.js.map +1 -0
  21. package/dist/css/grid.d.ts +4 -1
  22. package/dist/css/grid.d.ts.map +1 -1
  23. package/dist/css/grid.js +16 -7
  24. package/dist/css/grid.js.map +1 -1
  25. package/dist/css/index.d.ts +2 -2
  26. package/dist/css/index.js +2 -2
  27. package/dist/css/values.d.ts +6 -1
  28. package/dist/css/values.d.ts.map +1 -1
  29. package/dist/font-style.d.ts +2 -1
  30. package/dist/font-style.d.ts.map +1 -1
  31. package/dist/font-style.js +31 -6
  32. package/dist/font-style.js.map +1 -1
  33. package/dist/geometry.d.ts +68 -3
  34. package/dist/geometry.d.ts.map +1 -1
  35. package/dist/geometry.js +155 -5
  36. package/dist/geometry.js.map +1 -1
  37. package/dist/gradient.d.ts +9 -2
  38. package/dist/gradient.d.ts.map +1 -1
  39. package/dist/gradient.js +29 -1
  40. package/dist/gradient.js.map +1 -1
  41. package/dist/graph.d.ts +4 -0
  42. package/dist/graph.d.ts.map +1 -1
  43. package/dist/graph.js +30 -4
  44. package/dist/graph.js.map +1 -1
  45. package/dist/group-bounds.d.ts.map +1 -1
  46. package/dist/group-bounds.js +4 -3
  47. package/dist/group-bounds.js.map +1 -1
  48. package/dist/guides.d.ts +11 -2
  49. package/dist/guides.d.ts.map +1 -1
  50. package/dist/guides.js +30 -1
  51. package/dist/guides.js.map +1 -1
  52. package/dist/hit-test.d.ts.map +1 -1
  53. package/dist/hit-test.js +57 -5
  54. package/dist/hit-test.js.map +1 -1
  55. package/dist/icons/fit.d.ts +17 -0
  56. package/dist/icons/fit.d.ts.map +1 -0
  57. package/dist/icons/fit.js +21 -0
  58. package/dist/icons/fit.js.map +1 -0
  59. package/dist/icons/glyph.d.ts.map +1 -1
  60. package/dist/icons/glyph.js +5 -4
  61. package/dist/icons/glyph.js.map +1 -1
  62. package/dist/icons/index.d.ts +2 -1
  63. package/dist/icons/index.js +2 -1
  64. package/dist/images.d.ts +14 -4
  65. package/dist/images.d.ts.map +1 -1
  66. package/dist/images.js +45 -5
  67. package/dist/images.js.map +1 -1
  68. package/dist/instances/assignments.js +39 -6
  69. package/dist/instances/assignments.js.map +1 -1
  70. package/dist/instances/index.d.ts.map +1 -1
  71. package/dist/instances/index.js +39 -2
  72. package/dist/instances/index.js.map +1 -1
  73. package/dist/instances/kept-overrides.js +2 -2
  74. package/dist/instances/kept-overrides.js.map +1 -1
  75. package/dist/instances/matching.js +68 -0
  76. package/dist/instances/matching.js.map +1 -0
  77. package/dist/instances/overrides.d.ts +4 -1
  78. package/dist/instances/overrides.d.ts.map +1 -1
  79. package/dist/instances/overrides.js +13 -1
  80. package/dist/instances/overrides.js.map +1 -1
  81. package/dist/instances/protection.js +67 -0
  82. package/dist/instances/protection.js.map +1 -0
  83. package/dist/instances/swap.js +7 -5
  84. package/dist/instances/swap.js.map +1 -1
  85. package/dist/instances/sync.js +10 -116
  86. package/dist/instances/sync.js.map +1 -1
  87. package/dist/layout/guides.d.ts +17 -1
  88. package/dist/layout/guides.d.ts.map +1 -1
  89. package/dist/layout/guides.js +31 -11
  90. package/dist/layout/guides.js.map +1 -1
  91. package/dist/layout/index.d.ts +4 -2
  92. package/dist/layout/index.js +4 -2
  93. package/dist/layout/limits.d.ts +40 -0
  94. package/dist/layout/limits.d.ts.map +1 -0
  95. package/dist/layout/limits.js +54 -0
  96. package/dist/layout/limits.js.map +1 -0
  97. package/dist/layout/sizing.d.ts.map +1 -1
  98. package/dist/layout/sizing.js +0 -1
  99. package/dist/layout/sizing.js.map +1 -1
  100. package/dist/layout/spacing.d.ts +10 -0
  101. package/dist/layout/spacing.d.ts.map +1 -0
  102. package/dist/layout/spacing.js +12 -0
  103. package/dist/layout/spacing.js.map +1 -0
  104. package/dist/masks.d.ts +28 -0
  105. package/dist/masks.d.ts.map +1 -0
  106. package/dist/masks.js +53 -0
  107. package/dist/masks.js.map +1 -0
  108. package/dist/matrix.d.ts +2 -1
  109. package/dist/matrix.d.ts.map +1 -1
  110. package/dist/matrix.js +14 -1
  111. package/dist/matrix.js.map +1 -1
  112. package/dist/node-defaults.d.ts +9 -1
  113. package/dist/node-defaults.d.ts.map +1 -1
  114. package/dist/node-defaults.js +16 -1
  115. package/dist/node-defaults.js.map +1 -1
  116. package/dist/outline/flatten.d.ts.map +1 -1
  117. package/dist/outline/flatten.js +24 -17
  118. package/dist/outline/flatten.js.map +1 -1
  119. package/dist/outline/hit.d.ts +2 -8
  120. package/dist/outline/hit.d.ts.map +1 -1
  121. package/dist/outline/hit.js +16 -25
  122. package/dist/outline/hit.js.map +1 -1
  123. package/dist/outline/index.d.ts +3 -3
  124. package/dist/outline/index.js +3 -3
  125. package/dist/outline/shapes.d.ts +3 -1
  126. package/dist/outline/shapes.d.ts.map +1 -1
  127. package/dist/outline/shapes.js +18 -39
  128. package/dist/outline/shapes.js.map +1 -1
  129. package/dist/paints/index.d.ts +3 -1
  130. package/dist/paints/index.js +3 -1
  131. package/dist/paints/pattern.d.ts +13 -0
  132. package/dist/paints/pattern.d.ts.map +1 -0
  133. package/dist/paints/pattern.js +47 -0
  134. package/dist/paints/pattern.js.map +1 -0
  135. package/dist/paints/shows.d.ts +11 -0
  136. package/dist/paints/shows.d.ts.map +1 -0
  137. package/dist/paints/shows.js +16 -0
  138. package/dist/paints/shows.js.map +1 -0
  139. package/dist/paths/arc.d.ts +32 -0
  140. package/dist/paths/arc.d.ts.map +1 -0
  141. package/dist/paths/arc.js +49 -0
  142. package/dist/paths/arc.js.map +1 -0
  143. package/dist/paths/index.d.ts +5 -0
  144. package/dist/paths/index.js +5 -0
  145. package/dist/paths/network.d.ts +43 -0
  146. package/dist/paths/network.d.ts.map +1 -0
  147. package/dist/paths/network.js +249 -0
  148. package/dist/paths/network.js.map +1 -0
  149. package/dist/paths/sink.d.ts +20 -0
  150. package/dist/paths/sink.d.ts.map +1 -0
  151. package/dist/paths/sink.js +32 -0
  152. package/dist/paths/sink.js.map +1 -0
  153. package/dist/paths/smooth-corners.d.ts +25 -0
  154. package/dist/paths/smooth-corners.d.ts.map +1 -0
  155. package/dist/paths/smooth-corners.js +196 -0
  156. package/dist/paths/smooth-corners.js.map +1 -0
  157. package/dist/plugin-data/fields.d.ts +4 -0
  158. package/dist/plugin-data/fields.d.ts.map +1 -1
  159. package/dist/plugin-data/fields.js +2 -0
  160. package/dist/plugin-data/fields.js.map +1 -1
  161. package/dist/polygon.d.ts +1 -3
  162. package/dist/polygon.d.ts.map +1 -1
  163. package/dist/polygon.js +1 -16
  164. package/dist/polygon.js.map +1 -1
  165. package/dist/resize.d.ts.map +1 -1
  166. package/dist/resize.js +5 -5
  167. package/dist/resize.js.map +1 -1
  168. package/dist/scaling/node.d.ts.map +1 -1
  169. package/dist/scaling/node.js +25 -3
  170. package/dist/scaling/node.js.map +1 -1
  171. package/dist/text/auto-resize.d.ts +15 -0
  172. package/dist/text/auto-resize.d.ts.map +1 -0
  173. package/dist/text/auto-resize.js +23 -0
  174. package/dist/text/auto-resize.js.map +1 -0
  175. package/dist/text/index.d.ts +4 -3
  176. package/dist/text/index.js +4 -3
  177. package/dist/text/lengths.d.ts +6 -1
  178. package/dist/text/lengths.d.ts.map +1 -1
  179. package/dist/text/lengths.js +10 -1
  180. package/dist/text/lengths.js.map +1 -1
  181. package/dist/text/overflow.d.ts +14 -5
  182. package/dist/text/overflow.d.ts.map +1 -1
  183. package/dist/text/overflow.js +24 -6
  184. package/dist/text/overflow.js.map +1 -1
  185. package/dist/text/path.js +10 -0
  186. package/dist/text/path.js.map +1 -0
  187. package/dist/text/picture.js +5 -5
  188. package/dist/text/picture.js.map +1 -1
  189. package/dist/transfer/dependencies.d.ts.map +1 -1
  190. package/dist/transfer/dependencies.js +3 -2
  191. package/dist/transfer/dependencies.js.map +1 -1
  192. package/dist/transfer/nodes.js +2 -1
  193. package/dist/transfer/nodes.js.map +1 -1
  194. package/dist/types.d.ts +21 -8
  195. package/dist/types.d.ts.map +1 -1
  196. package/dist/types.js.map +1 -1
  197. package/dist/variables/index.d.ts +4 -3
  198. package/dist/variables/index.d.ts.map +1 -1
  199. package/dist/variables/index.js +4 -3
  200. package/dist/variables/index.js.map +1 -1
  201. package/dist/variables/lookup.d.ts +20 -0
  202. package/dist/variables/lookup.d.ts.map +1 -0
  203. package/dist/variables/lookup.js +34 -0
  204. package/dist/variables/lookup.js.map +1 -0
  205. package/dist/variables/paints.d.ts +35 -4
  206. package/dist/variables/paints.d.ts.map +1 -1
  207. package/dist/variables/paints.js +67 -16
  208. package/dist/variables/paints.js.map +1 -1
  209. package/dist/variables/resolution.d.ts +3 -1
  210. package/dist/variables/resolution.d.ts.map +1 -1
  211. package/dist/variables/resolution.js +8 -3
  212. package/dist/variables/resolution.js.map +1 -1
  213. package/dist/variables/subtree.js +1 -1
  214. package/dist/variables/subtree.js.map +1 -1
  215. package/dist/vector-network.d.ts.map +1 -1
  216. package/dist/vector-network.js +12 -76
  217. package/dist/vector-network.js.map +1 -1
  218. package/package.json +25 -1
  219. package/src/arrays.ts +37 -0
  220. package/src/buffered-events.ts +2 -1
  221. package/src/clipping.ts +11 -8
  222. package/src/components/properties.ts +1 -13
  223. package/src/corners.ts +93 -0
  224. package/src/css/grid.ts +21 -7
  225. package/src/css/index.ts +1 -1
  226. package/src/font-style.ts +38 -4
  227. package/src/geometry.ts +219 -6
  228. package/src/gradient.ts +23 -1
  229. package/src/graph.ts +22 -4
  230. package/src/group-bounds.ts +7 -3
  231. package/src/guides.ts +26 -1
  232. package/src/hit-test.ts +68 -10
  233. package/src/icons/fit.ts +24 -0
  234. package/src/icons/glyph.ts +5 -4
  235. package/src/icons/index.ts +1 -0
  236. package/src/images.ts +67 -7
  237. package/src/instances/assignments.ts +65 -7
  238. package/src/instances/index.ts +53 -4
  239. package/src/instances/kept-overrides.ts +2 -5
  240. package/src/instances/matching.ts +101 -0
  241. package/src/instances/overrides.ts +18 -0
  242. package/src/instances/protection.ts +103 -0
  243. package/src/instances/swap.ts +11 -9
  244. package/src/instances/sync.ts +11 -184
  245. package/src/layout/guides.ts +42 -9
  246. package/src/layout/index.ts +2 -0
  247. package/src/layout/limits.ts +64 -0
  248. package/src/layout/sizing.ts +0 -2
  249. package/src/layout/spacing.ts +11 -0
  250. package/src/masks.ts +51 -0
  251. package/src/matrix.ts +15 -2
  252. package/src/node-defaults.ts +23 -3
  253. package/src/outline/flatten.ts +30 -18
  254. package/src/outline/hit.ts +16 -33
  255. package/src/outline/index.ts +2 -2
  256. package/src/outline/shapes.ts +19 -36
  257. package/src/paints/index.ts +2 -0
  258. package/src/paints/pattern.ts +45 -0
  259. package/src/paints/shows.ts +13 -0
  260. package/src/paths/arc.ts +59 -0
  261. package/src/paths/index.ts +4 -0
  262. package/src/paths/network.ts +334 -0
  263. package/src/paths/sink.ts +50 -0
  264. package/src/paths/smooth-corners.ts +356 -0
  265. package/src/plugin-data/fields.ts +2 -0
  266. package/src/polygon.ts +0 -17
  267. package/src/resize.ts +6 -7
  268. package/src/scaling/node.ts +29 -3
  269. package/src/text/auto-resize.ts +25 -0
  270. package/src/text/index.ts +1 -0
  271. package/src/text/lengths.ts +11 -0
  272. package/src/text/overflow.ts +32 -6
  273. package/src/text/path.ts +13 -0
  274. package/src/text/picture.ts +5 -5
  275. package/src/transfer/dependencies.ts +3 -2
  276. package/src/transfer/nodes.ts +2 -1
  277. package/src/types.ts +25 -8
  278. package/src/variables/index.ts +1 -0
  279. package/src/variables/lookup.ts +31 -0
  280. package/src/variables/paints.ts +101 -20
  281. package/src/variables/resolution.ts +14 -2
  282. package/src/variables/subtree.ts +1 -1
  283. package/src/vector-network.ts +17 -138
@@ -1,6 +1,7 @@
1
1
  // What enclosing instances assign the component properties that drive a copy's layers.
2
2
  import type { SceneGraph } from '../graph'
3
3
  import type { ComponentPropertyReferenceField, SceneNode } from '../types'
4
+ import { assignmentOverrideField } from './overrides'
4
5
 
5
6
  /** SLOT_CONTENT is absent because it drives children, which `ownsSlotContent` keeps instead. */
6
7
  const PROPERTY_REFERENCE_FIELDS: Partial<Record<ComponentPropertyReferenceField, string>> = {
@@ -30,22 +31,28 @@ function definesProperty(graph: SceneGraph, instance: SceneNode, propertyId: str
30
31
  .some((definition) => definition.id === propertyId)
31
32
  }
32
33
 
34
+ interface Assignment {
35
+ /** The instance whose assignment it is. */
36
+ instance: SceneNode
37
+ value: string
38
+ }
39
+
33
40
  /**
34
41
  * What the nearest enclosing instance assigns `propertyId`, if any does. An instance assigns its
35
42
  * children, not itself, so the walk starts at the parent. A property id belongs to the component
36
43
  * that defines it, so the walk stops at an instance of that component even when it assigns
37
44
  * nothing; otherwise an outer instance's unrelated property of the same id wins.
38
45
  */
39
- function enclosingAssignment(
46
+ function findEnclosingAssignment(
40
47
  graph: SceneGraph,
41
48
  node: SceneNode,
42
49
  propertyId: string
43
- ): string | undefined {
50
+ ): Assignment | undefined {
44
51
  let current = node.parentId ? graph.nodes.get(node.parentId) : undefined
45
52
  while (current) {
46
53
  if (current.type === 'INSTANCE') {
47
54
  if (Object.hasOwn(current.componentPropertyAssignments, propertyId))
48
- return current.componentPropertyAssignments[propertyId]
55
+ return { instance: current, value: current.componentPropertyAssignments[propertyId] }
49
56
  if (definesProperty(graph, current, propertyId)) return undefined
50
57
  }
51
58
  current = current.parentId ? graph.nodes.get(current.parentId) : undefined
@@ -53,6 +60,55 @@ function enclosingAssignment(
53
60
  return undefined
54
61
  }
55
62
 
63
+ function enclosingAssignment(
64
+ graph: SceneGraph,
65
+ node: SceneNode,
66
+ propertyId: string
67
+ ): string | undefined {
68
+ return findEnclosingAssignment(graph, node, propertyId)?.value
69
+ }
70
+
71
+ /** The instances around `node`, nearest first. */
72
+ function enclosingInstances(graph: SceneGraph, node: SceneNode): SceneNode[] {
73
+ const instances: SceneNode[] = []
74
+ if (node.parentId)
75
+ graph.closest(node.parentId, (ancestor) => {
76
+ if (ancestor.type === 'INSTANCE') instances.push(ancestor)
77
+ return false
78
+ })
79
+ return instances
80
+ }
81
+
82
+ /** The outermost of `instances` that records `field` as an edit of `node`, or -1. */
83
+ function outermostEdit(instances: SceneNode[], node: SceneNode, field: string): number {
84
+ return instances.findLastIndex((instance) =>
85
+ instance.instanceOverrides.descendants.get(node.id)?.has(field)
86
+ )
87
+ }
88
+
89
+ /**
90
+ * Whether a text edit of `layer` outranks the assignment that drives it. As in Figma, the edit
91
+ * made in the outer instance wins, and at the same level the assignment does. An assignment is
92
+ * made where an override of it is recorded, or else by the instance that holds it.
93
+ */
94
+ function textEditOutranks(
95
+ graph: SceneGraph,
96
+ layer: SceneNode,
97
+ propertyId: string,
98
+ assignment: Assignment
99
+ ): boolean {
100
+ const instances = enclosingInstances(graph, layer)
101
+ const textLevel = outermostEdit(instances, layer, 'text')
102
+ if (textLevel < 0) return false
103
+ const overridden = outermostEdit(
104
+ instances,
105
+ assignment.instance,
106
+ assignmentOverrideField(propertyId)
107
+ )
108
+ const assignmentLevel = overridden < 0 ? instances.indexOf(assignment.instance) : overridden
109
+ return textLevel > assignmentLevel
110
+ }
111
+
56
112
  function hasEnclosingAssignment(graph: SceneGraph, node: SceneNode, propertyId: string): boolean {
57
113
  return enclosingAssignment(graph, node, propertyId) !== undefined
58
114
  }
@@ -107,10 +163,12 @@ export function assignedFieldValues(
107
163
  ): Partial<SceneNode> {
108
164
  const values: Partial<SceneNode> = {}
109
165
  for (const reference of compChild.componentPropertyReferences) {
110
- const value = enclosingAssignment(graph, instChild, reference.propertyId)
111
- if (value === undefined) continue
112
- if (reference.field === 'TEXT' && instChild.type === 'TEXT') values.text = value
113
- else if (reference.field === 'VISIBLE') values.visible = value === 'true'
166
+ const assignment = findEnclosingAssignment(graph, instChild, reference.propertyId)
167
+ if (!assignment) continue
168
+ if (reference.field === 'TEXT' && instChild.type === 'TEXT') {
169
+ if (!textEditOutranks(graph, instChild, reference.propertyId, assignment))
170
+ values.text = assignment.value
171
+ } else if (reference.field === 'VISIBLE') values.visible = assignment.value === 'true'
114
172
  }
115
173
  return values
116
174
  }
@@ -14,19 +14,23 @@ import { INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_PROPS } from './fields'
14
14
  import { findInstanceAncestor, instanceMainComponent } from './main-component'
15
15
  import {
16
16
  clearInstanceOverrides,
17
+ cloneInstanceOverrideState,
17
18
  getInstanceOverride,
18
19
  hasInstanceOverride as hasNodeInstanceOverride,
19
20
  setInstanceOverride
20
21
  } from './overrides'
21
- import { followComponentLayers } from './swap'
22
22
  import {
23
23
  bindingProtection,
24
- cloneChildrenWithMapping,
25
- copyProp,
26
24
  enclosingInstanceOverrideFields,
27
25
  isProtectedSyncField,
26
+ syncBindingFields
27
+ } from './protection'
28
+ import { walkInstanceSources } from './source-walk'
29
+ import { followComponentLayers } from './swap'
30
+ import {
31
+ cloneChildrenWithMapping,
32
+ copyProp,
28
33
  sourceInTargetCoordinates,
29
- syncBindingFields,
30
34
  syncChildren,
31
35
  updateSyncedProps
32
36
  } from './sync'
@@ -249,6 +253,8 @@ function syncInstancesOf(
249
253
  export function detachInstance(graph: SceneGraph, instanceId: string): void {
250
254
  const node = graph.nodes.get(instanceId)
251
255
  if (node?.type !== 'INSTANCE') return
256
+ for (const nested of outermostNestedInstances(graph, node))
257
+ adoptNestedOverrides(graph, node, nested)
252
258
  if (node.componentId) {
253
259
  graph.instanceIndex.get(node.componentId)?.delete(instanceId)
254
260
  }
@@ -257,6 +263,49 @@ export function detachInstance(graph: SceneGraph, instanceId: string): void {
257
263
  clearInstanceOverrides(node.instanceOverrides)
258
264
  }
259
265
 
266
+ // A swap's bookkeeping belongs to the instance enclosing the swapped one, never to the swapped one.
267
+ const OWNER_ONLY_FIELDS = new Set(['componentId', 'sourceComponentId'])
268
+
269
+ function outermostNestedInstances(graph: SceneGraph, instance: SceneNode): SceneNode[] {
270
+ return graph
271
+ .getChildren(instance.id)
272
+ .flatMap((child) =>
273
+ child.type === 'INSTANCE' ? [child] : outermostNestedInstances(graph, child)
274
+ )
275
+ }
276
+
277
+ /**
278
+ * An instance a detached one holds stands on its own once that one is a frame, and so must what
279
+ * makes it differ from its component. Cloned in, it kept the overrides of the instance it copies,
280
+ * under that instance's layers; those, and the ones the detached instance recorded on its layers,
281
+ * now go under its own.
282
+ */
283
+ function adoptNestedOverrides(graph: SceneGraph, detached: SceneNode, nested: SceneNode): void {
284
+ const ownLayer = new Map<string, string>()
285
+ walkInstanceSources(graph, nested, (source, target) => {
286
+ ownLayer.set(source.id, target.id)
287
+ return true
288
+ })
289
+ const { self, descendants: kept } = cloneInstanceOverrideState(nested.instanceOverrides)
290
+ const outer = cloneInstanceOverrideState(detached.instanceOverrides).descendants
291
+ const descendants = new Map<string, Map<string, unknown>>()
292
+ const claim = (layerId: string, fields: Map<string, unknown>): void => {
293
+ const merged = descendants.get(layerId)
294
+ if (!merged) descendants.set(layerId, fields)
295
+ else for (const [field, value] of fields) merged.set(field, value)
296
+ }
297
+ // What was recorded on its own layers is more recent than what it copied, so it is laid on top.
298
+ for (const [layerId, fields] of kept)
299
+ if (ownLayer.has(layerId)) claim(ownLayer.get(layerId) ?? layerId, fields)
300
+ for (const [layerId, fields] of kept) if (!ownLayer.has(layerId)) claim(layerId, fields)
301
+ for (const [layerId, fields] of outer) {
302
+ if (layerId === nested.id) {
303
+ for (const [field, value] of fields) if (!OWNER_ONLY_FIELDS.has(field)) self.set(field, value)
304
+ } else if (graph.isDescendant(layerId, nested.id)) claim(layerId, fields)
305
+ }
306
+ graph.updateNode(nested.id, { instanceOverrides: { self, descendants } })
307
+ }
308
+
260
309
  export function getMainComponent(graph: SceneGraph, instanceId: string): SceneNode | undefined {
261
310
  const node = graph.nodes.get(instanceId)
262
311
  if (!node?.componentId) return undefined
@@ -2,14 +2,11 @@
2
2
  import { omit } from 'es-toolkit/object'
3
3
  import { isEqual } from 'es-toolkit/predicate'
4
4
 
5
- import {
6
- overriddenAssignmentId,
7
- reapplyComponentPropertyAssignments
8
- } from '../components/properties'
5
+ import { reapplyComponentPropertyAssignments } from '../components/properties'
9
6
  import type { SceneGraph } from '../graph'
10
7
  import type { SceneNode } from '../types'
11
8
  import { instanceMainComponent } from './main-component'
12
- import { overriddenBindingField } from './overrides'
9
+ import { overriddenAssignmentId, overriddenBindingField } from './overrides'
13
10
 
14
11
  /** Where a switch runs: the switched instance and the instances enclosing the layers it changes. */
15
12
  export interface OverrideScope {
@@ -0,0 +1,101 @@
1
+ // Pairing an instance's children with its component's before a sync copies fields across.
2
+ import type { SceneGraph } from '../graph'
3
+ import type { SceneNode } from '../types'
4
+ import type { InstanceSyncContext } from './copies'
5
+ import {
6
+ copiedComponentLayerId,
7
+ hasInstanceOverride,
8
+ setInstanceOverride,
9
+ type InstanceOverrideState
10
+ } from './overrides'
11
+
12
+ /** Links a copy to the component layer it matched, reporting each changed node and its owner. */
13
+ function linkMatchedChild(
14
+ graph: SceneGraph,
15
+ context: InstanceSyncContext,
16
+ instParentId: string,
17
+ instChild: SceneNode,
18
+ compChildId: string
19
+ ): void {
20
+ const { overrides } = context
21
+ const recorded = hasInstanceOverride(overrides, instParentId, instChild.id, 'sourceComponentId')
22
+ if (instChild.type === 'INSTANCE' || recorded) {
23
+ const owner = graph.nodes.get(context.instanceId)
24
+ graph.willChangeNode(owner)
25
+ setInstanceOverride(overrides, instParentId, instChild.id, 'sourceComponentId', compChildId)
26
+ if (owner) graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
27
+ }
28
+ if (instChild.type !== 'INSTANCE') graph.updateNode(instChild.id, { componentId: compChildId })
29
+ }
30
+
31
+ /** Children no link pairs, matched by type and name where the counts leave no doubt. */
32
+ export function matchFallbackChildren(
33
+ graph: SceneGraph,
34
+ compParent: SceneNode,
35
+ instParent: SceneNode,
36
+ instParentId: string,
37
+ context: InstanceSyncContext,
38
+ instChildMap: Map<string, SceneNode>,
39
+ usedInstChildIds: Set<string>
40
+ ): void {
41
+ const fallbackByType = new Map<SceneNode['type'], Map<string, SceneNode[]>>()
42
+ const remainingCandidateCount = new Map<SceneNode['type'], number>()
43
+ const unmatchedComponentCount = new Map<SceneNode['type'], number>()
44
+ for (const compChildId of compParent.childIds) {
45
+ if (instChildMap.has(compChildId)) continue
46
+ const child = graph.nodes.get(compChildId)
47
+ if (child) {
48
+ unmatchedComponentCount.set(child.type, (unmatchedComponentCount.get(child.type) ?? 0) + 1)
49
+ }
50
+ }
51
+ for (const childId of instParent.childIds) {
52
+ const child = graph.nodes.get(childId)
53
+ if (!child || usedInstChildIds.has(child.id)) continue
54
+ remainingCandidateCount.set(child.type, (remainingCandidateCount.get(child.type) ?? 0) + 1)
55
+ let byName = fallbackByType.get(child.type)
56
+ if (!byName) {
57
+ byName = new Map()
58
+ fallbackByType.set(child.type, byName)
59
+ }
60
+ const queue = byName.get(child.name)
61
+ if (queue) queue.push(child)
62
+ else byName.set(child.name, [child])
63
+ }
64
+
65
+ // Match by name and type with FIFO queues to preserve sibling order in linear time.
66
+ for (const compChildId of compParent.childIds) {
67
+ if (instChildMap.has(compChildId)) continue
68
+ const compChild = graph.nodes.get(compChildId)
69
+ if (!compChild) continue
70
+ const candidatesByName = fallbackByType.get(compChild.type)
71
+ const candidateCount = remainingCandidateCount.get(compChild.type) ?? 0
72
+ if (candidateCount > (unmatchedComponentCount.get(compChild.type) ?? 0)) continue
73
+ const match = candidatesByName?.get(compChild.name)?.shift()
74
+ if (!match) continue
75
+ remainingCandidateCount.set(compChild.type, candidateCount - 1)
76
+ instChildMap.set(compChildId, match)
77
+ usedInstChildIds.add(match.id)
78
+ linkMatchedChild(graph, context, instParentId, match, compChildId)
79
+ }
80
+ }
81
+
82
+ /** Children already linked to a component child by sourceComponentId or componentId. */
83
+ export function matchLinkedChildren(
84
+ graph: SceneGraph,
85
+ instParent: SceneNode,
86
+ instParentId: string,
87
+ overrides: InstanceOverrideState,
88
+ compChildIdSet: ReadonlySet<string>,
89
+ instChildMap: Map<string, SceneNode>,
90
+ usedInstChildIds: Set<string>
91
+ ): void {
92
+ for (const childId of instParent.childIds) {
93
+ const child = graph.nodes.get(childId)
94
+ if (!child) continue
95
+ const mappedComponentId = copiedComponentLayerId(overrides, instParentId, child)
96
+ if (mappedComponentId && compChildIdSet.has(mappedComponentId)) {
97
+ instChildMap.set(mappedComponentId, child)
98
+ usedInstChildIds.add(child.id)
99
+ }
100
+ }
101
+ }
@@ -110,6 +110,19 @@ export interface InstanceOverrideReferenceMapper {
110
110
  node: (id: string) => string
111
111
  variable: (id: string) => string
112
112
  collection?: (id: string) => string | undefined
113
+ property?: (id: string) => string | undefined
114
+ }
115
+
116
+ const ASSIGNMENT_OVERRIDE_PREFIX = 'componentPropertyAssignments/'
117
+
118
+ export function assignmentOverrideField(propertyId: string): string {
119
+ return `${ASSIGNMENT_OVERRIDE_PREFIX}${propertyId}`
120
+ }
121
+
122
+ export function overriddenAssignmentId(field: string): string | undefined {
123
+ return field.startsWith(ASSIGNMENT_OVERRIDE_PREFIX)
124
+ ? field.slice(ASSIGNMENT_OVERRIDE_PREFIX.length)
125
+ : undefined
113
126
  }
114
127
 
115
128
  const VARIABLE_MODE_FIELD_PREFIX = 'variableModes/'
@@ -136,6 +149,11 @@ export function remapInstanceOverrideState(
136
149
  return value
137
150
  }
138
151
  const remapField = (field: string): string | undefined => {
152
+ const property = overriddenAssignmentId(field)
153
+ if (property !== undefined && references.property) {
154
+ const mapped = references.property(property)
155
+ return mapped === undefined ? undefined : assignmentOverrideField(mapped)
156
+ }
139
157
  const collection = variableModeOverrideCollection(field)
140
158
  if (collection === undefined || !references.collection) {
141
159
  return field
@@ -0,0 +1,103 @@
1
+ // What an instance's copy keeps through a sync: the fields it, or an instance around it, overrides.
2
+ import { isEmptyObject } from 'es-toolkit/predicate'
3
+
4
+ import type { SceneGraph } from '../graph'
5
+ import type { SceneNode } from '../types'
6
+ import { effectBindingTarget, paintBindingTarget } from '../variables/fields'
7
+ import { ownsSelfFillField } from './main-component'
8
+ import {
9
+ bindingOverrideField,
10
+ overriddenBindingField,
11
+ type InstanceOverrideState
12
+ } from './overrides'
13
+
14
+ /** Protection for a copy that keeps nothing of its own. */
15
+ export const NOTHING_PROTECTED = (_field: string): boolean => false
16
+
17
+ export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string) => boolean {
18
+ const fields = new Set<string>()
19
+ for (const scope of scopes) {
20
+ const perField = [...scope].some((field) => overriddenBindingField(field) !== null)
21
+ for (const field of scope) {
22
+ if (field !== 'boundVariables' || !perField) fields.add(field)
23
+ }
24
+ }
25
+ return (field) => {
26
+ if (fields.has(field)) return true
27
+ // Paints and effects an instance set itself own their bindings too, bound or not.
28
+ const binding = overriddenBindingField(field)
29
+ if (binding === null) return false
30
+ const paint = paintBindingTarget(binding)
31
+ if (paint) return fields.has(paint.list)
32
+ return fields.has('effects') && effectBindingTarget(binding) !== null
33
+ }
34
+ }
35
+
36
+ function isUnbound(node: SceneNode): boolean {
37
+ return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales)
38
+ }
39
+
40
+ export function syncBindingFields(
41
+ target: SceneNode,
42
+ source: SceneNode,
43
+ updates: Partial<SceneNode>,
44
+ protectedField: (field: string) => boolean
45
+ ): void {
46
+ if (protectedField('boundVariables') || (isUnbound(source) && isUnbound(target))) {
47
+ return
48
+ }
49
+ const bindings = { ...source.boundVariables }
50
+ const scales = { ...source.variableBindingScales }
51
+ for (const field of new Set([
52
+ ...Object.keys(source.boundVariables),
53
+ ...Object.keys(target.boundVariables),
54
+ ...Object.keys(target.variableBindingScales)
55
+ ])) {
56
+ if (!protectedField(bindingOverrideField(field))) continue
57
+ if (!Object.hasOwn(target.boundVariables, field)) Reflect.deleteProperty(bindings, field)
58
+ else bindings[field] = target.boundVariables[field]
59
+ if (target.variableBindingScales[field] === undefined) Reflect.deleteProperty(scales, field)
60
+ else scales[field] = target.variableBindingScales[field]
61
+ }
62
+ updates.boundVariables = bindings
63
+ updates.variableBindingScales = scales
64
+ }
65
+
66
+ export function isProtectedSyncField(
67
+ node: SceneNode,
68
+ key: string,
69
+ protectedField: (field: string) => boolean
70
+ ): boolean {
71
+ return (
72
+ protectedField(key) ||
73
+ (key in node.boundVariables &&
74
+ (protectedField('boundVariables') || protectedField(bindingOverrideField(key))))
75
+ )
76
+ }
77
+
78
+ export function enclosingInstanceOverrideFields(graph: SceneGraph, node: SceneNode): Set<string>[] {
79
+ const fields: Set<string>[] = []
80
+ if (!node.parentId) return fields
81
+ graph.closest(node.parentId, (ancestor) => {
82
+ if (ancestor.type === 'INSTANCE') {
83
+ fields.push(new Set(ancestor.instanceOverrides.descendants.get(node.id)?.keys()))
84
+ }
85
+ return false
86
+ })
87
+ return fields
88
+ }
89
+
90
+ /** The fields a copy keeps: its own overrides and those of every instance around it. */
91
+ export function childBindingProtection(
92
+ graph: SceneGraph,
93
+ child: SceneNode,
94
+ overrides: InstanceOverrideState
95
+ ) {
96
+ const fields = enclosingInstanceOverrideFields(graph, child)
97
+ fields.push(new Set(overrides.descendants.get(child.id)?.keys()))
98
+ if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) {
99
+ return NOTHING_PROTECTED
100
+ }
101
+ const protectedField = bindingProtection(fields)
102
+ return (field: string) => protectedField(field) || ownsSelfFillField(child, field)
103
+ }
@@ -1,9 +1,7 @@
1
1
  import { isEqual } from 'es-toolkit/predicate'
2
2
 
3
- import {
4
- overriddenAssignmentId,
5
- reapplyComponentPropertyAssignments
6
- } from '../components/properties'
3
+ import { appendAll } from '../arrays'
4
+ import { reapplyComponentPropertyAssignments } from '../components/properties'
7
5
  import type { SceneGraph } from '../graph'
8
6
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
9
7
  import type { SceneNode } from '../types'
@@ -11,12 +9,16 @@ import { applyBoundPaintColorsIn, refreshBoundPaintsIn } from '../variables/pain
11
9
  import { applyEnclosingAssignments } from './assignments'
12
10
  import { keepOverrides, restoreOverrides, type OverrideScope } from './kept-overrides'
13
11
  import { ownsSelfFillField } from './main-component'
14
- import { hasInstanceOverride, overriddenBindingField, setInstanceOverride } from './overrides'
15
12
  import {
16
- bindingProtection,
13
+ hasInstanceOverride,
14
+ overriddenAssignmentId,
15
+ overriddenBindingField,
16
+ setInstanceOverride
17
+ } from './overrides'
18
+ import { bindingProtection, NOTHING_PROTECTED } from './protection'
19
+ import {
17
20
  cloneChildInCoordinates,
18
21
  cloneChildrenWithMapping,
19
- NOTHING_PROTECTED,
20
22
  syncCopy,
21
23
  type MatchedCopy
22
24
  } from './sync'
@@ -128,7 +130,7 @@ function untakenCopies(byLayer: CopiesByLayer): SceneNode[] {
128
130
  }
129
131
  }
130
132
  for (const namesakes of byLayer.unlinked.values()) {
131
- untaken.push(...namesakes)
133
+ appendAll(untaken, namesakes)
132
134
  }
133
135
  return untaken
134
136
  }
@@ -328,7 +330,7 @@ function matchCopies(swap: Swap, compParent: SceneNode, instParent: SceneNode) {
328
330
  retyped.set(index, copy)
329
331
  return undefined
330
332
  })
331
- stale.push(...untakenCopies(byLayer))
333
+ appendAll(stale, untakenCopies(byLayer))
332
334
  return { matched, stale, retyped }
333
335
  }
334
336