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

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 (345) hide show
  1. package/dist/buffered-events.js +1 -1
  2. package/dist/buffered-events.js.map +1 -1
  3. package/dist/clipping.d.ts +7 -1
  4. package/dist/clipping.d.ts.map +1 -1
  5. package/dist/clipping.js +23 -19
  6. package/dist/clipping.js.map +1 -1
  7. package/dist/comments/schema.d.ts.map +1 -1
  8. package/dist/comments/schema.js +1 -0
  9. package/dist/comments/schema.js.map +1 -1
  10. package/dist/comments/threads.d.ts +2 -0
  11. package/dist/comments/threads.d.ts.map +1 -1
  12. package/dist/comments/threads.js +11 -5
  13. package/dist/comments/threads.js.map +1 -1
  14. package/dist/components/index.d.ts +2 -2
  15. package/dist/components/index.js +2 -2
  16. package/dist/components/properties.d.ts +1 -3
  17. package/dist/components/properties.d.ts.map +1 -1
  18. package/dist/components/properties.js +3 -10
  19. package/dist/components/properties.js.map +1 -1
  20. package/dist/constants.d.ts +12 -2
  21. package/dist/constants.d.ts.map +1 -1
  22. package/dist/constants.js +42 -1
  23. package/dist/constants.js.map +1 -1
  24. package/dist/copy.d.ts.map +1 -1
  25. package/dist/copy.js +2 -0
  26. package/dist/copy.js.map +1 -1
  27. package/dist/corners.d.ts +22 -0
  28. package/dist/corners.d.ts.map +1 -0
  29. package/dist/corners.js +52 -0
  30. package/dist/corners.js.map +1 -0
  31. package/dist/css/grid.d.ts +4 -1
  32. package/dist/css/grid.d.ts.map +1 -1
  33. package/dist/css/grid.js +40 -7
  34. package/dist/css/grid.js.map +1 -1
  35. package/dist/css/index.d.ts +3 -3
  36. package/dist/css/index.js +3 -3
  37. package/dist/css/layout.d.ts +13 -1
  38. package/dist/css/layout.d.ts.map +1 -1
  39. package/dist/css/layout.js +26 -1
  40. package/dist/css/layout.js.map +1 -1
  41. package/dist/css/values.d.ts +23 -1
  42. package/dist/css/values.d.ts.map +1 -1
  43. package/dist/css/values.js +39 -1
  44. package/dist/css/values.js.map +1 -1
  45. package/dist/font-style.d.ts +2 -1
  46. package/dist/font-style.d.ts.map +1 -1
  47. package/dist/font-style.js +31 -6
  48. package/dist/font-style.js.map +1 -1
  49. package/dist/geometry.d.ts +68 -3
  50. package/dist/geometry.d.ts.map +1 -1
  51. package/dist/geometry.js +159 -25
  52. package/dist/geometry.js.map +1 -1
  53. package/dist/gradient.d.ts +9 -2
  54. package/dist/gradient.d.ts.map +1 -1
  55. package/dist/gradient.js +29 -1
  56. package/dist/gradient.js.map +1 -1
  57. package/dist/graph.d.ts +9 -2
  58. package/dist/graph.d.ts.map +1 -1
  59. package/dist/graph.js +46 -10
  60. package/dist/graph.js.map +1 -1
  61. package/dist/group-bounds.d.ts.map +1 -1
  62. package/dist/group-bounds.js +4 -3
  63. package/dist/group-bounds.js.map +1 -1
  64. package/dist/guides.d.ts +11 -2
  65. package/dist/guides.d.ts.map +1 -1
  66. package/dist/guides.js +30 -1
  67. package/dist/guides.js.map +1 -1
  68. package/dist/hit-test.d.ts.map +1 -1
  69. package/dist/hit-test.js +57 -5
  70. package/dist/hit-test.js.map +1 -1
  71. package/dist/icons/fit.d.ts +17 -0
  72. package/dist/icons/fit.d.ts.map +1 -0
  73. package/dist/icons/fit.js +21 -0
  74. package/dist/icons/fit.js.map +1 -0
  75. package/dist/icons/glyph.d.ts.map +1 -1
  76. package/dist/icons/glyph.js +5 -4
  77. package/dist/icons/glyph.js.map +1 -1
  78. package/dist/icons/index.d.ts +2 -1
  79. package/dist/icons/index.js +2 -1
  80. package/dist/ids.js +1 -1
  81. package/dist/ids.js.map +1 -1
  82. package/dist/images.d.ts +14 -4
  83. package/dist/images.d.ts.map +1 -1
  84. package/dist/images.js +45 -5
  85. package/dist/images.js.map +1 -1
  86. package/dist/index.d.ts +2 -2
  87. package/dist/instances/assignments.js +39 -6
  88. package/dist/instances/assignments.js.map +1 -1
  89. package/dist/instances/index.d.ts +6 -1
  90. package/dist/instances/index.d.ts.map +1 -1
  91. package/dist/instances/index.js +58 -9
  92. package/dist/instances/index.js.map +1 -1
  93. package/dist/instances/kept-overrides.js +2 -2
  94. package/dist/instances/kept-overrides.js.map +1 -1
  95. package/dist/instances/matching.js +68 -0
  96. package/dist/instances/matching.js.map +1 -0
  97. package/dist/instances/overrides.d.ts +4 -1
  98. package/dist/instances/overrides.d.ts.map +1 -1
  99. package/dist/instances/overrides.js +13 -1
  100. package/dist/instances/overrides.js.map +1 -1
  101. package/dist/instances/protection.js +67 -0
  102. package/dist/instances/protection.js.map +1 -0
  103. package/dist/instances/swap.js +7 -5
  104. package/dist/instances/swap.js.map +1 -1
  105. package/dist/instances/sync.js +34 -116
  106. package/dist/instances/sync.js.map +1 -1
  107. package/dist/layout/guides.d.ts +17 -1
  108. package/dist/layout/guides.d.ts.map +1 -1
  109. package/dist/layout/guides.js +31 -11
  110. package/dist/layout/guides.js.map +1 -1
  111. package/dist/layout/index.d.ts +5 -2
  112. package/dist/layout/index.js +5 -2
  113. package/dist/layout/limits.d.ts +40 -0
  114. package/dist/layout/limits.d.ts.map +1 -0
  115. package/dist/layout/limits.js +54 -0
  116. package/dist/layout/limits.js.map +1 -0
  117. package/dist/layout/sizing.d.ts.map +1 -1
  118. package/dist/layout/sizing.js +0 -1
  119. package/dist/layout/sizing.js.map +1 -1
  120. package/dist/layout/spacing.d.ts +10 -0
  121. package/dist/layout/spacing.d.ts.map +1 -0
  122. package/dist/layout/spacing.js +12 -0
  123. package/dist/layout/spacing.js.map +1 -0
  124. package/dist/layout/strokes.d.ts +10 -0
  125. package/dist/layout/strokes.d.ts.map +1 -0
  126. package/dist/layout/strokes.js +30 -0
  127. package/dist/layout/strokes.js.map +1 -0
  128. package/dist/masks.d.ts +28 -0
  129. package/dist/masks.d.ts.map +1 -0
  130. package/dist/masks.js +53 -0
  131. package/dist/masks.js.map +1 -0
  132. package/dist/matrix.d.ts +2 -1
  133. package/dist/matrix.d.ts.map +1 -1
  134. package/dist/matrix.js +14 -1
  135. package/dist/matrix.js.map +1 -1
  136. package/dist/node-defaults.d.ts +9 -1
  137. package/dist/node-defaults.d.ts.map +1 -1
  138. package/dist/node-defaults.js +65 -38
  139. package/dist/node-defaults.js.map +1 -1
  140. package/dist/outline/flatten.d.ts.map +1 -1
  141. package/dist/outline/flatten.js +24 -17
  142. package/dist/outline/flatten.js.map +1 -1
  143. package/dist/outline/hit.d.ts +2 -8
  144. package/dist/outline/hit.d.ts.map +1 -1
  145. package/dist/outline/hit.js +16 -25
  146. package/dist/outline/hit.js.map +1 -1
  147. package/dist/outline/index.d.ts +3 -3
  148. package/dist/outline/index.js +3 -3
  149. package/dist/outline/shapes.d.ts +3 -1
  150. package/dist/outline/shapes.d.ts.map +1 -1
  151. package/dist/outline/shapes.js +18 -39
  152. package/dist/outline/shapes.js.map +1 -1
  153. package/dist/paints/index.d.ts +3 -1
  154. package/dist/paints/index.js +3 -1
  155. package/dist/paints/pattern.d.ts +13 -0
  156. package/dist/paints/pattern.d.ts.map +1 -0
  157. package/dist/paints/pattern.js +47 -0
  158. package/dist/paints/pattern.js.map +1 -0
  159. package/dist/paints/shows.d.ts +11 -0
  160. package/dist/paints/shows.d.ts.map +1 -0
  161. package/dist/paints/shows.js +16 -0
  162. package/dist/paints/shows.js.map +1 -0
  163. package/dist/parse-path.d.ts +4 -1
  164. package/dist/parse-path.d.ts.map +1 -1
  165. package/dist/parse-path.js +15 -1
  166. package/dist/parse-path.js.map +1 -1
  167. package/dist/paths/arc.d.ts +32 -0
  168. package/dist/paths/arc.d.ts.map +1 -0
  169. package/dist/paths/arc.js +49 -0
  170. package/dist/paths/arc.js.map +1 -0
  171. package/dist/paths/index.d.ts +5 -0
  172. package/dist/paths/index.js +5 -0
  173. package/dist/paths/network.d.ts +63 -0
  174. package/dist/paths/network.d.ts.map +1 -0
  175. package/dist/paths/network.js +315 -0
  176. package/dist/paths/network.js.map +1 -0
  177. package/dist/paths/sink.d.ts +20 -0
  178. package/dist/paths/sink.d.ts.map +1 -0
  179. package/dist/paths/sink.js +32 -0
  180. package/dist/paths/sink.js.map +1 -0
  181. package/dist/paths/smooth-corners.d.ts +25 -0
  182. package/dist/paths/smooth-corners.d.ts.map +1 -0
  183. package/dist/paths/smooth-corners.js +196 -0
  184. package/dist/paths/smooth-corners.js.map +1 -0
  185. package/dist/plugin-data/fields.d.ts +4 -0
  186. package/dist/plugin-data/fields.d.ts.map +1 -1
  187. package/dist/plugin-data/fields.js +2 -0
  188. package/dist/plugin-data/fields.js.map +1 -1
  189. package/dist/polygon.d.ts +1 -3
  190. package/dist/polygon.d.ts.map +1 -1
  191. package/dist/polygon.js +1 -16
  192. package/dist/polygon.js.map +1 -1
  193. package/dist/primitives/index.d.ts +1 -7
  194. package/dist/primitives/index.d.ts.map +1 -1
  195. package/dist/resize.d.ts.map +1 -1
  196. package/dist/resize.js +5 -5
  197. package/dist/resize.js.map +1 -1
  198. package/dist/scaling/node.d.ts.map +1 -1
  199. package/dist/scaling/node.js +25 -3
  200. package/dist/scaling/node.js.map +1 -1
  201. package/dist/shaders/model.d.ts.map +1 -1
  202. package/dist/shaders/model.js +1 -1
  203. package/dist/shaders/model.js.map +1 -1
  204. package/dist/shared-styles.d.ts.map +1 -1
  205. package/dist/shared-styles.js +2 -9
  206. package/dist/shared-styles.js.map +1 -1
  207. package/dist/strokes.d.ts +39 -0
  208. package/dist/strokes.d.ts.map +1 -0
  209. package/dist/strokes.js +104 -0
  210. package/dist/strokes.js.map +1 -0
  211. package/dist/text/auto-resize.d.ts +15 -0
  212. package/dist/text/auto-resize.d.ts.map +1 -0
  213. package/dist/text/auto-resize.js +23 -0
  214. package/dist/text/auto-resize.js.map +1 -0
  215. package/dist/text/index.d.ts +5 -3
  216. package/dist/text/index.js +5 -3
  217. package/dist/text/lengths.d.ts +6 -1
  218. package/dist/text/lengths.d.ts.map +1 -1
  219. package/dist/text/lengths.js +10 -1
  220. package/dist/text/lengths.js.map +1 -1
  221. package/dist/text/overflow.d.ts +14 -5
  222. package/dist/text/overflow.d.ts.map +1 -1
  223. package/dist/text/overflow.js +24 -6
  224. package/dist/text/overflow.js.map +1 -1
  225. package/dist/text/path.js +10 -0
  226. package/dist/text/path.js.map +1 -0
  227. package/dist/text/picture.js +5 -5
  228. package/dist/text/picture.js.map +1 -1
  229. package/dist/text/runs.d.ts +29 -0
  230. package/dist/text/runs.d.ts.map +1 -0
  231. package/dist/text/runs.js +72 -0
  232. package/dist/text/runs.js.map +1 -0
  233. package/dist/transfer/dependencies.d.ts.map +1 -1
  234. package/dist/transfer/dependencies.js +3 -2
  235. package/dist/transfer/dependencies.js.map +1 -1
  236. package/dist/transfer/nodes.js +2 -1
  237. package/dist/transfer/nodes.js.map +1 -1
  238. package/dist/types.d.ts +56 -10
  239. package/dist/types.d.ts.map +1 -1
  240. package/dist/types.js.map +1 -1
  241. package/dist/variables/index.d.ts +4 -3
  242. package/dist/variables/index.d.ts.map +1 -1
  243. package/dist/variables/index.js +4 -3
  244. package/dist/variables/index.js.map +1 -1
  245. package/dist/variables/lookup.d.ts +20 -0
  246. package/dist/variables/lookup.d.ts.map +1 -0
  247. package/dist/variables/lookup.js +34 -0
  248. package/dist/variables/lookup.js.map +1 -0
  249. package/dist/variables/paints.d.ts +35 -4
  250. package/dist/variables/paints.d.ts.map +1 -1
  251. package/dist/variables/paints.js +67 -16
  252. package/dist/variables/paints.js.map +1 -1
  253. package/dist/variables/resolution.d.ts +3 -1
  254. package/dist/variables/resolution.d.ts.map +1 -1
  255. package/dist/variables/resolution.js +8 -3
  256. package/dist/variables/resolution.js.map +1 -1
  257. package/dist/variables/subtree.js +1 -1
  258. package/dist/variables/subtree.js.map +1 -1
  259. package/dist/vector-network.d.ts.map +1 -1
  260. package/dist/vector-network.js +13 -79
  261. package/dist/vector-network.js.map +1 -1
  262. package/package.json +28 -7
  263. package/src/buffered-events.ts +2 -1
  264. package/src/clipping.ts +11 -8
  265. package/src/comments/schema.ts +1 -0
  266. package/src/comments/threads.ts +13 -5
  267. package/src/components/properties.ts +3 -14
  268. package/src/constants.ts +25 -1
  269. package/src/copy.ts +2 -0
  270. package/src/corners.ts +93 -0
  271. package/src/css/grid.ts +49 -7
  272. package/src/css/index.ts +13 -2
  273. package/src/css/layout.ts +36 -0
  274. package/src/css/values.ts +49 -0
  275. package/src/font-style.ts +38 -4
  276. package/src/geometry.ts +223 -28
  277. package/src/gradient.ts +23 -1
  278. package/src/graph.ts +35 -10
  279. package/src/group-bounds.ts +7 -3
  280. package/src/guides.ts +26 -1
  281. package/src/hit-test.ts +68 -10
  282. package/src/icons/fit.ts +24 -0
  283. package/src/icons/glyph.ts +6 -4
  284. package/src/icons/index.ts +1 -0
  285. package/src/ids.ts +1 -1
  286. package/src/images.ts +67 -7
  287. package/src/instances/assignments.ts +65 -7
  288. package/src/instances/index.ts +77 -13
  289. package/src/instances/kept-overrides.ts +2 -5
  290. package/src/instances/matching.ts +101 -0
  291. package/src/instances/overrides.ts +18 -0
  292. package/src/instances/protection.ts +103 -0
  293. package/src/instances/swap.ts +12 -9
  294. package/src/instances/sync.ts +52 -184
  295. package/src/layout/guides.ts +42 -9
  296. package/src/layout/index.ts +3 -0
  297. package/src/layout/limits.ts +64 -0
  298. package/src/layout/sizing.ts +0 -2
  299. package/src/layout/spacing.ts +11 -0
  300. package/src/layout/strokes.ts +23 -0
  301. package/src/masks.ts +51 -0
  302. package/src/matrix.ts +15 -2
  303. package/src/node-defaults.ts +71 -36
  304. package/src/outline/flatten.ts +31 -18
  305. package/src/outline/hit.ts +16 -33
  306. package/src/outline/index.ts +2 -2
  307. package/src/outline/shapes.ts +19 -36
  308. package/src/paints/index.ts +2 -0
  309. package/src/paints/pattern.ts +45 -0
  310. package/src/paints/shows.ts +13 -0
  311. package/src/parse-path.ts +14 -0
  312. package/src/paths/arc.ts +59 -0
  313. package/src/paths/index.ts +4 -0
  314. package/src/paths/network.ts +411 -0
  315. package/src/paths/sink.ts +50 -0
  316. package/src/paths/smooth-corners.ts +357 -0
  317. package/src/plugin-data/fields.ts +2 -0
  318. package/src/polygon.ts +0 -17
  319. package/src/primitives/index.ts +0 -5
  320. package/src/resize.ts +6 -7
  321. package/src/scaling/node.ts +29 -3
  322. package/src/shaders/model.ts +2 -1
  323. package/src/shared-styles.ts +3 -4
  324. package/src/strokes.ts +121 -0
  325. package/src/text/auto-resize.ts +25 -0
  326. package/src/text/index.ts +2 -0
  327. package/src/text/lengths.ts +11 -0
  328. package/src/text/overflow.ts +32 -6
  329. package/src/text/path.ts +13 -0
  330. package/src/text/picture.ts +5 -5
  331. package/src/text/runs.ts +80 -0
  332. package/src/transfer/dependencies.ts +4 -2
  333. package/src/transfer/nodes.ts +2 -1
  334. package/src/types.ts +62 -8
  335. package/src/variables/index.ts +1 -0
  336. package/src/variables/lookup.ts +31 -0
  337. package/src/variables/paints.ts +101 -20
  338. package/src/variables/resolution.ts +15 -2
  339. package/src/variables/subtree.ts +1 -1
  340. package/src/vector-network.ts +19 -142
  341. package/dist/random.d.ts +0 -7
  342. package/dist/random.d.ts.map +0 -1
  343. package/dist/random.js +0 -25
  344. package/dist/random.js.map +0 -1
  345. package/src/random.ts +0 -28
