{
  "version": 3,
  "sources": ["../../../../src/lib/editor/overlays/OverlayManager.ts"],
  "sourcesContent": ["import { atom, computed } from '@tldraw/state'\nimport { Geometry2d } from '../../primitives/geometry/Geometry2d'\nimport { VecLike } from '../../primitives/Vec'\nimport type { Editor } from '../Editor'\nimport { OverlayUtil, TLOverlay } from './OverlayUtil'\n\n/**\n * An active overlay util paired with the overlays it produced for the current\n * editor state. Returned by {@link OverlayManager.getActiveOverlayEntries} so\n * hit-test, render, and debug paths share a single scan per reactive tick.\n *\n * @public\n */\nexport interface TLOverlayEntry {\n\tutil: OverlayUtil\n\toverlays: TLOverlay[]\n}\n\n/** @public */\nexport class OverlayManager {\n\tconstructor(public readonly editor: Editor) {}\n\n\t/** @internal */\n\treadonly _overlayUtils = new Map<string, OverlayUtil>()\n\n\t/**\n\t * Register an overlay util instance. Called during editor construction.\n\t * @internal\n\t */\n\tregisterUtil(util: OverlayUtil) {\n\t\tconst type = (util.constructor as typeof OverlayUtil).type\n\t\tif (!type) {\n\t\t\tthrow new Error(`Overlay util ${util.constructor.name} is missing a static 'type' property.`)\n\t\t}\n\t\tif (this._overlayUtils.has(type)) {\n\t\t\tthrow new Error(`Duplicate overlay util type: \"${type}\"`)\n\t\t}\n\t\tthis._overlayUtils.set(type, util)\n\t}\n\n\t/**\n\t * Get an overlay util by type string, overlay instance, or by passing\n\t * a util class as a generic parameter for type-safe lookup.\n\t *\n\t * @example\n\t * ```ts\n\t * const util = editor.overlays.getOverlayUtil('brush')\n\t * const util = editor.overlays.getOverlayUtil<BrushOverlayUtil>('brush')\n\t * const util = editor.overlays.getOverlayUtil(myOverlay)\n\t * ```\n\t *\n\t * @public\n\t */\n\tgetOverlayUtil<T extends OverlayUtil>(\n\t\ttype: T extends OverlayUtil<infer O> ? O['type'] : string\n\t): T\n\tgetOverlayUtil<O extends TLOverlay>(overlay: O): OverlayUtil<O>\n\tgetOverlayUtil(arg: string | TLOverlay): OverlayUtil {\n\t\tconst type = typeof arg === 'string' ? arg : arg.type\n\t\tconst util = this._overlayUtils.get(type)\n\t\tif (!util) throw new Error(`No overlay util found for type: \"${type}\"`)\n\t\treturn util\n\t}\n\n\t/**\n\t * Returns all registered overlay utils in paint order (ascending zIndex).\n\t * Utils with the same zIndex preserve their registration order.\n\t *\n\t * @public\n\t */\n\t@computed getOverlayUtilsInZOrder(): OverlayUtil[] {\n\t\tconst utils = Array.from(this._overlayUtils.values())\n\t\t// Stable sort by zIndex (registration order breaks ties).\n\t\treturn utils\n\t\t\t.map((util, i) => ({ util, i, z: util.options.zIndex ?? 0 }))\n\t\t\t.sort((a, b) => a.z - b.z || a.i - b.i)\n\t\t\t.map((entry) => entry.util)\n\t}\n\n\t/**\n\t * Reactive list of active overlay utils paired with the overlays they\n\t * produced for the current editor state, in paint order (ascending\n\t * zIndex). Both the hit-test and render paths read from this single\n\t * cached scan instead of each re-deriving the active set. Active utils\n\t * are included even when their `getOverlays()` returns an empty array,\n\t * since `render()` may still draw non-interactive UI (e.g. the selection\n\t * bounding box during brushing).\n\t *\n\t * @public\n\t */\n\t@computed getActiveOverlayEntries(): TLOverlayEntry[] {\n\t\tconst entries: TLOverlayEntry[] = []\n\t\tfor (const util of this.getOverlayUtilsInZOrder()) {\n\t\t\tif (!util.isActive()) continue\n\t\t\tentries.push({ util, overlays: util.getOverlays() })\n\t\t}\n\t\treturn entries\n\t}\n\n\t/**\n\t * Reactively computed list of all currently active overlays, in paint order.\n\t * @public\n\t */\n\t@computed getCurrentOverlays(): TLOverlay[] {\n\t\tconst all: TLOverlay[] = []\n\t\tfor (const { overlays } of this.getActiveOverlayEntries()) {\n\t\t\tall.push(...overlays)\n\t\t}\n\t\treturn all\n\t}\n\n\t// Hit-test geometry cache keyed by overlay identity. Entries remain valid\n\t// while getActiveOverlayEntries() keeps returning the same overlay\n\t// instances; when its reactive deps change, getOverlays() emits fresh\n\t// objects and stale entries fall out by GC.\n\tprivate _geometryCache = new WeakMap<TLOverlay, Geometry2d | null>()\n\n\t/**\n\t * Get hit-test geometry for an overlay, cached by overlay identity. Lets\n\t * hit-testing on a pointermove storm skip the per-overlay geometry\n\t * allocation that {@link OverlayUtil.getGeometry} would otherwise do on\n\t * every call.\n\t *\n\t * @public\n\t */\n\tgetOverlayGeometry(overlay: TLOverlay): Geometry2d | null {\n\t\tconst cached = this._geometryCache.get(overlay)\n\t\tif (cached !== undefined) return cached\n\t\tconst util = this.getOverlayUtil(overlay)\n\t\tconst geometry = util.getGeometry(overlay)\n\t\tthis._geometryCache.set(overlay, geometry)\n\t\treturn geometry\n\t}\n\n\t/**\n\t * The currently hovered overlay id.\n\t * @public\n\t */\n\tprivate _hoveredOverlayId = atom<string | null>('hoveredOverlayId', null)\n\n\tgetHoveredOverlayId(): string | null {\n\t\treturn this._hoveredOverlayId.get()\n\t}\n\n\tgetHoveredOverlay(): TLOverlay | null {\n\t\tconst id = this._hoveredOverlayId.get()\n\t\tif (!id) return null\n\t\treturn this.getCurrentOverlays().find((o) => o.id === id) ?? null\n\t}\n\n\tsetHoveredOverlay(id: string | null) {\n\t\tif (id === this._hoveredOverlayId.get()) return\n\t\tthis._hoveredOverlayId.set(id)\n\t}\n\n\t/**\n\t * Hit test all active overlays at a given page point.\n\t * Returns the topmost overlay whose geometry contains the point, or null.\n\t * Utils are walked from highest zIndex to lowest so the overlay painted on\n\t * top also wins the hit test. Within a util, overlays are walked in\n\t * array order: the first overlay whose geometry contains the point wins,\n\t * so utils should place highest-priority overlays first in `getOverlays`.\n\t * Interactive overlays (those with geometry) are checked; non-interactive are skipped.\n\t *\n\t * @param point - Point in page coordinates\n\t * @param margin - Hit test margin\n\t * @public\n\t */\n\tgetOverlayAtPoint(point: VecLike, margin = 0): TLOverlay | null {\n\t\tconst entries = this.getActiveOverlayEntries()\n\t\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\t\tconst { overlays } = entries[i]\n\t\t\tfor (const overlay of overlays) {\n\t\t\t\tconst geometry = this.getOverlayGeometry(overlay)\n\t\t\t\tif (!geometry) continue\n\t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