@pairdraw/scene-graph 0.19.2 → 0.21.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 (285) hide show
  1. package/dist/color/index.d.ts +28 -7
  2. package/dist/color/index.d.ts.map +1 -1
  3. package/dist/color/index.js +40 -15
  4. package/dist/color/index.js.map +1 -1
  5. package/dist/color/management.d.ts +19 -6
  6. package/dist/color/management.d.ts.map +1 -1
  7. package/dist/color/management.js +56 -1
  8. package/dist/color/management.js.map +1 -1
  9. package/dist/color/profile.d.ts +32 -0
  10. package/dist/color/profile.d.ts.map +1 -0
  11. package/dist/color/profile.js +140 -0
  12. package/dist/color/profile.js.map +1 -0
  13. package/dist/comments/index.d.ts +2 -2
  14. package/dist/comments/index.js +2 -2
  15. package/dist/comments/merge.d.ts.map +1 -1
  16. package/dist/comments/merge.js +1 -0
  17. package/dist/comments/merge.js.map +1 -1
  18. package/dist/comments/schema.d.ts +3 -1
  19. package/dist/comments/schema.d.ts.map +1 -1
  20. package/dist/comments/schema.js +19 -2
  21. package/dist/comments/schema.js.map +1 -1
  22. package/dist/comments/threads.d.ts +3 -1
  23. package/dist/comments/threads.d.ts.map +1 -1
  24. package/dist/comments/threads.js +2 -1
  25. package/dist/comments/threads.js.map +1 -1
  26. package/dist/components/index.d.ts +2 -2
  27. package/dist/components/index.js +2 -2
  28. package/dist/components/properties.d.ts.map +1 -1
  29. package/dist/components/properties.js +7 -7
  30. package/dist/components/properties.js.map +1 -1
  31. package/dist/components/variants.d.ts +12 -1
  32. package/dist/components/variants.d.ts.map +1 -1
  33. package/dist/components/variants.js +30 -1
  34. package/dist/components/variants.js.map +1 -1
  35. package/dist/constants.d.ts +25 -1
  36. package/dist/constants.d.ts.map +1 -1
  37. package/dist/constants.js +94 -1
  38. package/dist/constants.js.map +1 -1
  39. package/dist/copy.d.ts.map +1 -1
  40. package/dist/copy.js +17 -1
  41. package/dist/copy.js.map +1 -1
  42. package/dist/css/index.d.ts +2 -2
  43. package/dist/css/index.js +2 -2
  44. package/dist/css/shadows.d.ts +2 -2
  45. package/dist/css/shadows.d.ts.map +1 -1
  46. package/dist/css/shadows.js +4 -4
  47. package/dist/css/shadows.js.map +1 -1
  48. package/dist/css/text.d.ts +17 -1
  49. package/dist/css/text.d.ts.map +1 -1
  50. package/dist/css/text.js +62 -1
  51. package/dist/css/text.js.map +1 -1
  52. package/dist/css/values.d.ts +7 -2
  53. package/dist/css/values.d.ts.map +1 -1
  54. package/dist/css/values.js +8 -4
  55. package/dist/css/values.js.map +1 -1
  56. package/dist/design-memory/index.d.ts +3 -0
  57. package/dist/design-memory/index.js +3 -0
  58. package/dist/design-memory/model.d.ts +33 -0
  59. package/dist/design-memory/model.d.ts.map +1 -0
  60. package/dist/design-memory/model.js +142 -0
  61. package/dist/design-memory/model.js.map +1 -0
  62. package/dist/design-memory/schema.d.ts +76 -0
  63. package/dist/design-memory/schema.d.ts.map +1 -0
  64. package/dist/design-memory/schema.js +72 -0
  65. package/dist/design-memory/schema.js.map +1 -0
  66. package/dist/effects.d.ts +7 -4
  67. package/dist/effects.d.ts.map +1 -1
  68. package/dist/effects.js +9 -4
  69. package/dist/effects.js.map +1 -1
  70. package/dist/empty-values.js +102 -0
  71. package/dist/empty-values.js.map +1 -0
  72. package/dist/fields/components.d.ts +9 -0
  73. package/dist/fields/components.d.ts.map +1 -0
  74. package/dist/fields/components.js +24 -0
  75. package/dist/fields/components.js.map +1 -0
  76. package/dist/fields/index.d.ts +3 -2
  77. package/dist/fields/index.js +3 -2
  78. package/dist/fields/layout.d.ts +3 -1
  79. package/dist/fields/layout.d.ts.map +1 -1
  80. package/dist/fields/layout.js +10 -1
  81. package/dist/fields/layout.js.map +1 -1
  82. package/dist/graph.d.ts +12 -1
  83. package/dist/graph.d.ts.map +1 -1
  84. package/dist/graph.js +39 -7
  85. package/dist/graph.js.map +1 -1
  86. package/dist/grid-values.d.ts +10 -0
  87. package/dist/grid-values.d.ts.map +1 -0
  88. package/dist/grid-values.js +53 -0
  89. package/dist/grid-values.js.map +1 -0
  90. package/dist/hit-test/drop.d.ts +20 -0
  91. package/dist/hit-test/drop.d.ts.map +1 -0
  92. package/dist/hit-test/drop.js +54 -0
  93. package/dist/hit-test/drop.js.map +1 -0
  94. package/dist/hit-test/geometry.js +86 -0
  95. package/dist/hit-test/geometry.js.map +1 -0
  96. package/dist/hit-test/masks.d.ts +14 -0
  97. package/dist/hit-test/masks.d.ts.map +1 -0
  98. package/dist/hit-test/masks.js +41 -0
  99. package/dist/hit-test/masks.js.map +1 -0
  100. package/dist/hit-test/paint.d.ts +6 -0
  101. package/dist/hit-test/paint.d.ts.map +1 -0
  102. package/dist/hit-test/paint.js +19 -0
  103. package/dist/hit-test/paint.js.map +1 -0
  104. package/dist/hit-test.d.ts +4 -15
  105. package/dist/hit-test.d.ts.map +1 -1
  106. package/dist/hit-test.js +5 -173
  107. package/dist/hit-test.js.map +1 -1
  108. package/dist/images.d.ts +3 -2
  109. package/dist/images.d.ts.map +1 -1
  110. package/dist/images.js +18 -2
  111. package/dist/images.js.map +1 -1
  112. package/dist/index.d.ts +2 -2
  113. package/dist/instances/copies.js +2 -1
  114. package/dist/instances/copies.js.map +1 -1
  115. package/dist/instances/fields.d.ts.map +1 -1
  116. package/dist/instances/fields.js +2 -0
  117. package/dist/instances/fields.js.map +1 -1
  118. package/dist/instances/index.d.ts +2 -1
  119. package/dist/instances/index.d.ts.map +1 -1
  120. package/dist/instances/index.js +13 -12
  121. package/dist/instances/index.js.map +1 -1
  122. package/dist/instances/overrides.d.ts +15 -1
  123. package/dist/instances/overrides.d.ts.map +1 -1
  124. package/dist/instances/overrides.js +37 -1
  125. package/dist/instances/overrides.js.map +1 -1
  126. package/dist/instances/source-walk.d.ts +12 -0
  127. package/dist/instances/source-walk.d.ts.map +1 -0
  128. package/dist/instances/swap-overrides.js +3 -3
  129. package/dist/instances/swap-overrides.js.map +1 -1
  130. package/dist/instances/sync.js +9 -7
  131. package/dist/instances/sync.js.map +1 -1
  132. package/dist/node-defaults.d.ts.map +1 -1
  133. package/dist/node-defaults.js +43 -38
  134. package/dist/node-defaults.js.map +1 -1
  135. package/dist/plugin-data/fields.d.ts +90 -0
  136. package/dist/plugin-data/fields.d.ts.map +1 -1
  137. package/dist/plugin-data/fields.js +33 -24
  138. package/dist/plugin-data/fields.js.map +1 -1
  139. package/dist/plugin-data/index.d.ts +2 -2
  140. package/dist/plugin-data/index.js +2 -2
  141. package/dist/plugin-data/schema.d.ts +6 -1
  142. package/dist/plugin-data/schema.d.ts.map +1 -1
  143. package/dist/plugin-data/schema.js +24 -1
  144. package/dist/plugin-data/schema.js.map +1 -1
  145. package/dist/preview.d.ts.map +1 -1
  146. package/dist/preview.js +2 -0
  147. package/dist/preview.js.map +1 -1
  148. package/dist/scaling/node.d.ts.map +1 -1
  149. package/dist/scaling/node.js +5 -1
  150. package/dist/scaling/node.js.map +1 -1
  151. package/dist/shaders/animation.d.ts +21 -0
  152. package/dist/shaders/animation.d.ts.map +1 -0
  153. package/dist/shaders/animation.js +25 -0
  154. package/dist/shaders/animation.js.map +1 -0
  155. package/dist/shaders/index.d.ts +2 -1
  156. package/dist/shaders/index.js +2 -1
  157. package/dist/slots/content.d.ts.map +1 -1
  158. package/dist/slots/content.js +4 -4
  159. package/dist/slots/content.js.map +1 -1
  160. package/dist/statuses/index.d.ts +2 -2
  161. package/dist/statuses/index.js +2 -2
  162. package/dist/statuses/workflow.d.ts +3 -1
  163. package/dist/statuses/workflow.d.ts.map +1 -1
  164. package/dist/statuses/workflow.js +6 -2
  165. package/dist/statuses/workflow.js.map +1 -1
  166. package/dist/token-sources/document.d.ts +26 -0
  167. package/dist/token-sources/document.d.ts.map +1 -0
  168. package/dist/token-sources/document.js +80 -0
  169. package/dist/token-sources/document.js.map +1 -0
  170. package/dist/token-sources/index.d.ts +3 -0
  171. package/dist/token-sources/index.js +3 -0
  172. package/dist/token-sources/schema.d.ts +43 -0
  173. package/dist/token-sources/schema.d.ts.map +1 -0
  174. package/dist/token-sources/schema.js +50 -0
  175. package/dist/token-sources/schema.js.map +1 -0
  176. package/dist/transfer/variables.d.ts.map +1 -1
  177. package/dist/transfer/variables.js +1 -1
  178. package/dist/transfer/variables.js.map +1 -1
  179. package/dist/types.d.ts +135 -15
  180. package/dist/types.d.ts.map +1 -1
  181. package/dist/types.js.map +1 -1
  182. package/dist/variables/bindings.d.ts +6 -5
  183. package/dist/variables/bindings.d.ts.map +1 -1
  184. package/dist/variables/bindings.js +24 -16
  185. package/dist/variables/bindings.js.map +1 -1
  186. package/dist/variables/formula/index.d.ts.map +1 -1
  187. package/dist/variables/formula/index.js +12 -6
  188. package/dist/variables/formula/index.js.map +1 -1
  189. package/dist/variables/index.d.ts +14 -1
  190. package/dist/variables/index.d.ts.map +1 -1
  191. package/dist/variables/index.js +38 -10
  192. package/dist/variables/index.js.map +1 -1
  193. package/dist/variables/paints.d.ts +9 -8
  194. package/dist/variables/paints.d.ts.map +1 -1
  195. package/dist/variables/paints.js +35 -26
  196. package/dist/variables/paints.js.map +1 -1
  197. package/dist/variables/resolver.d.ts +45 -0
  198. package/dist/variables/resolver.d.ts.map +1 -0
  199. package/dist/variables/resolver.js +106 -0
  200. package/dist/variables/resolver.js.map +1 -0
  201. package/dist/variations/copy.d.ts +16 -0
  202. package/dist/variations/copy.d.ts.map +1 -0
  203. package/dist/variations/copy.js +78 -0
  204. package/dist/variations/copy.js.map +1 -0
  205. package/dist/variations/index.d.ts +6 -0
  206. package/dist/variations/index.js +6 -0
  207. package/dist/variations/match.d.ts +23 -0
  208. package/dist/variations/match.d.ts.map +1 -0
  209. package/dist/variations/match.js +80 -0
  210. package/dist/variations/match.js.map +1 -0
  211. package/dist/variations/model.d.ts +39 -0
  212. package/dist/variations/model.d.ts.map +1 -0
  213. package/dist/variations/model.js +90 -0
  214. package/dist/variations/model.js.map +1 -0
  215. package/dist/variations/pick.d.ts +43 -0
  216. package/dist/variations/pick.d.ts.map +1 -0
  217. package/dist/variations/pick.js +211 -0
  218. package/dist/variations/pick.js.map +1 -0
  219. package/dist/variations/schema.d.ts +27 -0
  220. package/dist/variations/schema.d.ts.map +1 -0
  221. package/dist/variations/schema.js +27 -0
  222. package/dist/variations/schema.js.map +1 -0
  223. package/package.json +21 -1
  224. package/src/color/index.ts +43 -9
  225. package/src/color/management.ts +54 -1
  226. package/src/color/profile.ts +131 -0
  227. package/src/comments/merge.ts +3 -0
  228. package/src/comments/schema.ts +32 -2
  229. package/src/comments/threads.ts +5 -2
  230. package/src/components/properties.ts +13 -7
  231. package/src/components/variants.ts +39 -0
  232. package/src/constants.ts +75 -0
  233. package/src/copy.ts +20 -1
  234. package/src/css/index.ts +7 -1
  235. package/src/css/shadows.ts +8 -5
  236. package/src/css/text.ts +62 -0
  237. package/src/css/values.ts +12 -5
  238. package/src/design-memory/index.ts +2 -0
  239. package/src/design-memory/model.ts +143 -0
  240. package/src/design-memory/schema.ts +84 -0
  241. package/src/effects.ts +16 -7
  242. package/src/empty-values.ts +117 -0
  243. package/src/fields/components.ts +21 -0
  244. package/src/fields/index.ts +1 -0
  245. package/src/fields/layout.ts +10 -0
  246. package/src/graph.ts +50 -6
  247. package/src/grid-values.ts +47 -0
  248. package/src/hit-test/drop.ts +90 -0
  249. package/src/hit-test/geometry.ts +131 -0
  250. package/src/hit-test/masks.ts +57 -0
  251. package/src/hit-test/paint.ts +24 -0
  252. package/src/hit-test.ts +8 -277
  253. package/src/images.ts +27 -3
  254. package/src/instances/copies.ts +2 -1
  255. package/src/instances/fields.ts +2 -0
  256. package/src/instances/index.ts +20 -11
  257. package/src/instances/overrides.ts +49 -0
  258. package/src/instances/swap-overrides.ts +3 -2
  259. package/src/instances/sync.ts +11 -7
  260. package/src/node-defaults.ts +51 -41
  261. package/src/plugin-data/fields.ts +34 -32
  262. package/src/plugin-data/index.ts +1 -0
  263. package/src/plugin-data/schema.ts +30 -0
  264. package/src/preview.ts +2 -0
  265. package/src/scaling/node.ts +7 -2
  266. package/src/shaders/animation.ts +39 -0
  267. package/src/shaders/index.ts +1 -0
  268. package/src/slots/content.ts +8 -4
  269. package/src/statuses/workflow.ts +6 -4
  270. package/src/token-sources/document.ts +104 -0
  271. package/src/token-sources/index.ts +2 -0
  272. package/src/token-sources/schema.ts +59 -0
  273. package/src/transfer/variables.ts +2 -1
  274. package/src/types.ts +145 -14
  275. package/src/variables/bindings.ts +35 -18
  276. package/src/variables/formula/index.ts +19 -18
  277. package/src/variables/index.ts +49 -10
  278. package/src/variables/paints.ts +64 -29
  279. package/src/variables/resolver.ts +178 -0
  280. package/src/variations/copy.ts +89 -0
  281. package/src/variations/index.ts +5 -0
  282. package/src/variations/match.ts +100 -0
  283. package/src/variations/model.ts +132 -0
  284. package/src/variations/pick.ts +293 -0
  285. package/src/variations/schema.ts +29 -0
