@tldraw/driver 4.5.0-canary.84ac7a331515

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.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # @tldraw/driver
2
+
3
+ Imperative API for driving the tldraw editor programmatically. Useful for scripting, automation, REPL usage, and testing.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @tldraw/driver
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ```ts
14
+ import { Driver } from '@tldraw/driver'
15
+
16
+ const driver = new Driver(editor)
17
+
18
+ // Simulate user interactions
19
+ driver.click(100, 200).pointerDown(300, 400).pointerMove(500, 600).pointerUp()
20
+
21
+ // Keyboard input
22
+ driver.keyPress('a')
23
+ driver.keyDown('Shift')
24
+ driver.keyUp('Shift')
25
+
26
+ // Clipboard
27
+ driver.copy()
28
+ driver.paste({ x: 100, y: 100 })
29
+
30
+ // Selection manipulation
31
+ driver.translateSelection(50, 0)
32
+ driver.rotateSelection(Math.PI / 4)
33
+ driver.resizeSelection({ scaleX: 2 }, 'bottom_right')
34
+
35
+ // Camera
36
+ driver.pan({ x: 100, y: 0 })
37
+ driver.wheel(0, -100)
38
+
39
+ // Cleanup
40
+ driver.dispose()
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### Constructor
46
+
47
+ #### `new Driver(editor: Editor)`
48
+
49
+ Wraps an existing `Editor` instance. All methods use only public Editor APIs.
50
+
51
+ ### Input events
52
+
53
+ All input methods return `this` for fluent chaining. Coordinates are in screen space.
54
+
55
+ | Method | Description |
56
+ | --------------------------------------------- | -------------------------------------- |
57
+ | `pointerDown(x?, y?, options?, modifiers?)` | Dispatch a pointer down event |
58
+ | `pointerMove(x?, y?, options?, modifiers?)` | Dispatch a pointer move event |
59
+ | `pointerUp(x?, y?, options?, modifiers?)` | Dispatch a pointer up event |
60
+ | `click(x?, y?, options?, modifiers?)` | Pointer down + up |
61
+ | `rightClick(x?, y?, options?, modifiers?)` | Right-click (button 2) down + up |
62
+ | `doubleClick(x?, y?, options?, modifiers?)` | Double-click sequence |
63
+ | `keyDown(key, options?)` | Dispatch a key down event |
64
+ | `keyUp(key, options?)` | Dispatch a key up event |
65
+ | `keyPress(key, options?)` | Key down + up |
66
+ | `keyRepeat(key, options?)` | Dispatch a key repeat event |
67
+ | `wheel(dx, dy, options?)` | Dispatch a wheel/scroll event |
68
+ | `pinchStart(x?, y?, z, dx, dy, dz, options?)` | Begin a pinch gesture |
69
+ | `pinchTo(x?, y?, z, dx, dy, dz, options?)` | Continue a pinch gesture |
70
+ | `pinchEnd(x?, y?, z, dx, dy, dz, options?)` | End a pinch gesture |
71
+ | `forceTick(count?)` | Emit tick events to advance the editor |
72
+
73
+ ### Clipboard
74
+
75
+ | Method | Description |
76
+ | --------------- | --------------------------------------------------- |
77
+ | `copy(ids?)` | Copy shapes to the driver's clipboard |
78
+ | `cut(ids?)` | Cut shapes (copy + delete) |
79
+ | `paste(point?)` | Paste from the driver's clipboard |
80
+ | `clipboard` | The current clipboard content (`TLContent \| null`) |
81
+
82
+ ### Selection manipulation
83
+
84
+ These methods work in page coordinates and handle the screen-space conversion internally.
85
+
86
+ | Method | Description |
87
+ | ------------------------------------------- | ------------------------------------------- |
88
+ | `translateSelection(dx, dy, options?)` | Move the selection by a page-space delta |
89
+ | `rotateSelection(angle, options?)` | Rotate the selection by an angle in radians |
90
+ | `resizeSelection(scale?, handle, options?)` | Resize the selection via a handle |
91
+
92
+ ### Queries
93
+
94
+ | Method | Description |
95
+ | ------------------------------ | ------------------------------------------- |
96
+ | `getViewportPageCenter()` | Center of the viewport in page coordinates |
97
+ | `getSelectionPageCenter()` | Center of the selection in page coordinates |
98
+ | `getPageCenter(shape)` | Center of a shape in page coordinates |
99
+ | `getPageRotation(shape)` | Rotation of a shape in page space (radians) |
100
+ | `getPageRotationById(id)` | Rotation of a shape by ID (radians) |
101
+ | `getArrowsBoundTo(shapeId)` | All arrows bound to a shape |
102
+ | `getLastCreatedShape()` | The most recently created shape |
103
+ | `getLastCreatedShapes(count?)` | The last N created shapes |
104
+
105
+ ### Camera
106
+
107
+ | Method | Description |
108
+ | ------------- | ------------------------------------- |
109
+ | `pan(offset)` | Pan the camera by a page-space offset |
110
+
111
+ ### Lifecycle
112
+
113
+ | Method | Description |
114
+ | ----------- | -------------------------------------------- |
115
+ | `dispose()` | Remove side-effect handlers. Call when done. |
116
+
117
+ ## License
118
+
119
+ See [LICENSE.md](../../LICENSE.md).
@@ -0,0 +1,270 @@
1
+ import { Editor } from '@tldraw/editor';
2
+ import { RotateCorner } from '@tldraw/editor';
3
+ import { SelectionHandle } from '@tldraw/editor';
4
+ import { TLArrowShape } from '@tldraw/editor';
5
+ import { TLContent } from '@tldraw/editor';
6
+ import { TLKeyboardEventInfo } from '@tldraw/editor';
7
+ import { TLPageId } from '@tldraw/editor';
8
+ import { TLPinchEventInfo } from '@tldraw/editor';
9
+ import { TLPointerEventInfo } from '@tldraw/editor';
10
+ import { TLShape } from '@tldraw/editor';
11
+ import { TLShapeId } from '@tldraw/editor';
12
+ import { TLWheelEventInfo } from '@tldraw/editor';
13
+ import { Vec } from '@tldraw/editor';
14
+ import { VecLike } from '@tldraw/editor';
15
+
16
+ /**
17
+ * Driver wraps an Editor instance and provides an imperative API for driving it
18
+ * programmatically. Useful for scripting, automation, REPL usage, and testing.
19
+ *
20
+ * All methods use only public Editor APIs and return `this` for fluent chaining.
21
+ *
22
+ * @public
23
+ */
24
+ export declare class Driver {
25
+ readonly editor: Editor;
26
+ /** The underlying Editor instance. */
27
+ private _cleanup;
28
+ constructor(editor: Editor);
29
+ /** Remove all registered side-effect handlers. Call when this controller is no longer needed. */
30
+ dispose(): void;
31
+ /** Local clipboard content. Used by copy, cut, and paste. */
32
+ clipboard: null | TLContent;
33
+ private _lastCreatedShapes;
34
+ /**
35
+ * Get the last created shapes.
36
+ * @param count - The number of shapes to get.
37
+ */
38
+ getLastCreatedShapes(count?: number): TLShape[];
39
+ /**
40
+ * Get the last created shape.
41
+ */
42
+ getLastCreatedShape<T extends TLShape>(): T;
43
+ /**
44
+ * Creates a shape ID from a string.
45
+ * @param id - The string to convert to a shape ID.
46
+ */
47
+ createShapeID(id: string): TLShapeId;
48
+ /**
49
+ * Creates a page ID from a string.
50
+ * @param id - The string to convert to a page ID.
51
+ */
52
+ createPageID(id: string): TLPageId;
53
+ /**
54
+ * Copies the given shapes to the controller clipboard. Defaults to the current selection.
55
+ * @param ids - Shape IDs to copy. Defaults to the current selection.
56
+ */
57
+ copy(ids?: TLShapeId[]): this;
58
+ /**
59
+ * Cuts the given shapes (copy to clipboard, then delete). Defaults to the current selection.
60
+ * @param ids - Shape IDs to cut. Defaults to the current selection.
61
+ */
62
+ cut(ids?: TLShapeId[]): this;
63
+ /**
64
+ * Pastes content from the controller clipboard. If shift is held, uses current pointer. Otherwise uses the given point.
65
+ * @param point - Page coordinates for paste location. If omitted and shift is not held, placement may vary.
66
+ */
67
+ paste(point?: VecLike): this;
68
+ /** Returns the center of the viewport in page coordinates. */
69
+ getViewportPageCenter(): Vec;
70
+ /** Returns the center of the current selection in page coordinates, or null if nothing is selected. */
71
+ getSelectionPageCenter(): null | Vec;
72
+ /**
73
+ * Returns the center of a shape in page coordinates, or null if the shape has no page transform.
74
+ * @param shape - The shape to get the center of.
75
+ */
76
+ getPageCenter(shape: TLShape): null | Vec;
77
+ /**
78
+ * Returns the rotation of a shape in page space by ID, in radians.
79
+ * @param id - The shape ID.
80
+ */
81
+ getPageRotationById(id: TLShapeId): number;
82
+ /**
83
+ * Returns the rotation of a shape in page space, in radians.
84
+ * @param shape - The shape to get the rotation of.
85
+ */
86
+ getPageRotation(shape: TLShape): number;
87
+ /**
88
+ * Returns all arrow shapes bound to the given shape.
89
+ * @param shapeId - The shape ID to find arrows bound to.
90
+ */
91
+ getArrowsBoundTo(shapeId: TLShapeId): TLArrowShape[];
92
+ /**
93
+ * Builds a TLPointerEventInfo object for input simulation.
94
+ * @param x - Screen x coordinate. Defaults to current pointer.
95
+ * @param y - Screen y coordinate. Defaults to current pointer.
96
+ * @param options - Target shape/selection or partial event info.
97
+ * @param modifiers - Override shift, ctrl, or alt key state.
98
+ */
99
+ private getPointerEventInfo;
100
+ /**
101
+ * Builds a TLKeyboardEventInfo object for input simulation.
102
+ * @param key - The key being pressed.
103
+ * @param name - The event name (key_down, key_up, key_repeat).
104
+ * @param options - Partial event info overrides.
105
+ */
106
+ private getKeyboardEventInfo;
107
+ /**
108
+ * Emits tick events to advance the editor by the given number of frames (default 1).
109
+ * @param count - Number of tick events to emit. Defaults to 1.
110
+ */
111
+ forceTick(count?: number): this;
112
+ /**
113
+ * Dispatches a pointer move event at the given screen coordinates.
114
+ * @param x - Screen x coordinate. Defaults to current pointer.
115
+ * @param y - Screen y coordinate. Defaults to current pointer.
116
+ * @param options - Target shape/selection or partial event info.
117
+ * @param modifiers - Override shift, ctrl, or alt key state.
118
+ */
119
+ pointerMove(x?: number, y?: number, options?: PointerEventInit_2, modifiers?: EventModifiers): this;
120
+ /**
121
+ * Dispatches a pointer down event at the given screen coordinates.
122
+ * @param x - Screen x coordinate. Defaults to current pointer.
123
+ * @param y - Screen y coordinate. Defaults to current pointer.
124
+ * @param options - Target shape/selection or partial event info.
125
+ * @param modifiers - Override shift, ctrl, or alt key state.
126
+ */
127
+ pointerDown(x?: number, y?: number, options?: PointerEventInit_2, modifiers?: EventModifiers): this;
128
+ /**
129
+ * Dispatches a pointer up event at the given screen coordinates.
130
+ * @param x - Screen x coordinate. Defaults to current pointer.
131
+ * @param y - Screen y coordinate. Defaults to current pointer.
132
+ * @param options - Target shape/selection or partial event info.
133
+ * @param modifiers - Override shift, ctrl, or alt key state.
134
+ */
135
+ pointerUp(x?: number, y?: number, options?: PointerEventInit_2, modifiers?: EventModifiers): this;
136
+ /**
137
+ * Dispatches a pointer down followed by pointer up at the given screen coordinates.
138
+ * @param x - Screen x coordinate. Defaults to current pointer.
139
+ * @param y - Screen y coordinate. Defaults to current pointer.
140
+ * @param options - Target shape/selection or partial event info.
141
+ * @param modifiers - Override shift, ctrl, or alt key state.
142
+ */
143
+ click(x?: number, y?: number, options?: PointerEventInit_2, modifiers?: EventModifiers): this;
144
+ /**
145
+ * Dispatches a right-click (button 2) down and up at the given screen coordinates.
146
+ * @param x - Screen x coordinate. Defaults to current pointer.
147
+ * @param y - Screen y coordinate. Defaults to current pointer.
148
+ * @param options - Target shape/selection or partial event info.
149
+ * @param modifiers - Override shift, ctrl, or alt key state.
150
+ */
151
+ rightClick(x?: number, y?: number, options?: PointerEventInit_2, modifiers?: EventModifiers): this;
152
+ /**
153
+ * Dispatches a double-click sequence at the given screen coordinates.
154
+ * @param x - Screen x coordinate. Defaults to current pointer.
155
+ * @param y - Screen y coordinate. Defaults to current pointer.
156
+ * @param options - Target shape/selection or partial event info.
157
+ * @param modifiers - Override shift, ctrl, or alt key state.
158
+ */
159
+ doubleClick(x?: number, y?: number, options?: PointerEventInit_2, modifiers?: EventModifiers): this;
160
+ /**
161
+ * Dispatches a key down followed by key up for the given key.
162
+ * @param key - The key to press (e.g. 'a', 'Enter', 'Shift').
163
+ * @param options - Partial keyboard event overrides.
164
+ */
165
+ keyPress(key: string, options?: Partial<Omit<TLKeyboardEventInfo, "key">>): this;
166
+ /**
167
+ * Dispatches a key down event for the given key.
168
+ * @param key - The key to press (e.g. 'a', 'Enter', 'Shift').
169
+ * @param options - Partial keyboard event overrides.
170
+ */
171
+ keyDown(key: string, options?: Partial<Omit<TLKeyboardEventInfo, "key">>): this;
172
+ /**
173
+ * Dispatches a key repeat event for the given key.
174
+ * @param key - The key that is repeating (e.g. 'a', 'ArrowDown').
175
+ * @param options - Partial keyboard event overrides.
176
+ */
177
+ keyRepeat(key: string, options?: Partial<Omit<TLKeyboardEventInfo, "key">>): this;
178
+ /**
179
+ * Dispatches a key up event for the given key.
180
+ * @param key - The key to release (e.g. 'a', 'Enter', 'Shift').
181
+ * @param options - Partial keyboard event overrides.
182
+ */
183
+ keyUp(key: string, options?: Partial<Omit<TLKeyboardEventInfo, "key">>): this;
184
+ /**
185
+ * Dispatches a wheel event with the given delta values.
186
+ * @param dx - Horizontal scroll delta.
187
+ * @param dy - Vertical scroll delta.
188
+ * @param options - Partial wheel event overrides.
189
+ */
190
+ wheel(dx: number, dy: number, options?: Partial<Omit<TLWheelEventInfo, "delta">>): this;
191
+ /**
192
+ * Pans the camera by the given offset (in page coordinates). Does nothing if camera is locked.
193
+ * @param offset - The pan delta (x, y) in page coordinates.
194
+ */
195
+ pan(offset: VecLike): this;
196
+ /**
197
+ * Dispatches a pinch start event.
198
+ * @param x - Screen x coordinate. Defaults to current pointer.
199
+ * @param y - Screen y coordinate. Defaults to current pointer.
200
+ * @param z - Pinch scale/factor.
201
+ * @param dx - Delta x for pinch.
202
+ * @param dy - Delta y for pinch.
203
+ * @param dz - Delta z for pinch.
204
+ * @param options - Partial pinch event overrides.
205
+ */
206
+ pinchStart(x: number | undefined, y: number | undefined, z: number, dx: number, dy: number, dz: number, options?: Partial<Omit<TLPinchEventInfo, "delta" | "offset" | "point">>): this;
207
+ /**
208
+ * Dispatches a pinch move event (pinch_to).
209
+ * @param x - Screen x coordinate. Defaults to current pointer.
210
+ * @param y - Screen y coordinate. Defaults to current pointer.
211
+ * @param z - Pinch scale/factor.
212
+ * @param dx - Delta x for pinch.
213
+ * @param dy - Delta y for pinch.
214
+ * @param dz - Delta z for pinch.
215
+ * @param options - Partial pinch event overrides.
216
+ */
217
+ pinchTo(x: number | undefined, y: number | undefined, z: number, dx: number, dy: number, dz: number, options?: Partial<Omit<TLPinchEventInfo, "delta" | "offset" | "point">>): this;
218
+ /**
219
+ * Dispatches a pinch end event.
220
+ * @param x - Screen x coordinate. Defaults to current pointer.
221
+ * @param y - Screen y coordinate. Defaults to current pointer.
222
+ * @param z - Pinch scale/factor.
223
+ * @param dx - Delta x for pinch.
224
+ * @param dy - Delta y for pinch.
225
+ * @param dz - Delta z for pinch.
226
+ * @param options - Partial pinch event overrides.
227
+ */
228
+ pinchEnd(x: number | undefined, y: number | undefined, z: number, dx: number, dy: number, dz: number, options?: Partial<Omit<TLPinchEventInfo, "delta" | "offset" | "point">>): this;
229
+ /**
230
+ * Converts a point from page coordinates to screen coordinates.
231
+ * Pointer events operate in screen space, so page-space points must be
232
+ * converted before being passed to pointerDown/Move/Up.
233
+ */
234
+ private pageToScreen;
235
+ /**
236
+ * Simulates rotating the current selection by the given angle in radians via the rotation handle.
237
+ * @param angleRadians - Rotation angle in radians.
238
+ * @param options - Optional handle and shiftKey. handle defaults to 'top_left_rotate'.
239
+ */
240
+ rotateSelection(angleRadians: number, options?: {
241
+ handle?: RotateCorner;
242
+ shiftKey?: boolean;
243
+ }): this;
244
+ /**
245
+ * Simulates translating the current selection by the given delta in page coordinates.
246
+ * @param dx - Horizontal delta in page coordinates.
247
+ * @param dy - Vertical delta in page coordinates.
248
+ * @param options - Partial pointer event overrides (e.g. altKey for center-based scaling).
249
+ */
250
+ translateSelection(dx: number, dy: number, options?: Partial<TLPointerEventInfo>): this;
251
+ /**
252
+ * Simulates resizing the current selection via the given handle, with optional scale factors.
253
+ * @param scale - Scale factors for x and y. Defaults to `\{ scaleX: 1, scaleY: 1 \}`.
254
+ * @param handle - The selection handle to drag (e.g. 'top', 'bottom_right').
255
+ * @param options - Partial pointer event overrides (e.g. altKey to scale from center).
256
+ */
257
+ resizeSelection(scale: {
258
+ scaleX?: number | undefined;
259
+ scaleY?: number | undefined;
260
+ } | undefined, handle: SelectionHandle, options?: Partial<TLPointerEventInfo>): this;
261
+ }
262
+
263
+ /** Modifier keys for events. @public */
264
+ export declare type EventModifiers = Partial<Pick<TLPointerEventInfo, 'altKey' | 'ctrlKey' | 'shiftKey'>>;
265
+
266
+ /** Options for pointer events. Either partial pointer event info or a shape ID to target. @public */
267
+ declare type PointerEventInit_2 = Partial<TLPointerEventInfo> | TLShapeId;
268
+ export { PointerEventInit_2 as PointerEventInit }
269
+
270
+ export { }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var index_exports = {};
20
+ __export(index_exports, {
21
+ Driver: () => import_Driver.Driver
22
+ });
23
+ module.exports = __toCommonJS(index_exports);
24
+ var import_utils = require("@tldraw/utils");
25
+ var import_Driver = require("./lib/Driver");
26
+ (0, import_utils.registerTldrawLibraryVersion)(
27
+ "@tldraw/driver",
28
+ "4.5.0-canary.84ac7a331515",
29
+ "cjs"
30
+ );
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": ["import { registerTldrawLibraryVersion } from '@tldraw/utils'\n\nexport { Driver } from './lib/Driver'\nexport type { EventModifiers, PointerEventInit } from './lib/Driver'\n\nregisterTldrawLibraryVersion(\n\t(globalThis as any).TLDRAW_LIBRARY_NAME,\n\t(globalThis as any).TLDRAW_LIBRARY_VERSION,\n\t(globalThis as any).TLDRAW_LIBRARY_MODULES\n)\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA6C;AAE7C,oBAAuB;AAAA,IAGvB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AACF;",
6
+ "names": []
7
+ }