@tldraw/editor 4.3.0 → 4.4.0-canary.1e3b436e33e4

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 (39) hide show
  1. package/README.md +1 -1
  2. package/dist-cjs/index.d.ts +5 -0
  3. package/dist-cjs/index.js +3 -1
  4. package/dist-cjs/index.js.map +2 -2
  5. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +1 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  7. package/dist-cjs/lib/editor/Editor.js +54 -5
  8. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  9. package/dist-cjs/lib/editor/derivations/notVisibleShapes.js +13 -21
  10. package/dist-cjs/lib/editor/derivations/notVisibleShapes.js.map +2 -2
  11. package/dist-cjs/lib/editor/managers/SpatialIndexManager/RBushIndex.js +144 -0
  12. package/dist-cjs/lib/editor/managers/SpatialIndexManager/RBushIndex.js.map +7 -0
  13. package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js +181 -0
  14. package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js.map +7 -0
  15. package/dist-cjs/version.js +3 -3
  16. package/dist-cjs/version.js.map +1 -1
  17. package/dist-esm/index.d.mts +5 -0
  18. package/dist-esm/index.mjs +3 -1
  19. package/dist-esm/index.mjs.map +2 -2
  20. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +1 -2
  21. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  22. package/dist-esm/lib/editor/Editor.mjs +54 -5
  23. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  24. package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs +13 -21
  25. package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs.map +2 -2
  26. package/dist-esm/lib/editor/managers/SpatialIndexManager/RBushIndex.mjs +114 -0
  27. package/dist-esm/lib/editor/managers/SpatialIndexManager/RBushIndex.mjs.map +7 -0
  28. package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs +161 -0
  29. package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs.map +7 -0
  30. package/dist-esm/version.mjs +3 -3
  31. package/dist-esm/version.mjs.map +1 -1
  32. package/package.json +10 -8
  33. package/src/index.ts +1 -0
  34. package/src/lib/components/default-components/DefaultCanvas.tsx +1 -5
  35. package/src/lib/editor/Editor.ts +74 -5
  36. package/src/lib/editor/derivations/notVisibleShapes.ts +15 -41
  37. package/src/lib/editor/managers/SpatialIndexManager/RBushIndex.ts +144 -0
  38. package/src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts +215 -0
  39. package/src/version.ts +3 -3
@@ -1,32 +1,24 @@
1
1
  import { computed, isUninitialized } from "@tldraw/state";
