@pairdraw/scene-graph 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/dist/behaviours/index.d.ts +2 -2
  2. package/dist/behaviours/index.js +2 -2
  3. package/dist/behaviours/spec.d.ts +6 -1
  4. package/dist/behaviours/spec.d.ts.map +1 -1
  5. package/dist/behaviours/spec.js +62 -5
  6. package/dist/behaviours/spec.js.map +1 -1
  7. package/dist/canvas-layout.js +66 -0
  8. package/dist/canvas-layout.js.map +1 -0
  9. package/dist/components/index.d.ts +2 -2
  10. package/dist/components/index.js +2 -2
  11. package/dist/components/variants.d.ts +12 -1
  12. package/dist/components/variants.d.ts.map +1 -1
  13. package/dist/components/variants.js +30 -1
  14. package/dist/components/variants.js.map +1 -1
  15. package/dist/constants.d.ts +25 -1
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +94 -1
  18. package/dist/constants.js.map +1 -1
  19. package/dist/coordinate.js +1 -1
  20. package/dist/copy.d.ts.map +1 -1
  21. package/dist/copy.js +17 -1
  22. package/dist/copy.js.map +1 -1
  23. package/dist/css/index.d.ts +2 -2
  24. package/dist/css/index.js +2 -2
  25. package/dist/css/text.d.ts +22 -1
  26. package/dist/css/text.d.ts.map +1 -1
  27. package/dist/css/text.js +81 -1
  28. package/dist/css/text.js.map +1 -1
  29. package/dist/design-memory/index.d.ts +3 -0
  30. package/dist/design-memory/index.js +3 -0
  31. package/dist/design-memory/model.d.ts +33 -0
  32. package/dist/design-memory/model.d.ts.map +1 -0
  33. package/dist/design-memory/model.js +142 -0
  34. package/dist/design-memory/model.js.map +1 -0
  35. package/dist/design-memory/schema.d.ts +76 -0
  36. package/dist/design-memory/schema.d.ts.map +1 -0
  37. package/dist/design-memory/schema.js +72 -0
  38. package/dist/design-memory/schema.js.map +1 -0
  39. package/dist/effects.d.ts +7 -4
  40. package/dist/effects.d.ts.map +1 -1
  41. package/dist/effects.js +9 -4
  42. package/dist/effects.js.map +1 -1
  43. package/dist/empty-values.js +93 -0
  44. package/dist/empty-values.js.map +1 -0
  45. package/dist/fields/components.d.ts +9 -0
  46. package/dist/fields/components.d.ts.map +1 -0
  47. package/dist/fields/components.js +24 -0
  48. package/dist/fields/components.js.map +1 -0
  49. package/dist/fields/index.d.ts +3 -2
  50. package/dist/fields/index.js +3 -2
  51. package/dist/fields/layout.d.ts +3 -1
  52. package/dist/fields/layout.d.ts.map +1 -1
  53. package/dist/fields/layout.js +10 -1
  54. package/dist/fields/layout.js.map +1 -1
  55. package/dist/generated/index.d.ts +2 -2
  56. package/dist/generated/index.js +2 -2
  57. package/dist/generated/shape-with-text.d.ts +7 -2
  58. package/dist/generated/shape-with-text.d.ts.map +1 -1
  59. package/dist/generated/shape-with-text.js +20 -3
  60. package/dist/generated/shape-with-text.js.map +1 -1
  61. package/dist/graph.d.ts +8 -1
  62. package/dist/graph.d.ts.map +1 -1
  63. package/dist/graph.js +36 -7
  64. package/dist/graph.js.map +1 -1
  65. package/dist/grid-values.d.ts +10 -0
  66. package/dist/grid-values.d.ts.map +1 -0
  67. package/dist/grid-values.js +53 -0
  68. package/dist/grid-values.js.map +1 -0
  69. package/dist/group-bounds.d.ts +4 -1
  70. package/dist/group-bounds.d.ts.map +1 -1
  71. package/dist/group-bounds.js +11 -1
  72. package/dist/group-bounds.js.map +1 -1
  73. package/dist/hit-test/drop.d.ts +20 -0
  74. package/dist/hit-test/drop.d.ts.map +1 -0
  75. package/dist/hit-test/drop.js +54 -0
  76. package/dist/hit-test/drop.js.map +1 -0
  77. package/dist/hit-test/geometry.js +86 -0
  78. package/dist/hit-test/geometry.js.map +1 -0
  79. package/dist/hit-test/masks.d.ts +14 -0
  80. package/dist/hit-test/masks.d.ts.map +1 -0
  81. package/dist/hit-test/masks.js +41 -0
  82. package/dist/hit-test/masks.js.map +1 -0
  83. package/dist/hit-test/paint.d.ts +6 -0
  84. package/dist/hit-test/paint.d.ts.map +1 -0
  85. package/dist/hit-test/paint.js +19 -0
  86. package/dist/hit-test/paint.js.map +1 -0
  87. package/dist/hit-test.d.ts +4 -15
  88. package/dist/hit-test.d.ts.map +1 -1
  89. package/dist/hit-test.js +33 -178
  90. package/dist/hit-test.js.map +1 -1
  91. package/dist/index.d.ts +2 -2
  92. package/dist/instances/fields.d.ts.map +1 -1
  93. package/dist/instances/fields.js +2 -0
  94. package/dist/instances/fields.js.map +1 -1
  95. package/dist/instances/index.d.ts +2 -1
  96. package/dist/instances/index.d.ts.map +1 -1
  97. package/dist/instances/index.js +1 -1
  98. package/dist/instances/index.js.map +1 -1
  99. package/dist/instances/source-walk.d.ts +12 -0
  100. package/dist/instances/source-walk.d.ts.map +1 -0
  101. package/dist/node-defaults.d.ts +21 -1
  102. package/dist/node-defaults.d.ts.map +1 -1
  103. package/dist/node-defaults.js +63 -50
  104. package/dist/node-defaults.js.map +1 -1
  105. package/dist/plugin-data/fields.d.ts +75 -0
  106. package/dist/plugin-data/fields.d.ts.map +1 -1
  107. package/dist/plugin-data/fields.js +18 -1
  108. package/dist/plugin-data/fields.js.map +1 -1
  109. package/dist/preview.d.ts.map +1 -1
  110. package/dist/preview.js +2 -0
  111. package/dist/preview.js.map +1 -1
  112. package/dist/resize.d.ts.map +1 -1
  113. package/dist/resize.js +3 -6
  114. package/dist/resize.js.map +1 -1
  115. package/dist/scaling/node.d.ts.map +1 -1
  116. package/dist/scaling/node.js +28 -4
  117. package/dist/scaling/node.js.map +1 -1
  118. package/dist/shared-styles.d.ts +3 -1
  119. package/dist/shared-styles.d.ts.map +1 -1
  120. package/dist/shared-styles.js +7 -1
  121. package/dist/shared-styles.js.map +1 -1
  122. package/dist/text/path.js +11 -0
  123. package/dist/text/path.js.map +1 -0
  124. package/dist/text/picture.d.ts.map +1 -1
  125. package/dist/text/picture.js +5 -5
  126. package/dist/text/picture.js.map +1 -1
  127. package/dist/token-sources/document.d.ts +26 -0
  128. package/dist/token-sources/document.d.ts.map +1 -0
  129. package/dist/token-sources/document.js +80 -0
  130. package/dist/token-sources/document.js.map +1 -0
  131. package/dist/token-sources/index.d.ts +3 -0
  132. package/dist/token-sources/index.js +3 -0
  133. package/dist/token-sources/schema.d.ts +43 -0
  134. package/dist/token-sources/schema.d.ts.map +1 -0
  135. package/dist/token-sources/schema.js +50 -0
  136. package/dist/token-sources/schema.js.map +1 -0
  137. package/dist/transfer/copied.d.ts +14 -0
  138. package/dist/transfer/copied.d.ts.map +1 -0
  139. package/dist/transfer/copied.js +52 -0
  140. package/dist/transfer/copied.js.map +1 -0
  141. package/dist/transfer/index.d.ts +2 -1
  142. package/dist/transfer/index.js +2 -1
  143. package/dist/transfer/nodes.js +7 -0
  144. package/dist/transfer/nodes.js.map +1 -1
  145. package/dist/types.d.ts +104 -15
  146. package/dist/types.d.ts.map +1 -1
  147. package/dist/types.js.map +1 -1
  148. package/dist/undo.d.ts +6 -0
  149. package/dist/undo.d.ts.map +1 -1
  150. package/dist/undo.js +13 -0
  151. package/dist/undo.js.map +1 -1
  152. package/dist/variables/index.d.ts.map +1 -1
  153. package/dist/variables/index.js.map +1 -1
  154. package/dist/variations/copy.d.ts +16 -0
  155. package/dist/variations/copy.d.ts.map +1 -0
  156. package/dist/variations/copy.js +78 -0
  157. package/dist/variations/copy.js.map +1 -0
  158. package/dist/variations/index.d.ts +6 -0
  159. package/dist/variations/index.js +6 -0
  160. package/dist/variations/match.d.ts +23 -0
  161. package/dist/variations/match.d.ts.map +1 -0
  162. package/dist/variations/match.js +80 -0
  163. package/dist/variations/match.js.map +1 -0
  164. package/dist/variations/model.d.ts +39 -0
  165. package/dist/variations/model.d.ts.map +1 -0
  166. package/dist/variations/model.js +90 -0
  167. package/dist/variations/model.js.map +1 -0
  168. package/dist/variations/pick.d.ts +43 -0
  169. package/dist/variations/pick.d.ts.map +1 -0
  170. package/dist/variations/pick.js +211 -0
  171. package/dist/variations/pick.js.map +1 -0
  172. package/dist/variations/schema.d.ts +27 -0
  173. package/dist/variations/schema.d.ts.map +1 -0
  174. package/dist/variations/schema.js +27 -0
  175. package/dist/variations/schema.js.map +1 -0
  176. package/package.json +21 -1
  177. package/src/behaviours/spec.ts +94 -4
  178. package/src/canvas-layout.ts +76 -0
  179. package/src/components/variants.ts +39 -0
  180. package/src/constants.ts +75 -0
  181. package/src/copy.ts +20 -1
  182. package/src/css/index.ts +12 -1
  183. package/src/css/text.ts +86 -0
  184. package/src/design-memory/index.ts +2 -0
  185. package/src/design-memory/model.ts +143 -0
  186. package/src/design-memory/schema.ts +84 -0
  187. package/src/effects.ts +16 -7
  188. package/src/empty-values.ts +101 -0
  189. package/src/fields/components.ts +21 -0
  190. package/src/fields/index.ts +1 -0
  191. package/src/fields/layout.ts +10 -0
  192. package/src/generated/shape-with-text.ts +28 -2
  193. package/src/graph.ts +48 -6
  194. package/src/grid-values.ts +47 -0
  195. package/src/group-bounds.ts +13 -0
  196. package/src/hit-test/drop.ts +90 -0
  197. package/src/hit-test/geometry.ts +131 -0
  198. package/src/hit-test/masks.ts +57 -0
  199. package/src/hit-test/paint.ts +24 -0
  200. package/src/hit-test.ts +78 -291
  201. package/src/instances/fields.ts +2 -0
  202. package/src/instances/index.ts +1 -0
  203. package/src/node-defaults.ts +71 -52
  204. package/src/plugin-data/fields.ts +18 -1
  205. package/src/preview.ts +2 -0
  206. package/src/resize.ts +3 -14
  207. package/src/scaling/node.ts +34 -5
  208. package/src/shared-styles.ts +8 -0
  209. package/src/text/path.ts +14 -0
  210. package/src/text/picture.ts +5 -2
  211. package/src/token-sources/document.ts +104 -0
  212. package/src/token-sources/index.ts +2 -0
  213. package/src/token-sources/schema.ts +59 -0
  214. package/src/transfer/copied.ts +72 -0
  215. package/src/transfer/index.ts +1 -0
  216. package/src/transfer/nodes.ts +10 -0
  217. package/src/types.ts +117 -14
  218. package/src/undo.ts +11 -0
  219. package/src/variables/index.ts +1 -4
  220. package/src/variations/copy.ts +89 -0
  221. package/src/variations/index.ts +5 -0
  222. package/src/variations/match.ts +100 -0
  223. package/src/variations/model.ts +132 -0
  224. package/src/variations/pick.ts +293 -0
  225. package/src/variations/schema.ts +29 -0
