@tldraw/editor 3.16.0-canary.ca347c5375a5 → 3.16.0-canary.dfdf6b7de8c2

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 (38) hide show
  1. package/dist-cjs/index.d.ts +20 -0
  2. package/dist-cjs/index.js +3 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +3 -1
  5. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  6. package/dist-cjs/lib/components/MenuClickCapture.js +0 -5
  7. package/dist-cjs/lib/components/MenuClickCapture.js.map +2 -2
  8. package/dist-cjs/lib/hooks/useCanvasEvents.js +24 -20
  9. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  10. package/dist-cjs/lib/hooks/useStateAttribute.js +35 -0
  11. package/dist-cjs/lib/hooks/useStateAttribute.js.map +7 -0
  12. package/dist-cjs/lib/utils/EditorAtom.js +45 -0
  13. package/dist-cjs/lib/utils/EditorAtom.js.map +7 -0
  14. package/dist-cjs/version.js +3 -3
  15. package/dist-cjs/version.js.map +1 -1
  16. package/dist-esm/index.d.mts +20 -0
  17. package/dist-esm/index.mjs +3 -1
  18. package/dist-esm/index.mjs.map +2 -2
  19. package/dist-esm/lib/TldrawEditor.mjs +3 -1
  20. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  21. package/dist-esm/lib/components/MenuClickCapture.mjs +0 -5
  22. package/dist-esm/lib/components/MenuClickCapture.mjs.map +2 -2
  23. package/dist-esm/lib/hooks/useCanvasEvents.mjs +25 -21
  24. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  25. package/dist-esm/lib/hooks/useStateAttribute.mjs +15 -0
  26. package/dist-esm/lib/hooks/useStateAttribute.mjs.map +7 -0
  27. package/dist-esm/lib/utils/EditorAtom.mjs +25 -0
  28. package/dist-esm/lib/utils/EditorAtom.mjs.map +7 -0
  29. package/dist-esm/version.mjs +3 -3
  30. package/dist-esm/version.mjs.map +1 -1
  31. package/package.json +7 -7
  32. package/src/index.ts +1 -0
  33. package/src/lib/TldrawEditor.tsx +7 -5
  34. package/src/lib/components/MenuClickCapture.tsx +0 -8
  35. package/src/lib/hooks/useCanvasEvents.ts +39 -32
  36. package/src/lib/hooks/useStateAttribute.ts +15 -0
  37. package/src/lib/utils/EditorAtom.ts +37 -0
  38. package/src/version.ts +3 -3
