@open-pencil/scene-graph 0.17.0-beta.2 → 0.17.0-beta.3

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 (244) hide show
  1. package/dist/asset-keys.d.ts +11 -1
  2. package/dist/asset-keys.d.ts.map +1 -1
  3. package/dist/asset-keys.js +16 -1
  4. package/dist/asset-keys.js.map +1 -1
  5. package/dist/checkpoint.d.ts +3 -3
  6. package/dist/checkpoint.d.ts.map +1 -1
  7. package/dist/checkpoint.js +53 -23
  8. package/dist/checkpoint.js.map +1 -1
  9. package/dist/components/index.d.ts +2 -2
  10. package/dist/components/index.js +2 -2
  11. package/dist/components/properties.d.ts +6 -1
  12. package/dist/components/properties.d.ts.map +1 -1
  13. package/dist/components/properties.js +9 -1
  14. package/dist/components/properties.js.map +1 -1
  15. package/dist/constants.d.ts +6 -1
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +17 -1
  18. package/dist/constants.js.map +1 -1
  19. package/dist/constraints.d.ts +25 -0
  20. package/dist/constraints.d.ts.map +1 -0
  21. package/dist/constraints.js +51 -0
  22. package/dist/constraints.js.map +1 -0
  23. package/dist/coordinate.d.ts +64 -1
  24. package/dist/coordinate.d.ts.map +1 -1
  25. package/dist/coordinate.js +217 -1
  26. package/dist/coordinate.js.map +1 -1
  27. package/dist/copy.d.ts.map +1 -1
  28. package/dist/copy.js +8 -1
  29. package/dist/copy.js.map +1 -1
  30. package/dist/css/index.d.ts +3 -2
  31. package/dist/css/index.js +3 -2
  32. package/dist/css/text.d.ts +28 -1
  33. package/dist/css/text.d.ts.map +1 -1
  34. package/dist/css/text.js +84 -17
  35. package/dist/css/text.js.map +1 -1
  36. package/dist/css/transform.d.ts +34 -0
  37. package/dist/css/transform.d.ts.map +1 -0
  38. package/dist/css/transform.js +320 -0
  39. package/dist/css/transform.js.map +1 -0
  40. package/dist/css/values.d.ts.map +1 -1
  41. package/dist/css/values.js +11 -1
  42. package/dist/css/values.js.map +1 -1
  43. package/dist/figjam/palette.d.ts +1 -1
  44. package/dist/figjam/palette.js +1 -1
  45. package/dist/figjam/palette.js.map +1 -1
  46. package/dist/geometry.d.ts +8 -1
  47. package/dist/geometry.d.ts.map +1 -1
  48. package/dist/geometry.js +19 -8
  49. package/dist/geometry.js.map +1 -1
  50. package/dist/graph.d.ts +48 -12
  51. package/dist/graph.d.ts.map +1 -1
  52. package/dist/graph.js +110 -24
  53. package/dist/graph.js.map +1 -1
  54. package/dist/group-bounds.d.ts +14 -4
  55. package/dist/group-bounds.d.ts.map +1 -1
  56. package/dist/group-bounds.js +76 -11
  57. package/dist/group-bounds.js.map +1 -1
  58. package/dist/hit-test.d.ts +5 -0
  59. package/dist/hit-test.d.ts.map +1 -1
  60. package/dist/hit-test.js +18 -12
  61. package/dist/hit-test.js.map +1 -1
  62. package/dist/images.d.ts +27 -3
  63. package/dist/images.d.ts.map +1 -1
  64. package/dist/images.js +57 -3
  65. package/dist/images.js.map +1 -1
  66. package/dist/index.d.ts +2 -2
  67. package/dist/instances/index.d.ts.map +1 -1
  68. package/dist/instances/index.js +3 -0
  69. package/dist/instances/index.js.map +1 -1
  70. package/dist/instances/placement.js +55 -12
  71. package/dist/instances/placement.js.map +1 -1
  72. package/dist/instances/swap.js +3 -0
  73. package/dist/instances/swap.js.map +1 -1
  74. package/dist/instances/sync.js +34 -13
  75. package/dist/instances/sync.js.map +1 -1
  76. package/dist/node-defaults.d.ts.map +1 -1
  77. package/dist/node-defaults.js +1 -0
  78. package/dist/node-defaults.js.map +1 -1
  79. package/dist/order-keys.d.ts +3 -1
  80. package/dist/order-keys.d.ts.map +1 -1
  81. package/dist/order-keys.js +6 -1
  82. package/dist/order-keys.js.map +1 -1
  83. package/dist/plugin-data/fields.d.ts +2 -0
  84. package/dist/plugin-data/fields.d.ts.map +1 -1
  85. package/dist/plugin-data/fields.js +2 -0
  86. package/dist/plugin-data/fields.js.map +1 -1
  87. package/dist/resize.d.ts +22 -11
  88. package/dist/resize.d.ts.map +1 -1
  89. package/dist/resize.js +227 -93
  90. package/dist/resize.js.map +1 -1
  91. package/dist/scaling/index.d.ts +3 -2
  92. package/dist/scaling/index.js +3 -2
  93. package/dist/scaling/node.d.ts.map +1 -1
  94. package/dist/scaling/node.js +5 -1
  95. package/dist/scaling/node.js.map +1 -1
  96. package/dist/scaling/snapshot.d.ts +25 -0
  97. package/dist/scaling/snapshot.d.ts.map +1 -0
  98. package/dist/scaling/snapshot.js +39 -0
  99. package/dist/scaling/snapshot.js.map +1 -0
  100. package/dist/scaling/tree.d.ts +24 -2
  101. package/dist/scaling/tree.d.ts.map +1 -1
  102. package/dist/scaling/tree.js +37 -14
  103. package/dist/scaling/tree.js.map +1 -1
  104. package/dist/shaders/model.d.ts.map +1 -1
  105. package/dist/shaders/model.js +2 -1
  106. package/dist/shaders/model.js.map +1 -1
  107. package/dist/shared-styles.d.ts +3 -1
  108. package/dist/shared-styles.d.ts.map +1 -1
  109. package/dist/shared-styles.js +7 -1
  110. package/dist/shared-styles.js.map +1 -1
  111. package/dist/slots/authoring.d.ts +3 -2
  112. package/dist/slots/authoring.d.ts.map +1 -1
  113. package/dist/slots/authoring.js +5 -3
  114. package/dist/slots/authoring.js.map +1 -1
  115. package/dist/slots/content.d.ts.map +1 -1
  116. package/dist/slots/content.js +4 -1
  117. package/dist/slots/content.js.map +1 -1
  118. package/dist/text/direction.d.ts +3 -1
  119. package/dist/text/direction.d.ts.map +1 -1
  120. package/dist/text/direction.js +5 -1
  121. package/dist/text/direction.js.map +1 -1
  122. package/dist/transfer/apply.d.ts.map +1 -1
  123. package/dist/transfer/apply.js +1 -6
  124. package/dist/transfer/apply.js.map +1 -1
  125. package/dist/transfer/copied.d.ts +35 -3
  126. package/dist/transfer/copied.d.ts.map +1 -1
  127. package/dist/transfer/copied.js +44 -13
  128. package/dist/transfer/copied.js.map +1 -1
  129. package/dist/transfer/dependencies.d.ts +38 -0
  130. package/dist/transfer/dependencies.d.ts.map +1 -0
  131. package/dist/transfer/dependencies.js +100 -0
  132. package/dist/transfer/dependencies.js.map +1 -0
  133. package/dist/transfer/history.js +0 -1
  134. package/dist/transfer/history.js.map +1 -1
  135. package/dist/transfer/index.d.ts +5 -3
  136. package/dist/transfer/index.js +5 -3
  137. package/dist/transfer/nodes.d.ts.map +1 -1
  138. package/dist/transfer/nodes.js +7 -14
  139. package/dist/transfer/nodes.js.map +1 -1
  140. package/dist/transfer/plan.d.ts +0 -1
  141. package/dist/transfer/plan.js +3 -5
  142. package/dist/transfer/plan.js.map +1 -1
  143. package/dist/transfer/remove.js +5 -2
  144. package/dist/transfer/remove.js.map +1 -1
  145. package/dist/transfer/resources.d.ts +21 -0
  146. package/dist/transfer/resources.d.ts.map +1 -0
  147. package/dist/transfer/resources.js +38 -0
  148. package/dist/transfer/resources.js.map +1 -0
  149. package/dist/transfer/variables.d.ts +8 -1
  150. package/dist/transfer/variables.d.ts.map +1 -1
  151. package/dist/transfer/variables.js +24 -1
  152. package/dist/transfer/variables.js.map +1 -1
  153. package/dist/types.d.ts +47 -2
  154. package/dist/types.d.ts.map +1 -1
  155. package/dist/types.js.map +1 -1
  156. package/dist/undo.d.ts +28 -3
  157. package/dist/undo.d.ts.map +1 -1
  158. package/dist/undo.js +24 -7
  159. package/dist/undo.js.map +1 -1
  160. package/dist/variables/bindings.d.ts +17 -14
  161. package/dist/variables/bindings.d.ts.map +1 -1
  162. package/dist/variables/bindings.js +30 -72
  163. package/dist/variables/bindings.js.map +1 -1
  164. package/dist/variables/fields.d.ts +1 -1
  165. package/dist/variables/fields.js +1 -1
  166. package/dist/variables/fields.js.map +1 -1
  167. package/dist/variables/formula/check.d.ts +1 -1
  168. package/dist/variables/formula/index.d.ts +2 -2
  169. package/dist/variables/formula/index.js +1 -1
  170. package/dist/variables/formula/index.js.map +1 -1
  171. package/dist/variables/formula/print.d.ts +1 -1
  172. package/dist/variables/formula/signatures.js +1 -1
  173. package/dist/variables/index.d.ts +6 -6
  174. package/dist/variables/index.d.ts.map +1 -1
  175. package/dist/variables/index.js +40 -24
  176. package/dist/variables/index.js.map +1 -1
  177. package/dist/variables/paints.d.ts +57 -0
  178. package/dist/variables/paints.d.ts.map +1 -0
  179. package/dist/variables/paints.js +231 -0
  180. package/dist/variables/paints.js.map +1 -0
  181. package/dist/variables/resolution.d.ts +14 -11
  182. package/dist/variables/resolution.d.ts.map +1 -1
  183. package/dist/variables/resolution.js +36 -17
  184. package/dist/variables/resolution.js.map +1 -1
  185. package/dist/variables/subtree.js +17 -0
  186. package/dist/variables/subtree.js.map +1 -0
  187. package/dist/vector-network.d.ts.map +1 -1
  188. package/dist/vector-network.js +2 -8
  189. package/dist/vector-network.js.map +1 -1
  190. package/package.json +8 -1
  191. package/src/asset-keys.ts +20 -0
  192. package/src/checkpoint.ts +87 -32
  193. package/src/components/properties.ts +9 -0
  194. package/src/constants.ts +17 -0
  195. package/src/constraints.ts +60 -0
  196. package/src/coordinate.ts +220 -0
  197. package/src/copy.ts +9 -4
  198. package/src/css/index.ts +17 -1
  199. package/src/css/text.ts +85 -17
  200. package/src/css/transform.ts +266 -0
  201. package/src/css/values.ts +8 -0
  202. package/src/figjam/palette.ts +1 -1
  203. package/src/geometry.ts +20 -9
  204. package/src/graph.ts +128 -42
  205. package/src/group-bounds.ts +84 -17
  206. package/src/hit-test.ts +30 -14
  207. package/src/images.ts +71 -3
  208. package/src/instances/index.ts +4 -0
  209. package/src/instances/placement.ts +63 -10
  210. package/src/instances/swap.ts +5 -0
  211. package/src/instances/sync.ts +48 -21
  212. package/src/node-defaults.ts +1 -0
  213. package/src/order-keys.ts +6 -0
  214. package/src/plugin-data/fields.ts +6 -0
  215. package/src/resize.ts +267 -119
  216. package/src/scaling/index.ts +9 -1
  217. package/src/scaling/node.ts +9 -1
  218. package/src/scaling/snapshot.ts +51 -0
  219. package/src/scaling/tree.ts +63 -19
  220. package/src/shaders/model.ts +2 -3
  221. package/src/shared-styles.ts +8 -0
  222. package/src/slots/authoring.ts +5 -3
  223. package/src/slots/content.ts +4 -1
  224. package/src/text/direction.ts +5 -0
  225. package/src/transfer/apply.ts +1 -13
  226. package/src/transfer/copied.ts +83 -15
  227. package/src/transfer/dependencies.ts +126 -0
  228. package/src/transfer/history.ts +0 -6
  229. package/src/transfer/index.ts +20 -2
  230. package/src/transfer/nodes.ts +10 -26
  231. package/src/transfer/plan.ts +3 -11
  232. package/src/transfer/remove.ts +5 -2
  233. package/src/transfer/resources.ts +54 -0
  234. package/src/transfer/variables.ts +34 -0
  235. package/src/types.ts +55 -1
  236. package/src/undo.ts +48 -10
  237. package/src/variables/bindings.ts +44 -91
  238. package/src/variables/fields.ts +1 -1
  239. package/src/variables/formula/index.ts +3 -1
  240. package/src/variables/index.ts +56 -38
  241. package/src/variables/paints.ts +296 -0
  242. package/src/variables/resolution.ts +39 -28
  243. package/src/variables/subtree.ts +15 -0
  244. package/src/vector-network.ts +4 -2
