{"version":3,"file":"CanvasEditor.mjs","sources":["../../../../../packages/sdk/utils/CanvasEditor.ts"],"sourcesContent":["interface Point {\n  x: number\n  y: number\n}\n\ninterface Shape {\n  draw(ctx: CanvasRenderingContext2D): void\n  containsPoint(point: Point): boolean\n  getNearestEditPoint(mousePosition: Point): Point | null\n}\n\nexport class CanvasEditor {\n  canvas: HTMLCanvasElement\n  ctx: CanvasRenderingContext2D\n  shapes: Shape[]\n  selectedShape: Shape | null\n  mousePosition: Point\n  draggingPoint: Point | null\n  // 新增属性\n  private leafletMap: L.Map | undefined\n\n  constructor(canvas: HTMLCanvasElement, leafletMap?: L.Map) {\n    this.canvas = canvas\n    this.ctx = this.canvas.getContext('2d')!\n    this.shapes = []\n    this.selectedShape = null\n    this.mousePosition = { x: 0, y: 0 }\n    this.draggingPoint = null\n    this.leafletMap = leafletMap\n\n    canvas.addEventListener('mousedown', this.handleMouseDown.bind(this))\n    canvas.addEventListener('mousemove', this.handleMouseMove.bind(this))\n    canvas.addEventListener('mouseup', this.handleMouseUp.bind(this))\n  }\n\n  // 新增方法\n  disableLeafletDrag() {\n    if (this.leafletMap) {\n      this.leafletMap.dragging.disable()\n    }\n  }\n\n  enableLeafletDrag() {\n    if (this.leafletMap) {\n      this.leafletMap.dragging.enable()\n    }\n  }\n\n  addShape(shape: Shape) {\n    this.shapes.push(shape)\n    this.redraw()\n  }\n\n  removeShape(shape: Shape) {\n    const index = this.shapes.indexOf(shape)\n    if (index !== -1) {\n      this.shapes.splice(index, 1)\n      this.redraw()\n    }\n  }\n\n  redraw() {\n    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)\n    this.shapes.forEach((shape) => shape.draw(this.ctx))\n  }\n\n  private handleMouseDown(e: MouseEvent) {\n    const rect = this.canvas.getBoundingClientRect()\n    this.mousePosition = { x: e.clientX - rect.left, y: e.clientY - rect.top }\n\n    for (const shape of this.shapes) {\n      if (shape.containsPoint(this.mousePosition)) {\n        this.selectedShape = shape\n        break\n      }\n    }\n\n    if (this.selectedShape) {\n      this.disableLeafletDrag() // 禁用地图拖拽\n      const selectedPoint = this.selectedShape.getNearestEditPoint(\n        this.mousePosition\n      )\n      if (selectedPoint) {\n        this.draggingPoint = selectedPoint\n        this.canvas.style.cursor = 'move'\n      } else {\n        this.canvas.style.cursor = 'grab'\n      }\n    } else {\n      this.canvas.style.cursor = 'default'\n    }\n  }\n\n  private handleMouseMove(e: MouseEvent) {\n    if (this.draggingPoint) {\n      const rect = this.canvas.getBoundingClientRect()\n      const newX = e.clientX - rect.left\n      const newY = e.clientY - rect.top\n      this.draggingPoint.x = newX\n      this.draggingPoint.y = newY\n      this.redraw()\n    }\n  }\n\n  private handleMouseUp() {\n    this.draggingPoint = null\n    this.canvas.style.cursor = 'default'\n    if (this.leafletMap) {\n      this.enableLeafletDrag() // 恢复地图拖拽\n    }\n  }\n}\n\nexport class EditableRectangle implements Shape {\n  points: Point[]\n\n  constructor(x: number, y: number, width: number, height: number) {\n    this.points = [\n      { x, y },\n      { x: x + width, y },\n      { x: x + width, y: y + height },\n      { x, y: y + height },\n    ]\n  }\n\n  draw(ctx: CanvasRenderingContext2D) {\n    ctx.beginPath()\n    ctx.moveTo(this.points[0].x, this.points[0].y)\n    for (let i = 1; i < this.points.length; i++) {\n      ctx.lineTo(this.points[i].x, this.points[i].y)\n    }\n    ctx.closePath()\n    ctx.stroke()\n\n    this.points.forEach((point) => {\n      ctx.beginPath()\n      ctx.arc(point.x, point.y, 5, 0, Math.PI * 2)\n      ctx.fill()\n    })\n  }\n\n  containsPoint(point: Point): boolean {\n    const minX = Math.min(...this.points.map((p) => p.x))\n    const maxX = Math.max(...this.points.map((p) => p.x))\n    const minY = Math.min(...this.points.map((p) => p.y))\n    const maxY = Math.max(...this.points.map((p) => p.y))\n\n    return (\n      point.x >= minX && point.x <= maxX && point.y >= minY && point.y <= maxY\n    )\n  }\n\n  getNearestEditPoint(mousePosition: Point): Point | null {\n    let nearestPoint: Point | null = null\n    let minDistance = Number.POSITIVE_INFINITY\n    for (const point of this.points) {\n      const distance = Math.hypot(\n        mousePosition.x - point.x,\n        mousePosition.y - point.y\n      )\n      if (distance < minDistance) {\n        minDistance = distance\n        nearestPoint = point\n      }\n    }\n    return nearestPoint\n  }\n}\n"],"names":["CanvasEditor","constructor","t","e","this","canvas","ctx","getContext","shapes","selectedShape","mousePosition","x","y","draggingPoint","leafletMap","addEventListener","handleMouseDown","bind","handleMouseMove","handleMouseUp","disableLeafletDrag","dragging","disable","enableLeafletDrag","enable","addShape","push","redraw","removeShape","indexOf","splice","clearRect","width","height","forEach","draw","getBoundingClientRect","clientX","left","clientY","top","s","containsPoint","getNearestEditPoint","style","cursor","i","EditableRectangle","points","beginPath","moveTo","length","lineTo","closePath","stroke","arc","Math","PI","fill","min","map","n","max","a","Number","POSITIVE_INFINITY","hypot"],"mappings":"AAAO,MAAMA,EAAa,WAAAC,CAAYC,EAAEC,GAAGC,KAAKC,OAAOH,EAAEE,KAAKE,IAAIF,KAAKC,OAAOE,WAAW,MAAMH,KAAKI,OAAO,GAAGJ,KAAKK,cAAc,KAAKL,KAAKM,cAAc,CAACC,EAAE,EAAEC,EAAE,GAAGR,KAAKS,cAAc,KAAKT,KAAKU,WAAWX,EAAED,EAAEa,iBAAiB,YAAYX,KAAKY,gBAAgBC,KAAKb,OAAOF,EAAEa,iBAAiB,YAAYX,KAAKc,gBAAgBD,KAAKb,OAAOF,EAAEa,iBAAiB,UAAUX,KAAKe,cAAcF,KAAKb,MAAM,CAAC,kBAAAgB,GAAqBhB,KAAKU,YAAYV,KAAKU,WAAWO,SAASC,SAAS,CAAC,iBAAAC,GAAoBnB,KAAKU,YAAYV,KAAKU,WAAWO,SAASG,QAAQ,CAAC,QAAAC,CAASvB,GAAGE,KAAKI,OAAOkB,KAAKxB,GAAGE,KAAKuB,QAAQ,CAAC,WAAAC,CAAY1B,GAAG,MAAMC,EAAEC,KAAKI,OAAOqB,QAAQ3B,IAAQ,IAALC,IAASC,KAAKI,OAAOsB,OAAO3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