@tldraw/editor 5.2.0-canary.fff413eea248 → 5.2.0-next.1f1ecb7994d5

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 (55) hide show
  1. package/dist-cjs/index.d.ts +5 -0
  2. package/dist-cjs/index.js +1 -1
  3. package/dist-cjs/lib/components/MenuClickCapture.js +8 -5
  4. package/dist-cjs/lib/components/MenuClickCapture.js.map +2 -2
  5. package/dist-cjs/lib/editor/Editor.js +10 -1
  6. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  7. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js +12 -0
  8. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +2 -2
  9. package/dist-cjs/lib/editor/managers/InputsManager/InputsManager.js +14 -3
  10. package/dist-cjs/lib/editor/managers/InputsManager/InputsManager.js.map +2 -2
  11. package/dist-cjs/lib/editor/managers/TickManager/TickManager.js +0 -1
  12. package/dist-cjs/lib/editor/managers/TickManager/TickManager.js.map +2 -2
  13. package/dist-cjs/lib/hooks/useCanvasEvents.js +14 -7
  14. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  15. package/dist-cjs/lib/utils/getPointerInfo.js +2 -1
  16. package/dist-cjs/lib/utils/getPointerInfo.js.map +2 -2
  17. package/dist-cjs/lib/utils/pointer.js +32 -0
  18. package/dist-cjs/lib/utils/pointer.js.map +7 -0
  19. package/dist-cjs/version.js +3 -3
  20. package/dist-cjs/version.js.map +1 -1
  21. package/dist-esm/index.d.mts +5 -0
  22. package/dist-esm/index.mjs +1 -1
  23. package/dist-esm/lib/components/MenuClickCapture.mjs +8 -5
  24. package/dist-esm/lib/components/MenuClickCapture.mjs.map +2 -2
  25. package/dist-esm/lib/editor/Editor.mjs +10 -1
  26. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  27. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +12 -0
  28. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +2 -2
  29. package/dist-esm/lib/editor/managers/InputsManager/InputsManager.mjs +14 -3
  30. package/dist-esm/lib/editor/managers/InputsManager/InputsManager.mjs.map +2 -2
  31. package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs +0 -1
  32. package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs.map +2 -2
  33. package/dist-esm/lib/hooks/useCanvasEvents.mjs +14 -7
  34. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  35. package/dist-esm/lib/utils/getPointerInfo.mjs +2 -1
  36. package/dist-esm/lib/utils/getPointerInfo.mjs.map +2 -2
  37. package/dist-esm/lib/utils/pointer.mjs +12 -0
  38. package/dist-esm/lib/utils/pointer.mjs.map +7 -0
  39. package/dist-esm/version.mjs +3 -3
  40. package/dist-esm/version.mjs.map +1 -1
  41. package/editor.css +3 -3
  42. package/package.json +7 -7
  43. package/src/lib/components/MenuClickCapture.tsx +8 -4
  44. package/src/lib/editor/Editor.ts +11 -3
  45. package/src/lib/editor/managers/HistoryManager/HistoryManager.test.ts +32 -0
  46. package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +14 -2
  47. package/src/lib/editor/managers/InputsManager/InputsManager.test.ts +61 -0
  48. package/src/lib/editor/managers/InputsManager/InputsManager.ts +16 -4
  49. package/src/lib/editor/managers/TickManager/TickManager.test.ts +0 -40
  50. package/src/lib/editor/managers/TickManager/TickManager.ts +0 -1
  51. package/src/lib/hooks/useCanvasEvents.ts +19 -12
  52. package/src/lib/utils/getPointerInfo.ts +2 -1
  53. package/src/lib/utils/pointer.test.ts +48 -0
  54. package/src/lib/utils/pointer.ts +18 -0
  55. package/src/version.ts +3 -3
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tldraw/editor",
3
3
  "description": "tldraw infinite canvas SDK (editor).",
4
- "version": "5.2.0-canary.fff413eea248",
4
+ "version": "5.2.0-next.1f1ecb7994d5",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -49,12 +49,12 @@
49
49
  "@tiptap/core": "^3.12.1",
50
50
  "@tiptap/pm": "^3.12.1",
51
51
  "@tiptap/react": "^3.12.1",
