@tldraw/editor 4.3.0 → 4.4.0-canary.15cff7ea86f8
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 +1 -1
- package/dist-cjs/index.d.ts +9 -0
- package/dist-cjs/index.js +3 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/components/default-components/DefaultCanvas.js +1 -2
- package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
- package/dist-cjs/lib/config/TLUserPreferences.js +9 -3
- package/dist-cjs/lib/config/TLUserPreferences.js.map +2 -2
- package/dist-cjs/lib/editor/Editor.js +58 -6
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/derivations/notVisibleShapes.js +13 -21
- package/dist-cjs/lib/editor/derivations/notVisibleShapes.js.map +2 -2
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/RBushIndex.js +144 -0
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/RBushIndex.js.map +7 -0
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js +180 -0
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js.map +7 -0
- package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js +8 -3
- package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js.map +2 -2
- package/dist-cjs/version.js +3 -3
- package/dist-cjs/version.js.map +1 -1
- package/dist-esm/index.d.mts +9 -0
- package/dist-esm/index.mjs +3 -1
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +1 -2
- package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
- package/dist-esm/lib/config/TLUserPreferences.mjs +9 -3
- package/dist-esm/lib/config/TLUserPreferences.mjs.map +2 -2
- package/dist-esm/lib/editor/Editor.mjs +58 -6
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs +13 -21
- package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/SpatialIndexManager/RBushIndex.mjs +114 -0
- package/dist-esm/lib/editor/managers/SpatialIndexManager/RBushIndex.mjs.map +7 -0
- package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs +160 -0
- package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs.map +7 -0
- package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs +8 -3
- package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs.map +2 -2
- package/dist-esm/version.mjs +3 -3
- package/dist-esm/version.mjs.map +1 -1
- package/package.json +10 -8
- package/src/index.ts +1 -0
- package/src/lib/components/default-components/DefaultCanvas.tsx +1 -5
- package/src/lib/config/TLUserPreferences.test.ts +1 -0
- package/src/lib/config/TLUserPreferences.ts +8 -0
- package/src/lib/editor/Editor.ts +84 -6
- package/src/lib/editor/derivations/notVisibleShapes.ts +15 -41
- package/src/lib/editor/managers/SpatialIndexManager/RBushIndex.ts +144 -0
- package/src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts +214 -0
- package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.test.ts +24 -0
- package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.ts +8 -0
- package/src/version.ts +3 -3
package/src/lib/editor/Editor.ts
CHANGED
|
@@ -149,6 +149,7 @@ import { HistoryManager } from './managers/HistoryManager/HistoryManager'
|
|
|
149
149
|
import { InputsManager } from './managers/InputsManager/InputsManager'
|
|
150
150
|
import { ScribbleManager } from './managers/ScribbleManager/ScribbleManager'
|
|
151
151
|
import { SnapManager } from './managers/SnapManager/SnapManager'
|
|
152
|
+
import { SpatialIndexManager } from './managers/SpatialIndexManager/SpatialIndexManager'
|
|
152
153
|
import { TextManager } from './managers/TextManager/TextManager'
|
|
153
154
|
import { TickManager } from './managers/TickManager/TickManager'
|
|
154
155
|
import { UserPreferencesManager } from './managers/UserPreferencesManager/UserPreferencesManager'
|
|
@@ -308,6 +309,9 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
308
309
|
|
|
309
310
|
this.snaps = new SnapManager(this)
|
|
310
311
|
|
|
312
|
+
this._spatialIndex = new SpatialIndexManager(this)
|
|
313
|
+
this.disposables.add(() => this._spatialIndex.dispose())
|
|
314
|
+
|
|
311
315
|
this.disposables.add(this.timers.dispose)
|
|
312
316
|
|
|
313
317
|
this._cameraOptions.set({ ...DEFAULT_CAMERA_OPTIONS, ...cameraOptions })
|
|
@@ -895,6 +899,8 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
895
899
|
*/
|
|
896
900
|
readonly snaps: SnapManager
|
|
897
901
|
|
|
902
|
+
private readonly _spatialIndex: SpatialIndexManager
|
|
903
|
+
|
|
898
904
|
/**
|
|
899
905
|
* A manager for the any asynchronous events and making sure they're
|
|
900
906
|
* cleaned up upon disposal.
|
|
@@ -5135,6 +5141,7 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
5135
5141
|
}
|
|
5136
5142
|
|
|
5137
5143
|
private _notVisibleShapes = notVisibleShapes(this)
|
|
5144
|
+
private _culledShapesCache: Set<TLShapeId> | null = null
|
|
5138
5145
|
|
|
5139
5146
|
/**
|
|
5140
5147
|
* Get culled shapes (those that should not render), taking into account which shapes are selected or editing.
|
|
@@ -5146,16 +5153,41 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
5146
5153
|
const notVisibleShapes = this.getNotVisibleShapes()
|
|
5147
5154
|
const selectedShapeIds = this.getSelectedShapeIds()
|
|
5148
5155
|
const editingId = this.getEditingShapeId()
|
|
5149
|
-
const
|
|
5156
|
+
const nextValue = new Set<TLShapeId>(notVisibleShapes)
|
|
5150
5157
|
// we don't cull the shape we are editing
|
|
5151
5158
|
if (editingId) {
|
|
5152
|
-
|
|
5159
|
+
nextValue.delete(editingId)
|
|
5153
5160
|
}
|
|
5154
5161
|
// we also don't cull selected shapes
|
|
5155
5162
|
selectedShapeIds.forEach((id) => {
|
|
5156
|
-
|
|
5163
|
+
nextValue.delete(id)
|
|
5157
5164
|
})
|
|
5158
|
-
|
|
5165
|
+
|
|
5166
|
+
// Cache optimization: return same Set object if contents unchanged
|
|
5167
|
+
// This allows consumers to use === comparison and prevents unnecessary re-renders
|
|
5168
|
+
const prevValue = this._culledShapesCache
|
|
5169
|
+
if (prevValue) {
|
|
5170
|
+
// If sizes differ, contents must differ
|
|
5171
|
+
if (prevValue.size !== nextValue.size) {
|
|
5172
|
+
this._culledShapesCache = nextValue
|
|
5173
|
+
return nextValue
|
|
5174
|
+
}
|
|
5175
|
+
|
|
5176
|
+
// Check if all elements are the same
|
|
5177
|
+
for (const id of prevValue) {
|
|
5178
|
+
if (!nextValue.has(id)) {
|
|
5179
|
+
// Found a difference, update cache and return new set
|
|
5180
|
+
this._culledShapesCache = nextValue
|
|
5181
|
+
return nextValue
|
|
5182
|
+
}
|
|
5183
|
+
}
|
|
5184
|
+
|
|
5185
|
+
// Loop completed without finding differences - contents identical
|
|
5186
|
+
return prevValue
|
|
5187
|
+
}
|
|
5188
|
+
|
|
5189
|
+
this._culledShapesCache = nextValue
|
|
5190
|
+
return nextValue
|
|
5159
5191
|
}
|
|
5160
5192
|
|
|
5161
5193
|
/**
|
|
@@ -5222,11 +5254,18 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
5222
5254
|
let inMarginClosestToEdgeDistance = Infinity
|
|
5223
5255
|
let inMarginClosestToEdgeHit: TLShape | null = null
|
|
5224
5256
|
|
|
5257
|
+
// Use larger margin for spatial search to account for edge distance checks
|
|
5258
|
+
const searchMargin = Math.max(innerMargin, outerMargin, this.options.hitTestMargin / zoomLevel)
|
|
5259
|
+
const candidateIds = this._spatialIndex.getShapeIdsAtPoint(point, searchMargin)
|
|
5260
|
+
|
|
5225
5261
|
const shapesToCheck = (
|
|
5226
5262
|
opts.renderingOnly
|
|
5227
5263
|
? this.getCurrentPageRenderingShapesSorted()
|
|
5228
5264
|
: this.getCurrentPageShapesSorted()
|
|
5229
5265
|
).filter((shape) => {
|
|
5266
|
+
// Frames have labels positioned above the shape (outside bounds), so always include them
|
|
5267
|
+
if (!candidateIds.has(shape.id) && !this.isShapeOfType(shape, 'frame')) return false
|
|
5268
|
+
|
|
5230
5269
|
if (
|
|
5231
5270
|
(shape.isLocked && !hitLocked) ||
|
|
5232
5271
|
this.isShapeHidden(shape) ||
|
|
@@ -5412,11 +5451,41 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
5412
5451
|
point: VecLike,
|
|
5413
5452
|
opts = {} as { margin?: number; hitInside?: boolean }
|
|
5414
5453
|
): TLShape[] {
|
|
5454
|
+
const margin = opts.margin ?? 0
|
|
5455
|
+
const candidateIds = this._spatialIndex.getShapeIdsAtPoint(point, margin)
|
|
5456
|
+
|
|
5457
|
+
// Get all page shapes in z-index order and filter to candidates that pass isPointInShape
|
|
5458
|
+
// Frames are always checked because their labels can be outside their bounds
|
|
5415
5459
|
return this.getCurrentPageShapesSorted()
|
|
5416
|
-
.filter((shape) =>
|
|
5460
|
+
.filter((shape) => {
|
|
5461
|
+
if (this.isShapeHidden(shape)) return false
|
|
5462
|
+
if (!candidateIds.has(shape.id) && !this.isShapeOfType(shape, 'frame')) return false
|
|
5463
|
+
return this.isPointInShape(shape, point, opts)
|
|
5464
|
+
})
|
|
5417
5465
|
.reverse()
|
|
5418
5466
|
}
|
|
5419
5467
|
|
|
5468
|
+
/**
|
|
5469
|
+
* Get shape IDs within the given bounds.
|
|
5470
|
+
*
|
|
5471
|
+
* Note: Uses shape page bounds only. Frames with labels outside their bounds
|
|
5472
|
+
* may not be included even if the label is within the search bounds.
|
|
5473
|
+
*
|
|
5474
|
+
* Note: Results are unordered. If you need z-order, combine with sorted shapes:
|
|
5475
|
+
* ```ts
|
|
5476
|
+
* const candidates = editor.getShapeIdsInsideBounds(bounds)
|
|
5477
|
+
* const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))
|
|
5478
|
+
* ```
|
|
5479
|
+
*
|
|
5480
|
+
* @param bounds - The bounds to search within.
|
|
5481
|
+
* @returns Unordered set of shape IDs within the given bounds.
|
|
5482
|
+
*
|
|
5483
|
+
* @internal
|
|
5484
|
+
*/
|
|
5485
|
+
getShapeIdsInsideBounds(bounds: Box): Set<TLShapeId> {
|
|
5486
|
+
return this._spatialIndex.getShapeIdsInsideBounds(bounds)
|
|
5487
|
+
}
|
|
5488
|
+
|
|
5420
5489
|
/**
|
|
5421
5490
|
* Test whether a point (in the current page space) will will a shape. This method takes into account masks,
|
|
5422
5491
|
* such as when a shape is the child of a frame and is partially clipped by the frame.
|
|
@@ -10475,7 +10544,16 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
10475
10544
|
}
|
|
10476
10545
|
}
|
|
10477
10546
|
|
|
10478
|
-
|
|
10547
|
+
// because we can't for sure detect whether a user is using a mouse or a trackpad,
|
|
10548
|
+
// we need to check the input mode preference, and only invert the zoom direction
|
|
10549
|
+
// if the user has specifically set it to a mouse.
|
|
10550
|
+
const isZoomDirectionInverted =
|
|
10551
|
+
(this.user.getUserPreferences().isZoomDirectionInverted && inputMode === 'mouse') ??
|
|
10552
|
+
false
|
|
10553
|
+
const deltaValue = delta ?? 0
|
|
10554
|
+
const finalDelta = isZoomDirectionInverted ? -deltaValue : deltaValue
|
|
10555
|
+
|
|
10556
|
+
const zoom = cz + finalDelta * zoomSpeed * cz
|
|
10479
10557
|
this._setCamera(new Vec(cx + x / zoom - x / cz, cy + y / zoom - y / cz, zoom), {
|
|
10480
10558
|
immediate: true,
|
|
10481
10559
|
})
|
|
@@ -9,60 +9,34 @@ import { Editor } from '../Editor'
|
|
|
9
9
|
* @returns Incremental derivation of non visible shapes.
|
|
10
10
|
*/
|
|
11
11
|
export function notVisibleShapes(editor: Editor) {
|
|
12
|
-
return computed<Set<TLShapeId>>('notVisibleShapes', function
|
|
13
|
-
const
|
|
14
|
-
const nextValue = new Set<TLShapeId>()
|
|
15
|
-
|
|
16
|
-
// Extract viewport bounds once to avoid repeated property access
|
|
12
|
+
return computed<Set<TLShapeId>>('notVisibleShapes', function (prevValue) {
|
|
13
|
+
const allShapeIds = editor.getCurrentPageShapeIds()
|
|
17
14
|
const viewportPageBounds = editor.getViewportPageBounds()
|
|
18
|
-
const
|
|
19
|
-
const viewMinY = viewportPageBounds.minY
|
|
20
|
-
const viewMaxX = viewportPageBounds.maxX
|
|
21
|
-
const viewMaxY = viewportPageBounds.maxY
|
|
15
|
+
const visibleIds = editor.getShapeIdsInsideBounds(viewportPageBounds)
|
|
22
16
|
|
|
23
|
-
|
|
24
|
-
const pageBounds = editor.getShapePageBounds(id)
|
|
25
|
-
|
|
26
|
-
// Hybrid check: if bounds exist and shape overlaps viewport, it's visible.
|
|
27
|
-
// This inlines Box.Collides to avoid function call overhead and the
|
|
28
|
-
// redundant Contains check that Box.Includes was doing.
|
|
29
|
-
if (
|
|
30
|
-
pageBounds !== undefined &&
|
|
31
|
-
pageBounds.maxX >= viewMinX &&
|
|
32
|
-
pageBounds.minX <= viewMaxX &&
|
|
33
|
-
pageBounds.maxY >= viewMinY &&
|
|
34
|
-
pageBounds.minY <= viewMaxY
|
|
35
|
-
) {
|
|
36
|
-
continue
|
|
37
|
-
}
|
|
17
|
+
const nextValue = new Set<TLShapeId>()
|
|
38
18
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
19
|
+
// Non-visible shapes are all shapes minus visible shapes
|
|
20
|
+
for (const id of allShapeIds) {
|
|
21
|
+
if (!visibleIds.has(id)) {
|
|
22
|
+
const shape = editor.getShape(id)
|
|
23
|
+
if (!shape) continue
|
|
44
24
|
|
|
45
|
-
|
|
46
|
-
|
|
25
|
+
const canCull = editor.getShapeUtil(shape.type).canCull(shape)
|
|
26
|
+
if (!canCull) continue
|
|
47
27
|
|
|
48
|
-
|
|
28
|
+
nextValue.add(id)
|
|
29
|
+
}
|
|
49
30
|
}
|
|
50
31
|
|
|
51
|
-
if (isUninitialized(prevValue)) {
|
|
32
|
+
if (isUninitialized(prevValue) || prevValue.size !== nextValue.size) {
|
|
52
33
|
return nextValue
|
|
53
34
|
}
|
|
54
35
|
|
|
55
|
-
// If there are more or less shapes, we know there's a change
|
|
56
|
-
if (prevValue.size !== nextValue.size) return nextValue
|
|
57
|
-
|
|
58
|
-
// If any of the old shapes are not in the new set, we know there's a change
|
|
59
36
|
for (const prev of prevValue) {
|
|
60
|
-
if (!nextValue.has(prev))
|
|
61
|
-
return nextValue
|
|
62
|
-
}
|
|
37
|
+
if (!nextValue.has(prev)) return nextValue
|
|
63
38
|
}
|
|
64
39
|
|
|
65
|
-
// If we've made it here, we know that the set is the same
|
|
66
40
|
return prevValue
|
|
67
41
|
})
|
|
68
42
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { TLShapeId } from '@tldraw/tlschema'
|
|
2
|
+
import RBush from 'rbush'
|
|
3
|
+
import { Box } from '../../../primitives/Box'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Element stored in the R-tree spatial index.
|
|
7
|
+
* Contains bounds (minX, minY, maxX, maxY) and shape ID.
|
|
8
|
+
*/
|
|
9
|
+
export interface SpatialElement {
|
|
10
|
+
minX: number
|
|
11
|
+
minY: number
|
|
12
|
+
maxX: number
|
|
13
|
+
maxY: number
|
|
14
|
+
id: TLShapeId
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Custom RBush class for tldraw shapes.
|
|
19
|
+
*/
|
|
20
|
+
class TldrawRBush extends RBush<SpatialElement> {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Wrapper around RBush R-tree for efficient spatial queries.
|
|
24
|
+
* Maintains a map of elements currently in the tree for efficient updates.
|
|
25
|
+
*/
|
|
26
|
+
export class RBushIndex {
|
|
27
|
+
private rBush: TldrawRBush
|
|
28
|
+
private elementsInTree: Map<TLShapeId, SpatialElement>
|
|
29
|
+
|
|
30
|
+
constructor() {
|
|
31
|
+
this.rBush = new TldrawRBush()
|
|
32
|
+
this.elementsInTree = new Map()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Search for shapes within the given bounds.
|
|
37
|
+
* Returns set of shape IDs that intersect with the bounds.
|
|
38
|
+
*/
|
|
39
|
+
search(bounds: Box): Set<TLShapeId> {
|
|
40
|
+
const results = this.rBush.search({
|
|
41
|
+
minX: bounds.minX,
|
|
42
|
+
minY: bounds.minY,
|
|
43
|
+
maxX: bounds.maxX,
|
|
44
|
+
maxY: bounds.maxY,
|
|
45
|
+
})
|
|
46
|
+
return new Set(results.map((e: SpatialElement) => e.id))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Insert or update a shape in the spatial index.
|
|
51
|
+
* If the shape already exists, it will be removed first to prevent duplicates.
|
|
52
|
+
*/
|
|
53
|
+
upsert(id: TLShapeId, bounds: Box): void {
|
|
54
|
+
// Remove existing entry to prevent map-tree desync
|
|
55
|
+
const existing = this.elementsInTree.get(id)
|
|
56
|
+
if (existing) {
|
|
57
|
+
this.rBush.remove(existing)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const element: SpatialElement = {
|
|
61
|
+
minX: bounds.minX,
|
|
62
|
+
minY: bounds.minY,
|
|
63
|
+
maxX: bounds.maxX,
|
|
64
|
+
maxY: bounds.maxY,
|
|
65
|
+
id,
|
|
66
|
+
}
|
|
67
|
+
this.rBush.insert(element)
|
|
68
|
+
this.elementsInTree.set(id, element)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Remove a shape from the spatial index.
|
|
73
|
+
*/
|
|
74
|
+
remove(id: TLShapeId): void {
|
|
75
|
+
const element = this.elementsInTree.get(id)
|
|
76
|
+
if (element) {
|
|
77
|
+
this.rBush.remove(element)
|
|
78
|
+
this.elementsInTree.delete(id)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Bulk load elements into the spatial index.
|
|
84
|
+
* More efficient than individual inserts for initial loading.
|
|
85
|
+
*/
|
|
86
|
+
bulkLoad(elements: SpatialElement[]): void {
|
|
87
|
+
this.rBush.load(elements)
|
|
88
|
+
for (const element of elements) {
|
|
89
|
+
this.elementsInTree.set(element.id, element)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Clear all elements from the spatial index.
|
|
95
|
+
*/
|
|
96
|
+
clear(): void {
|
|
97
|
+
this.rBush.clear()
|
|
98
|
+
this.elementsInTree.clear()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Check if a shape is in the spatial index.
|
|
103
|
+
*/
|
|
104
|
+
has(id: TLShapeId): boolean {
|
|
105
|
+
return this.elementsInTree.has(id)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the number of elements in the spatial index.
|
|
110
|
+
*/
|
|
111
|
+
getSize(): number {
|
|
112
|
+
return this.elementsInTree.size
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Get all shape IDs currently in the spatial index.
|
|
117
|
+
*/
|
|
118
|
+
getAllShapeIds(): TLShapeId[] {
|
|
119
|
+
return Array.from(this.elementsInTree.keys())
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Get the bounds currently stored in the spatial index for a shape.
|
|
124
|
+
* Returns undefined if the shape is not in the index.
|
|
125
|
+
*/
|
|
126
|
+
getBounds(id: TLShapeId): Box | undefined {
|
|
127
|
+
const element = this.elementsInTree.get(id)
|
|
128
|
+
if (!element) return undefined
|
|
129
|
+
return new Box(
|
|
130
|
+
element.minX,
|
|
131
|
+
element.minY,
|
|
132
|
+
element.maxX - element.minX,
|
|
133
|
+
element.maxY - element.minY
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Dispose of the spatial index.
|
|
139
|
+
* Clears all data structures to prevent memory leaks.
|
|
140
|
+
*/
|
|
141
|
+
dispose(): void {
|
|
142
|
+
this.clear()
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { Computed, RESET_VALUE, computed, isUninitialized } from '@tldraw/state'
|
|
2
|
+
import type { RecordsDiff } from '@tldraw/store'
|
|
3
|
+
import type { TLRecord } from '@tldraw/tlschema'
|
|
4
|
+
import { TLPageId, TLShape, TLShapeId, isShape } from '@tldraw/tlschema'
|
|
5
|
+
import { objectMapValues } from '@tldraw/utils'
|
|
6
|
+
import { Box } from '../../../primitives/Box'
|
|
7
|
+
import type { Editor } from '../../Editor'
|
|
8
|
+
import { RBushIndex, type SpatialElement } from './RBushIndex'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Manages spatial indexing for efficient shape location queries.
|
|
12
|
+
*
|
|
13
|
+
* Uses an R-tree (via RBush) to enable O(log n) spatial queries instead of O(n) iteration.
|
|
14
|
+
* Handles shapes with computed bounds (arrows, groups, custom shapes) by checking all shapes'
|
|
15
|
+
* bounds on each update using the reactive bounds cache.
|
|
16
|
+
*
|
|
17
|
+
* Key features:
|
|
18
|
+
* - Incremental updates using filterHistory pattern
|
|
19
|
+
* - Leverages existing bounds cache reactivity for dependency tracking
|
|
20
|
+
* - Works with any custom shape type with computed bounds
|
|
21
|
+
* - Per-page index (rebuilds on page change)
|
|
22
|
+
* - Optimized for viewport culling queries
|
|
23
|
+
*
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export class SpatialIndexManager {
|
|
27
|
+
private rbush: RBushIndex
|
|
28
|
+
private spatialIndexComputed: Computed<number>
|
|
29
|
+
private lastPageId: TLPageId | null = null
|
|
30
|
+
|
|
31
|
+
constructor(public readonly editor: Editor) {
|
|
32
|
+
this.rbush = new RBushIndex()
|
|
33
|
+
this.spatialIndexComputed = this.createSpatialIndexComputed()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private createSpatialIndexComputed() {
|
|
37
|
+
const shapeHistory = this.editor.store.query.filterHistory('shape')
|
|
38
|
+
|
|
39
|
+
return computed<number>('spatialIndex', (_prevValue, lastComputedEpoch) => {
|
|
40
|
+
if (isUninitialized(_prevValue)) {
|
|
41
|
+
return this.buildFromScratch(lastComputedEpoch)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const shapeDiff = shapeHistory.getDiffSince(lastComputedEpoch)
|
|
45
|
+
|
|
46
|
+
if (shapeDiff === RESET_VALUE) {
|
|
47
|
+
return this.buildFromScratch(lastComputedEpoch)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const currentPageId = this.editor.getCurrentPageId()
|
|
51
|
+
if (this.lastPageId !== currentPageId) {
|
|
52
|
+
return this.buildFromScratch(lastComputedEpoch)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// No shape changes - index is already up to date
|
|
56
|
+
if (shapeDiff.length === 0) {
|
|
57
|
+
return lastComputedEpoch
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Process incremental updates
|
|
61
|
+
this.processIncrementalUpdate(shapeDiff)
|
|
62
|
+
|
|
63
|
+
return lastComputedEpoch
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private buildFromScratch(epoch: number): number {
|
|
68
|
+
this.rbush.clear()
|
|
69
|
+
this.lastPageId = this.editor.getCurrentPageId()
|
|
70
|
+
|
|
71
|
+
const shapes = this.editor.getCurrentPageShapes()
|
|
72
|
+
const elements: SpatialElement[] = []
|
|
73
|
+
|
|
74
|
+
// Collect all shape elements for bulk loading
|
|
75
|
+
for (const shape of shapes) {
|
|
76
|
+
const bounds = this.editor.getShapePageBounds(shape.id)
|
|
77
|
+
if (bounds) {
|
|
78
|
+
elements.push({
|
|
79
|
+
minX: bounds.minX,
|
|
80
|
+
minY: bounds.minY,
|
|
81
|
+
maxX: bounds.maxX,
|
|
82
|
+
maxY: bounds.maxY,
|
|
83
|
+
id: shape.id,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Bulk load for efficiency
|
|
89
|
+
this.rbush.bulkLoad(elements)
|
|
90
|
+
|
|
91
|
+
return epoch
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private processIncrementalUpdate(shapeDiff: RecordsDiff<TLRecord>[]): void {
|
|
95
|
+
// Track shapes we've already processed from the diff
|
|
96
|
+
const processedShapeIds = new Set<TLShapeId>()
|
|
97
|
+
|
|
98
|
+
// 1. Process shape additions, removals, and updates from diff
|
|
99
|
+
for (const changes of shapeDiff) {
|
|
100
|
+
// Handle additions (only for shapes on current page)
|
|
101
|
+
for (const shape of objectMapValues(changes.added) as TLShape[]) {
|
|
102
|
+
if (isShape(shape) && this.editor.getAncestorPageId(shape) === this.lastPageId) {
|
|
103
|
+
const bounds = this.editor.getShapePageBounds(shape.id)
|
|
104
|
+
if (bounds) {
|
|
105
|
+
this.rbush.upsert(shape.id, bounds)
|
|
106
|
+
}
|
|
107
|
+
processedShapeIds.add(shape.id)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Handle removals
|
|
112
|
+
for (const shape of objectMapValues(changes.removed) as TLShape[]) {
|
|
113
|
+
if (isShape(shape)) {
|
|
114
|
+
this.rbush.remove(shape.id)
|
|
115
|
+
processedShapeIds.add(shape.id)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Handle updated shapes: page changes and bounds updates
|
|
120
|
+
for (const [, to] of objectMapValues(changes.updated) as [TLShape, TLShape][]) {
|
|
121
|
+
if (!isShape(to)) continue
|
|
122
|
+
processedShapeIds.add(to.id)
|
|
123
|
+
|
|
124
|
+
const isOnPage = this.editor.getAncestorPageId(to) === this.lastPageId
|
|
125
|
+
|
|
126
|
+
if (isOnPage) {
|
|
127
|
+
const bounds = this.editor.getShapePageBounds(to.id)
|
|
128
|
+
if (bounds) {
|
|
129
|
+
this.rbush.upsert(to.id, bounds)
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
this.rbush.remove(to.id)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 2. Check remaining shapes in index for bounds changes
|
|
138
|
+
// This handles shapes with computed bounds (arrows bound to moved shapes, groups with moved children, etc.)
|
|
139
|
+
const allShapeIds = this.rbush.getAllShapeIds()
|
|
140
|
+
|
|
141
|
+
for (const shapeId of allShapeIds) {
|
|
142
|
+
if (processedShapeIds.has(shapeId)) continue
|
|
143
|
+
|
|
144
|
+
const currentBounds = this.editor.getShapePageBounds(shapeId)
|
|
145
|
+
const indexedBounds = this.rbush.getBounds(shapeId)
|
|
146
|
+
|
|
147
|
+
if (!this.areBoundsEqual(currentBounds, indexedBounds)) {
|
|
148
|
+
if (currentBounds) {
|
|
149
|
+
this.rbush.upsert(shapeId, currentBounds)
|
|
150
|
+
} else {
|
|
151
|
+
this.rbush.remove(shapeId)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private areBoundsEqual(a: Box | undefined, b: Box | undefined): boolean {
|
|
158
|
+
if (!a && !b) return true
|
|
159
|
+
if (!a || !b) return false
|
|
160
|
+
return a.minX === b.minX && a.minY === b.minY && a.maxX === b.maxX && a.maxY === b.maxY
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get shape IDs within the given bounds.
|
|
165
|
+
* Optimized for viewport culling queries.
|
|
166
|
+
*
|
|
167
|
+
* Note: Results are unordered. If you need z-order, combine with sorted shapes:
|
|
168
|
+
* ```ts
|
|
169
|
+
* const candidates = editor.spatialIndex.getShapeIdsInsideBounds(bounds)
|
|
170
|
+
* const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @param bounds - The bounds to search within
|
|
174
|
+
* @returns Unordered set of shape IDs within the bounds
|
|
175
|
+
*
|
|
176
|
+
* @public
|
|
177
|
+
*/
|
|
178
|
+
getShapeIdsInsideBounds(bounds: Box): Set<TLShapeId> {
|
|
179
|
+
this.spatialIndexComputed.get()
|
|
180
|
+
return this.rbush.search(bounds)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get shape IDs at a point (with optional margin).
|
|
185
|
+
* Creates a small bounding box around the point and searches the spatial index.
|
|
186
|
+
*
|
|
187
|
+
* Note: Results are unordered. If you need z-order, combine with sorted shapes:
|
|
188
|
+
* ```ts
|
|
189
|
+
* const candidates = editor.spatialIndex.getShapeIdsAtPoint(point, margin)
|
|
190
|
+
* const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @param point - The point to search at
|
|
194
|
+
* @param margin - The margin around the point to search (default: 0)
|
|
195
|
+
* @returns Unordered set of shape IDs that could potentially contain the point
|
|
196
|
+
*
|
|
197
|
+
* @public
|
|
198
|
+
*/
|
|
199
|
+
getShapeIdsAtPoint(point: { x: number; y: number }, margin = 0): Set<TLShapeId> {
|
|
200
|
+
this.spatialIndexComputed.get()
|
|
201
|
+
return this.rbush.search(new Box(point.x - margin, point.y - margin, margin * 2, margin * 2))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Dispose of the spatial index manager.
|
|
206
|
+
* Clears the R-tree to prevent memory leaks.
|
|
207
|
+
*
|
|
208
|
+
* @public
|
|
209
|
+
*/
|
|
210
|
+
dispose(): void {
|
|
211
|
+
this.rbush.dispose()
|
|
212
|
+
this.lastPageId = null
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -31,6 +31,7 @@ describe('UserPreferencesManager', () => {
|
|
|
31
31
|
isDynamicSizeMode: false,
|
|
32
32
|
isPasteAtCursorMode: false,
|
|
33
33
|
inputMode: null,
|
|
34
|
+
isZoomDirectionInverted: false,
|
|
34
35
|
...overrides,
|
|
35
36
|
})
|
|
36
37
|
|
|
@@ -235,6 +236,7 @@ describe('UserPreferencesManager', () => {
|
|
|
235
236
|
isWrapMode: mockUserPreferences.isWrapMode,
|
|
236
237
|
isDynamicResizeMode: mockUserPreferences.isDynamicSizeMode,
|
|
237
238
|
inputMode: mockUserPreferences.inputMode,
|
|
239
|
+
isZoomDirectionInverted: mockUserPreferences.isZoomDirectionInverted,
|
|
238
240
|
})
|
|
239
241
|
})
|
|
240
242
|
|
|
@@ -475,6 +477,24 @@ describe('UserPreferencesManager', () => {
|
|
|
475
477
|
expect(userPreferencesManager.getInputMode()).toBe('mouse')
|
|
476
478
|
})
|
|
477
479
|
})
|
|
480
|
+
|
|
481
|
+
describe('getIsZoomDirectionInverted', () => {
|
|
482
|
+
it('should return user zoom direction inverted setting', () => {
|
|
483
|
+
expect(userPreferencesManager.getIsZoomDirectionInverted()).toBe(false)
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
it('should return default when null', () => {
|
|
487
|
+
userPreferencesAtom.set({ ...mockUserPreferences, isZoomDirectionInverted: null })
|
|
488
|
+
expect(userPreferencesManager.getIsZoomDirectionInverted()).toBe(
|
|
489
|
+
defaultUserPreferences.isZoomDirectionInverted
|
|
490
|
+
)
|
|
491
|
+
})
|
|
492
|
+
|
|
493
|
+
it('should return true when inverted', () => {
|
|
494
|
+
userPreferencesAtom.set({ ...mockUserPreferences, isZoomDirectionInverted: true })
|
|
495
|
+
expect(userPreferencesManager.getIsZoomDirectionInverted()).toBe(true)
|
|
496
|
+
})
|
|
497
|
+
})
|
|
478
498
|
})
|
|
479
499
|
|
|
480
500
|
describe('reactive behavior', () => {
|
|
@@ -536,6 +556,7 @@ describe('UserPreferencesManager', () => {
|
|
|
536
556
|
isWrapMode: null,
|
|
537
557
|
isDynamicSizeMode: null,
|
|
538
558
|
isPasteAtCursorMode: null,
|
|
559
|
+
isZoomDirectionInverted: null,
|
|
539
560
|
})
|
|
540
561
|
|
|
541
562
|
userPreferencesAtom.set(nullPrefs)
|
|
@@ -558,6 +579,9 @@ describe('UserPreferencesManager', () => {
|
|
|
558
579
|
expect(userPreferencesManager.getIsPasteAtCursorMode()).toBe(
|
|
559
580
|
defaultUserPreferences.isPasteAtCursorMode
|
|
560
581
|
)
|
|
582
|
+
expect(userPreferencesManager.getIsZoomDirectionInverted()).toBe(
|
|
583
|
+
defaultUserPreferences.isZoomDirectionInverted
|
|
584
|
+
)
|
|
561
585
|
})
|
|
562
586
|
|
|
563
587
|
it('should handle matchMedia with null response', () => {
|
|
@@ -51,6 +51,7 @@ export class UserPreferencesManager {
|
|
|
51
51
|
isDynamicResizeMode: this.getIsDynamicResizeMode(),
|
|
52
52
|
enhancedA11yMode: this.getEnhancedA11yMode(),
|
|
53
53
|
inputMode: this.getInputMode(),
|
|
54
|
+
isZoomDirectionInverted: this.getIsZoomDirectionInverted(),
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -131,4 +132,11 @@ export class UserPreferencesManager {
|
|
|
131
132
|
@computed getInputMode() {
|
|
132
133
|
return this.user.userPreferences.get().inputMode ?? defaultUserPreferences.inputMode
|
|
133
134
|
}
|
|
135
|
+
|
|
136
|
+
@computed getIsZoomDirectionInverted() {
|
|
137
|
+
return (
|
|
138
|
+
this.user.userPreferences.get().isZoomDirectionInverted ??
|
|
139
|
+
defaultUserPreferences.isZoomDirectionInverted
|
|
140
|
+
)
|
|
141
|
+
}
|
|
134
142
|
}
|
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 = '4.
|
|
4
|
+
export const version = '4.4.0-canary.15cff7ea86f8'
|
|
5
5
|
export const publishDates = {
|
|
6
6
|
major: '2025-09-18T14:39:22.803Z',
|
|
7
|
-
minor: '2026-01-
|
|
8
|
-
patch: '2026-01-
|
|
7
|
+
minor: '2026-01-26T14:52:43.591Z',
|
|
8
|
+
patch: '2026-01-26T14:52:43.591Z',
|
|
9
9
|
}
|