@@ -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
- }
package/src/images.ts CHANGED
@@ -13,7 +13,7 @@ export function computeImageHash(data: Uint8Array): string {
13
13
  return bytesToHex(sha1(data))
14
14
  }
15
15
 
16
- export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp'
16
+ export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp' | 'image/avif'
17
17
 
18
18
  /** The image types documents store, by the name `image-size` gives each format. */
19
19
  const IMAGE_MIME_TYPES: Partial<Record<string, ImageMimeType>> = {
@@ -58,12 +58,36 @@ export function imageHeader(data: Uint8Array): ImageHeader | null {
58
58
  return type ? { type, width: header.width, height: header.height } : null
59
59
  }
60
60
 
61
+ /** A four-character ISO media brand at `start`, or an empty string when the bytes are short. */
62
+ function isoBrand(data: Uint8Array, start: number): string {
63
+ if (start + 4 > data.length) return ''
64
+ return String.fromCharCode(
65
+ data[start] ?? 0,
66
+ data[start + 1] ?? 0,
67
+ data[start + 2] ?? 0,
68
+ data[start + 3] ?? 0
69
+ )
70
+ }
71
+
72
+ /** Whether bytes are an AVIF image, whose `ftyp` box names the brand or lists it as compatible. */
73
+ function isAVIF(data: Uint8Array): boolean {
74
+ if (isoBrand(data, 4) !== 'ftyp') return false
75
+ const size = ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]) >>> 0
76
+ const isAvif = (brand: string) => brand === 'avif' || brand === 'avis'
77
+ if (isAvif(isoBrand(data, 8))) return true
78
+ const end = Math.min(size >= 16 ? size : data.length, data.length)
79
+ // The major brand is followed by a minor version, then the compatible brands.
80
+ for (let start = 16; start + 4 <= end; start += 4) if (isAvif(isoBrand(data, start))) return true
81
+ return false
82
+ }
83
+
61
84
  /**
62
85
  * The media type of encoded image bytes, read from their header, for writing them out as a data
63
- * URL or file. Bytes it cannot read are reported as PNG, as image fills began.
86
+ * URL or file. AVIF is read from its ISO media brand, which `image-size` does not decode. Bytes it
87
+ * cannot read are reported as PNG, as image fills began.
64
88
  */