52
- "@tldraw/state": "5.2.0-canary.fff413eea248",
53
- "@tldraw/state-react": "5.2.0-canary.fff413eea248",
54
- "@tldraw/store": "5.2.0-canary.fff413eea248",
55
- "@tldraw/tlschema": "5.2.0-canary.fff413eea248",
56
- "@tldraw/utils": "5.2.0-canary.fff413eea248",
57
- "@tldraw/validate": "5.2.0-canary.fff413eea248",
52
+ "@tldraw/state": "5.2.0-next.1f1ecb7994d5",
53
+ "@tldraw/state-react": "5.2.0-next.1f1ecb7994d5",
54
+ "@tldraw/store": "5.2.0-next.1f1ecb7994d5",
55
+ "@tldraw/tlschema": "5.2.0-next.1f1ecb7994d5",
56
+ "@tldraw/utils": "5.2.0-next.1f1ecb7994d5",
57
+ "@tldraw/validate": "5.2.0-next.1f1ecb7994d5",
58
58
  "classnames": "^2.5.1",
59
59
  "eventemitter3": "^4.0.7",
60
60
  "idb": "^7.1.1",
@@ -6,6 +6,7 @@ import { useEditor } from '../hooks/useEditor'
6
6
  import { Vec } from '../primitives/Vec'
7
7
  import { releasePointerCapture, setPointerCapture } from '../utils/dom'
8
8
  import { getPointerInfo } from '../utils/getPointerInfo'
9
+ import { getPointerEventButton } from '../utils/pointer'
9
10
 
10
11
  /**
11
12
  * When a menu is open, this component prevents the user from interacting with the canvas.
@@ -68,18 +69,19 @@ export function MenuClickCapture() {
68
69
 
69
70
  const handlePointerDown = useCallback(
70
71
  (e: PointerEvent) => {
71
- if (e.button !== 0 && e.button !== 2) return
72
+ const button = getPointerEventButton(e)
73
+ if (button !== 0 && button !== 2) return
72
74
 
73
75
  flushSync(() => setIsPointing(true))
74
76
  setPointerCapture(e.currentTarget, e)
75
77
  rPointerState.current = {
76
78
  isDown: true,
77
79
  isDragging: false,
78
- button: e.button,
80
+ button,
79
81
  start: new Vec(e.clientX, e.clientY),
80
82
  }
81
83
 
82
- if (e.button === 2) {
84
+ if (button === 2) {
83
85
  if (!editor.options.rightClickPanning) {
84
86
  // Right-click panning off: close the open menu and swallow the native
85
87
  // contextmenu that would otherwise briefly open a new one (causing a flash).
@@ -141,13 +143,15 @@ export function MenuClickCapture() {
141
143
 
142
144
  const handlePointerUp = useCallback(
143
145
  (e: PointerEvent) => {
144
- const isStaticRightClick = e.button === 2 && !rPointerState.current.isDragging
146
+ const isStaticRightClick =
147
+ rPointerState.current.button === 2 && !rPointerState.current.isDragging
145
148
 
146
149
  editor.dispatch({
147
150
  type: 'pointer',
148
151
  target: 'canvas',
149
152
  name: 'pointer_up',
150
153
  ...getPointerInfo(editor, e),
154
+ button: rPointerState.current.button === 2 ? 2 : getPointerEventButton(e),
151
155
  })
152
156
 
153
157
  if (isStaticRightClick && editor.options.rightClickPanning) {
@@ -409,9 +409,6 @@ export class Editor extends EventEmitter<TLEventMap> {
409
409
  this._tickManager = new TickManager(this)
410
410
  this.disposables.add(() => this._tickManager.dispose())
411
411
  this.disposables.add(() => {
412
- // Reset camera state to 'idle' so the store isn't left stuck at 'moving'
413
- // when tick events stop (e.g. React strict mode disposes while camera is moving)
414
- this.off('tick', this._decayCameraStateTimeout)
415
412
  this._setCameraState('idle')
416
413
  })
417
414
 
@@ -419,6 +416,7 @@ export class Editor extends EventEmitter<TLEventMap> {
419
416
  this.disposables.add(() => this.fonts.dispose())
420
417
 
421
418
  this.inputs = new InputsManager(this)
419
+ this.disposables.add(() => this.inputs.dispose())
422
420
  this.performance = new PerformanceManager(this)
423
421
  this.disposables.add(() => this.performance.dispose())
424
422
  this.collaborators = new CollaboratorsManager(this)
@@ -1179,6 +1177,7 @@ export class Editor extends EventEmitter<TLEventMap> {
1179
1177
  this.store.dispose()
1180
1178
  this.isDisposed = true
1181
1179
  this.emit('dispose')
1180
+ this.removeAllListeners()
1182
1181
  }
1183
1182
 
1184
1183
  /* ------------------ Themes (shadowing the theme manager) ------------------ */
