@open-pencil/scene-graph 0.15.1 → 0.16.0-beta.1

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 (232) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -1
  3. package/dist/chunks/behaviours/kinds.js +273 -0
  4. package/dist/chunks/behaviours/kinds.js.map +1 -0
  5. package/dist/chunks/behaviours/layers.js +596 -0
  6. package/dist/chunks/behaviours/layers.js.map +1 -0
  7. package/dist/chunks/behaviours/spec.js +181 -0
  8. package/dist/chunks/behaviours/spec.js.map +1 -0
  9. package/dist/chunks/buffered-events.js +57 -0
  10. package/dist/chunks/buffered-events.js.map +1 -0
  11. package/dist/chunks/color/contrast.js +57 -0
  12. package/dist/chunks/color/contrast.js.map +1 -0
  13. package/dist/chunks/{components/properties.js → color/index.js} +573 -521
  14. package/dist/chunks/color/index.js.map +1 -0
  15. package/dist/chunks/comments/document.js +32 -0
  16. package/dist/chunks/comments/document.js.map +1 -0
  17. package/dist/chunks/comments/list.js +182 -0
  18. package/dist/chunks/comments/list.js.map +1 -0
  19. package/dist/chunks/comments/merge.js +58 -0
  20. package/dist/chunks/comments/merge.js.map +1 -0
  21. package/dist/chunks/components/exposure.js +2021 -0
  22. package/dist/chunks/components/exposure.js.map +1 -0
  23. package/dist/chunks/components/variants.js +32 -0
  24. package/dist/chunks/components/variants.js.map +1 -0
  25. package/dist/chunks/coordinate.js +93 -55
  26. package/dist/chunks/coordinate.js.map +1 -1
  27. package/dist/chunks/css/grid.js +94 -0
  28. package/dist/chunks/css/grid.js.map +1 -0
  29. package/dist/chunks/css/index.js +160 -0
  30. package/dist/chunks/css/index.js.map +1 -0
  31. package/dist/chunks/fields/geometry.js +14 -0
  32. package/dist/chunks/fields/geometry.js.map +1 -0
  33. package/dist/chunks/fields/text.js +36 -0
  34. package/dist/chunks/fields/text.js.map +1 -0
  35. package/dist/chunks/group-bounds.js +104 -0
  36. package/dist/chunks/group-bounds.js.map +1 -0
  37. package/dist/chunks/icons/glyph.js +147 -0
  38. package/dist/chunks/icons/glyph.js.map +1 -0
  39. package/dist/chunks/index.js +1711 -661
  40. package/dist/chunks/index.js.map +1 -1
  41. package/dist/color.d.ts +72 -0
  42. package/dist/color.d.ts.map +1 -0
  43. package/dist/color.js +3 -0
  44. package/dist/constants.d.ts +2 -2
  45. package/dist/constants.js +2 -2
  46. package/dist/constants2.d.ts +3 -1
  47. package/dist/constants2.d.ts.map +1 -1
  48. package/dist/coordinate.d.ts +2 -448
  49. package/dist/coordinate.js +2 -2
  50. package/dist/copy.d.ts +1 -1
  51. package/dist/copy.js +1 -1
  52. package/dist/css.d.ts +39 -0
  53. package/dist/css.d.ts.map +1 -0
  54. package/dist/css.js +3 -0
  55. package/dist/geometry.d.ts +1 -1
  56. package/dist/geometry.js +1 -1
  57. package/dist/gradient.d.ts +51 -0
  58. package/dist/gradient.d.ts.map +1 -0
  59. package/dist/gradient.js +132 -0
  60. package/dist/gradient.js.map +1 -0
  61. package/dist/guides.d.ts +2 -2
  62. package/dist/guides.js +39 -0
  63. package/dist/guides.js.map +1 -0
  64. package/dist/hit-test.d.ts +2 -9
  65. package/dist/hit-test.js +154 -18
  66. package/dist/hit-test.js.map +1 -1
  67. package/dist/index.d.ts +4 -8
  68. package/dist/index.js +20 -5
  69. package/dist/instances.d.ts +2 -2
  70. package/dist/instances.js +2 -2
  71. package/dist/layout.d.ts +2 -0
  72. package/dist/layout.js +4 -0
  73. package/dist/node-defaults.d.ts +2 -8
  74. package/dist/node-defaults.js +2 -2
  75. package/dist/order-keys.d.ts +39 -0
  76. package/dist/order-keys.d.ts.map +1 -0
  77. package/dist/order-keys.js +136 -0
  78. package/dist/order-keys.js.map +1 -0
  79. package/dist/parse-path.d.ts +7 -1
  80. package/dist/parse-path.d.ts.map +1 -1
  81. package/dist/parse-path.js +6 -2
  82. package/dist/parse-path.js.map +1 -1
  83. package/dist/polygon.d.ts +53 -0
  84. package/dist/polygon.d.ts.map +1 -0
  85. package/dist/polygon.js +205 -0
  86. package/dist/polygon.js.map +1 -0
  87. package/dist/preview.d.ts +1 -1
  88. package/dist/random.d.ts +7 -0
  89. package/dist/random.d.ts.map +1 -0
  90. package/dist/random.js +2 -0
  91. package/dist/resize.d.ts +11 -2
  92. package/dist/resize.d.ts.map +1 -1
  93. package/dist/resize.js +2 -184
  94. package/dist/snap.d.ts +1 -1
  95. package/dist/text-direction.d.ts +13 -0
  96. package/dist/text-direction.d.ts.map +1 -0
  97. package/dist/text-direction.js +31 -0
  98. package/dist/text-direction.js.map +1 -0
  99. package/dist/text-picture.d.ts +1 -1
  100. package/dist/text-picture.d.ts.map +1 -1
  101. package/dist/text.d.ts +2 -0
  102. package/dist/text.js +3 -0
  103. package/dist/types.d.ts +2063 -13
  104. package/dist/types.d.ts.map +1 -1
  105. package/dist/types.js +2 -1
  106. package/dist/types2.d.ts +2 -2
  107. package/dist/undo.d.ts +2 -2
  108. package/dist/undo.js +2 -2
  109. package/dist/undo2.d.ts +29 -1
  110. package/dist/undo2.d.ts.map +1 -1
  111. package/dist/variables.d.ts +2 -29
  112. package/dist/variables.js +2 -2
  113. package/dist/variant-properties.d.ts +9 -1
  114. package/dist/variant-properties.d.ts.map +1 -1
  115. package/dist/variant-properties.js +36 -1
  116. package/dist/variant-properties.js.map +1 -1
  117. package/dist/vector-network.d.ts +1 -1
  118. package/dist/vector-network.js +1 -1
  119. package/package.json +60 -9
  120. package/src/behaviours/kinds.ts +185 -0
  121. package/src/behaviours/layers.ts +51 -0
  122. package/src/behaviours/model.ts +192 -0
  123. package/src/behaviours/schema.ts +58 -0
  124. package/src/behaviours/spec.ts +223 -0
  125. package/src/buffered-events.ts +64 -0
  126. package/src/color/contrast.ts +31 -0
  127. package/src/color/index.ts +88 -0
  128. package/src/color/management.ts +210 -0
  129. package/src/color/normalize.ts +7 -0
  130. package/src/color/okhcl.ts +201 -0
  131. package/src/comments/document.ts +51 -0
  132. package/src/comments/list.ts +52 -0
  133. package/src/comments/merge.ts +63 -0
  134. package/src/comments/threads.ts +156 -0
  135. package/src/components/exposure.ts +61 -0
  136. package/src/components/properties.ts +18 -15
  137. package/src/components/variants.ts +53 -0
  138. package/src/constants.ts +2 -0
  139. package/src/coordinate.ts +100 -43
  140. package/src/copy.ts +13 -5
  141. package/src/css/grid.ts +70 -0
  142. package/src/css/index.ts +9 -0
  143. package/src/css/layout.ts +53 -0
  144. package/src/css/shadows.ts +68 -0
  145. package/src/css/values.ts +42 -0
  146. package/src/export-format.ts +12 -0
  147. package/src/fields/geometry.ts +11 -0
  148. package/src/fields/text.ts +35 -0
  149. package/src/gradient.ts +166 -0
  150. package/src/group-bounds.ts +131 -0
  151. package/src/guides.ts +39 -0
  152. package/src/hit-test.ts +219 -25
  153. package/src/icons/glyph.ts +128 -0
  154. package/src/icons/model.ts +54 -0
  155. package/src/icons/schema.ts +18 -0
  156. package/src/index.ts +343 -55
  157. package/src/instance-overrides.ts +27 -0
  158. package/src/instances/copies.ts +239 -0
  159. package/src/instances/cycles.ts +73 -0
  160. package/src/instances/fields.ts +143 -0
  161. package/src/instances/main-component.ts +19 -0
  162. package/src/instances/placement.ts +29 -0
  163. package/src/instances/source-walk.ts +41 -0
  164. package/src/instances/sync.ts +599 -0
  165. package/src/instances.ts +122 -330
  166. package/src/layout/aspect-ratio.ts +51 -0
  167. package/src/{layout-guides.ts → layout/guides.ts} +1 -1
  168. package/src/layout/index.ts +4 -0
  169. package/src/layout/keywords.ts +73 -0
  170. package/src/layout/sizing.ts +196 -0
  171. package/src/mutation-impact.ts +25 -10
  172. package/src/node-defaults.ts +50 -2
  173. package/src/order-keys.ts +162 -0
  174. package/src/parse-path.ts +10 -2
  175. package/src/plugin-data/field.ts +131 -0
  176. package/src/plugin-data/fields.ts +246 -0
  177. package/src/polygon.ts +211 -0
  178. package/src/preview.ts +1 -1
  179. package/src/random.ts +28 -0
  180. package/src/resize.ts +99 -12
  181. package/src/scaling/index.ts +2 -0
  182. package/src/scaling/node.ts +164 -0
  183. package/src/scaling/tree.ts +50 -0
  184. package/src/shaders/model.ts +98 -0
  185. package/src/shaders/schema.ts +48 -0
  186. package/src/slots/authoring.ts +101 -0
  187. package/src/slots/content.ts +124 -0
  188. package/src/slots/frames.ts +160 -0
  189. package/src/slots/limits.ts +49 -0
  190. package/src/text/direction.ts +55 -0
  191. package/src/text/index.ts +2 -0
  192. package/src/text/lists.ts +138 -0
  193. package/src/text/paragraphs.ts +241 -0
  194. package/src/{text-picture.ts → text/picture.ts} +21 -29
  195. package/src/transfer/apply.ts +84 -0
  196. package/src/transfer/history.ts +49 -0
  197. package/src/transfer/identity.ts +16 -0
  198. package/src/transfer/index.ts +6 -0
  199. package/src/transfer/nodes.ts +108 -0
  200. package/src/transfer/plan.ts +107 -0
  201. package/src/transfer/references.ts +9 -0
  202. package/src/transfer/remove.ts +39 -0
  203. package/src/transfer/validation.ts +54 -0
  204. package/src/transfer/variables.ts +82 -0
  205. package/src/types.ts +221 -11
  206. package/src/undo.ts +54 -2
  207. package/src/variables/bindings.ts +186 -0
  208. package/src/variables/fields.ts +43 -0
  209. package/src/variables/token.ts +14 -0
  210. package/src/variables/units.ts +15 -0
  211. package/src/variables.ts +118 -69
  212. package/src/variant-properties.ts +50 -0
  213. package/dist/chunks/components/properties.js.map +0 -1
  214. package/dist/chunks/export-scale.js +0 -19
  215. package/dist/chunks/export-scale.js.map +0 -1
  216. package/dist/coordinate.d.ts.map +0 -1
  217. package/dist/coordinate2.d.ts +0 -2
  218. package/dist/guides2.d.ts +0 -11
  219. package/dist/guides2.d.ts.map +0 -1
  220. package/dist/hit-test.d.ts.map +0 -1
  221. package/dist/layout-guides.d.ts +0 -2
  222. package/dist/layout-guides.js +0 -2
  223. package/dist/layout-guides2.d.ts +0 -16
  224. package/dist/layout-guides2.d.ts.map +0 -1
  225. package/dist/node-defaults.d.ts.map +0 -1
  226. package/dist/preview2.d.ts +0 -12
  227. package/dist/preview2.d.ts.map +0 -1
  228. package/dist/resize.js.map +0 -1
  229. package/dist/variables.d.ts.map +0 -1
  230. package/dist/vector-network2.d.ts +0 -32
  231. package/dist/vector-network2.d.ts.map +0 -1
  232. package/src/instance-overrides.test.ts +0 -67