@@ -0,0 +1,72 @@
1
+ import { isEqual, mapValues } from 'es-toolkit'
2
+
3
+ import { componentPropertyDefinitions } from '../components/properties'
4
+ import type { SceneGraph } from '../graph'
5
+ import { remapInstanceOverrideState } from '../instances/overrides'
6
+ import type { ComponentPropertyDefinition, SceneNode } from '../types'
7
+
8
+ function swapPropertyIds(definitions: readonly ComponentPropertyDefinition[]): string[] {
9
+ return definitions
10
+ .filter((definition) => definition.type === 'INSTANCE_SWAP')
11
+ .map((definition) => definition.id)
12
+ }
13
+
14
+ function remappedReferences(
15
+ node: SceneNode,
16
+ mapped: (id: string) => string,
17
+ swapProperties: ReadonlySet<string>
18
+ ): Partial<SceneNode> {
19
+ const changes: Partial<SceneNode> = {}
20
+ if (node.componentId && mapped(node.componentId) !== node.componentId)
21
+ changes.componentId = mapped(node.componentId)
22
+ // Only what a mapping changes: every write is an update collaboration sends.
23
+ const overrides = remapInstanceOverrideState(node.instanceOverrides, {
24
+ node: mapped,
25
+ variable: (id) => id
26
+ })
27
+ if (!isEqual(overrides, node.instanceOverrides)) changes.instanceOverrides = overrides
28
+ const definitions = node.componentPropertyDefinitions.map((definition) =>
29
+ definition.type === 'INSTANCE_SWAP' && definition.defaultValue
30
+ ? { ...definition, defaultValue: mapped(definition.defaultValue) }
31
+ : definition
32
+ )
33
+ if (!isEqual(definitions, node.componentPropertyDefinitions))
34
+ changes.componentPropertyDefinitions = definitions
35
+ const assignments = mapValues(node.componentPropertyAssignments, (value, id) =>
36
+ swapProperties.has(id) ? mapped(value) : value
37
+ )
38
+ if (!isEqual(assignments, node.componentPropertyAssignments))
39
+ changes.componentPropertyAssignments = assignments
40
+ for (const field of ['connectorStart', 'connectorEnd'] as const) {
41
+ const endpoint = node[field]
42
+ if (endpoint?.nodeId && mapped(endpoint.nodeId) !== endpoint.nodeId)
43
+ changes[field] = { ...endpoint, nodeId: mapped(endpoint.nodeId) }
44
+ }
45
+ return changes
46
+ }
47
+
48
+ /**
49
+ * Points copied layers at one another, as `prepareNodeTransfer` does for a paste: an instance's
50
+ * main component, the layers its overrides change and the components they swap in, the
51
+ * components an instance-swap property defaults to or is set to, and the layers a connector's
52
+ * ends follow. `mappedIds` maps each copied layer to its copy, such as a library copy or a design
53
+ * JSX render folded into the layers it replaces; a reference outside the copy stays as it is, and
54
+ * so do variable IDs, since the copies keep their source's variables.
55
+ */
56
+ export function remapCopiedReferences(
57
+ graph: SceneGraph,
58
+ mappedIds: ReadonlyMap<string, string>
59
+ ): void {
60
+ const mapped = (id: string) => mappedIds.get(id) ?? id
61
+ const copies = [...mappedIds.values()].flatMap((id) => graph.getNode(id) ?? [])
62
+ const copiedSwaps = copies.flatMap((node) => swapPropertyIds(node.componentPropertyDefinitions))
63
+ for (const node of copies) {
64
+ // An instance's swap property may be defined on a component the copy leaves behind.
65
+ const swapProperties = new Set([
66
+ ...copiedSwaps,
67
+ ...swapPropertyIds(componentPropertyDefinitions(graph, node))
68
+ ])
69
+ const changes = remappedReferences(node, mapped, swapProperties)
70
+ if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes)
71
+ }
72
+ }
@@ -4,3 +4,4 @@ export { applyGraphTransfer, type GraphTransferPlan } from './apply'
4
4
  export { prepareGraphTransfer, type GraphTransferInput } from './plan'
