@tldraw/editor 3.12.0-canary.608410b0faeb → 3.12.0-canary.71368dc000db

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 (50) hide show
  1. package/dist-cjs/index.d.ts +45 -5
  2. package/dist-cjs/index.js +1 -1
  3. package/dist-cjs/lib/TldrawEditor.js +4 -0
  4. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  5. package/dist-cjs/lib/components/Shape.js +8 -12
  6. package/dist-cjs/lib/components/Shape.js.map +2 -2
  7. package/dist-cjs/lib/editor/Editor.js +31 -20
  8. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  9. package/dist-cjs/lib/editor/derivations/notVisibleShapes.js +1 -1
  10. package/dist-cjs/lib/editor/derivations/notVisibleShapes.js.map +2 -2
  11. package/dist-cjs/lib/editor/tools/StateNode.js +4 -1
  12. package/dist-cjs/lib/editor/tools/StateNode.js.map +2 -2
  13. package/dist-cjs/lib/hooks/useCanvasEvents.js +12 -7
  14. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +3 -3
  15. package/dist-cjs/lib/hooks/useGestureEvents.js +12 -6
  16. package/dist-cjs/lib/hooks/useGestureEvents.js.map +2 -2
  17. package/dist-cjs/lib/utils/debug-flags.js +2 -1
  18. package/dist-cjs/lib/utils/debug-flags.js.map +2 -2
  19. package/dist-cjs/version.js +3 -3
  20. package/dist-cjs/version.js.map +1 -1
  21. package/dist-esm/index.d.mts +45 -5
  22. package/dist-esm/index.mjs +1 -1
  23. package/dist-esm/lib/TldrawEditor.mjs +4 -0
  24. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  25. package/dist-esm/lib/components/Shape.mjs +9 -13
  26. package/dist-esm/lib/components/Shape.mjs.map +2 -2
  27. package/dist-esm/lib/editor/Editor.mjs +31 -20
  28. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  29. package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs +1 -1
  30. package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs.map +2 -2
  31. package/dist-esm/lib/editor/tools/StateNode.mjs +4 -1
  32. package/dist-esm/lib/editor/tools/StateNode.mjs.map +2 -2
  33. package/dist-esm/lib/hooks/useCanvasEvents.mjs +12 -7
  34. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +3 -3
  35. package/dist-esm/lib/hooks/useGestureEvents.mjs +12 -6
  36. package/dist-esm/lib/hooks/useGestureEvents.mjs.map +2 -2
  37. package/dist-esm/lib/utils/debug-flags.mjs +2 -1
  38. package/dist-esm/lib/utils/debug-flags.mjs.map +2 -2
  39. package/dist-esm/version.mjs +3 -3
  40. package/dist-esm/version.mjs.map +1 -1
  41. package/package.json +7 -7
  42. package/src/lib/TldrawEditor.tsx +29 -3
  43. package/src/lib/components/Shape.tsx +13 -17
  44. package/src/lib/editor/Editor.ts +59 -21
  45. package/src/lib/editor/derivations/notVisibleShapes.ts +1 -1
  46. package/src/lib/editor/tools/StateNode.ts +6 -1
  47. package/src/lib/hooks/useCanvasEvents.ts +14 -7
  48. package/src/lib/hooks/useGestureEvents.ts +12 -6
  49. package/src/lib/utils/debug-flags.ts +1 -0
  50. package/src/version.ts +3 -3
@@ -241,10 +241,33 @@ export interface TLEditorOptions {
241
241
  fontAssetUrls?: { [key: string]: string | undefined }
242
242
  /**
243
243
  * A predicate that should return true if the given shape should be hidden.
244
+ *
245
+ * @deprecated Use {@link Editor#getShapeVisibility} instead.
246
+ *
244
247
  * @param shape - The shape to check.
245
248
  * @param editor - The editor instance.
246
249
  */
247
250
  isShapeHidden?(shape: TLShape, editor: Editor): boolean
251
+
252
+ /**
253
+ * Provides a way to hide shapes.
254
+ *
255
+ * @example
256
+ * ```ts
257
+ * getShapeVisibility={(shape, editor) => shape.meta.hidden ? 'hidden' : 'inherit'}
258
+ * ```
259
+ *
260
+ * - `'inherit' | undefined` - (default) The shape will be visible unless its parent is hidden.
261
+ * - `'hidden'` - The shape will be hidden.
262
+ * - `'visible'` - The shape will be visible.
263
+ *
264
+ * @param shape - The shape to check.
265
+ * @param editor - The editor instance.
266
+ */
267
+ getShapeVisibility?(
268
+ shape: TLShape,
269
+ editor: Editor
270
+ ): 'visible' | 'hidden' | 'inherit' | null | undefined
248
271
  }