@@ -5,6 +5,7 @@ import {
5
5
  restoreOwnedSlotContent,
6
6
  type ParkedSlotContent
7
7
  } from '../slots/frames'
8
+ import { changesMoreThanStrokeLines } from '../strokes'
8
9
  import type { SceneNode } from '../types'
9
10
  import { applyBoundPaintColorsIn, keepCopyBoundPaints } from '../variables/paints'
10
11
  import { keepOwnContentSize } from './content-size'
@@ -14,19 +15,24 @@ import { INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_PROPS } from './fields'
14
15
  import { findInstanceAncestor, instanceMainComponent } from './main-component'
15
16
  import {
16
17
  clearInstanceOverrides,
18
+ cloneInstanceOverrideState,
17
19
  getInstanceOverride,
18
20
  hasInstanceOverride as hasNodeInstanceOverride,
19
21
  setInstanceOverride
20
22
  } from './overrides'
21
- import { followComponentLayers } from './swap'
22
23
  import {
23
24
  bindingProtection,
24
- cloneChildrenWithMapping,
25
- copyProp,
26
25
  enclosingInstanceOverrideFields,
27
26
  isProtectedSyncField,
27
+ syncBindingFields
28
+ } from './protection'
29
+ import { walkInstanceSources } from './source-walk'
30
+ import { followComponentLayers } from './swap'
31
+ import {
32
+ applySyncedStrokeLines,
33
+ cloneChildrenWithMapping,
34
+ copyProp,
28
35
  sourceInTargetCoordinates,
29
- syncBindingFields,
30
36
  syncChildren,
31
37
  updateSyncedProps
32
38
  } from './sync'
