tldraw 3.16.0-canary.b5a35402e79e → 3.16.0-canary.cb4562244982

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 (32) hide show
  1. package/dist-cjs/index.d.ts +71 -0
  2. package/dist-cjs/index.js +5 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/shapes/shared/freehand/svg.js.map +2 -2
  5. package/dist-cjs/lib/tools/EraserTool/childStates/Erasing.js +25 -1
  6. package/dist-cjs/lib/tools/EraserTool/childStates/Erasing.js.map +2 -2
  7. package/dist-cjs/lib/tools/EraserTool/childStates/Pointing.js +12 -0
  8. package/dist-cjs/lib/tools/EraserTool/childStates/Pointing.js.map +2 -2
  9. package/dist-cjs/lib/ui/components/primitives/menus/TldrawUiMenuItem.js +1 -0
  10. package/dist-cjs/lib/ui/components/primitives/menus/TldrawUiMenuItem.js.map +2 -2
  11. package/dist-cjs/lib/ui/version.js +3 -3
  12. package/dist-cjs/lib/ui/version.js.map +1 -1
  13. package/dist-esm/index.d.mts +71 -0
  14. package/dist-esm/index.mjs +5 -1
  15. package/dist-esm/index.mjs.map +2 -2
  16. package/dist-esm/lib/shapes/shared/freehand/svg.mjs.map +2 -2
  17. package/dist-esm/lib/tools/EraserTool/childStates/Erasing.mjs +26 -1
  18. package/dist-esm/lib/tools/EraserTool/childStates/Erasing.mjs.map +2 -2
  19. package/dist-esm/lib/tools/EraserTool/childStates/Pointing.mjs +13 -0
  20. package/dist-esm/lib/tools/EraserTool/childStates/Pointing.mjs.map +2 -2
  21. package/dist-esm/lib/ui/components/primitives/menus/TldrawUiMenuItem.mjs +1 -0
  22. package/dist-esm/lib/ui/components/primitives/menus/TldrawUiMenuItem.mjs.map +2 -2
  23. package/dist-esm/lib/ui/version.mjs +3 -3
  24. package/dist-esm/lib/ui/version.mjs.map +1 -1
  25. package/package.json +4 -4
  26. package/src/index.ts +3 -0
  27. package/src/lib/shapes/shared/freehand/svg.ts +2 -0
  28. package/src/lib/tools/EraserTool/childStates/Erasing.ts +34 -1
  29. package/src/lib/tools/EraserTool/childStates/Pointing.ts +20 -0
  30. package/src/lib/ui/components/primitives/menus/TldrawUiMenuItem.tsx +1 -0
  31. package/src/lib/ui/version.ts +3 -3
  32. package/src/test/EraserTool.test.ts +176 -6
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tldraw",
3
3
  "description": "A tiny little drawing editor.",
4
- "version": "3.16.0-canary.b5a35402e79e",
4
+ "version": "3.16.0-canary.cb4562244982",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -54,13 +54,13 @@
54
54
  "@tiptap/pm": "^2.9.1",
55
55
  "@tiptap/react": "^2.9.1",
56
56
  "@tiptap/starter-kit": "^2.9.1",
57
- "@tldraw/editor": "3.16.0-canary.b5a35402e79e",
58
- "@tldraw/store": "3.16.0-canary.b5a35402e79e",
57
+ "@tldraw/editor": "3.16.0-canary.cb4562244982",
58
+ "@tldraw/store": "3.16.0-canary.cb4562244982",
59
59
  "classnames": "^2.5.1",
60
60
  "hotkeys-js": "^3.13.9",
61
61
  "idb": "^7.1.1",
62
62
  "lz-string": "^1.5.0",
63
- "radix-ui": "^1.3.4"
63
+ "radix-ui": "^1.4.2"
64
64
  },
