@tldraw/editor 3.16.0-internal.a478398270c6 → 3.16.0-next.15f085081fd5

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 (235) hide show
  1. package/dist-cjs/index.d.ts +243 -16
  2. package/dist-cjs/index.js +8 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +8 -2
  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/components/SVGContainer.js +1 -1
  9. package/dist-cjs/lib/components/SVGContainer.js.map +2 -2
  10. package/dist-cjs/lib/components/Shape.js +11 -36
  11. package/dist-cjs/lib/components/Shape.js.map +2 -2
  12. package/dist-cjs/lib/components/default-components/DefaultBrush.js +1 -1
  13. package/dist-cjs/lib/components/default-components/DefaultBrush.js.map +2 -2
  14. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +5 -24
  15. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  16. package/dist-cjs/lib/components/default-components/DefaultCollaboratorHint.js +2 -2
  17. package/dist-cjs/lib/components/default-components/DefaultCollaboratorHint.js.map +2 -2
  18. package/dist-cjs/lib/components/default-components/DefaultCursor.js +1 -1
  19. package/dist-cjs/lib/components/default-components/DefaultCursor.js.map +2 -2
  20. package/dist-cjs/lib/components/default-components/DefaultErrorFallback.js +1 -1
  21. package/dist-cjs/lib/components/default-components/DefaultErrorFallback.js.map +2 -2
  22. package/dist-cjs/lib/components/default-components/DefaultGrid.js +1 -1
  23. package/dist-cjs/lib/components/default-components/DefaultGrid.js.map +2 -2
  24. package/dist-cjs/lib/components/default-components/DefaultHandles.js +1 -1
  25. package/dist-cjs/lib/components/default-components/DefaultHandles.js.map +2 -2
  26. package/dist-cjs/lib/components/default-components/DefaultScribble.js +1 -1
  27. package/dist-cjs/lib/components/default-components/DefaultScribble.js.map +2 -2
  28. package/dist-cjs/lib/components/default-components/DefaultShapeIndicator.js +9 -1
  29. package/dist-cjs/lib/components/default-components/DefaultShapeIndicator.js.map +2 -2
  30. package/dist-cjs/lib/components/default-components/DefaultShapeWrapper.js +53 -0
  31. package/dist-cjs/lib/components/default-components/DefaultShapeWrapper.js.map +7 -0
  32. package/dist-cjs/lib/components/default-components/DefaultSnapIndictor.js +1 -1
  33. package/dist-cjs/lib/components/default-components/DefaultSnapIndictor.js.map +2 -2
  34. package/dist-cjs/lib/components/default-components/DefaultSpinner.js +27 -15
  35. package/dist-cjs/lib/components/default-components/DefaultSpinner.js.map +3 -3
  36. package/dist-cjs/lib/config/TLUserPreferences.js +15 -3
  37. package/dist-cjs/lib/config/TLUserPreferences.js.map +2 -2
  38. package/dist-cjs/lib/editor/Editor.js +151 -67
  39. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  40. package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js +14 -4
  41. package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js.map +2 -2
  42. package/dist-cjs/lib/editor/shapes/ShapeUtil.js +13 -0
  43. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  44. package/dist-cjs/lib/editor/tools/StateNode.js +20 -1
  45. package/dist-cjs/lib/editor/tools/StateNode.js.map +2 -2
  46. package/dist-cjs/lib/editor/types/misc-types.js.map +1 -1
  47. package/dist-cjs/lib/exports/getSvgJsx.js +35 -16
  48. package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
  49. package/dist-cjs/lib/hooks/useCanvasEvents.js +31 -25
  50. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  51. package/dist-cjs/lib/hooks/useEditor.js +1 -4
  52. package/dist-cjs/lib/hooks/useEditor.js.map +2 -2
  53. package/dist-cjs/lib/hooks/useEditorComponents.js +2 -0
  54. package/dist-cjs/lib/hooks/useEditorComponents.js.map +2 -2
  55. package/dist-cjs/lib/hooks/usePassThroughWheelEvents.js +4 -1
  56. package/dist-cjs/lib/hooks/usePassThroughWheelEvents.js.map +2 -2
  57. package/dist-cjs/lib/{utils/nearestMultiple.js → hooks/useStateAttribute.js} +15 -14
  58. package/dist-cjs/lib/hooks/useStateAttribute.js.map +7 -0
  59. package/dist-cjs/lib/license/Watermark.js +8 -8
  60. package/dist-cjs/lib/license/Watermark.js.map +2 -2
  61. package/dist-cjs/lib/options.js +7 -0
  62. package/dist-cjs/lib/options.js.map +2 -2
  63. package/dist-cjs/lib/primitives/Box.js +3 -0
  64. package/dist-cjs/lib/primitives/Box.js.map +2 -2
  65. package/dist-cjs/lib/primitives/geometry/Arc2d.js +1 -1
  66. package/dist-cjs/lib/primitives/geometry/Arc2d.js.map +2 -2
  67. package/dist-cjs/lib/primitives/geometry/Circle2d.js +1 -1
  68. package/dist-cjs/lib/primitives/geometry/Circle2d.js.map +2 -2
  69. package/dist-cjs/lib/primitives/geometry/CubicBezier2d.js +3 -1
  70. package/dist-cjs/lib/primitives/geometry/CubicBezier2d.js.map +2 -2
  71. package/dist-cjs/lib/primitives/geometry/Ellipse2d.js +1 -1
  72. package/dist-cjs/lib/primitives/geometry/Ellipse2d.js.map +2 -2
  73. package/dist-cjs/lib/primitives/geometry/geometry-constants.js +2 -2
  74. package/dist-cjs/lib/primitives/geometry/geometry-constants.js.map +2 -2
  75. package/dist-cjs/lib/primitives/intersect.js +4 -4
  76. package/dist-cjs/lib/primitives/intersect.js.map +2 -2
  77. package/dist-cjs/lib/primitives/utils.js +4 -0
  78. package/dist-cjs/lib/primitives/utils.js.map +2 -2
  79. package/dist-cjs/lib/utils/EditorAtom.js +45 -0
  80. package/dist-cjs/lib/utils/EditorAtom.js.map +7 -0
  81. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +0 -1
  82. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js.map +2 -2
  83. package/dist-cjs/version.js +3 -3
  84. package/dist-cjs/version.js.map +1 -1
  85. package/dist-esm/index.d.mts +243 -16
  86. package/dist-esm/index.mjs +16 -2
  87. package/dist-esm/index.mjs.map +2 -2
  88. package/dist-esm/lib/TldrawEditor.mjs +8 -2
  89. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  90. package/dist-esm/lib/components/MenuClickCapture.mjs +0 -5
  91. package/dist-esm/lib/components/MenuClickCapture.mjs.map +2 -2
  92. package/dist-esm/lib/components/SVGContainer.mjs +1 -1
  93. package/dist-esm/lib/components/SVGContainer.mjs.map +2 -2
  94. package/dist-esm/lib/components/Shape.mjs +11 -36
  95. package/dist-esm/lib/components/Shape.mjs.map +2 -2
  96. package/dist-esm/lib/components/default-components/DefaultBrush.mjs +1 -1
  97. package/dist-esm/lib/components/default-components/DefaultBrush.mjs.map +2 -2
  98. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +5 -24
  99. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  100. package/dist-esm/lib/components/default-components/DefaultCollaboratorHint.mjs +2 -2
  101. package/dist-esm/lib/components/default-components/DefaultCollaboratorHint.mjs.map +2 -2
  102. package/dist-esm/lib/components/default-components/DefaultCursor.mjs +1 -1
  103. package/dist-esm/lib/components/default-components/DefaultCursor.mjs.map +2 -2
  104. package/dist-esm/lib/components/default-components/DefaultErrorFallback.mjs +1 -1
  105. package/dist-esm/lib/components/default-components/DefaultErrorFallback.mjs.map +2 -2
  106. package/dist-esm/lib/components/default-components/DefaultGrid.mjs +1 -1
  107. package/dist-esm/lib/components/default-components/DefaultGrid.mjs.map +2 -2
  108. package/dist-esm/lib/components/default-components/DefaultHandles.mjs +1 -1
  109. package/dist-esm/lib/components/default-components/DefaultHandles.mjs.map +2 -2
  110. package/dist-esm/lib/components/default-components/DefaultScribble.mjs +1 -1
  111. package/dist-esm/lib/components/default-components/DefaultScribble.mjs.map +2 -2
  112. package/dist-esm/lib/components/default-components/DefaultShapeIndicator.mjs +9 -1
  113. package/dist-esm/lib/components/default-components/DefaultShapeIndicator.mjs.map +2 -2
  114. package/dist-esm/lib/components/default-components/DefaultShapeWrapper.mjs +23 -0
  115. package/dist-esm/lib/components/default-components/DefaultShapeWrapper.mjs.map +7 -0
  116. package/dist-esm/lib/components/default-components/DefaultSnapIndictor.mjs +1 -1
  117. package/dist-esm/lib/components/default-components/DefaultSnapIndictor.mjs.map +2 -2
  118. package/dist-esm/lib/components/default-components/DefaultSpinner.mjs +17 -15
  119. package/dist-esm/lib/components/default-components/DefaultSpinner.mjs.map +2 -2
  120. package/dist-esm/lib/config/TLUserPreferences.mjs +15 -3
  121. package/dist-esm/lib/config/TLUserPreferences.mjs.map +2 -2
  122. package/dist-esm/lib/editor/Editor.mjs +151 -67
  123. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  124. package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs +14 -4
  125. package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs.map +2 -2
  126. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +13 -0
  127. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  128. package/dist-esm/lib/editor/tools/StateNode.mjs +20 -1
  129. package/dist-esm/lib/editor/tools/StateNode.mjs.map +2 -2
  130. package/dist-esm/lib/exports/getSvgJsx.mjs +36 -16
  131. package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
  132. package/dist-esm/lib/hooks/useCanvasEvents.mjs +32 -26
  133. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  134. package/dist-esm/lib/hooks/useEditor.mjs +1 -4
  135. package/dist-esm/lib/hooks/useEditor.mjs.map +2 -2
  136. package/dist-esm/lib/hooks/useEditorComponents.mjs +4 -0
  137. package/dist-esm/lib/hooks/useEditorComponents.mjs.map +2 -2
  138. package/dist-esm/lib/hooks/usePassThroughWheelEvents.mjs +4 -1
  139. package/dist-esm/lib/hooks/usePassThroughWheelEvents.mjs.map +2 -2
  140. package/dist-esm/lib/hooks/useStateAttribute.mjs +15 -0
  141. package/dist-esm/lib/hooks/useStateAttribute.mjs.map +7 -0
  142. package/dist-esm/lib/license/Watermark.mjs +8 -8
  143. package/dist-esm/lib/license/Watermark.mjs.map +2 -2
  144. package/dist-esm/lib/options.mjs +7 -0
  145. package/dist-esm/lib/options.mjs.map +2 -2
  146. package/dist-esm/lib/primitives/Box.mjs +4 -1
  147. package/dist-esm/lib/primitives/Box.mjs.map +2 -2
  148. package/dist-esm/lib/primitives/geometry/Arc2d.mjs +2 -2
  149. package/dist-esm/lib/primitives/geometry/Arc2d.mjs.map +2 -2
  150. package/dist-esm/lib/primitives/geometry/Circle2d.mjs +2 -2
  151. package/dist-esm/lib/primitives/geometry/Circle2d.mjs.map +2 -2
  152. package/dist-esm/lib/primitives/geometry/CubicBezier2d.mjs +3 -1
  153. package/dist-esm/lib/primitives/geometry/CubicBezier2d.mjs.map +2 -2
  154. package/dist-esm/lib/primitives/geometry/Ellipse2d.mjs +2 -2
  155. package/dist-esm/lib/primitives/geometry/Ellipse2d.mjs.map +2 -2
  156. package/dist-esm/lib/primitives/geometry/geometry-constants.mjs +2 -2
  157. package/dist-esm/lib/primitives/geometry/geometry-constants.mjs.map +2 -2
  158. package/dist-esm/lib/primitives/intersect.mjs +5 -5
  159. package/dist-esm/lib/primitives/intersect.mjs.map +2 -2
  160. package/dist-esm/lib/primitives/utils.mjs +4 -0
  161. package/dist-esm/lib/primitives/utils.mjs.map +2 -2
  162. package/dist-esm/lib/utils/EditorAtom.mjs +25 -0
  163. package/dist-esm/lib/utils/EditorAtom.mjs.map +7 -0
  164. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +0 -1
  165. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs.map +2 -2
  166. package/dist-esm/version.mjs +3 -3
  167. package/dist-esm/version.mjs.map +1 -1
  168. package/editor.css +320 -313
  169. package/package.json +16 -38
  170. package/src/index.ts +15 -1
  171. package/src/lib/TldrawEditor.tsx +13 -6
  172. package/src/lib/components/MenuClickCapture.tsx +0 -8
  173. package/src/lib/components/SVGContainer.tsx +1 -1
  174. package/src/lib/components/Shape.tsx +12 -33
  175. package/src/lib/components/default-components/DefaultBrush.tsx +1 -1
  176. package/src/lib/components/default-components/DefaultCanvas.tsx +6 -23
  177. package/src/lib/components/default-components/DefaultCollaboratorHint.tsx +2 -2
  178. package/src/lib/components/default-components/DefaultCursor.tsx +1 -1
  179. package/src/lib/components/default-components/DefaultErrorFallback.tsx +1 -1
  180. package/src/lib/components/default-components/DefaultGrid.tsx +1 -1
  181. package/src/lib/components/default-components/DefaultHandles.tsx +5 -1
  182. package/src/lib/components/default-components/DefaultScribble.tsx +1 -1
  183. package/src/lib/components/default-components/DefaultShapeIndicator.tsx +6 -2
  184. package/src/lib/components/default-components/DefaultShapeWrapper.tsx +35 -0
  185. package/src/lib/components/default-components/DefaultSnapIndictor.tsx +1 -1
  186. package/src/lib/components/default-components/DefaultSpinner.tsx +12 -12
  187. package/src/lib/config/TLUserPreferences.ts +15 -1
  188. package/src/lib/editor/Editor.test.ts +416 -8
  189. package/src/lib/editor/Editor.ts +195 -92
  190. package/src/lib/editor/managers/ClickManager/ClickManager.test.ts +15 -14
  191. package/src/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.test.ts +16 -15
  192. package/src/lib/editor/managers/FocusManager/FocusManager.test.ts +49 -48
  193. package/src/lib/editor/managers/FontManager/FontManager.test.ts +24 -23
  194. package/src/lib/editor/managers/HistoryManager/HistoryManager.test.ts +7 -6
  195. package/src/lib/editor/managers/ScribbleManager/ScribbleManager.test.ts +12 -11
  196. package/src/lib/editor/managers/SnapManager/SnapManager.test.ts +57 -50
  197. package/src/lib/editor/managers/TextManager/TextManager.test.ts +51 -26
  198. package/src/lib/editor/managers/TickManager/TickManager.test.ts +14 -13
  199. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.test.ts +55 -26
  200. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.ts +14 -1
  201. package/src/lib/editor/shapes/ShapeUtil.ts +71 -0
  202. package/src/lib/editor/tools/StateNode.test.ts +285 -0
  203. package/src/lib/editor/tools/StateNode.ts +27 -1
  204. package/src/lib/editor/types/misc-types.ts +73 -1
  205. package/src/lib/exports/getSvgJsx.test.ts +868 -0
  206. package/src/lib/exports/getSvgJsx.tsx +78 -21
  207. package/src/lib/hooks/useCanvasEvents.ts +45 -38
  208. package/src/lib/hooks/useEditor.tsx +6 -5
  209. package/src/lib/hooks/useEditorComponents.tsx +8 -2
  210. package/src/lib/hooks/usePassThroughWheelEvents.ts +6 -1
  211. package/src/lib/hooks/useStateAttribute.ts +15 -0
  212. package/src/lib/license/LicenseManager.test.ts +3 -1
  213. package/src/lib/license/Watermark.test.tsx +2 -1
  214. package/src/lib/license/Watermark.tsx +8 -8
  215. package/src/lib/options.ts +8 -0
  216. package/src/lib/primitives/Box.test.ts +126 -0
  217. package/src/lib/primitives/Box.ts +10 -1
  218. package/src/lib/primitives/geometry/Arc2d.ts +2 -2
  219. package/src/lib/primitives/geometry/Circle2d.ts +2 -2
  220. package/src/lib/primitives/geometry/CubicBezier2d.ts +4 -1
  221. package/src/lib/primitives/geometry/Ellipse2d.ts +2 -2
  222. package/src/lib/primitives/geometry/geometry-constants.ts +2 -1
  223. package/src/lib/primitives/intersect.test.ts +946 -0
  224. package/src/lib/primitives/intersect.ts +12 -5
  225. package/src/lib/primitives/utils.ts +11 -0
  226. package/src/lib/utils/EditorAtom.ts +37 -0
  227. package/src/lib/utils/sync/LocalIndexedDb.test.ts +2 -1
  228. package/src/lib/utils/sync/TLLocalSyncClient.test.ts +15 -15
  229. package/src/lib/utils/sync/TLLocalSyncClient.ts +0 -1
  230. package/src/version.ts +3 -3
  231. package/dist-cjs/lib/utils/nearestMultiple.js.map +0 -7
  232. package/dist-esm/lib/utils/nearestMultiple.mjs +0 -14
  233. package/dist-esm/lib/utils/nearestMultiple.mjs.map +0 -7
  234. package/src/lib/test/currentToolIdMask.test.ts +0 -49
  235. package/src/lib/utils/nearestMultiple.ts +0 -13