249
272
 
250
273
  /**
@@ -281,12 +304,21 @@ export class Editor extends EventEmitter<TLEventMap> {
281
304
  autoFocus,
282
305
  inferDarkMode,
283
306
  options,
307
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
284
308
  isShapeHidden,
309
+ getShapeVisibility,
285
310
  fontAssetUrls,
286
311
  }: TLEditorOptions) {
287
312
  super()
313
+ assert(
314
+ !(isShapeHidden && getShapeVisibility),
315
+ 'Cannot use both isShapeHidden and getShapeVisibility'
316
+ )
288
317
 
289
- this._isShapeHiddenPredicate = isShapeHidden
318
+ this._getShapeVisibility = isShapeHidden
319
+ ? // eslint-disable-next-line @typescript-eslint/no-deprecated
320
+ (shape: TLShape, editor: Editor) => (isShapeHidden(shape, editor) ? 'hidden' : 'inherit')
321
+ : getShapeVisibility
290
322
 
291
323
  this.options = { ...defaultTldrawOptions, ...options }
292
324
 
@@ -773,18 +805,22 @@ export class Editor extends EventEmitter<TLEventMap> {
773
805
  }
774
806
  }
775
807
 
776
- private readonly _isShapeHiddenPredicate?: (shape: TLShape, editor: Editor) => boolean
808
+ private readonly _getShapeVisibility?: TLEditorOptions['getShapeVisibility']
777
809
  @computed
778
810
  private getIsShapeHiddenCache() {
779
- if (!this._isShapeHiddenPredicate) return null
811
+ if (!this._getShapeVisibility) return null
780
812
  return this.store.createComputedCache<boolean, TLShape>('isShapeHidden', (shape: TLShape) => {
781
- const hiddenParent = this.findShapeAncestor(shape, (p) => this.isShapeHidden(p))
782
- if (hiddenParent) return true
783
- return this._isShapeHiddenPredicate!(shape, this) ?? false
813
+ const visibility = this._getShapeVisibility!(shape, this)
814
+ const isParentHidden = PageRecordType.isId(shape.parentId)
815
+ ? false
816
+ : this.isShapeHidden(shape.parentId)
817
+
818
+ if (isParentHidden) return visibility !== 'visible'
819
+ return visibility === 'hidden'
784
820
  })
785
821
  }
786
822
  isShapeHidden(shapeOrId: TLShape | TLShapeId): boolean {
787
- if (!this._isShapeHiddenPredicate) return false
823
+ if (!this._getShapeVisibility) return false
788
824
  return !!this.getIsShapeHiddenCache!()!.get(
789
825
  typeof shapeOrId === 'string' ? shapeOrId : shapeOrId.id
790
826
  )
@@ -1216,7 +1252,6 @@ export class Editor extends EventEmitter<TLEventMap> {
1216
1252
  run(fn: () => void, opts?: TLEditorRunOptions): this {
1217
1253
  const previousIgnoreShapeLock = this._shouldIgnoreShapeLock
1218
1254
  this._shouldIgnoreShapeLock = opts?.ignoreShapeLock ?? previousIgnoreShapeLock
1219
-
1220
1255
  try {
1221
1256
  this.history.batch(fn, opts)
1222
1257
  } finally {
@@ -3712,7 +3747,15 @@ export class Editor extends EventEmitter<TLEventMap> {
3712
3747
  const addShapeById = (id: TLShapeId, opacity: number, isAncestorErasing: boolean) => {
3713
3748
  const shape = this.getShape(id)
3714
3749
  if (!shape) return
3715
- if (this.isShapeHidden(shape)) return
3750
+
3751
+ if (this.isShapeHidden(shape)) {
3752
+ // process children just in case they are overriding the hidden state
3753
+ const isErasing = isAncestorErasing || erasingShapeIds.includes(id)
3754
+ for (const childId of this.getSortedChildIdsForParent(id)) {
3755
+ addShapeById(childId, opacity, isErasing)
3756
+ }
3757
+ return
3758
+ }
3716
3759
 
3717
3760
  opacity *= shape.opacity
3718
3761
  let isShapeErasing = false
@@ -9859,12 +9902,12 @@ export class Editor extends EventEmitter<TLEventMap> {
9859
9902
 
9860
9903
  const { x: cx, y: cy, z: cz } = unsafe__withoutCapture(() => this.getCamera())
9861
9904
 
9862
- const { panSpeed, zoomSpeed } = cameraOptions
9905
+ const { panSpeed } = cameraOptions
9863
9906
  this._setCamera(
9864
9907
  new Vec(
9865
- cx + (dx * panSpeed) / cz - x / cz + x / (z * zoomSpeed),
9866
- cy + (dy * panSpeed) / cz - y / cz + y / (z * zoomSpeed),
9867
- z * zoomSpeed
9908
+ cx + (dx * panSpeed) / cz - x / cz + x / z,
9909
+ cy + (dy * panSpeed) / cz - y / cz + y / z,
9910
+ z
9868
9911
  ),
9869
9912
  { immediate: true }
9870
9913
  )
@@ -9939,14 +9982,9 @@ export class Editor extends EventEmitter<TLEventMap> {
9939
9982
  }
9940
9983
 
9941
9984
  const zoom = cz + (delta ?? 0) * zoomSpeed * cz
9942
- this._setCamera(
9943
- new Vec(
9944
- cx + (x / zoom - x) - (x / cz - x),
9945
- cy + (y / zoom - y) - (y / cz - y),
9946
- zoom
9947
- ),
9948
- { immediate: true }
9949
- )
9985
+ this._setCamera(new Vec(cx + x / zoom - x / cz, cy + y / zoom - y / cz, zoom), {
9986
+ immediate: true,
9987
+ })
9950
9988
  this.maybeTrackPerformance('Zooming')
9951
9989
  return
9952
9990
  }
@@ -31,7 +31,7 @@ export const notVisibleShapes = (editor: Editor) => {
31
31
  })
32
32
  return notVisibleShapes
33
33
  }
34
- return computed<Set<TLShapeId>>('getCulledShapes', (prevValue) => {
34
+ return computed<Set<TLShapeId>>('notVisibleShapes', (prevValue) => {
35
35
  if (isUninitialized(prevValue)) {
36
36
  return fromScratch(editor)
37
37
  }
@@ -38,6 +38,7 @@ export interface TLStateNodeConstructor {
38
38
  initial?: string
39
39
  children?(): TLStateNodeConstructor[]
40
40
  isLockable: boolean
41
+ useCoalescedEvents: boolean
41
42
  }
42
43
 
43
44
  /** @public */
