@open-pencil/scene-graph 0.13.2 → 0.15.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 (130) hide show
  1. package/dist/arrow-caps.d.ts +44 -0
  2. package/dist/arrow-caps.d.ts.map +1 -0
  3. package/dist/arrow-caps.js +131 -0
  4. package/dist/arrow-caps.js.map +1 -0
  5. package/dist/chunks/checkpoint.js +78 -0
  6. package/dist/chunks/checkpoint.js.map +1 -0
  7. package/dist/chunks/components/properties.js +1571 -0
  8. package/dist/chunks/components/properties.js.map +1 -0
  9. package/dist/chunks/coordinate.js +87 -33
  10. package/dist/chunks/coordinate.js.map +1 -1
  11. package/dist/chunks/font-style.js +2 -1
  12. package/dist/chunks/font-style.js.map +1 -1
  13. package/dist/chunks/index.js +1283 -0
  14. package/dist/chunks/index.js.map +1 -0
  15. package/dist/constants.js +1 -18
  16. package/dist/constants2.d.ts +0 -1
  17. package/dist/constants2.d.ts.map +1 -1
  18. package/dist/coordinate.d.ts +107 -11
  19. package/dist/coordinate.d.ts.map +1 -1
  20. package/dist/coordinate.js +2 -2
  21. package/dist/coordinate2.d.ts +2 -2
  22. package/dist/copy.d.ts +2 -2
  23. package/dist/copy.js +2 -2
  24. package/dist/geometry.d.ts +2 -2
  25. package/dist/geometry.js +2 -431
  26. package/dist/guides.d.ts +2 -0
  27. package/dist/guides.js +0 -0
  28. package/dist/guides2.d.ts +11 -0
  29. package/dist/guides2.d.ts.map +1 -0
  30. package/dist/hit-test.d.ts +1 -2
  31. package/dist/hit-test.d.ts.map +1 -1
  32. package/dist/hit-test.js +26 -14
  33. package/dist/hit-test.js.map +1 -1
  34. package/dist/images.js +0 -1
  35. package/dist/images.js.map +1 -1
  36. package/dist/images2.d.ts.map +1 -1
  37. package/dist/index.d.ts +5 -3
  38. package/dist/index.js +5 -6
  39. package/dist/instances.d.ts +2 -2
  40. package/dist/instances.js +2 -2
  41. package/dist/layout-guides.d.ts +2 -0
  42. package/dist/layout-guides.js +2 -0
  43. package/dist/layout-guides2.d.ts +16 -0
  44. package/dist/layout-guides2.d.ts.map +1 -0
  45. package/dist/matrix2.d.ts +0 -1
  46. package/dist/matrix2.d.ts.map +1 -1
  47. package/dist/node-defaults.d.ts +1 -2
  48. package/dist/node-defaults.d.ts.map +1 -1
  49. package/dist/node-defaults.js +1 -1
  50. package/dist/parse-path.d.ts +1 -2
  51. package/dist/parse-path.d.ts.map +1 -1
  52. package/dist/parse-path.js.map +1 -1
  53. package/dist/preview.d.ts +2 -12
  54. package/dist/preview.js +1 -1
  55. package/dist/preview2.d.ts +12 -0
  56. package/dist/preview2.d.ts.map +1 -0
  57. package/dist/primitives2.d.ts.map +1 -1
  58. package/dist/resize.d.ts +18 -4
  59. package/dist/resize.d.ts.map +1 -1
  60. package/dist/resize.js +70 -22
  61. package/dist/resize.js.map +1 -1
  62. package/dist/snap.js +1 -1
  63. package/dist/source-metadata.d.ts +1 -2
  64. package/dist/source-metadata.d.ts.map +1 -1
  65. package/dist/source-metadata.js +1 -1
  66. package/dist/text-picture.d.ts +13 -2
  67. package/dist/text-picture.d.ts.map +1 -1
  68. package/dist/text-picture.js +2 -2
  69. package/dist/types.d.ts +499 -2
  70. package/dist/types.d.ts.map +1 -0
  71. package/dist/types.js +1 -1296
  72. package/dist/types2.d.ts +2 -430
  73. package/dist/undo.js +1 -1
  74. package/dist/undo2.d.ts +5 -0
  75. package/dist/undo2.d.ts.map +1 -1
  76. package/dist/variables.d.ts +1 -2
  77. package/dist/variables.d.ts.map +1 -1
  78. package/dist/variables.js +1 -1
  79. package/dist/variant-name.d.ts.map +1 -1
  80. package/dist/variant-properties.d.ts +10 -0
  81. package/dist/variant-properties.d.ts.map +1 -0
  82. package/dist/variant-properties.js +39 -0
  83. package/dist/variant-properties.js.map +1 -0
  84. package/dist/vector-network.js +1 -1
  85. package/dist/vector-network2.d.ts +1 -2
  86. package/dist/vector-network2.d.ts.map +1 -1
  87. package/package.json +40 -15
  88. package/src/arrow-caps.ts +146 -0
  89. package/src/bindings.ts +20 -0
  90. package/src/checkpoint.ts +123 -0
  91. package/src/components/properties.ts +238 -0
  92. package/src/constants.ts +6 -0
  93. package/src/coordinate.ts +226 -0
  94. package/src/copy.ts +294 -0
  95. package/src/events.ts +21 -0
  96. package/src/export-scale.ts +16 -0
  97. package/src/font-style.ts +77 -0
  98. package/src/geometry.ts +530 -0
  99. package/src/guides.ts +8 -0
  100. package/src/hit-test.ts +205 -0
  101. package/src/images.ts +34 -0
  102. package/src/index.ts +686 -0
  103. package/src/instance-overrides.test.ts +67 -0
  104. package/src/instance-overrides.ts +152 -0
  105. package/src/instances.ts +480 -0
  106. package/src/layout-guides.ts +103 -0
  107. package/src/matrix.ts +116 -0
  108. package/src/mutation-impact.ts +70 -0
  109. package/src/node-defaults.ts +186 -0
  110. package/src/parse-path.ts +191 -0
  111. package/src/preview.ts +80 -0
  112. package/src/primitives.ts +42 -0
  113. package/src/resize.ts +302 -0
  114. package/src/shared-styles.ts +73 -0
  115. package/src/snap.ts +129 -0
  116. package/src/source-metadata.ts +8 -0
  117. package/src/text-picture.ts +65 -0
  118. package/src/types.ts +635 -0
  119. package/src/undo.ts +171 -0
  120. package/src/variables.ts +399 -0
  121. package/src/variant-name.ts +15 -0
  122. package/src/variant-properties.ts +48 -0
  123. package/src/vector-network.ts +293 -0
  124. package/dist/chunks/copy.js +0 -576
  125. package/dist/chunks/copy.js.map +0 -1
  126. package/dist/constants.js.map +0 -1
  127. package/dist/geometry.js.map +0 -1
  128. package/dist/preview.d.ts.map +0 -1
  129. package/dist/types.js.map +0 -1
  130. package/dist/types2.d.ts.map +0 -1