@@ -0,0 +1,285 @@
1
+ import { createTLStore } from '../../config/createTLStore'
2
+ import { Editor } from '../Editor'
3
+ import { StateNode } from './StateNode'
4
+
5
+ describe('StateNode.addChild', () => {
6
+ // Test state node classes for addChild tests
7
+ class ParentState extends StateNode {
8
+ static override id = 'parent'
9
+ static override initial = 'child1'
10
+ static override children() {
11
+ return [ChildState1]
12
+ }
13
+ }
14
+
15
+ class ChildState1 extends StateNode {
16
+ static override id = 'child1'
17
+ }
18
+
19
+ class ChildState2 extends StateNode {
20
+ static override id = 'child2'
21
+ }
22
+
23
+ class ChildState3 extends StateNode {
24
+ static override id = 'child3'
25
+ }
26
+
27
+ class LeafState extends StateNode {
28
+ static override id = 'leaf'
29
+ }
30
+
31
+ class RootState extends StateNode {
32
+ static override id = 'root'
33
+ static override initial = 'child1'
34
+ static override children() {
35
+ return [ChildState1]
36
+ }
37
+ }
38
+
39
+ class RootStateWithoutChildren extends StateNode {
40
+ static override id = 'rootWithoutChildren'
41
+ }
42
+
43
+ let editor: Editor
44
+
45
+ beforeEach(() => {
46
+ editor = new Editor({
47
+ initialState: 'parent',
48
+ shapeUtils: [],
49
+ bindingUtils: [],
50
+ tools: [
51
+ ParentState,
52
+ ChildState1,
53
+ ChildState2,
54
+ ChildState3,
55
+ LeafState,
56
+ RootState,
57
+ RootStateWithoutChildren,
58
+ ],
59
+ store: createTLStore({ shapeUtils: [], bindingUtils: [] }),
60
+ getContainer: () => document.body,
61
+ })
62
+ })
63
+
64
+ it('should add a child to a branch state node', () => {
65
+ const parentState = editor.root.children!['parent'] as ParentState
66
+
67
+ // Initially should have one child
68
+ expect(Object.keys(parentState.children!)).toHaveLength(1)
69
+ expect(parentState.children!['child1']).toBeDefined()
70
+
71
+ // Add a new child
72
+ parentState.addChild(ChildState2)
73
+
74
+ // Should now have two children
75
+ expect(Object.keys(parentState.children!)).toHaveLength(2)
76
+ expect(parentState.children!['child1']).toBeDefined()
77
+ expect(parentState.children!['child2']).toBeDefined()
78
+ expect(parentState.children!['child2']).toBeInstanceOf(ChildState2)
79
+ })
80
+
81
+ it('should add a child to a root state node', () => {
82
+ const rootState = editor.root.children!['root'] as RootState
83
+
84
+ // Initially should have one child
85
+ expect(Object.keys(rootState.children!)).toHaveLength(1)
86
+ expect(rootState.children!['child1']).toBeDefined()
87
+
88
+ // Add a new child
89
+ rootState.addChild(ChildState2)
90
+
91
+ // Should now have two children
92
+ expect(Object.keys(rootState.children!)).toHaveLength(2)
93
+ expect(rootState.children!['child1']).toBeDefined()
94
+ expect(rootState.children!['child2']).toBeDefined()
95
+ expect(rootState.children!['child2']).toBeInstanceOf(ChildState2)
96
+ })
97
+
98
+ it('should throw an error when trying to add a child to a leaf state node', () => {
99
+ const leafState = editor.root.children!['leaf'] as LeafState
100
+
101
+ // Leaf state should not have children
102
+ expect(leafState.children).toBeUndefined()
103
+
104
+ // Should throw an error when trying to add a child
105
+ expect(() => {
106
+ leafState.addChild(ChildState2)
107
+ }).toThrow('StateNode.addChild: cannot add child to a leaf node')
108
+ })
109
+
110
+ it('should return the parent state node for chaining', () => {
111
+ const parentState = editor.root.children!['parent'] as ParentState
112
+
113
+ const result = parentState.addChild(ChildState2)
114
+
115
+ expect(result).toBe(parentState)
116
+ })
117
+
118
+ it('should create the child with the correct editor and parent', () => {
119
+ const parentState = editor.root.children!['parent'] as ParentState
120
+
121
+ parentState.addChild(ChildState2)
122
+ const childState = parentState.children!['child2'] as ChildState2
123
+
124
+ expect(childState.editor).toBe(editor)
125
+ expect(childState.parent).toBe(parentState)
126
+ })
127
+
128
+ it('should allow adding multiple children', () => {
129
+ const parentState = editor.root.children!['parent'] as ParentState
130
+
131
+ // Add multiple children
132
+ parentState.addChild(ChildState2).addChild(ChildState3)
133
+
134
+ // Should have three children
135
+ expect(Object.keys(parentState.children!)).toHaveLength(3)
136
+ expect(parentState.children!['child1']).toBeDefined()
137
+ expect(parentState.children!['child2']).toBeDefined()
138
+ expect(parentState.children!['child3']).toBeDefined()
139
+ expect(parentState.children!['child2']).toBeInstanceOf(ChildState2)
140
+ expect(parentState.children!['child3']).toBeInstanceOf(ChildState3)
141
+ })
142
+
143
+ it('should allow transitioning to added children', () => {
144
+ const parentState = editor.root.children!['parent'] as ParentState
145
+
146
+ // Add a new child
147
+ parentState.addChild(ChildState2)
148
+
149
+ // Should be able to transition to the new child
150
+ expect(() => {
151
+ parentState.transition('child2')
152
+ }).not.toThrow()
153
+
154
+ // The current state should be the new child
155
+ expect(parentState.getCurrent()?.id).toBe('child2')
156
+ })
157
+
158
+ it('should maintain existing children when adding new ones', () => {
159
+ const parentState = editor.root.children!['parent'] as ParentState
160
+ const originalChild = parentState.children!['child1']
161
+
162
+ // Add a new child
163
+ parentState.addChild(ChildState2)
164
+
165
+ // Original child should still exist and be the same instance
166
+ expect(parentState.children!['child1']).toBe(originalChild)
167
+ expect(parentState.children!['child1']).toBeInstanceOf(ChildState1)
168
+ })
169
+
170
+ it('should initialize children object for root nodes without static children', () => {
171
+ // Create a StateNode directly as a root node (no parent)
172
+ const mockEditor = {} as Editor
173
+ const rootStateWithoutChildren = new RootStateWithoutChildren(mockEditor, undefined)
174
+
175
+ // Root state without static children should not have children initially
176
+ expect(rootStateWithoutChildren.children).toBeUndefined()
177
+
178
+ // Adding a child should initialize the children object
179
+ rootStateWithoutChildren.addChild(ChildState2)
180
+
181
+ // Should now have children object with the added child
182
+ expect(rootStateWithoutChildren.children).toBeDefined()
183
+ expect(Object.keys(rootStateWithoutChildren.children!)).toHaveLength(1)
184
+ expect(rootStateWithoutChildren.children!['child2']).toBeDefined()
185
+ expect(rootStateWithoutChildren.children!['child2']).toBeInstanceOf(ChildState2)
186
+ })
187
+
188
+ it('should throw an error when trying to add a child with a duplicate ID', () => {
189
+ const parentState = editor.root.children!['parent'] as ParentState
190
+
191
+ // Initially should have one child
192
+ expect(Object.keys(parentState.children!)).toHaveLength(1)
193
+ expect(parentState.children!['child1']).toBeDefined()
194
+
195
+ // Should throw an error when trying to add a child with the same ID
196
+ expect(() => {
197
+ parentState.addChild(ChildState1)
198
+ }).toThrow("StateNode.addChild: a child with id 'child1' already exists")
199
+
200
+ // Should still have only one child
201
+ expect(Object.keys(parentState.children!)).toHaveLength(1)
202
+ expect(parentState.children!['child1']).toBeDefined()
203
+ })
204
+
205
+ it('should throw an error when trying to add a child with a duplicate ID to a root state', () => {
206
+ const rootState = editor.root.children!['root'] as RootState
207
+
208
+ // Initially should have one child
209
+ expect(Object.keys(rootState.children!)).toHaveLength(1)
210
+ expect(rootState.children!['child1']).toBeDefined()
211
+
212
+ // Should throw an error when trying to add a child with the same ID
213
+ expect(() => {
214
+ rootState.addChild(ChildState1)
215
+ }).toThrow("StateNode.addChild: a child with id 'child1' already exists")
216
+
217
+ // Should still have only one child
218
+ expect(Object.keys(rootState.children!)).toHaveLength(1)
219
+ expect(rootState.children!['child1']).toBeDefined()
220
+ })
221
+
222
+ it('should throw an error when trying to add a child with a duplicate ID to a root state without static children', () => {
223
+ // Create a StateNode directly as a root node (no parent)
224
+ const mockEditor = {} as Editor
225
+ const rootStateWithoutChildren = new RootStateWithoutChildren(mockEditor, undefined)
226
+
227
+ // Add a child first
228
+ rootStateWithoutChildren.addChild(ChildState1)
229
+
230
+ // Should throw an error when trying to add a child with the same ID
231
+ expect(() => {
232
+ rootStateWithoutChildren.addChild(ChildState1)
233
+ }).toThrow("StateNode.addChild: a child with id 'child1' already exists")
234
+
235
+ // Should still have only one child
236
+ expect(Object.keys(rootStateWithoutChildren.children!)).toHaveLength(1)
237
+ expect(rootStateWithoutChildren.children!['child1']).toBeDefined()
238
+ })
239
+ })
240
+
241
+ describe('current tool id mask', () => {
242
+ // Tool mask test classes
243
+ class ToolA extends StateNode {
244
+ static override id = 'A'
245
+ }
246
+
247
+ class ToolB extends StateNode {
248
+ static override id = 'B'
249
+ }
250
+
251
+ class ToolC extends StateNode {
252
+ static override id = 'C'
253
+
254
+ override onEnter() {
255
+ this.setCurrentToolIdMask('A')
256
+ }
257
+ }
258
+
259
+ let toolMaskEditor: Editor
260
+
261
+ beforeEach(() => {
262
+ toolMaskEditor = new Editor({
263
+ initialState: 'A',
264
+ shapeUtils: [],
265
+ bindingUtils: [],
266
+ tools: [ToolA, ToolB, ToolC],
267
+ store: createTLStore({ shapeUtils: [], bindingUtils: [] }),
268
+ getContainer: () => document.body,
269
+ })
270
+ })
271
+
272
+ it('starts with the correct tool id', () => {
273
+ expect(toolMaskEditor.getCurrentToolId()).toBe('A')
274
+ })
275
+
276
+ it('updates the current tool id', () => {
277
+ toolMaskEditor.setCurrentTool('B')
278
+ expect(toolMaskEditor.getCurrentToolId()).toBe('B')
279
+ })
280
+
281
+ it('masks the current tool id', () => {
282
+ toolMaskEditor.setCurrentTool('C')
283
+ expect(toolMaskEditor.getCurrentToolId()).toBe('A')
284
+ })
285
+ })
@@ -62,7 +62,7 @@ export abstract class StateNode implements Partial<TLEventHandlers> {
62
62
 
63
63
  this.parent = parent ?? ({} as any)
64
64
 
65
- if (this.parent) {
65
+ if (parent) {
66
66
  if (children && initial) {
67
67
  this.type = 'branch'
68
68
  this.initial = initial
@@ -238,6 +238,32 @@ export abstract class StateNode implements Partial<TLEventHandlers> {
238
238
  this._currentToolIdMask.set(id)
239
239
  }
240
240
 
241
+ /**
242
+ * Add a child node to this state node.
243
+ *
244
+ * @public
245
+ */
246
+ addChild(childConstructor: TLStateNodeConstructor): this {
247
+ if (this.type === 'leaf') {
248
+ throw new Error('StateNode.addChild: cannot add child to a leaf node')
249
+ }
250
+
251
+ // Initialize children if it's undefined (for root nodes without static children)
252
+ if (!this.children) {
253
+ this.children = {}
254
+ }
255
+
256
+ const child = new childConstructor(this.editor, this)
257
+
258
+ // Check if a child with this ID already exists
259
+ if (this.children[child.id]) {
260
+ throw new Error(`StateNode.addChild: a child with id '${child.id}' already exists`)
261
+ }
262
+
263
+ this.children[child.id] = child
264
+ return this
265
+ }
266
+
241
267
  onWheel?(info: TLWheelEventInfo): void
242
268
  onPointerDown?(info: TLPointerEventInfo): void
243
269
  onPointerMove?(info: TLPointerEventInfo): void
@@ -1,4 +1,4 @@
1
- import { BoxModel } from '@tldraw/tlschema'
1
+ import { BoxModel, TLShape } from '@tldraw/tlschema'
2
2
  import { Box } from '../../primitives/Box'
3
3
  import { VecLike } from '../../primitives/Vec'
4
4
 
@@ -187,3 +187,75 @@ export interface TLCameraConstraints {
187
187
  y: 'free' | 'fixed' | 'inside' | 'outside' | 'contain'
188
188
  }
189
189
  }
190
+
191
+ /** @public */
192
+ export interface TLUpdatePointerOptions {
193
+ /** Whether to update the pointer immediately, rather than on the next tick. */
194
+ immediate?: boolean
195
+ /**
196
+ * The point, in screen-space, to update the pointer to. Defaults to the position of the last
197
+ * pointer event.
198
+ */
199
+ point?: VecLike
200
+ pointerId?: number
201
+ ctrlKey?: boolean
202
+ altKey?: boolean
203
+ shiftKey?: boolean
204
+ metaKey?: boolean
205
+ accelKey?: boolean
206
+ isPen?: boolean
207
+ button?: number
208
+ }
209
+
210
+ /**
211
+ * Options to {@link Editor.getShapeAtPoint}.
212
+ *
213
+ * @public
214
+ */
215
+ export interface TLGetShapeAtPointOptions {
216
+ /**
217
+ * The margin to apply to the shape.
218
+ * If a number, it will be applied to both the inside and outside of the shape.
219
+ * If an array, the first element will be applied to the inside of the shape, and the second element will be applied to the outside.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * // Get the shape at the center of the screen
224
+ * const shape = editor.getShapeAtProps({
225
+ * margin: 10,
226
+ * })
227
+ *
228
+ * // Get the shape at the center of the screen with a 10px inner margin and a 5px outer margin
229
+ * const shape = editor.getShapeAtProps({
230
+ * margin: [10, 5],
231
+ * })
232
+ * ```
233
+ */
234
+ margin?: number | [number, number]
235
+ /**
236
+ * Whether to register hits inside of shapes (beyond the margin), such as the inside of a solid shape.
237
+ */
238
+ hitInside?: boolean
239
+ /**
240
+ * Whether to register hits on locked shapes.
241
+ */
242
+ hitLocked?: boolean
243
+ /**
244
+ * Whether to register hits on labels.
245
+ */
246
+ hitLabels?: boolean
247
+ /**
248
+ * Whether to only return hits on shapes that are currently being rendered.
249
+ * todo: rename this to hitCulled or hitNotRendering
250
+ */
251
+ renderingOnly?: boolean
252
+ /**
253
+ * Whether to register hits on the inside of frame shapes.
254
+ * todo: rename this to hitInsideFrames
255
+ */
256
+ hitFrameInside?: boolean
257
+ /**
258
+ * A filter function to apply to the shapes.
259
+ */
260
+ filter?(shape: TLShape): boolean
261
+ }