@@ -47,7 +48,8 @@ export abstract class StateNode implements Partial<TLEventHandlers> {
47
48
  public editor: Editor,
48
49
  parent?: StateNode
49
50
  ) {
50
- const { id, children, initial, isLockable } = this.constructor as TLStateNodeConstructor
51
+ const { id, children, initial, isLockable, useCoalescedEvents } = this
52
+ .constructor as TLStateNodeConstructor
51
53
 
52
54
  this.id = id
53
55
  this._isActive = atom<boolean>('toolIsActive' + this.id, false)
@@ -83,6 +85,7 @@ export abstract class StateNode implements Partial<TLEventHandlers> {
83
85
  }
84
86
  }
85
87
  this.isLockable = isLockable
88
+ this.useCoalescedEvents = useCoalescedEvents
86
89
  this.performanceTracker = new PerformanceTracker()
87
90
  }
88
91
 
@@ -90,6 +93,7 @@ export abstract class StateNode implements Partial<TLEventHandlers> {
90
93
  static initial?: string
91
94
  static children?: () => TLStateNodeConstructor[]
92
95
  static isLockable = true
96
+ static useCoalescedEvents = false
93
97
 
94
98
  id: string
95
99
  type: 'branch' | 'leaf' | 'root'
@@ -97,6 +101,7 @@ export abstract class StateNode implements Partial<TLEventHandlers> {
97
101
  initial?: string
98
102
  children?: Record<string, StateNode>
99
103
  isLockable: boolean
104
+ useCoalescedEvents: boolean
100
105
  parent: StateNode
101
106
 
102
107
  /**
@@ -1,3 +1,4 @@
1
+ import { useValue } from '@tldraw/state-react'
1
2
  import React, { useMemo } from 'react'
2
3
  import { RIGHT_MOUSE_BUTTON } from '../constants'
3
4
  import {
@@ -11,6 +12,7 @@ import { useEditor } from './useEditor'
11
12
 
12
13
  export function useCanvasEvents() {
13
14
  const editor = useEditor()
15
+ const currentTool = useValue('current tool', () => editor.getCurrentTool(), [editor])
14
16
 
15
17
  const events = useMemo(
16
18
  function canvasEvents() {
@@ -49,12 +51,17 @@ export function useCanvasEvents() {
49
51
  lastX = e.clientX
50
52
  lastY = e.clientY
51
53
 
52
- editor.dispatch({
53
- type: 'pointer',
54
- target: 'canvas',
55
- name: 'pointer_move',
56
- ...getPointerInfo(e),
57
- })
54
+ // For tools that benefit from a higher fidelity of events,
55
+ // we dispatch the coalesced events.
56
+ const events = currentTool.useCoalescedEvents ? e.nativeEvent.getCoalescedEvents() : [e]
57
+ for (const singleEvent of events) {
58
+ editor.dispatch({
59
+ type: 'pointer',
60
+ target: 'canvas',
61
+ name: 'pointer_move',
62
+ ...getPointerInfo(singleEvent),
63
+ })
64
+ }
58
65
  }
59
66
 
60
67
  function onPointerUp(e: React.PointerEvent) {
@@ -159,7 +166,7 @@ export function useCanvasEvents() {
159
166
  onClick,
160
167
  }
161
168
  },
162
- [editor]
169
+ [editor, currentTool]
163
170
  )
164
171
 
165
172
  return events
@@ -135,7 +135,6 @@ export function useGestureEvents(ref: React.RefObject<HTMLDivElement>) {
135
135
 
136
136
  let initDistanceBetweenFingers = 1 // the distance between the two fingers when the pinch starts
137
137
  let initZoom = 1 // the browser's zoom level when the pinch starts
138
- let currZoom = 1 // the current zoom level according to the pinch gesture recognizer
139
138
  let currDistanceBetweenFingers = 0
140
139
  const initPointBetweenFingers = new Vec()
141
140
  const prevPointBetweenFingers = new Vec()
@@ -239,7 +238,7 @@ export function useGestureEvents(ref: React.RefObject<HTMLDivElement>) {
239
238
 
240
239
  switch (pinchState) {
241
240
  case 'zooming': {
242
- currZoom = offset[0]
241
+ const currZoom = offset[0] ** editor.getCameraOptions().zoomSpeed
243
242
 
244
243
  editor.dispatch({
245
244
  type: 'pinch',
@@ -278,7 +277,7 @@ export function useGestureEvents(ref: React.RefObject<HTMLDivElement>) {
278
277
  if (event instanceof WheelEvent) return
279
278
  if (!(event.target === elm || elm?.contains(event.target as Node))) return
280
279
 
281
- const scale = offset[0]
280
+ const scale = offset[0] ** editor.getCameraOptions().zoomSpeed
282
281
 
283
282
  pinchState = 'not sure'
284
283
 
@@ -309,14 +308,21 @@ export function useGestureEvents(ref: React.RefObject<HTMLDivElement>) {
309
308
  target: ref,
310
309
  eventOptions: { passive: false },
311
310
  pinch: {
312
- from: () => [editor.getZoomLevel(), 0], // Return the camera z to use when pinch starts
311
+ from: () => {
312
+ const { zoomSpeed } = editor.getCameraOptions()
313
+ const level = editor.getZoomLevel() ** (1 / zoomSpeed)
314
+ return [level, 0]
315
+ }, // Return the camera z to use when pinch starts
313
316
  scaleBounds: () => {
314
317
  const baseZoom = editor.getBaseZoom()
315
- const zoomSteps = editor.getCameraOptions().zoomSteps
318
+ const { zoomSteps, zoomSpeed } = editor.getCameraOptions()
316
319
  const zoomMin = zoomSteps[0] * baseZoom
317
320
  const zoomMax = zoomSteps[zoomSteps.length - 1] * baseZoom
318
321
 
319
- return { from: editor.getZoomLevel(), max: zoomMax, min: zoomMin }
322
+ return {
323
+ max: zoomMax ** (1 / zoomSpeed),
324
+ min: zoomMin ** (1 / zoomSpeed),
325
+ }
320
326
  },
321
327
  },
322
328
  })
@@ -53,6 +53,7 @@ export const debugFlags = {
53
53
  debugGeometry: createDebugValue('debugGeometry', { defaults: { all: false } }),
54
54
  hideShapes: createDebugValue('hideShapes', { defaults: { all: false } }),
55
55
  editOnType: createDebugValue('editOnType', { defaults: { all: false } }),
56
+ a11y: createDebugValue('a11y', { defaults: { all: false } }),
56
57
  } as const
57
58
 
58
59
  declare global {
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 = '3.12.0-canary.608410b0faeb'
4
+ export const version = '3.12.0-canary.71368dc000db'
5
5
  export const publishDates = {
6
6
  major: '2024-09-13T14:36:29.063Z',
7
- minor: '2025-03-25T14:11:33.155Z',
8
- patch: '2025-03-25T14:11:33.155Z',
7
+ minor: '2025-04-03T09:35:34.669Z',
8
+ patch: '2025-04-03T09:35:34.669Z',
9
9
  }