tldraw 3.16.0-canary.ed8bd30c0f28 → 3.16.0-canary.f60032f16651

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 (36) hide show
  1. package/dist-cjs/index.d.ts +32 -1
  2. package/dist-cjs/index.js +2 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/shapes/arrow/arrowTargetState.js +1 -1
  5. package/dist-cjs/lib/shapes/arrow/arrowTargetState.js.map +2 -2
  6. package/dist-cjs/lib/tools/SelectTool/childStates/Translating.js.map +2 -2
  7. package/dist-cjs/lib/ui/components/primitives/menus/TldrawUiMenuItem.js +149 -1
  8. package/dist-cjs/lib/ui/components/primitives/menus/TldrawUiMenuItem.js.map +2 -2
  9. package/dist-cjs/lib/ui/context/events.js.map +2 -2
  10. package/dist-cjs/lib/ui/hooks/useTools.js +76 -9
  11. package/dist-cjs/lib/ui/hooks/useTools.js.map +2 -2
  12. package/dist-cjs/lib/ui/version.js +3 -3
  13. package/dist-cjs/lib/ui/version.js.map +1 -1
  14. package/dist-esm/index.d.mts +32 -1
  15. package/dist-esm/index.mjs +3 -1
  16. package/dist-esm/index.mjs.map +2 -2
  17. package/dist-esm/lib/shapes/arrow/arrowTargetState.mjs +1 -1
  18. package/dist-esm/lib/shapes/arrow/arrowTargetState.mjs.map +2 -2
  19. package/dist-esm/lib/tools/SelectTool/childStates/Translating.mjs.map +2 -2
  20. package/dist-esm/lib/ui/components/primitives/menus/TldrawUiMenuItem.mjs +157 -3
  21. package/dist-esm/lib/ui/components/primitives/menus/TldrawUiMenuItem.mjs.map +2 -2
  22. package/dist-esm/lib/ui/context/events.mjs.map +2 -2
  23. package/dist-esm/lib/ui/hooks/useTools.mjs +83 -10
  24. package/dist-esm/lib/ui/hooks/useTools.mjs.map +2 -2
  25. package/dist-esm/lib/ui/version.mjs +3 -3
  26. package/dist-esm/lib/ui/version.mjs.map +1 -1
  27. package/package.json +3 -3
  28. package/src/index.ts +2 -0
  29. package/src/lib/shapes/arrow/arrowTargetState.ts +2 -1
  30. package/src/lib/tools/SelectTool/childStates/Translating.ts +0 -1
  31. package/src/lib/ui/components/primitives/menus/TldrawUiMenuItem.tsx +213 -2
  32. package/src/lib/ui/context/events.tsx +1 -0
  33. package/src/lib/ui/hooks/useTools.tsx +118 -10
  34. package/src/lib/ui/version.ts +3 -3
  35. package/src/test/arrows-megabus.test.tsx +12 -6
  36. package/src/test/inner-outer-margin.test.ts +315 -0
