@pairdraw/scene-graph 0.19.2 → 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 (166) hide show
  1. package/dist/components/index.d.ts +2 -2
  2. package/dist/components/index.js +2 -2
  3. package/dist/components/variants.d.ts +12 -1
  4. package/dist/components/variants.d.ts.map +1 -1
  5. package/dist/components/variants.js +30 -1
  6. package/dist/components/variants.js.map +1 -1
  7. package/dist/constants.d.ts +25 -1
  8. package/dist/constants.d.ts.map +1 -1
  9. package/dist/constants.js +94 -1
  10. package/dist/constants.js.map +1 -1
  11. package/dist/copy.d.ts.map +1 -1
  12. package/dist/copy.js +17 -1
  13. package/dist/copy.js.map +1 -1
  14. package/dist/css/index.d.ts +2 -2
  15. package/dist/css/index.js +2 -2
  16. package/dist/css/text.d.ts +17 -1
  17. package/dist/css/text.d.ts.map +1 -1
  18. package/dist/css/text.js +62 -1
  19. package/dist/css/text.js.map +1 -1
  20. package/dist/design-memory/index.d.ts +3 -0
  21. package/dist/design-memory/index.js +3 -0
  22. package/dist/design-memory/model.d.ts +33 -0
  23. package/dist/design-memory/model.d.ts.map +1 -0
  24. package/dist/design-memory/model.js +142 -0
  25. package/dist/design-memory/model.js.map +1 -0
  26. package/dist/design-memory/schema.d.ts +76 -0
  27. package/dist/design-memory/schema.d.ts.map +1 -0
  28. package/dist/design-memory/schema.js +72 -0
  29. package/dist/design-memory/schema.js.map +1 -0
  30. package/dist/effects.d.ts +7 -4
  31. package/dist/effects.d.ts.map +1 -1
  32. package/dist/effects.js +9 -4
  33. package/dist/effects.js.map +1 -1
  34. package/dist/empty-values.js +93 -0
  35. package/dist/empty-values.js.map +1 -0
  36. package/dist/fields/components.d.ts +9 -0
  37. package/dist/fields/components.d.ts.map +1 -0
  38. package/dist/fields/components.js +24 -0
  39. package/dist/fields/components.js.map +1 -0
  40. package/dist/fields/index.d.ts +3 -2
  41. package/dist/fields/index.js +3 -2
  42. package/dist/fields/layout.d.ts +3 -1
  43. package/dist/fields/layout.d.ts.map +1 -1
  44. package/dist/fields/layout.js +10 -1
  45. package/dist/fields/layout.js.map +1 -1
  46. package/dist/graph.d.ts +8 -1
  47. package/dist/graph.d.ts.map +1 -1
  48. package/dist/graph.js +28 -5
  49. package/dist/graph.js.map +1 -1
  50. package/dist/grid-values.d.ts +10 -0
  51. package/dist/grid-values.d.ts.map +1 -0
  52. package/dist/grid-values.js +53 -0
  53. package/dist/grid-values.js.map +1 -0
  54. package/dist/hit-test/drop.d.ts +20 -0
  55. package/dist/hit-test/drop.d.ts.map +1 -0
  56. package/dist/hit-test/drop.js +54 -0
  57. package/dist/hit-test/drop.js.map +1 -0
  58. package/dist/hit-test/geometry.js +86 -0
  59. package/dist/hit-test/geometry.js.map +1 -0
  60. package/dist/hit-test/masks.d.ts +14 -0
  61. package/dist/hit-test/masks.d.ts.map +1 -0
  62. package/dist/hit-test/masks.js +41 -0
  63. package/dist/hit-test/masks.js.map +1 -0
  64. package/dist/hit-test/paint.d.ts +6 -0
  65. package/dist/hit-test/paint.d.ts.map +1 -0
  66. package/dist/hit-test/paint.js +19 -0
  67. package/dist/hit-test/paint.js.map +1 -0
  68. package/dist/hit-test.d.ts +4 -15
  69. package/dist/hit-test.d.ts.map +1 -1
  70. package/dist/hit-test.js +5 -173
  71. package/dist/hit-test.js.map +1 -1
  72. package/dist/index.d.ts +2 -2
  73. package/dist/instances/fields.d.ts.map +1 -1
  74. package/dist/instances/fields.js +2 -0
  75. package/dist/instances/fields.js.map +1 -1
  76. package/dist/instances/index.d.ts +2 -1
  77. package/dist/instances/index.d.ts.map +1 -1
  78. package/dist/instances/index.js +1 -1
  79. package/dist/instances/index.js.map +1 -1
  80. package/dist/instances/source-walk.d.ts +12 -0
  81. package/dist/instances/source-walk.d.ts.map +1 -0
  82. package/dist/node-defaults.d.ts.map +1 -1
  83. package/dist/node-defaults.js +41 -36
  84. package/dist/node-defaults.js.map +1 -1
  85. package/dist/plugin-data/fields.d.ts +75 -0
  86. package/dist/plugin-data/fields.d.ts.map +1 -1
  87. package/dist/plugin-data/fields.js +18 -1
  88. package/dist/plugin-data/fields.js.map +1 -1
  89. package/dist/preview.d.ts.map +1 -1
  90. package/dist/preview.js +2 -0
  91. package/dist/preview.js.map +1 -1
  92. package/dist/scaling/node.d.ts.map +1 -1
  93. package/dist/scaling/node.js +5 -1
  94. package/dist/scaling/node.js.map +1 -1
  95. package/dist/token-sources/document.d.ts +26 -0
  96. package/dist/token-sources/document.d.ts.map +1 -0
  97. package/dist/token-sources/document.js +80 -0
  98. package/dist/token-sources/document.js.map +1 -0
  99. package/dist/token-sources/index.d.ts +3 -0
  100. package/dist/token-sources/index.js +3 -0
  101. package/dist/token-sources/schema.d.ts +43 -0
  102. package/dist/token-sources/schema.d.ts.map +1 -0
  103. package/dist/token-sources/schema.js +50 -0
  104. package/dist/token-sources/schema.js.map +1 -0
  105. package/dist/types.d.ts +102 -14
  106. package/dist/types.d.ts.map +1 -1
  107. package/dist/types.js.map +1 -1
  108. package/dist/variations/copy.d.ts +16 -0
  109. package/dist/variations/copy.d.ts.map +1 -0
  110. package/dist/variations/copy.js +78 -0
  111. package/dist/variations/copy.js.map +1 -0
  112. package/dist/variations/index.d.ts +6 -0
  113. package/dist/variations/index.js +6 -0
  114. package/dist/variations/match.d.ts +23 -0
  115. package/dist/variations/match.d.ts.map +1 -0
  116. package/dist/variations/match.js +80 -0
  117. package/dist/variations/match.js.map +1 -0
  118. package/dist/variations/model.d.ts +39 -0
  119. package/dist/variations/model.d.ts.map +1 -0
  120. package/dist/variations/model.js +90 -0
  121. package/dist/variations/model.js.map +1 -0
  122. package/dist/variations/pick.d.ts +43 -0
  123. package/dist/variations/pick.d.ts.map +1 -0
  124. package/dist/variations/pick.js +211 -0
  125. package/dist/variations/pick.js.map +1 -0
  126. package/dist/variations/schema.d.ts +27 -0
  127. package/dist/variations/schema.d.ts.map +1 -0
  128. package/dist/variations/schema.js +27 -0
  129. package/dist/variations/schema.js.map +1 -0
  130. package/package.json +21 -1
  131. package/src/components/variants.ts +39 -0
  132. package/src/constants.ts +75 -0
  133. package/src/copy.ts +20 -1
  134. package/src/css/index.ts +7 -1
  135. package/src/css/text.ts +62 -0
  136. package/src/design-memory/index.ts +2 -0
  137. package/src/design-memory/model.ts +143 -0
  138. package/src/design-memory/schema.ts +84 -0
  139. package/src/effects.ts +16 -7
  140. package/src/empty-values.ts +101 -0
  141. package/src/fields/components.ts +21 -0
  142. package/src/fields/index.ts +1 -0
  143. package/src/fields/layout.ts +10 -0
  144. package/src/graph.ts +37 -5
  145. package/src/grid-values.ts +47 -0
  146. package/src/hit-test/drop.ts +90 -0
  147. package/src/hit-test/geometry.ts +131 -0
  148. package/src/hit-test/masks.ts +57 -0
  149. package/src/hit-test/paint.ts +24 -0
  150. package/src/hit-test.ts +8 -277
  151. package/src/instances/fields.ts +2 -0
  152. package/src/instances/index.ts +1 -0
  153. package/src/node-defaults.ts +49 -39
  154. package/src/plugin-data/fields.ts +18 -1
  155. package/src/preview.ts +2 -0
  156. package/src/scaling/node.ts +7 -2
  157. package/src/token-sources/document.ts +104 -0
  158. package/src/token-sources/index.ts +2 -0
  159. package/src/token-sources/schema.ts +59 -0
  160. package/src/types.ts +115 -13
  161. package/src/variations/copy.ts +89 -0
  162. package/src/variations/index.ts +5 -0
  163. package/src/variations/match.ts +100 -0
  164. package/src/variations/model.ts +132 -0
  165. package/src/variations/pick.ts +293 -0
  166. package/src/variations/schema.ts +29 -0