package/src/resize.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { CONSTRAINT_PARENT_TYPES } from './constraints'
2
+ import { getNodeLocalMatrix } from './coordinate'
1
3
  import {
2
4
  cloneVectorNetwork,
3
5
  copyDerivedGlyphs,
@@ -6,7 +8,10 @@ import {
6
8
  copyStrokes,
7
9
  scaleGeometryPaths
8
10
  } from './copy'
11
+ import { groupFitChanges } from './group-bounds'
9
12
  import { enforcedAspectRatio } from './layout/aspect-ratio'
13
+ import Matrix, { type Mat3 } from './matrix'
14
+ import { FITTED_CONTAINER_TYPES } from './node-defaults'
10
15
  import type { Rect } from './primitives'
11
16
  import type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'
12
17
 
@@ -23,6 +28,8 @@ export type ResizeSnapshot = Pick<
23
28
  | 'y'
24
29
  | 'width'
25
30
  | 'height'
31
+ | 'flipX'
32
+ | 'flipY'
26
33
  | 'vectorNetwork'
27
34
  | 'fillGeometry'
28
35
  | 'strokeGeometry'
@@ -33,38 +40,58 @@ export type ResizeSnapshot = Pick<
33
40
  >
34
41
 
35
42
  interface ResizeGraph {
43
+ readonly rootId: string
36
44
  getNode(id: string): SceneNode | undefined
37
45
  }
38
46
 
39
- const CONSTRAINT_CONTAINER_TYPES = new Set([
40
- 'FRAME',
41
- 'COMPONENT',
42
- 'COMPONENT_SET',
43
- 'INSTANCE',
44
- 'GROUP',
45
- 'BOOLEAN_OPERATION'
47
+ /**
48
+ * Containers whose resize moves their children: the constraint parents, and groups and booleans,
49
+ * which scale theirs.
50
+ */
51
+ const CONSTRAINT_CONTAINER_TYPES = new Set<SceneNode['type']>([
52
+ ...CONSTRAINT_PARENT_TYPES,
53
+ ...FITTED_CONTAINER_TYPES
46
54
  ])
47
55
 
56
+ type AxisPlacement = { position: number; size: number; flipped?: boolean }
57
+
58
+ /** A child's box after its parent resizes, and the axes on which it turned over. */
59
+ export type ConstrainedRect = Rect & { flipsX?: boolean; flipsY?: boolean }
60
+
61
+ /**
62
+ * Where a constraint places a child on one axis when its parent goes from `parentBefore` to
63
+ * `parentAfter`, as Figma does (live-measured, #1060, #1816): Right and Left & right move or grow
64
+ * it by the change in the parent's size rounded to whole pixels, Center by the change in its
65
+ * rounded half, and Scale by the exact ratio. The child's own box is never rounded, so a resize
66
+ * to the same size changes nothing. Left & right that would leave no size keeps the axis as it
67
+ * was; past zero, the child turns over and its box runs back from the edge it was pinned to, as
68
+ * the layer is mirrored in Figma.
69
+ */
48
70
  function constrainedAxis(
49
71
  position: number,
50
72
  size: number,
51
73
  parentBefore: number,
52
74
  parentAfter: number,
53
75
  constraint: ConstraintType
54
- ): { position: number; size: number } {
55
- const delta = parentAfter - parentBefore
76
+ ): AxisPlacement {
77
+ const delta = Math.round(parentAfter) - Math.round(parentBefore)
56
78
  if (constraint === 'MAX') return { position: position + delta, size }
57
- if (constraint === 'CENTER') return { position: position + delta / 2, size }
58
- if (constraint === 'STRETCH') return { position, size: Math.max(1, size + delta) }
79
+ if (constraint === 'CENTER') {
80
+ return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
81
+ }
82
+ if (constraint === 'STRETCH') {
83
+ const stretched = size + delta
84
+ if (stretched === 0) return { position, size }
85
+ if (stretched > 0) return { position, size: stretched }
86
+ return { position: position + stretched, size: -stretched, flipped: true }
87
+ }
59
88
  if (constraint === 'SCALE' && parentBefore > 0) {
60
89
  const scale = parentAfter / parentBefore
61
- return { position: position * scale, size: Math.max(1, size * scale) }
90
+ return { position: position * scale, size: size * scale }
62
91
  }
63
92
  return { position, size }
64
93
  }
65
94
 
66
- type AxisPlacement = { position: number; size: number }
67
-
68
95
  const FLEXIBLE_CONSTRAINTS: ReadonlySet<ConstraintType> = new Set(['STRETCH', 'SCALE'])
69
96
 
70
97
  /** The axis at `size`, keeping the edge or centre its constraint pins where it was placed. */
@@ -108,74 +135,29 @@ function keepLockedRatio(
108
135
  : { x: resizedInPlace(x, y.size * aspectRatio, horizontal), y }
109
136
  }
110
137
 
111
- /** A child's box after its parent resizes, keeping a locked aspect ratio as Figma does. */
112
- export function constrainedChildRect(
113
- child: Rect,
114
- parentBefore: Pick<Rect, 'width' | 'height'>,
115
- parentAfter: Pick<Rect, 'width' | 'height'>,
116
- horizontal: ConstraintType,
117
- vertical: ConstraintType,
118
- aspectRatio: number | null = null
119
- ): Rect {
120
- let x = constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal)
121
- let y = constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical)
122
- ;({ x, y } = keepLockedRatio(x, y, horizontal, vertical, aspectRatio))
123
- return {
124
- x: Math.round(x.position),
125
- y: Math.round(y.position),
126
- width: Math.round(x.size),
127
- height: Math.round(y.size)
128
- }
129
- }
130
-
131
- function layoutConstrainedAxis(
132
- position: number,
133
- size: number,
134
- parentBefore: number,
135
- parentAfter: number,
136
- constraint: ConstraintType
137
- ): { position: number; size: number } {
138
- const delta = Math.round(parentAfter) - Math.round(parentBefore)
139
- if (constraint === 'MAX') return { position: position + delta, size }
140
- if (constraint === 'CENTER') {
141
- return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
142
- }
143
- if (constraint === 'STRETCH') return { position, size: Math.abs(size + delta) }
144
- if (constraint === 'SCALE' && parentBefore > 0) {
145
- const scale = parentAfter / parentBefore
146
- return { position: Math.round(position * scale * 128) / 128, size: size * scale }
147
- }
148
- return { position, size }
149
- }
150
-
151
138
  /**
152
- * Where a child goes when layout resizes its parent, as Figma places a layer that ignores auto
153
- * layout when its Hug frame grows (measured on desktop 126): Right and Left & right move or grow
154
- * it by the change in the parent's size rounded to whole pixels, Center by the change in its
155
- * rounded half, and Scale by the exact ratio, its position to 1/128 of a pixel. Sizes are not
156
- * rounded, so applying a run of changes one at a time lands where applying them at once does.
139
+ * A child's box after its parent resizes, by a handle, `resize()`, layout, or an instance taking
140
+ * another size than its component, keeping a locked aspect ratio as Figma does.
157
141
  */
158
- export function layoutConstrainedChildRect(
142
+ export function constrainedChildRect(
159
143
  child: Rect,
160
144
  parentBefore: Pick<Rect, 'width' | 'height'>,
161
145
  parentAfter: Pick<Rect, 'width' | 'height'>,
162
146
  horizontal: ConstraintType,
163
147
  vertical: ConstraintType,
164
148
  aspectRatio: number | null = null
165
- ): Rect {
166
- const placed = keepLockedRatio(
167
- layoutConstrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),
168
- layoutConstrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),
149
+ ): ConstrainedRect {
150
+ const { x, y } = keepLockedRatio(
151
+ constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),
152
+ constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),
169
153
  horizontal,
170
154
  vertical,
171
155
  aspectRatio
172
156
  )
