@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.
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/spec.d.ts +6 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +62 -5
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/canvas-layout.js +66 -0
- package/dist/canvas-layout.js.map +1 -0
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/coordinate.js +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/text.d.ts +22 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +81 -1
- package/dist/css/text.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +93 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/graph.d.ts +8 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +36 -7
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/group-bounds.d.ts +4 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +11 -1
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +33 -178
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +1 -1
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/node-defaults.d.ts +21 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +63 -50
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +75 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +18 -1
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +3 -6
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +28 -4
- package/dist/scaling/node.js.map +1 -1
- package/dist/shared-styles.d.ts +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- package/dist/shared-styles.js.map +1 -1
- package/dist/text/path.js +11 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/transfer/copied.d.ts +14 -0
- package/dist/transfer/copied.d.ts.map +1 -0
- package/dist/transfer/copied.js +52 -0
- package/dist/transfer/copied.js.map +1 -0
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +2 -1
- package/dist/transfer/nodes.js +7 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +104 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +6 -0
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +13 -0
- package/dist/undo.js.map +1 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js.map +1 -1
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/behaviours/spec.ts +94 -4
- package/src/canvas-layout.ts +76 -0
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +12 -1
- package/src/css/text.ts +86 -0
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +101 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/generated/shape-with-text.ts +28 -2
- package/src/graph.ts +48 -6
- package/src/grid-values.ts +47 -0
- package/src/group-bounds.ts +13 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +78 -291
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +1 -0
- package/src/node-defaults.ts +71 -52
- package/src/plugin-data/fields.ts +18 -1
- package/src/preview.ts +2 -0
- package/src/resize.ts +3 -14
- package/src/scaling/node.ts +34 -5
- package/src/shared-styles.ts +8 -0
- package/src/text/path.ts +14 -0
- package/src/text/picture.ts +5 -2
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/transfer/copied.ts +72 -0
- package/src/transfer/index.ts +1 -0
- package/src/transfer/nodes.ts +10 -0
- package/src/types.ts +117 -14
- package/src/undo.ts +11 -0
- package/src/variables/index.ts +1 -4
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- 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
|
|
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
|
-
*
|
|
22
|
-
* a
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
*
|
|
64
|
-
*
|
|
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
|
|
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(
|
|
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(
|
|
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
|
-
(
|
|
117
|
+
takesEmptyArea(graph, child, instanceBodies)
|
|
241
118
|
)
|
|
242
119
|
return child
|
|
243
120
|
return null
|
|
244
121
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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 =
|
|
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 =
|
|
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
|
|
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
|
-
}
|
package/src/instances/fields.ts
CHANGED
package/src/instances/index.ts
CHANGED
|
@@ -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,
|