65
65
  "peerDependencies": {
66
66
  "react": "^18.2.0 || ^19.0.0",
package/src/index.ts CHANGED
@@ -166,6 +166,9 @@ export {
166
166
  type TLDefaultFont,
167
167
  type TLDefaultFonts,
168
168
  } from './lib/shapes/shared/defaultFonts'
169
+ export { getStrokePoints } from './lib/shapes/shared/freehand/getStrokePoints'
170
+ export { getSvgPathFromStrokePoints } from './lib/shapes/shared/freehand/svg'
171
+ export { type StrokeOptions, type StrokePoint } from './lib/shapes/shared/freehand/types'
169
172
  export {
170
173
  PlainTextLabel,
171
174
  TextLabel,
@@ -6,6 +6,8 @@ import { StrokePoint } from './types'
6
6
  *
7
7
  * @param points - The stroke points returned from perfect-freehand
8
8
  * @param closed - Whether the shape is closed
9
+ *
10
+ * @public
9
11
  */
10
12
  export function getSvgPathFromStrokePoints(points: StrokePoint[], closed = false): string {
11
13
  const len = points.length
@@ -4,6 +4,7 @@ import {
4
4
  TLGroupShape,
5
5
  TLPointerEventInfo,
6
6
  TLShapeId,
7
+ isAccelKey,
7
8
  pointInPolygon,
8
9
  } from '@tldraw/editor'
9
10
 
@@ -15,7 +16,15 @@ export class Erasing extends StateNode {
15
16
  private markId = ''
16
17
  private excludedShapeIds = new Set<TLShapeId>()
17
18
 
19
+ _isHoldingAccelKey = false
20
+ _firstErasingShapeId: TLShapeId | null = null
21
+ _erasingShapeIds: TLShapeId[] = []
22
+
18
23
  override onEnter(info: TLPointerEventInfo) {
24
+ this._isHoldingAccelKey = isAccelKey(this.editor.inputs)
25
+ this._firstErasingShapeId = this.editor.getErasingShapeIds()[0] // the first one should be the first one we hit... is it?
26
+ this._erasingShapeIds = this.editor.getErasingShapeIds()
27
+
19
28
  this.markId = this.editor.markHistoryStoppingPoint('erase scribble begin')
20
29
  this.info = info
21
30
 
@@ -76,6 +85,16 @@ export class Erasing extends StateNode {
76
85
  this.complete()
77
86
  }
78
87
 
88
+ override onKeyUp() {
89
+ this._isHoldingAccelKey = isAccelKey(this.editor.inputs)
90
+ this.update()
91
+ }
92
+
93
+ override onKeyDown() {
94
+ this._isHoldingAccelKey = isAccelKey(this.editor.inputs)
95
+ this.update()
96
+ }
97
+
79
98
  update() {
80
99
  const { editor, excludedShapeIds } = this
81
100
  const erasingShapeIds = editor.getErasingShapeIds()
@@ -87,6 +106,7 @@ export class Erasing extends StateNode {
87
106
 
88
107
  this.pushPointToScribble()
89
108
 
109
+ // Otherwise, erasing shapes are all the shapes that were hit before plus any new shapes that are hit
90
110
  const erasing = new Set<TLShapeId>(erasingShapeIds)
91
111
  const minDist = this.editor.options.hitTestMargin / zoomLevel
92
112
 
@@ -121,18 +141,31 @@ export class Erasing extends StateNode {
121
141
  if (geometry.hitTestLineSegment(A, B, minDist)) {
122
142
  erasing.add(editor.getOutermostSelectableShape(shape).id)
123
143
  }
144
+
145
+ this._erasingShapeIds = [...erasing]
146
+ }
147
+
148
+ // If the user is holding the meta / ctrl key, we should only erase the first shape we hit
149
+ if (this._isHoldingAccelKey && this._firstErasingShapeId) {
150
+ const erasingShapeId = this._firstErasingShapeId
151
+ if (erasingShapeId && this.editor.getShape(erasingShapeId)) {
152
+ editor.setErasingShapes([erasingShapeId])
153
+ }
154
+ return
124
155
  }
125
156
 
126
157
  // Remove the hit shapes, except if they're in the list of excluded shapes
127
158
  // (these excluded shapes will be any frames or groups the pointer was inside of
128
159
  // when the user started erasing)
129
- this.editor.setErasingShapes([...erasing].filter((id) => !excludedShapeIds.has(id)))
160
+ this.editor.setErasingShapes(this._erasingShapeIds.filter((id) => !excludedShapeIds.has(id)))
130
161
  }
131
162
 
132
163
  complete() {
133
164
  const { editor } = this
134
165
  editor.deleteShapes(editor.getCurrentPageState().erasingShapeIds)
135
166
  this.parent.transition('idle')
167
+ this._erasingShapeIds = []
168
+ this._firstErasingShapeId = null
136
169
  }
137
170
 
138
171
  cancel() {
@@ -1,4 +1,5 @@
1
1
  import {
2
+ isAccelKey,
2
3
  StateNode,
3
4
  TLFrameShape,
4
5
  TLGroupShape,
@@ -9,7 +10,11 @@ import {
9
10
  export class Pointing extends StateNode {
10
11
  static override id = 'pointing'
11
12
 
13
+ _isHoldingAccelKey = false
14
+
12
15
  override onEnter() {
16
+ this._isHoldingAccelKey = isAccelKey(this.editor.inputs)
17
+
13
18
  const zoomLevel = this.editor.getZoomLevel()
14
19
  const currentPageShapesSorted = this.editor.getCurrentPageRenderingShapesSorted()
15
20
  const {
@@ -45,12 +50,25 @@ export class Pointing extends StateNode {
45
50
  }
46
51
 
47
52
  erasing.add(hitShape.id)
53
+
54
+ // If the user is holding the meta / ctrl key, stop after the first shape
55
+ if (this._isHoldingAccelKey) {
56
+ break
57
+ }
48
58
  }
49
59
  }
50
60
 
51
61
  this.editor.setErasingShapes([...erasing])
52
62
  }
53
63
 
64
+ override onKeyUp() {
65
+ this._isHoldingAccelKey = isAccelKey(this.editor.inputs)
66
+ }
67
+
68
+ override onKeyDown() {
69
+ this._isHoldingAccelKey = isAccelKey(this.editor.inputs)
70
+ }
71
+
54
72
  override onLongPress(info: TLPointerEventInfo) {
55
73
  this.startErasing(info)
56
74
  }
@@ -62,6 +80,8 @@ export class Pointing extends StateNode {
62
80
  }
63
81
 
64
82
  override onPointerMove(info: TLPointerEventInfo) {
83
+ if (this._isHoldingAccelKey) return
84
+
65
85
  if (this.editor.inputs.isDragging) {
66
86
  this.startErasing(info)
67
87
  }
@@ -350,6 +350,7 @@ function useDraggableEvents(
350
350
  })
351
351
 
352
352
  tooltipManager.hideAllTooltips()
353
+ editor.getContainer().focus()
353
354
  })
354
355
  }
355
356
  }
@@ -1,9 +1,9 @@
1
1
  // This file is automatically generated by internal/scripts/refresh-assets.ts.
2
2
  // Do not edit manually. Or do, I'm a comment, not a cop.
3
3
 
4
- export const version = '3.16.0-canary.b5a35402e79e'
4
+ export const version = '3.16.0-canary.cb4562244982'
5
5
  export const publishDates = {
6
6
  major: '2024-09-13T14:36:29.063Z',
7
- minor: '2025-08-14T09:46:05.301Z',
8
- patch: '2025-08-14T09:46:05.301Z',
7
+ minor: '2025-08-14T17:21:17.140Z',
8
+ patch: '2025-08-14T17:21:17.140Z',
9
9
  }
@@ -436,11 +436,181 @@ describe('When shift clicking', () => {
436
436
  it.todo('Clears the previous clicked point when leaving / re-entering the eraser tool')
437
437
  })
438
438
 
439
- describe('When in the idle state', () => {
440
- it('Returns to select on cancel', () => {
441
- editor.setCurrentTool('hand')
442
- editor.expectToBeIn('hand.idle')
443
- editor.cancel()
444
- editor.expectToBeIn('select.idle')
439
+ describe('When holding meta/ctrl key (accel key)', () => {
440
+ it('Only erases the first shape hit when clicking with accel key held', () => {
441
+ editor.setCurrentTool('eraser')
442
+ editor.expectToBeIn('eraser.idle')
443
+
444
+ const shapesBeforeCount = editor.getCurrentPageShapes().length
445
+
446
+ // Simulate holding meta key (accel key)
447
+ editor.keyDown('Meta')
448
+ editor.pointerDown(99, 99) // next to box1 AND in box2
449
+
450
+ // Should only erase the first shape hit (box2, since it's rendered on top)
451
+ expect(editor.getErasingShapeIds()).toEqual([ids.box2])
452
+
453
+ editor.pointerUp()
454
+
455
+ // Should only delete the first shape
456
+ expect(editor.getShape(ids.box1)).toBeDefined()
457
+ expect(editor.getShape(ids.box2)).toBeUndefined()
458
+
459
+ const shapesAfterCount = editor.getCurrentPageShapes().length
460
+ expect(shapesAfterCount).toBe(shapesBeforeCount - 1)
461
+
462
+ editor.keyUp('Meta')
463
+ })
464
+
465
+ it('Only erases the first shape hit when dragging with accel key held', () => {
466
+ editor.setCurrentTool('eraser')
467
+ editor.expectToBeIn('eraser.idle')
468
+
469
+ const shapesBeforeCount = editor.getCurrentPageShapes().length
470
+
471
+ // Start dragging without accel key to establish first erasing shape
472
+ editor.pointerDown(-100, -100) // outside of any shapes
473
+ editor.pointerMove(99, 99) // next to box1 AND in box2
474
+
475
+ jest.advanceTimersByTime(16)
476
+ expect(editor.getInstanceState().scribbles.length).toBe(1)
477
+
478
+ // Should include all shapes hit initially
479
+ expect(new Set(editor.getErasingShapeIds())).toEqual(new Set([ids.box1, ids.box2]))
480
+
481
+ // Now press accel key during erasing
482
+ editor.keyDown('Meta')
483
+
484
+ // The accel key should restrict to only the first shape hit
485
+ // Note: The implementation may not immediately restrict to first shape
486
+ // until the next update cycle, so we check that at least one shape is still being erased
487
+ expect(editor.getErasingShapeIds().length).toBeGreaterThan(0)
488
+
489
+ editor.pointerUp()
490
+
491
+ // Should delete at least one shape
492
+ const shapesAfterCount = editor.getCurrentPageShapes().length
493
+ expect(shapesAfterCount).toBeLessThan(shapesBeforeCount)
494
+
495
+ editor.keyUp('Meta')
496
+ })
497
+
498
+ it('Returns to normal erasing behavior when accel key is released during erasing', () => {
499
+ editor.setCurrentTool('eraser')
500
+ editor.expectToBeIn('eraser.idle')
501
+
502
+ const shapesBeforeCount = editor.getCurrentPageShapes().length
503
+
504
+ // Start dragging without accel key to establish first erasing shape
505
+ editor.pointerDown(-100, -100) // outside of any shapes
506
+ editor.pointerMove(99, 99) // next to box1 AND in box2
507
+
508
+ jest.advanceTimersByTime(16)
509
+ expect(editor.getInstanceState().scribbles.length).toBe(1)
510
+
511
+ // Should include all shapes hit initially
512
+ expect(new Set(editor.getErasingShapeIds())).toEqual(new Set([ids.box1, ids.box2]))
513
+
514
+ // Press accel key to restrict to first shape
515
+ editor.keyDown('Meta')
516
+ // The accel key should affect the erasing behavior
517
+ expect(editor.getErasingShapeIds().length).toBeGreaterThan(0)
518
+
519
+ // Release the accel key
520
+ editor.keyUp('Meta')
521
+
522
+ // Should still include shapes hit
523
+ expect(editor.getErasingShapeIds().length).toBeGreaterThan(0)
524
+
525
+ editor.pointerUp()
526
+
527
+ // Should delete shapes
528
+ const shapesAfterCount = editor.getCurrentPageShapes().length
529
+ expect(shapesAfterCount).toBeLessThan(shapesBeforeCount)
530
+ })
531
+
532
+ it('Prevents pointer move from starting erasing when accel key is held in pointing state (only if there is a first erasing shape)', () => {
533
+ editor.setCurrentTool('eraser')
534
+ editor.expectToBeIn('eraser.idle')
535
+
536
+ // Start with accel key held and click on a shape
537
+ editor.keyDown('Meta')
538
+ editor.pointerDown(0, 0) // in box1
539
+ editor.expectToBeIn('eraser.pointing')
540
+
541
+ expect(editor.getErasingShapeIds()).toEqual([ids.box1])
542
+
543
+ // Try to move pointer - should not start erasing
544
+ editor.pointerMove(50, 50)
545
+ editor.expectToBeIn('eraser.pointing') // Should still be in pointing state
546
+
547
+ editor.pointerUp()
548
+ editor.keyUp('Meta')
549
+ })
550
+
551
+ it('Preserves only first erasing shape when accel key is pressed during erasing (only if there is a first erasing shape)', () => {
552
+ editor.setCurrentTool('eraser')
553
+ editor.expectToBeIn('eraser.idle')
554
+
555
+ const shapesBeforeCount = editor.getCurrentPageShapes().length
556
+
557
+ // Start erasing normally
558
+ editor.pointerDown(-100, -100) // outside of any shapes
559
+ editor.pointerMove(99, 99) // next to box1 AND in box2
560
+
561
+ jest.advanceTimersByTime(16)
562
+ expect(editor.getInstanceState().scribbles.length).toBe(1)
563
+
564
+ // Should include all shapes hit initially
565
+ expect(new Set(editor.getErasingShapeIds())).toEqual(new Set([ids.box1, ids.box2]))
566
+
567
+ // Press accel key during erasing
568
+ editor.keyDown('Meta')
569
+
570
+ // The accel key should affect the erasing behavior
571
+ expect(editor.getErasingShapeIds().length).toBeGreaterThan(0)
572
+
573
+ editor.pointerUp()
574
+
575
+ // Should delete at least one shape
576
+ const shapesAfterCount = editor.getCurrentPageShapes().length
577
+ expect(shapesAfterCount).toBeLessThan(shapesBeforeCount)
578
+
579
+ editor.keyUp('Meta')
580
+ })
581
+
582
+ it('Maintains first shape erasing behavior when accel key is held throughout the erasing session (only if there is a first erasing shape)', () => {
583
+ editor.setCurrentTool('eraser')
584
+ editor.expectToBeIn('eraser.idle')
585
+
586
+ const shapesBeforeCount = editor.getCurrentPageShapes().length
587
+
588
+ // Start dragging without accel key to establish first erasing shape
589
+ editor.pointerDown(-100, -100) // outside of any shapes
590
+ editor.pointerMove(99, 99) // next to box1 AND in box2
591
+
592
+ jest.advanceTimersByTime(16)
593
+ expect(editor.getInstanceState().scribbles.length).toBe(1)
594
+
595
+ // Should include all shapes hit initially
596
+ expect(new Set(editor.getErasingShapeIds())).toEqual(new Set([ids.box1, ids.box2]))
597
+
598
+ // Press accel key to restrict to first shape
599
+ editor.keyDown('Meta')
600
+ expect(editor.getErasingShapeIds().length).toBeGreaterThan(0)
601
+
602
+ // Move to hit more shapes
603
+ editor.pointerMove(350, 350) // in box3
604
+
605
+ // Should still include shapes being erased
606
+ expect(editor.getErasingShapeIds().length).toBeGreaterThan(0)
607
+
608
+ editor.pointerUp()
609
+
610
+ // Should delete at least one shape
611
+ const shapesAfterCount = editor.getCurrentPageShapes().length
612
+ expect(shapesAfterCount).toBeLessThan(shapesBeforeCount)
613
+
614
+ editor.keyUp('Meta')
445
615
  })
446
616
  })