@@ -1551,6 +1550,15 @@ export class Editor extends EventEmitter<TLEventMap> {
1551
1550
  return this.history.getMarkIdMatching(idSubstring)
1552
1551
  }
1553
1552
 
1553
+ /**
1554
+ * Whether the editor is currently replaying history (i.e. an undo or redo is being applied).
1555
+ *
1556
+ * @internal
1557
+ */
1558
+ isReplayingHistory(): boolean {
1559
+ return this.history.isReplaying()
1560
+ }
1561
+
1554
1562
  /**
1555
1563
  * Coalesces all changes since the given mark into a single change, removing any intermediate marks.
1556
1564
  *
@@ -584,6 +584,38 @@ describe('HistoryManager getters and utilities', () => {
584
584
  store.update(ids.a, (s) => ({ ...s, value: 2 }))
585
585
  expect(manager.getNumRedos()).toBe(0)
586
586
  })
587
+
588
+ it('should only report replaying while undoing or redoing', () => {
589
+ const replayStates: boolean[] = []
590
+ const dispose = store.addHistoryInterceptor(() => {
591
+ replayStates.push(manager.isReplaying())
592
+ })
593
+
594
+ store.update(ids.a, (s) => ({ ...s, value: 1 }))
595
+ expect(replayStates).toEqual([false])
596
+
597
+ replayStates.length = 0
598
+ manager.undo()
599
+ expect(replayStates).toEqual([true])
600
+ expect(manager.isReplaying()).toBe(false)
601
+
602
+ replayStates.length = 0
603
+ manager.redo()
604
+ expect(replayStates).toEqual([true])
605
+ expect(manager.isReplaying()).toBe(false)
606
+
607
+ replayStates.length = 0
608
+ manager.batch(
609
+ () => {
610
+ store.update(ids.a, (s) => ({ ...s, value: 2 }))
611
+ },
612
+ { history: 'ignore' }
613
+ )
614
+ expect(replayStates).toEqual([false])
615
+ expect(manager.isReplaying()).toBe(false)
616
+
617
+ dispose()
618
+ })
587
619
  })
588
620
 
589
621
  describe('getMarkIdMatching', () => {
@@ -83,6 +83,14 @@ export class HistoryManager<R extends UnknownRecord> {
83
83
  return this.stacks.get().redos.length
84
84
  }
85
85
 
86
+ /** @internal */
87
+ private _isReplaying = false
88
+
89
+ /** @internal */
90
+ isReplaying() {
91
+ return this._isReplaying
92
+ }
93
+
86
94
  /** @internal */
87
95
  _isInBatch = false
88
96
 