@@ -0,0 +1,315 @@
1
+ import { TLArrowShape, createShapeId } from '@tldraw/editor'
2
+ import { getArrowBindings } from '../lib/shapes/arrow/shared'
3
+ import { TestEditor } from './TestEditor'
4
+
5
+ let editor: TestEditor
6
+
7
+ const ids = {
8
+ solidShape: createShapeId('solidShape'),
9
+ hollowShape: createShapeId('hollowShape'),
10
+ arrow: createShapeId('arrow'),
11
+ }
12
+
13
+ const _arrow = () => editor.getOnlySelectedShape() as TLArrowShape
14
+
15
+ beforeEach(() => {
16
+ editor = new TestEditor()
17
+ })
18
+
19
+ describe('Inner/Outer Margin Shape Detection', () => {
20
+ describe('getShapeAtPoint with inner/outer margins', () => {
21
+ beforeEach(() => {
22
+ // Create a solid filled shape
23
+ editor.createShape({
24
+ id: ids.solidShape,
25
+ type: 'geo',
26
+ x: 100,
27
+ y: 100,
28
+ props: {
29
+ w: 100,
30
+ h: 100,
31
+ fill: 'solid',
32
+ },
33
+ })
34
+
35
+ // Create a hollow shape on top (same position, smaller size)
36
+ editor.createShape({
37
+ id: ids.hollowShape,
38
+ type: 'geo',
39
+ x: 125,
40
+ y: 125,
41
+ props: {
42
+ w: 50,
43
+ h: 50,
44
+ // No fill property - defaults to 'none' (hollow)
45
+ },
46
+ })
47
+ })
48
+
49
+ it('should support inner/outer margin options', () => {
50
+ // Test that the new margin options are accepted
51
+ const point = { x: 150, y: 150 }
52
+
53
+ // Test with array margin [innerMargin, outerMargin]
54
+ const arrayMarginHit = editor.getShapeAtPoint(point, {
55
+ hitInside: true,
56
+ margin: [8, 4],
57
+ })
58
+ expect(arrayMarginHit).toBeDefined()
59
+
60
+ // Test with insideMargin option
61
+ const insideMarginHit = editor.getShapeAtPoint(point, {
62
+ hitInside: true,
63
+ })
64
+ expect(insideMarginHit).toBeDefined()
65
+
66
+ // Test with single number margin (should use same for both)
67
+ const singleMarginHit = editor.getShapeAtPoint(point, {
68
+ hitInside: true,
69
+ margin: 8,
70
+ })
71
+ expect(singleMarginHit).toBeDefined()
72
+ })
73
+
74
+ it('should respect hitInside option for hollow shapes', () => {
75
+ const point = { x: 150, y: 150 }
76
+
77
+ // Without hitInside, should not hit hollow shape
78
+ const noHitInsideHit = editor.getShapeAtPoint(point, {
79
+ margin: [8, 0],
80
+ })
81
+ expect(noHitInsideHit?.id).toBe(ids.solidShape)
82
+
83
+ // With hitInside, should be able to hit hollow shape
84
+ const withHitInsideHit = editor.getShapeAtPoint(point, {
85
+ hitInside: true,
86
+ margin: [8, 0],
87
+ })
88
+ expect(withHitInsideHit).toBeDefined()
89
+ })
90
+
91
+ it('should handle edge cases correctly', () => {
92
+ // Test point exactly on the edge of hollow shape
93
+ const edgePoint = { x: 125, y: 150 }
94
+
95
+ const edgeHit = editor.getShapeAtPoint(edgePoint, {
96
+ hitInside: true,
97
+ margin: [8, 8],
98
+ })
99
+ expect(edgeHit).toBeDefined()
100
+ })
101
+ })
102
+
103
+ describe('Arrow binding with inner/outer margins', () => {
104
+ beforeEach(() => {
105
+ // Create a solid filled shape
106
+ editor.createShape({
107
+ id: ids.solidShape,
108
+ type: 'geo',
109
+ x: 100,
110
+ y: 100,
111
+ props: {
112
+ w: 100,
113
+ h: 100,
114
+ fill: 'solid',
115
+ },
116
+ })
117
+
118
+ // Create a hollow shape on top (same position, smaller size)
119
+ editor.createShape({
120
+ id: ids.hollowShape,
121
+ type: 'geo',
122
+ x: 125,
123
+ y: 125,
124
+ props: {
125
+ w: 50,
126
+ h: 50,
127
+ // No fill property - defaults to 'none' (hollow)
128
+ },
129
+ })
130
+ })
131
+
132
+ it('should create arrow bindings with inner/outer margin support', () => {
133
+ editor.setCurrentTool('arrow')
134
+
135
+ // Start arrow outside both shapes
136
+ editor.pointerDown(50, 150)
137
+
138
+ // Move to center of hollow shape (which overlaps solid shape)
139
+ editor.pointerMove(150, 150)
140
+ editor.pointerUp()
141
+
142
+ const createdArrow = editor
143
+ .getCurrentPageShapes()
144
+ .find((s) => s.type === 'arrow') as TLArrowShape
145
+ expect(createdArrow).toBeDefined()
146
+
147
+ const arrowBindings = getArrowBindings(editor, createdArrow)
148
+ expect(arrowBindings.end).toBeDefined()
149
+ // The binding should be to one of the shapes
150
+ expect([ids.solidShape, ids.hollowShape]).toContain(arrowBindings.end?.toId)
151
+ })
152
+
153
+ it('should bind to solid shape when no hollow shape is present', () => {
154
+ // Remove the hollow shape
155
+ editor.deleteShape(ids.hollowShape)
156
+
157
+ editor.setCurrentTool('arrow')
158
+
159
+ // Start arrow outside shape
160
+ editor.pointerDown(50, 150)
161
+
162
+ // Move to center of solid shape
163
+ editor.pointerMove(150, 150)
164
+ editor.pointerUp()
165
+
166
+ const createdArrow = editor
167
+ .getCurrentPageShapes()
168
+ .find((s) => s.type === 'arrow') as TLArrowShape
169
+ expect(createdArrow).toBeDefined()
170
+
171
+ const arrowBindings = getArrowBindings(editor, createdArrow)
172
+ expect(arrowBindings.end).toBeDefined()
173
+ expect(arrowBindings.end?.toId).toBe(ids.solidShape)
174
+ })
175
+ })
176
+
177
+ describe('Complex overlapping scenarios', () => {
178
+ it('should handle multiple overlapping shapes correctly', () => {
179
+ // Create multiple shapes with different fill states
180
+ editor.createShape({
181
+ id: ids.solidShape,
182
+ type: 'geo',
183
+ x: 100,
184
+ y: 100,
185
+ props: {
186
+ w: 100,
187
+ h: 100,
188
+ fill: 'solid',
189
+ },
190
+ })
191
+
192
+ editor.createShape({
193
+ id: ids.hollowShape,
194
+ type: 'geo',
195
+ x: 125,
196
+ y: 125,
197
+ props: {
198
+ w: 50,
199
+ h: 50,
200
+ // No fill property - defaults to 'none' (hollow)
201
+ },
202
+ })
203
+
204
+ // Create another hollow shape
205
+ const hollowShape2 = createShapeId('hollowShape2')
206
+ editor.createShape({
207
+ id: hollowShape2,
208
+ type: 'geo',
209
+ x: 140,
210
+ y: 140,
211
+ props: {
212
+ w: 30,
213
+ h: 30,
214
+ // No fill property - defaults to 'none' (hollow)
215
+ },
216
+ })
217
+
218
+ // Test point in the innermost hollow shape
219
+ const point = { x: 155, y: 155 }
220
+
221
+ const hit = editor.getShapeAtPoint(point, {
222
+ hitInside: true,
223
+ margin: [8, 8],
224
+ })
225
+ expect(hit).toBeDefined()
226
+ // Should hit one of the shapes
227
+ expect([ids.solidShape, ids.hollowShape, hollowShape2]).toContain(hit?.id)
228
+ })
229
+
230
+ it('should handle shapes with different geometries', () => {
231
+ // Create a solid rectangle
232
+ editor.createShape({
233
+ id: ids.solidShape,
234
+ type: 'geo',
235
+ x: 100,
236
+ y: 100,
237
+ props: {
238
+ w: 100,
239
+ h: 100,
240
+ fill: 'solid',
241
+ },
242
+ })
243
+
244
+ // Create a hollow circle on top
245
+ editor.createShape({
246
+ id: ids.hollowShape,
247
+ type: 'geo',
248
+ x: 125,
249
+ y: 125,
250
+ props: {
251
+ w: 50,
252
+ h: 50,
253
+ geo: 'ellipse',
254
+ // No fill property - defaults to 'none' (hollow)
255
+ },
256
+ })
257
+
258
+ // Test point inside the circle
259
+ const point = { x: 150, y: 150 }
260
+
261
+ const hit = editor.getShapeAtPoint(point, {
262
+ hitInside: true,
263
+ margin: [8, 8],
264
+ })
265
+ expect(hit).toBeDefined()
266
+ // Should hit one of the shapes
267
+ expect([ids.solidShape, ids.hollowShape]).toContain(hit?.id)
268
+ })
269
+ })
270
+
271
+ describe('Regression tests for the original bug', () => {
272
+ it('should support binding to hollow shapes when overlapping solid shapes', () => {
273
+ // This test verifies that the infrastructure exists for the bug fix
274
+ // Create a solid shape
275
+ editor.createShape({
276
+ id: ids.solidShape,
277
+ type: 'geo',
278
+ x: 100,
279
+ y: 100,
280
+ props: {
281
+ w: 100,
282
+ h: 100,
283
+ fill: 'solid',
284
+ },
285
+ })
286
+
287
+ // Create a hollow shape on top
288
+ editor.createShape({
289
+ id: ids.hollowShape,
290
+ type: 'geo',
291
+ x: 125,
292
+ y: 125,
293
+ props: {
294
+ w: 50,
295
+ h: 50,
296
+ // No fill property - defaults to 'none' (hollow)
297
+ },
298
+ })
299
+
300
+ // Test that we can detect both shapes
301
+ const point = { x: 150, y: 150 }
302
+
303
+ // Should be able to hit the solid shape without hitInside
304
+ const solidHit = editor.getShapeAtPoint(point)
305
+ expect(solidHit?.id).toBe(ids.solidShape)
306
+
307
+ // Should be able to hit the hollow shape with hitInside and inner margin
308
+ const hollowHit = editor.getShapeAtPoint(point, {
309
+ hitInside: true,
310
+ margin: [8, 0],
311
+ })
312
+ expect(hollowHit).toBeDefined()
313
+ })
314
+ })
315
+ })