@@ -0,0 +1,25 @@
1
+ import { atom } from "@tldraw/state";
2
+ import { WeakCache } from "@tldraw/utils";
3
+ class EditorAtom {
4
+ constructor(name, getInitialState) {
5
+ this.name = name;
6
+ this.getInitialState = getInitialState;
7
+ }
8
+ states = new WeakCache();
9
+ getAtom(editor) {
10
+ return this.states.get(editor, () => atom(this.name, this.getInitialState(editor)));
11
+ }
12
+ get(editor) {
13
+ return this.getAtom(editor).get();
14
+ }
15
+ update(editor, update) {
16
+ return this.getAtom(editor).update(update);
17
+ }
18
+ set(editor, state) {
19
+ return this.getAtom(editor).set(state);
20
+ }
21
+ }
22
+ export {
23
+ EditorAtom
24
+ };
25
+ //# sourceMappingURL=EditorAtom.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/lib/utils/EditorAtom.ts"],
4
+ "sourcesContent": ["import { atom, Atom } from '@tldraw/state'\nimport { WeakCache } from '@tldraw/utils'\nimport { Editor } from '../editor/Editor'\n\n/**\n * An Atom that is scoped to the lifetime of an Editor.\n *\n * This is useful for storing UI state for tldraw applications. Keeping state scoped to an editor\n * instead of stored in a global atom can prevent issues with state being shared between editors\n * when navigating between pages, or when multiple editor instances are used on the same page.\n *\n * @public\n */\nexport class EditorAtom<T> {\n\tprivate states = new WeakCache<Editor, Atom<T>>()\n\n\tconstructor(\n\t\tprivate name: string,\n\t\tprivate getInitialState: (editor: Editor) => T\n\t) {}\n\n\tgetAtom(editor: Editor): Atom<T> {\n\t\treturn this.states.get(editor, () => atom(this.name, this.getInitialState(editor)))\n\t}\n\n\tget(editor: Editor): T {\n\t\treturn this.getAtom(editor).get()\n\t}\n\n\tupdate(editor: Editor, update: (state: T) => T): T {\n\t\treturn this.getAtom(editor).update(update)\n\t}\n\n\tset(editor: Editor, state: T): T {\n\t\treturn this.getAtom(editor).set(state)\n\t}\n}\n"],
5
+ "mappings": "AAAA,SAAS,YAAkB;AAC3B,SAAS,iBAAiB;AAYnB,MAAM,WAAc;AAAA,EAG1B,YACS,MACA,iBACP;AAFO;AACA;AAAA,EACN;AAAA,EALK,SAAS,IAAI,UAA2B;AAAA,EAOhD,QAAQ,QAAyB;AAChC,WAAO,KAAK,OAAO,IAAI,QAAQ,MAAM,KAAK,KAAK,MAAM,KAAK,gBAAgB,MAAM,CAAC,CAAC;AAAA,EACnF;AAAA,EAEA,IAAI,QAAmB;AACtB,WAAO,KAAK,QAAQ,MAAM,EAAE,IAAI;AAAA,EACjC;AAAA,EAEA,OAAO,QAAgB,QAA4B;AAClD,WAAO,KAAK,QAAQ,MAAM,EAAE,OAAO,MAAM;AAAA,EAC1C;AAAA,EAEA,IAAI,QAAgB,OAAa;AAChC,WAAO,KAAK,QAAQ,MAAM,EAAE,IAAI,KAAK;AAAA,EACtC;AACD;",
6
+ "names": []
7
+ }
@@ -1,8 +1,8 @@
1
- const version = "3.16.0-canary.ca347c5375a5";
1
+ const version = "3.16.0-canary.dfdf6b7de8c2";
2
2
  const publishDates = {
3
3
  major: "2024-09-13T14:36:29.063Z",
4
- minor: "2025-07-30T14:49:37.329Z",
5
- patch: "2025-07-30T14:49:37.329Z"
4
+ minor: "2025-08-04T10:21:53.495Z",
5
+ patch: "2025-08-04T10:21:53.495Z"
6
6
  };
7
7
  export {
8
8
  publishDates,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/version.ts"],
4
- "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '3.16.0-canary.ca347c5375a5'\nexport const publishDates = {\n\tmajor: '2024-09-13T14:36:29.063Z',\n\tminor: '2025-07-30T14:49:37.329Z',\n\tpatch: '2025-07-30T14:49:37.329Z',\n}\n"],
4
+ "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '3.16.0-canary.dfdf6b7de8c2'\nexport const publishDates = {\n\tmajor: '2024-09-13T14:36:29.063Z',\n\tminor: '2025-08-04T10:21:53.495Z',\n\tpatch: '2025-08-04T10:21:53.495Z',\n}\n"],
5
5
  "mappings": "AAGO,MAAM,UAAU;AAChB,MAAM,eAAe;AAAA,EAC3B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AACR;",
6
6
  "names": []
7
7
  }
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": "3.16.0-canary.ca347c5375a5",
4
+ "version": "3.16.0-canary.dfdf6b7de8c2",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -49,12 +49,12 @@
49
49
  "@tiptap/core": "^2.9.1",
50
50
  "@tiptap/pm": "^2.9.1",
51
51
  "@tiptap/react": "^2.9.1",