5
5
  export { prepareNodeTransfer, type NodeTransferReferences } from './nodes'
6
6
  export { prepareVariableTransfer, type VariableTransferReferences } from './variables'
7
+ export { remapCopiedReferences } from './copied'
@@ -104,6 +104,16 @@ export function prepareNodeTransfer(
104
104
  node: (id) => refs.nodes.get(id) ?? id
105
105
  })
106
106
  }
107
+ // A reaction's destination keeps its name when the transfer leaves it behind; a copied frame
108
+ // with a copy of its destination points at the copy, as Figma's paste does.
109
+ result.prototypeReactions = (result.prototypeReactions ?? []).map((reaction) => ({
110
+ ...reaction,
111
+ actions: reaction.actions.map((action) =>
112
+ action.type === 'NODE' && action.nodeId
113
+ ? { ...action, nodeId: refs.nodes.get(action.nodeId) ?? action.nodeId }
114
+ : action
115
+ )
116
+ }))
107
117
  // A connector keeps the layers it was attached to when they are left behind; the end becomes
108
118
  // free where nothing by that ID exists, as a pasted Figma connector does.
109
119
  for (const field of ['connectorStart', 'connectorEnd'] as const) {
package/src/types.ts CHANGED
@@ -64,12 +64,31 @@ export interface FigmaSourcePayload {
64
64
  uniformScaleFactor: number | null
65
65
  }
66
66
 
67
+ /**
68
+ * An edge as its draw.io file wrote it: the style pairs and geometry the author set. A connector
69
+ * read from a file keeps them so saving an unedited edge writes back what draw.io had, instead of
70
+ * re-routing it.
71
+ */
72
+ export interface DrawIOEdgeSource {
73
+ /** The style's names and its pairs, in the order the file wrote them. */
74
+ styleNames: string[]
75
+ styleValues: Partial<Record<string, string>>
76
+ /** The label's place along the edge, as the geometry's `x`. */
77
+ labelX: number
78
+ /** Bend points and free ends, in the edge's parent's axes. */
79
+ points: Vector[]
80
+ sourcePoint: Vector | null
81
+ targetPoint: Vector | null
82
+ }
83
+
67
84
  export interface SourceMetadata {
68
85
  format: 'fig' | null
69
86
  id: string | null
70
87
  orderKey: string | null
71
88
  editedFields: string[]
72
89
  fig: FigmaSourcePayload
90
+ /** The edge cell a connector was read from, kept until the connector is edited. */
91
+ drawio?: DrawIOEdgeSource | null
73
92
  }
74
93
 
75
94
  export type HandleMirroring = 'NONE' | 'ANGLE' | 'ANGLE_AND_LENGTH'
@@ -217,7 +236,7 @@ export interface Fill {
217
236
  imageTransform?: GradientTransform
218
237
  /** Clockwise turn of a Fill, Fit or Tile image, in multiples of 90°; a crop holds its own. */
219
238
  imageRotation?: number
220
- /** An image's adjustments, kept and saved; the canvas does not draw them. */
239
+ /** An image's adjustments, in Figma's plugin API units. */
221
240
  filters?: ImageFilters
222
241
  sourceNodeId?: string
223
242
  scale?: number
@@ -245,6 +264,7 @@ export type StrokeJoin = 'MITER' | 'BEVEL' | 'ROUND'
245
264
  export type SharedStyleType = 'FILL' | 'TEXT' | 'EFFECT' | 'GRID'
246
265
  export type SharedStyleKind = 'fill' | 'stroke' | 'text' | 'effect' | 'grid'
247
266
  export type MaskType = 'ALPHA' | 'VECTOR' | 'LUMINANCE'
267
+ export type BooleanOperation = 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE'
248
268
 
249
269
  export interface LayoutGrid {
250
270
  visible?: boolean
@@ -278,7 +298,7 @@ export interface Stroke extends Fill {
278
298
  dashPattern?: number[]
279
299
  }
280
300
 
281
- export type Effect = ShadowEffect | ShaderEffect
301
+ export type Effect = ShadowEffect | ShaderEffect | NoiseEffect | TextureEffect
282
302
 
283
303
  export interface ShadowEffect {
284
304
  type: 'DROP_SHADOW' | 'INNER_SHADOW' | 'LAYER_BLUR' | 'BACKGROUND_BLUR' | 'FOREGROUND_BLUR'
@@ -291,6 +311,50 @@ export interface ShadowEffect {
291
311
  showShadowBehindNode?: boolean
292
312
  }
293
313
 
314
+ /**
315
+ * A Noise effect's grain, in Figma's plugin terms: `MONOTONE` grains in the effect's colour,
316
+ * `DUOTONE` in it and `secondaryColor`, `MULTITONE` in many colours at `opacity`. `noiseSize` is a
317
+ * grain's size in px, which Figma keeps within 0 to 100, and `density` how much of the layer the
318
+ * grains cover, 0 to 1. `seed` picks the pattern; Figma's plugin API neither shows nor sets it.
319
+ */
320
+ export interface NoiseEffectParams {
321
+ noiseType: NoiseType
322
+ noiseSize: Vector
323
+ density: number
324
+ secondaryColor?: Color
325
+ opacity?: number
326
+ seed?: number
327
+ }
328
+
329
+ /**
330
+ * A Texture effect's grain, which roughens the layer's edges: `noiseSize` is the grain's size in
331
+ * px, within 0 to 100 as Figma keeps it, and `clipToShape` keeps the roughened layer inside its
332
+ * own outline. The effect's `radius` is how far the grain moves the edges. `.fig` files name the
333
+ * effect `GRAIN`.
334
+ */
335
+ export interface TextureEffectParams {
336
+ noiseSize: Vector
337
+ clipToShape: boolean
338
+ seed?: number
339
+ }
340
+
341
+ export interface NoiseEffect {
342
+ type: 'NOISE'
343
+ color: Color
344
+ visible: boolean
345
+ blendMode?: BlendMode
346
+ noise: NoiseEffectParams
347
+ }
348
+
349
+ export interface TextureEffect {
350
+ type: 'TEXTURE'
351
+ color: Color
352
+ radius: number
353
+ visible: boolean
354
+ blendMode?: BlendMode
355
+ texture: TextureEffectParams
356
+ }
357
+
294
358
  /** A layer post-processed by shader code the user wrote; the layer is the `pencil:object` sample. */
295
359
  export interface ShaderEffect {
296
360
  type: 'SHADER'
@@ -666,17 +730,47 @@ export interface CommentThread {
666
730
  replies: CommentReply[]
667
731
  }
668
732
 
669
- /** How a prototype reaction starts, as `.fig` keeps `PrototypeEvent.interactionType`. */
670
- export type PrototypeTriggerType = 'ON_CLICK' | 'AFTER_TIMEOUT'
733
+ /**
734
+ * How a prototype reaction starts, as `.fig` keeps `PrototypeEvent.interactionType`: a click, the
735
+ * pointer held over the layer (`ON_HOVER`, While hovering) or pressed on it (`ON_PRESS`, While
736
+ * pressing), which undo themselves when it leaves or lifts, the pointer entering or leaving it,
737
+ * or a delay.
738
+ */
739
+ export type PrototypeTriggerType =
740
+ | 'ON_CLICK'
741
+ | 'ON_HOVER'
742
+ | 'ON_PRESS'
743
+ | 'MOUSE_ENTER'
744
+ | 'MOUSE_LEAVE'
745
+ | 'AFTER_TIMEOUT'
671
746
 
672
747
  /** What a reaction does, as `.fig` keeps `ConnectionType` on `PrototypeAction`. */
673
748
  export type PrototypeActionType = 'NODE' | 'BACK' | 'CLOSE'
674
749
 
675
- /** Whether an action moves to a frame or opens it over the screen, as `NavigationType`. */
676
- export type PrototypeNavigationType = 'NAVIGATE' | 'OVERLAY'
750
+ /**
751
+ * What a node action does with its node, as `NavigationType`: move to a frame, open it over the
752
+ * screen, show another variant of the instance's set (Figma's Change to, `SWAP_STATE` in `.fig`),
753
+ * or scroll the layer into view.
754
+ */
755
+ export type PrototypeNavigationType = 'NAVIGATE' | 'OVERLAY' | 'CHANGE_TO' | 'SCROLL_TO'
756
+
757
+ /** Which way a frame's content scrolls in play, as Figma's `overflowDirection`. */
758
+ export type OverflowDirection = 'NONE' | 'HORIZONTAL' | 'VERTICAL' | 'BOTH'
677
759
 
678
- /** How a reaction animates between frames, as `TransitionType` keeps them. */
679
- export type PrototypeTransitionType = 'INSTANT' | 'DISSOLVE'
760
+ /**
761
+ * How a layer moves when its parent frame scrolls in play: with the content, fixed in the frame,
762
+ * or scrolled until it reaches the frame's top/left edge and then stuck there.
763
+ */
764
+ export type ScrollBehavior = 'SCROLLS' | 'FIXED' | 'STICKY_SCROLLS'
765
+
766
+ /**
767
+ * How a reaction animates between frames, as `TransitionType` keeps them: a cut, a crossfade, or
768
+ * Smart animate, which moves the layers two frames share by name and fades the rest.
769
+ */
770
+ export type PrototypeTransitionType = 'INSTANT' | 'DISSOLVE' | 'SMART_ANIMATE'
771
+
772
+ /** How a transition eases, by the names Figma's plugin API gives its presets. */
773
+ export type PrototypeEasingType = 'LINEAR' | 'EASE_IN' | 'EASE_OUT' | 'EASE_IN_AND_OUT'
680
774
 
681
775
  /** Where an overlay sits over the screen, as `OverlayPositionType`. */
682
776
  export type PrototypeOverlayPositionType =
@@ -688,16 +782,21 @@ export type PrototypeOverlayPositionType =
688
782
  | 'BOTTOM_CENTER'
689
783
  | 'BOTTOM_RIGHT'
690
784
 
691
- /** How a reaction animates, and how long it takes in seconds. */
785
+ /** How a reaction animates, how long it takes in seconds, and how it eases. */
692
786
  export interface PrototypeTransition {
693
787
  type: PrototypeTransitionType
694
788
  duration: number
789
+ /** Absent eases as Figma's default does, `DEFAULT_PROTOTYPE_EASING`. */
790
+ easing?: PrototypeEasingType
695
791
  }
696
792
 
697
- /** A reaction's move to another frame, or the overlay it opens. */
793
+ /** A reaction's move to another frame, the overlay it opens, the variant or layer it shows. */
698
794
  export interface PrototypeNodeAction {
699
795
  type: 'NODE'
700
- /** The frame the action shows; `null` keeps the connection when its target is gone. */
796
+ /**
797
+ * The frame the action shows, the variant a Change to shows, or the layer a Scroll to brings
798
+ * into view; `null` keeps the connection when its target is gone.
799
+ */
701
800
  nodeId: string | null
702
801
  navigation: PrototypeNavigationType
703
802
  transition: PrototypeTransition
@@ -717,10 +816,10 @@ export interface PrototypeCloseAction {
717
816
 
718
817
  export type PrototypeAction = PrototypeNodeAction | PrototypeBackAction | PrototypeCloseAction
719
818
 
720
- /** What starts a reaction, and after how long an AFTER_TIMEOUT does. */
819
+ /** What starts a reaction, and after how long an AFTER_TIMEOUT or mouse enter or leave does. */
721
820
  export interface PrototypeTrigger {
722
821
  type: PrototypeTriggerType
723
- /** AFTER_TIMEOUT only: the seconds before the reaction runs. */
822
+ /** AFTER_TIMEOUT, MOUSE_ENTER and MOUSE_LEAVE: the seconds before the reaction runs. */
724
823
  delay: number
725
824
  }
726
825
 
@@ -774,6 +873,10 @@ export interface SceneNode {
774
873
  visible: boolean
775
874
  locked: boolean
776
875
  clipsContent: boolean
876
+ /** Which way the frame's content scrolls in play; a nested frame scrolls only while it clips. */
877
+ overflowDirection: OverflowDirection
878
+ /** How this layer moves while its parent frame scrolls in play. */
879
+ scrollBehavior: ScrollBehavior
777
880
 
778
881
  blendMode: BlendMode
779
882
 
@@ -850,7 +953,7 @@ export interface SceneNode {
850
953
 
851
954
  vectorNetwork: VectorNetwork | null
852
955
  handleMirroring: HandleMirroring
853
- booleanOperation: 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE' | undefined
956
+ booleanOperation: BooleanOperation | undefined
854
957
  fillGeometry: GeometryPath[]
855
958
  strokeGeometry: GeometryPath[]
856
959
 
package/src/undo.ts CHANGED
@@ -214,6 +214,17 @@ export class UndoManager {
214
214
  return history
215
215
  }
216
216
 
217
+ /**
218
+ * Adds a step to a history kept aside while another graph is open, and returns it. The step's
219
+ * marks stay in its own graph, which is not the open one, and nothing is announced: the open
220
+ * document's history does not change.
221
+ */
222
+ pushIntoParked(history: UndoHistory, entry: UndoEntry): UndoHistory {
223
+ const undo = [...history.undo, entry]
224
+ const overflow = Number.isFinite(this.limit) && this.limit > 0 ? undo.length - this.limit : 0
225
+ return { undo: overflow > 0 ? undo.slice(overflow) : undo, redo: [] }
226
+ }
227
+
217
228
  /** Makes `history`, recorded on the graph now open, the committed one. */
218
229
  adoptHistory(history: UndoHistory | undefined): void {
219
230
  this.undoStack = history ? [...history.undo] : []
@@ -289,10 +289,7 @@ export function modesDefaultFirst(collection: VariableCollection): VariableColle
289
289
  * The collection's own array keeps the file's saved order, which an export writes back as it was.
290
290
  */
291
291
  export function modesInListedOrder(collection: VariableCollection): VariableCollectionMode[] {
292
- return sortBy(collection.modes, [
293
- (mode) => !mode.sortPosition,
294
- (mode) => mode.sortPosition ?? ''
295
- ])
292
+ return sortBy(collection.modes, [(mode) => !mode.sortPosition, (mode) => mode.sortPosition ?? ''])
296
293
  }
297
294
 
298
295
  export function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {
@@ -0,0 +1,89 @@
1
+ import { pick } from 'es-toolkit'
2
+
3
+ import { MAIN_COMPONENT_FIELDS } from '../fields'
4
+ import type { SceneGraph } from '../graph'
5
+ import { createDefaultNode } from '../node-defaults'
6
+ import { readPluginData, withPluginData } from '../plugin-data/field'
7
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
8
+ import type { SceneNode } from '../types'
9
+
10
+ /** `node`'s plugin data marking it as the stand-in for `mainId`, or unmarked for null. */
11
+ export function withStandIn(node: Pick<SceneNode, 'pluginData'>, mainId: string | null) {
12
+ return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn, mainId ?? undefined)
13
+ }
14
+
15
+ /** What a copied main component or component set clears to become a plain frame. */
16
+ function plainFrameFields(): Partial<SceneNode> {
17
+ const frame = createDefaultNode(() => '', 'FRAME')
18
+ return { type: 'FRAME', ...pick(frame, MAIN_COMPONENT_FIELDS) }
19
+ }
20
+
21
+ /**
22
+ * A copied main component inside the copy shows the component it copied, as an instance in its
23
+ * place, so copies never add main components; one that cannot become an instance, such as one
24
+ * that would hold itself, stays a plain frame.
25
+ */
26
+ function instanceInPlace(graph: SceneGraph, component: SceneNode, copy: SceneNode, index: number) {
27
+ const parentId = copy.parentId
28
+ const instance = parentId
29
+ ? graph.createInstance(component.id, parentId, { name: copy.name, x: copy.x, y: copy.y })
30
+ : null
31
+ if (!instance || !parentId) {
32
+ graph.updateNode(copy.id, plainFrameFields())
33
+ return
34
+ }
35
+ graph.updateNode(instance.id, { pluginData: withStandIn(instance, component.id) })
36
+ graph.insertChildAt(instance.id, parentId, index)
37
+ graph.deleteNode(copy.id)
38
+ }
39
+
40
+ /**
41
+ * Copies `source` under `parentId` as a version: its root is never a main component or a
42
+ * component set (it becomes a plain frame), and the main components inside it are instances of
43
+ * the components they copy. Instances stay instances. Each layer standing in for a main
44
+ * component or set is marked with its id (`variationStandIn`), which is how a pick knows it. Right after a copy the two trees have the
45
+ * same shape, so each copied layer is found at its source's place.
46
+ */
47
+ export function copyVersion(
48
+ graph: SceneGraph,
49
+ sourceId: string,
50
+ parentId: string,
51
+ overrides: Partial<SceneNode>
52
+ ): SceneNode | null {
53
+ const copy = graph.cloneTree(sourceId, parentId, overrides)
54
+ if (!copy) return null
55
+ if (copy.type === 'COMPONENT' || copy.type === 'COMPONENT_SET') {
56
+ graph.updateNode(copy.id, plainFrameFields())
57
+ }
58
+ const pending: Array<[string, string]> = [[sourceId, copy.id]]
59
+ // Pairs are appended while the list is walked, parents before their children.
60
+ for (const [fromId, toId] of pending) {
61
+ const from = graph.getNode(fromId)
62
+ const to = graph.getNode(toId)
63
+ // An instance's layers are its component's copies, kept by sync.
64
+ if (!from || !to || from.type === 'INSTANCE') continue
65
+ const copied = [...to.childIds]
66
+ from.childIds.forEach((childId, index) => {
67
+ const child = graph.getNode(childId)
68
+ const childCopy = graph.getNode(copied[index] ?? '')
69
+ if (!child || !childCopy) return
70
+ // Copying a version copies its stand-ins as stand-ins, which a plain copy drops.
71
+ const mark = readPluginData(child.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn)
72
+ if (mark !== undefined) {
73
+ graph.updateNode(childCopy.id, { pluginData: withStandIn(childCopy, mark) })
74
+ }
75
+ if (child.type === 'COMPONENT') {
76
+ instanceInPlace(graph, child, childCopy, index)
77
+ return
78
+ }
79
+ if (child.type === 'COMPONENT_SET') {
80
+ graph.updateNode(childCopy.id, {
81
+ ...plainFrameFields(),
82
+ pluginData: withStandIn(childCopy, child.id)
83
+ })
84
+ }
85
+ pending.push([childId, childCopy.id])
86
+ })
87
+ }
88
+ return graph.getNode(copy.id) ?? null
89
+ }
@@ -0,0 +1,5 @@
1
+ export * from './copy'
2
+ export * from './match'
3
+ export * from './model'
4
+ export * from './pick'
5
+ export * from './schema'
@@ -0,0 +1,100 @@
1
+ import { groupBy } from 'es-toolkit'
2
+
3
+ import type { SceneGraph } from '../graph'
4
+ import { readPluginData } from '../plugin-data/field'
5
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
6
+ import type { SceneNode } from '../types'
7
+
8
+ /**
9
+ * Which layer of the original each layer of a version stands for, as version id → original id.
10
+ * The roots always pair. Under each paired parent, children pair first by type and name in
11
+ * order, then the ones left by type in order, so a renamed or moved layer still finds its
12
+ * counterpart; layers of different types never pair, except a main component's or component
13
+ * set's stand-in, marked as such (`standsIn`), which pairs with it; a marked layer that does
14
+ * not find its component here pairs as any other layer. Only layers paired with their own type are
15
+ * searched further. Nothing is stored: a version is a copy of the original, so its layers mostly
16
+ * keep the names and types they were copied with.
17
+ */
18
+ export function matchLayers(
19
+ graph: SceneGraph,
20
+ originalId: string,
21
+ versionId: string
22
+ ): Map<string, string> {
23
+ const pairs = new Map<string, string>([[versionId, originalId]])
24
+ const pending: Array<[string, string]> = [[versionId, originalId]]
25
+ for (let next = pending.pop(); next; next = pending.pop()) {
26
+ const [versionParent, originalParent] = next
27
+ for (const [version, original] of matchChildren(
28
+ graph.getChildren(versionParent),
29
+ graph.getChildren(originalParent)
30
+ )) {
31
+ pairs.set(version.id, original.id)
32
+ if (version.type === original.type) pending.push([version.id, original.id])
33
+ }
34
+ }
35
+ return pairs
36
+ }
37
+
38
+ /**
39
+ * Whether `version` is what a copy holds in place of the main component or component set
40
+ * `original`, as its mark says (`copyVersion`): an instance of the component, or a frame of
41
+ * instances of the set's variants. A name proves nothing, since an instance may share it.
42
+ */
43
+ export function standsIn(version: SceneNode, original: SceneNode): boolean {
44
+ if (original.type !== 'COMPONENT' && original.type !== 'COMPONENT_SET') return false
45
+ return readPluginData(version.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn) === original.id
46
+ }
47
+
48
+ /** Takes the next free layer of a bucket, skipping ones another bucket already gave away. */
49
+ function takeNext(
50
+ bucket: readonly SceneNode[] | undefined,
51
+ cursors: Map<readonly SceneNode[], number>,
52
+ taken: Set<string>
53
+ ): SceneNode | undefined {
54
+ if (!bucket) return undefined
55
+ let index = cursors.get(bucket) ?? 0
56
+ while (index < bucket.length && taken.has(bucket[index].id)) index++
57
+ cursors.set(bucket, Math.min(index + 1, bucket.length))
58
+ if (index === bucket.length) return undefined
59
+ const found = bucket[index]
60
+ taken.add(found.id)
61
+ return found
62
+ }
63
+
64
+ /**
65
+ * Pairs in one pass over each list: stand-ins for main components and sets first, then through
66
+ * buckets by type and name and by type alone.
67
+ */
68
+ function matchChildren(
69
+ versions: SceneNode[],
70
+ originals: SceneNode[]
71
+ ): Array<[SceneNode, SceneNode]> {
72
+ const mains = new Map(
73
+ originals
74
+ .filter((node) => node.type === 'COMPONENT' || node.type === 'COMPONENT_SET')
75
+ .map((node) => [node.id, node])
76
+ )
77
+ const byName = groupBy(originals, (node) => `${node.type}\u0000${node.name}`)
78
+ const byType = groupBy(originals, (node) => node.type)
79
+ const cursors = new Map<readonly SceneNode[], number>()
80
+ const taken = new Set<string>()
81
+ const pairs: Array<[SceneNode, SceneNode]> = []
82
+ const left: SceneNode[] = []
83
+ for (const version of versions) {
84
+ const mainId = readPluginData(version.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn)
85
+ const main = mainId === undefined ? undefined : mains.get(mainId)
86
+ if (main && !taken.has(main.id) && standsIn(version, main)) {
87
+ taken.add(main.id)
88
+ pairs.push([version, main])
89
+ continue
90
+ }
91
+ const named = takeNext(byName[`${version.type}\u0000${version.name}`], cursors, taken)
92
+ if (named) pairs.push([version, named])
93
+ else left.push(version)
94
+ }
95
+ for (const version of left) {
96
+ const typed = takeNext(byType[version.type], cursors, taken)
97
+ if (typed) pairs.push([version, typed])
98
+ }
99
+ return pairs
100
+ }
@@ -0,0 +1,132 @@
1
+ import type { SceneGraph } from '../graph'
2
+ import { readPluginData, withPluginData } from '../plugin-data/field'
3
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
4
+ import type { SceneNode } from '../types'
5
+ import type { VariationSet } from './schema'
6
+
7
+ /** A set of variations as the document holds it now. */
8
+ export interface VariationSetView {
9
+ /** The section holding the versions, which carries the set's mark. */
10
+ section: SceneNode
11
+ set: VariationSet
12
+ /** The layer the versions are variations of, or null when it is gone. */
13
+ original: SceneNode | null
14
+ /** The section's children, in their order. */
15
+ versions: SceneNode[]
16
+ }
17
+
18
+ /** The set a section marks, or null when the layer is not a section or carries none. */
19
+ export function readVariationSet(
20
+ node: Pick<SceneNode, 'type' | 'pluginData'>
21
+ ): VariationSet | null {
22
+ if (node.type !== 'SECTION') return null
23
+ return readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationSet) ?? null
24
+ }
25
+
26
+ /** The section's plugin data with its mark replaced by `set`, or removed when it is null. */
27
+ export function withVariationSet(
28
+ node: Pick<SceneNode, 'pluginData'>,
29
+ set: VariationSet | null
30
+ ): SceneNode['pluginData'] {
31
+ return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationSet, set ?? undefined)
32
+ }
33
+
34
+ function viewOf(graph: SceneGraph, section: SceneNode): VariationSetView | null {
35
+ const set = readVariationSet(section)
36
+ if (!set) return null
37
+ const original = graph.getNode(set.originalId) ?? null
38
+ const usable =
39
+ original !== null &&
40
+ original.type !== 'CANVAS' &&
41
+ graph.pageOf(original.id) !== null &&
42
+ !graph.isDescendant(original.id, section.id)
43
+ return {
44
+ section,
45
+ set,
46
+ original: usable ? original : null,
47
+ versions: graph.getChildren(section.id)
48
+ }
49
+ }
50
+
51
+ /** The set a layer belongs to: the marked section itself, one of its versions, or a layer inside one. */
52
+ export function variationSetOf(graph: SceneGraph, nodeId: string): VariationSetView | null {
53
+ const section = graph.closest(nodeId, (node) => readVariationSet(node) !== null)
54
+ return section ? viewOf(graph, section) : null
55
+ }
56
+
57
+ /** The version of `view` that holds `nodeId`, or the version itself; null outside its versions. */
58
+ export function versionOf(
59
+ graph: SceneGraph,
60
+ view: VariationSetView,
61
+ nodeId: string
62
+ ): SceneNode | null {
63
+ return graph.closest(nodeId, (node) => node.parentId === view.section.id) ?? null
64
+ }
65
+
66
+ /** The sets on a page; they sit on the page itself, beside the layers they vary. */
67
+ export function variationSetsOnPage(graph: SceneGraph, pageId: string): VariationSetView[] {
68
+ return graph.getChildren(pageId).flatMap((node) => viewOf(graph, node) ?? [])
69
+ }
70
+
71
+ /** The set whose versions vary `originalId`, if there is one on its page. */
72
+ export function variationSetForOriginal(
73
+ graph: SceneGraph,
74
+ originalId: string
75
+ ): VariationSetView | null {
76
+ const pageId = graph.pageOf(originalId)
77
+ if (!pageId) return null
78
+ return variationSetsOnPage(graph, pageId).find((view) => view.original?.id === originalId) ?? null
79
+ }
80
+
81
+ const FNV_OFFSET = 0x811c9dc5
82
+ const FNV_PRIME = 0x01000193
83
+
84
+ function hashText(hash: number, text: string): number {
85
+ let next = hash
86
+ for (let index = 0; index < text.length; index++) {
87
+ next = Math.imul(next ^ text.charCodeAt(index), FNV_PRIME) >>> 0
88
+ }
89
+ return next
90
+ }
91
+
92
+ /**
93
+ * A short hash of what a layer and its layers look like at a glance: their types, names, boxes,
94
+ * visibility, opacity, text, and how many fills and children they have. The layer's own position
95
+ * is left out, so moving it, or its auto layout parent placing it, is no change. It walks the
96
+ * whole subtree, so callers compute it when they show it rather than on every edit; it changes
97
+ * with most edits, which is all a "changed since" notice needs, and is not a full comparison.
98
+ */
99
+ export function variationFingerprint(graph: SceneGraph, nodeId: string): string {
100
+ let hash = FNV_OFFSET
101
+ const pending = [nodeId]
102
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
103
+ const node = graph.getNode(id)
104
+ if (!node) continue
105
+ hash = hashText(
106
+ hash,
107
+ [
108
+ node.type,
109
+ node.name,
110
+ id === nodeId ? '' : node.x,
111
+ id === nodeId ? '' : node.y,
112
+ node.width,
113
+ node.height,
114
+ node.rotation,
115
+ node.visible,
116
+ node.opacity,
117
+ node.text,
118
+ node.fills.length,
119
+ node.childIds.length
120
+ ].join('\u0000')
121
+ )
122
+ pending.push(...node.childIds)
123
+ }
124
+ return hash.toString(16).padStart(8, '0')
125
+ }
126
+
127
+ /** Whether the set's original looks different from when its versions were made. */
128
+ export function originalChangedSince(graph: SceneGraph, view: VariationSetView): boolean {
129
+ const { original, set } = view
130
+ if (!original || set.originalFingerprint === undefined) return false
131
+ return variationFingerprint(graph, original.id) !== set.originalFingerprint
132
+ }