@@ -0,0 +1,90 @@
1
+ // Where dragged layers land: the frame, section, component, or instance under the point.
2
+ import type { SceneGraph } from '../graph'
3
+ import type { NodeType, SceneNode } from '../types'
4
+ import { containsPoint } from './geometry'
5
+
6
+ /** Which layers take a drop, and which are only looked through, as in Figma. */
7
+ interface DropRules {
8
+ takes: (node: SceneNode) => boolean
9
+ passThrough: ReadonlySet<NodeType>
10
+ }
11
+
12
+ const LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])
13
+ const COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])
14
+
15
+ function dropRules(options: DropTargetOptions): DropRules {
16
+ const { componentSetIds } = options
17
+ if (!componentSetIds) {
18
+ return {
19
+ takes: (node) => LAYER_TARGETS.has(node.type),
20
+ passThrough: new Set(['GROUP', 'COMPONENT_SET'])
21
+ }
22
+ }
23
+ // Components never go into other components, and a set only takes back its own variants.
24
+ return {
25
+ takes: (node) =>
26
+ COMPONENT_TARGETS.has(node.type) ||
27
+ (node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),
28
+ passThrough: new Set(['GROUP'])
29
+ }
30
+ }
31
+
32
+ function dropTargetIn(
33
+ graph: SceneGraph,
34
+ px: number,
35
+ py: number,
36
+ parent: SceneNode,
37
+ excludeIds: ReadonlySet<string>,
38
+ rules: DropRules,
39
+ transformCache: Map<string, boolean>
40
+ ): SceneNode | null {
41
+ // A clipped-away part of a child is not under the cursor.
42
+ if (
43
+ parent.type !== 'CANVAS' &&
44
+ parent.clipsContent &&
45
+ !containsPoint(px, py, parent, graph, transformCache)
46
+ )
47
+ return null
48
+
49
+ for (let i = parent.childIds.length - 1; i >= 0; i--) {
50
+ const childId = parent.childIds[i]
51
+ if (excludeIds.has(childId)) continue
52
+ const child = graph.nodes.get(childId)
53
+ if (!child || child.internalOnly || !child.visible || child.locked) continue
54
+ const target = rules.takes(child)
55
+ if (!target && !rules.passThrough.has(child.type)) continue
56
+
57
+ const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)
58
+ if (deeper) return deeper
59
+ if (target && containsPoint(px, py, child, graph, transformCache)) return child
60
+ }
61
+
62
+ return null
63
+ }
64
+
65
+ export interface DropTargetOptions {
66
+ /**
67
+ * Set when the dropped layers are all main components: the component sets they come from.
68
+ * Components then go only into frames, sections, instances, and those sets.
69
+ */
70
+ componentSetIds?: ReadonlySet<string>
71
+ }
72
+
73
+ /**
74
+ * The topmost unlocked frame, section, component, or instance under the point, in its rotated
75
+ * shape and inside its clipping ancestors. Groups and component sets are looked through but never
76
+ * returned, except that a set takes dropped components; locked layers and their contents are
77
+ * skipped.
78
+ */
79
+ export function hitTestDropTarget(
80
+ graph: SceneGraph,
81
+ px: number,
82
+ py: number,
83
+ excludeIds: ReadonlySet<string>,
84
+ scopeId?: string,
85
+ options: DropTargetOptions = {}
86
+ ): SceneNode | null {
87
+ const scope = graph.nodes.get(scopeId ?? graph.rootId)
88
+ if (!scope) return null
89
+ return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())
90
+ }
@@ -0,0 +1,131 @@
1
+ // Where a point lies against a layer's box and a mask's outline, in the layer's own coordinates.
2
+ import { getWorldMatrix } from '../coordinate'
3
+ import { geometryBlobPolylines } from '../geometry'
4
+ import type { SceneGraph } from '../graph'
5
+ import Matrix from '../matrix'
6
+ import type { Vector } from '../primitives'
7
+ import type { SceneNode } from '../types'
8
+
9
+ /**
10
+ * How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
11
+ * a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
12
+ */
13
+ const DEGENERATE_AXIS_SLOP = 4
14
+
15
+ function hasTransformedAncestor(
16
+ node: SceneNode,
17
+ graph: SceneGraph,
18
+ cache: Map<string, boolean>
19
+ ): boolean {
20
+ const cached = cache.get(node.id)
21
+ if (cached !== undefined) return cached
22
+ const parent = node.parentId ? graph.getNode(node.parentId) : undefined
23
+ const transformed =
24
+ node.rotation !== 0 ||
25
+ node.flipX ||
26
+ node.flipY ||
27
+ (parent ? hasTransformedAncestor(parent, graph, cache) : false)
28
+ cache.set(node.id, transformed)
29
+ return transformed
30
+ }
31
+
32
+ /**
33
+ * Where the point lies in the node's own coordinates: its offset from the position when nothing
34
+ * above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
35
+ */
36
+ export function localPoint(
37
+ px: number,
38
+ py: number,
39
+ node: SceneNode,
40
+ graph: SceneGraph,
41
+ transformCache: Map<string, boolean>
42
+ ): Vector | null {
43
+ if (!hasTransformedAncestor(node, graph, transformCache)) {
44
+ const absolute = graph.getAbsolutePosition(node.id)
45
+ return { x: px - absolute.x, y: py - absolute.y }
46
+ }
47
+ const inv = Matrix.invert(getWorldMatrix(node, graph))
48
+ if (!inv) return null
49
+ const [x, y] = Matrix.mapPoints(inv, [px, py])
50
+ return { x, y }
51
+ }
52
+
53
+ /** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
54
+ export function containsPoint(
55
+ px: number,
56
+ py: number,
57
+ node: SceneNode,
58
+ graph: SceneGraph,
59
+ transformCache: Map<string, boolean>,
60
+ pad = 0
61
+ ): boolean {
62
+ const local = localPoint(px, py, node, graph, transformCache)
63
+ if (!local) return false
64
+ const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
65
+ const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
66
+ return (
67
+ local.x >= -padX &&
68
+ local.x <= node.width + padX &&
69
+ local.y >= -padY &&
70
+ local.y <= node.height + padY
71
+ )
72
+ }
73
+
74
+ /** Whether a rounded rectangle of `width` × `height` holds the point. */
75
+ function roundedRectContains(
76
+ point: Vector,
77
+ width: number,
78
+ height: number,
79
+ radius: number
80
+ ): boolean {
81
+ if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false
82
+ const r = Math.max(0, Math.min(radius, width / 2, height / 2))
83
+ const cx = Math.min(Math.max(point.x, r), width - r)
84
+ const cy = Math.min(Math.max(point.y, r), height - r)
85
+ return Math.hypot(point.x - cx, point.y - cy) <= r
86
+ }
87
+
88
+ /** Whether the point is inside a closed polyline, by the even-odd rule. */
89
+ function insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {
90
+ let inside = false
91
+ for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
92
+ const a = polyline[i]
93
+ const b = polyline[j]
94
+ if (a.y > y === b.y > y) continue
95
+ if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside
96
+ }
97
+ return inside
98
+ }
99
+
100
+ /**
101
+ * Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
102
+ * rectangle's corners, or a vector's filled path. Other shapes clip by their box.
103
+ */
104
+ export function maskContainsPoint(
105
+ px: number,
106
+ py: number,
107
+ mask: SceneNode,
108
+ graph: SceneGraph,
109
+ transformCache: Map<string, boolean>
110
+ ): boolean {
111
+ const outlined =
112
+ (mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||
113
+ (mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||
114
+ (mask.type === 'VECTOR' && mask.fillGeometry.length > 0)
115
+ if (!outlined) return containsPoint(px, py, mask, graph, transformCache)
116
+ const local = localPoint(px, py, mask, graph, transformCache)
117
+ if (!local) return false
118
+ if (mask.type === 'ELLIPSE') {
119
+ const rx = mask.width / 2
120
+ const ry = mask.height / 2
121
+ const dx = (local.x - rx) / rx
122
+ const dy = (local.y - ry) / ry
123
+ return dx * dx + dy * dy <= 1
124
+ }
125
+ if (mask.type === 'RECTANGLE') {
126
+ return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)
127
+ }
128
+ return geometryBlobPolylines(mask.fillGeometry).some((points) =>
129
+ insidePolyline(points, local.x, local.y)
130
+ )
131
+ }
@@ -0,0 +1,57 @@
1
+ // The masks that clip a container's children, and whether they hide a point.
2
+ import type { SceneGraph } from '../graph'
3
+ import type { SceneNode } from '../types'
4
+ import { maskContainsPoint } from './geometry'
5
+
6
+ function isVisibleMask(node: SceneNode | undefined): node is SceneNode {
7
+ return node !== undefined && node.visible && node.isMask
8
+ }
9
+
10
+ /**
11
+ * The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
12
+ * a run of visible masks clips the layers after it up to the next visible mask. Empty when the
13
+ * container has no visible mask.
14
+ */
15
+ export function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {
16
+ const children = childIds.map((childId) => graph.nodes.get(childId))
17
+ if (!children.some(isVisibleMask)) return []
18
+ const result: SceneNode[][] = []
19
+ let run: SceneNode[] = []
20
+ let previousWasMask = false
21
+ for (const child of children) {
22
+ if (isVisibleMask(child)) {
23
+ run = previousWasMask ? [...run, child] : [child]
24
+ previousWasMask = true
25
+ result.push([])
26
+ continue
27
+ }
28
+ previousWasMask = false
29
+ result.push(run)
30
+ }
31
+ return result
32
+ }
33
+
34
+ /** Whether the masks that clip a layer leave the point outside every one of their outlines. */
35
+ export function isMaskedOut(
36
+ px: number,
37
+ py: number,
38
+ masks: readonly SceneNode[] | undefined,
39
+ graph: SceneGraph,
40
+ transformCache: Map<string, boolean>
41
+ ): boolean {
42
+ if (!masks) return false
43
+ return (
44
+ masks.length > 0 &&
45
+ !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))
46
+ )
47
+ }
48
+
49
+ /** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */
50
+ export function isPointMaskedOut(
51
+ graph: SceneGraph,
52
+ masks: readonly SceneNode[] | undefined,
53
+ px: number,
54
+ py: number
55
+ ): boolean {
56
+ return isMaskedOut(px, py, masks, graph, new Map())
57
+ }
@@ -0,0 +1,24 @@
1
+ // Which layers show a paint where a pointer lands, so their area takes it.
2
+ import type { SceneNode } from '../types'
3
+
4
+ /**
5
+ * Whether a paint shows, so its container's empty area counts when a click reaches past
6
+ * containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
7
+ * while 1% does, and the layer's own opacity does not matter.
8
+ */
9
+ function isHitPaint(paint: SceneNode['fills'][number]): boolean {
10
+ if (!paint.visible || !(paint.opacity > 0)) return false
11
+ if (paint.type === 'SOLID') return paint.color.a > 0
12
+ if (
13
+ paint.type === 'GRADIENT_LINEAR' ||
14
+ paint.type === 'GRADIENT_RADIAL' ||
15
+ paint.type === 'GRADIENT_ANGULAR' ||
16
+ paint.type === 'GRADIENT_DIAMOND'
17
+ )
18
+ return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0)
19
+ return true
20
+ }
21
+
22
+ export function showsPaint(node: SceneNode): boolean {
23
+ return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)
24
+ }
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',
@@ -17,34 +23,6 @@ const CONTAINER_TYPES = new Set<NodeType>([
17
23
  'INSTANCE'
18
24
  ])