2
2
  function notVisibleShapes(editor) {
3
- return computed("notVisibleShapes", function updateNotVisibleShapes(prevValue) {
4
- const shapeIds = editor.getCurrentPageShapeIds();
5
- const nextValue = /* @__PURE__ */ new Set();
3
+ return computed("notVisibleShapes", function(prevValue) {
4
+ const allShapeIds = editor.getCurrentPageShapeIds();
6
5
  const viewportPageBounds = editor.getViewportPageBounds();
7
- const viewMinX = viewportPageBounds.minX;
8
- const viewMinY = viewportPageBounds.minY;
9
- const viewMaxX = viewportPageBounds.maxX;
10
- const viewMaxY = viewportPageBounds.maxY;
11
- for (const id of shapeIds) {
12
- const pageBounds = editor.getShapePageBounds(id);
13
- if (pageBounds !== void 0 && pageBounds.maxX >= viewMinX && pageBounds.minX <= viewMaxX && pageBounds.maxY >= viewMinY && pageBounds.minY <= viewMaxY) {
14
- continue;
6
+ const visibleIds = editor.getShapeIdsInsideBounds(viewportPageBounds);
7
+ const nextValue = /* @__PURE__ */ new Set();
8
+ for (const id of allShapeIds) {
9
+ if (!visibleIds.has(id)) {
10
+ const shape = editor.getShape(id);
11
+ if (!shape) continue;
12
+ const canCull = editor.getShapeUtil(shape.type).canCull(shape);
13
+ if (!canCull) continue;
14
+ nextValue.add(id);
15
15
  }
16
- const shape = editor.getShape(id);
17
- if (!shape) continue;
18
- const canCull = editor.getShapeUtil(shape.type).canCull(shape);
19
- if (!canCull) continue;
20
- nextValue.add(id);
21
16
  }
22
- if (isUninitialized(prevValue)) {
17
+ if (isUninitialized(prevValue) || prevValue.size !== nextValue.size) {
23
18
  return nextValue;
24
19
  }
25
- if (prevValue.size !== nextValue.size) return nextValue;
26
20
  for (const prev of prevValue) {
27
- if (!nextValue.has(prev)) {
28
- return nextValue;
29
- }
21
+ if (!nextValue.has(prev)) return nextValue;
30
22
  }
31
23
  return prevValue;
32
24
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/lib/editor/derivations/notVisibleShapes.ts"],
4
- "sourcesContent": ["import { computed, isUninitialized } from '@tldraw/state'\nimport { TLShapeId } from '@tldraw/tlschema'\nimport { Editor } from '../Editor'\n\n/**\n * Non visible shapes are shapes outside of the viewport page bounds.\n *\n * @param editor - Instance of the tldraw Editor.\n * @returns Incremental derivation of non visible shapes.\n */\nexport function notVisibleShapes(editor: Editor) {\n\treturn computed<Set<TLShapeId>>('notVisibleShapes', function updateNotVisibleShapes(prevValue) {\n\t\tconst shapeIds = editor.getCurrentPageShapeIds()\n\t\tconst nextValue = new Set<TLShapeId>()\n\n\t\t// Extract viewport bounds once to avoid repeated property access\n\t\tconst viewportPageBounds = editor.getViewportPageBounds()\n\t\tconst viewMinX = viewportPageBounds.minX\n\t\tconst viewMinY = viewportPageBounds.minY\n\t\tconst viewMaxX = viewportPageBounds.maxX\n\t\tconst viewMaxY = viewportPageBounds.maxY\n\n\t\tfor (const id of shapeIds) {\n\t\t\tconst pageBounds = editor.getShapePageBounds(id)\n\n\t\t\t// Hybrid check: if bounds exist and shape overlaps viewport, it's visible.\n\t\t\t// This inlines Box.Collides to avoid function call overhead and the\n\t\t\t// redundant Contains check that Box.Includes was doing.\n\t\t\tif (\n\t\t\t\tpageBounds !== undefined &&\n\t\t\t\tpageBounds.maxX >= viewMinX &&\n\t\t\t\tpageBounds.minX <= viewMaxX &&\n\t\t\t\tpageBounds.maxY >= viewMinY &&\n\t\t\t\tpageBounds.minY <= viewMaxY\n\t\t\t) {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// Shape is outside viewport or has no bounds - check if it can be culled.\n\t\t\t// We defer getShape and canCull checks until here since most shapes are\n\t\t\t// typically visible and we can skip these calls for them.\n\t\t\tconst shape = editor.getShape(id)\n\t\t\tif (!shape) continue\n\n\t\t\tconst canCull = editor.getShapeUtil(shape.type).canCull(shape)\n\t\t\tif (!canCull) continue\n\n\t\t\tnextValue.add(id)\n\t\t}\n\n\t\tif (isUninitialized(prevValue)) {\n\t\t\treturn nextValue\n\t\t}\n\n\t\t// If there are more or less shapes, we know there's a change\n\t\tif (prevValue.size !== nextValue.size) return nextValue\n\n\t\t// If any of the old shapes are not in the new set, we know there's a change\n\t\tfor (const prev of prevValue) {\n\t\t\tif (!nextValue.has(prev)) {\n\t\t\t\treturn nextValue\n\t\t\t}\n\t\t}\n\n\t\t// If we've made it here, we know that the set is the same\n\t\treturn prevValue\n\t})\n}\n"],
5
- "mappings": "AAAA,SAAS,UAAU,uBAAuB;AAUnC,SAAS,iBAAiB,QAAgB;AAChD,SAAO,SAAyB,oBAAoB,SAAS,uBAAuB,WAAW;AAC9F,UAAM,WAAW,OAAO,uBAAuB;AAC/C,UAAM,YAAY,oBAAI,IAAe;AAGrC,UAAM,qBAAqB,OAAO,sBAAsB;AACxD,UAAM,WAAW,mBAAmB;AACpC,UAAM,WAAW,mBAAmB;AACpC,UAAM,WAAW,mBAAmB;AACpC,UAAM,WAAW,mBAAmB;AAEpC,eAAW,MAAM,UAAU;AAC1B,YAAM,aAAa,OAAO,mBAAmB,EAAE;AAK/C,UACC,eAAe,UACf,WAAW,QAAQ,YACnB,WAAW,QAAQ,YACnB,WAAW,QAAQ,YACnB,WAAW,QAAQ,UAClB;AACD;AAAA,MACD;AAKA,YAAM,QAAQ,OAAO,SAAS,EAAE;AAChC,UAAI,CAAC,MAAO;AAEZ,YAAM,UAAU,OAAO,aAAa,MAAM,IAAI,EAAE,QAAQ,KAAK;AAC7D,UAAI,CAAC,QAAS;AAEd,gBAAU,IAAI,EAAE;AAAA,IACjB;AAEA,QAAI,gBAAgB,SAAS,GAAG;AAC/B,aAAO;AAAA,IACR;AAGA,QAAI,UAAU,SAAS,UAAU,KAAM,QAAO;AAG9C,eAAW,QAAQ,WAAW;AAC7B,UAAI,CAAC,UAAU,IAAI,IAAI,GAAG;AACzB,eAAO;AAAA,MACR;AAAA,IACD;AAGA,WAAO;AAAA,EACR,CAAC;AACF;",
4
+ "sourcesContent": ["import { computed, isUninitialized } from '@tldraw/state'\nimport { TLShapeId } from '@tldraw/tlschema'\nimport { Editor } from '../Editor'\n\n/**\n * Non visible shapes are shapes outside of the viewport page bounds.\n *\n * @param editor - Instance of the tldraw Editor.\n * @returns Incremental derivation of non visible shapes.\n */\nexport function notVisibleShapes(editor: Editor) {\n\treturn computed<Set<TLShapeId>>('notVisibleShapes', function (prevValue) {\n\t\tconst allShapeIds = editor.getCurrentPageShapeIds()\n\t\tconst viewportPageBounds = editor.getViewportPageBounds()\n\t\tconst visibleIds = editor.getShapeIdsInsideBounds(viewportPageBounds)\n\n\t\tconst nextValue = new Set<TLShapeId>()\n\n\t\t// Non-visible shapes are all shapes minus visible shapes\n\t\tfor (const id of allShapeIds) {\n\t\t\tif (!visibleIds.has(id)) {\n\t\t\t\tconst shape = editor.getShape(id)\n\t\t\t\tif (!shape) continue\n\n\t\t\t\tconst canCull = editor.getShapeUtil(shape.type).canCull(shape)\n\t\t\t\tif (!canCull) continue\n\n\t\t\t\tnextValue.add(id)\n\t\t\t}\n\t\t}\n\n\t\tif (isUninitialized(prevValue) || prevValue.size !== nextValue.size) {\n\t\t\treturn nextValue\n\t\t}\n\n\t\tfor (const prev of prevValue) {\n\t\t\tif (!nextValue.has(prev)) return nextValue\n\t\t}\n\n\t\treturn prevValue\n\t})\n}\n"],
5
+ "mappings": "AAAA,SAAS,UAAU,uBAAuB;AAUnC,SAAS,iBAAiB,QAAgB;AAChD,SAAO,SAAyB,oBAAoB,SAAU,WAAW;AACxE,UAAM,cAAc,OAAO,uBAAuB;AAClD,UAAM,qBAAqB,OAAO,sBAAsB;AACxD,UAAM,aAAa,OAAO,wBAAwB,kBAAkB;AAEpE,UAAM,YAAY,oBAAI,IAAe;AAGrC,eAAW,MAAM,aAAa;AAC7B,UAAI,CAAC,WAAW,IAAI,EAAE,GAAG;AACxB,cAAM,QAAQ,OAAO,SAAS,EAAE;AAChC,YAAI,CAAC,MAAO;AAEZ,cAAM,UAAU,OAAO,aAAa,MAAM,IAAI,EAAE,QAAQ,KAAK;AAC7D,YAAI,CAAC,QAAS;AAEd,kBAAU,IAAI,EAAE;AAAA,MACjB;AAAA,IACD;AAEA,QAAI,gBAAgB,SAAS,KAAK,UAAU,SAAS,UAAU,MAAM;AACpE,aAAO;AAAA,IACR;AAEA,eAAW,QAAQ,WAAW;AAC7B,UAAI,CAAC,UAAU,IAAI,IAAI,EAAG,QAAO;AAAA,IAClC;AAEA,WAAO;AAAA,EACR,CAAC;AACF;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,114 @@
1
+ import RBush from "rbush";
2
+ import { Box } from "../../../primitives/Box.mjs";
3
+ class TldrawRBush extends RBush {
4
+ }
5
+ class RBushIndex {
6
+ rBush;
7
+ elementsInTree;
8
+ constructor() {
9
+ this.rBush = new TldrawRBush();
10
+ this.elementsInTree = /* @__PURE__ */ new Map();
11
+ }
12
+ /**
13
+ * Search for shapes within the given bounds.
14
+ * Returns set of shape IDs that intersect with the bounds.
15
+ */
16
+ search(bounds) {
17
+ const results = this.rBush.search({
18
+ minX: bounds.minX,
19
+ minY: bounds.minY,
20
+ maxX: bounds.maxX,
21
+ maxY: bounds.maxY
22
+ });
23
+ return new Set(results.map((e) => e.id));
24
+ }
25
+ /**
26
+ * Insert or update a shape in the spatial index.
27
+ * If the shape already exists, it will be removed first to prevent duplicates.
28
+ */
29
+ upsert(id, bounds) {
30
+ const existing = this.elementsInTree.get(id);
31
+ if (existing) {
32
+ this.rBush.remove(existing);
33
+ }
34
+ const element = {
35
+ minX: bounds.minX,
36
+ minY: bounds.minY,
37
+ maxX: bounds.maxX,
38
+ maxY: bounds.maxY,
39
+ id
40
+ };
41
+ this.rBush.insert(element);
42
+ this.elementsInTree.set(id, element);
43
+ }
44
+ /**
45
+ * Remove a shape from the spatial index.
46
+ */
47
+ remove(id) {
48
+ const element = this.elementsInTree.get(id);
49
+ if (element) {
50
+ this.rBush.remove(element);
51
+ this.elementsInTree.delete(id);
52
+ }
53
+ }
54
+ /**
55
+ * Bulk load elements into the spatial index.
56
+ * More efficient than individual inserts for initial loading.
57
+ */
58
+ bulkLoad(elements) {
59
+ this.rBush.load(elements);
60
+ for (const element of elements) {
61
+ this.elementsInTree.set(element.id, element);
62
+ }
63
+ }
64
+ /**
65
+ * Clear all elements from the spatial index.
66
+ */
67
+ clear() {
68
+ this.rBush.clear();
69
+ this.elementsInTree.clear();
70
+ }
71
+ /**
72
+ * Check if a shape is in the spatial index.
73
+ */
74
+ has(id) {
75
+ return this.elementsInTree.has(id);
76
+ }
77
+ /**
78
+ * Get the number of elements in the spatial index.
79
+ */
80
+ getSize() {
81
+ return this.elementsInTree.size;
82
+ }
83
+ /**
84
+ * Get all shape IDs currently in the spatial index.
85
+ */
86
+ getAllShapeIds() {
87
+ return Array.from(this.elementsInTree.keys());
88
+ }
89
+ /**
90
+ * Get the bounds currently stored in the spatial index for a shape.
91
+ * Returns undefined if the shape is not in the index.
92
+ */
93
+ getBounds(id) {
94
+ const element = this.elementsInTree.get(id);
95
+ if (!element) return void 0;
96
+ return new Box(
97
+ element.minX,
98
+ element.minY,
99
+ element.maxX - element.minX,
100
+ element.maxY - element.minY
101
+ );
102
+ }
103
+ /**
104
+ * Dispose of the spatial index.
105
+ * Clears all data structures to prevent memory leaks.
106
+ */
107
+ dispose() {
108
+ this.clear();
109
+ }
110
+ }
111
+ export {
112
+ RBushIndex
113
+ };
114
+ //# sourceMappingURL=RBushIndex.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/lib/editor/managers/SpatialIndexManager/RBushIndex.ts"],
4
+ "sourcesContent": ["import type { TLShapeId } from '@tldraw/tlschema'\nimport RBush from 'rbush'\nimport { Box } from '../../../primitives/Box'\n\n/**\n * Element stored in the R-tree spatial index.\n * Contains bounds (minX, minY, maxX, maxY) and shape ID.\n */\nexport interface SpatialElement {\n\tminX: number\n\tminY: number\n\tmaxX: number\n\tmaxY: number\n\tid: TLShapeId\n}\n\n/**\n * Custom RBush class for tldraw shapes.\n */\nclass TldrawRBush extends RBush<SpatialElement> {}\n\n/**\n * Wrapper around RBush R-tree for efficient spatial queries.\n * Maintains a map of elements currently in the tree for efficient updates.\n */\nexport class RBushIndex {\n\tprivate rBush: TldrawRBush\n\tprivate elementsInTree: Map<TLShapeId, SpatialElement>\n\n\tconstructor() {\n\t\tthis.rBush = new TldrawRBush()\n\t\tthis.elementsInTree = new Map()\n\t}\n\n\t/**\n\t * Search for shapes within the given bounds.\n\t * Returns set of shape IDs that intersect with the bounds.\n\t */\n\tsearch(bounds: Box): Set<TLShapeId> {\n\t\tconst results = this.rBush.search({\n\t\t\tminX: bounds.minX,\n\t\t\tminY: bounds.minY,\n\t\t\tmaxX: bounds.maxX,\n\t\t\tmaxY: bounds.maxY,\n\t\t})\n\t\treturn new Set(results.map((e: SpatialElement) => e.id))\n\t}\n\n\t/**\n\t * Insert or update a shape in the spatial index.\n\t * If the shape already exists, it will be removed first to prevent duplicates.\n\t */\n\tupsert(id: TLShapeId, bounds: Box): void {\n\t\t// Remove existing entry to prevent map-tree desync\n\t\tconst existing = this.elementsInTree.get(id)\n\t\tif (existing) {\n\t\t\tthis.rBush.remove(existing)\n\t\t}\n\n\t\tconst element: SpatialElement = {\n\t\t\tminX: bounds.minX,\n\t\t\tminY: bounds.minY,\n\t\t\tmaxX: bounds.maxX,\n\t\t\tmaxY: bounds.maxY,\n\t\t\tid,\n\t\t}\n\t\tthis.rBush.insert(element)\n\t\tthis.elementsInTree.set(id, element)\n\t}\n\n\t/**\n\t * Remove a shape from the spatial index.\n\t */\n\tremove(id: TLShapeId): void {\n\t\tconst element = this.elementsInTree.get(id)\n\t\tif (element) {\n\t\t\tthis.rBush.remove(element)\n\t\t\tthis.elementsInTree.delete(id)\n\t\t}\n\t}\n\n\t/**\n\t * Bulk load elements into the spatial index.\n\t * More efficient than individual inserts for initial loading.\n\t */\n\tbulkLoad(elements: SpatialElement[]): void {\n\t\tthis.rBush.load(elements)\n\t\tfor (const element of elements) {\n\t\t\tthis.elementsInTree.set(element.id, element)\n\t\t}\n\t}\n\n\t/**\n\t * Clear all elements from the spatial index.\n\t */\n\tclear(): void {\n\t\tthis.rBush.clear()\n\t\tthis.elementsInTree.clear()\n\t}\n\n\t/**\n\t * Check if a shape is in the spatial index.\n\t */\n\thas(id: TLShapeId): boolean {\n\t\treturn this.elementsInTree.has(id)\n\t}\n\n\t/**\n\t * Get the number of elements in the spatial index.\n\t */\n\tgetSize(): number {\n\t\treturn this.elementsInTree.size\n\t}\n\n\t/**\n\t * Get all shape IDs currently in the spatial index.\n\t */\n\tgetAllShapeIds(): TLShapeId[] {\n\t\treturn Array.from(this.elementsInTree.keys())\n\t}\n\n\t/**\n\t * Get the bounds currently stored in the spatial index for a shape.\n\t * Returns undefined if the shape is not in the index.\n\t */\n\tgetBounds(id: TLShapeId): Box | undefined {\n\t\tconst element = this.elementsInTree.get(id)\n\t\tif (!element) return undefined\n\t\treturn new Box(\n\t\t\telement.minX,\n\t\t\telement.minY,\n\t\t\telement.maxX - element.minX,\n\t\t\telement.maxY - element.minY\n\t\t)\n\t}\n\n\t/**\n\t * Dispose of the spatial index.\n\t * Clears all data structures to prevent memory leaks.\n\t */\n\tdispose(): void {\n\t\tthis.clear()\n\t}\n}\n"],
5
+ "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AA;AAAA;AAAA,EAKA,QAAc;AACb,SAAK,MAAM,MAAM;AACjB,SAAK,eAAe,MAAM;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,IAAwB;AAC3B,WAAO,KAAK,eAAe,IAAI,EAAE;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKA,UAAkB;AACjB,WAAO,KAAK,eAAe;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,iBAA8B;AAC7B,WAAO,MAAM,KAAK,KAAK,eAAe,KAAK,CAAC;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,IAAgC;AACzC,UAAM,UAAU,KAAK,eAAe,IAAI,EAAE;AAC1C,QAAI,CAAC,QAAS,QAAO;AACrB,WAAO,IAAI;AAAA,MACV,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,QAAQ,OAAO,QAAQ;AAAA,MACvB,QAAQ,OAAO,QAAQ;AAAA,IACxB;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAgB;AACf,SAAK,MAAM;AAAA,EACZ;AACD;",
6
+ "names": []
7
+ }
@@ -0,0 +1,161 @@
1
+ import { RESET_VALUE, computed, isUninitialized } from "@tldraw/state";
2
+ import { isShape } from "@tldraw/tlschema";
3
+ import { objectMapValues } from "@tldraw/utils";
4
+ import { Box } from "../../../primitives/Box.mjs";
5
+ import { RBushIndex } from "./RBushIndex.mjs";
6
+ class SpatialIndexManager {
7
+ constructor(editor) {
8
+ this.editor = editor;
9
+ this.rbush = new RBushIndex();
10
+ this.spatialIndexComputed = this.createSpatialIndexComputed();
11
+ }
12
+ rbush;
13
+ spatialIndexComputed;
14
+ lastPageId = null;
15
+ createSpatialIndexComputed() {
16
+ const shapeHistory = this.editor.store.query.filterHistory("shape");
17
+ return computed("spatialIndex", (_prevValue, lastComputedEpoch) => {
18
+ if (isUninitialized(_prevValue)) {
19
+ return this.buildFromScratch(lastComputedEpoch);
20
+ }
21
+ const shapeDiff = shapeHistory.getDiffSince(lastComputedEpoch);
22
+ if (shapeDiff === RESET_VALUE) {
23
+ return this.buildFromScratch(lastComputedEpoch);
24
+ }
25
+ const currentPageId = this.editor.getCurrentPageId();
26
+ if (this.lastPageId !== currentPageId) {
27
+ return this.buildFromScratch(lastComputedEpoch);
28
+ }
29
+ if (shapeDiff.length === 0) {
30
+ return lastComputedEpoch;
31
+ }
32
+ this.processIncrementalUpdate(shapeDiff);
33
+ return lastComputedEpoch;
34
+ });
35
+ }
36
+ buildFromScratch(epoch) {
37
+ this.rbush.clear();
38
+ this.lastPageId = this.editor.getCurrentPageId();
39
+ const shapes = this.editor.getCurrentPageShapes();
40
+ const elements = [];
41
+ for (const shape of shapes) {
42
+ const bounds = this.editor.getShapePageBounds(shape.id);
43
+ if (bounds) {
44
+ elements.push({
45
+ minX: bounds.minX,
46
+ minY: bounds.minY,
47
+ maxX: bounds.maxX,
48
+ maxY: bounds.maxY,
49
+ id: shape.id
50
+ });
51
+ }
52
+ }
53
+ this.rbush.bulkLoad(elements);
54
+ return epoch;
55
+ }
56
+ processIncrementalUpdate(shapeDiff) {
57
+ const processedShapeIds = /* @__PURE__ */ new Set();
58
+ for (const changes of shapeDiff) {
59
+ for (const shape of objectMapValues(changes.added)) {
60
+ if (isShape(shape) && this.editor.getAncestorPageId(shape) === this.lastPageId) {
61
+ const bounds = this.editor.getShapePageBounds(shape.id);
62
+ if (bounds) {
63
+ this.rbush.upsert(shape.id, bounds);
64
+ }
65
+ processedShapeIds.add(shape.id);
66
+ }
67
+ }
68
+ for (const shape of objectMapValues(changes.removed)) {
69
+ if (isShape(shape)) {
70
+ this.rbush.remove(shape.id);
71
+ processedShapeIds.add(shape.id);
72
+ }
73
+ }
74
+ for (const [from, to] of objectMapValues(changes.updated)) {
75
+ if (!isShape(to)) continue;
76
+ processedShapeIds.add(to.id);
77
+ const wasOnPage = this.editor.getAncestorPageId(from) === this.lastPageId;
78
+ const isOnPage = this.editor.getAncestorPageId(to) === this.lastPageId;
79
+ if (isOnPage) {
80
+ const bounds = this.editor.getShapePageBounds(to.id);
81
+ if (bounds) {
82
+ this.rbush.upsert(to.id, bounds);
83
+ }
84
+ } else if (wasOnPage) {
85
+ this.rbush.remove(to.id);
86
+ }
87
+ }
88
+ }
89
+ const allShapeIds = this.rbush.getAllShapeIds();
90
+ for (const shapeId of allShapeIds) {
91
+ if (processedShapeIds.has(shapeId)) continue;
92
+ const currentBounds = this.editor.getShapePageBounds(shapeId);
93
+ const indexedBounds = this.rbush.getBounds(shapeId);
94
+ if (!this.areBoundsEqual(currentBounds, indexedBounds)) {
95
+ if (currentBounds) {
96
+ this.rbush.upsert(shapeId, currentBounds);
97
+ } else {
98
+ this.rbush.remove(shapeId);
99
+ }
100
+ }
101
+ }
102
+ }
103
+ areBoundsEqual(a, b) {
104
+ if (!a && !b) return true;
105
+ if (!a || !b) return false;
106
+ return a.minX === b.minX && a.minY === b.minY && a.maxX === b.maxX && a.maxY === b.maxY;
107
+ }
108
+ /**
109
+ * Get shape IDs within the given bounds.
110
+ * Optimized for viewport culling queries.
111
+ *
112
+ * Note: Results are unordered. If you need z-order, combine with sorted shapes:
113
+ * ```ts
114
+ * const candidates = editor.spatialIndex.getShapeIdsInsideBounds(bounds)
115
+ * const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))
116
+ * ```
117
+ *
118
+ * @param bounds - The bounds to search within
119
+ * @returns Unordered set of shape IDs within the bounds
120
+ *
121
+ * @public
122
+ */
123
+ getShapeIdsInsideBounds(bounds) {
124
+ this.spatialIndexComputed.get();
125
+ return this.rbush.search(bounds);
126
+ }
127
+ /**
128
+ * Get shape IDs at a point (with optional margin).
129
+ * Creates a small bounding box around the point and searches the spatial index.
130
+ *
131
+ * Note: Results are unordered. If you need z-order, combine with sorted shapes:
132
+ * ```ts
133
+ * const candidates = editor.spatialIndex.getShapeIdsAtPoint(point, margin)
134
+ * const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))
135
+ * ```
136
+ *
137
+ * @param point - The point to search at
138
+ * @param margin - The margin around the point to search (default: 0)
139
+ * @returns Unordered set of shape IDs that could potentially contain the point
140
+ *
141
+ * @public
142
+ */
143
+ getShapeIdsAtPoint(point, margin = 0) {
144
+ this.spatialIndexComputed.get();
145
+ return this.rbush.search(new Box(point.x - margin, point.y - margin, margin * 2, margin * 2));
146
+ }
147
+ /**
148
+ * Dispose of the spatial index manager.
149
+ * Clears the R-tree to prevent memory leaks.
150
+ *
151
+ * @public
152
+ */
153
+ dispose() {
154
+ this.rbush.dispose();
155
+ this.lastPageId = null;
156
+ }
157
+ }
158
+ export {
159
+ SpatialIndexManager
160
+ };
161
+ //# sourceMappingURL=SpatialIndexManager.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts"],
4
+ "sourcesContent": ["import { Computed, RESET_VALUE, computed, isUninitialized } from '@tldraw/state'\nimport type { RecordsDiff } from '@tldraw/store'\nimport type { TLRecord } from '@tldraw/tlschema'\nimport { TLPageId, TLShape, TLShapeId, isShape } from '@tldraw/tlschema'\nimport { objectMapValues } from '@tldraw/utils'\nimport { Box } from '../../../primitives/Box'\nimport type { Editor } from '../../Editor'\nimport { RBushIndex, type SpatialElement } from './RBushIndex'\n\n/**\n * Manages spatial indexing for efficient shape location queries.\n *\n * Uses an R-tree (via RBush) to enable O(log n) spatial queries instead of O(n) iteration.\n * Handles shapes with computed bounds (arrows, groups, custom shapes) by checking all shapes'\n * bounds on each update using the reactive bounds cache.\n *\n * Key features:\n * - Incremental updates using filterHistory pattern\n * - Leverages existing bounds cache reactivity for dependency tracking\n * - Works with any custom shape type with computed bounds\n * - Per-page index (rebuilds on page change)\n * - Optimized for viewport culling queries\n *\n * @internal\n */\nexport class SpatialIndexManager {\n\tprivate rbush: RBushIndex\n\tprivate spatialIndexComputed: Computed<number>\n\tprivate lastPageId: TLPageId | null = null\n\n\tconstructor(public readonly editor: Editor) {\n\t\tthis.rbush = new RBushIndex()\n\t\tthis.spatialIndexComputed = this.createSpatialIndexComputed()\n\t}\n\n\tprivate createSpatialIndexComputed() {\n\t\tconst shapeHistory = this.editor.store.query.filterHistory('shape')\n\n\t\treturn computed<number>('spatialIndex', (_prevValue, lastComputedEpoch) => {\n\t\t\tif (isUninitialized(_prevValue)) {\n\t\t\t\treturn this.buildFromScratch(lastComputedEpoch)\n\t\t\t}\n\n\t\t\tconst shapeDiff = shapeHistory.getDiffSince(lastComputedEpoch)\n\n\t\t\tif (shapeDiff === RESET_VALUE) {\n\t\t\t\treturn this.buildFromScratch(lastComputedEpoch)\n\t\t\t}\n\n\t\t\tconst currentPageId = this.editor.getCurrentPageId()\n\t\t\tif (this.lastPageId !== currentPageId) {\n\t\t\t\treturn this.buildFromScratch(lastComputedEpoch)\n\t\t\t}\n\n\t\t\t// No shape changes - index is already up to date\n\t\t\tif (shapeDiff.length === 0) {\n\t\t\t\treturn lastComputedEpoch\n\t\t\t}\n\n\t\t\t// Process incremental updates\n\t\t\tthis.processIncrementalUpdate(shapeDiff)\n\n\t\t\treturn lastComputedEpoch\n\t\t})\n\t}\n\n\tprivate buildFromScratch(epoch: number): number {\n\t\tthis.rbush.clear()\n\t\tthis.lastPageId = this.editor.getCurrentPageId()\n\n\t\tconst shapes = this.editor.getCurrentPageShapes()\n\t\tconst elements: SpatialElement[] = []\n\n\t\t// Collect all shape elements for bulk loading\n\t\tfor (const shape of shapes) {\n\t\t\tconst bounds = this.editor.getShapePageBounds(shape.id)\n\t\t\tif (bounds) {\n\t\t\t\telements.push({\n\t\t\t\t\tminX: bounds.minX,\n\t\t\t\t\tminY: bounds.minY,\n\t\t\t\t\tmaxX: bounds.maxX,\n\t\t\t\t\tmaxY: bounds.maxY,\n\t\t\t\t\tid: shape.id,\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\t// Bulk load for efficiency\n\t\tthis.rbush.bulkLoad(elements)\n\n\t\treturn epoch\n\t}\n\n\tprivate processIncrementalUpdate(shapeDiff: RecordsDiff<TLRecord>[]): void {\n\t\t// Track shapes we've already processed from the diff\n\t\tconst processedShapeIds = new Set<TLShapeId>()\n\n\t\t// 1. Process shape additions, removals, and updates from diff\n\t\tfor (const changes of shapeDiff) {\n\t\t\t// Handle additions (only for shapes on current page)\n\t\t\tfor (const shape of objectMapValues(changes.added) as TLShape[]) {\n\t\t\t\tif (isShape(shape) && this.editor.getAncestorPageId(shape) === this.lastPageId) {\n\t\t\t\t\tconst bounds = this.editor.getShapePageBounds(shape.id)\n\t\t\t\t\tif (bounds) {\n\t\t\t\t\t\tthis.rbush.upsert(shape.id, bounds)\n\t\t\t\t\t}\n\t\t\t\t\tprocessedShapeIds.add(shape.id)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Handle removals\n\t\t\tfor (const shape of objectMapValues(changes.removed) as TLShape[]) {\n\t\t\t\tif (isShape(shape)) {\n\t\t\t\t\tthis.rbush.remove(shape.id)\n\t\t\t\t\tprocessedShapeIds.add(shape.id)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Handle updated shapes: page changes and bounds updates\n\t\t\tfor (const [from, to] of objectMapValues(changes.updated) as [TLShape, TLShape][]) {\n\t\t\t\tif (!isShape(to)) continue\n\t\t\t\tprocessedShapeIds.add(to.id)\n\n\t\t\t\tconst wasOnPage = this.editor.getAncestorPageId(from) === this.lastPageId\n\t\t\t\tconst isOnPage = this.editor.getAncestorPageId(to) === this.lastPageId\n\n\t\t\t\tif (isOnPage) {\n\t\t\t\t\tconst bounds = this.editor.getShapePageBounds(to.id)\n\t\t\t\t\tif (bounds) {\n\t\t\t\t\t\tthis.rbush.upsert(to.id, bounds)\n\t\t\t\t\t}\n\t\t\t\t} else if (wasOnPage) {\n\t\t\t\t\tthis.rbush.remove(to.id)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 2. Check remaining shapes in index for bounds changes\n\t\t// This handles shapes with computed bounds (arrows bound to moved shapes, groups with moved children, etc.)\n\t\tconst allShapeIds = this.rbush.getAllShapeIds()\n\n\t\tfor (const shapeId of allShapeIds) {\n\t\t\tif (processedShapeIds.has(shapeId)) continue\n\n\t\t\tconst currentBounds = this.editor.getShapePageBounds(shapeId)\n\t\t\tconst indexedBounds = this.rbush.getBounds(shapeId)\n\n\t\t\tif (!this.areBoundsEqual(currentBounds, indexedBounds)) {\n\t\t\t\tif (currentBounds) {\n\t\t\t\t\tthis.rbush.upsert(shapeId, currentBounds)\n\t\t\t\t} else {\n\t\t\t\t\tthis.rbush.remove(shapeId)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate areBoundsEqual(a: Box | undefined, b: Box | undefined): boolean {\n\t\tif (!a && !b) return true\n\t\tif (!a || !b) return false\n\t\treturn a.minX === b.minX && a.minY === b.minY && a.maxX === b.maxX && a.maxY === b.maxY\n\t}\n\n\t/**\n\t * Get shape IDs within the given bounds.\n\t * Optimized for viewport culling queries.\n\t *\n\t * Note: Results are unordered. If you need z-order, combine with sorted shapes:\n\t * ```ts\n\t * const candidates = editor.spatialIndex.getShapeIdsInsideBounds(bounds)\n\t * const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))\n\t * ```\n\t *\n\t * @param bounds - The bounds to search within\n\t * @returns Unordered set of shape IDs within the bounds\n\t *\n\t * @public\n\t */\n\tgetShapeIdsInsideBounds(bounds: Box): Set<TLShapeId> {\n\t\tthis.spatialIndexComputed.get()\n\t\treturn this.rbush.search(bounds)\n\t}\n\n\t/**\n\t * Get shape IDs at a point (with optional margin).\n\t * Creates a small bounding box around the point and searches the spatial index.\n\t *\n\t * Note: Results are unordered. If you need z-order, combine with sorted shapes:\n\t * ```ts\n\t * const candidates = editor.spatialIndex.getShapeIdsAtPoint(point, margin)\n\t * const sorted = editor.getCurrentPageShapesSorted().filter(s => candidates.has(s.id))\n\t * ```\n\t *\n\t * @param point - The point to search at\n\t * @param margin - The margin around the point to search (default: 0)\n\t * @returns Unordered set of shape IDs that could potentially contain the point\n\t *\n\t * @public\n\t */\n\tgetShapeIdsAtPoint(point: { x: number; y: number }, margin = 0): Set<TLShapeId> {\n\t\tthis.spatialIndexComputed.get()\n\t\treturn this.rbush.search(new Box(point.x - margin, point.y - margin, margin * 2, margin * 2))\n\t}\n\n\t/**\n\t * Dispose of the spatial index manager.\n\t * Clears the R-tree to prevent memory leaks.\n\t *\n\t * @public\n\t */\n\tdispose(): void {\n\t\tthis.rbush.dispose()\n\t\tthis.lastPageId = null\n\t}\n}\n"],
5
+ "mappings": "AAAA,SAAmB,aAAa,UAAU,uBAAuB;AAGjE,SAAuC,eAAe;AACtD,SAAS,uBAAuB;AAChC,SAAS,WAAW;AAEpB,SAAS,kBAAuC;AAkBzC,MAAM,oBAAoB;AAAA,EAKhC,YAA4B,QAAgB;AAAhB;AAC3B,SAAK,QAAQ,IAAI,WAAW;AAC5B,SAAK,uBAAuB,KAAK,2BAA2B;AAAA,EAC7D;AAAA,EAPQ;AAAA,EACA;AAAA,EACA,aAA8B;AAAA,EAO9B,6BAA6B;AACpC,UAAM,eAAe,KAAK,OAAO,MAAM,MAAM,cAAc,OAAO;AAElE,WAAO,SAAiB,gBAAgB,CAAC,YAAY,sBAAsB;AAC1E,UAAI,gBAAgB,UAAU,GAAG;AAChC,eAAO,KAAK,iBAAiB,iBAAiB;AAAA,MAC/C;AAEA,YAAM,YAAY,aAAa,aAAa,iBAAiB;AAE7D,UAAI,cAAc,aAAa;AAC9B,eAAO,KAAK,iBAAiB,iBAAiB;AAAA,MAC/C;AAEA,YAAM,gBAAgB,KAAK,OAAO,iBAAiB;AACnD,UAAI,KAAK,eAAe,eAAe;AACtC,eAAO,KAAK,iBAAiB,iBAAiB;AAAA,MAC/C;AAGA,UAAI,UAAU,WAAW,GAAG;AAC3B,eAAO;AAAA,MACR;AAGA,WAAK,yBAAyB,SAAS;AAEvC,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAAA,EAEQ,iBAAiB,OAA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AAmB;AAC/E,SAAK,qBAAqB,IAAI;AAC9B,WAAO,KAAK,MAAM,OAAO,IAAI,IAAI,MAAM,IAAI,QAAQ,MAAM,IAAI,QAAQ,SAAS,GAAG,SAAS,CAAC,CAAC;AAAA,EAC7F;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,UAAgB;AACf,SAAK,MAAM,QAAQ;AACnB,SAAK,aAAa;AAAA,EACnB;AACD;",
6
+ "names": []
7
+ }
@@ -1,8 +1,8 @@
1
- const version = "4.3.0";
1
+ const version = "4.4.0-canary.1e3b436e33e4";
2
2
  const publishDates = {
3
3
  major: "2025-09-18T14:39:22.803Z",
4
- minor: "2026-01-21T11:56:39.106Z",
5
- patch: "2026-01-21T11:56:39.106Z"
4
+ minor: "2026-01-21T13:45:38.121Z",
5
+ patch: "2026-01-21T13:45:38.121Z"
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 = '4.3.0'\nexport const publishDates = {\n\tmajor: '2025-09-18T14:39:22.803Z',\n\tminor: '2026-01-21T11:56:39.106Z',\n\tpatch: '2026-01-21T11:56:39.106Z',\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 = '4.4.0-canary.1e3b436e33e4'\nexport const publishDates = {\n\tmajor: '2025-09-18T14:39:22.803Z',\n\tminor: '2026-01-21T13:45:38.121Z',\n\tpatch: '2026-01-21T13:45:38.121Z',\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": "4.3.0",
4
+ "version": "4.4.0-canary.1e3b436e33e4",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -50,19 +50,20 @@
50
50
  "@tiptap/core": "^3.12.1",
51
51
  "@tiptap/pm": "^3.12.1",
52
52
  "@tiptap/react": "^3.12.1",
53
- "@tldraw/state": "4.3.0",
54
- "@tldraw/state-react": "4.3.0",
55
- "@tldraw/store": "4.3.0",
56
- "@tldraw/tlschema": "4.3.0",
57
- "@tldraw/utils": "4.3.0",
58
- "@tldraw/validate": "4.3.0",
53
+ "@tldraw/state": "4.4.0-canary.1e3b436e33e4",
54
+ "@tldraw/state-react": "4.4.0-canary.1e3b436e33e4",
55
+ "@tldraw/store": "4.4.0-canary.1e3b436e33e4",
56
+ "@tldraw/tlschema": "4.4.0-canary.1e3b436e33e4",
57
+ "@tldraw/utils": "4.4.0-canary.1e3b436e33e4",
58
+ "@tldraw/validate": "4.4.0-canary.1e3b436e33e4",
59
59
  "@types/core-js": "^2.5.8",
60
60
  "@use-gesture/react": "^10.3.1",
61
61
  "classnames": "^2.5.1",
62
62
  "core-js": "^3.40.0",
63
63
  "eventemitter3": "^4.0.7",
64
64
  "idb": "^7.1.1",
65
- "is-plain-object": "^5.0.0"
65
+ "is-plain-object": "^5.0.0",
66
+ "rbush": "^4.0.1"
66
67
  },
67
68
  "peerDependencies": {
68
69
  "react": "^18.2.0 || ^19.2.1",
@@ -73,6 +74,7 @@
73
74
  "@testing-library/dom": "^10.0.0",
74
75
  "@testing-library/react": "^16.0.0",
75
76
  "@types/benchmark": "^2.1.5",
77
+ "@types/rbush": "^4.0.0",
76
78
  "@types/react": "^19.2.7",
77
79
  "@types/react-dom": "^19.2.3",
78
80
  "@types/wicg-file-system-access": "^2020.9.8",
package/src/index.ts CHANGED
@@ -164,6 +164,7 @@ export {
164
164
  type SnapData,
165
165
  type SnapIndicator,
166
166
  } from './lib/editor/managers/SnapManager/SnapManager'
167
+ export { SpatialIndexManager } from './lib/editor/managers/SpatialIndexManager/SpatialIndexManager'
167
168
  export {
168
169
  TextManager,
169
170
  type TLMeasureTextOpts,
@@ -412,11 +412,7 @@ function ReflowIfNeeded() {
412
412
  'reflow for culled shapes',
413
413
  () => {
414
414
  const culledShapes = editor.getCulledShapes()
415
- if (
416
- culledShapesRef.current.size === culledShapes.size &&
417
- [...culledShapes].every((id) => culledShapesRef.current.has(id))
418
- )
419
- return
415
+ if (culledShapesRef.current === culledShapes) return
420
416
 
421
417
  culledShapesRef.current = culledShapes
422
418
  const canvas = document.getElementsByClassName('tl-canvas')
@@ -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 culledShapes = new Set<TLShapeId>(notVisibleShapes)
5156
+ const nextValue = new Set<TLShapeId>(notVisibleShapes)
5150
5157
  // we don't cull the shape we are editing
5151
5158
  if (editingId) {
5152
- culledShapes.delete(editingId)
5159
+ nextValue.delete(editingId)
5153
5160
  }
5154
5161
  // we also don't cull selected shapes
5155
5162
  selectedShapeIds.forEach((id) => {
5156
- culledShapes.delete(id)
5163
+ nextValue.delete(id)
5157
5164
  })
5158
- return culledShapes
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) => !this.isShapeHidden(shape) && this.isPointInShape(shape, point, opts))
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.