@@ -119,7 +127,9 @@ export class HistoryManager<R extends UnknownRecord> {
119
127
  // History
120
128
  _undo({ pushToRedoStack, toMark = undefined }: { pushToRedoStack: boolean; toMark?: string }) {
121
129
  const previousState = this.state
130
+ const previousIsReplaying = this._isReplaying
122
131
  this.state = HistoryRecorderState.Paused
132
+ this._isReplaying = true
123
133
  try {
124
134
  let { undos, redos } = this.stacks.get()
125
135
 
@@ -180,11 +190,11 @@ export class HistoryManager<R extends UnknownRecord> {
180
190
  this.pendingDiff.restore(pendingDiff)
181
191
  return this
182
192
  }
183
-
184
193
  this.store.applyDiff(diffToUndo, { ignoreEphemeralKeys: true })
185
194
  this.store.ensureStoreIsUsable()
186
195
  this.stacks.set({ undos, redos })
187
196
  } finally {
197
+ this._isReplaying = previousIsReplaying
188
198
  this.state = previousState
189
199
  }
190
200
 
@@ -199,7 +209,9 @@ export class HistoryManager<R extends UnknownRecord> {
199
209
 
200
210
  redo() {
201
211
  const previousState = this.state
212
+ const previousIsReplaying = this._isReplaying
202
213
  this.state = HistoryRecorderState.Paused
214
+ this._isReplaying = true
203
215
  try {
204
216
  this.flushPendingDiff()
205
217
 
@@ -228,11 +240,11 @@ export class HistoryManager<R extends UnknownRecord> {
228
240
  break
229
241
  }
230
242
  }
231
-
232
243
  this.store.applyDiff(diffToRedo, { ignoreEphemeralKeys: true })
233
244
  this.store.ensureStoreIsUsable()
234
245
  this.stacks.set({ undos, redos })
235
246
  } finally {
247
+ this._isReplaying = previousIsReplaying
236
248
  this.state = previousState
237
249
  }
238
250
 
@@ -0,0 +1,61 @@
1
+ import { createTLStore } from '../../../config/createTLStore'
2
+ import { Editor } from '../../Editor'
3
+
4
+ function createTestEditor() {
5
+ const store = createTLStore({})
6
+ store.ensureStoreIsUsable()
7
+ return new Editor({
8
+ store,
9
+ bindingUtils: [],
10
+ shapeUtils: [],
11
+ getContainer: () => document.createElement('div'),
12
+ tools: [],
13
+ })
14
+ }
15
+
16
+ describe('InputsManager', () => {
17
+ let editor: Editor
18
+
19
+ beforeEach(() => {
20
+ editor = createTestEditor()
21
+ })
22
+
23
+ afterEach(() => {
24
+ editor.dispose()
25
+ })
26
+
27
+ it('updates pointer velocity on frame events', () => {
28
+ const point = editor.inputs.getCurrentScreenPoint()
29
+ point.x = 0
30
+ point.y = 0
31
+ editor.emit('frame', 16)
32
+
33
+ point.x = 100
34
+ point.y = 0
35
+ editor.emit('frame', 16)
36
+
37
+ expect(editor.inputs.getPointerVelocity().len()).toBeGreaterThan(0)
38
+ })
39
+
40
+ it('stops updating pointer velocity after dispose', () => {
41
+ const point = editor.inputs.getCurrentScreenPoint()
42
+ point.x = 0
43
+ point.y = 0
44
+ editor.emit('frame', 16)
45
+
46
+ point.x = 100
47
+ point.y = 0
48
+ editor.emit('frame', 16)
49
+
50
+ const velocityBeforeDispose = editor.inputs.getPointerVelocity().clone()
51
+ expect(velocityBeforeDispose.len()).toBeGreaterThan(0)
52
+
53
+ editor.inputs.dispose()
54
+
55
+ point.x = 200
56
+ point.y = 0
57
+ editor.emit('frame', 16)
58
+
59
+ expect(editor.inputs.getPointerVelocity()).toEqual(velocityBeforeDispose)
60
+ })
61
+ })
@@ -1,6 +1,7 @@
1
1
  import { atom, computed, unsafe__withoutCapture } from '@tldraw/state'
2
2
  import { AtomSet } from '@tldraw/store'
3
3
  import { TLINSTANCE_ID, TLPOINTER_ID } from '@tldraw/tlschema'
4
+ import { bind } from '@tldraw/utils'
4
5
  import { INTERNAL_POINTER_IDS } from '../../../constants'
5
6
  import { Vec } from '../../../primitives/Vec'
6
7
  import { isAccelKey } from '../../../utils/keyboard'
@@ -12,7 +13,19 @@ const POINTER_VELOCITY_REFERENCE_SMOOTHING = 0.5
12
13
 
13
14
  /** @public */
14
15
  export class InputsManager {
15
- constructor(private readonly editor: Editor) {}
16
+ constructor(private readonly editor: Editor) {
17
+ this.editor.on('frame', this._onFrame)
18
+ }
19
+
20
+ /** @internal */
21
+ dispose() {
22
+ this.editor.off('frame', this._onFrame)
23
+ }
24
+
25
+ @bind
26
+ private _onFrame(elapsed: number) {
27
+ this.updatePointerVelocity(elapsed)
28
+ }
16
29
 
17
30
  private _originPagePoint = atom<Vec>('originPagePoint', new Vec())
18
31
  /**
@@ -120,8 +133,7 @@ export class InputsManager {
120
133
  }
121
134
 
122
135
  /**
123
- * Normally you shouldn't need to set the pointer velocity directly, this is set by the tick manager.
124
- * However, this is currently used in tests to fake pointer velocity.
136
+ * Normally you shouldn't need to set the pointer velocity directly. Used in tests to fake pointer velocity.
125
137
  * @param pointerVelocity - The pointer velocity.
126
138
  * @internal
127
139
  */
@@ -460,7 +472,7 @@ export class InputsManager {
460
472
  private _velocityPrevPoint = new Vec()
461
473
 
462
474
  /**
463
- * Update the pointer velocity based on elapsed time. Called by the tick manager.
475
+ * Update the pointer velocity based on elapsed time. Called each frame.
464
476
  * @param elapsed - The time elapsed since the last tick in milliseconds.
465
477
  * @internal
466
478
  */
@@ -1,5 +1,4 @@
1
1
  import { Mock, Mocked, vi } from 'vitest'
2
- import { Vec } from '../../../primitives/Vec'
3
2
  import { Editor } from '../../Editor'
4
3
  import { TickManager } from './TickManager'
5
4
 
@@ -21,17 +20,6 @@ describe('TickManager', () => {
21
20
  let tickManager: TickManager
22
21
  let mockEmit: Mock
23
22
  let mockDisposablesAdd: Mock
24
- let mockInputs: {
25
- _currentScreenPoint: Vec
26
- getCurrentScreenPoint(): Vec
27
- currentScreenPoint: Vec
28
- setCurrentScreenPoint(value: Vec): void
29
- _pointerVelocity: Vec
30
- getPointerVelocity(): Vec
31
- pointerVelocity: Vec
32
- setPointerVelocity(value: Vec): void
33
- updatePointerVelocity(elapsed: number): void
34
- }
35
23
 
36
24
  beforeEach(() => {
37
25
  vi.clearAllMocks()
@@ -52,39 +40,11 @@ describe('TickManager', () => {
52
40
  mockEmit = vi.fn()
53
41
  mockDisposablesAdd = vi.fn()
54
42
 
55
- // Create a mock inputs object with getters and setters
56
- mockInputs = {
57
- _currentScreenPoint: new Vec(100, 100),
58
- getCurrentScreenPoint() {
59
- return this._currentScreenPoint
60
- },
61
- get currentScreenPoint() {
62
- return this.getCurrentScreenPoint()
63
- },
64
- setCurrentScreenPoint(value: Vec) {
65
- this._currentScreenPoint = value
66
- },
67
- _pointerVelocity: new Vec(0, 0),
68
- getPointerVelocity() {
69
- return this._pointerVelocity
70
- },
71
- get pointerVelocity() {
72
- return this.getPointerVelocity()
73
- },
74
- setPointerVelocity(value: Vec) {
75
- this._pointerVelocity = value
76
- },
77
- updatePointerVelocity(_elapsed: number) {
78
- // Mock implementation - no-op for tests
79
- },
80
- }
81
-
82
43
  editor = {
83
44
  emit: mockEmit,
84
45
  disposables: {
85
46
  add: mockDisposablesAdd,
86
47
  },
87
- inputs: mockInputs as unknown as Editor['inputs'],
88
48
  } as unknown as Mocked<Editor>
89
49
 
90
50
  tickManager = new TickManager(editor)
@@ -40,7 +40,6 @@ export class TickManager {
40
40
  const elapsed = now - this.now
41
41
  this.now = now
42
42
 
43
- this.editor.inputs.updatePointerVelocity(elapsed)
44
43
  this.editor.emit('frame', elapsed)
45
44
  this.editor.emit('tick', elapsed)
46
45
  this.cancelRaf = throttleToNextFrame(this.tick)
@@ -8,6 +8,7 @@ import {
8
8
  setPointerCapture,
9
9
  } from '../utils/dom'
10
10
  import { getPointerInfo } from '../utils/getPointerInfo'
11
+ import { getPointerEventButton, isSecondaryClickEvent } from '../utils/pointer'
11
12
  import { useEditor } from './useEditor'
12
13
 
13
14
  export function useCanvasEvents() {
@@ -17,12 +18,16 @@ export function useCanvasEvents() {
17
18
 
18
19
  const events = useMemo(
19
20
  function canvasEvents() {
21
+ let isSecondaryClickPointerDown = false
22
+
20
23
  function onPointerDown(e: React.PointerEvent) {
21
24
  if (editor.wasEventAlreadyHandled(e)) return
25
+ const button = getPointerEventButton(e)
26
+ isSecondaryClickPointerDown = button === 2
22
27
 
23
28
  // With right-click panning disabled, fire right_click on press and let the
24
29
  // native contextmenu through so the menu opens at the pointer-down location.
25
- if (e.button === 2 && !editor.options.rightClickPanning) {
30
+ if (button === 2 && !editor.options.rightClickPanning) {
26
31
  editor.dispatch({
27
32
  type: 'pointer',
28
33
  target: 'canvas',
@@ -32,7 +37,7 @@ export function useCanvasEvents() {
32
37
  return
33
38
  }
34
39
 
35
- if (e.button !== 0 && e.button !== 1 && e.button !== 2 && e.button !== 5) return
40
+ if (button !== 0 && button !== 1 && button !== 2 && button !== 5) return
36
41
 
37
42
  setPointerCapture(e.currentTarget, e)
38
43
 
@@ -46,12 +51,13 @@ export function useCanvasEvents() {
46
51
 
47
52
  function onPointerUp(e: React.PointerEvent) {
48
53
  if (editor.wasEventAlreadyHandled(e)) return
49
- if (e.button !== 0 && e.button !== 1 && e.button !== 2 && e.button !== 5) return
54
+ const button = isSecondaryClickPointerDown ? 2 : getPointerEventButton(e)
55
+ if (button !== 0 && button !== 1 && button !== 2 && button !== 5) return
50
56
 
51
57
  const rightClickPanning = editor.options.rightClickPanning
52
58
  // Check before dispatch (which resets isPanning)
53
59
  const wasRightClickPanning =
54
- rightClickPanning && e.button === 2 && editor.inputs.getIsPanning()
60
+ rightClickPanning && button === 2 && editor.inputs.getIsPanning()
55
61
 
56
62
  releasePointerCapture(e.currentTarget, e)
57
63
 
@@ -60,10 +66,11 @@ export function useCanvasEvents() {
60
66
  target: 'canvas',
61
67
  name: 'pointer_up',
62
68
  ...getPointerInfo(editor, e),
69
+ button,
63
70
  })
64
71
 
65
72
  // Static right-click: fire contextmenu at the pointer-up location
66
- if (rightClickPanning && e.button === 2 && !wasRightClickPanning) {
73
+ if (rightClickPanning && button === 2 && !wasRightClickPanning) {
67
74
  const contextMenuEvent = new PointerEvent('contextmenu', {
68
75
  bubbles: true,
69
76
  clientX: e.clientX,
@@ -76,6 +83,7 @@ export function useCanvasEvents() {
76
83
  })
77
84
  e.currentTarget.dispatchEvent(contextMenuEvent)
78
85
  }
86
+ isSecondaryClickPointerDown = false
79
87
  }
80
88
 
81
89
  function onPointerEnter(e: React.PointerEvent) {
@@ -171,16 +179,15 @@ export function useCanvasEvents() {
171
179
  // Synthetic events — our own dispatch from onPointerUp, or tests using
172
180
  // fireEvent.contextMenu — pass through so Radix can open the menu.
173
181
  if (!e.nativeEvent.isTrusted) return
174
- // Only suppress the native browser contextmenu when it follows a real
175
- // right-click (button=2 with no ctrl modifier). For those, our pointer
176
- // handling has already decided what to do (either we'll dispatch a
177
- // synthetic contextmenu on pointerup to open the menu at the release
178
- // position, or we panned and don't want a menu at all).
182
+ // Only suppress the native browser contextmenu when it follows a
183
+ // secondary click. For those, our pointer handling has already
184
+ // decided what to do (either we'll dispatch a synthetic contextmenu on
185
+ // pointerup to open the menu at the release position, or we panned and
186
+ // don't want a menu at all).
179
187
  //
180
188
  // Other contextmenu sources must reach Radix so the menu opens:
181
- // - ctrl+click on macOS (button=0, or button=2 with ctrlKey=true)
182
189
  // - long-press on touch devices (button=0, pointerType=touch)
183
- if (e.button !== 2 || e.ctrlKey) return
190
+ if (!isSecondaryClickEvent(e)) return
184
191
  preventDefault(e)
185
192
  }
186
193
 
@@ -1,6 +1,7 @@
1
1
  import type React from 'react'
2
2
  import { Editor } from '../editor/Editor'
3
3
  import { isAccelKey } from './keyboard'
4
+ import { getPointerEventButton } from './pointer'
4
5
 
5
6
  /** @public */
6
7
  export function getPointerInfo(editor: Editor, e: React.PointerEvent | PointerEvent) {
@@ -18,7 +19,7 @@ export function getPointerInfo(editor: Editor, e: React.PointerEvent | PointerEv
18
19
  metaKey: e.metaKey,
19
20
  accelKey: isAccelKey(e),
20
21
  pointerId: e.pointerId,
21
- button: e.button,
22
+ button: getPointerEventButton(e),
22
23
  isPen: e.pointerType === 'pen',
23
24
  }
24
25
  }
@@ -0,0 +1,48 @@
1
+ import { tlenv } from '../globals/environment'
2
+ import { TestEditor } from '../test/TestEditor'
3
+ import { getPointerInfo } from './getPointerInfo'
4
+ import { isSecondaryClickEvent } from './pointer'
5
+
6
+ const originalIsDarwin = tlenv.isDarwin
7
+
8
+ afterEach(() => {
9
+ tlenv.isDarwin = originalIsDarwin
10
+ })
11
+
12
+ describe('isSecondaryClickEvent', () => {
13
+ it('treats ctrl + left-click as a secondary click on macOS', () => {
14
+ tlenv.isDarwin = true
15
+ expect(isSecondaryClickEvent({ button: 0, ctrlKey: true, metaKey: false })).toBe(true)
16
+ })
17
+
18
+ it('does not treat ctrl + left-click as a secondary click off macOS', () => {
19
+ tlenv.isDarwin = false
20
+ expect(isSecondaryClickEvent({ button: 0, ctrlKey: true, metaKey: false })).toBe(false)
21
+ })
22
+ })
23
+
24
+ describe('ctrl + left-click on macOS fires as a right-click (regression)', () => {
25
+ // Regression test for https://github.com/tldraw/tldraw/issues/8217
26
+ // On macOS, a ctrl + left-click should be treated as a right-click
27
+ let editor: TestEditor
28
+
29
+ beforeEach(() => {
30
+ editor = new TestEditor()
31
+ })
32
+
33
+ afterEach(() => {
34
+ editor.dispose()
35
+ })
36
+
37
+ it('reports button 2 in the dispatched pointer info on macOS', () => {
38
+ tlenv.isDarwin = true
39
+ const event = new PointerEvent('pointerdown', { button: 0, ctrlKey: true })
40
+ expect(getPointerInfo(editor, event).button).toBe(2)
41
+ })
42
+
43
+ it('stays button 0 for the same gesture off macOS', () => {
44
+ tlenv.isDarwin = false
45
+ const event = new PointerEvent('pointerdown', { button: 0, ctrlKey: true })
46
+ expect(getPointerInfo(editor, event).button).toBe(0)
47
+ })
48
+ })
@@ -0,0 +1,18 @@
1
+ import { tlenv } from '../globals/environment'
2
+
3
+ /** @internal */
4
+ interface PointerLike {
5
+ button: number
6
+ ctrlKey: boolean
7
+ metaKey: boolean
8
+ }
9
+
10
+ /** @internal */
11
+ export function isSecondaryClickEvent(e: PointerLike) {
12
+ return e.button === 2 || (tlenv.isDarwin && e.button === 0 && e.ctrlKey && !e.metaKey)
13
+ }
14
+
15
+ /** @internal */
16
+ export function getPointerEventButton(e: PointerLike) {
17
+ return isSecondaryClickEvent(e) ? 2 : e.button
18
+ }
package/src/version.ts CHANGED
@@ -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 = '5.2.0-canary.fff413eea248'
4
+ export const version = '5.2.0-next.1f1ecb7994d5'
5
5
  export const publishDates = {
6
6
  major: '2026-05-06T16:28:18.473Z',
7
- minor: '2026-06-16T08:45:01.794Z',
8
- patch: '2026-06-16T08:45:01.794Z',
7
+ minor: '2026-06-18T11:19:35.548Z',
8
+ patch: '2026-06-18T11:19:35.548Z',
9
9
  }