19
25
  const OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])
20
- /**
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`).
23
- */
24
- const DEGENERATE_AXIS_SLOP = 4
25
-
26
- /**
27
- * Whether a paint shows, so its container's empty area counts when a click reaches past
28
- * containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
29
- * while 1% does, and the layer's own opacity does not matter.
30
- */
31
- function isHitPaint(paint: SceneNode['fills'][number]): boolean {
32
- if (!paint.visible || !(paint.opacity > 0)) return false
33
- if (paint.type === 'SOLID') return paint.color.a > 0
34
- if (
35
- paint.type === 'GRADIENT_LINEAR' ||
36
- paint.type === 'GRADIENT_RADIAL' ||
37
- paint.type === 'GRADIENT_ANGULAR' ||
38
- paint.type === 'GRADIENT_DIAMOND'
39
- )
40
- return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0)
41
- return true
42
- }
43
-
44
- function showsPaint(node: SceneNode): boolean {
45
- return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)
46
- }
47
-
48
26
  /**
49
27
  * Whether a container's empty area takes the click. Figma counts it only where a paint shows,
50
28
  * except a main component's, which always counts, and an instance's when the click is a plain one.
@@ -69,152 +47,6 @@ function ownsItsEmptyArea(graph: SceneGraph, node: SceneNode): boolean {
69
47
  return node.type === 'FRAME' && node.layoutMode !== 'NONE' && isTopLevel(graph, node)
70
48
  }
71
49
 
72
- function hasTransformedAncestor(
73
- node: SceneNode,
74
- graph: SceneGraph,
75
- cache: Map<string, boolean>
76
- ): boolean {
77
- const cached = cache.get(node.id)
78
- if (cached !== undefined) return cached
79
- const parent = node.parentId ? graph.getNode(node.parentId) : undefined
80
- const transformed =
81
- node.rotation !== 0 ||
82
- node.flipX ||
83
- node.flipY ||
84
- (parent ? hasTransformedAncestor(parent, graph, cache) : false)
85
- cache.set(node.id, transformed)
86
- return transformed
87
- }
88
-
89
- /**
90
- * Where the point lies in the node's own coordinates: its offset from the position when nothing
91
- * above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
92
- */
93
- function localPoint(
94
- px: number,
95
- py: number,
96
- node: SceneNode,
97
- graph: SceneGraph,
98
- transformCache: Map<string, boolean>
99
- ): Vector | null {
100
- if (!hasTransformedAncestor(node, graph, transformCache)) {
101
- const absolute = graph.getAbsolutePosition(node.id)
102
- return { x: px - absolute.x, y: py - absolute.y }
103
- }
104
- const inv = Matrix.invert(getWorldMatrix(node, graph))
105
- if (!inv) return null
106
- const [x, y] = Matrix.mapPoints(inv, [px, py])
107
- return { x, y }
108
- }
109
-
110
- /** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
111
- function containsPoint(
112
- px: number,
113
- py: number,
114
- node: SceneNode,
115
- graph: SceneGraph,
116
- transformCache: Map<string, boolean>,
117
- pad = 0
118
- ): boolean {
119
- const local = localPoint(px, py, node, graph, transformCache)
120
- if (!local) return false
121
- const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
122
- const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
123
- return (
124
- local.x >= -padX &&
125
- local.x <= node.width + padX &&
126
- local.y >= -padY &&
127
- local.y <= node.height + padY
128
- )
129
- }
130
-
131
- /** Whether a rounded rectangle of `width` × `height` holds the point. */
132
- function roundedRectContains(
133
- point: Vector,
134
- width: number,
135
- height: number,
136
- radius: number
137
- ): boolean {
138
- if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false
139
- const r = Math.max(0, Math.min(radius, width / 2, height / 2))
140
- const cx = Math.min(Math.max(point.x, r), width - r)
141
- const cy = Math.min(Math.max(point.y, r), height - r)
142
- return Math.hypot(point.x - cx, point.y - cy) <= r
143
- }
144
-
145
- /** Whether the point is inside a closed polyline, by the even-odd rule. */
146
- function insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {
147
- let inside = false
148
- for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
149
- const a = polyline[i]
150
- const b = polyline[j]
151
- if (a.y > y === b.y > y) continue
152
- if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside
153
- }
154
- return inside
155
- }
156
-
157
- /**
158
- * Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
159
- * rectangle's corners, or a vector's filled path. Other shapes clip by their box.
160
- */
161
- function maskContainsPoint(
162
- px: number,
163
- py: number,
164
- mask: SceneNode,
165
- graph: SceneGraph,
166
- transformCache: Map<string, boolean>
167
- ): boolean {
168
- const outlined =
169
- (mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||
170
- (mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||
171
- (mask.type === 'VECTOR' && mask.fillGeometry.length > 0)
172
- if (!outlined) return containsPoint(px, py, mask, graph, transformCache)
173
- const local = localPoint(px, py, mask, graph, transformCache)
174
- if (!local) return false
175
- if (mask.type === 'ELLIPSE') {
176
- const rx = mask.width / 2
177
- const ry = mask.height / 2
178
- const dx = (local.x - rx) / rx
179
- const dy = (local.y - ry) / ry
180
- return dx * dx + dy * dy <= 1
181
- }
182
- if (mask.type === 'RECTANGLE') {
183
- return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)
184
- }
185
- return geometryBlobPolylines(mask.fillGeometry).some((points) =>
186
- insidePolyline(points, local.x, local.y)
187
- )
188
- }
189
-
190
- function isVisibleMask(node: SceneNode | undefined): node is SceneNode {
191
- return node !== undefined && node.visible && node.isMask
192
- }
193
-
194
- /**
195
- * The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
196
- * a run of visible masks clips the layers after it up to the next visible mask. Empty when the
197
- * container has no visible mask.
198
- */
199
- function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {
200
- const children = childIds.map((childId) => graph.nodes.get(childId))
201
- if (!children.some(isVisibleMask)) return []
202
- const result: SceneNode[][] = []
203
- let run: SceneNode[] = []
204
- let previousWasMask = false
205
- for (const child of children) {
206
- if (isVisibleMask(child)) {
207
- run = previousWasMask ? [...run, child] : [child]
208
- previousWasMask = true
209
- result.push([])
210
- continue
211
- }
212
- previousWasMask = false
213
- result.push(run)
214
- }
215
- return result
216
- }
217
-
218
50
  /**
219
51
  * What a click inside a container reports. `opaque` is an instance or component whose body takes
220
52
  * the click without looking through it; every other container looks through to its children first.
@@ -295,21 +127,6 @@ function standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode
295
127
  return container.locked || !deep ? container : hit
296
128
  }
297
129
 
298
- /** Whether the masks that clip a layer leave the point outside every one of their outlines. */
299
- function isMaskedOut(
300
- px: number,
301
- py: number,
302
- masks: readonly SceneNode[] | undefined,
303
- graph: SceneGraph,
304
- transformCache: Map<string, boolean>
305
- ): boolean {
306
- if (!masks) return false
307
- return (
308
- masks.length > 0 &&
309
- !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))
310
- )
311
- }
312
-
313
130
  /**
314
131
  * The topmost layer under the point among a container's children. `pad`, in canvas units, grows
315
132
  * each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
@@ -634,89 +451,3 @@ export function isPointInNode(graph: SceneGraph, nodeId: string, px: number, py:
634
451
  const node = graph.nodes.get(nodeId)
635
452
  return node !== undefined && containsPoint(px, py, node, graph, new Map())
636
453
  }
637
-
638
- /** Which layers take a drop, and which are only looked through, as in Figma. */
639
- interface DropRules {
640
- takes: (node: SceneNode) => boolean
641
- passThrough: ReadonlySet<NodeType>
642
- }
643
-
644
- const LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])
645
- const COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])
646
-
647
- function dropRules(options: DropTargetOptions): DropRules {
648
- const { componentSetIds } = options
649
- if (!componentSetIds) {
650
- return {
651
- takes: (node) => LAYER_TARGETS.has(node.type),
652
- passThrough: new Set(['GROUP', 'COMPONENT_SET'])
653
- }
654
- }
655
- // Components never go into other components, and a set only takes back its own variants.
656
- return {
657
- takes: (node) =>
658
- COMPONENT_TARGETS.has(node.type) ||
659
- (node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),
660
- passThrough: new Set(['GROUP'])
661
- }
662
- }
663
-
664
- function dropTargetIn(
665
- graph: SceneGraph,
666
- px: number,
667
- py: number,
668
- parent: SceneNode,
669
- excludeIds: ReadonlySet<string>,
670
- rules: DropRules,
671
- transformCache: Map<string, boolean>
672
- ): SceneNode | null {
673
- // A clipped-away part of a child is not under the cursor.
674
- if (
675
- parent.type !== 'CANVAS' &&
676
- parent.clipsContent &&
677
- !containsPoint(px, py, parent, graph, transformCache)
678
- )
679
- return null
680
-
681
- for (let i = parent.childIds.length - 1; i >= 0; i--) {
682
- const childId = parent.childIds[i]
683
- if (excludeIds.has(childId)) continue
684
- const child = graph.nodes.get(childId)
685
- if (!child || child.internalOnly || !child.visible || child.locked) continue
686
- const target = rules.takes(child)
687
- if (!target && !rules.passThrough.has(child.type)) continue
688
-
689
- const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)
690
- if (deeper) return deeper
691
- if (target && containsPoint(px, py, child, graph, transformCache)) return child
692
- }
693
-
694
- return null
695
- }
696
-
697
- export interface DropTargetOptions {
698
- /**
699
- * Set when the dropped layers are all main components: the component sets they come from.
700
- * Components then go only into frames, sections, instances, and those sets.
701
- */
702
- componentSetIds?: ReadonlySet<string>
703
- }
704
-
705
- /**
706
- * The topmost unlocked frame, section, component, or instance under the point, in its rotated
707
- * shape and inside its clipping ancestors. Groups and component sets are looked through but never
708
- * returned, except that a set takes dropped components; locked layers and their contents are
709
- * skipped.
710
- */
711
- export function hitTestDropTarget(
712
- graph: SceneGraph,
713
- px: number,
714
- py: number,
715
- excludeIds: ReadonlySet<string>,
716
- scopeId?: string,
717
- options: DropTargetOptions = {}
718
- ): SceneNode | null {
719
- const scope = graph.nodes.get(scopeId ?? graph.rootId)
720
- if (!scope) return null
721
- return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())
722
- }
@@ -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,