@tldraw/editor 5.2.0-canary.6421dbe4e25a → 5.2.0-canary.667cbd6bb6ff

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 (94) hide show
  1. package/dist-cjs/index.d.ts +13 -13
  2. package/dist-cjs/index.js +1 -5
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/components/MenuClickCapture.js +8 -5
  5. package/dist-cjs/lib/components/MenuClickCapture.js.map +2 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +1 -16
  7. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  8. package/dist-cjs/lib/editor/Editor.js +11 -2
  9. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  10. package/dist-cjs/lib/editor/managers/FontManager/FontManager.js +28 -4
  11. package/dist-cjs/lib/editor/managers/FontManager/FontManager.js.map +3 -3
  12. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js +12 -0
  13. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +2 -2
  14. package/dist-cjs/lib/editor/managers/InputsManager/InputsManager.js +14 -3
  15. package/dist-cjs/lib/editor/managers/InputsManager/InputsManager.js.map +2 -2
  16. package/dist-cjs/lib/editor/managers/TickManager/TickManager.js +0 -1
  17. package/dist-cjs/lib/editor/managers/TickManager/TickManager.js.map +2 -2
  18. package/dist-cjs/lib/editor/types/event-types.js.map +2 -2
  19. package/dist-cjs/lib/globals/environment.js +6 -1
  20. package/dist-cjs/lib/globals/environment.js.map +2 -2
  21. package/dist-cjs/lib/hooks/EditorComponentsContext.js.map +2 -2
  22. package/dist-cjs/lib/hooks/useCanvasEvents.js +17 -8
  23. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  24. package/dist-cjs/lib/hooks/useEditorComponents.js +0 -1
  25. package/dist-cjs/lib/hooks/useEditorComponents.js.map +2 -2
  26. package/dist-cjs/lib/utils/getPointerInfo.js +2 -1
  27. package/dist-cjs/lib/utils/getPointerInfo.js.map +2 -2
  28. package/dist-cjs/lib/{hooks/useTransform.js → utils/pointer.js} +17 -23
  29. package/dist-cjs/lib/utils/pointer.js.map +7 -0
  30. package/dist-cjs/version.js +3 -3
  31. package/dist-cjs/version.js.map +1 -1
  32. package/dist-esm/index.d.mts +13 -13
  33. package/dist-esm/index.mjs +1 -7
  34. package/dist-esm/index.mjs.map +2 -2
  35. package/dist-esm/lib/components/MenuClickCapture.mjs +8 -5
  36. package/dist-esm/lib/components/MenuClickCapture.mjs.map +2 -2
  37. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +1 -16
  38. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  39. package/dist-esm/lib/editor/Editor.mjs +11 -2
  40. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  41. package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs +28 -4
  42. package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs.map +3 -3
  43. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +12 -0
  44. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +2 -2
  45. package/dist-esm/lib/editor/managers/InputsManager/InputsManager.mjs +14 -3
  46. package/dist-esm/lib/editor/managers/InputsManager/InputsManager.mjs.map +2 -2
  47. package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs +0 -1
  48. package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs.map +2 -2
  49. package/dist-esm/lib/editor/types/event-types.mjs.map +2 -2
  50. package/dist-esm/lib/globals/environment.mjs +6 -1
  51. package/dist-esm/lib/globals/environment.mjs.map +2 -2
  52. package/dist-esm/lib/hooks/EditorComponentsContext.mjs.map +2 -2
  53. package/dist-esm/lib/hooks/useCanvasEvents.mjs +17 -8
  54. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  55. package/dist-esm/lib/hooks/useEditorComponents.mjs +0 -1
  56. package/dist-esm/lib/hooks/useEditorComponents.mjs.map +2 -2
  57. package/dist-esm/lib/utils/getPointerInfo.mjs +2 -1
  58. package/dist-esm/lib/utils/getPointerInfo.mjs.map +2 -2
  59. package/dist-esm/lib/utils/pointer.mjs +17 -0
  60. package/dist-esm/lib/utils/pointer.mjs.map +7 -0
  61. package/dist-esm/version.mjs +3 -3
  62. package/dist-esm/version.mjs.map +1 -1
  63. package/editor.css +3 -3
  64. package/package.json +10 -7
  65. package/src/index.ts +0 -5
  66. package/src/lib/components/MenuClickCapture.tsx +8 -4
  67. package/src/lib/components/default-components/DefaultCanvas.tsx +1 -17
  68. package/src/lib/editor/Editor.ts +15 -5
  69. package/src/lib/editor/managers/FontManager/FontManager.test.ts +24 -1
  70. package/src/lib/editor/managers/FontManager/FontManager.ts +46 -1
  71. package/src/lib/editor/managers/HistoryManager/HistoryManager.test.ts +32 -0
  72. package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +14 -2
  73. package/src/lib/editor/managers/InputsManager/InputsManager.test.ts +61 -0
  74. package/src/lib/editor/managers/InputsManager/InputsManager.ts +16 -4
  75. package/src/lib/editor/managers/TickManager/TickManager.test.ts +0 -40
  76. package/src/lib/editor/managers/TickManager/TickManager.ts +0 -1
  77. package/src/lib/editor/types/event-types.ts +7 -0
  78. package/src/lib/globals/environment.ts +7 -0
  79. package/src/lib/hooks/EditorComponentsContext.tsx +0 -2
  80. package/src/lib/hooks/useCanvasEvents.ts +24 -12
  81. package/src/lib/hooks/useEditorComponents.tsx +0 -1
  82. package/src/lib/utils/getPointerInfo.ts +2 -1
  83. package/src/lib/utils/pointer.test.ts +76 -0
  84. package/src/lib/utils/pointer.ts +42 -0
  85. package/src/version.ts +3 -3
  86. package/dist-cjs/lib/components/default-components/DefaultSelectionBackground.js +0 -49
  87. package/dist-cjs/lib/components/default-components/DefaultSelectionBackground.js.map +0 -7
  88. package/dist-cjs/lib/hooks/useTransform.js.map +0 -7
  89. package/dist-esm/lib/components/default-components/DefaultSelectionBackground.mjs +0 -19
  90. package/dist-esm/lib/components/default-components/DefaultSelectionBackground.mjs.map +0 -7
  91. package/dist-esm/lib/hooks/useTransform.mjs +0 -23
  92. package/dist-esm/lib/hooks/useTransform.mjs.map +0 -7
  93. package/src/lib/components/default-components/DefaultSelectionBackground.tsx +0 -25
  94. package/src/lib/hooks/useTransform.ts +0 -30