52
- "@tldraw/state": "3.16.0-canary.ca347c5375a5",
53
- "@tldraw/state-react": "3.16.0-canary.ca347c5375a5",
54
- "@tldraw/store": "3.16.0-canary.ca347c5375a5",
55
- "@tldraw/tlschema": "3.16.0-canary.ca347c5375a5",
56
- "@tldraw/utils": "3.16.0-canary.ca347c5375a5",
57
- "@tldraw/validate": "3.16.0-canary.ca347c5375a5",
52
+ "@tldraw/state": "3.16.0-canary.dfdf6b7de8c2",
53
+ "@tldraw/state-react": "3.16.0-canary.dfdf6b7de8c2",
54
+ "@tldraw/store": "3.16.0-canary.dfdf6b7de8c2",
55
+ "@tldraw/tlschema": "3.16.0-canary.dfdf6b7de8c2",
56
+ "@tldraw/utils": "3.16.0-canary.dfdf6b7de8c2",
57
+ "@tldraw/validate": "3.16.0-canary.dfdf6b7de8c2",
58
58
  "@types/core-js": "^2.5.8",
59
59
  "@use-gesture/react": "^10.3.1",
60
60
  "classnames": "^2.5.1",
package/src/index.ts CHANGED
@@ -450,6 +450,7 @@ export {
450
450
  setPointerCapture,
451
451
  stopEventPropagation,
452
452
  } from './lib/utils/dom'
453
+ export { EditorAtom } from './lib/utils/EditorAtom'
453
454
  export { getIncrementedName } from './lib/utils/getIncrementedName'
454
455
  export { getPointerInfo } from './lib/utils/getPointerInfo'
455
456
  export { getSvgPathFromPoints } from './lib/utils/getSvgPathFromPoints'
@@ -1,9 +1,9 @@
1
1
  import { MigrationSequence, Store } from '@tldraw/store'
2
2
  import { TLShape, TLStore, TLStoreSnapshot } from '@tldraw/tlschema'