@@ -0,0 +1,205 @@
1
+ import type { SceneGraph, SceneNode, NodeType } from './'
2
+ import { getWorldMatrix } from './coordinate'
3
+ import Matrix from './matrix'
4
+
5
+ const CONTAINER_TYPES = new Set<NodeType>([
6
+ 'CANVAS',
7
+ 'FRAME',
8
+ 'GROUP',
9
+ 'SECTION',
10
+ 'COMPONENT',
11
+ 'COMPONENT_SET',
12
+ 'INSTANCE'
13
+ ])
14
+ const OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])
15
+
16
+ function hasVisibleFillOrStroke(node: SceneNode): boolean {
17
+ return node.fills.some((f) => f.visible) || node.strokes.some((s) => s.visible)
18
+ }
19
+
20
+ function hasTransformedAncestor(
21
+ node: SceneNode,
22
+ graph: SceneGraph,
23
+ cache: Map<string, boolean>
24
+ ): boolean {
25
+ const cached = cache.get(node.id)
26
+ if (cached !== undefined) return cached
27
+ const parent = node.parentId ? graph.getNode(node.parentId) : undefined
28
+ const transformed =
29
+ node.rotation !== 0 ||
30
+ node.flipX ||
31
+ node.flipY ||
32
+ (parent ? hasTransformedAncestor(parent, graph, cache) : false)
33
+ cache.set(node.id, transformed)
34
+ return transformed
35
+ }
36
+
37
+ function containsPoint(
38
+ px: number,
39
+ py: number,
40
+ node: SceneNode,
41
+ graph: SceneGraph,
42
+ transformCache: Map<string, boolean>
43
+ ): boolean {
44
+ if (!hasTransformedAncestor(node, graph, transformCache)) {
45
+ const absolute = graph.getAbsolutePosition(node.id)
46
+ return (
47
+ px >= absolute.x &&
48
+ px <= absolute.x + node.width &&
49
+ py >= absolute.y &&
50
+ py <= absolute.y + node.height
51
+ )
52
+ }
53
+
54
+ const m = getWorldMatrix(node, graph)
55
+
56
+ const inv = Matrix.invert(m)
57
+ if (!inv) return false
58
+
59
+ const [localX, localY] = Matrix.mapPoints(inv, [px, py])
60
+ return localX >= 0 && localX <= node.width && localY >= 0 && localY <= node.height
61
+ }
62
+
63
+ function hitTestOpaqueContainer(
64
+ graph: SceneGraph,
65
+ px: number,
66
+ py: number,
67
+ child: SceneNode,
68
+ childId: string,
69
+ deep: boolean,
70
+ transformCache: Map<string, boolean>
71
+ ): SceneNode | null {
72
+ if (!containsPoint(px, py, child, graph, transformCache)) return null
73
+ const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache)
74
+ if (childHit) return child
75
+ if (hasVisibleFillOrStroke(child)) return child
76
+ return null
77
+ }
78
+ function hitTestTransparentContainer(
79
+ graph: SceneGraph,
80
+ px: number,
81
+ py: number,
82
+ child: SceneNode,
83
+ childId: string,
84
+ deep: boolean,
85
+ transformCache: Map<string, boolean>
86
+ ): SceneNode | null {
87
+ if (child.type === 'GROUP') {
88
+ if (!containsPoint(px, py, child, graph, transformCache)) return null
89
+
90
+ if (deep) return hitTestChildren(graph, px, py, childId, deep, transformCache) ?? child
91
+
92
+ return child
93
+ }
94
+
95
+ const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache)
96
+ if (childHit) {
97
+ if (child.locked) return child
98
+ return childHit
99
+ }
100
+
101
+ if (containsPoint(px, py, child, graph, transformCache) && hasVisibleFillOrStroke(child))
102
+ return child
103
+ return null
104
+ }
105
+
106
+ function hitTestChildren(
107
+ graph: SceneGraph,
108
+ px: number,
109
+ py: number,
110
+ parentId: string,
111
+ deep = false,
112
+ transformCache = new Map<string, boolean>()
113
+ ): SceneNode | null {
114
+ const parent = graph.nodes.get(parentId)
115
+ if (!parent) return null
116
+
117
+ if (parent.clipsContent) {
118
+ if (!containsPoint(px, py, parent, graph, transformCache)) return null
119
+ }
120
+
121
+ for (let i = parent.childIds.length - 1; i >= 0; i--) {
122
+ const childId = parent.childIds[i]
123
+ const child = graph.nodes.get(childId)
124
+ if (!child || child.internalOnly || !child.visible) continue
125
+ if (CONTAINER_TYPES.has(child.type)) {
126
+ if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {
127
+ const hit = hitTestOpaqueContainer(graph, px, py, child, childId, deep, transformCache)
128
+ if (hit) return hit
129
+ continue
130
+ }
131
+
132
+ const hit = hitTestTransparentContainer(graph, px, py, child, childId, deep, transformCache)
133
+ if (hit) return hit
134
+ continue
135
+ }
136
+
137
+ if (containsPoint(px, py, child, graph, transformCache)) return child
138
+ }
139
+
140
+ return null
141
+ }
142
+
143
+ export function hitTest(
144
+ graph: SceneGraph,
145
+ px: number,
146
+ py: number,
147
+ scopeId?: string
148
+ ): SceneNode | null {
149
+ const scope = scopeId ?? graph.rootId
150
+ return hitTestChildren(graph, px, py, scope, false)
151
+ }
152
+
153
+ export function hitTestDeep(
154
+ graph: SceneGraph,
155
+ px: number,
156
+ py: number,
157
+ scopeId?: string
158
+ ): SceneNode | null {
159
+ const scope = scopeId ?? graph.rootId
160
+ return hitTestChildren(graph, px, py, scope, true)
161
+ }
162
+
163
+ function hitTestFrameChildren(
164
+ graph: SceneGraph,
165
+ px: number,
166
+ py: number,
167
+ parentId: string,
168
+ offsetX: number,
169
+ offsetY: number,
170
+ excludeIds: Set<string>
171
+ ): SceneNode | null {
172
+ const parent = graph.nodes.get(parentId)
173
+ if (!parent) return null
174
+
175
+ let best: SceneNode | null = null
176
+
177
+ for (const childId of parent.childIds) {
178
+ if (excludeIds.has(childId)) continue
179
+ const child = graph.nodes.get(childId)
180
+ if (!child || child.internalOnly || !child.visible) continue
181
+
182
+ const ax = offsetX + child.x
183
+ const ay = offsetY + child.y
184
+
185
+ if (!CONTAINER_TYPES.has(child.type)) continue
186
+ if (px < ax || px > ax + child.width || py < ay || py > ay + child.height) continue
187
+
188
+ best = child
189
+
190
+ const deeper = hitTestFrameChildren(graph, px, py, childId, ax, ay, excludeIds)
191
+ if (deeper) best = deeper
192
+ }
193
+
194
+ return best
195
+ }
196
+
197
+ export function hitTestFrame(
198
+ graph: SceneGraph,
199
+ px: number,
200
+ py: number,
201
+ excludeIds: Set<string>,
202
+ scopeId?: string
203
+ ): SceneNode | null {
204
+ return hitTestFrameChildren(graph, px, py, scopeId ?? graph.rootId, 0, 0, excludeIds)
205
+ }
package/src/images.ts ADDED
@@ -0,0 +1,34 @@
1
+ export function computeImageHash(data: Uint8Array): string {
2
+ let h1 = 0x811c9dc5 >>> 0
3
+ let h2 = 0x811c9dc5 >>> 0
4
+ let h3 = 0x811c9dc5 >>> 0
5
+ let h4 = 0x811c9dc5 >>> 0
6
+ let h5 = 0x811c9dc5 >>> 0
7
+ for (let i = 0; i < data.length; i++) {
8
+ const b = data[i]
9
+ switch (i % 5) {
10
+ case 0:
11
+ h1 ^= b
12
+ h1 = Math.imul(h1, 0x01000193) >>> 0
13
+ break
14
+ case 1:
15
+ h2 ^= b
16
+ h2 = Math.imul(h2, 0x01000193) >>> 0
17
+ break
18
+ case 2:
19
+ h3 ^= b
20
+ h3 = Math.imul(h3, 0x01000193) >>> 0
21
+ break
22
+ case 3:
23
+ h4 ^= b
24
+ h4 = Math.imul(h4, 0x01000193) >>> 0
25
+ break
26
+ default:
27
+ h5 ^= b
28
+ h5 = Math.imul(h5, 0x01000193) >>> 0
29
+ break
30
+ }
31
+ }
32
+ h5 = Math.imul(h5 ^ data.length, 0x01000193) >>> 0
33
+ return [h1, h2, h3, h4, h5].map((hash) => hash.toString(16).padStart(8, '0')).join('')
34
+ }