@@ -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)
@@ -63,6 +63,13 @@ export type TLPointerEventInfo = TLBaseEventInfo & {
63
63
  pointerId: number
64
64
  button: number
65
65
  isPen: boolean
66
+ /**
67
+ * Whether this pen event appears to be direct manipulation on the display (e.g. Apple Pencil on
68
+ * an iPad or a Surface Pen on a touchscreen) rather than indirect input from a desktop graphics
69
+ * tablet (e.g. a Wacom Intuos). Only direct-display pens should auto-enable pen mode. Drawing and
70
+ * pressure behavior is driven by `isPen` and applies to all pens regardless of this flag.
71
+ */
72
+ isPenDirect?: boolean
66
73
  } & TLPointerEventTarget
67
74
 
68
75
  /** @public */
@@ -17,6 +17,10 @@ const tlenv = {
17
17
  isWebview: false,
18
18
  isDarwin: false,
19
19
  hasCanvasSupport: false,
20
+ // Whether the device has a touch screen (an integrated coarse pointer, e.g. an iPad or a
21
+ // touchscreen laptop). Unlike `isCoarsePointer`, this reflects the device's hardware rather than
22
+ // the pointer currently in use, so it stays stable when a pen or mouse is used.
23
+ isTouchDevice: false,
20
24
  }
21
25
 
22
26
  let isForcedFinePointer = false
@@ -32,6 +36,9 @@ if (typeof window !== 'undefined') {
32
36
  }
33
37
  tlenv.hasCanvasSupport = 'Promise' in window && 'HTMLCanvasElement' in window
