@tldraw/editor 5.2.1 → 5.3.0-canary.7d00ddf70f63
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/dist-cjs/index.d.ts +45 -8
- package/dist-cjs/index.js +3 -3
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/editor/Editor.js +12 -2
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/managers/EditorManager.js +52 -0
- package/dist-cjs/lib/editor/managers/EditorManager.js.map +7 -0
- package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js +9 -11
- package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/InputsManager/InputsManager.js +5 -9
- package/dist-cjs/lib/editor/managers/InputsManager/InputsManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/RBushIndex.js +18 -29
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/RBushIndex.js.map +2 -2
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js +96 -54
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/TextManager/TextManager.js +24 -19
- package/dist-cjs/lib/editor/managers/TextManager/TextManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/TickManager/TickManager.js +4 -7
- package/dist-cjs/lib/editor/managers/TickManager/TickManager.js.map +2 -2
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +1 -1
- package/dist-cjs/lib/editor/tools/BaseBoxShapeTool/children/Pointing.js +1 -2
- package/dist-cjs/lib/editor/tools/BaseBoxShapeTool/children/Pointing.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 +45 -8
- package/dist-esm/index.mjs +3 -3
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/editor/Editor.mjs +12 -2
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/EditorManager.mjs +32 -0
- package/dist-esm/lib/editor/managers/EditorManager.mjs.map +7 -0
- package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs +9 -11
- package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/InputsManager/InputsManager.mjs +5 -9
- package/dist-esm/lib/editor/managers/InputsManager/InputsManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/SpatialIndexManager/RBushIndex.mjs +18 -29
- package/dist-esm/lib/editor/managers/SpatialIndexManager/RBushIndex.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs +103 -56
- package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/TextManager/TextManager.mjs +24 -19
- package/dist-esm/lib/editor/managers/TextManager/TextManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs +4 -7
- package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs.map +2 -2
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +1 -1
- package/dist-esm/lib/editor/tools/BaseBoxShapeTool/children/Pointing.mjs +1 -2
- package/dist-esm/lib/editor/tools/BaseBoxShapeTool/children/Pointing.mjs.map +2 -2
- package/dist-esm/version.mjs +3 -3
- package/dist-esm/version.mjs.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +1 -1
- package/src/lib/editor/Editor.ts +15 -2
- package/src/lib/editor/managers/EditorManager.test.ts +51 -0
- package/src/lib/editor/managers/EditorManager.ts +60 -0
- package/src/lib/editor/managers/FocusManager/FocusManager.test.ts +2 -1
- package/src/lib/editor/managers/FocusManager/FocusManager.ts +10 -14
- package/src/lib/editor/managers/InputsManager/InputsManager.ts +6 -9
- package/src/lib/editor/managers/SpatialIndexManager/RBushIndex.ts +19 -33
- package/src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts +173 -86
- package/src/lib/editor/managers/TextManager/TextManager.test.ts +61 -1
- package/src/lib/editor/managers/TextManager/TextManager.ts +33 -19
- package/src/lib/editor/managers/TickManager/TickManager.test.ts +0 -10
- package/src/lib/editor/managers/TickManager/TickManager.ts +5 -6
- package/src/lib/editor/shapes/ShapeUtil.ts +1 -0
- package/src/lib/editor/tools/BaseBoxShapeTool/children/Pointing.ts +2 -2
- package/src/version.ts +3 -3
- package/dist-cjs/lib/editor/tools/cancelShapeCreationOnLongPress.js +0 -28
- package/dist-cjs/lib/editor/tools/cancelShapeCreationOnLongPress.js.map +0 -7
- package/dist-esm/lib/editor/tools/cancelShapeCreationOnLongPress.mjs +0 -8
- package/dist-esm/lib/editor/tools/cancelShapeCreationOnLongPress.mjs.map +0 -7
- package/src/lib/editor/tools/cancelShapeCreationOnLongPress.ts +0 -31
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class EditorManager {
|
|
2
|
+
constructor(editor) {
|
|
3
|
+
this.editor = editor;
|
|
4
|
+
}
|
|
5
|
+
editor;
|
|
6
|
+
disposables = /* @__PURE__ */ new Set();
|
|
7
|
+
/**
|
|
8
|
+
* Register a teardown function to run on `dispose()`. The universal way for a manager to
|
|
9
|
+
* make a resource "see itself out". Returns the same function for convenience.
|
|
10
|
+
*/
|
|
11
|
+
register(dispose) {
|
|
12
|
+
this.disposables.add(dispose);
|
|
13
|
+
return dispose;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Subscribe to an editor bus event and register the matching unsubscribe, so the listener
|
|
17
|
+
* is removed automatically on `dispose()`.
|
|
18
|
+
*/
|
|
19
|
+
addEditorEvent(event, fn) {
|
|
20
|
+
this.editor.on(event, fn);
|
|
21
|
+
this.register(() => this.editor.off(event, fn));
|
|
22
|
+
}
|
|
23
|
+
/** @internal */
|
|
24
|
+
dispose() {
|
|
25
|
+
this.disposables.forEach((d) => d());
|
|
26
|
+
this.disposables.clear();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
EditorManager
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=EditorManager.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/lib/editor/managers/EditorManager.ts"],
|
|
4
|
+
"sourcesContent": ["import type { Editor } from '../Editor'\nimport type { TLEventMap } from '../types/emit-types'\n\n/**\n * Base class for editor-attached managers that need to clean up after themselves.\n *\n * Extending `EditorManager` gives a manager one consistent teardown contract: register\n * anything that needs undoing, and it runs automatically on `dispose()`. This keeps\n * subscription lifecycle symmetric \u2014 whatever sets a thing up is what tears it down \u2014 so\n * it can't be forgotten (the failure mode behind the strict-mode camera bug, #8892).\n *\n * Register cleanup by what you're creating:\n *\n * - Editor bus event (`tick`, `frame`, `change`, \u2026): {@link EditorManager.addEditorEvent}\n * - Store side effect (`editor.sideEffects.register\u2026`): `this.register(disposer)`\n * - Reaction (`react(...)` from `@tldraw/state`): `this.register(react(...))`\n * - DOM listener: `this.register(() => el.removeEventListener(...))`\n * - Other resource (cache, index, child manager): `this.register(() => x.dispose())`\n *\n * For timeouts, intervals, and animation frames prefer `editor.timers` (context-grouped\n * and auto-disposed) over raw `setTimeout`. For cleanup on the editor itself rather than a\n * manager, use `editor.disposables`.\n *\n * If a manager needs ordered teardown (e.g. pause a loop before cancelling it), override\n * `dispose()` and call `super.dispose()` last \u2014 see `TickManager`.\n *\n * @public\n */\nexport abstract class EditorManager {\n\tprotected readonly disposables = new Set<() => void>()\n\n\tconstructor(protected readonly editor: Editor) {}\n\n\t/**\n\t * Register a teardown function to run on `dispose()`. The universal way for a manager to\n\t * make a resource \"see itself out\". Returns the same function for convenience.\n\t */\n\tprotected register(dispose: () => void): () => void {\n\t\tthis.disposables.add(dispose)\n\t\treturn dispose\n\t}\n\n\t/**\n\t * Subscribe to an editor bus event and register the matching unsubscribe, so the listener\n\t * is removed automatically on `dispose()`.\n\t */\n\tprotected addEditorEvent<E extends keyof TLEventMap>(\n\t\tevent: E,\n\t\tfn: (...args: TLEventMap[E]) => void\n\t): void {\n\t\tthis.editor.on(event, fn as any)\n\t\tthis.register(() => this.editor.off(event, fn as any))\n\t}\n\n\t/** @internal */\n\tdispose(): void {\n\t\tthis.disposables.forEach((d) => d())\n\t\tthis.disposables.clear()\n\t}\n}\n"],
|
|
5
|
+
"mappings": "AA4BO,MAAe,cAAc;AAAA,EAGnC,YAA+B,QAAgB;AAAhB;AAAA,EAAiB;AAAA,EAAjB;AAAA,EAFZ,cAAc,oBAAI,IAAgB;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ3C,SAAS,SAAiC;AACnD,SAAK,YAAY,IAAI,OAAO;AAC5B,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,eACT,OACA,IACO;AACP,SAAK,OAAO,GAAG,OAAO,EAAS;AAC/B,SAAK,SAAS,MAAM,KAAK,OAAO,IAAI,OAAO,EAAS,CAAC;AAAA,EACtD;AAAA;AAAA,EAGA,UAAgB;AACf,SAAK,YAAY,QAAQ,CAAC,MAAM,EAAE,CAAC;AACnC,SAAK,YAAY,MAAM;AAAA,EACxB;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -9,10 +9,11 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
9
9
|
return result;
|
|
10
10
|
};
|
|
11
11
|
import { bind } from "@tldraw/utils";
|
|
12
|
-
|
|
12
|
+
import { EditorManager } from "../EditorManager.mjs";
|
|
13
|
+
class FocusManager extends EditorManager {
|
|
13
14
|
constructor(editor, autoFocus) {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
super(editor);
|
|
16
|
+
const disposeSideEffectListener = editor.sideEffects.registerAfterChangeHandler(
|
|
16
17
|
"instance",
|
|
17
18
|
(prev, next) => {
|
|
18
19
|
if (prev.isFocused !== next.isFocused) {
|
|
@@ -20,6 +21,7 @@ class FocusManager {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
);
|
|
24
|
+
this.register(() => disposeSideEffectListener?.());
|
|
23
25
|
const currentFocusState = editor.getInstanceState().isFocused;
|
|
24
26
|
if (autoFocus !== currentFocusState) {
|
|
25
27
|
editor.updateInstanceState({ isFocused: !!autoFocus });
|
|
@@ -28,9 +30,11 @@ class FocusManager {
|
|
|
28
30
|
const body = editor.getContainerDocument().body;
|
|
29
31
|
body.addEventListener("keydown", this.handleKeyDown);
|
|
30
32
|
body.addEventListener("mousedown", this.handleMouseDown);
|
|
33
|
+
this.register(() => {
|
|
34
|
+
body.removeEventListener("keydown", this.handleKeyDown);
|
|
35
|
+
body.removeEventListener("mousedown", this.handleMouseDown);
|
|
36
|
+
});
|
|
31
37
|
}
|
|
32
|
-
editor;
|
|
33
|
-
disposeSideEffectListener;
|
|
34
38
|
/**
|
|
35
39
|
* The editor's focus state and the container's focus state
|
|
36
40
|
* are not necessarily always in sync. For that reason we
|
|
@@ -70,12 +74,6 @@ class FocusManager {
|
|
|
70
74
|
this.editor.complete();
|
|
71
75
|
if (blurContainer) this.editor.getContainer().blur();
|
|
72
76
|
}
|
|
73
|
-
dispose() {
|
|
74
|
-
const body = this.editor.getContainerDocument().body;
|
|
75
|
-
body.removeEventListener("keydown", this.handleKeyDown);
|
|
76
|
-
body.removeEventListener("mousedown", this.handleMouseDown);
|
|
77
|
-
this.disposeSideEffectListener?.();
|
|
78
|
-
}
|
|
79
77
|
}
|
|
80
78
|
__decorateClass([
|
|
81
79
|
bind
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../src/lib/editor/managers/FocusManager/FocusManager.ts"],
|
|
4
|
-
"sourcesContent": ["import { bind } from '@tldraw/utils'\nimport type { Editor } from '../../Editor'\n\n/**\n * A manager for ensuring correct focus across the editor.\n * It will listen for changes in the instance state to make sure the\n * container is focused when the editor is focused.\n * Also, it will make sure that the focus is on things like text\n * labels when the editor is in editing mode.\n *\n * @internal\n */\nexport class FocusManager
|
|
5
|
-
"mappings": ";;;;;;;;;;AAAA,SAAS,YAAY;
|
|
4
|
+
"sourcesContent": ["import { bind } from '@tldraw/utils'\nimport type { Editor } from '../../Editor'\nimport { EditorManager } from '../EditorManager'\n\n/**\n * A manager for ensuring correct focus across the editor.\n * It will listen for changes in the instance state to make sure the\n * container is focused when the editor is focused.\n * Also, it will make sure that the focus is on things like text\n * labels when the editor is in editing mode.\n *\n * @internal\n */\nexport class FocusManager extends EditorManager {\n\tconstructor(editor: Editor, autoFocus?: boolean) {\n\t\tsuper(editor)\n\n\t\tconst disposeSideEffectListener = editor.sideEffects.registerAfterChangeHandler(\n\t\t\t'instance',\n\t\t\t(prev, next) => {\n\t\t\t\tif (prev.isFocused !== next.isFocused) {\n\t\t\t\t\tthis.updateContainerClass()\n\t\t\t\t}\n\t\t\t}\n\t\t)\n\t\tthis.register(() => disposeSideEffectListener?.())\n\n\t\tconst currentFocusState = editor.getInstanceState().isFocused\n\t\tif (autoFocus !== currentFocusState) {\n\t\t\teditor.updateInstanceState({ isFocused: !!autoFocus })\n\t\t}\n\t\tthis.updateContainerClass()\n\n\t\tconst body = editor.getContainerDocument().body\n\t\tbody.addEventListener('keydown', this.handleKeyDown)\n\t\tbody.addEventListener('mousedown', this.handleMouseDown)\n\t\tthis.register(() => {\n\t\t\tbody.removeEventListener('keydown', this.handleKeyDown)\n\t\t\tbody.removeEventListener('mousedown', this.handleMouseDown)\n\t\t})\n\t}\n\n\t/**\n\t * The editor's focus state and the container's focus state\n\t * are not necessarily always in sync. For that reason we\n\t * can't rely on the css `:focus` or `:focus-within` selectors to style the\n\t * editor when it is in focus.\n\t *\n\t * For that reason we synchronize the editor's focus state with a\n\t * special class on the container: tl-container__focused\n\t */\n\tprivate updateContainerClass() {\n\t\tconst container = this.editor.getContainer()\n\t\tconst instanceState = this.editor.getInstanceState()\n\n\t\tif (instanceState.isFocused) {\n\t\t\tcontainer.classList.add('tl-container__focused')\n\t\t} else {\n\t\t\tcontainer.classList.remove('tl-container__focused')\n\t\t}\n\t\tcontainer.classList.add('tl-container__no-focus-ring')\n\t}\n\n\t@bind private handleKeyDown(keyEvent: KeyboardEvent) {\n\t\tconst container = this.editor.getContainer()\n\t\tconst activeEl = container.ownerDocument.activeElement\n\t\t// Edit mode should remove the focus ring, however if the active element's\n\t\t// parent is the contextual toolbar, then allow it.\n\t\tif (this.editor.getEditingShapeId() && !activeEl?.closest('.tlui-contextual-toolbar')) return\n\t\tif (activeEl === container && this.editor.getSelectedShapeIds().length > 0) return\n\t\tif (['Tab', 'ArrowUp', 'ArrowDown'].includes(keyEvent.key)) {\n\t\t\tcontainer.classList.remove('tl-container__no-focus-ring')\n\t\t}\n\t}\n\n\t@bind private handleMouseDown() {\n\t\tconst container = this.editor.getContainer()\n\t\tcontainer.classList.add('tl-container__no-focus-ring')\n\t}\n\n\tfocus() {\n\t\tthis.editor.getContainer().focus()\n\t}\n\n\tblur({ blurContainer = true } = {}) {\n\t\tthis.editor.complete() // stop any interaction\n\t\tif (blurContainer) this.editor.getContainer().blur() // blur the container\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAAA,SAAS,YAAY;AAErB,SAAS,qBAAqB;AAWvB,MAAM,qBAAqB,cAAc;AAAA,EAC/C,YAAY,QAAgB,WAAqB;AAChD,UAAM,MAAM;AAEZ,UAAM,4BAA4B,OAAO,YAAY;AAAA,MACpD;AAAA,MACA,CAAC,MAAM,SAAS;AACf,YAAI,KAAK,cAAc,KAAK,WAAW;AACtC,eAAK,qBAAqB;AAAA,QAC3B;AAAA,MACD;AAAA,IACD;AACA,SAAK,SAAS,MAAM,4BAA4B,CAAC;AAEjD,UAAM,oBAAoB,OAAO,iBAAiB,EAAE;AACpD,QAAI,cAAc,mBAAmB;AACpC,aAAO,oBAAoB,EAAE,WAAW,CAAC,CAAC,UAAU,CAAC;AAAA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AAa,EAAE,KAAK;AAAA,EACpD;AACD;AAzBe;AAAA,EAAb;AAAA,GAlDW,aAkDE;AAYA;AAAA,EAAb;AAAA,GA9DW,aA8DE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -15,17 +15,13 @@ import { bind } from "@tldraw/utils";
|
|
|
15
15
|
import { INTERNAL_POINTER_IDS } from "../../../constants.mjs";
|
|
16
16
|
import { Vec } from "../../../primitives/Vec.mjs";
|
|
17
17
|
import { isAccelKey } from "../../../utils/keyboard.mjs";
|
|
18
|
+
import { EditorManager } from "../EditorManager.mjs";
|
|
18
19
|
const POINTER_VELOCITY_REFERENCE_INTERVAL_MS = 16;
|
|
19
20
|
const POINTER_VELOCITY_REFERENCE_SMOOTHING = 0.5;
|
|
20
|
-
class InputsManager {
|
|
21
|
+
class InputsManager extends EditorManager {
|
|
21
22
|
constructor(editor) {
|
|
22
|
-
|
|
23
|
-
this.
|
|
24
|
-
}
|
|
25
|
-
editor;
|
|
26
|
-
/** @internal */
|
|
27
|
-
dispose() {
|
|
28
|
-
this.editor.off("frame", this._onFrame);
|
|
23
|
+
super(editor);
|
|
24
|
+
this.addEditorEvent("frame", this._onFrame);
|
|
29
25
|
}
|
|
30
26
|
_onFrame(elapsed) {
|
|
31
27
|
this.updatePointerVelocity(elapsed);
|
|
@@ -449,7 +445,7 @@ class InputsManager {
|
|
|
449
445
|
*/
|
|
450
446
|
_velocityPrevPoint = new Vec();
|
|
451
447
|
/**
|
|
452
|
-
* Update the pointer velocity based on elapsed time. Called
|
|
448
|
+
* Update the pointer velocity based on elapsed time. Called by the tick manager.
|
|
453
449
|
* @param elapsed - The time elapsed since the last tick in milliseconds.
|
|
454
450
|
* @internal
|
|
455
451
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../src/lib/editor/managers/InputsManager/InputsManager.ts"],
|
|
4
|
-
"sourcesContent": ["import { atom, computed, unsafe__withoutCapture } from '@tldraw/state'\nimport { AtomSet } from '@tldraw/store'\nimport { TLINSTANCE_ID, TLPOINTER_ID } from '@tldraw/tlschema'\nimport { bind } from '@tldraw/utils'\nimport { INTERNAL_POINTER_IDS } from '../../../constants'\nimport { Vec } from '../../../primitives/Vec'\nimport { isAccelKey } from '../../../utils/keyboard'\nimport type { Editor } from '../../Editor'\nimport { TLPinchEventInfo, TLPointerEventInfo, TLWheelEventInfo } from '../../types/event-types'\n\nconst POINTER_VELOCITY_REFERENCE_INTERVAL_MS = 16\nconst POINTER_VELOCITY_REFERENCE_SMOOTHING = 0.5\n\n/** @public */\nexport class InputsManager {\n\tconstructor(private readonly editor: Editor) {\n\t\tthis.editor.on('frame', this._onFrame)\n\t}\n\n\t/** @internal */\n\tdispose() {\n\t\tthis.editor.off('frame', this._onFrame)\n\t}\n\n\t@bind\n\tprivate _onFrame(elapsed: number) {\n\t\tthis.updatePointerVelocity(elapsed)\n\t}\n\n\tprivate _originPagePoint = atom<Vec>('originPagePoint', new Vec())\n\t/**\n\t * The most recent pointer down's position in the current page space.\n\t */\n\tgetOriginPagePoint() {\n\t\treturn this._originPagePoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getOriginPagePoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget originPagePoint() {\n\t\treturn this.getOriginPagePoint()\n\t}\n\n\tprivate _originScreenPoint = atom<Vec>('originScreenPoint', new Vec())\n\t/**\n\t * The most recent pointer down's position in screen space.\n\t */\n\tgetOriginScreenPoint() {\n\t\treturn this._originScreenPoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getOriginScreenPoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget originScreenPoint() {\n\t\treturn this.getOriginScreenPoint()\n\t}\n\n\tprivate _previousPagePoint = atom<Vec>('previousPagePoint', new Vec())\n\t/**\n\t * The previous pointer position in the current page space.\n\t */\n\tgetPreviousPagePoint() {\n\t\treturn this._previousPagePoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getPreviousPagePoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget previousPagePoint() {\n\t\treturn this.getPreviousPagePoint()\n\t}\n\n\tprivate _previousScreenPoint = atom<Vec>('previousScreenPoint', new Vec())\n\t/**\n\t * The previous pointer position in screen space.\n\t */\n\tgetPreviousScreenPoint() {\n\t\treturn this._previousScreenPoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getPreviousScreenPoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget previousScreenPoint() {\n\t\treturn this.getPreviousScreenPoint()\n\t}\n\n\tprivate _currentPagePoint = atom<Vec>('currentPagePoint', new Vec())\n\t/**\n\t * The most recent pointer position in the current page space.\n\t */\n\tgetCurrentPagePoint() {\n\t\treturn this._currentPagePoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getCurrentPagePoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget currentPagePoint() {\n\t\treturn this.getCurrentPagePoint()\n\t}\n\n\tprivate _currentScreenPoint = atom<Vec>('currentScreenPoint', new Vec())\n\t/**\n\t * The most recent pointer position in screen space.\n\t */\n\tgetCurrentScreenPoint() {\n\t\treturn this._currentScreenPoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getCurrentScreenPoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget currentScreenPoint() {\n\t\treturn this.getCurrentScreenPoint()\n\t}\n\n\tprivate _pointerVelocity = atom<Vec>('pointerVelocity', new Vec())\n\t/**\n\t * Velocity of mouse pointer, in pixels per millisecond.\n\t */\n\tgetPointerVelocity() {\n\t\treturn this._pointerVelocity.get()\n\t}\n\t/**\n\t * @deprecated Use `getPointerVelocity()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget pointerVelocity() {\n\t\treturn this.getPointerVelocity()\n\t}\n\n\t/**\n\t * Normally you shouldn't need to set the pointer velocity directly. Used in tests to fake pointer velocity.\n\t * @param pointerVelocity - The pointer velocity.\n\t * @internal\n\t */\n\tsetPointerVelocity(pointerVelocity: Vec) {\n\t\tthis._pointerVelocity.set(pointerVelocity)\n\t}\n\n\t/**\n\t * A set containing the currently pressed keys.\n\t */\n\treadonly keys = new AtomSet<string>('keys')\n\n\t/**\n\t * A set containing the currently pressed buttons.\n\t */\n\treadonly buttons = new AtomSet<number>('buttons')\n\n\tprivate _isPen = atom<boolean>('isPen', false)\n\n\t/**\n\t * Whether the input is from a pen.\n\t */\n\tgetIsPen() {\n\t\treturn this._isPen.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPen()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPen() {\n\t\treturn this.getIsPen()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPen(isPen: boolean) {\n\t\tthis.setIsPen(isPen)\n\t}\n\t/**\n\t * @param isPen - Whether the input is from a pen.\n\t */\n\tsetIsPen(isPen: boolean) {\n\t\tthis._isPen.set(isPen)\n\t}\n\n\tprivate _shiftKey = atom<boolean>('shiftKey', false)\n\t/**\n\t * Whether the shift key is currently pressed.\n\t */\n\tgetShiftKey() {\n\t\treturn this._shiftKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getShiftKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget shiftKey() {\n\t\treturn this.getShiftKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset shiftKey(shiftKey: boolean) {\n\t\tthis.setShiftKey(shiftKey)\n\t}\n\t/**\n\t * @param shiftKey - Whether the shift key is pressed.\n\t * @internal\n\t */\n\tsetShiftKey(shiftKey: boolean) {\n\t\tthis._shiftKey.set(shiftKey)\n\t}\n\n\tprivate _metaKey = atom<boolean>('metaKey', false)\n\t/**\n\t * Whether the meta key is currently pressed.\n\t */\n\tgetMetaKey() {\n\t\treturn this._metaKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getMetaKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget metaKey() {\n\t\treturn this.getMetaKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset metaKey(metaKey: boolean) {\n\t\tthis.setMetaKey(metaKey)\n\t}\n\t/**\n\t * @param metaKey - Whether the meta key is pressed.\n\t * @internal\n\t */\n\tsetMetaKey(metaKey: boolean) {\n\t\tthis._metaKey.set(metaKey)\n\t}\n\n\tprivate _ctrlKey = atom<boolean>('ctrlKey', false)\n\t/**\n\t * Whether the ctrl or command key is currently pressed.\n\t */\n\tgetCtrlKey() {\n\t\treturn this._ctrlKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getCtrlKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget ctrlKey() {\n\t\treturn this.getCtrlKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset ctrlKey(ctrlKey: boolean) {\n\t\tthis.setCtrlKey(ctrlKey)\n\t}\n\t/**\n\t * @param ctrlKey - Whether the ctrl key is pressed.\n\t * @internal\n\t */\n\tsetCtrlKey(ctrlKey: boolean) {\n\t\tthis._ctrlKey.set(ctrlKey)\n\t}\n\n\tprivate _altKey = atom<boolean>('altKey', false)\n\t/**\n\t * Whether the alt or option key is currently pressed.\n\t */\n\tgetAltKey() {\n\t\treturn this._altKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getAltKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget altKey() {\n\t\treturn this.getAltKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset altKey(altKey: boolean) {\n\t\tthis.setAltKey(altKey)\n\t}\n\t/**\n\t * @param altKey - Whether the alt key is pressed.\n\t * @internal\n\t */\n\tsetAltKey(altKey: boolean) {\n\t\tthis._altKey.set(altKey)\n\t}\n\n\t/**\n\t * Is the accelerator key (cmd on mac, ctrl elsewhere) currently pressed.\n\t */\n\tgetAccelKey() {\n\t\treturn isAccelKey({ metaKey: this.getMetaKey(), ctrlKey: this.getCtrlKey() })\n\t}\n\t/**\n\t * @deprecated Use `getAccelKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget accelKey() {\n\t\treturn this.getAccelKey()\n\t}\n\n\tprivate _isDragging = atom<boolean>('isDragging', false)\n\t/**\n\t * Whether the user is dragging.\n\t */\n\tgetIsDragging() {\n\t\treturn this._isDragging.get()\n\t}\n\t/**\n\t * Soon to be deprecated, use `getIsDragging()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isDragging() {\n\t\treturn this.getIsDragging()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isDragging(isDragging: boolean) {\n\t\tthis.setIsDragging(isDragging)\n\t}\n\t/**\n\t * @param isDragging - Whether the user is dragging.\n\t */\n\tsetIsDragging(isDragging: boolean) {\n\t\tthis._isDragging.set(isDragging)\n\t}\n\n\tprivate _isPointing = atom<boolean>('isPointing', false)\n\t/**\n\t * Whether the user is pointing.\n\t */\n\tgetIsPointing() {\n\t\treturn this._isPointing.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPointing()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPointing() {\n\t\treturn this.getIsPointing()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPointing(isPointing: boolean) {\n\t\tthis.setIsPointing(isPointing)\n\t}\n\t/**\n\t * @param isPointing - Whether the user is pointing.\n\t * @internal\n\t */\n\tsetIsPointing(isPointing: boolean) {\n\t\tthis._isPointing.set(isPointing)\n\t}\n\n\tprivate _isRightPointing = atom<boolean>('isRightPointing', false)\n\t/**\n\t * Whether the user is right-click pointing (before drag threshold).\n\t */\n\tgetIsRightPointing() {\n\t\treturn this._isRightPointing.get()\n\t}\n\t/** @internal */\n\tsetIsRightPointing(isRightPointing: boolean) {\n\t\tthis._isRightPointing.set(isRightPointing)\n\t}\n\n\tprivate _isPinching = atom<boolean>('isPinching', false)\n\t/**\n\t * Whether the user is pinching.\n\t */\n\tgetIsPinching() {\n\t\treturn this._isPinching.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPinching()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPinching() {\n\t\treturn this.getIsPinching()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPinching(isPinching: boolean) {\n\t\tthis.setIsPinching(isPinching)\n\t}\n\t/**\n\t * @param isPinching - Whether the user is pinching.\n\t * @internal\n\t */\n\tsetIsPinching(isPinching: boolean) {\n\t\tthis._isPinching.set(isPinching)\n\t}\n\n\tprivate _isEditing = atom<boolean>('isEditing', false)\n\t/**\n\t * Whether the user is editing.\n\t */\n\tgetIsEditing() {\n\t\treturn this._isEditing.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsEditing()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isEditing() {\n\t\treturn this.getIsEditing()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isEditing(isEditing: boolean) {\n\t\tthis.setIsEditing(isEditing)\n\t}\n\t/**\n\t * @param isEditing - Whether the user is editing.\n\t */\n\tsetIsEditing(isEditing: boolean) {\n\t\tthis._isEditing.set(isEditing)\n\t}\n\n\tprivate _isPanning = atom<boolean>('isPanning', false)\n\t/**\n\t * Whether the user is panning.\n\t */\n\tgetIsPanning() {\n\t\treturn this._isPanning.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPanning()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPanning() {\n\t\treturn this.getIsPanning()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPanning(isPanning: boolean) {\n\t\tthis.setIsPanning(isPanning)\n\t}\n\t/**\n\t * @param isPanning - Whether the user is panning.\n\t * @internal\n\t */\n\tsetIsPanning(isPanning: boolean) {\n\t\tthis._isPanning.set(isPanning)\n\t}\n\n\tprivate _isSpacebarPanning = atom<boolean>('isSpacebarPanning', false)\n\t/**\n\t * Whether the user is spacebar panning.\n\t */\n\tgetIsSpacebarPanning() {\n\t\treturn this._isSpacebarPanning.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsSpacebarPanning()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isSpacebarPanning() {\n\t\treturn this.getIsSpacebarPanning()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isSpacebarPanning(isSpacebarPanning: boolean) {\n\t\tthis.setIsSpacebarPanning(isSpacebarPanning)\n\t}\n\t/**\n\t * @param isSpacebarPanning - Whether the user is spacebar panning.\n\t * @internal\n\t */\n\tsetIsSpacebarPanning(isSpacebarPanning: boolean) {\n\t\tthis._isSpacebarPanning.set(isSpacebarPanning)\n\t}\n\n\t@computed private _getHasCollaborators() {\n\t\treturn this.editor.getCollaborators().length > 0 // could we do this more efficiently?\n\t}\n\n\t/**\n\t * The previous point used for velocity calculation (updated each tick, not each pointer event).\n\t * @internal\n\t */\n\tprivate _velocityPrevPoint = new Vec()\n\n\t/**\n\t * Update the pointer velocity based on elapsed time. Called each frame.\n\t * @param elapsed - The time elapsed since the last tick in milliseconds.\n\t * @internal\n\t */\n\tupdatePointerVelocity(elapsed: number) {\n\t\tconst currentScreenPoint = this.getCurrentScreenPoint()\n\t\tconst pointerVelocity = this.getPointerVelocity()\n\n\t\tif (elapsed === 0) return\n\n\t\tconst delta = Vec.Sub(currentScreenPoint, this._velocityPrevPoint)\n\t\tthis._velocityPrevPoint = currentScreenPoint.clone()\n\n\t\tconst length = delta.len()\n\t\tconst direction = length ? delta.div(length) : new Vec(0, 0)\n\n\t\t// Preserve the old 16ms smoothing with alpha = 1 - (1 - 0.5)^(elapsed / 16).\n\t\tconst smoothing =\n\t\t\t1 -\n\t\t\tMath.pow(\n\t\t\t\t1 - POINTER_VELOCITY_REFERENCE_SMOOTHING,\n\t\t\t\telapsed / POINTER_VELOCITY_REFERENCE_INTERVAL_MS\n\t\t\t)\n\t\tconst next = pointerVelocity.clone().lrp(direction.mul(length / elapsed), smoothing)\n\n\t\t// if the velocity is very small, just set it to 0\n\t\tif (Math.abs(next.x) < 0.01) next.x = 0\n\t\tif (Math.abs(next.y) < 0.01) next.y = 0\n\n\t\tif (!pointerVelocity.equals(next)) {\n\t\t\tthis._pointerVelocity.set(next)\n\t\t}\n\t}\n\n\t/**\n\t * Update the input points from a pointer, pinch, or wheel event.\n\t *\n\t * @param info - The event info.\n\t * @internal\n\t */\n\tupdateFromEvent(info: TLPointerEventInfo | TLPinchEventInfo | TLWheelEventInfo): void {\n\t\tconst currentScreenPoint = this._currentScreenPoint.__unsafe__getWithoutCapture()\n\t\tconst currentPagePoint = this._currentPagePoint.__unsafe__getWithoutCapture()\n\t\tconst isPinching = this._isPinching.__unsafe__getWithoutCapture()\n\t\tconst { screenBounds } = this.editor.store.unsafeGetWithoutCapture(TLINSTANCE_ID)!\n\t\tconst { x: cx, y: cy, z: cz } = unsafe__withoutCapture(() => this.editor.getCamera())\n\n\t\tconst sx = info.point.x - screenBounds.x\n\t\tconst sy = info.point.y - screenBounds.y\n\t\tconst sz = info.point.z ?? 0.5\n\n\t\tthis._previousScreenPoint.set(currentScreenPoint)\n\t\tthis._previousPagePoint.set(currentPagePoint)\n\n\t\t// The \"screen bounds\" is relative to the user's actual screen.\n\t\t// The \"screen point\" is relative to the \"screen bounds\";\n\t\t// it will be 0,0 when its actual screen position is equal\n\t\t// to screenBounds.point. This is confusing!\n\t\tthis._currentScreenPoint.set(new Vec(sx, sy))\n\t\tconst nx = sx / cz - cx\n\t\tconst ny = sy / cz - cy\n\t\tif (isFinite(nx) && isFinite(ny)) {\n\t\t\tthis._currentPagePoint.set(new Vec(nx, ny, sz))\n\t\t}\n\n\t\tthis._isPen.set(info.type === 'pointer' && info.isPen)\n\n\t\t// Reset velocity on pointer down, or when a pinch starts or ends\n\t\tif (info.name === 'pointer_down' || isPinching) {\n\t\t\tthis._pointerVelocity.set(new Vec())\n\t\t\tthis._originScreenPoint.set(this._currentScreenPoint.__unsafe__getWithoutCapture())\n\t\t\tthis._originPagePoint.set(this._currentPagePoint.__unsafe__getWithoutCapture())\n\t\t}\n\n\t\tif (this._getHasCollaborators()) {\n\t\t\tthis.editor.run(\n\t\t\t\t() => {\n\t\t\t\t\tconst pagePoint = this._currentPagePoint.__unsafe__getWithoutCapture()\n\t\t\t\t\tthis.editor.store.put([\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tid: TLPOINTER_ID,\n\t\t\t\t\t\t\ttypeName: 'pointer',\n\t\t\t\t\t\t\tx: pagePoint.x,\n\t\t\t\t\t\t\ty: pagePoint.y,\n\t\t\t\t\t\t\tlastActivityTimestamp:\n\t\t\t\t\t\t\t\t// If our pointer moved only because we're following some other user, then don't\n\t\t\t\t\t\t\t\t// update our last activity timestamp; otherwise, update it to the current timestamp.\n\t\t\t\t\t\t\t\tinfo.type === 'pointer' && info.pointerId === INTERNAL_POINTER_IDS.CAMERA_MOVE\n\t\t\t\t\t\t\t\t\t? (this.editor.store.unsafeGetWithoutCapture(TLPOINTER_ID)\n\t\t\t\t\t\t\t\t\t\t\t?.lastActivityTimestamp ?? Date.now())\n\t\t\t\t\t\t\t\t\t: Date.now(),\n\t\t\t\t\t\t\tmeta: {},\n\t\t\t\t\t\t},\n\t\t\t\t\t])\n\t\t\t\t},\n\t\t\t\t{ history: 'ignore' }\n\t\t\t)\n\t\t}\n\t}\n\n\ttoJson() {\n\t\treturn {\n\t\t\toriginPagePoint: this._originPagePoint.get().toJson(),\n\t\t\toriginScreenPoint: this._originScreenPoint.get().toJson(),\n\t\t\tpreviousPagePoint: this._previousPagePoint.get().toJson(),\n\t\t\tpreviousScreenPoint: this._previousScreenPoint.get().toJson(),\n\t\t\tcurrentPagePoint: this._currentPagePoint.get().toJson(),\n\t\t\tcurrentScreenPoint: this._currentScreenPoint.get().toJson(),\n\t\t\tpointerVelocity: this._pointerVelocity.get().toJson(),\n\t\t\tshiftKey: this._shiftKey.get(),\n\t\t\tmetaKey: this._metaKey.get(),\n\t\t\tctrlKey: this._ctrlKey.get(),\n\t\t\taltKey: this._altKey.get(),\n\t\t\tisPen: this._isPen.get(),\n\t\t\tisDragging: this._isDragging.get(),\n\t\t\tisPointing: this._isPointing.get(),\n\t\t\tisPinching: this._isPinching.get(),\n\t\t\tisEditing: this._isEditing.get(),\n\t\t\tisPanning: this._isPanning.get(),\n\t\t\tisSpacebarPanning: this._isSpacebarPanning.get(),\n\t\t\tkeys: Array.from(this.keys.keys()),\n\t\t\tbuttons: Array.from(this.buttons.keys()),\n\t\t}\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;AAAA,SAAS,MAAM,UAAU,8BAA8B;AACvD,SAAS,eAAe;AACxB,SAAS,eAAe,oBAAoB;AAC5C,SAAS,YAAY;AACrB,SAAS,4BAA4B;AACrC,SAAS,WAAW;AACpB,SAAS,kBAAkB;
|
|
4
|
+
"sourcesContent": ["import { atom, computed, unsafe__withoutCapture } from '@tldraw/state'\nimport { AtomSet } from '@tldraw/store'\nimport { TLINSTANCE_ID, TLPOINTER_ID } from '@tldraw/tlschema'\nimport { bind } from '@tldraw/utils'\nimport { INTERNAL_POINTER_IDS } from '../../../constants'\nimport { Vec } from '../../../primitives/Vec'\nimport { isAccelKey } from '../../../utils/keyboard'\nimport type { Editor } from '../../Editor'\nimport { TLPinchEventInfo, TLPointerEventInfo, TLWheelEventInfo } from '../../types/event-types'\nimport { EditorManager } from '../EditorManager'\n\nconst POINTER_VELOCITY_REFERENCE_INTERVAL_MS = 16\nconst POINTER_VELOCITY_REFERENCE_SMOOTHING = 0.5\n\n/** @public */\nexport class InputsManager extends EditorManager {\n\tconstructor(editor: Editor) {\n\t\tsuper(editor)\n\t\tthis.addEditorEvent('frame', this._onFrame)\n\t}\n\n\t@bind\n\tprivate _onFrame(elapsed: number) {\n\t\tthis.updatePointerVelocity(elapsed)\n\t}\n\n\tprivate _originPagePoint = atom<Vec>('originPagePoint', new Vec())\n\t/**\n\t * The most recent pointer down's position in the current page space.\n\t */\n\tgetOriginPagePoint() {\n\t\treturn this._originPagePoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getOriginPagePoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget originPagePoint() {\n\t\treturn this.getOriginPagePoint()\n\t}\n\n\tprivate _originScreenPoint = atom<Vec>('originScreenPoint', new Vec())\n\t/**\n\t * The most recent pointer down's position in screen space.\n\t */\n\tgetOriginScreenPoint() {\n\t\treturn this._originScreenPoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getOriginScreenPoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget originScreenPoint() {\n\t\treturn this.getOriginScreenPoint()\n\t}\n\n\tprivate _previousPagePoint = atom<Vec>('previousPagePoint', new Vec())\n\t/**\n\t * The previous pointer position in the current page space.\n\t */\n\tgetPreviousPagePoint() {\n\t\treturn this._previousPagePoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getPreviousPagePoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget previousPagePoint() {\n\t\treturn this.getPreviousPagePoint()\n\t}\n\n\tprivate _previousScreenPoint = atom<Vec>('previousScreenPoint', new Vec())\n\t/**\n\t * The previous pointer position in screen space.\n\t */\n\tgetPreviousScreenPoint() {\n\t\treturn this._previousScreenPoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getPreviousScreenPoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget previousScreenPoint() {\n\t\treturn this.getPreviousScreenPoint()\n\t}\n\n\tprivate _currentPagePoint = atom<Vec>('currentPagePoint', new Vec())\n\t/**\n\t * The most recent pointer position in the current page space.\n\t */\n\tgetCurrentPagePoint() {\n\t\treturn this._currentPagePoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getCurrentPagePoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget currentPagePoint() {\n\t\treturn this.getCurrentPagePoint()\n\t}\n\n\tprivate _currentScreenPoint = atom<Vec>('currentScreenPoint', new Vec())\n\t/**\n\t * The most recent pointer position in screen space.\n\t */\n\tgetCurrentScreenPoint() {\n\t\treturn this._currentScreenPoint.get()\n\t}\n\t/**\n\t * @deprecated Use `getCurrentScreenPoint()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget currentScreenPoint() {\n\t\treturn this.getCurrentScreenPoint()\n\t}\n\n\tprivate _pointerVelocity = atom<Vec>('pointerVelocity', new Vec())\n\t/**\n\t * Velocity of mouse pointer, in pixels per millisecond.\n\t */\n\tgetPointerVelocity() {\n\t\treturn this._pointerVelocity.get()\n\t}\n\t/**\n\t * @deprecated Use `getPointerVelocity()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget pointerVelocity() {\n\t\treturn this.getPointerVelocity()\n\t}\n\n\t/**\n\t * Normally you shouldn't need to set the pointer velocity directly. Used in tests to fake pointer velocity.\n\t * @param pointerVelocity - The pointer velocity.\n\t * @internal\n\t */\n\tsetPointerVelocity(pointerVelocity: Vec) {\n\t\tthis._pointerVelocity.set(pointerVelocity)\n\t}\n\n\t/**\n\t * A set containing the currently pressed keys.\n\t */\n\treadonly keys = new AtomSet<string>('keys')\n\n\t/**\n\t * A set containing the currently pressed buttons.\n\t */\n\treadonly buttons = new AtomSet<number>('buttons')\n\n\tprivate _isPen = atom<boolean>('isPen', false)\n\n\t/**\n\t * Whether the input is from a pen.\n\t */\n\tgetIsPen() {\n\t\treturn this._isPen.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPen()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPen() {\n\t\treturn this.getIsPen()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPen(isPen: boolean) {\n\t\tthis.setIsPen(isPen)\n\t}\n\t/**\n\t * @param isPen - Whether the input is from a pen.\n\t */\n\tsetIsPen(isPen: boolean) {\n\t\tthis._isPen.set(isPen)\n\t}\n\n\tprivate _shiftKey = atom<boolean>('shiftKey', false)\n\t/**\n\t * Whether the shift key is currently pressed.\n\t */\n\tgetShiftKey() {\n\t\treturn this._shiftKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getShiftKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget shiftKey() {\n\t\treturn this.getShiftKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset shiftKey(shiftKey: boolean) {\n\t\tthis.setShiftKey(shiftKey)\n\t}\n\t/**\n\t * @param shiftKey - Whether the shift key is pressed.\n\t * @internal\n\t */\n\tsetShiftKey(shiftKey: boolean) {\n\t\tthis._shiftKey.set(shiftKey)\n\t}\n\n\tprivate _metaKey = atom<boolean>('metaKey', false)\n\t/**\n\t * Whether the meta key is currently pressed.\n\t */\n\tgetMetaKey() {\n\t\treturn this._metaKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getMetaKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget metaKey() {\n\t\treturn this.getMetaKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset metaKey(metaKey: boolean) {\n\t\tthis.setMetaKey(metaKey)\n\t}\n\t/**\n\t * @param metaKey - Whether the meta key is pressed.\n\t * @internal\n\t */\n\tsetMetaKey(metaKey: boolean) {\n\t\tthis._metaKey.set(metaKey)\n\t}\n\n\tprivate _ctrlKey = atom<boolean>('ctrlKey', false)\n\t/**\n\t * Whether the ctrl or command key is currently pressed.\n\t */\n\tgetCtrlKey() {\n\t\treturn this._ctrlKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getCtrlKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget ctrlKey() {\n\t\treturn this.getCtrlKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset ctrlKey(ctrlKey: boolean) {\n\t\tthis.setCtrlKey(ctrlKey)\n\t}\n\t/**\n\t * @param ctrlKey - Whether the ctrl key is pressed.\n\t * @internal\n\t */\n\tsetCtrlKey(ctrlKey: boolean) {\n\t\tthis._ctrlKey.set(ctrlKey)\n\t}\n\n\tprivate _altKey = atom<boolean>('altKey', false)\n\t/**\n\t * Whether the alt or option key is currently pressed.\n\t */\n\tgetAltKey() {\n\t\treturn this._altKey.get()\n\t}\n\t/**\n\t * @deprecated Use `getAltKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget altKey() {\n\t\treturn this.getAltKey()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset altKey(altKey: boolean) {\n\t\tthis.setAltKey(altKey)\n\t}\n\t/**\n\t * @param altKey - Whether the alt key is pressed.\n\t * @internal\n\t */\n\tsetAltKey(altKey: boolean) {\n\t\tthis._altKey.set(altKey)\n\t}\n\n\t/**\n\t * Is the accelerator key (cmd on mac, ctrl elsewhere) currently pressed.\n\t */\n\tgetAccelKey() {\n\t\treturn isAccelKey({ metaKey: this.getMetaKey(), ctrlKey: this.getCtrlKey() })\n\t}\n\t/**\n\t * @deprecated Use `getAccelKey()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget accelKey() {\n\t\treturn this.getAccelKey()\n\t}\n\n\tprivate _isDragging = atom<boolean>('isDragging', false)\n\t/**\n\t * Whether the user is dragging.\n\t */\n\tgetIsDragging() {\n\t\treturn this._isDragging.get()\n\t}\n\t/**\n\t * Soon to be deprecated, use `getIsDragging()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isDragging() {\n\t\treturn this.getIsDragging()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isDragging(isDragging: boolean) {\n\t\tthis.setIsDragging(isDragging)\n\t}\n\t/**\n\t * @param isDragging - Whether the user is dragging.\n\t */\n\tsetIsDragging(isDragging: boolean) {\n\t\tthis._isDragging.set(isDragging)\n\t}\n\n\tprivate _isPointing = atom<boolean>('isPointing', false)\n\t/**\n\t * Whether the user is pointing.\n\t */\n\tgetIsPointing() {\n\t\treturn this._isPointing.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPointing()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPointing() {\n\t\treturn this.getIsPointing()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPointing(isPointing: boolean) {\n\t\tthis.setIsPointing(isPointing)\n\t}\n\t/**\n\t * @param isPointing - Whether the user is pointing.\n\t * @internal\n\t */\n\tsetIsPointing(isPointing: boolean) {\n\t\tthis._isPointing.set(isPointing)\n\t}\n\n\tprivate _isRightPointing = atom<boolean>('isRightPointing', false)\n\t/**\n\t * Whether the user is right-click pointing (before drag threshold).\n\t */\n\tgetIsRightPointing() {\n\t\treturn this._isRightPointing.get()\n\t}\n\t/** @internal */\n\tsetIsRightPointing(isRightPointing: boolean) {\n\t\tthis._isRightPointing.set(isRightPointing)\n\t}\n\n\tprivate _isPinching = atom<boolean>('isPinching', false)\n\t/**\n\t * Whether the user is pinching.\n\t */\n\tgetIsPinching() {\n\t\treturn this._isPinching.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPinching()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPinching() {\n\t\treturn this.getIsPinching()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPinching(isPinching: boolean) {\n\t\tthis.setIsPinching(isPinching)\n\t}\n\t/**\n\t * @param isPinching - Whether the user is pinching.\n\t * @internal\n\t */\n\tsetIsPinching(isPinching: boolean) {\n\t\tthis._isPinching.set(isPinching)\n\t}\n\n\tprivate _isEditing = atom<boolean>('isEditing', false)\n\t/**\n\t * Whether the user is editing.\n\t */\n\tgetIsEditing() {\n\t\treturn this._isEditing.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsEditing()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isEditing() {\n\t\treturn this.getIsEditing()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isEditing(isEditing: boolean) {\n\t\tthis.setIsEditing(isEditing)\n\t}\n\t/**\n\t * @param isEditing - Whether the user is editing.\n\t */\n\tsetIsEditing(isEditing: boolean) {\n\t\tthis._isEditing.set(isEditing)\n\t}\n\n\tprivate _isPanning = atom<boolean>('isPanning', false)\n\t/**\n\t * Whether the user is panning.\n\t */\n\tgetIsPanning() {\n\t\treturn this._isPanning.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsPanning()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isPanning() {\n\t\treturn this.getIsPanning()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isPanning(isPanning: boolean) {\n\t\tthis.setIsPanning(isPanning)\n\t}\n\t/**\n\t * @param isPanning - Whether the user is panning.\n\t * @internal\n\t */\n\tsetIsPanning(isPanning: boolean) {\n\t\tthis._isPanning.set(isPanning)\n\t}\n\n\tprivate _isSpacebarPanning = atom<boolean>('isSpacebarPanning', false)\n\t/**\n\t * Whether the user is spacebar panning.\n\t */\n\tgetIsSpacebarPanning() {\n\t\treturn this._isSpacebarPanning.get()\n\t}\n\t/**\n\t * @deprecated Use `getIsSpacebarPanning()` instead.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isSpacebarPanning() {\n\t\treturn this.getIsSpacebarPanning()\n\t}\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tset isSpacebarPanning(isSpacebarPanning: boolean) {\n\t\tthis.setIsSpacebarPanning(isSpacebarPanning)\n\t}\n\t/**\n\t * @param isSpacebarPanning - Whether the user is spacebar panning.\n\t * @internal\n\t */\n\tsetIsSpacebarPanning(isSpacebarPanning: boolean) {\n\t\tthis._isSpacebarPanning.set(isSpacebarPanning)\n\t}\n\n\t@computed private _getHasCollaborators() {\n\t\treturn this.editor.getCollaborators().length > 0 // could we do this more efficiently?\n\t}\n\n\t/**\n\t * The previous point used for velocity calculation (updated each tick, not each pointer event).\n\t * @internal\n\t */\n\tprivate _velocityPrevPoint = new Vec()\n\n\t/**\n\t * Update the pointer velocity based on elapsed time. Called by the tick manager.\n\t * @param elapsed - The time elapsed since the last tick in milliseconds.\n\t * @internal\n\t */\n\tupdatePointerVelocity(elapsed: number) {\n\t\tconst currentScreenPoint = this.getCurrentScreenPoint()\n\t\tconst pointerVelocity = this.getPointerVelocity()\n\n\t\tif (elapsed === 0) return\n\n\t\tconst delta = Vec.Sub(currentScreenPoint, this._velocityPrevPoint)\n\t\tthis._velocityPrevPoint = currentScreenPoint.clone()\n\n\t\tconst length = delta.len()\n\t\tconst direction = length ? delta.div(length) : new Vec(0, 0)\n\n\t\t// Preserve the old 16ms smoothing with alpha = 1 - (1 - 0.5)^(elapsed / 16).\n\t\tconst smoothing =\n\t\t\t1 -\n\t\t\tMath.pow(\n\t\t\t\t1 - POINTER_VELOCITY_REFERENCE_SMOOTHING,\n\t\t\t\telapsed / POINTER_VELOCITY_REFERENCE_INTERVAL_MS\n\t\t\t)\n\t\tconst next = pointerVelocity.clone().lrp(direction.mul(length / elapsed), smoothing)\n\n\t\t// if the velocity is very small, just set it to 0\n\t\tif (Math.abs(next.x) < 0.01) next.x = 0\n\t\tif (Math.abs(next.y) < 0.01) next.y = 0\n\n\t\tif (!pointerVelocity.equals(next)) {\n\t\t\tthis._pointerVelocity.set(next)\n\t\t}\n\t}\n\n\t/**\n\t * Update the input points from a pointer, pinch, or wheel event.\n\t *\n\t * @param info - The event info.\n\t * @internal\n\t */\n\tupdateFromEvent(info: TLPointerEventInfo | TLPinchEventInfo | TLWheelEventInfo): void {\n\t\tconst currentScreenPoint = this._currentScreenPoint.__unsafe__getWithoutCapture()\n\t\tconst currentPagePoint = this._currentPagePoint.__unsafe__getWithoutCapture()\n\t\tconst isPinching = this._isPinching.__unsafe__getWithoutCapture()\n\t\tconst { screenBounds } = this.editor.store.unsafeGetWithoutCapture(TLINSTANCE_ID)!\n\t\tconst { x: cx, y: cy, z: cz } = unsafe__withoutCapture(() => this.editor.getCamera())\n\n\t\tconst sx = info.point.x - screenBounds.x\n\t\tconst sy = info.point.y - screenBounds.y\n\t\tconst sz = info.point.z ?? 0.5\n\n\t\tthis._previousScreenPoint.set(currentScreenPoint)\n\t\tthis._previousPagePoint.set(currentPagePoint)\n\n\t\t// The \"screen bounds\" is relative to the user's actual screen.\n\t\t// The \"screen point\" is relative to the \"screen bounds\";\n\t\t// it will be 0,0 when its actual screen position is equal\n\t\t// to screenBounds.point. This is confusing!\n\t\tthis._currentScreenPoint.set(new Vec(sx, sy))\n\t\tconst nx = sx / cz - cx\n\t\tconst ny = sy / cz - cy\n\t\tif (isFinite(nx) && isFinite(ny)) {\n\t\t\tthis._currentPagePoint.set(new Vec(nx, ny, sz))\n\t\t}\n\n\t\tthis._isPen.set(info.type === 'pointer' && info.isPen)\n\n\t\t// Reset velocity on pointer down, or when a pinch starts or ends\n\t\tif (info.name === 'pointer_down' || isPinching) {\n\t\t\tthis._pointerVelocity.set(new Vec())\n\t\t\tthis._originScreenPoint.set(this._currentScreenPoint.__unsafe__getWithoutCapture())\n\t\t\tthis._originPagePoint.set(this._currentPagePoint.__unsafe__getWithoutCapture())\n\t\t}\n\n\t\tif (this._getHasCollaborators()) {\n\t\t\tthis.editor.run(\n\t\t\t\t() => {\n\t\t\t\t\tconst pagePoint = this._currentPagePoint.__unsafe__getWithoutCapture()\n\t\t\t\t\tthis.editor.store.put([\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tid: TLPOINTER_ID,\n\t\t\t\t\t\t\ttypeName: 'pointer',\n\t\t\t\t\t\t\tx: pagePoint.x,\n\t\t\t\t\t\t\ty: pagePoint.y,\n\t\t\t\t\t\t\tlastActivityTimestamp:\n\t\t\t\t\t\t\t\t// If our pointer moved only because we're following some other user, then don't\n\t\t\t\t\t\t\t\t// update our last activity timestamp; otherwise, update it to the current timestamp.\n\t\t\t\t\t\t\t\tinfo.type === 'pointer' && info.pointerId === INTERNAL_POINTER_IDS.CAMERA_MOVE\n\t\t\t\t\t\t\t\t\t? (this.editor.store.unsafeGetWithoutCapture(TLPOINTER_ID)\n\t\t\t\t\t\t\t\t\t\t\t?.lastActivityTimestamp ?? Date.now())\n\t\t\t\t\t\t\t\t\t: Date.now(),\n\t\t\t\t\t\t\tmeta: {},\n\t\t\t\t\t\t},\n\t\t\t\t\t])\n\t\t\t\t},\n\t\t\t\t{ history: 'ignore' }\n\t\t\t)\n\t\t}\n\t}\n\n\ttoJson() {\n\t\treturn {\n\t\t\toriginPagePoint: this._originPagePoint.get().toJson(),\n\t\t\toriginScreenPoint: this._originScreenPoint.get().toJson(),\n\t\t\tpreviousPagePoint: this._previousPagePoint.get().toJson(),\n\t\t\tpreviousScreenPoint: this._previousScreenPoint.get().toJson(),\n\t\t\tcurrentPagePoint: this._currentPagePoint.get().toJson(),\n\t\t\tcurrentScreenPoint: this._currentScreenPoint.get().toJson(),\n\t\t\tpointerVelocity: this._pointerVelocity.get().toJson(),\n\t\t\tshiftKey: this._shiftKey.get(),\n\t\t\tmetaKey: this._metaKey.get(),\n\t\t\tctrlKey: this._ctrlKey.get(),\n\t\t\taltKey: this._altKey.get(),\n\t\t\tisPen: this._isPen.get(),\n\t\t\tisDragging: this._isDragging.get(),\n\t\t\tisPointing: this._isPointing.get(),\n\t\t\tisPinching: this._isPinching.get(),\n\t\t\tisEditing: this._isEditing.get(),\n\t\t\tisPanning: this._isPanning.get(),\n\t\t\tisSpacebarPanning: this._isSpacebarPanning.get(),\n\t\t\tkeys: Array.from(this.keys.keys()),\n\t\t\tbuttons: Array.from(this.buttons.keys()),\n\t\t}\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAAA,SAAS,MAAM,UAAU,8BAA8B;AACvD,SAAS,eAAe;AACxB,SAAS,eAAe,oBAAoB;AAC5C,SAAS,YAAY;AACrB,SAAS,4BAA4B;AACrC,SA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KAAK,KAAK,KAAK,CAAC;AAAA,MACjC,SAAS,MAAM,KAAK,KAAK,QAAQ,KAAK,CAAC;AAAA,IACxC;AAAA,EACD;AACD;AA7jBS;AAAA,EADP;AAAA,GANW,cAOJ;AAsbU;AAAA,EAAjB;AAAA,GA7bW,cA6bM;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -21,25 +21,6 @@ class RBushIndex {
|
|
|
21
21
|
});
|
|
22
22
|
return new Set(results.map((e) => e.id));
|
|
23
23
|
}
|
|
24
|
-
/**
|
|
25
|
-
* Insert or update a shape in the spatial index.
|
|
26
|
-
* If the shape already exists, it will be removed first to prevent duplicates.
|
|
27
|
-
*/
|
|
28
|
-
upsert(id, bounds) {
|
|
29
|
-
const existing = this.elementsInTree.get(id);
|
|
30
|
-
if (existing) {
|
|
31
|
-
this.rBush.remove(existing);
|
|
32
|
-
}
|
|
33
|
-
const element = {
|
|
34
|
-
minX: bounds.minX,
|
|
35
|
-
minY: bounds.minY,
|
|
36
|
-
maxX: bounds.maxX,
|
|
37
|
-
maxY: bounds.maxY,
|
|
38
|
-
id
|
|
39
|
-
};
|
|
40
|
-
this.rBush.insert(element);
|
|
41
|
-
this.elementsInTree.set(id, element);
|
|
42
|
-
}
|
|
43
24
|
/**
|
|
44
25
|
* Remove a shape from the spatial index.
|
|
45
26
|
*/
|
|
@@ -50,6 +31,24 @@ class RBushIndex {
|
|
|
50
31
|
this.elementsInTree.delete(id);
|
|
51
32
|
}
|
|
52
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Apply the removals and upserts collected during one incremental update.
|
|
36
|
+
* Mutations are applied per item: bulk-rebuilding the whole tree instead
|
|
37
|
+
* was measured slower even with ~10% of the index in the batch.
|
|
38
|
+
*/
|
|
39
|
+
applyBatch(removes, upserts) {
|
|
40
|
+
for (const id of removes) {
|
|
41
|
+
this.remove(id);
|
|
42
|
+
}
|
|
43
|
+
for (const element of upserts) {
|
|
44
|
+
const existing = this.elementsInTree.get(element.id);
|
|
45
|
+
if (existing) {
|
|
46
|
+
this.rBush.remove(existing);
|
|
47
|
+
}
|
|
48
|
+
this.rBush.insert(element);
|
|
49
|
+
this.elementsInTree.set(element.id, element);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
53
52
|
/**
|
|
54
53
|
* Bulk load elements into the spatial index.
|
|
55
54
|
* More efficient than individual inserts for initial loading.
|
|
@@ -88,16 +87,6 @@ class RBushIndex {
|
|
|
88
87
|
getElement(id) {
|
|
89
88
|
return this.elementsInTree.get(id);
|
|
90
89
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Iterate the entries currently in the index. Callers may upsert existing
|
|
93
|
-
* keys or remove keys during iteration; current callers do not insert new
|
|
94
|
-
* keys.
|
|
95
|
-
*
|
|
96
|
-
* @internal
|
|
97
|
-
*/
|
|
98
|
-
entries() {
|
|
99
|
-
return this.elementsInTree.entries();
|
|
100
|
-
}
|
|
101
90
|
/**
|
|
102
91
|
* Dispose of the spatial index.
|
|
103
92
|
* Clears all data structures to prevent memory leaks.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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 *
|
|
5
|
-
"mappings": "AACA,OAAO,WAAW;AAkBlB,MAAM,oBAAoB,MAAsB;AAAC;AAM1C,MAAM,WAAW;AAAA,EACf;AAAA,EACA;AAAA,EAER,cAAc;AACb,SAAK,QAAQ,IAAI,YAAY;AAC7B,SAAK,iBAAiB,oBAAI,IAAI;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,QAA6B;AACnC,UAAM,UAAU,KAAK,MAAM,OAAO;AAAA,MACjC,MAAM,OAAO;AAAA,MACb,MAAM,OAAO;AAAA,MACb,MAAM,OAAO;AAAA,MACb,MAAM,OAAO;AAAA,IACd,CAAC;AACD,WAAO,IAAI,IAAI,QAAQ,IAAI,CAAC,MAAsB,EAAE,EAAE,CAAC;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA
|
|
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 * 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 * Apply the removals and upserts collected during one incremental update.\n\t * Mutations are applied per item: bulk-rebuilding the whole tree instead\n\t * was measured slower even with ~10% of the index in the batch.\n\t */\n\tapplyBatch(removes: Set<TLShapeId>, upserts: SpatialElement[]): void {\n\t\tfor (const id of removes) {\n\t\t\tthis.remove(id)\n\t\t}\n\t\tfor (const element of upserts) {\n\t\t\tconst existing = this.elementsInTree.get(element.id)\n\t\t\tif (existing) {\n\t\t\t\tthis.rBush.remove(existing)\n\t\t\t}\n\t\t\tthis.rBush.insert(element)\n\t\t\tthis.elementsInTree.set(element.id, element)\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 the raw stored element for a shape, without allocating a Box.\n\t * Use when you only need to read the indexed bounds for comparison.\n\t *\n\t * @internal\n\t */\n\tgetElement(id: TLShapeId): SpatialElement | undefined {\n\t\treturn this.elementsInTree.get(id)\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
|
+
"mappings": "AACA,OAAO,WAAW;AAkBlB,MAAM,oBAAoB,MAAsB;AAAC;AAM1C,MAAM,WAAW;AAAA,EACf;AAAA,EACA;AAAA,EAER,cAAc;AACb,SAAK,QAAQ,IAAI,YAAY;AAC7B,SAAK,iBAAiB,oBAAI,IAAI;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,QAA6B;AACnC,UAAM,UAAU,KAAK,MAAM,OAAO;AAAA,MACjC,MAAM,OAAO;AAAA,MACb,MAAM,OAAO;AAAA,MACb,MAAM,OAAO;AAAA,MACb,MAAM,OAAO;AAAA,IACd,CAAC;AACD,WAAO,IAAI,IAAI,QAAQ,IAAI,CAAC,MAAsB,EAAE,EAAE,CAAC;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,IAAqB;AAC3B,UAAM,UAAU,KAAK,eAAe,IAAI,EAAE;AAC1C,QAAI,SAAS;AACZ,WAAK,MAAM,OAAO,OAAO;AACzB,WAAK,eAAe,OAAO,EAAE;AAAA,IAC9B;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAW,SAAyB,SAAiC;AACpE,eAAW,MAAM,SAAS;AACzB,WAAK,OAAO,EAAE;AAAA,IACf;AACA,eAAW,WAAW,SAAS;AAC9B,YAAM,WAAW,KAAK,eAAe,IAAI,QAAQ,EAAE;AACnD,UAAI,UAAU;AACb,aAAK,MAAM,OAAO,QAAQ;AAAA,MAC3B;AACA,WAAK,MAAM,OAAO,OAAO;AACzB,WAAK,eAAe,IAAI,QAAQ,IAAI,OAAO;AAAA,IAC5C;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAS,UAAkC;AAC1C,SAAK,MAAM,KAAK,QAAQ;AACxB,eAAW,WAAW,UAAU;AAC/B,WAAK,eAAe,IAAI,QAAQ,IAAI,OAAO;AAAA,IAC5C;AAAA,EACD;AAAA;AAAA;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;AAAA;AAAA;AAAA,EAQA,WAAW,IAA2C;AACrD,WAAO,KAAK,eAAe,IAAI,EAAE;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAgB;AACf,SAAK,MAAM;AAAA,EACZ;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
RESET_VALUE,
|
|
3
|
+
computed,
|
|
4
|
+
isUninitialized,
|
|
5
|
+
unsafe__withoutCapture
|
|
6
|
+
} from "@tldraw/state";
|
|
7
|
+
import { isBinding, isPageId, isShape } from "@tldraw/tlschema";
|
|
3
8
|
import { objectMapValues } from "@tldraw/utils";
|
|
4
9
|
import { Box } from "../../../primitives/Box.mjs";
|
|
5
10
|
import { RBushIndex } from "./RBushIndex.mjs";
|
|
@@ -26,20 +31,14 @@ class SpatialIndexManager {
|
|
|
26
31
|
const shapeHistory = this.editor.store.query.filterHistory("shape");
|
|
27
32
|
const bindingHistory = this.editor.store.query.filterHistory("binding");
|
|
28
33
|
return computed("spatialIndex", (_prevValue, lastComputedEpoch) => {
|
|
29
|
-
if (isUninitialized(_prevValue)) {
|
|
30
|
-
return this.rebuildAndBumpEpoch();
|
|
31
|
-
}
|
|
32
34
|
const shapeDiff = shapeHistory.getDiffSince(lastComputedEpoch);
|
|
33
35
|
const bindingDiff = bindingHistory.getDiffSince(lastComputedEpoch);
|
|
34
|
-
if (shapeDiff === RESET_VALUE || bindingDiff === RESET_VALUE) {
|
|
35
|
-
return this.rebuildAndBumpEpoch();
|
|
36
|
-
}
|
|
37
36
|
const currentPageId = this.editor.getCurrentPageId();
|
|
38
|
-
if (this.lastPageId !== currentPageId) {
|
|
39
|
-
return this.rebuildAndBumpEpoch();
|
|
37
|
+
if (isUninitialized(_prevValue) || shapeDiff === RESET_VALUE || bindingDiff === RESET_VALUE || this.lastPageId !== currentPageId) {
|
|
38
|
+
return unsafe__withoutCapture(() => this.rebuildAndBumpEpoch());
|
|
40
39
|
}
|
|
41
40
|
if (shapeDiff.length === 0 && bindingDiff.length === 0) return this._boundsEpoch;
|
|
42
|
-
if (this.processIncrementalUpdate(shapeDiff)) {
|
|
41
|
+
if (unsafe__withoutCapture(() => this.processIncrementalUpdate(shapeDiff, bindingDiff))) {
|
|
43
42
|
this._boundsEpoch++;
|
|
44
43
|
}
|
|
45
44
|
return this._boundsEpoch;
|
|
@@ -63,63 +62,111 @@ class SpatialIndexManager {
|
|
|
63
62
|
}
|
|
64
63
|
this.rbush.bulkLoad(elements);
|
|
65
64
|
}
|
|
66
|
-
processIncrementalUpdate(shapeDiff) {
|
|
67
|
-
const
|
|
68
|
-
|
|
65
|
+
processIncrementalUpdate(shapeDiff, bindingDiff) {
|
|
66
|
+
const pendingRemoves = /* @__PURE__ */ new Set();
|
|
67
|
+
const pendingUpserts = [];
|
|
68
|
+
const transformChanged = /* @__PURE__ */ new Set();
|
|
69
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
70
|
+
const queue = [];
|
|
71
|
+
const markDirty = (id) => {
|
|
72
|
+
if (!dirty.has(id)) {
|
|
73
|
+
dirty.add(id);
|
|
74
|
+
queue.push(id);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
69
77
|
for (const changes of shapeDiff) {
|
|
70
78
|
for (const shape of objectMapValues(changes.added)) {
|
|
71
|
-
if (isShape(shape)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.rbush.upsert(shape.id, bounds);
|
|
75
|
-
changed = true;
|
|
76
|
-
}
|
|
77
|
-
processedShapeIds.add(shape.id);
|
|
78
|
-
}
|
|
79
|
+
if (!isShape(shape)) continue;
|
|
80
|
+
transformChanged.add(shape.id);
|
|
81
|
+
markDirty(shape.id);
|
|
79
82
|
}
|
|
80
83
|
for (const shape of objectMapValues(changes.removed)) {
|
|
81
|
-
if (isShape(shape))
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
changed = true;
|
|
85
|
-
}
|
|
86
|
-
processedShapeIds.add(shape.id);
|
|
84
|
+
if (!isShape(shape)) continue;
|
|
85
|
+
if (this.rbush.has(shape.id)) {
|
|
86
|
+
pendingRemoves.add(shape.id);
|
|
87
87
|
}
|
|
88
|
+
if (!isPageId(shape.parentId)) markDirty(shape.parentId);
|
|
88
89
|
}
|
|
89
|
-
for (const [, to] of objectMapValues(changes.updated)) {
|
|
90
|
+
for (const [from, to] of objectMapValues(changes.updated)) {
|
|
90
91
|
if (!isShape(to)) continue;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
92
|
+
transformChanged.add(to.id);
|
|
93
|
+
markDirty(to.id);
|
|
94
|
+
if (isShape(from) && from.parentId !== to.parentId && !isPageId(from.parentId)) {
|
|
95
|
+
markDirty(from.parentId);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
for (const changes of bindingDiff) {
|
|
100
|
+
for (const binding of objectMapValues(changes.added)) {
|
|
101
|
+
if (!isBinding(binding)) continue;
|
|
102
|
+
markDirty(binding.fromId);
|
|
103
|
+
markDirty(binding.toId);
|
|
104
|
+
}
|
|
105
|
+
for (const binding of objectMapValues(changes.removed)) {
|
|
106
|
+
if (!isBinding(binding)) continue;
|
|
107
|
+
markDirty(binding.fromId);
|
|
108
|
+
markDirty(binding.toId);
|
|
109
|
+
}
|
|
110
|
+
for (const [from, to] of objectMapValues(changes.updated)) {
|
|
111
|
+
if (!isBinding(to)) continue;
|
|
112
|
+
markDirty(to.fromId);
|
|
113
|
+
markDirty(to.toId);
|
|
114
|
+
if (isBinding(from) && (from.fromId !== to.fromId || from.toId !== to.toId)) {
|
|
115
|
+
markDirty(from.fromId);
|
|
116
|
+
markDirty(from.toId);
|
|
108
117
|
}
|
|
109
118
|
}
|
|
110
119
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
120
|
+
const transformQueue = [...transformChanged];
|
|
121
|
+
while (transformQueue.length) {
|
|
122
|
+
const id = transformQueue.pop();
|
|
123
|
+
for (const childId of this.editor.getSortedChildIdsForParent(id)) {
|
|
124
|
+
if (!transformChanged.has(childId)) {
|
|
125
|
+
transformChanged.add(childId);
|
|
126
|
+
transformQueue.push(childId);
|
|
127
|
+
}
|
|
128
|
+
markDirty(childId);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
while (queue.length) {
|
|
132
|
+
const id = queue.pop();
|
|
133
|
+
const record = this.editor.store.unsafeGetWithoutCapture(id);
|
|
134
|
+
if (record && isShape(record) && !isPageId(record.parentId)) {
|
|
135
|
+
markDirty(record.parentId);
|
|
136
|
+
}
|
|
137
|
+
for (const binding of this.editor.getBindingsInvolvingShape(id)) {
|
|
138
|
+
markDirty(binding.fromId);
|
|
139
|
+
markDirty(binding.toId);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const id of dirty) {
|
|
143
|
+
const indexedElement = pendingRemoves.has(id) ? void 0 : this.rbush.getElement(id);
|
|
144
|
+
const record = this.editor.store.unsafeGetWithoutCapture(id);
|
|
145
|
+
if (!record || !isShape(record) || this.editor.getAncestorPageId(record) !== this.lastPageId) {
|
|
146
|
+
if (this.rbush.has(id)) {
|
|
147
|
+
pendingRemoves.add(id);
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const bounds = this.editor.getShapePageBounds(id);
|
|
152
|
+
if (bounds && bounds.isValid()) {
|
|
153
|
+
if (!this.areBoundsEqualToSpatialElement(bounds, indexedElement)) {
|
|
154
|
+
pendingRemoves.delete(id);
|
|
155
|
+
pendingUpserts.push({
|
|
156
|
+
minX: bounds.minX,
|
|
157
|
+
minY: bounds.minY,
|
|
158
|
+
maxX: bounds.maxX,
|
|
159
|
+
maxY: bounds.maxY,
|
|
160
|
+
id
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
} else if (this.rbush.has(id)) {
|
|
164
|
+
pendingRemoves.add(id);
|
|
119
165
|
}
|
|
120
|
-
changed = true;
|
|
121
166
|
}
|
|
122
|
-
return
|
|
167
|
+
if (pendingRemoves.size === 0 && pendingUpserts.length === 0) return false;
|
|
168
|
+
this.rbush.applyBatch(pendingRemoves, pendingUpserts);
|
|
169
|
+
return true;
|
|
123
170
|
}
|
|
124
171
|
areBoundsEqualToSpatialElement(a, b) {
|
|
125
172
|
if (!a && !b) return true;
|