@@ -147,15 +153,14 @@ export function swapInstanceComponent(
147
153
  const previousComponent = namedAfter(graph, instance, component)
148
154
  recordNestedSwap(graph, instance, componentId)
149
155
  const source = sourceInTargetCoordinates(component, instance.componentScale)
150
- const updates: Partial<SceneNode> = {
151
- componentId,
152
- ...ownSyncedProps(graph, instance, source).updates
153
- }
156
+ const own = ownSyncedProps(graph, instance, source)
157
+ const updates: Partial<SceneNode> = { componentId, ...own.updates }
154
158
 
155
159
  if (!keepName && (!previousComponent || instance.name === previousComponent.name))
156
160
  updates.name = component.name
157
161
 
158
162
  keepCopyBoundPaints(graph, instance, updates)
163
+ applySyncedStrokeLines(instance, updates, own.kept)
159
164
  const slotContent = detachOwnedSlotContent(graph, instance)
160
165
  graph.updateNode(instanceId, updates)
161
166
  followComponentLayers(graph, instance, component, parkedContent(slotContent))
@@ -249,6 +254,8 @@ function syncInstancesOf(
249
254
  export function detachInstance(graph: SceneGraph, instanceId: string): void {
250
255
  const node = graph.nodes.get(instanceId)
251
256
  if (node?.type !== 'INSTANCE') return
257
+ for (const nested of outermostNestedInstances(graph, node))
258
+ adoptNestedOverrides(graph, node, nested)
252
259
  if (node.componentId) {
253
260
  graph.instanceIndex.get(node.componentId)?.delete(instanceId)
254
261
  }
@@ -257,6 +264,49 @@ export function detachInstance(graph: SceneGraph, instanceId: string): void {
257
264
  clearInstanceOverrides(node.instanceOverrides)
258
265
  }
259
266
 
267
+ // A swap's bookkeeping belongs to the instance enclosing the swapped one, never to the swapped one.
268
+ const OWNER_ONLY_FIELDS = new Set(['componentId', 'sourceComponentId'])
269
+
270
+ function outermostNestedInstances(graph: SceneGraph, instance: SceneNode): SceneNode[] {
271
+ return graph
272
+ .getChildren(instance.id)
273
+ .flatMap((child) =>
274
+ child.type === 'INSTANCE' ? [child] : outermostNestedInstances(graph, child)
275
+ )
276
+ }
277
+
278
+ /**
279
+ * An instance a detached one holds stands on its own once that one is a frame, and so must what
280
+ * makes it differ from its component. Cloned in, it kept the overrides of the instance it copies,
281
+ * under that instance's layers; those, and the ones the detached instance recorded on its layers,
282
+ * now go under its own.
283
+ */
284
+ function adoptNestedOverrides(graph: SceneGraph, detached: SceneNode, nested: SceneNode): void {
285
+ const ownLayer = new Map<string, string>()
286
+ walkInstanceSources(graph, nested, (source, target) => {
287
+ ownLayer.set(source.id, target.id)
288
+ return true
289
+ })
290
+ const { self, descendants: kept } = cloneInstanceOverrideState(nested.instanceOverrides)
291
+ const outer = cloneInstanceOverrideState(detached.instanceOverrides).descendants
292
+ const descendants = new Map<string, Map<string, unknown>>()
293
+ const claim = (layerId: string, fields: Map<string, unknown>): void => {
294
+ const merged = descendants.get(layerId)
295
+ if (!merged) descendants.set(layerId, fields)
296
+ else for (const [field, value] of fields) merged.set(field, value)
297
+ }
298
+ // What was recorded on its own layers is more recent than what it copied, so it is laid on top.
299
+ for (const [layerId, fields] of kept)
300
+ if (ownLayer.has(layerId)) claim(ownLayer.get(layerId) ?? layerId, fields)
301
+ for (const [layerId, fields] of kept) if (!ownLayer.has(layerId)) claim(layerId, fields)
302
+ for (const [layerId, fields] of outer) {
303
+ if (layerId === nested.id) {
304
+ for (const [field, value] of fields) if (!OWNER_ONLY_FIELDS.has(field)) self.set(field, value)
305
+ } else if (graph.isDescendant(layerId, nested.id)) claim(layerId, fields)
306
+ }
307
+ graph.updateNode(nested.id, { instanceOverrides: { self, descendants } })
308
+ }
309
+
260
310
  export function getMainComponent(graph: SceneGraph, instanceId: string): SceneNode | undefined {
261
311
  const node = graph.nodes.get(instanceId)
262
312
  if (!node?.componentId) return undefined
@@ -283,6 +333,16 @@ export function hasInstanceOverride(graph: SceneGraph, nodeId: string, field: st
283
333
  return hasNodeInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
284
334
  }
285
335
 
336
+ /**
337
+ * The fields `changes` overrides on an instance layer. New strokes that change only what the
338
+ * layer's line fields say override those fields, not the paints (`changesMoreThanStrokeLines`).
339
+ */
340
+ export function instanceOverrideFields(node: SceneNode, changes: Partial<SceneNode>): string[] {
341
+ const fields = Object.keys(changes)
342
+ if (!changes.strokes || changesMoreThanStrokeLines(node, changes)) return fields
343
+ return fields.filter((field) => field !== 'strokes')
344
+ }
345
+
286
346
  export function recordInstanceOverride(
287
347
  graph: SceneGraph,
288
348
  nodeId: string,
@@ -295,10 +355,14 @@ export function recordInstanceOverride(
295
355
  (INSTANCE_OVERRIDE_FIELDS as readonly string[]).includes(field)
296
356
  )
297
357
 
298
- if (relevant.length === 0) return
358
+ const added = relevant.filter(
359
+ (field) => !hasNodeInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
360
+ )
361
+ if (added.length === 0) return
299
362
 
300
- graph.willChangeNode(instance)
301
- for (const field of relevant)
302
- setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
303
- graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
363
+ // A new state rather than an edit of the old one, so a preview that captured the old state
364
+ // puts it back on cancel.
365
+ const overrides = cloneInstanceOverrideState(instance.instanceOverrides)
366
+ for (const field of added) setInstanceOverride(overrides, instance.id, nodeId, field)
367
+ graph.updateNode(instance.id, { instanceOverrides: overrides })
304
368
  }
@@ -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,8 @@
1
1
  import { isEqual } from 'es-toolkit/predicate'
2
2
 
3
- import {
4
- overriddenAssignmentId,
5
- reapplyComponentPropertyAssignments
6
- } from '../components/properties'
3
+ import { appendAll } from '@open-pencil/std/arrays'
4
+
5
+ import { reapplyComponentPropertyAssignments } from '../components/properties'
7
6
  import type { SceneGraph } from '../graph'
8
7
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
9
8
  import type { SceneNode } from '../types'
@@ -11,12 +10,16 @@ import { applyBoundPaintColorsIn, refreshBoundPaintsIn } from '../variables/pain
11
10
  import { applyEnclosingAssignments } from './assignments'
12
11
  import { keepOverrides, restoreOverrides, type OverrideScope } from './kept-overrides'
13
12
  import { ownsSelfFillField } from './main-component'
14
- import { hasInstanceOverride, overriddenBindingField, setInstanceOverride } from './overrides'
15
13
  import {
16
- bindingProtection,
14
+ hasInstanceOverride,
15
+ overriddenAssignmentId,
16
+ overriddenBindingField,
17
+ setInstanceOverride
18
+ } from './overrides'
19
+ import { bindingProtection, NOTHING_PROTECTED } from './protection'
20
+ import {
17
21
  cloneChildInCoordinates,
18
22
  cloneChildrenWithMapping,
19
- NOTHING_PROTECTED,
20
23
  syncCopy,
21
24
  type MatchedCopy
22
25
  } from './sync'
@@ -128,7 +131,7 @@ function untakenCopies(byLayer: CopiesByLayer): SceneNode[] {
128
131
  }
129
132
  }
130
133
  for (const namesakes of byLayer.unlinked.values()) {
131
- untaken.push(...namesakes)
134
+ appendAll(untaken, namesakes)
132
135
  }
133
136
  return untaken
134
137
  }
@@ -328,7 +331,7 @@ function matchCopies(swap: Swap, compParent: SceneNode, instParent: SceneNode) {
328
331
  retyped.set(index, copy)
329
332
  return undefined
330
333
  })
331
- stale.push(...untakenCopies(byLayer))
334
+ appendAll(stale, untakenCopies(byLayer))
332
335
  return { matched, stale, retyped }
333
336
  }
334
337