34
38
  isForcedFinePointer = tlenv.isFirefox && !tlenv.isAndroid && !tlenv.isIos
39
+ tlenv.isTouchDevice =
40
+ ('navigator' in window && window.navigator.maxTouchPoints > 0) ||
41
+ (typeof window.matchMedia === 'function' && window.matchMedia('(any-pointer: coarse)').matches)
35
42
  }
36
43
 
37
44
  /**
@@ -2,7 +2,6 @@ import { ComponentType, RefAttributes, createContext, useContext } from 'react'
2
2
  import type { TLCanvasComponentProps } from '../components/default-components/DefaultCanvas'
3
3
  import type { TLErrorFallbackComponent } from '../components/default-components/DefaultErrorFallback'
4
4
  import type { TLGridProps } from '../components/default-components/DefaultGrid'
5
- import type { TLSelectionBackgroundProps } from '../components/default-components/DefaultSelectionBackground'
6
5
  import type { TLShapeErrorFallbackComponent } from '../components/default-components/DefaultShapeErrorFallback'
7
6
  import type { TLShapeWrapperProps } from '../components/default-components/DefaultShapeWrapper'
8
7
 
@@ -14,7 +13,6 @@ export interface TLEditorComponents {
14
13
  InFrontOfTheCanvas?: ComponentType | null
15
14
  LoadingScreen?: ComponentType | null
16
15
  OnTheCanvas?: ComponentType | null
17
- SelectionBackground?: ComponentType<TLSelectionBackgroundProps> | null
18
16
  ShapeWrapper?: ComponentType<TLShapeWrapperProps & RefAttributes<HTMLDivElement>> | null
19
17
  Spinner?: ComponentType<React.SVGProps<SVGSVGElement>> | null
20
18
  SvgDefs?: ComponentType | null
@@ -8,6 +8,7 @@ import {
8
8
  setPointerCapture,
9
9
  } from '../utils/dom'
10
10
  import { getPointerInfo } from '../utils/getPointerInfo'
11
+ import { getPointerEventButton, isDirectDisplayPen, 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,11 @@ 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
41
+
42
+ // Detect direct-display pen input (Apple Pencil, Surface Pen on a touchscreen) so we
43
+ // only auto-enable pen mode for it, not for an indirect desktop tablet stylus.
44
+ const isPenDirect = isDirectDisplayPen(e)
36
45
 
37
46
  setPointerCapture(e.currentTarget, e)
38
47
 
@@ -41,17 +50,19 @@ export function useCanvasEvents() {
41
50
  target: 'canvas',
42
51
  name: 'pointer_down',
43
52
  ...getPointerInfo(editor, e),
53
+ isPenDirect,
44
54
  })
45
55
  }
46
56
 
47
57
  function onPointerUp(e: React.PointerEvent) {
48
58
  if (editor.wasEventAlreadyHandled(e)) return
49
- if (e.button !== 0 && e.button !== 1 && e.button !== 2 && e.button !== 5) return
59
+ const button = isSecondaryClickPointerDown ? 2 : getPointerEventButton(e)
60
+ if (button !== 0 && button !== 1 && button !== 2 && button !== 5) return
50
61
 
51
62
  const rightClickPanning = editor.options.rightClickPanning
52
63
  // Check before dispatch (which resets isPanning)
53
64
  const wasRightClickPanning =
54
- rightClickPanning && e.button === 2 && editor.inputs.getIsPanning()
65
+ rightClickPanning && button === 2 && editor.inputs.getIsPanning()
55
66
 
56
67
  releasePointerCapture(e.currentTarget, e)
57
68
 
@@ -60,10 +71,11 @@ export function useCanvasEvents() {
60
71
  target: 'canvas',
61
72
  name: 'pointer_up',
62
73
  ...getPointerInfo(editor, e),
74
+ button,
63
75
  })
64
76
 
65
77
  // Static right-click: fire contextmenu at the pointer-up location
66
- if (rightClickPanning && e.button === 2 && !wasRightClickPanning) {
78
+ if (rightClickPanning && button === 2 && !wasRightClickPanning) {
67
79
  const contextMenuEvent = new PointerEvent('contextmenu', {
68
80
  bubbles: true,
69
81
  clientX: e.clientX,
@@ -76,6 +88,7 @@ export function useCanvasEvents() {
76
88
  })
77
89
  e.currentTarget.dispatchEvent(contextMenuEvent)
78
90
  }
91
+ isSecondaryClickPointerDown = false
79
92
  }
80
93
 
81
94
  function onPointerEnter(e: React.PointerEvent) {
@@ -171,16 +184,15 @@ export function useCanvasEvents() {
171
184
  // Synthetic events — our own dispatch from onPointerUp, or tests using
172
185
  // fireEvent.contextMenu — pass through so Radix can open the menu.
173
186
  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).
187
+ // Only suppress the native browser contextmenu when it follows a
188
+ // secondary click. For those, our pointer handling has already
189
+ // decided what to do (either we'll dispatch a synthetic contextmenu on
190
+ // pointerup to open the menu at the release position, or we panned and
191
+ // don't want a menu at all).
179
192
  //
180
193
  // Other contextmenu sources must reach Radix so the menu opens:
181
- // - ctrl+click on macOS (button=0, or button=2 with ctrlKey=true)
182
194
  // - long-press on touch devices (button=0, pointerType=touch)
183
- if (e.button !== 2 || e.ctrlKey) return
195
+ if (!isSecondaryClickEvent(e)) return
184
196
  preventDefault(e)
185
197
  }
186
198
 
@@ -33,7 +33,6 @@ export function EditorComponentsProvider({
33
33
  InFrontOfTheCanvas: null,
34
34
  LoadingScreen: DefaultLoadingScreen,
35
35
  OnTheCanvas: null,
36
- SelectionBackground: null,
37
36
  ShapeWrapper: DefaultShapeWrapper,
38
37
  Spinner: DefaultSpinner,
39
38
  SvgDefs: DefaultSvgDefs,
@@ -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,76 @@
1
+ import { tlenv } from '../globals/environment'
2
+ import { TestEditor } from '../test/TestEditor'
3
+ import { getPointerInfo } from './getPointerInfo'
4
+ import { isDirectDisplayPen, 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('isDirectDisplayPen', () => {
25
+ const originalIsTouchDevice = tlenv.isTouchDevice
26
+
27
+ afterEach(() => {
28
+ tlenv.isTouchDevice = originalIsTouchDevice
29
+ })
30
+
31
+ function event(pointerType: string) {
32
+ return { pointerType, pointerId: 1 } as unknown as PointerEvent
33
+ }
34
+
35
+ it('treats a pen on a touch-capable device as a direct-display pen', () => {
36
+ tlenv.isTouchDevice = true
37
+ expect(isDirectDisplayPen(event('pen'))).toBe(true)
38
+ })
39
+
40
+ it('treats a pen on a non-touch device as indirect', () => {
41
+ tlenv.isTouchDevice = false
42
+ expect(isDirectDisplayPen(event('pen'))).toBe(false)
43
+ })
44
+
45
+ it('is never true for mouse or touch input, even on a touch-capable device', () => {
46
+ tlenv.isTouchDevice = true
47
+ expect(isDirectDisplayPen(event('mouse'))).toBe(false)
48
+ expect(isDirectDisplayPen(event('touch'))).toBe(false)
49
+ })
50
+ })
51
+
52
+ describe('ctrl + left-click on macOS fires as a right-click (regression)', () => {
53
+ // Regression test for https://github.com/tldraw/tldraw/issues/8217
54
+ // On macOS, a ctrl + left-click should be treated as a right-click
55
+ let editor: TestEditor
56
+
57
+ beforeEach(() => {
58
+ editor = new TestEditor()
59
+ })
60
+
61
+ afterEach(() => {
62
+ editor.dispose()
63
+ })
64
+
65
+ it('reports button 2 in the dispatched pointer info on macOS', () => {
66
+ tlenv.isDarwin = true
67
+ const event = new PointerEvent('pointerdown', { button: 0, ctrlKey: true })
68
+ expect(getPointerInfo(editor, event).button).toBe(2)
69
+ })
70
+
71
+ it('stays button 0 for the same gesture off macOS', () => {
72
+ tlenv.isDarwin = false
73
+ const event = new PointerEvent('pointerdown', { button: 0, ctrlKey: true })
74
+ expect(getPointerInfo(editor, event).button).toBe(0)
75
+ })
76
+ })
@@ -0,0 +1,42 @@
1
+ import type React from 'react'
2
+ import { tlenv } from '../globals/environment'
3
+
4
+ /**
5
+ * Decide whether a pen pointer event looks like direct manipulation on the display (e.g. Apple
6
+ * Pencil on an iPad or a Surface Pen on a touchscreen) rather than indirect input from a desktop
7
+ * graphics tablet (e.g. a Wacom Intuos).
8
+ *
9
+ * We can't tell the two apart from the pointer event itself: both report `pointerType: 'pen'`, and
10
+ * implicit pointer capture — which in theory distinguishes direct-manipulation pointers — isn't
11
+ * reliably observable across browsers (notably WebKit/iPad). Instead we key off the device: a
12
+ * direct-display pen draws on a touch-capable screen, while an indirect graphics tablet is used on
13
+ * a non-touch desktop alongside a mouse. A device with no touch input therefore can't host a
14
+ * direct-display pen.
15
+ *
16
+ * Note this uses {@link tlenv.isTouchDevice} — the device's fixed touch capability — not the
17
+ * editor's dynamic `isCoarsePointer` state, which a pen `pointerdown` flips to coarse regardless
18
+ * of device.
19
+ *
20
+ * @internal
21
+ */
22
+ export function isDirectDisplayPen(e: React.PointerEvent | PointerEvent): boolean {
23
+ if (e.pointerType !== 'pen') return false
24
+ return tlenv.isTouchDevice
25
+ }
26
+
27
+ /** @internal */
28
+ interface PointerLike {
29
+ button: number
30
+ ctrlKey: boolean
31
+ metaKey: boolean
32
+ }
33
+
34
+ /** @internal */
35
+ export function isSecondaryClickEvent(e: PointerLike) {
36
+ return e.button === 2 || (tlenv.isDarwin && e.button === 0 && e.ctrlKey && !e.metaKey)
37
+ }
38
+
39
+ /** @internal */
40
+ export function getPointerEventButton(e: PointerLike) {
41
+ return isSecondaryClickEvent(e) ? 2 : e.button
42
+ }
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.6421dbe4e25a'
4
+ export const version = '5.2.0-canary.667cbd6bb6ff'
5
5
  export const publishDates = {
6
6
  major: '2026-05-06T16:28:18.473Z',
7
- minor: '2026-06-16T10:18:19.799Z',
8
- patch: '2026-06-16T10:18:19.799Z',
7
+ minor: '2026-06-22T09:09:59.347Z',
8
+ patch: '2026-06-22T09:09:59.347Z',
9
9
  }
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var DefaultSelectionBackground_exports = {};
30
- __export(DefaultSelectionBackground_exports, {
31
- DefaultSelectionBackground: () => DefaultSelectionBackground
32
- });
33
- module.exports = __toCommonJS(DefaultSelectionBackground_exports);
34
- var import_jsx_runtime = require("react/jsx-runtime");
35
- var React = __toESM(require("react"), 1);
36
- var import_useTransform = require("../../hooks/useTransform");
37
- var import_utils = require("../../primitives/utils");
38
- function DefaultSelectionBackground({ bounds, rotation }) {
39
- const rDiv = React.useRef(null);
40
- (0, import_useTransform.useTransform)(rDiv, bounds.x, bounds.y, 1, rotation);
41
- React.useLayoutEffect(() => {
42
- const div = rDiv.current;
43
- if (!div) return;
44
- div.style.width = (0, import_utils.toDomPrecision)(Math.max(1, bounds.width)) + "px";
45
- div.style.height = (0, import_utils.toDomPrecision)(Math.max(1, bounds.height)) + "px";
46
- }, [bounds.width, bounds.height]);
47
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref: rDiv, className: "tl-selection__bg", draggable: false });
48
- }
49
- //# sourceMappingURL=DefaultSelectionBackground.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../src/lib/components/default-components/DefaultSelectionBackground.tsx"],
4
- "sourcesContent": ["import * as React from 'react'\nimport { useTransform } from '../../hooks/useTransform'\nimport { Box } from '../../primitives/Box'\nimport { toDomPrecision } from '../../primitives/utils'\n\n/** @public */\nexport interface TLSelectionBackgroundProps {\n\tbounds: Box\n\trotation: number\n}\n\n/** @public @react */\nexport function DefaultSelectionBackground({ bounds, rotation }: TLSelectionBackgroundProps) {\n\tconst rDiv = React.useRef<HTMLDivElement>(null)\n\tuseTransform(rDiv, bounds.x, bounds.y, 1, rotation)\n\n\tReact.useLayoutEffect(() => {\n\t\tconst div = rDiv.current\n\t\tif (!div) return\n\t\tdiv.style.width = toDomPrecision(Math.max(1, bounds.width)) + 'px'\n\t\tdiv.style.height = toDomPrecision(Math.max(1, bounds.height)) + 'px'\n\t}, [bounds.width, bounds.height])\n\n\treturn <div ref={rDiv} className=\"tl-selection__bg\" draggable={false} />\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBQ;AAvBR,YAAuB;AACvB,0BAA6B;AAE7B,mBAA+B;AASxB,SAAS,2BAA2B,EAAE,QAAQ,SAAS,GAA+B;AAC5F,QAAM,OAAO,MAAM,OAAuB,IAAI;AAC9C,wCAAa,MAAM,OAAO,GAAG,OAAO,GAAG,GAAG,QAAQ;AAElD,QAAM,gBAAgB,MAAM;AAC3B,UAAM,MAAM,KAAK;AACjB,QAAI,CAAC,IAAK;AACV,QAAI,MAAM,YAAQ,6BAAe,KAAK,IAAI,GAAG,OAAO,KAAK,CAAC,IAAI;AAC9D,QAAI,MAAM,aAAS,6BAAe,KAAK,IAAI,GAAG,OAAO,MAAM,CAAC,IAAI;AAAA,EACjE,GAAG,CAAC,OAAO,OAAO,OAAO,MAAM,CAAC;AAEhC,SAAO,4CAAC,SAAI,KAAK,MAAM,WAAU,oBAAmB,WAAW,OAAO;AACvE;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/lib/hooks/useTransform.ts"],
4
- "sourcesContent": ["import { useLayoutEffect } from 'react'\nimport { VecLike } from '../primitives/Vec'\n\n/** @public */\nexport function useTransform(\n\tref: React.RefObject<HTMLElement | SVGElement | null>,\n\tx?: number,\n\ty?: number,\n\tscale?: number,\n\trotate?: number,\n\tadditionalOffset?: VecLike\n) {\n\tuseLayoutEffect(() => {\n\t\tconst elm = ref.current\n\t\tif (!elm) return\n\t\tif (x === undefined) return\n\n\t\tlet trans = `translate(${x}px, ${y}px)`\n\t\tif (scale !== undefined) {\n\t\t\ttrans += ` scale(${scale})`\n\t\t}\n\t\tif (rotate !== undefined) {\n\t\t\ttrans += ` rotate(${rotate}rad)`\n\t\t}\n\t\tif (additionalOffset) {\n\t\t\ttrans += ` translate(${additionalOffset.x}px, ${additionalOffset.y}px)`\n\t\t}\n\t\telm.style.transform = trans\n\t})\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgC;AAIzB,SAAS,aACf,KACA,GACA,GACA,OACA,QACA,kBACC;AACD,oCAAgB,MAAM;AACrB,UAAM,MAAM,IAAI;AAChB,QAAI,CAAC,IAAK;AACV,QAAI,MAAM,OAAW;AAErB,QAAI,QAAQ,aAAa,CAAC,OAAO,CAAC;AAClC,QAAI,UAAU,QAAW;AACxB,eAAS,UAAU,KAAK;AAAA,IACzB;AACA,QAAI,WAAW,QAAW;AACzB,eAAS,WAAW,MAAM;AAAA,IAC3B;AACA,QAAI,kBAAkB;AACrB,eAAS,cAAc,iBAAiB,CAAC,OAAO,iBAAiB,CAAC;AAAA,IACnE;AACA,QAAI,MAAM,YAAY;AAAA,EACvB,CAAC;AACF;",
6
- "names": []
7
- }
@@ -1,19 +0,0 @@
1
- import { jsx } from "react/jsx-runtime";
2
- import * as React from "react";
3
- import { useTransform } from "../../hooks/useTransform.mjs";
4
- import { toDomPrecision } from "../../primitives/utils.mjs";
5
- function DefaultSelectionBackground({ bounds, rotation }) {
6
- const rDiv = React.useRef(null);
7
- useTransform(rDiv, bounds.x, bounds.y, 1, rotation);
8
- React.useLayoutEffect(() => {
9
- const div = rDiv.current;
10
- if (!div) return;
11
- div.style.width = toDomPrecision(Math.max(1, bounds.width)) + "px";
12
- div.style.height = toDomPrecision(Math.max(1, bounds.height)) + "px";
13
- }, [bounds.width, bounds.height]);
14
- return /* @__PURE__ */ jsx("div", { ref: rDiv, className: "tl-selection__bg", draggable: false });
15
- }
16
- export {
17
- DefaultSelectionBackground
18
- };
19
- //# sourceMappingURL=DefaultSelectionBackground.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../src/lib/components/default-components/DefaultSelectionBackground.tsx"],
4
- "sourcesContent": ["import * as React from 'react'\nimport { useTransform } from '../../hooks/useTransform'\nimport { Box } from '../../primitives/Box'\nimport { toDomPrecision } from '../../primitives/utils'\n\n/** @public */\nexport interface TLSelectionBackgroundProps {\n\tbounds: Box\n\trotation: number\n}\n\n/** @public @react */\nexport function DefaultSelectionBackground({ bounds, rotation }: TLSelectionBackgroundProps) {\n\tconst rDiv = React.useRef<HTMLDivElement>(null)\n\tuseTransform(rDiv, bounds.x, bounds.y, 1, rotation)\n\n\tReact.useLayoutEffect(() => {\n\t\tconst div = rDiv.current\n\t\tif (!div) return\n\t\tdiv.style.width = toDomPrecision(Math.max(1, bounds.width)) + 'px'\n\t\tdiv.style.height = toDomPrecision(Math.max(1, bounds.height)) + 'px'\n\t}, [bounds.width, bounds.height])\n\n\treturn <div ref={rDiv} className=\"tl-selection__bg\" draggable={false} />\n}\n"],
5
- "mappings": "AAuBQ;AAvBR,YAAY,WAAW;AACvB,SAAS,oBAAoB;AAE7B,SAAS,sBAAsB;AASxB,SAAS,2BAA2B,EAAE,QAAQ,SAAS,GAA+B;AAC5F,QAAM,OAAO,MAAM,OAAuB,IAAI;AAC9C,eAAa,MAAM,OAAO,GAAG,OAAO,GAAG,GAAG,QAAQ;AAElD,QAAM,gBAAgB,MAAM;AAC3B,UAAM,MAAM,KAAK;AACjB,QAAI,CAAC,IAAK;AACV,QAAI,MAAM,QAAQ,eAAe,KAAK,IAAI,GAAG,OAAO,KAAK,CAAC,IAAI;AAC9D,QAAI,MAAM,SAAS,eAAe,KAAK,IAAI,GAAG,OAAO,MAAM,CAAC,IAAI;AAAA,EACjE,GAAG,CAAC,OAAO,OAAO,OAAO,MAAM,CAAC;AAEhC,SAAO,oBAAC,SAAI,KAAK,MAAM,WAAU,oBAAmB,WAAW,OAAO;AACvE;",
6
- "names": []
7
- }