@@ -0,0 +1,239 @@
1
+ // How an instance's copies follow the component's layers when its structure changes.
2
+ import type { SceneGraph, SceneNode } from '../'
3
+ import {
4
+ getInstanceOverride,
5
+ hasInstanceOverride as hasNodeInstanceOverride,
6
+ type InstanceOverrideState
7
+ } from '../instance-overrides'
8
+ import { ownsSlotContent, slotPropertyId } from '../slots/frames'
9
+ import { DEFAULT_HISTORY_LIMIT } from '../undo'
10
+
11
+ /**
12
+ * One instance's sync: the copies it has matched to the component's layers, the parents it
13
+ * synced, and, built on first need, the copies the instance had before, by the layer they copy.
14
+ * Copies follow their layers when the component's structure changes, as Figma moves them: a
15
+ * layer moved inside the component keeps its copy and the copy's overrides, and a copy whose
16
+ * layer left or was deleted goes.
17
+ */
18
+ export interface InstanceSyncContext {
19
+ instanceId: string
20
+ overrides: InstanceOverrideState
21
+ matched: Set<string>
22
+ leftovers: Set<string>
23
+ copies?: Map<string, SceneNode[]>
24
+ }
25
+
26
+ export function createInstanceSyncContext(
27
+ instanceId: string,
28
+ overrides: InstanceOverrideState
29
+ ): InstanceSyncContext {
30
+ return { instanceId, overrides, matched: new Set(), leftovers: new Set() }
31
+ }
32
+
33
+ /** The component layer a copy in the instance stands for. */
34
+ function copiedLayerId(node: SceneNode, context: InstanceSyncContext): string | undefined {
35
+ const source =
36
+ node.type === 'INSTANCE'
37
+ ? getInstanceOverride(context.overrides, context.instanceId, node.id, 'sourceComponentId')
38
+ : undefined
39
+ return typeof source === 'string' ? source : (node.componentId ?? undefined)
40
+ }
41
+
42
+ /** Whether sync goes inside a copy: not into slot content, a swapped instance, or an instance. */
43
+ function syncsInside(graph: SceneGraph, node: SceneNode, context: InstanceSyncContext): boolean {
44
+ if (node.type === 'INSTANCE') return false
45
+ if (hasNodeInstanceOverride(context.overrides, context.instanceId, node.id, 'componentId')) {
46
+ return false
47
+ }
48
+ const layer = node.componentId ? graph.nodes.get(node.componentId) : undefined
49
+ return !layer || !ownsSlotContent(graph, node, slotPropertyId(layer))
50
+ }
51
+
52
+ function copiesOf(graph: SceneGraph, context: InstanceSyncContext): Map<string, SceneNode[]> {
53
+ if (context.copies) return context.copies
54
+ const copies = new Map<string, SceneNode[]>()
55
+ const visit = (parent: SceneNode) => {
56
+ for (const id of parent.childIds) {
57
+ const child = graph.nodes.get(id)
58
+ if (!child) continue
59
+ const layerId = copiedLayerId(child, context)
60
+ if (layerId) copies.set(layerId, [...(copies.get(layerId) ?? []), child])
61
+ if (syncsInside(graph, child, context)) visit(child)
62
+ }
63
+ }
64
+ const instance = graph.nodes.get(context.instanceId)
65
+ if (instance) visit(instance)
66
+ context.copies = copies
67
+ return copies
68
+ }
69
+
70
+ /** A copy of `layerId` the sync has not placed yet, which a moved layer takes along. */
71
+ export function unplacedCopy(
72
+ graph: SceneGraph,
73
+ context: InstanceSyncContext,
74
+ layerId: string,
75
+ instParentId: string
76
+ ): SceneNode | undefined {
77
+ return copiesOf(graph, context)
78
+ .get(layerId)
79
+ ?.find(
80
+ (copy) =>
81
+ !context.matched.has(copy.id) &&
82
+ graph.nodes.has(copy.id) &&
83
+ copy.id !== instParentId &&
84
+ !graph.isDescendant(instParentId, copy.id)
85
+ )
86
+ }
87
+
88
+ /** Whether any layer in the component subtree at `layerId` already has a copy to move. */
89
+ export function hasUnplacedCopiesBelow(
90
+ graph: SceneGraph,
91
+ context: InstanceSyncContext,
92
+ layerId: string
93
+ ): boolean {
94
+ const copies = copiesOf(graph, context)
95
+ if (copies.size === 0) return false
96
+ const layer = graph.nodes.get(layerId)
97
+ return (layer?.childIds ?? []).some(
98
+ (id) =>
99
+ copies.get(id)?.some((copy) => !context.matched.has(copy.id)) ||
100
+ hasUnplacedCopiesBelow(graph, context, id)
101
+ )
102
+ }
103
+
104
+ /** The copies one sync of a component's instances removed, by instance and layer. */
105
+ export interface CopyRemovals {
106
+ keys: Array<[instanceId: string, layerId: string]>
107
+ }
108
+
109
+ /** A copy the sync removed, kept so the layer coming back, as on undo, brings it back as it was. */
110
+ interface RemovedCopy {
111
+ /** The copy and everything in it, parents first. */
112
+ nodes: SceneNode[]
113
+ overrides: Map<string, Map<string, unknown>>
114
+ removals: CopyRemovals
115
+ }
116
+
117
+ interface RemovedCopyStore {
118
+ /** Removed copies by instance, then by the layer they copied. */
119
+ byInstance: Map<string, Map<string, RemovedCopy>>
120
+ /** Syncs that removed copies, oldest first. */
121
+ removals: CopyRemovals[]
122
+ }
123
+
124
+ const removedCopies = new WeakMap<SceneGraph, RemovedCopyStore>()
125
+
126
+ function removedCopyStore(graph: SceneGraph): RemovedCopyStore {
127
+ let store = removedCopies.get(graph)
128
+ if (!store) {
129
+ store = { byInstance: new Map(), removals: [] }
130
+ removedCopies.set(graph, store)
131
+ }
132
+ return store
133
+ }
134
+
135
+ /**
136
+ * Keeps one edit's removed copies restorable. Only as many edits as undo keeps are kept, so
137
+ * copies no undo can bring back are forgotten, oldest first.
138
+ */
139
+ function keepCopyRemovals(graph: SceneGraph, removals: CopyRemovals): void {
140
+ if (removals.keys.length === 0) return
141
+ const store = removedCopyStore(graph)
142
+ store.removals.push(removals)
143
+ while (store.removals.length > DEFAULT_HISTORY_LIMIT) {
144
+ const oldest = store.removals.shift()
145
+ for (const [instanceId, layerId] of oldest?.keys ?? []) {
146
+ const copies = store.byInstance.get(instanceId)
147
+ // A later sync may have removed the same layer's copy again; that one stays.
148
+ if (!copies || copies.get(layerId)?.removals !== oldest) continue
149
+ copies.delete(layerId)
150
+ if (copies.size === 0) store.byInstance.delete(instanceId)
151
+ }
152
+ }
153
+ }
154
+
155
+ const activeRemovals = new WeakMap<SceneGraph, CopyRemovals>()
156
+
157
+ /**
158
+ * Runs `fn` with every copy the syncs inside it remove kept as one group: one edit's syncs, however
159
+ * many components they reach, take one place in the history of removals.
160
+ */
161
+ export function groupCopyRemovals<T>(graph: SceneGraph, fn: (removals: CopyRemovals) => T): T {
162
+ const active = activeRemovals.get(graph)
163
+ if (active) return fn(active)
164
+ const removals: CopyRemovals = { keys: [] }
165
+ activeRemovals.set(graph, removals)
166
+ try {
167
+ return fn(removals)
168
+ } finally {
169
+ activeRemovals.delete(graph)
170
+ keepCopyRemovals(graph, removals)
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Brings back the copy of `layerId` the sync removed from this instance, with the same layers
176
+ * and overrides, as Figma restores an instance's override when the layer's deletion is undone.
177
+ */
178
+ export function restoreRemovedCopy(
179
+ graph: SceneGraph,
180
+ context: InstanceSyncContext,
181
+ layerId: string,
182
+ instParentId: string
183
+ ): SceneNode | undefined {
184
+ const store = removedCopyStore(graph)
185
+ const removed = store.byInstance.get(context.instanceId)
186
+ const entry = removed?.get(layerId)
187
+ if (!removed || !entry) return undefined
188
+ removed.delete(layerId)
189
+ if (removed.size === 0) store.byInstance.delete(context.instanceId)
190
+ const root = entry.nodes.at(0)
191
+ if (!root || entry.nodes.some((node) => graph.nodes.has(node.id))) return undefined
192
+ for (const node of entry.nodes) {
193
+ const { id, type, parentId, childIds: _childIds, ...props } = structuredClone(node)
194
+ graph.createNodeWithId(id, type, id === root.id ? instParentId : parentId, props)
195
+ }
196
+ for (const [id, fields] of entry.overrides) context.overrides.descendants.set(id, new Map(fields))
197
+ return graph.nodes.get(root.id)
198
+ }
199
+
200
+ /**
201
+ * Deletes the copies the sync left over: ones whose layer left the component, was deleted, or
202
+ * now has another copy. Copies the instance owns, such as slot content, are never reached.
203
+ */
204
+ export function removeLeftoverCopies(
205
+ graph: SceneGraph,
206
+ context: InstanceSyncContext,
207
+ removals: CopyRemovals
208
+ ): void {
209
+ const store = removedCopyStore(graph)
210
+ for (const id of context.leftovers) {
211
+ const node = graph.nodes.get(id)
212
+ const layerId = node ? copiedLayerId(node, context) : undefined
213
+ if (!node || !layerId || context.matched.has(id)) continue
214
+ const nodes: SceneNode[] = []
215
+ const collect = (current: SceneNode) => {
216
+ nodes.push(structuredClone(current))
217
+ for (const childId of current.childIds) {
218
+ const child = graph.nodes.get(childId)
219
+ if (child) collect(child)
220
+ }
221
+ }
222
+ collect(node)
223
+ const overrides = new Map<string, Map<string, unknown>>()
224
+ for (const removed of nodes) {
225
+ const fields = context.overrides.descendants.get(removed.id)
226
+ if (fields) overrides.set(removed.id, new Map(fields))
227
+ context.overrides.descendants.delete(removed.id)
228
+ }
229
+ let copies = store.byInstance.get(context.instanceId)
230
+ if (!copies) {
231
+ copies = new Map()
232
+ store.byInstance.set(context.instanceId, copies)
233
+ }
234
+ copies.set(layerId, { nodes, overrides, removals })
235
+ removals.keys.push([context.instanceId, layerId])
236
+ graph.deleteNode(id)
237
+ }
238
+ context.leftovers.clear()
239
+ }
@@ -0,0 +1,73 @@
1
+ import type { SceneGraph } from '../'
2
+ import { instanceMainComponent } from './main-component'
3
+
4
+ /** Components at or above `parentId`, counting the component each enclosing instance shows. */
5
+ function enclosingComponents(graph: SceneGraph, parentId: string): Set<string> {
6
+ const ancestors = new Set<string>()
7
+ graph.closest(parentId, (node) => {
8
+ if (node.type === 'COMPONENT') ancestors.add(node.id)
9
+ if (node.type === 'INSTANCE') {
10
+ const main = instanceMainComponent(graph, node)
11
+ if (main) ancestors.add(main.id)
12
+ }
13
+ return false
14
+ })
15
+ return ancestors
16
+ }
17
+
18
+ /**
19
+ * Whether moving `nodeId` under `parentId` would make a component contain itself: its layers,
20
+ * followed through instances into their components, reach a component at or above the parent.
21
+ * Both components and instances above the parent count: an instance's layers are its
22
+ * component's, so a swap inside one may not bring that component back in.
23
+ */
24
+ export function createsComponentCycle(
25
+ graph: SceneGraph,
26
+ nodeId: string,
27
+ parentId: string
28
+ ): boolean {
29
+ const ancestors = enclosingComponents(graph, parentId)
30
+ if (ancestors.size === 0) return false
31
+ const visited = new Set<string>()
32
+ function reachesAncestor(id: string): boolean {
33
+ if (ancestors.has(id)) return true
34
+ if (visited.has(id)) return false
35
+ visited.add(id)
36
+ const node = graph.getNode(id)
37
+ if (!node) return false
38
+ if (node.type === 'INSTANCE' && node.componentId && reachesAncestor(node.componentId))
39
+ return true
40
+ return node.childIds.some(reachesAncestor)
41
+ }
42
+ return reachesAncestor(nodeId)
43
+ }
44
+
45
+ /** Whether placing this definition's instance under `parentId` would make a component contain itself. */
46
+ export function canCreateInstance(
47
+ graph: SceneGraph,
48
+ componentId: string,
49
+ parentId: string
50
+ ): boolean {
51
+ if (graph.getNode(componentId)?.type !== 'COMPONENT') return false
52
+ return !createsComponentCycle(graph, componentId, parentId)
53
+ }
54
+
55
+ /**
56
+ * `parentId`, or its nearest ancestor that can hold these layers or instances of these
57
+ * components without a component containing itself. Figma pastes an instance next to its own
58
+ * component, not in it.
59
+ */
60
+ export function cycleSafeParent(
61
+ graph: SceneGraph,
62
+ parentId: string,
63
+ nodeIds: readonly string[]
64
+ ): string {
65
+ const safe = graph.closest(
66
+ parentId,
67
+ (node) =>
68
+ // Climbing out of a variant passes its set, which holds only variants.
69
+ (node.id === parentId || node.type !== 'COMPONENT_SET') &&
70
+ nodeIds.every((id) => !createsComponentCycle(graph, id, node.id))
71
+ )
72
+ return safe?.id ?? parentId
73
+ }
@@ -0,0 +1,143 @@
1
+ import type { SceneNode } from '../'
2
+
3
+ export const INSTANCE_SYNC_TEXT_PROPS = [
4
+ 'name',
5
+ 'text',
6
+ 'fontSize',
7
+ 'fontWeight',
8
+ 'fontFamily',
9
+ 'italic',
10
+ 'textDirection',
11
+ 'letterSpacing',
12
+ 'lineHeight',
13
+ 'textCase',
14
+ 'textDecoration',
15
+ 'textDecorationStyle',
16
+ 'textDecorationThickness',
17
+ 'textUnderlineOffset',
18
+ 'textDecorationFills',
19
+ 'textDecorationSkipInk',
20
+ 'textAlignHorizontal',
21
+ 'textAlignVertical',
22
+ 'textAutoResize',
23
+ 'textTruncation',
24
+ 'maxLines',
25
+ 'leadingTrim',
26
+ 'paragraphSpacing',
27
+ 'paragraphIndent',
28
+ 'listSpacing',
29
+ 'hangingList',
30
+ 'fontVariations',
31
+ 'fontFeatures',
32
+ // Styled ranges belong to the characters: an instance that overrides its text keeps them.
33
+ 'styleRuns',
34
+ 'textParagraphs'
35
+ ] as const
36
+
37
+ /** Fields that belong to another one: overriding the owner keeps them too. */
38
+ export const INSTANCE_SYNC_FIELD_OWNERS: Readonly<Partial<Record<string, string>>> = {
39
+ styleRuns: 'text',
40
+ textParagraphs: 'text'
41
+ }
42
+
43
+ export const INSTANCE_SYNC_PROPS: (keyof SceneNode)[] = [
44
+ 'width',
45
+ 'height',
46
+ 'minWidth',
47
+ 'maxWidth',
48
+ 'minHeight',
49
+ 'maxHeight',
50
+ 'targetAspectRatio',
51
+ 'fills',
52
+ 'strokes',
53
+ 'effects',
54
+ 'opacity',
55
+ 'cornerRadius',
56
+ 'topLeftRadius',
57
+ 'topRightRadius',
58
+ 'bottomRightRadius',
59
+ 'bottomLeftRadius',
60
+ 'independentCorners',
61
+ 'layoutMode',
62
+ 'layoutDirection',
63
+ 'layoutWrap',
64
+ 'primaryAxisAlign',
65
+ 'counterAxisAlign',
66
+ 'primaryAxisSizing',
67
+ 'counterAxisSizing',
68
+ 'itemSpacing',
69
+ 'counterAxisSpacing',
70
+ 'paddingTop',
71
+ 'paddingRight',
72
+ 'paddingBottom',
73
+ 'paddingLeft',
74
+ 'gridTemplateColumns',
75
+ 'gridTemplateRows',
76
+ 'gridColumnGap',
77
+ 'gridRowGap',
78
+ 'gridPosition',
79
+ 'clipsContent',
80
+ 'counterAxisAlignContent',
81
+ 'itemReverseZIndex',
82
+ 'strokesIncludedInLayout',
83
+ 'layoutGrids',
84
+ 'blendMode',
85
+ 'cornerSmoothing',
86
+ 'strokeWeight',
87
+ 'strokeAlign',
88
+ 'strokeCap',
89
+ 'strokeJoin',
90
+ 'strokeMiterLimit',
91
+ 'dashPattern',
92
+ 'independentStrokeWeights',
93
+ 'borderTopWeight',
94
+ 'borderRightWeight',
95
+ 'borderBottomWeight',
96
+ 'borderLeftWeight',
97
+ 'boundVariables',
98
+ 'variableModes',
99
+ // Applied shared styles follow the component unless an instance overrides them.
100
+ 'fillStyleId',
101
+ 'strokeStyleId',
102
+ 'textStyleId',
103
+ 'effectStyleId',
104
+ 'gridStyleId'
105
+ ]
106
+
107
+ /** How a layer inside an instance sits in its parent and whether it is locked, which its overrides may change. */
108
+ export const INSTANCE_SYNC_CHILD_PROPS = ['layoutGrow', 'layoutAlignSelf', 'locked'] as const
109
+
110
+ /**
111
+ * Where a component's layer sits and how it is shaped, which Figma refuses to override in an
112
+ * instance ("This property cannot be overridden in an instance"), so its copies always follow.
113
+ * A layer in an auto layout flow takes its position from the layout instead.
114
+ */
115
+ export const INSTANCE_FOLLOWED_FIELDS = [
116
+ 'x',
117
+ 'y',
118
+ 'rotation',
119
+ 'flipX',
120
+ 'flipY',
121
+ 'horizontalConstraint',
122
+ 'verticalConstraint',
123
+ 'layoutPositioning',
124
+ 'isMask',
125
+ 'maskType',
126
+ 'arcData',
127
+ 'pointCount',
128
+ 'starInnerRadius'
129
+ ] as const
130
+
131
+ /** Fields an edit to a layer inside an instance records as that instance's override. */
132
+ export const INSTANCE_OVERRIDE_FIELDS = [
133
+ ...INSTANCE_SYNC_PROPS,
134
+ ...INSTANCE_SYNC_TEXT_PROPS,
135
+ ...INSTANCE_SYNC_CHILD_PROPS,
136
+ 'visible'
137
+ ] as const
138
+
139
+ /** Fields a component's layer gives the copies of it in every instance. */
140
+ export const INSTANCE_SYNC_FIELDS = [
141
+ ...INSTANCE_OVERRIDE_FIELDS,
142
+ ...INSTANCE_FOLLOWED_FIELDS
143
+ ] as const
@@ -0,0 +1,19 @@
1
+ import type { SceneGraph } from '../index'
2
+ import type { SceneNode } from '../types'
3
+
4
+ /**
5
+ * The main component an instance shows. An instance nested in another instance points at the
6
+ * instance it was cloned from, so follow those links to the component.
7
+ */
8
+ export function instanceMainComponent(
9
+ graph: SceneGraph,
10
+ instance: SceneNode
11
+ ): SceneNode | undefined {
12
+ const seen = new Set<string>()
13
+ let current = instance.componentId ? graph.getNode(instance.componentId) : undefined
14
+ while (current?.type === 'INSTANCE' && current.componentId && !seen.has(current.id)) {
15
+ seen.add(current.id)
16
+ current = graph.getNode(current.componentId)
17
+ }
18
+ return current?.type === 'COMPONENT' ? current : undefined
19
+ }
@@ -0,0 +1,29 @@
1
+ import type { SceneNode } from '../'
2
+ import { enforcedAspectRatio } from '../layout/aspect-ratio'
3
+ import { layoutConstrainedChildRect } from '../resize'
4
+
5
+ /** The fields that say where a copy sits in its parent. */
6
+ export const PLACEMENT_FIELDS: ReadonlySet<string> = new Set(['x', 'y', 'width', 'height'])
7
+
8
+ /**
9
+ * The component's layer placed in the copy's parent: where its constraints put it when the
10
+ * instance's frame has another size than the component's, as Figma resizes an instance.
11
+ */
12
+ export function placedByConstraints(
13
+ source: SceneNode,
14
+ compParent: SceneNode,
15
+ instParent: SceneNode,
16
+ factor: number
17
+ ): SceneNode {
18
+ const before = { width: compParent.width * factor, height: compParent.height * factor }
19
+ if (before.width === instParent.width && before.height === instParent.height) return source
20
+ const rect = layoutConstrainedChildRect(
21
+ source,
22
+ before,
23
+ instParent,
24
+ source.horizontalConstraint,
25
+ source.verticalConstraint,
26
+ enforcedAspectRatio(source)
27
+ )
28
+ return { ...source, ...rect }
29
+ }
@@ -0,0 +1,41 @@
1
+ import type { SceneGraph } from '../'
2
+ import { getInstanceOverride } from '../instance-overrides'
3
+ import type { SceneNode } from '../types'
4
+
5
+ /**
6
+ * Walk an instance's layers beside the component layers they copy, matched the way instance
7
+ * sync matches them. `visit` returns whether to continue into that pair's children. With
8
+ * `strict`, two layers claiming one source throw instead of the later one winning.
9
+ */
10
+ export function walkInstanceSources(
11
+ graph: SceneGraph,
12
+ instance: SceneNode,
13
+ visit: (source: SceneNode, target: SceneNode) => boolean,
14
+ strict = false
15
+ ): void {
16
+ if (instance.type !== 'INSTANCE' || !instance.componentId) return
17
+ const component = graph.getNode(instance.componentId)
18
+ if (!component) return
19
+ const walk = (sourceParent: SceneNode, instanceParent: SceneNode): void => {
20
+ const bySource = new Map<string, SceneNode>()
21
+ for (const child of graph.getChildren(instanceParent.id)) {
22
+ const mapped = getInstanceOverride(
23
+ instance.instanceOverrides,
24
+ instance.id,
25
+ child.id,
26
+ 'sourceComponentId'
27
+ )
28
+ const sourceId = typeof mapped === 'string' ? mapped : child.componentId
29
+ if (!sourceId) continue
30
+ if (strict && bySource.has(sourceId))
31
+ throw new Error(`Ambiguous component-property target ${sourceId}`)
32
+ bySource.set(sourceId, child)
33
+ }
34
+ for (const sourceId of sourceParent.childIds) {
35
+ const source = graph.getNode(sourceId)
36
+ const target = bySource.get(sourceId)
37
+ if (source && target && visit(source, target)) walk(source, target)
38
+ }
39
+ }
40
+ walk(component, instance)
41
+ }