{ "version": 3, "sources": ["../../../../src/lib/tools/SelectTool/DragAndDropManager.ts"], "sourcesContent": ["import { Editor, TLShape, TLShapeId, Vec2d, compact } from '@tldraw/editor'\n\nconst LAG_DURATION = 100\n\n/** @public */\nexport class DragAndDropManager {\n\tconstructor(public editor: Editor) {\n\t\teditor.disposables.add(this.dispose)\n\t}\n\n\tprevDroppingShapeId: TLShapeId | null = null\n\n\tdroppingNodeTimer: ReturnType | null = null\n\n\tfirst = true\n\n\tupdateDroppingNode(movingShapes: TLShape[], cb: () => void) {\n\t\tif (this.first) {\n\t\t\tthis.prevDroppingShapeId =\n\t\t\t\tthis.editor.getDroppingOverShape(this.editor.inputs.originPagePoint, movingShapes)?.id ??\n\t\t\t\tnull\n\t\t\tthis.first = false\n\t\t}\n\n\t\tif (this.droppingNodeTimer === null) {\n\t\t\tthis.setDragTimer(movingShapes, LAG_DURATION * 10, cb)\n\t\t} else if (this.editor.inputs.pointerVelocity.len() > 0.5) {\n\t\t\tclearInterval(this.droppingNodeTimer)\n\t\t\tthis.setDragTimer(movingShapes, LAG_DURATION, cb)\n\t\t}\n\t}\n\n\tprivate setDragTimer(movingShapes: TLShape[], duration: number, cb: () => void) {\n\t\tthis.droppingNodeTimer = setTimeout(() => {\n\t\t\tthis.editor.batch(() => {\n\t\t\t\tthis.handleDrag(this.editor.inputs.currentPagePoint, movingShapes, cb)\n\t\t\t})\n\t\t\tthis.droppingNodeTimer = null\n\t\t}, duration)\n\t}\n\n\tprivate handleDrag(point: Vec2d, movingShapes: TLShape[], cb?: () => void) {\n\t\tmovingShapes = compact(movingShapes.map((shape) => this.editor.getShape(shape.id)))\n\n\t\tconst nextDroppingShapeId = this.editor.getDroppingOverShape(point, movingShapes)?.id ?? null\n\n\t\t// is the next dropping shape id different than the last one?\n\t\tif (nextDroppingShapeId === this.prevDroppingShapeId) {\n\t\t\treturn\n\t\t}\n\n\t\t// the old previous one\n\t\tconst { prevDroppingShapeId } = this\n\n\t\tconst prevDroppingShape = prevDroppingShapeId && this.editor.getShape(prevDroppingShapeId)\n\t\tconst nextDroppingShape = nextDroppingShapeId && this.editor.getShape(nextDroppingShapeId)\n\n\t\t// Even if we don't have a next dropping shape id (i.e. if we're dropping\n\t\t// onto the page) set the prev to the current, to avoid repeat calls to\n\t\t// the previous parent's onDragShapesOut\n\n\t\tif (prevDroppingShape) {\n\t\t\tthis.editor.getShapeUtil(prevDroppingShape).onDragShapesOut?.(prevDroppingShape, movingShapes)\n\t\t}\n\n\t\tif (nextDroppingShape) {\n\t\t\tconst res = this.editor\n\t\t\t\t.getShapeUtil(nextDroppingShape)\n\t\t\t\t.onDragShapesOver?.(nextDroppingShape, movingShapes)\n\n\t\t\tif (res && res.shouldHint) {\n\t\t\t\tthis.editor.setHintingShapes([nextDroppingShape.id])\n\t\t\t}\n\t\t} else {\n\t\t\t// If we're dropping onto the page, then clear hinting ids\n\t\t\tthis.editor.setHintingShapes([])\n\t\t}\n\n\t\tcb?.()\n\n\t\t// next -> curr\n\t\tthis.prevDroppingShapeId = nextDroppingShapeId\n\t}\n\n\tdropShapes(shapes: TLShape[]) {\n\t\tconst { prevDroppingShapeId } = this\n\n\t\tthis.handleDrag(this.editor.inputs.currentPagePoint, shapes)\n\n\t\tif (prevDroppingShapeId) {\n\t\t\tconst shape = this.editor.getShape(prevDroppingShapeId)\n\t\t\tif (!shape) return\n\t\t\tthis.editor.getShapeUtil(shape).onDropShapesOver?.(shape, shapes)\n\t\t}\n\t}\n\n\tclear() {\n\t\tthis.prevDroppingShapeId = null\n\n\t\tif (this.droppingNodeTimer !== null) {\n\t\t\tclearInterval(this.droppingNodeTimer)\n\t\t}\n\n\t\tthis.droppingNodeTimer = null\n\t\tthis.editor.setHintingShapes([])\n\t\tthis.first = true\n\t}\n\n\tdispose = () => {\n\t\tthis.clear()\n\t}\n}\n"], "mappings": "AAAA,SAA4C,eAAe;AAE3D,MAAM,eAAe;AAGd,MAAM,mBAAmB;AAAA,EAC/B,YAAmB,QAAgB;AAAhB;AA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