3
- import { Required, annotateError } from '@tldraw/utils'
3
+ import { annotateError, Required } from '@tldraw/utils'
4
4
  import React, {
5
- ReactNode,
6
5
  memo,
6
+ ReactNode,
7
7
  useCallback,
8
8
  useEffect,
9
9
  useLayoutEffect,
@@ -15,13 +15,13 @@ import React, {
15
15
 
16
16
  import classNames from 'classnames'
17
17
  import { version } from '../version'
18
- import { OptionalErrorBoundary } from './components/ErrorBoundary'
19
18
  import { DefaultErrorFallback } from './components/default-components/DefaultErrorFallback'
20
- import { TLEditorSnapshot } from './config/TLEditorSnapshot'
19
+ import { OptionalErrorBoundary } from './components/ErrorBoundary'
21
20
  import { TLStoreBaseOptions } from './config/createTLStore'
22
- import { TLUser, createTLUser } from './config/createTLUser'
21
+ import { createTLUser, TLUser } from './config/createTLUser'
23
22
  import { TLAnyBindingUtilConstructor } from './config/defaultBindings'
24
23
  import { TLAnyShapeUtilConstructor } from './config/defaultShapes'
24
+ import { TLEditorSnapshot } from './config/TLEditorSnapshot'
25
25
  import { Editor } from './editor/Editor'
26
26
  import { TLStateNodeConstructor } from './editor/tools/StateNode'
27
27
  import { TLCameraOptions } from './editor/types/misc-types'
@@ -39,6 +39,7 @@ import { useForceUpdate } from './hooks/useForceUpdate'
39
39
  import { useShallowObjectIdentity } from './hooks/useIdentity'
40
40
  import { useLocalStore } from './hooks/useLocalStore'
41
41
  import { useRefState } from './hooks/useRefState'
42
+ import { useStateAttribute } from './hooks/useStateAttribute'
42
43
  import { useZoomCss } from './hooks/useZoomCss'
43
44
  import { LicenseProvider } from './license/LicenseProvider'
44
45
  import { Watermark } from './license/Watermark'
@@ -646,6 +647,7 @@ function Layout({ children, onMount }: { children: ReactNode; onMount?: TLOnMoun
646
647
  useCursor()
647
648
  useDarkMode()
648
649
  useForceUpdate()
650
+ useStateAttribute()
649
651
  useOnMount((editor) => {
650
652
  const teardownStore = editor.store.props.onMount(editor)
651
653
  const teardownCallback = onMount?.(editor)
@@ -50,12 +50,6 @@ export function MenuClickCapture() {
50
50
  // Do nothing unless we're pointing
51
51
  if (!rPointerState.current.isDown) return
52
52
 
53
- // If we're already dragging, pass on the event as it is
54
- if (rPointerState.current.isDragging) {
55
- canvasEvents.onPointerMove?.(e)
56
- return
57
- }
58
-
59
53
  if (
60
54
  // We're pointing, but are we dragging?
61
55
  Vec.Dist2(rPointerState.current.start, new Vec(e.clientX, e.clientY)) >
@@ -75,8 +69,6 @@ export function MenuClickCapture() {
75
69
  clientY: y,
76
70
  button: 0,
77
71
  })
78
- // call the pointer move with the current pointer position
79
- canvasEvents.onPointerMove?.(e)
80
72
  }
81
73
  },
82
74
  [canvasEvents, editor]
@@ -1,5 +1,5 @@
1
1
  import { useValue } from '@tldraw/state-react'
2
- import React, { useMemo } from 'react'
2
+ import React, { useEffect, useMemo } from 'react'
3
3
  import { RIGHT_MOUSE_BUTTON } from '../constants'
4
4
  import {
5
5
  preventDefault,
@@ -16,9 +16,6 @@ export function useCanvasEvents() {
16
16
 
17
17
  const events = useMemo(
18
18
  function canvasEvents() {
19
- // Track the last screen point
20
- let lastX: number, lastY: number
21
-
22
19
  function onPointerDown(e: React.PointerEvent) {
23
20
  if ((e as any).isKilled) return
24
21
 
@@ -44,35 +41,9 @@ export function useCanvasEvents() {
44
41
  })
45
42
  }
46
43
 
47
- function onPointerMove(e: React.PointerEvent) {
48
- if ((e as any).isKilled) return
49
-
50
- if (e.clientX === lastX && e.clientY === lastY) return
51
- lastX = e.clientX
52
- lastY = e.clientY
53
-
54
- // For tools that benefit from a higher fidelity of events,
55
- // we dispatch the coalesced events.
56
- // N.B. Sometimes getCoalescedEvents isn't present on iOS, ugh.
57
- const events =
58
- currentTool.useCoalescedEvents && e.nativeEvent.getCoalescedEvents
59
- ? e.nativeEvent.getCoalescedEvents()
60
- : [e]
61
- for (const singleEvent of events) {
62
- editor.dispatch({
63
- type: 'pointer',
64
- target: 'canvas',
65
- name: 'pointer_move',
66
- ...getPointerInfo(singleEvent),
67
- })
68
- }
69
- }
70
-
71
44
  function onPointerUp(e: React.PointerEvent) {
72
45
  if ((e as any).isKilled) return
73
46
  if (e.button !== 0 && e.button !== 1 && e.button !== 2 && e.button !== 5) return
74
- lastX = e.clientX
75
- lastY = e.clientY
76
47
 
77
48
  releasePointerCapture(e.currentTarget, e)
78
49
 
@@ -158,7 +129,6 @@ export function useCanvasEvents() {
158
129
 
159
130
  return {
160
131
  onPointerDown,
161
- onPointerMove,
162
132
  onPointerUp,
163
133
  onPointerEnter,
164
134
  onPointerLeave,
@@ -169,8 +139,45 @@ export function useCanvasEvents() {
169
139
  onClick,
170
140
  }
171
141
  },
172
- [editor, currentTool]
142
+ [editor]
173
143
  )
174
144
 
145
+ // onPointerMove is special: where we're only interested in the other events when they're
146
+ // happening _on_ the canvas (as opposed to outside of it, or on UI floating over it), we want
147
+ // the pointer position to be up to date regardless of whether it's over the tldraw canvas or
148
+ // not. So instead of returning a listener to be attached to the canvas, we directly attach a
149
+ // listener to the whole document instead.
150
+ useEffect(() => {
151
+ let lastX: number, lastY: number
152
+
153
+ function onPointerMove(e: PointerEvent) {
154
+ if ((e as any).isKilled) return
155
+ ;(e as any).isKilled = true
156
+
157
+ if (e.clientX === lastX && e.clientY === lastY) return
158
+ lastX = e.clientX
159
+ lastY = e.clientY
160
+
161
+ // For tools that benefit from a higher fidelity of events,
162
+ // we dispatch the coalesced events.
163
+ // N.B. Sometimes getCoalescedEvents isn't present on iOS, ugh.
164
+ const events =
165
+ currentTool.useCoalescedEvents && e.getCoalescedEvents ? e.getCoalescedEvents() : [e]
166
+ for (const singleEvent of events) {
167
+ editor.dispatch({
168
+ type: 'pointer',
169
+ target: 'canvas',
170
+ name: 'pointer_move',
171
+ ...getPointerInfo(singleEvent),
172
+ })
173
+ }
174
+ }
175
+
176
+ document.body.addEventListener('pointermove', onPointerMove)
177
+ return () => {
178
+ document.body.removeEventListener('pointermove', onPointerMove)
179
+ }
180
+ }, [editor, currentTool])
181
+
175
182
  return events
176
183
  }
@@ -0,0 +1,15 @@
1
+ import { react } from '@tldraw/state'
2
+ import { useLayoutEffect } from 'react'
3
+ import { useEditor } from './useEditor'
4
+
5
+ export function useStateAttribute() {
6
+ const editor = useEditor()
7
+
8
+ // we use a layout effect because we don't want there to be any perceptible delay between the
9
+ // editor mounting and this attribute being applied, because styles may depend on it:
10
+ useLayoutEffect(() => {
11
+ return react('stateAttribute', () => {
12
+ editor.getContainer().setAttribute('data-state', editor.getPath())
13
+ })
14
+ }, [editor])
15
+ }
@@ -0,0 +1,37 @@
1
+ import { atom, Atom } from '@tldraw/state'
2
+ import { WeakCache } from '@tldraw/utils'
3
+ import { Editor } from '../editor/Editor'
4
+
5
+ /**
6
+ * An Atom that is scoped to the lifetime of an Editor.
7
+ *
8
+ * This is useful for storing UI state for tldraw applications. Keeping state scoped to an editor
9
+ * instead of stored in a global atom can prevent issues with state being shared between editors
10
+ * when navigating between pages, or when multiple editor instances are used on the same page.
11
+ *
12
+ * @public
13
+ */
14
+ export class EditorAtom<T> {
15
+ private states = new WeakCache<Editor, Atom<T>>()
16
+
17
+ constructor(
18
+ private name: string,
19
+ private getInitialState: (editor: Editor) => T
20
+ ) {}
21
+
22
+ getAtom(editor: Editor): Atom<T> {
23
+ return this.states.get(editor, () => atom(this.name, this.getInitialState(editor)))
24
+ }
25
+
26
+ get(editor: Editor): T {
27
+ return this.getAtom(editor).get()
28
+ }
29
+
30
+ update(editor: Editor, update: (state: T) => T): T {
31
+ return this.getAtom(editor).update(update)
32
+ }
33
+
34
+ set(editor: Editor, state: T): T {
35
+ return this.getAtom(editor).set(state)
36
+ }
37
+ }
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.16.0-canary.ca347c5375a5'
4
+ export const version = '3.16.0-canary.dfdf6b7de8c2'
5
5
  export const publishDates = {
6
6
  major: '2024-09-13T14:36:29.063Z',
7
- minor: '2025-07-30T14:49:37.329Z',
8
- patch: '2025-07-30T14:49:37.329Z',
7
+ minor: '2025-08-04T10:21:53.495Z',
8
+ patch: '2025-08-04T10:21:53.495Z',
9
9
  }