65
89
  export function imageMimeType(data: Uint8Array): ImageMimeType {
66
- return imageHeader(data)?.type ?? 'image/png'
90
+ return imageHeader(data)?.type ?? (isAVIF(data) ? 'image/avif' : 'image/png')
67
91
  }
68
92
 
69
93
  /** An image's size in pixels, read from its header; null for bytes it cannot read. */
@@ -228,7 +228,8 @@ export function removeLeftoverCopies(
228
228
  const overrides = new Map<string, Map<string, unknown>>()
229
229
  for (const removed of nodes) {
230
230
  const fields = context.overrides.descendants.get(removed.id)
231
- if (fields) overrides.set(removed.id, new Map(fields))
231
+ if (!fields) continue
232
+ overrides.set(removed.id, new Map(fields))
232
233
  context.overrides.descendants.delete(removed.id)
233
234
  }
234
235
  let copies = store.byInstance.get(context.instanceId)
@@ -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',
@@ -24,6 +24,7 @@ import {
24
24
  hasInstanceOverride as hasNodeInstanceOverride,
25
25
  type InstanceOverrideState,
26
26
  overriddenAssignmentId,
27
+ ownInstanceOverrides,
27
28
  REPLACED_LAYER_OVERRIDE,
28
29
  setInstanceOverride,
29
30
  variableModeOverrideField
@@ -46,6 +47,7 @@ import {
46
47
  } from './sync'
47
48
 
48
49
  export { canCreateInstance, createsComponentCycle, cycleSafeParent } from './cycles'
50
+ export { walkInstanceSources } from './source-walk'
49
51
  export { instanceMainComponent } from './main-component'
50
52
  export {
51
53
  isInShownReplacedLayer,
@@ -133,14 +135,20 @@ function recordNestedSwap(graph: SceneGraph, instance: SceneNode, componentId: s
133
135
  )
134
136
  if (typeof existing !== 'string' && instance.componentId)
135
137
  setInstanceOverride(
136
- owner.instanceOverrides,
138
+ ownInstanceOverrides(owner),
137
139
  owner.id,
138
140
  instance.id,
139
141
  'sourceComponentId',
140
142
  instance.componentId
141
143
  )
142
- setInstanceOverride(owner.instanceOverrides, owner.id, instance.id, 'componentId', componentId)
143
- graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
144
+ setInstanceOverride(
145
+ ownInstanceOverrides(owner),
146
+ owner.id,
147
+ instance.id,
148
+ 'componentId',
149
+ componentId
150
+ )
151
+ graph.updateNode(owner.id, { instanceOverrides: ownInstanceOverrides(owner) })
144
152
  }
145
153
 
146
154
  export interface SwapInstanceOptions {
@@ -298,8 +306,9 @@ function syncInstancesOf(
298
306
  updateSyncedProps(graph, instance, updates, (key) =>
299
307
  isProtectedSyncField(instance, key, protectedField)
300
308
  )
301
- const context = createInstanceSyncContext(instance.id, instance.instanceOverrides)
302
- syncChildren(graph, component.id, instance.id, instance.instanceOverrides, context)
309
+ const owned = ownInstanceOverrides(instance)
310
+ const context = createInstanceSyncContext(instance.id, owned)
311
+ syncChildren(graph, component.id, instance.id, owned, context)
303
312
  removeLeftoverCopies(graph, context, removals)
304
313
  }
305
314
  })
@@ -525,7 +534,7 @@ export function recordAssignmentOverride(
525
534
  const outer = assignmentOverrideOwner(graph, instanceId)
526
535
  if (!outer) return
527
536
  setInstanceOverride(
528
- outer.instanceOverrides,
537
+ ownInstanceOverrides(outer),
529
538
  outer.id,
530
539
  instanceId,
531
540
  assignmentOverrideField(propertyId),
@@ -593,9 +602,9 @@ function forgetOverridesOf(graph: SceneGraph, nodeId: string): void {
593
602
  if (!parentId) return
594
603
  graph.closest(parentId, (ancestor) => {
595
604
  if (ancestor.type !== 'INSTANCE') return false
596
- const { descendants } = ancestor.instanceOverrides
597
- if (ids.some((id) => descendants.delete(id)))
598
- graph.updateNode(ancestor.id, { instanceOverrides: ancestor.instanceOverrides })
605
+ const owned = ownInstanceOverrides(ancestor)
606
+ if (ids.some((id) => owned.descendants.delete(id)))
607
+ graph.updateNode(ancestor.id, { instanceOverrides: owned })
599
608
  return false
600
609
  })
601
610
  }
@@ -662,13 +671,13 @@ export function replaceInstanceLayer(
662
671
  })
663
672
  graph.deleteNode(target.id)
664
673
  setInstanceOverride(
665
- owner.instanceOverrides,
674
+ ownInstanceOverrides(owner),
666
675
  owner.id,
667
676
  replacementId,
668
677
  REPLACED_LAYER_OVERRIDE,
669
678
  layerId
670
679
  )
671
- graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
680
+ graph.updateNode(owner.id, { instanceOverrides: ownInstanceOverrides(owner) })
672
681
  return true
673
682
  }
674
683