diff --git a/src/components/fabric/QPolygon.js b/src/components/fabric/QPolygon.js index 8e450e77..f5e7eaae 100644 --- a/src/components/fabric/QPolygon.js +++ b/src/components/fabric/QPolygon.js @@ -1,8 +1,15 @@ import { fabric } from 'fabric' import { v4 as uuidv4 } from 'uuid' import { QLine } from '@/components/fabric/QLine' -import { distanceBetweenPoints, findTopTwoIndexesByDistance, getDirectionByPoint, sortedPointLessEightPoint, sortedPoints } from '@/util/canvas-util' -import { calculateAngle, drawRidgeRoof, drawShedRoof, inPolygon, toGeoJSON } from '@/util/qpolygon-utils' +import { + distanceBetweenPoints, + findTopTwoIndexesByDistance, + getAllRelatedObjects, + getDirectionByPoint, + sortedPointLessEightPoint, + sortedPoints, +} from '@/util/canvas-util' +import { calculateAngle, drawRidgeRoof, inPolygon, toGeoJSON } from '@/util/qpolygon-utils' import * as turf from '@turf/turf' import { LINE_TYPE } from '@/common/common' @@ -131,7 +138,7 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, { }) this.on('removed', () => { - const children = this.canvas.getObjects().filter((obj) => obj.parentId === this.id) + const children = getAllRelatedObjects(this.id, this.canvas) children.forEach((child) => { this.canvas.remove(child) }) @@ -169,6 +176,7 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, { offset: 0, }, parent: this, + parentId: this.id, direction: getDirectionByPoint(point, nextPoint), idx: i + 1, }) diff --git a/src/util/canvas-util.js b/src/util/canvas-util.js index 1a78f991..a78beb15 100644 --- a/src/util/canvas-util.js +++ b/src/util/canvas-util.js @@ -925,3 +925,32 @@ export function checkLineOrientation(line) { return 'diagonal' // 대각선 } } + +// 최상위 parentId를 통해 모든 하위 객체를 찾는 함수 +export const getAllRelatedObjects = (id, canvas) => { + const result = [] + const map = new Map() + + // Create a map of objects by their id + canvas.getObjects().forEach((obj) => { + map.set(obj.id, obj) + }) + + // Helper function to recursively find all related objects + function findRelatedObjects(id) { + const obj = map.get(id) + if (obj) { + result.push(obj) + canvas.getObjects().forEach((o) => { + if (o.parentId === id) { + findRelatedObjects(o.id) + } + }) + } + } + + // Start the search with the given parentId + findRelatedObjects(id) + + return result +}