{ "version": 3, "sources": ["../../../../../src/lib/tools/EraserTool/children/Erasing.ts"], "sourcesContent": ["import {\n\tHIT_TEST_MARGIN,\n\tStateNode,\n\tTLEventHandlers,\n\tTLFrameShape,\n\tTLGroupShape,\n\tTLPointerEventInfo,\n\tTLScribble,\n\tTLShapeId,\n\tpointInPolygon,\n} from '@tldraw/editor'\nimport { ScribbleManager } from '../../../shapes/shared/ScribbleManager'\n\nexport class Erasing extends StateNode {\n\tstatic override id = 'erasing'\n\n\tprivate info = {} as TLPointerEventInfo\n\tprivate scribble = {} as ScribbleManager\n\tprivate markId = ''\n\tprivate excludedShapeIds = new Set()\n\n\toverride onEnter = (info: TLPointerEventInfo) => {\n\t\tthis.markId = 'erase scribble begin'\n\t\tthis.editor.mark(this.markId)\n\t\tthis.info = info\n\n\t\tconst { originPagePoint } = this.editor.inputs\n\t\tthis.excludedShapeIds = new Set(\n\t\t\tthis.editor.currentPageShapes\n\t\t\t\t.filter((shape) => {\n\t\t\t\t\t//If the shape is locked, we shouldn't erase it\n\t\t\t\t\tif (this.editor.isShapeOrAncestorLocked(shape)) return true\n\t\t\t\t\t//If the shape is a group or frame, check we're inside it when we start erasing\n\t\t\t\t\tif (\n\t\t\t\t\t\tthis.editor.isShapeOfType(shape, 'group') ||\n\t\t\t\t\t\tthis.editor.isShapeOfType(shape, 'frame')\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst pointInShapeShape = this.editor.getPointInShapeSpace(shape, originPagePoint)\n\t\t\t\t\t\tconst geometry = this.editor.getShapeGeometry(shape)\n\t\t\t\t\t\treturn geometry.bounds.containsPoint(pointInShapeShape)\n\t\t\t\t\t}\n\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\t.map((shape) => shape.id)\n\t\t)\n\n\t\tthis.startScribble()\n\t\tthis.update()\n\t}\n\n\tprivate startScribble = () => {\n\t\tif (this.scribble.tick) {\n\t\t\tthis.editor.off('tick', this.scribble?.tick)\n\t\t}\n\n\t\tthis.scribble = new ScribbleManager({\n\t\t\tonUpdate: this.onScribbleUpdate,\n\t\t\tonComplete: this.onScribbleComplete,\n\t\t\tcolor: 'muted-1',\n\t\t\tsize: 12,\n\t\t})\n\n\t\tthis.editor.on('tick', this.scribble.tick)\n\t}\n\n\tprivate pushPointToScribble = () => {\n\t\tconst { x, y } = this.editor.inputs.currentPagePoint\n\t\tthis.scribble.addPoint(x, y)\n\t}\n\n\tprivate onScribbleUpdate = (scribble: TLScribble) => {\n\t\tthis.editor.updateInstanceState({ scribble })\n\t}\n\n\tprivate onScribbleComplete = () => {\n\t\tthis.editor.off('tick', this.scribble.tick)\n\t\tthis.editor.updateInstanceState({ scribble: null })\n\t}\n\n\toverride onExit = () => {\n\t\tthis.scribble.stop()\n\t}\n\n\toverride onPointerMove = () => {\n\t\tthis.update()\n\t}\n\n\toverride onPointerUp: TLEventHandlers['onPointerUp'] = () => {\n\t\tthis.complete()\n\t}\n\n\toverride onCancel: TLEventHandlers['onCancel'] = () => {\n\t\tthis.cancel()\n\t}\n\n\toverride onComplete: TLEventHandlers['onComplete'] = () => {\n\t\tthis.complete()\n\t}\n\n\tupdate() {\n\t\tconst {\n\t\t\tzoomLevel,\n\t\t\tcurrentPageShapes: currentPageShapes,\n\t\t\terasingShapeIds,\n\t\t\tinputs: { currentPagePoint, previousPagePoint },\n\t\t} = this.editor\n\n\t\tconst { excludedShapeIds } = this\n\n\t\tthis.pushPointToScribble()\n\n\t\tconst erasing = new Set(erasingShapeIds)\n\n\t\tfor (const shape of currentPageShapes) {\n\t\t\tif (this.editor.isShapeOfType(shape, 'group')) continue\n\n\t\t\t// Avoid testing masked shapes, unless the pointer is inside the mask\n\t\t\tconst pageMask = this.editor.getShapeMask(shape.id)\n\t\t\tif (pageMask && !pointInPolygon(currentPagePoint, pageMask)) {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// Hit test the shape using a line segment\n\t\t\tconst geometry = this.editor.getShapeGeometry(shape)\n\t\t\tconst A = this.editor.getPointInShapeSpace(shape, previousPagePoint)\n\t\t\tconst B = this.editor.getPointInShapeSpace(shape, currentPagePoint)\n\n\t\t\tif (geometry.hitTestLineSegment(A, B, HIT_TEST_MARGIN / zoomLevel)) {\n\t\t\t\terasing.add(this.editor.getOutermostSelectableShape(shape).id)\n\t\t\t}\n\t\t}\n\n\t\t// Remove the hit shapes, except if they're in the list of excluded shapes\n\t\t// (these excluded shapes will be any frames or groups the pointer was inside of\n\t\t// when the user started erasing)\n\t\tthis.editor.setErasingShapes([...erasing].filter((id) => !excludedShapeIds.has(id)))\n\t}\n\n\tcomplete() {\n\t\tthis.editor.deleteShapes(this.editor.currentPageState.erasingShapeIds)\n\t\tthis.editor.setErasingShapes([])\n\t\tthis.parent.transition('idle', {})\n\t}\n\n\tcancel() {\n\t\tthis.editor.setErasingShapes([])\n\t\tthis.editor.bailToMark(this.markId)\n\t\tthis.parent.transition('idle', this.info)\n\t}\n}\n"], "mappings": "AAAA;AAAA,EACC;AAAA,EACA;AAAA,EAOA;AAAA,OACM;AACP,SAAS,uBAAuB;AAEzB,MAAM,gBAAgB,UAAU;AAAA,EACtC,OAAgB,KAAK;AAAA,EAEb,OAAO,CAAC;AAAA,EACR,WAAW,CAAC;AAAA,EACZ,SAAS;AAAA,EACT,mBAAmB,oBAAI,IAAe;AAAA,EAErC,UAAU,CAAC,SAA6B;AAChD,SAAK,SAAS;AACd,SAAK,OAAO,KAAK,KAAK,MAAM;AAC5B,SAAK,OAAO;AAEZ,UAAM,EAAE,gBAAgB,IAAI,KAAK,OAAO;AACxC,SAAK,mBAAmB,IAAI;AAAA,MAC3B,KAAK,OAAO,kBACV,OAAO,CAAC,UAAU;AAElB,YAAI,KAAK,OAAO,wBAAwB,KAAK;AAAG,iBAAO;AAEvD,YACC,KAAK,OAAO,cAA4B,OAAO,OAAO,KACtD,KAAK,OAAO,cAA4B,OAAO,OAAO,GACrD;AACD,gBAAM,oBAAoB,KAAK,OAAO,qBAAqB,OAAO,eAAe;AACjF,gBAAM,WAAW,KA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