@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
package/src/hit-test.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  import { hitsConnectorLine } from './connectors/hit'
2
2
  import { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from './coordinate'
3
- import { geometryBlobPolylines } from './geometry'
4
3
  import type { SceneGraph } from './graph'
4
+ import { containsPoint } from './hit-test/geometry'
5
+ import { clippingMasks, isMaskedOut } from './hit-test/masks'
6
+ import { showsPaint } from './hit-test/paint'
5
7
  import { drawnChildIds } from './layout/draw-order'
6
8
  import Matrix, { type Mat3 } from './matrix'
7
- import type { Rect, Vector } from './primitives'
9
+ import type { Rect } from './primitives'
8
10
  import type { SceneNode, NodeType } from './types'
9
11
 
12
+ export { clippingMasks, isPointMaskedOut } from './hit-test/masks'
13
+ export { hitTestDropTarget, type DropTargetOptions } from './hit-test/drop'
14
+ export { showsPaint } from './hit-test/paint'
15
+
10
16
  const CONTAINER_TYPES = new Set<NodeType>([
11
17
  'CANVAS',
12
18
  'FRAME',
@@ -18,13 +24,12 @@ const CONTAINER_TYPES = new Set<NodeType>([
18
24
  ])
19
25
  const OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])
20
26
  /**
21
- * How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
22
- * a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
27
+ * Whether a container's empty area takes the click. Figma counts it only where a paint shows,
28
+ * except a main component's, which always counts, and an instance's when the click is a plain one.
23
29
  */
24
- const DEGENERATE_AXIS_SLOP = 4
25
-
26
- function hasVisibleFillOrStroke(node: SceneNode): boolean {
27
- return node.fills.some((f) => f.visible) || node.strokes.some((s) => s.visible)
30
+ function takesEmptyArea(graph: SceneGraph, node: SceneNode, instanceBodies: boolean): boolean {
31
+ if (node.type === 'COMPONENT' || showsPaint(node) || ownsItsEmptyArea(graph, node)) return true
32
+ return instanceBodies && node.type === 'INSTANCE'
28
33
  }
29
34
 
30
35
  function isTopLevel(graph: SceneGraph, node: SceneNode): boolean {
@@ -42,178 +47,11 @@ function ownsItsEmptyArea(graph: SceneGraph, node: SceneNode): boolean {
42
47
  return node.type === 'FRAME' && node.layoutMode !== 'NONE' && isTopLevel(graph, node)
43
48
  }
44
49
 
45
- function hasTransformedAncestor(
46
- node: SceneNode,
47
- graph: SceneGraph,
48
- cache: Map<string, boolean>
49
- ): boolean {
50
- const cached = cache.get(node.id)
51
- if (cached !== undefined) return cached
52
- const parent = node.parentId ? graph.getNode(node.parentId) : undefined
53
- const transformed =
54
- node.rotation !== 0 ||
55
- node.flipX ||
56
- node.flipY ||
57
- (parent ? hasTransformedAncestor(parent, graph, cache) : false)
58
- cache.set(node.id, transformed)
59
- return transformed
60
- }
61
-
62
50
  /**
63
- * Where the point lies in the node's own coordinates: its offset from the position when nothing
64
- * above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
65
- */
66
- function localPoint(
67
- px: number,
68
- py: number,
69
- node: SceneNode,
70
- graph: SceneGraph,
71
- transformCache: Map<string, boolean>
72
- ): Vector | null {
73
- if (!hasTransformedAncestor(node, graph, transformCache)) {
74
- const absolute = graph.getAbsolutePosition(node.id)
75
- return { x: px - absolute.x, y: py - absolute.y }
76
- }
77
- const inv = Matrix.invert(getWorldMatrix(node, graph))
78
- if (!inv) return null
79
- const [x, y] = Matrix.mapPoints(inv, [px, py])
80
- return { x, y }
81
- }
82
-
83
- /** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
84
- function containsPoint(
85
- px: number,
86
- py: number,
87
- node: SceneNode,
88
- graph: SceneGraph,
89
- transformCache: Map<string, boolean>,
90
- pad = 0
91
- ): boolean {
92
- const local = localPoint(px, py, node, graph, transformCache)
93
- if (!local) return false
94
- const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
95
- const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
96
- return (
97
- local.x >= -padX &&
98
- local.x <= node.width + padX &&
99
- local.y >= -padY &&
100
- local.y <= node.height + padY
101
- )
102
- }
103
-
104
- /** Whether a rounded rectangle of `width` × `height` holds the point. */
105
- function roundedRectContains(
106
- point: Vector,
107
- width: number,
108
- height: number,
109
- radius: number
110
- ): boolean {
111
- if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false
112
- const r = Math.max(0, Math.min(radius, width / 2, height / 2))
113
- const cx = Math.min(Math.max(point.x, r), width - r)
114
- const cy = Math.min(Math.max(point.y, r), height - r)
115
- return Math.hypot(point.x - cx, point.y - cy) <= r
116
- }
117
-
118
- /** Whether the point is inside a closed polyline, by the even-odd rule. */
119
- function insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {
120
- let inside = false
121
- for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
122
- const a = polyline[i]
123
- const b = polyline[j]
124
- if (a.y > y === b.y > y) continue
125
- if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside
126
- }
127
- return inside
128
- }
129
-
130
- /**
131
- * Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
132
- * rectangle's corners, or a vector's filled path. Other shapes clip by their box.
133
- */
134
- function maskContainsPoint(
135
- px: number,
136
- py: number,
137
- mask: SceneNode,
138
- graph: SceneGraph,
139
- transformCache: Map<string, boolean>
140
- ): boolean {
141
- const outlined =
142
- (mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||
143
- (mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||
144
- (mask.type === 'VECTOR' && mask.fillGeometry.length > 0)
145
- if (!outlined) return containsPoint(px, py, mask, graph, transformCache)
146
- const local = localPoint(px, py, mask, graph, transformCache)
147
- if (!local) return false
148
- if (mask.type === 'ELLIPSE') {
149
- const rx = mask.width / 2
150
- const ry = mask.height / 2
151
- const dx = (local.x - rx) / rx
152
- const dy = (local.y - ry) / ry
153
- return dx * dx + dy * dy <= 1
154
- }
155
- if (mask.type === 'RECTANGLE') {
156
- return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)
157
- }
158
- return geometryBlobPolylines(mask.fillGeometry).some((points) =>
159
- insidePolyline(points, local.x, local.y)
160
- )
161
- }
162
-
163
- function isVisibleMask(node: SceneNode | undefined): node is SceneNode {
164
- return node !== undefined && node.visible && node.isMask
165
- }
166
-
167
- /**
168
- * The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
169
- * a run of visible masks clips the layers after it up to the next visible mask. Empty when the
170
- * container has no visible mask.
171
- */
172
- function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {
173
- const children = childIds.map((childId) => graph.nodes.get(childId))
174
- if (!children.some(isVisibleMask)) return []
175
- const result: SceneNode[][] = []
176
- let run: SceneNode[] = []
177
- let previousWasMask = false
178
- for (const child of children) {
179
- if (isVisibleMask(child)) {
180
- run = previousWasMask ? [...run, child] : [child]
181
- previousWasMask = true
182
- result.push([])
183
- continue
184
- }
185
- previousWasMask = false
186
- result.push(run)
187
- }
188
- return result
189
- }
190
-
191
- function hitTestOpaqueContainer(
192
- graph: SceneGraph,
193
- px: number,
194
- py: number,
195
- child: SceneNode,
196
- childId: string,
197
- deep: boolean,
198
- transformCache: Map<string, boolean>,
199
- pad: number,
200
- locked = false
201
- ): SceneNode | null {
202
- if (!containsPoint(px, py, child, graph, transformCache)) return null
203
- const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad, locked)
204
- if (childHit) return child
205
- if (hasVisibleFillOrStroke(child)) return child
206
- return null
207
- }
208
- /**
209
- * What a hit inside a container reports: the container itself when it is locked (a locked layer,
210
- * where one is hit at all, stands in for everything inside it) or the walk stops at containers.
51
+ * What a click inside a container reports. `opaque` is an instance or component whose body takes
52
+ * the click without looking through it; every other container looks through to its children first.
211
53
  */
212
- function standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode {
213
- return container.locked || !deep ? container : hit
214
- }
215
-
216
- function hitTestTransparentContainer(
54
+ function hitTestContainer(
217
55
  graph: SceneGraph,
218
56
  px: number,
219
57
  py: number,
@@ -222,40 +60,71 @@ function hitTestTransparentContainer(
222
60
  deep: boolean,
223
61
  transformCache: Map<string, boolean>,
224
62
  pad: number,
225
- locked = false
63
+ locked = false,
64
+ instanceBodies = false,
65
+ opaque = false
226
66
  ): SceneNode | null {
67
+ if (opaque) {
68
+ if (!containsPoint(px, py, child, graph, transformCache)) return null
69
+ const childHit = hitTestChildren(
70
+ graph,
71
+ px,
72
+ py,
73
+ childId,
74
+ deep,
75
+ transformCache,
76
+ pad,
77
+ locked,
78
+ instanceBodies
79
+ )
80
+ if (childHit) return child
81
+ if (takesEmptyArea(graph, child, instanceBodies)) return child
82
+ return null
83
+ }
227
84
  if (child.type === 'GROUP') {
228
85
  // A group fits its layers, so a padded click on one at its edge falls just outside it.
229
86
  if (!containsPoint(px, py, child, graph, transformCache, pad)) return null
230
- const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad, locked)
87
+ const childHit = hitTestChildren(
88
+ graph,
89
+ px,
90
+ py,
91
+ childId,
92
+ deep,
93
+ transformCache,
94
+ pad,
95
+ locked,
96
+ instanceBodies
97
+ )
231
98
  if (childHit) return standIn(child, childHit, deep)
232
99
  return containsPoint(px, py, child, graph, transformCache) ? child : null
233
100
  }
234
101
 
235
- const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad, locked)
102
+ const childHit = hitTestChildren(
103
+ graph,
104
+ px,
105
+ py,
106
+ childId,
107
+ deep,
108
+ transformCache,
109
+ pad,
110
+ locked,
111
+ instanceBodies
112
+ )
236
113
  if (childHit) return standIn(child, childHit, true)
237
114
 
238
115
  if (
239
116
  containsPoint(px, py, child, graph, transformCache) &&
240
- (hasVisibleFillOrStroke(child) || ownsItsEmptyArea(graph, child))
117
+ takesEmptyArea(graph, child, instanceBodies)
241
118
  )
242
119
  return child
243
120
  return null
244
121
  }
245
-
246
- /** Whether the masks that clip a layer leave the point outside every one of their outlines. */
247
- function isMaskedOut(
248
- px: number,
249
- py: number,
250
- masks: readonly SceneNode[] | undefined,
251
- graph: SceneGraph,
252
- transformCache: Map<string, boolean>
253
- ): boolean {
254
- if (!masks) return false
255
- return (
256
- masks.length > 0 &&
257
- !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))
258
- )
122
+ /**
123
+ * What a hit inside a container reports: the container itself when it is locked (a locked layer,
124
+ * where one is hit at all, stands in for everything inside it) or the walk stops at containers.
125
+ */
126
+ function standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode {
127
+ return container.locked || !deep ? container : hit
259
128
  }
260
129
 
261
130
  /**
@@ -288,7 +157,8 @@ function hitTestChildren(
288
157
  deep = false,
289
158
  transformCache = new Map<string, boolean>(),
290
159
  pad = 0,
291
- locked = false
160
+ locked = false,
161
+ instanceBodies = false
292
162
  ): SceneNode | null {
293
163
  const parent = graph.nodes.get(parentId)
294
164
  if (!parent) return null
@@ -306,7 +176,7 @@ function hitTestChildren(
306
176
  if (!takesClick(graph, px, py, child, masks[i], locked, transformCache)) continue
307
177
  if (CONTAINER_TYPES.has(child.type)) {
308
178
  if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {
309
- const hit = hitTestOpaqueContainer(
179
+ const hit = hitTestContainer(
310
180
  graph,
311
181
  px,
312
182
  py,
@@ -315,13 +185,15 @@ function hitTestChildren(
315
185
  deep,
316
186
  transformCache,
317
187
  pad,
318
- locked
188
+ locked,
189
+ instanceBodies,
190
+ true
319
191
  )
320
192
  if (hit) return hit
321
193
  continue
322
194
  }
323
195
 
324
- const hit = hitTestTransparentContainer(
196
+ const hit = hitTestContainer(
325
197
  graph,
326
198
  px,
327
199
  py,
@@ -330,7 +202,8 @@ function hitTestChildren(
330
202
  deep,
331
203
  transformCache,
332
204
  pad,
333
- locked
205
+ locked,
206
+ instanceBodies
334
207
  )
335
208
  if (hit) return hit
336
209
  continue
@@ -353,7 +226,7 @@ export function hitTest(
353
226
  scopeId?: string
354
227
  ): SceneNode | null {
355
228
  const scope = scopeId ?? graph.rootId
356
- return hitTestChildren(graph, px, py, scope, false)
229
+ return hitTestChildren(graph, px, py, scope, false, new Map(), 0, false, true)
357
230
  }
358
231
 
359
232
  export function hitTestDeep(
@@ -449,7 +322,7 @@ export function hitTestSelectable(
449
322
  /** Whether locked layers are hit, as a right-click hits them; see `hitTestAtPoint`. */
450
323
  locked = false
451
324
  ): SceneNode | null {
452
- const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad, locked)
325
+ const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad, locked, true)
453
326
  if (!deepest) {
454
327
  const scope = graph.nodes.get(scopeId)
455
328
  return scope ? frameChildAt(graph, scope, px, py, locked) : null
@@ -492,7 +365,7 @@ export function hitTestOpenContainer(
492
365
  py: number,
493
366
  scopeId: string
494
367
  ): SceneNode | null {
495
- const deepest = hitTestChildren(graph, px, py, scopeId, true)
368
+ const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), 0, false, false)
496
369
  for (let node: SceneNode | undefined = deepest ?? undefined; node && node.id !== scopeId;) {
497
370
  if (!opensByItself(graph, node) || ownsItsEmptyArea(graph, node)) return null
498
371
  node = node.parentId ? graph.nodes.get(node.parentId) : undefined
@@ -523,7 +396,7 @@ function rectOverlap(a: Rect, b: Rect): Rect | null {
523
396
  */
524
397
  function selectsByOwnArea(graph: SceneGraph, node: SceneNode): boolean {
525
398
  if (node.type === 'GROUP' || ownsItsEmptyArea(graph, node)) return true
526
- return hasVisibleFillOrStroke(node) && !opensByItself(graph, node)
399
+ return showsPaint(node) && !opensByItself(graph, node)
527
400
  }
528
401
 
529
402
  /**
@@ -578,89 +451,3 @@ export function isPointInNode(graph: SceneGraph, nodeId: string, px: number, py:
578
451
  const node = graph.nodes.get(nodeId)
579
452
  return node !== undefined && containsPoint(px, py, node, graph, new Map())
580
453
  }
581
-
582
- /** Which layers take a drop, and which are only looked through, as in Figma. */
583
- interface DropRules {
584
- takes: (node: SceneNode) => boolean
585
- passThrough: ReadonlySet<NodeType>
586
- }
587
-
588
- const LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])
589
- const COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])
590
-
591
- function dropRules(options: DropTargetOptions): DropRules {
592
- const { componentSetIds } = options
593
- if (!componentSetIds) {
594
- return {
595
- takes: (node) => LAYER_TARGETS.has(node.type),
596
- passThrough: new Set(['GROUP', 'COMPONENT_SET'])
597
- }
598
- }
599
- // Components never go into other components, and a set only takes back its own variants.
600
- return {
601
- takes: (node) =>
602
- COMPONENT_TARGETS.has(node.type) ||
603
- (node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),
604
- passThrough: new Set(['GROUP'])
605
- }
606
- }
607
-
608
- function dropTargetIn(
609
- graph: SceneGraph,
610
- px: number,
611
- py: number,
612
- parent: SceneNode,
613
- excludeIds: ReadonlySet<string>,
614
- rules: DropRules,
615
- transformCache: Map<string, boolean>
616
- ): SceneNode | null {
617
- // A clipped-away part of a child is not under the cursor.
618
- if (
619
- parent.type !== 'CANVAS' &&
620
- parent.clipsContent &&
621
- !containsPoint(px, py, parent, graph, transformCache)
622
- )
623
- return null
624
-
625
- for (let i = parent.childIds.length - 1; i >= 0; i--) {
626
- const childId = parent.childIds[i]
627
- if (excludeIds.has(childId)) continue
628
- const child = graph.nodes.get(childId)
629
- if (!child || child.internalOnly || !child.visible || child.locked) continue
630
- const target = rules.takes(child)
631
- if (!target && !rules.passThrough.has(child.type)) continue
632
-
633
- const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)
634
- if (deeper) return deeper
635
- if (target && containsPoint(px, py, child, graph, transformCache)) return child
636
- }
637
-
638
- return null
639
- }
640
-
641
- export interface DropTargetOptions {
642
- /**
643
- * Set when the dropped layers are all main components: the component sets they come from.
644
- * Components then go only into frames, sections, instances, and those sets.
645
- */
646
- componentSetIds?: ReadonlySet<string>
647
- }
648
-
649
- /**
650
- * The topmost unlocked frame, section, component, or instance under the point, in its rotated
651
- * shape and inside its clipping ancestors. Groups and component sets are looked through but never
652
- * returned, except that a set takes dropped components; locked layers and their contents are
653
- * skipped.
654
- */
655
- export function hitTestDropTarget(
656
- graph: SceneGraph,
657
- px: number,
658
- py: number,
659
- excludeIds: ReadonlySet<string>,
660
- scopeId?: string,
661
- options: DropTargetOptions = {}
662
- ): SceneNode | null {
663
- const scope = graph.nodes.get(scopeId ?? graph.rootId)
664
- if (!scope) return null
665
- return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())
666
- }
@@ -71,6 +71,8 @@ export const INSTANCE_SYNC_PROPS: (keyof SceneNode)[] = difference(
71
71
  'itemReverseZIndex',
72
72
  'gridPosition',
73
73
  'clipsContent',
74
+ 'overflowDirection',
75
+ 'scrollBehavior',
74
76
  'layoutGrids',
75
77
  'boundVariables',
76
78
  'variableModes',
@@ -46,6 +46,7 @@ import {
46
46
  } from './sync'
47
47
 
48
48
  export { canCreateInstance, createsComponentCycle, cycleSafeParent } from './cycles'
49
+ export { walkInstanceSources } from './source-walk'
49
50
  export { instanceMainComponent } from './main-component'
50
51
  export {
51
52
  isInShownReplacedLayer,