173
- return {
174
- x: placed.x.position,
175
- y: placed.y.position,
176
- width: placed.x.size,
177
- height: placed.y.size
178
- }
157
+ const rect: ConstrainedRect = { x: x.position, y: y.position, width: x.size, height: y.size }
158
+ if (x.flipped) rect.flipsX = true
159
+ if (y.flipped) rect.flipsY = true
160
+ return rect
179
161
  }
180
162
 
181
163
  export function scaledChildRect(
@@ -186,6 +168,11 @@ export function scaledChildRect(
186
168
  return constrainedChildRect(child, parentBefore, parentAfter, 'SCALE', 'SCALE')
187
169
  }
188
170
 
171
+ /** How much a side grows; a flat side, as a straight line's, has nothing to scale along it. */
172
+ function sideScale(original: number, size: number): number {
173
+ return original > 0 ? size / original : 1
174
+ }
175
+
189
176
  export function scaleVectorNetworkForResize(
190
177
  vectorNetwork: VectorNetwork | null,
191
178
  originalWidth: number,
@@ -195,9 +182,8 @@ export function scaleVectorNetworkForResize(
195
182
  ): VectorNetwork | null {
196
183
  if (!vectorNetwork || (originalWidth <= 0 && originalHeight <= 0)) return null
197
184
 
198
- // A flat side, as a straight line's, has nothing to scale along it.
199
- const scaleX = originalWidth > 0 ? width / originalWidth : 1
200
- const scaleY = originalHeight > 0 ? height / originalHeight : 1
185
+ const scaleX = sideScale(originalWidth, width)
186
+ const scaleY = sideScale(originalHeight, height)
201
187
  if (scaleX === 1 && scaleY === 1) return null
202
188
 
203
189
  return {
@@ -229,6 +215,8 @@ export function createResizeSnapshot(node: SceneNode): ResizeSnapshot {
229
215
  y: node.y,
230
216
  width: node.width,
231
217
  height: node.height,
218
+ flipX: node.flipX,
219
+ flipY: node.flipY,
232
220
  vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,
233
221
  fillGeometry: copyGeometryPaths(node.fillGeometry),
234
222
  strokeGeometry: copyGeometryPaths(node.strokeGeometry),
@@ -289,9 +277,10 @@ export function scaledGeometryChanges(
289
277
  width: number,
290
278
  height: number
291
279
  ): Partial<SceneNode> {
292
- if (origWidth <= 0 || origHeight <= 0) return {}
293
- const sx = width / origWidth
294
- const sy = height / origHeight
280
+ if (origWidth <= 0 && origHeight <= 0) return {}
281
+ // A straight line's other side still scales.
282
+ const sx = sideScale(origWidth, width)
283
+ const sy = sideScale(origHeight, height)
295
284
  if (sx === 1 && sy === 1) return {}
296
285
 
297
286
  const changes: Partial<SceneNode> = {}
@@ -341,6 +330,38 @@ export function scaledGeometryChanges(
341
330
  return changes
342
331
  }
343
332
 
333
+ /** Copies of the geometry a resize may change, as the snapshot held it before the resize. */
334
+ function snapshotGeometry(orig: ResizeSnapshot): Partial<SceneNode> {
335
+ return {
336
+ vectorNetwork: orig.vectorNetwork ? cloneVectorNetwork(orig.vectorNetwork) : null,
337
+ fillGeometry: copyGeometryPaths(orig.fillGeometry),
338
+ strokeGeometry: copyGeometryPaths(orig.strokeGeometry),
339
+ derivedTextGlyphs: copyDerivedGlyphs(orig.derivedTextGlyphs),
340
+ strokes: copyStrokes(orig.strokes),
341
+ textPathBox: orig.textPathBox ? { ...orig.textPathBox } : null
342
+ }
343
+ }
344
+
345
+ /** Whether a box of `size` scales the snapshot's geometry, a flat side left out. */
346
+ function scalesGeometry(orig: ResizeSnapshot, size: Pick<Rect, 'width' | 'height'>): boolean {
347
+ return sideScale(orig.width, size.width) !== 1 || sideScale(orig.height, size.height) !== 1
348
+ }
349
+
350
+ /**
351
+ * A child's geometry for its constrained `box`, scaled from the snapshot. At a box that scales
352
+ * nothing, after an earlier frame of the same drag scaled it, the child takes its snapshot
353
+ * geometry again, as nothing else would undo that scaling.
354
+ */
355
+ function constrainedGeometryChanges(
356
+ orig: ResizeSnapshot,
357
+ current: Pick<SceneNode, 'width' | 'height'>,
358
+ box: Pick<Rect, 'width' | 'height'>
359
+ ): Partial<SceneNode> {
360
+ if (scalesGeometry(orig, box))
361
+ return scaledGeometryChanges(orig, orig.width, orig.height, box.width, box.height)
362
+ return scalesGeometry(orig, current) ? snapshotGeometry(orig) : {}
363
+ }
364
+
344
365
  export function collectResizeDescendants(
345
366
  graph: ResizeGraph,
346
367
  rootId: string
@@ -364,52 +385,179 @@ export function collectResizeDescendants(
364
385
  return snapshots.size > 0 ? snapshots : null
365
386
  }
366
387
 
367
- export function computeConstrainedResizeChanges(
368
- graph: ResizeGraph,
369
- rootId: string,
370
- rootBefore: Pick<Rect, 'width' | 'height'>,
371
- rootAfter: Pick<Rect, 'width' | 'height'>,
388
+ /**
389
+ * The flips a child takes when its constraints place it from `from`, written only where they
390
+ * differ from the ones it has now, so a drag that crosses back undoes its own turn.
391
+ */
392
+ export function flipChanges(
393
+ from: Pick<SceneNode, 'flipX' | 'flipY'>,
394
+ current: Pick<SceneNode, 'flipX' | 'flipY'>,
395
+ flipsX = false,
396
+ flipsY = false
397
+ ): Partial<Pick<SceneNode, 'flipX' | 'flipY'>> {
398
+ const changes: Partial<Pick<SceneNode, 'flipX' | 'flipY'>> = {}
399
+ const flipX = from.flipX !== flipsX
400
+ const flipY = from.flipY !== flipsY
401
+ if (flipX !== current.flipX) changes.flipX = flipX
402
+ if (flipY !== current.flipY) changes.flipY = flipY
403
+ return changes
404
+ }
405
+
406
+ type Size = Pick<Rect, 'width' | 'height'>
407
+
408
+ /** One constrained resize: the graph, where each layer was when it began, and what it changes. */
409
+ interface ResizePlan {
410
+ graph: ResizeGraph
372
411
  originals: ReadonlyMap<string, ResizeSnapshot>
373
- ): Map<string, Partial<SceneNode>> {
374
- const changes = new Map<string, Partial<SceneNode>>()
412
+ changes: Map<string, Partial<SceneNode>>
413
+ }
375
414
 
376
- const compute = (
377
- parentId: string,
378
- parentBefore: Pick<Rect, 'width' | 'height'>,
379
- parentAfter: Pick<Rect, 'width' | 'height'>
380
- ) => {
381
- const parent = graph.getNode(parentId)
382
- if (!parent) return
383
- const scalesChildren = parent.type === 'GROUP' || parent.type === 'BOOLEAN_OPERATION'
384
- for (const childId of parent.childIds) {
385
- const original = originals.get(childId)
386
- const child = graph.getNode(childId)
387
- if (!original || !child) continue
388
- const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'
389
- if (isInFlow) {
390
- compute(childId, original, child)
391
- continue
392
- }
393
- const rect = scalesChildren
394
- ? scaledChildRect(original, parentBefore, parentAfter)
395
- : constrainedChildRect(
396
- original,
397
- parentBefore,
398
- parentAfter,
399
- child.horizontalConstraint,
400
- child.verticalConstraint,
401
- enforcedAspectRatio(child)
402
- )
403
- const childChanges: Partial<SceneNode> = {
404
- ...rect,
405
- ...scaledGeometryChanges(original, original.width, original.height, rect.width, rect.height)
406
- }
407
- changes.set(childId, childChanges)
408
- // The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.
409
- compute(childId, original, child.layoutMode === 'NONE' ? rect : child)
415
+ /** The layer as the resize found it, or as the resize leaves it with its changes so far. */
416
+ function startOf(plan: ResizePlan, id: string): SceneNode | undefined {
417
+ const node = plan.graph.getNode(id)
418
+ const original = plan.originals.get(id)
419
+ return node && original ? { ...node, ...original } : node
420
+ }
421
+
422
+ function plannedOf(plan: ResizePlan, id: string): SceneNode | undefined {
423
+ const node = startOf(plan, id)
424
+ const changes = plan.changes.get(id)
425
+ return node && changes ? { ...node, ...changes } : node
426
+ }
427
+
428
+ function mergeChanges(plan: ResizePlan, id: string, changes: Partial<SceneNode>) {
429
+ plan.changes.set(id, { ...plan.changes.get(id), ...changes })
430
+ }
431
+
432
+ /** Writes where a layer's constraints put it, then places its own children. */
433
+ function placeConstrained(
434
+ plan: ResizePlan,
435
+ child: SceneNode,
436
+ original: ResizeSnapshot,
437
+ rect: ConstrainedRect,
438
+ toParent: Mat3 | null
439
+ ) {
440
+ const { flipsX, flipsY, ...box } = rect
441
+ const position = toParent ? Matrix.mapPoint(toParent, box) : box
442
+ plan.changes.set(child.id, {
443
+ ...box,
444
+ ...position,
445
+ ...flipChanges(original, child, flipsX, flipsY),
446
+ ...constrainedGeometryChanges(original, child, box)
447
+ })
448
+ // The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.
449
+ placeChildren(plan, child.id, original, child.layoutMode === 'NONE' ? box : child)
450
+ }
451
+
452
+ /**
453
+ * Places the layers in a group or boolean whose frame resized. The group owns no constraints: each
454
+ * layer in it keeps its place in the frame by its own, through nested groups, and the groups then
455
+ * fit around them, as in Figma (live, #1835). A layer is placed in the frame's space and written
456
+ * back in its group's.
457
+ */
458
+ function placeGroupedLayers(
459
+ plan: ResizePlan,
460
+ group: SceneNode,
461
+ toFrame: Mat3,
462
+ frameBefore: Size,
463
+ frameAfter: Size
464
+ ) {
465
+ for (const childId of group.childIds) {
466
+ const child = plan.graph.getNode(childId)
467
+ const original = plan.originals.get(childId)
468
+ const start = startOf(plan, childId)
469
+ if (!child || !original || !start) continue
470
+ if (FITTED_CONTAINER_TYPES.has(child.type)) {
471
+ const inner = Matrix.multiply(toFrame, getNodeLocalMatrix(start))
472
+ placeGroupedLayers(plan, start, inner, frameBefore, frameAfter)
473
+ continue
410
474
  }
475
+ const inFrame = Matrix.mapPoint(toFrame, original)
476
+ const rect = constrainedChildRect(
477
+ { ...inFrame, width: original.width, height: original.height },
478
+ frameBefore,
479
+ frameAfter,
480
+ child.horizontalConstraint,
481
+ child.verticalConstraint,
482
+ enforcedAspectRatio(child)
483
+ )
484
+ placeConstrained(plan, child, original, rect, Matrix.invert(toFrame))
411
485
  }
486
+ fitPlannedGroup(plan, group.id)
487
+ }
412
488
 
413
- compute(rootId, rootBefore, rootAfter)
414
- return changes
489
+ const PLACEMENT_KEYS = ['x', 'y', 'width', 'height'] as const
490
+
491
+ /**
492
+ * Fits a group to its layers where the resize put them, when any of them moved: a boolean's box
493
+ * is its result's, which only the renderer measures, so one whose operands stay keeps its box. A
494
+ * drag places from the start each time, so the group's box is always written.
495
+ */
496
+ function fitPlannedGroup(plan: ResizePlan, groupId: string) {
497
+ const group = plannedOf(plan, groupId)
498
+ if (!group) return
499
+ const { x, y, width, height } = group
500
+ mergeChanges(plan, groupId, { x, y, width, height })
501
+ const moved = group.childIds.some((id) => {
502
+ const start = startOf(plan, id)
503
+ const planned = plannedOf(plan, id)
504
+ return start && planned && PLACEMENT_KEYS.some((key) => start[key] !== planned[key])
505
+ })
506
+ if (!moved) return
507
+ const reader = { rootId: plan.graph.rootId, getNode: (id: string) => plannedOf(plan, id) }
508
+ const children = group.childIds.flatMap((id) => reader.getNode(id) ?? [])
509
+ const fit = groupFitChanges(reader, group, children)
510
+ for (const [id, changes] of fit ?? []) mergeChanges(plan, id, changes)
511
+ }
512
+
513
+ /**
514
+ * Places the children of a layer that went from `parentBefore` to `parentAfter`: a frame's by their
515
+ * constraints, the layers in its groups and booleans by theirs, a group's or boolean's own scaled.
516
+ */
517
+ function placeChildren(plan: ResizePlan, parentId: string, parentBefore: Size, parentAfter: Size) {
518
+ const parent = plan.graph.getNode(parentId)
519
+ if (!parent) return
520
+ const scalesChildren = FITTED_CONTAINER_TYPES.has(parent.type)
521
+ for (const childId of parent.childIds) {
522
+ const original = plan.originals.get(childId)
523
+ const child = plan.graph.getNode(childId)
524
+ if (!original || !child) continue
525
+ const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'
526
+ if (isInFlow) {
527
+ placeChildren(plan, childId, original, child)
528
+ continue
529
+ }
530
+ if (!scalesChildren && FITTED_CONTAINER_TYPES.has(child.type)) {
531
+ const start = { ...child, ...original }
532
+ placeGroupedLayers(plan, start, getNodeLocalMatrix(start), parentBefore, parentAfter)
533
+ continue
534
+ }
535
+ const rect: ConstrainedRect = scalesChildren
536
+ ? scaledChildRect(original, parentBefore, parentAfter)
537
+ : constrainedChildRect(
538
+ original,
539
+ parentBefore,
540
+ parentAfter,
541
+ child.horizontalConstraint,
542
+ child.verticalConstraint,
543
+ enforcedAspectRatio(child)
544
+ )
545
+ placeConstrained(plan, child, original, rect, null)
546
+ }
547
+ }
548
+
549
+ /**
550
+ * Where the descendants of a layer go when it resizes from `rootBefore` to `rootAfter`, from the
551
+ * places `originals` recorded.
552
+ */
553
+ export function computeConstrainedResizeChanges(
554
+ graph: ResizeGraph,
555
+ rootId: string,
556
+ rootBefore: Size,
557
+ rootAfter: Size,
558
+ originals: ReadonlyMap<string, ResizeSnapshot>
559
+ ): Map<string, Partial<SceneNode>> {
560
+ const plan: ResizePlan = { graph, originals, changes: new Map() }
561
+ placeChildren(plan, rootId, rootBefore, rootAfter)
562
+ return plan.changes
415
563
  }
@@ -1,2 +1,10 @@
1
1
  export { scaleNodeChanges } from './node'
2
- export { rescaleNodeTree } from './tree'
2
+ export {
3
+ MIN_RESCALE,
4
+ rescaleNodeChanges,
5
+ rescaleNodeTree,
6
+ rescaleTargets,
7
+ scaleRoots,
8
+ type RescaleOptions
9
+ } from './tree'
10
+ export { scaledSnapshotChanges, snapshotScale, type ScaleSnapshot } from './snapshot'
@@ -29,7 +29,15 @@ function scaledEffects(effects: readonly Effect[], scale: number): Effect[] {
29
29
  ...structuredClone(effect),
30
30
  offset: { x: effect.offset.x * scale, y: effect.offset.y * scale },
31
31
  radius: effect.radius * scale,
32
- spread: effect.spread * scale
32
+ spread: effect.spread * scale,
33
+ ...(effect.progressive
34
+ ? {
35
+ progressive: {
36
+ ...structuredClone(effect.progressive),
37
+ startRadius: effect.progressive.startRadius * scale
38
+ }
39
+ }
40
+ : {})
33
41
  }))
34
42
  }
35
43
 
@@ -0,0 +1,51 @@
1
+ import { placeScaledAbout, selectionPlacement, type SelectionPlacement } from '../coordinate'
2
+ import type { SceneGraph } from '../graph'
3
+ import type { Vector } from '../primitives'
4
+ import type { SceneNode } from '../types'
5
+ import { rescaleNodeChanges, rescaleTargets } from './tree'
6
+
7
+ /**
8
+ * The layers a Scale edit changes, as they were when it began, so every step of a drag or a
9
+ * scrubbed field scales from the same start rather than from its own last preview.
10
+ */
11
+ export interface ScaleSnapshot {
12
+ /** Each scaled root's place in its parent and on the canvas, by ID. */
13
+ roots: Map<string, SelectionPlacement>
14
+ /** Every layer the edit changes, roots first; locked layers inside are left out. */
15
+ nodes: Map<string, SceneNode>
16
+ }
17
+
18
+ /** Records the roots and the layers inside them that Figma's Scale tool changes. */
19
+ export function snapshotScale(graph: SceneGraph, roots: readonly SceneNode[]): ScaleSnapshot {
20
+ const snapshot: ScaleSnapshot = { roots: new Map(), nodes: new Map() }
21
+ for (const root of roots) {
22
+ snapshot.roots.set(root.id, selectionPlacement(root, graph))
23
+ for (const node of rescaleTargets(graph, root, { keepLocked: true }))
24
+ snapshot.nodes.set(node.id, { ...node })
25
+ }
26
+ return snapshot
27
+ }
28
+
29
+ /**
30
+ * What scaling the snapshot evenly by `scale` about a canvas point writes to each layer: its
31
+ * lengths, and for a root its place, so the point stays put and the root keeps its angle.
32
+ */
33
+ export function scaledSnapshotChanges(
34
+ graph: SceneGraph,
35
+ snapshot: ScaleSnapshot,
36
+ scale: number,
37
+ anchor: Vector
38
+ ): Map<string, Partial<SceneNode>> {
39
+ const changes = new Map<string, Partial<SceneNode>>()
40
+ for (const [id, node] of snapshot.nodes) {
41
+ const placement = snapshot.roots.get(id)
42
+ const change = rescaleNodeChanges(graph, node, scale, placement !== undefined)
43
+ // At its own size, a root takes back exactly the place it started from.
44
+ if (placement) {
45
+ const { x, y } = scale === 1 ? node : placeScaledAbout(placement, anchor, scale)
46
+ Object.assign(change, { x, y })
47
+ }
48
+ changes.set(id, change)
49
+ }
50
+ return changes
51
+ }
@@ -4,7 +4,9 @@ import type { SceneNode } from '../types'
4
4
  import { scaleVariableBindingUnits } from '../variables/units'
5
5
  import { scaleNodeChanges } from './node'
6
6
 
7
- const MIN_SCALE = 0.01
7
+ /** The smallest scale a node tree takes. */
8
+ export const MIN_RESCALE = 0.01
9
+
8
10
  const RESCALABLE_NODE_TYPES = new Set<SceneNode['type']>([
9
11
  'BOOLEAN_OPERATION',
10
12
  'COMPONENT',
@@ -22,30 +24,72 @@ const RESCALABLE_NODE_TYPES = new Set<SceneNode['type']>([
22
24
  'VECTOR'
23
25
  ])
24
26
 
25
- /** Scale a node tree from the root node's top-left, matching Figma's Scale tool. */
26
- export function rescaleNodeTree(graph: SceneGraph, rootId: string, scale: number): void {
27
- if (!Number.isFinite(scale)) throw new TypeError('Scale must be a finite number')
28
- if (scale < MIN_SCALE) throw new RangeError(`Scale must be at least ${MIN_SCALE}`)
29
- const root = graph.getNode(rootId)
30
- if (!root || !RESCALABLE_NODE_TYPES.has(root.type))
31
- throw new Error('rescale() is not supported on this node')
27
+ export interface RescaleOptions {
28
+ /**
29
+ * Locked layers below the root, and everything inside them, keep their size and their place in
30
+ * their parent, as Figma's Scale tool leaves them; the plugin API's `rescale()` scales them.
31
+ */
32
+ keepLocked?: boolean
33
+ }
34
+
35
+ /**
36
+ * The selected layers the Scale tool scales: unlocked layers of a type that rescales, outside any
37
+ * instance, and not inside another of them, which scales them with itself.
38
+ */
39
+ export function scaleRoots(graph: SceneGraph, nodes: readonly SceneNode[]): SceneNode[] {
40
+ const ids = new Set(nodes.map((node) => node.id))
41
+ return nodes.filter((node) => {
42
+ if (node.locked || !RESCALABLE_NODE_TYPES.has(node.type) || !node.parentId) return false
43
+ if (findInstanceAncestor(graph, node.parentId)) return false
44
+ return !graph.closest(node.parentId, (parent) => ids.has(parent.id))
45
+ })
46
+ }
47
+
48
+ /** The root and every layer below it that a rescale changes, root first. */
49
+ export function rescaleTargets(
50
+ graph: SceneGraph,
51
+ root: SceneNode,
52
+ { keepLocked = false }: RescaleOptions = {}
53
+ ): SceneNode[] {
32
54
  const nodes: SceneNode[] = [root]
33
55
  const collect = (node: SceneNode): void => {
34
- for (const childId of node.childIds) {
35
- const child = graph.getNode(childId)
36
- if (!child) continue
56
+ for (const child of graph.getChildren(node.id)) {
57
+ if (keepLocked && child.locked) continue
37
58
  nodes.push(child)
38
59
  collect(child)
39
60
  }
40
61
  }
41
62
  collect(root)
42
- for (const node of nodes) {
43
- graph.updateNode(node.id, {
44
- ...scaleNodeChanges(node, scale, node.id !== rootId),
45
- ...scaleVariableBindingUnits(node, scale),
46
- componentScale: findInstanceAncestor(graph, node.id)
47
- ? node.componentScale * scale
48
- : node.componentScale
49
- })
63
+ return nodes
64
+ }
65
+
66
+ /**
67
+ * What a rescale by `scale` writes to `node`: its own lengths and those of its bound variables,
68
+ * the scale of an instance's occurrence, and, below the root, its place in its parent.
69
+ */
70
+ export function rescaleNodeChanges(
71
+ graph: SceneGraph,
72
+ node: SceneNode,
73
+ scale: number,
74
+ isRoot: boolean
75
+ ): Partial<SceneNode> {
76
+ return {
77
+ ...scaleNodeChanges(node, scale, !isRoot),
78
+ ...scaleVariableBindingUnits(node, scale),
79
+ componentScale: findInstanceAncestor(graph, node.id)
80
+ ? node.componentScale * scale
81
+ : node.componentScale
82
+ }
83
+ }
84
+
85
+ /** Scale a node tree from the root node's top-left, matching Figma's `rescale()`. */
86
+ export function rescaleNodeTree(graph: SceneGraph, rootId: string, scale: number): void {
87
+ if (!Number.isFinite(scale)) throw new TypeError('Scale must be a finite number')
88
+ if (scale < MIN_RESCALE) throw new RangeError(`Scale must be at least ${MIN_RESCALE}`)
89
+ const root = graph.getNode(rootId)
90
+ if (!root || !RESCALABLE_NODE_TYPES.has(root.type))
91
+ throw new Error('rescale() is not supported on this node')
92
+ for (const node of rescaleTargets(graph, root)) {
93
+ graph.updateNode(node.id, rescaleNodeChanges(graph, node, scale, node.id === rootId))
50
94
  }
51
95
  }